blob: 5acda86f549657b289d685bc24b26dc0b3f995a7 [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):
40 self.test_dir = dirname
Henrique Nakashima3bcabf32017-06-27 09:48:24 -040041 self.enforce_expected_images = False
Dan Sinclairaeadad12017-07-18 16:43:41 -040042 self.oneshot_renderer = False
dsinclair2a8a20c2016-04-25 09:46:17 -070043
stephanafa05e972017-01-02 06:19:41 -080044 # GenerateAndTest returns a tuple <success, outputfiles> where
45 # success is a boolean indicating whether the tests passed comparison
46 # tests and outputfiles is a list tuples:
47 # (path_to_image, md5_hash_of_pixelbuffer)
dsinclair2a8a20c2016-04-25 09:46:17 -070048 def GenerateAndTest(self, input_filename, source_dir):
49 input_root, _ = os.path.splitext(input_filename)
50 expected_txt_path = os.path.join(source_dir, input_root + '_expected.txt')
51
52 pdf_path = os.path.join(self.working_dir, input_root + '.pdf')
53
54 # Remove any existing generated images from previous runs.
55 actual_images = self.image_differ.GetActualFiles(input_filename, source_dir,
56 self.working_dir)
57 for image in actual_images:
58 if os.path.exists(image):
59 os.remove(image)
60
61 sys.stdout.flush()
62
63 raised_exception = self.Generate(source_dir, input_filename, input_root,
64 pdf_path)
65
Henrique Nakashima3bcabf32017-06-27 09:48:24 -040066 if raised_exception is not None:
67 print 'FAILURE: %s; %s' % (input_filename, raised_exception)
stephanafa05e972017-01-02 06:19:41 -080068 return False, []
dsinclair2a8a20c2016-04-25 09:46:17 -070069
stephanafa05e972017-01-02 06:19:41 -080070 results = []
dsinclair2a8a20c2016-04-25 09:46:17 -070071 if os.path.exists(expected_txt_path):
72 raised_exception = self.TestText(input_root, expected_txt_path, pdf_path)
73 else:
stephanafa05e972017-01-02 06:19:41 -080074 raised_exception, results = self.TestPixel(input_root, pdf_path)
dsinclair2a8a20c2016-04-25 09:46:17 -070075
Henrique Nakashima3bcabf32017-06-27 09:48:24 -040076 if raised_exception is not None:
77 print 'FAILURE: %s; %s' % (input_filename, raised_exception)
stephanafa05e972017-01-02 06:19:41 -080078 return False, results
dsinclair2a8a20c2016-04-25 09:46:17 -070079
Henrique Nakashima3bcabf32017-06-27 09:48:24 -040080 if actual_images:
dsinclair2a8a20c2016-04-25 09:46:17 -070081 if self.image_differ.HasDifferences(input_filename, source_dir,
82 self.working_dir):
Henrique Nakashima15bc9742018-04-26 15:55:07 +000083 self.RegenerateIfNeeded_(input_filename, source_dir)
stephanafa05e972017-01-02 06:19:41 -080084 return False, results
Henrique Nakashima3bcabf32017-06-27 09:48:24 -040085 else:
86 if (self.enforce_expected_images
Henrique Nakashima6fac27d2017-06-27 15:52:45 -040087 and not self.test_suppressor.IsImageDiffSuppressed(input_filename)):
Henrique Nakashima15bc9742018-04-26 15:55:07 +000088 self.RegenerateIfNeeded_(input_filename, source_dir)
Henrique Nakashima3bcabf32017-06-27 09:48:24 -040089 print 'FAILURE: %s; Missing expected images' % input_filename
90 return False, results
91
stephanafa05e972017-01-02 06:19:41 -080092 return True, results
dsinclair2a8a20c2016-04-25 09:46:17 -070093
Henrique Nakashima15bc9742018-04-26 15:55:07 +000094 def RegenerateIfNeeded_(self, input_filename, source_dir):
95 if (not self.options.regenerate_expected
96 or self.test_suppressor.IsResultSuppressed(input_filename)
97 or self.test_suppressor.IsImageDiffSuppressed(input_filename)):
98 return
99
100 platform_only = (self.options.regenerate_expected == 'platform')
101 self.image_differ.Regenerate(input_filename, source_dir,
102 self.working_dir, platform_only)
103
dsinclair2a8a20c2016-04-25 09:46:17 -0700104 def Generate(self, source_dir, input_filename, input_root, pdf_path):
105 original_path = os.path.join(source_dir, input_filename)
106 input_path = os.path.join(source_dir, input_root + '.in')
107
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400108 input_event_path = os.path.join(source_dir, input_root + '.evt')
dsinclair849284d2016-05-17 06:13:36 -0700109 if os.path.exists(input_event_path):
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400110 output_event_path = os.path.splitext(pdf_path)[0] + '.evt'
dsinclair849284d2016-05-17 06:13:36 -0700111 shutil.copyfile(input_event_path, output_event_path)
112
dsinclair2a8a20c2016-04-25 09:46:17 -0700113 if not os.path.exists(input_path):
114 if os.path.exists(original_path):
115 shutil.copyfile(original_path, pdf_path)
116 return None
117
118 sys.stdout.flush()
dsinclair849284d2016-05-17 06:13:36 -0700119
dsinclair2a8a20c2016-04-25 09:46:17 -0700120 return common.RunCommand(
121 [sys.executable, self.fixup_path, '--output-dir=' + self.working_dir,
122 input_path])
123
dsinclair2a8a20c2016-04-25 09:46:17 -0700124 def TestText(self, input_root, expected_txt_path, pdf_path):
125 txt_path = os.path.join(self.working_dir, input_root + '.txt')
126
127 with open(txt_path, 'w') as outfile:
Lei Zhang63b01262017-08-31 08:54:46 -0700128 cmd_to_run = [self.pdfium_test_path, '--send-events', pdf_path]
dsinclair2a8a20c2016-04-25 09:46:17 -0700129 subprocess.check_call(cmd_to_run, stdout=outfile)
130
131 cmd = [sys.executable, self.text_diff_path, expected_txt_path, txt_path]
132 return common.RunCommand(cmd)
133
dsinclair2a8a20c2016-04-25 09:46:17 -0700134 def TestPixel(self, input_root, pdf_path):
Henrique Nakashimaea4a56d2017-11-29 19:34:19 +0000135 cmd_to_run = [self.pdfium_test_path, '--send-events', '--png', '--md5']
Dan Sinclairaeadad12017-07-18 16:43:41 -0400136 if self.oneshot_renderer:
137 cmd_to_run.append('--render-oneshot')
stephanafa05e972017-01-02 06:19:41 -0800138 cmd_to_run.append(pdf_path)
139 return common.RunCommandExtractHashedFiles(cmd_to_run)
dsinclair2a8a20c2016-04-25 09:46:17 -0700140
141 def HandleResult(self, input_filename, input_path, result):
dan sinclair00d40642017-01-30 19:48:54 -0800142 success, image_paths = result
Henrique Nakashimaea4a56d2017-11-29 19:34:19 +0000143
144 if image_paths:
145 for img_path, md5_hash in image_paths:
146 # The output filename without image extension becomes the test name.
147 # For example, "/path/to/.../testing/corpus/example_005.pdf.0.png"
148 # becomes "example_005.pdf.0".
149 test_name = os.path.splitext(os.path.split(img_path)[1])[0]
150
151 if not self.test_suppressor.IsResultSuppressed(input_filename):
152 matched = self.gold_baseline.MatchLocalResult(test_name, md5_hash)
153 if matched == gold.GoldBaseline.MISMATCH:
154 print 'Skia Gold hash mismatch for test case: %s' % test_name
155 elif matched == gold.GoldBaseline.NO_BASELINE:
156 print 'No Skia Gold baseline found for test case: %s' % test_name
157
158 if self.gold_results:
stephana38c27052017-01-13 13:16:40 -0800159 self.gold_results.AddTestResult(test_name, md5_hash, img_path)
stephanafa05e972017-01-02 06:19:41 -0800160
dsinclair2a8a20c2016-04-25 09:46:17 -0700161 if self.test_suppressor.IsResultSuppressed(input_filename):
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400162 self.result_suppressed_cases.append(input_filename)
dan sinclair00d40642017-01-30 19:48:54 -0800163 if success:
dsinclair2a8a20c2016-04-25 09:46:17 -0700164 self.surprises.append(input_path)
165 else:
dan sinclair00d40642017-01-30 19:48:54 -0800166 if not success:
dsinclair2a8a20c2016-04-25 09:46:17 -0700167 self.failures.append(input_path)
168
dsinclair2a8a20c2016-04-25 09:46:17 -0700169 def Run(self):
170 parser = optparse.OptionParser()
stephanafa05e972017-01-02 06:19:41 -0800171
dsinclair2a8a20c2016-04-25 09:46:17 -0700172 parser.add_option('--build-dir', default=os.path.join('out', 'Debug'),
173 help='relative path from the base source directory')
stephanafa05e972017-01-02 06:19:41 -0800174
dsinclair849284d2016-05-17 06:13:36 -0700175 parser.add_option('-j', default=multiprocessing.cpu_count(),
dsinclair2a8a20c2016-04-25 09:46:17 -0700176 dest='num_workers', type='int',
177 help='run NUM_WORKERS jobs in parallel')
stephanafa05e972017-01-02 06:19:41 -0800178
stephanafa05e972017-01-02 06:19:41 -0800179 parser.add_option('--gold_properties', default='', dest="gold_properties",
Henrique Nakashima352e2512017-10-26 11:22:52 -0400180 help='Key value pairs that are written to the top level '
181 'of the JSON file that is ingested by Gold.')
stephanafa05e972017-01-02 06:19:41 -0800182
183 parser.add_option('--gold_key', default='', dest="gold_key",
Henrique Nakashima352e2512017-10-26 11:22:52 -0400184 help='Key value pairs that are added to the "key" field '
185 'of the JSON file that is ingested by Gold.')
stephanafa05e972017-01-02 06:19:41 -0800186
187 parser.add_option('--gold_output_dir', default='', dest="gold_output_dir",
Henrique Nakashima352e2512017-10-26 11:22:52 -0400188 help='Path of where to write the JSON output to be '
189 'uploaded to Gold.')
stephanafa05e972017-01-02 06:19:41 -0800190
Henrique Nakashima352e2512017-10-26 11:22:52 -0400191 parser.add_option('--gold_ignore_hashes', default='',
192 dest="gold_ignore_hashes",
stephanad5320362017-01-26 15:18:54 -0800193 help='Path to a file with MD5 hashes we wish to ignore.')
194
Henrique Nakashima352e2512017-10-26 11:22:52 -0400195 parser.add_option('--regenerate_expected', default='',
196 dest="regenerate_expected",
197 help='Regenerates expected images. Valid values are '
198 '"all" to regenerate all expected pngs, and '
199 '"platform" to regenerate only platform-specific '
200 'expected pngs.')
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400201
Henrique Nakashima352e2512017-10-26 11:22:52 -0400202 parser.add_option('--ignore_errors', action="store_true",
203 dest="ignore_errors",
204 help='Prevents the return value from being non-zero '
205 'when image comparison fails.')
stephanafa05e972017-01-02 06:19:41 -0800206
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400207 self.options, self.args = parser.parse_args()
dsinclair2a8a20c2016-04-25 09:46:17 -0700208
Henrique Nakashima352e2512017-10-26 11:22:52 -0400209 if (self.options.regenerate_expected
210 and self.options.regenerate_expected not in ['all', 'platform']) :
211 print 'FAILURE: --regenerate_expected must be "all" or "platform"'
212 return 1
213
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400214 finder = common.DirectoryFinder(self.options.build_dir)
dsinclair2a8a20c2016-04-25 09:46:17 -0700215 self.fixup_path = finder.ScriptPath('fixup_pdf_template.py')
216 self.text_diff_path = finder.ScriptPath('text_diff.py')
217
dsinclair2a8a20c2016-04-25 09:46:17 -0700218 self.source_dir = finder.TestingDir()
dsinclair849284d2016-05-17 06:13:36 -0700219 if self.test_dir != 'corpus':
220 test_dir = finder.TestingDir(os.path.join('resources', self.test_dir))
221 else:
222 test_dir = finder.TestingDir(self.test_dir)
223
dsinclair2a8a20c2016-04-25 09:46:17 -0700224 self.pdfium_test_path = finder.ExecutablePath('pdfium_test')
225 if not os.path.exists(self.pdfium_test_path):
226 print "FAILURE: Can't find test executable '%s'" % self.pdfium_test_path
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400227 print 'Use --build-dir to specify its location.'
dsinclair2a8a20c2016-04-25 09:46:17 -0700228 return 1
229
230 self.working_dir = finder.WorkingDir(os.path.join('testing', self.test_dir))
231 if not os.path.exists(self.working_dir):
232 os.makedirs(self.working_dir)
233
234 self.feature_string = subprocess.check_output([self.pdfium_test_path,
235 '--show-config'])
236 self.test_suppressor = suppressor.Suppressor(finder, self.feature_string)
237 self.image_differ = pngdiffer.PNGDiffer(finder)
238
Henrique Nakashimaea4a56d2017-11-29 19:34:19 +0000239 self.gold_baseline = gold.GoldBaseline(self.options.gold_properties)
240
dsinclair2a8a20c2016-04-25 09:46:17 -0700241 walk_from_dir = finder.TestingDir(test_dir);
242
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400243 self.test_cases = []
244 self.execution_suppressed_cases = []
Henrique Nakashima62d50762017-06-27 13:06:23 -0400245 input_file_re = re.compile('^.+[.](in|pdf)$')
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400246 if self.args:
247 for file_name in self.args:
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400248 file_name.replace('.pdf', '.in')
dsinclair2a8a20c2016-04-25 09:46:17 -0700249 input_path = os.path.join(walk_from_dir, file_name)
250 if not os.path.isfile(input_path):
251 print "Can't find test file '%s'" % file_name
252 return 1
253
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400254 self.test_cases.append((os.path.basename(input_path),
dsinclair2a8a20c2016-04-25 09:46:17 -0700255 os.path.dirname(input_path)))
256 else:
257 for file_dir, _, filename_list in os.walk(walk_from_dir):
258 for input_filename in filename_list:
259 if input_file_re.match(input_filename):
260 input_path = os.path.join(file_dir, input_filename)
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400261 if self.test_suppressor.IsExecutionSuppressed(input_path):
262 self.execution_suppressed_cases.append(input_path)
263 else:
dsinclair2a8a20c2016-04-25 09:46:17 -0700264 if os.path.isfile(input_path):
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400265 self.test_cases.append((input_filename, file_dir))
dsinclair2a8a20c2016-04-25 09:46:17 -0700266
Lei Zhang1ee96012018-04-09 17:31:14 +0000267 self.test_cases.sort()
dsinclair2a8a20c2016-04-25 09:46:17 -0700268 self.failures = []
269 self.surprises = []
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400270 self.result_suppressed_cases = []
dsinclair2a8a20c2016-04-25 09:46:17 -0700271
stephanafa05e972017-01-02 06:19:41 -0800272 # Collect Gold results if an output directory was named.
273 self.gold_results = None
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400274 if self.options.gold_output_dir:
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400275 self.gold_results = gold.GoldResults('pdfium',
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400276 self.options.gold_output_dir,
277 self.options.gold_properties,
278 self.options.gold_key,
279 self.options.gold_ignore_hashes)
stephanafa05e972017-01-02 06:19:41 -0800280
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400281 if self.options.num_workers > 1 and len(self.test_cases) > 1:
dsinclair849284d2016-05-17 06:13:36 -0700282 try:
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400283 pool = multiprocessing.Pool(self.options.num_workers)
dsinclair849284d2016-05-17 06:13:36 -0700284 worker_func = functools.partial(TestOneFileParallel, self)
285
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400286 worker_results = pool.imap(worker_func, self.test_cases)
dsinclair849284d2016-05-17 06:13:36 -0700287 for worker_result in worker_results:
288 result, input_filename, source_dir = worker_result
289 input_path = os.path.join(source_dir, input_filename)
290
291 self.HandleResult(input_filename, input_path, result)
292
293 except KeyboardInterrupt:
294 pool.terminate()
295 finally:
296 pool.close()
297 pool.join()
298 else:
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400299 for test_case in self.test_cases:
dsinclair849284d2016-05-17 06:13:36 -0700300 input_filename, input_file_dir = test_case
301 result = self.GenerateAndTest(input_filename, input_file_dir)
302 self.HandleResult(input_filename,
303 os.path.join(input_file_dir, input_filename), result)
dsinclair2a8a20c2016-04-25 09:46:17 -0700304
stephanafa05e972017-01-02 06:19:41 -0800305 if self.gold_results:
306 self.gold_results.WriteResults()
307
dsinclair2a8a20c2016-04-25 09:46:17 -0700308 if self.surprises:
309 self.surprises.sort()
310 print '\n\nUnexpected Successes:'
311 for surprise in self.surprises:
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400312 print surprise
dsinclair2a8a20c2016-04-25 09:46:17 -0700313
314 if self.failures:
315 self.failures.sort()
316 print '\n\nSummary of Failures:'
317 for failure in self.failures:
318 print failure
dan sinclair00d40642017-01-30 19:48:54 -0800319
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400320 self._PrintSummary()
321
322 if self.failures:
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400323 if not self.options.ignore_errors:
dan sinclair00d40642017-01-30 19:48:54 -0800324 return 1
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400325
dsinclair2a8a20c2016-04-25 09:46:17 -0700326 return 0
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400327
328 def _PrintSummary(self):
329 number_test_cases = len(self.test_cases)
330 number_failures = len(self.failures)
331 number_suppressed = len(self.result_suppressed_cases)
332 number_successes = number_test_cases - number_failures - number_suppressed
333 number_surprises = len(self.surprises)
334 print
335 print 'Test cases executed: %d' % number_test_cases
336 print ' Successes: %d' % number_successes
337 print ' Suppressed: %d' % number_suppressed
338 print ' Surprises: %d' % number_surprises
339 print ' Failures: %d' % number_failures
340 print
341 print 'Test cases not executed: %d' % len(self.execution_suppressed_cases)
342
343 def SetEnforceExpectedImages(self, new_value):
344 """Set whether to enforce that each test case provide an expected image."""
345 self.enforce_expected_images = new_value
Dan Sinclairaeadad12017-07-18 16:43:41 -0400346
347 def SetOneShotRenderer(self, new_value):
348 """Set whether to use the oneshot renderer. """
349 self.oneshot_renderer = new_value