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