blob: 162531a6adc698acc1f056912a3f5d9269bbce2c [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
13import json
14import os
15import shutil
16import tempfile
17
18# Imports from within Skia
19import base_unittest
20
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +000021# Maximum length of text diffs to show when tests fail
22MAX_DIFF_LENGTH = 30000
23
commit-bot@chromium.org11f15622014-01-07 17:03:40 +000024
25class RenderPicturesTest(base_unittest.TestCase):
26
27 def setUp(self):
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +000028 self._input_skp_dir = tempfile.mkdtemp()
commit-bot@chromium.org11f15622014-01-07 17:03:40 +000029 self._temp_dir = tempfile.mkdtemp()
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +000030 self.maxDiff = MAX_DIFF_LENGTH
commit-bot@chromium.org11f15622014-01-07 17:03:40 +000031
32 def tearDown(self):
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +000033 shutil.rmtree(self._input_skp_dir)
commit-bot@chromium.org11f15622014-01-07 17:03:40 +000034 shutil.rmtree(self._temp_dir)
35
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +000036 def test_tiled_whole_image(self):
commit-bot@chromium.org11f15622014-01-07 17:03:40 +000037 """Run render_pictures with tiles and --writeWholeImage flag."""
commit-bot@chromium.org11f15622014-01-07 17:03:40 +000038 output_json_path = os.path.join(self._temp_dir, 'output.json')
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +000039 self._generate_skps()
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000040 # 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.orgf11943f2014-03-12 20:09:46 +000044 self._run_render_pictures(['-r', self._input_skp_dir,
commit-bot@chromium.org11f15622014-01-07 17:03:40 +000045 '--bbh', 'grid', '256', '256',
46 '--mode', 'tile', '256', '256',
47 '--writeJsonSummaryPath', output_json_path,
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000048 '--writePath', self._temp_dir,
commit-bot@chromium.org11f15622014-01-07 17:03:40 +000049 '--writeWholeImage'])
50 expected_summary_dict = {
51 "actual-results" : {
52 "no-comparison" : {
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +000053 # Manually verified: 640x400 red rectangle with black border
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +000054 "red.png" : [ "bitmap-64bitMD5", 11092453015575919668 ],
55 # Manually verified: 640x400 green rectangle with black border
56 "green.png" : [ "bitmap-64bitMD5", 8891695120562235492 ],
commit-bot@chromium.org11f15622014-01-07 17:03:40 +000057 }
58 }
commit-bot@chromium.org238771c2014-01-15 16:33:31 +000059 }
commit-bot@chromium.org11f15622014-01-07 17:03:40 +000060 self._assert_json_contents(output_json_path, expected_summary_dict)
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000061 self._assert_directory_contents(
62 self._temp_dir, ['red.png', 'green.png', 'output.json'])
commit-bot@chromium.org11f15622014-01-07 17:03:40 +000063
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +000064 def test_untiled(self):
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +000065 """Run without tiles."""
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +000066 output_json_path = os.path.join(self._temp_dir, 'output.json')
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +000067 self._generate_skps()
68 self._run_render_pictures(['-r', self._input_skp_dir,
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +000069 '--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.orgf11943f2014-03-12 20:09:46 +000075 "red.png" : ["bitmap-64bitMD5", 11092453015575919668],
76 # Manually verified: 640x400 green rectangle with black border
77 "green.png" : ["bitmap-64bitMD5", 8891695120562235492],
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +000078 }
79 }
80 }
81 self._assert_json_contents(output_json_path, expected_summary_dict)
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000082 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.org11f15622014-01-07 17:03:40 +0000108
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +0000109 def test_untiled_validate(self):
110 """Same as test_untiled, but with --validate.
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +0000111
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.orgf11943f2014-03-12 20:09:46 +0000114 test_untiled.
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +0000115 See https://code.google.com/p/skia/issues/detail?id=2044 ('render_pictures:
116 --validate fails')
117 """
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +0000118 output_json_path = os.path.join(self._temp_dir, 'output.json')
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +0000119 self._generate_skps()
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +0000120 with self.assertRaises(Exception):
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +0000121 self._run_render_pictures(['-r', self._input_skp_dir,
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +0000122 '--validate',
123 '--writePath', self._temp_dir,
124 '--writeJsonSummaryPath', output_json_path])
125
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +0000126 def test_untiled_without_writePath(self):
127 """Same as test_untiled, but without --writePath.
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +0000128
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.orgc3147c62014-01-15 20:35:54 +0000133 output_json_path = os.path.join(self._temp_dir, 'output.json')
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +0000134 self._generate_skps()
135 self._run_render_pictures(['-r', self._input_skp_dir,
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +0000136 '--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.orgf11943f2014-03-12 20:09:46 +0000144 def test_tiled(self):
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +0000145 """Generate individual tiles."""
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000146 output_json_path = os.path.join(self._temp_dir, 'output.json')
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +0000147 self._generate_skps()
148 self._run_render_pictures(['-r', self._input_skp_dir,
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000149 '--bbh', 'grid', '256', '256',
150 '--mode', 'tile', '256', '256',
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +0000151 '--writePath', self._temp_dir,
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000152 '--writeJsonSummaryPath', output_json_path])
153 expected_summary_dict = {
154 "actual-results" : {
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +0000155 "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.orgf11943f2014-03-12 20:09:46 +0000159 "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.org11f15622014-01-07 17:03:40 +0000174 }
175 }
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +0000176 }
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000177 self._assert_json_contents(output_json_path, expected_summary_dict)
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000178 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.org11f15622014-01-07 17:03:40 +0000234
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.org11f15622014-01-07 17:03:40 +0000240 ] + args)
241
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +0000242 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.org11f15622014-01-07 17:03:40 +0000261 binary = self.find_path_to_program('skpmaker')
262 return self.run_command([binary,
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +0000263 '--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.org11f15622014-01-07 17:03:40 +0000270
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000271 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.org11f15622014-01-07 17:03:40 +0000285 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
302def main():
303 base_unittest.main(RenderPicturesTest)
304
305
306if __name__ == '__main__':
307 main()