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 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
281 def testEnableRoot_rootFails(self): | 281 def testEnableRoot_rootFails(self): |
282 with self.assertCallsSequence([ | 282 with self.assertCallsSequence([ |
283 ('adb -s 0123456789abcdef shell getprop ro.build.type', | 283 ('adb -s 0123456789abcdef shell getprop ro.build.type', |
284 'userdebug\r\n'), | 284 'userdebug\r\n'), |
285 ('adb -s 0123456789abcdef root', 'no\r\n'), | 285 ('adb -s 0123456789abcdef root', 'no\r\n'), |
286 ('adb -s 0123456789abcdef wait-for-device', '')]): | 286 ('adb -s 0123456789abcdef wait-for-device', '')]): |
287 with self.assertRaises(device_errors.CommandFailedError): | 287 with self.assertRaises(device_errors.CommandFailedError): |
288 self.device.EnableRoot() | 288 self.device.EnableRoot() |
289 | 289 |
290 | 290 |
291 class DeviceUtilsGetExternalStoragePathTest(DeviceUtilsOldImplTest): | 291 class DeviceUtilsGetExternalStoragePathTest(DeviceUtilsNewImplTest): |
292 | 292 |
293 def testGetExternalStoragePath_succeeds(self): | 293 def testGetExternalStoragePath_succeeds(self): |
294 fakeStoragePath = '/fake/storage/path' | 294 fakeStoragePath = '/fake/storage/path' |
295 with self.assertCalls( | 295 with mock.patch.object(self.device, '_NewRunShellImpl', |
296 "adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", | 296 return_value='%s\r\n' % fakeStoragePath): |
297 '%s\r\n' % fakeStoragePath): | |
298 self.assertEquals(fakeStoragePath, | 297 self.assertEquals(fakeStoragePath, |
299 self.device.GetExternalStoragePath()) | 298 self.device.GetExternalStoragePath()) |
300 | 299 |
301 def testGetExternalStoragePath_fails(self): | 300 def testGetExternalStoragePath_fails(self): |
302 with self.assertCalls( | 301 with mock.patch.object(self.device, '_NewRunShellImpl', |
jbudorick
2014/10/14 16:55:19
I'm not sure I like mocking here vs mocking betwee
| |
303 "adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", '\r\n'): | 302 return_value='\r\n'): |
304 with self.assertRaises(device_errors.CommandFailedError): | 303 with self.assertRaises(device_errors.CommandFailedError): |
305 self.device.GetExternalStoragePath() | 304 self.device.GetExternalStoragePath() |
306 | 305 |
307 | 306 |
308 class DeviceUtilsWaitUntilFullyBootedTest(DeviceUtilsOldImplTest): | 307 class DeviceUtilsWaitUntilFullyBootedTest(DeviceUtilsOldImplTest): |
309 | 308 |
310 def testWaitUntilFullyBooted_succeedsNoWifi(self): | 309 def testWaitUntilFullyBooted_succeedsNoWifi(self): |
311 with self.assertCallsSequence([ | 310 with self.assertCallsSequence([ |
312 # AndroidCommands.WaitForSystemBootCompleted | 311 # AndroidCommands.WaitForSystemBootCompleted |
313 ('adb -s 0123456789abcdef wait-for-device', ''), | 312 ('adb -s 0123456789abcdef wait-for-device', ''), |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
563 self.device.RunShellCommand('echo $ANDROID_DATA', check_return=True) | 562 self.device.RunShellCommand('echo $ANDROID_DATA', check_return=True) |
564 | 563 |
565 def testRunShellCommand_checkReturn_failure(self): | 564 def testRunShellCommand_checkReturn_failure(self): |
566 with self.assertCalls( | 565 with self.assertCalls( |
567 "adb -s 0123456789abcdef shell 'echo $ANDROID_DATA; echo %$?'", | 566 "adb -s 0123456789abcdef shell 'echo $ANDROID_DATA; echo %$?'", |
568 '\r\n%1\r\n'): | 567 '\r\n%1\r\n'): |
569 with self.assertRaises(device_errors.CommandFailedError): | 568 with self.assertRaises(device_errors.CommandFailedError): |
570 self.device.RunShellCommand('echo $ANDROID_DATA', check_return=True) | 569 self.device.RunShellCommand('echo $ANDROID_DATA', check_return=True) |
571 | 570 |
572 | 571 |
572 class DeviceUtilsNewRunShellImplTest(DeviceUtilsNewImplTest): | |
573 def assert_called_shell_with(self, cmd): | |
574 self.adb.Shell.assert_called_once_with(cmd, expect_rc=0, | |
575 timeout=device_utils._DEFAULT_TIMEOUT, | |
576 retries=device_utils._DEFAULT_RETRIES) | |
577 | |
578 def testNewRunShellImpl_simple(self): | |
579 self.device._NewRunShellImpl('echo hello') | |
580 self.assert_called_shell_with('echo hello') | |
581 | |
582 def testNewRunShellImpl_withlist(self): | |
583 self.device._NewRunShellImpl(['echo', 'hello']) | |
584 self.assert_called_shell_with('echo hello') | |
585 | |
586 def testNewRunShellImpl_withfancylist(self): | |
587 self.device._NewRunShellImpl(['echo', 'hello world']) | |
588 self.assert_called_shell_with("echo 'hello world'") | |
589 | |
590 def testNewRunShellImpl_withenv(self): | |
591 self.device._NewRunShellImpl('echo "$VAR"', env={'VAR': 'some_string'}) | |
592 self.assert_called_shell_with('VAR=some_string echo "$VAR"') | |
593 | |
594 def testNewRunShellImpl_withfancyenv(self): | |
595 self.device._NewRunShellImpl('echo "$VAR"', env={'VAR': 'hello world'}) | |
596 self.assert_called_shell_with('''VAR='hello world' echo "$VAR"''') | |
597 | |
598 def testNewRunShellImpl_withenv_invalid(self): | |
599 with self.assertRaises(KeyError): | |
600 self.device._NewRunShellImpl('some_cmd', env={'INVALID NAME': 'value'}) | |
601 | |
602 def testNewRunShellImpl_withcwd(self): | |
603 self.device._NewRunShellImpl('ls', cwd='/some/test/path') | |
604 self.assert_called_shell_with('cd /some/test/path && ls') | |
605 | |
606 def testNewRunShellImpl_withfancycwd(self): | |
607 self.device._NewRunShellImpl('ls', cwd='/some test/path with/spaces') | |
608 self.assert_called_shell_with("cd '/some test/path with/spaces' && ls") | |
609 | |
610 def testNewRunShellImpl_asroot_withoutroot(self): | |
611 with mock.patch.object(self.device, '_HasRootImpl', return_value=False): | |
612 self.device._NewRunShellImpl('ls /path/to/protected', as_root=True) | |
613 self.assert_called_shell_with('su -c ls /path/to/protected') | |
614 | |
615 def testNewRunShellImpl_asroot_withroot(self): | |
616 with mock.patch.object(self.device, '_HasRootImpl', return_value=True): | |
617 self.device._NewRunShellImpl('ls /path/to/protected', as_root=True) | |
618 self.assert_called_shell_with('ls /path/to/protected') | |
619 | |
620 | |
573 class DeviceUtilsKillAllTest(DeviceUtilsOldImplTest): | 621 class DeviceUtilsKillAllTest(DeviceUtilsOldImplTest): |
574 | 622 |
575 def testKillAll_noMatchingProcesses(self): | 623 def testKillAll_noMatchingProcesses(self): |
576 with self.assertCalls( | 624 with self.assertCalls( |
577 "adb -s 0123456789abcdef shell 'ps'", | 625 "adb -s 0123456789abcdef shell 'ps'", |
578 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n'): | 626 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n'): |
579 with self.assertRaises(device_errors.CommandFailedError): | 627 with self.assertRaises(device_errors.CommandFailedError): |
580 self.device.KillAll('test_process') | 628 self.device.KillAll('test_process') |
581 | 629 |
582 def testKillAll_nonblocking(self): | 630 def testKillAll_nonblocking(self): |
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
904 self.device._InstallCommands = mock.Mock() | 952 self.device._InstallCommands = mock.Mock() |
905 | 953 |
906 def testPushChangedFilesZipped_empty(self): | 954 def testPushChangedFilesZipped_empty(self): |
907 test_files = [] | 955 test_files = [] |
908 self.device._PushChangedFilesZipped(test_files) | 956 self.device._PushChangedFilesZipped(test_files) |
909 self.assertEqual(0, self.adb.Push.call_count) | 957 self.assertEqual(0, self.adb.Push.call_count) |
910 | 958 |
911 def testPushChangedFilesZipped_single(self): | 959 def testPushChangedFilesZipped_single(self): |
912 test_files = [('/test/host/path/file1', '/test/device/path/file1')] | 960 test_files = [('/test/host/path/file1', '/test/device/path/file1')] |
913 | 961 |
914 self.device._GetExternalStoragePathImpl = mock.Mock( | 962 self.device.GetExternalStoragePath = mock.Mock( |
915 return_value='/test/device/external_dir') | 963 return_value='/test/device/external_dir') |
916 self.device._IsOnlineImpl = mock.Mock(return_value=True) | 964 self.device._IsOnlineImpl = mock.Mock(return_value=True) |
917 self.device._RunShellCommandImpl = mock.Mock() | 965 self.device._RunShellCommandImpl = mock.Mock() |
918 mock_zip_temp = mock.mock_open() | 966 mock_zip_temp = mock.mock_open() |
919 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip' | 967 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip' |
920 with mock.patch('multiprocessing.Process') as mock_zip_proc, ( | 968 with mock.patch('multiprocessing.Process') as mock_zip_proc, ( |
921 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)): | 969 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)): |
922 self.device._PushChangedFilesZipped(test_files) | 970 self.device._PushChangedFilesZipped(test_files) |
923 | 971 |
924 mock_zip_proc.assert_called_once_with( | 972 mock_zip_proc.assert_called_once_with( |
925 target=device_utils.DeviceUtils._CreateDeviceZip, | 973 target=device_utils.DeviceUtils._CreateDeviceZip, |
926 args=('/test/temp/file/tmp.zip', test_files)) | 974 args=('/test/temp/file/tmp.zip', test_files)) |
927 self.adb.Push.assert_called_once_with( | 975 self.adb.Push.assert_called_once_with( |
928 '/test/temp/file/tmp.zip', '/test/device/external_dir/tmp.zip') | 976 '/test/temp/file/tmp.zip', '/test/device/external_dir/tmp.zip') |
929 self.assertEqual(2, self.device._RunShellCommandImpl.call_count) | 977 self.assertEqual(2, self.device._RunShellCommandImpl.call_count) |
930 self.device._RunShellCommandImpl.assert_any_call( | 978 self.device._RunShellCommandImpl.assert_any_call( |
931 ['unzip', '/test/device/external_dir/tmp.zip'], | 979 ['unzip', '/test/device/external_dir/tmp.zip'], |
932 as_root=True, check_return=True, | 980 as_root=True, check_return=True, |
933 env={'PATH': '$PATH:/data/local/tmp/bin'}) | 981 env={'PATH': '$PATH:/data/local/tmp/bin'}) |
934 self.device._RunShellCommandImpl.assert_any_call( | 982 self.device._RunShellCommandImpl.assert_any_call( |
935 ['rm', '/test/device/external_dir/tmp.zip']) | 983 ['rm', '/test/device/external_dir/tmp.zip']) |
936 | 984 |
937 def testPushChangedFilesZipped_multiple(self): | 985 def testPushChangedFilesZipped_multiple(self): |
938 test_files = [('/test/host/path/file1', '/test/device/path/file1'), | 986 test_files = [('/test/host/path/file1', '/test/device/path/file1'), |
939 ('/test/host/path/file2', '/test/device/path/file2')] | 987 ('/test/host/path/file2', '/test/device/path/file2')] |
940 | 988 |
941 self.device._GetExternalStoragePathImpl = mock.Mock( | 989 self.device.GetExternalStoragePath = mock.Mock( |
942 return_value='/test/device/external_dir') | 990 return_value='/test/device/external_dir') |
943 self.device._IsOnlineImpl = mock.Mock(return_value=True) | 991 self.device._IsOnlineImpl = mock.Mock(return_value=True) |
944 self.device._RunShellCommandImpl = mock.Mock() | 992 self.device._RunShellCommandImpl = mock.Mock() |
945 mock_zip_temp = mock.mock_open() | 993 mock_zip_temp = mock.mock_open() |
946 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip' | 994 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip' |
947 with mock.patch('multiprocessing.Process') as mock_zip_proc, ( | 995 with mock.patch('multiprocessing.Process') as mock_zip_proc, ( |
948 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)): | 996 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)): |
949 self.device._PushChangedFilesZipped(test_files) | 997 self.device._PushChangedFilesZipped(test_files) |
950 | 998 |
951 mock_zip_proc.assert_called_once_with( | 999 mock_zip_proc.assert_called_once_with( |
(...skipping 559 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1511 self.device = device_utils.DeviceUtils(None) | 1559 self.device = device_utils.DeviceUtils(None) |
1512 with self.assertCalls('adb get-serialno', 'unknown'), ( | 1560 with self.assertCalls('adb get-serialno', 'unknown'), ( |
1513 self.assertRaises(device_errors.NoDevicesError)): | 1561 self.assertRaises(device_errors.NoDevicesError)): |
1514 str(self.device) | 1562 str(self.device) |
1515 | 1563 |
1516 | 1564 |
1517 if __name__ == '__main__': | 1565 if __name__ == '__main__': |
1518 logging.getLogger().setLevel(logging.DEBUG) | 1566 logging.getLogger().setLevel(logging.DEBUG) |
1519 unittest.main(verbosity=2) | 1567 unittest.main(verbosity=2) |
1520 | 1568 |
OLD | NEW |