OSDN Git Service

Merge WebKit at r78450: Initial merge by git.
[android-x86/external-webkit.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 unittest
30 import StringIO
31
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
37
38
39 import chromium
40 import chromium_linux
41 import chromium_mac
42 import chromium_win
43
44 class ChromiumDriverTest(unittest.TestCase):
45
46     def setUp(self):
47         mock_port = Mock()
48         mock_port.get_option = lambda option_name: ''
49         self.driver = chromium.ChromiumDriver(mock_port, worker_number=0)
50
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)
54
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:
57             if input:
58                 expected_stdin = input
59             else:
60                 # We reset stdin, so we should expect stdin.getValue = ""
61                 expected_stdin = ""
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)
67
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")
72
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")
77         # Test str() input.
78         self._assert_write_command_and_read_line(input="foo", expected_line="second\n")
79         # Test input=None
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.
83         def mock_readline():
84             raise IOError
85         self.driver._proc.stdout.readline = mock_readline
86         self._assert_write_command_and_read_line(expected_crash=True)
87
88
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',
94                                                   options=options)
95
96         def default_configuration(self):
97             self.default_configuration_called = True
98             return 'default'
99
100     class TestLinuxPort(chromium_linux.ChromiumLinuxPort):
101         def __init__(self, options):
102             chromium_linux.ChromiumLinuxPort.__init__(self,
103                                                       port_name='test-port',
104                                                       options=options,
105                                                       filesystem=filesystem_mock.MockFileSystem())
106
107         def default_configuration(self):
108             self.default_configuration_called = True
109             return 'default'
110
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())
121
122     def test_skipped_layout_tests(self):
123         mock_options = mocktool.MockOptions()
124         port = ChromiumPortTest.TestLinuxPort(options=mock_options)
125
126         fake_test = port._filesystem.join(port.layout_tests_dir(), "fast/js/not-good.js")
127
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
133
134         skipped_tests = port.skipped_layout_tests(extra_test_files=[fake_test, ])
135         self.assertTrue("fast/js/not-good.js" in skipped_tests)
136
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)
142
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)
147
148     def test_diff_image(self):
149         class TestPort(ChromiumPortTest.TestLinuxPort):
150             def _path_to_image_diff(self):
151                 return "/path/to/image_diff"
152
153         mock_options = mocktool.MockOptions()
154         port = ChromiumPortTest.TestLinuxPort(mock_options)
155
156         # Images are different.
157         port._executive = executive_mock.MockExecutive2(exit_code=0)
158         self.assertEquals(False, port.diff_image("EXPECTED", "ACTUAL"))
159
160         # Images are the same.
161         port._executive = executive_mock.MockExecutive2(exit_code=1)
162         self.assertEquals(True, port.diff_image("EXPECTED", "ACTUAL"))
163
164         # There was some error running image_diff.
165         port._executive = executive_mock.MockExecutive2(exit_code=2)
166         exception_raised = False
167         try:
168             port.diff_image("EXPECTED", "ACTUAL")
169         except ValueError, e:
170             exception_raised = True
171         self.assertFalse(exception_raised)
172
173
174 class ChromiumPortLoggingTest(logtesting.LoggingTestCase):
175     def test_check_sys_deps(self):
176         mock_options = mocktool.MockOptions()
177         port = ChromiumPortTest.TestLinuxPort(options=mock_options)
178
179         # Success
180         port._executive = executive_mock.MockExecutive2(exit_code=0)
181         self.assertTrue(port.check_sys_deps(needs_http=False))
182
183         # Failure
184         port._executive = executive_mock.MockExecutive2(exit_code=1,
185             output='testing output failure')
186         self.assertFalse(port.check_sys_deps(needs_http=False))
187         self.assertLog([
188             'ERROR: System dependencies check failed.\n',
189             'ERROR: To override, invoke with --nocheck-sys-deps\n',
190             'ERROR: \n',
191             'ERROR: testing output failure\n'])
192
193 if __name__ == '__main__':
194     unittest.main()