OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_TEST_SUITE_H_ | 5 #ifndef BASE_TEST_SUITE_H_ |
6 #define BASE_TEST_SUITE_H_ | 6 #define BASE_TEST_SUITE_H_ |
7 | 7 |
8 // Defines a basic test suite framework for running gtest based tests. You can | 8 // Defines a basic test suite framework for running gtest based tests. You can |
9 // instantiate this class in your main function and call its Run method to run | 9 // instantiate this class in your main function and call its Run method to run |
10 // any gtest based tests that are linked into your executable. | 10 // any gtest based tests that are linked into your executable. |
11 | 11 |
12 #include "base/at_exit.h" | 12 #include "base/at_exit.h" |
13 #include "base/base_paths.h" | 13 #include "base/base_paths.h" |
14 #include "base/command_line.h" | |
15 #include "base/debug_on_start.h" | 14 #include "base/debug_on_start.h" |
16 #include "base/debug_util.h" | |
17 #include "base/file_path.h" | |
18 #include "base/i18n/icu_util.h" | 15 #include "base/i18n/icu_util.h" |
19 #include "base/logging.h" | |
20 #include "base/multiprocess_test.h" | 16 #include "base/multiprocess_test.h" |
| 17 #include "base/process_util.h" |
21 #include "base/scoped_nsautorelease_pool.h" | 18 #include "base/scoped_nsautorelease_pool.h" |
22 #include "base/time.h" | 19 #include "base/time.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
24 #include "testing/multiprocess_func_list.h" | 21 #include "testing/multiprocess_func_list.h" |
25 | 22 |
26 #if defined(OS_WIN) | |
27 #include <windows.h> | |
28 #endif | |
29 | |
30 #if defined(OS_POSIX) | |
31 #include <signal.h> | |
32 #endif | |
33 | |
34 #if defined(OS_LINUX) | 23 #if defined(OS_LINUX) |
35 #include <gtk/gtk.h> | 24 #include <gtk/gtk.h> |
36 #endif | 25 #endif |
37 | 26 |
38 #if defined(OS_POSIX) | |
39 static void TestSuiteCrashHandler(int signal) { | |
40 StackTrace().PrintBacktrace(); | |
41 _exit(1); | |
42 } | |
43 #endif // OS_POSIX | |
44 | |
45 #if defined(OS_WIN) | |
46 // Previous unhandled filter. Will be called if not NULL when we intercept an | |
47 // exception. | |
48 __declspec(selectany) LPTOP_LEVEL_EXCEPTION_FILTER g_previous_filter = NULL; | |
49 | |
50 // Prints the exception call stack. | |
51 // This is the unit tests exception filter. | |
52 inline long WINAPI UnitTestExceptionFilter(EXCEPTION_POINTERS* info) { | |
53 StackTrace(info).PrintBacktrace(); | |
54 if (g_previous_filter) | |
55 return g_previous_filter(info); | |
56 return EXCEPTION_EXECUTE_HANDLER; | |
57 } | |
58 #endif // OS_WIN | |
59 | |
60 // Match function used by the GetTestCount method. | 27 // Match function used by the GetTestCount method. |
61 typedef bool (*TestMatch)(const testing::TestInfo&); | 28 typedef bool (*TestMatch)(const testing::TestInfo&); |
62 | 29 |
63 class TestSuite { | 30 class TestSuite { |
64 public: | 31 public: |
65 TestSuite(int argc, char** argv) { | 32 TestSuite(int argc, char** argv) { |
66 base::EnableTerminationOnHeapCorruption(); | 33 base::EnableTerminationOnHeapCorruption(); |
67 CommandLine::Init(argc, argv); | 34 CommandLine::Init(argc, argv); |
68 testing::InitGoogleTest(&argc, argv); | 35 testing::InitGoogleTest(&argc, argv); |
69 #if defined(OS_LINUX) | 36 #if defined(OS_LINUX) |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 PathService::Get(base::FILE_EXE, &exe); | 145 PathService::Get(base::FILE_EXE, &exe); |
179 FilePath log_filename = exe.ReplaceExtension(FILE_PATH_LITERAL("log")); | 146 FilePath log_filename = exe.ReplaceExtension(FILE_PATH_LITERAL("log")); |
180 logging::InitLogging(log_filename.value().c_str(), | 147 logging::InitLogging(log_filename.value().c_str(), |
181 logging::LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG, | 148 logging::LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG, |
182 logging::LOCK_LOG_FILE, | 149 logging::LOCK_LOG_FILE, |
183 logging::DELETE_OLD_LOG_FILE); | 150 logging::DELETE_OLD_LOG_FILE); |
184 // We want process and thread IDs because we may have multiple processes. | 151 // We want process and thread IDs because we may have multiple processes. |
185 // Note: temporarily enabled timestamps in an effort to catch bug 6361. | 152 // Note: temporarily enabled timestamps in an effort to catch bug 6361. |
186 logging::SetLogItems(true, true, true, true); | 153 logging::SetLogItems(true, true, true, true); |
187 | 154 |
188 #if defined(OS_POSIX) | 155 CHECK(base::EnableInProcessStackDumping()); |
189 // When running in an application, our code typically expects SIGPIPE | |
190 // to be ignored. Therefore, when testing that same code, it should run | |
191 // with SIGPIPE ignored as well. | |
192 struct sigaction action; | |
193 action.sa_handler = SIG_IGN; | |
194 action.sa_flags = 0; | |
195 sigemptyset(&action.sa_mask); | |
196 CHECK(sigaction(SIGPIPE, &action, NULL) == 0); | |
197 | |
198 // TODO(phajdan.jr): Catch other crashy signals, like SIGABRT. | |
199 CHECK(signal(SIGSEGV, &TestSuiteCrashHandler) != SIG_ERR); | |
200 CHECK(signal(SIGILL, &TestSuiteCrashHandler) != SIG_ERR); | |
201 CHECK(signal(SIGBUS, &TestSuiteCrashHandler) != SIG_ERR); | |
202 CHECK(signal(SIGFPE, &TestSuiteCrashHandler) != SIG_ERR); | |
203 #endif // OS_POSIX | |
204 | |
205 #if defined(OS_WIN) | 156 #if defined(OS_WIN) |
206 // For unit tests we turn on the high resolution timer and disable | 157 // For unit tests we turn on the high resolution timer and disable |
207 // base::Time's use of SystemMonitor. Tests create and destroy the message | 158 // base::Time's use of SystemMonitor. Tests create and destroy the message |
208 // loop, which causes a crash with SystemMonitor (http://crbug.com/12187). | 159 // loop, which causes a crash with SystemMonitor (http://crbug.com/12187). |
209 base::Time::EnableHiResClockForTests(); | 160 base::Time::EnableHiResClockForTests(); |
210 | 161 |
211 // In some cases, we do not want to see standard error dialogs. | 162 // In some cases, we do not want to see standard error dialogs. |
212 if (!IsDebuggerPresent() && | 163 if (!IsDebuggerPresent() && |
213 !CommandLine::ForCurrentProcess()->HasSwitch("show-error-dialogs")) { | 164 !CommandLine::ForCurrentProcess()->HasSwitch("show-error-dialogs")) { |
214 SuppressErrorDialogs(); | 165 SuppressErrorDialogs(); |
215 #if !defined(PURIFY) | 166 #if !defined(PURIFY) |
216 // When the code in this file moved around, bug 6436 resurfaced. | 167 // When the code in this file moved around, bug 6436 resurfaced. |
217 // As a hack workaround, just #ifdef out this code for Purify builds. | 168 // As a hack workaround, just #ifdef out this code for Purify builds. |
218 logging::SetLogAssertHandler(UnitTestAssertHandler); | 169 logging::SetLogAssertHandler(UnitTestAssertHandler); |
219 #endif // !defined(PURIFY) | 170 #endif // !defined(PURIFY) |
220 // Add stack dumping support on exception on windows. Similar to OS_POSIX | |
221 // signal() handling above. | |
222 g_previous_filter = SetUnhandledExceptionFilter(&UnitTestExceptionFilter); | |
223 } | 171 } |
224 #endif // defined(OS_WIN) | 172 #endif // defined(OS_WIN) |
225 | 173 |
226 icu_util::Initialize(); | 174 icu_util::Initialize(); |
227 } | 175 } |
228 | 176 |
229 virtual void Shutdown() { | 177 virtual void Shutdown() { |
230 } | 178 } |
231 | 179 |
232 // Make sure that we setup an AtExitManager so Singleton objects will be | 180 // Make sure that we setup an AtExitManager so Singleton objects will be |
233 // destroyed. | 181 // destroyed. |
234 base::AtExitManager at_exit_manager_; | 182 base::AtExitManager at_exit_manager_; |
235 }; | 183 }; |
236 | 184 |
237 #endif // BASE_TEST_SUITE_H_ | 185 #endif // BASE_TEST_SUITE_H_ |
OLD | NEW |