commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | """ |
| 4 | Copyright 2014 Google Inc. |
| 5 | |
| 6 | Use of this source code is governed by a BSD-style license that can be |
| 7 | found in the LICENSE file. |
| 8 | |
| 9 | Test the render_pictures binary. |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 10 | """ |
| 11 | |
| 12 | # System-level imports |
| 13 | import json |
| 14 | import os |
| 15 | import shutil |
| 16 | import tempfile |
| 17 | |
| 18 | # Imports from within Skia |
| 19 | import base_unittest |
| 20 | |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 21 | # Maximum length of text diffs to show when tests fail |
| 22 | MAX_DIFF_LENGTH = 30000 |
| 23 | |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 24 | |
| 25 | class RenderPicturesTest(base_unittest.TestCase): |
| 26 | |
| 27 | def setUp(self): |
| 28 | self._temp_dir = tempfile.mkdtemp() |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 29 | self.maxDiff = MAX_DIFF_LENGTH |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 30 | |
| 31 | def tearDown(self): |
| 32 | shutil.rmtree(self._temp_dir) |
| 33 | |
| 34 | def test_tiled_whole_image_no_comparison(self): |
| 35 | """Run render_pictures with tiles and --writeWholeImage flag.""" |
| 36 | input_skp_path = os.path.join(self._temp_dir, 'input.skp') |
| 37 | output_json_path = os.path.join(self._temp_dir, 'output.json') |
| 38 | self._run_skpmaker(['--writePath', input_skp_path]) |
| 39 | self._run_render_pictures(['-r', input_skp_path, |
| 40 | '--bbh', 'grid', '256', '256', |
| 41 | '--mode', 'tile', '256', '256', |
| 42 | '--writeJsonSummaryPath', output_json_path, |
| 43 | '--writeWholeImage']) |
| 44 | expected_summary_dict = { |
| 45 | "actual-results" : { |
| 46 | "no-comparison" : { |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 47 | # Manually verified: 640x400 red rectangle with black border |
commit-bot@chromium.org | 238771c | 2014-01-15 16:33:31 +0000 | [diff] [blame] | 48 | "input.png" : [ "bitmap-64bitMD5", 11092453015575919668 ] |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 49 | } |
| 50 | } |
commit-bot@chromium.org | 238771c | 2014-01-15 16:33:31 +0000 | [diff] [blame] | 51 | } |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 52 | self._assert_json_contents(output_json_path, expected_summary_dict) |
| 53 | |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 54 | def test_untiled_no_comparison(self): |
| 55 | """Run without tiles.""" |
| 56 | input_skp_path = os.path.join(self._temp_dir, 'input.skp') |
| 57 | output_json_path = os.path.join(self._temp_dir, 'output.json') |
| 58 | self._run_skpmaker(['--writePath', input_skp_path]) |
| 59 | self._run_render_pictures(['-r', input_skp_path, |
| 60 | '--writePath', self._temp_dir, |
| 61 | '--writeJsonSummaryPath', output_json_path]) |
| 62 | expected_summary_dict = { |
| 63 | "actual-results" : { |
| 64 | "no-comparison" : { |
| 65 | # Manually verified: 640x400 red rectangle with black border |
| 66 | "input.png" : ["bitmap-64bitMD5", 11092453015575919668], |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | self._assert_json_contents(output_json_path, expected_summary_dict) |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 71 | |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 72 | def test_validate(self): |
| 73 | """Same as test_untiled_no_comparison, but with --validate. |
| 74 | |
| 75 | TODO(epoger): This test generates undesired results! The call |
| 76 | to render_pictures should succeed, and generate the same output as |
| 77 | test_untiled_no_comparison. |
| 78 | See https://code.google.com/p/skia/issues/detail?id=2044 ('render_pictures: |
| 79 | --validate fails') |
| 80 | """ |
| 81 | input_skp_path = os.path.join(self._temp_dir, 'input.skp') |
| 82 | output_json_path = os.path.join(self._temp_dir, 'output.json') |
| 83 | self._run_skpmaker(['--writePath', input_skp_path]) |
| 84 | with self.assertRaises(Exception): |
| 85 | self._run_render_pictures(['-r', input_skp_path, |
| 86 | '--validate', |
| 87 | '--writePath', self._temp_dir, |
| 88 | '--writeJsonSummaryPath', output_json_path]) |
| 89 | |
| 90 | def test_without_writePath(self): |
| 91 | """Same as test_untiled_no_comparison, but without --writePath. |
| 92 | |
| 93 | TODO(epoger): This test generates undesired results! |
| 94 | See https://code.google.com/p/skia/issues/detail?id=2043 ('render_pictures: |
| 95 | --writeJsonSummaryPath fails unless --writePath is specified') |
| 96 | """ |
| 97 | input_skp_path = os.path.join(self._temp_dir, 'input.skp') |
| 98 | output_json_path = os.path.join(self._temp_dir, 'output.json') |
| 99 | self._run_skpmaker(['--writePath', input_skp_path]) |
| 100 | self._run_render_pictures(['-r', input_skp_path, |
| 101 | '--writeJsonSummaryPath', output_json_path]) |
| 102 | expected_summary_dict = { |
| 103 | "actual-results" : { |
| 104 | "no-comparison" : None, |
| 105 | } |
| 106 | } |
| 107 | self._assert_json_contents(output_json_path, expected_summary_dict) |
| 108 | |
| 109 | def test_tiled_no_comparison(self): |
| 110 | """Generate individual tiles.""" |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 111 | input_skp_path = os.path.join(self._temp_dir, 'input.skp') |
| 112 | output_json_path = os.path.join(self._temp_dir, 'output.json') |
| 113 | self._run_skpmaker(['--writePath', input_skp_path]) |
| 114 | self._run_render_pictures(['-r', input_skp_path, |
| 115 | '--bbh', 'grid', '256', '256', |
| 116 | '--mode', 'tile', '256', '256', |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 117 | '--writePath', self._temp_dir, |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 118 | '--writeJsonSummaryPath', output_json_path]) |
| 119 | expected_summary_dict = { |
| 120 | "actual-results" : { |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 121 | "no-comparison" : { |
| 122 | # Manually verified these 6 images, all 256x256 tiles, |
| 123 | # consistent with a tiled version of the 640x400 red rect |
| 124 | # with black borders. |
| 125 | "input0.png" : ["bitmap-64bitMD5", 5815827069051002745], |
| 126 | "input1.png" : ["bitmap-64bitMD5", 9323613075234140270], |
| 127 | "input2.png" : ["bitmap-64bitMD5", 16670399404877552232], |
| 128 | "input3.png" : ["bitmap-64bitMD5", 2507897274083364964], |
| 129 | "input4.png" : ["bitmap-64bitMD5", 7325267995523877959], |
| 130 | "input5.png" : ["bitmap-64bitMD5", 2181381724594493116], |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 133 | } |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 134 | self._assert_json_contents(output_json_path, expected_summary_dict) |
| 135 | |
| 136 | def _run_render_pictures(self, args): |
| 137 | binary = self.find_path_to_program('render_pictures') |
| 138 | return self.run_command([binary, |
| 139 | '--clone', '1', |
| 140 | '--config', '8888', |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 141 | ] + args) |
| 142 | |
| 143 | def _run_skpmaker(self, args): |
| 144 | binary = self.find_path_to_program('skpmaker') |
| 145 | return self.run_command([binary, |
| 146 | '--red', '255', |
| 147 | '--green', '0', |
| 148 | '--blue', '0', |
| 149 | '--width', '640', |
| 150 | '--height', '400', |
| 151 | ] + args) |
| 152 | |
| 153 | def _assert_json_contents(self, json_path, expected_dict): |
| 154 | """Asserts that contents of a JSON file are identical to expected_dict. |
| 155 | |
| 156 | Args: |
| 157 | json_path: Path to a JSON file. |
| 158 | expected_dict: Dictionary indicating the expected contents of the JSON |
| 159 | file. |
| 160 | |
| 161 | Raises: |
| 162 | AssertionError: contents of the JSON file are not identical to |
| 163 | expected_dict. |
| 164 | """ |
| 165 | file_contents = open(json_path, 'r').read() |
| 166 | actual_dict = json.loads(file_contents) |
| 167 | self.assertEqual(actual_dict, expected_dict) |
| 168 | |
| 169 | |
| 170 | def main(): |
| 171 | base_unittest.main(RenderPicturesTest) |
| 172 | |
| 173 | |
| 174 | if __name__ == '__main__': |
| 175 | main() |