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_util.h"
22 #include "base/test/launcher/unit_test_launcher.h"
23 #include "base/test/multiprocess_test.h"
24 #include "base/test/test_switches.h"
25 #include "base/test/test_timeouts.h"
26 #include "base/time/time.h"
27 #include "testing/gmock/include/gmock/gmock.h"
28 #include "testing/gtest/include/gtest/gtest.h"
29 #include "testing/multiprocess_func_list.h"
31 #if defined(OS_MACOSX)
32 #include "base/mac/scoped_nsautorelease_pool.h"
34 #include "base/test/test_listener_ios.h"
38 #if defined(OS_ANDROID)
39 #include "base/test/test_support_android.h"
43 #include "base/test/test_support_ios.h"
48 class MaybeTestDisabler
: public testing::EmptyTestEventListener
{
50 virtual void OnTestStart(const testing::TestInfo
& test_info
) override
{
51 ASSERT_FALSE(TestSuite::IsMarkedMaybe(test_info
))
52 << "Probably the OS #ifdefs don't include all of the necessary "
53 "platforms.\nPlease ensure that no tests have the MAYBE_ prefix "
54 "after the code is preprocessed.";
58 class TestClientInitializer
: public testing::EmptyTestEventListener
{
60 TestClientInitializer()
61 : old_command_line_(base::CommandLine::NO_PROGRAM
) {
64 virtual void OnTestStart(const testing::TestInfo
& test_info
) override
{
65 old_command_line_
= *base::CommandLine::ForCurrentProcess();
68 virtual void OnTestEnd(const testing::TestInfo
& test_info
) override
{
69 *base::CommandLine::ForCurrentProcess() = old_command_line_
;
73 base::CommandLine old_command_line_
;
75 DISALLOW_COPY_AND_ASSIGN(TestClientInitializer
);
82 int RunUnitTestsUsingBaseTestSuite(int argc
, char **argv
) {
83 TestSuite
test_suite(argc
, argv
);
84 return base::LaunchUnitTests(
85 argc
, argv
, Bind(&TestSuite::Run
, Unretained(&test_suite
)));
90 TestSuite::TestSuite(int argc
, char** argv
) : initialized_command_line_(false) {
92 InitializeFromCommandLine(argc
, argv
);
96 TestSuite::TestSuite(int argc
, wchar_t** argv
)
97 : initialized_command_line_(false) {
99 InitializeFromCommandLine(argc
, argv
);
101 #endif // defined(OS_WIN)
103 TestSuite::TestSuite(int argc
, char** argv
, bool create_at_exit_manager
)
104 : initialized_command_line_(false) {
105 PreInitialize(create_at_exit_manager
);
106 InitializeFromCommandLine(argc
, argv
);
109 TestSuite::~TestSuite() {
110 if (initialized_command_line_
)
111 base::CommandLine::Reset();
114 void TestSuite::InitializeFromCommandLine(int argc
, char** argv
) {
115 initialized_command_line_
= base::CommandLine::Init(argc
, argv
);
116 testing::InitGoogleTest(&argc
, argv
);
117 testing::InitGoogleMock(&argc
, argv
);
120 InitIOSRunHook(this, argc
, argv
);
125 void TestSuite::InitializeFromCommandLine(int argc
, wchar_t** argv
) {
126 // Windows CommandLine::Init ignores argv anyway.
127 initialized_command_line_
= base::CommandLine::Init(argc
, NULL
);
128 testing::InitGoogleTest(&argc
, argv
);
129 testing::InitGoogleMock(&argc
, argv
);
131 #endif // defined(OS_WIN)
133 void TestSuite::PreInitialize(bool create_at_exit_manager
) {
135 testing::GTEST_FLAG(catch_exceptions
) = false;
137 base::EnableTerminationOnHeapCorruption();
138 #if defined(OS_LINUX) && defined(USE_AURA)
139 // When calling native char conversion functions (e.g wrctomb) we need to
140 // have the locale set. In the absence of such a call the "C" locale is the
141 // default. In the gtk code (below) gtk_init() implicitly sets a locale.
142 setlocale(LC_ALL
, "");
143 #endif // defined(OS_LINUX) && defined(USE_AURA)
145 // On Android, AtExitManager is created in
146 // testing/android/native_test_wrapper.cc before main() is called.
147 #if !defined(OS_ANDROID)
148 if (create_at_exit_manager
)
149 at_exit_manager_
.reset(new base::AtExitManager
);
152 // Don't add additional code to this function. Instead add it to
153 // Initialize(). See bug 6436.
158 bool TestSuite::IsMarkedMaybe(const testing::TestInfo
& test
) {
159 return strncmp(test
.name(), "MAYBE_", 6) == 0;
162 void TestSuite::CatchMaybeTests() {
163 testing::TestEventListeners
& listeners
=
164 testing::UnitTest::GetInstance()->listeners();
165 listeners
.Append(new MaybeTestDisabler
);
168 void TestSuite::ResetCommandLine() {
169 testing::TestEventListeners
& listeners
=
170 testing::UnitTest::GetInstance()->listeners();
171 listeners
.Append(new TestClientInitializer
);
175 void TestSuite::AddTestLauncherResultPrinter() {
176 // Only add the custom printer if requested.
177 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
178 switches::kTestLauncherOutput
)) {
182 FilePath
output_path(base::CommandLine::ForCurrentProcess()->
183 GetSwitchValuePath(switches::kTestLauncherOutput
));
185 // Do not add the result printer if output path already exists. It's an
186 // indicator there is a process printing to that file, and we're likely
187 // its child. Do not clobber the results in that case.
188 if (PathExists(output_path
)) {
189 LOG(WARNING
) << "Test launcher output path " << output_path
.AsUTF8Unsafe()
190 << " exists. Not adding test launcher result printer.";
194 XmlUnitTestResultPrinter
* printer
= new XmlUnitTestResultPrinter
;
195 CHECK(printer
->Initialize(output_path
));
196 testing::TestEventListeners
& listeners
=
197 testing::UnitTest::GetInstance()->listeners();
198 listeners
.Append(printer
);
200 #endif // !defined(OS_IOS)
202 // Don't add additional code to this method. Instead add it to
203 // Initialize(). See bug 6436.
204 int TestSuite::Run() {
206 RunTestsFromIOSApp();
209 #if defined(OS_MACOSX)
210 base::mac::ScopedNSAutoreleasePool scoped_pool
;
214 std::string client_func
=
215 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
216 switches::kTestChildProcess
);
218 // Check to see if we are being run as a client process.
219 if (!client_func
.empty())
220 return multi_process_function_list::InvokeChildProcessTest(client_func
);
222 base::test_listener_ios::RegisterTestEndListener();
224 int result
= RUN_ALL_TESTS();
226 #if defined(OS_MACOSX)
227 // This MUST happen before Shutdown() since Shutdown() tears down
228 // objects (such as NotificationService::current()) that Cocoa
229 // objects use to remove themselves as observers.
230 scoped_pool
.Recycle();
239 void TestSuite::UnitTestAssertHandler(const std::string
& str
) {
240 #if defined(OS_ANDROID)
241 // Correlating test stdio with logcat can be difficult, so we emit this
242 // helpful little hint about what was running. Only do this for Android
243 // because other platforms don't separate out the relevant logs in the same
245 const ::testing::TestInfo
* const test_info
=
246 ::testing::UnitTest::GetInstance()->current_test_info();
248 LOG(ERROR
) << "Currently running: " << test_info
->test_case_name() << "."
249 << test_info
->name();
252 #endif // defined(OS_ANDROID)
254 // The logging system actually prints the message before calling the assert
255 // handler. Just exit now to avoid printing too many stack traces.
259 void TestSuite::SuppressErrorDialogs() {
261 UINT new_flags
= SEM_FAILCRITICALERRORS
|
262 SEM_NOGPFAULTERRORBOX
|
263 SEM_NOOPENFILEERRORBOX
;
265 // Preserve existing error mode, as discussed at
266 // http://blogs.msdn.com/oldnewthing/archive/2004/07/27/198410.aspx
267 UINT existing_flags
= SetErrorMode(new_flags
);
268 SetErrorMode(existing_flags
| new_flags
);
270 #if defined(_DEBUG) && defined(_HAS_EXCEPTIONS) && (_HAS_EXCEPTIONS == 1)
271 // Suppress the "Debug Assertion Failed" dialog.
272 // TODO(hbono): remove this code when gtest has it.
273 // http://groups.google.com/d/topic/googletestframework/OjuwNlXy5ac/discussion
274 _CrtSetReportMode(_CRT_ASSERT
, _CRTDBG_MODE_FILE
| _CRTDBG_MODE_DEBUG
);
275 _CrtSetReportFile(_CRT_ASSERT
, _CRTDBG_FILE_STDERR
);
276 #endif // defined(_DEBUG) && defined(_HAS_EXCEPTIONS) && (_HAS_EXCEPTIONS == 1)
277 #endif // defined(OS_WIN)
280 void TestSuite::Initialize() {
282 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
283 switches::kWaitForDebugger
)) {
284 base::debug::WaitForDebugger(60, true);
289 InitIOSTestMessageLoop();
292 #if defined(OS_ANDROID)
295 // Initialize logging.
297 PathService::Get(base::FILE_EXE
, &exe
);
298 base::FilePath log_filename
= exe
.ReplaceExtension(FILE_PATH_LITERAL("log"));
299 logging::LoggingSettings settings
;
300 settings
.logging_dest
= logging::LOG_TO_ALL
;
301 settings
.log_file
= log_filename
.value().c_str();
302 settings
.delete_old
= logging::DELETE_OLD_LOG_FILE
;
303 logging::InitLogging(settings
);
304 // We want process and thread IDs because we may have multiple processes.
305 // Note: temporarily enabled timestamps in an effort to catch bug 6361.
306 logging::SetLogItems(true, true, true, true);
307 #endif // else defined(OS_ANDROID)
309 CHECK(base::debug::EnableInProcessStackDumping());
311 // Make sure we run with high resolution timer to minimize differences
312 // between production code and test code.
313 base::Time::EnableHighResolutionTimer(true);
314 #endif // defined(OS_WIN)
316 // In some cases, we do not want to see standard error dialogs.
317 if (!base::debug::BeingDebugged() &&
318 !base::CommandLine::ForCurrentProcess()->HasSwitch(
319 "show-error-dialogs")) {
320 SuppressErrorDialogs();
321 base::debug::SetSuppressDebugUI(true);
322 logging::SetLogAssertHandler(UnitTestAssertHandler
);
325 base::i18n::InitializeICU();
330 AddTestLauncherResultPrinter();
331 #endif // !defined(OS_IOS)
333 TestTimeouts::Initialize();
335 trace_to_file_
.BeginTracingFromCommandLineOptions();
338 void TestSuite::Shutdown() {