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 925 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
936 with self.assertCalls( | 936 with self.assertCalls( |
937 self.call.device.RunShellCommand( | 937 self.call.device.RunShellCommand( |
938 ['am', 'instrument', '-e', 'foo', 'Foo', '-e', 'bar', 'Bar', | 938 ['am', 'instrument', '-e', 'foo', 'Foo', '-e', 'bar', 'Bar', |
939 'test.package/.TestInstrumentation'], | 939 'test.package/.TestInstrumentation'], |
940 check_return=True)): | 940 check_return=True)): |
941 self.device.StartInstrumentation( | 941 self.device.StartInstrumentation( |
942 'test.package/.TestInstrumentation', | 942 'test.package/.TestInstrumentation', |
943 finish=False, raw=False, extras={'foo': 'Foo', 'bar': 'Bar'}) | 943 finish=False, raw=False, extras={'foo': 'Foo', 'bar': 'Bar'}) |
944 | 944 |
945 | 945 |
946 class DeviceUtilsBroadcastIntentTest(DeviceUtilsOldImplTest): | 946 class DeviceUtilsBroadcastIntentTest(DeviceUtilsNewImplTest): |
947 | 947 |
948 def testBroadcastIntent_noExtras(self): | 948 def testBroadcastIntent_noExtras(self): |
949 test_intent = intent.Intent(action='test.package.with.an.INTENT') | 949 test_intent = intent.Intent(action='test.package.with.an.INTENT') |
950 with self.assertCalls( | 950 with self.assertCall( |
951 "adb -s 0123456789abcdef shell 'am broadcast " | 951 self.call.adb.Shell('am broadcast -a test.package.with.an.INTENT'), |
952 "-a test.package.with.an.INTENT '", | |
953 'Broadcasting: Intent { act=test.package.with.an.INTENT } '): | 952 'Broadcasting: Intent { act=test.package.with.an.INTENT } '): |
954 self.device.BroadcastIntent(test_intent) | 953 self.device.BroadcastIntent(test_intent) |
955 | 954 |
956 def testBroadcastIntent_withExtra(self): | 955 def testBroadcastIntent_withExtra(self): |
957 test_intent = intent.Intent(action='test.package.with.an.INTENT', | 956 test_intent = intent.Intent(action='test.package.with.an.INTENT', |
958 extras={'foo': 'bar'}) | 957 extras={'foo': 'bar value'}) |
959 with self.assertCalls( | 958 with self.assertCall( |
960 "adb -s 0123456789abcdef shell 'am broadcast " | 959 self.call.adb.Shell( |
961 "-a test.package.with.an.INTENT " | 960 "am broadcast -a test.package.with.an.INTENT -e foo 'bar value'"), |
962 "-e foo \"bar\"'", | |
963 'Broadcasting: Intent { act=test.package.with.an.INTENT } '): | 961 'Broadcasting: Intent { act=test.package.with.an.INTENT } '): |
964 self.device.BroadcastIntent(test_intent) | 962 self.device.BroadcastIntent(test_intent) |
965 | 963 |
966 def testBroadcastIntent_withExtra_noValue(self): | 964 def testBroadcastIntent_withExtra_noValue(self): |
967 test_intent = intent.Intent(action='test.package.with.an.INTENT', | 965 test_intent = intent.Intent(action='test.package.with.an.INTENT', |
968 extras={'foo': None}) | 966 extras={'foo': None}) |
969 with self.assertCalls( | 967 with self.assertCall( |
970 "adb -s 0123456789abcdef shell 'am broadcast " | 968 self.call.adb.Shell( |
971 "-a test.package.with.an.INTENT " | 969 'am broadcast -a test.package.with.an.INTENT -e foo'), |
972 "-e foo'", | |
973 'Broadcasting: Intent { act=test.package.with.an.INTENT } '): | 970 'Broadcasting: Intent { act=test.package.with.an.INTENT } '): |
974 self.device.BroadcastIntent(test_intent) | 971 self.device.BroadcastIntent(test_intent) |
975 | 972 |
976 | 973 |
977 class DeviceUtilsGoHomeTest(DeviceUtilsOldImplTest): | 974 class DeviceUtilsGoHomeTest(DeviceUtilsOldImplTest): |
978 | 975 |
979 def testGoHome(self): | 976 def testGoHome(self): |
980 with self.assertCalls( | 977 with self.assertCalls( |
981 "adb -s 0123456789abcdef shell 'am start " | 978 "adb -s 0123456789abcdef shell 'am start " |
982 "-W " | 979 "-W " |
983 "-a android.intent.action.MAIN " | 980 "-a android.intent.action.MAIN " |
984 "-c android.intent.category.HOME'", | 981 "-c android.intent.category.HOME'", |
985 'Starting: Intent { act=android.intent.action.MAIN }\r\n'): | 982 'Starting: Intent { act=android.intent.action.MAIN }\r\n'): |
986 self.device.GoHome() | 983 self.device.GoHome() |
987 | 984 |
988 | 985 |
989 class DeviceUtilsForceStopTest(DeviceUtilsOldImplTest): | 986 class DeviceUtilsForceStopTest(DeviceUtilsNewImplTest): |
990 | 987 |
991 def testForceStop(self): | 988 def testForceStop(self): |
992 with self.assertCalls( | 989 with self.assertCall( |
993 "adb -s 0123456789abcdef shell 'am force-stop this.is.a.test.package'", | 990 self.call.adb.Shell('am force-stop this.is.a.test.package'), |
994 ''): | 991 ''): |
995 self.device.ForceStop('this.is.a.test.package') | 992 self.device.ForceStop('this.is.a.test.package') |
996 | 993 |
997 | 994 |
998 class DeviceUtilsClearApplicationStateTest(DeviceUtilsOldImplTest): | 995 class DeviceUtilsClearApplicationStateTest(DeviceUtilsNewImplTest): |
| 996 |
| 997 def testClearApplicationState_packageDoesntExist(self): |
| 998 with self.assertCall( |
| 999 self.call.device.GetApplicationPath('this.package.does.not.exist'), |
| 1000 None): |
| 1001 self.device.ClearApplicationState('this.package.does.not.exist') |
999 | 1002 |
1000 def testClearApplicationState_packageExists(self): | 1003 def testClearApplicationState_packageExists(self): |
1001 with self.assertCalls( | 1004 with self.assertCalls( |
1002 "adb -s 0123456789abcdef shell 'pm path this.package.does.not.exist'", | 1005 (self.call.device.GetApplicationPath('this.package.exists'), |
1003 ''): | 1006 '/data/app/this.package.exists.apk'), |
1004 self.device.ClearApplicationState('this.package.does.not.exist') | 1007 (self.call.adb.Shell('pm clear this.package.exists'), |
1005 | 1008 'Success\r\n')): |
1006 def testClearApplicationState_packageDoesntExist(self): | |
1007 with self.assertCallsSequence([ | |
1008 ("adb -s 0123456789abcdef shell 'pm path this.package.exists'", | |
1009 'package:/data/app/this.package.exists.apk'), | |
1010 ("adb -s 0123456789abcdef shell 'pm clear this.package.exists'", | |
1011 'Success\r\n')]): | |
1012 self.device.ClearApplicationState('this.package.exists') | 1009 self.device.ClearApplicationState('this.package.exists') |
1013 | 1010 |
1014 | 1011 |
1015 class DeviceUtilsSendKeyEventTest(DeviceUtilsOldImplTest): | 1012 class DeviceUtilsSendKeyEventTest(DeviceUtilsNewImplTest): |
1016 | 1013 |
1017 def testSendKeyEvent(self): | 1014 def testSendKeyEvent(self): |
1018 with self.assertCalls( | 1015 with self.assertCall(self.call.adb.Shell('input keyevent 66'), ''): |
1019 "adb -s 0123456789abcdef shell 'input keyevent 66'", | |
1020 ''): | |
1021 self.device.SendKeyEvent(66) | 1016 self.device.SendKeyEvent(66) |
1022 | 1017 |
1023 | 1018 |
1024 class DeviceUtilsPushChangedFilesIndividuallyTest(DeviceUtilsNewImplTest): | 1019 class DeviceUtilsPushChangedFilesIndividuallyTest(DeviceUtilsNewImplTest): |
1025 | 1020 |
1026 def testPushChangedFilesIndividually_empty(self): | 1021 def testPushChangedFilesIndividually_empty(self): |
1027 test_files = [] | 1022 test_files = [] |
1028 with self.assertCalls(): | 1023 with self.assertCalls(): |
1029 self.device._PushChangedFilesIndividually(test_files) | 1024 self.device._PushChangedFilesIndividually(test_files) |
1030 | 1025 |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1230 self.device.WriteFile('/test/file/to write', 'the contents') | 1225 self.device.WriteFile('/test/file/to write', 'the contents') |
1231 | 1226 |
1232 def testWriteFile_withEchoAndSU(self): | 1227 def testWriteFile_withEchoAndSU(self): |
1233 with self.assertCalls( | 1228 with self.assertCalls( |
1234 (self.call.device.NeedsSU(), True), | 1229 (self.call.device.NeedsSU(), True), |
1235 (self.call.adb.Shell("su -c sh -c 'echo -n contents > /test/file'"), | 1230 (self.call.adb.Shell("su -c sh -c 'echo -n contents > /test/file'"), |
1236 '')): | 1231 '')): |
1237 self.device.WriteFile('/test/file', 'contents', as_root=True) | 1232 self.device.WriteFile('/test/file', 'contents', as_root=True) |
1238 | 1233 |
1239 | 1234 |
1240 class DeviceUtilsLsTest(DeviceUtilsOldImplTest): | 1235 class DeviceUtilsLsTest(DeviceUtilsNewImplTest): |
| 1236 |
| 1237 def testLs_directory(self): |
| 1238 result = [('.', adb_wrapper.DeviceStat(16889, 4096, 1417436123)), |
| 1239 ('..', adb_wrapper.DeviceStat(16873, 4096, 12382237)), |
| 1240 ('testfile.txt', adb_wrapper.DeviceStat(33206, 3, 1417436122))] |
| 1241 with self.assertCalls( |
| 1242 (self.call.adb.Ls('/data/local/tmp'), result)): |
| 1243 self.assertEquals(result, |
| 1244 self.device.Ls('/data/local/tmp')) |
1241 | 1245 |
1242 def testLs_nothing(self): | 1246 def testLs_nothing(self): |
1243 with self.assertCallsSequence([ | 1247 with self.assertCalls( |
1244 ("adb -s 0123456789abcdef shell 'ls -lR /this/file/does.not.exist'", | 1248 (self.call.adb.Ls('/data/local/tmp/testfile.txt'), [])): |
1245 '/this/file/does.not.exist: No such file or directory\r\n'), | 1249 self.assertEquals([], |
1246 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]): | 1250 self.device.Ls('/data/local/tmp/testfile.txt')) |
1247 self.assertEqual({}, self.device.Ls('/this/file/does.not.exist')) | |
1248 | 1251 |
1249 def testLs_file(self): | |
1250 with self.assertCallsSequence([ | |
1251 ("adb -s 0123456789abcdef shell 'ls -lR /this/is/a/test.file'", | |
1252 '-rw-rw---- testuser testgroup 4096 1970-01-01 00:00 test.file\r\n'), | |
1253 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]): | |
1254 self.assertEqual( | |
1255 {'test.file': (4096, datetime.datetime(1970, 1, 1))}, | |
1256 self.device.Ls('/this/is/a/test.file')) | |
1257 | 1252 |
1258 def testLs_directory(self): | 1253 class DeviceUtilsStatTest(DeviceUtilsNewImplTest): |
1259 with self.assertCallsSequence([ | |
1260 ("adb -s 0123456789abcdef shell 'ls -lR /this/is/a/test.directory'", | |
1261 '\r\n' | |
1262 '/this/is/a/test.directory:\r\n' | |
1263 '-rw-rw---- testuser testgroup 4096 1970-01-01 18:19 test.file\r\n'), | |
1264 ("adb -s 0123456789abcdef shell 'date +%z'", '+0000')]): | |
1265 self.assertEqual( | |
1266 {'test.file': (4096, datetime.datetime(1970, 1, 1, 18, 19))}, | |
1267 self.device.Ls('/this/is/a/test.directory')) | |
1268 | 1254 |
1269 def testLs_directories(self): | 1255 def testStat_file(self): |
1270 with self.assertCallsSequence([ | 1256 result = [('.', adb_wrapper.DeviceStat(16889, 4096, 1417436123)), |
1271 ("adb -s 0123456789abcdef shell 'ls -lR /this/is/a/test.directory'", | 1257 ('..', adb_wrapper.DeviceStat(16873, 4096, 12382237)), |
1272 '\r\n' | 1258 ('testfile.txt', adb_wrapper.DeviceStat(33206, 3, 1417436122))] |
1273 '/this/is/a/test.directory:\r\n' | 1259 with self.assertCalls( |
1274 'drwxr-xr-x testuser testgroup 1970-01-01 00:00 test.subdirectory\r\n' | 1260 (self.call.adb.Ls('/data/local/tmp'), result)): |
1275 '\r\n' | 1261 self.assertEquals(adb_wrapper.DeviceStat(33206, 3, 1417436122), |
1276 '/this/is/a/test.directory/test.subdirectory:\r\n' | 1262 self.device.Stat('/data/local/tmp/testfile.txt')) |
1277 '-rw-rw---- testuser testgroup 4096 1970-01-01 00:00 test.file\r\n'), | 1263 |
1278 ("adb -s 0123456789abcdef shell 'date +%z'", '-0700')]): | 1264 def testStat_directory(self): |
1279 self.assertEqual( | 1265 result = [('.', adb_wrapper.DeviceStat(16873, 4096, 12382237)), |
1280 {'test.subdirectory/test.file': | 1266 ('..', adb_wrapper.DeviceStat(16873, 4096, 12382237)), |
1281 (4096, datetime.datetime(1970, 1, 1, 7, 0, 0))}, | 1267 ('tmp', adb_wrapper.DeviceStat(16889, 4096, 1417436123))] |
1282 self.device.Ls('/this/is/a/test.directory')) | 1268 with self.assertCalls( |
| 1269 (self.call.adb.Ls('/data/local'), result)): |
| 1270 self.assertEquals(adb_wrapper.DeviceStat(16889, 4096, 1417436123), |
| 1271 self.device.Stat('/data/local/tmp')) |
| 1272 |
| 1273 def testStat_doesNotExist(self): |
| 1274 result = [('.', adb_wrapper.DeviceStat(16889, 4096, 1417436123)), |
| 1275 ('..', adb_wrapper.DeviceStat(16873, 4096, 12382237)), |
| 1276 ('testfile.txt', adb_wrapper.DeviceStat(33206, 3, 1417436122))] |
| 1277 with self.assertCalls( |
| 1278 (self.call.adb.Ls('/data/local/tmp'), result)): |
| 1279 with self.assertRaises(device_errors.CommandFailedError): |
| 1280 self.device.Stat('/data/local/tmp/does.not.exist.txt') |
1283 | 1281 |
1284 | 1282 |
1285 class DeviceUtilsSetJavaAssertsTest(DeviceUtilsOldImplTest): | 1283 class DeviceUtilsSetJavaAssertsTest(DeviceUtilsOldImplTest): |
1286 | 1284 |
1287 @staticmethod | 1285 @staticmethod |
1288 def mockNamedTemporary(name='/tmp/file/property.file', | 1286 def mockNamedTemporary(name='/tmp/file/property.file', |
1289 read_contents=''): | 1287 read_contents=''): |
1290 mock_file = mock.MagicMock(spec=file) | 1288 mock_file = mock.MagicMock(spec=file) |
1291 mock_file.name = name | 1289 mock_file.name = name |
1292 mock_file.__enter__.return_value = mock_file | 1290 mock_file.__enter__.return_value = mock_file |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1531 def testStr_returnsSerial(self): | 1529 def testStr_returnsSerial(self): |
1532 with self.assertCalls( | 1530 with self.assertCalls( |
1533 (self.call.adb.GetDeviceSerial(), '0123456789abcdef')): | 1531 (self.call.adb.GetDeviceSerial(), '0123456789abcdef')): |
1534 self.assertEqual('0123456789abcdef', str(self.device)) | 1532 self.assertEqual('0123456789abcdef', str(self.device)) |
1535 | 1533 |
1536 | 1534 |
1537 if __name__ == '__main__': | 1535 if __name__ == '__main__': |
1538 logging.getLogger().setLevel(logging.DEBUG) | 1536 logging.getLogger().setLevel(logging.DEBUG) |
1539 unittest.main(verbosity=2) | 1537 unittest.main(verbosity=2) |
1540 | 1538 |
OLD | NEW |