OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Runs the Monkey tests on one or more devices.""" | 6 """Runs the Monkey tests on one or more devices.""" |
7 import logging | 7 import logging |
8 import optparse | 8 import optparse |
9 import random | 9 import random |
10 import sys | 10 import sys |
11 import time | |
12 | 11 |
13 from pylib import android_commands | 12 from pylib import android_commands |
14 from pylib.base import test_result | 13 from pylib.base import base_test_result |
15 from pylib.host_driven import python_test_base | 14 from pylib.host_driven import python_test_base |
16 from pylib.host_driven import python_test_sharder | 15 from pylib.host_driven import python_test_sharder |
16 from pylib.utils import report_results | |
17 from pylib.utils import test_options_parser | 17 from pylib.utils import test_options_parser |
18 | 18 |
19 | 19 |
20 class MonkeyTest(python_test_base.PythonTestBase): | 20 class MonkeyTest(python_test_base.PythonTestBase): |
21 def testMonkey(self): | 21 def testMonkey(self): |
22 start_ms = int(time.time()) * 1000 | |
23 | |
24 # Launch and wait for Chrome to launch. | 22 # Launch and wait for Chrome to launch. |
25 self.adb.StartActivity(self.options.package_name, | 23 self.adb.StartActivity(self.options.package_name, |
26 self.options.activity_name, | 24 self.options.activity_name, |
27 wait_for_completion=True, | 25 wait_for_completion=True, |
28 action='android.intent.action.MAIN', | 26 action='android.intent.action.MAIN', |
29 force_stop=True) | 27 force_stop=True) |
30 | 28 |
31 # Chrome crashes are not always caught by Monkey test runner. | 29 # Chrome crashes are not always caught by Monkey test runner. |
32 # Verify Chrome has the same PID before and after the test. | 30 # Verify Chrome has the same PID before and after the test. |
33 before_pids = self.adb.ExtractPid(self.options.package_name) | 31 before_pids = self.adb.ExtractPid(self.options.package_name) |
34 | 32 |
35 # Run the test. | 33 # Run the test. |
36 output = '' | 34 output = '' |
37 duration_ms = 0 | |
38 if before_pids: | 35 if before_pids: |
39 output = '\n'.join(self._LaunchMonkeyTest()) | 36 output = '\n'.join(self._LaunchMonkeyTest()) |
40 duration_ms = int(time.time()) * 1000 - start_ms | |
41 after_pids = self.adb.ExtractPid(self.options.package_name) | 37 after_pids = self.adb.ExtractPid(self.options.package_name) |
42 | 38 |
43 crashed = (not before_pids or not after_pids | 39 crashed = (not before_pids or not after_pids |
44 or after_pids[0] != before_pids[0]) | 40 or after_pids[0] != before_pids[0]) |
45 result = test_result.SingleTestResult(self.qualified_name, start_ms, | |
46 duration_ms, log=output) | |
47 results = test_result.TestResults() | |
48 | 41 |
42 results = base_test_result.TestRunResults() | |
49 if 'Monkey finished' in output and not crashed: | 43 if 'Monkey finished' in output and not crashed: |
50 results.ok = [result] | 44 result = base_test_result.BaseTestResult( |
45 self.qualified_name, base_test_result.TestType.PASS, log=output) | |
51 else: | 46 else: |
52 results.crashed = [result] | 47 result = base_test_result.BaseTestResult( |
48 self.qualified_name, base_test_result.TestType.FAIL, log=output) | |
49 results.AddResult(result) | |
53 | 50 |
craigdh
2013/03/25 19:08:45
nit: no blank line
frankf
2013/03/25 22:37:39
Done.
| |
54 return results | 51 return results |
55 | 52 |
56 def _LaunchMonkeyTest(self): | 53 def _LaunchMonkeyTest(self): |
57 """Runs monkey test for a given package. | 54 """Runs monkey test for a given package. |
58 | 55 |
59 Looks at the following parameters in the options object provided | 56 Looks at the following parameters in the options object provided |
60 in class initializer: | 57 in class initializer: |
61 package_name: Allowed package. | 58 package_name: Allowed package. |
62 category: A list of allowed categories. | 59 category: A list of allowed categories. |
63 throttle: Delay between events (ms). | 60 throttle: Delay between events (ms). |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
100 attached_devices = android_commands.GetAttachedDevices() | 97 attached_devices = android_commands.GetAttachedDevices() |
101 if not attached_devices: | 98 if not attached_devices: |
102 raise Exception('You have no devices attached or visible!') | 99 raise Exception('You have no devices attached or visible!') |
103 | 100 |
104 # Actually run the tests. | 101 # Actually run the tests. |
105 logging.debug('Running monkey tests.') | 102 logging.debug('Running monkey tests.') |
106 available_tests *= len(attached_devices) | 103 available_tests *= len(attached_devices) |
107 options.ensure_value('shard_retries', 1) | 104 options.ensure_value('shard_retries', 1) |
108 sharder = python_test_sharder.PythonTestSharder( | 105 sharder = python_test_sharder.PythonTestSharder( |
109 attached_devices, available_tests, options) | 106 attached_devices, available_tests, options) |
110 result = sharder.RunShardedTests() | 107 results = sharder.RunShardedTests() |
111 result.LogFull( | 108 report_results.LogFull( |
109 results=results, | |
112 test_type='Monkey', | 110 test_type='Monkey', |
113 test_package='Monkey', | 111 test_package='Monkey', |
114 build_type=options.build_type) | 112 build_type=options.build_type) |
115 result.PrintAnnotation() | 113 report_results.PrintAnnotation(results) |
116 | 114 |
117 | 115 |
118 def main(): | 116 def main(): |
119 desc = 'Run the Monkey tests on 1 or more devices.' | 117 desc = 'Run the Monkey tests on 1 or more devices.' |
120 parser = optparse.OptionParser(description=desc) | 118 parser = optparse.OptionParser(description=desc) |
121 test_options_parser.AddBuildTypeOption(parser) | 119 test_options_parser.AddBuildTypeOption(parser) |
122 parser.add_option('--package-name', help='Allowed package.') | 120 parser.add_option('--package-name', help='Allowed package.') |
123 parser.add_option('--activity-name', | 121 parser.add_option('--activity-name', |
124 default='com.google.android.apps.chrome.Main', | 122 default='com.google.android.apps.chrome.Main', |
125 help='Name of the activity to start [default: %default].') | 123 help='Name of the activity to start [default: %default].') |
(...skipping 23 matching lines...) Expand all Loading... | |
149 parser.error('Missing package name') | 147 parser.error('Missing package name') |
150 | 148 |
151 if options.category: | 149 if options.category: |
152 options.category = options.category.split(',') | 150 options.category = options.category.split(',') |
153 | 151 |
154 DispatchPythonTests(options) | 152 DispatchPythonTests(options) |
155 | 153 |
156 | 154 |
157 if __name__ == '__main__': | 155 if __name__ == '__main__': |
158 main() | 156 main() |
OLD | NEW |