Update broken references to image assets
[chromium-blink-merge.git] / base / test / test_suite.cc
bloba37270c514f097f4b76263169dbaea6d1a818348
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/launch.h"
21 #include "base/process/memory.h"
22 #include "base/test/gtest_xml_unittest_result_printer.h"
23 #include "base/test/gtest_xml_util.h"
24 #include "base/test/launcher/unit_test_launcher.h"
25 #include "base/test/multiprocess_test.h"
26 #include "base/test/test_switches.h"
27 #include "base/test/test_timeouts.h"
28 #include "base/time/time.h"
29 #include "testing/gmock/include/gmock/gmock.h"
30 #include "testing/gtest/include/gtest/gtest.h"
31 #include "testing/multiprocess_func_list.h"
33 #if defined(OS_MACOSX)
34 #include "base/mac/scoped_nsautorelease_pool.h"
35 #if defined(OS_IOS)
36 #include "base/test/test_listener_ios.h"
37 #endif // OS_IOS
38 #endif // OS_MACOSX
40 #if !defined(OS_WIN)
41 #include "base/i18n/rtl.h"
42 #if !defined(OS_IOS)
43 #include "base/strings/string_util.h"
44 #include "third_party/icu/source/common/unicode/uloc.h"
45 #endif
46 #endif
48 #if defined(OS_ANDROID)
49 #include "base/test/test_support_android.h"
50 #endif
52 #if defined(OS_IOS)
53 #include "base/test/test_support_ios.h"
54 #endif
56 namespace base {
58 namespace {
60 class MaybeTestDisabler : public testing::EmptyTestEventListener {
61 public:
62 void OnTestStart(const testing::TestInfo& test_info) override {
63 ASSERT_FALSE(TestSuite::IsMarkedMaybe(test_info))
64 << "Probably the OS #ifdefs don't include all of the necessary "
65 "platforms.\nPlease ensure that no tests have the MAYBE_ prefix "
66 "after the code is preprocessed.";
70 class TestClientInitializer : public testing::EmptyTestEventListener {
71 public:
72 TestClientInitializer()
73 : old_command_line_(CommandLine::NO_PROGRAM) {
76 void OnTestStart(const testing::TestInfo& test_info) override {
77 old_command_line_ = *CommandLine::ForCurrentProcess();
80 void OnTestEnd(const testing::TestInfo& test_info) override {
81 *CommandLine::ForCurrentProcess() = old_command_line_;
84 private:
85 CommandLine old_command_line_;
87 DISALLOW_COPY_AND_ASSIGN(TestClientInitializer);
90 class AtExitManagerResetter : public testing::EmptyTestEventListener {
91 public:
92 AtExitManagerResetter() {}
94 void OnTestStart(const testing::TestInfo& test_info) override {
95 at_exit_manager_.reset(new ShadowingAtExitManager);
98 void OnTestEnd(const testing::TestInfo& test_info) override {
99 at_exit_manager_.reset();
102 private:
103 // Ensures that singletons created during a test run are destroyed afterwards.
104 scoped_ptr<base::ShadowingAtExitManager> at_exit_manager_;
106 DISALLOW_COPY_AND_ASSIGN(AtExitManagerResetter);
109 } // namespace
111 int RunUnitTestsUsingBaseTestSuite(int argc, char **argv) {
112 TestSuite test_suite(argc, argv);
113 return LaunchUnitTests(argc, argv,
114 Bind(&TestSuite::Run, Unretained(&test_suite)));
117 TestSuite::TestSuite(int argc, char** argv) : initialized_command_line_(false) {
118 PreInitialize(true);
119 InitializeFromCommandLine(argc, argv);
122 #if defined(OS_WIN)
123 TestSuite::TestSuite(int argc, wchar_t** argv)
124 : initialized_command_line_(false) {
125 PreInitialize(true);
126 InitializeFromCommandLine(argc, argv);
128 #endif // defined(OS_WIN)
130 TestSuite::TestSuite(int argc, char** argv, bool create_at_exit_manager)
131 : initialized_command_line_(false) {
132 PreInitialize(create_at_exit_manager);
133 InitializeFromCommandLine(argc, argv);
136 TestSuite::~TestSuite() {
137 if (initialized_command_line_)
138 CommandLine::Reset();
141 void TestSuite::InitializeFromCommandLine(int argc, char** argv) {
142 initialized_command_line_ = CommandLine::Init(argc, argv);
143 testing::InitGoogleTest(&argc, argv);
144 testing::InitGoogleMock(&argc, argv);
146 #if defined(OS_IOS)
147 InitIOSRunHook(this, argc, argv);
148 #endif
151 #if defined(OS_WIN)
152 void TestSuite::InitializeFromCommandLine(int argc, wchar_t** argv) {
153 // Windows CommandLine::Init ignores argv anyway.
154 initialized_command_line_ = CommandLine::Init(argc, NULL);
155 testing::InitGoogleTest(&argc, argv);
156 testing::InitGoogleMock(&argc, argv);
158 #endif // defined(OS_WIN)
160 void TestSuite::PreInitialize(bool create_at_exit_manager) {
161 #if defined(OS_WIN)
162 testing::GTEST_FLAG(catch_exceptions) = false;
163 #endif
164 EnableTerminationOnHeapCorruption();
165 #if defined(OS_LINUX) && defined(USE_AURA)
166 // When calling native char conversion functions (e.g wrctomb) we need to
167 // have the locale set. In the absence of such a call the "C" locale is the
168 // default. In the gtk code (below) gtk_init() implicitly sets a locale.
169 setlocale(LC_ALL, "");
170 #endif // defined(OS_LINUX) && defined(USE_AURA)
172 // On Android, AtExitManager is created in
173 // testing/android/native_test_wrapper.cc before main() is called.
174 #if !defined(OS_ANDROID)
175 if (create_at_exit_manager)
176 CreateAtExitManager();
177 #endif
179 // Don't add additional code to this function. Instead add it to
180 // Initialize(). See bug 6436.
184 // static
185 bool TestSuite::IsMarkedMaybe(const testing::TestInfo& test) {
186 return strncmp(test.name(), "MAYBE_", 6) == 0;
189 void TestSuite::CatchMaybeTests() {
190 testing::TestEventListeners& listeners =
191 testing::UnitTest::GetInstance()->listeners();
192 listeners.Append(new MaybeTestDisabler);
195 void TestSuite::ResetCommandLine() {
196 testing::TestEventListeners& listeners =
197 testing::UnitTest::GetInstance()->listeners();
198 listeners.Append(new TestClientInitializer);
201 void TestSuite::CreateAtExitManager() {
202 at_exit_manager_.reset(new base::AtExitManager);
204 testing::TestEventListeners& listeners =
205 testing::UnitTest::GetInstance()->listeners();
206 listeners.Append(new AtExitManagerResetter);
209 void TestSuite::AddTestLauncherResultPrinter() {
210 // Only add the custom printer if requested.
211 if (!CommandLine::ForCurrentProcess()->HasSwitch(
212 switches::kTestLauncherOutput)) {
213 return;
216 FilePath output_path(CommandLine::ForCurrentProcess()->GetSwitchValuePath(
217 switches::kTestLauncherOutput));
219 // Do not add the result printer if output path already exists. It's an
220 // indicator there is a process printing to that file, and we're likely
221 // its child. Do not clobber the results in that case.
222 if (PathExists(output_path)) {
223 LOG(WARNING) << "Test launcher output path " << output_path.AsUTF8Unsafe()
224 << " exists. Not adding test launcher result printer.";
225 return;
228 XmlUnitTestResultPrinter* printer = new XmlUnitTestResultPrinter;
229 CHECK(printer->Initialize(output_path));
230 testing::TestEventListeners& listeners =
231 testing::UnitTest::GetInstance()->listeners();
232 listeners.Append(printer);
235 // Don't add additional code to this method. Instead add it to
236 // Initialize(). See bug 6436.
237 int TestSuite::Run() {
238 #if defined(OS_IOS)
239 RunTestsFromIOSApp();
240 #endif
242 #if defined(OS_MACOSX)
243 mac::ScopedNSAutoreleasePool scoped_pool;
244 #endif
246 Initialize();
247 std::string client_func =
248 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
249 switches::kTestChildProcess);
251 // Check to see if we are being run as a client process.
252 if (!client_func.empty())
253 return multi_process_function_list::InvokeChildProcessTest(client_func);
254 #if defined(OS_IOS)
255 test_listener_ios::RegisterTestEndListener();
256 #endif
257 int result = RUN_ALL_TESTS();
259 #if defined(OS_MACOSX)
260 // This MUST happen before Shutdown() since Shutdown() tears down
261 // objects (such as NotificationService::current()) that Cocoa
262 // objects use to remove themselves as observers.
263 scoped_pool.Recycle();
264 #endif
266 Shutdown();
268 return result;
271 // static
272 void TestSuite::UnitTestAssertHandler(const std::string& str) {
273 #if defined(OS_ANDROID)
274 // Correlating test stdio with logcat can be difficult, so we emit this
275 // helpful little hint about what was running. Only do this for Android
276 // because other platforms don't separate out the relevant logs in the same
277 // way.
278 const ::testing::TestInfo* const test_info =
279 ::testing::UnitTest::GetInstance()->current_test_info();
280 if (test_info) {
281 LOG(ERROR) << "Currently running: " << test_info->test_case_name() << "."
282 << test_info->name();
283 fflush(stderr);
285 #endif // defined(OS_ANDROID)
287 // The logging system actually prints the message before calling the assert
288 // handler. Just exit now to avoid printing too many stack traces.
289 _exit(1);
292 void TestSuite::SuppressErrorDialogs() {
293 #if defined(OS_WIN)
294 UINT new_flags = SEM_FAILCRITICALERRORS |
295 SEM_NOGPFAULTERRORBOX |
296 SEM_NOOPENFILEERRORBOX;
298 // Preserve existing error mode, as discussed at
299 // http://blogs.msdn.com/oldnewthing/archive/2004/07/27/198410.aspx
300 UINT existing_flags = SetErrorMode(new_flags);
301 SetErrorMode(existing_flags | new_flags);
303 #if defined(_DEBUG) && defined(_HAS_EXCEPTIONS) && (_HAS_EXCEPTIONS == 1)
304 // Suppress the "Debug Assertion Failed" dialog.
305 // TODO(hbono): remove this code when gtest has it.
306 // http://groups.google.com/d/topic/googletestframework/OjuwNlXy5ac/discussion
307 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
308 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
309 #endif // defined(_DEBUG) && defined(_HAS_EXCEPTIONS) && (_HAS_EXCEPTIONS == 1)
310 #endif // defined(OS_WIN)
313 void TestSuite::Initialize() {
314 #if !defined(OS_IOS)
315 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kWaitForDebugger)) {
316 debug::WaitForDebugger(60, true);
318 #endif
320 #if defined(OS_IOS)
321 InitIOSTestMessageLoop();
322 #endif // OS_IOS
324 #if defined(OS_ANDROID)
325 InitAndroidTest();
326 #else
327 // Initialize logging.
328 FilePath exe;
329 PathService::Get(FILE_EXE, &exe);
330 FilePath log_filename = exe.ReplaceExtension(FILE_PATH_LITERAL("log"));
331 logging::LoggingSettings settings;
332 settings.logging_dest = logging::LOG_TO_ALL;
333 settings.log_file = log_filename.value().c_str();
334 settings.delete_old = logging::DELETE_OLD_LOG_FILE;
335 logging::InitLogging(settings);
336 // We want process and thread IDs because we may have multiple processes.
337 // Note: temporarily enabled timestamps in an effort to catch bug 6361.
338 logging::SetLogItems(true, true, true, true);
339 #endif // else defined(OS_ANDROID)
341 CHECK(debug::EnableInProcessStackDumping());
342 #if defined(OS_WIN)
343 RouteStdioToConsole(true);
344 // Make sure we run with high resolution timer to minimize differences
345 // between production code and test code.
346 Time::EnableHighResolutionTimer(true);
347 #endif // defined(OS_WIN)
349 // In some cases, we do not want to see standard error dialogs.
350 if (!debug::BeingDebugged() &&
351 !CommandLine::ForCurrentProcess()->HasSwitch("show-error-dialogs")) {
352 SuppressErrorDialogs();
353 debug::SetSuppressDebugUI(true);
354 logging::SetLogAssertHandler(UnitTestAssertHandler);
357 i18n::InitializeICU();
358 // On the Mac OS X command line, the default locale is *_POSIX. In Chromium,
359 // the locale is set via an OS X locale API and is never *_POSIX.
360 // Some tests (such as those involving word break iterator) will behave
361 // differently and fail if we use *POSIX locale. Setting it to en_US here
362 // does not affect tests that explicitly overrides the locale for testing.
363 // This can be an issue on all platforms other than Windows.
364 // TODO(jshin): Should we set the locale via an OS X locale API here?
365 #if !defined(OS_WIN)
366 #if defined(OS_IOS)
367 i18n::SetICUDefaultLocale("en_US");
368 #else
369 std::string default_locale(uloc_getDefault());
370 if (EndsWith(default_locale, "POSIX", CompareCase::INSENSITIVE_ASCII))
371 i18n::SetICUDefaultLocale("en_US");
372 #endif
373 #endif
375 CatchMaybeTests();
376 ResetCommandLine();
377 AddTestLauncherResultPrinter();
379 TestTimeouts::Initialize();
381 trace_to_file_.BeginTracingFromCommandLineOptions();
384 void TestSuite::Shutdown() {
387 } // namespace base