Skip to content

Commit

Permalink
Make device serial required in AdbWrapper/DeviceUtils
Browse files Browse the repository at this point in the history
As a side effect, we also:
- help migrate DeviceUtils.__str__ to AdbWrapper
- fix bug with the presubmit pylib.instrumentation.tests_runner_test
  when no devices are attached.

BUG=267773,430731

Review URL: https://codereview.chromium.org/762883002

Cr-Commit-Position: refs/heads/master@{#306585}
  • Loading branch information
perezju authored and Commit bot committed Dec 3, 2014
1 parent 8950090 commit d10fd29
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 37 deletions.
2 changes: 2 additions & 0 deletions build/android/pylib/device/adb_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ def __init__(self, device_serial):
Args:
device_serial: The device serial number as a string.
"""
if not device_serial:
raise ValueError('A device serial must be specified')
self._device_serial = str(device_serial)

# pylint: disable=unused-argument
Expand Down
16 changes: 4 additions & 12 deletions build/android/pylib/device/device_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ def __init__(self, device, default_timeout=_DEFAULT_TIMEOUT,
"""DeviceUtils constructor.
Args:
device: Either a device serial, an existing AdbWrapper instance, an
an existing AndroidCommands instance, or nothing.
device: Either a device serial, an existing AdbWrapper instance, or an
an existing AndroidCommands instance.
default_timeout: An integer containing the default number of seconds to
wait for an operation to complete if no explicit value
is provided.
Expand All @@ -85,11 +85,8 @@ def __init__(self, device, default_timeout=_DEFAULT_TIMEOUT,
elif isinstance(device, pylib.android_commands.AndroidCommands):
self.adb = adb_wrapper.AdbWrapper(device.GetDevice())
self.old_interface = device
elif not device:
self.adb = adb_wrapper.AdbWrapper('')
self.old_interface = pylib.android_commands.AndroidCommands()
else:
raise ValueError('Unsupported type passed for argument "device"')
raise ValueError('Unsupported device value: %r' % device)
self._commands_installed = None
self._default_timeout = default_timeout
self._default_retries = default_retries
Expand Down Expand Up @@ -1145,12 +1142,7 @@ def GetMemoryUsageForPid(self, pid, timeout=None, retries=None):

def __str__(self):
"""Returns the device serial."""
s = self.old_interface.GetDevice()
if not s:
s = self.old_interface.Adb().GetSerialNumber()
if s == 'unknown':
raise device_errors.NoDevicesError()
return s
return self.adb.GetDeviceSerial()

@staticmethod
def parallel(devices=None, async=False):
Expand Down
36 changes: 14 additions & 22 deletions build/android/pylib/device/device_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,30 @@ class DeviceUtilsTest(unittest.TestCase):
def testInitWithStr(self):
serial_as_str = str('0123456789abcdef')
d = device_utils.DeviceUtils('0123456789abcdef')
self.assertEqual(serial_as_str, d.old_interface.GetDevice())
self.assertEqual(serial_as_str, d.adb.GetDeviceSerial())

def testInitWithUnicode(self):
serial_as_unicode = unicode('fedcba9876543210')
d = device_utils.DeviceUtils(serial_as_unicode)
self.assertEqual(serial_as_unicode, d.old_interface.GetDevice())
self.assertEqual(serial_as_unicode, d.adb.GetDeviceSerial())

def testInitWithAdbWrapper(self):
serial = '123456789abcdef0'
a = adb_wrapper.AdbWrapper(serial)
d = device_utils.DeviceUtils(a)
self.assertEqual(serial, d.old_interface.GetDevice())
self.assertEqual(serial, d.adb.GetDeviceSerial())

def testInitWithAndroidCommands(self):
serial = '0fedcba987654321'
a = android_commands.AndroidCommands(device=serial)
d = device_utils.DeviceUtils(a)
self.assertEqual(serial, d.old_interface.GetDevice())
self.assertEqual(serial, d.adb.GetDeviceSerial())

def testInitWithNone(self):
d = device_utils.DeviceUtils(None)
self.assertIsNone(d.old_interface.GetDevice())
def testInitWithMissing_fails(self):
with self.assertRaises(ValueError):
device_utils.DeviceUtils(None)
with self.assertRaises(ValueError):
device_utils.DeviceUtils('')


class MockTempFile(object):
Expand Down Expand Up @@ -246,7 +248,7 @@ def setUp(self):
self.adb.GetDeviceSerial.return_value = test_serial
self.device = device_utils.DeviceUtils(
self.adb, default_timeout=10, default_retries=0)
self.watchMethodCalls(self.call.adb)
self.watchMethodCalls(self.call.adb, ignore=['GetDeviceSerial'])

def ShellError(self, output=None, exit_code=1):
def action(cmd, *args, **kwargs):
Expand Down Expand Up @@ -1524,23 +1526,13 @@ def testGetMemoryUsageForPid_invalidPid(self):
self.assertEqual({}, self.device.GetMemoryUsageForPid(4321))


class DeviceUtilsStrTest(DeviceUtilsOldImplTest):
class DeviceUtilsStrTest(DeviceUtilsNewImplTest):

def testStr_noAdbCalls(self):
with self.assertNoAdbCalls():
self.assertEqual('0123456789abcdef', str(self.device))

def testStr_noSerial(self):
self.device = device_utils.DeviceUtils(None)
with self.assertCalls('adb get-serialno', '0123456789abcdef'):
def testStr_returnsSerial(self):
with self.assertCalls(
(self.call.adb.GetDeviceSerial(), '0123456789abcdef')):
self.assertEqual('0123456789abcdef', str(self.device))

def testStr_noSerial_noDevices(self):
self.device = device_utils.DeviceUtils(None)
with self.assertCalls('adb get-serialno', 'unknown'), (
self.assertRaises(device_errors.NoDevicesError)):
str(self.device)


if __name__ == '__main__':
logging.getLogger().setLevel(logging.DEBUG)
Expand Down
5 changes: 4 additions & 1 deletion build/android/pylib/instrumentation/test_runner_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def setUp(self):
options = mock.Mock()
options.tool = ''
package = mock.Mock()
self.instance = test_runner.TestRunner(options, None, 0, package)
self.instance = test_runner.TestRunner(
options, '123456789abcdef0', 0, package)

def testParseAmInstrumentRawOutput_nothing(self):
code, result, statuses = (
Expand Down Expand Up @@ -226,6 +227,8 @@ def testGenerateTestResult_testFailed(self):
'test': ['testMethod'],
}),
]
self.instance.device.old_interface.DismissCrashDialogIfNeeded = mock.Mock(
return_value=None)
result = self.instance._GenerateTestResult(
'test.package.TestClass#testMethod', statuses, 0, 1000)
self.assertEqual(base_test_result.ResultType.FAIL, result.GetType())
Expand Down
7 changes: 5 additions & 2 deletions build/android/pylib/utils/mock_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,19 @@ def watchCalls(self, calls):
"""
self._watched.update((call.name, call) for call in calls)

def watchMethodCalls(self, call):
def watchMethodCalls(self, call, ignore=None):
"""Watch all public methods of the target identified by a self.call.
Args:
call: a self.call instance indetifying an object
ignore: a list of public methods to ignore when watching for calls
"""
target = self.call_target(call)
if ignore is None:
ignore = []
self.watchCalls(getattr(call, method)
for method in dir(target.__class__)
if not method.startswith('_'))
if not method.startswith('_') and not method in ignore)

def clearWatched(self):
"""Clear the set of watched calls."""
Expand Down

0 comments on commit d10fd29

Please sign in to comment.