1 # Copyright (C) 2010 Google Inc. All rights reserved.
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
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
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.
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.
32 from webkitpy.common.system import logtesting
33 from webkitpy.common.system import executive_mock
34 from webkitpy.common.system import filesystem_mock
35 from webkitpy.tool import mocktool
36 from webkitpy.thirdparty.mock import Mock
44 class ChromiumDriverTest(unittest.TestCase):
48 mock_port.get_option = lambda option_name: ''
49 self.driver = chromium.ChromiumDriver(mock_port, worker_number=0)
51 def test_test_shell_command(self):
52 expected_command = "test.html 2 checksum\n"
53 self.assertEqual(self.driver._test_shell_command("test.html", 2, "checksum"), expected_command)
55 def _assert_write_command_and_read_line(self, input=None, expected_line=None, expected_stdin=None, expected_crash=False):
56 if not expected_stdin:
58 expected_stdin = input
60 # We reset stdin, so we should expect stdin.getValue = ""
62 self.driver._proc.stdin = StringIO.StringIO()
63 line, did_crash = self.driver._write_command_and_read_line(input)
64 self.assertEqual(self.driver._proc.stdin.getvalue(), expected_stdin)
65 self.assertEqual(line, expected_line)
66 self.assertEqual(did_crash, expected_crash)
68 def test_write_command_and_read_line(self):
69 self.driver._proc = Mock()
70 # Set up to read 3 lines before we get an IOError
71 self.driver._proc.stdout = StringIO.StringIO("first\nsecond\nthird\n")
73 unicode_input = u"I \u2661 Unicode"
74 utf8_input = unicode_input.encode("utf-8")
75 # Test unicode input conversion to utf-8
76 self._assert_write_command_and_read_line(input=unicode_input, expected_stdin=utf8_input, expected_line="first\n")
78 self._assert_write_command_and_read_line(input="foo", expected_line="second\n")
80 self._assert_write_command_and_read_line(expected_line="third\n")
81 # Test reading from a closed/empty stream.
82 # reading from a StringIO does not raise IOError like a real file would, so raise IOError manually.
85 self.driver._proc.stdout.readline = mock_readline
86 self._assert_write_command_and_read_line(expected_crash=True)
89 class ChromiumPortTest(unittest.TestCase):
90 class TestMacPort(chromium_mac.ChromiumMacPort):
91 def __init__(self, options):
92 chromium_mac.ChromiumMacPort.__init__(self,
93 port_name='test-port',
96 def default_configuration(self):
97 self.default_configuration_called = True
100 class TestLinuxPort(chromium_linux.ChromiumLinuxPort):
101 def __init__(self, options):
102 chromium_linux.ChromiumLinuxPort.__init__(self,
103 port_name='test-port',
105 filesystem=filesystem_mock.MockFileSystem())
107 def default_configuration(self):
108 self.default_configuration_called = True
111 def test_path_to_image_diff(self):
112 mock_options = mocktool.MockOptions()
113 port = ChromiumPortTest.TestLinuxPort(options=mock_options)
114 self.assertTrue(port._path_to_image_diff().endswith(
115 '/out/default/ImageDiff'), msg=port._path_to_image_diff())
116 port = ChromiumPortTest.TestMacPort(options=mock_options)
117 self.assertTrue(port._path_to_image_diff().endswith(
118 '/xcodebuild/default/ImageDiff'))
119 # FIXME: Figure out how this is going to work on Windows.
120 #port = chromium_win.ChromiumWinPort('test-port', options=MockOptions())
122 def test_skipped_layout_tests(self):
123 mock_options = mocktool.MockOptions()
124 port = ChromiumPortTest.TestLinuxPort(options=mock_options)
126 fake_test = port._filesystem.join(port.layout_tests_dir(), "fast/js/not-good.js")
128 port.test_expectations = lambda: """BUG_TEST SKIP : fast/js/not-good.js = TEXT
129 LINUX WIN : fast/js/very-good.js = TIMEOUT PASS"""
130 port.test_expectations_overrides = lambda: ''
131 port.tests = lambda paths: set()
132 port.path_exists = lambda test: True
134 skipped_tests = port.skipped_layout_tests(extra_test_files=[fake_test, ])
135 self.assertTrue("fast/js/not-good.js" in skipped_tests)
137 def test_default_configuration(self):
138 mock_options = mocktool.MockOptions()
139 port = ChromiumPortTest.TestLinuxPort(options=mock_options)
140 self.assertEquals(mock_options.configuration, 'default')
141 self.assertTrue(port.default_configuration_called)
143 mock_options = mocktool.MockOptions(configuration=None)
144 port = ChromiumPortTest.TestLinuxPort(mock_options)
145 self.assertEquals(mock_options.configuration, 'default')
146 self.assertTrue(port.default_configuration_called)
148 def test_diff_image(self):
149 class TestPort(ChromiumPortTest.TestLinuxPort):
150 def _path_to_image_diff(self):
151 return "/path/to/image_diff"
153 mock_options = mocktool.MockOptions()
154 port = ChromiumPortTest.TestLinuxPort(mock_options)
156 # Images are different.
157 port._executive = executive_mock.MockExecutive2(exit_code=0)
158 self.assertEquals(False, port.diff_image("EXPECTED", "ACTUAL"))
160 # Images are the same.
161 port._executive = executive_mock.MockExecutive2(exit_code=1)
162 self.assertEquals(True, port.diff_image("EXPECTED", "ACTUAL"))
164 # There was some error running image_diff.
165 port._executive = executive_mock.MockExecutive2(exit_code=2)
166 exception_raised = False
168 port.diff_image("EXPECTED", "ACTUAL")
169 except ValueError, e:
170 exception_raised = True
171 self.assertFalse(exception_raised)
174 class ChromiumPortLoggingTest(logtesting.LoggingTestCase):
175 def test_check_sys_deps(self):
176 mock_options = mocktool.MockOptions()
177 port = ChromiumPortTest.TestLinuxPort(options=mock_options)
180 port._executive = executive_mock.MockExecutive2(exit_code=0)
181 self.assertTrue(port.check_sys_deps(needs_http=False))
184 port._executive = executive_mock.MockExecutive2(exit_code=1,
185 output='testing output failure')
186 self.assertFalse(port.check_sys_deps(needs_http=False))
188 'ERROR: System dependencies check failed.\n',
189 'ERROR: To override, invoke with --nocheck-sys-deps\n',
191 'ERROR: testing output failure\n'])
193 if __name__ == '__main__':