| 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.ResultType.PASS, log=output) |
| 51 else: | 46 else: |
| 52 results.crashed = [result] | 47 result = base_test_result.BaseTestResult( |
| 53 | 48 self.qualified_name, base_test_result.ResultType.FAIL, log=output) |
| 49 results.AddResult(result) |
| 54 return results | 50 return results |
| 55 | 51 |
| 56 def _LaunchMonkeyTest(self): | 52 def _LaunchMonkeyTest(self): |
| 57 """Runs monkey test for a given package. | 53 """Runs monkey test for a given package. |
| 58 | 54 |
| 59 Looks at the following parameters in the options object provided | 55 Looks at the following parameters in the options object provided |
| 60 in class initializer: | 56 in class initializer: |
| 61 package_name: Allowed package. | 57 package_name: Allowed package. |
| 62 category: A list of allowed categories. | 58 category: A list of allowed categories. |
| 63 throttle: Delay between events (ms). | 59 throttle: Delay between events (ms). |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 attached_devices = android_commands.GetAttachedDevices() | 96 attached_devices = android_commands.GetAttachedDevices() |
| 101 if not attached_devices: | 97 if not attached_devices: |
| 102 raise Exception('You have no devices attached or visible!') | 98 raise Exception('You have no devices attached or visible!') |
| 103 | 99 |
| 104 # Actually run the tests. | 100 # Actually run the tests. |
| 105 logging.debug('Running monkey tests.') | 101 logging.debug('Running monkey tests.') |
| 106 available_tests *= len(attached_devices) | 102 available_tests *= len(attached_devices) |
| 107 options.ensure_value('shard_retries', 1) | 103 options.ensure_value('shard_retries', 1) |
| 108 sharder = python_test_sharder.PythonTestSharder( | 104 sharder = python_test_sharder.PythonTestSharder( |
| 109 attached_devices, available_tests, options) | 105 attached_devices, available_tests, options) |
| 110 result = sharder.RunShardedTests() | 106 results = sharder.RunShardedTests() |
| 111 result.LogFull( | 107 report_results.LogFull( |
| 108 results=results, |
| 112 test_type='Monkey', | 109 test_type='Monkey', |
| 113 test_package='Monkey', | 110 test_package='Monkey', |
| 114 build_type=options.build_type) | 111 build_type=options.build_type) |
| 115 result.PrintAnnotation() | 112 report_results.PrintAnnotation(results) |
| 116 | 113 |
| 117 | 114 |
| 118 def main(): | 115 def main(): |
| 119 desc = 'Run the Monkey tests on 1 or more devices.' | 116 desc = 'Run the Monkey tests on 1 or more devices.' |
| 120 parser = optparse.OptionParser(description=desc) | 117 parser = optparse.OptionParser(description=desc) |
| 121 test_options_parser.AddBuildTypeOption(parser) | 118 test_options_parser.AddBuildTypeOption(parser) |
| 122 parser.add_option('--package-name', help='Allowed package.') | 119 parser.add_option('--package-name', help='Allowed package.') |
| 123 parser.add_option('--activity-name', | 120 parser.add_option('--activity-name', |
| 124 default='com.google.android.apps.chrome.Main', | 121 default='com.google.android.apps.chrome.Main', |
| 125 help='Name of the activity to start [default: %default].') | 122 help='Name of the activity to start [default: %default].') |
| (...skipping 23 matching lines...) Expand all Loading... |
| 149 parser.error('Missing package name') | 146 parser.error('Missing package name') |
| 150 | 147 |
| 151 if options.category: | 148 if options.category: |
| 152 options.category = options.category.split(',') | 149 options.category = options.category.split(',') |
| 153 | 150 |
| 154 DispatchPythonTests(options) | 151 DispatchPythonTests(options) |
| 155 | 152 |
| 156 | 153 |
| 157 if __name__ == '__main__': | 154 if __name__ == '__main__': |
| 158 main() | 155 main() |
| OLD | NEW |