aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/tests/render_pictures_test.py
blob: ea14b608d3112d0363283cbd1a892d4f1b61286b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/python

"""
Copyright 2014 Google Inc.

Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.

Test the render_pictures binary.

TODO(epoger): Combine with overlapping tools/test_rendering.py and
tools/test_pictures.py .
See https://code.google.com/p/skia/issues/detail?id=1943#c2
"""

# System-level imports
import json
import os
import shutil
import tempfile

# Imports from within Skia
import base_unittest


class RenderPicturesTest(base_unittest.TestCase):

  def setUp(self):
    self._temp_dir = tempfile.mkdtemp()

  def tearDown(self):
    shutil.rmtree(self._temp_dir)

  def test_tiled_whole_image_no_comparison(self):
    """Run render_pictures with tiles and --writeWholeImage flag."""
    input_skp_path = os.path.join(self._temp_dir, 'input.skp')
    output_json_path = os.path.join(self._temp_dir, 'output.json')
    self._run_skpmaker(['--writePath', input_skp_path])
    self._run_render_pictures(['-r', input_skp_path,
                               '--bbh', 'grid', '256', '256',
                               '--mode', 'tile', '256', '256',
                               '--writeJsonSummaryPath', output_json_path,
                               '--writeWholeImage'])
    expected_summary_dict = {
        "actual-results" : {
            "no-comparison" : {
                "input.png" : [ "bitmap-64bitMD5", 12793741875005523433 ]
                }
            }
        }
    self._assert_json_contents(output_json_path, expected_summary_dict)

  def test_tiled_no_comparison(self):
    """Generate individual tiles.

    TODO(epoger): The results of this test are currently broken!
    The summary should contain a list of tiles, but for some reason, it is
    empty."""
    input_skp_path = os.path.join(self._temp_dir, 'input.skp')
    output_json_path = os.path.join(self._temp_dir, 'output.json')
    self._run_skpmaker(['--writePath', input_skp_path])
    self._run_render_pictures(['-r', input_skp_path,
                               '--bbh', 'grid', '256', '256',
                               '--mode', 'tile', '256', '256',
                               '--writeJsonSummaryPath', output_json_path])
    expected_summary_dict = {
        "actual-results" : {
            "no-comparison" : None
            }
        }
    self._assert_json_contents(output_json_path, expected_summary_dict)

  def test_untiled_no_comparison(self):
    """Run without tiles.

    TODO(epoger): The results of this test are currently broken!
    The summary should contain a single image, but for some reason, it is
    empty."""
    input_skp_path = os.path.join(self._temp_dir, 'input.skp')
    output_json_path = os.path.join(self._temp_dir, 'output.json')
    self._run_skpmaker(['--writePath', input_skp_path])
    self._run_render_pictures(['-r', input_skp_path,
                               '--writeJsonSummaryPath', output_json_path])
    expected_summary_dict = {
        "actual-results" : {
            "no-comparison" : None
            }
        }
    self._assert_json_contents(output_json_path, expected_summary_dict)

  def _run_render_pictures(self, args):
    binary = self.find_path_to_program('render_pictures')
    return self.run_command([binary,
                             '--clone', '1',
                             '--config', '8888',
                             '--validate'
                             ] + args)

  def _run_skpmaker(self, args):
    binary = self.find_path_to_program('skpmaker')
    return self.run_command([binary,
                             '--red', '255',
                             '--green', '0',
                             '--blue', '0',
                             '--width', '640',
                             '--height', '400',
                             ] + args)

  def _assert_json_contents(self, json_path, expected_dict):
    """Asserts that contents of a JSON file are identical to expected_dict.

    Args:
      json_path: Path to a JSON file.
      expected_dict: Dictionary indicating the expected contents of the JSON
                     file.

    Raises:
      AssertionError: contents of the JSON file are not identical to
                      expected_dict.
    """
    file_contents = open(json_path, 'r').read()
    actual_dict = json.loads(file_contents)
    self.assertEqual(actual_dict, expected_dict)


def main():
  base_unittest.main(RenderPicturesTest)


if __name__ == '__main__':
  main()