blob: ea8f2b8eafb56503ef0a6e4a84f56e022c91b49e [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 cStringIO
7import functools
8import multiprocessing
dsinclair2a8a20c2016-04-25 09:46:17 -07009import optparse
10import os
11import re
dsinclair849284d2016-05-17 06:13:36 -070012import shutil
dsinclair2a8a20c2016-04-25 09:46:17 -070013import subprocess
14import sys
15
16import common
stephanafa05e972017-01-02 06:19:41 -080017import gold
dsinclair2a8a20c2016-04-25 09:46:17 -070018import pngdiffer
19import suppressor
20
dsinclair849284d2016-05-17 06:13:36 -070021class KeyboardInterruptError(Exception): pass
22
dsinclair2a8a20c2016-04-25 09:46:17 -070023# Nomenclature:
24# x_root - "x"
25# x_filename - "x.ext"
26# x_path - "path/to/a/b/c/x.ext"
27# c_dir - "path/to/a/b/c"
28
dsinclair849284d2016-05-17 06:13:36 -070029def TestOneFileParallel(this, test_case):
30 """Wrapper to call GenerateAndTest() and redirect output to stdout."""
31 try:
32 input_filename, source_dir = test_case
33 result = this.GenerateAndTest(input_filename, source_dir);
34 return (result, input_filename, source_dir)
35 except KeyboardInterrupt:
36 raise KeyboardInterruptError()
37
38
dsinclair2a8a20c2016-04-25 09:46:17 -070039class TestRunner:
40 def __init__(self, dirname):
41 self.test_dir = dirname
Henrique Nakashima3bcabf32017-06-27 09:48:24 -040042 self.enforce_expected_images = False
Dan Sinclairaeadad12017-07-18 16:43:41 -040043 self.oneshot_renderer = False
dsinclair2a8a20c2016-04-25 09:46:17 -070044
stephanafa05e972017-01-02 06:19:41 -080045 # GenerateAndTest returns a tuple <success, outputfiles> where
46 # success is a boolean indicating whether the tests passed comparison
47 # tests and outputfiles is a list tuples:
48 # (path_to_image, md5_hash_of_pixelbuffer)
dsinclair2a8a20c2016-04-25 09:46:17 -070049 def GenerateAndTest(self, input_filename, source_dir):
50 input_root, _ = os.path.splitext(input_filename)
51 expected_txt_path = os.path.join(source_dir, input_root + '_expected.txt')
52
53 pdf_path = os.path.join(self.working_dir, input_root + '.pdf')
54
55 # Remove any existing generated images from previous runs.
56 actual_images = self.image_differ.GetActualFiles(input_filename, source_dir,
57 self.working_dir)
58 for image in actual_images:
59 if os.path.exists(image):
60 os.remove(image)
61
62 sys.stdout.flush()
63
64 raised_exception = self.Generate(source_dir, input_filename, input_root,
65 pdf_path)
66
Henrique Nakashima3bcabf32017-06-27 09:48:24 -040067 if raised_exception is not None:
68 print 'FAILURE: %s; %s' % (input_filename, raised_exception)
stephanafa05e972017-01-02 06:19:41 -080069 return False, []
dsinclair2a8a20c2016-04-25 09:46:17 -070070
stephanafa05e972017-01-02 06:19:41 -080071 results = []
dsinclair2a8a20c2016-04-25 09:46:17 -070072 if os.path.exists(expected_txt_path):
73 raised_exception = self.TestText(input_root, expected_txt_path, pdf_path)
74 else:
stephanafa05e972017-01-02 06:19:41 -080075 raised_exception, results = self.TestPixel(input_root, pdf_path)
dsinclair2a8a20c2016-04-25 09:46:17 -070076
Henrique Nakashima3bcabf32017-06-27 09:48:24 -040077 if raised_exception is not None:
78 print 'FAILURE: %s; %s' % (input_filename, raised_exception)
stephanafa05e972017-01-02 06:19:41 -080079 return False, results
dsinclair2a8a20c2016-04-25 09:46:17 -070080
Henrique Nakashima3bcabf32017-06-27 09:48:24 -040081 if actual_images:
dsinclair2a8a20c2016-04-25 09:46:17 -070082 if self.image_differ.HasDifferences(input_filename, source_dir,
83 self.working_dir):
Henrique Nakashima06673ed2017-10-25 17:31:13 -040084 if (self.options.regenerate_expected
85 and not self.test_suppressor.IsResultSuppressed(input_filename)
86 and not self.test_suppressor.IsImageDiffSuppressed(input_filename)):
87 self.image_differ.Regenerate(input_filename, source_dir,
88 self.working_dir)
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
118
119 def TestText(self, input_root, expected_txt_path, pdf_path):
120 txt_path = os.path.join(self.working_dir, input_root + '.txt')
121
122 with open(txt_path, 'w') as outfile:
Lei Zhang63b01262017-08-31 08:54:46 -0700123 cmd_to_run = [self.pdfium_test_path, '--send-events', pdf_path]
dsinclair2a8a20c2016-04-25 09:46:17 -0700124 subprocess.check_call(cmd_to_run, stdout=outfile)
125
126 cmd = [sys.executable, self.text_diff_path, expected_txt_path, txt_path]
127 return common.RunCommand(cmd)
128
129
130 def TestPixel(self, input_root, pdf_path):
dan sinclair5c19c352017-02-01 09:46:52 -0800131 cmd_to_run = [self.pdfium_test_path, '--send-events', '--png']
stephanafa05e972017-01-02 06:19:41 -0800132 if self.gold_results:
133 cmd_to_run.append('--md5')
Dan Sinclairaeadad12017-07-18 16:43:41 -0400134 if self.oneshot_renderer:
135 cmd_to_run.append('--render-oneshot')
stephanafa05e972017-01-02 06:19:41 -0800136 cmd_to_run.append(pdf_path)
137 return common.RunCommandExtractHashedFiles(cmd_to_run)
dsinclair2a8a20c2016-04-25 09:46:17 -0700138
139 def HandleResult(self, input_filename, input_path, result):
dan sinclair00d40642017-01-30 19:48:54 -0800140 success, image_paths = result
stephanafa05e972017-01-02 06:19:41 -0800141 if self.gold_results:
stephana38c27052017-01-13 13:16:40 -0800142 if image_paths:
143 for img_path, md5_hash in image_paths:
144 # the output filename (without extension becomes the test name)
145 test_name = os.path.splitext(os.path.split(img_path)[1])[0]
146 self.gold_results.AddTestResult(test_name, md5_hash, img_path)
stephanafa05e972017-01-02 06:19:41 -0800147
dsinclair2a8a20c2016-04-25 09:46:17 -0700148 if self.test_suppressor.IsResultSuppressed(input_filename):
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400149 self.result_suppressed_cases.append(input_filename)
dan sinclair00d40642017-01-30 19:48:54 -0800150 if success:
dsinclair2a8a20c2016-04-25 09:46:17 -0700151 self.surprises.append(input_path)
152 else:
dan sinclair00d40642017-01-30 19:48:54 -0800153 if not success:
dsinclair2a8a20c2016-04-25 09:46:17 -0700154 self.failures.append(input_path)
155
156
157 def Run(self):
158 parser = optparse.OptionParser()
stephanafa05e972017-01-02 06:19:41 -0800159
dsinclair2a8a20c2016-04-25 09:46:17 -0700160 parser.add_option('--build-dir', default=os.path.join('out', 'Debug'),
161 help='relative path from the base source directory')
stephanafa05e972017-01-02 06:19:41 -0800162
dsinclair849284d2016-05-17 06:13:36 -0700163 parser.add_option('-j', default=multiprocessing.cpu_count(),
dsinclair2a8a20c2016-04-25 09:46:17 -0700164 dest='num_workers', type='int',
165 help='run NUM_WORKERS jobs in parallel')
stephanafa05e972017-01-02 06:19:41 -0800166
stephanafa05e972017-01-02 06:19:41 -0800167 parser.add_option('--gold_properties', default='', dest="gold_properties",
168 help='Key value pairs that are written to the top level of the JSON file that is ingested by Gold.')
169
170 parser.add_option('--gold_key', default='', dest="gold_key",
171 help='Key value pairs that are added to the "key" field of the JSON file that is ingested by Gold.')
172
173 parser.add_option('--gold_output_dir', default='', dest="gold_output_dir",
174 help='Path of where to write the JSON output to be uploaded to Gold.')
175
stephanad5320362017-01-26 15:18:54 -0800176 parser.add_option('--gold_ignore_hashes', default='', dest="gold_ignore_hashes",
177 help='Path to a file with MD5 hashes we wish to ignore.')
178
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400179 parser.add_option('--regenerate_expected', action="store_true", dest="regenerate_expected",
180 help='Regenerates expected images.')
181
stephanafa05e972017-01-02 06:19:41 -0800182 parser.add_option('--ignore_errors', action="store_true", dest="ignore_errors",
183 help='Prevents the return value from being non-zero when image comparison fails.')
184
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400185 self.options, self.args = parser.parse_args()
dsinclair2a8a20c2016-04-25 09:46:17 -0700186
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400187 finder = common.DirectoryFinder(self.options.build_dir)
dsinclair2a8a20c2016-04-25 09:46:17 -0700188 self.fixup_path = finder.ScriptPath('fixup_pdf_template.py')
189 self.text_diff_path = finder.ScriptPath('text_diff.py')
190
dsinclair2a8a20c2016-04-25 09:46:17 -0700191 self.source_dir = finder.TestingDir()
dsinclair849284d2016-05-17 06:13:36 -0700192 if self.test_dir != 'corpus':
193 test_dir = finder.TestingDir(os.path.join('resources', self.test_dir))
194 else:
195 test_dir = finder.TestingDir(self.test_dir)
196
dsinclair2a8a20c2016-04-25 09:46:17 -0700197 self.pdfium_test_path = finder.ExecutablePath('pdfium_test')
198 if not os.path.exists(self.pdfium_test_path):
199 print "FAILURE: Can't find test executable '%s'" % self.pdfium_test_path
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400200 print 'Use --build-dir to specify its location.'
dsinclair2a8a20c2016-04-25 09:46:17 -0700201 return 1
202
203 self.working_dir = finder.WorkingDir(os.path.join('testing', self.test_dir))
204 if not os.path.exists(self.working_dir):
205 os.makedirs(self.working_dir)
206
207 self.feature_string = subprocess.check_output([self.pdfium_test_path,
208 '--show-config'])
209 self.test_suppressor = suppressor.Suppressor(finder, self.feature_string)
210 self.image_differ = pngdiffer.PNGDiffer(finder)
211
dsinclair2a8a20c2016-04-25 09:46:17 -0700212 walk_from_dir = finder.TestingDir(test_dir);
213
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400214 self.test_cases = []
215 self.execution_suppressed_cases = []
Henrique Nakashima62d50762017-06-27 13:06:23 -0400216 input_file_re = re.compile('^.+[.](in|pdf)$')
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400217 if self.args:
218 for file_name in self.args:
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400219 file_name.replace('.pdf', '.in')
dsinclair2a8a20c2016-04-25 09:46:17 -0700220 input_path = os.path.join(walk_from_dir, file_name)
221 if not os.path.isfile(input_path):
222 print "Can't find test file '%s'" % file_name
223 return 1
224
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400225 self.test_cases.append((os.path.basename(input_path),
dsinclair2a8a20c2016-04-25 09:46:17 -0700226 os.path.dirname(input_path)))
227 else:
228 for file_dir, _, filename_list in os.walk(walk_from_dir):
229 for input_filename in filename_list:
230 if input_file_re.match(input_filename):
231 input_path = os.path.join(file_dir, input_filename)
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400232 if self.test_suppressor.IsExecutionSuppressed(input_path):
233 self.execution_suppressed_cases.append(input_path)
234 else:
dsinclair2a8a20c2016-04-25 09:46:17 -0700235 if os.path.isfile(input_path):
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400236 self.test_cases.append((input_filename, file_dir))
dsinclair2a8a20c2016-04-25 09:46:17 -0700237
238 self.failures = []
239 self.surprises = []
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400240 self.result_suppressed_cases = []
dsinclair2a8a20c2016-04-25 09:46:17 -0700241
stephanafa05e972017-01-02 06:19:41 -0800242 # Collect Gold results if an output directory was named.
243 self.gold_results = None
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400244 if self.options.gold_output_dir:
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400245 self.gold_results = gold.GoldResults('pdfium',
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400246 self.options.gold_output_dir,
247 self.options.gold_properties,
248 self.options.gold_key,
249 self.options.gold_ignore_hashes)
stephanafa05e972017-01-02 06:19:41 -0800250
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400251 if self.options.num_workers > 1 and len(self.test_cases) > 1:
dsinclair849284d2016-05-17 06:13:36 -0700252 try:
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400253 pool = multiprocessing.Pool(self.options.num_workers)
dsinclair849284d2016-05-17 06:13:36 -0700254 worker_func = functools.partial(TestOneFileParallel, self)
255
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400256 worker_results = pool.imap(worker_func, self.test_cases)
dsinclair849284d2016-05-17 06:13:36 -0700257 for worker_result in worker_results:
258 result, input_filename, source_dir = worker_result
259 input_path = os.path.join(source_dir, input_filename)
260
261 self.HandleResult(input_filename, input_path, result)
262
263 except KeyboardInterrupt:
264 pool.terminate()
265 finally:
266 pool.close()
267 pool.join()
268 else:
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400269 for test_case in self.test_cases:
dsinclair849284d2016-05-17 06:13:36 -0700270 input_filename, input_file_dir = test_case
271 result = self.GenerateAndTest(input_filename, input_file_dir)
272 self.HandleResult(input_filename,
273 os.path.join(input_file_dir, input_filename), result)
dsinclair2a8a20c2016-04-25 09:46:17 -0700274
stephanafa05e972017-01-02 06:19:41 -0800275 if self.gold_results:
276 self.gold_results.WriteResults()
277
dsinclair2a8a20c2016-04-25 09:46:17 -0700278 if self.surprises:
279 self.surprises.sort()
280 print '\n\nUnexpected Successes:'
281 for surprise in self.surprises:
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400282 print surprise
dsinclair2a8a20c2016-04-25 09:46:17 -0700283
284 if self.failures:
285 self.failures.sort()
286 print '\n\nSummary of Failures:'
287 for failure in self.failures:
288 print failure
dan sinclair00d40642017-01-30 19:48:54 -0800289
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400290 self._PrintSummary()
291
292 if self.failures:
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400293 if not self.options.ignore_errors:
dan sinclair00d40642017-01-30 19:48:54 -0800294 return 1
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400295
dsinclair2a8a20c2016-04-25 09:46:17 -0700296 return 0
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400297
298 def _PrintSummary(self):
299 number_test_cases = len(self.test_cases)
300 number_failures = len(self.failures)
301 number_suppressed = len(self.result_suppressed_cases)
302 number_successes = number_test_cases - number_failures - number_suppressed
303 number_surprises = len(self.surprises)
304 print
305 print 'Test cases executed: %d' % number_test_cases
306 print ' Successes: %d' % number_successes
307 print ' Suppressed: %d' % number_suppressed
308 print ' Surprises: %d' % number_surprises
309 print ' Failures: %d' % number_failures
310 print
311 print 'Test cases not executed: %d' % len(self.execution_suppressed_cases)
312
313 def SetEnforceExpectedImages(self, new_value):
314 """Set whether to enforce that each test case provide an expected image."""
315 self.enforce_expected_images = new_value
Dan Sinclairaeadad12017-07-18 16:43:41 -0400316
317 def SetOneShotRenderer(self, new_value):
318 """Set whether to use the oneshot renderer. """
319 self.oneshot_renderer = new_value