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=C0321 |
9 # pylint: disable=W0212 | 10 # pylint: disable=W0212 |
10 # pylint: disable=W0613 | 11 # pylint: disable=W0613 |
11 | 12 |
12 import functools | 13 import os |
13 import random | 14 import sys |
14 import time | |
15 import unittest | 15 import unittest |
16 | 16 |
17 from pylib import android_commands | 17 from pylib import android_commands |
18 from pylib import cmd_helper | 18 from pylib import constants |
19 from pylib.device import adb_wrapper | 19 from pylib.device import adb_wrapper |
20 from pylib.device import device_errors | 20 from pylib.device import device_errors |
21 from pylib.device import device_utils | 21 from pylib.device import device_utils |
22 | 22 |
| 23 sys.path.append(os.path.join( |
| 24 constants.DIR_SOURCE_ROOT, 'third_party', 'android_testrunner')) |
| 25 import run_command as atr_run_command |
23 | 26 |
24 def TestRequiresDevice(f): | 27 sys.path.append(os.path.join( |
25 @functools.wraps(f) | 28 constants.DIR_SOURCE_ROOT, 'third_party', 'pymock')) |
26 def wrapper(*args, **kwargs): | 29 import mock # pylint: disable=F0401 |
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 | 30 |
32 | 31 |
33 class DeviceUtilsTest(unittest.TestCase): | 32 class DeviceUtilsTest(unittest.TestCase): |
34 def testGetAVDs(self): | |
35 pass | |
36 | |
37 @TestRequiresDevice | |
38 def testRestartServerNotRunning(self): | |
39 self.assertEqual(0, cmd_helper.RunCmd(['pkill', 'adb']), | |
40 msg='Unable to kill adb during setup.') | |
41 self.assertNotEqual(0, cmd_helper.RunCmd(['pgrep', 'adb']), | |
42 msg='Unexpectedly found adb during setup.') | |
43 device_utils.RestartServer() | |
44 self.assertEqual(0, cmd_helper.RunCmd(['pgrep', 'adb'])) | |
45 | |
46 @TestRequiresDevice | |
47 def testRestartServerAlreadyRunning(self): | |
48 if cmd_helper.RunCmd(['pgrep', 'adb']) != 0: | |
49 device_utils.RestartServer() | |
50 code, original_pid = cmd_helper.GetCmdStatusAndOutput(['pgrep', 'adb']) | |
51 self.assertEqual(0, code) | |
52 device_utils.RestartServer() | |
53 code, new_pid = cmd_helper.GetCmdStatusAndOutput(['pgrep', 'adb']) | |
54 self.assertEqual(0, code) | |
55 self.assertNotEqual(original_pid, new_pid) | |
56 | 33 |
57 def testInitWithStr(self): | 34 def testInitWithStr(self): |
58 serial_as_str = str('0123456789abcdef') | 35 serial_as_str = str('0123456789abcdef') |
59 d = device_utils.DeviceUtils('0123456789abcdef') | 36 d = device_utils.DeviceUtils('0123456789abcdef') |
60 self.assertEqual(serial_as_str, d.old_interface.GetDevice()) | 37 self.assertEqual(serial_as_str, d.old_interface.GetDevice()) |
61 | 38 |
62 def testInitWithUnicode(self): | 39 def testInitWithUnicode(self): |
63 serial_as_unicode = unicode('fedcba9876543210') | 40 serial_as_unicode = unicode('fedcba9876543210') |
64 d = device_utils.DeviceUtils(serial_as_unicode) | 41 d = device_utils.DeviceUtils(serial_as_unicode) |
65 self.assertEqual(serial_as_unicode, d.old_interface.GetDevice()) | 42 self.assertEqual(serial_as_unicode, d.old_interface.GetDevice()) |
66 | 43 |
67 def testInitWithAdbWrapper(self): | 44 def testInitWithAdbWrapper(self): |
68 serial = '123456789abcdef0' | 45 serial = '123456789abcdef0' |
69 a = adb_wrapper.AdbWrapper(serial) | 46 a = adb_wrapper.AdbWrapper(serial) |
70 d = device_utils.DeviceUtils(a) | 47 d = device_utils.DeviceUtils(a) |
71 self.assertEqual(serial, d.old_interface.GetDevice()) | 48 self.assertEqual(serial, d.old_interface.GetDevice()) |
72 | 49 |
73 def testInitWithAndroidCommands(self): | 50 def testInitWithAndroidCommands(self): |
74 serial = '0fedcba987654321' | 51 serial = '0fedcba987654321' |
75 a = android_commands.AndroidCommands(device=serial) | 52 a = android_commands.AndroidCommands(device=serial) |
76 d = device_utils.DeviceUtils(a) | 53 d = device_utils.DeviceUtils(a) |
77 self.assertEqual(serial, d.old_interface.GetDevice()) | 54 self.assertEqual(serial, d.old_interface.GetDevice()) |
78 | 55 |
79 def testInitWithNone(self): | 56 def testInitWithNone(self): |
80 d = device_utils.DeviceUtils(None) | 57 d = device_utils.DeviceUtils(None) |
81 self.assertIsNone(d.old_interface.GetDevice()) | 58 self.assertIsNone(d.old_interface.GetDevice()) |
82 | 59 |
83 @staticmethod | 60 |
84 def _getTestAdbWrapper(): | 61 class DeviceUtilsOldImplTest(unittest.TestCase): |
85 devices = adb_wrapper.AdbWrapper.GetDevices() | 62 |
86 start_time = time.time() | 63 class AndroidCommandsCalls(object): |
87 while time.time() - start_time < device_utils._DEFAULT_TIMEOUT: | 64 |
88 if devices: | 65 def __init__(self, test_case, cmd_ret): |
89 return random.choice(devices) | 66 self._cmds = cmd_ret |
90 time.sleep(1) | 67 self._test_case = test_case |
91 raise device_errors.DeviceUnreachableError( | 68 self._total_received = 0 |
92 'No devices available for testing...') | 69 |
93 | 70 def __enter__(self): |
94 @staticmethod | 71 atr_run_command.RunCommand = mock.Mock() |
95 def _getUnusedSerial(): | 72 atr_run_command.RunCommand.side_effect = lambda c, **kw: self._ret(c) |
96 used_devices = [str(d) for d in adb_wrapper.AdbWrapper.GetDevices()] | 73 |
97 while True: | 74 def _ret(self, actual_cmd): |
98 serial = '' | 75 on_failure_fmt = ('\n' |
99 for _ in xrange(0, 16): | 76 ' received command: %s\n' |
100 serial += random.choice('0123456789abcdef') | 77 ' expected command: %s') |
101 if serial not in used_devices: | 78 self._test_case.assertGreater( |
102 return serial | 79 len(self._cmds), self._total_received, |
103 | 80 msg=on_failure_fmt % (actual_cmd, None)) |
104 @TestRequiresDevice | 81 expected_cmd, ret = self._cmds[self._total_received] |
105 def testIsOnline(self): | 82 self._total_received += 1 |
106 d = device_utils.DeviceUtils(self._getTestAdbWrapper()) | 83 self._test_case.assertEqual( |
107 self.assertTrue(d is None or d.IsOnline()) | 84 actual_cmd, expected_cmd, |
108 d = device_utils.DeviceUtils(self._getUnusedSerial()) | 85 msg=on_failure_fmt % (actual_cmd, expected_cmd)) |
109 self.assertFalse(d.IsOnline()) | 86 return ret |
110 | 87 |
111 @TestRequiresDevice | 88 def __exit__(self, exc_type, _exc_val, exc_trace): |
112 def testHasRoot(self): | 89 if exc_type is None: |
113 a = self._getTestAdbWrapper() | 90 on_failure = "adb commands don't match.\nExpected:%s\nActual:%s" % ( |
114 d = device_utils.DeviceUtils(a) | 91 ''.join('\n %s' % c for c, _ in self._cmds), |
115 secure_prop = a.Shell('getprop ro.secure').strip() | 92 ''.join('\n %s' % a[0] |
116 if secure_prop == '1': | 93 for _, a, kw in atr_run_command.RunCommand.mock_calls)) |
117 build_type_prop = a.Shell('getprop ro.build.type').strip() | 94 self._test_case.assertEqual( |
118 if build_type_prop == 'userdebug': | 95 len(self._cmds), len(atr_run_command.RunCommand.mock_calls), |
119 adb_root_prop = a.Shell('getprop service.adb.root').strip() | 96 msg=on_failure) |
120 if adb_root_prop is None or adb_root_prop == '0': | 97 for (expected_cmd, _r), (_n, actual_args, actual_kwargs) in zip( |
121 self.assertFalse(d.HasRoot()) | 98 self._cmds, atr_run_command.RunCommand.mock_calls): |
122 else: | 99 self._test_case.assertEqual(1, len(actual_args), msg=on_failure) |
123 self.assertTrue(d.HasRoot()) | 100 self._test_case.assertEqual(expected_cmd, actual_args[0], |
124 else: | 101 msg=on_failure) |
125 self.assertFalse(d.HasRoot()) | 102 self._test_case.assertTrue('timeout_time' in actual_kwargs, |
126 else: | 103 msg=on_failure) |
127 self.assertTrue(d.HasRoot()) | 104 self._test_case.assertTrue('retry_count' in actual_kwargs, |
128 | 105 msg=on_failure) |
129 @TestRequiresDevice | 106 |
130 def testEnableRoot(self): | 107 def assertOldImplCalls(self, cmd, ret): |
131 a = self._getTestAdbWrapper() | 108 return type(self).AndroidCommandsCalls(self, [(cmd, ret)]) |
132 d = device_utils.DeviceUtils(a) | 109 |
133 | 110 def assertOldImplCallsSequence(self, cmd_ret): |
134 secure_prop = a.Shell('getprop ro.secure').strip() | 111 return type(self).AndroidCommandsCalls(self, cmd_ret) |
135 if secure_prop == '1': | 112 |
136 build_type_prop = a.Shell('getprop ro.build.type').strip() | 113 def setUp(self): |
137 if build_type_prop == 'userdebug': | 114 self.device = device_utils.DeviceUtils( |
138 # Turn off the adb root property | 115 '0123456789abcdef', default_timeout=1, default_retries=0) |
139 adb_root_prop = a.Shell('getprop service.adb.root').strip() | 116 |
140 if adb_root_prop == '1': | 117 def testIsOnline_true(self): |
141 a.Shell('setprop service.adb.root 0') | 118 with self.assertOldImplCalls('adb -s 0123456789abcdef get-state', |
142 | 119 'device\r\n'): |
143 # Make sure that adbd is running without root by restarting it | 120 self.assertTrue(self.device.IsOnline()) |
144 ps_out = a.Shell('ps') | 121 |
145 adbd_pids = [] | 122 def testIsOnline_false(self): |
146 for line in ps_out.splitlines(): | 123 with self.assertOldImplCalls('adb -s 0123456789abcdef get-state', '\r\n'): |
147 if 'adbd' in line: | 124 self.assertFalse(self.device.IsOnline()) |
148 pid = line.split()[1] | 125 |
149 adbd_pids.append(pid) | 126 def testHasRoot_true(self): |
150 for pid in adbd_pids: | 127 with self.assertOldImplCalls("adb -s 0123456789abcdef shell 'ls /root'", |
151 a.Shell('kill %s' % str(pid)) | 128 'foo\r\n'): |
152 a.WaitForDevice() | 129 self.assertTrue(self.device.HasRoot()) |
153 | 130 |
154 self.assertFalse(d.HasRoot()) | 131 def testHasRoot_false(self): |
155 d.EnableRoot() | 132 with self.assertOldImplCalls("adb -s 0123456789abcdef shell 'ls /root'", |
156 self.assertTrue(d.HasRoot()) | 133 'Permission denied\r\n'): |
157 else: | 134 self.assertFalse(self.device.HasRoot()) |
158 self.assertFalse(d.HasRoot()) | 135 |
| 136 def testEnableRoot_succeeds(self): |
| 137 with self.assertOldImplCallsSequence([ |
| 138 ('adb -s 0123456789abcdef shell getprop ro.build.type', |
| 139 'userdebug\r\n'), |
| 140 ('adb -s 0123456789abcdef root', 'restarting adbd as root\r\n'), |
| 141 ('adb -s 0123456789abcdef wait-for-device', ''), |
| 142 ('adb -s 0123456789abcdef wait-for-device', '')]): |
| 143 self.device.EnableRoot() |
| 144 |
| 145 def testEnableRoot_userBuild(self): |
| 146 with self.assertOldImplCallsSequence([ |
| 147 ('adb -s 0123456789abcdef shell getprop ro.build.type', 'user\r\n')]): |
| 148 with self.assertRaises(device_errors.CommandFailedError): |
| 149 self.device.EnableRoot() |
| 150 |
| 151 def testEnableRoot_rootFails(self): |
| 152 with self.assertOldImplCallsSequence([ |
| 153 ('adb -s 0123456789abcdef shell getprop ro.build.type', |
| 154 'userdebug\r\n'), |
| 155 ('adb -s 0123456789abcdef root', 'no\r\n'), |
| 156 ('adb -s 0123456789abcdef wait-for-device', '')]): |
| 157 with self.assertRaises(device_errors.CommandFailedError): |
| 158 self.device.EnableRoot() |
| 159 |
| 160 def testGetExternalStoragePath_succeeds(self): |
| 161 fakeStoragePath = '/fake/storage/path' |
| 162 with self.assertOldImplCalls( |
| 163 "adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", |
| 164 '%s\r\n' % fakeStoragePath): |
| 165 self.assertEquals(fakeStoragePath, |
| 166 self.device.GetExternalStoragePath()) |
| 167 |
| 168 def testGetExternalStoragePath_fails(self): |
| 169 with self.assertOldImplCalls( |
| 170 "adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", '\r\n'): |
| 171 with self.assertRaises(device_errors.CommandFailedError): |
| 172 self.device.GetExternalStoragePath() |
| 173 |
| 174 def testWaitUntilFullyBooted_succeedsNoWifi(self): |
| 175 with self.assertOldImplCallsSequence([ |
| 176 # AndroidCommands.WaitForSystemBootCompleted |
| 177 ('adb -s 0123456789abcdef wait-for-device', ''), |
| 178 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', '1\r\n'), |
| 179 # AndroidCommands.WaitForDevicePm |
| 180 ('adb -s 0123456789abcdef wait-for-device', ''), |
| 181 ('adb -s 0123456789abcdef shell pm path android', |
| 182 'package:this.is.a.test.package'), |
| 183 # AndroidCommands.WaitForSdCardReady |
| 184 ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", |
| 185 '/fake/storage/path'), |
| 186 ("adb -s 0123456789abcdef shell 'ls /fake/storage/path'", |
| 187 'nothing\r\n') |
| 188 ]): |
| 189 self.device.WaitUntilFullyBooted(wifi=False) |
| 190 |
| 191 def testWaitUntilFullyBooted_succeedsWithWifi(self): |
| 192 with self.assertOldImplCallsSequence([ |
| 193 # AndroidCommands.WaitForSystemBootCompleted |
| 194 ('adb -s 0123456789abcdef wait-for-device', ''), |
| 195 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', '1\r\n'), |
| 196 # AndroidCommands.WaitForDevicePm |
| 197 ('adb -s 0123456789abcdef wait-for-device', ''), |
| 198 ('adb -s 0123456789abcdef shell pm path android', |
| 199 'package:this.is.a.test.package'), |
| 200 # AndroidCommands.WaitForSdCardReady |
| 201 ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", |
| 202 '/fake/storage/path'), |
| 203 ("adb -s 0123456789abcdef shell 'ls /fake/storage/path'", |
| 204 'nothing\r\n'), |
| 205 # wait for wifi |
| 206 ("adb -s 0123456789abcdef shell 'dumpsys wifi'", 'Wi-Fi is enabled')]): |
| 207 self.device.WaitUntilFullyBooted(wifi=True) |
| 208 |
| 209 def testWaitUntilFullyBooted_bootFails(self): |
| 210 with mock.patch('time.sleep'): |
| 211 with self.assertOldImplCallsSequence([ |
| 212 # AndroidCommands.WaitForSystemBootCompleted |
| 213 ('adb -s 0123456789abcdef wait-for-device', ''), |
| 214 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', |
| 215 '0\r\n')]): |
| 216 with self.assertRaises(device_errors.CommandTimeoutError): |
| 217 self.device.WaitUntilFullyBooted(wifi=False) |
| 218 |
| 219 def testWaitUntilFullyBooted_devicePmFails(self): |
| 220 with mock.patch('time.sleep'): |
| 221 with self.assertOldImplCallsSequence([ |
| 222 # AndroidCommands.WaitForSystemBootCompleted |
| 223 ('adb -s 0123456789abcdef wait-for-device', ''), |
| 224 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', |
| 225 '1\r\n')] |
| 226 # AndroidCommands.WaitForDevicePm |
| 227 + 3 * ([('adb -s 0123456789abcdef wait-for-device', '')] |
| 228 + 24 * [('adb -s 0123456789abcdef shell pm path android', '\r\n')] |
| 229 + [("adb -s 0123456789abcdef shell 'stop'", '\r\n'), |
| 230 ("adb -s 0123456789abcdef shell 'start'", '\r\n')])): |
| 231 with self.assertRaises(device_errors.CommandTimeoutError): |
| 232 self.device.WaitUntilFullyBooted(wifi=False) |
| 233 |
| 234 def testWaitUntilFullyBooted_sdCardReadyFails_noPath(self): |
| 235 with mock.patch('time.sleep'): |
| 236 with self.assertOldImplCallsSequence([ |
| 237 # AndroidCommands.WaitForSystemBootCompleted |
| 238 ('adb -s 0123456789abcdef wait-for-device', ''), |
| 239 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', |
| 240 '1\r\n'), |
| 241 # AndroidCommands.WaitForDevicePm |
| 242 ('adb -s 0123456789abcdef wait-for-device', ''), |
| 243 ('adb -s 0123456789abcdef shell pm path android', |
| 244 'package:this.is.a.test.package'), |
| 245 ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", '\r\n')]): |
159 with self.assertRaises(device_errors.CommandFailedError): | 246 with self.assertRaises(device_errors.CommandFailedError): |
160 d.EnableRoot() | 247 self.device.WaitUntilFullyBooted(wifi=False) |
161 else: | 248 |
162 self.assertTrue(d.HasRoot()) | 249 def testWaitUntilFullyBooted_sdCardReadyFails_emptyPath(self): |
163 d.EnableRoot() | 250 with mock.patch('time.sleep'): |
164 self.assertTrue(d.HasRoot()) | 251 with self.assertOldImplCallsSequence([ |
165 | 252 # AndroidCommands.WaitForSystemBootCompleted |
166 @TestRequiresDevice | 253 ('adb -s 0123456789abcdef wait-for-device', ''), |
167 def testGetExternalStorage(self): | 254 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', |
168 a = self._getTestAdbWrapper() | 255 '1\r\n'), |
169 d = device_utils.DeviceUtils(a) | 256 # AndroidCommands.WaitForDevicePm |
170 | 257 ('adb -s 0123456789abcdef wait-for-device', ''), |
171 actual_external_storage = a.Shell('echo $EXTERNAL_STORAGE').splitlines()[0] | 258 ('adb -s 0123456789abcdef shell pm path android', |
172 if actual_external_storage and len(actual_external_storage) != 0: | 259 'package:this.is.a.test.package'), |
173 self.assertEquals(actual_external_storage, d.GetExternalStoragePath()) | 260 ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", |
174 | 261 '/fake/storage/path\r\n'), |
175 @TestRequiresDevice | 262 ("adb -s 0123456789abcdef shell 'ls /fake/storage/path'", '')]): |
176 def testWaitUntilFullyBooted(self): | 263 with self.assertRaises(device_errors.CommandTimeoutError): |
177 a = self._getTestAdbWrapper() | 264 self.device.WaitUntilFullyBooted(wifi=False) |
178 d = device_utils.DeviceUtils(a) | 265 |
179 | 266 def testReboot_nonBlocking(self): |
180 a.Reboot() | 267 with mock.patch('time.sleep'): |
181 while a.GetState() == 'device': | 268 with self.assertOldImplCallsSequence([ |
182 time.sleep(0.1) | 269 ('adb -s 0123456789abcdef reboot', ''), |
183 d.WaitUntilFullyBooted(wifi=True) | 270 ('adb -s 0123456789abcdef get-state', 'unknown\r\n'), |
184 self.assertEquals( | 271 ('adb -s 0123456789abcdef wait-for-device', ''), |
185 '1', a.Shell('getprop sys.boot_completed').splitlines()[0]) | 272 ('adb -s 0123456789abcdef shell pm path android', |
186 self.assertTrue( | 273 'package:this.is.a.test.package'), |
187 a.Shell('pm path android').splitlines()[0].startswith('package:')) | 274 ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", |
188 self.assertTrue(a.Shell('ls $EXTERNAL_STORAGE')) | 275 '/fake/storage/path\r\n'), |
189 self.assertTrue( | 276 ("adb -s 0123456789abcdef shell 'ls /fake/storage/path'", |
190 'Wi-Fi is enabled' in a.Shell('dumpsys wifi').splitlines()) | 277 'nothing\r\n')]): |
191 | 278 self.device.Reboot(block=False) |
192 @TestRequiresDevice | 279 |
193 def testBlockingReboot(self): | 280 def testReboot_blocking(self): |
194 a = self._getTestAdbWrapper() | 281 with mock.patch('time.sleep'): |
195 d = device_utils.DeviceUtils(a) | 282 with self.assertOldImplCallsSequence([ |
196 | 283 ('adb -s 0123456789abcdef reboot', ''), |
197 old_boot_time = a.Shell('getprop ro.runtime.firstboot').strip() | 284 ('adb -s 0123456789abcdef get-state', 'unknown\r\n'), |
198 if old_boot_time and len(old_boot_time): | 285 ('adb -s 0123456789abcdef wait-for-device', ''), |
199 d.Reboot(block=True, timeout=120) | 286 ('adb -s 0123456789abcdef shell pm path android', |
200 self.assertNotEquals(old_boot_time, | 287 'package:this.is.a.test.package'), |
201 a.Shell('getprop ro.runtime.firstboot').strip()) | 288 ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", |
202 self.assertEquals( | 289 '/fake/storage/path\r\n'), |
203 '1', a.Shell('getprop sys.boot_completed').splitlines()[0]) | 290 ("adb -s 0123456789abcdef shell 'ls /fake/storage/path'", |
204 self.assertTrue( | 291 'nothing\r\n'), |
205 a.Shell('pm path android').splitlines()[0].startswith('package:')) | 292 ('adb -s 0123456789abcdef wait-for-device', ''), |
206 self.assertTrue(a.Shell('ls $EXTERNAL_STORAGE')) | 293 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', |
207 else: | 294 '1\r\n'), |
208 self.skipTest("No 'ro.runtime.firstboot' property on %s." % str(a)) | 295 ('adb -s 0123456789abcdef wait-for-device', ''), |
209 | 296 ('adb -s 0123456789abcdef shell pm path android', |
| 297 'package:this.is.a.test.package'), |
| 298 ("adb -s 0123456789abcdef shell 'ls /fake/storage/path'", |
| 299 'nothing\r\n')]): |
| 300 self.device.Reboot(block=True) |
| 301 |
| 302 def testInstall_noPriorInstall(self): |
| 303 with mock.patch('os.path.isfile', return_value=True), ( |
| 304 mock.patch('pylib.utils.apk_helper.GetPackageName', |
| 305 return_value='this.is.a.test.package')): |
| 306 with self.assertOldImplCallsSequence([ |
| 307 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", |
| 308 ''), |
| 309 ("adb -s 0123456789abcdef install /fake/test/app.apk", |
| 310 'Success\r\n')]): |
| 311 self.device.Install('/fake/test/app.apk', retries=0) |
| 312 |
| 313 def testInstall_differentPriorInstall(self): |
| 314 def mockGetFilesChanged(host_path, device_path, ignore_filenames): |
| 315 return [(host_path, device_path)] |
| 316 |
| 317 # Pylint raises a false positive "operator not preceded by a space" |
| 318 # warning below. |
| 319 # pylint: disable=C0322 |
| 320 with mock.patch('os.path.isfile', return_value=True), ( |
| 321 mock.patch('os.path.exists', return_value=True)), ( |
| 322 mock.patch('pylib.utils.apk_helper.GetPackageName', |
| 323 return_value='this.is.a.test.package')), ( |
| 324 mock.patch('pylib.constants.GetOutDirectory', |
| 325 return_value='/fake/test/out')), ( |
| 326 mock.patch('pylib.android_commands.AndroidCommands.GetFilesChanged', |
| 327 side_effect=mockGetFilesChanged)): |
| 328 # pylint: enable=C0322 |
| 329 with self.assertOldImplCallsSequence([ |
| 330 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", |
| 331 'package:/fake/data/app/this.is.a.test.package.apk\r\n'), |
| 332 # GetFilesChanged is mocked, so its adb calls are omitted. |
| 333 ('adb -s 0123456789abcdef uninstall this.is.a.test.package', |
| 334 'Success\r\n'), |
| 335 ('adb -s 0123456789abcdef install /fake/test/app.apk', |
| 336 'Success\r\n')]): |
| 337 self.device.Install('/fake/test/app.apk', retries=0) |
| 338 |
| 339 def testInstall_differentPriorInstall_reinstall(self): |
| 340 def mockGetFilesChanged(host_path, device_path, ignore_filenames): |
| 341 return [(host_path, device_path)] |
| 342 |
| 343 # Pylint raises a false positive "operator not preceded by a space" |
| 344 # warning below. |
| 345 # pylint: disable=C0322 |
| 346 with mock.patch('os.path.isfile', return_value=True), ( |
| 347 mock.patch('pylib.utils.apk_helper.GetPackageName', |
| 348 return_value='this.is.a.test.package')), ( |
| 349 mock.patch('pylib.constants.GetOutDirectory', |
| 350 return_value='/fake/test/out')), ( |
| 351 mock.patch('pylib.android_commands.AndroidCommands.GetFilesChanged', |
| 352 side_effect=mockGetFilesChanged)): |
| 353 # pylint: enable=C0322 |
| 354 with self.assertOldImplCallsSequence([ |
| 355 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", |
| 356 'package:/fake/data/app/this.is.a.test.package.apk\r\n'), |
| 357 # GetFilesChanged is mocked, so its adb calls are omitted. |
| 358 ('adb -s 0123456789abcdef install -r /fake/test/app.apk', |
| 359 'Success\r\n')]): |
| 360 self.device.Install('/fake/test/app.apk', reinstall=True, retries=0) |
| 361 |
| 362 def testInstall_identicalPriorInstall(self): |
| 363 def mockGetFilesChanged(host_path, device_path, ignore_filenames): |
| 364 return [] |
| 365 |
| 366 with mock.patch('pylib.utils.apk_helper.GetPackageName', |
| 367 return_value='this.is.a.test.package'), ( |
| 368 mock.patch('pylib.android_commands.AndroidCommands.GetFilesChanged', |
| 369 side_effect=mockGetFilesChanged)): |
| 370 with self.assertOldImplCallsSequence([ |
| 371 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", |
| 372 'package:/fake/data/app/this.is.a.test.package.apk\r\n') |
| 373 # GetFilesChanged is mocked, so its adb calls are omitted. |
| 374 ]): |
| 375 self.device.Install('/fake/test/app.apk', retries=0) |
| 376 |
| 377 def testInstall_fails(self): |
| 378 with mock.patch('os.path.isfile', return_value=True), ( |
| 379 mock.patch('pylib.utils.apk_helper.GetPackageName', |
| 380 return_value='this.is.a.test.package')): |
| 381 with self.assertOldImplCallsSequence([ |
| 382 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", |
| 383 ''), |
| 384 ("adb -s 0123456789abcdef install /fake/test/app.apk", |
| 385 'Failure\r\n')]): |
| 386 with self.assertRaises(device_errors.CommandFailedError): |
| 387 self.device.Install('/fake/test/app.apk', retries=0) |
| 388 |
| 389 def testRunShellCommand_commandAsList(self): |
| 390 with self.assertOldImplCalls( |
| 391 "adb -s 0123456789abcdef shell 'pm list packages'", |
| 392 'pacakge:android\r\n'): |
| 393 self.device.RunShellCommand(['pm', 'list', 'packages']) |
| 394 |
| 395 def testRunShellCommand_commandAsString(self): |
| 396 with self.assertOldImplCalls( |
| 397 "adb -s 0123456789abcdef shell 'dumpsys wifi'", |
| 398 'Wi-Fi is enabled\r\n'): |
| 399 self.device.RunShellCommand('dumpsys wifi') |
| 400 |
| 401 def testRunShellCommand_withSu(self): |
| 402 with self.assertOldImplCallsSequence([ |
| 403 ("adb -s 0123456789abcdef shell 'ls /root'", 'Permission denied\r\n'), |
| 404 ("adb -s 0123456789abcdef shell 'su -c setprop service.adb.root 0'", |
| 405 '')]): |
| 406 self.device.RunShellCommand('setprop service.adb.root 0', root=True) |
| 407 |
| 408 def testRunShellCommand_withRoot(self): |
| 409 with self.assertOldImplCallsSequence([ |
| 410 ("adb -s 0123456789abcdef shell 'ls /root'", 'hello\r\nworld\r\n'), |
| 411 ("adb -s 0123456789abcdef shell 'setprop service.adb.root 0'", '')]): |
| 412 self.device.RunShellCommand('setprop service.adb.root 0', root=True) |
| 413 |
| 414 def testRunShellCommand_checkReturn_success(self): |
| 415 with self.assertOldImplCalls( |
| 416 "adb -s 0123456789abcdef shell 'echo $ANDROID_DATA; echo %$?'", |
| 417 '/data\r\n%0\r\n'): |
| 418 self.device.RunShellCommand('echo $ANDROID_DATA', check_return=True) |
| 419 |
| 420 def testRunShellCommand_checkReturn_failure(self): |
| 421 with self.assertOldImplCalls( |
| 422 "adb -s 0123456789abcdef shell 'echo $ANDROID_DATA; echo %$?'", |
| 423 '\r\n%1\r\n'): |
| 424 with self.assertRaises(device_errors.CommandFailedError): |
| 425 self.device.RunShellCommand('echo $ANDROID_DATA', check_return=True) |
210 | 426 |
211 if __name__ == '__main__': | 427 if __name__ == '__main__': |
212 unittest.main(verbosity=2) | 428 unittest.main(verbosity=2) |
213 | 429 |
OLD | NEW |