blob: 99ba8758e094ca94cc1c8fff43b8597dd7e80653 [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 Nakashima06673ed2017-10-25 17:31:13 -040083 if (self.options.regenerate_expected
84 and not self.test_suppressor.IsResultSuppressed(input_filename)
85 and not self.test_suppressor.IsImageDiffSuppressed(input_filename)):
Henrique Nakashima352e2512017-10-26 11:22:52 -040086 platform_only = (self.options.regenerate_expected == 'platform')
Henrique Nakashima06673ed2017-10-25 17:31:13 -040087 self.image_differ.Regenerate(input_filename, source_dir,
Henrique Nakashima352e2512017-10-26 11:22:52 -040088 self.working_dir, platform_only)
stephanafa05e972017-01-02 06:19:41 -080089 return False, results
Henrique Nakashima3bcabf32017-06-27 09:48:24 -040090 else:
91 if (self.enforce_expected_images
Henrique Nakashima6fac27d2017-06-27 15:52:45 -040092 and not self.test_suppressor.IsImageDiffSuppressed(input_filename)):
Henrique Nakashima3bcabf32017-06-27 09:48:24 -040093 print 'FAILURE: %s; Missing expected images' % input_filename
94 return False, results
95
stephanafa05e972017-01-02 06:19:41 -080096 return True, results
dsinclair2a8a20c2016-04-25 09:46:17 -070097
98 def Generate(self, source_dir, input_filename, input_root, pdf_path):
99 original_path = os.path.join(source_dir, input_filename)
100 input_path = os.path.join(source_dir, input_root + '.in')
101
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400102 input_event_path = os.path.join(source_dir, input_root + '.evt')
dsinclair849284d2016-05-17 06:13:36 -0700103 if os.path.exists(input_event_path):
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400104 output_event_path = os.path.splitext(pdf_path)[0] + '.evt'
dsinclair849284d2016-05-17 06:13:36 -0700105 shutil.copyfile(input_event_path, output_event_path)
106
dsinclair2a8a20c2016-04-25 09:46:17 -0700107 if not os.path.exists(input_path):
108 if os.path.exists(original_path):
109 shutil.copyfile(original_path, pdf_path)
110 return None
111
112 sys.stdout.flush()
dsinclair849284d2016-05-17 06:13:36 -0700113
dsinclair2a8a20c2016-04-25 09:46:17 -0700114 return common.RunCommand(
115 [sys.executable, self.fixup_path, '--output-dir=' + self.working_dir,
116 input_path])
117
dsinclair2a8a20c2016-04-25 09:46:17 -0700118 def TestText(self, input_root, expected_txt_path, pdf_path):
119 txt_path = os.path.join(self.working_dir, input_root + '.txt')
120
121 with open(txt_path, 'w') as outfile:
Lei Zhang63b01262017-08-31 08:54:46 -0700122 cmd_to_run = [self.pdfium_test_path, '--send-events', pdf_path]
dsinclair2a8a20c2016-04-25 09:46:17 -0700123 subprocess.check_call(cmd_to_run, stdout=outfile)
124
125 cmd = [sys.executable, self.text_diff_path, expected_txt_path, txt_path]
126 return common.RunCommand(cmd)
127
dsinclair2a8a20c2016-04-25 09:46:17 -0700128 def TestPixel(self, input_root, pdf_path):
Henrique Nakashimaea4a56d2017-11-29 19:34:19 +0000129 cmd_to_run = [self.pdfium_test_path, '--send-events', '--png', '--md5']
Dan Sinclairaeadad12017-07-18 16:43:41 -0400130 if self.oneshot_renderer:
131 cmd_to_run.append('--render-oneshot')
stephanafa05e972017-01-02 06:19:41 -0800132 cmd_to_run.append(pdf_path)
133 return common.RunCommandExtractHashedFiles(cmd_to_run)
dsinclair2a8a20c2016-04-25 09:46:17 -0700134
135 def HandleResult(self, input_filename, input_path, result):
dan sinclair00d40642017-01-30 19:48:54 -0800136 success, image_paths = result
Henrique Nakashimaea4a56d2017-11-29 19:34:19 +0000137
138 if image_paths:
139 for img_path, md5_hash in image_paths:
140 # The output filename without image extension becomes the test name.
141 # For example, "/path/to/.../testing/corpus/example_005.pdf.0.png"
142 # becomes "example_005.pdf.0".
143 test_name = os.path.splitext(os.path.split(img_path)[1])[0]
144
145 if not self.test_suppressor.IsResultSuppressed(input_filename):
146 matched = self.gold_baseline.MatchLocalResult(test_name, md5_hash)
147 if matched == gold.GoldBaseline.MISMATCH:
148 print 'Skia Gold hash mismatch for test case: %s' % test_name
149 elif matched == gold.GoldBaseline.NO_BASELINE:
150 print 'No Skia Gold baseline found for test case: %s' % test_name
151
152 if self.gold_results:
stephana38c27052017-01-13 13:16:40 -0800153 self.gold_results.AddTestResult(test_name, md5_hash, img_path)
stephanafa05e972017-01-02 06:19:41 -0800154
dsinclair2a8a20c2016-04-25 09:46:17 -0700155 if self.test_suppressor.IsResultSuppressed(input_filename):
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400156 self.result_suppressed_cases.append(input_filename)
dan sinclair00d40642017-01-30 19:48:54 -0800157 if success:
dsinclair2a8a20c2016-04-25 09:46:17 -0700158 self.surprises.append(input_path)
159 else:
dan sinclair00d40642017-01-30 19:48:54 -0800160 if not success:
dsinclair2a8a20c2016-04-25 09:46:17 -0700161 self.failures.append(input_path)
162
dsinclair2a8a20c2016-04-25 09:46:17 -0700163 def Run(self):
164 parser = optparse.OptionParser()
stephanafa05e972017-01-02 06:19:41 -0800165
dsinclair2a8a20c2016-04-25 09:46:17 -0700166 parser.add_option('--build-dir', default=os.path.join('out', 'Debug'),
167 help='relative path from the base source directory')
stephanafa05e972017-01-02 06:19:41 -0800168
dsinclair849284d2016-05-17 06:13:36 -0700169 parser.add_option('-j', default=multiprocessing.cpu_count(),
dsinclair2a8a20c2016-04-25 09:46:17 -0700170 dest='num_workers', type='int',
171 help='run NUM_WORKERS jobs in parallel')
stephanafa05e972017-01-02 06:19:41 -0800172
stephanafa05e972017-01-02 06:19:41 -0800173 parser.add_option('--gold_properties', default='', dest="gold_properties",
Henrique Nakashima352e2512017-10-26 11:22:52 -0400174 help='Key value pairs that are written to the top level '
175 'of the JSON file that is ingested by Gold.')
stephanafa05e972017-01-02 06:19:41 -0800176
177 parser.add_option('--gold_key', default='', dest="gold_key",
Henrique Nakashima352e2512017-10-26 11:22:52 -0400178 help='Key value pairs that are added to the "key" field '
179 'of the JSON file that is ingested by Gold.')
stephanafa05e972017-01-02 06:19:41 -0800180
181 parser.add_option('--gold_output_dir', default='', dest="gold_output_dir",
Henrique Nakashima352e2512017-10-26 11:22:52 -0400182 help='Path of where to write the JSON output to be '
183 'uploaded to Gold.')
stephanafa05e972017-01-02 06:19:41 -0800184
Henrique Nakashima352e2512017-10-26 11:22:52 -0400185 parser.add_option('--gold_ignore_hashes', default='',
186 dest="gold_ignore_hashes",
stephanad5320362017-01-26 15:18:54 -0800187 help='Path to a file with MD5 hashes we wish to ignore.')
188
Henrique Nakashima352e2512017-10-26 11:22:52 -0400189 parser.add_option('--regenerate_expected', default='',
190 dest="regenerate_expected",
191 help='Regenerates expected images. Valid values are '
192 '"all" to regenerate all expected pngs, and '
193 '"platform" to regenerate only platform-specific '
194 'expected pngs.')
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400195
Henrique Nakashima352e2512017-10-26 11:22:52 -0400196 parser.add_option('--ignore_errors', action="store_true",
197 dest="ignore_errors",
198 help='Prevents the return value from being non-zero '
199 'when image comparison fails.')
stephanafa05e972017-01-02 06:19:41 -0800200
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400201 self.options, self.args = parser.parse_args()
dsinclair2a8a20c2016-04-25 09:46:17 -0700202
Henrique Nakashima352e2512017-10-26 11:22:52 -0400203 if (self.options.regenerate_expected
204 and self.options.regenerate_expected not in ['all', 'platform']) :
205 print 'FAILURE: --regenerate_expected must be "all" or "platform"'
206 return 1
207
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400208 finder = common.DirectoryFinder(self.options.build_dir)
dsinclair2a8a20c2016-04-25 09:46:17 -0700209 self.fixup_path = finder.ScriptPath('fixup_pdf_template.py')
210 self.text_diff_path = finder.ScriptPath('text_diff.py')
211
dsinclair2a8a20c2016-04-25 09:46:17 -0700212 self.source_dir = finder.TestingDir()
dsinclair849284d2016-05-17 06:13:36 -0700213 if self.test_dir != 'corpus':
214 test_dir = finder.TestingDir(os.path.join('resources', self.test_dir))
215 else:
216 test_dir = finder.TestingDir(self.test_dir)
217
dsinclair2a8a20c2016-04-25 09:46:17 -0700218 self.pdfium_test_path = finder.ExecutablePath('pdfium_test')
219 if not os.path.exists(self.pdfium_test_path):
220 print "FAILURE: Can't find test executable '%s'" % self.pdfium_test_path
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400221 print 'Use --build-dir to specify its location.'
dsinclair2a8a20c2016-04-25 09:46:17 -0700222 return 1
223
224 self.working_dir = finder.WorkingDir(os.path.join('testing', self.test_dir))
225 if not os.path.exists(self.working_dir):
226 os.makedirs(self.working_dir)
227
228 self.feature_string = subprocess.check_output([self.pdfium_test_path,
229 '--show-config'])
230 self.test_suppressor = suppressor.Suppressor(finder, self.feature_string)
231 self.image_differ = pngdiffer.PNGDiffer(finder)
232
Henrique Nakashimaea4a56d2017-11-29 19:34:19 +0000233 self.gold_baseline = gold.GoldBaseline(self.options.gold_properties)
234
dsinclair2a8a20c2016-04-25 09:46:17 -0700235 walk_from_dir = finder.TestingDir(test_dir);
236
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400237 self.test_cases = []
238 self.execution_suppressed_cases = []
Henrique Nakashima62d50762017-06-27 13:06:23 -0400239 input_file_re = re.compile('^.+[.](in|pdf)$')
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400240 if self.args:
241 for file_name in self.args:
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400242 file_name.replace('.pdf', '.in')
dsinclair2a8a20c2016-04-25 09:46:17 -0700243 input_path = os.path.join(walk_from_dir, file_name)
244 if not os.path.isfile(input_path):
245 print "Can't find test file '%s'" % file_name
246 return 1
247
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400248 self.test_cases.append((os.path.basename(input_path),
dsinclair2a8a20c2016-04-25 09:46:17 -0700249 os.path.dirname(input_path)))
250 else:
251 for file_dir, _, filename_list in os.walk(walk_from_dir):
252 for input_filename in filename_list:
253 if input_file_re.match(input_filename):
254 input_path = os.path.join(file_dir, input_filename)
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400255 if self.test_suppressor.IsExecutionSuppressed(input_path):
256 self.execution_suppressed_cases.append(input_path)
257 else:
dsinclair2a8a20c2016-04-25 09:46:17 -0700258 if os.path.isfile(input_path):
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400259 self.test_cases.append((input_filename, file_dir))
dsinclair2a8a20c2016-04-25 09:46:17 -0700260
261 self.failures = []
262 self.surprises = []
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400263 self.result_suppressed_cases = []
dsinclair2a8a20c2016-04-25 09:46:17 -0700264
stephanafa05e972017-01-02 06:19:41 -0800265 # Collect Gold results if an output directory was named.
266 self.gold_results = None
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400267 if self.options.gold_output_dir:
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400268 self.gold_results = gold.GoldResults('pdfium',
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400269 self.options.gold_output_dir,
270 self.options.gold_properties,
271 self.options.gold_key,
272 self.options.gold_ignore_hashes)
stephanafa05e972017-01-02 06:19:41 -0800273
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400274 if self.options.num_workers > 1 and len(self.test_cases) > 1:
dsinclair849284d2016-05-17 06:13:36 -0700275 try:
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400276 pool = multiprocessing.Pool(self.options.num_workers)
dsinclair849284d2016-05-17 06:13:36 -0700277 worker_func = functools.partial(TestOneFileParallel, self)
278
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400279 worker_results = pool.imap(worker_func, self.test_cases)
dsinclair849284d2016-05-17 06:13:36 -0700280 for worker_result in worker_results:
281 result, input_filename, source_dir = worker_result
282 input_path = os.path.join(source_dir, input_filename)
283
284 self.HandleResult(input_filename, input_path, result)
285
286 except KeyboardInterrupt:
287 pool.terminate()
288 finally:
289 pool.close()
290 pool.join()
291 else:
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400292 for test_case in self.test_cases:
dsinclair849284d2016-05-17 06:13:36 -0700293 input_filename, input_file_dir = test_case
294 result = self.GenerateAndTest(input_filename, input_file_dir)
295 self.HandleResult(input_filename,
296 os.path.join(input_file_dir, input_filename), result)
dsinclair2a8a20c2016-04-25 09:46:17 -0700297
stephanafa05e972017-01-02 06:19:41 -0800298 if self.gold_results:
299 self.gold_results.WriteResults()
300
dsinclair2a8a20c2016-04-25 09:46:17 -0700301 if self.surprises:
302 self.surprises.sort()
303 print '\n\nUnexpected Successes:'
304 for surprise in self.surprises:
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400305 print surprise
dsinclair2a8a20c2016-04-25 09:46:17 -0700306
307 if self.failures:
308 self.failures.sort()
309 print '\n\nSummary of Failures:'
310 for failure in self.failures:
311 print failure
dan sinclair00d40642017-01-30 19:48:54 -0800312
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400313 self._PrintSummary()
314
315 if self.failures:
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400316 if not self.options.ignore_errors:
dan sinclair00d40642017-01-30 19:48:54 -0800317 return 1
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400318
dsinclair2a8a20c2016-04-25 09:46:17 -0700319 return 0
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400320
321 def _PrintSummary(self):
322 number_test_cases = len(self.test_cases)
323 number_failures = len(self.failures)
324 number_suppressed = len(self.result_suppressed_cases)
325 number_successes = number_test_cases - number_failures - number_suppressed
326 number_surprises = len(self.surprises)
327 print
328 print 'Test cases executed: %d' % number_test_cases
329 print ' Successes: %d' % number_successes
330 print ' Suppressed: %d' % number_suppressed
331 print ' Surprises: %d' % number_surprises
332 print ' Failures: %d' % number_failures
333 print
334 print 'Test cases not executed: %d' % len(self.execution_suppressed_cases)
335
336 def SetEnforceExpectedImages(self, new_value):
337 """Set whether to enforce that each test case provide an expected image."""
338 self.enforce_expected_images = new_value
Dan Sinclairaeadad12017-07-18 16:43:41 -0400339
340 def SetOneShotRenderer(self, new_value):
341 """Set whether to use the oneshot renderer. """
342 self.oneshot_renderer = new_value