Enterprise policy: Ignore the deprecated ForceSafeSearch if ForceGoogleSafeSearch...
[chromium-blink-merge.git] / base / test / test_suite.cc
blobd40dd983eb6b143fdbf97466ce25e66ec2be92dc
1 // Copyright (c) 2012 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 #include "base/test/test_suite.h"
7 #include "base/at_exit.h"
8 #include "base/base_paths.h"
9 #include "base/base_switches.h"
10 #include "base/bind.h"
11 #include "base/command_line.h"
12 #include "base/debug/debugger.h"
13 #include "base/debug/stack_trace.h"
14 #include "base/files/file_path.h"
15 #include "base/files/file_util.h"
16 #include "base/i18n/icu_util.h"
17 #include "base/logging.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/path_service.h"
20 #include "base/process/memory.h"
21 #include "base/test/gtest_xml_util.h"
22 #include "base/test/launcher/unit_test_launcher.h"
23 #include "base/test/multiprocess_test.h"
24 #include "base/test/test_switches.h"
25 #include "base/test/test_timeouts.h"
26 #include "base/time/time.h"
27 #include "testing/gmock/include/gmock/gmock.h"
28 #include "testing/gtest/include/gtest/gtest.h"
29 #include "testing/multiprocess_func_list.h"
31 #if defined(OS_MACOSX)
32 #include "base/mac/scoped_nsautorelease_pool.h"
33 #if defined(OS_IOS)
34 #include "base/test/test_listener_ios.h"
35 #endif // OS_IOS
36 #endif // OS_MACOSX
38 #if !defined(OS_WIN)
39 #include "base/i18n/rtl.h"
40 #if !defined(OS_IOS)
41 #include "base/strings/string_util.h"
42 #include "third_party/icu/source/common/unicode/uloc.h"
43 #endif
44 #endif
46 #if defined(OS_ANDROID)
47 #include "base/test/test_support_android.h"
48 #endif
50 #if defined(OS_IOS)
51 #include "base/test/test_support_ios.h"
52 #endif
54 namespace {
56 class MaybeTestDisabler : public testing::EmptyTestEventListener {
57 public:
58 virtual void OnTestStart(const testing::TestInfo& test_info) override {
59 ASSERT_FALSE(TestSuite::IsMarkedMaybe(test_info))
60 << "Probably the OS #ifdefs don't include all of the necessary "
61 "platforms.\nPlease ensure that no tests have the MAYBE_ prefix "
62 "after the code is preprocessed.";
66 class TestClientInitializer : public testing::EmptyTestEventListener {
67 public:
68 TestClientInitializer()
69 : old_command_line_(base::CommandLine::NO_PROGRAM) {
72 virtual void OnTestStart(const testing::TestInfo& test_info) override {
73 old_command_line_ = *base::CommandLine::ForCurrentProcess();
76 virtual void OnTestEnd(const testing::TestInfo& test_info) override {
77 *base::CommandLine::ForCurrentProcess() = old_command_line_;
80 private:
81 base::CommandLine old_command_line_;
83 DISALLOW_COPY_AND_ASSIGN(TestClientInitializer);
86 } // namespace
88 namespace base {
90 int RunUnitTestsUsingBaseTestSuite(int argc, char **argv) {
91 TestSuite test_suite(argc, argv);
92 return base::LaunchUnitTests(
93 argc, argv, Bind(&TestSuite::Run, Unretained(&test_suite)));
96 } // namespace base
98 TestSuite::TestSuite(int argc, char** argv) : initialized_command_line_(false) {
99 PreInitialize(true);
100 InitializeFromCommandLine(argc, argv);
103 #if defined(OS_WIN)
104 TestSuite::TestSuite(int argc, wchar_t** argv)
105 : initialized_command_line_(false) {
106 PreInitialize(true);
107 InitializeFromCommandLine(argc, argv);
109 #endif // defined(OS_WIN)
111 TestSuite::TestSuite(int argc, char** argv, bool create_at_exit_manager)
112 : initialized_command_line_(false) {
113 PreInitialize(create_at_exit_manager);
114 InitializeFromCommandLine(argc, argv);
117 TestSuite::~TestSuite() {
118 if (initialized_command_line_)
119 base::CommandLine::Reset();
122 void TestSuite::InitializeFromCommandLine(int argc, char** argv) {
123 initialized_command_line_ = base::CommandLine::Init(argc, argv);
124 testing::InitGoogleTest(&argc, argv);
125 testing::InitGoogleMock(&argc, argv);
127 #if defined(OS_IOS)
128 InitIOSRunHook(this, argc, argv);
129 #endif
132 #if defined(OS_WIN)
133 void TestSuite::InitializeFromCommandLine(int argc, wchar_t** argv) {
134 // Windows CommandLine::Init ignores argv anyway.
135 initialized_command_line_ = base::CommandLine::Init(argc, NULL);
136 testing::InitGoogleTest(&argc, argv);
137 testing::InitGoogleMock(&argc, argv);
139 #endif // defined(OS_WIN)
141 void TestSuite::PreInitialize(bool create_at_exit_manager) {
142 #if defined(OS_WIN)
143 testing::GTEST_FLAG(catch_exceptions) = false;
144 #endif
145 base::EnableTerminationOnHeapCorruption();
146 #if defined(OS_LINUX) && defined(USE_AURA)
147 // When calling native char conversion functions (e.g wrctomb) we need to
148 // have the locale set. In the absence of such a call the "C" locale is the
149 // default. In the gtk code (below) gtk_init() implicitly sets a locale.
150 setlocale(LC_ALL, "");
151 #endif // defined(OS_LINUX) && defined(USE_AURA)
153 // On Android, AtExitManager is created in
154 // testing/android/native_test_wrapper.cc before main() is called.
155 #if !defined(OS_ANDROID)
156 if (create_at_exit_manager)
157 at_exit_manager_.reset(new base::AtExitManager);
158 #endif
160 // Don't add additional code to this function. Instead add it to
161 // Initialize(). See bug 6436.
165 // static
166 bool TestSuite::IsMarkedMaybe(const testing::TestInfo& test) {
167 return strncmp(test.name(), "MAYBE_", 6) == 0;
170 void TestSuite::CatchMaybeTests() {
171 testing::TestEventListeners& listeners =
172 testing::UnitTest::GetInstance()->listeners();
173 listeners.Append(new MaybeTestDisabler);
176 void TestSuite::ResetCommandLine() {
177 testing::TestEventListeners& listeners =
178 testing::UnitTest::GetInstance()->listeners();
179 listeners.Append(new TestClientInitializer);
182 void TestSuite::AddTestLauncherResultPrinter() {
183 // Only add the custom printer if requested.
184 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
185 switches::kTestLauncherOutput)) {
186 return;
189 FilePath output_path(base::CommandLine::ForCurrentProcess()->
190 GetSwitchValuePath(switches::kTestLauncherOutput));
192 // Do not add the result printer if output path already exists. It's an
193 // indicator there is a process printing to that file, and we're likely
194 // its child. Do not clobber the results in that case.
195 if (PathExists(output_path)) {
196 LOG(WARNING) << "Test launcher output path " << output_path.AsUTF8Unsafe()
197 << " exists. Not adding test launcher result printer.";
198 return;
201 XmlUnitTestResultPrinter* printer = new XmlUnitTestResultPrinter;
202 CHECK(printer->Initialize(output_path));
203 testing::TestEventListeners& listeners =
204 testing::UnitTest::GetInstance()->listeners();
205 listeners.Append(printer);
208 // Don't add additional code to this method. Instead add it to
209 // Initialize(). See bug 6436.
210 int TestSuite::Run() {
211 #if defined(OS_IOS)
212 RunTestsFromIOSApp();
213 #endif
215 #if defined(OS_MACOSX)
216 base::mac::ScopedNSAutoreleasePool scoped_pool;
217 #endif
219 Initialize();
220 std::string client_func =
221 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
222 switches::kTestChildProcess);
224 // Check to see if we are being run as a client process.
225 if (!client_func.empty())
226 return multi_process_function_list::InvokeChildProcessTest(client_func);
227 #if defined(OS_IOS)
228 base::test_listener_ios::RegisterTestEndListener();
229 #endif
230 int result = RUN_ALL_TESTS();
232 #if defined(OS_MACOSX)
233 // This MUST happen before Shutdown() since Shutdown() tears down
234 // objects (such as NotificationService::current()) that Cocoa
235 // objects use to remove themselves as observers.
236 scoped_pool.Recycle();
237 #endif
239 Shutdown();
241 return result;
244 // static
245 void TestSuite::UnitTestAssertHandler(const std::string& str) {
246 #if defined(OS_ANDROID)
247 // Correlating test stdio with logcat can be difficult, so we emit this
248 // helpful little hint about what was running. Only do this for Android
249 // because other platforms don't separate out the relevant logs in the same
250 // way.
251 const ::testing::TestInfo* const test_info =
252 ::testing::UnitTest::GetInstance()->current_test_info();
253 if (test_info) {
254 LOG(ERROR) << "Currently running: " << test_info->test_case_name() << "."
255 << test_info->name();
256 fflush(stderr);
258 #endif // defined(OS_ANDROID)
260 // The logging system actually prints the message before calling the assert
261 // handler. Just exit now to avoid printing too many stack traces.
262 _exit(1);
265 void TestSuite::SuppressErrorDialogs() {
266 #if defined(OS_WIN)
267 UINT new_flags = SEM_FAILCRITICALERRORS |
268 SEM_NOGPFAULTERRORBOX |
269 SEM_NOOPENFILEERRORBOX;
271 // Preserve existing error mode, as discussed at
272 // http://blogs.msdn.com/oldnewthing/archive/2004/07/27/198410.aspx
273 UINT existing_flags = SetErrorMode(new_flags);
274 SetErrorMode(existing_flags | new_flags);
276 #if defined(_DEBUG) && defined(_HAS_EXCEPTIONS) && (_HAS_EXCEPTIONS == 1)
277 // Suppress the "Debug Assertion Failed" dialog.
278 // TODO(hbono): remove this code when gtest has it.
279 // http://groups.google.com/d/topic/googletestframework/OjuwNlXy5ac/discussion
280 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
281 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
282 #endif // defined(_DEBUG) && defined(_HAS_EXCEPTIONS) && (_HAS_EXCEPTIONS == 1)
283 #endif // defined(OS_WIN)
286 void TestSuite::Initialize() {
287 #if !defined(OS_IOS)
288 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
289 switches::kWaitForDebugger)) {
290 base::debug::WaitForDebugger(60, true);
292 #endif
294 #if defined(OS_IOS)
295 InitIOSTestMessageLoop();
296 #endif // OS_IOS
298 #if defined(OS_ANDROID)
299 InitAndroidTest();
300 #else
301 // Initialize logging.
302 base::FilePath exe;
303 PathService::Get(base::FILE_EXE, &exe);
304 base::FilePath log_filename = exe.ReplaceExtension(FILE_PATH_LITERAL("log"));
305 logging::LoggingSettings settings;
306 settings.logging_dest = logging::LOG_TO_ALL;
307 settings.log_file = log_filename.value().c_str();
308 settings.delete_old = logging::DELETE_OLD_LOG_FILE;
309 logging::InitLogging(settings);
310 // We want process and thread IDs because we may have multiple processes.
311 // Note: temporarily enabled timestamps in an effort to catch bug 6361.
312 logging::SetLogItems(true, true, true, true);
313 #endif // else defined(OS_ANDROID)
315 CHECK(base::debug::EnableInProcessStackDumping());
316 #if defined(OS_WIN)
317 // Make sure we run with high resolution timer to minimize differences
318 // between production code and test code.
319 base::Time::EnableHighResolutionTimer(true);
320 #endif // defined(OS_WIN)
322 // In some cases, we do not want to see standard error dialogs.
323 if (!base::debug::BeingDebugged() &&
324 !base::CommandLine::ForCurrentProcess()->HasSwitch(
325 "show-error-dialogs")) {
326 SuppressErrorDialogs();
327 base::debug::SetSuppressDebugUI(true);
328 logging::SetLogAssertHandler(UnitTestAssertHandler);
331 base::i18n::InitializeICU();
332 // On the Mac OS X command line, the default locale is *_POSIX. In Chromium,
333 // the locale is set via an OS X locale API and is never *_POSIX.
334 // Some tests (such as those involving word break iterator) will behave
335 // differently and fail if we use *POSIX locale. Setting it to en_US here
336 // does not affect tests that explicitly overrides the locale for testing.
337 // This can be an issue on all platforms other than Windows.
338 // TODO(jshin): Should we set the locale via an OS X locale API here?
339 #if !defined(OS_WIN)
340 #if defined(OS_IOS)
341 base::i18n::SetICUDefaultLocale("en_US");
342 #else
343 std::string default_locale(uloc_getDefault());
344 if (EndsWith(default_locale, "POSIX", false))
345 base::i18n::SetICUDefaultLocale("en_US");
346 #endif
347 #endif
349 CatchMaybeTests();
350 ResetCommandLine();
351 AddTestLauncherResultPrinter();
353 TestTimeouts::Initialize();
355 trace_to_file_.BeginTracingFromCommandLineOptions();
358 void TestSuite::Shutdown() {