| 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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 d = device_utils.DeviceUtils(None) | 65 d = device_utils.DeviceUtils(None) |
| 66 self.assertIsNone(d.old_interface.GetDevice()) | 66 self.assertIsNone(d.old_interface.GetDevice()) |
| 67 | 67 |
| 68 | 68 |
| 69 class _PatchedFunction(object): | 69 class _PatchedFunction(object): |
| 70 def __init__(self, patched=None, mocked=None): | 70 def __init__(self, patched=None, mocked=None): |
| 71 self.patched = patched | 71 self.patched = patched |
| 72 self.mocked = mocked | 72 self.mocked = mocked |
| 73 | 73 |
| 74 | 74 |
| 75 class MockFileSystem(object): |
| 76 |
| 77 @staticmethod |
| 78 def osStatResult( |
| 79 st_mode=None, st_ino=None, st_dev=None, st_nlink=None, st_uid=None, |
| 80 st_gid=None, st_size=None, st_atime=None, st_mtime=None, st_ctime=None): |
| 81 MockOSStatResult = collections.namedtuple('MockOSStatResult', [ |
| 82 'st_mode', 'st_ino', 'st_dev', 'st_nlink', 'st_uid', 'st_gid', |
| 83 'st_size', 'st_atime', 'st_mtime', 'st_ctime']) |
| 84 return MockOSStatResult(st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid, |
| 85 st_size, st_atime, st_mtime, st_ctime) |
| 86 |
| 87 MOCKED_FUNCTIONS = [ |
| 88 ('os.path.abspath', ''), |
| 89 ('os.path.dirname', ''), |
| 90 ('os.path.exists', False), |
| 91 ('os.path.getsize', 0), |
| 92 ('os.path.isdir', False), |
| 93 ('os.stat', osStatResult.__func__()), |
| 94 ('os.walk', []), |
| 95 ] |
| 96 |
| 97 def _get(self, mocked, path, default_val): |
| 98 if self._verbose: |
| 99 logging.debug('%s(%s)' % (mocked, path)) |
| 100 return (self.mock_file_info[path][mocked] |
| 101 if path in self.mock_file_info |
| 102 else default_val) |
| 103 |
| 104 def _patched(self, target, default_val=None): |
| 105 r = lambda f: self._get(target, f, default_val) |
| 106 return _PatchedFunction(patched=mock.patch(target, side_effect=r)) |
| 107 |
| 108 def __init__(self, verbose=False): |
| 109 self.mock_file_info = {} |
| 110 self._patched_functions = [ |
| 111 self._patched(m, d) for m, d in type(self).MOCKED_FUNCTIONS] |
| 112 self._verbose = verbose |
| 113 |
| 114 def addMockFile(self, path, **kw): |
| 115 self._addMockThing(path, False, **kw) |
| 116 |
| 117 def addMockDirectory(self, path, **kw): |
| 118 self._addMockThing(path, True, **kw) |
| 119 |
| 120 def _addMockThing(self, path, is_dir, size=0, stat=None, walk=None): |
| 121 if stat is None: |
| 122 stat = self.osStatResult() |
| 123 if walk is None: |
| 124 walk = [] |
| 125 self.mock_file_info[path] = { |
| 126 'os.path.abspath': path, |
| 127 'os.path.dirname': '/' + '/'.join(path.strip('/').split('/')[:-1]), |
| 128 'os.path.exists': True, |
| 129 'os.path.isdir': is_dir, |
| 130 'os.path.getsize': size, |
| 131 'os.stat': stat, |
| 132 'os.walk': walk, |
| 133 } |
| 134 |
| 135 def __enter__(self): |
| 136 for p in self._patched_functions: |
| 137 p.mocked = p.patched.__enter__() |
| 138 |
| 139 def __exit__(self, exc_type, exc_val, exc_tb): |
| 140 for p in self._patched_functions: |
| 141 p.patched.__exit__() |
| 142 |
| 143 |
| 75 class DeviceUtilsOldImplTest(unittest.TestCase): | 144 class DeviceUtilsOldImplTest(unittest.TestCase): |
| 76 | 145 |
| 77 class AndroidCommandsCalls(object): | 146 class AndroidCommandsCalls(object): |
| 78 | 147 |
| 79 def __init__(self, test_case, cmd_ret, comp): | 148 def __init__(self, test_case, cmd_ret, comp): |
| 80 self._cmds = cmd_ret | 149 self._cmds = cmd_ret |
| 81 self._comp = comp | 150 self._comp = comp |
| 82 self._run_command = _PatchedFunction() | 151 self._run_command = _PatchedFunction() |
| 83 self._test_case = test_case | 152 self._test_case = test_case |
| 84 self._total_received = 0 | 153 self._total_received = 0 |
| (...skipping 695 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 780 | 849 |
| 781 def testSendKeyEvent(self): | 850 def testSendKeyEvent(self): |
| 782 with self.assertCalls( | 851 with self.assertCalls( |
| 783 "adb -s 0123456789abcdef shell 'input keyevent 66'", | 852 "adb -s 0123456789abcdef shell 'input keyevent 66'", |
| 784 ''): | 853 ''): |
| 785 self.device.SendKeyEvent(66) | 854 self.device.SendKeyEvent(66) |
| 786 | 855 |
| 787 | 856 |
| 788 class DeviceUtilsPushChangedFilesTest(DeviceUtilsOldImplTest): | 857 class DeviceUtilsPushChangedFilesTest(DeviceUtilsOldImplTest): |
| 789 | 858 |
| 790 class MockFileSystem(object): | |
| 791 | |
| 792 @staticmethod | |
| 793 def osStatResult( | |
| 794 st_mode=None, st_ino=None, st_dev=None, st_nlink=None, st_uid=None, | |
| 795 st_gid=None, st_size=None, st_atime=None, st_mtime=None, st_ctime=None): | |
| 796 MockOSStatResult = collections.namedtuple('MockOSStatResult', [ | |
| 797 'st_mode', 'st_ino', 'st_dev', 'st_nlink', 'st_uid', 'st_gid', | |
| 798 'st_size', 'st_atime', 'st_mtime', 'st_ctime']) | |
| 799 return MockOSStatResult(st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid, | |
| 800 st_size, st_atime, st_mtime, st_ctime) | |
| 801 | |
| 802 def _get(self, mocked, path, default_val): | |
| 803 return (self.mock_file_info[path][mocked] | |
| 804 if path in self.mock_file_info | |
| 805 else default_val) | |
| 806 | |
| 807 def _patched(self, target, default_val=None): | |
| 808 r = lambda f: self._get(target, f, default_val) | |
| 809 return _PatchedFunction(patched=mock.patch(target, side_effect=r)) | |
| 810 | |
| 811 def __init__(self): | |
| 812 self.mock_file_info = {} | |
| 813 self._os_path_exists = self._patched('os.path.exists', default_val=False) | |
| 814 self._os_path_getsize = self._patched('os.path.getsize', default_val=0) | |
| 815 self._os_path_isdir = self._patched('os.path.isdir', default_val=False) | |
| 816 self._os_stat = self._patched('os.stat', default_val=self.osStatResult()) | |
| 817 self._os_walk = self._patched('os.walk', default_val=[]) | |
| 818 | |
| 819 def addMockFile(self, path, size, **kw): | |
| 820 self._addMockThing(path, size, False, **kw) | |
| 821 | |
| 822 def addMockDirectory(self, path, size, **kw): | |
| 823 self._addMockThing(path, size, True, **kw) | |
| 824 | |
| 825 def _addMockThing(self, path, size, is_dir, stat=None, walk=None): | |
| 826 if stat is None: | |
| 827 stat = self.osStatResult() | |
| 828 if walk is None: | |
| 829 walk = [] | |
| 830 self.mock_file_info[path] = { | |
| 831 'os.path.exists': True, | |
| 832 'os.path.isdir': is_dir, | |
| 833 'os.path.getsize': size, | |
| 834 'os.stat': stat, | |
| 835 'os.walk': walk, | |
| 836 } | |
| 837 | |
| 838 def __enter__(self): | |
| 839 self._os_path_exists.mocked = self._os_path_exists.patched.__enter__() | |
| 840 self._os_path_getsize.mocked = self._os_path_getsize.patched.__enter__() | |
| 841 self._os_path_isdir.mocked = self._os_path_isdir.patched.__enter__() | |
| 842 self._os_stat.mocked = self._os_stat.patched.__enter__() | |
| 843 self._os_walk.mocked = self._os_walk.patched.__enter__() | |
| 844 | |
| 845 def __exit__(self, exc_type, exc_val, exc_tb): | |
| 846 self._os_walk.patched.__exit__() | |
| 847 self._os_stat.patched.__exit__() | |
| 848 self._os_path_isdir.patched.__exit__(exc_type, exc_val, exc_tb) | |
| 849 self._os_path_getsize.patched.__exit__(exc_type, exc_val, exc_tb) | |
| 850 self._os_path_exists.patched.__exit__(exc_type, exc_val, exc_tb) | |
| 851 | |
| 852 | 859 |
| 853 def testPushChangedFiles_noHostPath(self): | 860 def testPushChangedFiles_noHostPath(self): |
| 854 with mock.patch('os.path.exists', return_value=False): | 861 with mock.patch('os.path.exists', return_value=False): |
| 855 with self.assertRaises(device_errors.CommandFailedError): | 862 with self.assertRaises(device_errors.CommandFailedError): |
| 856 self.device.PushChangedFiles('/test/host/path', '/test/device/path') | 863 self.device.PushChangedFiles('/test/host/path', '/test/device/path') |
| 857 | 864 |
| 858 def testPushChangedFiles_file_noChange(self): | 865 def testPushChangedFiles_file_noChange(self): |
| 859 self.device.old_interface._push_if_needed_cache = {} | 866 self.device.old_interface._push_if_needed_cache = {} |
| 860 | 867 |
| 861 host_file_path = '/test/host/path' | 868 host_file_path = '/test/host/path' |
| 862 device_file_path = '/test/device/path' | 869 device_file_path = '/test/device/path' |
| 863 | 870 |
| 864 mock_fs = self.MockFileSystem() | 871 mock_fs = MockFileSystem() |
| 865 mock_fs.addMockFile(host_file_path, 100) | 872 mock_fs.addMockFile(host_file_path, size=100) |
| 866 | 873 |
| 867 self.device.old_interface.GetFilesChanged = mock.Mock(return_value=[]) | 874 self.device.old_interface.GetFilesChanged = mock.Mock(return_value=[]) |
| 868 | 875 |
| 869 with mock_fs: | 876 with mock_fs: |
| 870 # GetFilesChanged is mocked, so its adb calls are omitted. | 877 # GetFilesChanged is mocked, so its adb calls are omitted. |
| 871 with self.assertNoAdbCalls(): | 878 with self.assertNoAdbCalls(): |
| 872 self.device.PushChangedFiles(host_file_path, device_file_path) | 879 self.device.PushChangedFiles(host_file_path, device_file_path) |
| 873 | 880 |
| 874 def testPushChangedFiles_file_changed(self): | 881 def testPushChangedFiles_file_changed(self): |
| 875 self.device.old_interface._push_if_needed_cache = {} | 882 self.device.old_interface._push_if_needed_cache = {} |
| 876 | 883 |
| 877 host_file_path = '/test/host/path' | 884 host_file_path = '/test/host/path' |
| 878 device_file_path = '/test/device/path' | 885 device_file_path = '/test/device/path' |
| 879 | 886 |
| 880 mock_fs = self.MockFileSystem() | 887 mock_fs = MockFileSystem() |
| 881 mock_fs.addMockFile( | 888 mock_fs.addMockFile( |
| 882 host_file_path, 100, | 889 host_file_path, size=100, |
| 883 stat=self.MockFileSystem.osStatResult(st_mtime=1000000000)) | 890 stat=MockFileSystem.osStatResult(st_mtime=1000000000)) |
| 884 | 891 |
| 885 self.device.old_interface.GetFilesChanged = mock.Mock( | 892 self.device.old_interface.GetFilesChanged = mock.Mock( |
| 886 return_value=[('/test/host/path', '/test/device/path')]) | 893 return_value=[('/test/host/path', '/test/device/path')]) |
| 887 | 894 |
| 888 with mock_fs: | 895 with mock_fs: |
| 889 with self.assertCalls('adb -s 0123456789abcdef push ' | 896 with self.assertCalls('adb -s 0123456789abcdef push ' |
| 890 '/test/host/path /test/device/path', '100 B/s (100 B in 1.000s)\r\n'): | 897 '/test/host/path /test/device/path', '100 B/s (100 B in 1.000s)\r\n'): |
| 891 self.device.PushChangedFiles(host_file_path, device_file_path) | 898 self.device.PushChangedFiles(host_file_path, device_file_path) |
| 892 | 899 |
| 893 def testPushChangedFiles_directory_nothingChanged(self): | 900 def testPushChangedFiles_directory_nothingChanged(self): |
| 894 self.device.old_interface._push_if_needed_cache = {} | 901 self.device.old_interface._push_if_needed_cache = {} |
| 895 | 902 |
| 896 host_file_path = '/test/host/path' | 903 host_file_path = '/test/host/path' |
| 897 device_file_path = '/test/device/path' | 904 device_file_path = '/test/device/path' |
| 898 | 905 |
| 899 mock_fs = self.MockFileSystem() | 906 mock_fs = MockFileSystem() |
| 900 mock_fs.addMockDirectory( | 907 mock_fs.addMockDirectory( |
| 901 host_file_path, 256, | 908 host_file_path, size=256, |
| 902 stat=self.MockFileSystem.osStatResult(st_mtime=1000000000)) | 909 stat=MockFileSystem.osStatResult(st_mtime=1000000000)) |
| 903 mock_fs.addMockFile( | 910 mock_fs.addMockFile( |
| 904 host_file_path + '/file1', 251, | 911 host_file_path + '/file1', size=251, |
| 905 stat=self.MockFileSystem.osStatResult(st_mtime=1000000001)) | 912 stat=MockFileSystem.osStatResult(st_mtime=1000000001)) |
| 906 mock_fs.addMockFile( | 913 mock_fs.addMockFile( |
| 907 host_file_path + '/file2', 252, | 914 host_file_path + '/file2', size=252, |
| 908 stat=self.MockFileSystem.osStatResult(st_mtime=1000000002)) | 915 stat=MockFileSystem.osStatResult(st_mtime=1000000002)) |
| 909 | 916 |
| 910 self.device.old_interface.GetFilesChanged = mock.Mock(return_value=[]) | 917 self.device.old_interface.GetFilesChanged = mock.Mock(return_value=[]) |
| 911 | 918 |
| 912 with mock_fs: | 919 with mock_fs: |
| 913 with self.assertCallsSequence([ | 920 with self.assertCallsSequence([ |
| 914 ("adb -s 0123456789abcdef shell 'mkdir -p \"/test/device/path\"'", | 921 ("adb -s 0123456789abcdef shell 'mkdir -p \"/test/device/path\"'", |
| 915 '')]): | 922 '')]): |
| 916 self.device.PushChangedFiles(host_file_path, device_file_path) | 923 self.device.PushChangedFiles(host_file_path, device_file_path) |
| 917 | 924 |
| 918 def testPushChangedFiles_directory_somethingChanged(self): | 925 def testPushChangedFiles_directory_somethingChanged(self): |
| 919 self.device.old_interface._push_if_needed_cache = {} | 926 self.device.old_interface._push_if_needed_cache = {} |
| 920 | 927 |
| 921 host_file_path = '/test/host/path' | 928 host_file_path = '/test/host/path' |
| 922 device_file_path = '/test/device/path' | 929 device_file_path = '/test/device/path' |
| 923 | 930 |
| 924 mock_fs = self.MockFileSystem() | 931 mock_fs = MockFileSystem() |
| 925 mock_fs.addMockDirectory( | 932 mock_fs.addMockDirectory( |
| 926 host_file_path, 256, | 933 host_file_path, size=256, |
| 927 stat=self.MockFileSystem.osStatResult(st_mtime=1000000000), | 934 stat=MockFileSystem.osStatResult(st_mtime=1000000000), |
| 928 walk=[('/test/host/path', [], ['file1', 'file2'])]) | 935 walk=[('/test/host/path', [], ['file1', 'file2'])]) |
| 929 mock_fs.addMockFile( | 936 mock_fs.addMockFile( |
| 930 host_file_path + '/file1', 256, | 937 host_file_path + '/file1', size=256, |
| 931 stat=self.MockFileSystem.osStatResult(st_mtime=1000000001)) | 938 stat=MockFileSystem.osStatResult(st_mtime=1000000001)) |
| 932 mock_fs.addMockFile( | 939 mock_fs.addMockFile( |
| 933 host_file_path + '/file2', 256, | 940 host_file_path + '/file2', size=256, |
| 934 stat=self.MockFileSystem.osStatResult(st_mtime=1000000002)) | 941 stat=MockFileSystem.osStatResult(st_mtime=1000000002)) |
| 935 | 942 |
| 936 self.device.old_interface.GetFilesChanged = mock.Mock( | 943 self.device.old_interface.GetFilesChanged = mock.Mock( |
| 937 return_value=[('/test/host/path/file1', '/test/device/path/file1')]) | 944 return_value=[('/test/host/path/file1', '/test/device/path/file1')]) |
| 938 | 945 |
| 939 with mock_fs: | 946 with mock_fs: |
| 940 with self.assertCallsSequence([ | 947 with self.assertCallsSequence([ |
| 941 ("adb -s 0123456789abcdef shell 'mkdir -p \"/test/device/path\"'", | 948 ("adb -s 0123456789abcdef shell 'mkdir -p \"/test/device/path\"'", |
| 942 ''), | 949 ''), |
| 943 ('adb -s 0123456789abcdef push ' | 950 ('adb -s 0123456789abcdef push ' |
| 944 '/test/host/path/file1 /test/device/path/file1', | 951 '/test/host/path/file1 /test/device/path/file1', |
| 945 '256 B/s (256 B in 1.000s)\r\n')]): | 952 '256 B/s (256 B in 1.000s)\r\n')]): |
| 946 self.device.PushChangedFiles(host_file_path, device_file_path) | 953 self.device.PushChangedFiles(host_file_path, device_file_path) |
| 947 | 954 |
| 948 def testPushChangedFiles_directory_everythingChanged(self): | 955 def testPushChangedFiles_directory_everythingChanged(self): |
| 949 self.device.old_interface._push_if_needed_cache = {} | 956 self.device.old_interface._push_if_needed_cache = {} |
| 950 | 957 |
| 951 host_file_path = '/test/host/path' | 958 host_file_path = '/test/host/path' |
| 952 device_file_path = '/test/device/path' | 959 device_file_path = '/test/device/path' |
| 953 | 960 |
| 954 mock_fs = self.MockFileSystem() | 961 mock_fs = MockFileSystem() |
| 955 mock_fs.addMockDirectory( | 962 mock_fs.addMockDirectory( |
| 956 host_file_path, 256, | 963 host_file_path, size=256, |
| 957 stat=self.MockFileSystem.osStatResult(st_mtime=1000000000)) | 964 stat=MockFileSystem.osStatResult(st_mtime=1000000000)) |
| 958 mock_fs.addMockFile( | 965 mock_fs.addMockFile( |
| 959 host_file_path + '/file1', 256, | 966 host_file_path + '/file1', size=256, |
| 960 stat=self.MockFileSystem.osStatResult(st_mtime=1000000001)) | 967 stat=MockFileSystem.osStatResult(st_mtime=1000000001)) |
| 961 mock_fs.addMockFile( | 968 mock_fs.addMockFile( |
| 962 host_file_path + '/file2', 256, | 969 host_file_path + '/file2', size=256, |
| 963 stat=self.MockFileSystem.osStatResult(st_mtime=1000000002)) | 970 stat=MockFileSystem.osStatResult(st_mtime=1000000002)) |
| 964 | 971 |
| 965 self.device.old_interface.GetFilesChanged = mock.Mock( | 972 self.device.old_interface.GetFilesChanged = mock.Mock( |
| 966 return_value=[('/test/host/path/file1', '/test/device/path/file1'), | 973 return_value=[('/test/host/path/file1', '/test/device/path/file1'), |
| 967 ('/test/host/path/file2', '/test/device/path/file2')]) | 974 ('/test/host/path/file2', '/test/device/path/file2')]) |
| 968 | 975 |
| 969 with mock_fs: | 976 with mock_fs: |
| 970 with self.assertCallsSequence([ | 977 with self.assertCallsSequence([ |
| 971 ("adb -s 0123456789abcdef shell 'mkdir -p \"/test/device/path\"'", | 978 ("adb -s 0123456789abcdef shell 'mkdir -p \"/test/device/path\"'", |
| 972 ''), | 979 ''), |
| 973 ('adb -s 0123456789abcdef push /test/host/path /test/device/path', | 980 ('adb -s 0123456789abcdef push /test/host/path /test/device/path', |
| (...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1349 class DeviceUtilsSetPropTest(DeviceUtilsOldImplTest): | 1356 class DeviceUtilsSetPropTest(DeviceUtilsOldImplTest): |
| 1350 | 1357 |
| 1351 def testSetProp(self): | 1358 def testSetProp(self): |
| 1352 with self.assertCalls( | 1359 with self.assertCalls( |
| 1353 'adb -s 0123456789abcdef shell ' | 1360 'adb -s 0123456789abcdef shell ' |
| 1354 'setprop this.is.a.test.property "test_property_value"', | 1361 'setprop this.is.a.test.property "test_property_value"', |
| 1355 ''): | 1362 ''): |
| 1356 self.device.SetProp('this.is.a.test.property', 'test_property_value') | 1363 self.device.SetProp('this.is.a.test.property', 'test_property_value') |
| 1357 | 1364 |
| 1358 | 1365 |
| 1366 class DeviceUtilsGetPidsTest(DeviceUtilsOldImplTest): |
| 1367 |
| 1368 def testGetPids_noMatches(self): |
| 1369 with self.assertCalls( |
| 1370 "adb -s 0123456789abcdef shell 'ps'", |
| 1371 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
| 1372 'user 1000 100 1024 1024 ffffffff 00000000 no.match\r\n'): |
| 1373 self.assertEqual({}, self.device.GetPids('does.not.match')) |
| 1374 |
| 1375 def testGetPids_oneMatch(self): |
| 1376 with self.assertCalls( |
| 1377 "adb -s 0123456789abcdef shell 'ps'", |
| 1378 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
| 1379 'user 1000 100 1024 1024 ffffffff 00000000 not.a.match\r\n' |
| 1380 'user 1001 100 1024 1024 ffffffff 00000000 one.match\r\n'): |
| 1381 self.assertEqual({'one.match': '1001'}, self.device.GetPids('one.match')) |
| 1382 |
| 1383 def testGetPids_mutlipleMatches(self): |
| 1384 with self.assertCalls( |
| 1385 "adb -s 0123456789abcdef shell 'ps'", |
| 1386 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
| 1387 'user 1000 100 1024 1024 ffffffff 00000000 not\r\n' |
| 1388 'user 1001 100 1024 1024 ffffffff 00000000 one.match\r\n' |
| 1389 'user 1002 100 1024 1024 ffffffff 00000000 two.match\r\n' |
| 1390 'user 1003 100 1024 1024 ffffffff 00000000 three.match\r\n'): |
| 1391 self.assertEqual( |
| 1392 {'one.match': '1001', 'two.match': '1002', 'three.match': '1003'}, |
| 1393 self.device.GetPids('match')) |
| 1394 |
| 1395 def testGetPids_exactMatch(self): |
| 1396 with self.assertCalls( |
| 1397 "adb -s 0123456789abcdef shell 'ps'", |
| 1398 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
| 1399 'user 1000 100 1024 1024 ffffffff 00000000 not.exact.match\r\n' |
| 1400 'user 1234 100 1024 1024 ffffffff 00000000 exact.match\r\n'): |
| 1401 self.assertEqual( |
| 1402 {'not.exact.match': '1000', 'exact.match': '1234'}, |
| 1403 self.device.GetPids('exact.match')) |
| 1404 |
| 1405 |
| 1406 class DeviceUtilsTakeScreenshotTest(DeviceUtilsOldImplTest): |
| 1407 |
| 1408 def testTakeScreenshot_fileNameProvided(self): |
| 1409 mock_fs = MockFileSystem() |
| 1410 mock_fs.addMockDirectory('/test/host') |
| 1411 mock_fs.addMockFile('/test/host/screenshot.png') |
| 1412 |
| 1413 with mock_fs: |
| 1414 with self.assertCallsSequence( |
| 1415 cmd_ret=[ |
| 1416 (r"adb -s 0123456789abcdef shell 'echo \$EXTERNAL_STORAGE'", |
| 1417 '/test/external/storage\r\n'), |
| 1418 (r"adb -s 0123456789abcdef shell '/system/bin/screencap -p \S+'", |
| 1419 ''), |
| 1420 (r"adb -s 0123456789abcdef shell ls \S+", |
| 1421 '/test/external/storage/screenshot.png\r\n'), |
| 1422 (r'adb -s 0123456789abcdef pull \S+ /test/host/screenshot.png', |
| 1423 '100 B/s (100 B in 1.000s)\r\n'), |
| 1424 (r"adb -s 0123456789abcdef shell 'rm -f \S+'", '') |
| 1425 ], |
| 1426 comp=re.match): |
| 1427 self.device.TakeScreenshot('/test/host/screenshot.png') |
| 1428 |
| 1429 |
| 1430 class DeviceUtilsGetIOStatsTest(DeviceUtilsOldImplTest): |
| 1431 |
| 1432 def testGetIOStats(self): |
| 1433 with self.assertCalls( |
| 1434 "adb -s 0123456789abcdef shell 'cat \"/proc/diskstats\" 2>/dev/null'", |
| 1435 '179 0 mmcblk0 1 2 3 4 5 6 7 8 9 10 11\r\n'): |
| 1436 self.assertEqual( |
| 1437 { |
| 1438 'num_reads': 1, |
| 1439 'num_writes': 5, |
| 1440 'read_ms': 4, |
| 1441 'write_ms': 8, |
| 1442 }, |
| 1443 self.device.GetIOStats()) |
| 1444 |
| 1445 |
| 1359 if __name__ == '__main__': | 1446 if __name__ == '__main__': |
| 1360 logging.getLogger().setLevel(logging.DEBUG) | 1447 logging.getLogger().setLevel(logging.DEBUG) |
| 1361 unittest.main(verbosity=2) | 1448 unittest.main(verbosity=2) |
| 1362 | 1449 |
| OLD | NEW |