OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import unittest | 5 import unittest |
6 | 6 |
| 7 from telemetry import decorators |
7 from telemetry.internal.browser import browser_options | 8 from telemetry.internal.browser import browser_options |
8 from telemetry.internal.platform import android_device | 9 from telemetry.internal.platform import android_device |
9 from telemetry.testing import system_stub | 10 from telemetry.testing import system_stub |
10 import mock | 11 import mock |
11 | 12 |
12 from devil.android import device_utils | 13 from devil.android import device_utils |
13 | 14 |
14 | 15 |
15 class _BaseAndroidDeviceTest(unittest.TestCase): | 16 class _BaseAndroidDeviceTest(unittest.TestCase): |
16 def setUp(self): | 17 def setUp(self): |
17 self._healthy_device_patcher = mock.patch( | 18 self._healthy_device_patcher = mock.patch( |
18 'devil.android.device_utils.DeviceUtils.HealthyDevices') | 19 'devil.android.device_utils.DeviceUtils.HealthyDevices') |
19 self._healthy_device_mock = self._healthy_device_patcher.start() | 20 self._healthy_device_mock = self._healthy_device_patcher.start() |
20 self._android_device_stub = system_stub.Override( | 21 self._android_device_stub = system_stub.Override( |
21 android_device, ['subprocess', 'logging']) | 22 android_device, ['subprocess', 'logging']) |
22 | 23 |
23 def _GetMockDeviceUtils(self, device_serial, is_online=True): | 24 def _GetMockDeviceUtils(self, device_serial, is_online=True): |
24 device = device_utils.DeviceUtils(device_serial) | 25 device = device_utils.DeviceUtils(device_serial) |
25 device.IsOnline = mock.MagicMock(return_value=is_online) | 26 device.IsOnline = mock.MagicMock(return_value=is_online) |
26 return device | 27 return device |
27 | 28 |
28 def tearDown(self): | 29 def tearDown(self): |
29 self._healthy_device_patcher.stop() | 30 self._healthy_device_patcher.stop() |
30 self._android_device_stub.Restore() | 31 self._android_device_stub.Restore() |
31 | 32 |
32 | 33 |
33 class AndroidDeviceTest(_BaseAndroidDeviceTest): | 34 class AndroidDeviceTest(_BaseAndroidDeviceTest): |
| 35 @decorators.Enabled('android') |
34 def testGetAllAttachedAndroidDevices(self): | 36 def testGetAllAttachedAndroidDevices(self): |
35 self._healthy_device_mock.return_value = [ | 37 self._healthy_device_mock.return_value = [ |
36 self._GetMockDeviceUtils('01'), | 38 self._GetMockDeviceUtils('01'), |
37 self._GetMockDeviceUtils('07', is_online=False), | 39 self._GetMockDeviceUtils('07', is_online=False), |
38 self._GetMockDeviceUtils('02'), | 40 self._GetMockDeviceUtils('02'), |
39 self._GetMockDeviceUtils('03', is_online=False)] | 41 self._GetMockDeviceUtils('03', is_online=False)] |
40 self.assertEquals( | 42 self.assertEquals( |
41 set(['01', '02']), | 43 set(['01', '02']), |
42 set(device.device_id for device in | 44 set(device.device_id for device in |
43 android_device.AndroidDevice.GetAllConnectedDevices(None))) | 45 android_device.AndroidDevice.GetAllConnectedDevices(None))) |
44 | 46 |
| 47 @decorators.Enabled('android') |
45 def testNoAdbReturnsNone(self): | 48 def testNoAdbReturnsNone(self): |
46 finder_options = browser_options.BrowserFinderOptions() | 49 finder_options = browser_options.BrowserFinderOptions() |
47 with ( | 50 with ( |
48 mock.patch('os.path.isabs', return_value=True)), ( | 51 mock.patch('os.path.isabs', return_value=True)), ( |
49 mock.patch('os.path.exists', return_value=False)): | 52 mock.patch('os.path.exists', return_value=False)): |
50 self.assertEquals([], self._android_device_stub.logging.warnings) | 53 self.assertEquals([], self._android_device_stub.logging.warnings) |
51 self.assertIsNone(android_device.GetDevice(finder_options)) | 54 self.assertIsNone(android_device.GetDevice(finder_options)) |
52 | 55 |
| 56 @decorators.Enabled('android') |
53 def testAdbNoDevicesReturnsNone(self): | 57 def testAdbNoDevicesReturnsNone(self): |
54 finder_options = browser_options.BrowserFinderOptions() | 58 finder_options = browser_options.BrowserFinderOptions() |
55 with mock.patch('os.path.isabs', return_value=False): | 59 with mock.patch('os.path.isabs', return_value=False): |
56 self._healthy_device_mock.return_value = [] | 60 self._healthy_device_mock.return_value = [] |
57 self.assertEquals([], self._android_device_stub.logging.warnings) | 61 self.assertEquals([], self._android_device_stub.logging.warnings) |
58 self.assertIsNone(android_device.GetDevice(finder_options)) | 62 self.assertIsNone(android_device.GetDevice(finder_options)) |
59 | 63 |
| 64 @decorators.Enabled('android') |
60 def testAdbTwoDevicesReturnsNone(self): | 65 def testAdbTwoDevicesReturnsNone(self): |
61 finder_options = browser_options.BrowserFinderOptions() | 66 finder_options = browser_options.BrowserFinderOptions() |
62 with mock.patch('os.path.isabs', return_value=False): | 67 with mock.patch('os.path.isabs', return_value=False): |
63 self._healthy_device_mock.return_value = [ | 68 self._healthy_device_mock.return_value = [ |
64 self._GetMockDeviceUtils('015d14fec128220c'), | 69 self._GetMockDeviceUtils('015d14fec128220c'), |
65 self._GetMockDeviceUtils('015d14fec128220d')] | 70 self._GetMockDeviceUtils('015d14fec128220d')] |
66 device = android_device.GetDevice(finder_options) | 71 device = android_device.GetDevice(finder_options) |
67 self.assertEquals([ | 72 self.assertEquals([ |
68 'Multiple devices attached. Please specify one of the following:\n' | 73 'Multiple devices attached. Please specify one of the following:\n' |
69 ' --device=015d14fec128220c\n' | 74 ' --device=015d14fec128220c\n' |
70 ' --device=015d14fec128220d'], | 75 ' --device=015d14fec128220d'], |
71 self._android_device_stub.logging.warnings) | 76 self._android_device_stub.logging.warnings) |
72 self.assertIsNone(device) | 77 self.assertIsNone(device) |
73 | 78 |
| 79 @decorators.Enabled('android') |
74 def testAdbPickOneDeviceReturnsDeviceInstance(self): | 80 def testAdbPickOneDeviceReturnsDeviceInstance(self): |
75 finder_options = browser_options.BrowserFinderOptions() | 81 finder_options = browser_options.BrowserFinderOptions() |
76 finder_options.device = '555d14fecddddddd' # pick one | 82 finder_options.device = '555d14fecddddddd' # pick one |
77 with mock.patch('os.path.isabs', return_value=False): | 83 with mock.patch('os.path.isabs', return_value=False): |
78 self._healthy_device_mock.return_value = [ | 84 self._healthy_device_mock.return_value = [ |
79 self._GetMockDeviceUtils('015d14fec128220c'), | 85 self._GetMockDeviceUtils('015d14fec128220c'), |
80 self._GetMockDeviceUtils('555d14fecddddddd')] | 86 self._GetMockDeviceUtils('555d14fecddddddd')] |
81 device = android_device.GetDevice(finder_options) | 87 device = android_device.GetDevice(finder_options) |
82 self.assertEquals([], self._android_device_stub.logging.warnings) | 88 self.assertEquals([], self._android_device_stub.logging.warnings) |
83 self.assertEquals('555d14fecddddddd', device.device_id) | 89 self.assertEquals('555d14fecddddddd', device.device_id) |
84 | 90 |
| 91 @decorators.Enabled('android') |
85 def testAdbOneDeviceReturnsDeviceInstance(self): | 92 def testAdbOneDeviceReturnsDeviceInstance(self): |
86 finder_options = browser_options.BrowserFinderOptions() | 93 finder_options = browser_options.BrowserFinderOptions() |
87 with mock.patch('os.path.isabs', return_value=False): | 94 with mock.patch('os.path.isabs', return_value=False): |
88 self._healthy_device_mock.return_value = [ | 95 self._healthy_device_mock.return_value = [ |
89 self._GetMockDeviceUtils('015d14fec128220c')] | 96 self._GetMockDeviceUtils('015d14fec128220c')] |
90 device = android_device.GetDevice(finder_options) | 97 device = android_device.GetDevice(finder_options) |
91 self.assertEquals([], self._android_device_stub.logging.warnings) | 98 self.assertEquals([], self._android_device_stub.logging.warnings) |
92 self.assertEquals('015d14fec128220c', device.device_id) | 99 self.assertEquals('015d14fec128220c', device.device_id) |
93 | 100 |
94 | 101 |
95 class FindAllAvailableDevicesTest(_BaseAndroidDeviceTest): | 102 class FindAllAvailableDevicesTest(_BaseAndroidDeviceTest): |
| 103 @decorators.Enabled('android') |
96 def testAdbNoDeviceReturnsEmptyList(self): | 104 def testAdbNoDeviceReturnsEmptyList(self): |
97 finder_options = browser_options.BrowserFinderOptions() | 105 finder_options = browser_options.BrowserFinderOptions() |
98 with mock.patch('os.path.isabs', return_value=False): | 106 with mock.patch('os.path.isabs', return_value=False): |
99 self._healthy_device_mock.return_value = [] | 107 self._healthy_device_mock.return_value = [] |
100 devices = android_device.FindAllAvailableDevices(finder_options) | 108 devices = android_device.FindAllAvailableDevices(finder_options) |
101 self.assertEquals([], self._android_device_stub.logging.warnings) | 109 self.assertEquals([], self._android_device_stub.logging.warnings) |
102 self.assertIsNotNone(devices) | 110 self.assertIsNotNone(devices) |
103 self.assertEquals(len(devices), 0) | 111 self.assertEquals(len(devices), 0) |
104 | 112 |
| 113 @decorators.Enabled('android') |
105 def testAdbOneDeviceReturnsListWithOneDeviceInstance(self): | 114 def testAdbOneDeviceReturnsListWithOneDeviceInstance(self): |
106 finder_options = browser_options.BrowserFinderOptions() | 115 finder_options = browser_options.BrowserFinderOptions() |
107 with mock.patch('os.path.isabs', return_value=False): | 116 with mock.patch('os.path.isabs', return_value=False): |
108 self._healthy_device_mock.return_value = [ | 117 self._healthy_device_mock.return_value = [ |
109 self._GetMockDeviceUtils('015d14fec128220c')] | 118 self._GetMockDeviceUtils('015d14fec128220c')] |
110 devices = android_device.FindAllAvailableDevices(finder_options) | 119 devices = android_device.FindAllAvailableDevices(finder_options) |
111 self.assertEquals([], self._android_device_stub.logging.warnings) | 120 self.assertEquals([], self._android_device_stub.logging.warnings) |
112 self.assertIsNotNone(devices) | 121 self.assertIsNotNone(devices) |
113 self.assertEquals(len(devices), 1) | 122 self.assertEquals(len(devices), 1) |
114 self.assertEquals('015d14fec128220c', devices[0].device_id) | 123 self.assertEquals('015d14fec128220c', devices[0].device_id) |
115 | 124 |
| 125 @decorators.Enabled('android') |
116 def testAdbMultipleDevicesReturnsListWithAllDeviceInstances(self): | 126 def testAdbMultipleDevicesReturnsListWithAllDeviceInstances(self): |
117 finder_options = browser_options.BrowserFinderOptions() | 127 finder_options = browser_options.BrowserFinderOptions() |
118 with mock.patch('os.path.isabs', return_value=False): | 128 with mock.patch('os.path.isabs', return_value=False): |
119 self._healthy_device_mock.return_value = [ | 129 self._healthy_device_mock.return_value = [ |
120 self._GetMockDeviceUtils('015d14fec128220c'), | 130 self._GetMockDeviceUtils('015d14fec128220c'), |
121 self._GetMockDeviceUtils('this0should0not0show', is_online=False), | 131 self._GetMockDeviceUtils('this0should0not0show', is_online=False), |
122 self._GetMockDeviceUtils('015d14fec128220d'), | 132 self._GetMockDeviceUtils('015d14fec128220d'), |
123 self._GetMockDeviceUtils('015d14fec128220e')] | 133 self._GetMockDeviceUtils('015d14fec128220e')] |
124 devices = android_device.FindAllAvailableDevices(finder_options) | 134 devices = android_device.FindAllAvailableDevices(finder_options) |
125 self.assertEquals([], self._android_device_stub.logging.warnings) | 135 self.assertEquals([], self._android_device_stub.logging.warnings) |
126 self.assertIsNotNone(devices) | 136 self.assertIsNotNone(devices) |
127 self.assertEquals(len(devices), 3) | 137 self.assertEquals(len(devices), 3) |
128 self.assertEquals(devices[0].guid, '015d14fec128220c') | 138 self.assertEquals(devices[0].guid, '015d14fec128220c') |
129 self.assertEquals(devices[1].guid, '015d14fec128220d') | 139 self.assertEquals(devices[1].guid, '015d14fec128220d') |
130 self.assertEquals(devices[2].guid, '015d14fec128220e') | 140 self.assertEquals(devices[2].guid, '015d14fec128220e') |
OLD | NEW |