0e6bc1fb4eb1f2ad7f6150145ed83e0fcad90e35
[WebKit-https.git] / Tools / Scripts / webkitpy / layout_tests / test_types / image_diff.py
1 #!/usr/bin/env python
2 # Copyright (C) 2010 Google Inc. All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7 #
8 #     * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 #     * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
13 # distribution.
14 #     * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 """Compares the image output of a test to the expected image output.
31
32 Compares hashes for the generated and expected images. If the output doesn't
33 match, returns FailureImageHashMismatch and outputs both hashes into the layout
34 test results directory.
35 """
36
37 import errno
38 import logging
39
40 from webkitpy.layout_tests.layout_package import test_failures
41 from webkitpy.layout_tests.test_types import test_type_base
42
43 # Cache whether we have the image_diff executable available.
44 _compare_available = True
45 _compare_msg_printed = False
46
47 _log = logging.getLogger("webkitpy.layout_tests.test_types.image_diff")
48
49
50 class ImageDiff(test_type_base.TestTypeBase):
51
52     def _save_baseline_files(self, filename, image, image_hash,
53                              generate_new_baseline):
54         """Saves new baselines for the PNG and checksum.
55
56         Args:
57           filename: test filename
58           image: a image output
59           image_hash: a checksum of the image
60           generate_new_baseline: whether to generate a new, platform-specific
61             baseline, or update the existing one
62         """
63         self._save_baseline_data(filename, image, ".png", encoding=None,
64                                  generate_new_baseline=generate_new_baseline)
65         self._save_baseline_data(filename, image_hash, ".checksum",
66                                  encoding="ascii",
67                                  generate_new_baseline=generate_new_baseline)
68
69     def _copy_image(self, filename, actual_image, expected_image):
70         self.write_output_files(filename, '.png',
71                                 output=actual_image, expected=expected_image,
72                                 encoding=None, print_text_diffs=False)
73
74     def _copy_image_hash(self, filename, actual_image_hash, expected_image_hash):
75         self.write_output_files(filename, '.checksum',
76                                 actual_image_hash, expected_image_hash,
77                                 encoding="ascii", print_text_diffs=False)
78
79     def _create_diff_image(self, port, filename, actual_image, expected_image):
80         """Creates the visual diff of the expected/actual PNGs.
81
82         Returns True if the images are different.
83         """
84         diff_filename = self.output_filename(filename,
85                                              self.FILENAME_SUFFIX_COMPARE)
86         return port.diff_image(actual_image, expected_image, diff_filename)
87
88     def compare_output(self, port, filename, options, actual_driver_output,
89                        expected_driver_output):
90         """Implementation of CompareOutput that checks the output image and
91         checksum against the expected files from the LayoutTest directory.
92         """
93         failures = []
94
95         # If we didn't produce a hash file, this test must be text-only.
96         if actual_driver_output.image_hash is None:
97             return failures
98
99         # If we're generating a new baseline, we pass.
100         if options.new_baseline or options.reset_results:
101             self._save_baseline_files(filename, actual_driver_output.image,
102                                       actual_driver_output.image_hash,
103                                       options.new_baseline)
104             return failures
105
106         if not expected_driver_output.image:
107             # Report a missing expected PNG file.
108             self._copy_image(filename, actual_driver_output.image, expected_image=None)
109             self._copy_image_hash(filename, actual_driver_output.image_hash,
110                                   expected_driver_output.image_hash)
111             failures.append(test_failures.FailureMissingImage())
112             return failures
113         if not expected_driver_output.image_hash:
114             # Report a missing expected checksum file.
115             self._copy_image(filename, actual_driver_output.image,
116                              expected_driver_output.image)
117             self._copy_image_hash(filename, actual_driver_output.image_hash,
118                                   expected_image_hash=None)
119             failures.append(test_failures.FailureMissingImageHash())
120             return failures
121
122         if actual_driver_output.image_hash == expected_driver_output.image_hash:
123             # Hash matched (no diff needed, okay to return).
124             return failures
125
126         self._copy_image(filename, actual_driver_output.image,
127                          expected_driver_output.image)
128         self._copy_image_hash(filename, actual_driver_output.image_hash,
129                               expected_driver_output.image_hash)
130
131         # Even though we only use the result in one codepath below but we
132         # still need to call CreateImageDiff for other codepaths.
133         images_are_different = self._create_diff_image(port, filename,
134                                                        actual_driver_output.image,
135                                                        expected_driver_output.image)
136         if not images_are_different:
137             failures.append(test_failures.FailureImageHashIncorrect())
138         else:
139             failures.append(test_failures.FailureImageHashMismatch())
140
141         return failures