2011-02-08 Hayato Ito <hayato@chromium.org>
[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 _copy_image(self, filename, actual_image, expected_image):
53         self.write_output_files(filename, '.png',
54                                 output=actual_image, expected=expected_image,
55                                 encoding=None, print_text_diffs=False)
56
57     def _copy_image_hash(self, filename, actual_image_hash, expected_image_hash):
58         self.write_output_files(filename, '.checksum',
59                                 actual_image_hash, expected_image_hash,
60                                 encoding="ascii", print_text_diffs=False)
61
62     def _create_diff_image(self, port, filename, actual_image, expected_image):
63         """Creates the visual diff of the expected/actual PNGs.
64
65         Returns True if the images are different.
66         """
67         diff_filename = self.output_filename(filename,
68                                              self.FILENAME_SUFFIX_COMPARE)
69         return port.diff_image(actual_image, expected_image, diff_filename)
70
71     def compare_output(self, port, filename, options, actual_driver_output,
72                        expected_driver_output):
73         """Implementation of CompareOutput that checks the output image and
74         checksum against the expected files from the LayoutTest directory.
75         """
76         failures = []
77
78         # If we didn't produce a hash file, this test must be text-only.
79         if actual_driver_output.image_hash is None:
80             return failures
81
82         if not expected_driver_output.image:
83             # Report a missing expected PNG file.
84             self._copy_image(filename, actual_driver_output.image, expected_image=None)
85             self._copy_image_hash(filename, actual_driver_output.image_hash,
86                                   expected_driver_output.image_hash)
87             failures.append(test_failures.FailureMissingImage())
88             return failures
89         if not expected_driver_output.image_hash:
90             # Report a missing expected checksum file.
91             self._copy_image(filename, actual_driver_output.image,
92                              expected_driver_output.image)
93             self._copy_image_hash(filename, actual_driver_output.image_hash,
94                                   expected_image_hash=None)
95             failures.append(test_failures.FailureMissingImageHash())
96             return failures
97
98         if actual_driver_output.image_hash == expected_driver_output.image_hash:
99             # Hash matched (no diff needed, okay to return).
100             return failures
101
102         self._copy_image(filename, actual_driver_output.image,
103                          expected_driver_output.image)
104         self._copy_image_hash(filename, actual_driver_output.image_hash,
105                               expected_driver_output.image_hash)
106
107         # Even though we only use the result in one codepath below but we
108         # still need to call CreateImageDiff for other codepaths.
109         images_are_different = self._create_diff_image(port, filename,
110                                                        actual_driver_output.image,
111                                                        expected_driver_output.image)
112         if not images_are_different:
113             failures.append(test_failures.FailureImageHashIncorrect())
114         else:
115             failures.append(test_failures.FailureImageHashMismatch())
116
117         return failures