blob: 8feef673186576ca0315ec75f8b0ef0532b26fbd [file] [log] [blame]
commit-bot@chromium.org11f15622014-01-07 17:03:40 +00001#!/usr/bin/python
2
3"""
4Copyright 2014 Google Inc.
5
6Use of this source code is governed by a BSD-style license that can be
7found in the LICENSE file.
8
9Test the render_pictures binary.
commit-bot@chromium.org11f15622014-01-07 17:03:40 +000010"""
11
12# System-level imports
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000013import copy
commit-bot@chromium.org11f15622014-01-07 17:03:40 +000014import json
15import os
16import shutil
17import tempfile
18
19# Imports from within Skia
20import base_unittest
21
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +000022# Maximum length of text diffs to show when tests fail
23MAX_DIFF_LENGTH = 30000
24
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +000025EXPECTED_HEADER_CONTENTS = {
26 "type" : "ChecksummedImages",
27 "revision" : 1,
28}
29
commit-bot@chromium.org4610a462014-04-29 19:39:22 +000030# Manually verified: 640x400 red rectangle with black border
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000031# Standard expectations will be set up in such a way that this image fails
32# the comparison.
commit-bot@chromium.org4610a462014-04-29 19:39:22 +000033RED_WHOLEIMAGE = {
34 "checksumAlgorithm" : "bitmap-64bitMD5",
35 "checksumValue" : 11092453015575919668,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000036 "comparisonResult" : "failed",
commit-bot@chromium.org4610a462014-04-29 19:39:22 +000037 "filepath" : "red_skp.png",
38}
39
40# Manually verified: 640x400 green rectangle with black border
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000041# Standard expectations will be set up in such a way that this image passes
42# the comparison.
commit-bot@chromium.org4610a462014-04-29 19:39:22 +000043GREEN_WHOLEIMAGE = {
44 "checksumAlgorithm" : "bitmap-64bitMD5",
45 "checksumValue" : 8891695120562235492,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000046 "comparisonResult" : "succeeded",
commit-bot@chromium.org4610a462014-04-29 19:39:22 +000047 "filepath" : "green_skp.png",
48}
49
50# Manually verified these 6 images, all 256x256 tiles,
51# consistent with a tiled version of the 640x400 red rect
52# with black borders.
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000053# Standard expectations will be set up in such a way that these images fail
54# the comparison.
commit-bot@chromium.org4610a462014-04-29 19:39:22 +000055RED_TILES = [{
56 "checksumAlgorithm" : "bitmap-64bitMD5",
57 "checksumValue" : 5815827069051002745,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000058 "comparisonResult" : "failed",
commit-bot@chromium.org4610a462014-04-29 19:39:22 +000059 "filepath" : "red_skp-tile0.png",
60},{
61 "checksumAlgorithm" : "bitmap-64bitMD5",
62 "checksumValue" : 9323613075234140270,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000063 "comparisonResult" : "failed",
commit-bot@chromium.org4610a462014-04-29 19:39:22 +000064 "filepath" : "red_skp-tile1.png",
65}, {
66 "checksumAlgorithm" : "bitmap-64bitMD5",
67 "checksumValue" : 16670399404877552232,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000068 "comparisonResult" : "failed",
commit-bot@chromium.org4610a462014-04-29 19:39:22 +000069 "filepath" : "red_skp-tile2.png",
70}, {
71 "checksumAlgorithm" : "bitmap-64bitMD5",
72 "checksumValue" : 2507897274083364964,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000073 "comparisonResult" : "failed",
commit-bot@chromium.org4610a462014-04-29 19:39:22 +000074 "filepath" : "red_skp-tile3.png",
75}, {
76 "checksumAlgorithm" : "bitmap-64bitMD5",
77 "checksumValue" : 7325267995523877959,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000078 "comparisonResult" : "failed",
commit-bot@chromium.org4610a462014-04-29 19:39:22 +000079 "filepath" : "red_skp-tile4.png",
80}, {
81 "checksumAlgorithm" : "bitmap-64bitMD5",
82 "checksumValue" : 2181381724594493116,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000083 "comparisonResult" : "failed",
commit-bot@chromium.org4610a462014-04-29 19:39:22 +000084 "filepath" : "red_skp-tile5.png",
85}]
86
87# Manually verified these 6 images, all 256x256 tiles,
88# consistent with a tiled version of the 640x400 green rect
89# with black borders.
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000090# Standard expectations will be set up in such a way that these images pass
91# the comparison.
commit-bot@chromium.org4610a462014-04-29 19:39:22 +000092GREEN_TILES = [{
93 "checksumAlgorithm" : "bitmap-64bitMD5",
94 "checksumValue" : 12587324416545178013,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000095 "comparisonResult" : "succeeded",
commit-bot@chromium.org4610a462014-04-29 19:39:22 +000096 "filepath" : "green_skp-tile0.png",
97}, {
98 "checksumAlgorithm" : "bitmap-64bitMD5",
99 "checksumValue" : 7624374914829746293,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000100 "comparisonResult" : "succeeded",
commit-bot@chromium.org4610a462014-04-29 19:39:22 +0000101 "filepath" : "green_skp-tile1.png",
102}, {
103 "checksumAlgorithm" : "bitmap-64bitMD5",
104 "checksumValue" : 5686489729535631913,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000105 "comparisonResult" : "succeeded",
commit-bot@chromium.org4610a462014-04-29 19:39:22 +0000106 "filepath" : "green_skp-tile2.png",
107}, {
108 "checksumAlgorithm" : "bitmap-64bitMD5",
109 "checksumValue" : 7980646035555096146,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000110 "comparisonResult" : "succeeded",
commit-bot@chromium.org4610a462014-04-29 19:39:22 +0000111 "filepath" : "green_skp-tile3.png",
112}, {
113 "checksumAlgorithm" : "bitmap-64bitMD5",
114 "checksumValue" : 17817086664365875131,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000115 "comparisonResult" : "succeeded",
commit-bot@chromium.org4610a462014-04-29 19:39:22 +0000116 "filepath" : "green_skp-tile4.png",
117}, {
118 "checksumAlgorithm" : "bitmap-64bitMD5",
119 "checksumValue" : 10673669813016809363,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000120 "comparisonResult" : "succeeded",
commit-bot@chromium.org4610a462014-04-29 19:39:22 +0000121 "filepath" : "green_skp-tile5.png",
122}]
123
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000124
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000125def modified_dict(input_dict, modification_dict):
126 """Returns a dict, with some modifications applied to it.
127
128 Args:
129 input_dict: a dictionary (which will be copied, not modified in place)
130 modification_dict: a set of key/value pairs to overwrite in the dict
131 """
132 output_dict = input_dict.copy()
133 output_dict.update(modification_dict)
134 return output_dict
135
136
137def modified_list_of_dicts(input_list, modification_dict):
138 """Returns a list of dicts, with some modifications applied to each dict.
139
140 Args:
141 input_list: a list of dictionaries; these dicts will be copied, not
142 modified in place
143 modification_dict: a set of key/value pairs to overwrite in each dict
144 within input_list
145 """
146 output_list = []
147 for input_dict in input_list:
148 output_dict = modified_dict(input_dict, modification_dict)
149 output_list.append(output_dict)
150 return output_list
151
152
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000153class RenderPicturesTest(base_unittest.TestCase):
154
155 def setUp(self):
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000156 self.maxDiff = MAX_DIFF_LENGTH
157 self._expectations_dir = tempfile.mkdtemp()
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +0000158 self._input_skp_dir = tempfile.mkdtemp()
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000159 # All output of render_pictures binary will go into this directory.
160 self._output_dir = tempfile.mkdtemp()
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000161
162 def tearDown(self):
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000163 shutil.rmtree(self._expectations_dir)
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +0000164 shutil.rmtree(self._input_skp_dir)
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000165 shutil.rmtree(self._output_dir)
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000166
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +0000167 def test_tiled_whole_image(self):
commit-bot@chromium.org4610a462014-04-29 19:39:22 +0000168 """Run render_pictures with tiles and --writeWholeImage flag.
169
170 TODO(epoger): This test generates undesired results! The JSON summary
171 includes both whole-image and tiled-images (as it should), but only
172 whole-images are written out to disk. See http://skbug.com/2463
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000173 Once I fix that, I should add a similar test that exercises mismatchPath.
commit-bot@chromium.org4610a462014-04-29 19:39:22 +0000174
175 TODO(epoger): I noticed that when this is run without --writePath being
176 specified, this test writes red_skp.png and green_skp.png to the current
177 directory. We should fix that... if --writePath is not specified, this
178 probably shouldn't write out red_skp.png and green_skp.png at all!
179 See http://skbug.com/2464
180 """
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000181 output_json_path = os.path.join(self._output_dir, 'actuals.json')
182 write_path_dir = self.create_empty_dir(
183 path=os.path.join(self._output_dir, 'writePath'))
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +0000184 self._generate_skps()
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000185 expectations_path = self._create_expectations()
186 self._run_render_pictures([
187 '-r', self._input_skp_dir,
188 '--bbh', 'grid', '256', '256',
189 '--mode', 'tile', '256', '256',
190 '--readJsonSummaryPath', expectations_path,
191 '--writeJsonSummaryPath', output_json_path,
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000192 '--writePath', write_path_dir,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000193 '--writeWholeImage'])
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000194 expected_summary_dict = {
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000195 "header" : EXPECTED_HEADER_CONTENTS,
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000196 "actual-results" : {
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000197 "red.skp": {
commit-bot@chromium.org4610a462014-04-29 19:39:22 +0000198 "tiled-images": RED_TILES,
199 "whole-image": RED_WHOLEIMAGE,
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000200 },
201 "green.skp": {
commit-bot@chromium.org4610a462014-04-29 19:39:22 +0000202 "tiled-images": GREEN_TILES,
203 "whole-image": GREEN_WHOLEIMAGE,
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000204 }
205 }
commit-bot@chromium.org238771c2014-01-15 16:33:31 +0000206 }
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000207 self._assert_json_contents(output_json_path, expected_summary_dict)
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000208 self._assert_directory_contents(
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000209 write_path_dir, ['red_skp.png', 'green_skp.png'])
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000210
211 def test_missing_tile_and_whole_image(self):
212 """test_tiled_whole_image, but missing expectations for some images.
213 """
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000214 output_json_path = os.path.join(self._output_dir, 'actuals.json')
215 write_path_dir = self.create_empty_dir(
216 path=os.path.join(self._output_dir, 'writePath'))
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000217 self._generate_skps()
218 expectations_path = self._create_expectations(missing_some_images=True)
219 self._run_render_pictures([
220 '-r', self._input_skp_dir,
221 '--bbh', 'grid', '256', '256',
222 '--mode', 'tile', '256', '256',
223 '--readJsonSummaryPath', expectations_path,
224 '--writeJsonSummaryPath', output_json_path,
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000225 '--writePath', write_path_dir,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000226 '--writeWholeImage'])
227 modified_red_tiles = copy.deepcopy(RED_TILES)
228 modified_red_tiles[5]['comparisonResult'] = 'no-comparison'
229 expected_summary_dict = {
230 "header" : EXPECTED_HEADER_CONTENTS,
231 "actual-results" : {
232 "red.skp": {
233 "tiled-images": modified_red_tiles,
234 "whole-image": modified_dict(
235 RED_WHOLEIMAGE, {"comparisonResult" : "no-comparison"}),
236 },
237 "green.skp": {
238 "tiled-images": GREEN_TILES,
239 "whole-image": GREEN_WHOLEIMAGE,
240 }
241 }
242 }
243 self._assert_json_contents(output_json_path, expected_summary_dict)
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000244
commit-bot@chromium.orgabeb9582014-05-19 15:25:10 +0000245 def _test_untiled(self, expectations_path=None, expected_summary_dict=None,
246 additional_args=None):
247 """Base for multiple tests without tiles.
248
249 Args:
250 expectations_path: path we should pass using --readJsonSummaryPath, or
251 None if we should create the default expectations file
252 expected_summary_dict: dict we should compare against the output actual
253 results summary, or None if we should use a default comparison dict
254 additional_args: array of command-line args to add when we run
255 render_pictures
256 """
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000257 output_json_path = os.path.join(self._output_dir, 'actuals.json')
258 write_path_dir = self.create_empty_dir(
259 path=os.path.join(self._output_dir, 'writePath'))
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +0000260 self._generate_skps()
commit-bot@chromium.orgabeb9582014-05-19 15:25:10 +0000261 if expectations_path == None:
262 expectations_path = self._create_expectations()
263 args = [
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000264 '-r', self._input_skp_dir,
265 '--readJsonSummaryPath', expectations_path,
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000266 '--writePath', write_path_dir,
commit-bot@chromium.orgabeb9582014-05-19 15:25:10 +0000267 '--writeJsonSummaryPath', output_json_path,
268 ]
269 if additional_args:
270 args.extend(additional_args)
271 self._run_render_pictures(args)
272 if expected_summary_dict == None:
273 expected_summary_dict = {
274 "header" : EXPECTED_HEADER_CONTENTS,
275 "actual-results" : {
276 "red.skp": {
277 "whole-image": RED_WHOLEIMAGE,
278 },
279 "green.skp": {
280 "whole-image": GREEN_WHOLEIMAGE,
281 }
282 }
283 }
284 self._assert_json_contents(output_json_path, expected_summary_dict)
285 self._assert_directory_contents(
286 write_path_dir, ['red_skp.png', 'green_skp.png'])
287
288 def test_untiled(self):
289 """Basic test without tiles."""
290 self._test_untiled()
291
292 def test_untiled_empty_expectations_file(self):
293 """Same as test_untiled, but with an empty expectations file."""
294 expectations_path = os.path.join(self._expectations_dir, 'empty')
295 with open(expectations_path, 'w') as fh:
296 pass
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +0000297 expected_summary_dict = {
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000298 "header" : EXPECTED_HEADER_CONTENTS,
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +0000299 "actual-results" : {
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000300 "red.skp": {
commit-bot@chromium.orgabeb9582014-05-19 15:25:10 +0000301 "whole-image": modified_dict(
302 RED_WHOLEIMAGE, {"comparisonResult" : "no-comparison"}),
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000303 },
304 "green.skp": {
commit-bot@chromium.orgabeb9582014-05-19 15:25:10 +0000305 "whole-image": modified_dict(
306 GREEN_WHOLEIMAGE, {"comparisonResult" : "no-comparison"}),
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +0000307 }
308 }
309 }
commit-bot@chromium.orgabeb9582014-05-19 15:25:10 +0000310 self._test_untiled(expectations_path=expectations_path,
311 expected_summary_dict=expected_summary_dict)
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000312
313 def test_untiled_writeChecksumBasedFilenames(self):
314 """Same as test_untiled, but with --writeChecksumBasedFilenames."""
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000315 output_json_path = os.path.join(self._output_dir, 'actuals.json')
316 write_path_dir = self.create_empty_dir(
317 path=os.path.join(self._output_dir, 'writePath'))
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000318 self._generate_skps()
319 self._run_render_pictures(['-r', self._input_skp_dir,
320 '--writeChecksumBasedFilenames',
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000321 '--writePath', write_path_dir,
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000322 '--writeJsonSummaryPath', output_json_path])
323 expected_summary_dict = {
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000324 "header" : EXPECTED_HEADER_CONTENTS,
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000325 "actual-results" : {
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000326 "red.skp": {
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000327 # Manually verified: 640x400 red rectangle with black border
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000328 "whole-image": {
329 "checksumAlgorithm" : "bitmap-64bitMD5",
330 "checksumValue" : 11092453015575919668,
331 "comparisonResult" : "no-comparison",
332 "filepath" : "red_skp/bitmap-64bitMD5_11092453015575919668.png",
333 },
334 },
335 "green.skp": {
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000336 # Manually verified: 640x400 green rectangle with black border
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000337 "whole-image": {
338 "checksumAlgorithm" : "bitmap-64bitMD5",
339 "checksumValue" : 8891695120562235492,
340 "comparisonResult" : "no-comparison",
341 "filepath" : "green_skp/bitmap-64bitMD5_8891695120562235492.png",
342 },
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000343 }
344 }
345 }
346 self._assert_json_contents(output_json_path, expected_summary_dict)
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000347 self._assert_directory_contents(write_path_dir, ['red_skp', 'green_skp'])
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000348 self._assert_directory_contents(
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000349 os.path.join(write_path_dir, 'red_skp'),
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000350 ['bitmap-64bitMD5_11092453015575919668.png'])
351 self._assert_directory_contents(
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000352 os.path.join(write_path_dir, 'green_skp'),
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000353 ['bitmap-64bitMD5_8891695120562235492.png'])
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000354
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +0000355 def test_untiled_validate(self):
commit-bot@chromium.org4610a462014-04-29 19:39:22 +0000356 """Same as test_untiled, but with --validate."""
commit-bot@chromium.orgabeb9582014-05-19 15:25:10 +0000357 self._test_untiled(additional_args=['--validate'])
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +0000358
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +0000359 def test_untiled_without_writePath(self):
commit-bot@chromium.org4610a462014-04-29 19:39:22 +0000360 """Same as test_untiled, but without --writePath."""
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000361 output_json_path = os.path.join(self._output_dir, 'actuals.json')
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +0000362 self._generate_skps()
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000363 expectations_path = self._create_expectations()
364 self._run_render_pictures([
365 '-r', self._input_skp_dir,
366 '--readJsonSummaryPath', expectations_path,
367 '--writeJsonSummaryPath', output_json_path])
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +0000368 expected_summary_dict = {
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000369 "header" : EXPECTED_HEADER_CONTENTS,
commit-bot@chromium.org4610a462014-04-29 19:39:22 +0000370 "actual-results" : {
371 "red.skp": {
372 "whole-image": RED_WHOLEIMAGE,
373 },
374 "green.skp": {
375 "whole-image": GREEN_WHOLEIMAGE,
376 }
377 }
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +0000378 }
379 self._assert_json_contents(output_json_path, expected_summary_dict)
380
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +0000381 def test_tiled(self):
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +0000382 """Generate individual tiles."""
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000383 output_json_path = os.path.join(self._output_dir, 'actuals.json')
384 write_path_dir = self.create_empty_dir(
385 path=os.path.join(self._output_dir, 'writePath'))
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +0000386 self._generate_skps()
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000387 expectations_path = self._create_expectations()
388 self._run_render_pictures([
389 '-r', self._input_skp_dir,
390 '--bbh', 'grid', '256', '256',
391 '--mode', 'tile', '256', '256',
392 '--readJsonSummaryPath', expectations_path,
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000393 '--writePath', write_path_dir,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000394 '--writeJsonSummaryPath', output_json_path])
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000395 expected_summary_dict = {
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000396 "header" : EXPECTED_HEADER_CONTENTS,
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000397 "actual-results" : {
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000398 "red.skp": {
commit-bot@chromium.org4610a462014-04-29 19:39:22 +0000399 "tiled-images": RED_TILES,
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000400 },
401 "green.skp": {
commit-bot@chromium.org4610a462014-04-29 19:39:22 +0000402 "tiled-images": GREEN_TILES,
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000403 }
404 }
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +0000405 }
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000406 self._assert_json_contents(output_json_path, expected_summary_dict)
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000407 self._assert_directory_contents(
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000408 write_path_dir,
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000409 ['red_skp-tile0.png', 'red_skp-tile1.png', 'red_skp-tile2.png',
410 'red_skp-tile3.png', 'red_skp-tile4.png', 'red_skp-tile5.png',
411 'green_skp-tile0.png', 'green_skp-tile1.png', 'green_skp-tile2.png',
412 'green_skp-tile3.png', 'green_skp-tile4.png', 'green_skp-tile5.png',
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000413 ])
414
415 def test_tiled_mismatches(self):
416 """Same as test_tiled, but only write out mismatching images."""
417 output_json_path = os.path.join(self._output_dir, 'actuals.json')
418 mismatch_path_dir = self.create_empty_dir(
419 path=os.path.join(self._output_dir, 'mismatchPath'))
420 self._generate_skps()
421 expectations_path = self._create_expectations()
422 self._run_render_pictures([
423 '-r', self._input_skp_dir,
424 '--bbh', 'grid', '256', '256',
425 '--mode', 'tile', '256', '256',
426 '--readJsonSummaryPath', expectations_path,
427 '--mismatchPath', mismatch_path_dir,
428 '--writeJsonSummaryPath', output_json_path])
429 expected_summary_dict = {
430 "header" : EXPECTED_HEADER_CONTENTS,
431 "actual-results" : {
432 "red.skp": {
433 "tiled-images": RED_TILES,
434 },
435 "green.skp": {
436 "tiled-images": GREEN_TILES,
437 }
438 }
439 }
440 self._assert_json_contents(output_json_path, expected_summary_dict)
441 self._assert_directory_contents(
442 mismatch_path_dir,
443 ['red_skp-tile0.png', 'red_skp-tile1.png', 'red_skp-tile2.png',
444 'red_skp-tile3.png', 'red_skp-tile4.png', 'red_skp-tile5.png',
445 ])
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000446
447 def test_tiled_writeChecksumBasedFilenames(self):
448 """Same as test_tiled, but with --writeChecksumBasedFilenames."""
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000449 output_json_path = os.path.join(self._output_dir, 'actuals.json')
450 write_path_dir = self.create_empty_dir(
451 path=os.path.join(self._output_dir, 'writePath'))
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000452 self._generate_skps()
453 self._run_render_pictures(['-r', self._input_skp_dir,
454 '--bbh', 'grid', '256', '256',
455 '--mode', 'tile', '256', '256',
456 '--writeChecksumBasedFilenames',
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000457 '--writePath', write_path_dir,
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000458 '--writeJsonSummaryPath', output_json_path])
459 expected_summary_dict = {
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000460 "header" : EXPECTED_HEADER_CONTENTS,
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000461 "actual-results" : {
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000462 "red.skp": {
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000463 # Manually verified these 6 images, all 256x256 tiles,
464 # consistent with a tiled version of the 640x400 red rect
465 # with black borders.
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000466 "tiled-images": [{
467 "checksumAlgorithm" : "bitmap-64bitMD5",
468 "checksumValue" : 5815827069051002745,
469 "comparisonResult" : "no-comparison",
470 "filepath" : "red_skp/bitmap-64bitMD5_5815827069051002745.png",
471 }, {
472 "checksumAlgorithm" : "bitmap-64bitMD5",
473 "checksumValue" : 9323613075234140270,
474 "comparisonResult" : "no-comparison",
475 "filepath" : "red_skp/bitmap-64bitMD5_9323613075234140270.png",
476 }, {
477 "checksumAlgorithm" : "bitmap-64bitMD5",
478 "checksumValue" : 16670399404877552232,
479 "comparisonResult" : "no-comparison",
480 "filepath" : "red_skp/bitmap-64bitMD5_16670399404877552232.png",
481 }, {
482 "checksumAlgorithm" : "bitmap-64bitMD5",
483 "checksumValue" : 2507897274083364964,
484 "comparisonResult" : "no-comparison",
485 "filepath" : "red_skp/bitmap-64bitMD5_2507897274083364964.png",
486 }, {
487 "checksumAlgorithm" : "bitmap-64bitMD5",
488 "checksumValue" : 7325267995523877959,
489 "comparisonResult" : "no-comparison",
490 "filepath" : "red_skp/bitmap-64bitMD5_7325267995523877959.png",
491 }, {
492 "checksumAlgorithm" : "bitmap-64bitMD5",
493 "checksumValue" : 2181381724594493116,
494 "comparisonResult" : "no-comparison",
495 "filepath" : "red_skp/bitmap-64bitMD5_2181381724594493116.png",
496 }],
497 },
498 "green.skp": {
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000499 # Manually verified these 6 images, all 256x256 tiles,
500 # consistent with a tiled version of the 640x400 green rect
501 # with black borders.
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000502 "tiled-images": [{
503 "checksumAlgorithm" : "bitmap-64bitMD5",
504 "checksumValue" : 12587324416545178013,
505 "comparisonResult" : "no-comparison",
506 "filepath" : "green_skp/bitmap-64bitMD5_12587324416545178013.png",
507 }, {
508 "checksumAlgorithm" : "bitmap-64bitMD5",
509 "checksumValue" : 7624374914829746293,
510 "comparisonResult" : "no-comparison",
511 "filepath" : "green_skp/bitmap-64bitMD5_7624374914829746293.png",
512 }, {
513 "checksumAlgorithm" : "bitmap-64bitMD5",
514 "checksumValue" : 5686489729535631913,
515 "comparisonResult" : "no-comparison",
516 "filepath" : "green_skp/bitmap-64bitMD5_5686489729535631913.png",
517 }, {
518 "checksumAlgorithm" : "bitmap-64bitMD5",
519 "checksumValue" : 7980646035555096146,
520 "comparisonResult" : "no-comparison",
521 "filepath" : "green_skp/bitmap-64bitMD5_7980646035555096146.png",
522 }, {
523 "checksumAlgorithm" : "bitmap-64bitMD5",
524 "checksumValue" : 17817086664365875131,
525 "comparisonResult" : "no-comparison",
526 "filepath" : "green_skp/bitmap-64bitMD5_17817086664365875131.png",
527 }, {
528 "checksumAlgorithm" : "bitmap-64bitMD5",
529 "checksumValue" : 10673669813016809363,
530 "comparisonResult" : "no-comparison",
531 "filepath" : "green_skp/bitmap-64bitMD5_10673669813016809363.png",
532 }],
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000533 }
534 }
535 }
536 self._assert_json_contents(output_json_path, expected_summary_dict)
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000537 self._assert_directory_contents(write_path_dir, ['red_skp', 'green_skp'])
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000538 self._assert_directory_contents(
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000539 os.path.join(write_path_dir, 'red_skp'),
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000540 ['bitmap-64bitMD5_5815827069051002745.png',
541 'bitmap-64bitMD5_9323613075234140270.png',
542 'bitmap-64bitMD5_16670399404877552232.png',
543 'bitmap-64bitMD5_2507897274083364964.png',
544 'bitmap-64bitMD5_7325267995523877959.png',
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000545 'bitmap-64bitMD5_2181381724594493116.png'])
546 self._assert_directory_contents(
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000547 os.path.join(write_path_dir, 'green_skp'),
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000548 ['bitmap-64bitMD5_12587324416545178013.png',
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000549 'bitmap-64bitMD5_7624374914829746293.png',
550 'bitmap-64bitMD5_5686489729535631913.png',
551 'bitmap-64bitMD5_7980646035555096146.png',
552 'bitmap-64bitMD5_17817086664365875131.png',
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000553 'bitmap-64bitMD5_10673669813016809363.png'])
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000554
555 def _run_render_pictures(self, args):
556 binary = self.find_path_to_program('render_pictures')
557 return self.run_command([binary,
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000558 '--config', '8888',
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000559 ] + args)
560
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000561 def _create_expectations(self, missing_some_images=False,
562 rel_path='expectations.json'):
563 """Creates expectations JSON file within self._expectations_dir .
564
565 Args:
566 missing_some_images: (bool) whether to remove expectations for a subset
567 of the images
568 rel_path: (string) relative path within self._expectations_dir to write
569 the expectations into
570
571 Returns: full path to the expectations file created.
572 """
573 expectations_dict = {
574 "header" : EXPECTED_HEADER_CONTENTS,
575 "expected-results" : {
576 # red.skp: these should fail the comparison
577 "red.skp": {
578 "tiled-images": modified_list_of_dicts(
579 RED_TILES, {'checksumValue': 11111}),
580 "whole-image": modified_dict(
581 RED_WHOLEIMAGE, {'checksumValue': 22222}),
582 },
583 # green.skp: these should pass the comparison
584 "green.skp": {
585 "tiled-images": GREEN_TILES,
586 "whole-image": GREEN_WHOLEIMAGE,
587 }
588 }
589 }
590 if missing_some_images:
591 del expectations_dict['expected-results']['red.skp']['whole-image']
592 del expectations_dict['expected-results']['red.skp']['tiled-images'][-1]
593 path = os.path.join(self._expectations_dir, rel_path)
594 with open(path, 'w') as fh:
595 json.dump(expectations_dict, fh)
596 return path
597
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +0000598 def _generate_skps(self):
599 """Runs the skpmaker binary to generate files in self._input_skp_dir."""
600 self._run_skpmaker(
601 output_path=os.path.join(self._input_skp_dir, 'red.skp'), red=255)
602 self._run_skpmaker(
603 output_path=os.path.join(self._input_skp_dir, 'green.skp'), green=255)
604
605 def _run_skpmaker(self, output_path, red=0, green=0, blue=0,
606 width=640, height=400):
607 """Runs the skpmaker binary to generate SKP with known characteristics.
608
609 Args:
610 output_path: Filepath to write the SKP into.
611 red: Value of red color channel in image, 0-255.
612 green: Value of green color channel in image, 0-255.
613 blue: Value of blue color channel in image, 0-255.
614 width: Width of canvas to create.
615 height: Height of canvas to create.
616 """
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000617 binary = self.find_path_to_program('skpmaker')
618 return self.run_command([binary,
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +0000619 '--red', str(red),
620 '--green', str(green),
621 '--blue', str(blue),
622 '--width', str(width),
623 '--height', str(height),
624 '--writePath', str(output_path),
625 ])
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000626
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000627 def _assert_directory_contents(self, dir_path, expected_filenames):
628 """Asserts that files found in a dir are identical to expected_filenames.
629
630 Args:
631 dir_path: Path to a directory on local disk.
632 expected_filenames: Set containing the expected filenames within the dir.
633
634 Raises:
635 AssertionError: contents of the directory are not identical to
636 expected_filenames.
637 """
638 self.assertEqual(set(os.listdir(dir_path)), set(expected_filenames))
639
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000640 def _assert_json_contents(self, json_path, expected_dict):
641 """Asserts that contents of a JSON file are identical to expected_dict.
642
643 Args:
644 json_path: Path to a JSON file.
645 expected_dict: Dictionary indicating the expected contents of the JSON
646 file.
647
648 Raises:
649 AssertionError: contents of the JSON file are not identical to
650 expected_dict.
651 """
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000652 prettyprinted_expected_dict = json.dumps(expected_dict, sort_keys=True,
653 indent=2)
654 with open(json_path, 'r') as fh:
655 prettyprinted_json_dict = json.dumps(json.load(fh), sort_keys=True,
656 indent=2)
657 self.assertMultiLineEqual(prettyprinted_expected_dict,
658 prettyprinted_json_dict)
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000659
660
661def main():
662 base_unittest.main(RenderPicturesTest)
663
664
665if __name__ == '__main__':
666 main()