OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """ | 5 """ |
6 Unit tests for the contents of device_utils.py (mostly DeviceUtils). | 6 Unit tests for the contents of device_utils.py (mostly DeviceUtils). |
7 """ | 7 """ |
8 | 8 |
9 # pylint: disable=C0321 | 9 # pylint: disable=C0321 |
10 # pylint: disable=W0212 | 10 # pylint: disable=W0212 |
11 # pylint: disable=W0613 | 11 # pylint: disable=W0613 |
12 | 12 |
13 import os | 13 import os |
| 14 import signal |
14 import sys | 15 import sys |
15 import unittest | 16 import unittest |
16 | 17 |
17 from pylib import android_commands | 18 from pylib import android_commands |
18 from pylib import constants | 19 from pylib import constants |
19 from pylib.device import adb_wrapper | 20 from pylib.device import adb_wrapper |
20 from pylib.device import device_errors | 21 from pylib.device import device_errors |
21 from pylib.device import device_utils | 22 from pylib.device import device_utils |
| 23 from pylib.device import intent |
22 | 24 |
23 sys.path.append(os.path.join( | 25 sys.path.append(os.path.join( |
24 constants.DIR_SOURCE_ROOT, 'third_party', 'android_testrunner')) | 26 constants.DIR_SOURCE_ROOT, 'third_party', 'android_testrunner')) |
25 import run_command as atr_run_command | 27 import run_command as atr_run_command |
26 | 28 |
27 sys.path.append(os.path.join( | 29 sys.path.append(os.path.join( |
28 constants.DIR_SOURCE_ROOT, 'third_party', 'pymock')) | 30 constants.DIR_SOURCE_ROOT, 'third_party', 'pymock')) |
29 import mock # pylint: disable=F0401 | 31 import mock # pylint: disable=F0401 |
30 | 32 |
31 | 33 |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 with self.assertOldImplCalls( | 398 with self.assertOldImplCalls( |
397 "adb -s 0123456789abcdef shell 'dumpsys wifi'", | 399 "adb -s 0123456789abcdef shell 'dumpsys wifi'", |
398 'Wi-Fi is enabled\r\n'): | 400 'Wi-Fi is enabled\r\n'): |
399 self.device.RunShellCommand('dumpsys wifi') | 401 self.device.RunShellCommand('dumpsys wifi') |
400 | 402 |
401 def testRunShellCommand_withSu(self): | 403 def testRunShellCommand_withSu(self): |
402 with self.assertOldImplCallsSequence([ | 404 with self.assertOldImplCallsSequence([ |
403 ("adb -s 0123456789abcdef shell 'ls /root'", 'Permission denied\r\n'), | 405 ("adb -s 0123456789abcdef shell 'ls /root'", 'Permission denied\r\n'), |
404 ("adb -s 0123456789abcdef shell 'su -c setprop service.adb.root 0'", | 406 ("adb -s 0123456789abcdef shell 'su -c setprop service.adb.root 0'", |
405 '')]): | 407 '')]): |
406 self.device.RunShellCommand('setprop service.adb.root 0', root=True) | 408 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) |
407 | 409 |
408 def testRunShellCommand_withRoot(self): | 410 def testRunShellCommand_withRoot(self): |
409 with self.assertOldImplCallsSequence([ | 411 with self.assertOldImplCallsSequence([ |
410 ("adb -s 0123456789abcdef shell 'ls /root'", 'hello\r\nworld\r\n'), | 412 ("adb -s 0123456789abcdef shell 'ls /root'", 'hello\r\nworld\r\n'), |
411 ("adb -s 0123456789abcdef shell 'setprop service.adb.root 0'", '')]): | 413 ("adb -s 0123456789abcdef shell 'setprop service.adb.root 0'", '')]): |
412 self.device.RunShellCommand('setprop service.adb.root 0', root=True) | 414 self.device.RunShellCommand('setprop service.adb.root 0', as_root=True) |
413 | 415 |
414 def testRunShellCommand_checkReturn_success(self): | 416 def testRunShellCommand_checkReturn_success(self): |
415 with self.assertOldImplCalls( | 417 with self.assertOldImplCalls( |
416 "adb -s 0123456789abcdef shell 'echo $ANDROID_DATA; echo %$?'", | 418 "adb -s 0123456789abcdef shell 'echo $ANDROID_DATA; echo %$?'", |
417 '/data\r\n%0\r\n'): | 419 '/data\r\n%0\r\n'): |
418 self.device.RunShellCommand('echo $ANDROID_DATA', check_return=True) | 420 self.device.RunShellCommand('echo $ANDROID_DATA', check_return=True) |
419 | 421 |
420 def testRunShellCommand_checkReturn_failure(self): | 422 def testRunShellCommand_checkReturn_failure(self): |
421 with self.assertOldImplCalls( | 423 with self.assertOldImplCalls( |
422 "adb -s 0123456789abcdef shell 'echo $ANDROID_DATA; echo %$?'", | 424 "adb -s 0123456789abcdef shell 'echo $ANDROID_DATA; echo %$?'", |
423 '\r\n%1\r\n'): | 425 '\r\n%1\r\n'): |
424 with self.assertRaises(device_errors.CommandFailedError): | 426 with self.assertRaises(device_errors.CommandFailedError): |
425 self.device.RunShellCommand('echo $ANDROID_DATA', check_return=True) | 427 self.device.RunShellCommand('echo $ANDROID_DATA', check_return=True) |
426 | 428 |
| 429 def testKillAll_noMatchingProcesses(self): |
| 430 with self.assertOldImplCalls( |
| 431 "adb -s 0123456789abcdef shell 'ps'", |
| 432 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n'): |
| 433 with self.assertRaises(device_errors.CommandFailedError): |
| 434 self.device.KillAll('test_process') |
| 435 |
| 436 def testKillAll_nonblocking(self): |
| 437 with self.assertOldImplCallsSequence([ |
| 438 ("adb -s 0123456789abcdef shell 'ps'", |
| 439 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
| 440 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' |
| 441 'this.is.a.test.process\r\n'), |
| 442 ("adb -s 0123456789abcdef shell 'ps'", |
| 443 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
| 444 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' |
| 445 'this.is.a.test.process\r\n'), |
| 446 ("adb -s 0123456789abcdef shell 'kill -9 1234'", '')]): |
| 447 self.device.KillAll('this.is.a.test.process', blocking=False) |
| 448 |
| 449 def testKillAll_blocking(self): |
| 450 with mock.patch('time.sleep'): |
| 451 with self.assertOldImplCallsSequence([ |
| 452 ("adb -s 0123456789abcdef shell 'ps'", |
| 453 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
| 454 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' |
| 455 'this.is.a.test.process\r\n'), |
| 456 ("adb -s 0123456789abcdef shell 'ps'", |
| 457 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
| 458 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' |
| 459 'this.is.a.test.process\r\n'), |
| 460 ("adb -s 0123456789abcdef shell 'kill -9 1234'", ''), |
| 461 ("adb -s 0123456789abcdef shell 'ps'", |
| 462 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
| 463 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' |
| 464 'this.is.a.test.process\r\n'), |
| 465 ("adb -s 0123456789abcdef shell 'ps'", |
| 466 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n')]): |
| 467 self.device.KillAll('this.is.a.test.process', blocking=True) |
| 468 |
| 469 def testKillAll_root(self): |
| 470 with self.assertOldImplCallsSequence([ |
| 471 ("adb -s 0123456789abcdef shell 'ps'", |
| 472 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
| 473 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' |
| 474 'this.is.a.test.process\r\n'), |
| 475 ("adb -s 0123456789abcdef shell 'ps'", |
| 476 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
| 477 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' |
| 478 'this.is.a.test.process\r\n'), |
| 479 ("adb -s 0123456789abcdef shell 'su -c kill -9 1234'", '')]): |
| 480 self.device.KillAll('this.is.a.test.process', as_root=True) |
| 481 |
| 482 def testKillAll_sigterm(self): |
| 483 with self.assertOldImplCallsSequence([ |
| 484 ("adb -s 0123456789abcdef shell 'ps'", |
| 485 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
| 486 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' |
| 487 'this.is.a.test.process\r\n'), |
| 488 ("adb -s 0123456789abcdef shell 'ps'", |
| 489 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
| 490 'u0_a1 1234 174 123456 54321 ffffffff 456789ab ' |
| 491 'this.is.a.test.process\r\n'), |
| 492 ("adb -s 0123456789abcdef shell 'kill -15 1234'", '')]): |
| 493 self.device.KillAll('this.is.a.test.process', signum=signal.SIGTERM) |
| 494 |
| 495 def testStartActivity_actionOnly(self): |
| 496 test_intent = intent.Intent(action='android.intent.action.VIEW') |
| 497 with self.assertOldImplCalls( |
| 498 "adb -s 0123456789abcdef shell 'am start " |
| 499 "-a android.intent.action.VIEW'", |
| 500 'Starting: Intent { act=android.intent.action.VIEW }'): |
| 501 self.device.StartActivity(test_intent) |
| 502 |
| 503 def testStartActivity_success(self): |
| 504 test_intent = intent.Intent(action='android.intent.action.VIEW', |
| 505 package='this.is.a.test.package', |
| 506 activity='.Main') |
| 507 with self.assertOldImplCalls( |
| 508 "adb -s 0123456789abcdef shell 'am start " |
| 509 "-a android.intent.action.VIEW " |
| 510 "-n this.is.a.test.package/.Main'", |
| 511 'Starting: Intent { act=android.intent.action.VIEW }'): |
| 512 self.device.StartActivity(test_intent) |
| 513 |
| 514 def testStartActivity_failure(self): |
| 515 test_intent = intent.Intent(action='android.intent.action.VIEW', |
| 516 package='this.is.a.test.package', |
| 517 activity='.Main') |
| 518 with self.assertOldImplCalls( |
| 519 "adb -s 0123456789abcdef shell 'am start " |
| 520 "-a android.intent.action.VIEW " |
| 521 "-n this.is.a.test.package/.Main'", |
| 522 'Error: Failed to start test activity'): |
| 523 with self.assertRaises(device_errors.CommandFailedError): |
| 524 self.device.StartActivity(test_intent) |
| 525 |
| 526 def testStartActivity_blocking(self): |
| 527 test_intent = intent.Intent(action='android.intent.action.VIEW', |
| 528 package='this.is.a.test.package', |
| 529 activity='.Main') |
| 530 with self.assertOldImplCalls( |
| 531 "adb -s 0123456789abcdef shell 'am start " |
| 532 "-a android.intent.action.VIEW " |
| 533 "-W " |
| 534 "-n this.is.a.test.package/.Main'", |
| 535 'Starting: Intent { act=android.intent.action.VIEW }'): |
| 536 self.device.StartActivity(test_intent, blocking=True) |
| 537 |
| 538 def testStartActivity_withCategory(self): |
| 539 test_intent = intent.Intent(action='android.intent.action.VIEW', |
| 540 package='this.is.a.test.package', |
| 541 activity='.Main', |
| 542 category='android.intent.category.HOME') |
| 543 with self.assertOldImplCalls( |
| 544 "adb -s 0123456789abcdef shell 'am start " |
| 545 "-a android.intent.action.VIEW " |
| 546 "-c android.intent.category.HOME " |
| 547 "-n this.is.a.test.package/.Main'", |
| 548 'Starting: Intent { act=android.intent.action.VIEW }'): |
| 549 self.device.StartActivity(test_intent) |
| 550 |
| 551 def testStartActivity_withMultipleCategories(self): |
| 552 # The new implementation will start the activity with all provided |
| 553 # categories. The old one only uses the first category. |
| 554 test_intent = intent.Intent(action='android.intent.action.VIEW', |
| 555 package='this.is.a.test.package', |
| 556 activity='.Main', |
| 557 category=['android.intent.category.HOME', |
| 558 'android.intent.category.BROWSABLE']) |
| 559 with self.assertOldImplCalls( |
| 560 "adb -s 0123456789abcdef shell 'am start " |
| 561 "-a android.intent.action.VIEW " |
| 562 "-c android.intent.category.HOME " |
| 563 "-n this.is.a.test.package/.Main'", |
| 564 'Starting: Intent { act=android.intent.action.VIEW }'): |
| 565 self.device.StartActivity(test_intent) |
| 566 |
| 567 def testStartActivity_withData(self): |
| 568 test_intent = intent.Intent(action='android.intent.action.VIEW', |
| 569 package='this.is.a.test.package', |
| 570 activity='.Main', |
| 571 data='http://www.google.com/') |
| 572 with self.assertOldImplCalls( |
| 573 "adb -s 0123456789abcdef shell 'am start " |
| 574 "-a android.intent.action.VIEW " |
| 575 "-n this.is.a.test.package/.Main " |
| 576 "-d \"http://www.google.com/\"'", |
| 577 'Starting: Intent { act=android.intent.action.VIEW }'): |
| 578 self.device.StartActivity(test_intent) |
| 579 |
| 580 def testStartActivity_withStringExtra(self): |
| 581 test_intent = intent.Intent(action='android.intent.action.VIEW', |
| 582 package='this.is.a.test.package', |
| 583 activity='.Main', |
| 584 extras={'foo': 'test'}) |
| 585 with self.assertOldImplCalls( |
| 586 "adb -s 0123456789abcdef shell 'am start " |
| 587 "-a android.intent.action.VIEW " |
| 588 "-n this.is.a.test.package/.Main " |
| 589 "--es foo test'", |
| 590 'Starting: Intent { act=android.intent.action.VIEW }'): |
| 591 self.device.StartActivity(test_intent) |
| 592 |
| 593 def testStartActivity_withBoolExtra(self): |
| 594 test_intent = intent.Intent(action='android.intent.action.VIEW', |
| 595 package='this.is.a.test.package', |
| 596 activity='.Main', |
| 597 extras={'foo': True}) |
| 598 with self.assertOldImplCalls( |
| 599 "adb -s 0123456789abcdef shell 'am start " |
| 600 "-a android.intent.action.VIEW " |
| 601 "-n this.is.a.test.package/.Main " |
| 602 "--ez foo True'", |
| 603 'Starting: Intent { act=android.intent.action.VIEW }'): |
| 604 self.device.StartActivity(test_intent) |
| 605 |
| 606 def testStartActivity_withIntExtra(self): |
| 607 test_intent = intent.Intent(action='android.intent.action.VIEW', |
| 608 package='this.is.a.test.package', |
| 609 activity='.Main', |
| 610 extras={'foo': 123}) |
| 611 with self.assertOldImplCalls( |
| 612 "adb -s 0123456789abcdef shell 'am start " |
| 613 "-a android.intent.action.VIEW " |
| 614 "-n this.is.a.test.package/.Main " |
| 615 "--ei foo 123'", |
| 616 'Starting: Intent { act=android.intent.action.VIEW }'): |
| 617 self.device.StartActivity(test_intent) |
| 618 |
| 619 def testStartActivity_withTraceFile(self): |
| 620 test_intent = intent.Intent(action='android.intent.action.VIEW', |
| 621 package='this.is.a.test.package', |
| 622 activity='.Main') |
| 623 with self.assertOldImplCalls( |
| 624 "adb -s 0123456789abcdef shell 'am start " |
| 625 "-a android.intent.action.VIEW " |
| 626 "-n this.is.a.test.package/.Main " |
| 627 "--start-profiler test_trace_file.out'", |
| 628 'Starting: Intent { act=android.intent.action.VIEW }'): |
| 629 self.device.StartActivity(test_intent, |
| 630 trace_file_name='test_trace_file.out') |
| 631 |
| 632 def testStartActivity_withForceStop(self): |
| 633 test_intent = intent.Intent(action='android.intent.action.VIEW', |
| 634 package='this.is.a.test.package', |
| 635 activity='.Main') |
| 636 with self.assertOldImplCalls( |
| 637 "adb -s 0123456789abcdef shell 'am start " |
| 638 "-a android.intent.action.VIEW " |
| 639 "-S " |
| 640 "-n this.is.a.test.package/.Main'", |
| 641 'Starting: Intent { act=android.intent.action.VIEW }'): |
| 642 self.device.StartActivity(test_intent, force_stop=True) |
| 643 |
| 644 def testStartActivity_withFlags(self): |
| 645 test_intent = intent.Intent(action='android.intent.action.VIEW', |
| 646 package='this.is.a.test.package', |
| 647 activity='.Main', |
| 648 flags='0x10000000') |
| 649 with self.assertOldImplCalls( |
| 650 "adb -s 0123456789abcdef shell 'am start " |
| 651 "-a android.intent.action.VIEW " |
| 652 "-n this.is.a.test.package/.Main " |
| 653 "-f 0x10000000'", |
| 654 'Starting: Intent { act=android.intent.action.VIEW }'): |
| 655 self.device.StartActivity(test_intent) |
| 656 |
| 657 def testBroadcastIntent_noExtras(self): |
| 658 test_intent = intent.Intent(action='test.package.with.an.INTENT') |
| 659 with self.assertOldImplCalls( |
| 660 "adb -s 0123456789abcdef shell 'am broadcast " |
| 661 "-a test.package.with.an.INTENT '", |
| 662 'Broadcasting: Intent { act=test.package.with.an.INTENT } '): |
| 663 self.device.BroadcastIntent(test_intent) |
| 664 |
| 665 def testBroadcastIntent_withExtra(self): |
| 666 test_intent = intent.Intent(action='test.package.with.an.INTENT', |
| 667 extras={'foo': 'bar'}) |
| 668 with self.assertOldImplCalls( |
| 669 "adb -s 0123456789abcdef shell 'am broadcast " |
| 670 "-a test.package.with.an.INTENT " |
| 671 "-e foo \"bar\"'", |
| 672 'Broadcasting: Intent { act=test.package.with.an.INTENT } '): |
| 673 self.device.BroadcastIntent(test_intent) |
| 674 |
| 675 def testBroadcastIntent_withExtra_noValue(self): |
| 676 test_intent = intent.Intent(action='test.package.with.an.INTENT', |
| 677 extras={'foo': None}) |
| 678 with self.assertOldImplCalls( |
| 679 "adb -s 0123456789abcdef shell 'am broadcast " |
| 680 "-a test.package.with.an.INTENT " |
| 681 "-e foo'", |
| 682 'Broadcasting: Intent { act=test.package.with.an.INTENT } '): |
| 683 self.device.BroadcastIntent(test_intent) |
| 684 |
| 685 |
427 if __name__ == '__main__': | 686 if __name__ == '__main__': |
428 unittest.main(verbosity=2) | 687 unittest.main(verbosity=2) |
429 | 688 |
OLD | NEW |