| 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 |
| 11 # pylint: disable=W0212 | 11 # pylint: disable=W0212 |
| 12 # pylint: disable=W0613 | 12 # pylint: disable=W0613 |
| 13 | 13 |
| 14 import collections | 14 import collections |
| 15 import datetime | 15 import datetime |
| 16 import logging | 16 import logging |
| 17 import os | 17 import os |
| 18 import re | 18 import re |
| 19 import signal | |
| 20 import sys | 19 import sys |
| 21 import unittest | 20 import unittest |
| 22 | 21 |
| 23 from pylib import android_commands | 22 from pylib import android_commands |
| 24 from pylib import cmd_helper | 23 from pylib import cmd_helper |
| 25 from pylib import constants | 24 from pylib import constants |
| 25 from pylib import device_signal |
| 26 from pylib.device import adb_wrapper | 26 from pylib.device import adb_wrapper |
| 27 from pylib.device import device_errors | 27 from pylib.device import device_errors |
| 28 from pylib.device import device_utils | 28 from pylib.device import device_utils |
| 29 from pylib.device import intent | 29 from pylib.device import intent |
| 30 from pylib.utils import mock_calls | 30 from pylib.utils import mock_calls |
| 31 | 31 |
| 32 # RunCommand from third_party/android_testrunner/run_command.py is mocked | 32 # RunCommand from third_party/android_testrunner/run_command.py is mocked |
| 33 # below, so its path needs to be in sys.path. | 33 # below, so its path needs to be in sys.path. |
| 34 sys.path.append(os.path.join( | 34 sys.path.append(os.path.join( |
| 35 constants.DIR_SOURCE_ROOT, 'third_party', 'android_testrunner')) | 35 constants.DIR_SOURCE_ROOT, 'third_party', 'android_testrunner')) |
| (...skipping 598 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 634 constants.ANDROID_SDK_VERSION_CODES.ICE_CREAM_SANDWICH), | 634 constants.ANDROID_SDK_VERSION_CODES.ICE_CREAM_SANDWICH), |
| 635 (mock.call.pylib.constants.GetOutDirectory(), '/foo/bar'), | 635 (mock.call.pylib.constants.GetOutDirectory(), '/foo/bar'), |
| 636 (mock.call.os.path.exists(mock.ANY), True), | 636 (mock.call.os.path.exists(mock.ANY), True), |
| 637 (self.call.adb.Push(mock.ANY, mock.ANY), '')): | 637 (self.call.adb.Push(mock.ANY, mock.ANY), '')): |
| 638 self.assertNotEqual('', self.device.GetDevicePieWrapper()) | 638 self.assertNotEqual('', self.device.GetDevicePieWrapper()) |
| 639 | 639 |
| 640 | 640 |
| 641 @mock.patch('time.sleep', mock.Mock()) | 641 @mock.patch('time.sleep', mock.Mock()) |
| 642 class DeviceUtilsKillAllTest(DeviceUtilsTest): | 642 class DeviceUtilsKillAllTest(DeviceUtilsTest): |
| 643 | 643 |
| 644 def testKillAll_noMatchingProcesses(self): | 644 def testKillAll_noMatchingProcessesFailure(self): |
| 645 with self.assertCall(self.call.adb.Shell('ps'), | 645 with self.assertCall(self.call.device.GetPids('test_process'), {}): |
| 646 'USER PID PPID VSIZE RSS WCHAN PC NAME\n'): | |
| 647 with self.assertRaises(device_errors.CommandFailedError): | 646 with self.assertRaises(device_errors.CommandFailedError): |
| 648 self.device.KillAll('test_process') | 647 self.device.KillAll('test_process') |
| 649 | 648 |
| 649 def testKillAll_noMatchingProcessesQuiet(self): |
| 650 with self.assertCall(self.call.device.GetPids('test_process'), {}): |
| 651 self.assertEqual(0, self.device.KillAll('test_process', quiet=True)) |
| 652 |
| 650 def testKillAll_nonblocking(self): | 653 def testKillAll_nonblocking(self): |
| 651 with self.assertCalls( | 654 with self.assertCalls( |
| 652 (self.call.adb.Shell('ps'), | 655 (self.call.device.GetPids('some.process'), {'some.process': '1234'}), |
| 653 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' | |
| 654 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), | |
| 655 (self.call.adb.Shell('kill -9 1234'), '')): | 656 (self.call.adb.Shell('kill -9 1234'), '')): |
| 656 self.assertEquals(1, | 657 self.assertEquals(1, |
| 657 self.device.KillAll('some.process', blocking=False)) | 658 self.device.KillAll('some.process', blocking=False)) |
| 658 | 659 |
| 659 def testKillAll_blocking(self): | 660 def testKillAll_blocking(self): |
| 660 with self.assertCalls( | 661 with self.assertCalls( |
| 661 (self.call.adb.Shell('ps'), | 662 (self.call.device.GetPids('some.process'), {'some.process': '1234'}), |
| 662 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' | |
| 663 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), | |
| 664 (self.call.adb.Shell('kill -9 1234'), ''), | 663 (self.call.adb.Shell('kill -9 1234'), ''), |
| 665 (self.call.adb.Shell('ps'), | 664 (self.call.device.GetPids('some.process'), {'some.process': '1234'}), |
| 666 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' | 665 (self.call.device.GetPids('some.process'), {})): |
| 667 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), | |
| 668 (self.call.adb.Shell('ps'), | |
| 669 'USER PID PPID VSIZE RSS WCHAN PC NAME\n')): | |
| 670 self.assertEquals(1, | 666 self.assertEquals(1, |
| 671 self.device.KillAll('some.process', blocking=True)) | 667 self.device.KillAll('some.process', blocking=True)) |
| 672 | 668 |
| 673 def testKillAll_root(self): | 669 def testKillAll_root(self): |
| 674 with self.assertCalls( | 670 with self.assertCalls( |
| 675 (self.call.adb.Shell('ps'), | 671 (self.call.device.GetPids('some.process'), {'some.process': '1234'}), |
| 676 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' | |
| 677 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), | |
| 678 (self.call.device.NeedsSU(), True), | 672 (self.call.device.NeedsSU(), True), |
| 679 (self.call.adb.Shell("su -c sh -c 'kill -9 1234'"), '')): | 673 (self.call.adb.Shell("su -c sh -c 'kill -9 1234'"), '')): |
| 680 self.assertEquals(1, | 674 self.assertEquals(1, |
| 681 self.device.KillAll('some.process', as_root=True)) | 675 self.device.KillAll('some.process', as_root=True)) |
| 682 | 676 |
| 683 def testKillAll_sigterm(self): | 677 def testKillAll_sigterm(self): |
| 684 with self.assertCalls( | 678 with self.assertCalls( |
| 685 (self.call.adb.Shell('ps'), | 679 (self.call.device.GetPids('some.process'), {'some.process': '1234'}), |
| 686 'USER PID PPID VSIZE RSS WCHAN PC NAME\n' | |
| 687 'u0_a1 1234 174 123456 54321 ffffffff 456789ab some.process\n'), | |
| 688 (self.call.adb.Shell('kill -15 1234'), '')): | 680 (self.call.adb.Shell('kill -15 1234'), '')): |
| 689 self.assertEquals(1, | 681 self.assertEquals(1, |
| 690 self.device.KillAll('some.process', signum=signal.SIGTERM)) | 682 self.device.KillAll('some.process', signum=device_signal.SIGTERM)) |
| 691 | 683 |
| 692 | 684 |
| 693 class DeviceUtilsStartActivityTest(DeviceUtilsTest): | 685 class DeviceUtilsStartActivityTest(DeviceUtilsTest): |
| 694 | 686 |
| 695 def testStartActivity_actionOnly(self): | 687 def testStartActivity_actionOnly(self): |
| 696 test_intent = intent.Intent(action='android.intent.action.VIEW') | 688 test_intent = intent.Intent(action='android.intent.action.VIEW') |
| 697 with self.assertCall( | 689 with self.assertCall( |
| 698 self.call.adb.Shell('am start ' | 690 self.call.adb.Shell('am start ' |
| 699 '-a android.intent.action.VIEW'), | 691 '-a android.intent.action.VIEW'), |
| 700 'Starting: Intent { act=android.intent.action.VIEW }'): | 692 'Starting: Intent { act=android.intent.action.VIEW }'): |
| (...skipping 839 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1540 self.assertEqual(client_cache_one, {'test': 1}) | 1532 self.assertEqual(client_cache_one, {'test': 1}) |
| 1541 self.assertEqual(client_cache_two, {'test': 1}) | 1533 self.assertEqual(client_cache_two, {'test': 1}) |
| 1542 self.device._ClearCache() | 1534 self.device._ClearCache() |
| 1543 self.assertEqual(client_cache_one, {}) | 1535 self.assertEqual(client_cache_one, {}) |
| 1544 self.assertEqual(client_cache_two, {}) | 1536 self.assertEqual(client_cache_two, {}) |
| 1545 | 1537 |
| 1546 if __name__ == '__main__': | 1538 if __name__ == '__main__': |
| 1547 logging.getLogger().setLevel(logging.DEBUG) | 1539 logging.getLogger().setLevel(logging.DEBUG) |
| 1548 unittest.main(verbosity=2) | 1540 unittest.main(verbosity=2) |
| 1549 | 1541 |
| OLD | NEW |