| 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 """Base class for host-driven test cases. | 5 """Base class for host-driven test cases. |
| 6 | 6 |
| 7 This test case is intended to serve as the base class for any host-driven | 7 This test case is intended to serve as the base class for any host-driven |
| 8 test cases. It is similar to the Python unitttest module in that test cases | 8 test cases. It is similar to the Python unitttest module in that test cases |
| 9 inherit from this class and add methods which will be run as tests. | 9 inherit from this class and add methods which will be run as tests. |
| 10 | 10 |
| 11 When a HostDrivenTestCase object is instantiated, its purpose is to run only one | 11 When a HostDrivenTestCase object is instantiated, its purpose is to run only one |
| 12 test method in the derived class. The test runner gives it the name of the test | 12 test method in the derived class. The test runner gives it the name of the test |
| 13 method the instance will run. The test runner calls SetUp with the device ID | 13 method the instance will run. The test runner calls SetUp with the device ID |
| 14 which the test method will run against. The test runner runs the test method | 14 which the test method will run against. The test runner runs the test method |
| 15 itself, collecting the result, and calls TearDown. | 15 itself, collecting the result, and calls TearDown. |
| 16 | 16 |
| 17 Tests can perform arbitrary Python commands and asserts in test methods. Tests | 17 Tests can perform arbitrary Python commands and asserts in test methods. Tests |
| 18 that run instrumentation tests can make use of the _RunJavaTestFilters helper | 18 that run instrumentation tests can make use of the _RunJavaTestFilters helper |
| 19 function to trigger Java tests and convert results into a single host-driven | 19 function to trigger Java tests and convert results into a single host-driven |
| 20 test result. | 20 test result. |
| 21 """ | 21 """ |
| 22 | 22 |
| 23 import logging | 23 import logging |
| 24 import os | 24 import os |
| 25 import time | 25 import time |
| 26 | 26 |
| 27 from pylib import android_commands | 27 from pylib import android_commands |
| 28 from pylib import constants |
| 28 from pylib import forwarder | 29 from pylib import forwarder |
| 29 from pylib import valgrind_tools | 30 from pylib import valgrind_tools |
| 30 from pylib.base import base_test_result | 31 from pylib.base import base_test_result |
| 31 from pylib.instrumentation import test_package | 32 from pylib.instrumentation import test_package |
| 32 from pylib.instrumentation import test_result | 33 from pylib.instrumentation import test_result |
| 33 from pylib.instrumentation import test_runner | 34 from pylib.instrumentation import test_runner |
| 34 | 35 |
| 35 # aka the parent of com.google.android | 36 # aka the parent of com.google.android |
| 36 BASE_ROOT = 'src' + os.sep | 37 BASE_ROOT = 'src' + os.sep |
| 37 | 38 |
| 38 | 39 |
| 39 class HostDrivenTestCase(object): | 40 class HostDrivenTestCase(object): |
| 40 """Base class for host-driven test cases.""" | 41 """Base class for host-driven test cases.""" |
| 41 | 42 |
| 42 _HOST_DRIVEN_TAG = 'HostDriven' | 43 _HOST_DRIVEN_TAG = 'HostDriven' |
| 43 | 44 |
| 44 def __init__(self, test_name, instrumentation_options=None): | 45 def __init__(self, test_name, instrumentation_options=None): |
| 45 """Create a test case initialized to run |test_name|. | 46 """Create a test case initialized to run |test_name|. |
| 46 | 47 |
| 47 Args: | 48 Args: |
| 48 test_name: The name of the method to run as the test. | 49 test_name: The name of the method to run as the test. |
| 49 instrumentation_options: An InstrumentationOptions object. | 50 instrumentation_options: An InstrumentationOptions object. |
| 50 """ | 51 """ |
| 52 self.test_name = test_name |
| 51 class_name = self.__class__.__name__ | 53 class_name = self.__class__.__name__ |
| 52 self.adb = None | 54 self.qualified_name = '%s.%s' % (class_name, self.test_name) |
| 53 self.cleanup_test_files = False | 55 # Use tagged_name when creating results, so that we can identify host-driven |
| 54 self.device_id = '' | 56 # tests in the overall results. |
| 55 self.has_forwarded_ports = False | 57 self.tagged_name = '%s_%s' % (self._HOST_DRIVEN_TAG, self.qualified_name) |
| 58 |
| 56 self.instrumentation_options = instrumentation_options | 59 self.instrumentation_options = instrumentation_options |
| 57 self.ports_to_forward = [] | 60 self.ports_to_forward = [] |
| 58 self.push_deps = False | 61 self.has_forwarded_ports = False |
| 59 self.shard_index = 0 | |
| 60 | |
| 61 # Use tagged_name when creating results, so that we can identify host-driven | |
| 62 # tests in the overall results. | |
| 63 self.test_name = test_name | |
| 64 self.qualified_name = '%s.%s' % (class_name, self.test_name) | |
| 65 self.tagged_name = '%s_%s' % (self._HOST_DRIVEN_TAG, self.qualified_name) | |
| 66 | 62 |
| 67 # TODO(bulach): make ports_to_forward not optional and move the Forwarder | 63 # TODO(bulach): make ports_to_forward not optional and move the Forwarder |
| 68 # mapping here. | 64 # mapping here. |
| 69 def SetUp(self, device, shard_index, push_deps, | 65 def SetUp(self, device, shard_index, push_deps, |
| 70 cleanup_test_files, ports_to_forward=None): | 66 cleanup_test_files, ports_to_forward=[]): |
| 71 if not ports_to_forward: | |
| 72 ports_to_forward = [] | |
| 73 self.device_id = device | 67 self.device_id = device |
| 74 self.shard_index = shard_index | 68 self.shard_index = shard_index |
| 75 self.adb = android_commands.AndroidCommands(self.device_id) | 69 self.adb = android_commands.AndroidCommands(self.device_id) |
| 76 self.push_deps = push_deps | 70 self.push_deps = push_deps |
| 77 self.cleanup_test_files = cleanup_test_files | 71 self.cleanup_test_files = cleanup_test_files |
| 78 if ports_to_forward: | 72 if ports_to_forward: |
| 79 self.ports_to_forward = ports_to_forward | 73 self.ports_to_forward = ports_to_forward |
| 80 | 74 |
| 81 def TearDown(self): | 75 def TearDown(self): |
| 82 pass | 76 pass |
| 83 | 77 |
| 78 # TODO(craigdh): Remove GetOutDir once references have been removed |
| 79 # downstream. |
| 80 def GetOutDir(self): |
| 81 return constants.GetOutDirectory() |
| 82 |
| 84 def Run(self): | 83 def Run(self): |
| 85 logging.info('Running host-driven test: %s', self.tagged_name) | 84 logging.info('Running host-driven test: %s', self.tagged_name) |
| 86 # Get the test method on the derived class and execute it | 85 # Get the test method on the derived class and execute it |
| 87 return getattr(self, self.test_name)() | 86 return getattr(self, self.test_name)() |
| 88 | 87 |
| 89 @staticmethod | 88 def __GetHostForwarderLog(self): |
| 90 def __GetHostForwarderLog(): | |
| 91 return ('-- Begin Full HostForwarder log\n' | 89 return ('-- Begin Full HostForwarder log\n' |
| 92 '%s\n' | 90 '%s\n' |
| 93 '--End Full HostForwarder log\n' % forwarder.Forwarder.GetHostLog()) | 91 '--End Full HostForwarder log\n' % forwarder.Forwarder.GetHostLog()) |
| 94 | 92 |
| 95 def __StartForwarder(self): | 93 def __StartForwarder(self): |
| 96 logging.warning('Forwarding %s %s', self.ports_to_forward, | 94 logging.warning('Forwarding %s %s', self.ports_to_forward, |
| 97 self.has_forwarded_ports) | 95 self.has_forwarded_ports) |
| 98 if self.ports_to_forward and not self.has_forwarded_ports: | 96 if self.ports_to_forward and not self.has_forwarded_ports: |
| 99 self.has_forwarded_ports = True | 97 self.has_forwarded_ports = True |
| 100 tool = valgrind_tools.CreateTool(None, self.adb) | 98 tool = valgrind_tools.CreateTool(None, self.adb) |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 test_type = base_test_result.ResultType.PASS | 144 test_type = base_test_result.ResultType.PASS |
| 147 log = '' | 145 log = '' |
| 148 | 146 |
| 149 test_pkg = test_package.TestPackage( | 147 test_pkg = test_package.TestPackage( |
| 150 self.instrumentation_options.test_apk_path, | 148 self.instrumentation_options.test_apk_path, |
| 151 self.instrumentation_options.test_apk_jar_path) | 149 self.instrumentation_options.test_apk_jar_path) |
| 152 | 150 |
| 153 start_ms = int(time.time()) * 1000 | 151 start_ms = int(time.time()) * 1000 |
| 154 done = False | 152 done = False |
| 155 for test_filter in test_filters: | 153 for test_filter in test_filters: |
| 156 tests = test_pkg.GetAllMatchingTests(None, None, test_filter) | 154 tests = test_pkg._GetAllMatchingTests(None, None, test_filter) |
| 157 # Filters should always result in >= 1 test. | 155 # Filters should always result in >= 1 test. |
| 158 if len(tests) == 0: | 156 if len(tests) == 0: |
| 159 raise Exception('Java test filter "%s" returned no tests.' | 157 raise Exception('Java test filter "%s" returned no tests.' |
| 160 % test_filter) | 158 % test_filter) |
| 161 for test in tests: | 159 for test in tests: |
| 162 # We're only running one test at a time, so this TestRunResults object | 160 # We're only running one test at a time, so this TestRunResults object |
| 163 # will hold only one result. | 161 # will hold only one result. |
| 164 java_result = self.__RunJavaTest(test, test_pkg, additional_flags) | 162 java_result = self.__RunJavaTest(test, test_pkg, additional_flags) |
| 165 assert len(java_result.GetAll()) == 1 | 163 assert len(java_result.GetAll()) == 1 |
| 166 if not java_result.DidRunPass(): | 164 if not java_result.DidRunPass(): |
| (...skipping 11 matching lines...) Expand all Loading... |
| 178 overall_result.AddResult( | 176 overall_result.AddResult( |
| 179 test_result.InstrumentationTestResult( | 177 test_result.InstrumentationTestResult( |
| 180 self.tagged_name, test_type, start_ms, duration_ms, log=log)) | 178 self.tagged_name, test_type, start_ms, duration_ms, log=log)) |
| 181 return overall_result | 179 return overall_result |
| 182 | 180 |
| 183 def __str__(self): | 181 def __str__(self): |
| 184 return self.tagged_name | 182 return self.tagged_name |
| 185 | 183 |
| 186 def __repr__(self): | 184 def __repr__(self): |
| 187 return self.tagged_name | 185 return self.tagged_name |
| OLD | NEW |