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=10, 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=10, 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() |
444 | 464 |
445 def testWaitUntilFullyBooted_bootFails(self): | 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): |
446 with mock.patch('time.sleep'): | 473 with mock.patch('time.sleep'): |
447 with self.assertCallsSequence([ | 474 with self.assertShellCallSequence([ |
448 # AndroidCommands.WaitForSystemBootCompleted | 475 # sc_card_ready |
449 ('adb -s 0123456789abcdef wait-for-device', ''), | 476 ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'), |
450 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', | 477 ('ls /fake/storage/path', '\r\n'), |
451 '0\r\n')]): | 478 # sc_card_ready |
| 479 ('ls /fake/storage/path', '\r\n'), |
| 480 # sc_card_ready |
| 481 ('ls /fake/storage/path', _CmdTimeout())]): |
452 with self.assertRaises(device_errors.CommandTimeoutError): | 482 with self.assertRaises(device_errors.CommandTimeoutError): |
453 self.device.WaitUntilFullyBooted(wifi=False) | 483 self.device.WaitUntilFullyBooted(wifi=False) |
454 | 484 |
455 def testWaitUntilFullyBooted_devicePmFails(self): | 485 def testWaitUntilFullyBooted_devicePmFails(self): |
456 with mock.patch('time.sleep'): | 486 with mock.patch('time.sleep'): |
457 with self.assertCallsSequence([ | 487 with self.assertShellCallSequence([ |
458 # AndroidCommands.WaitForSystemBootCompleted | 488 # sc_card_ready |
459 ('adb -s 0123456789abcdef wait-for-device', ''), | 489 ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'), |
460 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', | 490 ('ls /fake/storage/path', '/fake/storage/path\r\n'), |
461 '1\r\n')] | 491 # pm_ready |
462 # AndroidCommands.WaitForDevicePm | 492 ('pm path android', 'Error. Is package manager running?\r\n'), |
463 + 3 * ([('adb -s 0123456789abcdef wait-for-device', '')] | 493 # pm_ready |
464 + 24 * [('adb -s 0123456789abcdef shell pm path android', '\r\n')] | 494 ('pm path android', 'Error. Is package manager running?\r\n'), |
465 + [("adb -s 0123456789abcdef shell 'stop'", '\r\n'), | 495 # pm_ready |
466 ("adb -s 0123456789abcdef shell 'start'", '\r\n')])): | 496 ('pm path android', _CmdTimeout())]): |
467 with self.assertRaises(device_errors.CommandTimeoutError): | 497 with self.assertRaises(device_errors.CommandTimeoutError): |
468 self.device.WaitUntilFullyBooted(wifi=False) | 498 self.device.WaitUntilFullyBooted(wifi=False) |
469 | 499 |
470 def testWaitUntilFullyBooted_sdCardReadyFails_noPath(self): | 500 def testWaitUntilFullyBooted_bootFails(self): |
471 with mock.patch('time.sleep'): | 501 with mock.patch('time.sleep'): |
472 with self.assertCallsSequence([ | 502 with self.assertShellCallSequence([ |
473 # AndroidCommands.WaitForSystemBootCompleted | 503 # sc_card_ready |
474 ('adb -s 0123456789abcdef wait-for-device', ''), | 504 ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'), |
475 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', | 505 ('ls /fake/storage/path', '/fake/storage/path\r\n'), |
476 '1\r\n'), | 506 # pm_ready |
477 # AndroidCommands.WaitForDevicePm | 507 ('pm path android', 'package:this.is.a.test.package\r\n'), |
478 ('adb -s 0123456789abcdef wait-for-device', ''), | 508 # boot_completed |
479 ('adb -s 0123456789abcdef shell pm path android', | 509 ('getprop sys.boot_completed', '0\r\n'), |
480 'package:this.is.a.test.package'), | 510 # boot_completed |
481 ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", '\r\n')]): | 511 ('getprop sys.boot_completed', '0\r\n'), |
482 with self.assertRaises(device_errors.CommandFailedError): | 512 # boot_completed |
483 self.device.WaitUntilFullyBooted(wifi=False) | 513 ('getprop sys.boot_completed', _CmdTimeout())]): |
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): | 514 with self.assertRaises(device_errors.CommandTimeoutError): |
500 self.device.WaitUntilFullyBooted(wifi=False) | 515 self.device.WaitUntilFullyBooted(wifi=False) |
501 | 516 |
| 517 def testWaitUntilFullyBooted_wifiFails(self): |
| 518 with mock.patch('time.sleep'): |
| 519 with self.assertShellCallSequence([ |
| 520 # sc_card_ready |
| 521 ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'), |
| 522 ('ls /fake/storage/path', '/fake/storage/path\r\n'), |
| 523 # pm_ready |
| 524 ('pm path android', 'package:this.is.a.test.package\r\n'), |
| 525 # boot_completed |
| 526 ('getprop sys.boot_completed', '1\r\n'), |
| 527 # wifi_enabled |
| 528 ('dumpsys wifi', 'stuff\r\nmore stuff\r\n'), |
| 529 # wifi_enabled |
| 530 ('dumpsys wifi', 'stuff\r\nmore stuff\r\n'), |
| 531 # wifi_enabled |
| 532 ('dumpsys wifi', _CmdTimeout())]): |
| 533 with self.assertRaises(device_errors.CommandTimeoutError): |
| 534 self.device.WaitUntilFullyBooted(wifi=True) |
502 | 535 |
503 class DeviceUtilsRebootTest(DeviceUtilsOldImplTest): | 536 |
| 537 class DeviceUtilsRebootTest(DeviceUtilsNewImplTest): |
504 | 538 |
505 def testReboot_nonBlocking(self): | 539 def testReboot_nonBlocking(self): |
506 with mock.patch('time.sleep'): | 540 self.adb.Reboot = mock.Mock() |
507 with self.assertCallsSequence([ | 541 self.device.IsOnline = mock.Mock(return_value=False) |
508 ('adb -s 0123456789abcdef reboot', ''), | 542 self.device.Reboot(block=False) |
509 ('adb -s 0123456789abcdef devices', 'unknown\r\n'), | 543 self.adb.Reboot.assert_called_once_with() |
510 ('adb -s 0123456789abcdef wait-for-device', ''), | 544 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 | 545 |
519 def testReboot_blocking(self): | 546 def testReboot_blocking(self): |
520 with mock.patch('time.sleep'): | 547 self.adb.Reboot = mock.Mock() |
521 with self.assertCallsSequence([ | 548 self.device.IsOnline = mock.Mock(return_value=False) |
522 ('adb -s 0123456789abcdef reboot', ''), | 549 with self.assertShellCallSequence([ |
523 ('adb -s 0123456789abcdef devices', 'unknown\r\n'), | 550 # sc_card_ready |
524 ('adb -s 0123456789abcdef wait-for-device', ''), | 551 ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'), |
525 ('adb -s 0123456789abcdef shell pm path android', | 552 ('ls /fake/storage/path', '/fake/storage/path\r\n'), |
526 'package:this.is.a.test.package'), | 553 # pm_ready |
527 ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", | 554 ('pm path android', 'package:this.is.a.test.package\r\n'), |
528 '/fake/storage/path\r\n'), | 555 # boot_completed |
529 ("adb -s 0123456789abcdef shell 'ls /fake/storage/path'", | 556 ('getprop sys.boot_completed', '1\r\n')]): |
530 'nothing\r\n'), | 557 self.device.Reboot(block=True) |
531 ('adb -s 0123456789abcdef wait-for-device', ''), | 558 self.adb.Reboot.assert_called_once_with() |
532 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', | 559 self.device.IsOnline.assert_called_once_with() |
533 '1\r\n'), | 560 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 | 561 |
541 | 562 |
542 class DeviceUtilsInstallTest(DeviceUtilsOldImplTest): | 563 class DeviceUtilsInstallTest(DeviceUtilsOldImplTest): |
543 | 564 |
544 def testInstall_noPriorInstall(self): | 565 def testInstall_noPriorInstall(self): |
545 with mock.patch('os.path.isfile', return_value=True), ( | 566 with mock.patch('os.path.isfile', return_value=True), ( |
546 mock.patch('pylib.utils.apk_helper.GetPackageName', | 567 mock.patch('pylib.utils.apk_helper.GetPackageName', |
547 return_value='this.is.a.test.package')): | 568 return_value='this.is.a.test.package')): |
548 with self.assertCallsSequence([ | 569 with self.assertCallsSequence([ |
549 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", | 570 ("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'): | 712 with self.assertShellCall(cmd, '\r\n'): |
692 self.assertEquals('', | 713 self.assertEquals('', |
693 self.device.RunShellCommand(cmd, single_line=True)) | 714 self.device.RunShellCommand(cmd, single_line=True)) |
694 | 715 |
695 def testRunShellCommand_singleLine_successWithoutEndLine(self): | 716 def testRunShellCommand_singleLine_successWithoutEndLine(self): |
696 cmd = 'echo -n $VALUE' | 717 cmd = 'echo -n $VALUE' |
697 with self.assertShellCall(cmd, 'some value'): | 718 with self.assertShellCall(cmd, 'some value'): |
698 self.assertEquals('some value', | 719 self.assertEquals('some value', |
699 self.device.RunShellCommand(cmd, single_line=True)) | 720 self.device.RunShellCommand(cmd, single_line=True)) |
700 | 721 |
701 def testRunShellCommand_singleLine_failNoLines(self): | 722 def testRunShellCommand_singleLine_successNoOutput(self): |
702 cmd = 'echo $VALUE' | 723 cmd = 'echo -n $VALUE' |
703 with self.assertShellCall(cmd, ''): | 724 with self.assertShellCall(cmd, ''): |
704 with self.assertRaises(device_errors.CommandFailedError): | 725 self.assertEquals('', |
705 self.device.RunShellCommand(cmd, single_line=True) | 726 self.device.RunShellCommand(cmd, single_line=True)) |
706 | 727 |
707 def testRunShellCommand_singleLine_failTooManyLines(self): | 728 def testRunShellCommand_singleLine_failTooManyLines(self): |
708 cmd = 'echo $VALUE' | 729 cmd = 'echo $VALUE' |
709 with self.assertShellCall(cmd, 'some value\r\nanother value\r\n'): | 730 with self.assertShellCall(cmd, 'some value\r\nanother value\r\n'): |
710 with self.assertRaises(device_errors.CommandFailedError): | 731 with self.assertRaises(device_errors.CommandFailedError): |
711 self.device.RunShellCommand(cmd, single_line=True) | 732 self.device.RunShellCommand(cmd, single_line=True) |
712 | 733 |
713 def testRunShellCommand_checkReturn_success(self): | 734 def testRunShellCommand_checkReturn_success(self): |
714 cmd = 'echo $ANDROID_DATA' | 735 cmd = 'echo $ANDROID_DATA' |
715 output = '/data\r\n' | 736 output = '/data\r\n' |
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1061 def testPushChangedFilesZipped_empty(self): | 1082 def testPushChangedFilesZipped_empty(self): |
1062 test_files = [] | 1083 test_files = [] |
1063 self.device._PushChangedFilesZipped(test_files) | 1084 self.device._PushChangedFilesZipped(test_files) |
1064 self.assertEqual(0, self.adb.Push.call_count) | 1085 self.assertEqual(0, self.adb.Push.call_count) |
1065 | 1086 |
1066 def testPushChangedFilesZipped_single(self): | 1087 def testPushChangedFilesZipped_single(self): |
1067 test_files = [('/test/host/path/file1', '/test/device/path/file1')] | 1088 test_files = [('/test/host/path/file1', '/test/device/path/file1')] |
1068 | 1089 |
1069 self.device._GetExternalStoragePathImpl = mock.Mock( | 1090 self.device._GetExternalStoragePathImpl = mock.Mock( |
1070 return_value='/test/device/external_dir') | 1091 return_value='/test/device/external_dir') |
1071 self.device._IsOnlineImpl = mock.Mock(return_value=True) | 1092 self.device.IsOnline = mock.Mock(return_value=True) |
1072 self.device._RunShellCommandImpl = mock.Mock() | 1093 self.device._RunShellCommandImpl = mock.Mock() |
1073 mock_zip_temp = mock.mock_open() | 1094 mock_zip_temp = mock.mock_open() |
1074 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip' | 1095 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip' |
1075 with mock.patch('multiprocessing.Process') as mock_zip_proc, ( | 1096 with mock.patch('multiprocessing.Process') as mock_zip_proc, ( |
1076 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)): | 1097 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)): |
1077 self.device._PushChangedFilesZipped(test_files) | 1098 self.device._PushChangedFilesZipped(test_files) |
1078 | 1099 |
1079 mock_zip_proc.assert_called_once_with( | 1100 mock_zip_proc.assert_called_once_with( |
1080 target=device_utils.DeviceUtils._CreateDeviceZip, | 1101 target=device_utils.DeviceUtils._CreateDeviceZip, |
1081 args=('/test/temp/file/tmp.zip', test_files)) | 1102 args=('/test/temp/file/tmp.zip', test_files)) |
1082 self.adb.Push.assert_called_once_with( | 1103 self.adb.Push.assert_called_once_with( |
1083 '/test/temp/file/tmp.zip', '/test/device/external_dir/tmp.zip') | 1104 '/test/temp/file/tmp.zip', '/test/device/external_dir/tmp.zip') |
1084 self.assertEqual(2, self.device._RunShellCommandImpl.call_count) | 1105 self.assertEqual(2, self.device._RunShellCommandImpl.call_count) |
1085 self.device._RunShellCommandImpl.assert_any_call( | 1106 self.device._RunShellCommandImpl.assert_any_call( |
1086 ['unzip', '/test/device/external_dir/tmp.zip'], | 1107 ['unzip', '/test/device/external_dir/tmp.zip'], |
1087 as_root=True, | 1108 as_root=True, |
1088 env={'PATH': '$PATH:/data/local/tmp/bin'}, | 1109 env={'PATH': '$PATH:/data/local/tmp/bin'}, |
1089 check_return=True) | 1110 check_return=True) |
1090 self.device._RunShellCommandImpl.assert_any_call( | 1111 self.device._RunShellCommandImpl.assert_any_call( |
1091 ['rm', '/test/device/external_dir/tmp.zip'], check_return=True) | 1112 ['rm', '/test/device/external_dir/tmp.zip'], check_return=True) |
1092 | 1113 |
1093 def testPushChangedFilesZipped_multiple(self): | 1114 def testPushChangedFilesZipped_multiple(self): |
1094 test_files = [('/test/host/path/file1', '/test/device/path/file1'), | 1115 test_files = [('/test/host/path/file1', '/test/device/path/file1'), |
1095 ('/test/host/path/file2', '/test/device/path/file2')] | 1116 ('/test/host/path/file2', '/test/device/path/file2')] |
1096 | 1117 |
1097 self.device._GetExternalStoragePathImpl = mock.Mock( | 1118 self.device._GetExternalStoragePathImpl = mock.Mock( |
1098 return_value='/test/device/external_dir') | 1119 return_value='/test/device/external_dir') |
1099 self.device._IsOnlineImpl = mock.Mock(return_value=True) | 1120 self.device.IsOnline = mock.Mock(return_value=True) |
1100 self.device._RunShellCommandImpl = mock.Mock() | 1121 self.device._RunShellCommandImpl = mock.Mock() |
1101 mock_zip_temp = mock.mock_open() | 1122 mock_zip_temp = mock.mock_open() |
1102 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip' | 1123 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip' |
1103 with mock.patch('multiprocessing.Process') as mock_zip_proc, ( | 1124 with mock.patch('multiprocessing.Process') as mock_zip_proc, ( |
1104 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)): | 1125 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)): |
1105 self.device._PushChangedFilesZipped(test_files) | 1126 self.device._PushChangedFilesZipped(test_files) |
1106 | 1127 |
1107 mock_zip_proc.assert_called_once_with( | 1128 mock_zip_proc.assert_called_once_with( |
1108 target=device_utils.DeviceUtils._CreateDeviceZip, | 1129 target=device_utils.DeviceUtils._CreateDeviceZip, |
1109 args=('/test/temp/file/tmp.zip', test_files)) | 1130 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), | 1497 '%s\r\n' % constants.DEVICE_LOCAL_PROPERTIES_PATH), |
1477 ('adb -s 0123456789abcdef pull %s %s' % | 1498 ('adb -s 0123456789abcdef pull %s %s' % |
1478 (constants.DEVICE_LOCAL_PROPERTIES_PATH, mock_file.name), | 1499 (constants.DEVICE_LOCAL_PROPERTIES_PATH, mock_file.name), |
1479 '100 B/s (100 bytes in 1.000s)\r\n'), | 1500 '100 B/s (100 bytes in 1.000s)\r\n'), |
1480 ('adb -s 0123456789abcdef shell ' | 1501 ('adb -s 0123456789abcdef shell ' |
1481 'getprop dalvik.vm.enableassertions', | 1502 'getprop dalvik.vm.enableassertions', |
1482 'all\r\n')]): | 1503 'all\r\n')]): |
1483 self.assertFalse(self.device.SetJavaAsserts(True)) | 1504 self.assertFalse(self.device.SetJavaAsserts(True)) |
1484 | 1505 |
1485 | 1506 |
1486 class DeviceUtilsGetPropTest(DeviceUtilsOldImplTest): | 1507 class DeviceUtilsGetPropTest(DeviceUtilsNewImplTest): |
1487 | 1508 |
1488 def testGetProp_exists(self): | 1509 def testGetProp_exists(self): |
1489 with self.assertCalls( | 1510 with self.assertShellCall('getprop this.is.a.test.property', |
1490 'adb -s 0123456789abcdef shell getprop this.is.a.test.property', | 1511 'test_property_value\r\n'): |
1491 'test_property_value\r\n'): | |
1492 self.assertEqual('test_property_value', | 1512 self.assertEqual('test_property_value', |
1493 self.device.GetProp('this.is.a.test.property')) | 1513 self.device.GetProp('this.is.a.test.property')) |
1494 | 1514 |
1495 def testGetProp_doesNotExist(self): | 1515 def testGetProp_doesNotExist(self): |
1496 with self.assertCalls( | 1516 with self.assertShellCall('getprop this.property.does.not.exist', |
1497 'adb -s 0123456789abcdef shell ' | 1517 '\r\n'): |
1498 'getprop this.property.does.not.exist', ''): | |
1499 self.assertEqual('', self.device.GetProp('this.property.does.not.exist')) | 1518 self.assertEqual('', self.device.GetProp('this.property.does.not.exist')) |
1500 | 1519 |
1501 def testGetProp_cachedRoProp(self): | 1520 def testGetProp_cachedRoProp(self): |
1502 with self.assertCalls( | 1521 with self.assertShellCall('getprop ro.build.type', |
1503 'adb -s 0123456789abcdef shell ' | 1522 'userdebug\r\n'): |
1504 'getprop ro.build.type', 'userdebug'): | 1523 self.assertEqual('userdebug', |
1505 self.assertEqual('userdebug', self.device.GetProp('ro.build.type')) | 1524 self.device.GetProp('ro.build.type', cache=True)) |
1506 self.assertEqual('userdebug', self.device.GetProp('ro.build.type')) | 1525 self.assertEqual('userdebug', |
| 1526 self.device.GetProp('ro.build.type', cache=True)) |
| 1527 |
| 1528 def testGetProp_retryAndCache(self): |
| 1529 with self.assertShellCallSequence([ |
| 1530 ('getprop ro.build.type', _ShellError()), |
| 1531 ('getprop ro.build.type', _ShellError()), |
| 1532 ('getprop ro.build.type', 'userdebug\r\n')]): |
| 1533 self.assertEqual('userdebug', |
| 1534 self.device.GetProp('ro.build.type', |
| 1535 cache=True, retries=3)) |
| 1536 self.assertEqual('userdebug', |
| 1537 self.device.GetProp('ro.build.type', |
| 1538 cache=True, retries=3)) |
1507 | 1539 |
1508 | 1540 |
1509 class DeviceUtilsSetPropTest(DeviceUtilsOldImplTest): | 1541 class DeviceUtilsSetPropTest(DeviceUtilsNewImplTest): |
1510 | 1542 |
1511 def testSetProp(self): | 1543 def testSetProp(self): |
1512 with self.assertCalls( | 1544 with self.assertShellCall( |
1513 'adb -s 0123456789abcdef shell ' | 1545 "setprop this.is.a.test.property 'test property value'"): |
1514 'setprop this.is.a.test.property "test_property_value"', | 1546 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 | 1547 |
1518 | 1548 |
1519 class DeviceUtilsGetPidsTest(DeviceUtilsNewImplTest): | 1549 class DeviceUtilsGetPidsTest(DeviceUtilsNewImplTest): |
1520 | 1550 |
1521 def testGetPids_noMatches(self): | 1551 def testGetPids_noMatches(self): |
1522 with self.assertShellCall( | 1552 with self.assertShellCall( |
1523 'ps', | 1553 'ps', |
1524 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 1554 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
1525 'user 1000 100 1024 1024 ffffffff 00000000 no.match\r\n'): | 1555 'user 1000 100 1024 1024 ffffffff 00000000 no.match\r\n'): |
1526 self.assertEqual({}, self.device.GetPids('does.not.match')) | 1556 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) | 1676 self.device = device_utils.DeviceUtils(None) |
1647 with self.assertCalls('adb get-serialno', 'unknown'), ( | 1677 with self.assertCalls('adb get-serialno', 'unknown'), ( |
1648 self.assertRaises(device_errors.NoDevicesError)): | 1678 self.assertRaises(device_errors.NoDevicesError)): |
1649 str(self.device) | 1679 str(self.device) |
1650 | 1680 |
1651 | 1681 |
1652 if __name__ == '__main__': | 1682 if __name__ == '__main__': |
1653 logging.getLogger().setLevel(logging.DEBUG) | 1683 logging.getLogger().setLevel(logging.DEBUG) |
1654 unittest.main(verbosity=2) | 1684 unittest.main(verbosity=2) |
1655 | 1685 |
OLD | NEW |