15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/test/test_suite.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/at_exit.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/base_paths.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/base_switches.h"
10f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/bind.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/debug/debugger.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/debug/stack_trace.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h"
156e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "base/files/file_util.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/i18n/icu_util.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/path_service.h"
2058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch#include "base/process/memory.h"
21424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "base/test/gtest_xml_util.h"
22f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/test/launcher/unit_test_launcher.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/test/multiprocess_test.h"
24424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "base/test/test_switches.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/test/test_timeouts.h"
26eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h"
27f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/multiprocess_func_list.h"
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_MACOSX)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/mac/scoped_nsautorelease_pool.h"
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_IOS)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/test/test_listener_ios.h"
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/test/mock_chrome_application_mac.h"
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // OS_IOS
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // OS_MACOSX
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_ANDROID)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/test/test_support_android.h"
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_IOS)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/test/test_support_ios.h"
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MaybeTestDisabler : public testing::EmptyTestEventListener {
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnTestStart(const testing::TestInfo& test_info) OVERRIDE {
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ASSERT_FALSE(TestSuite::IsMarkedMaybe(test_info))
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        << "Probably the OS #ifdefs don't include all of the necessary "
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           "platforms.\nPlease ensure that no tests have the MAYBE_ prefix "
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           "after the code is preprocessed.";
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TestClientInitializer : public testing::EmptyTestEventListener {
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TestClientInitializer()
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      : old_command_line_(CommandLine::NO_PROGRAM) {
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnTestStart(const testing::TestInfo& test_info) OVERRIDE {
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    old_command_line_ = *CommandLine::ForCurrentProcess();
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnTestEnd(const testing::TestInfo& test_info) OVERRIDE {
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    *CommandLine::ForCurrentProcess() = old_command_line_;
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CommandLine old_command_line_;
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(TestClientInitializer);
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
82f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)namespace base {
83f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
84f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)int RunUnitTestsUsingBaseTestSuite(int argc, char **argv) {
85f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  TestSuite test_suite(argc, argv);
86f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return base::LaunchUnitTests(
87f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      argc, argv, Bind(&TestSuite::Run, Unretained(&test_suite)));
88f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
89f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
90f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}  // namespace base
91f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TestSuite::TestSuite(int argc, char** argv) : initialized_command_line_(false) {
93f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  PreInitialize(true);
94f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  InitializeFromCommandLine(argc, argv);
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
97f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#if defined(OS_WIN)
98f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)TestSuite::TestSuite(int argc, wchar_t** argv)
99f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    : initialized_command_line_(false) {
100f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  PreInitialize(true);
101f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  InitializeFromCommandLine(argc, argv);
102f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
103f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#endif  // defined(OS_WIN)
104f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TestSuite::TestSuite(int argc, char** argv, bool create_at_exit_manager)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : initialized_command_line_(false) {
107f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  PreInitialize(create_at_exit_manager);
108f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  InitializeFromCommandLine(argc, argv);
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TestSuite::~TestSuite() {
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (initialized_command_line_)
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CommandLine::Reset();
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
116f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void TestSuite::InitializeFromCommandLine(int argc, char** argv) {
117f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  initialized_command_line_ = CommandLine::Init(argc, argv);
118f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  testing::InitGoogleTest(&argc, argv);
119f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  testing::InitGoogleMock(&argc, argv);
120f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
121f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#if defined(OS_IOS)
122f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  InitIOSRunHook(this, argc, argv);
123f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#endif
124f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
125f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
126f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#if defined(OS_WIN)
127f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void TestSuite::InitializeFromCommandLine(int argc, wchar_t** argv) {
128f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Windows CommandLine::Init ignores argv anyway.
129f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  initialized_command_line_ = CommandLine::Init(argc, NULL);
130f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  testing::InitGoogleTest(&argc, argv);
131f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  testing::InitGoogleMock(&argc, argv);
132f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
133f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#endif  // defined(OS_WIN)
134f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
135f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void TestSuite::PreInitialize(bool create_at_exit_manager) {
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  testing::GTEST_FLAG(catch_exceptions) = false;
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::EnableTerminationOnHeapCorruption();
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_LINUX) && defined(USE_AURA)
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // When calling native char conversion functions (e.g wrctomb) we need to
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // have the locale set. In the absence of such a call the "C" locale is the
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // default. In the gtk code (below) gtk_init() implicitly sets a locale.
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  setlocale(LC_ALL, "");
1450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#endif  // defined(OS_LINUX) && defined(USE_AURA)
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // On Android, AtExitManager is created in
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // testing/android/native_test_wrapper.cc before main() is called.
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if !defined(OS_ANDROID)
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (create_at_exit_manager)
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    at_exit_manager_.reset(new base::AtExitManager);
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Don't add additional code to this function.  Instead add it to
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initialize().  See bug 6436.
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool TestSuite::IsMarkedMaybe(const testing::TestInfo& test) {
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return strncmp(test.name(), "MAYBE_", 6) == 0;
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestSuite::CatchMaybeTests() {
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  testing::TestEventListeners& listeners =
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      testing::UnitTest::GetInstance()->listeners();
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  listeners.Append(new MaybeTestDisabler);
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestSuite::ResetCommandLine() {
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  testing::TestEventListeners& listeners =
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      testing::UnitTest::GetInstance()->listeners();
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  listeners.Append(new TestClientInitializer);
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
176424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#if !defined(OS_IOS)
177424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void TestSuite::AddTestLauncherResultPrinter() {
178424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Only add the custom printer if requested.
179424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!CommandLine::ForCurrentProcess()->HasSwitch(
180424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          switches::kTestLauncherOutput)) {
181424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return;
182424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
183424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
184424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  FilePath output_path(CommandLine::ForCurrentProcess()->GetSwitchValuePath(
185424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                           switches::kTestLauncherOutput));
186424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
187424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Do not add the result printer if output path already exists. It's an
188424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // indicator there is a process printing to that file, and we're likely
189424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // its child. Do not clobber the results in that case.
190424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (PathExists(output_path)) {
191424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    LOG(WARNING) << "Test launcher output path " << output_path.AsUTF8Unsafe()
192424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                 << " exists. Not adding test launcher result printer.";
193424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return;
194424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
195424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
196424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  XmlUnitTestResultPrinter* printer = new XmlUnitTestResultPrinter;
197424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CHECK(printer->Initialize(output_path));
198424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  testing::TestEventListeners& listeners =
199424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      testing::UnitTest::GetInstance()->listeners();
200424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  listeners.Append(printer);
201424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
202424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#endif  // !defined(OS_IOS)
203424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Don't add additional code to this method.  Instead add it to
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Initialize().  See bug 6436.
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int TestSuite::Run() {
2072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if defined(OS_IOS)
2082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  RunTestsFromIOSApp();
2092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
2102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_MACOSX)
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::mac::ScopedNSAutoreleasePool scoped_pool;
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Initialize();
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string client_func =
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          switches::kTestChildProcess);
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check to see if we are being run as a client process.
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!client_func.empty())
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return multi_process_function_list::InvokeChildProcessTest(client_func);
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_IOS)
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::test_listener_ios::RegisterTestEndListener();
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int result = RUN_ALL_TESTS();
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_MACOSX)
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This MUST happen before Shutdown() since Shutdown() tears down
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // objects (such as NotificationService::current()) that Cocoa
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // objects use to remove themselves as observers.
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_pool.Recycle();
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Shutdown();
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return result;
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestSuite::UnitTestAssertHandler(const std::string& str) {
2425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(OS_ANDROID)
2435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Correlating test stdio with logcat can be difficult, so we emit this
2445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // helpful little hint about what was running.  Only do this for Android
2455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // because other platforms don't separate out the relevant logs in the same
2465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // way.
2475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const ::testing::TestInfo* const test_info =
2485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ::testing::UnitTest::GetInstance()->current_test_info();
2495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (test_info) {
2505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    LOG(ERROR) << "Currently running: " << test_info->test_case_name() << "."
2515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)               << test_info->name();
2525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    fflush(stderr);
2535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  // defined(OS_ANDROID)
2555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The logging system actually prints the message before calling the assert
2575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // handler. Just exit now to avoid printing too many stack traces.
2585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  _exit(1);
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestSuite::SuppressErrorDialogs() {
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  UINT new_flags = SEM_FAILCRITICALERRORS |
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   SEM_NOGPFAULTERRORBOX |
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   SEM_NOOPENFILEERRORBOX;
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Preserve existing error mode, as discussed at
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // http://blogs.msdn.com/oldnewthing/archive/2004/07/27/198410.aspx
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  UINT existing_flags = SetErrorMode(new_flags);
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetErrorMode(existing_flags | new_flags);
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(_DEBUG) && defined(_HAS_EXCEPTIONS) && (_HAS_EXCEPTIONS == 1)
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Suppress the "Debug Assertion Failed" dialog.
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(hbono): remove this code when gtest has it.
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // http://groups.google.com/d/topic/googletestframework/OjuwNlXy5ac/discussion
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // defined(_DEBUG) && defined(_HAS_EXCEPTIONS) && (_HAS_EXCEPTIONS == 1)
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // defined(OS_WIN)
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestSuite::Initialize() {
28303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#if !defined(OS_IOS)
28403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  if (base::CommandLine::ForCurrentProcess()->HasSwitch(
28503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)          switches::kWaitForDebugger)) {
28603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    base::debug::WaitForDebugger(60, true);
28703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  }
28803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#endif
28903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_MACOSX) && !defined(OS_IOS)
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Some of the app unit tests spin runloops.
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mock_cr_app::RegisterMockCrApp();
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_IOS)
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  InitIOSTestMessageLoop();
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // OS_IOS
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_ANDROID)
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  InitAndroidTest();
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initialize logging.
3032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath exe;
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PathService::Get(base::FILE_EXE, &exe);
3052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath log_filename = exe.ReplaceExtension(FILE_PATH_LITERAL("log"));
306eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  logging::LoggingSettings settings;
307eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  settings.logging_dest = logging::LOG_TO_ALL;
308eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  settings.log_file = log_filename.value().c_str();
309eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  settings.delete_old = logging::DELETE_OLD_LOG_FILE;
310eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  logging::InitLogging(settings);
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We want process and thread IDs because we may have multiple processes.
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Note: temporarily enabled timestamps in an effort to catch bug 6361.
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  logging::SetLogItems(true, true, true, true);
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // else defined(OS_ANDROID)
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(base::debug::EnableInProcessStackDumping());
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Make sure we run with high resolution timer to minimize differences
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // between production code and test code.
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::Time::EnableHighResolutionTimer(true);
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // defined(OS_WIN)
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // In some cases, we do not want to see standard error dialogs.
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!base::debug::BeingDebugged() &&
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      !CommandLine::ForCurrentProcess()->HasSwitch("show-error-dialogs")) {
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SuppressErrorDialogs();
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::debug::SetSuppressDebugUI(true);
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    logging::SetLogAssertHandler(UnitTestAssertHandler);
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::i18n::InitializeICU();
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CatchMaybeTests();
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ResetCommandLine();
335424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#if !defined(OS_IOS)
336424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  AddTestLauncherResultPrinter();
337424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#endif  // !defined(OS_IOS)
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TestTimeouts::Initialize();
3401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
3411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  trace_to_file_.BeginTracingFromCommandLineOptions();
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestSuite::Shutdown() {
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
346