OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2015 The PDFium Authors. All rights reserved. | 2 # Copyright 2015 The PDFium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import optparse | 6 import optparse |
7 import os | 7 import os |
8 import re | 8 import re |
| 9 import shutil |
9 import subprocess | 10 import subprocess |
10 import sys | 11 import sys |
11 | 12 |
| 13 import common |
| 14 import pngdiffer |
| 15 import suppressor |
| 16 |
12 # Nomenclature: | 17 # Nomenclature: |
13 # x_root - "x" | 18 # x_root - "x" |
14 # x_filename - "x.ext" | 19 # x_filename - "x.ext" |
15 # x_path - "path/to/a/b/c/x.ext" | 20 # x_path - "path/to/a/b/c/x.ext" |
16 # c_dir - "path/to/a/b/c" | 21 # c_dir - "path/to/a/b/c" |
17 | 22 |
18 def test_one_file(input_filename, source_dir, working_dir, | 23 def test_one_file(input_filename, source_dir, working_dir, |
19 pdfium_test_path, pdfium_diff_path): | 24 pdfium_test_path, image_differ): |
20 input_root, _ = os.path.splitext(input_filename) | |
21 input_path = os.path.join(source_dir, input_filename) | 25 input_path = os.path.join(source_dir, input_filename) |
22 pdf_path = os.path.join(working_dir, input_filename) | 26 pdf_path = os.path.join(working_dir, input_filename) |
23 actual_path_template = os.path.join(working_dir, input_root + '.pdf.%d.png') | |
24 expected_path_template = os.path.join(source_dir, | |
25 input_root + '_expected.pdf.%d.png') | |
26 try: | 27 try: |
27 subprocess.check_call(['cp', input_path, pdf_path]) | 28 shutil.copyfile(input_path, pdf_path) |
| 29 sys.stdout.flush() |
28 subprocess.check_call([pdfium_test_path, '--png', pdf_path]) | 30 subprocess.check_call([pdfium_test_path, '--png', pdf_path]) |
29 i = 0; | |
30 while True: | |
31 expected_path = expected_path_template % i; | |
32 actual_path = actual_path_template % i; | |
33 if not os.path.exists(expected_path): | |
34 if i == 0: | |
35 print "WARNING: no expected results files found for " + input_filename | |
36 break | |
37 print "Checking " + actual_path | |
38 subprocess.check_call([pdfium_diff_path, expected_path, actual_path]) | |
39 i += 1 | |
40 except subprocess.CalledProcessError as e: | 31 except subprocess.CalledProcessError as e: |
41 print "FAILURE: " + input_filename + "; " + str(e) | 32 print "FAILURE: " + input_filename + "; " + str(e) |
42 return False | 33 return False |
| 34 if image_differ.HasDifferences(input_filename, source_dir, working_dir): |
| 35 return False |
43 return True | 36 return True |
44 | 37 |
45 def main(): | 38 def main(): |
46 parser = optparse.OptionParser() | 39 parser = optparse.OptionParser() |
47 parser.add_option('--build-dir', default=os.path.join('out', 'Debug'), | 40 parser.add_option('--build-dir', default=os.path.join('out', 'Debug'), |
48 help='relative path from the base source directory') | 41 help='relative path from the base source directory') |
49 options, args = parser.parse_args() | 42 options, args = parser.parse_args() |
50 | 43 finder = common.DirectoryFinder(options.build_dir) |
51 # Expect |my_dir| to be .../pdfium/testing/tools. | 44 pdfium_test_path = finder.ExecutablePath('pdfium_test') |
52 my_dir = os.path.dirname(os.path.realpath(__file__)) | 45 working_dir = finder.WorkingDir(os.path.join('testing', 'corpus')) |
53 testing_dir = os.path.dirname(my_dir) | |
54 pdfium_dir = os.path.dirname(testing_dir) | |
55 if (os.path.basename(my_dir) != 'tools' or | |
56 os.path.basename(testing_dir) != 'testing'): | |
57 print 'Confused, can not find pdfium root directory, aborting.' | |
58 return 1 | |
59 | |
60 # Find path to build directory. This depends on whether this is a | |
61 # standalone build vs. a build as part of a chromium checkout. For | |
62 # standalone, we expect a path like .../pdfium/out/Debug, but for | |
63 # chromium, we expect a path like .../src/out/Debug two levels | |
64 # higher (to skip over the third_party/pdfium path component under | |
65 # which chromium sticks pdfium). | |
66 base_dir = pdfium_dir | |
67 one_up_dir = os.path.dirname(base_dir) | |
68 two_up_dir = os.path.dirname(one_up_dir) | |
69 if (os.path.basename(two_up_dir) == 'src' and | |
70 os.path.basename(one_up_dir) == 'third_party'): | |
71 base_dir = two_up_dir | |
72 build_dir = os.path.join(base_dir, options.build_dir) | |
73 | |
74 # Compiled binaries are found under the build path. | |
75 pdfium_test_path = os.path.join(build_dir, 'pdfium_test') | |
76 pdfium_diff_path = os.path.join(build_dir, 'pdfium_diff') | |
77 if sys.platform.startswith('win'): | |
78 pdfium_test_path = pdfium_test_path + '.exe' | |
79 pdfium_diff_path = pdfium_diff_path + '.exe' | |
80 # TODO(tsepez): Mac may require special handling here. | |
81 | |
82 # Place generated files under the build directory, not source directory. | |
83 working_dir = os.path.join(build_dir, 'gen', 'pdfium', 'testing', 'corpus') | |
84 if not os.path.exists(working_dir): | 46 if not os.path.exists(working_dir): |
85 os.makedirs(working_dir) | 47 os.makedirs(working_dir) |
86 | 48 |
| 49 test_suppressor = suppressor.Suppressor(finder) |
| 50 image_differ = pngdiffer.PNGDiffer(finder) |
| 51 |
87 # test files are under .../pdfium/testing/corpus. | 52 # test files are under .../pdfium/testing/corpus. |
88 os_exit_code = 0 | 53 failures = [] |
89 walk_from_dir = os.path.join(testing_dir, 'corpus'); | 54 walk_from_dir = finder.TestingDir('corpus'); |
90 input_file_re = re.compile('^[a-zA-Z0-9_.]+[.]pdf$') | 55 input_file_re = re.compile('^[a-zA-Z0-9_.]+[.]pdf$') |
91 for source_dir, _, filename_list in os.walk(walk_from_dir): | 56 for source_dir, _, filename_list in os.walk(walk_from_dir): |
92 for input_filename in filename_list: | 57 for input_filename in filename_list: |
93 if input_file_re.match(input_filename): | 58 if input_file_re.match(input_filename): |
94 input_path = os.path.join(source_dir, input_filename) | 59 input_path = os.path.join(source_dir, input_filename) |
95 if os.path.isfile(input_path): | 60 if os.path.isfile(input_path): |
96 if not test_one_file(input_filename, source_dir, working_dir, | 61 if test_suppressor.IsSuppressed(input_filename): |
97 pdfium_test_path, pdfium_diff_path): | 62 continue |
98 os_exit_code = 1 | 63 if not test_one_file(input_filename, source_dir, working_dir, |
| 64 pdfium_test_path, image_differ): |
| 65 failures.append(input_path) |
99 | 66 |
100 return os_exit_code | 67 if failures: |
| 68 print '\n\nSummary of Failures:' |
| 69 for failure in failures: |
| 70 print failure |
| 71 return 1 |
| 72 |
| 73 return 0 |
101 | 74 |
102 | 75 |
103 if __name__ == '__main__': | 76 if __name__ == '__main__': |
104 sys.exit(main()) | 77 sys.exit(main()) |
OLD | NEW |