summaryrefslogtreecommitdiffstats
path: root/Tools/Scripts/webkitpy/layout_tests/test_types/image_diff.py
blob: 1d7e1072476d436d4e8d22abbc9088397906dfbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python
# Copyright (C) 2010 Google Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""Compares the image output of a test to the expected image output.

Compares hashes for the generated and expected images. If the output doesn't
match, returns FailureImageHashMismatch and outputs both hashes into the layout
test results directory.
"""

import errno
import logging

from webkitpy.layout_tests.layout_package import test_failures
from webkitpy.layout_tests.test_types import test_type_base

# Cache whether we have the image_diff executable available.
_compare_available = True
_compare_msg_printed = False

_log = logging.getLogger("webkitpy.layout_tests.test_types.image_diff")


class ImageDiff(test_type_base.TestTypeBase):

    def _copy_image(self, filename, actual_image, expected_image):
        self.write_output_files(filename, '.png',
                                output=actual_image, expected=expected_image,
                                encoding=None, print_text_diffs=False)

    def _copy_image_hash(self, filename, actual_image_hash, expected_image_hash):
        self.write_output_files(filename, '.checksum',
                                actual_image_hash, expected_image_hash,
                                encoding="ascii", print_text_diffs=False)

    def _create_diff_image(self, port, filename, actual_image, expected_image):
        """Creates the visual diff of the expected/actual PNGs.

        Returns True if the images are different.
        """
        diff_filename = self.output_filename(filename,
                                             self.FILENAME_SUFFIX_COMPARE)
        return port.diff_image(actual_image, expected_image, diff_filename)

    def compare_output(self, port, filename, options, actual_driver_output,
                       expected_driver_output):
        """Implementation of CompareOutput that checks the output image and
        checksum against the expected files from the LayoutTest directory.
        """
        failures = []

        # If we didn't produce a hash file, this test must be text-only.
        if actual_driver_output.image_hash is None:
            return failures

        if not expected_driver_output.image:
            # Report a missing expected PNG file.
            self._copy_image(filename, actual_driver_output.image, expected_image=None)
            self._copy_image_hash(filename, actual_driver_output.image_hash,
                                  expected_driver_output.image_hash)
            failures.append(test_failures.FailureMissingImage())
            return failures
        if not expected_driver_output.image_hash:
            # Report a missing expected checksum file.
            self._copy_image(filename, actual_driver_output.image,
                             expected_driver_output.image)
            self._copy_image_hash(filename, actual_driver_output.image_hash,
                                  expected_image_hash=None)
            failures.append(test_failures.FailureMissingImageHash())
            return failures

        if actual_driver_output.image_hash == expected_driver_output.image_hash:
            # Hash matched (no diff needed, okay to return).
            return failures

        self._copy_image(filename, actual_driver_output.image,
                         expected_driver_output.image)
        self._copy_image_hash(filename, actual_driver_output.image_hash,
                              expected_driver_output.image_hash)

        # Even though we only use the result in one codepath below but we
        # still need to call CreateImageDiff for other codepaths.
        images_are_different = self._create_diff_image(port, filename,
                                                       actual_driver_output.image,
                                                       expected_driver_output.image)
        if not images_are_different:
            failures.append(test_failures.FailureImageHashIncorrect())
        else:
            failures.append(test_failures.FailureImageHashMismatch())

        return failures