Skip to content

Commit b7d2249

Browse files
[Test] Use selenium to render and compare converted HTMLs against reference files
1 parent ee94685 commit b7d2249

14 files changed

Lines changed: 110 additions & 7 deletions

File tree

.github/workflows/build.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ jobs:
126126
uses: actions/setup-python@v5
127127
with:
128128
python-version: 3.12
129-
- name: install python dependencies
130-
run: pip install --upgrade pip conan
129+
- run: pip install --upgrade pip conan adbPullAs selenium
131130

132131
- run: |
133132
conan config install .github/conan
@@ -159,6 +158,8 @@ jobs:
159158
adb pull /data/local/tmp/beforeTests.png testResults/screenshots/
160159
161160
./gradlew connectedCheck || touch sorry_but_tests_are_failing
161+
adbPullAs app.opendocument.android.pdf2htmlex.test /data/data/app.opendocument.android.pdf2htmlex.test/cache/pdf2htmlEX/output-htmls testResults || true
162+
162163
adb pull /sdcard/Pictures/screenshots testResults/ || true
163164
164165
adb shell screencap /data/local/tmp/afterTests.png
@@ -169,6 +170,8 @@ jobs:
169170
170171
test ! -f sorry_but_tests_are_failing
171172
173+
- run: python ci-scripts/browser_tests.py --html-dir testResults/output-htmls --png-destination-dir testResults/pngs --reference-png-dir test/reference_pngs
174+
172175
- uses: actions/upload-artifact@v4
173176
if: always()
174177
with:

ci-scripts/browser_tests.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env python3
2+
# A lot of test logic taken from
3+
# https://github.com/pdf2htmlEX/pdf2htmlEX/blob/v0.18.8.rc1/pdf2htmlEX/test/browser_tests.py
4+
5+
import argparse
6+
import os
7+
import sys
8+
import time
9+
10+
from PIL import Image, ImageChops
11+
from selenium import webdriver
12+
from selenium.webdriver.common.by import By
13+
from selenium.webdriver.support import expected_conditions
14+
from selenium.webdriver.support.ui import WebDriverWait
15+
16+
17+
def main():
18+
parser = argparse.ArgumentParser(description="Render test HTMLs into PNGs and compare against reference PNGs")
19+
parser.add_argument("--html-dir", action="store", required=True,
20+
help="Directory with converted HTMLs")
21+
parser.add_argument("--png-destination-dir", action="store", required=True,
22+
help="Directory where to save rendered PNGs")
23+
parser.add_argument("--reference-png-dir", action="store", required=True,
24+
help="Directory with reference PNGs")
25+
args = parser.parse_args()
26+
del parser
27+
28+
tests = ["basic_text", "fontfile3_opentype", "geneve_1564", "invalid_unicode_issue477", "issue_83", "pdf", "sample", "svg_background_with_page_rotation_issue402", "test_fail", "text_visibility", "with_form"]
29+
30+
test_result = True
31+
32+
html_dir = os.path.abspath(args.html_dir)
33+
reference_png_dir = os.path.abspath(args.reference_png_dir)
34+
png_destination_dir = os.path.abspath(args.png_destination_dir)
35+
if not os.path.exists(png_destination_dir):
36+
os.mkdir(png_destination_dir, 0o755)
37+
38+
os.environ["MOZ_HEADLESS"] = "1"
39+
for test in tests:
40+
html_file = os.path.join(html_dir, f"{test}/{test}.html")
41+
png_file = os.path.join(png_destination_dir, f"{test}.png")
42+
reference_png_file = os.path.join(reference_png_dir, f"{test}.png")
43+
diff_png_file = os.path.join(png_destination_dir, f"{test}-diff.png")
44+
45+
if not os.path.exists(html_file):
46+
test_result = False
47+
print(f" FAILURE: {test} html file not found!")
48+
continue
49+
50+
browser = webdriver.Firefox()
51+
browser.set_window_size(600, 800)
52+
try:
53+
browser.get('file://' + html_file)
54+
WebDriverWait(browser, 5) \
55+
.until(expected_conditions.presence_of_element_located((By.ID, 'page-container')))
56+
finally:
57+
time.sleep(1)
58+
browser.get_full_page_screenshot_as_file(png_file)
59+
browser.quit()
60+
61+
if not os.path.exists(reference_png_file):
62+
print(f' IGNORED: {test} reference png file not found')
63+
continue
64+
65+
out_img = Image.open(png_file).convert('RGB')
66+
ref_img = Image.open(reference_png_file).convert('RGB')
67+
68+
diff_img = ImageChops.difference(ref_img, out_img)
69+
diff_img.convert('RGB').save(diff_png_file)
70+
71+
diff_bbox = diff_img.getbbox()
72+
if test == "test_fail":
73+
if diff_bbox is None:
74+
test_result = False
75+
print(f" FAILURE: {test} should fail, but it did not. "
76+
f"Test system is potentially broken.")
77+
else:
78+
print(f" SUCCESS: {test}")
79+
elif diff_bbox is None:
80+
print(f" SUCCESS: {test}")
81+
else:
82+
test_result = False
83+
print(f" FAILURE: {test} diff bounding box: {diff_bbox} should be None")
84+
diff_size = (diff_bbox[2] - diff_bbox[0]) * (diff_bbox[3] - diff_bbox[1])
85+
img_size = ref_img.size[0] * ref_img.size[1]
86+
print(f'PNG file {png_file} and {reference_png_file} differ by at'
87+
f'most {diff_size} pixels, ({100.0*diff_size/img_size} of {img_size}'
88+
f'pixels in total), difference: {diff_png_file}', file=sys.stderr)
89+
90+
sys.exit(0 if test_result else 1)
91+
92+
93+
if __name__ == "__main__":
94+
main()

pdf2htmlEX/src/androidTest/java/com/viliussutkus89/android/pdf2htmlex/ConversionTests.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,21 @@ public static List<File> listPDFs() throws IOException {
8484
@Test
8585
public void convertPDF() throws pdf2htmlEX.ConversionFailedException, IOException {
8686
Context ctx = InstrumentationRegistry.getInstrumentation().getTargetContext();
87-
pdf2htmlEX converter = new pdf2htmlEX(ctx);
87+
pdf2htmlEX converter = new pdf2htmlEX(ctx).setProcessOutline(false);
88+
89+
switch(pdfFile.getName()) {
90+
case "with_form.pdf":
91+
converter.setProcessForm(true);
92+
break;
93+
case "svg_background_with_page_rotation_issue402.pdf":
94+
converter.setBackgroundImageFormat(pdf2htmlEX.BackgroundImageFormat.SVG);
95+
break;
96+
}
8897

8998
File htmlFile = converter.convert(pdfFile);
99+
converter.close();
90100

91101
assertTrue("Converted HTML file not found! " + pdfFile.getName(), htmlFile.exists());
92102
assertTrue("Converted HTML file empty! " + pdfFile.getName(), htmlFile.length() > 0);
93-
94-
htmlFile.delete();
95-
96-
converter.close();
97103
}
98104
}

test/reference_pngs/basic_text.png

19.7 KB
Loading
293 KB
Loading
177 KB
Loading
77.3 KB
Loading

test/reference_pngs/issue_83.png

61.6 KB
Loading

test/reference_pngs/pdf.png

114 KB
Loading

test/reference_pngs/sample.png

30.1 KB
Loading

0 commit comments

Comments
 (0)