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 DeviceUtilsWaitUntilFullyBootedTest(DeviceUtilsNewImplTest): |
409 | 417 |
410 def testWaitUntilFullyBooted_succeedsNoWifi(self): | 418 def testWaitUntilFullyBooted_succeedsNoWifi(self): |
411 with self.assertCallsSequence([ | 419 with self.assertShellCallSequence([ |
412 # AndroidCommands.WaitForSystemBootCompleted | 420 # sc_card_ready |
413 ('adb -s 0123456789abcdef wait-for-device', ''), | 421 ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'), |
414 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', '1\r\n'), | 422 ('ls /fake/storage/path', '/fake/storage/path\r\n'), |
415 # AndroidCommands.WaitForDevicePm | 423 # pm_ready |
416 ('adb -s 0123456789abcdef wait-for-device', ''), | 424 ('pm path android', 'package:this.is.a.test.package\r\n'), |
417 ('adb -s 0123456789abcdef shell pm path android', | 425 # boot_completed |
418 'package:this.is.a.test.package'), | 426 ('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) | 427 self.device.WaitUntilFullyBooted(wifi=False) |
| 428 self.adb.WaitForDevice.assert_called_once_with() |
426 | 429 |
427 def testWaitUntilFullyBooted_succeedsWithWifi(self): | 430 def testWaitUntilFullyBooted_succeedsWithWifi(self): |
428 with self.assertCallsSequence([ | 431 with self.assertShellCallSequence([ |
429 # AndroidCommands.WaitForSystemBootCompleted | 432 # sc_card_ready |
430 ('adb -s 0123456789abcdef wait-for-device', ''), | 433 ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'), |
431 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', '1\r\n'), | 434 ('ls /fake/storage/path', '/fake/storage/path\r\n'), |
432 # AndroidCommands.WaitForDevicePm | 435 # pm_ready |
433 ('adb -s 0123456789abcdef wait-for-device', ''), | 436 ('pm path android', 'package:this.is.a.test.package\r\n'), |
434 ('adb -s 0123456789abcdef shell pm path android', | 437 # boot_completed |
435 'package:this.is.a.test.package'), | 438 ('getprop sys.boot_completed', '1\r\n'), |
436 # AndroidCommands.WaitForSdCardReady | 439 # wifi_enabled |
437 ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", | 440 ('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) | 441 self.device.WaitUntilFullyBooted(wifi=True) |
| 442 self.adb.WaitForDevice.assert_called_once_with() |
444 | 443 |
445 def testWaitUntilFullyBooted_bootFails(self): | 444 def testWaitUntilFullyBooted_sdCardReadyFails_noPath(self): |
446 with mock.patch('time.sleep'): | 445 with mock.patch('time.sleep'): |
447 with self.assertCallsSequence([ | 446 with self.assertShellCallSequence([ |
448 # AndroidCommands.WaitForSystemBootCompleted | 447 # sc_card_ready |
449 ('adb -s 0123456789abcdef wait-for-device', ''), | 448 ('echo $EXTERNAL_STORAGE', '\r\n'), |
450 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', | 449 # sc_card_ready |
451 '0\r\n')]): | 450 ('echo $EXTERNAL_STORAGE', '\r\n'), |
| 451 # sc_card_ready |
| 452 ('echo $EXTERNAL_STORAGE', _CmdTimeout())]): |
| 453 with self.assertRaises(device_errors.CommandTimeoutError): |
| 454 self.device.WaitUntilFullyBooted(wifi=False) |
| 455 |
| 456 def testWaitUntilFullyBooted_sdCardReadyFails_emptyPath(self): |
| 457 with mock.patch('time.sleep'): |
| 458 with self.assertShellCallSequence([ |
| 459 # sc_card_ready |
| 460 ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'), |
| 461 ('ls /fake/storage/path', '\r\n'), |
| 462 # sc_card_ready |
| 463 ('ls /fake/storage/path', '\r\n'), |
| 464 # sc_card_ready |
| 465 ('ls /fake/storage/path', _CmdTimeout())]): |
452 with self.assertRaises(device_errors.CommandTimeoutError): | 466 with self.assertRaises(device_errors.CommandTimeoutError): |
453 self.device.WaitUntilFullyBooted(wifi=False) | 467 self.device.WaitUntilFullyBooted(wifi=False) |
454 | 468 |
455 def testWaitUntilFullyBooted_devicePmFails(self): | 469 def testWaitUntilFullyBooted_devicePmFails(self): |
456 with mock.patch('time.sleep'): | 470 with mock.patch('time.sleep'): |
457 with self.assertCallsSequence([ | 471 with self.assertShellCallSequence([ |
458 # AndroidCommands.WaitForSystemBootCompleted | 472 # sc_card_ready |
459 ('adb -s 0123456789abcdef wait-for-device', ''), | 473 ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'), |
460 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', | 474 ('ls /fake/storage/path', '/fake/storage/path\r\n'), |
461 '1\r\n')] | 475 # pm_ready |
462 # AndroidCommands.WaitForDevicePm | 476 ('pm path android', 'Error. Is package manager running?\r\n'), |
463 + 3 * ([('adb -s 0123456789abcdef wait-for-device', '')] | 477 # pm_ready |
464 + 24 * [('adb -s 0123456789abcdef shell pm path android', '\r\n')] | 478 ('pm path android', 'Error. Is package manager running?\r\n'), |
465 + [("adb -s 0123456789abcdef shell 'stop'", '\r\n'), | 479 # pm_ready |
466 ("adb -s 0123456789abcdef shell 'start'", '\r\n')])): | 480 ('pm path android', _CmdTimeout())]): |
467 with self.assertRaises(device_errors.CommandTimeoutError): | 481 with self.assertRaises(device_errors.CommandTimeoutError): |
468 self.device.WaitUntilFullyBooted(wifi=False) | 482 self.device.WaitUntilFullyBooted(wifi=False) |
469 | 483 |
470 def testWaitUntilFullyBooted_sdCardReadyFails_noPath(self): | 484 def testWaitUntilFullyBooted_bootFails(self): |
471 with mock.patch('time.sleep'): | 485 with mock.patch('time.sleep'): |
472 with self.assertCallsSequence([ | 486 with self.assertShellCallSequence([ |
473 # AndroidCommands.WaitForSystemBootCompleted | 487 # sc_card_ready |
474 ('adb -s 0123456789abcdef wait-for-device', ''), | 488 ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'), |
475 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', | 489 ('ls /fake/storage/path', '/fake/storage/path\r\n'), |
476 '1\r\n'), | 490 # pm_ready |
477 # AndroidCommands.WaitForDevicePm | 491 ('pm path android', 'package:this.is.a.test.package\r\n'), |
478 ('adb -s 0123456789abcdef wait-for-device', ''), | 492 # boot_completed |
479 ('adb -s 0123456789abcdef shell pm path android', | 493 ('getprop sys.boot_completed', '0\r\n'), |
480 'package:this.is.a.test.package'), | 494 # boot_completed |
481 ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", '\r\n')]): | 495 ('getprop sys.boot_completed', '0\r\n'), |
482 with self.assertRaises(device_errors.CommandFailedError): | 496 # boot_completed |
483 self.device.WaitUntilFullyBooted(wifi=False) | 497 ('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): | 498 with self.assertRaises(device_errors.CommandTimeoutError): |
500 self.device.WaitUntilFullyBooted(wifi=False) | 499 self.device.WaitUntilFullyBooted(wifi=False) |
501 | 500 |
| 501 def testWaitUntilFullyBooted_wifiFails(self): |
| 502 with mock.patch('time.sleep'): |
| 503 with self.assertShellCallSequence([ |
| 504 # sc_card_ready |
| 505 ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'), |
| 506 ('ls /fake/storage/path', '/fake/storage/path\r\n'), |
| 507 # pm_ready |
| 508 ('pm path android', 'package:this.is.a.test.package\r\n'), |
| 509 # boot_completed |
| 510 ('getprop sys.boot_completed', '1\r\n'), |
| 511 # wifi_enabled |
| 512 ('dumpsys wifi', 'stuff\r\nmore stuff\r\n'), |
| 513 # wifi_enabled |
| 514 ('dumpsys wifi', 'stuff\r\nmore stuff\r\n'), |
| 515 # wifi_enabled |
| 516 ('dumpsys wifi', _CmdTimeout())]): |
| 517 with self.assertRaises(device_errors.CommandTimeoutError): |
| 518 self.device.WaitUntilFullyBooted(wifi=True) |
502 | 519 |
503 class DeviceUtilsRebootTest(DeviceUtilsOldImplTest): | 520 class DeviceUtilsRebootTest(DeviceUtilsNewImplTest): |
504 | 521 |
505 def testReboot_nonBlocking(self): | 522 def testReboot_nonBlocking(self): |
| 523 self.adb.Reboot = mock.Mock() |
| 524 self.device.IsOnline = mock.Mock(return_value=False) |
506 with mock.patch('time.sleep'): | 525 with mock.patch('time.sleep'): |
507 with self.assertCallsSequence([ | 526 self.device.Reboot(block=False) |
508 ('adb -s 0123456789abcdef reboot', ''), | 527 self.adb.Reboot.assert_called_once_with() |
509 ('adb -s 0123456789abcdef devices', 'unknown\r\n'), | 528 self.device.IsOnline.assert_called_once_with() |
510 ('adb -s 0123456789abcdef wait-for-device', ''), | |
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 | 529 |
519 def testReboot_blocking(self): | 530 def testReboot_blocking(self): |
| 531 self.adb.Reboot = mock.Mock() |
| 532 self.device.IsOnline = mock.Mock(return_value=False) |
520 with mock.patch('time.sleep'): | 533 with mock.patch('time.sleep'): |
521 with self.assertCallsSequence([ | 534 with self.assertShellCallSequence([ |
522 ('adb -s 0123456789abcdef reboot', ''), | 535 # sc_card_ready |
523 ('adb -s 0123456789abcdef devices', 'unknown\r\n'), | 536 ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'), |
524 ('adb -s 0123456789abcdef wait-for-device', ''), | 537 ('ls /fake/storage/path', '/fake/storage/path\r\n'), |
525 ('adb -s 0123456789abcdef shell pm path android', | 538 # pm_ready |
526 'package:this.is.a.test.package'), | 539 ('pm path android', 'package:this.is.a.test.package\r\n'), |
527 ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", | 540 # boot_completed |
528 '/fake/storage/path\r\n'), | 541 ('getprop sys.boot_completed', '1\r\n')]): |
529 ("adb -s 0123456789abcdef shell 'ls /fake/storage/path'", | |
530 'nothing\r\n'), | |
531 ('adb -s 0123456789abcdef wait-for-device', ''), | |
532 ('adb -s 0123456789abcdef shell getprop sys.boot_completed', | |
533 '1\r\n'), | |
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) | 542 self.device.Reboot(block=True) |
| 543 self.adb.Reboot.assert_called_once_with() |
| 544 self.device.IsOnline.assert_called_once_with() |
| 545 self.adb.WaitForDevice.assert_called_once_with() |
540 | 546 |
541 | 547 |
542 class DeviceUtilsInstallTest(DeviceUtilsOldImplTest): | 548 class DeviceUtilsInstallTest(DeviceUtilsOldImplTest): |
543 | 549 |
544 def testInstall_noPriorInstall(self): | 550 def testInstall_noPriorInstall(self): |
545 with mock.patch('os.path.isfile', return_value=True), ( | 551 with mock.patch('os.path.isfile', return_value=True), ( |
546 mock.patch('pylib.utils.apk_helper.GetPackageName', | 552 mock.patch('pylib.utils.apk_helper.GetPackageName', |
547 return_value='this.is.a.test.package')): | 553 return_value='this.is.a.test.package')): |
548 with self.assertCallsSequence([ | 554 with self.assertCallsSequence([ |
549 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", | 555 ("adb -s 0123456789abcdef shell 'pm path this.is.a.test.package'", |
(...skipping 511 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1061 def testPushChangedFilesZipped_empty(self): | 1067 def testPushChangedFilesZipped_empty(self): |
1062 test_files = [] | 1068 test_files = [] |
1063 self.device._PushChangedFilesZipped(test_files) | 1069 self.device._PushChangedFilesZipped(test_files) |
1064 self.assertEqual(0, self.adb.Push.call_count) | 1070 self.assertEqual(0, self.adb.Push.call_count) |
1065 | 1071 |
1066 def testPushChangedFilesZipped_single(self): | 1072 def testPushChangedFilesZipped_single(self): |
1067 test_files = [('/test/host/path/file1', '/test/device/path/file1')] | 1073 test_files = [('/test/host/path/file1', '/test/device/path/file1')] |
1068 | 1074 |
1069 self.device._GetExternalStoragePathImpl = mock.Mock( | 1075 self.device._GetExternalStoragePathImpl = mock.Mock( |
1070 return_value='/test/device/external_dir') | 1076 return_value='/test/device/external_dir') |
1071 self.device._IsOnlineImpl = mock.Mock(return_value=True) | 1077 self.device.IsOnline = mock.Mock(return_value=True) |
1072 self.device._RunShellCommandImpl = mock.Mock() | 1078 self.device._RunShellCommandImpl = mock.Mock() |
1073 mock_zip_temp = mock.mock_open() | 1079 mock_zip_temp = mock.mock_open() |
1074 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip' | 1080 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip' |
1075 with mock.patch('multiprocessing.Process') as mock_zip_proc, ( | 1081 with mock.patch('multiprocessing.Process') as mock_zip_proc, ( |
1076 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)): | 1082 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)): |
1077 self.device._PushChangedFilesZipped(test_files) | 1083 self.device._PushChangedFilesZipped(test_files) |
1078 | 1084 |
1079 mock_zip_proc.assert_called_once_with( | 1085 mock_zip_proc.assert_called_once_with( |
1080 target=device_utils.DeviceUtils._CreateDeviceZip, | 1086 target=device_utils.DeviceUtils._CreateDeviceZip, |
1081 args=('/test/temp/file/tmp.zip', test_files)) | 1087 args=('/test/temp/file/tmp.zip', test_files)) |
1082 self.adb.Push.assert_called_once_with( | 1088 self.adb.Push.assert_called_once_with( |
1083 '/test/temp/file/tmp.zip', '/test/device/external_dir/tmp.zip') | 1089 '/test/temp/file/tmp.zip', '/test/device/external_dir/tmp.zip') |
1084 self.assertEqual(2, self.device._RunShellCommandImpl.call_count) | 1090 self.assertEqual(2, self.device._RunShellCommandImpl.call_count) |
1085 self.device._RunShellCommandImpl.assert_any_call( | 1091 self.device._RunShellCommandImpl.assert_any_call( |
1086 ['unzip', '/test/device/external_dir/tmp.zip'], | 1092 ['unzip', '/test/device/external_dir/tmp.zip'], |
1087 as_root=True, | 1093 as_root=True, |
1088 env={'PATH': '$PATH:/data/local/tmp/bin'}, | 1094 env={'PATH': '$PATH:/data/local/tmp/bin'}, |
1089 check_return=True) | 1095 check_return=True) |
1090 self.device._RunShellCommandImpl.assert_any_call( | 1096 self.device._RunShellCommandImpl.assert_any_call( |
1091 ['rm', '/test/device/external_dir/tmp.zip'], check_return=True) | 1097 ['rm', '/test/device/external_dir/tmp.zip'], check_return=True) |
1092 | 1098 |
1093 def testPushChangedFilesZipped_multiple(self): | 1099 def testPushChangedFilesZipped_multiple(self): |
1094 test_files = [('/test/host/path/file1', '/test/device/path/file1'), | 1100 test_files = [('/test/host/path/file1', '/test/device/path/file1'), |
1095 ('/test/host/path/file2', '/test/device/path/file2')] | 1101 ('/test/host/path/file2', '/test/device/path/file2')] |
1096 | 1102 |
1097 self.device._GetExternalStoragePathImpl = mock.Mock( | 1103 self.device._GetExternalStoragePathImpl = mock.Mock( |
1098 return_value='/test/device/external_dir') | 1104 return_value='/test/device/external_dir') |
1099 self.device._IsOnlineImpl = mock.Mock(return_value=True) | 1105 self.device.IsOnline = mock.Mock(return_value=True) |
1100 self.device._RunShellCommandImpl = mock.Mock() | 1106 self.device._RunShellCommandImpl = mock.Mock() |
1101 mock_zip_temp = mock.mock_open() | 1107 mock_zip_temp = mock.mock_open() |
1102 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip' | 1108 mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip' |
1103 with mock.patch('multiprocessing.Process') as mock_zip_proc, ( | 1109 with mock.patch('multiprocessing.Process') as mock_zip_proc, ( |
1104 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)): | 1110 mock.patch('tempfile.NamedTemporaryFile', mock_zip_temp)): |
1105 self.device._PushChangedFilesZipped(test_files) | 1111 self.device._PushChangedFilesZipped(test_files) |
1106 | 1112 |
1107 mock_zip_proc.assert_called_once_with( | 1113 mock_zip_proc.assert_called_once_with( |
1108 target=device_utils.DeviceUtils._CreateDeviceZip, | 1114 target=device_utils.DeviceUtils._CreateDeviceZip, |
1109 args=('/test/temp/file/tmp.zip', test_files)) | 1115 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), | 1482 '%s\r\n' % constants.DEVICE_LOCAL_PROPERTIES_PATH), |
1477 ('adb -s 0123456789abcdef pull %s %s' % | 1483 ('adb -s 0123456789abcdef pull %s %s' % |
1478 (constants.DEVICE_LOCAL_PROPERTIES_PATH, mock_file.name), | 1484 (constants.DEVICE_LOCAL_PROPERTIES_PATH, mock_file.name), |
1479 '100 B/s (100 bytes in 1.000s)\r\n'), | 1485 '100 B/s (100 bytes in 1.000s)\r\n'), |
1480 ('adb -s 0123456789abcdef shell ' | 1486 ('adb -s 0123456789abcdef shell ' |
1481 'getprop dalvik.vm.enableassertions', | 1487 'getprop dalvik.vm.enableassertions', |
1482 'all\r\n')]): | 1488 'all\r\n')]): |
1483 self.assertFalse(self.device.SetJavaAsserts(True)) | 1489 self.assertFalse(self.device.SetJavaAsserts(True)) |
1484 | 1490 |
1485 | 1491 |
1486 class DeviceUtilsGetPropTest(DeviceUtilsOldImplTest): | 1492 class DeviceUtilsGetPropTest(DeviceUtilsNewImplTest): |
1487 | 1493 |
1488 def testGetProp_exists(self): | 1494 def testGetProp_exists(self): |
1489 with self.assertCalls( | 1495 with self.assertShellCall('getprop this.is.a.test.property', |
1490 'adb -s 0123456789abcdef shell getprop this.is.a.test.property', | 1496 'test_property_value\r\n'): |
1491 'test_property_value\r\n'): | |
1492 self.assertEqual('test_property_value', | 1497 self.assertEqual('test_property_value', |
1493 self.device.GetProp('this.is.a.test.property')) | 1498 self.device.GetProp('this.is.a.test.property')) |
1494 | 1499 |
1495 def testGetProp_doesNotExist(self): | 1500 def testGetProp_doesNotExist(self): |
1496 with self.assertCalls( | 1501 with self.assertShellCall('getprop this.property.does.not.exist', |
1497 'adb -s 0123456789abcdef shell ' | 1502 '\r\n'): |
1498 'getprop this.property.does.not.exist', ''): | |
1499 self.assertEqual('', self.device.GetProp('this.property.does.not.exist')) | 1503 self.assertEqual('', self.device.GetProp('this.property.does.not.exist')) |
1500 | 1504 |
1501 def testGetProp_cachedRoProp(self): | 1505 def testGetProp_cachedRoProp(self): |
1502 with self.assertCalls( | 1506 with self.assertShellCall('getprop ro.build.type', |
1503 'adb -s 0123456789abcdef shell ' | 1507 'userdebug\r\n'): |
1504 'getprop ro.build.type', 'userdebug'): | 1508 self.assertEqual('userdebug', self.device.GetProp('ro.build.type', |
1505 self.assertEqual('userdebug', self.device.GetProp('ro.build.type')) | 1509 cache=True)) |
1506 self.assertEqual('userdebug', self.device.GetProp('ro.build.type')) | 1510 self.assertEqual('userdebug', self.device.GetProp('ro.build.type', |
| 1511 cache=True)) |
1507 | 1512 |
1508 | 1513 |
1509 class DeviceUtilsSetPropTest(DeviceUtilsOldImplTest): | 1514 class DeviceUtilsSetPropTest(DeviceUtilsNewImplTest): |
1510 | 1515 |
1511 def testSetProp(self): | 1516 def testSetProp(self): |
1512 with self.assertCalls( | 1517 with self.assertShellCall( |
1513 'adb -s 0123456789abcdef shell ' | 1518 "setprop this.is.a.test.property 'test property value'"): |
1514 'setprop this.is.a.test.property "test_property_value"', | 1519 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 | 1520 |
1518 | 1521 |
1519 class DeviceUtilsGetPidsTest(DeviceUtilsNewImplTest): | 1522 class DeviceUtilsGetPidsTest(DeviceUtilsNewImplTest): |
1520 | 1523 |
1521 def testGetPids_noMatches(self): | 1524 def testGetPids_noMatches(self): |
1522 with self.assertShellCall( | 1525 with self.assertShellCall( |
1523 'ps', | 1526 'ps', |
1524 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' | 1527 'USER PID PPID VSIZE RSS WCHAN PC NAME\r\n' |
1525 'user 1000 100 1024 1024 ffffffff 00000000 no.match\r\n'): | 1528 'user 1000 100 1024 1024 ffffffff 00000000 no.match\r\n'): |
1526 self.assertEqual({}, self.device.GetPids('does.not.match')) | 1529 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) | 1649 self.device = device_utils.DeviceUtils(None) |
1647 with self.assertCalls('adb get-serialno', 'unknown'), ( | 1650 with self.assertCalls('adb get-serialno', 'unknown'), ( |
1648 self.assertRaises(device_errors.NoDevicesError)): | 1651 self.assertRaises(device_errors.NoDevicesError)): |
1649 str(self.device) | 1652 str(self.device) |
1650 | 1653 |
1651 | 1654 |
1652 if __name__ == '__main__': | 1655 if __name__ == '__main__': |
1653 logging.getLogger().setLevel(logging.DEBUG) | 1656 logging.getLogger().setLevel(logging.DEBUG) |
1654 unittest.main(verbosity=2) | 1657 unittest.main(verbosity=2) |
1655 | 1658 |
OLD | NEW |