Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(773)

Side by Side Diff: build/android/pylib/device/device_utils_test.py

Issue 1254843002: telemetry: Fix killing the perf profiler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 805 matching lines...) Expand 10 before | Expand all | Expand 10 after
816 ['foo.bar'] * 256 + ['foo.ba']): 816 ['foo.bar'] * 256 + ['foo.ba']):
817 with self.assertRaises(device_errors.AdbShellCommandFailedError) as ec: 817 with self.assertRaises(device_errors.AdbShellCommandFailedError) as ec:
818 self.device._RunPipedShellCommand('ps | grep foo') 818 self.device._RunPipedShellCommand('ps | grep foo')
819 self.assertIs(None, ec.exception.status) 819 self.assertIs(None, ec.exception.status)
820 820
821 821
822 @mock.patch('time.sleep', mock.Mock()) 822 @mock.patch('time.sleep', mock.Mock())
823 class DeviceUtilsKillAllTest(DeviceUtilsTest): 823 class DeviceUtilsKillAllTest(DeviceUtilsTest):
824 824
825 def testKillAll_noMatchingProcessesFailure(self): 825 def testKillAll_noMatchingProcessesFailure(self):
826 with self.assertCall(self.call.device.GetPids('test_process'), {}): 826 with self.assertCall(
827 self.call.device.GetPids('test_process', multiple_instances=True), {}):
827 with self.assertRaises(device_errors.CommandFailedError): 828 with self.assertRaises(device_errors.CommandFailedError):
828 self.device.KillAll('test_process') 829 self.device.KillAll('test_process')
829 830
830 def testKillAll_noMatchingProcessesQuiet(self): 831 def testKillAll_noMatchingProcessesQuiet(self):
831 with self.assertCall(self.call.device.GetPids('test_process'), {}): 832 with self.assertCall(
833 self.call.device.GetPids('test_process', multiple_instances=True), {}):
832 self.assertEqual(0, self.device.KillAll('test_process', quiet=True)) 834 self.assertEqual(0, self.device.KillAll('test_process', quiet=True))
833 835
834 def testKillAll_nonblocking(self): 836 def testKillAll_nonblocking(self):
835 with self.assertCalls( 837 with self.assertCalls(
836 (self.call.device.GetPids('some.process'), {'some.process': '1234'}), 838 (self.call.device.GetPids('some.process', multiple_instances=True),
839 {'some.process': ['1234']}),
837 (self.call.adb.Shell('kill -9 1234'), '')): 840 (self.call.adb.Shell('kill -9 1234'), '')):
838 self.assertEquals( 841 self.assertEquals(
839 1, self.device.KillAll('some.process', blocking=False)) 842 1, self.device.KillAll('some.process', blocking=False))
840 843
841 def testKillAll_blocking(self): 844 def testKillAll_blocking(self):
842 with self.assertCalls( 845 with self.assertCalls(
843 (self.call.device.GetPids('some.process'), {'some.process': '1234'}), 846 (self.call.device.GetPids('some.process', multiple_instances=True),
847 {'some.process': ['1234']}),
844 (self.call.adb.Shell('kill -9 1234'), ''), 848 (self.call.adb.Shell('kill -9 1234'), ''),
845 (self.call.device.GetPids('some.process'), {'some.process': '1234'}), 849 (self.call.device.GetPids('some.process', multiple_instances=True),
846 (self.call.device.GetPids('some.process'), [])): 850 {'some.process': ['1234']}),
851 (self.call.device.GetPids('some.process', multiple_instances=True),
852 {})):
847 self.assertEquals( 853 self.assertEquals(
848 1, self.device.KillAll('some.process', blocking=True)) 854 1, self.device.KillAll('some.process', blocking=True))
849 855
850 def testKillAll_root(self): 856 def testKillAll_root(self):
851 with self.assertCalls( 857 with self.assertCalls(
852 (self.call.device.GetPids('some.process'), {'some.process': '1234'}), 858 (self.call.device.GetPids('some.process', multiple_instances=True),
859 {'some.process': ['1234']}),
853 (self.call.device.NeedsSU(), True), 860 (self.call.device.NeedsSU(), True),
854 (self.call.adb.Shell("su -c sh -c 'kill -9 1234'"), '')): 861 (self.call.adb.Shell("su -c sh -c 'kill -9 1234'"), '')):
855 self.assertEquals( 862 self.assertEquals(
856 1, self.device.KillAll('some.process', as_root=True)) 863 1, self.device.KillAll('some.process', as_root=True))
857 864
858 def testKillAll_sigterm(self): 865 def testKillAll_sigterm(self):
859 with self.assertCalls( 866 with self.assertCalls(
860 (self.call.device.GetPids('some.process'), {'some.process': '1234'}), 867 (self.call.device.GetPids('some.process', multiple_instances=True),
868 {'some.process': ['1234']}),
861 (self.call.adb.Shell('kill -15 1234'), '')): 869 (self.call.adb.Shell('kill -15 1234'), '')):
862 self.assertEquals( 870 self.assertEquals(
863 1, self.device.KillAll('some.process', signum=device_signal.SIGTERM)) 871 1, self.device.KillAll('some.process', signum=device_signal.SIGTERM))
864 872
873 def testKillAll_multipleInstances(self):
874 with self.assertCalls(
875 (self.call.device.GetPids('some.process', multiple_instances=True),
876 {'some.process': ['1234', '4567']}),
877 (self.call.adb.Shell('kill -15 1234 4567'), '')):
878 self.assertEquals(
879 2, self.device.KillAll('some.process', signum=device_signal.SIGTERM))
880
865 881
866 class DeviceUtilsStartActivityTest(DeviceUtilsTest): 882 class DeviceUtilsStartActivityTest(DeviceUtilsTest):
867 883
868 def testStartActivity_actionOnly(self): 884 def testStartActivity_actionOnly(self):
869 test_intent = intent.Intent(action='android.intent.action.VIEW') 885 test_intent = intent.Intent(action='android.intent.action.VIEW')
870 with self.assertCall( 886 with self.assertCall(
871 self.call.adb.Shell('am start ' 887 self.call.adb.Shell('am start '
872 '-a android.intent.action.VIEW'), 888 '-a android.intent.action.VIEW'),
873 'Starting: Intent { act=android.intent.action.VIEW }'): 889 'Starting: Intent { act=android.intent.action.VIEW }'):
874 self.device.StartActivity(test_intent) 890 self.device.StartActivity(test_intent)
(...skipping 785 matching lines...) Expand 10 before | Expand all | Expand 10 after
1660 self.call.device._RunPipedShellCommand('ps | grep -F does.not.match'), 1676 self.call.device._RunPipedShellCommand('ps | grep -F does.not.match'),
1661 []): 1677 []):
1662 self.assertEqual({}, self.device.GetPids('does.not.match')) 1678 self.assertEqual({}, self.device.GetPids('does.not.match'))
1663 1679
1664 def testGetPids_oneMatch(self): 1680 def testGetPids_oneMatch(self):
1665 with self.assertCall( 1681 with self.assertCall(
1666 self.call.device._RunPipedShellCommand('ps | grep -F one.match'), 1682 self.call.device._RunPipedShellCommand('ps | grep -F one.match'),
1667 ['user 1001 100 1024 1024 ffffffff 00000000 one.match']): 1683 ['user 1001 100 1024 1024 ffffffff 00000000 one.match']):
1668 self.assertEqual({'one.match': '1001'}, self.device.GetPids('one.match')) 1684 self.assertEqual({'one.match': '1001'}, self.device.GetPids('one.match'))
1669 1685
1670 def testGetPids_mutlipleMatches(self): 1686 def testGetPids_multipleMatches(self):
1671 with self.assertCall( 1687 with self.assertCall(
1672 self.call.device._RunPipedShellCommand('ps | grep -F match'), 1688 self.call.device._RunPipedShellCommand('ps | grep -F match'),
1673 ['user 1001 100 1024 1024 ffffffff 00000000 one.match', 1689 ['user 1001 100 1024 1024 ffffffff 00000000 one.match',
1674 'user 1002 100 1024 1024 ffffffff 00000000 two.match', 1690 'user 1002 100 1024 1024 ffffffff 00000000 two.match',
1675 'user 1003 100 1024 1024 ffffffff 00000000 three.match']): 1691 'user 1003 100 1024 1024 ffffffff 00000000 three.match']):
1676 self.assertEqual( 1692 self.assertEqual(
1677 {'one.match': '1001', 'two.match': '1002', 'three.match': '1003'}, 1693 {'one.match': '1001', 'two.match': '1002', 'three.match': '1003'},
1678 self.device.GetPids('match')) 1694 self.device.GetPids('match'))
1679 1695
1680 def testGetPids_exactMatch(self): 1696 def testGetPids_exactMatch(self):
1681 with self.assertCall( 1697 with self.assertCall(
1682 self.call.device._RunPipedShellCommand('ps | grep -F exact.match'), 1698 self.call.device._RunPipedShellCommand('ps | grep -F exact.match'),
1683 ['user 1000 100 1024 1024 ffffffff 00000000 not.exact.match', 1699 ['user 1000 100 1024 1024 ffffffff 00000000 not.exact.match',
1684 'user 1234 100 1024 1024 ffffffff 00000000 exact.match']): 1700 'user 1234 100 1024 1024 ffffffff 00000000 exact.match']):
1685 self.assertEqual( 1701 self.assertEqual(
1686 {'not.exact.match': '1000', 'exact.match': '1234'}, 1702 {'not.exact.match': '1000', 'exact.match': '1234'},
1687 self.device.GetPids('exact.match')) 1703 self.device.GetPids('exact.match'))
1688 1704
1689 def testGetPids_quotable(self): 1705 def testGetPids_quotable(self):
1690 with self.assertCall( 1706 with self.assertCall(
1691 self.call.device._RunPipedShellCommand("ps | grep -F 'my$process'"), 1707 self.call.device._RunPipedShellCommand("ps | grep -F 'my$process'"),
1692 ['user 1234 100 1024 1024 ffffffff 00000000 my$process']): 1708 ['user 1234 100 1024 1024 ffffffff 00000000 my$process']):
1693 self.assertEqual( 1709 self.assertEqual(
1694 {'my$process': '1234'}, self.device.GetPids('my$process')) 1710 {'my$process': '1234'}, self.device.GetPids('my$process'))
1695 1711
1712 def testGetPids_multipleInstances(self):
1713 with self.assertCall(
1714 self.call.device._RunPipedShellCommand("ps | grep -F process"),
1715 ['user 1234 100 1024 1024 ffffffff 00000000 process',
1716 'user 4567 100 1024 1024 ffffffff 00000000 process']):
1717 self.assertEqual(
1718 {'process': ['1234', '4567']},
1719 self.device.GetPids('process', multiple_instances=True))
1720
1696 1721
1697 class DeviceUtilsTakeScreenshotTest(DeviceUtilsTest): 1722 class DeviceUtilsTakeScreenshotTest(DeviceUtilsTest):
1698 1723
1699 def testTakeScreenshot_fileNameProvided(self): 1724 def testTakeScreenshot_fileNameProvided(self):
1700 with self.assertCalls( 1725 with self.assertCalls(
1701 (mock.call.pylib.utils.device_temp_file.DeviceTempFile( 1726 (mock.call.pylib.utils.device_temp_file.DeviceTempFile(
1702 self.adb, suffix='.png'), 1727 self.adb, suffix='.png'),
1703 MockTempFile('/tmp/path/temp-123.png')), 1728 MockTempFile('/tmp/path/temp-123.png')),
1704 (self.call.adb.Shell('/system/bin/screencap -p /tmp/path/temp-123.png'), 1729 (self.call.adb.Shell('/system/bin/screencap -p /tmp/path/temp-123.png'),
1705 ''), 1730 ''),
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
1836 devices = device_utils.DeviceUtils.HealthyDevices() 1861 devices = device_utils.DeviceUtils.HealthyDevices()
1837 self.assertEquals(1, len(devices)) 1862 self.assertEquals(1, len(devices))
1838 self.assertTrue(isinstance(devices[0], device_utils.DeviceUtils)) 1863 self.assertTrue(isinstance(devices[0], device_utils.DeviceUtils))
1839 self.assertEquals('0123456789abcdef', devices[0].adb.GetDeviceSerial()) 1864 self.assertEquals('0123456789abcdef', devices[0].adb.GetDeviceSerial())
1840 1865
1841 1866
1842 if __name__ == '__main__': 1867 if __name__ == '__main__':
1843 logging.getLogger().setLevel(logging.DEBUG) 1868 logging.getLogger().setLevel(logging.DEBUG)
1844 unittest.main(verbosity=2) 1869 unittest.main(verbosity=2)
1845 1870
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698