diff --git a/.travis.yml b/.travis.yml index 069068d..3f24e0e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,10 +4,10 @@ before_install: - sudo add-apt-repository ppa:fontforge/fontforge --yes - sudo add-apt-repository ppa:coolwanglu/pdf2htmlex --yes - sudo apt-get update -qq - - sudo apt-get install -qq libpoppler-dev libpoppler-private-dev libspiro-dev libcairo-dev libpango1.0-dev libfreetype6-dev libltdl-dev libfontforge-dev python-imaging ttfautohint -install: - - wget 'http://downloads.sourceforge.net/project/wkhtmltopdf/0.12.1/wkhtmltox-0.12.1_linux-precise-amd64.deb' -O wkhtmltox.deb - - sudo dpkg -i wkhtmltox.deb + - sudo apt-get install -qq libpoppler-dev libpoppler-private-dev libspiro-dev libcairo-dev libpango1.0-dev libfreetype6-dev libltdl-dev libfontforge-dev python-imaging ttfautohint firefox xvfb + - export DISPLAY=:99.0 + - test/start_xvfb.sh + - sleep 5 before_script: - cmake -DENABLE_SVG=ON . script: diff --git a/CMakeLists.txt b/CMakeLists.txt index f17578b..b1f5413 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -84,10 +84,20 @@ else() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x") endif() -include(CheckCXXCompilerFlag) -check_cxx_compiler_flag("${CMAKE_CXX_FLAGS}" CXX0X_SUPPORT) +# check the C++11 features we need +include(CheckCXXSourceCompiles) +check_cxx_source_compiles(" +#include +int main() +{ + char * ptr = nullptr; + std::vector v; + auto f = [&](){ for(auto & i : v) ++i; }; + f(); +} +" CXX0X_SUPPORT) if(NOT CXX0X_SUPPORT) - message(FATAL_ERROR "Error: your compiler does not support C++0x, please update it.") + message(FATAL_ERROR "Error: your compiler does not support C++0x/C++11, please update it.") endif() diff --git a/test/.gitattributes b/test/.gitattributes new file mode 100644 index 0000000..d72fd52 --- /dev/null +++ b/test/.gitattributes @@ -0,0 +1 @@ +*.pdf binary diff --git a/test/README.md b/test/README.md index 9b7bc7b..c8c2dde 100644 --- a/test/README.md +++ b/test/README.md @@ -1,14 +1,18 @@ ### Dependencies -- wkhtmltoimage -- python2 -- Python Imaging Library +- python2 and packages + - Python Imaging Library + - Selenium + - unittest +- Firefox ### Usage - Run all tests: - `./test.py` -- Run selected tests: - - `./test.py test_A test_B ...` +- Run selected test suites: + - `./test.py test_local_browser` +- Run selected test case: + - `./test.py test_basic_text - Environment variables: - set `P2H_TEST_SAVE_TMP=1` to keep the temporary files - set `P2H_TEST_GEN=1` to generate new reference images instead of comparing with old ones @@ -21,6 +25,5 @@ - One page only, unless the test case is about multiple pages. - Grayscale only, unless the test case is about colors. - Remove unnecessary elements. - - Set proper parameters for cropping in `wkhtml2image_args`. - [Optional] Include the source files that the PDF file is generated from. diff --git a/test/start_xvfb.sh b/test/start_xvfb.sh new file mode 100755 index 0000000..9664154 --- /dev/null +++ b/test/start_xvfb.sh @@ -0,0 +1,2 @@ +#!/bin/sh +/sbin/start-stop-daemon --start --quiet --pidfile /tmp/custom_xvfb_99.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :99 -ac -screen 0 1280x1920x16 diff --git a/test/test.py b/test/test.py index 85db25e..dc60464 100755 --- a/test/test.py +++ b/test/test.py @@ -14,6 +14,8 @@ class Common(object): PDF2HTMLEX_PATH = os.path.join(SRC_DIR, 'pdf2htmlEX') SAVE_TMP = os.environ.get('P2H_TEST_SAVE_TMP') + GENERATING_MODE = os.environ.get('P2H_TEST_GEN') + CANONICAL_TEMPDIR = '/tmp/pdf2htmlEX_test' def setUp(self): @@ -83,14 +85,23 @@ if __name__ == '__main__': exit(1) suites = [] loader = unittest.TestLoader() - test_names = list(map(lambda x: 'T.'+x, sys.argv[1:])) - for module_name in ['test_naming', 'test_conversion']: + all_modules = ['test_output', 'test_local_browser'] + test_names = [] + for name in sys.argv[1:]: + if name in all_modules or name.find('.') != -1: + test_names.append(name) + else: + for m in all_modules: + test_names.append(m + '.' + name) + + for module_name in all_modules: __import__(module_name) if len(test_names) > 0: - try: - suites.append(loader.loadTestsFromNames(test_names, sys.modules[module_name])) - except: - pass + for n in test_names: + try: + suites.append(loader.loadTestsFromName(n, sys.modules[module_name])) + except: + pass else: suites.append(loader.loadTestsFromModule(sys.modules[module_name])) diff --git a/test/test_conversion/basic_text.png b/test/test_conversion/basic_text.png deleted file mode 100644 index 9451e11..0000000 Binary files a/test/test_conversion/basic_text.png and /dev/null differ diff --git a/test/test_conversion/geneve_1564.png b/test/test_conversion/geneve_1564.png deleted file mode 100644 index c768327..0000000 Binary files a/test/test_conversion/geneve_1564.png and /dev/null differ diff --git a/test/test_conversion/text_visibility.png b/test/test_conversion/text_visibility.png deleted file mode 100644 index 1456678..0000000 Binary files a/test/test_conversion/text_visibility.png and /dev/null differ diff --git a/test/test_conversion.py b/test/test_local_browser.py similarity index 60% rename from test/test_conversion.py rename to test/test_local_browser.py index ab2b70c..980fa8c 100755 --- a/test/test_conversion.py +++ b/test/test_local_browser.py @@ -3,38 +3,42 @@ import unittest import os import subprocess +import shutil from PIL import Image, ImageChops +from selenium import webdriver from test import Common -class T(Common, unittest.TestCase): - GENERATING_MODE = os.environ.get('P2H_TEST_GEN') - - WKHTML2IMAGE = 'wkhtmltoimage' +class test_local_browser(Common, unittest.TestCase): TTFAUTOHINT = 'ttfautohint' - TEST_DATA_DIR = os.path.join(Common.TEST_DIR, 'test_conversion') + TEST_DATA_DIR = os.path.join(Common.TEST_DIR, 'test_local_browser') DEFAULT_PDF2HTMLEX_ARGS = [ '--external-hint-tool', 'ttfautohint', '--fit-width', 800, '--last-page', 1, '--correct-text-visibility', 1, + '--embed', 'fi', # avoid base64 to make it faster ] - DEFAULT_WKHTML2IMAGE_ARGS = [ - '-f', 'png', - '--height', 600, - '--width', 800, - '--quality', 0, - '--quiet' - ] + BROWSER_WIDTH=800 + BROWSER_HEIGHT=1200 @classmethod def setUpClass(cls): - subprocess.check_call([cls.WKHTML2IMAGE, '--version']) - subprocess.check_call([cls.TTFAUTOHINT, '--version']) + exit_code = subprocess.call([cls.TTFAUTOHINT, '--version']) + assert (exit_code == 0), 'Cannot execute ' + cls.TTFAUTOHINT + cls.browser = webdriver.Firefox() + cls.browser.maximize_window() + size = cls.browser.get_window_size() + assert ((size['width'] >= cls.BROWSER_WIDTH) and (size['height'] >= cls.BROWSER_HEIGHT)), 'Screen is not large enough' + cls.browser.set_window_size(cls.BROWSER_WIDTH, cls.BROWSER_HEIGHT) - def run_test_case(self, filename, pdf2htmlEX_args=[], wkhtml2image_args=[]): + @classmethod + def tearDownClass(cls): + cls.browser.quit() + + def run_test_case(self, filename, pdf2htmlEX_args=[]): basefilename, extension = os.path.splitext(filename) htmlfilename = basefilename + '.html' pngfilename = basefilename + '.png' @@ -56,21 +60,14 @@ class T(Common, unittest.TestCase): pngfilename_out_fullpath = os.path.join(png_out_dir, pngfilename) pngfilename_raw_fullpath = os.path.join(self.TEST_DATA_DIR, pngfilename) - wkhtml2image_args = [self.WKHTML2IMAGE] \ - + self.DEFAULT_WKHTML2IMAGE_ARGS \ - + list(wkhtml2image_args) + [ - os.path.join(self.cur_output_dir, htmlfilename), - pngfilename_out_fullpath - ] + self.generate_image(os.path.join(self.cur_output_dir, htmlfilename), pngfilename_out_fullpath) - return_code = subprocess.call(list(map(str, wkhtml2image_args))) - self.assertEquals(return_code, 0, 'cannot execute ' + self.WKHTML2IMAGE) if self.GENERATING_MODE: shutil.copy(pngfilename_out_fullpath, pngfilename_raw_fullpath) else: - original_img = Image.open(pngfilename_raw_fullpath) new_img = Image.open(pngfilename_out_fullpath) + original_img = Image.open(pngfilename_raw_fullpath) diff_img = ImageChops.difference(original_img, new_img); @@ -81,18 +78,16 @@ class T(Common, unittest.TestCase): diff_img.convert('RGB').save(os.path.join(png_out_dir, basefilename + '.diff.png')) self.fail('PNG files differ') + def generate_image(self, html_file, png_file): + self.browser.get('file://' + html_file) + self.browser.save_screenshot(png_file) + def test_basic_text(self): - self.run_test_case('basic_text.pdf', - wkhtml2image_args=[ - '--crop-x', 180, - '--crop-y', 150, - '--crop-w', 220, - '--crop-h', 260 - ]) + self.run_test_case('basic_text.pdf') def test_geneve_1564(self): - self.run_test_case('geneve_1564.pdf', wkhtml2image_args=['--height', 1100]) + self.run_test_case('geneve_1564.pdf') def test_text_visibility(self): - self.run_test_case('text_visibility.pdf', wkhtml2image_args=['--height', 1200]) + self.run_test_case('text_visibility.pdf') diff --git a/test/test_conversion/basic_text.pdf b/test/test_local_browser/basic_text.pdf similarity index 100% rename from test/test_conversion/basic_text.pdf rename to test/test_local_browser/basic_text.pdf diff --git a/test/test_local_browser/basic_text.png b/test/test_local_browser/basic_text.png new file mode 100644 index 0000000..1297ea3 Binary files /dev/null and b/test/test_local_browser/basic_text.png differ diff --git a/test/test_conversion/basic_text.tex b/test/test_local_browser/basic_text.tex similarity index 100% rename from test/test_conversion/basic_text.tex rename to test/test_local_browser/basic_text.tex diff --git a/test/test_conversion/geneve_1564.pdf b/test/test_local_browser/geneve_1564.pdf similarity index 100% rename from test/test_conversion/geneve_1564.pdf rename to test/test_local_browser/geneve_1564.pdf diff --git a/test/test_local_browser/geneve_1564.png b/test/test_local_browser/geneve_1564.png new file mode 100644 index 0000000..50d457c Binary files /dev/null and b/test/test_local_browser/geneve_1564.png differ diff --git a/test/test_conversion/text_visibility.pdf b/test/test_local_browser/text_visibility.pdf similarity index 100% rename from test/test_conversion/text_visibility.pdf rename to test/test_local_browser/text_visibility.pdf diff --git a/test/test_local_browser/text_visibility.png b/test/test_local_browser/text_visibility.png new file mode 100644 index 0000000..d6dee22 Binary files /dev/null and b/test/test_local_browser/text_visibility.png differ diff --git a/test/test_naming.py b/test/test_output.py similarity index 95% rename from test/test_naming.py rename to test/test_output.py index add17f8..741cd82 100644 --- a/test/test_naming.py +++ b/test/test_output.py @@ -1,16 +1,18 @@ #!/usr/bin/env python -# Test --split-page and --page-filename +# Test output files import unittest import os from test import Common -class T(Common, unittest.TestCase): +class test_output(Common, unittest.TestCase): def run_test_case(self, input_file, expected_output_files, args=[]): + if self.GENERATING_MODE: + self.skipTest("Skipping test_output test cases in generating mode") args = list(args) - args.insert(0, os.path.join(self.TEST_DIR, 'test_naming', input_file)) + args.insert(0, os.path.join(self.TEST_DIR, 'test_output', input_file)) self.assertItemsEqual(self.run_pdf2htmlEX(args)['output_files'], expected_output_files) def test_generate_single_html_default_name_single_page_pdf(self): diff --git a/test/test_naming/1-page.pdf b/test/test_output/1-page.pdf similarity index 100% rename from test/test_naming/1-page.pdf rename to test/test_output/1-page.pdf diff --git a/test/test_naming/2-pages.pdf b/test/test_output/2-pages.pdf similarity index 100% rename from test/test_naming/2-pages.pdf rename to test/test_output/2-pages.pdf diff --git a/test/test_naming/3-pages.pdf b/test/test_output/3-pages.pdf similarity index 100% rename from test/test_naming/3-pages.pdf rename to test/test_output/3-pages.pdf