1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-12-22 04:50:09 +00:00

check more c++11 features

use local browser to test
This commit is contained in:
Lu Wang 2014-11-17 22:53:42 +08:00
parent a9ab233f2b
commit abb278c3e2
21 changed files with 79 additions and 55 deletions

View File

@ -4,10 +4,10 @@ before_install:
- sudo add-apt-repository ppa:fontforge/fontforge --yes - sudo add-apt-repository ppa:fontforge/fontforge --yes
- sudo add-apt-repository ppa:coolwanglu/pdf2htmlex --yes - sudo add-apt-repository ppa:coolwanglu/pdf2htmlex --yes
- sudo apt-get update -qq - 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 - 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
install: - export DISPLAY=:99.0
- wget 'http://downloads.sourceforge.net/project/wkhtmltopdf/0.12.1/wkhtmltox-0.12.1_linux-precise-amd64.deb' -O wkhtmltox.deb - test/start_xvfb.sh
- sudo dpkg -i wkhtmltox.deb - sleep 5
before_script: before_script:
- cmake -DENABLE_SVG=ON . - cmake -DENABLE_SVG=ON .
script: script:

View File

@ -84,10 +84,20 @@ else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
endif() endif()
include(CheckCXXCompilerFlag) # check the C++11 features we need
check_cxx_compiler_flag("${CMAKE_CXX_FLAGS}" CXX0X_SUPPORT) include(CheckCXXSourceCompiles)
check_cxx_source_compiles("
#include <vector>
int main()
{
char * ptr = nullptr;
std::vector<int> v;
auto f = [&](){ for(auto & i : v) ++i; };
f();
}
" CXX0X_SUPPORT)
if(NOT 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() endif()

1
test/.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
*.pdf binary

View File

@ -1,14 +1,18 @@
### Dependencies ### Dependencies
- wkhtmltoimage - python2 and packages
- python2 - Python Imaging Library
- Python Imaging Library - Selenium
- unittest
- Firefox
### Usage ### Usage
- Run all tests: - Run all tests:
- `./test.py` - `./test.py`
- Run selected tests: - Run selected test suites:
- `./test.py test_A test_B ...` - `./test.py test_local_browser`
- Run selected test case:
- `./test.py test_basic_text
- Environment variables: - Environment variables:
- set `P2H_TEST_SAVE_TMP=1` to keep the temporary files - 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 - 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. - One page only, unless the test case is about multiple pages.
- Grayscale only, unless the test case is about colors. - Grayscale only, unless the test case is about colors.
- Remove unnecessary elements. - Remove unnecessary elements.
- Set proper parameters for cropping in `wkhtml2image_args`.
- [Optional] Include the source files that the PDF file is generated from. - [Optional] Include the source files that the PDF file is generated from.

2
test/start_xvfb.sh Executable file
View File

@ -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

View File

@ -14,6 +14,8 @@ class Common(object):
PDF2HTMLEX_PATH = os.path.join(SRC_DIR, 'pdf2htmlEX') PDF2HTMLEX_PATH = os.path.join(SRC_DIR, 'pdf2htmlEX')
SAVE_TMP = os.environ.get('P2H_TEST_SAVE_TMP') SAVE_TMP = os.environ.get('P2H_TEST_SAVE_TMP')
GENERATING_MODE = os.environ.get('P2H_TEST_GEN')
CANONICAL_TEMPDIR = '/tmp/pdf2htmlEX_test' CANONICAL_TEMPDIR = '/tmp/pdf2htmlEX_test'
def setUp(self): def setUp(self):
@ -83,14 +85,23 @@ if __name__ == '__main__':
exit(1) exit(1)
suites = [] suites = []
loader = unittest.TestLoader() loader = unittest.TestLoader()
test_names = list(map(lambda x: 'T.'+x, sys.argv[1:])) all_modules = ['test_output', 'test_local_browser']
for module_name in ['test_naming', 'test_conversion']: 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) __import__(module_name)
if len(test_names) > 0: if len(test_names) > 0:
try: for n in test_names:
suites.append(loader.loadTestsFromNames(test_names, sys.modules[module_name])) try:
except: suites.append(loader.loadTestsFromName(n, sys.modules[module_name]))
pass except:
pass
else: else:
suites.append(loader.loadTestsFromModule(sys.modules[module_name])) suites.append(loader.loadTestsFromModule(sys.modules[module_name]))

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.4 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 127 KiB

View File

@ -3,38 +3,42 @@
import unittest import unittest
import os import os
import subprocess import subprocess
import shutil
from PIL import Image, ImageChops from PIL import Image, ImageChops
from selenium import webdriver
from test import Common from test import Common
class T(Common, unittest.TestCase): class test_local_browser(Common, unittest.TestCase):
GENERATING_MODE = os.environ.get('P2H_TEST_GEN')
WKHTML2IMAGE = 'wkhtmltoimage'
TTFAUTOHINT = 'ttfautohint' 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 = [ DEFAULT_PDF2HTMLEX_ARGS = [
'--external-hint-tool', 'ttfautohint', '--external-hint-tool', 'ttfautohint',
'--fit-width', 800, '--fit-width', 800,
'--last-page', 1, '--last-page', 1,
'--correct-text-visibility', 1, '--correct-text-visibility', 1,
'--embed', 'fi', # avoid base64 to make it faster
] ]
DEFAULT_WKHTML2IMAGE_ARGS = [ BROWSER_WIDTH=800
'-f', 'png', BROWSER_HEIGHT=1200
'--height', 600,
'--width', 800,
'--quality', 0,
'--quiet'
]
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
subprocess.check_call([cls.WKHTML2IMAGE, '--version']) exit_code = subprocess.call([cls.TTFAUTOHINT, '--version'])
subprocess.check_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) basefilename, extension = os.path.splitext(filename)
htmlfilename = basefilename + '.html' htmlfilename = basefilename + '.html'
pngfilename = basefilename + '.png' pngfilename = basefilename + '.png'
@ -56,21 +60,14 @@ class T(Common, unittest.TestCase):
pngfilename_out_fullpath = os.path.join(png_out_dir, pngfilename) pngfilename_out_fullpath = os.path.join(png_out_dir, pngfilename)
pngfilename_raw_fullpath = os.path.join(self.TEST_DATA_DIR, pngfilename) pngfilename_raw_fullpath = os.path.join(self.TEST_DATA_DIR, pngfilename)
wkhtml2image_args = [self.WKHTML2IMAGE] \ self.generate_image(os.path.join(self.cur_output_dir, htmlfilename), pngfilename_out_fullpath)
+ self.DEFAULT_WKHTML2IMAGE_ARGS \
+ list(wkhtml2image_args) + [
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: if self.GENERATING_MODE:
shutil.copy(pngfilename_out_fullpath, pngfilename_raw_fullpath) shutil.copy(pngfilename_out_fullpath, pngfilename_raw_fullpath)
else: else:
original_img = Image.open(pngfilename_raw_fullpath)
new_img = Image.open(pngfilename_out_fullpath) new_img = Image.open(pngfilename_out_fullpath)
original_img = Image.open(pngfilename_raw_fullpath)
diff_img = ImageChops.difference(original_img, new_img); 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')) diff_img.convert('RGB').save(os.path.join(png_out_dir, basefilename + '.diff.png'))
self.fail('PNG files differ') 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): def test_basic_text(self):
self.run_test_case('basic_text.pdf', self.run_test_case('basic_text.pdf')
wkhtml2image_args=[
'--crop-x', 180,
'--crop-y', 150,
'--crop-w', 220,
'--crop-h', 260
])
def test_geneve_1564(self): 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): def test_text_visibility(self):
self.run_test_case('text_visibility.pdf', wkhtml2image_args=['--height', 1200]) self.run_test_case('text_visibility.pdf')

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 369 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 108 KiB

View File

@ -1,16 +1,18 @@
#!/usr/bin/env python #!/usr/bin/env python
# Test --split-page and --page-filename # Test output files
import unittest import unittest
import os import os
from test import Common 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=[]): 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 = 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) self.assertItemsEqual(self.run_pdf2htmlEX(args)['output_files'], expected_output_files)
def test_generate_single_html_default_name_single_page_pdf(self): def test_generate_single_html_default_name_single_page_pdf(self):