2010-12-02 Hayato Ito <hayato@chromium.org>
[WebKit-https.git] / WebKitTools / 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 from __future__ import with_statement
38
39 import codecs
40 import errno
41 import logging
42 import os
43 import shutil
44
45 from webkitpy.layout_tests.layout_package import test_failures
46 from webkitpy.layout_tests.test_types import test_type_base
47
48 # Cache whether we have the image_diff executable available.
49 _compare_available = True
50 _compare_msg_printed = False
51
52 _log = logging.getLogger("webkitpy.layout_tests.test_types.image_diff")
53
54
55 class ImageDiff(test_type_base.TestTypeBase):
56
57     def _save_baseline_files(self, filename, image, image_hash,
58                              generate_new_baseline):
59         """Saves new baselines for the PNG and checksum.
60
61         Args:
62           filename: test filename
63           image: a image output
64           image_hash: a checksum of the image
65           generate_new_baseline: whether to generate a new, platform-specific
66             baseline, or update the existing one
67         """
68         self._save_baseline_data(filename, image, ".png", encoding=None,
69                                  generate_new_baseline=generate_new_baseline)
70         self._save_baseline_data(filename, image_hash, ".checksum",
71                                  encoding="ascii",
72                                  generate_new_baseline=generate_new_baseline)
73
74     def _copy_image(self, filename, actual_image, expected_image):
75         self.write_output_files(filename, '.png',
76                                 output=actual_image, expected=expected_image,
77                                 encoding=None, print_text_diffs=False)
78
79     def _copy_image_hash(self, filename, actual_image_hash, expected_image_hash):
80         self.write_output_files(filename, '.checksum',
81                                 actual_image_hash, expected_image_hash,
82                                 encoding="ascii", print_text_diffs=False)
83
84     def _create_diff_image(self, port, filename, actual_image, expected_image):
85         """Creates the visual diff of the expected/actual PNGs.
86
87         Returns True if the images are different.
88         """
89         diff_filename = self.output_filename(filename,
90                                              self.FILENAME_SUFFIX_COMPARE)
91         return port.diff_image(actual_image, expected_image, diff_filename)
92
93     def compare_output(self, port, test_input, test_args, actual_test_output,
94                        expected_test_output):
95         """Implementation of CompareOutput that checks the output image and
96         checksum against the expected files from the LayoutTest directory.
97         """
98         failures = []
99         filename = test_input.filename
100
101         # If we didn't produce a hash file, this test must be text-only.
102         if actual_test_output.image_hash is None:
103             return failures
104
105         # If we're generating a new baseline, we pass.
106         if test_args.new_baseline or test_args.reset_results:
107             self._save_baseline_files(filename, actual_test_output.image,
108                                       actual_test_output.image_hash,
109                                       test_args.new_baseline)
110             return failures
111
112         if not expected_test_output.image:
113             # Report a missing expected PNG file.
114             self._copy_image(filename, actual_test_output.image, expected_image=None)
115             self._copy_image_hash(filename, actual_test_output.image_hash,
116                                   expected_test_output.image_hash)
117             failures.append(test_failures.FailureMissingImage())
118             return failures
119         if not expected_test_output.image_hash:
120             # Report a missing expected checksum file.
121             self._copy_image(filename, actual_test_output.image,
122                              expected_test_output.image)
123             self._copy_image_hash(filename, actual_test_output.image_hash,
124                                   expected_image_hash=None)
125             failures.append(test_failures.FailureMissingImageHash())
126             return failures
127
128         if actual_test_output.image_hash == expected_test_output.image_hash:
129             # Hash matched (no diff needed, okay to return).
130             return failures
131
132         self._copy_image(filename, actual_test_output.image,
133                          expected_test_output.image)
134         self._copy_image_hash(filename, actual_test_output.image_hash,
135                               expected_test_output.image_hash)
136
137         # Even though we only use the result in one codepath below but we
138         # still need to call CreateImageDiff for other codepaths.
139         images_are_different = self._create_diff_image(port, filename,
140                                                        actual_test_output.image,
141                                                        expected_test_output.image)
142         if not images_are_different:
143             failures.append(test_failures.FailureImageHashIncorrect())
144         else:
145             failures.append(test_failures.FailureImageHashMismatch())
146
147         return failures