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 random |
12 import unittest | 13 import unittest |
| 14 |
13 from pylib import android_commands | 15 from pylib import android_commands |
14 from pylib import cmd_helper | 16 from pylib import cmd_helper |
15 from pylib.device import adb_wrapper | 17 from pylib.device import adb_wrapper |
| 18 from pylib.device import device_errors |
16 from pylib.device import device_utils | 19 from pylib.device import device_utils |
17 | 20 |
18 | 21 |
19 class DeviceUtilsTest(unittest.TestCase): | 22 class DeviceUtilsTest(unittest.TestCase): |
20 def testGetAVDs(self): | 23 def testGetAVDs(self): |
21 pass | 24 pass |
22 | 25 |
23 def testRestartServerNotRunning(self): | 26 def testRestartServerNotRunning(self): |
24 # TODO(jbudorick) If these fail, it's not DeviceUtils's fault. | 27 self.assertEqual(0, cmd_helper.RunCmd(['pkill', 'adb']), |
25 self.assertEqual(0, cmd_helper.RunCmd(['pkill', 'adb'])) | 28 msg='Unable to kill adb during setup.') |
26 self.assertNotEqual(0, cmd_helper.RunCmd(['pgrep', 'adb'])) | 29 self.assertNotEqual(0, cmd_helper.RunCmd(['pgrep', 'adb']), |
| 30 msg='Unexpectedly found adb during setup.') |
27 device_utils.RestartServer() | 31 device_utils.RestartServer() |
28 self.assertEqual(0, cmd_helper.RunCmd(['pgrep', 'adb'])) | 32 self.assertEqual(0, cmd_helper.RunCmd(['pgrep', 'adb'])) |
29 | 33 |
30 def testRestartServerAlreadyRunning(self): | 34 def testRestartServerAlreadyRunning(self): |
31 if cmd_helper.RunCmd(['pgrep', 'adb']) != 0: | 35 if cmd_helper.RunCmd(['pgrep', 'adb']) != 0: |
32 device_utils.RestartServer() | 36 device_utils.RestartServer() |
33 code, original_pid = cmd_helper.GetCmdStatusAndOutput(['pgrep', 'adb']) | 37 code, original_pid = cmd_helper.GetCmdStatusAndOutput(['pgrep', 'adb']) |
34 self.assertEqual(0, code) | 38 self.assertEqual(0, code) |
35 device_utils.RestartServer() | 39 device_utils.RestartServer() |
36 code, new_pid = cmd_helper.GetCmdStatusAndOutput(['pgrep', 'adb']) | 40 code, new_pid = cmd_helper.GetCmdStatusAndOutput(['pgrep', 'adb']) |
(...skipping 19 matching lines...) Expand all Loading... |
56 def testInitWithAndroidCommands(self): | 60 def testInitWithAndroidCommands(self): |
57 serial = '0fedcba987654321' | 61 serial = '0fedcba987654321' |
58 a = android_commands.AndroidCommands(device=serial) | 62 a = android_commands.AndroidCommands(device=serial) |
59 d = device_utils.DeviceUtils(a) | 63 d = device_utils.DeviceUtils(a) |
60 self.assertEqual(serial, d.old_interface.GetDevice()) | 64 self.assertEqual(serial, d.old_interface.GetDevice()) |
61 | 65 |
62 def testInitWithNone(self): | 66 def testInitWithNone(self): |
63 d = device_utils.DeviceUtils(None) | 67 d = device_utils.DeviceUtils(None) |
64 self.assertIsNone(d.old_interface.GetDevice()) | 68 self.assertIsNone(d.old_interface.GetDevice()) |
65 | 69 |
| 70 @staticmethod |
| 71 def _getTestAdbWrapper(): |
| 72 devices = adb_wrapper.AdbWrapper.GetDevices() |
| 73 if devices: |
| 74 return random.choice(devices) |
| 75 return None |
| 76 |
| 77 @staticmethod |
| 78 def _getUnusedSerial(): |
| 79 used_devices = [str(d) for d in adb_wrapper.AdbWrapper.GetDevices()] |
| 80 while True: |
| 81 serial = '' |
| 82 for _ in xrange(0, 16): |
| 83 serial += random.choice('0123456789abcdef') |
| 84 if serial not in used_devices: |
| 85 return serial |
| 86 |
| 87 def testIsOnline(self): |
| 88 d = device_utils.DeviceUtils(self._getTestAdbWrapper()) |
| 89 self.assertTrue(d is None or d.IsOnline()) |
| 90 d = device_utils.DeviceUtils(self._getUnusedSerial()) |
| 91 self.assertFalse(d.IsOnline()) |
| 92 |
| 93 def testHasRoot(self): |
| 94 a = self._getTestAdbWrapper() |
| 95 d = device_utils.DeviceUtils(a) |
| 96 secure_prop = a.Shell('getprop ro.secure').strip() |
| 97 if secure_prop == '1': |
| 98 build_type_prop = a.Shell('getprop ro.build.type').strip() |
| 99 if build_type_prop == 'userdebug': |
| 100 adb_root_prop = a.Shell('getprop service.adb.root').strip() |
| 101 if adb_root_prop is None or adb_root_prop == '0': |
| 102 self.assertFalse(d.HasRoot()) |
| 103 else: |
| 104 self.assertTrue(d.HasRoot()) |
| 105 else: |
| 106 self.assertFalse(d.HasRoot()) |
| 107 else: |
| 108 self.assertTrue(d.HasRoot()) |
| 109 |
| 110 def testEnableRoot(self): |
| 111 a = self._getTestAdbWrapper() |
| 112 d = device_utils.DeviceUtils(a) |
| 113 |
| 114 secure_prop = a.Shell('getprop ro.secure').strip() |
| 115 if secure_prop == '1': |
| 116 build_type_prop = a.Shell('getprop ro.build.type').strip() |
| 117 if build_type_prop == 'userdebug': |
| 118 # Turn off the adb root property |
| 119 adb_root_prop = a.Shell('getprop service.adb.root').strip() |
| 120 if adb_root_prop == '1': |
| 121 a.Shell('setprop service.adb.root 0') |
| 122 |
| 123 # Make sure that adbd is running without root by restarting it |
| 124 ps_out = a.Shell('ps') |
| 125 adbd_pids = [] |
| 126 for line in ps_out.splitlines(): |
| 127 if 'adbd' in line: |
| 128 pid = line.split()[1] |
| 129 adbd_pids.append(pid) |
| 130 for pid in adbd_pids: |
| 131 a.Shell('kill %s' % str(pid)) |
| 132 a.WaitForDevice() |
| 133 |
| 134 self.assertFalse(d.HasRoot()) |
| 135 d.EnableRoot() |
| 136 self.assertTrue(d.HasRoot()) |
| 137 else: |
| 138 self.assertFalse(d.HasRoot()) |
| 139 with self.assertRaises(device_errors.CommandFailedError): |
| 140 d.EnableRoot() |
| 141 else: |
| 142 self.assertTrue(d.HasRoot()) |
| 143 d.EnableRoot() |
| 144 self.assertTrue(d.HasRoot()) |
| 145 |
66 | 146 |
67 if __name__ == '__main__': | 147 if __name__ == '__main__': |
68 unittest.main() | 148 unittest.main(verbosity=2, buffer=True) |
69 | 149 |
OLD | NEW |