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):
_MAX_ADB_COMMAND_LENGTH = 512
_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,
default_retries=_DEFAULT_RETRIES):
"""DeviceUtils constructor.
......@@ -1041,7 +1044,43 @@ class DeviceUtils(object):
Raises:
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
......
......@@ -1238,81 +1238,44 @@ class DeviceUtilsStatTest(DeviceUtilsNewImplTest):
self.device.Stat('/data/local/tmp/does.not.exist.txt')
class DeviceUtilsSetJavaAssertsTest(DeviceUtilsOldImplTest):
@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
class DeviceUtilsSetJavaAssertsTest(DeviceUtilsNewImplTest):
def testSetJavaAsserts_enable(self):
mock_file = self.mockNamedTemporary()
with mock.patch('tempfile.NamedTemporaryFile',
return_value=mock_file), (
mock.patch('__builtin__.open', return_value=mock_file)):
with self.assertCallsSequence(
[('adb -s 0123456789abcdef shell ls %s' %
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 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',
'\r\n'),
('adb -s 0123456789abcdef shell '
'setprop dalvik.vm.enableassertions "all"',
'')]):
self.assertTrue(self.device.SetJavaAsserts(True))
with self.assertCalls(
(self.call.device.ReadFile(constants.DEVICE_LOCAL_PROPERTIES_PATH),
'some.example.prop=with an example value\n'
'some.other.prop=value_ok\n'),
self.call.device.WriteFile(
constants.DEVICE_LOCAL_PROPERTIES_PATH,
'some.example.prop=with an example value\n'
'some.other.prop=value_ok\n'
'dalvik.vm.enableassertions=all\n'),
(self.call.device.GetProp('dalvik.vm.enableassertions'), ''),
self.call.device.SetProp('dalvik.vm.enableassertions', 'all')):
self.assertTrue(self.device.SetJavaAsserts(True))
def testSetJavaAsserts_disable(self):
mock_file = self.mockNamedTemporary(
read_contents='dalvik.vm.enableassertions=all\n')
with mock.patch('tempfile.NamedTemporaryFile',
return_value=mock_file), (
mock.patch('__builtin__.open', return_value=mock_file)):
with self.assertCallsSequence(
[('adb -s 0123456789abcdef shell ls %s' %
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 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))
with self.assertCalls(
(self.call.device.ReadFile(constants.DEVICE_LOCAL_PROPERTIES_PATH),
'some.example.prop=with an example value\n'
'dalvik.vm.enableassertions=all\n'
'some.other.prop=value_ok\n'),
self.call.device.WriteFile(
constants.DEVICE_LOCAL_PROPERTIES_PATH,
'some.example.prop=with an example value\n'
'some.other.prop=value_ok\n'),
(self.call.device.GetProp('dalvik.vm.enableassertions'), 'all'),
self.call.device.SetProp('dalvik.vm.enableassertions', '')):
self.assertTrue(self.device.SetJavaAsserts(False))
def testSetJavaAsserts_alreadyEnabled(self):
mock_file = self.mockNamedTemporary(
read_contents='dalvik.vm.enableassertions=all\n')
with mock.patch('tempfile.NamedTemporaryFile',
return_value=mock_file), (
mock.patch('__builtin__.open', return_value=mock_file)):
with self.assertCallsSequence(
[('adb -s 0123456789abcdef shell ls %s' %
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))
with self.assertCalls(
(self.call.device.ReadFile(constants.DEVICE_LOCAL_PROPERTIES_PATH),
'some.example.prop=with an example value\n'
'dalvik.vm.enableassertions=all\n'
'some.other.prop=value_ok\n'),
(self.call.device.GetProp('dalvik.vm.enableassertions'), 'all')):
self.assertFalse(self.device.SetJavaAsserts(True))
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