dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 1 | #!/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 | |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 6 | import functools |
| 7 | import multiprocessing |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 8 | import optparse |
| 9 | import os |
| 10 | import re |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 11 | import shutil |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 12 | import subprocess |
| 13 | import sys |
| 14 | |
| 15 | import common |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 16 | import gold |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 17 | import pngdiffer |
| 18 | import suppressor |
| 19 | |
Ryan Harrison | 70cca36 | 2018-08-10 18:55:46 +0000 | [diff] [blame] | 20 | # 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. |
| 23 | TEST_SEED_TIME = "1399672130" |
| 24 | |
Lei Zhang | 3054337 | 2019-11-19 19:02:30 +0000 | [diff] [blame] | 25 | |
| 26 | class KeyboardInterruptError(Exception): |
| 27 | pass |
| 28 | |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 29 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 30 | # Nomenclature: |
| 31 | # x_root - "x" |
| 32 | # x_filename - "x.ext" |
| 33 | # x_path - "path/to/a/b/c/x.ext" |
| 34 | # c_dir - "path/to/a/b/c" |
| 35 | |
Lei Zhang | 3054337 | 2019-11-19 19:02:30 +0000 | [diff] [blame] | 36 | |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 37 | def TestOneFileParallel(this, test_case): |
| 38 | """Wrapper to call GenerateAndTest() and redirect output to stdout.""" |
| 39 | try: |
| 40 | input_filename, source_dir = test_case |
Lei Zhang | 3054337 | 2019-11-19 19:02:30 +0000 | [diff] [blame] | 41 | result = this.GenerateAndTest(input_filename, source_dir) |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 42 | return (result, input_filename, source_dir) |
| 43 | except KeyboardInterrupt: |
| 44 | raise KeyboardInterruptError() |
| 45 | |
| 46 | |
Lei Zhang | 5767aca | 2018-12-05 19:57:46 +0000 | [diff] [blame] | 47 | def DeleteFiles(files): |
| 48 | """Utility function to delete a list of files""" |
| 49 | for f in files: |
| 50 | if os.path.exists(f): |
| 51 | os.remove(f) |
| 52 | |
| 53 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 54 | class TestRunner: |
Lei Zhang | 3054337 | 2019-11-19 19:02:30 +0000 | [diff] [blame] | 55 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 56 | def __init__(self, dirname): |
Ryan Harrison | 80302c7 | 2018-05-10 18:27:25 +0000 | [diff] [blame] | 57 | # Currently the only used directories are corpus, javascript, and pixel, |
| 58 | # which all correspond directly to the type for the test being run. In the |
| 59 | # future if there are tests that don't have this clean correspondence, then |
| 60 | # an argument for the type will need to be added. |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 61 | self.test_dir = dirname |
Ryan Harrison | 80302c7 | 2018-05-10 18:27:25 +0000 | [diff] [blame] | 62 | self.test_type = dirname |
Lei Zhang | 5767aca | 2018-12-05 19:57:46 +0000 | [diff] [blame] | 63 | self.delete_output_on_success = False |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 64 | self.enforce_expected_images = False |
Dan Sinclair | aeadad1 | 2017-07-18 16:43:41 -0400 | [diff] [blame] | 65 | self.oneshot_renderer = False |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 66 | |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 67 | # GenerateAndTest returns a tuple <success, outputfiles> where |
| 68 | # success is a boolean indicating whether the tests passed comparison |
| 69 | # tests and outputfiles is a list tuples: |
| 70 | # (path_to_image, md5_hash_of_pixelbuffer) |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 71 | def GenerateAndTest(self, input_filename, source_dir): |
Ryan Harrison | 1118a66 | 2018-05-31 19:26:52 +0000 | [diff] [blame] | 72 | use_ahem = 'use_ahem' in source_dir |
| 73 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 74 | input_root, _ = os.path.splitext(input_filename) |
| 75 | expected_txt_path = os.path.join(source_dir, input_root + '_expected.txt') |
| 76 | |
| 77 | pdf_path = os.path.join(self.working_dir, input_root + '.pdf') |
| 78 | |
| 79 | # Remove any existing generated images from previous runs. |
| 80 | actual_images = self.image_differ.GetActualFiles(input_filename, source_dir, |
| 81 | self.working_dir) |
Lei Zhang | 5767aca | 2018-12-05 19:57:46 +0000 | [diff] [blame] | 82 | DeleteFiles(actual_images) |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 83 | |
| 84 | sys.stdout.flush() |
| 85 | |
| 86 | raised_exception = self.Generate(source_dir, input_filename, input_root, |
| 87 | pdf_path) |
| 88 | |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 89 | if raised_exception is not None: |
| 90 | print 'FAILURE: %s; %s' % (input_filename, raised_exception) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 91 | return False, [] |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 92 | |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 93 | results = [] |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 94 | if os.path.exists(expected_txt_path): |
| 95 | raised_exception = self.TestText(input_root, expected_txt_path, pdf_path) |
| 96 | else: |
Ryan Harrison | 1118a66 | 2018-05-31 19:26:52 +0000 | [diff] [blame] | 97 | raised_exception, results = self.TestPixel(input_root, pdf_path, use_ahem) |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 98 | |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 99 | if raised_exception is not None: |
| 100 | print 'FAILURE: %s; %s' % (input_filename, raised_exception) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 101 | return False, results |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 102 | |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 103 | if actual_images: |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 104 | if self.image_differ.HasDifferences(input_filename, source_dir, |
| 105 | self.working_dir): |
Henrique Nakashima | 15bc974 | 2018-04-26 15:55:07 +0000 | [diff] [blame] | 106 | self.RegenerateIfNeeded_(input_filename, source_dir) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 107 | return False, results |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 108 | else: |
Lei Zhang | 3054337 | 2019-11-19 19:02:30 +0000 | [diff] [blame] | 109 | if (self.enforce_expected_images and |
| 110 | not self.test_suppressor.IsImageDiffSuppressed(input_filename)): |
Henrique Nakashima | 15bc974 | 2018-04-26 15:55:07 +0000 | [diff] [blame] | 111 | self.RegenerateIfNeeded_(input_filename, source_dir) |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 112 | print 'FAILURE: %s; Missing expected images' % input_filename |
| 113 | return False, results |
| 114 | |
Lei Zhang | 5767aca | 2018-12-05 19:57:46 +0000 | [diff] [blame] | 115 | if self.delete_output_on_success: |
| 116 | DeleteFiles(actual_images) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 117 | return True, results |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 118 | |
Henrique Nakashima | 15bc974 | 2018-04-26 15:55:07 +0000 | [diff] [blame] | 119 | def RegenerateIfNeeded_(self, input_filename, source_dir): |
Lei Zhang | 3054337 | 2019-11-19 19:02:30 +0000 | [diff] [blame] | 120 | if (not self.options.regenerate_expected or |
| 121 | self.test_suppressor.IsResultSuppressed(input_filename) or |
| 122 | self.test_suppressor.IsImageDiffSuppressed(input_filename)): |
Henrique Nakashima | 15bc974 | 2018-04-26 15:55:07 +0000 | [diff] [blame] | 123 | return |
| 124 | |
| 125 | platform_only = (self.options.regenerate_expected == 'platform') |
Lei Zhang | 3054337 | 2019-11-19 19:02:30 +0000 | [diff] [blame] | 126 | self.image_differ.Regenerate(input_filename, source_dir, self.working_dir, |
| 127 | platform_only) |
Henrique Nakashima | 15bc974 | 2018-04-26 15:55:07 +0000 | [diff] [blame] | 128 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 129 | def Generate(self, source_dir, input_filename, input_root, pdf_path): |
| 130 | original_path = os.path.join(source_dir, input_filename) |
| 131 | input_path = os.path.join(source_dir, input_root + '.in') |
| 132 | |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 133 | input_event_path = os.path.join(source_dir, input_root + '.evt') |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 134 | if os.path.exists(input_event_path): |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 135 | output_event_path = os.path.splitext(pdf_path)[0] + '.evt' |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 136 | shutil.copyfile(input_event_path, output_event_path) |
| 137 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 138 | if not os.path.exists(input_path): |
| 139 | if os.path.exists(original_path): |
| 140 | shutil.copyfile(original_path, pdf_path) |
| 141 | return None |
| 142 | |
| 143 | sys.stdout.flush() |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 144 | |
Lei Zhang | 3054337 | 2019-11-19 19:02:30 +0000 | [diff] [blame] | 145 | return common.RunCommand([ |
| 146 | sys.executable, self.fixup_path, '--output-dir=' + self.working_dir, |
| 147 | input_path |
| 148 | ]) |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 149 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 150 | def TestText(self, input_root, expected_txt_path, pdf_path): |
| 151 | txt_path = os.path.join(self.working_dir, input_root + '.txt') |
| 152 | |
| 153 | with open(txt_path, 'w') as outfile: |
Lei Zhang | 3054337 | 2019-11-19 19:02:30 +0000 | [diff] [blame] | 154 | cmd_to_run = [ |
| 155 | self.pdfium_test_path, '--send-events', '--time=' + TEST_SEED_TIME, |
| 156 | pdf_path |
| 157 | ] |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 158 | subprocess.check_call(cmd_to_run, stdout=outfile) |
| 159 | |
| 160 | cmd = [sys.executable, self.text_diff_path, expected_txt_path, txt_path] |
| 161 | return common.RunCommand(cmd) |
| 162 | |
Ryan Harrison | 1118a66 | 2018-05-31 19:26:52 +0000 | [diff] [blame] | 163 | def TestPixel(self, input_root, pdf_path, use_ahem): |
Lei Zhang | 3054337 | 2019-11-19 19:02:30 +0000 | [diff] [blame] | 164 | cmd_to_run = [ |
| 165 | self.pdfium_test_path, '--send-events', '--png', '--md5', |
| 166 | '--time=' + TEST_SEED_TIME |
| 167 | ] |
Ryan Harrison | 1118a66 | 2018-05-31 19:26:52 +0000 | [diff] [blame] | 168 | |
Dan Sinclair | aeadad1 | 2017-07-18 16:43:41 -0400 | [diff] [blame] | 169 | if self.oneshot_renderer: |
| 170 | cmd_to_run.append('--render-oneshot') |
Ryan Harrison | 1118a66 | 2018-05-31 19:26:52 +0000 | [diff] [blame] | 171 | |
| 172 | if use_ahem: |
| 173 | cmd_to_run.append('--font-dir=%s' % self.font_dir) |
| 174 | |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 175 | cmd_to_run.append(pdf_path) |
| 176 | return common.RunCommandExtractHashedFiles(cmd_to_run) |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 177 | |
| 178 | def HandleResult(self, input_filename, input_path, result): |
dan sinclair | 00d4064 | 2017-01-30 19:48:54 -0800 | [diff] [blame] | 179 | success, image_paths = result |
Henrique Nakashima | ea4a56d | 2017-11-29 19:34:19 +0000 | [diff] [blame] | 180 | |
| 181 | if image_paths: |
| 182 | for img_path, md5_hash in image_paths: |
| 183 | # The output filename without image extension becomes the test name. |
| 184 | # For example, "/path/to/.../testing/corpus/example_005.pdf.0.png" |
| 185 | # becomes "example_005.pdf.0". |
| 186 | test_name = os.path.splitext(os.path.split(img_path)[1])[0] |
| 187 | |
Stephan Altmueller | bcd66f5 | 2018-06-21 14:45:44 +0000 | [diff] [blame] | 188 | matched = "suppressed" |
Henrique Nakashima | ea4a56d | 2017-11-29 19:34:19 +0000 | [diff] [blame] | 189 | if not self.test_suppressor.IsResultSuppressed(input_filename): |
| 190 | matched = self.gold_baseline.MatchLocalResult(test_name, md5_hash) |
| 191 | if matched == gold.GoldBaseline.MISMATCH: |
| 192 | print 'Skia Gold hash mismatch for test case: %s' % test_name |
Lei Zhang | 3054337 | 2019-11-19 19:02:30 +0000 | [diff] [blame] | 193 | elif matched == gold.GoldBaseline.NO_BASELINE: |
Henrique Nakashima | ea4a56d | 2017-11-29 19:34:19 +0000 | [diff] [blame] | 194 | print 'No Skia Gold baseline found for test case: %s' % test_name |
| 195 | |
| 196 | if self.gold_results: |
Lei Zhang | 3054337 | 2019-11-19 19:02:30 +0000 | [diff] [blame] | 197 | self.gold_results.AddTestResult(test_name, md5_hash, img_path, |
| 198 | matched) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 199 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 200 | if self.test_suppressor.IsResultSuppressed(input_filename): |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 201 | self.result_suppressed_cases.append(input_filename) |
dan sinclair | 00d4064 | 2017-01-30 19:48:54 -0800 | [diff] [blame] | 202 | if success: |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 203 | self.surprises.append(input_path) |
| 204 | else: |
dan sinclair | 00d4064 | 2017-01-30 19:48:54 -0800 | [diff] [blame] | 205 | if not success: |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 206 | self.failures.append(input_path) |
| 207 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 208 | def Run(self): |
| 209 | parser = optparse.OptionParser() |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 210 | |
Lei Zhang | 3054337 | 2019-11-19 19:02:30 +0000 | [diff] [blame] | 211 | parser.add_option( |
| 212 | '--build-dir', |
| 213 | default=os.path.join('out', 'Debug'), |
| 214 | help='relative path from the base source directory') |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 215 | |
Lei Zhang | 3054337 | 2019-11-19 19:02:30 +0000 | [diff] [blame] | 216 | parser.add_option( |
| 217 | '-j', |
| 218 | default=multiprocessing.cpu_count(), |
| 219 | dest='num_workers', |
| 220 | type='int', |
| 221 | help='run NUM_WORKERS jobs in parallel') |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 222 | |
Lei Zhang | 3054337 | 2019-11-19 19:02:30 +0000 | [diff] [blame] | 223 | parser.add_option( |
| 224 | '--gold_properties', |
| 225 | default='', |
| 226 | dest="gold_properties", |
| 227 | help='Key value pairs that are written to the top level ' |
| 228 | 'of the JSON file that is ingested by Gold.') |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 229 | |
Lei Zhang | 3054337 | 2019-11-19 19:02:30 +0000 | [diff] [blame] | 230 | parser.add_option( |
| 231 | '--gold_key', |
| 232 | default='', |
| 233 | dest="gold_key", |
| 234 | help='Key value pairs that are added to the "key" field ' |
| 235 | 'of the JSON file that is ingested by Gold.') |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 236 | |
Lei Zhang | 3054337 | 2019-11-19 19:02:30 +0000 | [diff] [blame] | 237 | parser.add_option( |
| 238 | '--gold_output_dir', |
| 239 | default='', |
| 240 | dest="gold_output_dir", |
| 241 | help='Path of where to write the JSON output to be ' |
| 242 | 'uploaded to Gold.') |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 243 | |
Lei Zhang | 3054337 | 2019-11-19 19:02:30 +0000 | [diff] [blame] | 244 | parser.add_option( |
| 245 | '--gold_ignore_hashes', |
| 246 | default='', |
| 247 | dest="gold_ignore_hashes", |
| 248 | help='Path to a file with MD5 hashes we wish to ignore.') |
stephana | d532036 | 2017-01-26 15:18:54 -0800 | [diff] [blame] | 249 | |
Lei Zhang | 3054337 | 2019-11-19 19:02:30 +0000 | [diff] [blame] | 250 | parser.add_option( |
| 251 | '--regenerate_expected', |
| 252 | default='', |
| 253 | dest="regenerate_expected", |
| 254 | help='Regenerates expected images. Valid values are ' |
| 255 | '"all" to regenerate all expected pngs, and ' |
| 256 | '"platform" to regenerate only platform-specific ' |
| 257 | 'expected pngs.') |
Henrique Nakashima | 06673ed | 2017-10-25 17:31:13 -0400 | [diff] [blame] | 258 | |
Lei Zhang | 3054337 | 2019-11-19 19:02:30 +0000 | [diff] [blame] | 259 | parser.add_option( |
| 260 | '--ignore_errors', |
| 261 | action="store_true", |
| 262 | dest="ignore_errors", |
| 263 | help='Prevents the return value from being non-zero ' |
| 264 | 'when image comparison fails.') |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 265 | |
Henrique Nakashima | 06673ed | 2017-10-25 17:31:13 -0400 | [diff] [blame] | 266 | self.options, self.args = parser.parse_args() |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 267 | |
Lei Zhang | 3054337 | 2019-11-19 19:02:30 +0000 | [diff] [blame] | 268 | if (self.options.regenerate_expected and |
| 269 | self.options.regenerate_expected not in ['all', 'platform']): |
Henrique Nakashima | 352e251 | 2017-10-26 11:22:52 -0400 | [diff] [blame] | 270 | print 'FAILURE: --regenerate_expected must be "all" or "platform"' |
| 271 | return 1 |
| 272 | |
Henrique Nakashima | 06673ed | 2017-10-25 17:31:13 -0400 | [diff] [blame] | 273 | finder = common.DirectoryFinder(self.options.build_dir) |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 274 | self.fixup_path = finder.ScriptPath('fixup_pdf_template.py') |
| 275 | self.text_diff_path = finder.ScriptPath('text_diff.py') |
Ryan Harrison | 1118a66 | 2018-05-31 19:26:52 +0000 | [diff] [blame] | 276 | self.font_dir = os.path.join(finder.TestingDir(), 'resources', 'fonts') |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 277 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 278 | self.source_dir = finder.TestingDir() |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 279 | if self.test_dir != 'corpus': |
| 280 | test_dir = finder.TestingDir(os.path.join('resources', self.test_dir)) |
| 281 | else: |
| 282 | test_dir = finder.TestingDir(self.test_dir) |
| 283 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 284 | self.pdfium_test_path = finder.ExecutablePath('pdfium_test') |
| 285 | if not os.path.exists(self.pdfium_test_path): |
| 286 | print "FAILURE: Can't find test executable '%s'" % self.pdfium_test_path |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 287 | print 'Use --build-dir to specify its location.' |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 288 | return 1 |
| 289 | |
| 290 | self.working_dir = finder.WorkingDir(os.path.join('testing', self.test_dir)) |
Lei Zhang | 96eff7c | 2019-09-19 21:08:58 +0000 | [diff] [blame] | 291 | shutil.rmtree(self.working_dir, ignore_errors=True) |
| 292 | os.makedirs(self.working_dir) |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 293 | |
Lei Zhang | 3054337 | 2019-11-19 19:02:30 +0000 | [diff] [blame] | 294 | self.feature_string = subprocess.check_output( |
| 295 | [self.pdfium_test_path, '--show-config']) |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 296 | self.test_suppressor = suppressor.Suppressor(finder, self.feature_string) |
| 297 | self.image_differ = pngdiffer.PNGDiffer(finder) |
Henrique Nakashima | 40be505 | 2018-10-10 23:18:14 +0000 | [diff] [blame] | 298 | error_message = self.image_differ.CheckMissingTools( |
| 299 | self.options.regenerate_expected) |
| 300 | if error_message: |
| 301 | print "FAILURE: %s" % error_message |
| 302 | return 1 |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 303 | |
Henrique Nakashima | ea4a56d | 2017-11-29 19:34:19 +0000 | [diff] [blame] | 304 | self.gold_baseline = gold.GoldBaseline(self.options.gold_properties) |
| 305 | |
Lei Zhang | 3054337 | 2019-11-19 19:02:30 +0000 | [diff] [blame] | 306 | walk_from_dir = finder.TestingDir(test_dir) |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 307 | |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 308 | self.test_cases = [] |
| 309 | self.execution_suppressed_cases = [] |
Henrique Nakashima | 62d5076 | 2017-06-27 13:06:23 -0400 | [diff] [blame] | 310 | input_file_re = re.compile('^.+[.](in|pdf)$') |
Henrique Nakashima | 06673ed | 2017-10-25 17:31:13 -0400 | [diff] [blame] | 311 | if self.args: |
| 312 | for file_name in self.args: |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 313 | file_name.replace('.pdf', '.in') |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 314 | input_path = os.path.join(walk_from_dir, file_name) |
| 315 | if not os.path.isfile(input_path): |
| 316 | print "Can't find test file '%s'" % file_name |
| 317 | return 1 |
| 318 | |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 319 | self.test_cases.append((os.path.basename(input_path), |
Lei Zhang | 3054337 | 2019-11-19 19:02:30 +0000 | [diff] [blame] | 320 | os.path.dirname(input_path))) |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 321 | else: |
| 322 | for file_dir, _, filename_list in os.walk(walk_from_dir): |
| 323 | for input_filename in filename_list: |
| 324 | if input_file_re.match(input_filename): |
| 325 | input_path = os.path.join(file_dir, input_filename) |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 326 | if self.test_suppressor.IsExecutionSuppressed(input_path): |
| 327 | self.execution_suppressed_cases.append(input_path) |
| 328 | else: |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 329 | if os.path.isfile(input_path): |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 330 | self.test_cases.append((input_filename, file_dir)) |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 331 | |
Lei Zhang | 1ee9601 | 2018-04-09 17:31:14 +0000 | [diff] [blame] | 332 | self.test_cases.sort() |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 333 | self.failures = [] |
| 334 | self.surprises = [] |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 335 | self.result_suppressed_cases = [] |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 336 | |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 337 | # Collect Gold results if an output directory was named. |
| 338 | self.gold_results = None |
Henrique Nakashima | 06673ed | 2017-10-25 17:31:13 -0400 | [diff] [blame] | 339 | if self.options.gold_output_dir: |
Lei Zhang | 3054337 | 2019-11-19 19:02:30 +0000 | [diff] [blame] | 340 | self.gold_results = gold.GoldResults( |
| 341 | self.test_type, self.options.gold_output_dir, |
| 342 | self.options.gold_properties, self.options.gold_key, |
| 343 | self.options.gold_ignore_hashes) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 344 | |
Henrique Nakashima | 06673ed | 2017-10-25 17:31:13 -0400 | [diff] [blame] | 345 | if self.options.num_workers > 1 and len(self.test_cases) > 1: |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 346 | try: |
Henrique Nakashima | 06673ed | 2017-10-25 17:31:13 -0400 | [diff] [blame] | 347 | pool = multiprocessing.Pool(self.options.num_workers) |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 348 | worker_func = functools.partial(TestOneFileParallel, self) |
| 349 | |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 350 | worker_results = pool.imap(worker_func, self.test_cases) |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 351 | for worker_result in worker_results: |
| 352 | result, input_filename, source_dir = worker_result |
| 353 | input_path = os.path.join(source_dir, input_filename) |
| 354 | |
| 355 | self.HandleResult(input_filename, input_path, result) |
| 356 | |
| 357 | except KeyboardInterrupt: |
| 358 | pool.terminate() |
| 359 | finally: |
| 360 | pool.close() |
| 361 | pool.join() |
| 362 | else: |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 363 | for test_case in self.test_cases: |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 364 | input_filename, input_file_dir = test_case |
| 365 | result = self.GenerateAndTest(input_filename, input_file_dir) |
| 366 | self.HandleResult(input_filename, |
| 367 | os.path.join(input_file_dir, input_filename), result) |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 368 | |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 369 | if self.gold_results: |
| 370 | self.gold_results.WriteResults() |
| 371 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 372 | if self.surprises: |
| 373 | self.surprises.sort() |
| 374 | print '\n\nUnexpected Successes:' |
| 375 | for surprise in self.surprises: |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 376 | print surprise |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 377 | |
| 378 | if self.failures: |
| 379 | self.failures.sort() |
| 380 | print '\n\nSummary of Failures:' |
| 381 | for failure in self.failures: |
| 382 | print failure |
dan sinclair | 00d4064 | 2017-01-30 19:48:54 -0800 | [diff] [blame] | 383 | |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 384 | self._PrintSummary() |
| 385 | |
| 386 | if self.failures: |
Henrique Nakashima | 06673ed | 2017-10-25 17:31:13 -0400 | [diff] [blame] | 387 | if not self.options.ignore_errors: |
dan sinclair | 00d4064 | 2017-01-30 19:48:54 -0800 | [diff] [blame] | 388 | return 1 |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 389 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 390 | return 0 |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 391 | |
| 392 | def _PrintSummary(self): |
| 393 | number_test_cases = len(self.test_cases) |
| 394 | number_failures = len(self.failures) |
| 395 | number_suppressed = len(self.result_suppressed_cases) |
| 396 | number_successes = number_test_cases - number_failures - number_suppressed |
| 397 | number_surprises = len(self.surprises) |
| 398 | print |
| 399 | print 'Test cases executed: %d' % number_test_cases |
| 400 | print ' Successes: %d' % number_successes |
| 401 | print ' Suppressed: %d' % number_suppressed |
| 402 | print ' Surprises: %d' % number_surprises |
| 403 | print ' Failures: %d' % number_failures |
| 404 | print |
| 405 | print 'Test cases not executed: %d' % len(self.execution_suppressed_cases) |
| 406 | |
Lei Zhang | 5767aca | 2018-12-05 19:57:46 +0000 | [diff] [blame] | 407 | def SetDeleteOutputOnSuccess(self, new_value): |
| 408 | """Set whether to delete generated output if the test passes.""" |
| 409 | self.delete_output_on_success = new_value |
| 410 | |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 411 | def SetEnforceExpectedImages(self, new_value): |
| 412 | """Set whether to enforce that each test case provide an expected image.""" |
| 413 | self.enforce_expected_images = new_value |
Dan Sinclair | aeadad1 | 2017-07-18 16:43:41 -0400 | [diff] [blame] | 414 | |
| 415 | def SetOneShotRenderer(self, new_value): |
| 416 | """Set whether to use the oneshot renderer. """ |
| 417 | self.oneshot_renderer = new_value |