Adding Peter Thatcher to the owners file.
[chromium-blink-merge.git] / build / android / pylib / gtest / setup.py
blob1b882caf3ab7d7904230f6e6bed94879d9236f8f
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
3 # found in the LICENSE file.
5 """Generates test runner factory and tests for GTests."""
6 # pylint: disable=W0212
8 import logging
9 import os
10 import sys
12 from pylib import constants
14 from pylib.base import base_setup
15 from pylib.base import base_test_result
16 from pylib.base import test_dispatcher
17 from pylib.device import device_utils
18 from pylib.gtest import test_package_apk
19 from pylib.gtest import test_package_exe
20 from pylib.gtest import test_runner
22 sys.path.insert(0,
23 os.path.join(constants.DIR_SOURCE_ROOT, 'build', 'util', 'lib',
24 'common'))
25 import unittest_util # pylint: disable=F0401
28 ISOLATE_FILE_PATHS = {
29 'base_unittests': 'base/base_unittests.isolate',
30 'blink_heap_unittests':
31 'third_party/WebKit/Source/platform/heap/BlinkHeapUnitTests.isolate',
32 'breakpad_unittests': 'breakpad/breakpad_unittests.isolate',
33 'cc_perftests': 'cc/cc_perftests.isolate',
34 'components_unittests': 'components/components_unittests.isolate',
35 'content_browsertests': 'content/content_browsertests.isolate',
36 'content_unittests': 'content/content_unittests.isolate',
37 'media_perftests': 'media/media_perftests.isolate',
38 'media_unittests': 'media/media_unittests.isolate',
39 'net_unittests': 'net/net_unittests.isolate',
40 'sql_unittests': 'sql/sql_unittests.isolate',
41 'sync_unit_tests': 'sync/sync_unit_tests.isolate',
42 'ui_base_unittests': 'ui/base/ui_base_tests.isolate',
43 'unit_tests': 'chrome/unit_tests.isolate',
44 'webkit_unit_tests':
45 'third_party/WebKit/Source/web/WebKitUnitTests.isolate',
48 # Used for filtering large data deps at a finer grain than what's allowed in
49 # isolate files since pushing deps to devices is expensive.
50 # Wildcards are allowed.
51 DEPS_EXCLUSION_LIST = [
52 'chrome/test/data/extensions/api_test',
53 'chrome/test/data/extensions/secure_shell',
54 'chrome/test/data/firefox*',
55 'chrome/test/data/gpu',
56 'chrome/test/data/image_decoding',
57 'chrome/test/data/import',
58 'chrome/test/data/page_cycler',
59 'chrome/test/data/perf',
60 'chrome/test/data/pyauto_private',
61 'chrome/test/data/safari_import',
62 'chrome/test/data/scroll',
63 'chrome/test/data/third_party',
64 'third_party/hunspell_dictionaries/*.dic',
65 # crbug.com/258690
66 'webkit/data/bmp_decoder',
67 'webkit/data/ico_decoder',
71 def _GetDisabledTestsFilterFromFile(suite_name):
72 """Returns a gtest filter based on the *_disabled file.
74 Args:
75 suite_name: Name of the test suite (e.g. base_unittests).
77 Returns:
78 A gtest filter which excludes disabled tests.
79 Example: '*-StackTrace.*:StringPrintfTest.StringPrintfMisc'
80 """
81 filter_file_path = os.path.join(
82 os.path.abspath(os.path.dirname(__file__)),
83 'filter', '%s_disabled' % suite_name)
85 if not filter_file_path or not os.path.exists(filter_file_path):
86 logging.info('No filter file found at %s', filter_file_path)
87 return '*'
89 filters = [x for x in [x.strip() for x in file(filter_file_path).readlines()]
90 if x and x[0] != '#']
91 disabled_filter = '*-%s' % ':'.join(filters)
92 logging.info('Applying filter "%s" obtained from %s',
93 disabled_filter, filter_file_path)
94 return disabled_filter
97 def _GetTests(test_options, test_package, devices):
98 """Get a list of tests.
100 Args:
101 test_options: A GTestOptions object.
102 test_package: A TestPackageApk object.
103 devices: A list of attached devices.
105 Returns:
106 A list of all the tests in the test suite.
108 class TestListResult(base_test_result.BaseTestResult):
109 def __init__(self):
110 super(TestListResult, self).__init__(
111 'gtest_list_tests', base_test_result.ResultType.PASS)
112 self.test_list = []
114 def TestListerRunnerFactory(device, _shard_index):
115 class TestListerRunner(test_runner.TestRunner):
116 def RunTest(self, _test):
117 result = TestListResult()
118 self.test_package.Install(self.device)
119 result.test_list = self.test_package.GetAllTests(self.device)
120 results = base_test_result.TestRunResults()
121 results.AddResult(result)
122 return results, None
123 return TestListerRunner(test_options, device, test_package)
125 results, _no_retry = test_dispatcher.RunTests(
126 ['gtest_list_tests'], TestListerRunnerFactory, devices)
127 tests = []
128 for r in results.GetAll():
129 tests.extend(r.test_list)
130 return tests
133 def _FilterTestsUsingPrefixes(all_tests, pre=False, manual=False):
134 """Removes tests with disabled prefixes.
136 Args:
137 all_tests: List of tests to filter.
138 pre: If True, include tests with PRE_ prefix.
139 manual: If True, include tests with MANUAL_ prefix.
141 Returns:
142 List of tests remaining.
144 filtered_tests = []
145 filter_prefixes = ['DISABLED_', 'FLAKY_', 'FAILS_']
147 if not pre:
148 filter_prefixes.append('PRE_')
150 if not manual:
151 filter_prefixes.append('MANUAL_')
153 for t in all_tests:
154 test_case, test = t.split('.', 1)
155 if not any([test_case.startswith(prefix) or test.startswith(prefix) for
156 prefix in filter_prefixes]):
157 filtered_tests.append(t)
158 return filtered_tests
161 def _FilterDisabledTests(tests, suite_name, has_gtest_filter):
162 """Removes disabled tests from |tests|.
164 Applies the following filters in order:
165 1. Remove tests with disabled prefixes.
166 2. Remove tests specified in the *_disabled files in the 'filter' dir
168 Args:
169 tests: List of tests.
170 suite_name: Name of the test suite (e.g. base_unittests).
171 has_gtest_filter: Whether a gtest_filter is provided.
173 Returns:
174 List of tests remaining.
176 tests = _FilterTestsUsingPrefixes(
177 tests, has_gtest_filter, has_gtest_filter)
178 tests = unittest_util.FilterTestNames(
179 tests, _GetDisabledTestsFilterFromFile(suite_name))
181 return tests
184 def Setup(test_options, devices):
185 """Create the test runner factory and tests.
187 Args:
188 test_options: A GTestOptions object.
189 devices: A list of attached devices.
191 Returns:
192 A tuple of (TestRunnerFactory, tests).
194 test_package = test_package_apk.TestPackageApk(test_options.suite_name)
195 if not os.path.exists(test_package.suite_path):
196 exe_test_package = test_package_exe.TestPackageExecutable(
197 test_options.suite_name)
198 if not os.path.exists(exe_test_package.suite_path):
199 raise Exception(
200 'Did not find %s target. Ensure it has been built.\n'
201 '(not found at %s or %s)'
202 % (test_options.suite_name,
203 test_package.suite_path,
204 exe_test_package.suite_path))
205 test_package = exe_test_package
206 logging.warning('Found target %s', test_package.suite_path)
208 base_setup.GenerateDepsDirUsingIsolate(test_options.suite_name,
209 test_options.isolate_file_path,
210 ISOLATE_FILE_PATHS,
211 DEPS_EXCLUSION_LIST)
212 def push_data_deps_to_device_dir(device):
213 device_dir = (constants.TEST_EXECUTABLE_DIR
214 if test_package.suite_name == 'breakpad_unittests'
215 else device.GetExternalStoragePath())
216 base_setup.PushDataDeps(device, device_dir, test_options)
217 device_utils.DeviceUtils.parallel(devices).pMap(push_data_deps_to_device_dir)
219 tests = _GetTests(test_options, test_package, devices)
221 # Constructs a new TestRunner with the current options.
222 def TestRunnerFactory(device, _shard_index):
223 return test_runner.TestRunner(
224 test_options,
225 device,
226 test_package)
228 if test_options.run_disabled:
229 test_options = test_options._replace(
230 test_arguments=('%s --gtest_also_run_disabled_tests' %
231 test_options.test_arguments))
232 else:
233 tests = _FilterDisabledTests(tests, test_options.suite_name,
234 bool(test_options.gtest_filter))
235 if test_options.gtest_filter:
236 tests = unittest_util.FilterTestNames(tests, test_options.gtest_filter)
238 # Coalesce unit tests into a single test per device
239 if test_options.suite_name != 'content_browsertests':
240 num_devices = len(devices)
241 tests = [':'.join(tests[i::num_devices]) for i in xrange(num_devices)]
242 tests = [t for t in tests if t]
244 return (TestRunnerFactory, tests)