| 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 815 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 826 with self.assertCall(self.call.device.GetPids('test_process'), {}): | 826 with self.assertCall(self.call.device.GetPids('test_process'), {}): |
| 827 with self.assertRaises(device_errors.CommandFailedError): | 827 with self.assertRaises(device_errors.CommandFailedError): |
| 828 self.device.KillAll('test_process') | 828 self.device.KillAll('test_process') |
| 829 | 829 |
| 830 def testKillAll_noMatchingProcessesQuiet(self): | 830 def testKillAll_noMatchingProcessesQuiet(self): |
| 831 with self.assertCall(self.call.device.GetPids('test_process'), {}): | 831 with self.assertCall(self.call.device.GetPids('test_process'), {}): |
| 832 self.assertEqual(0, self.device.KillAll('test_process', quiet=True)) | 832 self.assertEqual(0, self.device.KillAll('test_process', quiet=True)) |
| 833 | 833 |
| 834 def testKillAll_nonblocking(self): | 834 def testKillAll_nonblocking(self): |
| 835 with self.assertCalls( | 835 with self.assertCalls( |
| 836 (self.call.device.GetPids('some.process'), {'some.process': '1234'}), | 836 (self.call.device.GetPids('some.process'), {'some.process': ['1234']}), |
| 837 (self.call.adb.Shell('kill -9 1234'), '')): | 837 (self.call.adb.Shell('kill -9 1234'), '')): |
| 838 self.assertEquals( | 838 self.assertEquals( |
| 839 1, self.device.KillAll('some.process', blocking=False)) | 839 1, self.device.KillAll('some.process', blocking=False)) |
| 840 | 840 |
| 841 def testKillAll_blocking(self): | 841 def testKillAll_blocking(self): |
| 842 with self.assertCalls( | 842 with self.assertCalls( |
| 843 (self.call.device.GetPids('some.process'), {'some.process': '1234'}), | 843 (self.call.device.GetPids('some.process'), {'some.process': ['1234']}), |
| 844 (self.call.adb.Shell('kill -9 1234'), ''), | 844 (self.call.adb.Shell('kill -9 1234'), ''), |
| 845 (self.call.device.GetPids('some.process'), {'some.process': '1234'}), | 845 (self.call.device.GetPids('some.process'), {'some.process': ['1234']}), |
| 846 (self.call.device.GetPids('some.process'), [])): | 846 (self.call.device.GetPids('some.process'), [])): |
| 847 self.assertEquals( | 847 self.assertEquals( |
| 848 1, self.device.KillAll('some.process', blocking=True)) | 848 1, self.device.KillAll('some.process', blocking=True)) |
| 849 | 849 |
| 850 def testKillAll_root(self): | 850 def testKillAll_root(self): |
| 851 with self.assertCalls( | 851 with self.assertCalls( |
| 852 (self.call.device.GetPids('some.process'), {'some.process': '1234'}), | 852 (self.call.device.GetPids('some.process'), {'some.process': ['1234']}), |
| 853 (self.call.device.NeedsSU(), True), | 853 (self.call.device.NeedsSU(), True), |
| 854 (self.call.adb.Shell("su -c sh -c 'kill -9 1234'"), '')): | 854 (self.call.adb.Shell("su -c sh -c 'kill -9 1234'"), '')): |
| 855 self.assertEquals( | 855 self.assertEquals( |
| 856 1, self.device.KillAll('some.process', as_root=True)) | 856 1, self.device.KillAll('some.process', as_root=True)) |
| 857 | 857 |
| 858 def testKillAll_sigterm(self): | 858 def testKillAll_sigterm(self): |
| 859 with self.assertCalls( | 859 with self.assertCalls( |
| 860 (self.call.device.GetPids('some.process'), {'some.process': '1234'}), | 860 (self.call.device.GetPids('some.process'), |
| 861 {'some.process': ['1234']}), |
| 861 (self.call.adb.Shell('kill -15 1234'), '')): | 862 (self.call.adb.Shell('kill -15 1234'), '')): |
| 862 self.assertEquals( | 863 self.assertEquals( |
| 863 1, self.device.KillAll('some.process', signum=device_signal.SIGTERM)) | 864 1, self.device.KillAll('some.process', signum=device_signal.SIGTERM)) |
| 864 | 865 |
| 866 def testKillAll_multipleInstances(self): |
| 867 with self.assertCalls( |
| 868 (self.call.device.GetPids('some.process'), |
| 869 {'some.process': ['1234', '4567']}), |
| 870 (self.call.adb.Shell('kill -15 1234 4567'), '')): |
| 871 self.assertEquals( |
| 872 2, self.device.KillAll('some.process', signum=device_signal.SIGTERM)) |
| 873 |
| 865 | 874 |
| 866 class DeviceUtilsStartActivityTest(DeviceUtilsTest): | 875 class DeviceUtilsStartActivityTest(DeviceUtilsTest): |
| 867 | 876 |
| 868 def testStartActivity_actionOnly(self): | 877 def testStartActivity_actionOnly(self): |
| 869 test_intent = intent.Intent(action='android.intent.action.VIEW') | 878 test_intent = intent.Intent(action='android.intent.action.VIEW') |
| 870 with self.assertCall( | 879 with self.assertCall( |
| 871 self.call.adb.Shell('am start ' | 880 self.call.adb.Shell('am start ' |
| 872 '-a android.intent.action.VIEW'), | 881 '-a android.intent.action.VIEW'), |
| 873 'Starting: Intent { act=android.intent.action.VIEW }'): | 882 'Starting: Intent { act=android.intent.action.VIEW }'): |
| 874 self.device.StartActivity(test_intent) | 883 self.device.StartActivity(test_intent) |
| (...skipping 790 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1665 def testGetPids_noMatches(self): | 1674 def testGetPids_noMatches(self): |
| 1666 with self.assertCall( | 1675 with self.assertCall( |
| 1667 self.call.device._RunPipedShellCommand('ps | grep -F does.not.match'), | 1676 self.call.device._RunPipedShellCommand('ps | grep -F does.not.match'), |
| 1668 []): | 1677 []): |
| 1669 self.assertEqual({}, self.device.GetPids('does.not.match')) | 1678 self.assertEqual({}, self.device.GetPids('does.not.match')) |
| 1670 | 1679 |
| 1671 def testGetPids_oneMatch(self): | 1680 def testGetPids_oneMatch(self): |
| 1672 with self.assertCall( | 1681 with self.assertCall( |
| 1673 self.call.device._RunPipedShellCommand('ps | grep -F one.match'), | 1682 self.call.device._RunPipedShellCommand('ps | grep -F one.match'), |
| 1674 ['user 1001 100 1024 1024 ffffffff 00000000 one.match']): | 1683 ['user 1001 100 1024 1024 ffffffff 00000000 one.match']): |
| 1675 self.assertEqual({'one.match': '1001'}, self.device.GetPids('one.match')) | 1684 self.assertEqual( |
| 1685 {'one.match': ['1001']}, |
| 1686 self.device.GetPids('one.match')) |
| 1676 | 1687 |
| 1677 def testGetPids_mutlipleMatches(self): | 1688 def testGetPids_multipleMatches(self): |
| 1678 with self.assertCall( | 1689 with self.assertCall( |
| 1679 self.call.device._RunPipedShellCommand('ps | grep -F match'), | 1690 self.call.device._RunPipedShellCommand('ps | grep -F match'), |
| 1680 ['user 1001 100 1024 1024 ffffffff 00000000 one.match', | 1691 ['user 1001 100 1024 1024 ffffffff 00000000 one.match', |
| 1681 'user 1002 100 1024 1024 ffffffff 00000000 two.match', | 1692 'user 1002 100 1024 1024 ffffffff 00000000 two.match', |
| 1682 'user 1003 100 1024 1024 ffffffff 00000000 three.match']): | 1693 'user 1003 100 1024 1024 ffffffff 00000000 three.match']): |
| 1683 self.assertEqual( | 1694 self.assertEqual( |
| 1684 {'one.match': '1001', 'two.match': '1002', 'three.match': '1003'}, | 1695 {'one.match': ['1001'], |
| 1696 'two.match': ['1002'], |
| 1697 'three.match': ['1003']}, |
| 1685 self.device.GetPids('match')) | 1698 self.device.GetPids('match')) |
| 1686 | 1699 |
| 1687 def testGetPids_exactMatch(self): | 1700 def testGetPids_exactMatch(self): |
| 1688 with self.assertCall( | 1701 with self.assertCall( |
| 1689 self.call.device._RunPipedShellCommand('ps | grep -F exact.match'), | 1702 self.call.device._RunPipedShellCommand('ps | grep -F exact.match'), |
| 1690 ['user 1000 100 1024 1024 ffffffff 00000000 not.exact.match', | 1703 ['user 1000 100 1024 1024 ffffffff 00000000 not.exact.match', |
| 1691 'user 1234 100 1024 1024 ffffffff 00000000 exact.match']): | 1704 'user 1234 100 1024 1024 ffffffff 00000000 exact.match']): |
| 1692 self.assertEqual( | 1705 self.assertEqual( |
| 1693 {'not.exact.match': '1000', 'exact.match': '1234'}, | 1706 {'not.exact.match': ['1000'], 'exact.match': ['1234']}, |
| 1694 self.device.GetPids('exact.match')) | 1707 self.device.GetPids('exact.match')) |
| 1695 | 1708 |
| 1696 def testGetPids_quotable(self): | 1709 def testGetPids_quotable(self): |
| 1697 with self.assertCall( | 1710 with self.assertCall( |
| 1698 self.call.device._RunPipedShellCommand("ps | grep -F 'my$process'"), | 1711 self.call.device._RunPipedShellCommand("ps | grep -F 'my$process'"), |
| 1699 ['user 1234 100 1024 1024 ffffffff 00000000 my$process']): | 1712 ['user 1234 100 1024 1024 ffffffff 00000000 my$process']): |
| 1700 self.assertEqual( | 1713 self.assertEqual( |
| 1701 {'my$process': '1234'}, self.device.GetPids('my$process')) | 1714 {'my$process': ['1234']}, self.device.GetPids('my$process')) |
| 1715 |
| 1716 def testGetPids_multipleInstances(self): |
| 1717 with self.assertCall( |
| 1718 self.call.device._RunPipedShellCommand('ps | grep -F foo'), |
| 1719 ['user 1000 100 1024 1024 ffffffff 00000000 foo', |
| 1720 'user 1234 100 1024 1024 ffffffff 00000000 foo']): |
| 1721 self.assertEqual( |
| 1722 {'foo': ['1000', '1234']}, |
| 1723 self.device.GetPids('foo')) |
| 1702 | 1724 |
| 1703 | 1725 |
| 1704 class DeviceUtilsTakeScreenshotTest(DeviceUtilsTest): | 1726 class DeviceUtilsTakeScreenshotTest(DeviceUtilsTest): |
| 1705 | 1727 |
| 1706 def testTakeScreenshot_fileNameProvided(self): | 1728 def testTakeScreenshot_fileNameProvided(self): |
| 1707 with self.assertCalls( | 1729 with self.assertCalls( |
| 1708 (mock.call.pylib.utils.device_temp_file.DeviceTempFile( | 1730 (mock.call.pylib.utils.device_temp_file.DeviceTempFile( |
| 1709 self.adb, suffix='.png'), | 1731 self.adb, suffix='.png'), |
| 1710 MockTempFile('/tmp/path/temp-123.png')), | 1732 MockTempFile('/tmp/path/temp-123.png')), |
| 1711 (self.call.adb.Shell('/system/bin/screencap -p /tmp/path/temp-123.png'), | 1733 (self.call.adb.Shell('/system/bin/screencap -p /tmp/path/temp-123.png'), |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1843 devices = device_utils.DeviceUtils.HealthyDevices() | 1865 devices = device_utils.DeviceUtils.HealthyDevices() |
| 1844 self.assertEquals(1, len(devices)) | 1866 self.assertEquals(1, len(devices)) |
| 1845 self.assertTrue(isinstance(devices[0], device_utils.DeviceUtils)) | 1867 self.assertTrue(isinstance(devices[0], device_utils.DeviceUtils)) |
| 1846 self.assertEquals('0123456789abcdef', devices[0].adb.GetDeviceSerial()) | 1868 self.assertEquals('0123456789abcdef', devices[0].adb.GetDeviceSerial()) |
| 1847 | 1869 |
| 1848 | 1870 |
| 1849 if __name__ == '__main__': | 1871 if __name__ == '__main__': |
| 1850 logging.getLogger().setLevel(logging.DEBUG) | 1872 logging.getLogger().setLevel(logging.DEBUG) |
| 1851 unittest.main(verbosity=2) | 1873 unittest.main(verbosity=2) |
| 1852 | 1874 |
| OLD | NEW |