OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """ | 6 """ |
7 Unit tests for the contents of device_utils.py (mostly DeviceUtils). | 7 Unit tests for the contents of device_utils.py (mostly DeviceUtils). |
8 """ | 8 """ |
9 | 9 |
10 # pylint: disable=C0321 | 10 # pylint: disable=C0321 |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
211 return type(self).AndroidCommandsCalls(self, [(cmd, ret)], comp) | 211 return type(self).AndroidCommandsCalls(self, [(cmd, ret)], comp) |
212 | 212 |
213 def assertCallsSequence(self, cmd_ret, comp=str.__eq__): | 213 def assertCallsSequence(self, cmd_ret, comp=str.__eq__): |
214 return type(self).AndroidCommandsCalls(self, cmd_ret, comp) | 214 return type(self).AndroidCommandsCalls(self, cmd_ret, comp) |
215 | 215 |
216 def setUp(self): | 216 def setUp(self): |
217 self.device = device_utils.DeviceUtils( | 217 self.device = device_utils.DeviceUtils( |
218 '0123456789abcdef', default_timeout=1, default_retries=0) | 218 '0123456789abcdef', default_timeout=1, default_retries=0) |
219 | 219 |
220 | 220 |
221 class Args: | |
222 def __init__(self, *args, **kwargs): | |
223 self.args = args | |
224 self.kwargs = kwargs | |
225 | |
226 def __eq__(self, other): | |
227 return (self.args, self.kwargs) == (other.args, other.kwargs) | |
228 | |
229 def __repr__(self): | |
jbudorick
2014/10/17 09:04:53
- Why are you defining __repr__?
- Why is __repr__
perezju
2014/10/17 11:17:09
From python docs:
repr: For many types, this func
jbudorick
2014/10/17 15:48:00
Makes sense. I might have to start trying to write
| |
230 return '%s(%s)' % (self.__class__.__name__, str(self)) | |
231 | |
232 def __str__(self): | |
233 toks = (['%r' % v for v in self.args] + | |
234 ['%s=%r' % (k, self.kwargs[k]) for k in sorted(self.kwargs)]) | |
235 return ', '.join(toks) | |
236 | |
237 | |
238 class MockCallSequence(object): | |
239 def __init__(self, test_case, obj, method, calls): | |
240 def assert_and_return(*args, **kwargs): | |
241 received_args = Args(*args, **kwargs) | |
242 test_case.assertTrue( | |
243 self._calls, | |
244 msg=('Unexpected call\n' | |
245 ' received: %s(%s)\n' % (self._method, received_args))) | |
246 expected_args, return_value = self._calls.pop(0) | |
247 test_case.assertTrue( | |
248 received_args == expected_args, | |
249 msg=('Call does not match expected args\n' | |
250 ' received: %s(%s)\n' | |
251 ' expected: %s(%s)\n' | |
252 % (self._method, received_args, | |
253 self._method, expected_args))) | |
254 if isinstance(return_value, Exception): | |
255 raise return_value | |
256 else: | |
257 return return_value | |
258 | |
259 self._calls = list(calls) | |
260 self._test_case = test_case | |
261 self._method = method | |
262 self._patched = mock.patch.object(obj, self._method, | |
263 side_effect=assert_and_return) | |
264 | |
265 def __enter__(self): | |
266 return self._patched.__enter__() | |
267 | |
268 def __exit__(self, exc_type, exc_val, exc_tb): | |
269 self._patched.__exit__(exc_type, exc_val, exc_tb) | |
270 if exc_type is None: | |
271 missing = ''.join(' expected: %s(%s)\n' | |
272 % (self._method, expected_args) | |
273 for expected_args, _ in self._calls) | |
274 self._test_case.assertTrue( | |
275 not missing, | |
276 msg=('Expected calls not found\n' + missing)) | |
277 | |
278 | |
279 class _ShellError: | |
280 def __init__(self, output=None, return_code=1): | |
281 if output is None: | |
282 self.output = 'Permission denied\r\n' | |
283 else: | |
284 self.output = output | |
285 self.return_code = return_code | |
286 | |
287 | |
221 class DeviceUtilsNewImplTest(unittest.TestCase): | 288 class DeviceUtilsNewImplTest(unittest.TestCase): |
222 | 289 |
223 def setUp(self): | 290 def setUp(self): |
224 test_serial = '0123456789abcdef' | 291 test_serial = '0123456789abcdef' |
225 self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper) | 292 self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper) |
226 self.adb.__str__ = mock.Mock(return_value=test_serial) | 293 self.adb.__str__ = mock.Mock(return_value=test_serial) |
227 self.adb.GetDeviceSerial.return_value = test_serial | 294 self.adb.GetDeviceSerial.return_value = test_serial |
228 self.device = device_utils.DeviceUtils( | 295 self.device = device_utils.DeviceUtils( |
229 self.adb, default_timeout=1, default_retries=0) | 296 self.adb, default_timeout=1, default_retries=0) |
230 | 297 |
298 def assertShellCallSequence(self, calls): | |
jbudorick
2014/10/17 09:04:53
I'm torn on these functions.
The result is great,
perezju
2014/10/17 11:17:09
Acknowledged.
| |
299 '''Assert that we expect a sequence of calls to adb.Shell. | |
300 | |
301 Args: | |
302 calls: a sequence of (cmd, return_value) pairs, where |cmd| is the | |
303 expected shell command to run on the device (with any quoting already | |
304 applied), and |return_value| is either a string to give as mock output | |
305 or a _ShellError object to raise an AdbShellCommandFailedError. | |
306 ''' | |
307 def mk_expected_call(cmd, return_value): | |
308 expected_args = Args(cmd, expect_rc=0) | |
309 if isinstance(return_value, _ShellError): | |
310 return_value = device_errors.AdbShellCommandFailedError(cmd, | |
311 return_value.return_code, return_value.output, str(self.device)) | |
312 return (expected_args, return_value) | |
313 | |
314 expected_calls = (mk_expected_call(a, r) for a, r in calls) | |
315 return MockCallSequence(self, self.adb, 'Shell', expected_calls) | |
316 | |
317 def assertShellCall(self, cmd, return_value=''): | |
318 return self.assertShellCallSequence([(cmd, return_value)]) | |
319 | |
231 | 320 |
232 class DeviceUtilsHybridImplTest(DeviceUtilsOldImplTest): | 321 class DeviceUtilsHybridImplTest(DeviceUtilsOldImplTest): |
233 | 322 |
234 def setUp(self): | 323 def setUp(self): |
235 super(DeviceUtilsHybridImplTest, self).setUp() | 324 super(DeviceUtilsHybridImplTest, self).setUp() |
236 self.device.adb = self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper) | 325 self.device.adb = self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper) |
237 | 326 |
238 | 327 |
239 class DeviceUtilsIsOnlineTest(DeviceUtilsOldImplTest): | 328 class DeviceUtilsIsOnlineTest(DeviceUtilsOldImplTest): |
240 | 329 |
241 def testIsOnline_true(self): | 330 def testIsOnline_true(self): |
242 with self.assertCalls('adb -s 0123456789abcdef devices', | 331 with self.assertCalls('adb -s 0123456789abcdef devices', |
243 '00123456789abcdef device\r\n'): | 332 '00123456789abcdef device\r\n'): |
244 self.assertTrue(self.device.IsOnline()) | 333 self.assertTrue(self.device.IsOnline()) |
245 | 334 |
246 def testIsOnline_false(self): | 335 def testIsOnline_false(self): |
247 with self.assertCalls('adb -s 0123456789abcdef devices', '\r\n'): | 336 with self.assertCalls('adb -s 0123456789abcdef devices', '\r\n'): |
248 self.assertFalse(self.device.IsOnline()) | 337 self.assertFalse(self.device.IsOnline()) |
249 | 338 |
250 | 339 |
251 class DeviceUtilsHasRootTest(DeviceUtilsOldImplTest): | 340 class DeviceUtilsHasRootTest(DeviceUtilsNewImplTest): |
252 | 341 |
253 def testHasRoot_true(self): | 342 def testHasRoot_true(self): |
254 with self.assertCalls("adb -s 0123456789abcdef shell 'ls /root'", | 343 with self.assertShellCall('ls /root', 'foo\r\n'): |
255 'foo\r\n'): | |
256 self.assertTrue(self.device.HasRoot()) | 344 self.assertTrue(self.device.HasRoot()) |
257 | 345 |
258 def testHasRoot_false(self): | 346 def testHasRoot_false(self): |
259 with self.assertCalls("adb -s 0123456789abcdef shell 'ls /root'", | 347 with self.assertShellCall('ls /root', _ShellError()): |
260 'Permission denied\r\n'): | |
261 self.assertFalse(self.device.HasRoot()) | 348 self.assertFalse(self.device.HasRoot()) |
262 | 349 |
263 | 350 |
264 class DeviceUtilsEnableRootTest(DeviceUtilsOldImplTest): | 351 class DeviceUtilsEnableRootTest(DeviceUtilsOldImplTest): |
265 | 352 |
266 def testEnableRoot_succeeds(self): | 353 def testEnableRoot_succeeds(self): |
267 with self.assertCallsSequence([ | 354 with self.assertCallsSequence([ |
268 ('adb -s 0123456789abcdef shell getprop ro.build.type', | 355 ('adb -s 0123456789abcdef shell getprop ro.build.type', |
269 'userdebug\r\n'), | 356 'userdebug\r\n'), |
270 ('adb -s 0123456789abcdef root', 'restarting adbd as root\r\n'), | 357 ('adb -s 0123456789abcdef root', 'restarting adbd as root\r\n'), |
(...skipping 25 matching lines...) Expand all Loading... | |
296 'user\r\n'): | 383 'user\r\n'): |
297 self.assertTrue(self.device.IsUserBuild()) | 384 self.assertTrue(self.device.IsUserBuild()) |
298 | 385 |
299 def testIsUserBuild_no(self): | 386 def testIsUserBuild_no(self): |
300 with self.assertCalls( | 387 with self.assertCalls( |
301 'adb -s 0123456789abcdef shell getprop ro.build.type', | 388 'adb -s 0123456789abcdef shell getprop ro.build.type', |
302 'userdebug\r\n'): | 389 'userdebug\r\n'): |
303 self.assertFalse(self.device.IsUserBuild()) | 390 self.assertFalse(self.device.IsUserBuild()) |
304 | 391 |
305 | 392 |
306 class DeviceUtilsGetExternalStoragePathTest(DeviceUtilsOldImplTest): | 393 class DeviceUtilsGetExternalStoragePathTest(DeviceUtilsNewImplTest): |
307 | 394 |
308 def testGetExternalStoragePath_succeeds(self): | 395 def testGetExternalStoragePath_succeeds(self): |
309 fakeStoragePath = '/fake/storage/path' | 396 fakeStoragePath = '/fake/storage/path' |
310 with self.assertCalls( | 397 with self.assertShellCall('echo $EXTERNAL_STORAGE', |
311 "adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", | 398 '%s\r\n' % fakeStoragePath): |
312 '%s\r\n' % fakeStoragePath): | |
313 self.assertEquals(fakeStoragePath, | 399 self.assertEquals(fakeStoragePath, |
314 self.device.GetExternalStoragePath()) | 400 self.device.GetExternalStoragePath()) |
315 | 401 |
316 def testGetExternalStoragePath_fails(self): | 402 def testGetExternalStoragePath_fails(self): |
317 with self.assertCalls( | 403 with self.assertShellCall('echo $EXTERNAL_STORAGE', '\r\n'): |
318 "adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", '\r\n'): | |
319 with self.assertRaises(device_errors.CommandFailedError): | 404 with self.assertRaises(device_errors.CommandFailedError): |
320 self.device.GetExternalStoragePath() | 405 self.device.GetExternalStoragePath() |
321 | 406 |
322 | 407 |
323 class DeviceUtilsWaitUntilFullyBootedTest(DeviceUtilsOldImplTest): | 408 class DeviceUtilsWaitUntilFullyBootedTest(DeviceUtilsOldImplTest): |
324 | 409 |
325 def testWaitUntilFullyBooted_succeedsNoWifi(self): | 410 def testWaitUntilFullyBooted_succeedsNoWifi(self): |
326 with self.assertCallsSequence([ | 411 with self.assertCallsSequence([ |
327 # AndroidCommands.WaitForSystemBootCompleted | 412 # AndroidCommands.WaitForSystemBootCompleted |
328 ('adb -s 0123456789abcdef wait-for-device', ''), | 413 ('adb -s 0123456789abcdef wait-for-device', ''), |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
537 return_value='this.is.a.test.package')): | 622 return_value='this.is.a.test.package')): |
538 with self.assertCallsSequence([ | 623 with self.assertCallsSequence([ |
539 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", | 624 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", |
540 ''), | 625 ''), |
541 ("adb -s 0123456789abcdef install /fake/test/app.apk", | 626 ("adb -s 0123456789abcdef install /fake/test/app.apk", |
542 'Failure\r\n')]): | 627 'Failure\r\n')]): |
543 with self.assertRaises(device_errors.CommandFailedError): | 628 with self.assertRaises(device_errors.CommandFailedError): |
544 self.device.Install('/fake/test/app.apk', retries=0) | 629 self.device.Install('/fake/test/app.apk', retries=0) |
545 | 630 |
546 | 631 |
547 class DeviceUtilsRunShellCommandTest(DeviceUtilsOldImplTest): | 632 class DeviceUtilsRunShellCommandTest(DeviceUtilsNewImplTest): |
548 | |
549 def testRunShellCommand_commandAsList(self): | 633 def testRunShellCommand_commandAsList(self): |
550 with self.assertCalls( | 634 with self.assertShellCall('pm list packages'): |
551 "adb -s 0123456789abcdef shell 'pm list packages'", | |
552 'pacakge:android\r\n'): | |
553 self.device.RunShellCommand(['pm', 'list', 'packages']) | 635 self.device.RunShellCommand(['pm', 'list', 'packages']) |
554 | 636 |
637 def testRunShellCommand_commandAsListQuoted(self): | |
638 with self.assertShellCall("echo 'hello world' '$10'"): | |
639 self.device.RunShellCommand(['echo', 'hello world', '$10']) | |
640 | |
555 def testRunShellCommand_commandAsString(self): | 641 def testRunShellCommand_commandAsString(self): |
556 with self.assertCalls( | 642 with self.assertShellCall('echo "$VAR"'): |
557 "adb -s 0123456789abcdef shell 'dumpsys wifi'", | 643 self.device.RunShellCommand('echo "$VAR"') |
558 'Wi-Fi is enabled\r\n'): | 644 |
559 self.device.RunShellCommand('dumpsys wifi') | 645 def testNewRunShellImpl_withEnv(self): |
646 with self.assertShellCall('VAR=some_string echo "$VAR"'): | |
647 self.device.RunShellCommand('echo "$VAR"', env={'VAR': 'some_string'}) | |
648 | |
649 def testNewRunShellImpl_withEnvQuoted(self): | |
650 with self.assertShellCall('PATH="$PATH:/other/path" run_this'): | |
651 self.device.RunShellCommand('run_this', env={'PATH': '$PATH:/other/path'}) | |
652 | |
653 def testNewRunShellImpl_withEnv_failure(self): | |
654 with self.assertRaises(KeyError): | |
655 self.device.RunShellCommand('some_cmd', env={'INVALID NAME': 'value'}) | |
656 | |
657 def testNewRunShellImpl_withCwd(self): | |
658 with self.assertShellCall('cd /some/test/path && ls'): | |
659 self.device.RunShellCommand('ls', cwd='/some/test/path') | |
660 | |
661 def testNewRunShellImpl_withCwdQuoted(self): | |
662 with self.assertShellCall("cd '/some test/path with/spaces' && ls"): | |
663 self.device.RunShellCommand('ls', cwd='/some test/path with/spaces') | |
560 | 664 |
561 def testRunShellCommand_withSu(self): | 665 def testRunShellCommand_withSu(self): |
562 with self.assertCallsSequence([ | 666 with self.assertShellCallSequence([ |
563 ("adb -s 0123456789abcdef shell 'ls /root'", 'Permission denied\r\n'), | 667 ('ls /root', _ShellError()), |
564 ("adb -s 0123456789abcdef shell 'su -c setprop service.adb.root 0'", | 668 ('su -c setprop service.adb.root 0', '')]): |
565 '')]): | |
566 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) | 669 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) |
567 | 670 |
568 def testRunShellCommand_withRoot(self): | 671 def testRunShellCommand_withRoot(self): |
569 with self.assertCallsSequence([ | 672 with self.assertShellCallSequence([ |
570 ("adb -s 0123456789abcdef shell 'ls /root'", 'hello\r\nworld\r\n'), | 673 ('ls /root', '\r\n'), |
571 ("adb -s 0123456789abcdef shell 'setprop service.adb.root 0'", '')]): | 674 ('setprop service.adb.root 0', '')]): |
572 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) | 675 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) |
573 | 676 |
677 def testRunShellCommand_wantLines(self): | |
678 cmd = 'ls /some/path' | |
679 output = 'file1\r\nfile2\r\nfile3\r\n' | |
680 with self.assertShellCall(cmd, output): | |
681 self.assertEquals(['file1', 'file2', 'file3'], | |
682 self.device.RunShellCommand(cmd, want_lines=True)) | |
683 | |
684 def testRunShellCommand_wantString(self): | |
685 cmd = 'ls /some/path' | |
686 output = 'file1\r\nfile2\r\nfile3\r\n' | |
687 with self.assertShellCall(cmd, output): | |
688 self.assertEquals(output, | |
689 self.device.RunShellCommand(cmd, want_lines=False)) | |
690 | |
574 def testRunShellCommand_checkReturn_success(self): | 691 def testRunShellCommand_checkReturn_success(self): |
575 with self.assertCalls( | 692 cmd = 'echo $ANDROID_DATA' |
576 "adb -s 0123456789abcdef shell 'echo $ANDROID_DATA; echo %$?'", | 693 output = '/data\r\n' |
577 '/data\r\n%0\r\n'): | 694 with self.assertShellCall(cmd, output): |
578 self.device.RunShellCommand('echo $ANDROID_DATA', check_return=True) | 695 self.assertEquals([output.rstrip()], |
696 self.device.RunShellCommand(cmd, check_return=True)) | |
579 | 697 |
580 def testRunShellCommand_checkReturn_failure(self): | 698 def testRunShellCommand_checkReturn_failure(self): |
581 with self.assertCalls( | 699 cmd = 'ls /root' |
582 "adb -s 0123456789abcdef shell 'echo $ANDROID_DATA; echo %$?'", | 700 output = 'opendir failed, Permission denied\r\n' |
583 '\r\n%1\r\n'): | 701 with self.assertShellCall(cmd, _ShellError(output)): |
584 with self.assertRaises(device_errors.CommandFailedError): | 702 with self.assertRaises(device_errors.AdbShellCommandFailedError): |
585 self.device.RunShellCommand('echo $ANDROID_DATA', check_return=True) | 703 self.device.RunShellCommand(cmd, check_return=True) |
704 | |
705 def testRunShellCommand_checkReturn_disabled(self): | |
706 cmd = 'ls /root' | |
707 output = 'opendir failed, Permission denied\r\n' | |
708 with self.assertShellCall(cmd, _ShellError(output)): | |
709 self.assertEquals([output.rstrip()], | |
710 self.device.RunShellCommand(cmd, check_return=False)) | |
586 | 711 |
587 | 712 |
588 class DeviceUtilsKillAllTest(DeviceUtilsOldImplTest): | 713 class DeviceUtilsKillAllTest(DeviceUtilsNewImplTest): |
589 | 714 |
590 def testKillAll_noMatchingProcesses(self): | 715 def testKillAll_noMatchingProcesses(self): |
591 with self.assertCalls( | 716 output = 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
592 "adb -s 0123456789abcdef shell 'ps'", | 717 with self.assertShellCallSequence([('ps', output)]): |
593 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n'): | |
594 with self.assertRaises(device_errors.CommandFailedError): | 718 with self.assertRaises(device_errors.CommandFailedError): |
595 self.device.KillAll('test_process') | 719 self.device.KillAll('test_process') |
596 | 720 |
597 def testKillAll_nonblocking(self): | 721 def testKillAll_nonblocking(self): |
598 with self.assertCallsSequence([ | 722 with self.assertShellCallSequence([ |
599 ("adb -s 0123456789abcdef shell 'ps'", | 723 ('ps', 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
600 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 724 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' |
601 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' | 725 'this.is.a.test.process\r\n'), |
602 'this.is.a.test.process\r\n'), | 726 ('kill -9 1234', '')]): |
603 ("adb -s 0123456789abcdef shell 'kill -9 1234'", '')]): | |
604 self.assertEquals(1, | 727 self.assertEquals(1, |
605 self.device.KillAll('this.is.a.test.process', blocking=False)) | 728 self.device.KillAll('this.is.a.test.process', blocking=False)) |
606 | 729 |
607 def testKillAll_blocking(self): | 730 def testKillAll_blocking(self): |
608 with mock.patch('time.sleep'): | 731 with mock.patch('time.sleep'): |
609 with self.assertCallsSequence([ | 732 with self.assertShellCallSequence([ |
610 ("adb -s 0123456789abcdef shell 'ps'", | 733 ('ps', 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
611 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 734 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' |
612 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' | 735 'this.is.a.test.process\r\n'), |
613 'this.is.a.test.process\r\n'), | 736 ('kill -9 1234', ''), |
614 ("adb -s 0123456789abcdef shell 'kill -9 1234'", ''), | 737 ('ps', 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
615 ("adb -s 0123456789abcdef shell 'ps'", | 738 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' |
616 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 739 'this.is.a.test.process\r\n'), |
617 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' | 740 ('ps', 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n')]): |
618 'this.is.a.test.process\r\n'), | |
619 ("adb -s 0123456789abcdef shell 'ps'", | |
620 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n')]): | |
621 self.assertEquals(1, | 741 self.assertEquals(1, |
622 self.device.KillAll('this.is.a.test.process', blocking=True)) | 742 self.device.KillAll('this.is.a.test.process', blocking=True)) |
623 | 743 |
624 def testKillAll_root(self): | 744 def testKillAll_root(self): |
625 with self.assertCallsSequence([ | 745 with self.assertShellCallSequence([ |
626 ("adb -s 0123456789abcdef shell 'ps'", | 746 ('ps', 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
627 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 747 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' |
628 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' | 748 'this.is.a.test.process\r\n'), |
629 'this.is.a.test.process\r\n'), | 749 ('ls /root', _ShellError()), |
630 ("adb -s 0123456789abcdef shell 'ls /root'", 'Permission denied\r\n'), | 750 ('su -c kill -9 1234', '')]): |
631 ("adb -s 0123456789abcdef shell 'su -c kill -9 1234'", '')]): | |
632 self.assertEquals(1, | 751 self.assertEquals(1, |
633 self.device.KillAll('this.is.a.test.process', as_root=True)) | 752 self.device.KillAll('this.is.a.test.process', as_root=True)) |
634 | 753 |
635 def testKillAll_sigterm(self): | 754 def testKillAll_sigterm(self): |
636 with self.assertCallsSequence([ | 755 with self.assertShellCallSequence([ |
637 ("adb -s 0123456789abcdef shell 'ps'", | 756 ('ps', 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
638 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 757 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' |
639 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' | 758 'this.is.a.test.process\r\n'), |
640 'this.is.a.test.process\r\n'), | 759 ('kill -15 1234', '')]): |
641 ("adb -s 0123456789abcdef shell 'kill -15 1234'", '')]): | |
642 self.assertEquals(1, | 760 self.assertEquals(1, |
643 self.device.KillAll('this.is.a.test.process', signum=signal.SIGTERM)) | 761 self.device.KillAll('this.is.a.test.process', signum=signal.SIGTERM)) |
644 | 762 |
645 | 763 |
646 class DeviceUtilsStartActivityTest(DeviceUtilsOldImplTest): | 764 class DeviceUtilsStartActivityTest(DeviceUtilsOldImplTest): |
647 | 765 |
648 def testStartActivity_actionOnly(self): | 766 def testStartActivity_actionOnly(self): |
649 test_intent = intent.Intent(action='android.intent.action.VIEW') | 767 test_intent = intent.Intent(action='android.intent.action.VIEW') |
650 with self.assertCalls( | 768 with self.assertCalls( |
651 "adb -s 0123456789abcdef shell 'am start " | 769 "adb -s 0123456789abcdef shell 'am start " |
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
937 self.device._PushChangedFilesZipped(test_files) | 1055 self.device._PushChangedFilesZipped(test_files) |
938 | 1056 |
939 mock_zip_proc.assert_called_once_with( | 1057 mock_zip_proc.assert_called_once_with( |
940 target=device_utils.DeviceUtils._CreateDeviceZip, | 1058 target=device_utils.DeviceUtils._CreateDeviceZip, |
941 args=('/test/temp/file/tmp.zip', test_files)) | 1059 args=('/test/temp/file/tmp.zip', test_files)) |
942 self.adb.Push.assert_called_once_with( | 1060 self.adb.Push.assert_called_once_with( |
943 '/test/temp/file/tmp.zip', '/test/device/external_dir/tmp.zip') | 1061 '/test/temp/file/tmp.zip', '/test/device/external_dir/tmp.zip') |
944 self.assertEqual(2, self.device._RunShellCommandImpl.call_count) | 1062 self.assertEqual(2, self.device._RunShellCommandImpl.call_count) |
945 self.device._RunShellCommandImpl.assert_any_call( | 1063 self.device._RunShellCommandImpl.assert_any_call( |
946 ['unzip', '/test/device/external_dir/tmp.zip'], | 1064 ['unzip', '/test/device/external_dir/tmp.zip'], |
947 as_root=True, check_return=True, | 1065 as_root=True, |
948 env={'PATH': '$PATH:/data/local/tmp/bin'}) | 1066 env={'PATH': '$PATH:/data/local/tmp/bin'}) |
949 self.device._RunShellCommandImpl.assert_any_call( | 1067 self.device._RunShellCommandImpl.assert_any_call( |
950 ['rm', '/test/device/external_dir/tmp.zip']) | 1068 ['rm', '/test/device/external_dir/tmp.zip']) |
951 | 1069 |
952 def testPushChangedFilesZipped_multiple(self): | 1070 def testPushChangedFilesZipped_multiple(self): |
953 test_files = [('/test/host/path/file1', '/test/device/path/file1'), | 1071 test_files = [('/test/host/path/file1', '/test/device/path/file1'), |
954 ('/test/host/path/file2', '/test/device/path/file2')] | 1072 ('/test/host/path/file2', '/test/device/path/file2')] |
955 | 1073 |
956 self.device._GetExternalStoragePathImpl = mock.Mock( | 1074 self.device._GetExternalStoragePathImpl = mock.Mock( |
957 return_value='/test/device/external_dir') | 1075 return_value='/test/device/external_dir') |
958 self.device._IsOnlineImpl = mock.Mock(return_value=True) | 1076 self.device._IsOnlineImpl = mock.Mock(return_value=True) |
959 self.device._RunShellCommandImpl = mock.Mock() | 1077 self.device._RunShellCommandImpl = mock.Mock() |
960 mock_zip_temp = mock.mock_open() | 1078 mock_zip_temp = mock.mock_open() |
961 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip' | 1079 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip' |
962 with mock.patch('multiprocessing.Process') as mock_zip_proc, ( | 1080 with mock.patch('multiprocessing.Process') as mock_zip_proc, ( |
963 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)): | 1081 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)): |
964 self.device._PushChangedFilesZipped(test_files) | 1082 self.device._PushChangedFilesZipped(test_files) |
965 | 1083 |
966 mock_zip_proc.assert_called_once_with( | 1084 mock_zip_proc.assert_called_once_with( |
967 target=device_utils.DeviceUtils._CreateDeviceZip, | 1085 target=device_utils.DeviceUtils._CreateDeviceZip, |
968 args=('/test/temp/file/tmp.zip', test_files)) | 1086 args=('/test/temp/file/tmp.zip', test_files)) |
969 self.adb.Push.assert_called_once_with( | 1087 self.adb.Push.assert_called_once_with( |
970 '/test/temp/file/tmp.zip', '/test/device/external_dir/tmp.zip') | 1088 '/test/temp/file/tmp.zip', '/test/device/external_dir/tmp.zip') |
971 self.assertEqual(2, self.device._RunShellCommandImpl.call_count) | 1089 self.assertEqual(2, self.device._RunShellCommandImpl.call_count) |
972 self.device._RunShellCommandImpl.assert_any_call( | 1090 self.device._RunShellCommandImpl.assert_any_call( |
973 ['unzip', '/test/device/external_dir/tmp.zip'], | 1091 ['unzip', '/test/device/external_dir/tmp.zip'], |
974 as_root=True, check_return=True, | 1092 as_root=True, |
975 env={'PATH': '$PATH:/data/local/tmp/bin'}) | 1093 env={'PATH': '$PATH:/data/local/tmp/bin'}) |
976 self.device._RunShellCommandImpl.assert_any_call( | 1094 self.device._RunShellCommandImpl.assert_any_call( |
977 ['rm', '/test/device/external_dir/tmp.zip']) | 1095 ['rm', '/test/device/external_dir/tmp.zip']) |
978 | 1096 |
979 | 1097 |
980 class DeviceUtilsFileExistsTest(DeviceUtilsOldImplTest): | 1098 class DeviceUtilsFileExistsTest(DeviceUtilsOldImplTest): |
981 | 1099 |
982 def testFileExists_usingTest_fileExists(self): | 1100 def testFileExists_usingTest_fileExists(self): |
983 with self.assertCalls( | 1101 with self.assertCalls( |
984 "adb -s 0123456789abcdef shell " | 1102 "adb -s 0123456789abcdef shell " |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1195 self.device.WriteFile('/test/file/written.to.device', | 1313 self.device.WriteFile('/test/file/written.to.device', |
1196 'new test file contents', as_root=True) | 1314 'new test file contents', as_root=True) |
1197 | 1315 |
1198 def testWriteFile_asRoot_rejected(self): | 1316 def testWriteFile_asRoot_rejected(self): |
1199 self.device.old_interface._privileged_command_runner = None | 1317 self.device.old_interface._privileged_command_runner = None |
1200 self.device.old_interface._protected_file_access_method_initialized = True | 1318 self.device.old_interface._protected_file_access_method_initialized = True |
1201 with self.assertRaises(device_errors.CommandFailedError): | 1319 with self.assertRaises(device_errors.CommandFailedError): |
1202 self.device.WriteFile('/test/file/no.permissions.to.write', | 1320 self.device.WriteFile('/test/file/no.permissions.to.write', |
1203 'new test file contents', as_root=True) | 1321 'new test file contents', as_root=True) |
1204 | 1322 |
1205 class DeviceUtilsWriteTextFileTest(DeviceUtilsOldImplTest): | 1323 class DeviceUtilsWriteTextFileTest(DeviceUtilsNewImplTest): |
1206 | 1324 |
1207 def testWriteTextFileTest_basic(self): | 1325 def testWriteTextFileTest_basic(self): |
1208 with self.assertCalls( | 1326 with self.assertShellCall('echo some.string > /test/file/to.write'): |
1209 "adb -s 0123456789abcdef shell 'echo some.string" | |
1210 " > /test/file/to.write; echo %$?'", '%0\r\n'): | |
1211 self.device.WriteTextFile('/test/file/to.write', 'some.string') | 1327 self.device.WriteTextFile('/test/file/to.write', 'some.string') |
1212 | 1328 |
1213 def testWriteTextFileTest_stringWithSpaces(self): | 1329 def testWriteTextFileTest_quoted(self): |
1214 with self.assertCalls( | 1330 with self.assertShellCall( |
1215 "adb -s 0123456789abcdef shell 'echo '\\''some other string'\\''" | 1331 "echo 'some other string' > '/test/file/to write'"): |
1216 " > /test/file/to.write; echo %$?'", '%0\r\n'): | 1332 self.device.WriteTextFile('/test/file/to write', 'some other string') |
1217 self.device.WriteTextFile('/test/file/to.write', 'some other string') | |
1218 | 1333 |
1219 def testWriteTextFileTest_asRoot_withSu(self): | 1334 def testWriteTextFileTest_asRoot(self): |
1220 with self.assertCallsSequence([ | 1335 with self.assertShellCallSequence([ |
jbudorick
2014/10/17 09:04:53
This is what I mean when I say the result looks gr
| |
1221 ("adb -s 0123456789abcdef shell 'ls /root'", 'Permission denied\r\n'), | 1336 ('ls /root', _ShellError()), |
1222 ("adb -s 0123456789abcdef shell 'su -c echo some.string" | 1337 ('su -c echo string > /test/file', '')]): |
1223 " > /test/file/to.write; echo %$?'", '%0\r\n')]): | 1338 self.device.WriteTextFile('/test/file', 'string', as_root=True) |
1224 self.device.WriteTextFile('/test/file/to.write', 'some.string', | |
1225 as_root=True) | |
1226 | |
1227 def testWriteTextFileTest_asRoot_withRoot(self): | |
1228 with self.assertCallsSequence([ | |
1229 ("adb -s 0123456789abcdef shell 'ls /root'", 'hello\r\nworld\r\n'), | |
1230 ("adb -s 0123456789abcdef shell 'echo some.string" | |
1231 " > /test/file/to.write; echo %$?'", '%0\r\n')]): | |
1232 self.device.WriteTextFile('/test/file/to.write', 'some.string', | |
1233 as_root=True) | |
1234 | |
1235 def testWriteTextFileTest_asRoot_rejected(self): | |
1236 with self.assertCallsSequence([ | |
1237 ("adb -s 0123456789abcdef shell 'ls /root'", 'Permission denied\r\n'), | |
1238 ("adb -s 0123456789abcdef shell 'su -c echo some.string" | |
1239 " > /test/file/to.write; echo %$?'", '%1\r\n')]): | |
1240 with self.assertRaises(device_errors.CommandFailedError): | |
1241 self.device.WriteTextFile('/test/file/to.write', 'some.string', | |
1242 as_root=True) | |
1243 | 1339 |
1244 class DeviceUtilsLsTest(DeviceUtilsOldImplTest): | 1340 class DeviceUtilsLsTest(DeviceUtilsOldImplTest): |
1245 | 1341 |
1246 def testLs_nothing(self): | 1342 def testLs_nothing(self): |
1247 with self.assertCallsSequence([ | 1343 with self.assertCallsSequence([ |
1248 ("adb -s 0123456789abcdef shell 'ls -lR /this/file/does.not.exist'", | 1344 ("adb -s 0123456789abcdef shell 'ls -lR /this/file/does.not.exist'", |
1249 '/this/file/does.not.exist: No such file or directory\r\n'), | 1345 '/this/file/does.not.exist: No such file or directory\r\n'), |
1250 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]): | 1346 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]): |
1251 self.assertEqual({}, self.device.Ls('/this/file/does.not.exist')) | 1347 self.assertEqual({}, self.device.Ls('/this/file/does.not.exist')) |
1252 | 1348 |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1389 class DeviceUtilsSetPropTest(DeviceUtilsOldImplTest): | 1485 class DeviceUtilsSetPropTest(DeviceUtilsOldImplTest): |
1390 | 1486 |
1391 def testSetProp(self): | 1487 def testSetProp(self): |
1392 with self.assertCalls( | 1488 with self.assertCalls( |
1393 'adb -s 0123456789abcdef shell ' | 1489 'adb -s 0123456789abcdef shell ' |
1394 'setprop this.is.a.test.property "test_property_value"', | 1490 'setprop this.is.a.test.property "test_property_value"', |
1395 ''): | 1491 ''): |
1396 self.device.SetProp('this.is.a.test.property', 'test_property_value') | 1492 self.device.SetProp('this.is.a.test.property', 'test_property_value') |
1397 | 1493 |
1398 | 1494 |
1399 class DeviceUtilsGetPidsTest(DeviceUtilsOldImplTest): | 1495 class DeviceUtilsGetPidsTest(DeviceUtilsNewImplTest): |
1400 | 1496 |
1401 def testGetPids_noMatches(self): | 1497 def testGetPids_noMatches(self): |
1402 with self.assertCalls( | 1498 with self.assertShellCall( |
1403 "adb -s 0123456789abcdef shell 'ps'", | 1499 'ps', |
1404 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 1500 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
1405 'user 1000 100 1024 1024 ffffffff 00000000 no.match\r\n'): | 1501 'user 1000 100 1024 1024 ffffffff 00000000 no.match\r\n'): |
1406 self.assertEqual({}, self.device.GetPids('does.not.match')) | 1502 self.assertEqual({}, self.device.GetPids('does.not.match')) |
1407 | 1503 |
1408 def testGetPids_oneMatch(self): | 1504 def testGetPids_oneMatch(self): |
1409 with self.assertCalls( | 1505 with self.assertShellCall( |
1410 "adb -s 0123456789abcdef shell 'ps'", | 1506 'ps', |
1411 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 1507 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
1412 'user 1000 100 1024 1024 ffffffff 00000000 not.a.match\r\n' | 1508 'user 1000 100 1024 1024 ffffffff 00000000 not.a.match\r\n' |
1413 'user 1001 100 1024 1024 ffffffff 00000000 one.match\r\n'): | 1509 'user 1001 100 1024 1024 ffffffff 00000000 one.match\r\n'): |
1414 self.assertEqual({'one.match': '1001'}, self.device.GetPids('one.match')) | 1510 self.assertEqual({'one.match': '1001'}, self.device.GetPids('one.match')) |
1415 | 1511 |
1416 def testGetPids_mutlipleMatches(self): | 1512 def testGetPids_mutlipleMatches(self): |
1417 with self.assertCalls( | 1513 with self.assertShellCall( |
1418 "adb -s 0123456789abcdef shell 'ps'", | 1514 'ps', |
1419 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 1515 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
1420 'user 1000 100 1024 1024 ffffffff 00000000 not\r\n' | 1516 'user 1000 100 1024 1024 ffffffff 00000000 not\r\n' |
1421 'user 1001 100 1024 1024 ffffffff 00000000 one.match\r\n' | 1517 'user 1001 100 1024 1024 ffffffff 00000000 one.match\r\n' |
1422 'user 1002 100 1024 1024 ffffffff 00000000 two.match\r\n' | 1518 'user 1002 100 1024 1024 ffffffff 00000000 two.match\r\n' |
1423 'user 1003 100 1024 1024 ffffffff 00000000 three.match\r\n'): | 1519 'user 1003 100 1024 1024 ffffffff 00000000 three.match\r\n'): |
1424 self.assertEqual( | 1520 self.assertEqual( |
1425 {'one.match': '1001', 'two.match': '1002', 'three.match': '1003'}, | 1521 {'one.match': '1001', 'two.match': '1002', 'three.match': '1003'}, |
1426 self.device.GetPids('match')) | 1522 self.device.GetPids('match')) |
1427 | 1523 |
1428 def testGetPids_exactMatch(self): | 1524 def testGetPids_exactMatch(self): |
1429 with self.assertCalls( | 1525 with self.assertShellCall( |
1430 "adb -s 0123456789abcdef shell 'ps'", | 1526 'ps', |
1431 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 1527 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
1432 'user 1000 100 1024 1024 ffffffff 00000000 not.exact.match\r\n' | 1528 'user 1000 100 1024 1024 ffffffff 00000000 not.exact.match\r\n' |
1433 'user 1234 100 1024 1024 ffffffff 00000000 exact.match\r\n'): | 1529 'user 1234 100 1024 1024 ffffffff 00000000 exact.match\r\n'): |
1434 self.assertEqual( | 1530 self.assertEqual( |
1435 {'not.exact.match': '1000', 'exact.match': '1234'}, | 1531 {'not.exact.match': '1000', 'exact.match': '1234'}, |
1436 self.device.GetPids('exact.match')) | 1532 self.device.GetPids('exact.match')) |
1437 | 1533 |
1438 | 1534 |
1439 class DeviceUtilsTakeScreenshotTest(DeviceUtilsOldImplTest): | 1535 class DeviceUtilsTakeScreenshotTest(DeviceUtilsOldImplTest): |
1440 | 1536 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1526 self.device = device_utils.DeviceUtils(None) | 1622 self.device = device_utils.DeviceUtils(None) |
1527 with self.assertCalls('adb get-serialno', 'unknown'), ( | 1623 with self.assertCalls('adb get-serialno', 'unknown'), ( |
1528 self.assertRaises(device_errors.NoDevicesError)): | 1624 self.assertRaises(device_errors.NoDevicesError)): |
1529 str(self.device) | 1625 str(self.device) |
1530 | 1626 |
1531 | 1627 |
1532 if __name__ == '__main__': | 1628 if __name__ == '__main__': |
1533 logging.getLogger().setLevel(logging.DEBUG) | 1629 logging.getLogger().setLevel(logging.DEBUG) |
1534 unittest.main(verbosity=2) | 1630 unittest.main(verbosity=2) |
1535 | 1631 |
OLD | NEW |