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() |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame^] | 40 | # TODO(epoger): I noticed that when this is run without --writePath being |
| 41 | # specified, this test writes red.png and green.png to the current working |
| 42 | # directory. We should fix that... if --writePath is not specified, this |
| 43 | # probably shouldn't write out red.png and green.png at all! |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 44 | self._run_render_pictures(['-r', self._input_skp_dir, |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 45 | '--bbh', 'grid', '256', '256', |
| 46 | '--mode', 'tile', '256', '256', |
| 47 | '--writeJsonSummaryPath', output_json_path, |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame^] | 48 | '--writePath', self._temp_dir, |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 49 | '--writeWholeImage']) |
| 50 | expected_summary_dict = { |
| 51 | "actual-results" : { |
| 52 | "no-comparison" : { |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 53 | # Manually verified: 640x400 red rectangle with black border |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 54 | "red.png" : [ "bitmap-64bitMD5", 11092453015575919668 ], |
| 55 | # Manually verified: 640x400 green rectangle with black border |
| 56 | "green.png" : [ "bitmap-64bitMD5", 8891695120562235492 ], |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 57 | } |
| 58 | } |
commit-bot@chromium.org | 238771c | 2014-01-15 16:33:31 +0000 | [diff] [blame] | 59 | } |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 60 | self._assert_json_contents(output_json_path, expected_summary_dict) |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame^] | 61 | self._assert_directory_contents( |
| 62 | self._temp_dir, ['red.png', 'green.png', 'output.json']) |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 63 | |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 64 | def test_untiled(self): |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 65 | """Run without tiles.""" |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 66 | 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] | 67 | self._generate_skps() |
| 68 | self._run_render_pictures(['-r', self._input_skp_dir, |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 69 | '--writePath', self._temp_dir, |
| 70 | '--writeJsonSummaryPath', output_json_path]) |
| 71 | expected_summary_dict = { |
| 72 | "actual-results" : { |
| 73 | "no-comparison" : { |
| 74 | # Manually verified: 640x400 red rectangle with black border |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 75 | "red.png" : ["bitmap-64bitMD5", 11092453015575919668], |
| 76 | # Manually verified: 640x400 green rectangle with black border |
| 77 | "green.png" : ["bitmap-64bitMD5", 8891695120562235492], |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | } |
| 81 | self._assert_json_contents(output_json_path, expected_summary_dict) |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame^] | 82 | self._assert_directory_contents( |
| 83 | self._temp_dir, ['red.png', 'green.png', 'output.json']) |
| 84 | |
| 85 | def test_untiled_writeChecksumBasedFilenames(self): |
| 86 | """Same as test_untiled, but with --writeChecksumBasedFilenames.""" |
| 87 | output_json_path = os.path.join(self._temp_dir, 'output.json') |
| 88 | self._generate_skps() |
| 89 | self._run_render_pictures(['-r', self._input_skp_dir, |
| 90 | '--writeChecksumBasedFilenames', |
| 91 | '--writePath', self._temp_dir, |
| 92 | '--writeJsonSummaryPath', output_json_path]) |
| 93 | expected_summary_dict = { |
| 94 | "actual-results" : { |
| 95 | "no-comparison" : { |
| 96 | # Manually verified: 640x400 red rectangle with black border |
| 97 | "red.png" : ["bitmap-64bitMD5", 11092453015575919668], |
| 98 | # Manually verified: 640x400 green rectangle with black border |
| 99 | "green.png" : ["bitmap-64bitMD5", 8891695120562235492], |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | self._assert_json_contents(output_json_path, expected_summary_dict) |
| 104 | self._assert_directory_contents( |
| 105 | self._temp_dir, ['bitmap-64bitMD5_11092453015575919668.png', |
| 106 | 'bitmap-64bitMD5_8891695120562235492.png', |
| 107 | 'output.json']) |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 108 | |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 109 | def test_untiled_validate(self): |
| 110 | """Same as test_untiled, but with --validate. |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 111 | |
| 112 | TODO(epoger): This test generates undesired results! The call |
| 113 | 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] | 114 | test_untiled. |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 115 | See https://code.google.com/p/skia/issues/detail?id=2044 ('render_pictures: |
| 116 | --validate fails') |
| 117 | """ |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 118 | 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] | 119 | self._generate_skps() |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 120 | with self.assertRaises(Exception): |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 121 | self._run_render_pictures(['-r', self._input_skp_dir, |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 122 | '--validate', |
| 123 | '--writePath', self._temp_dir, |
| 124 | '--writeJsonSummaryPath', output_json_path]) |
| 125 | |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 126 | def test_untiled_without_writePath(self): |
| 127 | """Same as test_untiled, but without --writePath. |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 128 | |
| 129 | TODO(epoger): This test generates undesired results! |
| 130 | See https://code.google.com/p/skia/issues/detail?id=2043 ('render_pictures: |
| 131 | --writeJsonSummaryPath fails unless --writePath is specified') |
| 132 | """ |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 133 | 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] | 134 | self._generate_skps() |
| 135 | self._run_render_pictures(['-r', self._input_skp_dir, |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 136 | '--writeJsonSummaryPath', output_json_path]) |
| 137 | expected_summary_dict = { |
| 138 | "actual-results" : { |
| 139 | "no-comparison" : None, |
| 140 | } |
| 141 | } |
| 142 | self._assert_json_contents(output_json_path, expected_summary_dict) |
| 143 | |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 144 | def test_tiled(self): |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 145 | """Generate individual tiles.""" |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 146 | 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] | 147 | self._generate_skps() |
| 148 | self._run_render_pictures(['-r', self._input_skp_dir, |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 149 | '--bbh', 'grid', '256', '256', |
| 150 | '--mode', 'tile', '256', '256', |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 151 | '--writePath', self._temp_dir, |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 152 | '--writeJsonSummaryPath', output_json_path]) |
| 153 | expected_summary_dict = { |
| 154 | "actual-results" : { |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 155 | "no-comparison" : { |
| 156 | # Manually verified these 6 images, all 256x256 tiles, |
| 157 | # consistent with a tiled version of the 640x400 red rect |
| 158 | # with black borders. |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 159 | "red0.png" : ["bitmap-64bitMD5", 5815827069051002745], |
| 160 | "red1.png" : ["bitmap-64bitMD5", 9323613075234140270], |
| 161 | "red2.png" : ["bitmap-64bitMD5", 16670399404877552232], |
| 162 | "red3.png" : ["bitmap-64bitMD5", 2507897274083364964], |
| 163 | "red4.png" : ["bitmap-64bitMD5", 7325267995523877959], |
| 164 | "red5.png" : ["bitmap-64bitMD5", 2181381724594493116], |
| 165 | # Manually verified these 6 images, all 256x256 tiles, |
| 166 | # consistent with a tiled version of the 640x400 green rect |
| 167 | # with black borders. |
| 168 | "green0.png" : ["bitmap-64bitMD5", 12587324416545178013], |
| 169 | "green1.png" : ["bitmap-64bitMD5", 7624374914829746293], |
| 170 | "green2.png" : ["bitmap-64bitMD5", 5686489729535631913], |
| 171 | "green3.png" : ["bitmap-64bitMD5", 7980646035555096146], |
| 172 | "green4.png" : ["bitmap-64bitMD5", 17817086664365875131], |
| 173 | "green5.png" : ["bitmap-64bitMD5", 10673669813016809363], |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
commit-bot@chromium.org | c3147c6 | 2014-01-15 20:35:54 +0000 | [diff] [blame] | 176 | } |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 177 | self._assert_json_contents(output_json_path, expected_summary_dict) |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame^] | 178 | self._assert_directory_contents( |
| 179 | self._temp_dir, |
| 180 | ['red0.png', 'red1.png', 'red2.png', 'red3.png', 'red4.png', 'red5.png', |
| 181 | 'green0.png', 'green1.png', 'green2.png', 'green3.png', 'green4.png', |
| 182 | 'green5.png', 'output.json']) |
| 183 | |
| 184 | def test_tiled_writeChecksumBasedFilenames(self): |
| 185 | """Same as test_tiled, but with --writeChecksumBasedFilenames.""" |
| 186 | output_json_path = os.path.join(self._temp_dir, 'output.json') |
| 187 | self._generate_skps() |
| 188 | self._run_render_pictures(['-r', self._input_skp_dir, |
| 189 | '--bbh', 'grid', '256', '256', |
| 190 | '--mode', 'tile', '256', '256', |
| 191 | '--writeChecksumBasedFilenames', |
| 192 | '--writePath', self._temp_dir, |
| 193 | '--writeJsonSummaryPath', output_json_path]) |
| 194 | expected_summary_dict = { |
| 195 | "actual-results" : { |
| 196 | "no-comparison" : { |
| 197 | # Manually verified these 6 images, all 256x256 tiles, |
| 198 | # consistent with a tiled version of the 640x400 red rect |
| 199 | # with black borders. |
| 200 | "red0.png" : ["bitmap-64bitMD5", 5815827069051002745], |
| 201 | "red1.png" : ["bitmap-64bitMD5", 9323613075234140270], |
| 202 | "red2.png" : ["bitmap-64bitMD5", 16670399404877552232], |
| 203 | "red3.png" : ["bitmap-64bitMD5", 2507897274083364964], |
| 204 | "red4.png" : ["bitmap-64bitMD5", 7325267995523877959], |
| 205 | "red5.png" : ["bitmap-64bitMD5", 2181381724594493116], |
| 206 | # Manually verified these 6 images, all 256x256 tiles, |
| 207 | # consistent with a tiled version of the 640x400 green rect |
| 208 | # with black borders. |
| 209 | "green0.png" : ["bitmap-64bitMD5", 12587324416545178013], |
| 210 | "green1.png" : ["bitmap-64bitMD5", 7624374914829746293], |
| 211 | "green2.png" : ["bitmap-64bitMD5", 5686489729535631913], |
| 212 | "green3.png" : ["bitmap-64bitMD5", 7980646035555096146], |
| 213 | "green4.png" : ["bitmap-64bitMD5", 17817086664365875131], |
| 214 | "green5.png" : ["bitmap-64bitMD5", 10673669813016809363], |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | self._assert_json_contents(output_json_path, expected_summary_dict) |
| 219 | self._assert_directory_contents( |
| 220 | self._temp_dir, |
| 221 | ['bitmap-64bitMD5_5815827069051002745.png', |
| 222 | 'bitmap-64bitMD5_9323613075234140270.png', |
| 223 | 'bitmap-64bitMD5_16670399404877552232.png', |
| 224 | 'bitmap-64bitMD5_2507897274083364964.png', |
| 225 | 'bitmap-64bitMD5_7325267995523877959.png', |
| 226 | 'bitmap-64bitMD5_2181381724594493116.png', |
| 227 | 'bitmap-64bitMD5_12587324416545178013.png', |
| 228 | 'bitmap-64bitMD5_7624374914829746293.png', |
| 229 | 'bitmap-64bitMD5_5686489729535631913.png', |
| 230 | 'bitmap-64bitMD5_7980646035555096146.png', |
| 231 | 'bitmap-64bitMD5_17817086664365875131.png', |
| 232 | 'bitmap-64bitMD5_10673669813016809363.png', |
| 233 | 'output.json']) |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 234 | |
| 235 | def _run_render_pictures(self, args): |
| 236 | binary = self.find_path_to_program('render_pictures') |
| 237 | return self.run_command([binary, |
| 238 | '--clone', '1', |
| 239 | '--config', '8888', |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 240 | ] + args) |
| 241 | |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 242 | def _generate_skps(self): |
| 243 | """Runs the skpmaker binary to generate files in self._input_skp_dir.""" |
| 244 | self._run_skpmaker( |
| 245 | output_path=os.path.join(self._input_skp_dir, 'red.skp'), red=255) |
| 246 | self._run_skpmaker( |
| 247 | output_path=os.path.join(self._input_skp_dir, 'green.skp'), green=255) |
| 248 | |
| 249 | def _run_skpmaker(self, output_path, red=0, green=0, blue=0, |
| 250 | width=640, height=400): |
| 251 | """Runs the skpmaker binary to generate SKP with known characteristics. |
| 252 | |
| 253 | Args: |
| 254 | output_path: Filepath to write the SKP into. |
| 255 | red: Value of red color channel in image, 0-255. |
| 256 | green: Value of green color channel in image, 0-255. |
| 257 | blue: Value of blue color channel in image, 0-255. |
| 258 | width: Width of canvas to create. |
| 259 | height: Height of canvas to create. |
| 260 | """ |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 261 | binary = self.find_path_to_program('skpmaker') |
| 262 | return self.run_command([binary, |
commit-bot@chromium.org | f11943f | 2014-03-12 20:09:46 +0000 | [diff] [blame] | 263 | '--red', str(red), |
| 264 | '--green', str(green), |
| 265 | '--blue', str(blue), |
| 266 | '--width', str(width), |
| 267 | '--height', str(height), |
| 268 | '--writePath', str(output_path), |
| 269 | ]) |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 270 | |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame^] | 271 | def _assert_directory_contents(self, dir_path, expected_filenames): |
| 272 | """Asserts that files found in a dir are identical to expected_filenames. |
| 273 | |
| 274 | Args: |
| 275 | dir_path: Path to a directory on local disk. |
| 276 | expected_filenames: Set containing the expected filenames within the dir. |
| 277 | |
| 278 | Raises: |
| 279 | AssertionError: contents of the directory are not identical to |
| 280 | expected_filenames. |
| 281 | """ |
| 282 | self.assertEqual(set(os.listdir(dir_path)), set(expected_filenames)) |
| 283 | |
| 284 | |
commit-bot@chromium.org | 11f1562 | 2014-01-07 17:03:40 +0000 | [diff] [blame] | 285 | def _assert_json_contents(self, json_path, expected_dict): |
| 286 | """Asserts that contents of a JSON file are identical to expected_dict. |
| 287 | |
| 288 | Args: |
| 289 | json_path: Path to a JSON file. |
| 290 | expected_dict: Dictionary indicating the expected contents of the JSON |
| 291 | file. |
| 292 | |
| 293 | Raises: |
| 294 | AssertionError: contents of the JSON file are not identical to |
| 295 | expected_dict. |
| 296 | """ |
| 297 | file_contents = open(json_path, 'r').read() |
| 298 | actual_dict = json.loads(file_contents) |
| 299 | self.assertEqual(actual_dict, expected_dict) |
| 300 | |
| 301 | |
| 302 | def main(): |
| 303 | base_unittest.main(RenderPicturesTest) |
| 304 | |
| 305 | |
| 306 | if __name__ == '__main__': |
| 307 | main() |