| 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): | |
| 230 return '%s(%s)' % (type(self).__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 | |
| 288 class DeviceUtilsNewImplTest(unittest.TestCase): | 221 class DeviceUtilsNewImplTest(unittest.TestCase): |
| 289 | 222 |
| 290 def setUp(self): | 223 def setUp(self): |
| 291 test_serial = '0123456789abcdef' | 224 test_serial = '0123456789abcdef' |
| 292 self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper) | 225 self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper) |
| 293 self.adb.__str__ = mock.Mock(return_value=test_serial) | 226 self.adb.__str__ = mock.Mock(return_value=test_serial) |
| 294 self.adb.GetDeviceSerial.return_value = test_serial | 227 self.adb.GetDeviceSerial.return_value = test_serial |
| 295 self.device = device_utils.DeviceUtils( | 228 self.device = device_utils.DeviceUtils( |
| 296 self.adb, default_timeout=1, default_retries=0) | 229 self.adb, default_timeout=1, default_retries=0) |
| 297 | 230 |
| 298 def assertShellCallSequence(self, calls): | |
| 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 | |
| 320 | 231 |
| 321 class DeviceUtilsHybridImplTest(DeviceUtilsOldImplTest): | 232 class DeviceUtilsHybridImplTest(DeviceUtilsOldImplTest): |
| 322 | 233 |
| 323 def setUp(self): | 234 def setUp(self): |
| 324 super(DeviceUtilsHybridImplTest, self).setUp() | 235 super(DeviceUtilsHybridImplTest, self).setUp() |
| 325 self.device.adb = self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper) | 236 self.device.adb = self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper) |
| 326 | 237 |
| 327 | 238 |
| 328 class DeviceUtilsIsOnlineTest(DeviceUtilsOldImplTest): | 239 class DeviceUtilsIsOnlineTest(DeviceUtilsOldImplTest): |
| 329 | 240 |
| 330 def testIsOnline_true(self): | 241 def testIsOnline_true(self): |
| 331 with self.assertCalls('adb -s 0123456789abcdef devices', | 242 with self.assertCalls('adb -s 0123456789abcdef devices', |
| 332 '00123456789abcdef device\r\n'): | 243 '00123456789abcdef device\r\n'): |
| 333 self.assertTrue(self.device.IsOnline()) | 244 self.assertTrue(self.device.IsOnline()) |
| 334 | 245 |
| 335 def testIsOnline_false(self): | 246 def testIsOnline_false(self): |
| 336 with self.assertCalls('adb -s 0123456789abcdef devices', '\r\n'): | 247 with self.assertCalls('adb -s 0123456789abcdef devices', '\r\n'): |
| 337 self.assertFalse(self.device.IsOnline()) | 248 self.assertFalse(self.device.IsOnline()) |
| 338 | 249 |
| 339 | 250 |
| 340 class DeviceUtilsHasRootTest(DeviceUtilsNewImplTest): | 251 class DeviceUtilsHasRootTest(DeviceUtilsOldImplTest): |
| 341 | 252 |
| 342 def testHasRoot_true(self): | 253 def testHasRoot_true(self): |
| 343 with self.assertShellCall('ls /root', 'foo\r\n'): | 254 with self.assertCalls("adb -s 0123456789abcdef shell 'ls /root'", |
| 255 'foo\r\n'): |
| 344 self.assertTrue(self.device.HasRoot()) | 256 self.assertTrue(self.device.HasRoot()) |
| 345 | 257 |
| 346 def testHasRoot_false(self): | 258 def testHasRoot_false(self): |
| 347 with self.assertShellCall('ls /root', _ShellError()): | 259 with self.assertCalls("adb -s 0123456789abcdef shell 'ls /root'", |
| 260 'Permission denied\r\n'): |
| 348 self.assertFalse(self.device.HasRoot()) | 261 self.assertFalse(self.device.HasRoot()) |
| 349 | 262 |
| 350 | 263 |
| 351 class DeviceUtilsEnableRootTest(DeviceUtilsOldImplTest): | 264 class DeviceUtilsEnableRootTest(DeviceUtilsOldImplTest): |
| 352 | 265 |
| 353 def testEnableRoot_succeeds(self): | 266 def testEnableRoot_succeeds(self): |
| 354 with self.assertCallsSequence([ | 267 with self.assertCallsSequence([ |
| 355 ('adb -s 0123456789abcdef shell getprop ro.build.type', | 268 ('adb -s 0123456789abcdef shell getprop ro.build.type', |
| 356 'userdebug\r\n'), | 269 'userdebug\r\n'), |
| 357 ('adb -s 0123456789abcdef root', 'restarting adbd as root\r\n'), | 270 ('adb -s 0123456789abcdef root', 'restarting adbd as root\r\n'), |
| (...skipping 25 matching lines...) Expand all Loading... |
| 383 'user\r\n'): | 296 'user\r\n'): |
| 384 self.assertTrue(self.device.IsUserBuild()) | 297 self.assertTrue(self.device.IsUserBuild()) |
| 385 | 298 |
| 386 def testIsUserBuild_no(self): | 299 def testIsUserBuild_no(self): |
| 387 with self.assertCalls( | 300 with self.assertCalls( |
| 388 'adb -s 0123456789abcdef shell getprop ro.build.type', | 301 'adb -s 0123456789abcdef shell getprop ro.build.type', |
| 389 'userdebug\r\n'): | 302 'userdebug\r\n'): |
| 390 self.assertFalse(self.device.IsUserBuild()) | 303 self.assertFalse(self.device.IsUserBuild()) |
| 391 | 304 |
| 392 | 305 |
| 393 class DeviceUtilsGetExternalStoragePathTest(DeviceUtilsNewImplTest): | 306 class DeviceUtilsGetExternalStoragePathTest(DeviceUtilsOldImplTest): |
| 394 | 307 |
| 395 def testGetExternalStoragePath_succeeds(self): | 308 def testGetExternalStoragePath_succeeds(self): |
| 396 fakeStoragePath = '/fake/storage/path' | 309 fakeStoragePath = '/fake/storage/path' |
| 397 with self.assertShellCall('echo $EXTERNAL_STORAGE', | 310 with self.assertCalls( |
| 398 '%s\r\n' % fakeStoragePath): | 311 "adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", |
| 312 '%s\r\n' % fakeStoragePath): |
| 399 self.assertEquals(fakeStoragePath, | 313 self.assertEquals(fakeStoragePath, |
| 400 self.device.GetExternalStoragePath()) | 314 self.device.GetExternalStoragePath()) |
| 401 | 315 |
| 402 def testGetExternalStoragePath_fails(self): | 316 def testGetExternalStoragePath_fails(self): |
| 403 with self.assertShellCall('echo $EXTERNAL_STORAGE', '\r\n'): | 317 with self.assertCalls( |
| 318 "adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", '\r\n'): |
| 404 with self.assertRaises(device_errors.CommandFailedError): | 319 with self.assertRaises(device_errors.CommandFailedError): |
| 405 self.device.GetExternalStoragePath() | 320 self.device.GetExternalStoragePath() |
| 406 | 321 |
| 407 | 322 |
| 408 class DeviceUtilsWaitUntilFullyBootedTest(DeviceUtilsOldImplTest): | 323 class DeviceUtilsWaitUntilFullyBootedTest(DeviceUtilsOldImplTest): |
| 409 | 324 |
| 410 def testWaitUntilFullyBooted_succeedsNoWifi(self): | 325 def testWaitUntilFullyBooted_succeedsNoWifi(self): |
| 411 with self.assertCallsSequence([ | 326 with self.assertCallsSequence([ |
| 412 # AndroidCommands.WaitForSystemBootCompleted | 327 # AndroidCommands.WaitForSystemBootCompleted |
| 413 ('adb -s 0123456789abcdef wait-for-device', ''), | 328 ('adb -s 0123456789abcdef wait-for-device', ''), |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 622 return_value='this.is.a.test.package')): | 537 return_value='this.is.a.test.package')): |
| 623 with self.assertCallsSequence([ | 538 with self.assertCallsSequence([ |
| 624 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", | 539 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", |
| 625 ''), | 540 ''), |
| 626 ("adb -s 0123456789abcdef install /fake/test/app.apk", | 541 ("adb -s 0123456789abcdef install /fake/test/app.apk", |
| 627 'Failure\r\n')]): | 542 'Failure\r\n')]): |
| 628 with self.assertRaises(device_errors.CommandFailedError): | 543 with self.assertRaises(device_errors.CommandFailedError): |
| 629 self.device.Install('/fake/test/app.apk', retries=0) | 544 self.device.Install('/fake/test/app.apk', retries=0) |
| 630 | 545 |
| 631 | 546 |
| 632 class DeviceUtilsRunShellCommandTest(DeviceUtilsNewImplTest): | 547 class DeviceUtilsRunShellCommandTest(DeviceUtilsOldImplTest): |
| 548 |
| 633 def testRunShellCommand_commandAsList(self): | 549 def testRunShellCommand_commandAsList(self): |
| 634 with self.assertShellCall('pm list packages'): | 550 with self.assertCalls( |
| 551 "adb -s 0123456789abcdef shell 'pm list packages'", |
| 552 'pacakge:android\r\n'): |
| 635 self.device.RunShellCommand(['pm', 'list', 'packages']) | 553 self.device.RunShellCommand(['pm', 'list', 'packages']) |
| 636 | 554 |
| 637 def testRunShellCommand_commandAsListQuoted(self): | |
| 638 with self.assertShellCall("echo 'hello world' '$10'"): | |
| 639 self.device.RunShellCommand(['echo', 'hello world', '$10']) | |
| 640 | |
| 641 def testRunShellCommand_commandAsString(self): | 555 def testRunShellCommand_commandAsString(self): |
| 642 with self.assertShellCall('echo "$VAR"'): | 556 with self.assertCalls( |
| 643 self.device.RunShellCommand('echo "$VAR"') | 557 "adb -s 0123456789abcdef shell 'dumpsys wifi'", |
| 644 | 558 'Wi-Fi is enabled\r\n'): |
| 645 def testNewRunShellImpl_withEnv(self): | 559 self.device.RunShellCommand('dumpsys wifi') |
| 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') | |
| 664 | 560 |
| 665 def testRunShellCommand_withSu(self): | 561 def testRunShellCommand_withSu(self): |
| 666 with self.assertShellCallSequence([ | 562 with self.assertCallsSequence([ |
| 667 ('ls /root', _ShellError()), | 563 ("adb -s 0123456789abcdef shell 'ls /root'", 'Permission denied\r\n'), |
| 668 ('su -c setprop service.adb.root 0', '')]): | 564 ("adb -s 0123456789abcdef shell 'su -c setprop service.adb.root 0'", |
| 565 '')]): |
| 669 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) | 566 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) |
| 670 | 567 |
| 671 def testRunShellCommand_withRoot(self): | 568 def testRunShellCommand_withRoot(self): |
| 672 with self.assertShellCallSequence([ | 569 with self.assertCallsSequence([ |
| 673 ('ls /root', '\r\n'), | 570 ("adb -s 0123456789abcdef shell 'ls /root'", 'hello\r\nworld\r\n'), |
| 674 ('setprop service.adb.root 0', '')]): | 571 ("adb -s 0123456789abcdef shell 'setprop service.adb.root 0'", '')]): |
| 675 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) | 572 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) |
| 676 | 573 |
| 677 def testRunShellCommand_manyLines(self): | |
| 678 cmd = 'ls /some/path' | |
| 679 with self.assertShellCall(cmd, 'file1\r\nfile2\r\nfile3\r\n'): | |
| 680 self.assertEquals(['file1', 'file2', 'file3'], | |
| 681 self.device.RunShellCommand(cmd)) | |
| 682 | |
| 683 def testRunShellCommand_singleLine_success(self): | |
| 684 cmd = 'echo $VALUE' | |
| 685 with self.assertShellCall(cmd, 'some value\r\n'): | |
| 686 self.assertEquals('some value', | |
| 687 self.device.RunShellCommand(cmd, single_line=True)) | |
| 688 | |
| 689 def testRunShellCommand_singleLine_successEmptyLine(self): | |
| 690 cmd = 'echo $VALUE' | |
| 691 with self.assertShellCall(cmd, '\r\n'): | |
| 692 self.assertEquals('', | |
| 693 self.device.RunShellCommand(cmd, single_line=True)) | |
| 694 | |
| 695 def testRunShellCommand_singleLine_successWithoutEndLine(self): | |
| 696 cmd = 'echo -n $VALUE' | |
| 697 with self.assertShellCall(cmd, 'some value'): | |
| 698 self.assertEquals('some value', | |
| 699 self.device.RunShellCommand(cmd, single_line=True)) | |
| 700 | |
| 701 def testRunShellCommand_singleLine_failNoLines(self): | |
| 702 cmd = 'echo $VALUE' | |
| 703 with self.assertShellCall(cmd, ''): | |
| 704 with self.assertRaises(device_errors.CommandFailedError): | |
| 705 self.device.RunShellCommand(cmd, single_line=True) | |
| 706 | |
| 707 def testRunShellCommand_singleLine_failTooManyLines(self): | |
| 708 cmd = 'echo $VALUE' | |
| 709 with self.assertShellCall(cmd, 'some value\r\nanother value\r\n'): | |
| 710 with self.assertRaises(device_errors.CommandFailedError): | |
| 711 self.device.RunShellCommand(cmd, single_line=True) | |
| 712 | |
| 713 def testRunShellCommand_checkReturn_success(self): | 574 def testRunShellCommand_checkReturn_success(self): |
| 714 cmd = 'echo $ANDROID_DATA' | 575 with self.assertCalls( |
| 715 output = '/data\r\n' | 576 "adb -s 0123456789abcdef shell 'echo $ANDROID_DATA; echo %$?'", |
| 716 with self.assertShellCall(cmd, output): | 577 '/data\r\n%0\r\n'): |
| 717 self.assertEquals([output.rstrip()], | 578 self.device.RunShellCommand('echo $ANDROID_DATA', check_return=True) |
| 718 self.device.RunShellCommand(cmd, check_return=True)) | |
| 719 | 579 |
| 720 def testRunShellCommand_checkReturn_failure(self): | 580 def testRunShellCommand_checkReturn_failure(self): |
| 721 cmd = 'ls /root' | 581 with self.assertCalls( |
| 722 output = 'opendir failed, Permission denied\r\n' | 582 "adb -s 0123456789abcdef shell 'echo $ANDROID_DATA; echo %$?'", |
| 723 with self.assertShellCall(cmd, _ShellError(output)): | 583 '\r\n%1\r\n'): |
| 724 with self.assertRaises(device_errors.AdbShellCommandFailedError): | 584 with self.assertRaises(device_errors.CommandFailedError): |
| 725 self.device.RunShellCommand(cmd, check_return=True) | 585 self.device.RunShellCommand('echo $ANDROID_DATA', check_return=True) |
| 726 | |
| 727 def testRunShellCommand_checkReturn_disabled(self): | |
| 728 cmd = 'ls /root' | |
| 729 output = 'opendir failed, Permission denied\r\n' | |
| 730 with self.assertShellCall(cmd, _ShellError(output)): | |
| 731 self.assertEquals([output.rstrip()], | |
| 732 self.device.RunShellCommand(cmd, check_return=False)) | |
| 733 | 586 |
| 734 | 587 |
| 735 class DeviceUtilsKillAllTest(DeviceUtilsNewImplTest): | 588 class DeviceUtilsKillAllTest(DeviceUtilsOldImplTest): |
| 736 | 589 |
| 737 def testKillAll_noMatchingProcesses(self): | 590 def testKillAll_noMatchingProcesses(self): |
| 738 output = 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 591 with self.assertCalls( |
| 739 with self.assertShellCallSequence([('ps', output)]): | 592 "adb -s 0123456789abcdef shell 'ps'", |
| 593 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n'): |
| 740 with self.assertRaises(device_errors.CommandFailedError): | 594 with self.assertRaises(device_errors.CommandFailedError): |
| 741 self.device.KillAll('test_process') | 595 self.device.KillAll('test_process') |
| 742 | 596 |
| 743 def testKillAll_nonblocking(self): | 597 def testKillAll_nonblocking(self): |
| 744 with self.assertShellCallSequence([ | 598 with self.assertCallsSequence([ |
| 745 ('ps', 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 599 ("adb -s 0123456789abcdef shell 'ps'", |
| 746 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' | 600 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
| 747 'this.is.a.test.process\r\n'), | 601 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' |
| 748 ('kill -9 1234', '')]): | 602 'this.is.a.test.process\r\n'), |
| 603 ("adb -s 0123456789abcdef shell 'kill -9 1234'", '')]): |
| 749 self.assertEquals(1, | 604 self.assertEquals(1, |
| 750 self.device.KillAll('this.is.a.test.process', blocking=False)) | 605 self.device.KillAll('this.is.a.test.process', blocking=False)) |
| 751 | 606 |
| 752 def testKillAll_blocking(self): | 607 def testKillAll_blocking(self): |
| 753 with mock.patch('time.sleep'): | 608 with mock.patch('time.sleep'): |
| 754 with self.assertShellCallSequence([ | 609 with self.assertCallsSequence([ |
| 755 ('ps', 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 610 ("adb -s 0123456789abcdef shell 'ps'", |
| 756 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' | 611 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
| 757 'this.is.a.test.process\r\n'), | 612 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' |
| 758 ('kill -9 1234', ''), | 613 'this.is.a.test.process\r\n'), |
| 759 ('ps', 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 614 ("adb -s 0123456789abcdef shell 'kill -9 1234'", ''), |
| 760 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' | 615 ("adb -s 0123456789abcdef shell 'ps'", |
| 761 'this.is.a.test.process\r\n'), | 616 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
| 762 ('ps', 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n')]): | 617 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' |
| 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')]): |
| 763 self.assertEquals(1, | 621 self.assertEquals(1, |
| 764 self.device.KillAll('this.is.a.test.process', blocking=True)) | 622 self.device.KillAll('this.is.a.test.process', blocking=True)) |
| 765 | 623 |
| 766 def testKillAll_root(self): | 624 def testKillAll_root(self): |
| 767 with self.assertShellCallSequence([ | 625 with self.assertCallsSequence([ |
| 768 ('ps', 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 626 ("adb -s 0123456789abcdef shell 'ps'", |
| 769 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' | 627 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
| 770 'this.is.a.test.process\r\n'), | 628 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' |
| 771 ('ls /root', _ShellError()), | 629 'this.is.a.test.process\r\n'), |
| 772 ('su -c kill -9 1234', '')]): | 630 ("adb -s 0123456789abcdef shell 'ls /root'", 'Permission denied\r\n'), |
| 631 ("adb -s 0123456789abcdef shell 'su -c kill -9 1234'", '')]): |
| 773 self.assertEquals(1, | 632 self.assertEquals(1, |
| 774 self.device.KillAll('this.is.a.test.process', as_root=True)) | 633 self.device.KillAll('this.is.a.test.process', as_root=True)) |
| 775 | 634 |
| 776 def testKillAll_sigterm(self): | 635 def testKillAll_sigterm(self): |
| 777 with self.assertShellCallSequence([ | 636 with self.assertCallsSequence([ |
| 778 ('ps', 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 637 ("adb -s 0123456789abcdef shell 'ps'", |
| 779 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' | 638 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
| 780 'this.is.a.test.process\r\n'), | 639 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' |
| 781 ('kill -15 1234', '')]): | 640 'this.is.a.test.process\r\n'), |
| 641 ("adb -s 0123456789abcdef shell 'kill -15 1234'", '')]): |
| 782 self.assertEquals(1, | 642 self.assertEquals(1, |
| 783 self.device.KillAll('this.is.a.test.process', signum=signal.SIGTERM)) | 643 self.device.KillAll('this.is.a.test.process', signum=signal.SIGTERM)) |
| 784 | 644 |
| 785 | 645 |
| 786 class DeviceUtilsStartActivityTest(DeviceUtilsOldImplTest): | 646 class DeviceUtilsStartActivityTest(DeviceUtilsOldImplTest): |
| 787 | 647 |
| 788 def testStartActivity_actionOnly(self): | 648 def testStartActivity_actionOnly(self): |
| 789 test_intent = intent.Intent(action='android.intent.action.VIEW') | 649 test_intent = intent.Intent(action='android.intent.action.VIEW') |
| 790 with self.assertCalls( | 650 with self.assertCalls( |
| 791 "adb -s 0123456789abcdef shell 'am start " | 651 "adb -s 0123456789abcdef shell 'am start " |
| (...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1077 self.device._PushChangedFilesZipped(test_files) | 937 self.device._PushChangedFilesZipped(test_files) |
| 1078 | 938 |
| 1079 mock_zip_proc.assert_called_once_with( | 939 mock_zip_proc.assert_called_once_with( |
| 1080 target=device_utils.DeviceUtils._CreateDeviceZip, | 940 target=device_utils.DeviceUtils._CreateDeviceZip, |
| 1081 args=('/test/temp/file/tmp.zip', test_files)) | 941 args=('/test/temp/file/tmp.zip', test_files)) |
| 1082 self.adb.Push.assert_called_once_with( | 942 self.adb.Push.assert_called_once_with( |
| 1083 '/test/temp/file/tmp.zip', '/test/device/external_dir/tmp.zip') | 943 '/test/temp/file/tmp.zip', '/test/device/external_dir/tmp.zip') |
| 1084 self.assertEqual(2, self.device._RunShellCommandImpl.call_count) | 944 self.assertEqual(2, self.device._RunShellCommandImpl.call_count) |
| 1085 self.device._RunShellCommandImpl.assert_any_call( | 945 self.device._RunShellCommandImpl.assert_any_call( |
| 1086 ['unzip', '/test/device/external_dir/tmp.zip'], | 946 ['unzip', '/test/device/external_dir/tmp.zip'], |
| 1087 as_root=True, | 947 as_root=True, check_return=True, |
| 1088 env={'PATH': '$PATH:/data/local/tmp/bin'}, | 948 env={'PATH': '$PATH:/data/local/tmp/bin'}) |
| 1089 check_return=True) | |
| 1090 self.device._RunShellCommandImpl.assert_any_call( | 949 self.device._RunShellCommandImpl.assert_any_call( |
| 1091 ['rm', '/test/device/external_dir/tmp.zip'], check_return=True) | 950 ['rm', '/test/device/external_dir/tmp.zip']) |
| 1092 | 951 |
| 1093 def testPushChangedFilesZipped_multiple(self): | 952 def testPushChangedFilesZipped_multiple(self): |
| 1094 test_files = [('/test/host/path/file1', '/test/device/path/file1'), | 953 test_files = [('/test/host/path/file1', '/test/device/path/file1'), |
| 1095 ('/test/host/path/file2', '/test/device/path/file2')] | 954 ('/test/host/path/file2', '/test/device/path/file2')] |
| 1096 | 955 |
| 1097 self.device._GetExternalStoragePathImpl = mock.Mock( | 956 self.device._GetExternalStoragePathImpl = mock.Mock( |
| 1098 return_value='/test/device/external_dir') | 957 return_value='/test/device/external_dir') |
| 1099 self.device._IsOnlineImpl = mock.Mock(return_value=True) | 958 self.device._IsOnlineImpl = mock.Mock(return_value=True) |
| 1100 self.device._RunShellCommandImpl = mock.Mock() | 959 self.device._RunShellCommandImpl = mock.Mock() |
| 1101 mock_zip_temp = mock.mock_open() | 960 mock_zip_temp = mock.mock_open() |
| 1102 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip' | 961 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip' |
| 1103 with mock.patch('multiprocessing.Process') as mock_zip_proc, ( | 962 with mock.patch('multiprocessing.Process') as mock_zip_proc, ( |
| 1104 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)): | 963 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)): |
| 1105 self.device._PushChangedFilesZipped(test_files) | 964 self.device._PushChangedFilesZipped(test_files) |
| 1106 | 965 |
| 1107 mock_zip_proc.assert_called_once_with( | 966 mock_zip_proc.assert_called_once_with( |
| 1108 target=device_utils.DeviceUtils._CreateDeviceZip, | 967 target=device_utils.DeviceUtils._CreateDeviceZip, |
| 1109 args=('/test/temp/file/tmp.zip', test_files)) | 968 args=('/test/temp/file/tmp.zip', test_files)) |
| 1110 self.adb.Push.assert_called_once_with( | 969 self.adb.Push.assert_called_once_with( |
| 1111 '/test/temp/file/tmp.zip', '/test/device/external_dir/tmp.zip') | 970 '/test/temp/file/tmp.zip', '/test/device/external_dir/tmp.zip') |
| 1112 self.assertEqual(2, self.device._RunShellCommandImpl.call_count) | 971 self.assertEqual(2, self.device._RunShellCommandImpl.call_count) |
| 1113 self.device._RunShellCommandImpl.assert_any_call( | 972 self.device._RunShellCommandImpl.assert_any_call( |
| 1114 ['unzip', '/test/device/external_dir/tmp.zip'], | 973 ['unzip', '/test/device/external_dir/tmp.zip'], |
| 1115 as_root=True, | 974 as_root=True, check_return=True, |
| 1116 env={'PATH': '$PATH:/data/local/tmp/bin'}, | 975 env={'PATH': '$PATH:/data/local/tmp/bin'}) |
| 1117 check_return=True) | |
| 1118 self.device._RunShellCommandImpl.assert_any_call( | 976 self.device._RunShellCommandImpl.assert_any_call( |
| 1119 ['rm', '/test/device/external_dir/tmp.zip'], check_return=True) | 977 ['rm', '/test/device/external_dir/tmp.zip']) |
| 1120 | 978 |
| 1121 | 979 |
| 1122 class DeviceUtilsFileExistsTest(DeviceUtilsOldImplTest): | 980 class DeviceUtilsFileExistsTest(DeviceUtilsOldImplTest): |
| 1123 | 981 |
| 1124 def testFileExists_usingTest_fileExists(self): | 982 def testFileExists_usingTest_fileExists(self): |
| 1125 with self.assertCalls( | 983 with self.assertCalls( |
| 1126 "adb -s 0123456789abcdef shell " | 984 "adb -s 0123456789abcdef shell " |
| 1127 "'test -e \"/data/app/test.file.exists\"; echo $?'", | 985 "'test -e \"/data/app/test.file.exists\"; echo $?'", |
| 1128 '0\r\n'): | 986 '0\r\n'): |
| 1129 self.assertTrue(self.device.FileExists('/data/app/test.file.exists')) | 987 self.assertTrue(self.device.FileExists('/data/app/test.file.exists')) |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1337 self.device.WriteFile('/test/file/written.to.device', | 1195 self.device.WriteFile('/test/file/written.to.device', |
| 1338 'new test file contents', as_root=True) | 1196 'new test file contents', as_root=True) |
| 1339 | 1197 |
| 1340 def testWriteFile_asRoot_rejected(self): | 1198 def testWriteFile_asRoot_rejected(self): |
| 1341 self.device.old_interface._privileged_command_runner = None | 1199 self.device.old_interface._privileged_command_runner = None |
| 1342 self.device.old_interface._protected_file_access_method_initialized = True | 1200 self.device.old_interface._protected_file_access_method_initialized = True |
| 1343 with self.assertRaises(device_errors.CommandFailedError): | 1201 with self.assertRaises(device_errors.CommandFailedError): |
| 1344 self.device.WriteFile('/test/file/no.permissions.to.write', | 1202 self.device.WriteFile('/test/file/no.permissions.to.write', |
| 1345 'new test file contents', as_root=True) | 1203 'new test file contents', as_root=True) |
| 1346 | 1204 |
| 1347 class DeviceUtilsWriteTextFileTest(DeviceUtilsNewImplTest): | 1205 class DeviceUtilsWriteTextFileTest(DeviceUtilsOldImplTest): |
| 1348 | 1206 |
| 1349 def testWriteTextFileTest_basic(self): | 1207 def testWriteTextFileTest_basic(self): |
| 1350 with self.assertShellCall('echo some.string > /test/file/to.write'): | 1208 with self.assertCalls( |
| 1209 "adb -s 0123456789abcdef shell 'echo some.string" |
| 1210 " > /test/file/to.write; echo %$?'", '%0\r\n'): |
| 1351 self.device.WriteTextFile('/test/file/to.write', 'some.string') | 1211 self.device.WriteTextFile('/test/file/to.write', 'some.string') |
| 1352 | 1212 |
| 1353 def testWriteTextFileTest_quoted(self): | 1213 def testWriteTextFileTest_stringWithSpaces(self): |
| 1354 with self.assertShellCall( | 1214 with self.assertCalls( |
| 1355 "echo 'some other string' > '/test/file/to write'"): | 1215 "adb -s 0123456789abcdef shell 'echo '\\''some other string'\\''" |
| 1356 self.device.WriteTextFile('/test/file/to write', 'some other string') | 1216 " > /test/file/to.write; echo %$?'", '%0\r\n'): |
| 1217 self.device.WriteTextFile('/test/file/to.write', 'some other string') |
| 1357 | 1218 |
| 1358 def testWriteTextFileTest_asRoot(self): | 1219 def testWriteTextFileTest_asRoot_withSu(self): |
| 1359 with self.assertShellCallSequence([ | 1220 with self.assertCallsSequence([ |
| 1360 ('ls /root', _ShellError()), | 1221 ("adb -s 0123456789abcdef shell 'ls /root'", 'Permission denied\r\n'), |
| 1361 ('su -c echo string > /test/file', '')]): | 1222 ("adb -s 0123456789abcdef shell 'su -c echo some.string" |
| 1362 self.device.WriteTextFile('/test/file', 'string', as_root=True) | 1223 " > /test/file/to.write; echo %$?'", '%0\r\n')]): |
| 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) |
| 1363 | 1243 |
| 1364 class DeviceUtilsLsTest(DeviceUtilsOldImplTest): | 1244 class DeviceUtilsLsTest(DeviceUtilsOldImplTest): |
| 1365 | 1245 |
| 1366 def testLs_nothing(self): | 1246 def testLs_nothing(self): |
| 1367 with self.assertCallsSequence([ | 1247 with self.assertCallsSequence([ |
| 1368 ("adb -s 0123456789abcdef shell 'ls -lR /this/file/does.not.exist'", | 1248 ("adb -s 0123456789abcdef shell 'ls -lR /this/file/does.not.exist'", |
| 1369 '/this/file/does.not.exist: No such file or directory\r\n'), | 1249 '/this/file/does.not.exist: No such file or directory\r\n'), |
| 1370 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]): | 1250 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]): |
| 1371 self.assertEqual({}, self.device.Ls('/this/file/does.not.exist')) | 1251 self.assertEqual({}, self.device.Ls('/this/file/does.not.exist')) |
| 1372 | 1252 |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1509 class DeviceUtilsSetPropTest(DeviceUtilsOldImplTest): | 1389 class DeviceUtilsSetPropTest(DeviceUtilsOldImplTest): |
| 1510 | 1390 |
| 1511 def testSetProp(self): | 1391 def testSetProp(self): |
| 1512 with self.assertCalls( | 1392 with self.assertCalls( |
| 1513 'adb -s 0123456789abcdef shell ' | 1393 'adb -s 0123456789abcdef shell ' |
| 1514 'setprop this.is.a.test.property "test_property_value"', | 1394 'setprop this.is.a.test.property "test_property_value"', |
| 1515 ''): | 1395 ''): |
| 1516 self.device.SetProp('this.is.a.test.property', 'test_property_value') | 1396 self.device.SetProp('this.is.a.test.property', 'test_property_value') |
| 1517 | 1397 |
| 1518 | 1398 |
| 1519 class DeviceUtilsGetPidsTest(DeviceUtilsNewImplTest): | 1399 class DeviceUtilsGetPidsTest(DeviceUtilsOldImplTest): |
| 1520 | 1400 |
| 1521 def testGetPids_noMatches(self): | 1401 def testGetPids_noMatches(self): |
| 1522 with self.assertShellCall( | 1402 with self.assertCalls( |
| 1523 'ps', | 1403 "adb -s 0123456789abcdef shell 'ps'", |
| 1524 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 1404 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
| 1525 'user 1000 100 1024 1024 ffffffff 00000000 no.match\r\n'): | 1405 'user 1000 100 1024 1024 ffffffff 00000000 no.match\r\n'): |
| 1526 self.assertEqual({}, self.device.GetPids('does.not.match')) | 1406 self.assertEqual({}, self.device.GetPids('does.not.match')) |
| 1527 | 1407 |
| 1528 def testGetPids_oneMatch(self): | 1408 def testGetPids_oneMatch(self): |
| 1529 with self.assertShellCall( | 1409 with self.assertCalls( |
| 1530 'ps', | 1410 "adb -s 0123456789abcdef shell 'ps'", |
| 1531 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 1411 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
| 1532 'user 1000 100 1024 1024 ffffffff 00000000 not.a.match\r\n' | 1412 'user 1000 100 1024 1024 ffffffff 00000000 not.a.match\r\n' |
| 1533 'user 1001 100 1024 1024 ffffffff 00000000 one.match\r\n'): | 1413 'user 1001 100 1024 1024 ffffffff 00000000 one.match\r\n'): |
| 1534 self.assertEqual({'one.match': '1001'}, self.device.GetPids('one.match')) | 1414 self.assertEqual({'one.match': '1001'}, self.device.GetPids('one.match')) |
| 1535 | 1415 |
| 1536 def testGetPids_mutlipleMatches(self): | 1416 def testGetPids_mutlipleMatches(self): |
| 1537 with self.assertShellCall( | 1417 with self.assertCalls( |
| 1538 'ps', | 1418 "adb -s 0123456789abcdef shell 'ps'", |
| 1539 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 1419 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
| 1540 'user 1000 100 1024 1024 ffffffff 00000000 not\r\n' | 1420 'user 1000 100 1024 1024 ffffffff 00000000 not\r\n' |
| 1541 'user 1001 100 1024 1024 ffffffff 00000000 one.match\r\n' | 1421 'user 1001 100 1024 1024 ffffffff 00000000 one.match\r\n' |
| 1542 'user 1002 100 1024 1024 ffffffff 00000000 two.match\r\n' | 1422 'user 1002 100 1024 1024 ffffffff 00000000 two.match\r\n' |
| 1543 'user 1003 100 1024 1024 ffffffff 00000000 three.match\r\n'): | 1423 'user 1003 100 1024 1024 ffffffff 00000000 three.match\r\n'): |
| 1544 self.assertEqual( | 1424 self.assertEqual( |
| 1545 {'one.match': '1001', 'two.match': '1002', 'three.match': '1003'}, | 1425 {'one.match': '1001', 'two.match': '1002', 'three.match': '1003'}, |
| 1546 self.device.GetPids('match')) | 1426 self.device.GetPids('match')) |
| 1547 | 1427 |
| 1548 def testGetPids_exactMatch(self): | 1428 def testGetPids_exactMatch(self): |
| 1549 with self.assertShellCall( | 1429 with self.assertCalls( |
| 1550 'ps', | 1430 "adb -s 0123456789abcdef shell 'ps'", |
| 1551 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 1431 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
| 1552 'user 1000 100 1024 1024 ffffffff 00000000 not.exact.match\r\n' | 1432 'user 1000 100 1024 1024 ffffffff 00000000 not.exact.match\r\n' |
| 1553 'user 1234 100 1024 1024 ffffffff 00000000 exact.match\r\n'): | 1433 'user 1234 100 1024 1024 ffffffff 00000000 exact.match\r\n'): |
| 1554 self.assertEqual( | 1434 self.assertEqual( |
| 1555 {'not.exact.match': '1000', 'exact.match': '1234'}, | 1435 {'not.exact.match': '1000', 'exact.match': '1234'}, |
| 1556 self.device.GetPids('exact.match')) | 1436 self.device.GetPids('exact.match')) |
| 1557 | 1437 |
| 1558 | 1438 |
| 1559 class DeviceUtilsTakeScreenshotTest(DeviceUtilsOldImplTest): | 1439 class DeviceUtilsTakeScreenshotTest(DeviceUtilsOldImplTest): |
| 1560 | 1440 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1646 self.device = device_utils.DeviceUtils(None) | 1526 self.device = device_utils.DeviceUtils(None) |
| 1647 with self.assertCalls('adb get-serialno', 'unknown'), ( | 1527 with self.assertCalls('adb get-serialno', 'unknown'), ( |
| 1648 self.assertRaises(device_errors.NoDevicesError)): | 1528 self.assertRaises(device_errors.NoDevicesError)): |
| 1649 str(self.device) | 1529 str(self.device) |
| 1650 | 1530 |
| 1651 | 1531 |
| 1652 if __name__ == '__main__': | 1532 if __name__ == '__main__': |
| 1653 logging.getLogger().setLevel(logging.DEBUG) | 1533 logging.getLogger().setLevel(logging.DEBUG) |
| 1654 unittest.main(verbosity=2) | 1534 unittest.main(verbosity=2) |
| 1655 | 1535 |
| OLD | NEW |