blob: fa530d5f8bf74c497f53ee6c31256e61ab99a532 [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()
40 self._run_render_pictures(['-r', self._input_skp_dir,
commit-bot@chromium.org11f15622014-01-07 17:03:40 +000041 '--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.orgc3147c62014-01-15 20:35:54 +000048 # Manually verified: 640x400 red rectangle with black border
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +000049 "red.png" : [ "bitmap-64bitMD5", 11092453015575919668 ],
50 # Manually verified: 640x400 green rectangle with black border
51 "green.png" : [ "bitmap-64bitMD5", 8891695120562235492 ],
commit-bot@chromium.org11f15622014-01-07 17:03:40 +000052 }
53 }
commit-bot@chromium.org238771c2014-01-15 16:33:31 +000054 }
commit-bot@chromium.org11f15622014-01-07 17:03:40 +000055 self._assert_json_contents(output_json_path, expected_summary_dict)
56
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +000057 def test_untiled(self):
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +000058 """Run without tiles."""
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +000059 output_json_path = os.path.join(self._temp_dir, 'output.json')
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +000060 self._generate_skps()
61 self._run_render_pictures(['-r', self._input_skp_dir,
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +000062 '--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.orgf11943f2014-03-12 20:09:46 +000068 "red.png" : ["bitmap-64bitMD5", 11092453015575919668],
69 # Manually verified: 640x400 green rectangle with black border
70 "green.png" : ["bitmap-64bitMD5", 8891695120562235492],
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +000071 }
72 }
73 }
74 self._assert_json_contents(output_json_path, expected_summary_dict)
commit-bot@chromium.org11f15622014-01-07 17:03:40 +000075
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +000076 def test_untiled_validate(self):
77 """Same as test_untiled, but with --validate.
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +000078
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.orgf11943f2014-03-12 20:09:46 +000081 test_untiled.
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +000082 See https://code.google.com/p/skia/issues/detail?id=2044 ('render_pictures:
83 --validate fails')
84 """
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +000085 output_json_path = os.path.join(self._temp_dir, 'output.json')
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +000086 self._generate_skps()
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +000087 with self.assertRaises(Exception):
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +000088 self._run_render_pictures(['-r', self._input_skp_dir,
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +000089 '--validate',
90 '--writePath', self._temp_dir,
91 '--writeJsonSummaryPath', output_json_path])
92
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +000093 def test_untiled_without_writePath(self):
94 """Same as test_untiled, but without --writePath.
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +000095
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.orgc3147c62014-01-15 20:35:54 +0000100 output_json_path = os.path.join(self._temp_dir, 'output.json')
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +0000101 self._generate_skps()
102 self._run_render_pictures(['-r', self._input_skp_dir,
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +0000103 '--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.orgf11943f2014-03-12 20:09:46 +0000111 def test_tiled(self):
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +0000112 """Generate individual tiles."""
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000113 output_json_path = os.path.join(self._temp_dir, 'output.json')
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +0000114 self._generate_skps()
115 self._run_render_pictures(['-r', self._input_skp_dir,
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000116 '--bbh', 'grid', '256', '256',
117 '--mode', 'tile', '256', '256',
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +0000118 '--writePath', self._temp_dir,
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000119 '--writeJsonSummaryPath', output_json_path])
120 expected_summary_dict = {
121 "actual-results" : {
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +0000122 "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.orgf11943f2014-03-12 20:09:46 +0000126 "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.org11f15622014-01-07 17:03:40 +0000141 }
142 }
commit-bot@chromium.orgc3147c62014-01-15 20:35:54 +0000143 }
commit-bot@chromium.org11f15622014-01-07 17:03:40 +0000144 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.org11f15622014-01-07 17:03:40 +0000151 ] + args)
152
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +0000153 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.org11f15622014-01-07 17:03:40 +0000172 binary = self.find_path_to_program('skpmaker')
173 return self.run_command([binary,
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +0000174 '--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.org11f15622014-01-07 17:03:40 +0000181
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
199def main():
200 base_unittest.main(RenderPicturesTest)
201
202
203if __name__ == '__main__':
204 main()