blob: 3737969fb2cfb070ed4b1f373622f2d05e078d2f [file] [log] [blame]
dsinclair2a8a20c2016-04-25 09:46:17 -07001#!/usr/bin/env python
2# Copyright 2016 The PDFium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
dsinclair849284d2016-05-17 06:13:36 -07006import functools
7import multiprocessing
dsinclair2a8a20c2016-04-25 09:46:17 -07008import optparse
9import os
10import re
dsinclair849284d2016-05-17 06:13:36 -070011import shutil
dsinclair2a8a20c2016-04-25 09:46:17 -070012import subprocess
13import sys
14
15import common
stephanafa05e972017-01-02 06:19:41 -080016import gold
dsinclair2a8a20c2016-04-25 09:46:17 -070017import pngdiffer
18import suppressor
19
dsinclair849284d2016-05-17 06:13:36 -070020class KeyboardInterruptError(Exception): pass
21
dsinclair2a8a20c2016-04-25 09:46:17 -070022# Nomenclature:
23# x_root - "x"
24# x_filename - "x.ext"
25# x_path - "path/to/a/b/c/x.ext"
26# c_dir - "path/to/a/b/c"
27
dsinclair849284d2016-05-17 06:13:36 -070028def TestOneFileParallel(this, test_case):
29 """Wrapper to call GenerateAndTest() and redirect output to stdout."""
30 try:
31 input_filename, source_dir = test_case
32 result = this.GenerateAndTest(input_filename, source_dir);
33 return (result, input_filename, source_dir)
34 except KeyboardInterrupt:
35 raise KeyboardInterruptError()
36
37
dsinclair2a8a20c2016-04-25 09:46:17 -070038class TestRunner:
39 def __init__(self, dirname):
Ryan Harrison80302c72018-05-10 18:27:25 +000040 # Currently the only used directories are corpus, javascript, and pixel,
41 # which all correspond directly to the type for the test being run. In the
42 # future if there are tests that don't have this clean correspondence, then
43 # an argument for the type will need to be added.
dsinclair2a8a20c2016-04-25 09:46:17 -070044 self.test_dir = dirname
Ryan Harrison80302c72018-05-10 18:27:25 +000045 self.test_type = dirname
Henrique Nakashima3bcabf32017-06-27 09:48:24 -040046 self.enforce_expected_images = False
Dan Sinclairaeadad12017-07-18 16:43:41 -040047 self.oneshot_renderer = False
dsinclair2a8a20c2016-04-25 09:46:17 -070048
stephanafa05e972017-01-02 06:19:41 -080049 # GenerateAndTest returns a tuple <success, outputfiles> where
50 # success is a boolean indicating whether the tests passed comparison
51 # tests and outputfiles is a list tuples:
52 # (path_to_image, md5_hash_of_pixelbuffer)
dsinclair2a8a20c2016-04-25 09:46:17 -070053 def GenerateAndTest(self, input_filename, source_dir):
Ryan Harrison1118a662018-05-31 19:26:52 +000054 use_ahem = 'use_ahem' in source_dir
55
dsinclair2a8a20c2016-04-25 09:46:17 -070056 input_root, _ = os.path.splitext(input_filename)
57 expected_txt_path = os.path.join(source_dir, input_root + '_expected.txt')
58
59 pdf_path = os.path.join(self.working_dir, input_root + '.pdf')
60
61 # Remove any existing generated images from previous runs.
62 actual_images = self.image_differ.GetActualFiles(input_filename, source_dir,
63 self.working_dir)
64 for image in actual_images:
65 if os.path.exists(image):
66 os.remove(image)
67
68 sys.stdout.flush()
69
70 raised_exception = self.Generate(source_dir, input_filename, input_root,
71 pdf_path)
72
Henrique Nakashima3bcabf32017-06-27 09:48:24 -040073 if raised_exception is not None:
74 print 'FAILURE: %s; %s' % (input_filename, raised_exception)
stephanafa05e972017-01-02 06:19:41 -080075 return False, []
dsinclair2a8a20c2016-04-25 09:46:17 -070076
stephanafa05e972017-01-02 06:19:41 -080077 results = []
dsinclair2a8a20c2016-04-25 09:46:17 -070078 if os.path.exists(expected_txt_path):
79 raised_exception = self.TestText(input_root, expected_txt_path, pdf_path)
80 else:
Ryan Harrison1118a662018-05-31 19:26:52 +000081 raised_exception, results = self.TestPixel(input_root, pdf_path, use_ahem)
dsinclair2a8a20c2016-04-25 09:46:17 -070082
Henrique Nakashima3bcabf32017-06-27 09:48:24 -040083 if raised_exception is not None:
84 print 'FAILURE: %s; %s' % (input_filename, raised_exception)
stephanafa05e972017-01-02 06:19:41 -080085 return False, results
dsinclair2a8a20c2016-04-25 09:46:17 -070086
Henrique Nakashima3bcabf32017-06-27 09:48:24 -040087 if actual_images:
dsinclair2a8a20c2016-04-25 09:46:17 -070088 if self.image_differ.HasDifferences(input_filename, source_dir,
89 self.working_dir):
Henrique Nakashima15bc9742018-04-26 15:55:07 +000090 self.RegenerateIfNeeded_(input_filename, source_dir)
stephanafa05e972017-01-02 06:19:41 -080091 return False, results
Henrique Nakashima3bcabf32017-06-27 09:48:24 -040092 else:
93 if (self.enforce_expected_images
Henrique Nakashima6fac27d2017-06-27 15:52:45 -040094 and not self.test_suppressor.IsImageDiffSuppressed(input_filename)):
Henrique Nakashima15bc9742018-04-26 15:55:07 +000095 self.RegenerateIfNeeded_(input_filename, source_dir)
Henrique Nakashima3bcabf32017-06-27 09:48:24 -040096 print 'FAILURE: %s; Missing expected images' % input_filename
97 return False, results
98
stephanafa05e972017-01-02 06:19:41 -080099 return True, results
dsinclair2a8a20c2016-04-25 09:46:17 -0700100
Henrique Nakashima15bc9742018-04-26 15:55:07 +0000101 def RegenerateIfNeeded_(self, input_filename, source_dir):
102 if (not self.options.regenerate_expected
103 or self.test_suppressor.IsResultSuppressed(input_filename)
104 or self.test_suppressor.IsImageDiffSuppressed(input_filename)):
105 return
106
107 platform_only = (self.options.regenerate_expected == 'platform')
108 self.image_differ.Regenerate(input_filename, source_dir,
109 self.working_dir, platform_only)
110
dsinclair2a8a20c2016-04-25 09:46:17 -0700111 def Generate(self, source_dir, input_filename, input_root, pdf_path):
112 original_path = os.path.join(source_dir, input_filename)
113 input_path = os.path.join(source_dir, input_root + '.in')
114
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400115 input_event_path = os.path.join(source_dir, input_root + '.evt')
dsinclair849284d2016-05-17 06:13:36 -0700116 if os.path.exists(input_event_path):
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400117 output_event_path = os.path.splitext(pdf_path)[0] + '.evt'
dsinclair849284d2016-05-17 06:13:36 -0700118 shutil.copyfile(input_event_path, output_event_path)
119
dsinclair2a8a20c2016-04-25 09:46:17 -0700120 if not os.path.exists(input_path):
121 if os.path.exists(original_path):
122 shutil.copyfile(original_path, pdf_path)
123 return None
124
125 sys.stdout.flush()
dsinclair849284d2016-05-17 06:13:36 -0700126
dsinclair2a8a20c2016-04-25 09:46:17 -0700127 return common.RunCommand(
128 [sys.executable, self.fixup_path, '--output-dir=' + self.working_dir,
129 input_path])
130
dsinclair2a8a20c2016-04-25 09:46:17 -0700131 def TestText(self, input_root, expected_txt_path, pdf_path):
132 txt_path = os.path.join(self.working_dir, input_root + '.txt')
133
134 with open(txt_path, 'w') as outfile:
Lei Zhang63b01262017-08-31 08:54:46 -0700135 cmd_to_run = [self.pdfium_test_path, '--send-events', pdf_path]
dsinclair2a8a20c2016-04-25 09:46:17 -0700136 subprocess.check_call(cmd_to_run, stdout=outfile)
137
138 cmd = [sys.executable, self.text_diff_path, expected_txt_path, txt_path]
139 return common.RunCommand(cmd)
140
Ryan Harrison1118a662018-05-31 19:26:52 +0000141 def TestPixel(self, input_root, pdf_path, use_ahem):
Henrique Nakashimaea4a56d2017-11-29 19:34:19 +0000142 cmd_to_run = [self.pdfium_test_path, '--send-events', '--png', '--md5']
Ryan Harrison1118a662018-05-31 19:26:52 +0000143
Dan Sinclairaeadad12017-07-18 16:43:41 -0400144 if self.oneshot_renderer:
145 cmd_to_run.append('--render-oneshot')
Ryan Harrison1118a662018-05-31 19:26:52 +0000146
147 if use_ahem:
148 cmd_to_run.append('--font-dir=%s' % self.font_dir)
149
stephanafa05e972017-01-02 06:19:41 -0800150 cmd_to_run.append(pdf_path)
151 return common.RunCommandExtractHashedFiles(cmd_to_run)
dsinclair2a8a20c2016-04-25 09:46:17 -0700152
153 def HandleResult(self, input_filename, input_path, result):
dan sinclair00d40642017-01-30 19:48:54 -0800154 success, image_paths = result
Henrique Nakashimaea4a56d2017-11-29 19:34:19 +0000155
156 if image_paths:
157 for img_path, md5_hash in image_paths:
158 # The output filename without image extension becomes the test name.
159 # For example, "/path/to/.../testing/corpus/example_005.pdf.0.png"
160 # becomes "example_005.pdf.0".
161 test_name = os.path.splitext(os.path.split(img_path)[1])[0]
162
163 if not self.test_suppressor.IsResultSuppressed(input_filename):
164 matched = self.gold_baseline.MatchLocalResult(test_name, md5_hash)
165 if matched == gold.GoldBaseline.MISMATCH:
166 print 'Skia Gold hash mismatch for test case: %s' % test_name
167 elif matched == gold.GoldBaseline.NO_BASELINE:
168 print 'No Skia Gold baseline found for test case: %s' % test_name
169
170 if self.gold_results:
stephana38c27052017-01-13 13:16:40 -0800171 self.gold_results.AddTestResult(test_name, md5_hash, img_path)
stephanafa05e972017-01-02 06:19:41 -0800172
dsinclair2a8a20c2016-04-25 09:46:17 -0700173 if self.test_suppressor.IsResultSuppressed(input_filename):
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400174 self.result_suppressed_cases.append(input_filename)
dan sinclair00d40642017-01-30 19:48:54 -0800175 if success:
dsinclair2a8a20c2016-04-25 09:46:17 -0700176 self.surprises.append(input_path)
177 else:
dan sinclair00d40642017-01-30 19:48:54 -0800178 if not success:
dsinclair2a8a20c2016-04-25 09:46:17 -0700179 self.failures.append(input_path)
180
dsinclair2a8a20c2016-04-25 09:46:17 -0700181 def Run(self):
182 parser = optparse.OptionParser()
stephanafa05e972017-01-02 06:19:41 -0800183
dsinclair2a8a20c2016-04-25 09:46:17 -0700184 parser.add_option('--build-dir', default=os.path.join('out', 'Debug'),
185 help='relative path from the base source directory')
stephanafa05e972017-01-02 06:19:41 -0800186
dsinclair849284d2016-05-17 06:13:36 -0700187 parser.add_option('-j', default=multiprocessing.cpu_count(),
dsinclair2a8a20c2016-04-25 09:46:17 -0700188 dest='num_workers', type='int',
189 help='run NUM_WORKERS jobs in parallel')
stephanafa05e972017-01-02 06:19:41 -0800190
stephanafa05e972017-01-02 06:19:41 -0800191 parser.add_option('--gold_properties', default='', dest="gold_properties",
Henrique Nakashima352e2512017-10-26 11:22:52 -0400192 help='Key value pairs that are written to the top level '
193 'of the JSON file that is ingested by Gold.')
stephanafa05e972017-01-02 06:19:41 -0800194
195 parser.add_option('--gold_key', default='', dest="gold_key",
Henrique Nakashima352e2512017-10-26 11:22:52 -0400196 help='Key value pairs that are added to the "key" field '
197 'of the JSON file that is ingested by Gold.')
stephanafa05e972017-01-02 06:19:41 -0800198
199 parser.add_option('--gold_output_dir', default='', dest="gold_output_dir",
Henrique Nakashima352e2512017-10-26 11:22:52 -0400200 help='Path of where to write the JSON output to be '
201 'uploaded to Gold.')
stephanafa05e972017-01-02 06:19:41 -0800202
Henrique Nakashima352e2512017-10-26 11:22:52 -0400203 parser.add_option('--gold_ignore_hashes', default='',
204 dest="gold_ignore_hashes",
stephanad5320362017-01-26 15:18:54 -0800205 help='Path to a file with MD5 hashes we wish to ignore.')
206
Henrique Nakashima352e2512017-10-26 11:22:52 -0400207 parser.add_option('--regenerate_expected', default='',
208 dest="regenerate_expected",
209 help='Regenerates expected images. Valid values are '
210 '"all" to regenerate all expected pngs, and '
211 '"platform" to regenerate only platform-specific '
212 'expected pngs.')
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400213
Henrique Nakashima352e2512017-10-26 11:22:52 -0400214 parser.add_option('--ignore_errors', action="store_true",
215 dest="ignore_errors",
216 help='Prevents the return value from being non-zero '
217 'when image comparison fails.')
stephanafa05e972017-01-02 06:19:41 -0800218
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400219 self.options, self.args = parser.parse_args()
dsinclair2a8a20c2016-04-25 09:46:17 -0700220
Henrique Nakashima352e2512017-10-26 11:22:52 -0400221 if (self.options.regenerate_expected
222 and self.options.regenerate_expected not in ['all', 'platform']) :
223 print 'FAILURE: --regenerate_expected must be "all" or "platform"'
224 return 1
225
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400226 finder = common.DirectoryFinder(self.options.build_dir)
dsinclair2a8a20c2016-04-25 09:46:17 -0700227 self.fixup_path = finder.ScriptPath('fixup_pdf_template.py')
228 self.text_diff_path = finder.ScriptPath('text_diff.py')
Ryan Harrison1118a662018-05-31 19:26:52 +0000229 self.font_dir = os.path.join(finder.TestingDir(), 'resources', 'fonts')
dsinclair2a8a20c2016-04-25 09:46:17 -0700230
dsinclair2a8a20c2016-04-25 09:46:17 -0700231 self.source_dir = finder.TestingDir()
dsinclair849284d2016-05-17 06:13:36 -0700232 if self.test_dir != 'corpus':
233 test_dir = finder.TestingDir(os.path.join('resources', self.test_dir))
234 else:
235 test_dir = finder.TestingDir(self.test_dir)
236
dsinclair2a8a20c2016-04-25 09:46:17 -0700237 self.pdfium_test_path = finder.ExecutablePath('pdfium_test')
238 if not os.path.exists(self.pdfium_test_path):
239 print "FAILURE: Can't find test executable '%s'" % self.pdfium_test_path
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400240 print 'Use --build-dir to specify its location.'
dsinclair2a8a20c2016-04-25 09:46:17 -0700241 return 1
242
243 self.working_dir = finder.WorkingDir(os.path.join('testing', self.test_dir))
244 if not os.path.exists(self.working_dir):
245 os.makedirs(self.working_dir)
246
247 self.feature_string = subprocess.check_output([self.pdfium_test_path,
248 '--show-config'])
249 self.test_suppressor = suppressor.Suppressor(finder, self.feature_string)
250 self.image_differ = pngdiffer.PNGDiffer(finder)
251
Henrique Nakashimaea4a56d2017-11-29 19:34:19 +0000252 self.gold_baseline = gold.GoldBaseline(self.options.gold_properties)
253
dsinclair2a8a20c2016-04-25 09:46:17 -0700254 walk_from_dir = finder.TestingDir(test_dir);
255
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400256 self.test_cases = []
257 self.execution_suppressed_cases = []
Henrique Nakashima62d50762017-06-27 13:06:23 -0400258 input_file_re = re.compile('^.+[.](in|pdf)$')
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400259 if self.args:
260 for file_name in self.args:
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400261 file_name.replace('.pdf', '.in')
dsinclair2a8a20c2016-04-25 09:46:17 -0700262 input_path = os.path.join(walk_from_dir, file_name)
263 if not os.path.isfile(input_path):
264 print "Can't find test file '%s'" % file_name
265 return 1
266
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400267 self.test_cases.append((os.path.basename(input_path),
dsinclair2a8a20c2016-04-25 09:46:17 -0700268 os.path.dirname(input_path)))
269 else:
270 for file_dir, _, filename_list in os.walk(walk_from_dir):
271 for input_filename in filename_list:
272 if input_file_re.match(input_filename):
273 input_path = os.path.join(file_dir, input_filename)
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400274 if self.test_suppressor.IsExecutionSuppressed(input_path):
275 self.execution_suppressed_cases.append(input_path)
276 else:
dsinclair2a8a20c2016-04-25 09:46:17 -0700277 if os.path.isfile(input_path):
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400278 self.test_cases.append((input_filename, file_dir))
dsinclair2a8a20c2016-04-25 09:46:17 -0700279
Lei Zhang1ee96012018-04-09 17:31:14 +0000280 self.test_cases.sort()
dsinclair2a8a20c2016-04-25 09:46:17 -0700281 self.failures = []
282 self.surprises = []
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400283 self.result_suppressed_cases = []
dsinclair2a8a20c2016-04-25 09:46:17 -0700284
stephanafa05e972017-01-02 06:19:41 -0800285 # Collect Gold results if an output directory was named.
286 self.gold_results = None
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400287 if self.options.gold_output_dir:
Ryan Harrison80302c72018-05-10 18:27:25 +0000288 self.gold_results = gold.GoldResults(self.test_type,
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400289 self.options.gold_output_dir,
290 self.options.gold_properties,
291 self.options.gold_key,
292 self.options.gold_ignore_hashes)
stephanafa05e972017-01-02 06:19:41 -0800293
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400294 if self.options.num_workers > 1 and len(self.test_cases) > 1:
dsinclair849284d2016-05-17 06:13:36 -0700295 try:
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400296 pool = multiprocessing.Pool(self.options.num_workers)
dsinclair849284d2016-05-17 06:13:36 -0700297 worker_func = functools.partial(TestOneFileParallel, self)
298
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400299 worker_results = pool.imap(worker_func, self.test_cases)
dsinclair849284d2016-05-17 06:13:36 -0700300 for worker_result in worker_results:
301 result, input_filename, source_dir = worker_result
302 input_path = os.path.join(source_dir, input_filename)
303
304 self.HandleResult(input_filename, input_path, result)
305
306 except KeyboardInterrupt:
307 pool.terminate()
308 finally:
309 pool.close()
310 pool.join()
311 else:
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400312 for test_case in self.test_cases:
dsinclair849284d2016-05-17 06:13:36 -0700313 input_filename, input_file_dir = test_case
314 result = self.GenerateAndTest(input_filename, input_file_dir)
315 self.HandleResult(input_filename,
316 os.path.join(input_file_dir, input_filename), result)
dsinclair2a8a20c2016-04-25 09:46:17 -0700317
stephanafa05e972017-01-02 06:19:41 -0800318 if self.gold_results:
319 self.gold_results.WriteResults()
320
dsinclair2a8a20c2016-04-25 09:46:17 -0700321 if self.surprises:
322 self.surprises.sort()
323 print '\n\nUnexpected Successes:'
324 for surprise in self.surprises:
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400325 print surprise
dsinclair2a8a20c2016-04-25 09:46:17 -0700326
327 if self.failures:
328 self.failures.sort()
329 print '\n\nSummary of Failures:'
330 for failure in self.failures:
331 print failure
dan sinclair00d40642017-01-30 19:48:54 -0800332
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400333 self._PrintSummary()
334
335 if self.failures:
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400336 if not self.options.ignore_errors:
dan sinclair00d40642017-01-30 19:48:54 -0800337 return 1
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400338
dsinclair2a8a20c2016-04-25 09:46:17 -0700339 return 0
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400340
341 def _PrintSummary(self):
342 number_test_cases = len(self.test_cases)
343 number_failures = len(self.failures)
344 number_suppressed = len(self.result_suppressed_cases)
345 number_successes = number_test_cases - number_failures - number_suppressed
346 number_surprises = len(self.surprises)
347 print
348 print 'Test cases executed: %d' % number_test_cases
349 print ' Successes: %d' % number_successes
350 print ' Suppressed: %d' % number_suppressed
351 print ' Surprises: %d' % number_surprises
352 print ' Failures: %d' % number_failures
353 print
354 print 'Test cases not executed: %d' % len(self.execution_suppressed_cases)
355
356 def SetEnforceExpectedImages(self, new_value):
357 """Set whether to enforce that each test case provide an expected image."""
358 self.enforce_expected_images = new_value
Dan Sinclairaeadad12017-07-18 16:43:41 -0400359
360 def SetOneShotRenderer(self, new_value):
361 """Set whether to use the oneshot renderer. """
362 self.oneshot_renderer = new_value