Simple Cache: a few tests for rare corner cases with CRC check missing.
[chromium-blink-merge.git] / base / test / test_suite.cc
blob07b9964578a4a860712524443bf977505ef7119c
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/command_line.h"
11 #include "base/debug/debug_on_start_win.h"
12 #include "base/debug/debugger.h"
13 #include "base/debug/stack_trace.h"
14 #include "base/files/file_path.h"
15 #include "base/i18n/icu_util.h"
16 #include "base/logging.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/path_service.h"
19 #include "base/process_util.h"
20 #include "base/test/multiprocess_test.h"
21 #include "base/test/test_timeouts.h"
22 #include "base/time.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "testing/multiprocess_func_list.h"
26 #if defined(OS_MACOSX)
27 #include "base/mac/scoped_nsautorelease_pool.h"
28 #if defined(OS_IOS)
29 #include "base/test/test_listener_ios.h"
30 #else
31 #include "base/test/mock_chrome_application_mac.h"
32 #endif // OS_IOS
33 #endif // OS_MACOSX
35 #if defined(OS_ANDROID)
36 #include "base/test/test_support_android.h"
37 #endif
39 #if defined(OS_IOS)
40 #include "base/test/test_support_ios.h"
41 #endif
43 #if defined(TOOLKIT_GTK)
44 #include <gtk/gtk.h>
45 #endif
47 namespace {
49 class MaybeTestDisabler : public testing::EmptyTestEventListener {
50 public:
51 virtual void OnTestStart(const testing::TestInfo& test_info) OVERRIDE {
52 ASSERT_FALSE(TestSuite::IsMarkedMaybe(test_info))
53 << "Probably the OS #ifdefs don't include all of the necessary "
54 "platforms.\nPlease ensure that no tests have the MAYBE_ prefix "
55 "after the code is preprocessed.";
59 class TestClientInitializer : public testing::EmptyTestEventListener {
60 public:
61 TestClientInitializer()
62 : old_command_line_(CommandLine::NO_PROGRAM) {
65 virtual void OnTestStart(const testing::TestInfo& test_info) OVERRIDE {
66 old_command_line_ = *CommandLine::ForCurrentProcess();
69 virtual void OnTestEnd(const testing::TestInfo& test_info) OVERRIDE {
70 *CommandLine::ForCurrentProcess() = old_command_line_;
73 private:
74 CommandLine old_command_line_;
76 DISALLOW_COPY_AND_ASSIGN(TestClientInitializer);
79 } // namespace
81 TestSuite::TestSuite(int argc, char** argv) : initialized_command_line_(false) {
82 PreInitialize(argc, argv, true);
85 TestSuite::TestSuite(int argc, char** argv, bool create_at_exit_manager)
86 : initialized_command_line_(false) {
87 PreInitialize(argc, argv, create_at_exit_manager);
90 TestSuite::~TestSuite() {
91 if (initialized_command_line_)
92 CommandLine::Reset();
95 void TestSuite::PreInitialize(int argc, char** argv,
96 bool create_at_exit_manager) {
97 #if defined(OS_WIN)
98 testing::GTEST_FLAG(catch_exceptions) = false;
99 #endif
100 base::EnableTerminationOnHeapCorruption();
101 initialized_command_line_ = CommandLine::Init(argc, argv);
102 testing::InitGoogleTest(&argc, argv);
103 #if defined(OS_LINUX) && defined(USE_AURA)
104 // When calling native char conversion functions (e.g wrctomb) we need to
105 // have the locale set. In the absence of such a call the "C" locale is the
106 // default. In the gtk code (below) gtk_init() implicitly sets a locale.
107 setlocale(LC_ALL, "");
108 #elif defined(TOOLKIT_GTK)
109 gtk_init_check(&argc, &argv);
110 #endif // defined(TOOLKIT_GTK)
112 // On Android, AtExitManager is created in
113 // testing/android/native_test_wrapper.cc before main() is called.
114 #if !defined(OS_ANDROID)
115 if (create_at_exit_manager)
116 at_exit_manager_.reset(new base::AtExitManager);
117 #endif
119 #if defined(OS_IOS)
120 InitIOSRunHook(this, argc, argv);
121 #endif
123 // Don't add additional code to this function. Instead add it to
124 // Initialize(). See bug 6436.
128 // static
129 bool TestSuite::IsMarkedMaybe(const testing::TestInfo& test) {
130 return strncmp(test.name(), "MAYBE_", 6) == 0;
133 void TestSuite::CatchMaybeTests() {
134 testing::TestEventListeners& listeners =
135 testing::UnitTest::GetInstance()->listeners();
136 listeners.Append(new MaybeTestDisabler);
139 void TestSuite::ResetCommandLine() {
140 testing::TestEventListeners& listeners =
141 testing::UnitTest::GetInstance()->listeners();
142 listeners.Append(new TestClientInitializer);
145 // Don't add additional code to this method. Instead add it to
146 // Initialize(). See bug 6436.
147 int TestSuite::Run() {
148 #if defined(OS_IOS)
149 RunTestsFromIOSApp();
150 #endif
152 #if defined(OS_MACOSX)
153 base::mac::ScopedNSAutoreleasePool scoped_pool;
154 #endif
156 Initialize();
157 std::string client_func =
158 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
159 switches::kTestChildProcess);
161 // Check to see if we are being run as a client process.
162 if (!client_func.empty())
163 return multi_process_function_list::InvokeChildProcessTest(client_func);
164 #if defined(OS_IOS)
165 base::test_listener_ios::RegisterTestEndListener();
166 #endif
167 int result = RUN_ALL_TESTS();
169 #if defined(OS_MACOSX)
170 // This MUST happen before Shutdown() since Shutdown() tears down
171 // objects (such as NotificationService::current()) that Cocoa
172 // objects use to remove themselves as observers.
173 scoped_pool.Recycle();
174 #endif
176 Shutdown();
178 return result;
181 // static
182 void TestSuite::UnitTestAssertHandler(const std::string& str) {
183 RAW_LOG(FATAL, str.c_str());
186 void TestSuite::SuppressErrorDialogs() {
187 #if defined(OS_WIN)
188 UINT new_flags = SEM_FAILCRITICALERRORS |
189 SEM_NOGPFAULTERRORBOX |
190 SEM_NOOPENFILEERRORBOX;
192 // Preserve existing error mode, as discussed at
193 // http://blogs.msdn.com/oldnewthing/archive/2004/07/27/198410.aspx
194 UINT existing_flags = SetErrorMode(new_flags);
195 SetErrorMode(existing_flags | new_flags);
197 #if defined(_DEBUG) && defined(_HAS_EXCEPTIONS) && (_HAS_EXCEPTIONS == 1)
198 // Suppress the "Debug Assertion Failed" dialog.
199 // TODO(hbono): remove this code when gtest has it.
200 // http://groups.google.com/d/topic/googletestframework/OjuwNlXy5ac/discussion
201 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
202 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
203 #endif // defined(_DEBUG) && defined(_HAS_EXCEPTIONS) && (_HAS_EXCEPTIONS == 1)
204 #endif // defined(OS_WIN)
207 void TestSuite::Initialize() {
208 #if defined(OS_MACOSX) && !defined(OS_IOS)
209 // Some of the app unit tests spin runloops.
210 mock_cr_app::RegisterMockCrApp();
211 #endif
213 #if defined(OS_IOS)
214 InitIOSTestMessageLoop();
215 #endif // OS_IOS
217 #if defined(OS_ANDROID)
218 InitAndroidTest();
219 #else
220 // Initialize logging.
221 base::FilePath exe;
222 PathService::Get(base::FILE_EXE, &exe);
223 base::FilePath log_filename = exe.ReplaceExtension(FILE_PATH_LITERAL("log"));
224 logging::InitLogging(
225 log_filename.value().c_str(),
226 logging::LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG,
227 logging::LOCK_LOG_FILE,
228 logging::DELETE_OLD_LOG_FILE,
229 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS);
230 // We want process and thread IDs because we may have multiple processes.
231 // Note: temporarily enabled timestamps in an effort to catch bug 6361.
232 logging::SetLogItems(true, true, true, true);
233 #endif // else defined(OS_ANDROID)
235 CHECK(base::debug::EnableInProcessStackDumping());
236 #if defined(OS_WIN)
237 // Make sure we run with high resolution timer to minimize differences
238 // between production code and test code.
239 base::Time::EnableHighResolutionTimer(true);
240 #endif // defined(OS_WIN)
242 // In some cases, we do not want to see standard error dialogs.
243 if (!base::debug::BeingDebugged() &&
244 !CommandLine::ForCurrentProcess()->HasSwitch("show-error-dialogs")) {
245 SuppressErrorDialogs();
246 base::debug::SetSuppressDebugUI(true);
247 logging::SetLogAssertHandler(UnitTestAssertHandler);
250 icu_util::Initialize();
252 CatchMaybeTests();
253 ResetCommandLine();
255 TestTimeouts::Initialize();
258 void TestSuite::Shutdown() {