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
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
23 os
.path
.join(constants
.DIR_SOURCE_ROOT
, 'build', 'util', 'lib',
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',
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',
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.
76 suite_name: Name of the test suite (e.g. base_unittests).
79 A gtest filter which excludes disabled tests.
80 Example: '*-StackTrace.*:StringPrintfTest.StringPrintfMisc'
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
)
90 filters
= [x
for x
in [x
.strip() for x
in file(filter_file_path
).readlines()]
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.
102 test_options: A GTestOptions object.
103 test_package: A TestPackageApk object.
104 devices: A list of attached devices.
107 A list of all the tests in the test suite.
109 class TestListResult(base_test_result
.BaseTestResult
):
111 super(TestListResult
, self
).__init
__(
112 'gtest_list_tests', base_test_result
.ResultType
.PASS
)
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
)
124 return TestListerRunner(test_options
, device
, test_package
)
126 results
, _no_retry
= test_dispatcher
.RunTests(
127 ['gtest_list_tests'], TestListerRunnerFactory
, devices
)
129 for r
in results
.GetAll():
130 tests
.extend(r
.test_list
)
134 def _FilterTestsUsingPrefixes(all_tests
, pre
=False, manual
=False):
135 """Removes tests with disabled prefixes.
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.
143 List of tests remaining.
146 filter_prefixes
= ['DISABLED_', 'FLAKY_', 'FAILS_']
149 filter_prefixes
.append('PRE_')
152 filter_prefixes
.append('MANUAL_')
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
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.
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
))
185 def Setup(test_options
, devices
):
186 """Create the test runner factory and tests.
189 test_options: A GTestOptions object.
190 devices: A list of attached devices.
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
):
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
,
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(
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
))
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
)