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 | |
29 from pylib import forwarder | 28 from pylib import forwarder |
30 from pylib import valgrind_tools | 29 from pylib import valgrind_tools |
31 from pylib.base import base_test_result | 30 from pylib.base import base_test_result |
32 from pylib.instrumentation import test_package | 31 from pylib.instrumentation import test_package |
33 from pylib.instrumentation import test_result | 32 from pylib.instrumentation import test_result |
34 from pylib.instrumentation import test_runner | 33 from pylib.instrumentation import test_runner |
35 | 34 |
36 # aka the parent of com.google.android | 35 # aka the parent of com.google.android |
37 BASE_ROOT = 'src' + os.sep | 36 BASE_ROOT = 'src' + os.sep |
38 | 37 |
39 | 38 |
40 class HostDrivenTestCase(object): | 39 class HostDrivenTestCase(object): |
41 """Base class for host-driven test cases.""" | 40 """Base class for host-driven test cases.""" |
42 | 41 |
43 _HOST_DRIVEN_TAG = 'HostDriven' | 42 _HOST_DRIVEN_TAG = 'HostDriven' |
44 | 43 |
45 def __init__(self, test_name, instrumentation_options=None): | 44 def __init__(self, test_name, instrumentation_options=None): |
46 """Create a test case initialized to run |test_name|. | 45 """Create a test case initialized to run |test_name|. |
47 | 46 |
48 Args: | 47 Args: |
49 test_name: The name of the method to run as the test. | 48 test_name: The name of the method to run as the test. |
50 instrumentation_options: An InstrumentationOptions object. | 49 instrumentation_options: An InstrumentationOptions object. |
51 """ | 50 """ |
52 self.test_name = test_name | |
53 class_name = self.__class__.__name__ | 51 class_name = self.__class__.__name__ |
54 self.qualified_name = '%s.%s' % (class_name, self.test_name) | 52 self.adb = None |
| 53 self.cleanup_test_files = False |
| 54 self.device_id = '' |
| 55 self.has_forwarded_ports = False |
| 56 self.instrumentation_options = instrumentation_options |
| 57 self.ports_to_forward = [] |
| 58 self.push_deps = False |
| 59 self.shard_index = 0 |
| 60 |
55 # Use tagged_name when creating results, so that we can identify host-driven | 61 # Use tagged_name when creating results, so that we can identify host-driven |
56 # tests in the overall results. | 62 # tests in the overall results. |
| 63 self.test_name = test_name |
| 64 self.qualified_name = '%s.%s' % (class_name, self.test_name) |
57 self.tagged_name = '%s_%s' % (self._HOST_DRIVEN_TAG, self.qualified_name) | 65 self.tagged_name = '%s_%s' % (self._HOST_DRIVEN_TAG, self.qualified_name) |
58 | 66 |
59 self.instrumentation_options = instrumentation_options | |
60 self.ports_to_forward = [] | |
61 self.has_forwarded_ports = False | |
62 | |
63 # TODO(bulach): make ports_to_forward not optional and move the Forwarder | 67 # TODO(bulach): make ports_to_forward not optional and move the Forwarder |
64 # mapping here. | 68 # mapping here. |
65 def SetUp(self, device, shard_index, push_deps, | 69 def SetUp(self, device, shard_index, push_deps, |
66 cleanup_test_files, ports_to_forward=[]): | 70 cleanup_test_files, ports_to_forward=None): |
| 71 if not ports_to_forward: |
| 72 ports_to_forward = [] |
67 self.device_id = device | 73 self.device_id = device |
68 self.shard_index = shard_index | 74 self.shard_index = shard_index |
69 self.adb = android_commands.AndroidCommands(self.device_id) | 75 self.adb = android_commands.AndroidCommands(self.device_id) |
70 self.push_deps = push_deps | 76 self.push_deps = push_deps |
71 self.cleanup_test_files = cleanup_test_files | 77 self.cleanup_test_files = cleanup_test_files |
72 if ports_to_forward: | 78 if ports_to_forward: |
73 self.ports_to_forward = ports_to_forward | 79 self.ports_to_forward = ports_to_forward |
74 | 80 |
75 def TearDown(self): | 81 def TearDown(self): |
76 pass | 82 pass |
77 | 83 |
78 # TODO(craigdh): Remove GetOutDir once references have been removed | 84 # TODO(craigdh): Remove GetOutDir once references have been removed |
79 # downstream. | 85 # downstream. |
80 def GetOutDir(self): | 86 def GetOutDir(self): |
81 return constants.GetOutDirectory() | 87 return constants.GetOutDirectory() |
82 | 88 |
83 def Run(self): | 89 def Run(self): |
84 logging.info('Running host-driven test: %s', self.tagged_name) | 90 logging.info('Running host-driven test: %s', self.tagged_name) |
85 # Get the test method on the derived class and execute it | 91 # Get the test method on the derived class and execute it |
86 return getattr(self, self.test_name)() | 92 return getattr(self, self.test_name)() |
87 | 93 |
88 def __GetHostForwarderLog(self): | 94 @staticmethod |
| 95 def __GetHostForwarderLog(): |
89 return ('-- Begin Full HostForwarder log\n' | 96 return ('-- Begin Full HostForwarder log\n' |
90 '%s\n' | 97 '%s\n' |
91 '--End Full HostForwarder log\n' % forwarder.Forwarder.GetHostLog()) | 98 '--End Full HostForwarder log\n' % forwarder.Forwarder.GetHostLog()) |
92 | 99 |
93 def __StartForwarder(self): | 100 def __StartForwarder(self): |
94 logging.warning('Forwarding %s %s', self.ports_to_forward, | 101 logging.warning('Forwarding %s %s', self.ports_to_forward, |
95 self.has_forwarded_ports) | 102 self.has_forwarded_ports) |
96 if self.ports_to_forward and not self.has_forwarded_ports: | 103 if self.ports_to_forward and not self.has_forwarded_ports: |
97 self.has_forwarded_ports = True | 104 self.has_forwarded_ports = True |
98 tool = valgrind_tools.CreateTool(None, self.adb) | 105 tool = valgrind_tools.CreateTool(None, self.adb) |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 overall_result.AddResult( | 183 overall_result.AddResult( |
177 test_result.InstrumentationTestResult( | 184 test_result.InstrumentationTestResult( |
178 self.tagged_name, test_type, start_ms, duration_ms, log=log)) | 185 self.tagged_name, test_type, start_ms, duration_ms, log=log)) |
179 return overall_result | 186 return overall_result |
180 | 187 |
181 def __str__(self): | 188 def __str__(self): |
182 return self.tagged_name | 189 return self.tagged_name |
183 | 190 |
184 def __repr__(self): | 191 def __repr__(self): |
185 return self.tagged_name | 192 return self.tagged_name |
OLD | NEW |