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