Clean up ChunkedUpdateDrawingAreaProxy
[WebKit-https.git] / WebKitTools / Scripts / webkitpy / layout_tests / rebaseline_chromium_webkit_tests_unittest.py
1 #!/usr/bin/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 """Unit tests for rebaseline_chromium_webkit_tests.py."""
31
32 import os
33 import sys
34 import unittest
35
36 from webkitpy.tool import mocktool
37 from webkitpy.layout_tests import port
38 from webkitpy.layout_tests import rebaseline_chromium_webkit_tests
39 from webkitpy.common.system.executive import Executive, ScriptError
40
41
42 class MockPort(object):
43     def __init__(self, image_diff_exists):
44         self.image_diff_exists = image_diff_exists
45
46     def check_image_diff(self, override_step, logging):
47         return self.image_diff_exists
48
49
50 def get_mock_get(config_expectations):
51     def mock_get(port_name, options):
52         return MockPort(config_expectations[options.configuration])
53     return mock_get
54
55
56 class TestGetHostPortObject(unittest.TestCase):
57     def assert_result(self, release_present, debug_present, valid_port_obj):
58         # Tests whether we get a valid port object returned when we claim
59         # that Image diff is (or isn't) present in the two configs.
60         port.get = get_mock_get({'Release': release_present,
61                                  'Debug': debug_present})
62         options = mocktool.MockOptions(configuration=None,
63                                        html_directory=None)
64         port_obj = rebaseline_chromium_webkit_tests.get_host_port_object(
65             options)
66         if valid_port_obj:
67             self.assertNotEqual(port_obj, None)
68         else:
69             self.assertEqual(port_obj, None)
70
71     def test_get_host_port_object(self):
72         # Save the normal port.get() function for future testing.
73         old_get = port.get
74
75         # Test whether we get a valid port object back for the four
76         # possible cases of having ImageDiffs built. It should work when
77         # there is at least one binary present.
78         self.assert_result(False, False, False)
79         self.assert_result(True, False, True)
80         self.assert_result(False, True, True)
81         self.assert_result(True, True, True)
82
83         # Restore the normal port.get() function.
84         port.get = old_get
85
86
87 class TestRebaseliner(unittest.TestCase):
88     def make_rebaseliner(self):
89         options = mocktool.MockOptions(configuration=None,
90                                        html_directory=None)
91         host_port_obj = port.get('test', options)
92         target_options = options
93         target_port_obj = port.get('test', target_options)
94         platform = 'test'
95         return rebaseline_chromium_webkit_tests.Rebaseliner(
96             host_port_obj, target_port_obj, platform, options)
97
98     def test_parse_options(self):
99         (options, target_options) = rebaseline_chromium_webkit_tests.parse_options([])
100         self.assertTrue(target_options.chromium)
101         self.assertEqual(options.tolerance, 0)
102
103         (options, target_options) = rebaseline_chromium_webkit_tests.parse_options(['--target-platform', 'qt'])
104         self.assertFalse(hasattr(target_options, 'chromium'))
105         self.assertEqual(options.tolerance, 0)
106
107     def test_noop(self):
108         # this method tests that was can at least instantiate an object, even
109         # if there is nothing to do.
110         rebaseliner = self.make_rebaseliner()
111         self.assertNotEqual(rebaseliner, None)
112
113     def test_diff_baselines_txt(self):
114         rebaseliner = self.make_rebaseliner()
115         output = rebaseliner._port.expected_text(
116             os.path.join(rebaseliner._port.layout_tests_dir(),
117                          'passes/text.html'))
118         self.assertFalse(rebaseliner._diff_baselines(output, output,
119                                                      is_image=False))
120
121     def test_diff_baselines_png(self):
122         rebaseliner = self.make_rebaseliner()
123         image = rebaseliner._port.expected_image(
124             os.path.join(rebaseliner._port.layout_tests_dir(),
125                          'passes/image.html'))
126         self.assertFalse(rebaseliner._diff_baselines(image, image,
127                                                      is_image=True))
128
129
130 class TestHtmlGenerator(unittest.TestCase):
131     def make_generator(self, tests):
132         return rebaseline_chromium_webkit_tests.HtmlGenerator(
133             target_port=None,
134             options=mocktool.MockOptions(configuration=None,
135                                          html_directory='/tmp'),
136             platforms=['mac'],
137             rebaselining_tests=tests,
138             executive=Executive())
139
140     def test_generate_baseline_links(self):
141         orig_platform = sys.platform
142         orig_exists = os.path.exists
143
144         try:
145             sys.platform = 'darwin'
146             os.path.exists = lambda x: True
147             generator = self.make_generator(["foo.txt"])
148             links = generator._generate_baseline_links("foo", ".txt", "mac")
149             expected_links = '<td align=center><a href="file:///tmp/foo-expected-mac-old.txt">foo-expected.txt</a></td><td align=center><a href="file:///tmp/foo-expected-mac-new.txt">foo-expected.txt</a></td><td align=center><a href="file:///tmp/foo-expected-mac-diff.txt">Diff</a></td>'
150             self.assertEqual(links, expected_links)
151         finally:
152             sys.platform = orig_platform
153             os.path.exists = orig_exists
154
155
156 if __name__ == '__main__':
157     unittest.main()