blob: 7dea24299a8c96c7d9ab9326ddf09d55c1abb5f0 [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
Ryan Harrison70cca362018-08-10 18:55:46 +000020# Arbitrary timestamp, expressed in seconds since the epoch, used to make sure
21# that tests that depend on the current time are stable. Happens to be the
22# timestamp of the first commit to repo, 2014/5/9 17:48:50.
23TEST_SEED_TIME = "1399672130"
24
dsinclair849284d2016-05-17 06:13:36 -070025class KeyboardInterruptError(Exception): pass
26
dsinclair2a8a20c2016-04-25 09:46:17 -070027# Nomenclature:
28# x_root - "x"
29# x_filename - "x.ext"
30# x_path - "path/to/a/b/c/x.ext"
31# c_dir - "path/to/a/b/c"
32
dsinclair849284d2016-05-17 06:13:36 -070033def TestOneFileParallel(this, test_case):
34 """Wrapper to call GenerateAndTest() and redirect output to stdout."""
35 try:
36 input_filename, source_dir = test_case
37 result = this.GenerateAndTest(input_filename, source_dir);
38 return (result, input_filename, source_dir)
39 except KeyboardInterrupt:
40 raise KeyboardInterruptError()
41
42
dsinclair2a8a20c2016-04-25 09:46:17 -070043class TestRunner:
44 def __init__(self, dirname):
Ryan Harrison80302c72018-05-10 18:27:25 +000045 # Currently the only used directories are corpus, javascript, and pixel,
46 # which all correspond directly to the type for the test being run. In the
47 # future if there are tests that don't have this clean correspondence, then
48 # an argument for the type will need to be added.
dsinclair2a8a20c2016-04-25 09:46:17 -070049 self.test_dir = dirname
Ryan Harrison80302c72018-05-10 18:27:25 +000050 self.test_type = dirname
Henrique Nakashima3bcabf32017-06-27 09:48:24 -040051 self.enforce_expected_images = False
Dan Sinclairaeadad12017-07-18 16:43:41 -040052 self.oneshot_renderer = False
dsinclair2a8a20c2016-04-25 09:46:17 -070053
stephanafa05e972017-01-02 06:19:41 -080054 # GenerateAndTest returns a tuple <success, outputfiles> where
55 # success is a boolean indicating whether the tests passed comparison
56 # tests and outputfiles is a list tuples:
57 # (path_to_image, md5_hash_of_pixelbuffer)
dsinclair2a8a20c2016-04-25 09:46:17 -070058 def GenerateAndTest(self, input_filename, source_dir):
Ryan Harrison1118a662018-05-31 19:26:52 +000059 use_ahem = 'use_ahem' in source_dir
60
dsinclair2a8a20c2016-04-25 09:46:17 -070061 input_root, _ = os.path.splitext(input_filename)
62 expected_txt_path = os.path.join(source_dir, input_root + '_expected.txt')
63
64 pdf_path = os.path.join(self.working_dir, input_root + '.pdf')
65
66 # Remove any existing generated images from previous runs.
67 actual_images = self.image_differ.GetActualFiles(input_filename, source_dir,
68 self.working_dir)
69 for image in actual_images:
70 if os.path.exists(image):
71 os.remove(image)
72
73 sys.stdout.flush()
74
75 raised_exception = self.Generate(source_dir, input_filename, input_root,
76 pdf_path)
77
Henrique Nakashima3bcabf32017-06-27 09:48:24 -040078 if raised_exception is not None:
79 print 'FAILURE: %s; %s' % (input_filename, raised_exception)
stephanafa05e972017-01-02 06:19:41 -080080 return False, []
dsinclair2a8a20c2016-04-25 09:46:17 -070081
stephanafa05e972017-01-02 06:19:41 -080082 results = []
dsinclair2a8a20c2016-04-25 09:46:17 -070083 if os.path.exists(expected_txt_path):
84 raised_exception = self.TestText(input_root, expected_txt_path, pdf_path)
85 else:
Ryan Harrison1118a662018-05-31 19:26:52 +000086 raised_exception, results = self.TestPixel(input_root, pdf_path, use_ahem)
dsinclair2a8a20c2016-04-25 09:46:17 -070087
Henrique Nakashima3bcabf32017-06-27 09:48:24 -040088 if raised_exception is not None:
89 print 'FAILURE: %s; %s' % (input_filename, raised_exception)
stephanafa05e972017-01-02 06:19:41 -080090 return False, results
dsinclair2a8a20c2016-04-25 09:46:17 -070091
Henrique Nakashima3bcabf32017-06-27 09:48:24 -040092 if actual_images:
dsinclair2a8a20c2016-04-25 09:46:17 -070093 if self.image_differ.HasDifferences(input_filename, source_dir,
94 self.working_dir):
Henrique Nakashima15bc9742018-04-26 15:55:07 +000095 self.RegenerateIfNeeded_(input_filename, source_dir)
stephanafa05e972017-01-02 06:19:41 -080096 return False, results
Henrique Nakashima3bcabf32017-06-27 09:48:24 -040097 else:
98 if (self.enforce_expected_images
Henrique Nakashima6fac27d2017-06-27 15:52:45 -040099 and not self.test_suppressor.IsImageDiffSuppressed(input_filename)):
Henrique Nakashima15bc9742018-04-26 15:55:07 +0000100 self.RegenerateIfNeeded_(input_filename, source_dir)
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400101 print 'FAILURE: %s; Missing expected images' % input_filename
102 return False, results
103
stephanafa05e972017-01-02 06:19:41 -0800104 return True, results
dsinclair2a8a20c2016-04-25 09:46:17 -0700105
Henrique Nakashima15bc9742018-04-26 15:55:07 +0000106 def RegenerateIfNeeded_(self, input_filename, source_dir):
107 if (not self.options.regenerate_expected
108 or self.test_suppressor.IsResultSuppressed(input_filename)
109 or self.test_suppressor.IsImageDiffSuppressed(input_filename)):
110 return
111
112 platform_only = (self.options.regenerate_expected == 'platform')
113 self.image_differ.Regenerate(input_filename, source_dir,
114 self.working_dir, platform_only)
115
dsinclair2a8a20c2016-04-25 09:46:17 -0700116 def Generate(self, source_dir, input_filename, input_root, pdf_path):
117 original_path = os.path.join(source_dir, input_filename)
118 input_path = os.path.join(source_dir, input_root + '.in')
119
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400120 input_event_path = os.path.join(source_dir, input_root + '.evt')
dsinclair849284d2016-05-17 06:13:36 -0700121 if os.path.exists(input_event_path):
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400122 output_event_path = os.path.splitext(pdf_path)[0] + '.evt'
dsinclair849284d2016-05-17 06:13:36 -0700123 shutil.copyfile(input_event_path, output_event_path)
124
dsinclair2a8a20c2016-04-25 09:46:17 -0700125 if not os.path.exists(input_path):
126 if os.path.exists(original_path):
127 shutil.copyfile(original_path, pdf_path)
128 return None
129
130 sys.stdout.flush()
dsinclair849284d2016-05-17 06:13:36 -0700131
dsinclair2a8a20c2016-04-25 09:46:17 -0700132 return common.RunCommand(
133 [sys.executable, self.fixup_path, '--output-dir=' + self.working_dir,
134 input_path])
135
dsinclair2a8a20c2016-04-25 09:46:17 -0700136 def TestText(self, input_root, expected_txt_path, pdf_path):
137 txt_path = os.path.join(self.working_dir, input_root + '.txt')
138
139 with open(txt_path, 'w') as outfile:
Ryan Harrison70cca362018-08-10 18:55:46 +0000140 cmd_to_run = [self.pdfium_test_path, '--send-events',
141 '--time=' + TEST_SEED_TIME, pdf_path]
dsinclair2a8a20c2016-04-25 09:46:17 -0700142 subprocess.check_call(cmd_to_run, stdout=outfile)
143
144 cmd = [sys.executable, self.text_diff_path, expected_txt_path, txt_path]
145 return common.RunCommand(cmd)
146
Ryan Harrison1118a662018-05-31 19:26:52 +0000147 def TestPixel(self, input_root, pdf_path, use_ahem):
Ryan Harrison70cca362018-08-10 18:55:46 +0000148 cmd_to_run = [self.pdfium_test_path, '--send-events', '--png', '--md5',
149 '--time=' + TEST_SEED_TIME]
Ryan Harrison1118a662018-05-31 19:26:52 +0000150
Dan Sinclairaeadad12017-07-18 16:43:41 -0400151 if self.oneshot_renderer:
152 cmd_to_run.append('--render-oneshot')
Ryan Harrison1118a662018-05-31 19:26:52 +0000153
154 if use_ahem:
155 cmd_to_run.append('--font-dir=%s' % self.font_dir)
156
stephanafa05e972017-01-02 06:19:41 -0800157 cmd_to_run.append(pdf_path)
158 return common.RunCommandExtractHashedFiles(cmd_to_run)
dsinclair2a8a20c2016-04-25 09:46:17 -0700159
160 def HandleResult(self, input_filename, input_path, result):
dan sinclair00d40642017-01-30 19:48:54 -0800161 success, image_paths = result
Henrique Nakashimaea4a56d2017-11-29 19:34:19 +0000162
163 if image_paths:
164 for img_path, md5_hash in image_paths:
165 # The output filename without image extension becomes the test name.
166 # For example, "/path/to/.../testing/corpus/example_005.pdf.0.png"
167 # becomes "example_005.pdf.0".
168 test_name = os.path.splitext(os.path.split(img_path)[1])[0]
169
Stephan Altmuellerbcd66f52018-06-21 14:45:44 +0000170 matched = "suppressed"
Henrique Nakashimaea4a56d2017-11-29 19:34:19 +0000171 if not self.test_suppressor.IsResultSuppressed(input_filename):
172 matched = self.gold_baseline.MatchLocalResult(test_name, md5_hash)
173 if matched == gold.GoldBaseline.MISMATCH:
174 print 'Skia Gold hash mismatch for test case: %s' % test_name
175 elif matched == gold.GoldBaseline.NO_BASELINE:
176 print 'No Skia Gold baseline found for test case: %s' % test_name
177
178 if self.gold_results:
Stephan Altmuellerbcd66f52018-06-21 14:45:44 +0000179 self.gold_results.AddTestResult(test_name, md5_hash, img_path, matched)
stephanafa05e972017-01-02 06:19:41 -0800180
dsinclair2a8a20c2016-04-25 09:46:17 -0700181 if self.test_suppressor.IsResultSuppressed(input_filename):
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400182 self.result_suppressed_cases.append(input_filename)
dan sinclair00d40642017-01-30 19:48:54 -0800183 if success:
dsinclair2a8a20c2016-04-25 09:46:17 -0700184 self.surprises.append(input_path)
185 else:
dan sinclair00d40642017-01-30 19:48:54 -0800186 if not success:
dsinclair2a8a20c2016-04-25 09:46:17 -0700187 self.failures.append(input_path)
188
dsinclair2a8a20c2016-04-25 09:46:17 -0700189 def Run(self):
190 parser = optparse.OptionParser()
stephanafa05e972017-01-02 06:19:41 -0800191
dsinclair2a8a20c2016-04-25 09:46:17 -0700192 parser.add_option('--build-dir', default=os.path.join('out', 'Debug'),
193 help='relative path from the base source directory')
stephanafa05e972017-01-02 06:19:41 -0800194
dsinclair849284d2016-05-17 06:13:36 -0700195 parser.add_option('-j', default=multiprocessing.cpu_count(),
dsinclair2a8a20c2016-04-25 09:46:17 -0700196 dest='num_workers', type='int',
197 help='run NUM_WORKERS jobs in parallel')
stephanafa05e972017-01-02 06:19:41 -0800198
stephanafa05e972017-01-02 06:19:41 -0800199 parser.add_option('--gold_properties', default='', dest="gold_properties",
Henrique Nakashima352e2512017-10-26 11:22:52 -0400200 help='Key value pairs that are written to the top level '
201 'of the JSON file that is ingested by Gold.')
stephanafa05e972017-01-02 06:19:41 -0800202
203 parser.add_option('--gold_key', default='', dest="gold_key",
Henrique Nakashima352e2512017-10-26 11:22:52 -0400204 help='Key value pairs that are added to the "key" field '
205 'of the JSON file that is ingested by Gold.')
stephanafa05e972017-01-02 06:19:41 -0800206
207 parser.add_option('--gold_output_dir', default='', dest="gold_output_dir",
Henrique Nakashima352e2512017-10-26 11:22:52 -0400208 help='Path of where to write the JSON output to be '
209 'uploaded to Gold.')
stephanafa05e972017-01-02 06:19:41 -0800210
Henrique Nakashima352e2512017-10-26 11:22:52 -0400211 parser.add_option('--gold_ignore_hashes', default='',
212 dest="gold_ignore_hashes",
stephanad5320362017-01-26 15:18:54 -0800213 help='Path to a file with MD5 hashes we wish to ignore.')
214
Henrique Nakashima352e2512017-10-26 11:22:52 -0400215 parser.add_option('--regenerate_expected', default='',
216 dest="regenerate_expected",
217 help='Regenerates expected images. Valid values are '
218 '"all" to regenerate all expected pngs, and '
219 '"platform" to regenerate only platform-specific '
220 'expected pngs.')
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400221
Henrique Nakashima352e2512017-10-26 11:22:52 -0400222 parser.add_option('--ignore_errors', action="store_true",
223 dest="ignore_errors",
224 help='Prevents the return value from being non-zero '
225 'when image comparison fails.')
stephanafa05e972017-01-02 06:19:41 -0800226
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400227 self.options, self.args = parser.parse_args()
dsinclair2a8a20c2016-04-25 09:46:17 -0700228
Henrique Nakashima352e2512017-10-26 11:22:52 -0400229 if (self.options.regenerate_expected
230 and self.options.regenerate_expected not in ['all', 'platform']) :
231 print 'FAILURE: --regenerate_expected must be "all" or "platform"'
232 return 1
233
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400234 finder = common.DirectoryFinder(self.options.build_dir)
dsinclair2a8a20c2016-04-25 09:46:17 -0700235 self.fixup_path = finder.ScriptPath('fixup_pdf_template.py')
236 self.text_diff_path = finder.ScriptPath('text_diff.py')
Ryan Harrison1118a662018-05-31 19:26:52 +0000237 self.font_dir = os.path.join(finder.TestingDir(), 'resources', 'fonts')
dsinclair2a8a20c2016-04-25 09:46:17 -0700238
dsinclair2a8a20c2016-04-25 09:46:17 -0700239 self.source_dir = finder.TestingDir()
dsinclair849284d2016-05-17 06:13:36 -0700240 if self.test_dir != 'corpus':
241 test_dir = finder.TestingDir(os.path.join('resources', self.test_dir))
242 else:
243 test_dir = finder.TestingDir(self.test_dir)
244
dsinclair2a8a20c2016-04-25 09:46:17 -0700245 self.pdfium_test_path = finder.ExecutablePath('pdfium_test')
246 if not os.path.exists(self.pdfium_test_path):
247 print "FAILURE: Can't find test executable '%s'" % self.pdfium_test_path
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400248 print 'Use --build-dir to specify its location.'
dsinclair2a8a20c2016-04-25 09:46:17 -0700249 return 1
250
251 self.working_dir = finder.WorkingDir(os.path.join('testing', self.test_dir))
252 if not os.path.exists(self.working_dir):
253 os.makedirs(self.working_dir)
254
255 self.feature_string = subprocess.check_output([self.pdfium_test_path,
256 '--show-config'])
257 self.test_suppressor = suppressor.Suppressor(finder, self.feature_string)
258 self.image_differ = pngdiffer.PNGDiffer(finder)
Henrique Nakashima40be5052018-10-10 23:18:14 +0000259 error_message = self.image_differ.CheckMissingTools(
260 self.options.regenerate_expected)
261 if error_message:
262 print "FAILURE: %s" % error_message
263 return 1
dsinclair2a8a20c2016-04-25 09:46:17 -0700264
Henrique Nakashimaea4a56d2017-11-29 19:34:19 +0000265 self.gold_baseline = gold.GoldBaseline(self.options.gold_properties)
266
dsinclair2a8a20c2016-04-25 09:46:17 -0700267 walk_from_dir = finder.TestingDir(test_dir);
268
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400269 self.test_cases = []
270 self.execution_suppressed_cases = []
Henrique Nakashima62d50762017-06-27 13:06:23 -0400271 input_file_re = re.compile('^.+[.](in|pdf)$')
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400272 if self.args:
273 for file_name in self.args:
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400274 file_name.replace('.pdf', '.in')
dsinclair2a8a20c2016-04-25 09:46:17 -0700275 input_path = os.path.join(walk_from_dir, file_name)
276 if not os.path.isfile(input_path):
277 print "Can't find test file '%s'" % file_name
278 return 1
279
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400280 self.test_cases.append((os.path.basename(input_path),
dsinclair2a8a20c2016-04-25 09:46:17 -0700281 os.path.dirname(input_path)))
282 else:
283 for file_dir, _, filename_list in os.walk(walk_from_dir):
284 for input_filename in filename_list:
285 if input_file_re.match(input_filename):
286 input_path = os.path.join(file_dir, input_filename)
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400287 if self.test_suppressor.IsExecutionSuppressed(input_path):
288 self.execution_suppressed_cases.append(input_path)
289 else:
dsinclair2a8a20c2016-04-25 09:46:17 -0700290 if os.path.isfile(input_path):
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400291 self.test_cases.append((input_filename, file_dir))
dsinclair2a8a20c2016-04-25 09:46:17 -0700292
Lei Zhang1ee96012018-04-09 17:31:14 +0000293 self.test_cases.sort()
dsinclair2a8a20c2016-04-25 09:46:17 -0700294 self.failures = []
295 self.surprises = []
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400296 self.result_suppressed_cases = []
dsinclair2a8a20c2016-04-25 09:46:17 -0700297
stephanafa05e972017-01-02 06:19:41 -0800298 # Collect Gold results if an output directory was named.
299 self.gold_results = None
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400300 if self.options.gold_output_dir:
Ryan Harrison80302c72018-05-10 18:27:25 +0000301 self.gold_results = gold.GoldResults(self.test_type,
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400302 self.options.gold_output_dir,
303 self.options.gold_properties,
304 self.options.gold_key,
305 self.options.gold_ignore_hashes)
stephanafa05e972017-01-02 06:19:41 -0800306
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400307 if self.options.num_workers > 1 and len(self.test_cases) > 1:
dsinclair849284d2016-05-17 06:13:36 -0700308 try:
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400309 pool = multiprocessing.Pool(self.options.num_workers)
dsinclair849284d2016-05-17 06:13:36 -0700310 worker_func = functools.partial(TestOneFileParallel, self)
311
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400312 worker_results = pool.imap(worker_func, self.test_cases)
dsinclair849284d2016-05-17 06:13:36 -0700313 for worker_result in worker_results:
314 result, input_filename, source_dir = worker_result
315 input_path = os.path.join(source_dir, input_filename)
316
317 self.HandleResult(input_filename, input_path, result)
318
319 except KeyboardInterrupt:
320 pool.terminate()
321 finally:
322 pool.close()
323 pool.join()
324 else:
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400325 for test_case in self.test_cases:
dsinclair849284d2016-05-17 06:13:36 -0700326 input_filename, input_file_dir = test_case
327 result = self.GenerateAndTest(input_filename, input_file_dir)
328 self.HandleResult(input_filename,
329 os.path.join(input_file_dir, input_filename), result)
dsinclair2a8a20c2016-04-25 09:46:17 -0700330
stephanafa05e972017-01-02 06:19:41 -0800331 if self.gold_results:
332 self.gold_results.WriteResults()
333
dsinclair2a8a20c2016-04-25 09:46:17 -0700334 if self.surprises:
335 self.surprises.sort()
336 print '\n\nUnexpected Successes:'
337 for surprise in self.surprises:
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400338 print surprise
dsinclair2a8a20c2016-04-25 09:46:17 -0700339
340 if self.failures:
341 self.failures.sort()
342 print '\n\nSummary of Failures:'
343 for failure in self.failures:
344 print failure
dan sinclair00d40642017-01-30 19:48:54 -0800345
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400346 self._PrintSummary()
347
348 if self.failures:
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400349 if not self.options.ignore_errors:
dan sinclair00d40642017-01-30 19:48:54 -0800350 return 1
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400351
dsinclair2a8a20c2016-04-25 09:46:17 -0700352 return 0
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400353
354 def _PrintSummary(self):
355 number_test_cases = len(self.test_cases)
356 number_failures = len(self.failures)
357 number_suppressed = len(self.result_suppressed_cases)
358 number_successes = number_test_cases - number_failures - number_suppressed
359 number_surprises = len(self.surprises)
360 print
361 print 'Test cases executed: %d' % number_test_cases
362 print ' Successes: %d' % number_successes
363 print ' Suppressed: %d' % number_suppressed
364 print ' Surprises: %d' % number_surprises
365 print ' Failures: %d' % number_failures
366 print
367 print 'Test cases not executed: %d' % len(self.execution_suppressed_cases)
368
369 def SetEnforceExpectedImages(self, new_value):
370 """Set whether to enforce that each test case provide an expected image."""
371 self.enforce_expected_images = new_value
Dan Sinclairaeadad12017-07-18 16:43:41 -0400372
373 def SetOneShotRenderer(self, new_value):
374 """Set whether to use the oneshot renderer. """
375 self.oneshot_renderer = new_value