Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: base/test/test_suite.cc

Issue 1128733002: Update from https://crrev.com/328418 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/test/test_suite.h ('k') | base/test/thread_test_helper.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 #include "base/test/test_suite.h" 5 #include "base/test/test_suite.h"
6 6
7 #include "base/at_exit.h" 7 #include "base/at_exit.h"
8 #include "base/base_paths.h" 8 #include "base/base_paths.h"
9 #include "base/base_switches.h" 9 #include "base/base_switches.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 #endif 44 #endif
45 45
46 #if defined(OS_ANDROID) 46 #if defined(OS_ANDROID)
47 #include "base/test/test_support_android.h" 47 #include "base/test/test_support_android.h"
48 #endif 48 #endif
49 49
50 #if defined(OS_IOS) 50 #if defined(OS_IOS)
51 #include "base/test/test_support_ios.h" 51 #include "base/test/test_support_ios.h"
52 #endif 52 #endif
53 53
54 namespace base {
55
54 namespace { 56 namespace {
55 57
56 class MaybeTestDisabler : public testing::EmptyTestEventListener { 58 class MaybeTestDisabler : public testing::EmptyTestEventListener {
57 public: 59 public:
58 void OnTestStart(const testing::TestInfo& test_info) override { 60 void OnTestStart(const testing::TestInfo& test_info) override {
59 ASSERT_FALSE(TestSuite::IsMarkedMaybe(test_info)) 61 ASSERT_FALSE(TestSuite::IsMarkedMaybe(test_info))
60 << "Probably the OS #ifdefs don't include all of the necessary " 62 << "Probably the OS #ifdefs don't include all of the necessary "
61 "platforms.\nPlease ensure that no tests have the MAYBE_ prefix " 63 "platforms.\nPlease ensure that no tests have the MAYBE_ prefix "
62 "after the code is preprocessed."; 64 "after the code is preprocessed.";
63 } 65 }
64 }; 66 };
65 67
66 class TestClientInitializer : public testing::EmptyTestEventListener { 68 class TestClientInitializer : public testing::EmptyTestEventListener {
67 public: 69 public:
68 TestClientInitializer() 70 TestClientInitializer()
69 : old_command_line_(base::CommandLine::NO_PROGRAM) { 71 : old_command_line_(CommandLine::NO_PROGRAM) {
70 } 72 }
71 73
72 void OnTestStart(const testing::TestInfo& test_info) override { 74 void OnTestStart(const testing::TestInfo& test_info) override {
73 old_command_line_ = *base::CommandLine::ForCurrentProcess(); 75 old_command_line_ = *CommandLine::ForCurrentProcess();
74 } 76 }
75 77
76 void OnTestEnd(const testing::TestInfo& test_info) override { 78 void OnTestEnd(const testing::TestInfo& test_info) override {
77 *base::CommandLine::ForCurrentProcess() = old_command_line_; 79 *CommandLine::ForCurrentProcess() = old_command_line_;
78 } 80 }
79 81
80 private: 82 private:
81 base::CommandLine old_command_line_; 83 CommandLine old_command_line_;
82 84
83 DISALLOW_COPY_AND_ASSIGN(TestClientInitializer); 85 DISALLOW_COPY_AND_ASSIGN(TestClientInitializer);
84 }; 86 };
85 87
86 } // namespace 88 } // namespace
87 89
88 namespace base {
89
90 int RunUnitTestsUsingBaseTestSuite(int argc, char **argv) { 90 int RunUnitTestsUsingBaseTestSuite(int argc, char **argv) {
91 TestSuite test_suite(argc, argv); 91 TestSuite test_suite(argc, argv);
92 return base::LaunchUnitTests( 92 return LaunchUnitTests(argc, argv,
93 argc, argv, Bind(&TestSuite::Run, Unretained(&test_suite))); 93 Bind(&TestSuite::Run, Unretained(&test_suite)));
94 } 94 }
95 95
96 } // namespace base
97
98 TestSuite::TestSuite(int argc, char** argv) : initialized_command_line_(false) { 96 TestSuite::TestSuite(int argc, char** argv) : initialized_command_line_(false) {
99 PreInitialize(true); 97 PreInitialize(true);
100 InitializeFromCommandLine(argc, argv); 98 InitializeFromCommandLine(argc, argv);
101 } 99 }
102 100
103 #if defined(OS_WIN) 101 #if defined(OS_WIN)
104 TestSuite::TestSuite(int argc, wchar_t** argv) 102 TestSuite::TestSuite(int argc, wchar_t** argv)
105 : initialized_command_line_(false) { 103 : initialized_command_line_(false) {
106 PreInitialize(true); 104 PreInitialize(true);
107 InitializeFromCommandLine(argc, argv); 105 InitializeFromCommandLine(argc, argv);
108 } 106 }
109 #endif // defined(OS_WIN) 107 #endif // defined(OS_WIN)
110 108
111 TestSuite::TestSuite(int argc, char** argv, bool create_at_exit_manager) 109 TestSuite::TestSuite(int argc, char** argv, bool create_at_exit_manager)
112 : initialized_command_line_(false) { 110 : initialized_command_line_(false) {
113 PreInitialize(create_at_exit_manager); 111 PreInitialize(create_at_exit_manager);
114 InitializeFromCommandLine(argc, argv); 112 InitializeFromCommandLine(argc, argv);
115 } 113 }
116 114
117 TestSuite::~TestSuite() { 115 TestSuite::~TestSuite() {
118 if (initialized_command_line_) 116 if (initialized_command_line_)
119 base::CommandLine::Reset(); 117 CommandLine::Reset();
120 } 118 }
121 119
122 void TestSuite::InitializeFromCommandLine(int argc, char** argv) { 120 void TestSuite::InitializeFromCommandLine(int argc, char** argv) {
123 initialized_command_line_ = base::CommandLine::Init(argc, argv); 121 initialized_command_line_ = CommandLine::Init(argc, argv);
124 testing::InitGoogleTest(&argc, argv); 122 testing::InitGoogleTest(&argc, argv);
125 testing::InitGoogleMock(&argc, argv); 123 testing::InitGoogleMock(&argc, argv);
126 124
127 #if defined(OS_IOS) 125 #if defined(OS_IOS)
128 InitIOSRunHook(this, argc, argv); 126 InitIOSRunHook(this, argc, argv);
129 #endif 127 #endif
130 } 128 }
131 129
132 #if defined(OS_WIN) 130 #if defined(OS_WIN)
133 void TestSuite::InitializeFromCommandLine(int argc, wchar_t** argv) { 131 void TestSuite::InitializeFromCommandLine(int argc, wchar_t** argv) {
134 // Windows CommandLine::Init ignores argv anyway. 132 // Windows CommandLine::Init ignores argv anyway.
135 initialized_command_line_ = base::CommandLine::Init(argc, NULL); 133 initialized_command_line_ = CommandLine::Init(argc, NULL);
136 testing::InitGoogleTest(&argc, argv); 134 testing::InitGoogleTest(&argc, argv);
137 testing::InitGoogleMock(&argc, argv); 135 testing::InitGoogleMock(&argc, argv);
138 } 136 }
139 #endif // defined(OS_WIN) 137 #endif // defined(OS_WIN)
140 138
141 void TestSuite::PreInitialize(bool create_at_exit_manager) { 139 void TestSuite::PreInitialize(bool create_at_exit_manager) {
142 #if defined(OS_WIN) 140 #if defined(OS_WIN)
143 testing::GTEST_FLAG(catch_exceptions) = false; 141 testing::GTEST_FLAG(catch_exceptions) = false;
144 #endif 142 #endif
145 base::EnableTerminationOnHeapCorruption(); 143 EnableTerminationOnHeapCorruption();
146 #if defined(OS_LINUX) && defined(USE_AURA) 144 #if defined(OS_LINUX) && defined(USE_AURA)
147 // When calling native char conversion functions (e.g wrctomb) we need to 145 // 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 146 // 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. 147 // default. In the gtk code (below) gtk_init() implicitly sets a locale.
150 setlocale(LC_ALL, ""); 148 setlocale(LC_ALL, "");
151 #endif // defined(OS_LINUX) && defined(USE_AURA) 149 #endif // defined(OS_LINUX) && defined(USE_AURA)
152 150
153 // On Android, AtExitManager is created in 151 // On Android, AtExitManager is created in
154 // testing/android/native_test_wrapper.cc before main() is called. 152 // testing/android/native_test_wrapper.cc before main() is called.
155 #if !defined(OS_ANDROID) 153 #if !defined(OS_ANDROID)
156 if (create_at_exit_manager) 154 if (create_at_exit_manager)
157 at_exit_manager_.reset(new base::AtExitManager); 155 at_exit_manager_.reset(new AtExitManager);
158 #endif 156 #endif
159 157
160 // Don't add additional code to this function. Instead add it to 158 // Don't add additional code to this function. Instead add it to
161 // Initialize(). See bug 6436. 159 // Initialize(). See bug 6436.
162 } 160 }
163 161
164 162
165 // static 163 // static
166 bool TestSuite::IsMarkedMaybe(const testing::TestInfo& test) { 164 bool TestSuite::IsMarkedMaybe(const testing::TestInfo& test) {
167 return strncmp(test.name(), "MAYBE_", 6) == 0; 165 return strncmp(test.name(), "MAYBE_", 6) == 0;
168 } 166 }
169 167
170 void TestSuite::CatchMaybeTests() { 168 void TestSuite::CatchMaybeTests() {
171 testing::TestEventListeners& listeners = 169 testing::TestEventListeners& listeners =
172 testing::UnitTest::GetInstance()->listeners(); 170 testing::UnitTest::GetInstance()->listeners();
173 listeners.Append(new MaybeTestDisabler); 171 listeners.Append(new MaybeTestDisabler);
174 } 172 }
175 173
176 void TestSuite::ResetCommandLine() { 174 void TestSuite::ResetCommandLine() {
177 testing::TestEventListeners& listeners = 175 testing::TestEventListeners& listeners =
178 testing::UnitTest::GetInstance()->listeners(); 176 testing::UnitTest::GetInstance()->listeners();
179 listeners.Append(new TestClientInitializer); 177 listeners.Append(new TestClientInitializer);
180 } 178 }
181 179
182 void TestSuite::AddTestLauncherResultPrinter() { 180 void TestSuite::AddTestLauncherResultPrinter() {
183 // Only add the custom printer if requested. 181 // Only add the custom printer if requested.
184 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( 182 if (!CommandLine::ForCurrentProcess()->HasSwitch(
185 switches::kTestLauncherOutput)) { 183 switches::kTestLauncherOutput)) {
186 return; 184 return;
187 } 185 }
188 186
189 FilePath output_path(base::CommandLine::ForCurrentProcess()-> 187 FilePath output_path(CommandLine::ForCurrentProcess()->GetSwitchValuePath(
190 GetSwitchValuePath(switches::kTestLauncherOutput)); 188 switches::kTestLauncherOutput));
191 189
192 // Do not add the result printer if output path already exists. It's an 190 // 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 191 // indicator there is a process printing to that file, and we're likely
194 // its child. Do not clobber the results in that case. 192 // its child. Do not clobber the results in that case.
195 if (PathExists(output_path)) { 193 if (PathExists(output_path)) {
196 LOG(WARNING) << "Test launcher output path " << output_path.AsUTF8Unsafe() 194 LOG(WARNING) << "Test launcher output path " << output_path.AsUTF8Unsafe()
197 << " exists. Not adding test launcher result printer."; 195 << " exists. Not adding test launcher result printer.";
198 return; 196 return;
199 } 197 }
200 198
201 XmlUnitTestResultPrinter* printer = new XmlUnitTestResultPrinter; 199 XmlUnitTestResultPrinter* printer = new XmlUnitTestResultPrinter;
202 CHECK(printer->Initialize(output_path)); 200 CHECK(printer->Initialize(output_path));
203 testing::TestEventListeners& listeners = 201 testing::TestEventListeners& listeners =
204 testing::UnitTest::GetInstance()->listeners(); 202 testing::UnitTest::GetInstance()->listeners();
205 listeners.Append(printer); 203 listeners.Append(printer);
206 } 204 }
207 205
208 // Don't add additional code to this method. Instead add it to 206 // Don't add additional code to this method. Instead add it to
209 // Initialize(). See bug 6436. 207 // Initialize(). See bug 6436.
210 int TestSuite::Run() { 208 int TestSuite::Run() {
211 #if defined(OS_IOS) 209 #if defined(OS_IOS)
212 RunTestsFromIOSApp(); 210 RunTestsFromIOSApp();
213 #endif 211 #endif
214 212
215 #if defined(OS_MACOSX) 213 #if defined(OS_MACOSX)
216 base::mac::ScopedNSAutoreleasePool scoped_pool; 214 mac::ScopedNSAutoreleasePool scoped_pool;
217 #endif 215 #endif
218 216
219 Initialize(); 217 Initialize();
220 std::string client_func = 218 std::string client_func =
221 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 219 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
222 switches::kTestChildProcess); 220 switches::kTestChildProcess);
223 221
224 // Check to see if we are being run as a client process. 222 // Check to see if we are being run as a client process.
225 if (!client_func.empty()) 223 if (!client_func.empty())
226 return multi_process_function_list::InvokeChildProcessTest(client_func); 224 return multi_process_function_list::InvokeChildProcessTest(client_func);
227 #if defined(OS_IOS) 225 #if defined(OS_IOS)
228 base::test_listener_ios::RegisterTestEndListener(); 226 test_listener_ios::RegisterTestEndListener();
229 #endif 227 #endif
230 int result = RUN_ALL_TESTS(); 228 int result = RUN_ALL_TESTS();
231 229
232 #if defined(OS_MACOSX) 230 #if defined(OS_MACOSX)
233 // This MUST happen before Shutdown() since Shutdown() tears down 231 // This MUST happen before Shutdown() since Shutdown() tears down
234 // objects (such as NotificationService::current()) that Cocoa 232 // objects (such as NotificationService::current()) that Cocoa
235 // objects use to remove themselves as observers. 233 // objects use to remove themselves as observers.
236 scoped_pool.Recycle(); 234 scoped_pool.Recycle();
237 #endif 235 #endif
238 236
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 // TODO(hbono): remove this code when gtest has it. 276 // TODO(hbono): remove this code when gtest has it.
279 // http://groups.google.com/d/topic/googletestframework/OjuwNlXy5ac/discussion 277 // http://groups.google.com/d/topic/googletestframework/OjuwNlXy5ac/discussion
280 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG); 278 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
281 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR); 279 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
282 #endif // defined(_DEBUG) && defined(_HAS_EXCEPTIONS) && (_HAS_EXCEPTIONS == 1) 280 #endif // defined(_DEBUG) && defined(_HAS_EXCEPTIONS) && (_HAS_EXCEPTIONS == 1)
283 #endif // defined(OS_WIN) 281 #endif // defined(OS_WIN)
284 } 282 }
285 283
286 void TestSuite::Initialize() { 284 void TestSuite::Initialize() {
287 #if !defined(OS_IOS) 285 #if !defined(OS_IOS)
288 if (base::CommandLine::ForCurrentProcess()->HasSwitch( 286 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kWaitForDebugger)) {
289 switches::kWaitForDebugger)) { 287 debug::WaitForDebugger(60, true);
290 base::debug::WaitForDebugger(60, true);
291 } 288 }
292 #endif 289 #endif
293 290
294 #if defined(OS_IOS) 291 #if defined(OS_IOS)
295 InitIOSTestMessageLoop(); 292 InitIOSTestMessageLoop();
296 #endif // OS_IOS 293 #endif // OS_IOS
297 294
298 #if defined(OS_ANDROID) 295 #if defined(OS_ANDROID)
299 InitAndroidTest(); 296 InitAndroidTest();
300 #else 297 #else
301 // Initialize logging. 298 // Initialize logging.
302 base::FilePath exe; 299 FilePath exe;
303 PathService::Get(base::FILE_EXE, &exe); 300 PathService::Get(FILE_EXE, &exe);
304 base::FilePath log_filename = exe.ReplaceExtension(FILE_PATH_LITERAL("log")); 301 FilePath log_filename = exe.ReplaceExtension(FILE_PATH_LITERAL("log"));
305 logging::LoggingSettings settings; 302 logging::LoggingSettings settings;
306 settings.logging_dest = logging::LOG_TO_ALL; 303 settings.logging_dest = logging::LOG_TO_ALL;
307 settings.log_file = log_filename.value().c_str(); 304 settings.log_file = log_filename.value().c_str();
308 settings.delete_old = logging::DELETE_OLD_LOG_FILE; 305 settings.delete_old = logging::DELETE_OLD_LOG_FILE;
309 logging::InitLogging(settings); 306 logging::InitLogging(settings);
310 // We want process and thread IDs because we may have multiple processes. 307 // We want process and thread IDs because we may have multiple processes.
311 // Note: temporarily enabled timestamps in an effort to catch bug 6361. 308 // Note: temporarily enabled timestamps in an effort to catch bug 6361.
312 logging::SetLogItems(true, true, true, true); 309 logging::SetLogItems(true, true, true, true);
313 #endif // else defined(OS_ANDROID) 310 #endif // else defined(OS_ANDROID)
314 311
315 CHECK(base::debug::EnableInProcessStackDumping()); 312 CHECK(debug::EnableInProcessStackDumping());
316 #if defined(OS_WIN) 313 #if defined(OS_WIN)
317 // Make sure we run with high resolution timer to minimize differences 314 // Make sure we run with high resolution timer to minimize differences
318 // between production code and test code. 315 // between production code and test code.
319 base::Time::EnableHighResolutionTimer(true); 316 Time::EnableHighResolutionTimer(true);
320 #endif // defined(OS_WIN) 317 #endif // defined(OS_WIN)
321 318
322 // In some cases, we do not want to see standard error dialogs. 319 // In some cases, we do not want to see standard error dialogs.
323 if (!base::debug::BeingDebugged() && 320 if (!debug::BeingDebugged() &&
324 !base::CommandLine::ForCurrentProcess()->HasSwitch( 321 !CommandLine::ForCurrentProcess()->HasSwitch("show-error-dialogs")) {
325 "show-error-dialogs")) {
326 SuppressErrorDialogs(); 322 SuppressErrorDialogs();
327 base::debug::SetSuppressDebugUI(true); 323 debug::SetSuppressDebugUI(true);
328 logging::SetLogAssertHandler(UnitTestAssertHandler); 324 logging::SetLogAssertHandler(UnitTestAssertHandler);
329 } 325 }
330 326
331 base::i18n::InitializeICU(); 327 i18n::InitializeICU();
332 // On the Mac OS X command line, the default locale is *_POSIX. In Chromium, 328 // 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. 329 // 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 330 // 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 331 // 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. 332 // does not affect tests that explicitly overrides the locale for testing.
337 // This can be an issue on all platforms other than Windows. 333 // 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? 334 // TODO(jshin): Should we set the locale via an OS X locale API here?
339 #if !defined(OS_WIN) 335 #if !defined(OS_WIN)
340 #if defined(OS_IOS) 336 #if defined(OS_IOS)
341 base::i18n::SetICUDefaultLocale("en_US"); 337 i18n::SetICUDefaultLocale("en_US");
342 #else 338 #else
343 std::string default_locale(uloc_getDefault()); 339 std::string default_locale(uloc_getDefault());
344 if (EndsWith(default_locale, "POSIX", false)) 340 if (EndsWith(default_locale, "POSIX", false))
345 base::i18n::SetICUDefaultLocale("en_US"); 341 i18n::SetICUDefaultLocale("en_US");
346 #endif 342 #endif
347 #endif 343 #endif
348 344
349 CatchMaybeTests(); 345 CatchMaybeTests();
350 ResetCommandLine(); 346 ResetCommandLine();
351 AddTestLauncherResultPrinter(); 347 AddTestLauncherResultPrinter();
352 348
353 TestTimeouts::Initialize(); 349 TestTimeouts::Initialize();
354 350
355 trace_to_file_.BeginTracingFromCommandLineOptions(); 351 trace_to_file_.BeginTracingFromCommandLineOptions();
356 } 352 }
357 353
358 void TestSuite::Shutdown() { 354 void TestSuite::Shutdown() {
359 } 355 }
356
357 } // namespace base
OLDNEW
« no previous file with comments | « base/test/test_suite.h ('k') | base/test/thread_test_helper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698