2014-11-16 05:40:02 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import os
|
|
|
|
import subprocess
|
2014-11-17 14:53:42 +00:00
|
|
|
import shutil
|
2014-11-16 05:40:02 +00:00
|
|
|
|
|
|
|
from PIL import Image, ImageChops
|
2014-11-17 14:53:42 +00:00
|
|
|
from selenium import webdriver
|
2014-11-16 05:40:02 +00:00
|
|
|
from test import Common
|
|
|
|
|
2014-11-17 14:53:42 +00:00
|
|
|
class test_local_browser(Common, unittest.TestCase):
|
2014-11-16 05:40:02 +00:00
|
|
|
TTFAUTOHINT = 'ttfautohint'
|
2014-11-17 14:53:42 +00:00
|
|
|
TEST_DATA_DIR = os.path.join(Common.TEST_DIR, 'test_local_browser')
|
2014-11-16 05:40:02 +00:00
|
|
|
|
|
|
|
DEFAULT_PDF2HTMLEX_ARGS = [
|
|
|
|
'--external-hint-tool', 'ttfautohint',
|
|
|
|
'--fit-width', 800,
|
|
|
|
'--last-page', 1,
|
|
|
|
'--correct-text-visibility', 1,
|
2014-11-17 14:53:42 +00:00
|
|
|
'--embed', 'fi', # avoid base64 to make it faster
|
2014-11-16 05:40:02 +00:00
|
|
|
]
|
|
|
|
|
2014-11-17 14:53:42 +00:00
|
|
|
BROWSER_WIDTH=800
|
|
|
|
BROWSER_HEIGHT=1200
|
2014-11-16 05:40:02 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2014-11-17 14:53:42 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
cls.browser.quit()
|
2014-11-16 05:40:02 +00:00
|
|
|
|
2014-11-17 14:53:42 +00:00
|
|
|
def run_test_case(self, filename, pdf2htmlEX_args=[]):
|
2014-11-16 05:40:02 +00:00
|
|
|
basefilename, extension = os.path.splitext(filename)
|
|
|
|
htmlfilename = basefilename + '.html'
|
|
|
|
pngfilename = basefilename + '.png'
|
|
|
|
|
|
|
|
self.assertEquals(extension.lower(), '.pdf', 'Input file is not PDF')
|
|
|
|
|
|
|
|
pdf2htmlEX_args = self.DEFAULT_PDF2HTMLEX_ARGS \
|
|
|
|
+ list(pdf2htmlEX_args) + [
|
|
|
|
os.path.join(self.TEST_DATA_DIR, filename),
|
|
|
|
htmlfilename
|
|
|
|
]
|
|
|
|
|
|
|
|
result = self.run_pdf2htmlEX(pdf2htmlEX_args)
|
|
|
|
self.assertIn(htmlfilename, result['output_files'], 'HTML file is not generated')
|
|
|
|
|
|
|
|
png_out_dir = os.path.join(self.cur_temp_dir, 'png_out')
|
|
|
|
os.mkdir(png_out_dir)
|
|
|
|
|
|
|
|
pngfilename_out_fullpath = os.path.join(png_out_dir, pngfilename)
|
|
|
|
pngfilename_raw_fullpath = os.path.join(self.TEST_DATA_DIR, pngfilename)
|
|
|
|
|
2014-11-17 14:53:42 +00:00
|
|
|
self.generate_image(os.path.join(self.cur_output_dir, htmlfilename), pngfilename_out_fullpath)
|
2014-11-16 05:40:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
if self.GENERATING_MODE:
|
|
|
|
shutil.copy(pngfilename_out_fullpath, pngfilename_raw_fullpath)
|
|
|
|
else:
|
|
|
|
new_img = Image.open(pngfilename_out_fullpath)
|
2014-11-17 14:53:42 +00:00
|
|
|
original_img = Image.open(pngfilename_raw_fullpath)
|
2014-11-16 05:40:02 +00:00
|
|
|
|
|
|
|
diff_img = ImageChops.difference(original_img, new_img);
|
|
|
|
|
|
|
|
if diff_img.getbbox() is not None:
|
|
|
|
if self.SAVE_TMP:
|
|
|
|
# save the diff image
|
|
|
|
# http://stackoverflow.com/questions/15721484/saving-in-png-using-pil-library-after-taking-imagechops-difference-of-two-png
|
|
|
|
diff_img.convert('RGB').save(os.path.join(png_out_dir, basefilename + '.diff.png'))
|
|
|
|
self.fail('PNG files differ')
|
|
|
|
|
2014-11-17 14:53:42 +00:00
|
|
|
def generate_image(self, html_file, png_file):
|
|
|
|
self.browser.get('file://' + html_file)
|
|
|
|
self.browser.save_screenshot(png_file)
|
|
|
|
|
2014-11-16 05:40:02 +00:00
|
|
|
def test_basic_text(self):
|
2014-11-17 14:53:42 +00:00
|
|
|
self.run_test_case('basic_text.pdf')
|
2014-11-16 05:40:02 +00:00
|
|
|
|
|
|
|
def test_geneve_1564(self):
|
2014-11-17 14:53:42 +00:00
|
|
|
self.run_test_case('geneve_1564.pdf')
|
2014-11-16 05:40:02 +00:00
|
|
|
|
|
|
|
def test_text_visibility(self):
|
2014-11-17 14:53:42 +00:00
|
|
|
self.run_test_case('text_visibility.pdf')
|
2014-11-16 05:40:02 +00:00
|
|
|
|