Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1464)

Unified Diff: build/android/pylib/device/device_utils_test.py

Issue 636273004: Make TimeoutRetryThread's stoppable (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: some fixes, tests ready Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: build/android/pylib/device/device_utils_test.py
diff --git a/build/android/pylib/device/device_utils_test.py b/build/android/pylib/device/device_utils_test.py
index 6638fad24e63d7d3013b25b9905d133af5d08e65..a45aa4e3b2971a69abbfda83d629fbda475bec98 100755
--- a/build/android/pylib/device/device_utils_test.py
+++ b/build/android/pylib/device/device_utils_test.py
@@ -285,6 +285,14 @@ class _ShellError:
self.return_code = return_code
+class _CmdTimeout:
+ def __init__(self, msg=None):
+ if msg is None:
+ self.msg = 'Operation timed out'
+ else:
+ self.msg = msg
+
+
class DeviceUtilsNewImplTest(unittest.TestCase):
def setUp(self):
@@ -309,6 +317,9 @@ class DeviceUtilsNewImplTest(unittest.TestCase):
if isinstance(return_value, _ShellError):
return_value = device_errors.AdbShellCommandFailedError(cmd,
return_value.return_code, return_value.output, str(self.device))
+ elif isinstance(return_value, _CmdTimeout):
+ return_value = device_errors.CommandTimeoutError(return_value.msg,
+ str(self.device))
return (expected_args, return_value)
expected_calls = (mk_expected_call(a, r) for a, r in calls)
@@ -325,16 +336,17 @@ class DeviceUtilsHybridImplTest(DeviceUtilsOldImplTest):
self.device.adb = self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper)
-class DeviceUtilsIsOnlineTest(DeviceUtilsOldImplTest):
+class DeviceUtilsIsOnlineTest(DeviceUtilsNewImplTest):
def testIsOnline_true(self):
- with self.assertCalls('adb -s 0123456789abcdef devices',
- '00123456789abcdef device\r\n'):
- self.assertTrue(self.device.IsOnline())
+ self.adb.GetState = mock.Mock(return_value='device')
+ self.assertTrue(self.device.IsOnline())
+ self.adb.GetState.assert_called_once_with()
def testIsOnline_false(self):
- with self.assertCalls('adb -s 0123456789abcdef devices', '\r\n'):
- self.assertFalse(self.device.IsOnline())
+ self.adb.GetState = mock.Mock(return_value='offline')
+ self.assertFalse(self.device.IsOnline())
+ self.adb.GetState.assert_called_once_with()
class DeviceUtilsHasRootTest(DeviceUtilsNewImplTest):
@@ -375,18 +387,14 @@ class DeviceUtilsEnableRootTest(DeviceUtilsOldImplTest):
self.device.EnableRoot()
-class DeviceUtilsIsUserBuildTest(DeviceUtilsOldImplTest):
+class DeviceUtilsIsUserBuildTest(DeviceUtilsNewImplTest):
def testIsUserBuild_yes(self):
- with self.assertCalls(
- 'adb -s 0123456789abcdef shell getprop ro.build.type',
- 'user\r\n'):
+ with self.assertShellCall('getprop ro.build.type', 'user\r\n'):
self.assertTrue(self.device.IsUserBuild())
def testIsUserBuild_no(self):
- with self.assertCalls(
- 'adb -s 0123456789abcdef shell getprop ro.build.type',
- 'userdebug\r\n'):
+ with self.assertShellCall('getprop ro.build.type', 'userdebug\r\n'):
self.assertFalse(self.device.IsUserBuild())
@@ -405,138 +413,136 @@ class DeviceUtilsGetExternalStoragePathTest(DeviceUtilsNewImplTest):
self.device.GetExternalStoragePath()
-class DeviceUtilsWaitUntilFullyBootedTest(DeviceUtilsOldImplTest):
+class DeviceUtilsWaitUntilFullyBootedTest(DeviceUtilsNewImplTest):
def testWaitUntilFullyBooted_succeedsNoWifi(self):
- with self.assertCallsSequence([
- # AndroidCommands.WaitForSystemBootCompleted
- ('adb -s 0123456789abcdef wait-for-device', ''),
- ('adb -s 0123456789abcdef shell getprop sys.boot_completed', '1\r\n'),
- # AndroidCommands.WaitForDevicePm
- ('adb -s 0123456789abcdef wait-for-device', ''),
- ('adb -s 0123456789abcdef shell pm path android',
- 'package:this.is.a.test.package'),
- # AndroidCommands.WaitForSdCardReady
- ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'",
- '/fake/storage/path'),
- ("adb -s 0123456789abcdef shell 'ls /fake/storage/path'",
- 'nothing\r\n')
- ]):
+ with self.assertShellCallSequence([
+ # sc_card_ready
+ ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'),
+ ('ls /fake/storage/path', '/fake/storage/path\r\n'),
+ # pm_ready
+ ('pm path android', 'package:this.is.a.test.package\r\n'),
+ # boot_completed
+ ('getprop sys.boot_completed', '1\r\n')]):
self.device.WaitUntilFullyBooted(wifi=False)
+ self.adb.WaitForDevice.assert_called_once_with()
def testWaitUntilFullyBooted_succeedsWithWifi(self):
- with self.assertCallsSequence([
- # AndroidCommands.WaitForSystemBootCompleted
- ('adb -s 0123456789abcdef wait-for-device', ''),
- ('adb -s 0123456789abcdef shell getprop sys.boot_completed', '1\r\n'),
- # AndroidCommands.WaitForDevicePm
- ('adb -s 0123456789abcdef wait-for-device', ''),
- ('adb -s 0123456789abcdef shell pm path android',
- 'package:this.is.a.test.package'),
- # AndroidCommands.WaitForSdCardReady
- ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'",
- '/fake/storage/path'),
- ("adb -s 0123456789abcdef shell 'ls /fake/storage/path'",
- 'nothing\r\n'),
- # wait for wifi
- ("adb -s 0123456789abcdef shell 'dumpsys wifi'", 'Wi-Fi is enabled')]):
+ with self.assertShellCallSequence([
+ # sc_card_ready
+ ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'),
+ ('ls /fake/storage/path', '/fake/storage/path\r\n'),
+ # pm_ready
+ ('pm path android', 'package:this.is.a.test.package\r\n'),
+ # boot_completed
+ ('getprop sys.boot_completed', '1\r\n'),
+ # wifi_enabled
+ ('dumpsys wifi', 'stuff\r\nWi-Fi is enabled\r\nmore stuff\r\n')]):
self.device.WaitUntilFullyBooted(wifi=True)
+ self.adb.WaitForDevice.assert_called_once_with()
- def testWaitUntilFullyBooted_bootFails(self):
+ def testWaitUntilFullyBooted_sdCardReadyFails_noPath(self):
with mock.patch('time.sleep'):
- with self.assertCallsSequence([
- # AndroidCommands.WaitForSystemBootCompleted
- ('adb -s 0123456789abcdef wait-for-device', ''),
- ('adb -s 0123456789abcdef shell getprop sys.boot_completed',
- '0\r\n')]):
+ with self.assertShellCallSequence([
+ # sc_card_ready
+ ('echo $EXTERNAL_STORAGE', '\r\n'),
+ # sc_card_ready
+ ('echo $EXTERNAL_STORAGE', '\r\n'),
+ # sc_card_ready
+ ('echo $EXTERNAL_STORAGE', _CmdTimeout())]):
with self.assertRaises(device_errors.CommandTimeoutError):
self.device.WaitUntilFullyBooted(wifi=False)
- def testWaitUntilFullyBooted_devicePmFails(self):
+ def testWaitUntilFullyBooted_sdCardReadyFails_emptyPath(self):
with mock.patch('time.sleep'):
- with self.assertCallsSequence([
- # AndroidCommands.WaitForSystemBootCompleted
- ('adb -s 0123456789abcdef wait-for-device', ''),
- ('adb -s 0123456789abcdef shell getprop sys.boot_completed',
- '1\r\n')]
- # AndroidCommands.WaitForDevicePm
- + 3 * ([('adb -s 0123456789abcdef wait-for-device', '')]
- + 24 * [('adb -s 0123456789abcdef shell pm path android', '\r\n')]
- + [("adb -s 0123456789abcdef shell 'stop'", '\r\n'),
- ("adb -s 0123456789abcdef shell 'start'", '\r\n')])):
+ with self.assertShellCallSequence([
+ # sc_card_ready
+ ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'),
+ ('ls /fake/storage/path', '\r\n'),
+ # sc_card_ready
+ ('ls /fake/storage/path', '\r\n'),
+ # sc_card_ready
+ ('ls /fake/storage/path', _CmdTimeout())]):
with self.assertRaises(device_errors.CommandTimeoutError):
self.device.WaitUntilFullyBooted(wifi=False)
- def testWaitUntilFullyBooted_sdCardReadyFails_noPath(self):
+ def testWaitUntilFullyBooted_devicePmFails(self):
with mock.patch('time.sleep'):
- with self.assertCallsSequence([
- # AndroidCommands.WaitForSystemBootCompleted
- ('adb -s 0123456789abcdef wait-for-device', ''),
- ('adb -s 0123456789abcdef shell getprop sys.boot_completed',
- '1\r\n'),
- # AndroidCommands.WaitForDevicePm
- ('adb -s 0123456789abcdef wait-for-device', ''),
- ('adb -s 0123456789abcdef shell pm path android',
- 'package:this.is.a.test.package'),
- ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'", '\r\n')]):
- with self.assertRaises(device_errors.CommandFailedError):
+ with self.assertShellCallSequence([
+ # sc_card_ready
+ ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'),
+ ('ls /fake/storage/path', '/fake/storage/path\r\n'),
+ # pm_ready
+ ('pm path android', 'Error. Is package manager running?\r\n'),
+ # pm_ready
+ ('pm path android', 'Error. Is package manager running?\r\n'),
+ # pm_ready
+ ('pm path android', _CmdTimeout())]):
+ with self.assertRaises(device_errors.CommandTimeoutError):
self.device.WaitUntilFullyBooted(wifi=False)
- def testWaitUntilFullyBooted_sdCardReadyFails_emptyPath(self):
+ def testWaitUntilFullyBooted_bootFails(self):
with mock.patch('time.sleep'):
- with self.assertCallsSequence([
- # AndroidCommands.WaitForSystemBootCompleted
- ('adb -s 0123456789abcdef wait-for-device', ''),
- ('adb -s 0123456789abcdef shell getprop sys.boot_completed',
- '1\r\n'),
- # AndroidCommands.WaitForDevicePm
- ('adb -s 0123456789abcdef wait-for-device', ''),
- ('adb -s 0123456789abcdef shell pm path android',
- 'package:this.is.a.test.package'),
- ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'",
- '/fake/storage/path\r\n'),
- ("adb -s 0123456789abcdef shell 'ls /fake/storage/path'", '')]):
+ with self.assertShellCallSequence([
+ # sc_card_ready
+ ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'),
+ ('ls /fake/storage/path', '/fake/storage/path\r\n'),
+ # pm_ready
+ ('pm path android', 'package:this.is.a.test.package\r\n'),
+ # boot_completed
+ ('getprop sys.boot_completed', '0\r\n'),
+ # boot_completed
+ ('getprop sys.boot_completed', '0\r\n'),
+ # boot_completed
+ ('getprop sys.boot_completed', _CmdTimeout())]):
with self.assertRaises(device_errors.CommandTimeoutError):
self.device.WaitUntilFullyBooted(wifi=False)
+ def testWaitUntilFullyBooted_wifiFails(self):
+ with mock.patch('time.sleep'):
+ with self.assertShellCallSequence([
+ # sc_card_ready
+ ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'),
+ ('ls /fake/storage/path', '/fake/storage/path\r\n'),
+ # pm_ready
+ ('pm path android', 'package:this.is.a.test.package\r\n'),
+ # boot_completed
+ ('getprop sys.boot_completed', '1\r\n'),
+ # wifi_enabled
+ ('dumpsys wifi', 'stuff\r\nmore stuff\r\n'),
+ # wifi_enabled
+ ('dumpsys wifi', 'stuff\r\nmore stuff\r\n'),
+ # wifi_enabled
+ ('dumpsys wifi', _CmdTimeout())]):
+ with self.assertRaises(device_errors.CommandTimeoutError):
+ self.device.WaitUntilFullyBooted(wifi=True)
-class DeviceUtilsRebootTest(DeviceUtilsOldImplTest):
+class DeviceUtilsRebootTest(DeviceUtilsNewImplTest):
def testReboot_nonBlocking(self):
+ self.adb.Reboot = mock.Mock()
+ self.device.IsOnline = mock.Mock(return_value=False)
with mock.patch('time.sleep'):
- with self.assertCallsSequence([
- ('adb -s 0123456789abcdef reboot', ''),
- ('adb -s 0123456789abcdef devices', 'unknown\r\n'),
- ('adb -s 0123456789abcdef wait-for-device', ''),
- ('adb -s 0123456789abcdef shell pm path android',
- 'package:this.is.a.test.package'),
- ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'",
- '/fake/storage/path\r\n'),
- ("adb -s 0123456789abcdef shell 'ls /fake/storage/path'",
- 'nothing\r\n')]):
- self.device.Reboot(block=False)
+ self.device.Reboot(block=False)
+ self.adb.Reboot.assert_called_once_with()
+ self.device.IsOnline.assert_called_once_with()
def testReboot_blocking(self):
+ self.adb.Reboot = mock.Mock()
+ self.device.IsOnline = mock.Mock(return_value=False)
with mock.patch('time.sleep'):
- with self.assertCallsSequence([
- ('adb -s 0123456789abcdef reboot', ''),
- ('adb -s 0123456789abcdef devices', 'unknown\r\n'),
- ('adb -s 0123456789abcdef wait-for-device', ''),
- ('adb -s 0123456789abcdef shell pm path android',
- 'package:this.is.a.test.package'),
- ("adb -s 0123456789abcdef shell 'echo $EXTERNAL_STORAGE'",
- '/fake/storage/path\r\n'),
- ("adb -s 0123456789abcdef shell 'ls /fake/storage/path'",
- 'nothing\r\n'),
- ('adb -s 0123456789abcdef wait-for-device', ''),
- ('adb -s 0123456789abcdef shell getprop sys.boot_completed',
- '1\r\n'),
- ('adb -s 0123456789abcdef wait-for-device', ''),
- ('adb -s 0123456789abcdef shell pm path android',
- 'package:this.is.a.test.package'),
- ("adb -s 0123456789abcdef shell 'ls /fake/storage/path'",
- 'nothing\r\n')]):
+ with self.assertShellCallSequence([
+ # sc_card_ready
+ ('echo $EXTERNAL_STORAGE', '/fake/storage/path\r\n'),
+ ('ls /fake/storage/path', '/fake/storage/path\r\n'),
+ # pm_ready
+ ('pm path android', 'package:this.is.a.test.package\r\n'),
+ # boot_completed
+ ('getprop sys.boot_completed', '1\r\n')]):
self.device.Reboot(block=True)
+ self.adb.Reboot.assert_called_once_with()
+ self.device.IsOnline.assert_called_once_with()
+ self.adb.WaitForDevice.assert_called_once_with()
class DeviceUtilsInstallTest(DeviceUtilsOldImplTest):
@@ -1068,7 +1074,7 @@ class DeviceUtilsPushChangedFilesZippedTest(DeviceUtilsHybridImplTest):
self.device._GetExternalStoragePathImpl = mock.Mock(
return_value='/test/device/external_dir')
- self.device._IsOnlineImpl = mock.Mock(return_value=True)
+ self.device.IsOnline = mock.Mock(return_value=True)
self.device._RunShellCommandImpl = mock.Mock()
mock_zip_temp = mock.mock_open()
mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip'
@@ -1096,7 +1102,7 @@ class DeviceUtilsPushChangedFilesZippedTest(DeviceUtilsHybridImplTest):
self.device._GetExternalStoragePathImpl = mock.Mock(
return_value='/test/device/external_dir')
- self.device._IsOnlineImpl = mock.Mock(return_value=True)
+ self.device.IsOnline = mock.Mock(return_value=True)
self.device._RunShellCommandImpl = mock.Mock()
mock_zip_temp = mock.mock_open()
mock_zip_temp.return_value.name = '/test/temp/file/tmp.zip'
@@ -1483,37 +1489,34 @@ class DeviceUtilsSetJavaAssertsTest(DeviceUtilsOldImplTest):
self.assertFalse(self.device.SetJavaAsserts(True))
-class DeviceUtilsGetPropTest(DeviceUtilsOldImplTest):
+class DeviceUtilsGetPropTest(DeviceUtilsNewImplTest):
def testGetProp_exists(self):
- with self.assertCalls(
- 'adb -s 0123456789abcdef shell getprop this.is.a.test.property',
- 'test_property_value\r\n'):
+ with self.assertShellCall('getprop this.is.a.test.property',
+ 'test_property_value\r\n'):
self.assertEqual('test_property_value',
self.device.GetProp('this.is.a.test.property'))
def testGetProp_doesNotExist(self):
- with self.assertCalls(
- 'adb -s 0123456789abcdef shell '
- 'getprop this.property.does.not.exist', ''):
+ with self.assertShellCall('getprop this.property.does.not.exist',
+ '\r\n'):
self.assertEqual('', self.device.GetProp('this.property.does.not.exist'))
def testGetProp_cachedRoProp(self):
- with self.assertCalls(
- 'adb -s 0123456789abcdef shell '
- 'getprop ro.build.type', 'userdebug'):
- self.assertEqual('userdebug', self.device.GetProp('ro.build.type'))
- self.assertEqual('userdebug', self.device.GetProp('ro.build.type'))
+ with self.assertShellCall('getprop ro.build.type',
+ 'userdebug\r\n'):
+ self.assertEqual('userdebug', self.device.GetProp('ro.build.type',
+ cache=True))
+ self.assertEqual('userdebug', self.device.GetProp('ro.build.type',
+ cache=True))
-class DeviceUtilsSetPropTest(DeviceUtilsOldImplTest):
+class DeviceUtilsSetPropTest(DeviceUtilsNewImplTest):
def testSetProp(self):
- with self.assertCalls(
- 'adb -s 0123456789abcdef shell '
- 'setprop this.is.a.test.property "test_property_value"',
- ''):
- self.device.SetProp('this.is.a.test.property', 'test_property_value')
+ with self.assertShellCall(
+ "setprop this.is.a.test.property 'test property value'"):
+ self.device.SetProp('this.is.a.test.property', 'test property value')
class DeviceUtilsGetPidsTest(DeviceUtilsNewImplTest):

Powered by Google App Engine
This is Rietveld 408576698