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