Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 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. |
| 4 | |
| 5 | """Runs a monkey test on a single device.""" |
| 6 | |
| 7 | import random |
| 8 | |
| 9 | from pylib.base import base_test_result |
| 10 | from pylib.base import base_test_runner |
| 11 | |
| 12 | |
| 13 | class TestRunner(base_test_runner.BaseTestRunner): |
| 14 | """A TestRunner instance runs a monkey test on a single device.""" |
| 15 | |
| 16 | def __init__(self, test_options, device, shard_index): |
| 17 | super(TestRunner, self).__init__(device, None, test_options.build_type) |
| 18 | self.options = test_options |
| 19 | |
| 20 | def _LaunchMonkeyTest(self): |
| 21 | """Runs monkey test for a given package. |
| 22 | |
| 23 | Returns: |
| 24 | Output from the monkey command on the device. |
| 25 | """ |
| 26 | |
| 27 | timeout_ms = self.options.event_count * self.options.throttle * 1.5 |
| 28 | |
| 29 | cmd = ['monkey', |
| 30 | '-p %s' % self.options.package_name, |
| 31 | ' '.join(['-c %s' % c for c in self.options.category]), |
| 32 | '--throttle %d' % self.options.throttle, |
| 33 | '-s %d' % (self.options.seed or random.randint(1, 100)), |
| 34 | '-v ' * self.options.verbose_count, |
| 35 | '--monitor-native-crashes', |
| 36 | '--kill-process-after-error', |
| 37 | self.options.extra_args, |
| 38 | '%d' % self.options.event_count] |
| 39 | return self.adb.RunShellCommand(' '.join(cmd), timeout_time=timeout_ms) |
| 40 | |
| 41 | def RunTest(self, test_name): |
| 42 | """Run a Monkey test on the device. |
| 43 | |
| 44 | Args: |
| 45 | test_name: String to use for logging the test result. |
| 46 | |
| 47 | Returns: |
| 48 | A tuple of (TestRunResults, retry). |
| 49 | """ |
| 50 | self.adb.StartActivity(self.options.package_name, |
| 51 | self.options.activity_name, |
| 52 | wait_for_completion=True, |
| 53 | action='android.intent.action.MAIN', |
| 54 | force_stop=True) |
| 55 | |
| 56 | # Chrome crashes are not always caught by Monkey test runner. |
| 57 | # Verify Chrome has the same PID before and after the test. |
| 58 | before_pids = self.adb.ExtractPid(self.options.package_name) |
| 59 | |
| 60 | # Run the test. |
| 61 | output = '' |
| 62 | if before_pids: |
| 63 | output = '\n'.join(self._LaunchMonkeyTest()) |
| 64 | after_pids = self.adb.ExtractPid(self.options.package_name) |
| 65 | |
| 66 | crashed = (not before_pids or not after_pids |
| 67 | or after_pids[0] != before_pids[0]) |
| 68 | |
| 69 | results = base_test_result.TestRunResults() |
| 70 | if 'Monkey finished' in output and not crashed: |
| 71 | result = base_test_result.BaseTestResult( |
| 72 | test_name, base_test_result.ResultType.PASS, log=output) |
| 73 | else: |
| 74 | result = base_test_result.BaseTestResult( |
| 75 | test_name, base_test_result.ResultType.FAIL, log=output) |
| 76 | results.AddResult(result) |
| 77 | return results, False |