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 |
frankf
2014/06/23 21:41:50
can you file a bug to add pylib unit tests to the
jbudorick
2014/06/24 00:04:17
Done. May also consider running them in presubmit.
| |
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 self.mocked = device_utils.DeviceUtils( |
frankf
2014/06/23 21:41:50
where's this used?
frankf
2014/06/23 21:41:50
mocked -> mock_device
jbudorick
2014/06/24 00:04:17
It isn't any more. Removed.
| |
140 if adb_root_prop == '1': | 117 mock.Mock(android_commands.AndroidCommands), |
141 a.Shell('setprop service.adb.root 0') | 118 default_timeout=1, |
142 | 119 default_retries=0) |
143 # Make sure that adbd is running without root by restarting it | 120 |
144 ps_out = a.Shell('ps') | 121 def tearDown(self): |
145 adbd_pids = [] | 122 self.mocked.old_interface.reset_mock() |
146 for line in ps_out.splitlines(): | 123 |
147 if 'adbd' in line: | 124 def testIsOnline_true(self): |
148 pid = line.split()[1] | 125 with self.assertOldImplCalls('adb -s 0123456789abcdef get-state', |
149 adbd_pids.append(pid) | 126 'device\r\n'): |
150 for pid in adbd_pids: | 127 self.assertTrue(self.device.IsOnline()) |
151 a.Shell('kill %s' % str(pid)) | 128 |
152 a.WaitForDevice() | 129 def testIsOnline_false(self): |
153 | 130 with self.assertOldImplCalls('adb -s 0123456789abcdef get-state', '\r\n'): |
154 self.assertFalse(d.HasRoot()) | 131 self.assertFalse(self.device.IsOnline()) |
155 d.EnableRoot() | 132 |
156 self.assertTrue(d.HasRoot()) | 133 def testHasRoot_true(self): |
157 else: | 134 with self.assertOldImplCalls("adb -s 0123456789abcdef shell 'ls /root'", |
158 self.assertFalse(d.HasRoot()) | 135 'foo\r\n'): |
136 self.assertTrue(self.device.HasRoot()) | |
137 | |
138 def testHasRoot_false(self): | |
139 with self.assertOldImplCalls("adb -s 0123456789abcdef shell 'ls /root'", | |
140 'Permission denied\r\n'): | |
141 self.assertFalse(self.device.HasRoot()) | |
142 | |
143 def testEnableRoot_succeeds(self): | |
144 with self.assertOldImplCallsSequence([ | |
145 ('adb -s 0123456789abcdef shell getprop ro.build.type', | |
146 'userdebug\r\n'), | |
147 ('adb -s 0123456789abcdef root', 'restarting adbd as root\r\n'), | |
148 ('adb -s 0123456789abcdef wait-for-device', ''), | |
149 ('adb -s 0123456789abcdef wait-for-device', '')]): | |
150 self.device.EnableRoot() | |
151 | |
152 def testEnableRoot_userBuild(self): | |
153 with self.assertOldImplCallsSequence([ | |
154 ('adb -s 0123456789abcdef shell getprop ro.build.type', 'user\r\n')]): | |
155 with self.assertRaises(device_errors.CommandFailedError): | |
156 self.device.EnableRoot() | |
157 | |
158 def testEnableRoot_rootFails(self): | |
159 with self.assertOldImplCallsSequence([ | |
160 ('adb -s 0123456789abcdef shell getprop ro.build.type', | |
161 'userdebug\r\n'), | |
162 ('adb -s 0123456789abcdef root', 'no\r\n'), | |
163 ('adb -s 0123456789abcdef wait-for-device', '')]): | |
164 with self.assertRaises(device_errors.CommandFailedError): | |
165 self.device.EnableRoot() | |
166 | |
167 def testGetExternalStoragePath_succeeds(self): | |
168 fakeStoragePath = '/fake/storage/path' | |
169 with self.assertOldImplCalls( | |
170 "adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", | |
171 '%s\r\n' % fakeStoragePath): | |
172 self.assertEquals(fakeStoragePath, | |
173 self.device.GetExternalStoragePath()) | |
174 | |
175 def testGetExternalStoragePath_fails(self): | |
176 with self.assertOldImplCalls( | |
177 "adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", '\r\n'): | |
178 with self.assertRaises(device_errors.CommandFailedError): | |
179 self.device.GetExternalStoragePath() | |
180 | |
181 def testWaitUntilFullyBooted_succeedsNoWifi(self): | |
182 with self.assertOldImplCallsSequence([ | |
183 # AndroidCommands.WaitForSystemBootCompleted | |
184 ('adb -s 0123456789abcdef wait-for-device', ''), | |
185 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', '1\r\n'), | |
186 # AndroidCommands.WaitForDevicePm | |
187 ('adb -s 0123456789abcdef wait-for-device', ''), | |
188 ('adb -s 0123456789abcdef shell pm path android', | |
189 'package:this.is.a.test.package'), | |
190 # AndroidCommands.WaitForSdCardReady | |
191 ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", | |
192 '/fake/storage/path'), | |
193 ("adb -s 0123456789abcdef shell 'ls /fake/storage/path'", | |
194 'nothing\r\n') | |
195 ]): | |
196 self.device.WaitUntilFullyBooted(wifi=False) | |
197 | |
198 def testWaitUntilFullyBooted_succeedsWithWifi(self): | |
199 with self.assertOldImplCallsSequence([ | |
200 # AndroidCommands.WaitForSystemBootCompleted | |
201 ('adb -s 0123456789abcdef wait-for-device', ''), | |
202 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', '1\r\n'), | |
203 # AndroidCommands.WaitForDevicePm | |
204 ('adb -s 0123456789abcdef wait-for-device', ''), | |
205 ('adb -s 0123456789abcdef shell pm path android', | |
206 'package:this.is.a.test.package'), | |
207 # AndroidCommands.WaitForSdCardReady | |
208 ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", | |
209 '/fake/storage/path'), | |
210 ("adb -s 0123456789abcdef shell 'ls /fake/storage/path'", | |
211 'nothing\r\n'), | |
212 # wait for wifi | |
213 ("adb -s 0123456789abcdef shell 'dumpsys wifi'", 'Wi-Fi is enabled')]): | |
214 self.device.WaitUntilFullyBooted(wifi=True) | |
215 | |
216 def testWaitUntilFullyBooted_bootFails(self): | |
217 with mock.patch('time.sleep'): | |
218 with self.assertOldImplCallsSequence([ | |
219 # AndroidCommands.WaitForSystemBootCompleted | |
220 ('adb -s 0123456789abcdef wait-for-device', ''), | |
221 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', | |
222 '0\r\n')]): | |
223 with self.assertRaises(device_errors.CommandTimeoutError): | |
224 self.device.WaitUntilFullyBooted(wifi=False) | |
225 | |
226 def testWaitUntilFullyBooted_devicePmFails(self): | |
227 with mock.patch('time.sleep'): | |
228 with self.assertOldImplCallsSequence([ | |
229 # AndroidCommands.WaitForSystemBootCompleted | |
230 ('adb -s 0123456789abcdef wait-for-device', ''), | |
231 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', | |
232 '1\r\n')] | |
233 # AndroidCommands.WaitForDevicePm | |
234 + 3 * ([('adb -s 0123456789abcdef wait-for-device', '')] | |
235 + 24 * [('adb -s 0123456789abcdef shell pm path android', '\r\n')] | |
236 + [("adb -s 0123456789abcdef shell 'stop'", '\r\n'), | |
237 ("adb -s 0123456789abcdef shell 'start'", '\r\n')])): | |
238 with self.assertRaises(device_errors.CommandTimeoutError): | |
239 self.device.WaitUntilFullyBooted(wifi=False) | |
240 | |
241 def testWaitUntilFullyBooted_sdCardReadyFails_noPath(self): | |
242 with mock.patch('time.sleep'): | |
243 with self.assertOldImplCallsSequence([ | |
244 # AndroidCommands.WaitForSystemBootCompleted | |
245 ('adb -s 0123456789abcdef wait-for-device', ''), | |
246 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', | |
247 '1\r\n'), | |
248 # AndroidCommands.WaitForDevicePm | |
249 ('adb -s 0123456789abcdef wait-for-device', ''), | |
250 ('adb -s 0123456789abcdef shell pm path android', | |
251 'package:this.is.a.test.package'), | |
252 ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", '\r\n')]): | |
159 with self.assertRaises(device_errors.CommandFailedError): | 253 with self.assertRaises(device_errors.CommandFailedError): |
160 d.EnableRoot() | 254 self.device.WaitUntilFullyBooted(wifi=False) |
161 else: | 255 |
162 self.assertTrue(d.HasRoot()) | 256 def testWaitUntilFullyBooted_sdCardReadyFails_emptyPath(self): |
163 d.EnableRoot() | 257 with mock.patch('time.sleep'): |
164 self.assertTrue(d.HasRoot()) | 258 with self.assertOldImplCallsSequence([ |
165 | 259 # AndroidCommands.WaitForSystemBootCompleted |
166 @TestRequiresDevice | 260 ('adb -s 0123456789abcdef wait-for-device', ''), |
167 def testGetExternalStorage(self): | 261 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', |
168 a = self._getTestAdbWrapper() | 262 '1\r\n'), |
169 d = device_utils.DeviceUtils(a) | 263 # AndroidCommands.WaitForDevicePm |
170 | 264 ('adb -s 0123456789abcdef wait-for-device', ''), |
171 actual_external_storage = a.Shell('echo $EXTERNAL_STORAGE').splitlines()[0] | 265 ('adb -s 0123456789abcdef shell pm path android', |
172 if actual_external_storage and len(actual_external_storage) != 0: | 266 'package:this.is.a.test.package'), |
173 self.assertEquals(actual_external_storage, d.GetExternalStoragePath()) | 267 ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", |
174 | 268 '/fake/storage/path\r\n'), |
175 @TestRequiresDevice | 269 ("adb -s 0123456789abcdef shell 'ls /fake/storage/path'", '')]): |
176 def testWaitUntilFullyBooted(self): | 270 with self.assertRaises(device_errors.CommandTimeoutError): |
177 a = self._getTestAdbWrapper() | 271 self.device.WaitUntilFullyBooted(wifi=False) |
178 d = device_utils.DeviceUtils(a) | 272 |
179 | 273 def testReboot_nonBlocking(self): |
180 a.Reboot() | 274 with mock.patch('time.sleep'): |
181 while a.GetState() == 'device': | 275 with self.assertOldImplCallsSequence([ |
182 time.sleep(0.1) | 276 ('adb -s 0123456789abcdef reboot', ''), |
183 d.WaitUntilFullyBooted(wifi=True) | 277 ('adb -s 0123456789abcdef get-state', 'unknown\r\n'), |
184 self.assertEquals( | 278 ('adb -s 0123456789abcdef wait-for-device', ''), |
185 '1', a.Shell('getprop sys.boot_completed').splitlines()[0]) | 279 ('adb -s 0123456789abcdef shell pm path android', |
186 self.assertTrue( | 280 'package:this.is.a.test.package'), |
187 a.Shell('pm path android').splitlines()[0].startswith('package:')) | 281 ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", |
188 self.assertTrue(a.Shell('ls $EXTERNAL_STORAGE')) | 282 '/fake/storage/path\r\n'), |
189 self.assertTrue( | 283 ("adb -s 0123456789abcdef shell 'ls /fake/storage/path'", |
190 'Wi-Fi is enabled' in a.Shell('dumpsys wifi').splitlines()) | 284 'nothing\r\n')]): |
191 | 285 self.device.Reboot(block=False) |
192 @TestRequiresDevice | 286 |
193 def testBlockingReboot(self): | 287 def testReboot_blocking(self): |
194 a = self._getTestAdbWrapper() | 288 with mock.patch('time.sleep'): |
195 d = device_utils.DeviceUtils(a) | 289 with self.assertOldImplCallsSequence([ |
196 | 290 ('adb -s 0123456789abcdef reboot', ''), |
197 old_boot_time = a.Shell('getprop ro.runtime.firstboot').strip() | 291 ('adb -s 0123456789abcdef get-state', 'unknown\r\n'), |
198 if old_boot_time and len(old_boot_time): | 292 ('adb -s 0123456789abcdef wait-for-device', ''), |
199 d.Reboot(block=True, timeout=120) | 293 ('adb -s 0123456789abcdef shell pm path android', |
200 self.assertNotEquals(old_boot_time, | 294 'package:this.is.a.test.package'), |
201 a.Shell('getprop ro.runtime.firstboot').strip()) | 295 ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", |
202 self.assertEquals( | 296 '/fake/storage/path\r\n'), |
203 '1', a.Shell('getprop sys.boot_completed').splitlines()[0]) | 297 ("adb -s 0123456789abcdef shell 'ls /fake/storage/path'", |
204 self.assertTrue( | 298 'nothing\r\n'), |
205 a.Shell('pm path android').splitlines()[0].startswith('package:')) | 299 ('adb -s 0123456789abcdef wait-for-device', ''), |
206 self.assertTrue(a.Shell('ls $EXTERNAL_STORAGE')) | 300 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', |
207 else: | 301 '1\r\n'), |
208 self.skipTest("No 'ro.runtime.firstboot' property on %s." % str(a)) | 302 ('adb -s 0123456789abcdef wait-for-device', ''), |
209 | 303 ('adb -s 0123456789abcdef shell pm path android', |
304 'package:this.is.a.test.package'), | |
305 ("adb -s 0123456789abcdef shell 'ls /fake/storage/path'", | |
306 'nothing\r\n')]): | |
307 self.device.Reboot(block=True) | |
308 | |
309 def testInstall_noPriorInstall(self): | |
310 with mock.patch('os.path.isfile', return_value=True), ( | |
311 mock.patch('pylib.utils.apk_helper.GetPackageName', | |
312 return_value='this.is.a.test.package')): | |
313 with self.assertOldImplCallsSequence([ | |
314 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", | |
315 ''), | |
316 ("adb -s 0123456789abcdef install /fake/test/app.apk", | |
317 'Success\r\n')]): | |
318 self.device.Install('/fake/test/app.apk', retries=0) | |
319 | |
320 def testInstall_differentPriorInstall(self): | |
321 def mockGetFilesChanged(host_path, device_path, ignore_filenames): | |
322 return [(host_path, device_path)] | |
323 | |
324 # Pylint raises a false positive "operator not preceded by a space" | |
325 # warning below. | |
326 # pylint: disable=C0322 | |
327 with mock.patch('os.path.isfile', return_value=True), ( | |
328 mock.patch('os.path.exists', return_value=True)), ( | |
329 mock.patch('pylib.utils.apk_helper.GetPackageName', | |
330 return_value='this.is.a.test.package')), ( | |
331 mock.patch('pylib.constants.GetOutDirectory', | |
332 return_value='/fake/test/out')), ( | |
333 mock.patch('pylib.android_commands.AndroidCommands.GetFilesChanged', | |
334 side_effect=mockGetFilesChanged)): | |
335 # pylint: enable=C0322 | |
336 with self.assertOldImplCallsSequence([ | |
337 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", | |
338 'package:/fake/data/app/this.is.a.test.package.apk\r\n'), | |
339 # GetFilesChanged is mocked, so its adb calls are omitted. | |
340 ('adb -s 0123456789abcdef uninstall this.is.a.test.package', | |
341 'Success\r\n'), | |
342 ('adb -s 0123456789abcdef install /fake/test/app.apk', | |
343 'Success\r\n')]): | |
344 self.device.Install('/fake/test/app.apk', retries=0) | |
345 | |
346 def testInstall_differentPriorInstall_reinstall(self): | |
347 def mockGetFilesChanged(host_path, device_path, ignore_filenames): | |
348 return [(host_path, device_path)] | |
349 | |
350 # Pylint raises a false positive "operator not preceded by a space" | |
351 # warning below. | |
352 # pylint: disable=C0322 | |
353 with mock.patch('os.path.isfile', return_value=True), ( | |
frankf
2014/06/23 21:41:50
FYI (no action required): this is called monkey-pa
| |
354 mock.patch('pylib.utils.apk_helper.GetPackageName', | |
355 return_value='this.is.a.test.package')), ( | |
356 mock.patch('pylib.constants.GetOutDirectory', | |
357 return_value='/fake/test/out')), ( | |
358 mock.patch('pylib.android_commands.AndroidCommands.GetFilesChanged', | |
359 side_effect=mockGetFilesChanged)): | |
360 # pylint: enable=C0322 | |
361 with self.assertOldImplCallsSequence([ | |
362 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", | |
363 'package:/fake/data/app/this.is.a.test.package.apk\r\n'), | |
364 # GetFilesChanged is mocked, so its adb calls are omitted. | |
365 ('adb -s 0123456789abcdef install -r /fake/test/app.apk', | |
366 'Success\r\n')]): | |
367 self.device.Install('/fake/test/app.apk', reinstall=True, retries=0) | |
368 | |
369 def testInstall_identicalPriorInstall(self): | |
370 def mockGetFilesChanged(host_path, device_path, ignore_filenames): | |
371 return [] | |
372 | |
373 with mock.patch('pylib.utils.apk_helper.GetPackageName', | |
374 return_value='this.is.a.test.package'), ( | |
375 mock.patch('pylib.android_commands.AndroidCommands.GetFilesChanged', | |
376 side_effect=mockGetFilesChanged)): | |
377 with self.assertOldImplCallsSequence([ | |
378 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", | |
379 'package:/fake/data/app/this.is.a.test.package.apk\r\n') | |
380 # GetFilesChanged is mocked, so its adb calls are omitted. | |
381 ]): | |
382 self.device.Install('/fake/test/app.apk', retries=0) | |
383 | |
384 def testInstall_fails(self): | |
385 with mock.patch('os.path.isfile', return_value=True), ( | |
386 mock.patch('pylib.utils.apk_helper.GetPackageName', | |
387 return_value='this.is.a.test.package')): | |
388 with self.assertOldImplCallsSequence([ | |
389 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", | |
390 ''), | |
391 ("adb -s 0123456789abcdef install /fake/test/app.apk", | |
392 'Failure\r\n')]): | |
393 with self.assertRaises(device_errors.CommandFailedError): | |
394 self.device.Install('/fake/test/app.apk', retries=0) | |
395 | |
396 def testRunShellCommand_commandAsList(self): | |
397 with self.assertOldImplCalls( | |
398 "adb -s 0123456789abcdef shell 'pm list packages'", | |
399 'pacakge:android\r\n'): | |
400 self.device.RunShellCommand(['pm', 'list', 'packages']) | |
401 | |
402 def testRunShellCommand_commandAsString(self): | |
403 with self.assertOldImplCalls( | |
404 "adb -s 0123456789abcdef shell 'dumpsys wifi'", | |
405 'Wi-Fi is enabled\r\n'): | |
406 self.device.RunShellCommand('dumpsys wifi') | |
407 | |
408 def testRunShellCommand_withSu(self): | |
409 with self.assertOldImplCallsSequence([ | |
410 ("adb -s 0123456789abcdef shell 'ls /root'", 'Permission denied\r\n'), | |
411 ("adb -s 0123456789abcdef shell 'su -c setprop service.adb.root 0'", | |
412 '')]): | |
413 self.device.RunShellCommand('setprop service.adb.root 0', root=True) | |
414 | |
415 def testRunShellCommand_withRoot(self): | |
416 with self.assertOldImplCallsSequence([ | |
417 ("adb -s 0123456789abcdef shell 'ls /root'", 'hello\r\nworld\r\n'), | |
418 ("adb -s 0123456789abcdef shell 'setprop service.adb.root 0'", '')]): | |
419 self.device.RunShellCommand('setprop service.adb.root 0', root=True) | |
420 | |
421 def testRunShellCommand_checkReturn_success(self): | |
422 with self.assertOldImplCalls( | |
423 "adb -s 0123456789abcdef shell 'echo $ANDROID_DATA; echo %$?'", | |
424 '/data\r\n%0\r\n'): | |
425 self.device.RunShellCommand('echo $ANDROID_DATA', check_return=True) | |
426 | |
427 def testRunShellCommand_checkReturn_failure(self): | |
428 with self.assertOldImplCalls( | |
429 "adb -s 0123456789abcdef shell 'echo $ANDROID_DATA; echo %$?'", | |
430 '\r\n%1\r\n'): | |
431 with self.assertRaises(device_errors.CommandFailedError): | |
432 self.device.RunShellCommand('echo $ANDROID_DATA', check_return=True) | |
210 | 433 |
211 if __name__ == '__main__': | 434 if __name__ == '__main__': |
212 unittest.main(verbosity=2) | 435 unittest.main(verbosity=2) |
213 | 436 |
OLD | NEW |