Clean up ChunkedUpdateDrawingAreaProxy
[WebKit-https.git] / Tools / Scripts / webkitpy / layout_tests / port / chromium_unittest.py
1 # Copyright (C) 2010 Google Inc. All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
5 # met:
6 #
7 #    * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 #    * Redistributions in binary form must reproduce the above
10 # copyright notice, this list of conditions and the following disclaimer
11 # in the documentation and/or other materials provided with the
12 # distribution.
13 #    * Neither the name of Google Inc. nor the names of its
14 # contributors may be used to endorse or promote products derived from
15 # this software without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 import os
30 import unittest
31 import StringIO
32
33 from webkitpy.tool import mocktool
34 from webkitpy.thirdparty.mock import Mock
35
36 import chromium
37 import chromium_linux
38 import chromium_mac
39 import chromium_win
40
41 class ChromiumDriverTest(unittest.TestCase):
42
43     def setUp(self):
44         mock_port = Mock()
45         mock_port.get_option = lambda option_name: ''
46         self.driver = chromium.ChromiumDriver(mock_port, worker_number=0)
47
48     def test_test_shell_command(self):
49         expected_command = "test.html 2 checksum\n"
50         self.assertEqual(self.driver._test_shell_command("test.html", 2, "checksum"), expected_command)
51
52     def _assert_write_command_and_read_line(self, input=None, expected_line=None, expected_stdin=None, expected_crash=False):
53         if not expected_stdin:
54             if input:
55                 expected_stdin = input
56             else:
57                 # We reset stdin, so we should expect stdin.getValue = ""
58                 expected_stdin = ""
59         self.driver._proc.stdin = StringIO.StringIO()
60         line, did_crash = self.driver._write_command_and_read_line(input)
61         self.assertEqual(self.driver._proc.stdin.getvalue(), expected_stdin)
62         self.assertEqual(line, expected_line)
63         self.assertEqual(did_crash, expected_crash)
64
65     def test_write_command_and_read_line(self):
66         self.driver._proc = Mock()
67         # Set up to read 3 lines before we get an IOError
68         self.driver._proc.stdout = StringIO.StringIO("first\nsecond\nthird\n")
69
70         unicode_input = u"I \u2661 Unicode"
71         utf8_input = unicode_input.encode("utf-8")
72         # Test unicode input conversion to utf-8
73         self._assert_write_command_and_read_line(input=unicode_input, expected_stdin=utf8_input, expected_line="first\n")
74         # Test str() input.
75         self._assert_write_command_and_read_line(input="foo", expected_line="second\n")
76         # Test input=None
77         self._assert_write_command_and_read_line(expected_line="third\n")
78         # Test reading from a closed/empty stream.
79         # reading from a StringIO does not raise IOError like a real file would, so raise IOError manually.
80         def mock_readline():
81             raise IOError
82         self.driver._proc.stdout.readline = mock_readline
83         self._assert_write_command_and_read_line(expected_crash=True)
84
85
86 class ChromiumPortTest(unittest.TestCase):
87     class TestMacPort(chromium_mac.ChromiumMacPort):
88         def __init__(self, options):
89             chromium_mac.ChromiumMacPort.__init__(self,
90                                                   port_name='test-port',
91                                                   options=options)
92
93         def default_configuration(self):
94             self.default_configuration_called = True
95             return 'default'
96
97     class TestLinuxPort(chromium_linux.ChromiumLinuxPort):
98         def __init__(self, options):
99             chromium_linux.ChromiumLinuxPort.__init__(self,
100                                                       port_name='test-port',
101                                                       options=options)
102
103         def default_configuration(self):
104             self.default_configuration_called = True
105             return 'default'
106
107     def test_path_to_image_diff(self):
108         mock_options = mocktool.MockOptions()
109         port = ChromiumPortTest.TestLinuxPort(options=mock_options)
110         self.assertTrue(port._path_to_image_diff().endswith(
111             '/out/default/ImageDiff'), msg=port._path_to_image_diff())
112         port = ChromiumPortTest.TestMacPort(options=mock_options)
113         self.assertTrue(port._path_to_image_diff().endswith(
114             '/xcodebuild/default/ImageDiff'))
115         mock_options = mocktool.MockOptions(use_test_shell=True)
116         port = ChromiumPortTest.TestLinuxPort(options=mock_options)
117         self.assertTrue(port._path_to_image_diff().endswith(
118             '/out/default/image_diff'), msg=port._path_to_image_diff())
119         port = ChromiumPortTest.TestMacPort(options=mock_options)
120         self.assertTrue(port._path_to_image_diff().endswith(
121             '/xcodebuild/default/image_diff'))
122         # FIXME: Figure out how this is going to work on Windows.
123         #port = chromium_win.ChromiumWinPort('test-port', options=MockOptions())
124
125     def test_skipped_layout_tests(self):
126         mock_options = mocktool.MockOptions()
127         port = ChromiumPortTest.TestLinuxPort(options=mock_options)
128
129         fake_test = os.path.join(port.layout_tests_dir(), "fast/js/not-good.js")
130
131         port.test_expectations = lambda: """BUG_TEST SKIP : fast/js/not-good.js = TEXT
132 LINUX WIN : fast/js/very-good.js = TIMEOUT PASS"""
133         port.test_expectations_overrides = lambda: ''
134         port.tests = lambda paths: set()
135         port.path_exists = lambda test: True
136
137         skipped_tests = port.skipped_layout_tests(extra_test_files=[fake_test, ])
138         self.assertTrue("fast/js/not-good.js" in skipped_tests)
139
140     def test_default_configuration(self):
141         mock_options = mocktool.MockOptions()
142         port = ChromiumPortTest.TestLinuxPort(options=mock_options)
143         self.assertEquals(mock_options.configuration, 'default')
144         self.assertTrue(port.default_configuration_called)
145
146         mock_options = mocktool.MockOptions(configuration=None)
147         port = ChromiumPortTest.TestLinuxPort(mock_options)
148         self.assertEquals(mock_options.configuration, 'default')
149         self.assertTrue(port.default_configuration_called)
150
151     def test_diff_image(self):
152         class TestPort(ChromiumPortTest.TestLinuxPort):
153             def _path_to_image_diff(self):
154                 return "/path/to/image_diff"
155
156         class MockExecute:
157             def __init__(self, result):
158                 self._result = result
159
160             def run_command(self,
161                             args,
162                             cwd=None,
163                             input=None,
164                             error_handler=None,
165                             return_exit_code=False,
166                             return_stderr=True,
167                             decode_output=False):
168                 if return_exit_code:
169                     return self._result
170                 return ''
171
172         mock_options = mocktool.MockOptions()
173         port = ChromiumPortTest.TestLinuxPort(mock_options)
174
175         # Images are different.
176         port._executive = MockExecute(0)
177         self.assertEquals(False, port.diff_image("EXPECTED", "ACTUAL"))
178
179         # Images are the same.
180         port._executive = MockExecute(1)
181         self.assertEquals(True, port.diff_image("EXPECTED", "ACTUAL"))
182
183         # There was some error running image_diff.
184         port._executive = MockExecute(2)
185         exception_raised = False
186         try:
187             port.diff_image("EXPECTED", "ACTUAL")
188         except ValueError, e:
189             exception_raised = True
190         self.assertFalse(exception_raised)
191
192 if __name__ == '__main__':
193     unittest.main()