1#!/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 compare_rendered_pictures.py
10
11TODO(epoger): Create a command to update the expected results (in
12self._output_dir_expected) when appropriate.  For now, you should:
131. examine the results in self._output_dir_actual and make sure they are ok
142. rm -rf self._output_dir_expected
153. mv self._output_dir_actual self._output_dir_expected
16Although, if you're using an SVN checkout, this will blow away .svn directories
17within self._output_dir_expected, which wouldn't be good...
18
19"""
20
21import os
22import subprocess
23import sys
24
25# Imports from within Skia
26import base_unittest
27import compare_rendered_pictures
28import results
29import gm_json  # must import results first, so that gm_json will be in sys.path
30
31
32class CompareRenderedPicturesTest(base_unittest.TestCase):
33
34  def test_endToEnd(self):
35    """Generate two sets of SKPs, run render_pictures over both, and compare
36    the results."""
37    self._generate_skps_and_run_render_pictures(
38        subdir='before_patch', skpdict={
39            'changed.skp': 200,
40            'unchanged.skp': 100,
41            'only-in-before.skp': 128,
42        })
43    self._generate_skps_and_run_render_pictures(
44        subdir='after_patch', skpdict={
45            'changed.skp': 201,
46            'unchanged.skp': 100,
47            'only-in-after.skp': 128,
48        })
49
50    results_obj = compare_rendered_pictures.RenderedPicturesComparisons(
51        actuals_root=self._temp_dir,
52        subdirs=('before_patch', 'after_patch'),
53        generated_images_root=self._temp_dir,
54        diff_base_url='/static/generated-images')
55    results_obj.get_timestamp = mock_get_timestamp
56
57    gm_json.WriteToFile(
58        results_obj.get_packaged_results_of_type(
59            results.KEY__HEADER__RESULTS_ALL),
60        os.path.join(self._output_dir_actual, 'compare_rendered_pictures.json'))
61
62  def _generate_skps_and_run_render_pictures(self, subdir, skpdict):
63    """Generate SKPs and run render_pictures on them.
64
65    Args:
66      subdir: subdirectory (within self._temp_dir) to write all files into
67      skpdict: {skpname: redvalue} dictionary describing the SKP files to render
68    """
69    out_path = os.path.join(self._temp_dir, subdir)
70    os.makedirs(out_path)
71    for skpname, redvalue in skpdict.iteritems():
72      self._run_skpmaker(
73          output_path=os.path.join(out_path, skpname), red=redvalue)
74
75    # TODO(epoger): Add --mode tile 256 256 --writeWholeImage to the unittest,
76    # and fix its result!  (imageURLs within whole-image entries are wrong when
77    # I tried adding that)
78    binary = self.find_path_to_program('render_pictures')
79    return subprocess.check_output([
80        binary,
81        '--clone', '1',
82        '--config', '8888',
83        '-r', out_path,
84        '--writeChecksumBasedFilenames',
85        '--writeJsonSummaryPath', os.path.join(out_path, 'summary.json'),
86        '--writePath', out_path])
87
88  def _run_skpmaker(self, output_path, red=0, green=0, blue=0,
89                    width=640, height=400):
90    """Runs the skpmaker binary to generate SKP with known characteristics.
91
92    Args:
93      output_path: Filepath to write the SKP into.
94      red: Value of red color channel in image, 0-255.
95      green: Value of green color channel in image, 0-255.
96      blue: Value of blue color channel in image, 0-255.
97      width: Width of canvas to create.
98      height: Height of canvas to create.
99    """
100    binary = self.find_path_to_program('skpmaker')
101    return subprocess.check_output([
102        binary,
103        '--red', str(red),
104        '--green', str(green),
105        '--blue', str(blue),
106        '--width', str(width),
107        '--height', str(height),
108        '--writePath', str(output_path)])
109
110def mock_get_timestamp():
111  """Mock version of BaseComparisons.get_timestamp() for testing."""
112  return 12345678
113
114
115def main():
116  base_unittest.main(CompareRenderedPicturesTest)
117
118
119if __name__ == '__main__':
120  main()
121