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 """ | 5 """ |
6 Unit tests for the contents of device_utils.py (mostly DeviceUtils). | 6 Unit tests for the contents of device_utils.py (mostly DeviceUtils). |
7 """ | 7 """ |
8 | 8 |
9 # pylint: disable=W0212 | 9 # pylint: disable=W0212 |
10 # pylint: disable=W0613 | 10 # pylint: disable=W0613 |
11 | 11 |
| 12 import functools |
12 import random | 13 import random |
13 import time | 14 import time |
14 import unittest | 15 import unittest |
15 | 16 |
16 from pylib import android_commands | 17 from pylib import android_commands |
17 from pylib import cmd_helper | 18 from pylib import cmd_helper |
18 from pylib.device import adb_wrapper | 19 from pylib.device import adb_wrapper |
19 from pylib.device import device_errors | 20 from pylib.device import device_errors |
20 from pylib.device import device_utils | 21 from pylib.device import device_utils |
21 | 22 |
22 | 23 |
| 24 def TestRequiresDevice(f): |
| 25 @functools.wraps(f) |
| 26 def wrapper(*args, **kwargs): |
| 27 if len(adb_wrapper.AdbWrapper.GetDevices()) > 0: |
| 28 return f(*args, **kwargs) |
| 29 return unittest.skip('Test requires an attached device.') |
| 30 return wrapper |
| 31 |
| 32 |
23 class DeviceUtilsTest(unittest.TestCase): | 33 class DeviceUtilsTest(unittest.TestCase): |
24 def testGetAVDs(self): | 34 def testGetAVDs(self): |
25 pass | 35 pass |
26 | 36 |
| 37 @TestRequiresDevice |
27 def testRestartServerNotRunning(self): | 38 def testRestartServerNotRunning(self): |
28 self.assertEqual(0, cmd_helper.RunCmd(['pkill', 'adb']), | 39 self.assertEqual(0, cmd_helper.RunCmd(['pkill', 'adb']), |
29 msg='Unable to kill adb during setup.') | 40 msg='Unable to kill adb during setup.') |
30 self.assertNotEqual(0, cmd_helper.RunCmd(['pgrep', 'adb']), | 41 self.assertNotEqual(0, cmd_helper.RunCmd(['pgrep', 'adb']), |
31 msg='Unexpectedly found adb during setup.') | 42 msg='Unexpectedly found adb during setup.') |
32 device_utils.RestartServer() | 43 device_utils.RestartServer() |
33 self.assertEqual(0, cmd_helper.RunCmd(['pgrep', 'adb'])) | 44 self.assertEqual(0, cmd_helper.RunCmd(['pgrep', 'adb'])) |
34 | 45 |
| 46 @TestRequiresDevice |
35 def testRestartServerAlreadyRunning(self): | 47 def testRestartServerAlreadyRunning(self): |
36 if cmd_helper.RunCmd(['pgrep', 'adb']) != 0: | 48 if cmd_helper.RunCmd(['pgrep', 'adb']) != 0: |
37 device_utils.RestartServer() | 49 device_utils.RestartServer() |
38 code, original_pid = cmd_helper.GetCmdStatusAndOutput(['pgrep', 'adb']) | 50 code, original_pid = cmd_helper.GetCmdStatusAndOutput(['pgrep', 'adb']) |
39 self.assertEqual(0, code) | 51 self.assertEqual(0, code) |
40 device_utils.RestartServer() | 52 device_utils.RestartServer() |
41 code, new_pid = cmd_helper.GetCmdStatusAndOutput(['pgrep', 'adb']) | 53 code, new_pid = cmd_helper.GetCmdStatusAndOutput(['pgrep', 'adb']) |
42 self.assertEqual(0, code) | 54 self.assertEqual(0, code) |
43 self.assertNotEqual(original_pid, new_pid) | 55 self.assertNotEqual(original_pid, new_pid) |
44 | 56 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 @staticmethod | 94 @staticmethod |
83 def _getUnusedSerial(): | 95 def _getUnusedSerial(): |
84 used_devices = [str(d) for d in adb_wrapper.AdbWrapper.GetDevices()] | 96 used_devices = [str(d) for d in adb_wrapper.AdbWrapper.GetDevices()] |
85 while True: | 97 while True: |
86 serial = '' | 98 serial = '' |
87 for _ in xrange(0, 16): | 99 for _ in xrange(0, 16): |
88 serial += random.choice('0123456789abcdef') | 100 serial += random.choice('0123456789abcdef') |
89 if serial not in used_devices: | 101 if serial not in used_devices: |
90 return serial | 102 return serial |
91 | 103 |
| 104 @TestRequiresDevice |
92 def testIsOnline(self): | 105 def testIsOnline(self): |
93 d = device_utils.DeviceUtils(self._getTestAdbWrapper()) | 106 d = device_utils.DeviceUtils(self._getTestAdbWrapper()) |
94 self.assertTrue(d is None or d.IsOnline()) | 107 self.assertTrue(d is None or d.IsOnline()) |
95 d = device_utils.DeviceUtils(self._getUnusedSerial()) | 108 d = device_utils.DeviceUtils(self._getUnusedSerial()) |
96 self.assertFalse(d.IsOnline()) | 109 self.assertFalse(d.IsOnline()) |
97 | 110 |
| 111 @TestRequiresDevice |
98 def testHasRoot(self): | 112 def testHasRoot(self): |
99 a = self._getTestAdbWrapper() | 113 a = self._getTestAdbWrapper() |
100 d = device_utils.DeviceUtils(a) | 114 d = device_utils.DeviceUtils(a) |
101 secure_prop = a.Shell('getprop ro.secure').strip() | 115 secure_prop = a.Shell('getprop ro.secure').strip() |
102 if secure_prop == '1': | 116 if secure_prop == '1': |
103 build_type_prop = a.Shell('getprop ro.build.type').strip() | 117 build_type_prop = a.Shell('getprop ro.build.type').strip() |
104 if build_type_prop == 'userdebug': | 118 if build_type_prop == 'userdebug': |
105 adb_root_prop = a.Shell('getprop service.adb.root').strip() | 119 adb_root_prop = a.Shell('getprop service.adb.root').strip() |
106 if adb_root_prop is None or adb_root_prop == '0': | 120 if adb_root_prop is None or adb_root_prop == '0': |
107 self.assertFalse(d.HasRoot()) | 121 self.assertFalse(d.HasRoot()) |
108 else: | 122 else: |
109 self.assertTrue(d.HasRoot()) | 123 self.assertTrue(d.HasRoot()) |
110 else: | 124 else: |
111 self.assertFalse(d.HasRoot()) | 125 self.assertFalse(d.HasRoot()) |
112 else: | 126 else: |
113 self.assertTrue(d.HasRoot()) | 127 self.assertTrue(d.HasRoot()) |
114 | 128 |
| 129 @TestRequiresDevice |
115 def testEnableRoot(self): | 130 def testEnableRoot(self): |
116 a = self._getTestAdbWrapper() | 131 a = self._getTestAdbWrapper() |
117 d = device_utils.DeviceUtils(a) | 132 d = device_utils.DeviceUtils(a) |
118 | 133 |
119 secure_prop = a.Shell('getprop ro.secure').strip() | 134 secure_prop = a.Shell('getprop ro.secure').strip() |
120 if secure_prop == '1': | 135 if secure_prop == '1': |
121 build_type_prop = a.Shell('getprop ro.build.type').strip() | 136 build_type_prop = a.Shell('getprop ro.build.type').strip() |
122 if build_type_prop == 'userdebug': | 137 if build_type_prop == 'userdebug': |
123 # Turn off the adb root property | 138 # Turn off the adb root property |
124 adb_root_prop = a.Shell('getprop service.adb.root').strip() | 139 adb_root_prop = a.Shell('getprop service.adb.root').strip() |
(...skipping 16 matching lines...) Expand all Loading... |
141 self.assertTrue(d.HasRoot()) | 156 self.assertTrue(d.HasRoot()) |
142 else: | 157 else: |
143 self.assertFalse(d.HasRoot()) | 158 self.assertFalse(d.HasRoot()) |
144 with self.assertRaises(device_errors.CommandFailedError): | 159 with self.assertRaises(device_errors.CommandFailedError): |
145 d.EnableRoot() | 160 d.EnableRoot() |
146 else: | 161 else: |
147 self.assertTrue(d.HasRoot()) | 162 self.assertTrue(d.HasRoot()) |
148 d.EnableRoot() | 163 d.EnableRoot() |
149 self.assertTrue(d.HasRoot()) | 164 self.assertTrue(d.HasRoot()) |
150 | 165 |
| 166 @TestRequiresDevice |
151 def testGetExternalStorage(self): | 167 def testGetExternalStorage(self): |
152 a = self._getTestAdbWrapper() | 168 a = self._getTestAdbWrapper() |
153 d = device_utils.DeviceUtils(a) | 169 d = device_utils.DeviceUtils(a) |
154 | 170 |
155 actual_external_storage = a.Shell('echo $EXTERNAL_STORAGE').splitlines()[0] | 171 actual_external_storage = a.Shell('echo $EXTERNAL_STORAGE').splitlines()[0] |
156 if actual_external_storage and len(actual_external_storage) != 0: | 172 if actual_external_storage and len(actual_external_storage) != 0: |
157 self.assertEquals(actual_external_storage, d.GetExternalStoragePath()) | 173 self.assertEquals(actual_external_storage, d.GetExternalStoragePath()) |
158 | 174 |
| 175 @TestRequiresDevice |
159 def testWaitUntilFullyBooted(self): | 176 def testWaitUntilFullyBooted(self): |
160 a = self._getTestAdbWrapper() | 177 a = self._getTestAdbWrapper() |
161 d = device_utils.DeviceUtils(a) | 178 d = device_utils.DeviceUtils(a) |
162 | 179 |
163 a.Reboot() | 180 a.Reboot() |
164 while a.GetState() == 'device': | 181 while a.GetState() == 'device': |
165 time.sleep(0.1) | 182 time.sleep(0.1) |
166 d.WaitUntilFullyBooted(wifi=True) | 183 d.WaitUntilFullyBooted(wifi=True) |
167 self.assertEquals( | 184 self.assertEquals( |
168 '1', a.Shell('getprop sys.boot_completed').splitlines()[0]) | 185 '1', a.Shell('getprop sys.boot_completed').splitlines()[0]) |
169 self.assertTrue( | 186 self.assertTrue( |
170 a.Shell('pm path android').splitlines()[0].startswith('package:')) | 187 a.Shell('pm path android').splitlines()[0].startswith('package:')) |
171 self.assertTrue(a.Shell('ls $EXTERNAL_STORAGE')) | 188 self.assertTrue(a.Shell('ls $EXTERNAL_STORAGE')) |
172 self.assertTrue( | 189 self.assertTrue( |
173 'Wi-Fi is enabled' in a.Shell('dumpsys wifi').splitlines()) | 190 'Wi-Fi is enabled' in a.Shell('dumpsys wifi').splitlines()) |
174 | 191 |
| 192 @TestRequiresDevice |
| 193 def testBlockingReboot(self): |
| 194 a = self._getTestAdbWrapper() |
| 195 d = device_utils.DeviceUtils(a) |
| 196 |
| 197 old_boot_time = a.Shell('getprop ro.runtime.firstboot').strip() |
| 198 if old_boot_time and len(old_boot_time): |
| 199 d.Reboot(block=True, timeout=120) |
| 200 self.assertNotEquals(old_boot_time, |
| 201 a.Shell('getprop ro.runtime.firstboot').strip()) |
| 202 self.assertEquals( |
| 203 '1', a.Shell('getprop sys.boot_completed').splitlines()[0]) |
| 204 self.assertTrue( |
| 205 a.Shell('pm path android').splitlines()[0].startswith('package:')) |
| 206 self.assertTrue(a.Shell('ls $EXTERNAL_STORAGE')) |
| 207 else: |
| 208 self.skipTest("No 'ro.runtime.firstboot' property on %s." % str(a)) |
| 209 |
175 | 210 |
176 if __name__ == '__main__': | 211 if __name__ == '__main__': |
177 unittest.main(verbosity=2, buffer=True) | 212 unittest.main(verbosity=2) |
178 | 213 |
OLD | NEW |