| 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 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 | 278 |
| 279 class _ShellError: | 279 class _ShellError: |
| 280 def __init__(self, output=None, return_code=1): | 280 def __init__(self, output=None, return_code=1): |
| 281 if output is None: | 281 if output is None: |
| 282 self.output = 'Permission denied\r\n' | 282 self.output = 'Permission denied\r\n' |
| 283 else: | 283 else: |
| 284 self.output = output | 284 self.output = output |
| 285 self.return_code = return_code | 285 self.return_code = return_code |
| 286 | 286 |
| 287 | 287 |
| 288 class _CmdTimeout: |
| 289 def __init__(self, msg=None): |
| 290 if msg is None: |
| 291 self.msg = 'Operation timed out' |
| 292 else: |
| 293 self.msg = msg |
| 294 |
| 295 |
| 288 class DeviceUtilsNewImplTest(unittest.TestCase): | 296 class DeviceUtilsNewImplTest(unittest.TestCase): |
| 289 | 297 |
| 290 def setUp(self): | 298 def setUp(self): |
| 291 test_serial = '0123456789abcdef' | 299 test_serial = '0123456789abcdef' |
| 292 self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper) | 300 self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper) |
| 293 self.adb.__str__ = mock.Mock(return_value=test_serial) | 301 self.adb.__str__ = mock.Mock(return_value=test_serial) |
| 294 self.adb.GetDeviceSerial.return_value = test_serial | 302 self.adb.GetDeviceSerial.return_value = test_serial |
| 295 self.device = device_utils.DeviceUtils( | 303 self.device = device_utils.DeviceUtils( |
| 296 self.adb, default_timeout=1, default_retries=0) | 304 self.adb, default_timeout=1, default_retries=0) |
| 297 | 305 |
| 298 def assertShellCallSequence(self, calls): | 306 def assertShellCallSequence(self, calls): |
| 299 '''Assert that we expect a sequence of calls to adb.Shell. | 307 '''Assert that we expect a sequence of calls to adb.Shell. |
| 300 | 308 |
| 301 Args: | 309 Args: |
| 302 calls: a sequence of (cmd, return_value) pairs, where |cmd| is the | 310 calls: a sequence of (cmd, return_value) pairs, where |cmd| is the |
| 303 expected shell command to run on the device (with any quoting already | 311 expected shell command to run on the device (with any quoting already |
| 304 applied), and |return_value| is either a string to give as mock output | 312 applied), and |return_value| is either a string to give as mock output |
| 305 or a _ShellError object to raise an AdbShellCommandFailedError. | 313 or a _ShellError object to raise an AdbShellCommandFailedError. |
| 306 ''' | 314 ''' |
| 307 def mk_expected_call(cmd, return_value): | 315 def mk_expected_call(cmd, return_value): |
| 308 expected_args = Args(cmd, expect_rc=0, timeout=1, retries=0) | 316 expected_args = Args(cmd, expect_rc=0, timeout=1, retries=0) |
| 309 if isinstance(return_value, _ShellError): | 317 if isinstance(return_value, _ShellError): |
| 310 return_value = device_errors.AdbShellCommandFailedError(cmd, | 318 return_value = device_errors.AdbShellCommandFailedError(cmd, |
| 311 return_value.return_code, return_value.output, str(self.device)) | 319 return_value.return_code, return_value.output, str(self.device)) |
| 320 elif isinstance(return_value, _CmdTimeout): |
| 321 return_value = device_errors.CommandTimeoutError(return_value.msg, |
| 322 str(self.device)) |
| 312 return (expected_args, return_value) | 323 return (expected_args, return_value) |
| 313 | 324 |
| 314 expected_calls = (mk_expected_call(a, r) for a, r in calls) | 325 expected_calls = (mk_expected_call(a, r) for a, r in calls) |
| 315 return MockCallSequence(self, self.adb, 'Shell', expected_calls) | 326 return MockCallSequence(self, self.adb, 'Shell', expected_calls) |
| 316 | 327 |
| 317 def assertShellCall(self, cmd, return_value=''): | 328 def assertShellCall(self, cmd, return_value=''): |
| 318 return self.assertShellCallSequence([(cmd, return_value)]) | 329 return self.assertShellCallSequence([(cmd, return_value)]) |
| 319 | 330 |
| 320 | 331 |
| 321 class DeviceUtilsHybridImplTest(DeviceUtilsOldImplTest): | 332 class DeviceUtilsHybridImplTest(DeviceUtilsOldImplTest): |
| 322 | 333 |
| 323 def setUp(self): | 334 def setUp(self): |
| 324 super(DeviceUtilsHybridImplTest, self).setUp() | 335 super(DeviceUtilsHybridImplTest, self).setUp() |
| 325 self.device.adb = self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper) | 336 self.device.adb = self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper) |
| 326 | 337 |
| 327 | 338 |
| 328 class DeviceUtilsIsOnlineTest(DeviceUtilsOldImplTest): | 339 class DeviceUtilsIsOnlineTest(DeviceUtilsNewImplTest): |
| 329 | 340 |
| 330 def testIsOnline_true(self): | 341 def testIsOnline_true(self): |
| 331 with self.assertCalls('adb -s 0123456789abcdef devices', | 342 self.adb.GetState = mock.Mock(return_value='device') |
| 332 '00123456789abcdef device\r\n'): | 343 self.assertTrue(self.device.IsOnline()) |
| 333 self.assertTrue(self.device.IsOnline()) | 344 self.adb.GetState.assert_called_once_with() |
| 334 | 345 |
| 335 def testIsOnline_false(self): | 346 def testIsOnline_false(self): |
| 336 with self.assertCalls('adb -s 0123456789abcdef devices', '\r\n'): | 347 self.adb.GetState = mock.Mock(return_value='offline') |
| 337 self.assertFalse(self.device.IsOnline()) | 348 self.assertFalse(self.device.IsOnline()) |
| 349 self.adb.GetState.assert_called_once_with() |
| 338 | 350 |
| 339 | 351 |
| 340 class DeviceUtilsHasRootTest(DeviceUtilsNewImplTest): | 352 class DeviceUtilsHasRootTest(DeviceUtilsNewImplTest): |
| 341 | 353 |
| 342 def testHasRoot_true(self): | 354 def testHasRoot_true(self): |
| 343 with self.assertShellCall('ls /root', 'foo\r\n'): | 355 with self.assertShellCall('ls /root', 'foo\r\n'): |
| 344 self.assertTrue(self.device.HasRoot()) | 356 self.assertTrue(self.device.HasRoot()) |
| 345 | 357 |
| 346 def testHasRoot_false(self): | 358 def testHasRoot_false(self): |
| 347 with self.assertShellCall('ls /root', _ShellError()): | 359 with self.assertShellCall('ls /root', _ShellError()): |
| (...skipping 20 matching lines...) Expand all Loading... |
| 368 def testEnableRoot_rootFails(self): | 380 def testEnableRoot_rootFails(self): |
| 369 with self.assertCallsSequence([ | 381 with self.assertCallsSequence([ |
| 370 ('adb -s 0123456789abcdef shell getprop ro.build.type', | 382 ('adb -s 0123456789abcdef shell getprop ro.build.type', |
| 371 'userdebug\r\n'), | 383 'userdebug\r\n'), |
| 372 ('adb -s 0123456789abcdef root', 'no\r\n'), | 384 ('adb -s 0123456789abcdef root', 'no\r\n'), |
| 373 ('adb -s 0123456789abcdef wait-for-device', '')]): | 385 ('adb -s 0123456789abcdef wait-for-device', '')]): |
| 374 with self.assertRaises(device_errors.CommandFailedError): | 386 with self.assertRaises(device_errors.CommandFailedError): |
| 375 self.device.EnableRoot() | 387 self.device.EnableRoot() |
| 376 | 388 |
| 377 | 389 |
| 378 class DeviceUtilsIsUserBuildTest(DeviceUtilsOldImplTest): | 390 class DeviceUtilsIsUserBuildTest(DeviceUtilsNewImplTest): |
| 379 | 391 |
| 380 def testIsUserBuild_yes(self): | 392 def testIsUserBuild_yes(self): |
| 381 with self.assertCalls( | 393 with self.assertShellCall('getprop ro.build.type', 'user\r\n'): |
| 382 'adb -s 0123456789abcdef shell getprop ro.build.type', | |
| 383 'user\r\n'): | |
| 384 self.assertTrue(self.device.IsUserBuild()) | 394 self.assertTrue(self.device.IsUserBuild()) |
| 385 | 395 |
| 386 def testIsUserBuild_no(self): | 396 def testIsUserBuild_no(self): |
| 387 with self.assertCalls( | 397 with self.assertShellCall('getprop ro.build.type', 'userdebug\r\n'): |
| 388 'adb -s 0123456789abcdef shell getprop ro.build.type', | |
| 389 'userdebug\r\n'): | |
| 390 self.assertFalse(self.device.IsUserBuild()) | 398 self.assertFalse(self.device.IsUserBuild()) |
| 391 | 399 |
| 392 | 400 |
| 393 class DeviceUtilsGetExternalStoragePathTest(DeviceUtilsNewImplTest): | 401 class DeviceUtilsGetExternalStoragePathTest(DeviceUtilsNewImplTest): |
| 394 | 402 |
| 395 def testGetExternalStoragePath_succeeds(self): | 403 def testGetExternalStoragePath_succeeds(self): |
| 396 fakeStoragePath = '/fake/storage/path' | 404 fakeStoragePath = '/fake/storage/path' |
| 397 with self.assertShellCall('echo $EXTERNAL_STORAGE', | 405 with self.assertShellCall('echo $EXTERNAL_STORAGE', |
| 398 '%s\r\n' % fakeStoragePath): | 406 '%s\r\n' % fakeStoragePath): |
| 399 self.assertEquals(fakeStoragePath, | 407 self.assertEquals(fakeStoragePath, |
| 400 self.device.GetExternalStoragePath()) | 408 self.device.GetExternalStoragePath()) |
| 401 | 409 |
| 402 def testGetExternalStoragePath_fails(self): | 410 def testGetExternalStoragePath_fails(self): |
| 403 with self.assertShellCall('echo $EXTERNAL_STORAGE', '\r\n'): | 411 with self.assertShellCall('echo $EXTERNAL_STORAGE', '\r\n'): |
| 404 with self.assertRaises(device_errors.CommandFailedError): | 412 with self.assertRaises(device_errors.CommandFailedError): |
| 405 self.device.GetExternalStoragePath() | 413 self.device.GetExternalStoragePath() |
| 406 | 414 |
| 407 | 415 |
| 408 class DeviceUtilsWaitUntilFullyBootedTest(DeviceUtilsOldImplTest): | 416 class DeviceUtilsGetApplicationPathTest(DeviceUtilsNewImplTest): |
| 417 |
| 418 def testGetApplicationPath_exists(self): |
| 419 with self.assertShellCall('pm path android', |
| 420 'package:/path/to/android.apk\n'): |
| 421 self.assertEquals('/path/to/android.apk', |
| 422 self.device.GetApplicationPath('android')) |
| 423 |
| 424 def testGetApplicationPath_notExists(self): |
| 425 with self.assertShellCall('pm path not.installed.app', |
| 426 ''): |
| 427 self.assertEquals(None, |
| 428 self.device.GetApplicationPath('not.installed.app')) |
| 429 |
| 430 def testGetApplicationPath_fails(self): |
| 431 with self.assertShellCall('pm path android', |
| 432 'ERROR. Is package manager running?\n'): |
| 433 with self.assertRaises(device_errors.CommandFailedError): |
| 434 self.device.GetApplicationPath('android') |
| 435 |
| 436 |
| 437 class DeviceUtilsWaitUntilFullyBootedTest(DeviceUtilsNewImplTest): |
| 409 | 438 |
| 410 def testWaitUntilFullyBooted_succeedsNoWifi(self): | 439 def testWaitUntilFullyBooted_succeedsNoWifi(self): |
| 411 with self.assertCallsSequence([ | 440 with self.assertShellCallSequence([ |
| 412 # AndroidCommands.WaitForSystemBootCompleted | 441 # sc_card_ready |
| 413 ('adb -s 0123456789abcdef wait-for-device', ''), | 442 ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'), |
| 414 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', '1\r\n'), | 443 ('ls /fake/storage/path', '/fake/storage/path\r\n'), |
| 415 # AndroidCommands.WaitForDevicePm | 444 # pm_ready |
| 416 ('adb -s 0123456789abcdef wait-for-device', ''), | 445 ('pm path android', 'package:this.is.a.test.package\r\n'), |
| 417 ('adb -s 0123456789abcdef shell pm path android', | 446 # boot_completed |
| 418 'package:this.is.a.test.package'), | 447 ('getprop sys.boot_completed', '1\r\n')]): |
| 419 # AndroidCommands.WaitForSdCardReady | |
| 420 ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", | |
| 421 '/fake/storage/path'), | |
| 422 ("adb -s 0123456789abcdef shell 'ls /fake/storage/path'", | |
| 423 'nothing\r\n') | |
| 424 ]): | |
| 425 self.device.WaitUntilFullyBooted(wifi=False) | 448 self.device.WaitUntilFullyBooted(wifi=False) |
| 449 self.adb.WaitForDevice.assert_called_once_with() |
| 426 | 450 |
| 427 def testWaitUntilFullyBooted_succeedsWithWifi(self): | 451 def testWaitUntilFullyBooted_succeedsWithWifi(self): |
| 428 with self.assertCallsSequence([ | 452 with self.assertShellCallSequence([ |
| 429 # AndroidCommands.WaitForSystemBootCompleted | 453 # sc_card_ready |
| 430 ('adb -s 0123456789abcdef wait-for-device', ''), | 454 ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'), |
| 431 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', '1\r\n'), | 455 ('ls /fake/storage/path', '/fake/storage/path\r\n'), |
| 432 # AndroidCommands.WaitForDevicePm | 456 # pm_ready |
| 433 ('adb -s 0123456789abcdef wait-for-device', ''), | 457 ('pm path android', 'package:this.is.a.test.package\r\n'), |
| 434 ('adb -s 0123456789abcdef shell pm path android', | 458 # boot_completed |
| 435 'package:this.is.a.test.package'), | 459 ('getprop sys.boot_completed', '1\r\n'), |
| 436 # AndroidCommands.WaitForSdCardReady | 460 # wifi_enabled |
| 437 ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", | 461 ('dumpsys wifi', 'stuff\r\nWi-Fi is enabled\r\nmore stuff\r\n')]): |
| 438 '/fake/storage/path'), | |
| 439 ("adb -s 0123456789abcdef shell 'ls /fake/storage/path'", | |
| 440 'nothing\r\n'), | |
| 441 # wait for wifi | |
| 442 ("adb -s 0123456789abcdef shell 'dumpsys wifi'", 'Wi-Fi is enabled')]): | |
| 443 self.device.WaitUntilFullyBooted(wifi=True) | 462 self.device.WaitUntilFullyBooted(wifi=True) |
| 463 self.adb.WaitForDevice.assert_called_once_with() |
| 464 |
| 465 def testWaitUntilFullyBooted_sdCardReadyFails_noPath(self): |
| 466 with self.assertShellCallSequence([ |
| 467 # sc_card_ready |
| 468 ('echo $EXTERNAL_STORAGE', '\r\n')]): |
| 469 with self.assertRaises(device_errors.CommandFailedError): |
| 470 self.device.WaitUntilFullyBooted(wifi=False) |
| 471 |
| 472 def testWaitUntilFullyBooted_sdCardReadyFails_emptyPath(self): |
| 473 with self.assertShellCallSequence([ |
| 474 # sc_card_ready |
| 475 ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'), |
| 476 ('ls /fake/storage/path', '\r\n'), |
| 477 # sc_card_ready |
| 478 ('ls /fake/storage/path', '\r\n'), |
| 479 # sc_card_ready |
| 480 ('ls /fake/storage/path', _CmdTimeout())]): |
| 481 with self.assertRaises(device_errors.CommandTimeoutError): |
| 482 self.device.WaitUntilFullyBooted(wifi=False) |
| 483 |
| 484 def testWaitUntilFullyBooted_devicePmFails(self): |
| 485 with self.assertShellCallSequence([ |
| 486 # sc_card_ready |
| 487 ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'), |
| 488 ('ls /fake/storage/path', '/fake/storage/path\r\n'), |
| 489 # pm_ready |
| 490 ('pm path android', 'Error. Is package manager running?\r\n'), |
| 491 # pm_ready |
| 492 ('pm path android', 'Error. Is package manager running?\r\n'), |
| 493 # pm_ready |
| 494 ('pm path android', _CmdTimeout())]): |
| 495 with self.assertRaises(device_errors.CommandTimeoutError): |
| 496 self.device.WaitUntilFullyBooted(wifi=False) |
| 444 | 497 |
| 445 def testWaitUntilFullyBooted_bootFails(self): | 498 def testWaitUntilFullyBooted_bootFails(self): |
| 446 with mock.patch('time.sleep'): | 499 with self.assertShellCallSequence([ |
| 447 with self.assertCallsSequence([ | 500 # sc_card_ready |
| 448 # AndroidCommands.WaitForSystemBootCompleted | 501 ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'), |
| 449 ('adb -s 0123456789abcdef wait-for-device', ''), | 502 ('ls /fake/storage/path', '/fake/storage/path\r\n'), |
| 450 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', | 503 # pm_ready |
| 451 '0\r\n')]): | 504 ('pm path android', 'package:this.is.a.test.package\r\n'), |
| 452 with self.assertRaises(device_errors.CommandTimeoutError): | 505 # boot_completed |
| 453 self.device.WaitUntilFullyBooted(wifi=False) | 506 ('getprop sys.boot_completed', '0\r\n'), |
| 507 # boot_completed |
| 508 ('getprop sys.boot_completed', '0\r\n'), |
| 509 # boot_completed |
| 510 ('getprop sys.boot_completed', _CmdTimeout())]): |
| 511 with self.assertRaises(device_errors.CommandTimeoutError): |
| 512 self.device.WaitUntilFullyBooted(wifi=False) |
| 454 | 513 |
| 455 def testWaitUntilFullyBooted_devicePmFails(self): | 514 def testWaitUntilFullyBooted_wifiFails(self): |
| 456 with mock.patch('time.sleep'): | 515 with self.assertShellCallSequence([ |
| 457 with self.assertCallsSequence([ | 516 # sc_card_ready |
| 458 # AndroidCommands.WaitForSystemBootCompleted | 517 ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'), |
| 459 ('adb -s 0123456789abcdef wait-for-device', ''), | 518 ('ls /fake/storage/path', '/fake/storage/path\r\n'), |
| 460 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', | 519 # pm_ready |
| 461 '1\r\n')] | 520 ('pm path android', 'package:this.is.a.test.package\r\n'), |
| 462 # AndroidCommands.WaitForDevicePm | 521 # boot_completed |
| 463 + 3 * ([('adb -s 0123456789abcdef wait-for-device', '')] | 522 ('getprop sys.boot_completed', '1\r\n'), |
| 464 + 24 * [('adb -s 0123456789abcdef shell pm path android', '\r\n')] | 523 # wifi_enabled |
| 465 + [("adb -s 0123456789abcdef shell 'stop'", '\r\n'), | 524 ('dumpsys wifi', 'stuff\r\nmore stuff\r\n'), |
| 466 ("adb -s 0123456789abcdef shell 'start'", '\r\n')])): | 525 # wifi_enabled |
| 467 with self.assertRaises(device_errors.CommandTimeoutError): | 526 ('dumpsys wifi', 'stuff\r\nmore stuff\r\n'), |
| 468 self.device.WaitUntilFullyBooted(wifi=False) | 527 # wifi_enabled |
| 469 | 528 ('dumpsys wifi', _CmdTimeout())]): |
| 470 def testWaitUntilFullyBooted_sdCardReadyFails_noPath(self): | 529 with self.assertRaises(device_errors.CommandTimeoutError): |
| 471 with mock.patch('time.sleep'): | 530 self.device.WaitUntilFullyBooted(wifi=True) |
| 472 with self.assertCallsSequence([ | |
| 473 # AndroidCommands.WaitForSystemBootCompleted | |
| 474 ('adb -s 0123456789abcdef wait-for-device', ''), | |
| 475 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', | |
| 476 '1\r\n'), | |
| 477 # AndroidCommands.WaitForDevicePm | |
| 478 ('adb -s 0123456789abcdef wait-for-device', ''), | |
| 479 ('adb -s 0123456789abcdef shell pm path android', | |
| 480 'package:this.is.a.test.package'), | |
| 481 ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", '\r\n')]): | |
| 482 with self.assertRaises(device_errors.CommandFailedError): | |
| 483 self.device.WaitUntilFullyBooted(wifi=False) | |
| 484 | |
| 485 def testWaitUntilFullyBooted_sdCardReadyFails_emptyPath(self): | |
| 486 with mock.patch('time.sleep'): | |
| 487 with self.assertCallsSequence([ | |
| 488 # AndroidCommands.WaitForSystemBootCompleted | |
| 489 ('adb -s 0123456789abcdef wait-for-device', ''), | |
| 490 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', | |
| 491 '1\r\n'), | |
| 492 # AndroidCommands.WaitForDevicePm | |
| 493 ('adb -s 0123456789abcdef wait-for-device', ''), | |
| 494 ('adb -s 0123456789abcdef shell pm path android', | |
| 495 'package:this.is.a.test.package'), | |
| 496 ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", | |
| 497 '/fake/storage/path\r\n'), | |
| 498 ("adb -s 0123456789abcdef shell 'ls /fake/storage/path'", '')]): | |
| 499 with self.assertRaises(device_errors.CommandTimeoutError): | |
| 500 self.device.WaitUntilFullyBooted(wifi=False) | |
| 501 | 531 |
| 502 | 532 |
| 503 class DeviceUtilsRebootTest(DeviceUtilsOldImplTest): | 533 class DeviceUtilsRebootTest(DeviceUtilsNewImplTest): |
| 504 | 534 |
| 505 def testReboot_nonBlocking(self): | 535 def testReboot_nonBlocking(self): |
| 506 with mock.patch('time.sleep'): | 536 self.adb.Reboot = mock.Mock() |
| 507 with self.assertCallsSequence([ | 537 self.device.IsOnline = mock.Mock(return_value=False) |
| 508 ('adb -s 0123456789abcdef reboot', ''), | 538 self.device.Reboot(block=False) |
| 509 ('adb -s 0123456789abcdef devices', 'unknown\r\n'), | 539 self.adb.Reboot.assert_called_once_with() |
| 510 ('adb -s 0123456789abcdef wait-for-device', ''), | 540 self.device.IsOnline.assert_called_once_with() |
| 511 ('adb -s 0123456789abcdef shell pm path android', | |
| 512 'package:this.is.a.test.package'), | |
| 513 ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", | |
| 514 '/fake/storage/path\r\n'), | |
| 515 ("adb -s 0123456789abcdef shell 'ls /fake/storage/path'", | |
| 516 'nothing\r\n')]): | |
| 517 self.device.Reboot(block=False) | |
| 518 | 541 |
| 519 def testReboot_blocking(self): | 542 def testReboot_blocking(self): |
| 520 with mock.patch('time.sleep'): | 543 self.adb.Reboot = mock.Mock() |
| 521 with self.assertCallsSequence([ | 544 self.device.IsOnline = mock.Mock(return_value=False) |
| 522 ('adb -s 0123456789abcdef reboot', ''), | 545 with self.assertShellCallSequence([ |
| 523 ('adb -s 0123456789abcdef devices', 'unknown\r\n'), | 546 # sc_card_ready |
| 524 ('adb -s 0123456789abcdef wait-for-device', ''), | 547 ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'), |
| 525 ('adb -s 0123456789abcdef shell pm path android', | 548 ('ls /fake/storage/path', '/fake/storage/path\r\n'), |
| 526 'package:this.is.a.test.package'), | 549 # pm_ready |
| 527 ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", | 550 ('pm path android', 'package:this.is.a.test.package\r\n'), |
| 528 '/fake/storage/path\r\n'), | 551 # boot_completed |
| 529 ("adb -s 0123456789abcdef shell 'ls /fake/storage/path'", | 552 ('getprop sys.boot_completed', '1\r\n')]): |
| 530 'nothing\r\n'), | 553 self.device.Reboot(block=True) |
| 531 ('adb -s 0123456789abcdef wait-for-device', ''), | 554 self.adb.Reboot.assert_called_once_with() |
| 532 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', | 555 self.device.IsOnline.assert_called_once_with() |
| 533 '1\r\n'), | 556 self.adb.WaitForDevice.assert_called_once_with() |
| 534 ('adb -s 0123456789abcdef wait-for-device', ''), | |
| 535 ('adb -s 0123456789abcdef shell pm path android', | |
| 536 'package:this.is.a.test.package'), | |
| 537 ("adb -s 0123456789abcdef shell 'ls /fake/storage/path'", | |
| 538 'nothing\r\n')]): | |
| 539 self.device.Reboot(block=True) | |
| 540 | 557 |
| 541 | 558 |
| 542 class DeviceUtilsInstallTest(DeviceUtilsOldImplTest): | 559 class DeviceUtilsInstallTest(DeviceUtilsOldImplTest): |
| 543 | 560 |
| 544 def testInstall_noPriorInstall(self): | 561 def testInstall_noPriorInstall(self): |
| 545 with mock.patch('os.path.isfile', return_value=True), ( | 562 with mock.patch('os.path.isfile', return_value=True), ( |
| 546 mock.patch('pylib.utils.apk_helper.GetPackageName', | 563 mock.patch('pylib.utils.apk_helper.GetPackageName', |
| 547 return_value='this.is.a.test.package')): | 564 return_value='this.is.a.test.package')): |
| 548 with self.assertCallsSequence([ | 565 with self.assertCallsSequence([ |
| 549 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", | 566 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 691 with self.assertShellCall(cmd, '\r\n'): | 708 with self.assertShellCall(cmd, '\r\n'): |
| 692 self.assertEquals('', | 709 self.assertEquals('', |
| 693 self.device.RunShellCommand(cmd, single_line=True)) | 710 self.device.RunShellCommand(cmd, single_line=True)) |
| 694 | 711 |
| 695 def testRunShellCommand_singleLine_successWithoutEndLine(self): | 712 def testRunShellCommand_singleLine_successWithoutEndLine(self): |
| 696 cmd = 'echo -n $VALUE' | 713 cmd = 'echo -n $VALUE' |
| 697 with self.assertShellCall(cmd, 'some value'): | 714 with self.assertShellCall(cmd, 'some value'): |
| 698 self.assertEquals('some value', | 715 self.assertEquals('some value', |
| 699 self.device.RunShellCommand(cmd, single_line=True)) | 716 self.device.RunShellCommand(cmd, single_line=True)) |
| 700 | 717 |
| 701 def testRunShellCommand_singleLine_failNoLines(self): | 718 def testRunShellCommand_singleLine_successNoOutput(self): |
| 702 cmd = 'echo $VALUE' | 719 cmd = 'echo -n $VALUE' |
| 703 with self.assertShellCall(cmd, ''): | 720 with self.assertShellCall(cmd, ''): |
| 704 with self.assertRaises(device_errors.CommandFailedError): | 721 self.assertEquals('', |
| 705 self.device.RunShellCommand(cmd, single_line=True) | 722 self.device.RunShellCommand(cmd, single_line=True)) |
| 706 | 723 |
| 707 def testRunShellCommand_singleLine_failTooManyLines(self): | 724 def testRunShellCommand_singleLine_failTooManyLines(self): |
| 708 cmd = 'echo $VALUE' | 725 cmd = 'echo $VALUE' |
| 709 with self.assertShellCall(cmd, 'some value\r\nanother value\r\n'): | 726 with self.assertShellCall(cmd, 'some value\r\nanother value\r\n'): |
| 710 with self.assertRaises(device_errors.CommandFailedError): | 727 with self.assertRaises(device_errors.CommandFailedError): |
| 711 self.device.RunShellCommand(cmd, single_line=True) | 728 self.device.RunShellCommand(cmd, single_line=True) |
| 712 | 729 |
| 713 def testRunShellCommand_checkReturn_success(self): | 730 def testRunShellCommand_checkReturn_success(self): |
| 714 cmd = 'echo $ANDROID_DATA' | 731 cmd = 'echo $ANDROID_DATA' |
| 715 output = '/data\r\n' | 732 output = '/data\r\n' |
| (...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1061 def testPushChangedFilesZipped_empty(self): | 1078 def testPushChangedFilesZipped_empty(self): |
| 1062 test_files = [] | 1079 test_files = [] |
| 1063 self.device._PushChangedFilesZipped(test_files) | 1080 self.device._PushChangedFilesZipped(test_files) |
| 1064 self.assertEqual(0, self.adb.Push.call_count) | 1081 self.assertEqual(0, self.adb.Push.call_count) |
| 1065 | 1082 |
| 1066 def testPushChangedFilesZipped_single(self): | 1083 def testPushChangedFilesZipped_single(self): |
| 1067 test_files = [('/test/host/path/file1', '/test/device/path/file1')] | 1084 test_files = [('/test/host/path/file1', '/test/device/path/file1')] |
| 1068 | 1085 |
| 1069 self.device._GetExternalStoragePathImpl = mock.Mock( | 1086 self.device._GetExternalStoragePathImpl = mock.Mock( |
| 1070 return_value='/test/device/external_dir') | 1087 return_value='/test/device/external_dir') |
| 1071 self.device._IsOnlineImpl = mock.Mock(return_value=True) | 1088 self.device.IsOnline = mock.Mock(return_value=True) |
| 1072 self.device._RunShellCommandImpl = mock.Mock() | 1089 self.device._RunShellCommandImpl = mock.Mock() |
| 1073 mock_zip_temp = mock.mock_open() | 1090 mock_zip_temp = mock.mock_open() |
| 1074 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip' | 1091 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip' |
| 1075 with mock.patch('multiprocessing.Process') as mock_zip_proc, ( | 1092 with mock.patch('multiprocessing.Process') as mock_zip_proc, ( |
| 1076 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)): | 1093 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)): |
| 1077 self.device._PushChangedFilesZipped(test_files) | 1094 self.device._PushChangedFilesZipped(test_files) |
| 1078 | 1095 |
| 1079 mock_zip_proc.assert_called_once_with( | 1096 mock_zip_proc.assert_called_once_with( |
| 1080 target=device_utils.DeviceUtils._CreateDeviceZip, | 1097 target=device_utils.DeviceUtils._CreateDeviceZip, |
| 1081 args=('/test/temp/file/tmp.zip', test_files)) | 1098 args=('/test/temp/file/tmp.zip', test_files)) |
| 1082 self.adb.Push.assert_called_once_with( | 1099 self.adb.Push.assert_called_once_with( |
| 1083 '/test/temp/file/tmp.zip', '/test/device/external_dir/tmp.zip') | 1100 '/test/temp/file/tmp.zip', '/test/device/external_dir/tmp.zip') |
| 1084 self.assertEqual(2, self.device._RunShellCommandImpl.call_count) | 1101 self.assertEqual(2, self.device._RunShellCommandImpl.call_count) |
| 1085 self.device._RunShellCommandImpl.assert_any_call( | 1102 self.device._RunShellCommandImpl.assert_any_call( |
| 1086 ['unzip', '/test/device/external_dir/tmp.zip'], | 1103 ['unzip', '/test/device/external_dir/tmp.zip'], |
| 1087 as_root=True, | 1104 as_root=True, |
| 1088 env={'PATH': '$PATH:/data/local/tmp/bin'}, | 1105 env={'PATH': '$PATH:/data/local/tmp/bin'}, |
| 1089 check_return=True) | 1106 check_return=True) |
| 1090 self.device._RunShellCommandImpl.assert_any_call( | 1107 self.device._RunShellCommandImpl.assert_any_call( |
| 1091 ['rm', '/test/device/external_dir/tmp.zip'], check_return=True) | 1108 ['rm', '/test/device/external_dir/tmp.zip'], check_return=True) |
| 1092 | 1109 |
| 1093 def testPushChangedFilesZipped_multiple(self): | 1110 def testPushChangedFilesZipped_multiple(self): |
| 1094 test_files = [('/test/host/path/file1', '/test/device/path/file1'), | 1111 test_files = [('/test/host/path/file1', '/test/device/path/file1'), |
| 1095 ('/test/host/path/file2', '/test/device/path/file2')] | 1112 ('/test/host/path/file2', '/test/device/path/file2')] |
| 1096 | 1113 |
| 1097 self.device._GetExternalStoragePathImpl = mock.Mock( | 1114 self.device._GetExternalStoragePathImpl = mock.Mock( |
| 1098 return_value='/test/device/external_dir') | 1115 return_value='/test/device/external_dir') |
| 1099 self.device._IsOnlineImpl = mock.Mock(return_value=True) | 1116 self.device.IsOnline = mock.Mock(return_value=True) |
| 1100 self.device._RunShellCommandImpl = mock.Mock() | 1117 self.device._RunShellCommandImpl = mock.Mock() |
| 1101 mock_zip_temp = mock.mock_open() | 1118 mock_zip_temp = mock.mock_open() |
| 1102 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip' | 1119 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip' |
| 1103 with mock.patch('multiprocessing.Process') as mock_zip_proc, ( | 1120 with mock.patch('multiprocessing.Process') as mock_zip_proc, ( |
| 1104 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)): | 1121 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)): |
| 1105 self.device._PushChangedFilesZipped(test_files) | 1122 self.device._PushChangedFilesZipped(test_files) |
| 1106 | 1123 |
| 1107 mock_zip_proc.assert_called_once_with( | 1124 mock_zip_proc.assert_called_once_with( |
| 1108 target=device_utils.DeviceUtils._CreateDeviceZip, | 1125 target=device_utils.DeviceUtils._CreateDeviceZip, |
| 1109 args=('/test/temp/file/tmp.zip', test_files)) | 1126 args=('/test/temp/file/tmp.zip', test_files)) |
| (...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1476 '%s\r\n' % constants.DEVICE_LOCAL_PROPERTIES_PATH), | 1493 '%s\r\n' % constants.DEVICE_LOCAL_PROPERTIES_PATH), |
| 1477 ('adb -s 0123456789abcdef pull %s %s' % | 1494 ('adb -s 0123456789abcdef pull %s %s' % |
| 1478 (constants.DEVICE_LOCAL_PROPERTIES_PATH, mock_file.name), | 1495 (constants.DEVICE_LOCAL_PROPERTIES_PATH, mock_file.name), |
| 1479 '100 B/s (100 bytes in 1.000s)\r\n'), | 1496 '100 B/s (100 bytes in 1.000s)\r\n'), |
| 1480 ('adb -s 0123456789abcdef shell ' | 1497 ('adb -s 0123456789abcdef shell ' |
| 1481 'getprop dalvik.vm.enableassertions', | 1498 'getprop dalvik.vm.enableassertions', |
| 1482 'all\r\n')]): | 1499 'all\r\n')]): |
| 1483 self.assertFalse(self.device.SetJavaAsserts(True)) | 1500 self.assertFalse(self.device.SetJavaAsserts(True)) |
| 1484 | 1501 |
| 1485 | 1502 |
| 1486 class DeviceUtilsGetPropTest(DeviceUtilsOldImplTest): | 1503 class DeviceUtilsGetPropTest(DeviceUtilsNewImplTest): |
| 1487 | 1504 |
| 1488 def testGetProp_exists(self): | 1505 def testGetProp_exists(self): |
| 1489 with self.assertCalls( | 1506 with self.assertShellCall('getprop this.is.a.test.property', |
| 1490 'adb -s 0123456789abcdef shell getprop this.is.a.test.property', | 1507 'test_property_value\r\n'): |
| 1491 'test_property_value\r\n'): | |
| 1492 self.assertEqual('test_property_value', | 1508 self.assertEqual('test_property_value', |
| 1493 self.device.GetProp('this.is.a.test.property')) | 1509 self.device.GetProp('this.is.a.test.property')) |
| 1494 | 1510 |
| 1495 def testGetProp_doesNotExist(self): | 1511 def testGetProp_doesNotExist(self): |
| 1496 with self.assertCalls( | 1512 with self.assertShellCall('getprop this.property.does.not.exist', |
| 1497 'adb -s 0123456789abcdef shell ' | 1513 '\r\n'): |
| 1498 'getprop this.property.does.not.exist', ''): | |
| 1499 self.assertEqual('', self.device.GetProp('this.property.does.not.exist')) | 1514 self.assertEqual('', self.device.GetProp('this.property.does.not.exist')) |
| 1500 | 1515 |
| 1501 def testGetProp_cachedRoProp(self): | 1516 def testGetProp_cachedRoProp(self): |
| 1502 with self.assertCalls( | 1517 with self.assertShellCall('getprop ro.build.type', |
| 1503 'adb -s 0123456789abcdef shell ' | 1518 'userdebug\r\n'): |
| 1504 'getprop ro.build.type', 'userdebug'): | 1519 self.assertEqual('userdebug', |
| 1505 self.assertEqual('userdebug', self.device.GetProp('ro.build.type')) | 1520 self.device.GetProp('ro.build.type', cache=True)) |
| 1506 self.assertEqual('userdebug', self.device.GetProp('ro.build.type')) | 1521 self.assertEqual('userdebug', |
| 1522 self.device.GetProp('ro.build.type', cache=True)) |
| 1523 |
| 1524 def testGetProp_retryAndCache(self): |
| 1525 with self.assertShellCallSequence([ |
| 1526 ('getprop ro.build.type', _ShellError()), |
| 1527 ('getprop ro.build.type', _ShellError()), |
| 1528 ('getprop ro.build.type', 'userdebug\r\n')]): |
| 1529 self.assertEqual('userdebug', |
| 1530 self.device.GetProp('ro.build.type', |
| 1531 cache=True, retries=3)) |
| 1532 self.assertEqual('userdebug', |
| 1533 self.device.GetProp('ro.build.type', |
| 1534 cache=True, retries=3)) |
| 1507 | 1535 |
| 1508 | 1536 |
| 1509 class DeviceUtilsSetPropTest(DeviceUtilsOldImplTest): | 1537 class DeviceUtilsSetPropTest(DeviceUtilsNewImplTest): |
| 1510 | 1538 |
| 1511 def testSetProp(self): | 1539 def testSetProp(self): |
| 1512 with self.assertCalls( | 1540 with self.assertShellCall( |
| 1513 'adb -s 0123456789abcdef shell ' | 1541 "setprop this.is.a.test.property 'test property value'"): |
| 1514 'setprop this.is.a.test.property "test_property_value"', | 1542 self.device.SetProp('this.is.a.test.property', 'test property value') |
| 1515 ''): | |
| 1516 self.device.SetProp('this.is.a.test.property', 'test_property_value') | |
| 1517 | 1543 |
| 1518 | 1544 |
| 1519 class DeviceUtilsGetPidsTest(DeviceUtilsNewImplTest): | 1545 class DeviceUtilsGetPidsTest(DeviceUtilsNewImplTest): |
| 1520 | 1546 |
| 1521 def testGetPids_noMatches(self): | 1547 def testGetPids_noMatches(self): |
| 1522 with self.assertShellCall( | 1548 with self.assertShellCall( |
| 1523 'ps', | 1549 'ps', |
| 1524 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 1550 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
| 1525 'user 1000 100 1024 1024 ffffffff 00000000 no.match\r\n'): | 1551 'user 1000 100 1024 1024 ffffffff 00000000 no.match\r\n'): |
| 1526 self.assertEqual({}, self.device.GetPids('does.not.match')) | 1552 self.assertEqual({}, self.device.GetPids('does.not.match')) |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1646 self.device = device_utils.DeviceUtils(None) | 1672 self.device = device_utils.DeviceUtils(None) |
| 1647 with self.assertCalls('adb get-serialno', 'unknown'), ( | 1673 with self.assertCalls('adb get-serialno', 'unknown'), ( |
| 1648 self.assertRaises(device_errors.NoDevicesError)): | 1674 self.assertRaises(device_errors.NoDevicesError)): |
| 1649 str(self.device) | 1675 str(self.device) |
| 1650 | 1676 |
| 1651 | 1677 |
| 1652 if __name__ == '__main__': | 1678 if __name__ == '__main__': |
| 1653 logging.getLogger().setLevel(logging.DEBUG) | 1679 logging.getLogger().setLevel(logging.DEBUG) |
| 1654 unittest.main(verbosity=2) | 1680 unittest.main(verbosity=2) |
| 1655 | 1681 |
| OLD | NEW |