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