1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Runs a monkey test on a single device."""
9 from pylib import constants
10 from pylib.base import base_test_result
11 from pylib.base import base_test_runner
14 class TestRunner(base_test_runner.BaseTestRunner):
15 """A TestRunner instance runs a monkey test on a single device."""
17 def __init__(self, test_options, device, _):
18 super(TestRunner, self).__init__(device, None)
19 self._options = test_options
20 self._package = constants.PACKAGE_INFO[self._options.package].package
21 self._activity = constants.PACKAGE_INFO[self._options.package].activity
23 def _LaunchMonkeyTest(self):
24 """Runs monkey test for a given package.
27 Output from the monkey command on the device.
30 timeout_ms = self._options.event_count * self._options.throttle * 1.5
33 '-p %s' % self._package,
34 ' '.join(['-c %s' % c for c in self._options.category]),
35 '--throttle %d' % self._options.throttle,
36 '-s %d' % (self._options.seed or random.randint(1, 100)),
37 '-v ' * self._options.verbose_count,
38 '--monitor-native-crashes',
39 '--kill-process-after-error',
40 self._options.extra_args,
41 '%d' % self._options.event_count]
42 return self.adb.RunShellCommand(' '.join(cmd), timeout_time=timeout_ms)
44 def RunTest(self, test_name):
45 """Run a Monkey test on the device.
48 test_name: String to use for logging the test result.
51 A tuple of (TestRunResults, retry).
53 self.adb.StartActivity(self._package,
55 wait_for_completion=True,
56 action='android.intent.action.MAIN',
59 # Chrome crashes are not always caught by Monkey test runner.
60 # Verify Chrome has the same PID before and after the test.
61 before_pids = self.adb.ExtractPid(self._package)
66 output = '\n'.join(self._LaunchMonkeyTest())
67 after_pids = self.adb.ExtractPid(self._package)
69 crashed = (not before_pids or not after_pids
70 or after_pids[0] != before_pids[0])
72 results = base_test_result.TestRunResults()
73 success_pattern = 'Events injected: %d' % self._options.event_count
74 if success_pattern in output and not crashed:
75 result = base_test_result.BaseTestResult(
76 test_name, base_test_result.ResultType.PASS, log=output)
78 result = base_test_result.BaseTestResult(
79 test_name, base_test_result.ResultType.FAIL, log=output)
80 results.AddResult(result)