2010-09-22 Dirk Pranke <dpranke@chromium.org>
[WebKit.git] / WebKitTools / Scripts / webkitpy / layout_tests / port / factory_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 sys
30 import unittest
31
32 import chromium_gpu
33 import chromium_linux
34 import chromium_mac
35 import chromium_win
36 import dryrun
37 import factory
38 import google_chrome
39 import gtk
40 import mac
41 import qt
42 import test
43 import win
44
45
46 class FactoryTest(unittest.TestCase):
47     """Test factory creates proper port object for the target.
48
49     Target is specified by port_name, sys.platform and options.
50
51     """
52     # FIXME: The ports themselves should expose what options they require,
53     # instead of passing generic "options".
54
55     class WebKitOptions(object):
56         """Represents the minimum options for WebKit port."""
57         def __init__(self):
58             self.pixel_tests = False
59
60     class ChromiumOptions(WebKitOptions):
61         """Represents minimum options for Chromium port."""
62         def __init__(self):
63             FactoryTest.WebKitOptions.__init__(self)
64             self.chromium = True
65
66     def setUp(self):
67         self.real_sys_platform = sys.platform
68         self.webkit_options = FactoryTest.WebKitOptions()
69         self.chromium_options = FactoryTest.ChromiumOptions()
70
71     def tearDown(self):
72         sys.platform = self.real_sys_platform
73
74     def assert_port(self, port_name, expected_port, port_obj=None):
75         """Helper assert for port_name.
76
77         Args:
78           port_name: port name to get port object.
79           expected_port: class of expected port object.
80           port_obj: optional port object
81         """
82         port_obj = port_obj or factory.get(port_name=port_name)
83         self.assertTrue(isinstance(port_obj, expected_port))
84
85     def assert_platform_port(self, platform, options, expected_port):
86         """Helper assert for platform and options.
87
88         Args:
89           platform: sys.platform.
90           options: options to get port object.
91           expected_port: class of expected port object.
92
93         """
94         orig_platform = sys.platform
95         sys.platform = platform
96         self.assertTrue(isinstance(factory.get(options=options),
97                                    expected_port))
98         sys.platform = orig_platform
99
100     def test_test(self):
101         self.assert_port("test", test.TestPort)
102
103     def test_dryrun(self):
104         self.assert_port("dryrun-test", dryrun.DryRunPort)
105         self.assert_port("dryrun-mac", dryrun.DryRunPort)
106
107     def test_mac(self):
108         self.assert_port("mac", mac.MacPort)
109         self.assert_platform_port("darwin", None, mac.MacPort)
110         self.assert_platform_port("darwin", self.webkit_options, mac.MacPort)
111
112     def test_win(self):
113         self.assert_port("win", win.WinPort)
114         self.assert_platform_port("win32", None, win.WinPort)
115         self.assert_platform_port("win32", self.webkit_options, win.WinPort)
116         self.assert_platform_port("cygwin", None, win.WinPort)
117         self.assert_platform_port("cygwin", self.webkit_options, win.WinPort)
118
119     def test_google_chrome(self):
120         # The actual Chrome class names aren't available so we test that the
121         # objects we get are at least subclasses of the Chromium versions.
122         self.assert_port("google-chrome-linux32",
123                          chromium_linux.ChromiumLinuxPort)
124         self.assert_port("google-chrome-linux64",
125                          chromium_linux.ChromiumLinuxPort)
126         self.assert_port("google-chrome-win",
127                          chromium_win.ChromiumWinPort)
128         self.assert_port("google-chrome-mac",
129                          chromium_mac.ChromiumMacPort)
130
131     def test_gtk(self):
132         self.assert_port("gtk", gtk.GtkPort)
133
134     def test_qt(self):
135         self.assert_port("qt", qt.QtPort)
136
137     def test_chromium_gpu_linux(self):
138         self.assert_port("chromium-gpu-linux", chromium_gpu.ChromiumGpuLinuxPort)
139
140     def test_chromium_gpu_mac(self):
141         self.assert_port("chromium-gpu-mac", chromium_gpu.ChromiumGpuMacPort)
142
143     def test_chromium_gpu_win(self):
144         self.assert_port("chromium-gpu-win", chromium_gpu.ChromiumGpuWinPort)
145
146     def test_chromium_mac(self):
147         self.assert_port("chromium-mac", chromium_mac.ChromiumMacPort)
148         self.assert_platform_port("darwin", self.chromium_options,
149                                   chromium_mac.ChromiumMacPort)
150
151     def test_chromium_linux(self):
152         self.assert_port("chromium-linux", chromium_linux.ChromiumLinuxPort)
153         self.assert_platform_port("linux2", self.chromium_options,
154                                   chromium_linux.ChromiumLinuxPort)
155
156     def test_chromium_win(self):
157         self.assert_port("chromium-win", chromium_win.ChromiumWinPort)
158         self.assert_platform_port("win32", self.chromium_options,
159                                   chromium_win.ChromiumWinPort)
160         self.assert_platform_port("cygwin", self.chromium_options,
161                                   chromium_win.ChromiumWinPort)
162
163     def test_get_all_ports(self):
164         ports = factory.get_all()
165         for name in factory.ALL_PORT_NAMES:
166             self.assertTrue(name in ports.keys())
167         self.assert_port("test", test.TestPort, ports["test"])
168         self.assert_port("dryrun-test", dryrun.DryRunPort, ports["dryrun"])
169         self.assert_port("dryrun-mac", dryrun.DryRunPort, ports["dryrun"])
170         self.assert_port("mac", mac.MacPort, ports["mac"])
171         self.assert_port("win", win.WinPort, ports["win"])
172         self.assert_port("gtk", gtk.GtkPort, ports["gtk"])
173         self.assert_port("qt", qt.QtPort, ports["qt"])
174         self.assert_port("chromium-mac", chromium_mac.ChromiumMacPort,
175                          ports["chromium-mac"])
176         self.assert_port("chromium-linux", chromium_linux.ChromiumLinuxPort,
177                          ports["chromium-linux"])
178         self.assert_port("chromium-win", chromium_win.ChromiumWinPort,
179                          ports["chromium-win"])
180
181     def test_unknown_specified(self):
182         # Test what happens when you specify an unknown port.
183         orig_platform = sys.platform
184         self.assertRaises(NotImplementedError, factory.get,
185                           port_name='unknown')
186
187     def test_unknown_default(self):
188         # Test what happens when you're running on an unknown platform.
189         orig_platform = sys.platform
190         sys.platform = 'unknown'
191         self.assertRaises(NotImplementedError, factory.get)
192         sys.platform = orig_platform
193
194
195 if __name__ == '__main__':
196     unittest.main()