Clean up ChunkedUpdateDrawingAreaProxy
[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, filename, 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
100         # If we didn't produce a hash file, this test must be text-only.
101         if actual_test_output.image_hash is None:
102             return failures
103
104         # If we're generating a new baseline, we pass.
105         if test_args.new_baseline or test_args.reset_results:
106             self._save_baseline_files(filename, actual_test_output.image,
107                                       actual_test_output.image_hash,
108                                       test_args.new_baseline)
109             return failures
110
111         if not expected_test_output.image:
112             # Report a missing expected PNG file.
113             self._copy_image(filename, actual_test_output.image, expected_image=None)
114             self._copy_image_hash(filename, actual_test_output.image_hash,
115                                   expected_test_output.image_hash)
116             failures.append(test_failures.FailureMissingImage())
117             return failures
118         if not expected_test_output.image_hash:
119             # Report a missing expected checksum file.
120             self._copy_image(filename, actual_test_output.image,
121                              expected_test_output.image)
122             self._copy_image_hash(filename, actual_test_output.image_hash,
123                                   expected_image_hash=None)
124             failures.append(test_failures.FailureMissingImageHash())
125             return failures
126
127         if actual_test_output.image_hash == expected_test_output.image_hash:
128             # Hash matched (no diff needed, okay to return).
129             return failures
130
131         self._copy_image(filename, actual_test_output.image,
132                          expected_test_output.image)
133         self._copy_image_hash(filename, actual_test_output.image_hash,
134                               expected_test_output.image_hash)
135
136         # Even though we only use the result in one codepath below but we
137         # still need to call CreateImageDiff for other codepaths.
138         images_are_different = self._create_diff_image(port, filename,
139                                                        actual_test_output.image,
140                                                        expected_test_output.image)
141         if not images_are_different:
142             failures.append(test_failures.FailureImageHashIncorrect())
143         else:
144             failures.append(test_failures.FailureImageHashMismatch())
145
146         return failures