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