Commit 10a67053 authored by jbudorick's avatar jbudorick Committed by Commit bot

[Android] Fix UIAutomator test running.

I broke the uiautomator tests with my change to instrumentation test
running in https://codereview.chromium.org/558883003/. The revised
implementation never called _RunTest, which the uiautomator test runner
overrides to run its tests. As a result, we were trying to run the
uiautomator tests with 'am instrument', which (obviously) doesn't work.

This patch revises the implementation of the instrumentation test runner
s.t. it does call _RunTest again.

BUG=417492

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

Cr-Commit-Position: refs/heads/master@{#296835}
parent b251cb9f
......@@ -339,44 +339,26 @@ class TestRunner(base_test_runner.BaseTestRunner):
"1 minute for timeout.").format(test))
return 1 * 60
def RunInstrumentationTest(self, test, test_package, instr_args, timeout):
def _RunTest(self, test, timeout):
"""Runs a single instrumentation test.
Args:
test: Test class/method.
test_package: Package name of test apk.
instr_args: Extra key/value to pass to am instrument.
timeout: Timeout time in seconds.
Returns:
An instance of InstrumentationTestResult
The raw output of am instrument as a list of lines.
"""
# Build the 'am instrument' command
instrumentation_path = (
'%s/%s' % (test_package, self.options.test_runner))
'%s/%s' % (self.test_pkg.GetPackageName(), self.options.test_runner))
cmd = ['am', 'instrument', '-r']
for k, v in instr_args.iteritems():
for k, v in self._GetInstrumentationArgs().iteritems():
cmd.extend(['-e', k, "'%s'" % v])
cmd.extend(['-e', 'class', "'%s'" % test])
cmd.extend(['-w', instrumentation_path])
time_ms = lambda: int(time.time() * 1000)
# Run the test.
start_ms = time_ms()
try:
instr_output = self.device.RunShellCommand(
cmd, timeout=timeout, retries=0)
except device_errors.CommandTimeoutError:
return test_result.InstrumentationTestResult(
test, base_test_result.ResultType.TIMEOUT, start_ms,
time_ms() - start_ms)
duration_ms = time_ms() - start_ms
# Parse the test output
_, _, statuses = self._ParseAmInstrumentRawOutput(instr_output)
return self._GenerateTestResult(test, statuses, start_ms, duration_ms)
return self.device.RunShellCommand(cmd, timeout=timeout, retries=0)
@staticmethod
def _ParseAmInstrumentRawOutput(raw_output):
......@@ -502,19 +484,28 @@ class TestRunner(base_test_runner.BaseTestRunner):
timeout = (self._GetIndividualTestTimeoutSecs(test) *
self._GetIndividualTestTimeoutScale(test) *
self.tool.GetTimeoutScale())
start_ms = 0
duration_ms = 0
try:
self.TestSetup(test)
result = self.RunInstrumentationTest(
test, self.test_pkg.GetPackageName(), self._GetInstrumentationArgs(),
timeout)
time_ms = lambda: int(time.time() * 1000)
start_ms = time_ms()
raw_output = self._RunTest(test, timeout)
duration_ms = time_ms() - start_ms
# Parse the test output
_, _, statuses = self._ParseAmInstrumentRawOutput(raw_output)
result = self._GenerateTestResult(test, statuses, start_ms, duration_ms)
results.AddResult(result)
except (device_errors.CommandTimeoutError,
device_errors.DeviceUnreachableError) as e:
message = str(e)
if not message:
message = 'No information.'
except device_errors.CommandTimeoutError as e:
results.AddResult(test_result.InstrumentationTestResult(
test, base_test_result.ResultType.TIMEOUT, start_ms, duration_ms,
log=str(e) or 'No information'))
except device_errors.DeviceUnreachableError as e:
results.AddResult(test_result.InstrumentationTestResult(
test, base_test_result.ResultType.CRASH, int(time.time() * 1000), 0,
log=message))
test, base_test_result.ResultType.CRASH, start_ms, duration_ms,
log=str(e) or 'No information'))
self.TestTeardown(test, results)
return (results, None if results.DidRunPass() else test)
......@@ -251,18 +251,14 @@ class InstrumentationTestRunnerTest(unittest.TestCase):
self.assertEqual(base_test_result.ResultType.CRASH, result.GetType())
self.assertEqual('\nfoo/bar.py (27)\nhello/world.py (42)', result.GetLog())
def testRunInstrumentationTest_verifyAdbShellCommand(self):
def test_RunTest_verifyAdbShellCommand(self):
self.instance.options.test_runner = 'MyTestRunner'
self.instance.device.RunShellCommand = mock.Mock()
self.instance._GenerateTestResult = mock.Mock()
with mock.patch('pylib.instrumentation.test_runner.'
'TestRunner._ParseAmInstrumentRawOutput',
return_value=(mock.Mock(), mock.Mock(), mock.Mock())):
self.instance.RunInstrumentationTest(
'test.package.TestClass#testMethod',
'test.package',
{'test_arg_key': 'test_arg_value'},
100)
self.instance.test_pkg.GetPackageName = mock.Mock(
return_value='test.package')
self.instance._GetInstrumentationArgs = mock.Mock(
return_value={'test_arg_key': 'test_arg_value'})
self.instance._RunTest('test.package.TestClass#testMethod', 100)
self.instance.device.RunShellCommand.assert_called_with(
['am', 'instrument', '-r',
'-e', 'test_arg_key', "'test_arg_value'",
......
......@@ -74,5 +74,13 @@ class TestRunner(instr_test_runner.TestRunner):
package=self._package),
blocking=True,
force_stop=True)
return self.device.old_interface.RunUIAutomatorTest(
test, self.test_pkg.GetPackageName(), timeout)
cmd = ['uiautomator', 'runtest', self.test_pkg.GetPackageName(),
'-e', 'class', test]
return self.device.RunShellCommand(cmd, timeout=timeout, retries=0)
#override
def _GenerateTestResult(self, test, instr_statuses, start_ms, duration_ms):
# uiautomator emits its summary status with INSTRUMENTATION_STATUS_CODE,
# not INSTRUMENTATION_CODE, so we have to drop if off the list of statuses.
return super(TestRunner, self)._GenerateTestResult(
test, instr_statuses[:-1], start_ms, duration_ms)
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