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 479 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
490 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", | 490 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", |
491 ''), | 491 ''), |
492 ("adb -s 0123456789abcdef install /fake/test/app.apk", | 492 ("adb -s 0123456789abcdef install /fake/test/app.apk", |
493 'Success\r\n')]): | 493 'Success\r\n')]): |
494 self.device.Install('/fake/test/app.apk', retries=0) | 494 self.device.Install('/fake/test/app.apk', retries=0) |
495 | 495 |
496 def testInstall_differentPriorInstall(self): | 496 def testInstall_differentPriorInstall(self): |
497 def mockGetFilesChanged(host_path, device_path, ignore_filenames): | 497 def mockGetFilesChanged(host_path, device_path, ignore_filenames): |
498 return [(host_path, device_path)] | 498 return [(host_path, device_path)] |
499 | 499 |
500 # Pylint raises a false positive "operator not preceded by a space" | |
501 # warning below. | |
502 # pylint: disable=C0322 | |
503 with mock.patch('os.path.isfile', return_value=True), ( | 500 with mock.patch('os.path.isfile', return_value=True), ( |
504 mock.patch('os.path.exists', return_value=True)), ( | 501 mock.patch('os.path.exists', return_value=True)), ( |
505 mock.patch('pylib.utils.apk_helper.GetPackageName', | 502 mock.patch('pylib.utils.apk_helper.GetPackageName', |
506 return_value='this.is.a.test.package')), ( | 503 return_value='this.is.a.test.package')), ( |
507 mock.patch('pylib.constants.GetOutDirectory', | 504 mock.patch('pylib.constants.GetOutDirectory', |
508 return_value='/fake/test/out')), ( | 505 return_value='/fake/test/out')), ( |
509 mock.patch('pylib.android_commands.AndroidCommands.GetFilesChanged', | 506 mock.patch('pylib.android_commands.AndroidCommands.GetFilesChanged', |
510 side_effect=mockGetFilesChanged)): | 507 side_effect=mockGetFilesChanged)): |
511 # pylint: enable=C0322 | |
512 with self.assertCallsSequence([ | 508 with self.assertCallsSequence([ |
513 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", | 509 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", |
514 'package:/fake/data/app/this.is.a.test.package.apk\r\n'), | 510 'package:/fake/data/app/this.is.a.test.package.apk\r\n'), |
515 # GetFilesChanged is mocked, so its adb calls are omitted. | 511 # GetFilesChanged is mocked, so its adb calls are omitted. |
516 ('adb -s 0123456789abcdef uninstall this.is.a.test.package', | 512 ('adb -s 0123456789abcdef uninstall this.is.a.test.package', |
517 'Success\r\n'), | 513 'Success\r\n'), |
518 ('adb -s 0123456789abcdef install /fake/test/app.apk', | 514 ('adb -s 0123456789abcdef install /fake/test/app.apk', |
519 'Success\r\n')]): | 515 'Success\r\n')]): |
520 self.device.Install('/fake/test/app.apk', retries=0) | 516 self.device.Install('/fake/test/app.apk', retries=0) |
521 | 517 |
522 def testInstall_differentPriorInstall_reinstall(self): | 518 def testInstall_differentPriorInstall_reinstall(self): |
523 def mockGetFilesChanged(host_path, device_path, ignore_filenames): | 519 def mockGetFilesChanged(host_path, device_path, ignore_filenames): |
524 return [(host_path, device_path)] | 520 return [(host_path, device_path)] |
525 | 521 |
526 # Pylint raises a false positive "operator not preceded by a space" | |
527 # warning below. | |
528 # pylint: disable=C0322 | |
529 with mock.patch('os.path.isfile', return_value=True), ( | 522 with mock.patch('os.path.isfile', return_value=True), ( |
530 mock.patch('pylib.utils.apk_helper.GetPackageName', | 523 mock.patch('pylib.utils.apk_helper.GetPackageName', |
531 return_value='this.is.a.test.package')), ( | 524 return_value='this.is.a.test.package')), ( |
532 mock.patch('pylib.constants.GetOutDirectory', | 525 mock.patch('pylib.constants.GetOutDirectory', |
533 return_value='/fake/test/out')), ( | 526 return_value='/fake/test/out')), ( |
534 mock.patch('pylib.android_commands.AndroidCommands.GetFilesChanged', | 527 mock.patch('pylib.android_commands.AndroidCommands.GetFilesChanged', |
535 side_effect=mockGetFilesChanged)): | 528 side_effect=mockGetFilesChanged)): |
536 # pylint: enable=C0322 | |
537 with self.assertCallsSequence([ | 529 with self.assertCallsSequence([ |
538 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", | 530 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", |
539 'package:/fake/data/app/this.is.a.test.package.apk\r\n'), | 531 'package:/fake/data/app/this.is.a.test.package.apk\r\n'), |
540 # GetFilesChanged is mocked, so its adb calls are omitted. | 532 # GetFilesChanged is mocked, so its adb calls are omitted. |
541 ('adb -s 0123456789abcdef install -r /fake/test/app.apk', | 533 ('adb -s 0123456789abcdef install -r /fake/test/app.apk', |
542 'Success\r\n')]): | 534 'Success\r\n')]): |
543 self.device.Install('/fake/test/app.apk', reinstall=True, retries=0) | 535 self.device.Install('/fake/test/app.apk', reinstall=True, retries=0) |
544 | 536 |
545 def testInstall_identicalPriorInstall(self): | 537 def testInstall_identicalPriorInstall(self): |
546 def mockGetFilesChanged(host_path, device_path, ignore_filenames): | 538 def mockGetFilesChanged(host_path, device_path, ignore_filenames): |
(...skipping 667 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1214 | 1206 |
1215 mock_file = mock.MagicMock(spec=file) | 1207 mock_file = mock.MagicMock(spec=file) |
1216 mock_file.name = '/tmp/file/to.be.pushed' | 1208 mock_file.name = '/tmp/file/to.be.pushed' |
1217 mock_file.__enter__.return_value = mock_file | 1209 mock_file.__enter__.return_value = mock_file |
1218 with mock.patch('tempfile.NamedTemporaryFile', | 1210 with mock.patch('tempfile.NamedTemporaryFile', |
1219 return_value=mock_file): | 1211 return_value=mock_file): |
1220 with self.assertCallsSequence( | 1212 with self.assertCallsSequence( |
1221 cmd_ret=[ | 1213 cmd_ret=[ |
1222 # Create temporary contents file | 1214 # Create temporary contents file |
1223 (r"adb -s 0123456789abcdef shell " | 1215 (r"adb -s 0123456789abcdef shell " |
1224 "'test -e \"/fake/storage/path/temp_file-\d+-\d+\"; " | 1216 r"'test -e \"/fake/storage/path/temp_file-\d+-\d+\"; " |
1225 "echo \$\?'", | 1217 r"echo \$\?'", |
1226 '1\r\n'), | 1218 '1\r\n'), |
1227 # Create temporary script file | 1219 # Create temporary script file |
1228 (r"adb -s 0123456789abcdef shell " | 1220 (r"adb -s 0123456789abcdef shell " |
1229 "'test -e \"/fake/storage/path/temp_file-\d+-\d+\.sh\"; " | 1221 r"'test -e \"/fake/storage/path/temp_file-\d+-\d+\.sh\"; " |
1230 "echo \$\?'", | 1222 r"echo \$\?'", |
1231 '1\r\n'), | 1223 '1\r\n'), |
1232 # Set contents file | 1224 # Set contents file |
1233 (r'adb -s 0123456789abcdef push /tmp/file/to\.be\.pushed ' | 1225 (r'adb -s 0123456789abcdef push /tmp/file/to\.be\.pushed ' |
1234 '/fake/storage/path/temp_file-\d+\d+', | 1226 r'/fake/storage/path/temp_file-\d+\d+', |
1235 '100 B/s (100 bytes in 1.000s)\r\n'), | 1227 '100 B/s (100 bytes in 1.000s)\r\n'), |
1236 # Set script file | 1228 # Set script file |
1237 (r'adb -s 0123456789abcdef push /tmp/file/to\.be\.pushed ' | 1229 (r'adb -s 0123456789abcdef push /tmp/file/to\.be\.pushed ' |
1238 '/fake/storage/path/temp_file-\d+\d+', | 1230 r'/fake/storage/path/temp_file-\d+\d+', |
1239 '100 B/s (100 bytes in 1.000s)\r\n'), | 1231 '100 B/s (100 bytes in 1.000s)\r\n'), |
1240 # Call script | 1232 # Call script |
1241 (r"adb -s 0123456789abcdef shell " | 1233 (r"adb -s 0123456789abcdef shell " |
1242 "'sh /fake/storage/path/temp_file-\d+-\d+\.sh'", ''), | 1234 r"'sh /fake/storage/path/temp_file-\d+-\d+\.sh'", ''), |
1243 # Remove device temporaries | 1235 # Remove device temporaries |
1244 (r"adb -s 0123456789abcdef shell " | 1236 (r"adb -s 0123456789abcdef shell " |
1245 "'rm /fake/storage/path/temp_file-\d+-\d+\.sh'", ''), | 1237 r"'rm /fake/storage/path/temp_file-\d+-\d+\.sh'", ''), |
1246 (r"adb -s 0123456789abcdef shell " | 1238 (r"adb -s 0123456789abcdef shell " |
1247 "'rm /fake/storage/path/temp_file-\d+-\d+'", '')], | 1239 r"'rm /fake/storage/path/temp_file-\d+-\d+'", '')], |
1248 comp=re.match): | 1240 comp=re.match): |
1249 self.device.WriteFile('/test/file/written.to.device', | 1241 self.device.WriteFile('/test/file/written.to.device', |
1250 'new test file contents', as_root=True) | 1242 'new test file contents', as_root=True) |
1251 | 1243 |
1252 def testWriteFile_asRoot_withSu(self): | 1244 def testWriteFile_asRoot_withSu(self): |
1253 self.device.old_interface._external_storage = '/fake/storage/path' | 1245 self.device.old_interface._external_storage = '/fake/storage/path' |
1254 self.device.old_interface._privileged_command_runner = ( | 1246 self.device.old_interface._privileged_command_runner = ( |
1255 self.device.old_interface.RunShellCommandWithSU) | 1247 self.device.old_interface.RunShellCommandWithSU) |
1256 self.device.old_interface._protected_file_access_method_initialized = True | 1248 self.device.old_interface._protected_file_access_method_initialized = True |
1257 | 1249 |
1258 mock_file = mock.MagicMock(spec=file) | 1250 mock_file = mock.MagicMock(spec=file) |
1259 mock_file.name = '/tmp/file/to.be.pushed' | 1251 mock_file.name = '/tmp/file/to.be.pushed' |
1260 mock_file.__enter__.return_value = mock_file | 1252 mock_file.__enter__.return_value = mock_file |
1261 with mock.patch('tempfile.NamedTemporaryFile', | 1253 with mock.patch('tempfile.NamedTemporaryFile', |
1262 return_value=mock_file): | 1254 return_value=mock_file): |
1263 with self.assertCallsSequence( | 1255 with self.assertCallsSequence( |
1264 cmd_ret=[ | 1256 cmd_ret=[ |
1265 # Create temporary contents file | 1257 # Create temporary contents file |
1266 (r"adb -s 0123456789abcdef shell " | 1258 (r"adb -s 0123456789abcdef shell " |
1267 "'test -e \"/fake/storage/path/temp_file-\d+-\d+\"; " | 1259 r"'test -e \"/fake/storage/path/temp_file-\d+-\d+\"; " |
1268 "echo \$\?'", | 1260 r"echo \$\?'", |
1269 '1\r\n'), | 1261 '1\r\n'), |
1270 # Create temporary script file | 1262 # Create temporary script file |
1271 (r"adb -s 0123456789abcdef shell " | 1263 (r"adb -s 0123456789abcdef shell " |
1272 "'test -e \"/fake/storage/path/temp_file-\d+-\d+\.sh\"; " | 1264 r"'test -e \"/fake/storage/path/temp_file-\d+-\d+\.sh\"; " |
1273 "echo \$\?'", | 1265 r"echo \$\?'", |
1274 '1\r\n'), | 1266 '1\r\n'), |
1275 # Set contents file | 1267 # Set contents file |
1276 (r'adb -s 0123456789abcdef push /tmp/file/to\.be\.pushed ' | 1268 (r'adb -s 0123456789abcdef push /tmp/file/to\.be\.pushed ' |
1277 '/fake/storage/path/temp_file-\d+\d+', | 1269 r'/fake/storage/path/temp_file-\d+\d+', |
1278 '100 B/s (100 bytes in 1.000s)\r\n'), | 1270 '100 B/s (100 bytes in 1.000s)\r\n'), |
1279 # Set script file | 1271 # Set script file |
1280 (r'adb -s 0123456789abcdef push /tmp/file/to\.be\.pushed ' | 1272 (r'adb -s 0123456789abcdef push /tmp/file/to\.be\.pushed ' |
1281 '/fake/storage/path/temp_file-\d+\d+', | 1273 r'/fake/storage/path/temp_file-\d+\d+', |
1282 '100 B/s (100 bytes in 1.000s)\r\n'), | 1274 '100 B/s (100 bytes in 1.000s)\r\n'), |
1283 # Call script | 1275 # Call script |
1284 (r"adb -s 0123456789abcdef shell " | 1276 (r"adb -s 0123456789abcdef shell " |
1285 "'su -c sh /fake/storage/path/temp_file-\d+-\d+\.sh'", ''), | 1277 r"'su -c sh /fake/storage/path/temp_file-\d+-\d+\.sh'", ''), |
1286 # Remove device temporaries | 1278 # Remove device temporaries |
1287 (r"adb -s 0123456789abcdef shell " | 1279 (r"adb -s 0123456789abcdef shell " |
1288 "'rm /fake/storage/path/temp_file-\d+-\d+\.sh'", ''), | 1280 r"'rm /fake/storage/path/temp_file-\d+-\d+\.sh'", ''), |
1289 (r"adb -s 0123456789abcdef shell " | 1281 (r"adb -s 0123456789abcdef shell " |
1290 "'rm /fake/storage/path/temp_file-\d+-\d+'", '')], | 1282 r"'rm /fake/storage/path/temp_file-\d+-\d+'", '')], |
1291 comp=re.match): | 1283 comp=re.match): |
1292 self.device.WriteFile('/test/file/written.to.device', | 1284 self.device.WriteFile('/test/file/written.to.device', |
1293 'new test file contents', as_root=True) | 1285 'new test file contents', as_root=True) |
1294 | 1286 |
1295 def testWriteFile_asRoot_rejected(self): | 1287 def testWriteFile_asRoot_rejected(self): |
1296 self.device.old_interface._privileged_command_runner = None | 1288 self.device.old_interface._privileged_command_runner = None |
1297 self.device.old_interface._protected_file_access_method_initialized = True | 1289 self.device.old_interface._protected_file_access_method_initialized = True |
1298 with self.assertRaises(device_errors.CommandFailedError): | 1290 with self.assertRaises(device_errors.CommandFailedError): |
1299 self.device.WriteFile('/test/file/no.permissions.to.write', | 1291 self.device.WriteFile('/test/file/no.permissions.to.write', |
1300 'new test file contents', as_root=True) | 1292 'new test file contents', as_root=True) |
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1624 self.device = device_utils.DeviceUtils(None) | 1616 self.device = device_utils.DeviceUtils(None) |
1625 with self.assertCalls('adb get-serialno', 'unknown'), ( | 1617 with self.assertCalls('adb get-serialno', 'unknown'), ( |
1626 self.assertRaises(device_errors.NoDevicesError)): | 1618 self.assertRaises(device_errors.NoDevicesError)): |
1627 str(self.device) | 1619 str(self.device) |
1628 | 1620 |
1629 | 1621 |
1630 if __name__ == '__main__': | 1622 if __name__ == '__main__': |
1631 logging.getLogger().setLevel(logging.DEBUG) | 1623 logging.getLogger().setLevel(logging.DEBUG) |
1632 unittest.main(verbosity=2) | 1624 unittest.main(verbosity=2) |
1633 | 1625 |
OLD | NEW |