xref: /aosp_15_r20/external/libchrome/base/test/test_suite.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #include "base/test/test_suite.h"
6*635a8641SAndroid Build Coastguard Worker 
7*635a8641SAndroid Build Coastguard Worker #include <signal.h>
8*635a8641SAndroid Build Coastguard Worker 
9*635a8641SAndroid Build Coastguard Worker #include <memory>
10*635a8641SAndroid Build Coastguard Worker 
11*635a8641SAndroid Build Coastguard Worker #include "base/at_exit.h"
12*635a8641SAndroid Build Coastguard Worker #include "base/base_paths.h"
13*635a8641SAndroid Build Coastguard Worker #include "base/base_switches.h"
14*635a8641SAndroid Build Coastguard Worker #include "base/bind.h"
15*635a8641SAndroid Build Coastguard Worker #include "base/command_line.h"
16*635a8641SAndroid Build Coastguard Worker #include "base/debug/debugger.h"
17*635a8641SAndroid Build Coastguard Worker #include "base/debug/profiler.h"
18*635a8641SAndroid Build Coastguard Worker #include "base/debug/stack_trace.h"
19*635a8641SAndroid Build Coastguard Worker #include "base/feature_list.h"
20*635a8641SAndroid Build Coastguard Worker #include "base/files/file_path.h"
21*635a8641SAndroid Build Coastguard Worker #include "base/files/file_util.h"
22*635a8641SAndroid Build Coastguard Worker #include "base/i18n/icu_util.h"
23*635a8641SAndroid Build Coastguard Worker #include "base/logging.h"
24*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
25*635a8641SAndroid Build Coastguard Worker #include "base/memory/ptr_util.h"
26*635a8641SAndroid Build Coastguard Worker #include "base/path_service.h"
27*635a8641SAndroid Build Coastguard Worker #include "base/process/launch.h"
28*635a8641SAndroid Build Coastguard Worker #include "base/process/memory.h"
29*635a8641SAndroid Build Coastguard Worker #include "base/test/gtest_xml_unittest_result_printer.h"
30*635a8641SAndroid Build Coastguard Worker #include "base/test/gtest_xml_util.h"
31*635a8641SAndroid Build Coastguard Worker #include "base/test/icu_test_util.h"
32*635a8641SAndroid Build Coastguard Worker #include "base/test/launcher/unit_test_launcher.h"
33*635a8641SAndroid Build Coastguard Worker #include "base/test/multiprocess_test.h"
34*635a8641SAndroid Build Coastguard Worker #include "base/test/test_switches.h"
35*635a8641SAndroid Build Coastguard Worker #include "base/test/test_timeouts.h"
36*635a8641SAndroid Build Coastguard Worker #include "base/time/time.h"
37*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
38*635a8641SAndroid Build Coastguard Worker #include "testing/gmock/include/gmock/gmock.h"
39*635a8641SAndroid Build Coastguard Worker #include "testing/gtest/include/gtest/gtest.h"
40*635a8641SAndroid Build Coastguard Worker #include "testing/multiprocess_func_list.h"
41*635a8641SAndroid Build Coastguard Worker 
42*635a8641SAndroid Build Coastguard Worker #if defined(OS_MACOSX)
43*635a8641SAndroid Build Coastguard Worker #include "base/mac/scoped_nsautorelease_pool.h"
44*635a8641SAndroid Build Coastguard Worker #if defined(OS_IOS)
45*635a8641SAndroid Build Coastguard Worker #include "base/test/test_listener_ios.h"
46*635a8641SAndroid Build Coastguard Worker #endif  // OS_IOS
47*635a8641SAndroid Build Coastguard Worker #endif  // OS_MACOSX
48*635a8641SAndroid Build Coastguard Worker 
49*635a8641SAndroid Build Coastguard Worker #if !defined(OS_WIN)
50*635a8641SAndroid Build Coastguard Worker #include "base/i18n/rtl.h"
51*635a8641SAndroid Build Coastguard Worker #if !defined(OS_IOS)
52*635a8641SAndroid Build Coastguard Worker #include "base/strings/string_util.h"
53*635a8641SAndroid Build Coastguard Worker #include "third_party/icu/source/common/unicode/uloc.h"
54*635a8641SAndroid Build Coastguard Worker #endif
55*635a8641SAndroid Build Coastguard Worker #endif
56*635a8641SAndroid Build Coastguard Worker 
57*635a8641SAndroid Build Coastguard Worker #if defined(OS_ANDROID)
58*635a8641SAndroid Build Coastguard Worker #include "base/test/test_support_android.h"
59*635a8641SAndroid Build Coastguard Worker #endif
60*635a8641SAndroid Build Coastguard Worker 
61*635a8641SAndroid Build Coastguard Worker #if defined(OS_IOS)
62*635a8641SAndroid Build Coastguard Worker #include "base/test/test_support_ios.h"
63*635a8641SAndroid Build Coastguard Worker #endif
64*635a8641SAndroid Build Coastguard Worker 
65*635a8641SAndroid Build Coastguard Worker #if defined(OS_LINUX)
66*635a8641SAndroid Build Coastguard Worker #include "base/test/fontconfig_util_linux.h"
67*635a8641SAndroid Build Coastguard Worker #endif
68*635a8641SAndroid Build Coastguard Worker 
69*635a8641SAndroid Build Coastguard Worker namespace base {
70*635a8641SAndroid Build Coastguard Worker 
71*635a8641SAndroid Build Coastguard Worker namespace {
72*635a8641SAndroid Build Coastguard Worker 
73*635a8641SAndroid Build Coastguard Worker class MaybeTestDisabler : public testing::EmptyTestEventListener {
74*635a8641SAndroid Build Coastguard Worker  public:
OnTestStart(const testing::TestInfo & test_info)75*635a8641SAndroid Build Coastguard Worker   void OnTestStart(const testing::TestInfo& test_info) override {
76*635a8641SAndroid Build Coastguard Worker     ASSERT_FALSE(TestSuite::IsMarkedMaybe(test_info))
77*635a8641SAndroid Build Coastguard Worker         << "Probably the OS #ifdefs don't include all of the necessary "
78*635a8641SAndroid Build Coastguard Worker            "platforms.\nPlease ensure that no tests have the MAYBE_ prefix "
79*635a8641SAndroid Build Coastguard Worker            "after the code is preprocessed.";
80*635a8641SAndroid Build Coastguard Worker   }
81*635a8641SAndroid Build Coastguard Worker };
82*635a8641SAndroid Build Coastguard Worker 
83*635a8641SAndroid Build Coastguard Worker class TestClientInitializer : public testing::EmptyTestEventListener {
84*635a8641SAndroid Build Coastguard Worker  public:
TestClientInitializer()85*635a8641SAndroid Build Coastguard Worker   TestClientInitializer()
86*635a8641SAndroid Build Coastguard Worker       : old_command_line_(CommandLine::NO_PROGRAM) {
87*635a8641SAndroid Build Coastguard Worker   }
88*635a8641SAndroid Build Coastguard Worker 
OnTestStart(const testing::TestInfo & test_info)89*635a8641SAndroid Build Coastguard Worker   void OnTestStart(const testing::TestInfo& test_info) override {
90*635a8641SAndroid Build Coastguard Worker     old_command_line_ = *CommandLine::ForCurrentProcess();
91*635a8641SAndroid Build Coastguard Worker   }
92*635a8641SAndroid Build Coastguard Worker 
OnTestEnd(const testing::TestInfo & test_info)93*635a8641SAndroid Build Coastguard Worker   void OnTestEnd(const testing::TestInfo& test_info) override {
94*635a8641SAndroid Build Coastguard Worker     *CommandLine::ForCurrentProcess() = old_command_line_;
95*635a8641SAndroid Build Coastguard Worker   }
96*635a8641SAndroid Build Coastguard Worker 
97*635a8641SAndroid Build Coastguard Worker  private:
98*635a8641SAndroid Build Coastguard Worker   CommandLine old_command_line_;
99*635a8641SAndroid Build Coastguard Worker 
100*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(TestClientInitializer);
101*635a8641SAndroid Build Coastguard Worker };
102*635a8641SAndroid Build Coastguard Worker 
GetProfileName()103*635a8641SAndroid Build Coastguard Worker std::string GetProfileName() {
104*635a8641SAndroid Build Coastguard Worker   static const char kDefaultProfileName[] = "test-profile-{pid}";
105*635a8641SAndroid Build Coastguard Worker   CR_DEFINE_STATIC_LOCAL(std::string, profile_name, ());
106*635a8641SAndroid Build Coastguard Worker   if (profile_name.empty()) {
107*635a8641SAndroid Build Coastguard Worker     const base::CommandLine& command_line =
108*635a8641SAndroid Build Coastguard Worker         *base::CommandLine::ForCurrentProcess();
109*635a8641SAndroid Build Coastguard Worker     if (command_line.HasSwitch(switches::kProfilingFile))
110*635a8641SAndroid Build Coastguard Worker       profile_name = command_line.GetSwitchValueASCII(switches::kProfilingFile);
111*635a8641SAndroid Build Coastguard Worker     else
112*635a8641SAndroid Build Coastguard Worker       profile_name = std::string(kDefaultProfileName);
113*635a8641SAndroid Build Coastguard Worker   }
114*635a8641SAndroid Build Coastguard Worker   return profile_name;
115*635a8641SAndroid Build Coastguard Worker }
116*635a8641SAndroid Build Coastguard Worker 
InitializeLogging()117*635a8641SAndroid Build Coastguard Worker void InitializeLogging() {
118*635a8641SAndroid Build Coastguard Worker #if defined(OS_ANDROID)
119*635a8641SAndroid Build Coastguard Worker   InitAndroidTestLogging();
120*635a8641SAndroid Build Coastguard Worker #else
121*635a8641SAndroid Build Coastguard Worker   FilePath exe;
122*635a8641SAndroid Build Coastguard Worker   PathService::Get(FILE_EXE, &exe);
123*635a8641SAndroid Build Coastguard Worker   FilePath log_filename = exe.ReplaceExtension(FILE_PATH_LITERAL("log"));
124*635a8641SAndroid Build Coastguard Worker   logging::LoggingSettings settings;
125*635a8641SAndroid Build Coastguard Worker   settings.logging_dest = logging::LOG_TO_ALL;
126*635a8641SAndroid Build Coastguard Worker   settings.log_file = log_filename.value().c_str();
127*635a8641SAndroid Build Coastguard Worker   settings.delete_old = logging::DELETE_OLD_LOG_FILE;
128*635a8641SAndroid Build Coastguard Worker   logging::InitLogging(settings);
129*635a8641SAndroid Build Coastguard Worker   // We want process and thread IDs because we may have multiple processes.
130*635a8641SAndroid Build Coastguard Worker   // Note: temporarily enabled timestamps in an effort to catch bug 6361.
131*635a8641SAndroid Build Coastguard Worker   logging::SetLogItems(true, true, true, true);
132*635a8641SAndroid Build Coastguard Worker #endif  // !defined(OS_ANDROID)
133*635a8641SAndroid Build Coastguard Worker }
134*635a8641SAndroid Build Coastguard Worker 
135*635a8641SAndroid Build Coastguard Worker }  // namespace
136*635a8641SAndroid Build Coastguard Worker 
RunUnitTestsUsingBaseTestSuite(int argc,char ** argv)137*635a8641SAndroid Build Coastguard Worker int RunUnitTestsUsingBaseTestSuite(int argc, char **argv) {
138*635a8641SAndroid Build Coastguard Worker   TestSuite test_suite(argc, argv);
139*635a8641SAndroid Build Coastguard Worker   return LaunchUnitTests(argc, argv,
140*635a8641SAndroid Build Coastguard Worker                          Bind(&TestSuite::Run, Unretained(&test_suite)));
141*635a8641SAndroid Build Coastguard Worker }
142*635a8641SAndroid Build Coastguard Worker 
TestSuite(int argc,char ** argv)143*635a8641SAndroid Build Coastguard Worker TestSuite::TestSuite(int argc, char** argv) : initialized_command_line_(false) {
144*635a8641SAndroid Build Coastguard Worker   PreInitialize();
145*635a8641SAndroid Build Coastguard Worker   InitializeFromCommandLine(argc, argv);
146*635a8641SAndroid Build Coastguard Worker   // Logging must be initialized before any thread has a chance to call logging
147*635a8641SAndroid Build Coastguard Worker   // functions.
148*635a8641SAndroid Build Coastguard Worker   InitializeLogging();
149*635a8641SAndroid Build Coastguard Worker }
150*635a8641SAndroid Build Coastguard Worker 
151*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
TestSuite(int argc,wchar_t ** argv)152*635a8641SAndroid Build Coastguard Worker TestSuite::TestSuite(int argc, wchar_t** argv)
153*635a8641SAndroid Build Coastguard Worker     : initialized_command_line_(false) {
154*635a8641SAndroid Build Coastguard Worker   PreInitialize();
155*635a8641SAndroid Build Coastguard Worker   InitializeFromCommandLine(argc, argv);
156*635a8641SAndroid Build Coastguard Worker   // Logging must be initialized before any thread has a chance to call logging
157*635a8641SAndroid Build Coastguard Worker   // functions.
158*635a8641SAndroid Build Coastguard Worker   InitializeLogging();
159*635a8641SAndroid Build Coastguard Worker }
160*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_WIN)
161*635a8641SAndroid Build Coastguard Worker 
~TestSuite()162*635a8641SAndroid Build Coastguard Worker TestSuite::~TestSuite() {
163*635a8641SAndroid Build Coastguard Worker   if (initialized_command_line_)
164*635a8641SAndroid Build Coastguard Worker     CommandLine::Reset();
165*635a8641SAndroid Build Coastguard Worker }
166*635a8641SAndroid Build Coastguard Worker 
InitializeFromCommandLine(int argc,char ** argv)167*635a8641SAndroid Build Coastguard Worker void TestSuite::InitializeFromCommandLine(int argc, char** argv) {
168*635a8641SAndroid Build Coastguard Worker   initialized_command_line_ = CommandLine::Init(argc, argv);
169*635a8641SAndroid Build Coastguard Worker   testing::InitGoogleTest(&argc, argv);
170*635a8641SAndroid Build Coastguard Worker   testing::InitGoogleMock(&argc, argv);
171*635a8641SAndroid Build Coastguard Worker 
172*635a8641SAndroid Build Coastguard Worker #if defined(OS_IOS)
173*635a8641SAndroid Build Coastguard Worker   InitIOSRunHook(this, argc, argv);
174*635a8641SAndroid Build Coastguard Worker #endif
175*635a8641SAndroid Build Coastguard Worker }
176*635a8641SAndroid Build Coastguard Worker 
177*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
InitializeFromCommandLine(int argc,wchar_t ** argv)178*635a8641SAndroid Build Coastguard Worker void TestSuite::InitializeFromCommandLine(int argc, wchar_t** argv) {
179*635a8641SAndroid Build Coastguard Worker   // Windows CommandLine::Init ignores argv anyway.
180*635a8641SAndroid Build Coastguard Worker   initialized_command_line_ = CommandLine::Init(argc, NULL);
181*635a8641SAndroid Build Coastguard Worker   testing::InitGoogleTest(&argc, argv);
182*635a8641SAndroid Build Coastguard Worker   testing::InitGoogleMock(&argc, argv);
183*635a8641SAndroid Build Coastguard Worker }
184*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_WIN)
185*635a8641SAndroid Build Coastguard Worker 
PreInitialize()186*635a8641SAndroid Build Coastguard Worker void TestSuite::PreInitialize() {
187*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
188*635a8641SAndroid Build Coastguard Worker   testing::GTEST_FLAG(catch_exceptions) = false;
189*635a8641SAndroid Build Coastguard Worker #endif
190*635a8641SAndroid Build Coastguard Worker   EnableTerminationOnHeapCorruption();
191*635a8641SAndroid Build Coastguard Worker #if defined(OS_LINUX) && defined(USE_AURA)
192*635a8641SAndroid Build Coastguard Worker   // When calling native char conversion functions (e.g wrctomb) we need to
193*635a8641SAndroid Build Coastguard Worker   // have the locale set. In the absence of such a call the "C" locale is the
194*635a8641SAndroid Build Coastguard Worker   // default. In the gtk code (below) gtk_init() implicitly sets a locale.
195*635a8641SAndroid Build Coastguard Worker   setlocale(LC_ALL, "");
196*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_LINUX) && defined(USE_AURA)
197*635a8641SAndroid Build Coastguard Worker 
198*635a8641SAndroid Build Coastguard Worker   // On Android, AtExitManager is created in
199*635a8641SAndroid Build Coastguard Worker   // testing/android/native_test_wrapper.cc before main() is called.
200*635a8641SAndroid Build Coastguard Worker #if !defined(OS_ANDROID)
201*635a8641SAndroid Build Coastguard Worker   at_exit_manager_.reset(new AtExitManager);
202*635a8641SAndroid Build Coastguard Worker #endif
203*635a8641SAndroid Build Coastguard Worker 
204*635a8641SAndroid Build Coastguard Worker   // Don't add additional code to this function.  Instead add it to
205*635a8641SAndroid Build Coastguard Worker   // Initialize().  See bug 6436.
206*635a8641SAndroid Build Coastguard Worker }
207*635a8641SAndroid Build Coastguard Worker 
208*635a8641SAndroid Build Coastguard Worker 
209*635a8641SAndroid Build Coastguard Worker // static
IsMarkedMaybe(const testing::TestInfo & test)210*635a8641SAndroid Build Coastguard Worker bool TestSuite::IsMarkedMaybe(const testing::TestInfo& test) {
211*635a8641SAndroid Build Coastguard Worker   return strncmp(test.name(), "MAYBE_", 6) == 0;
212*635a8641SAndroid Build Coastguard Worker }
213*635a8641SAndroid Build Coastguard Worker 
CatchMaybeTests()214*635a8641SAndroid Build Coastguard Worker void TestSuite::CatchMaybeTests() {
215*635a8641SAndroid Build Coastguard Worker   testing::TestEventListeners& listeners =
216*635a8641SAndroid Build Coastguard Worker       testing::UnitTest::GetInstance()->listeners();
217*635a8641SAndroid Build Coastguard Worker   listeners.Append(new MaybeTestDisabler);
218*635a8641SAndroid Build Coastguard Worker }
219*635a8641SAndroid Build Coastguard Worker 
ResetCommandLine()220*635a8641SAndroid Build Coastguard Worker void TestSuite::ResetCommandLine() {
221*635a8641SAndroid Build Coastguard Worker   testing::TestEventListeners& listeners =
222*635a8641SAndroid Build Coastguard Worker       testing::UnitTest::GetInstance()->listeners();
223*635a8641SAndroid Build Coastguard Worker   listeners.Append(new TestClientInitializer);
224*635a8641SAndroid Build Coastguard Worker }
225*635a8641SAndroid Build Coastguard Worker 
AddTestLauncherResultPrinter()226*635a8641SAndroid Build Coastguard Worker void TestSuite::AddTestLauncherResultPrinter() {
227*635a8641SAndroid Build Coastguard Worker   // Only add the custom printer if requested.
228*635a8641SAndroid Build Coastguard Worker   if (!CommandLine::ForCurrentProcess()->HasSwitch(
229*635a8641SAndroid Build Coastguard Worker           switches::kTestLauncherOutput)) {
230*635a8641SAndroid Build Coastguard Worker     return;
231*635a8641SAndroid Build Coastguard Worker   }
232*635a8641SAndroid Build Coastguard Worker 
233*635a8641SAndroid Build Coastguard Worker   FilePath output_path(CommandLine::ForCurrentProcess()->GetSwitchValuePath(
234*635a8641SAndroid Build Coastguard Worker       switches::kTestLauncherOutput));
235*635a8641SAndroid Build Coastguard Worker 
236*635a8641SAndroid Build Coastguard Worker   // Do not add the result printer if output path already exists. It's an
237*635a8641SAndroid Build Coastguard Worker   // indicator there is a process printing to that file, and we're likely
238*635a8641SAndroid Build Coastguard Worker   // its child. Do not clobber the results in that case.
239*635a8641SAndroid Build Coastguard Worker   if (PathExists(output_path)) {
240*635a8641SAndroid Build Coastguard Worker     LOG(WARNING) << "Test launcher output path " << output_path.AsUTF8Unsafe()
241*635a8641SAndroid Build Coastguard Worker                  << " exists. Not adding test launcher result printer.";
242*635a8641SAndroid Build Coastguard Worker     return;
243*635a8641SAndroid Build Coastguard Worker   }
244*635a8641SAndroid Build Coastguard Worker 
245*635a8641SAndroid Build Coastguard Worker   printer_ = new XmlUnitTestResultPrinter;
246*635a8641SAndroid Build Coastguard Worker   CHECK(printer_->Initialize(output_path))
247*635a8641SAndroid Build Coastguard Worker       << "Output path is " << output_path.AsUTF8Unsafe()
248*635a8641SAndroid Build Coastguard Worker       << " and PathExists(output_path) is " << PathExists(output_path);
249*635a8641SAndroid Build Coastguard Worker   testing::TestEventListeners& listeners =
250*635a8641SAndroid Build Coastguard Worker       testing::UnitTest::GetInstance()->listeners();
251*635a8641SAndroid Build Coastguard Worker   listeners.Append(printer_);
252*635a8641SAndroid Build Coastguard Worker }
253*635a8641SAndroid Build Coastguard Worker 
254*635a8641SAndroid Build Coastguard Worker // Don't add additional code to this method.  Instead add it to
255*635a8641SAndroid Build Coastguard Worker // Initialize().  See bug 6436.
Run()256*635a8641SAndroid Build Coastguard Worker int TestSuite::Run() {
257*635a8641SAndroid Build Coastguard Worker #if defined(OS_IOS)
258*635a8641SAndroid Build Coastguard Worker   RunTestsFromIOSApp();
259*635a8641SAndroid Build Coastguard Worker #endif
260*635a8641SAndroid Build Coastguard Worker 
261*635a8641SAndroid Build Coastguard Worker #if defined(OS_MACOSX)
262*635a8641SAndroid Build Coastguard Worker   mac::ScopedNSAutoreleasePool scoped_pool;
263*635a8641SAndroid Build Coastguard Worker #endif
264*635a8641SAndroid Build Coastguard Worker 
265*635a8641SAndroid Build Coastguard Worker   Initialize();
266*635a8641SAndroid Build Coastguard Worker   std::string client_func =
267*635a8641SAndroid Build Coastguard Worker       CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
268*635a8641SAndroid Build Coastguard Worker           switches::kTestChildProcess);
269*635a8641SAndroid Build Coastguard Worker 
270*635a8641SAndroid Build Coastguard Worker   // Check to see if we are being run as a client process.
271*635a8641SAndroid Build Coastguard Worker   if (!client_func.empty())
272*635a8641SAndroid Build Coastguard Worker     return multi_process_function_list::InvokeChildProcessTest(client_func);
273*635a8641SAndroid Build Coastguard Worker #if defined(OS_IOS)
274*635a8641SAndroid Build Coastguard Worker   test_listener_ios::RegisterTestEndListener();
275*635a8641SAndroid Build Coastguard Worker #endif
276*635a8641SAndroid Build Coastguard Worker 
277*635a8641SAndroid Build Coastguard Worker   int result = RUN_ALL_TESTS();
278*635a8641SAndroid Build Coastguard Worker 
279*635a8641SAndroid Build Coastguard Worker #if defined(OS_MACOSX)
280*635a8641SAndroid Build Coastguard Worker   // This MUST happen before Shutdown() since Shutdown() tears down
281*635a8641SAndroid Build Coastguard Worker   // objects (such as NotificationService::current()) that Cocoa
282*635a8641SAndroid Build Coastguard Worker   // objects use to remove themselves as observers.
283*635a8641SAndroid Build Coastguard Worker   scoped_pool.Recycle();
284*635a8641SAndroid Build Coastguard Worker #endif
285*635a8641SAndroid Build Coastguard Worker 
286*635a8641SAndroid Build Coastguard Worker   Shutdown();
287*635a8641SAndroid Build Coastguard Worker 
288*635a8641SAndroid Build Coastguard Worker   return result;
289*635a8641SAndroid Build Coastguard Worker }
290*635a8641SAndroid Build Coastguard Worker 
UnitTestAssertHandler(const char * file,int line,const base::StringPiece summary,const base::StringPiece stack_trace)291*635a8641SAndroid Build Coastguard Worker void TestSuite::UnitTestAssertHandler(const char* file,
292*635a8641SAndroid Build Coastguard Worker                                       int line,
293*635a8641SAndroid Build Coastguard Worker                                       const base::StringPiece summary,
294*635a8641SAndroid Build Coastguard Worker                                       const base::StringPiece stack_trace) {
295*635a8641SAndroid Build Coastguard Worker #if defined(OS_ANDROID)
296*635a8641SAndroid Build Coastguard Worker   // Correlating test stdio with logcat can be difficult, so we emit this
297*635a8641SAndroid Build Coastguard Worker   // helpful little hint about what was running.  Only do this for Android
298*635a8641SAndroid Build Coastguard Worker   // because other platforms don't separate out the relevant logs in the same
299*635a8641SAndroid Build Coastguard Worker   // way.
300*635a8641SAndroid Build Coastguard Worker   const ::testing::TestInfo* const test_info =
301*635a8641SAndroid Build Coastguard Worker       ::testing::UnitTest::GetInstance()->current_test_info();
302*635a8641SAndroid Build Coastguard Worker   if (test_info) {
303*635a8641SAndroid Build Coastguard Worker     LOG(ERROR) << "Currently running: " << test_info->test_case_name() << "."
304*635a8641SAndroid Build Coastguard Worker                << test_info->name();
305*635a8641SAndroid Build Coastguard Worker     fflush(stderr);
306*635a8641SAndroid Build Coastguard Worker   }
307*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_ANDROID)
308*635a8641SAndroid Build Coastguard Worker 
309*635a8641SAndroid Build Coastguard Worker   // XmlUnitTestResultPrinter inherits gtest format, where assert has summary
310*635a8641SAndroid Build Coastguard Worker   // and message. In GTest, summary is just a logged text, and message is a
311*635a8641SAndroid Build Coastguard Worker   // logged text, concatenated with stack trace of assert.
312*635a8641SAndroid Build Coastguard Worker   // Concatenate summary and stack_trace here, to pass it as a message.
313*635a8641SAndroid Build Coastguard Worker   if (printer_) {
314*635a8641SAndroid Build Coastguard Worker     const std::string summary_str = summary.as_string();
315*635a8641SAndroid Build Coastguard Worker     const std::string stack_trace_str = summary_str + stack_trace.as_string();
316*635a8641SAndroid Build Coastguard Worker     printer_->OnAssert(file, line, summary_str, stack_trace_str);
317*635a8641SAndroid Build Coastguard Worker   }
318*635a8641SAndroid Build Coastguard Worker 
319*635a8641SAndroid Build Coastguard Worker   // The logging system actually prints the message before calling the assert
320*635a8641SAndroid Build Coastguard Worker   // handler. Just exit now to avoid printing too many stack traces.
321*635a8641SAndroid Build Coastguard Worker   _exit(1);
322*635a8641SAndroid Build Coastguard Worker }
323*635a8641SAndroid Build Coastguard Worker 
324*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
325*635a8641SAndroid Build Coastguard Worker namespace {
326*635a8641SAndroid Build Coastguard Worker 
327*635a8641SAndroid Build Coastguard Worker // Disable optimizations to prevent function folding or other transformations
328*635a8641SAndroid Build Coastguard Worker // that will make the call stacks on failures more confusing.
329*635a8641SAndroid Build Coastguard Worker #pragma optimize("", off)
330*635a8641SAndroid Build Coastguard Worker // Handlers for invalid parameter, pure call, and abort. They generate a
331*635a8641SAndroid Build Coastguard Worker // breakpoint to ensure that we get a call stack on these failures.
InvalidParameter(const wchar_t * expression,const wchar_t * function,const wchar_t * file,unsigned int line,uintptr_t reserved)332*635a8641SAndroid Build Coastguard Worker void InvalidParameter(const wchar_t* expression,
333*635a8641SAndroid Build Coastguard Worker                       const wchar_t* function,
334*635a8641SAndroid Build Coastguard Worker                       const wchar_t* file,
335*635a8641SAndroid Build Coastguard Worker                       unsigned int line,
336*635a8641SAndroid Build Coastguard Worker                       uintptr_t reserved) {
337*635a8641SAndroid Build Coastguard Worker   // CRT printed message is sufficient.
338*635a8641SAndroid Build Coastguard Worker   __debugbreak();
339*635a8641SAndroid Build Coastguard Worker   _exit(1);
340*635a8641SAndroid Build Coastguard Worker }
341*635a8641SAndroid Build Coastguard Worker 
PureCall()342*635a8641SAndroid Build Coastguard Worker void PureCall() {
343*635a8641SAndroid Build Coastguard Worker   fprintf(stderr, "Pure-virtual function call. Terminating.\n");
344*635a8641SAndroid Build Coastguard Worker   __debugbreak();
345*635a8641SAndroid Build Coastguard Worker   _exit(1);
346*635a8641SAndroid Build Coastguard Worker }
347*635a8641SAndroid Build Coastguard Worker 
AbortHandler(int signal)348*635a8641SAndroid Build Coastguard Worker void AbortHandler(int signal) {
349*635a8641SAndroid Build Coastguard Worker   // Print EOL after the CRT abort message.
350*635a8641SAndroid Build Coastguard Worker   fprintf(stderr, "\n");
351*635a8641SAndroid Build Coastguard Worker   __debugbreak();
352*635a8641SAndroid Build Coastguard Worker }
353*635a8641SAndroid Build Coastguard Worker #pragma optimize("", on)
354*635a8641SAndroid Build Coastguard Worker 
355*635a8641SAndroid Build Coastguard Worker }  // namespace
356*635a8641SAndroid Build Coastguard Worker #endif
357*635a8641SAndroid Build Coastguard Worker 
SuppressErrorDialogs()358*635a8641SAndroid Build Coastguard Worker void TestSuite::SuppressErrorDialogs() {
359*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
360*635a8641SAndroid Build Coastguard Worker   UINT new_flags = SEM_FAILCRITICALERRORS |
361*635a8641SAndroid Build Coastguard Worker                    SEM_NOGPFAULTERRORBOX |
362*635a8641SAndroid Build Coastguard Worker                    SEM_NOOPENFILEERRORBOX;
363*635a8641SAndroid Build Coastguard Worker 
364*635a8641SAndroid Build Coastguard Worker   // Preserve existing error mode, as discussed at
365*635a8641SAndroid Build Coastguard Worker   // http://blogs.msdn.com/oldnewthing/archive/2004/07/27/198410.aspx
366*635a8641SAndroid Build Coastguard Worker   UINT existing_flags = SetErrorMode(new_flags);
367*635a8641SAndroid Build Coastguard Worker   SetErrorMode(existing_flags | new_flags);
368*635a8641SAndroid Build Coastguard Worker 
369*635a8641SAndroid Build Coastguard Worker #if defined(_DEBUG)
370*635a8641SAndroid Build Coastguard Worker   // Suppress the "Debug Assertion Failed" dialog.
371*635a8641SAndroid Build Coastguard Worker   // TODO(hbono): remove this code when gtest has it.
372*635a8641SAndroid Build Coastguard Worker   // http://groups.google.com/d/topic/googletestframework/OjuwNlXy5ac/discussion
373*635a8641SAndroid Build Coastguard Worker   _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
374*635a8641SAndroid Build Coastguard Worker   _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
375*635a8641SAndroid Build Coastguard Worker   _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
376*635a8641SAndroid Build Coastguard Worker   _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
377*635a8641SAndroid Build Coastguard Worker #endif  // defined(_DEBUG)
378*635a8641SAndroid Build Coastguard Worker 
379*635a8641SAndroid Build Coastguard Worker   // See crbug.com/783040 for test code to trigger all of these failures.
380*635a8641SAndroid Build Coastguard Worker   _set_invalid_parameter_handler(InvalidParameter);
381*635a8641SAndroid Build Coastguard Worker   _set_purecall_handler(PureCall);
382*635a8641SAndroid Build Coastguard Worker   signal(SIGABRT, AbortHandler);
383*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_WIN)
384*635a8641SAndroid Build Coastguard Worker }
385*635a8641SAndroid Build Coastguard Worker 
Initialize()386*635a8641SAndroid Build Coastguard Worker void TestSuite::Initialize() {
387*635a8641SAndroid Build Coastguard Worker   const CommandLine* command_line = CommandLine::ForCurrentProcess();
388*635a8641SAndroid Build Coastguard Worker #if !defined(OS_IOS)
389*635a8641SAndroid Build Coastguard Worker   if (command_line->HasSwitch(switches::kWaitForDebugger)) {
390*635a8641SAndroid Build Coastguard Worker     debug::WaitForDebugger(60, true);
391*635a8641SAndroid Build Coastguard Worker   }
392*635a8641SAndroid Build Coastguard Worker #endif
393*635a8641SAndroid Build Coastguard Worker   // Set up a FeatureList instance, so that code using that API will not hit a
394*635a8641SAndroid Build Coastguard Worker   // an error that it's not set. It will be cleared automatically.
395*635a8641SAndroid Build Coastguard Worker   // TestFeatureForBrowserTest1 and TestFeatureForBrowserTest2 used in
396*635a8641SAndroid Build Coastguard Worker   // ContentBrowserTestScopedFeatureListTest to ensure ScopedFeatureList keeps
397*635a8641SAndroid Build Coastguard Worker   // features from command line.
398*635a8641SAndroid Build Coastguard Worker   std::string enabled =
399*635a8641SAndroid Build Coastguard Worker       command_line->GetSwitchValueASCII(switches::kEnableFeatures);
400*635a8641SAndroid Build Coastguard Worker   std::string disabled =
401*635a8641SAndroid Build Coastguard Worker       command_line->GetSwitchValueASCII(switches::kDisableFeatures);
402*635a8641SAndroid Build Coastguard Worker   enabled += ",TestFeatureForBrowserTest1";
403*635a8641SAndroid Build Coastguard Worker   disabled += ",TestFeatureForBrowserTest2";
404*635a8641SAndroid Build Coastguard Worker   scoped_feature_list_.InitFromCommandLine(enabled, disabled);
405*635a8641SAndroid Build Coastguard Worker 
406*635a8641SAndroid Build Coastguard Worker   // The enable-features and disable-features flags were just slurped into a
407*635a8641SAndroid Build Coastguard Worker   // FeatureList, so remove them from the command line. Tests should enable and
408*635a8641SAndroid Build Coastguard Worker   // disable features via the ScopedFeatureList API rather than command-line
409*635a8641SAndroid Build Coastguard Worker   // flags.
410*635a8641SAndroid Build Coastguard Worker   CommandLine new_command_line(command_line->GetProgram());
411*635a8641SAndroid Build Coastguard Worker   CommandLine::SwitchMap switches = command_line->GetSwitches();
412*635a8641SAndroid Build Coastguard Worker 
413*635a8641SAndroid Build Coastguard Worker   switches.erase(switches::kEnableFeatures);
414*635a8641SAndroid Build Coastguard Worker   switches.erase(switches::kDisableFeatures);
415*635a8641SAndroid Build Coastguard Worker 
416*635a8641SAndroid Build Coastguard Worker   for (const auto& iter : switches)
417*635a8641SAndroid Build Coastguard Worker     new_command_line.AppendSwitchNative(iter.first, iter.second);
418*635a8641SAndroid Build Coastguard Worker 
419*635a8641SAndroid Build Coastguard Worker   *CommandLine::ForCurrentProcess() = new_command_line;
420*635a8641SAndroid Build Coastguard Worker 
421*635a8641SAndroid Build Coastguard Worker #if defined(OS_IOS)
422*635a8641SAndroid Build Coastguard Worker   InitIOSTestMessageLoop();
423*635a8641SAndroid Build Coastguard Worker #endif  // OS_IOS
424*635a8641SAndroid Build Coastguard Worker 
425*635a8641SAndroid Build Coastguard Worker #if defined(OS_ANDROID)
426*635a8641SAndroid Build Coastguard Worker   InitAndroidTestMessageLoop();
427*635a8641SAndroid Build Coastguard Worker #endif  // else defined(OS_ANDROID)
428*635a8641SAndroid Build Coastguard Worker 
429*635a8641SAndroid Build Coastguard Worker   CHECK(debug::EnableInProcessStackDumping());
430*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
431*635a8641SAndroid Build Coastguard Worker   RouteStdioToConsole(true);
432*635a8641SAndroid Build Coastguard Worker   // Make sure we run with high resolution timer to minimize differences
433*635a8641SAndroid Build Coastguard Worker   // between production code and test code.
434*635a8641SAndroid Build Coastguard Worker   Time::EnableHighResolutionTimer(true);
435*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_WIN)
436*635a8641SAndroid Build Coastguard Worker 
437*635a8641SAndroid Build Coastguard Worker   // In some cases, we do not want to see standard error dialogs.
438*635a8641SAndroid Build Coastguard Worker   if (!debug::BeingDebugged() &&
439*635a8641SAndroid Build Coastguard Worker       !command_line->HasSwitch("show-error-dialogs")) {
440*635a8641SAndroid Build Coastguard Worker     SuppressErrorDialogs();
441*635a8641SAndroid Build Coastguard Worker     debug::SetSuppressDebugUI(true);
442*635a8641SAndroid Build Coastguard Worker     assert_handler_ = std::make_unique<logging::ScopedLogAssertHandler>(
443*635a8641SAndroid Build Coastguard Worker         base::Bind(&TestSuite::UnitTestAssertHandler, base::Unretained(this)));
444*635a8641SAndroid Build Coastguard Worker   }
445*635a8641SAndroid Build Coastguard Worker 
446*635a8641SAndroid Build Coastguard Worker   base::test::InitializeICUForTesting();
447*635a8641SAndroid Build Coastguard Worker 
448*635a8641SAndroid Build Coastguard Worker   // On the Mac OS X command line, the default locale is *_POSIX. In Chromium,
449*635a8641SAndroid Build Coastguard Worker   // the locale is set via an OS X locale API and is never *_POSIX.
450*635a8641SAndroid Build Coastguard Worker   // Some tests (such as those involving word break iterator) will behave
451*635a8641SAndroid Build Coastguard Worker   // differently and fail if we use *POSIX locale. Setting it to en_US here
452*635a8641SAndroid Build Coastguard Worker   // does not affect tests that explicitly overrides the locale for testing.
453*635a8641SAndroid Build Coastguard Worker   // This can be an issue on all platforms other than Windows.
454*635a8641SAndroid Build Coastguard Worker   // TODO(jshin): Should we set the locale via an OS X locale API here?
455*635a8641SAndroid Build Coastguard Worker #if !defined(OS_WIN)
456*635a8641SAndroid Build Coastguard Worker #if defined(OS_IOS)
457*635a8641SAndroid Build Coastguard Worker   i18n::SetICUDefaultLocale("en_US");
458*635a8641SAndroid Build Coastguard Worker #else
459*635a8641SAndroid Build Coastguard Worker   std::string default_locale(uloc_getDefault());
460*635a8641SAndroid Build Coastguard Worker   if (EndsWith(default_locale, "POSIX", CompareCase::INSENSITIVE_ASCII))
461*635a8641SAndroid Build Coastguard Worker     i18n::SetICUDefaultLocale("en_US");
462*635a8641SAndroid Build Coastguard Worker #endif
463*635a8641SAndroid Build Coastguard Worker #endif
464*635a8641SAndroid Build Coastguard Worker 
465*635a8641SAndroid Build Coastguard Worker #if defined(OS_LINUX)
466*635a8641SAndroid Build Coastguard Worker   // TODO(thomasanderson): Call TearDownFontconfig() in Shutdown().  It would
467*635a8641SAndroid Build Coastguard Worker   // currently crash because of leaked FcFontSet's in font_fallback_linux.cc.
468*635a8641SAndroid Build Coastguard Worker   SetUpFontconfig();
469*635a8641SAndroid Build Coastguard Worker #endif
470*635a8641SAndroid Build Coastguard Worker 
471*635a8641SAndroid Build Coastguard Worker   CatchMaybeTests();
472*635a8641SAndroid Build Coastguard Worker   ResetCommandLine();
473*635a8641SAndroid Build Coastguard Worker   AddTestLauncherResultPrinter();
474*635a8641SAndroid Build Coastguard Worker 
475*635a8641SAndroid Build Coastguard Worker   TestTimeouts::Initialize();
476*635a8641SAndroid Build Coastguard Worker 
477*635a8641SAndroid Build Coastguard Worker   trace_to_file_.BeginTracingFromCommandLineOptions();
478*635a8641SAndroid Build Coastguard Worker 
479*635a8641SAndroid Build Coastguard Worker   base::debug::StartProfiling(GetProfileName());
480*635a8641SAndroid Build Coastguard Worker }
481*635a8641SAndroid Build Coastguard Worker 
Shutdown()482*635a8641SAndroid Build Coastguard Worker void TestSuite::Shutdown() {
483*635a8641SAndroid Build Coastguard Worker   base::debug::StopProfiling();
484*635a8641SAndroid Build Coastguard Worker }
485*635a8641SAndroid Build Coastguard Worker 
486*635a8641SAndroid Build Coastguard Worker }  // namespace base
487