Layout tests broke due to unexpected simctl output
[WebKit-https.git] / Tools / Scripts / webkitpy / xcode / simulator_unittest.py
1 # Copyright (C) 2015 Apple 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
5 # are met:
6 # 1.  Redistributions of source code must retain the above copyright
7 #     notice, this list of conditions and the following disclaimer.
8 # 2.  Redistributions in binary form must reproduce the above copyright
9 #     notice, this list of conditions and the following disclaimer in the
10 #     documentation and/or other materials provided with the distribution.
11 #
12 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
13 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 # DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
16 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
19 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22
23 import unittest
24
25 from webkitpy.common.host_mock import MockHost
26 from webkitpy.xcode.simulator import Simulator
27
28
29 class SimulatorTest(unittest.TestCase):
30
31     def setUp(self):
32         """ Set up method for SimulatorTest """
33         self._host = MockHost()
34
35     def _set_expected_xcrun_simctl_list(self, lines):
36         self._host.platform.expected_xcode_simctl_list = (line for line in lines.splitlines())
37
38     def test_simulator_device_types(self):
39         """ Tests that valid `xcrun simctl list` output is parsed as expected """
40         self._set_expected_xcrun_simctl_list('''== Device Types ==
41 iPhone 4s (com.apple.CoreSimulator.SimDeviceType.iPhone-4s)
42 iPhone 5 (com.apple.CoreSimulator.SimDeviceType.iPhone-5)
43 iPhone 5s (com.apple.CoreSimulator.SimDeviceType.iPhone-5s)
44 iPhone 6 Plus (com.apple.CoreSimulator.SimDeviceType.iPhone-6-Plus)
45 iPhone 6 (com.apple.CoreSimulator.SimDeviceType.iPhone-6)
46 iPad 2 (com.apple.CoreSimulator.SimDeviceType.iPad-2)
47 iPad Retina (com.apple.CoreSimulator.SimDeviceType.iPad-Retina)
48 iPad Air (com.apple.CoreSimulator.SimDeviceType.iPad-Air)
49 == Runtimes ==
50 iOS 8.0 (8.0 - 12A465) (com.apple.CoreSimulator.SimRuntime.iOS-8-0)
51 iOS 8.0 Internal (8.0 - Unknown) (com.apple.CoreSimulator.SimRuntime.iOS-8-0-Internal) (unavailable, runtime path not found)
52 == Devices ==
53 -- iOS 8.0 --
54     iPhone 4s (68D9A792-E3A9-462B-B211-762C6A5D3779) (Shutdown)
55     iPhone 5 (1C3767FF-C268-4961-B6DA-F4F75E99EF5D) (Shutdown)
56     iPhone 5s (2A1CB363-9A09-4438-B9BE-9C42BD208F72) (Shutdown)
57     iPhone 5s WebKit Tester (79BA9206-E0B6-4D0E-B0E8-A88E2D45515D) (Booted)
58     iPhone 6 Plus (7F8039BE-D4A0-4245-9D56-AF94413FD6F5) (Shutdown)
59     iPhone 6 (7BF9F835-0CEA-4EE3-BD15-A62BD9F4B691) (Shutdown)
60     iPad 2 (2967C54F-A499-4043-A82C-8C1F5ADBB4A9) (Shutdown)
61     iPad Retina (733FC71E-22F4-4077-BF79-25C27EA881FC) (Shutdown)
62     iPad Air (67266841-82F3-4545-AED6-568B117E41A8) (Shutdown)
63 -- iOS 8.0 Internal --
64 ''')
65         simulator = Simulator(host=self._host)
66         self.assertEqual(8, len(simulator.device_types))
67
68         device_type_iphone_4s = simulator.device_types[0]
69         self.assertEqual('iPhone 4s', device_type_iphone_4s.name)
70         self.assertEqual('com.apple.CoreSimulator.SimDeviceType.iPhone-4s', device_type_iphone_4s.identifier)
71
72         device_type_iphone_5 = simulator.device_types[1]
73         self.assertEqual('iPhone 5', device_type_iphone_5.name)
74         self.assertEqual('com.apple.CoreSimulator.SimDeviceType.iPhone-5', device_type_iphone_5.identifier)
75
76         device_type_iphone_5s = simulator.device_types[2]
77         self.assertEqual('iPhone 5s', device_type_iphone_5s.name)
78         self.assertEqual('com.apple.CoreSimulator.SimDeviceType.iPhone-5s', device_type_iphone_5s.identifier)
79
80         device_type_iphone_6_plus = simulator.device_types[3]
81         self.assertEqual('iPhone 6 Plus', device_type_iphone_6_plus.name)
82         self.assertEqual('com.apple.CoreSimulator.SimDeviceType.iPhone-6-Plus', device_type_iphone_6_plus.identifier)
83
84         device_type_iphone_6 = simulator.device_types[4]
85         self.assertEqual('iPhone 6', device_type_iphone_6.name)
86         self.assertEqual('com.apple.CoreSimulator.SimDeviceType.iPhone-6', device_type_iphone_6.identifier)
87
88         device_type_ipad_2 = simulator.device_types[5]
89         self.assertEqual('iPad 2', device_type_ipad_2.name)
90         self.assertEqual('com.apple.CoreSimulator.SimDeviceType.iPad-2', device_type_ipad_2.identifier)
91
92         device_type_ipad_retina = simulator.device_types[6]
93         self.assertEqual('iPad Retina', device_type_ipad_retina.name)
94         self.assertEqual('com.apple.CoreSimulator.SimDeviceType.iPad-Retina', device_type_ipad_retina.identifier)
95
96         device_type_ipad_air = simulator.device_types[7]
97         self.assertEqual('iPad Air', device_type_ipad_air.name)
98         self.assertEqual('com.apple.CoreSimulator.SimDeviceType.iPad-Air', device_type_ipad_air.identifier)
99
100         self.assertEqual(2, len(simulator.runtimes))
101
102         runtime_ios_8 = simulator.runtimes[0]
103         self.assertEqual('com.apple.CoreSimulator.SimRuntime.iOS-8-0', runtime_ios_8.identifier)
104         self.assertEqual(True, runtime_ios_8.available)
105         self.assertEqual(False, runtime_ios_8.is_internal_runtime)
106         self.assertEqual(tuple([8, 0]), runtime_ios_8.version)
107         self.assertEqual(9, len(runtime_ios_8.devices))
108
109         device_iphone_4s = runtime_ios_8.devices[0]
110         self.assertEqual('iPhone 4s', device_iphone_4s.name)
111         self.assertEqual('68D9A792-E3A9-462B-B211-762C6A5D3779', device_iphone_4s.udid)
112         self.assertEqual(True, device_iphone_4s.available)
113         self.assertEqual(runtime_ios_8, device_iphone_4s.runtime)
114
115         device_iphone_5 = runtime_ios_8.devices[1]
116         self.assertEqual('iPhone 5', device_iphone_5.name)
117         self.assertEqual('1C3767FF-C268-4961-B6DA-F4F75E99EF5D', device_iphone_5.udid)
118         self.assertEqual(True, device_iphone_5.available)
119         self.assertEqual(runtime_ios_8, device_iphone_5.runtime)
120
121         device_iphone_5s = runtime_ios_8.devices[2]
122         self.assertEqual('iPhone 5s', device_iphone_5s.name)
123         self.assertEqual('2A1CB363-9A09-4438-B9BE-9C42BD208F72', device_iphone_5s.udid)
124         self.assertEqual(True, device_iphone_5s.available)
125         self.assertEqual(runtime_ios_8, device_iphone_5s.runtime)
126
127         device_iphone_5s_webkit_tester = runtime_ios_8.devices[3]
128         self.assertEqual('iPhone 5s WebKit Tester', device_iphone_5s_webkit_tester.name)
129         self.assertEqual('79BA9206-E0B6-4D0E-B0E8-A88E2D45515D', device_iphone_5s_webkit_tester.udid)
130         self.assertEqual(True, device_iphone_5s_webkit_tester.available)
131         self.assertEqual(runtime_ios_8, device_iphone_5s_webkit_tester.runtime)
132
133         device_iphone_6_plus = runtime_ios_8.devices[4]
134         self.assertEqual('iPhone 6 Plus', device_iphone_6_plus.name)
135         self.assertEqual('7F8039BE-D4A0-4245-9D56-AF94413FD6F5', device_iphone_6_plus.udid)
136         self.assertEqual(True, device_iphone_6_plus.available)
137         self.assertEqual(runtime_ios_8, device_iphone_6_plus.runtime)
138
139         device_iphone_6 = runtime_ios_8.devices[5]
140         self.assertEqual('iPhone 6', device_iphone_6.name)
141         self.assertEqual('7BF9F835-0CEA-4EE3-BD15-A62BD9F4B691', device_iphone_6.udid)
142         self.assertEqual(True, device_iphone_6.available)
143         self.assertEqual(runtime_ios_8, device_iphone_6.runtime)
144
145         device_ipad_2 = runtime_ios_8.devices[6]
146         self.assertEqual('iPad 2', device_ipad_2.name)
147         self.assertEqual('2967C54F-A499-4043-A82C-8C1F5ADBB4A9', device_ipad_2.udid)
148         self.assertEqual(True, device_ipad_2.available)
149         self.assertEqual(runtime_ios_8, device_ipad_2.runtime)
150
151         device_ipad_retina = runtime_ios_8.devices[7]
152         self.assertEqual('iPad Retina', device_ipad_retina.name)
153         self.assertEqual('733FC71E-22F4-4077-BF79-25C27EA881FC', device_ipad_retina.udid)
154         self.assertEqual(True, device_ipad_retina.available)
155         self.assertEqual(runtime_ios_8, device_ipad_retina.runtime)
156
157         device_ipad_air = runtime_ios_8.devices[8]
158         self.assertEqual('iPad Air', device_ipad_air.name)
159         self.assertEqual('67266841-82F3-4545-AED6-568B117E41A8', device_ipad_air.udid)
160         self.assertEqual(True, device_ipad_air.available)
161         self.assertEqual(runtime_ios_8, device_ipad_air.runtime)
162
163         runtime_ios_8_internal = simulator.runtimes[1]
164         self.assertEqual('com.apple.CoreSimulator.SimRuntime.iOS-8-0-Internal', runtime_ios_8_internal.identifier)
165         self.assertEqual(False, runtime_ios_8_internal.available)
166         self.assertEqual(True, runtime_ios_8_internal.is_internal_runtime)
167         self.assertEqual(tuple([8, 0]), runtime_ios_8_internal.version)
168         self.assertEqual(0, len(runtime_ios_8_internal.devices))
169
170     def test_invalid_device_types_header(self):
171         """ Tests that an invalid Device Types header throws an exception """
172         self._set_expected_xcrun_simctl_list('''XX Device Types XX
173 ''')
174         with self.assertRaises(RuntimeError) as cm:
175             Simulator(host=self._host)
176         self.assertEqual('Expected == Device Types == header but got: "XX Device Types XX"', cm.exception.message)
177
178     def test_invalid_runtimes_header(self):
179         """ Tests that an invalid Runtimes header throws an exception """
180         self._set_expected_xcrun_simctl_list('''== Device Types ==
181 iPhone 4s (com.apple.CoreSimulator.SimDeviceType.iPhone-4s)
182 XX Runtimes XX
183 ''')
184         with self.assertRaises(RuntimeError) as cm:
185             Simulator(host=self._host)
186         self.assertEqual('Expected == Runtimes == header but got: "XX Runtimes XX"', cm.exception.message)
187
188     def test_invalid_devices_header(self):
189         """ Tests that an invalid Devices header throws an exception """
190         self._set_expected_xcrun_simctl_list('''== Device Types ==
191 iPhone 4s (com.apple.CoreSimulator.SimDeviceType.iPhone-4s)
192 == Runtimes ==
193 iOS 8.0 (8.0 - 12A465) (com.apple.CoreSimulator.SimRuntime.iOS-8-0)
194 XX Devices XX
195 ''')
196         with self.assertRaises(RuntimeError) as cm:
197             Simulator(host=self._host)
198         self.assertEqual('Expected == Devices == header but got: "XX Devices XX"', cm.exception.message)
199
200     def test_unavailable_devices(self):
201         """ Tests that unavailable devices are ignored """
202         self._set_expected_xcrun_simctl_list('''== Device Types ==
203 iPhone 4s (com.apple.CoreSimulator.SimDeviceType.iPhone-4s)
204 == Runtimes ==
205 iOS 8.0 (8.0 - 12A465) (com.apple.CoreSimulator.SimRuntime.iOS-8-0)
206 iOS 8.0 Internal (8.0 - Unknown) (com.apple.CoreSimulator.SimRuntime.iOS-8-0-Internal) (unavailable, runtime path not found)
207 == Devices ==
208 -- iOS 8.0 --
209     iPhone 4s (271BBEAC-1826-4CE1-B3AF-83F35CDD1D82) (Shutdown)
210 -- iOS 8.0 Internal --
211 -- Unavailable: com.apple.CoreSimulator.SimRuntime.iOS-8-1 --
212     iPhone 4s (08C0542B-65F7-46E8-B203-CB4055207BC8) (Shutdown) (unavailable, runtime profile not found)
213 -- Unavailable: com.apple.CoreSimulator.SimRuntime.iOS-8-2 --
214     iPhone 4s (A36F7432-0AF5-49C4-A261-C44383992597) (Shutdown) (unavailable, runtime profile not found)
215 ''')
216         simulator = Simulator(host=self._host)
217         self.assertEqual(1, len(simulator.device_types))
218
219         device_type_iphone_4s = simulator.device_types[0]
220         self.assertEqual('iPhone 4s', device_type_iphone_4s.name)
221         self.assertEqual('com.apple.CoreSimulator.SimDeviceType.iPhone-4s', device_type_iphone_4s.identifier)
222
223         self.assertEqual(2, len(simulator.runtimes))
224
225         runtime_ios_8 = simulator.runtimes[0]
226         self.assertEqual('com.apple.CoreSimulator.SimRuntime.iOS-8-0', runtime_ios_8.identifier)
227         self.assertEqual(True, runtime_ios_8.available)
228         self.assertEqual(False, runtime_ios_8.is_internal_runtime)
229         self.assertEqual(tuple([8, 0]), runtime_ios_8.version)
230         self.assertEqual(1, len(runtime_ios_8.devices))
231
232         device_iphone_4s = runtime_ios_8.devices[0]
233         self.assertEqual('iPhone 4s', device_iphone_4s.name)
234         self.assertEqual('271BBEAC-1826-4CE1-B3AF-83F35CDD1D82', device_iphone_4s.udid)
235         self.assertEqual(True, device_iphone_4s.available)
236         self.assertEqual(runtime_ios_8, device_iphone_4s.runtime)
237
238         runtime_ios_8_internal = simulator.runtimes[1]
239         self.assertEqual('com.apple.CoreSimulator.SimRuntime.iOS-8-0-Internal', runtime_ios_8_internal.identifier)
240         self.assertEqual(False, runtime_ios_8_internal.available)
241         self.assertEqual(True, runtime_ios_8_internal.is_internal_runtime)
242         self.assertEqual(tuple([8, 0]), runtime_ios_8_internal.version)
243         self.assertEqual(0, len(runtime_ios_8_internal.devices))
244
245     def test_device_pairs(self):
246         """ Tests that Device Pairs header does not cause parsing exception """
247         self._set_expected_xcrun_simctl_list('''== Device Types ==
248 iPhone 4s (com.apple.CoreSimulator.SimDeviceType.iPhone-4s)
249 == Runtimes ==
250 iOS 8.0 (8.0 - 12A465) (com.apple.CoreSimulator.SimRuntime.iOS-8-0)
251 iOS 8.0 Internal (8.0 - Unknown) (com.apple.CoreSimulator.SimRuntime.iOS-8-0-Internal) (unavailable, runtime path not found)
252 == Devices ==
253 -- iOS 8.0 --
254     iPhone 4s (271BBEAC-1826-4CE1-B3AF-83F35CDD1D82) (Shutdown)
255 == Device Pairs ==
256 ''')
257         simulator = Simulator(host=self._host)
258         self.assertEqual(1, len(simulator.device_types))
259
260         device_type_iphone_4s = simulator.device_types[0]
261         self.assertEqual('iPhone 4s', device_type_iphone_4s.name)
262         self.assertEqual('com.apple.CoreSimulator.SimDeviceType.iPhone-4s', device_type_iphone_4s.identifier)
263
264         self.assertEqual(2, len(simulator.runtimes))
265
266         runtime_ios_8 = simulator.runtimes[0]
267         self.assertEqual('com.apple.CoreSimulator.SimRuntime.iOS-8-0', runtime_ios_8.identifier)
268         self.assertEqual(True, runtime_ios_8.available)
269         self.assertEqual(False, runtime_ios_8.is_internal_runtime)
270         self.assertEqual(tuple([8, 0]), runtime_ios_8.version)
271         self.assertEqual(1, len(runtime_ios_8.devices))
272
273         device_iphone_4s = runtime_ios_8.devices[0]
274         self.assertEqual('iPhone 4s', device_iphone_4s.name)
275         self.assertEqual('271BBEAC-1826-4CE1-B3AF-83F35CDD1D82', device_iphone_4s.udid)
276         self.assertEqual(True, device_iphone_4s.available)
277         self.assertEqual(runtime_ios_8, device_iphone_4s.runtime)