Commit f4d73eee authored by perezju's avatar perezju Committed by Commit bot

Migrate DeviceUtils.SetJavaAsserts to adb_wrapper

The implementation mirrors that of android_commands, but is hopefully
clearer and more efficient. ReadFile/WriteFile are used to read
persistent properties first, and then GetProp/SetProp are used for
the current runtime properties.

BUG=267773

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

Cr-Commit-Position: refs/heads/master@{#314791}
parent fdbfd4e1
...@@ -104,6 +104,9 @@ class DeviceUtils(object): ...@@ -104,6 +104,9 @@ class DeviceUtils(object):
_MAX_ADB_COMMAND_LENGTH = 512 _MAX_ADB_COMMAND_LENGTH = 512
_VALID_SHELL_VARIABLE = re.compile('^[a-zA-Z_][a-zA-Z0-9_]*$') _VALID_SHELL_VARIABLE = re.compile('^[a-zA-Z_][a-zA-Z0-9_]*$')
# Property in /data/local.prop that controls Java assertions.
JAVA_ASSERT_PROPERTY = 'dalvik.vm.enableassertions'
def __init__(self, device, default_timeout=_DEFAULT_TIMEOUT, def __init__(self, device, default_timeout=_DEFAULT_TIMEOUT,
default_retries=_DEFAULT_RETRIES): default_retries=_DEFAULT_RETRIES):
"""DeviceUtils constructor. """DeviceUtils constructor.
...@@ -1041,7 +1044,43 @@ class DeviceUtils(object): ...@@ -1041,7 +1044,43 @@ class DeviceUtils(object):
Raises: Raises:
CommandTimeoutError on timeout. CommandTimeoutError on timeout.
""" """
return self.old_interface.SetJavaAssertsEnabled(enabled) def find_property(lines, property_name):
for index, line in enumerate(lines):
key, value = (s.strip() for s in line.split('=', 1))
if key == property_name:
return index, value
return None, ''
new_value = 'all' if enabled else ''
# First ensure the desired property is persisted.
try:
properties = self.ReadFile(
constants.DEVICE_LOCAL_PROPERTIES_PATH).splitlines()
except device_errors.CommandFailedError:
properties = []
index, value = find_property(properties, self.JAVA_ASSERT_PROPERTY)
if new_value != value:
if new_value:
new_line = '%s=%s' % (self.JAVA_ASSERT_PROPERTY, new_value)
if index is None:
properties.append(new_line)
else:
properties[index] = new_line
else:
assert index is not None # since new_value == '' and new_value != value
properties.pop(index)
self.WriteFile(constants.DEVICE_LOCAL_PROPERTIES_PATH,
_JoinLines(properties))
# Next, check the current runtime value is what we need, and
# if not, set it and report that a reboot is required.
value = self.GetProp(self.JAVA_ASSERT_PROPERTY)
if new_value != value:
self.SetProp(self.JAVA_ASSERT_PROPERTY, new_value)
return True
else:
return False
@property @property
......
...@@ -1238,81 +1238,44 @@ class DeviceUtilsStatTest(DeviceUtilsNewImplTest): ...@@ -1238,81 +1238,44 @@ class DeviceUtilsStatTest(DeviceUtilsNewImplTest):
self.device.Stat('/data/local/tmp/does.not.exist.txt') self.device.Stat('/data/local/tmp/does.not.exist.txt')
class DeviceUtilsSetJavaAssertsTest(DeviceUtilsOldImplTest): class DeviceUtilsSetJavaAssertsTest(DeviceUtilsNewImplTest):
@staticmethod
def mockNamedTemporary(name='/tmp/file/property.file',
read_contents=''):
mock_file = mock.MagicMock(spec=file)
mock_file.name = name
mock_file.__enter__.return_value = mock_file
mock_file.read.return_value = read_contents
return mock_file
def testSetJavaAsserts_enable(self): def testSetJavaAsserts_enable(self):
mock_file = self.mockNamedTemporary() with self.assertCalls(
with mock.patch('tempfile.NamedTemporaryFile', (self.call.device.ReadFile(constants.DEVICE_LOCAL_PROPERTIES_PATH),
return_value=mock_file), ( 'some.example.prop=with an example value\n'
mock.patch('__builtin__.open', return_value=mock_file)): 'some.other.prop=value_ok\n'),
with self.assertCallsSequence( self.call.device.WriteFile(
[('adb -s 0123456789abcdef shell ls %s' % constants.DEVICE_LOCAL_PROPERTIES_PATH,
constants.DEVICE_LOCAL_PROPERTIES_PATH, 'some.example.prop=with an example value\n'
'%s\r\n' % constants.DEVICE_LOCAL_PROPERTIES_PATH), 'some.other.prop=value_ok\n'
('adb -s 0123456789abcdef pull %s %s' % 'dalvik.vm.enableassertions=all\n'),
(constants.DEVICE_LOCAL_PROPERTIES_PATH, mock_file.name), (self.call.device.GetProp('dalvik.vm.enableassertions'), ''),
'100 B/s (100 bytes in 1.000s)\r\n'), self.call.device.SetProp('dalvik.vm.enableassertions', 'all')):
('adb -s 0123456789abcdef push %s %s' % self.assertTrue(self.device.SetJavaAsserts(True))
(mock_file.name, constants.DEVICE_LOCAL_PROPERTIES_PATH),
'100 B/s (100 bytes in 1.000s)\r\n'),
('adb -s 0123456789abcdef shell '
'getprop dalvik.vm.enableassertions',
'\r\n'),
('adb -s 0123456789abcdef shell '
'setprop dalvik.vm.enableassertions "all"',
'')]):
self.assertTrue(self.device.SetJavaAsserts(True))
def testSetJavaAsserts_disable(self): def testSetJavaAsserts_disable(self):
mock_file = self.mockNamedTemporary( with self.assertCalls(
read_contents='dalvik.vm.enableassertions=all\n') (self.call.device.ReadFile(constants.DEVICE_LOCAL_PROPERTIES_PATH),
with mock.patch('tempfile.NamedTemporaryFile', 'some.example.prop=with an example value\n'
return_value=mock_file), ( 'dalvik.vm.enableassertions=all\n'
mock.patch('__builtin__.open', return_value=mock_file)): 'some.other.prop=value_ok\n'),
with self.assertCallsSequence( self.call.device.WriteFile(
[('adb -s 0123456789abcdef shell ls %s' % constants.DEVICE_LOCAL_PROPERTIES_PATH,
constants.DEVICE_LOCAL_PROPERTIES_PATH, 'some.example.prop=with an example value\n'
'%s\r\n' % constants.DEVICE_LOCAL_PROPERTIES_PATH), 'some.other.prop=value_ok\n'),
('adb -s 0123456789abcdef pull %s %s' % (self.call.device.GetProp('dalvik.vm.enableassertions'), 'all'),
(constants.DEVICE_LOCAL_PROPERTIES_PATH, mock_file.name), self.call.device.SetProp('dalvik.vm.enableassertions', '')):
'100 B/s (100 bytes in 1.000s)\r\n'), self.assertTrue(self.device.SetJavaAsserts(False))
('adb -s 0123456789abcdef push %s %s' %
(mock_file.name, constants.DEVICE_LOCAL_PROPERTIES_PATH),
'100 B/s (100 bytes in 1.000s)\r\n'),
('adb -s 0123456789abcdef shell '
'getprop dalvik.vm.enableassertions',
'all\r\n'),
('adb -s 0123456789abcdef shell '
'setprop dalvik.vm.enableassertions ""',
'')]):
self.assertTrue(self.device.SetJavaAsserts(False))
def testSetJavaAsserts_alreadyEnabled(self): def testSetJavaAsserts_alreadyEnabled(self):
mock_file = self.mockNamedTemporary( with self.assertCalls(
read_contents='dalvik.vm.enableassertions=all\n') (self.call.device.ReadFile(constants.DEVICE_LOCAL_PROPERTIES_PATH),
with mock.patch('tempfile.NamedTemporaryFile', 'some.example.prop=with an example value\n'
return_value=mock_file), ( 'dalvik.vm.enableassertions=all\n'
mock.patch('__builtin__.open', return_value=mock_file)): 'some.other.prop=value_ok\n'),
with self.assertCallsSequence( (self.call.device.GetProp('dalvik.vm.enableassertions'), 'all')):
[('adb -s 0123456789abcdef shell ls %s' % self.assertFalse(self.device.SetJavaAsserts(True))
constants.DEVICE_LOCAL_PROPERTIES_PATH,
'%s\r\n' % constants.DEVICE_LOCAL_PROPERTIES_PATH),
('adb -s 0123456789abcdef pull %s %s' %
(constants.DEVICE_LOCAL_PROPERTIES_PATH, mock_file.name),
'100 B/s (100 bytes in 1.000s)\r\n'),
('adb -s 0123456789abcdef shell '
'getprop dalvik.vm.enableassertions',
'all\r\n')]):
self.assertFalse(self.device.SetJavaAsserts(True))
class DeviceUtilsGetPropTest(DeviceUtilsNewImplTest): class DeviceUtilsGetPropTest(DeviceUtilsNewImplTest):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment