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): |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 28 | self._input_skp_dir = tempfile.mkdtemp() |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 29 | self._temp_dir = tempfile.mkdtemp() |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 30 | self.maxDiff = MAX_DIFF_LENGTH |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 31 | |
| 32 | def tearDown(self): |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 33 | shutil.rmtree(self._input_skp_dir) |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 34 | shutil.rmtree(self._temp_dir) |
| 35 | |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 36 | def test_tiled_whole_image(self): |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 37 | """Run render_pictures with tiles and --writeWholeImage flag.""" |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 38 | output_json_path = os.path.join(self._temp_dir, 'output.json') |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 39 | self._generate_skps() |
| 40 | self._run_render_pictures(['-r', self._input_skp_dir, |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 41 | '--bbh', 'grid', '256', '256', |
| 42 | '--mode', 'tile', '256', '256', |
| 43 | '--writeJsonSummaryPath', output_json_path, |
| 44 | '--writeWholeImage']) |
| 45 | expected_summary_dict = { |
| 46 | "actual-results" : { |
| 47 | "no-comparison" : { |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 48 | # Manually verified: 640x400 red rectangle with black border |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 49 | "red.png" : [ "bitmap-64bitMD5", 11092453015575919668 ], |
| 50 | # Manually verified: 640x400 green rectangle with black border |
| 51 | "green.png" : [ "bitmap-64bitMD5", 8891695120562235492 ], |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 52 | } |
| 53 | } |
commit-bot@chromium.org | 238771c | 2014-01-15 16:33:31 +0000 | [diff] [blame] | 54 | } |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 55 | self._assert_json_contents(output_json_path, expected_summary_dict) |
| 56 | |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 57 | def test_untiled(self): |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 58 | """Run without tiles.""" |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 59 | output_json_path = os.path.join(self._temp_dir, 'output.json') |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 60 | self._generate_skps() |
| 61 | self._run_render_pictures(['-r', self._input_skp_dir, |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 62 | '--writePath', self._temp_dir, |
| 63 | '--writeJsonSummaryPath', output_json_path]) |
| 64 | expected_summary_dict = { |
| 65 | "actual-results" : { |
| 66 | "no-comparison" : { |
| 67 | # Manually verified: 640x400 red rectangle with black border |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 68 | "red.png" : ["bitmap-64bitMD5", 11092453015575919668], |
| 69 | # Manually verified: 640x400 green rectangle with black border |
| 70 | "green.png" : ["bitmap-64bitMD5", 8891695120562235492], |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | } |
| 74 | self._assert_json_contents(output_json_path, expected_summary_dict) |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 75 | |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 76 | def test_untiled_validate(self): |
| 77 | """Same as test_untiled, but with --validate. |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 78 | |
| 79 | TODO(epoger): This test generates undesired results! The call |
| 80 | to render_pictures should succeed, and generate the same output as |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 81 | test_untiled. |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 82 | See https://code.google.com/p/skia/issues/detail?id=2044 ('render_pictures: |
| 83 | --validate fails') |
| 84 | """ |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 85 | output_json_path = os.path.join(self._temp_dir, 'output.json') |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 86 | self._generate_skps() |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 87 | with self.assertRaises(Exception): |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 88 | self._run_render_pictures(['-r', self._input_skp_dir, |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 89 | '--validate', |
| 90 | '--writePath', self._temp_dir, |
| 91 | '--writeJsonSummaryPath', output_json_path]) |
| 92 | |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 93 | def test_untiled_without_writePath(self): |
| 94 | """Same as test_untiled, but without --writePath. |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 95 | |
| 96 | TODO(epoger): This test generates undesired results! |
| 97 | See https://code.google.com/p/skia/issues/detail?id=2043 ('render_pictures: |
| 98 | --writeJsonSummaryPath fails unless --writePath is specified') |
| 99 | """ |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 100 | output_json_path = os.path.join(self._temp_dir, 'output.json') |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 101 | self._generate_skps() |
| 102 | self._run_render_pictures(['-r', self._input_skp_dir, |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 103 | '--writeJsonSummaryPath', output_json_path]) |
| 104 | expected_summary_dict = { |
| 105 | "actual-results" : { |
| 106 | "no-comparison" : None, |
| 107 | } |
| 108 | } |
| 109 | self._assert_json_contents(output_json_path, expected_summary_dict) |
| 110 | |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 111 | def test_tiled(self): |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 112 | """Generate individual tiles.""" |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 113 | output_json_path = os.path.join(self._temp_dir, 'output.json') |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 114 | self._generate_skps() |
| 115 | self._run_render_pictures(['-r', self._input_skp_dir, |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 116 | '--bbh', 'grid', '256', '256', |
| 117 | '--mode', 'tile', '256', '256', |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 118 | '--writePath', self._temp_dir, |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 119 | '--writeJsonSummaryPath', output_json_path]) |
| 120 | expected_summary_dict = { |
| 121 | "actual-results" : { |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 122 | "no-comparison" : { |
| 123 | # Manually verified these 6 images, all 256x256 tiles, |
| 124 | # consistent with a tiled version of the 640x400 red rect |
| 125 | # with black borders. |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 126 | "red0.png" : ["bitmap-64bitMD5", 5815827069051002745], |
| 127 | "red1.png" : ["bitmap-64bitMD5", 9323613075234140270], |
| 128 | "red2.png" : ["bitmap-64bitMD5", 16670399404877552232], |
| 129 | "red3.png" : ["bitmap-64bitMD5", 2507897274083364964], |
| 130 | "red4.png" : ["bitmap-64bitMD5", 7325267995523877959], |
| 131 | "red5.png" : ["bitmap-64bitMD5", 2181381724594493116], |
| 132 | # Manually verified these 6 images, all 256x256 tiles, |
| 133 | # consistent with a tiled version of the 640x400 green rect |
| 134 | # with black borders. |
| 135 | "green0.png" : ["bitmap-64bitMD5", 12587324416545178013], |
| 136 | "green1.png" : ["bitmap-64bitMD5", 7624374914829746293], |
| 137 | "green2.png" : ["bitmap-64bitMD5", 5686489729535631913], |
| 138 | "green3.png" : ["bitmap-64bitMD5", 7980646035555096146], |
| 139 | "green4.png" : ["bitmap-64bitMD5", 17817086664365875131], |
| 140 | "green5.png" : ["bitmap-64bitMD5", 10673669813016809363], |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 143 | } |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 144 | self._assert_json_contents(output_json_path, expected_summary_dict) |
| 145 | |
| 146 | def _run_render_pictures(self, args): |
| 147 | binary = self.find_path_to_program('render_pictures') |
| 148 | return self.run_command([binary, |
| 149 | '--clone', '1', |
| 150 | '--config', '8888', |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 151 | ] + args) |
| 152 | |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 153 | def _generate_skps(self): |
| 154 | """Runs the skpmaker binary to generate files in self._input_skp_dir.""" |
| 155 | self._run_skpmaker( |
| 156 | output_path=os.path.join(self._input_skp_dir, 'red.skp'), red=255) |
| 157 | self._run_skpmaker( |
| 158 | output_path=os.path.join(self._input_skp_dir, 'green.skp'), green=255) |
| 159 | |
| 160 | def _run_skpmaker(self, output_path, red=0, green=0, blue=0, |
| 161 | width=640, height=400): |
| 162 | """Runs the skpmaker binary to generate SKP with known characteristics. |
| 163 | |
| 164 | Args: |
| 165 | output_path: Filepath to write the SKP into. |
| 166 | red: Value of red color channel in image, 0-255. |
| 167 | green: Value of green color channel in image, 0-255. |
| 168 | blue: Value of blue color channel in image, 0-255. |
| 169 | width: Width of canvas to create. |
| 170 | height: Height of canvas to create. |
| 171 | """ |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 172 | binary = self.find_path_to_program('skpmaker') |
| 173 | return self.run_command([binary, |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 174 | '--red', str(red), |
| 175 | '--green', str(green), |
| 176 | '--blue', str(blue), |
| 177 | '--width', str(width), |
| 178 | '--height', str(height), |
| 179 | '--writePath', str(output_path), |
| 180 | ]) |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 181 | |
| 182 | def _assert_json_contents(self, json_path, expected_dict): |
| 183 | """Asserts that contents of a JSON file are identical to expected_dict. |
| 184 | |
| 185 | Args: |
| 186 | json_path: Path to a JSON file. |
| 187 | expected_dict: Dictionary indicating the expected contents of the JSON |
| 188 | file. |
| 189 | |
| 190 | Raises: |
| 191 | AssertionError: contents of the JSON file are not identical to |
| 192 | expected_dict. |
| 193 | """ |
| 194 | file_contents = open(json_path, 'r').read() |
| 195 | actual_dict = json.loads(file_contents) |
| 196 | self.assertEqual(actual_dict, expected_dict) |
| 197 | |
| 198 | |
| 199 | def main(): |
| 200 | base_unittest.main(RenderPicturesTest) |
| 201 | |
| 202 | |
| 203 | if __name__ == '__main__': |
| 204 | main() |