webkitpy: Refactor port code for devices
[WebKit-https.git] / Tools / Scripts / webkitpy / port / ios_device_unittest.py
1 # Copyright (C) 2017 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
13 # ANY 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
16 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
20 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
21 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22
23 import time
24
25 from webkitpy.common.system.outputcapture import OutputCapture
26 from webkitpy.common.system.executive_mock import MockExecutive2, ScriptError
27 from webkitpy.common.version import Version
28 from webkitpy.port.ios_device import IOSDevicePort
29 from webkitpy.port import ios_testcase
30 from webkitpy.port import port_testcase
31
32
33 class IOSDeviceTest(ios_testcase.IOSTest):
34     os_name = 'ios'
35     os_version = None
36     port_name = 'ios-device'
37     port_maker = IOSDevicePort
38
39     def test_operating_system(self):
40         self.assertEqual('ios-device', self.make_port().operating_system())
41
42     def test_crashlog_path(self):
43         port = self.make_port()
44         with self.assertRaises(RuntimeError):
45             port.path_to_crash_logs()
46
47     def test_spindump(self):
48         def logging_run_command(args):
49             print(args)
50
51         port = self.make_port()
52         port.host.filesystem.files['/__im_tmp/tmp_0_/test-42-spindump.txt'] = 'Spindump file'
53         port.host.executive = MockExecutive2(run_command_fn=logging_run_command)
54         expected_stdout = "['/usr/sbin/spindump', 42, 10, 10, '-file', '/__im_tmp/tmp_0_/test-42-spindump.txt']\n"
55         OutputCapture().assert_outputs(self, port.sample_process, args=['test', 42], expected_stdout=expected_stdout)
56         self.assertEqual(port.host.filesystem.files['/mock-build/layout-test-results/test-42-spindump.txt'], 'Spindump file')
57         self.assertIsNone(port.host.filesystem.files['/__im_tmp/tmp_0_/test-42-spindump.txt'])
58
59     def test_sample_process(self):
60         def logging_run_command(args):
61             if args[0] == '/usr/sbin/spindump':
62                 return 1
63             print(args)
64             return 0
65
66         port = self.make_port()
67         port.host.filesystem.files['/__im_tmp/tmp_0_/test-42-sample.txt'] = 'Sample file'
68         port.host.executive = MockExecutive2(run_command_fn=logging_run_command)
69         expected_stdout = "['/usr/bin/sample', 42, 10, 10, '-file', '/__im_tmp/tmp_0_/test-42-sample.txt']\n"
70         OutputCapture().assert_outputs(self, port.sample_process, args=['test', 42], expected_stdout=expected_stdout)
71         self.assertEqual(port.host.filesystem.files['/mock-build/layout-test-results/test-42-sample.txt'], 'Sample file')
72         self.assertIsNone(port.host.filesystem.files['/__im_tmp/tmp_0_/test-42-sample.txt'])
73
74     def test_sample_process_exception(self):
75         def throwing_run_command(args):
76             if args[0] == '/usr/sbin/spindump':
77                 return 1
78             raise ScriptError('MOCK script error')
79
80         port = self.make_port()
81         port.host.executive = MockExecutive2(run_command_fn=throwing_run_command)
82         OutputCapture().assert_outputs(self, port.sample_process, args=['test', 42])
83
84     def test_get_crash_log(self):
85         port = self.make_port(port_name=self.port_name)
86         self.assertEqual((None, None), port._get_crash_log('DumpRenderTree', 1234, None, None, time.time(), wait_for_log=False))
87
88     def test_layout_test_searchpath_with_apple_additions(self):
89         with port_testcase.bind_mock_apple_additions():
90             search_path = self.make_port().default_baseline_search_path()
91         self.assertEqual(search_path[0], '/additional_testing_path/ios-device-add-ios11-wk1')
92         self.assertEqual(search_path[1], '/mock-checkout/LayoutTests/platform/ios-device-11-wk1')
93         self.assertEqual(search_path[2], '/additional_testing_path/ios-device-add-ios11')
94         self.assertEqual(search_path[3], '/mock-checkout/LayoutTests/platform/ios-device-11')
95         self.assertEqual(search_path[4], '/additional_testing_path/ios-device-wk1')
96         self.assertEqual(search_path[5], '/mock-checkout/LayoutTests/platform/ios-device-wk1')
97         self.assertEqual(search_path[6], '/additional_testing_path/ios-device')
98         self.assertEqual(search_path[7], '/mock-checkout/LayoutTests/platform/ios-device')
99         self.assertEqual(search_path[8], '/additional_testing_path/ios-add-ios11')
100         self.assertEqual(search_path[9], '/mock-checkout/LayoutTests/platform/ios-11')
101         self.assertEqual(search_path[10], '/additional_testing_path/ios-wk1')
102         self.assertEqual(search_path[11], '/mock-checkout/LayoutTests/platform/ios-wk1')
103
104     def test_layout_test_searchpath_without_apple_additions(self):
105         search_path = self.make_port(port_name='ios-device-wk2', os_version=Version(12)).default_baseline_search_path()
106
107         self.assertEqual(search_path[0], '/mock-checkout/LayoutTests/platform/ios-device-12-wk2')
108         self.assertEqual(search_path[1], '/mock-checkout/LayoutTests/platform/ios-device-12')
109         self.assertEqual(search_path[2], '/mock-checkout/LayoutTests/platform/ios-device-wk2')
110         self.assertEqual(search_path[3], '/mock-checkout/LayoutTests/platform/ios-device')
111         self.assertEqual(search_path[4], '/mock-checkout/LayoutTests/platform/ios-12')
112         self.assertEqual(search_path[5], '/mock-checkout/LayoutTests/platform/ios-wk2')
113         self.assertEqual(search_path[6], '/mock-checkout/LayoutTests/platform/ios')
114         self.assertEqual(search_path[7], '/mock-checkout/LayoutTests/platform/wk2')