Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / build / android / pylib / gtest / setup.py
blob44662d087caa1cc2db5f702c1132643ac7de40db
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_browsertests': 'components/components_browsertests.isolate',
35 'components_unittests': 'components/components_unittests.isolate',
36 'content_browsertests': 'content/content_browsertests.isolate',
37 'content_unittests': 'content/content_unittests.isolate',
38 'media_perftests': 'media/media_perftests.isolate',
39 'media_unittests': 'media/media_unittests.isolate',
40 'net_unittests': 'net/net_unittests.isolate',
41 'sql_unittests': 'sql/sql_unittests.isolate',
42 'sync_unit_tests': 'sync/sync_unit_tests.isolate',
43 'ui_base_unittests': 'ui/base/ui_base_tests.isolate',
44 'unit_tests': 'chrome/unit_tests.isolate',
45 'webkit_unit_tests':
46 'third_party/WebKit/Source/web/WebKitUnitTests.isolate',
49 # Used for filtering large data deps at a finer grain than what's allowed in
50 # isolate files since pushing deps to devices is expensive.
51 # Wildcards are allowed.
52 DEPS_EXCLUSION_LIST = [
53 'chrome/test/data/extensions/api_test',
54 'chrome/test/data/extensions/secure_shell',
55 'chrome/test/data/firefox*',
56 'chrome/test/data/gpu',
57 'chrome/test/data/image_decoding',
58 'chrome/test/data/import',
59 'chrome/test/data/page_cycler',
60 'chrome/test/data/perf',
61 'chrome/test/data/pyauto_private',
62 'chrome/test/data/safari_import',
63 'chrome/test/data/scroll',
64 'chrome/test/data/third_party',
65 'third_party/hunspell_dictionaries/*.dic',
66 # crbug.com/258690
67 'webkit/data/bmp_decoder',
68 'webkit/data/ico_decoder',
72 def _GetDisabledTestsFilterFromFile(suite_name):
73 """Returns a gtest filter based on the *_disabled file.
75 Args:
76 suite_name: Name of the test suite (e.g. base_unittests).
78 Returns:
79 A gtest filter which excludes disabled tests.
80 Example: '*-StackTrace.*:StringPrintfTest.StringPrintfMisc'
81 """
82 filter_file_path = os.path.join(
83 os.path.abspath(os.path.dirname(__file__)),
84 'filter', '%s_disabled' % suite_name)
86 if not filter_file_path or not os.path.exists(filter_file_path):
87 logging.info('No filter file found at %s', filter_file_path)
88 return '*'
90 filters = [x for x in [x.strip() for x in file(filter_file_path).readlines()]
91 if x and x[0] != '#']
92 disabled_filter = '*-%s' % ':'.join(filters)
93 logging.info('Applying filter "%s" obtained from %s',
94 disabled_filter, filter_file_path)
95 return disabled_filter
98 def _GetTests(test_options, test_package, devices):
99 """Get a list of tests.
101 Args:
102 test_options: A GTestOptions object.
103 test_package: A TestPackageApk object.
104 devices: A list of attached devices.
106 Returns:
107 A list of all the tests in the test suite.
109 class TestListResult(base_test_result.BaseTestResult):
110 def __init__(self):
111 super(TestListResult, self).__init__(
112 'gtest_list_tests', base_test_result.ResultType.PASS)
113 self.test_list = []
115 def TestListerRunnerFactory(device, _shard_index):
116 class TestListerRunner(test_runner.TestRunner):
117 def RunTest(self, _test):
118 result = TestListResult()
119 self.test_package.Install(self.device)
120 result.test_list = self.test_package.GetAllTests(self.device)
121 results = base_test_result.TestRunResults()
122 results.AddResult(result)
123 return results, None
124 return TestListerRunner(test_options, device, test_package)
126 results, _no_retry = test_dispatcher.RunTests(
127 ['gtest_list_tests'], TestListerRunnerFactory, devices)
128 tests = []
129 for r in results.GetAll():
130 tests.extend(r.test_list)
131 return tests
134 def _FilterTestsUsingPrefixes(all_tests, pre=False, manual=False):
135 """Removes tests with disabled prefixes.
137 Args:
138 all_tests: List of tests to filter.
139 pre: If True, include tests with PRE_ prefix.
140 manual: If True, include tests with MANUAL_ prefix.
142 Returns:
143 List of tests remaining.
145 filtered_tests = []
146 filter_prefixes = ['DISABLED_', 'FLAKY_', 'FAILS_']
148 if not pre:
149 filter_prefixes.append('PRE_')
151 if not manual:
152 filter_prefixes.append('MANUAL_')
154 for t in all_tests:
155 test_case, test = t.split('.', 1)
156 if not any([test_case.startswith(prefix) or test.startswith(prefix) for
157 prefix in filter_prefixes]):
158 filtered_tests.append(t)
159 return filtered_tests
162 def _FilterDisabledTests(tests, suite_name, has_gtest_filter):
163 """Removes disabled tests from |tests|.
165 Applies the following filters in order:
166 1. Remove tests with disabled prefixes.
167 2. Remove tests specified in the *_disabled files in the 'filter' dir
169 Args:
170 tests: List of tests.
171 suite_name: Name of the test suite (e.g. base_unittests).
172 has_gtest_filter: Whether a gtest_filter is provided.
174 Returns:
175 List of tests remaining.
177 tests = _FilterTestsUsingPrefixes(
178 tests, has_gtest_filter, has_gtest_filter)
179 tests = unittest_util.FilterTestNames(
180 tests, _GetDisabledTestsFilterFromFile(suite_name))
182 return tests
185 def Setup(test_options, devices):
186 """Create the test runner factory and tests.
188 Args:
189 test_options: A GTestOptions object.
190 devices: A list of attached devices.
192 Returns:
193 A tuple of (TestRunnerFactory, tests).
195 test_package = test_package_apk.TestPackageApk(test_options.suite_name)
196 if not os.path.exists(test_package.suite_path):
197 exe_test_package = test_package_exe.TestPackageExecutable(
198 test_options.suite_name)
199 if not os.path.exists(exe_test_package.suite_path):
200 raise Exception(
201 'Did not find %s target. Ensure it has been built.\n'
202 '(not found at %s or %s)'
203 % (test_options.suite_name,
204 test_package.suite_path,
205 exe_test_package.suite_path))
206 test_package = exe_test_package
207 logging.warning('Found target %s', test_package.suite_path)
209 base_setup.GenerateDepsDirUsingIsolate(test_options.suite_name,
210 test_options.isolate_file_path,
211 ISOLATE_FILE_PATHS,
212 DEPS_EXCLUSION_LIST)
213 def push_data_deps_to_device_dir(device):
214 device_dir = (constants.TEST_EXECUTABLE_DIR
215 if test_package.suite_name == 'breakpad_unittests'
216 else device.GetExternalStoragePath())
217 base_setup.PushDataDeps(device, device_dir, test_options)
218 device_utils.DeviceUtils.parallel(devices).pMap(push_data_deps_to_device_dir)
220 tests = _GetTests(test_options, test_package, devices)
222 # Constructs a new TestRunner with the current options.
223 def TestRunnerFactory(device, _shard_index):
224 return test_runner.TestRunner(
225 test_options,
226 device,
227 test_package)
229 if test_options.run_disabled:
230 test_options = test_options._replace(
231 test_arguments=('%s --gtest_also_run_disabled_tests' %
232 test_options.test_arguments))
233 else:
234 tests = _FilterDisabledTests(tests, test_options.suite_name,
235 bool(test_options.gtest_filter))
236 if test_options.gtest_filter:
237 tests = unittest_util.FilterTestNames(tests, test_options.gtest_filter)
239 # Coalesce unit tests into a single test per device
240 if (test_options.suite_name != 'content_browsertests' and
241 test_options.suite_name != 'components_browsertests'):
242 num_devices = len(devices)
243 tests = [':'.join(tests[i::num_devices]) for i in xrange(num_devices)]
244 tests = [t for t in tests if t]
246 return (TestRunnerFactory, tests)