2010-09-07 Dirk Pranke <dpranke@chromium.org>
[WebKit.git] / WebKitTools / 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 chromium
30 import chromium_linux
31 import chromium_mac
32 import chromium_win
33 import unittest
34 import StringIO
35 import os
36
37 from webkitpy.thirdparty.mock import Mock
38
39
40 class ChromiumDriverTest(unittest.TestCase):
41
42     def setUp(self):
43         mock_port = Mock()
44         # FIXME: The Driver should not be grabbing at port._options!
45         mock_port._options = Mock()
46         mock_port._options.wrapper = ""
47         self.driver = chromium.ChromiumDriver(mock_port, image_path=None, options=None)
48
49     def test_test_shell_command(self):
50         expected_command = "test.html 2 checksum\n"
51         self.assertEqual(self.driver._test_shell_command("test.html", 2, "checksum"), expected_command)
52
53     def _assert_write_command_and_read_line(self, input=None, expected_line=None, expected_stdin=None, expected_crash=False):
54         if not expected_stdin:
55             if input:
56                 expected_stdin = input
57             else:
58                 # We reset stdin, so we should expect stdin.getValue = ""
59                 expected_stdin = ""
60         self.driver._proc.stdin = StringIO.StringIO()
61         line, did_crash = self.driver._write_command_and_read_line(input)
62         self.assertEqual(self.driver._proc.stdin.getvalue(), expected_stdin)
63         self.assertEqual(line, expected_line)
64         self.assertEqual(did_crash, expected_crash)
65
66     def test_write_command_and_read_line(self):
67         self.driver._proc = Mock()
68         # Set up to read 3 lines before we get an IOError
69         self.driver._proc.stdout = StringIO.StringIO("first\nsecond\nthird\n")
70
71         unicode_input = u"I \u2661 Unicode"
72         utf8_input = unicode_input.encode("utf-8")
73         # Test unicode input conversion to utf-8
74         self._assert_write_command_and_read_line(input=unicode_input, expected_stdin=utf8_input, expected_line="first\n")
75         # Test str() input.
76         self._assert_write_command_and_read_line(input="foo", expected_line="second\n")
77         # Test input=None
78         self._assert_write_command_and_read_line(expected_line="third\n")
79         # Test reading from a closed/empty stream.
80         # reading from a StringIO does not raise IOError like a real file would, so raise IOError manually.
81         def mock_readline():
82             raise IOError
83         self.driver._proc.stdout.readline = mock_readline
84         self._assert_write_command_and_read_line(expected_crash=True)
85
86     def test_path_to_image_diff(self):
87         class MockOptions:
88             def __init__(self):
89                 self.use_drt = True
90
91         port = chromium_linux.ChromiumLinuxPort('test-port', options=MockOptions())
92         self.assertTrue(port._path_to_image_diff().endswith(
93             '/out/Release/ImageDiff'))
94         port = chromium_mac.ChromiumMacPort('test-port', options=MockOptions())
95         self.assertTrue(port._path_to_image_diff().endswith(
96             '/xcodebuild/Release/ImageDiff'))
97         # FIXME: Figure out how this is going to work on Windows.
98         #port = chromium_win.ChromiumWinPort('test-port', options=MockOptions())
99
100     def test_skipped_layout_tests(self):
101         class MockOptions:
102             def __init__(self):
103                 self.use_drt = True
104
105         port = chromium_linux.ChromiumLinuxPort('test-port', options=MockOptions())
106
107         fake_test = os.path.join(port.layout_tests_dir(), "fast/js/not-good.js")
108
109         port.test_expectations = lambda: """BUG_TEST SKIP : fast/js/not-good.js = TEXT
110 DEFER LINUX WIN : fast/js/very-good.js = TIMEOUT PASS"""
111         port.test_expectations_overrides = lambda: ''
112
113         skipped_tests = port.skipped_layout_tests(extra_test_files=[fake_test, ])
114         self.assertTrue("fast/js/not-good.js" in skipped_tests)
115
116 if __name__ == '__main__':
117     unittest.main()