| 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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 def osStatResult( | 78 def osStatResult( |
| 79 st_mode=None, st_ino=None, st_dev=None, st_nlink=None, st_uid=None, | 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): | 80 st_gid=None, st_size=None, st_atime=None, st_mtime=None, st_ctime=None): |
| 81 MockOSStatResult = collections.namedtuple('MockOSStatResult', [ | 81 MockOSStatResult = collections.namedtuple('MockOSStatResult', [ |
| 82 'st_mode', 'st_ino', 'st_dev', 'st_nlink', 'st_uid', 'st_gid', | 82 'st_mode', 'st_ino', 'st_dev', 'st_nlink', 'st_uid', 'st_gid', |
| 83 'st_size', 'st_atime', 'st_mtime', 'st_ctime']) | 83 'st_size', 'st_atime', 'st_mtime', 'st_ctime']) |
| 84 return MockOSStatResult(st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid, | 84 return MockOSStatResult(st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid, |
| 85 st_size, st_atime, st_mtime, st_ctime) | 85 st_size, st_atime, st_mtime, st_ctime) |
| 86 | 86 |
| 87 MOCKED_FUNCTIONS = [ | 87 MOCKED_FUNCTIONS = [ |
| 88 ('os.listdir', []), | |
| 89 ('os.path.abspath', ''), | 88 ('os.path.abspath', ''), |
| 90 ('os.path.dirname', ''), | 89 ('os.path.dirname', ''), |
| 91 ('os.path.exists', False), | 90 ('os.path.exists', False), |
| 92 ('os.path.getsize', 0), | 91 ('os.path.getsize', 0), |
| 93 ('os.path.isdir', False), | 92 ('os.path.isdir', False), |
| 94 ('os.stat', osStatResult.__func__()), | 93 ('os.stat', osStatResult.__func__()), |
| 95 ('os.walk', []), | 94 ('os.walk', []), |
| 96 ] | 95 ] |
| 97 | 96 |
| 98 def _get(self, mocked, path, default_val): | 97 def _get(self, mocked, path, default_val): |
| (...skipping 12 matching lines...) Expand all Loading... |
| 111 self._patched_functions = [ | 110 self._patched_functions = [ |
| 112 self._patched(m, d) for m, d in type(self).MOCKED_FUNCTIONS] | 111 self._patched(m, d) for m, d in type(self).MOCKED_FUNCTIONS] |
| 113 self._verbose = verbose | 112 self._verbose = verbose |
| 114 | 113 |
| 115 def addMockFile(self, path, **kw): | 114 def addMockFile(self, path, **kw): |
| 116 self._addMockThing(path, False, **kw) | 115 self._addMockThing(path, False, **kw) |
| 117 | 116 |
| 118 def addMockDirectory(self, path, **kw): | 117 def addMockDirectory(self, path, **kw): |
| 119 self._addMockThing(path, True, **kw) | 118 self._addMockThing(path, True, **kw) |
| 120 | 119 |
| 121 def _addMockThing(self, path, is_dir, listdir=None, size=0, stat=None, | 120 def _addMockThing(self, path, is_dir, size=0, stat=None, walk=None): |
| 122 walk=None): | |
| 123 if listdir is None: | |
| 124 listdir = [] | |
| 125 if stat is None: | 121 if stat is None: |
| 126 stat = self.osStatResult() | 122 stat = self.osStatResult() |
| 127 if walk is None: | 123 if walk is None: |
| 128 walk = [] | 124 walk = [] |
| 129 | |
| 130 dirname = os.sep.join(path.rstrip(os.sep).split(os.sep)[:-1]) | |
| 131 if dirname and not dirname in self.mock_file_info: | |
| 132 self._addMockThing(dirname, True) | |
| 133 | |
| 134 self.mock_file_info[path] = { | 125 self.mock_file_info[path] = { |
| 135 'os.listdir': listdir, | |
| 136 'os.path.abspath': path, | 126 'os.path.abspath': path, |
| 137 'os.path.dirname': dirname, | 127 'os.path.dirname': '/' + '/'.join(path.strip('/').split('/')[:-1]), |
| 138 'os.path.exists': True, | 128 'os.path.exists': True, |
| 139 'os.path.isdir': is_dir, | 129 'os.path.isdir': is_dir, |
| 140 'os.path.getsize': size, | 130 'os.path.getsize': size, |
| 141 'os.stat': stat, | 131 'os.stat': stat, |
| 142 'os.walk': walk, | 132 'os.walk': walk, |
| 143 } | 133 } |
| 144 | 134 |
| 145 def __enter__(self): | 135 def __enter__(self): |
| 146 for p in self._patched_functions: | 136 for p in self._patched_functions: |
| 147 p.mocked = p.patched.__enter__() | 137 p.mocked = p.patched.__enter__() |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 return type(self).AndroidCommandsCalls(self, [(cmd, ret)], comp) | 201 return type(self).AndroidCommandsCalls(self, [(cmd, ret)], comp) |
| 212 | 202 |
| 213 def assertCallsSequence(self, cmd_ret, comp=str.__eq__): | 203 def assertCallsSequence(self, cmd_ret, comp=str.__eq__): |
| 214 return type(self).AndroidCommandsCalls(self, cmd_ret, comp) | 204 return type(self).AndroidCommandsCalls(self, cmd_ret, comp) |
| 215 | 205 |
| 216 def setUp(self): | 206 def setUp(self): |
| 217 self.device = device_utils.DeviceUtils( | 207 self.device = device_utils.DeviceUtils( |
| 218 '0123456789abcdef', default_timeout=1, default_retries=0) | 208 '0123456789abcdef', default_timeout=1, default_retries=0) |
| 219 | 209 |
| 220 | 210 |
| 221 class DeviceUtilsNewImplTest(unittest.TestCase): | |
| 222 | |
| 223 def setUp(self): | |
| 224 test_serial = '0123456789abcdef' | |
| 225 self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper) | |
| 226 self.adb.__str__ = mock.Mock(return_value=test_serial) | |
| 227 self.adb.GetDeviceSerial.return_value = test_serial | |
| 228 self.device = device_utils.DeviceUtils( | |
| 229 self.adb, default_timeout=1, default_retries=0) | |
| 230 | |
| 231 | |
| 232 class DeviceUtilsHybridImplTest(DeviceUtilsOldImplTest): | |
| 233 | |
| 234 def setUp(self): | |
| 235 super(DeviceUtilsHybridImplTest, self).setUp() | |
| 236 self.device.adb = self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper) | |
| 237 | |
| 238 | |
| 239 class DeviceUtilsIsOnlineTest(DeviceUtilsOldImplTest): | 211 class DeviceUtilsIsOnlineTest(DeviceUtilsOldImplTest): |
| 240 | 212 |
| 241 def testIsOnline_true(self): | 213 def testIsOnline_true(self): |
| 242 with self.assertCalls('adb -s 0123456789abcdef devices', | 214 with self.assertCalls('adb -s 0123456789abcdef devices', |
| 243 '00123456789abcdef device\r\n'): | 215 '00123456789abcdef device\r\n'): |
| 244 self.assertTrue(self.device.IsOnline()) | 216 self.assertTrue(self.device.IsOnline()) |
| 245 | 217 |
| 246 def testIsOnline_false(self): | 218 def testIsOnline_false(self): |
| 247 with self.assertCalls('adb -s 0123456789abcdef devices', '\r\n'): | 219 with self.assertCalls('adb -s 0123456789abcdef devices', '\r\n'): |
| 248 self.assertFalse(self.device.IsOnline()) | 220 self.assertFalse(self.device.IsOnline()) |
| (...skipping 615 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 864 | 836 |
| 865 class DeviceUtilsSendKeyEventTest(DeviceUtilsOldImplTest): | 837 class DeviceUtilsSendKeyEventTest(DeviceUtilsOldImplTest): |
| 866 | 838 |
| 867 def testSendKeyEvent(self): | 839 def testSendKeyEvent(self): |
| 868 with self.assertCalls( | 840 with self.assertCalls( |
| 869 "adb -s 0123456789abcdef shell 'input keyevent 66'", | 841 "adb -s 0123456789abcdef shell 'input keyevent 66'", |
| 870 ''): | 842 ''): |
| 871 self.device.SendKeyEvent(66) | 843 self.device.SendKeyEvent(66) |
| 872 | 844 |
| 873 | 845 |
| 874 class DeviceUtilsPushChangedFilesIndividuallyTest(DeviceUtilsNewImplTest): | 846 class DeviceUtilsPushChangedFilesTest(DeviceUtilsOldImplTest): |
| 875 | |
| 876 def testPushChangedFilesIndividually_empty(self): | |
| 877 test_files = [] | |
| 878 self.device._PushChangedFilesIndividually(test_files) | |
| 879 self.assertEqual(0, self.adb.Push.call_count) | |
| 880 | |
| 881 def testPushChangedFilesIndividually_single(self): | |
| 882 test_files = [('/test/host/path', '/test/device/path')] | |
| 883 self.device._PushChangedFilesIndividually(test_files) | |
| 884 self.adb.Push.assert_called_once_with( | |
| 885 '/test/host/path', '/test/device/path') | |
| 886 | |
| 887 def testPushChangedFilesIndividually_multiple(self): | |
| 888 test_files = [ | |
| 889 ('/test/host/path/file1', '/test/device/path/file1'), | |
| 890 ('/test/host/path/file2', '/test/device/path/file2')] | |
| 891 self.device._PushChangedFilesIndividually(test_files) | |
| 892 self.assertEqual(2, self.adb.Push.call_count) | |
| 893 self.adb.Push.assert_any_call( | |
| 894 '/test/host/path/file1', '/test/device/path/file1') | |
| 895 self.adb.Push.assert_any_call( | |
| 896 '/test/host/path/file2', '/test/device/path/file2') | |
| 897 | 847 |
| 898 | 848 |
| 899 class DeviceUtilsPushChangedFilesZippedTest(DeviceUtilsHybridImplTest): | 849 def testPushChangedFiles_noHostPath(self): |
| 850 with mock.patch('os.path.exists', return_value=False): |
| 851 with self.assertRaises(device_errors.CommandFailedError): |
| 852 self.device.PushChangedFiles('/test/host/path', '/test/device/path') |
| 900 | 853 |
| 901 def setUp(self): | 854 def testPushChangedFiles_file_noChange(self): |
| 902 super(DeviceUtilsPushChangedFilesZippedTest, self).setUp() | 855 self.device.old_interface._push_if_needed_cache = {} |
| 903 self.original_install_commands = self.device._InstallCommands | |
| 904 self.device._InstallCommands = mock.Mock() | |
| 905 | 856 |
| 906 def testPushChangedFilesZipped_empty(self): | 857 host_file_path = '/test/host/path' |
| 907 test_files = [] | 858 device_file_path = '/test/device/path' |
| 908 self.device._PushChangedFilesZipped(test_files) | |
| 909 self.assertEqual(0, self.adb.Push.call_count) | |
| 910 | 859 |
| 911 def testPushChangedFilesZipped_single(self): | 860 mock_fs = MockFileSystem() |
| 912 test_files = [('/test/host/path/file1', '/test/device/path/file1')] | 861 mock_fs.addMockFile(host_file_path, size=100) |
| 913 | 862 |
| 914 self.device._GetExternalStoragePathImpl = mock.Mock( | 863 self.device.old_interface.GetFilesChanged = mock.Mock(return_value=[]) |
| 915 return_value='/test/device/external_dir') | |
| 916 self.device._IsOnlineImpl = mock.Mock(return_value=True) | |
| 917 self.device._RunShellCommandImpl = mock.Mock() | |
| 918 mock_zip_temp = mock.mock_open() | |
| 919 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip' | |
| 920 with mock.patch('multiprocessing.Process') as mock_zip_proc, ( | |
| 921 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)): | |
| 922 self.device._PushChangedFilesZipped(test_files) | |
| 923 | 864 |
| 924 mock_zip_proc.assert_called_once_with( | 865 with mock_fs: |
| 925 target=device_utils.DeviceUtils._CreateDeviceZip, | 866 # GetFilesChanged is mocked, so its adb calls are omitted. |
| 926 args=('/test/temp/file/tmp.zip', test_files)) | 867 with self.assertNoAdbCalls(): |
| 927 self.adb.Push.assert_called_once_with( | 868 self.device.PushChangedFiles(host_file_path, device_file_path) |
| 928 '/test/temp/file/tmp.zip', '/test/device/external_dir/tmp.zip') | |
| 929 self.assertEqual(2, self.device._RunShellCommandImpl.call_count) | |
| 930 self.device._RunShellCommandImpl.assert_any_call( | |
| 931 ['unzip', '/test/device/external_dir/tmp.zip'], | |
| 932 as_root=True, check_return=True, | |
| 933 env={'PATH': '$PATH:/data/local/tmp/bin'}) | |
| 934 self.device._RunShellCommandImpl.assert_any_call( | |
| 935 ['rm', '/test/device/external_dir/tmp.zip']) | |
| 936 | 869 |
| 937 def testPushChangedFilesZipped_multiple(self): | 870 def testPushChangedFiles_file_changed(self): |
| 938 test_files = [('/test/host/path/file1', '/test/device/path/file1'), | 871 self.device.old_interface._push_if_needed_cache = {} |
| 939 ('/test/host/path/file2', '/test/device/path/file2')] | |
| 940 | 872 |
| 941 self.device._GetExternalStoragePathImpl = mock.Mock( | 873 host_file_path = '/test/host/path' |
| 942 return_value='/test/device/external_dir') | 874 device_file_path = '/test/device/path' |
| 943 self.device._IsOnlineImpl = mock.Mock(return_value=True) | |
| 944 self.device._RunShellCommandImpl = mock.Mock() | |
| 945 mock_zip_temp = mock.mock_open() | |
| 946 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip' | |
| 947 with mock.patch('multiprocessing.Process') as mock_zip_proc, ( | |
| 948 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)): | |
| 949 self.device._PushChangedFilesZipped(test_files) | |
| 950 | 875 |
| 951 mock_zip_proc.assert_called_once_with( | 876 mock_fs = MockFileSystem() |
| 952 target=device_utils.DeviceUtils._CreateDeviceZip, | 877 mock_fs.addMockFile( |
| 953 args=('/test/temp/file/tmp.zip', test_files)) | 878 host_file_path, size=100, |
| 954 self.adb.Push.assert_called_once_with( | 879 stat=MockFileSystem.osStatResult(st_mtime=1000000000)) |
| 955 '/test/temp/file/tmp.zip', '/test/device/external_dir/tmp.zip') | 880 |
| 956 self.assertEqual(2, self.device._RunShellCommandImpl.call_count) | 881 self.device.old_interface.GetFilesChanged = mock.Mock( |
| 957 self.device._RunShellCommandImpl.assert_any_call( | 882 return_value=[('/test/host/path', '/test/device/path')]) |
| 958 ['unzip', '/test/device/external_dir/tmp.zip'], | 883 |
| 959 as_root=True, check_return=True, | 884 with mock_fs: |
| 960 env={'PATH': '$PATH:/data/local/tmp/bin'}) | 885 with self.assertCalls('adb -s 0123456789abcdef push ' |
| 961 self.device._RunShellCommandImpl.assert_any_call( | 886 '/test/host/path /test/device/path', '100 B/s (100 B in 1.000s)\r\n'): |
| 962 ['rm', '/test/device/external_dir/tmp.zip']) | 887 self.device.PushChangedFiles(host_file_path, device_file_path) |
| 888 |
| 889 def testPushChangedFiles_directory_nothingChanged(self): |
| 890 self.device.old_interface._push_if_needed_cache = {} |
| 891 |
| 892 host_file_path = '/test/host/path' |
| 893 device_file_path = '/test/device/path' |
| 894 |
| 895 mock_fs = MockFileSystem() |
| 896 mock_fs.addMockDirectory( |
| 897 host_file_path, size=256, |
| 898 stat=MockFileSystem.osStatResult(st_mtime=1000000000)) |
| 899 mock_fs.addMockFile( |
| 900 host_file_path + '/file1', size=251, |
| 901 stat=MockFileSystem.osStatResult(st_mtime=1000000001)) |
| 902 mock_fs.addMockFile( |
| 903 host_file_path + '/file2', size=252, |
| 904 stat=MockFileSystem.osStatResult(st_mtime=1000000002)) |
| 905 |
| 906 self.device.old_interface.GetFilesChanged = mock.Mock(return_value=[]) |
| 907 |
| 908 with mock_fs: |
| 909 with self.assertCallsSequence([ |
| 910 ("adb -s 0123456789abcdef shell 'mkdir -p \"/test/device/path\"'", |
| 911 '')]): |
| 912 self.device.PushChangedFiles(host_file_path, device_file_path) |
| 913 |
| 914 def testPushChangedFiles_directory_somethingChanged(self): |
| 915 self.device.old_interface._push_if_needed_cache = {} |
| 916 |
| 917 host_file_path = '/test/host/path' |
| 918 device_file_path = '/test/device/path' |
| 919 |
| 920 mock_fs = MockFileSystem() |
| 921 mock_fs.addMockDirectory( |
| 922 host_file_path, size=256, |
| 923 stat=MockFileSystem.osStatResult(st_mtime=1000000000), |
| 924 walk=[('/test/host/path', [], ['file1', 'file2'])]) |
| 925 mock_fs.addMockFile( |
| 926 host_file_path + '/file1', size=256, |
| 927 stat=MockFileSystem.osStatResult(st_mtime=1000000001)) |
| 928 mock_fs.addMockFile( |
| 929 host_file_path + '/file2', size=256, |
| 930 stat=MockFileSystem.osStatResult(st_mtime=1000000002)) |
| 931 |
| 932 self.device.old_interface.GetFilesChanged = mock.Mock( |
| 933 return_value=[('/test/host/path/file1', '/test/device/path/file1')]) |
| 934 |
| 935 with mock_fs: |
| 936 with self.assertCallsSequence([ |
| 937 ("adb -s 0123456789abcdef shell 'mkdir -p \"/test/device/path\"'", |
| 938 ''), |
| 939 ('adb -s 0123456789abcdef push ' |
| 940 '/test/host/path/file1 /test/device/path/file1', |
| 941 '256 B/s (256 B in 1.000s)\r\n')]): |
| 942 self.device.PushChangedFiles(host_file_path, device_file_path) |
| 943 |
| 944 def testPushChangedFiles_directory_everythingChanged(self): |
| 945 self.device.old_interface._push_if_needed_cache = {} |
| 946 |
| 947 host_file_path = '/test/host/path' |
| 948 device_file_path = '/test/device/path' |
| 949 |
| 950 mock_fs = MockFileSystem() |
| 951 mock_fs.addMockDirectory( |
| 952 host_file_path, size=256, |
| 953 stat=MockFileSystem.osStatResult(st_mtime=1000000000)) |
| 954 mock_fs.addMockFile( |
| 955 host_file_path + '/file1', size=256, |
| 956 stat=MockFileSystem.osStatResult(st_mtime=1000000001)) |
| 957 mock_fs.addMockFile( |
| 958 host_file_path + '/file2', size=256, |
| 959 stat=MockFileSystem.osStatResult(st_mtime=1000000002)) |
| 960 |
| 961 self.device.old_interface.GetFilesChanged = mock.Mock( |
| 962 return_value=[('/test/host/path/file1', '/test/device/path/file1'), |
| 963 ('/test/host/path/file2', '/test/device/path/file2')]) |
| 964 |
| 965 with mock_fs: |
| 966 with self.assertCallsSequence([ |
| 967 ("adb -s 0123456789abcdef shell 'mkdir -p \"/test/device/path\"'", |
| 968 ''), |
| 969 ('adb -s 0123456789abcdef push /test/host/path /test/device/path', |
| 970 '768 B/s (768 B in 1.000s)\r\n')]): |
| 971 self.device.PushChangedFiles(host_file_path, device_file_path) |
| 963 | 972 |
| 964 | 973 |
| 965 class DeviceUtilsFileExistsTest(DeviceUtilsOldImplTest): | 974 class DeviceUtilsFileExistsTest(DeviceUtilsOldImplTest): |
| 966 | 975 |
| 967 def testFileExists_usingTest_fileExists(self): | 976 def testFileExists_usingTest_fileExists(self): |
| 968 with self.assertCalls( | 977 with self.assertCalls( |
| 969 "adb -s 0123456789abcdef shell " | 978 "adb -s 0123456789abcdef shell " |
| 970 "'test -e \"/data/app/test.file.exists\"; echo $?'", | 979 "'test -e \"/data/app/test.file.exists\"; echo $?'", |
| 971 '0\r\n'): | 980 '0\r\n'): |
| 972 self.assertTrue(self.device.FileExists('/data/app/test.file.exists')) | 981 self.assertTrue(self.device.FileExists('/data/app/test.file.exists')) |
| (...skipping 538 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1511 self.device = device_utils.DeviceUtils(None) | 1520 self.device = device_utils.DeviceUtils(None) |
| 1512 with self.assertCalls('adb get-serialno', 'unknown'), ( | 1521 with self.assertCalls('adb get-serialno', 'unknown'), ( |
| 1513 self.assertRaises(device_errors.NoDevicesError)): | 1522 self.assertRaises(device_errors.NoDevicesError)): |
| 1514 str(self.device) | 1523 str(self.device) |
| 1515 | 1524 |
| 1516 | 1525 |
| 1517 if __name__ == '__main__': | 1526 if __name__ == '__main__': |
| 1518 logging.getLogger().setLevel(logging.DEBUG) | 1527 logging.getLogger().setLevel(logging.DEBUG) |
| 1519 unittest.main(verbosity=2) | 1528 unittest.main(verbosity=2) |
| 1520 | 1529 |
| OLD | NEW |