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