| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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. | |
| 4 | |
| 5 #include "base/test/test_suite.h" | |
| 6 | |
| 7 #include "base/at_exit.h" | |
| 8 #include "base/base_paths.h" | |
| 9 #include "base/base_switches.h" | |
| 10 #include "base/command_line.h" | |
| 11 #include "base/debug_on_start.h" | |
| 12 #include "base/debug_util.h" | |
| 13 #include "base/file_path.h" | |
| 14 #include "base/i18n/icu_util.h" | |
| 15 #include "base/logging.h" | |
| 16 #include "base/nss_util.h" | |
| 17 #include "base/path_service.h" | |
| 18 #include "base/process_util.h" | |
| 19 #include "base/scoped_nsautorelease_pool.h" | |
| 20 #include "base/scoped_ptr.h" | |
| 21 #include "base/test/multiprocess_test.h" | |
| 22 #include "base/time.h" | |
| 23 #include "testing/gtest/include/gtest/gtest.h" | |
| 24 #include "testing/multiprocess_func_list.h" | |
| 25 | |
| 26 #if defined(OS_POSIX) && !defined(OS_MACOSX) | |
| 27 #include <gtk/gtk.h> | |
| 28 #endif | |
| 29 | |
| 30 namespace base { | |
| 31 | |
| 32 namespace { | |
| 33 | |
| 34 // By setting up a shadow AtExitManager, this test event listener ensures that | |
| 35 // no state is carried between tests (like singletons, lazy instances, etc). | |
| 36 // Of course it won't help if the code under test corrupts memory. | |
| 37 class TestIsolationEnforcer : public testing::EmptyTestEventListener { | |
| 38 public: | |
| 39 virtual void OnTestStart(const testing::TestInfo& test_info) { | |
| 40 ASSERT_FALSE(exit_manager_.get()); | |
| 41 exit_manager_.reset(new ShadowingAtExitManager()); | |
| 42 } | |
| 43 | |
| 44 virtual void OnTestEnd(const testing::TestInfo& test_info) { | |
| 45 ASSERT_TRUE(exit_manager_.get()); | |
| 46 exit_manager_.reset(); | |
| 47 } | |
| 48 | |
| 49 private: | |
| 50 scoped_ptr<ShadowingAtExitManager> exit_manager_; | |
| 51 }; | |
| 52 | |
| 53 class MaybeTestDisabler : public testing::EmptyTestEventListener { | |
| 54 public: | |
| 55 virtual void OnTestStart(const testing::TestInfo& test_info) { | |
| 56 ASSERT_FALSE(TestSuite::IsMarkedMaybe(test_info)) | |
| 57 << "Probably the OS #ifdefs don't include all of the necessary " | |
| 58 "platforms. Please ensure that no tests have the MAYBE_ prefix " | |
| 59 "after the code is preprocessed."; | |
| 60 } | |
| 61 }; | |
| 62 | |
| 63 } // namespace | |
| 64 | |
| 65 // static | |
| 66 const char TestSuite::kStrictFailureHandling[] = "strict_failure_handling"; | |
| 67 | |
| 68 TestSuite::TestSuite(int argc, char** argv) { | |
| 69 EnableTerminationOnHeapCorruption(); | |
| 70 CommandLine::Init(argc, argv); | |
| 71 testing::InitGoogleTest(&argc, argv); | |
| 72 #if defined(OS_POSIX) && !defined(OS_MACOSX) | |
| 73 g_thread_init(NULL); | |
| 74 gtk_init_check(&argc, &argv); | |
| 75 #endif // defined(OS_LINUX) | |
| 76 // Don't add additional code to this constructor. Instead add it to | |
| 77 // Initialize(). See bug 6436. | |
| 78 } | |
| 79 | |
| 80 TestSuite::~TestSuite() { | |
| 81 CommandLine::Reset(); | |
| 82 } | |
| 83 | |
| 84 // static | |
| 85 bool TestSuite::IsMarkedFlaky(const testing::TestInfo& test) { | |
| 86 return strncmp(test.name(), "FLAKY_", 6) == 0; | |
| 87 } | |
| 88 | |
| 89 // static | |
| 90 bool TestSuite::IsMarkedFailing(const testing::TestInfo& test) { | |
| 91 return strncmp(test.name(), "FAILS_", 6) == 0; | |
| 92 } | |
| 93 | |
| 94 // static | |
| 95 bool TestSuite::IsMarkedMaybe(const testing::TestInfo& test) { | |
| 96 return strncmp(test.name(), "MAYBE_", 6) == 0; | |
| 97 } | |
| 98 | |
| 99 // static | |
| 100 bool TestSuite::ShouldIgnoreFailure(const testing::TestInfo& test) { | |
| 101 if (CommandLine::ForCurrentProcess()->HasSwitch(kStrictFailureHandling)) | |
| 102 return false; | |
| 103 return IsMarkedFlaky(test) || IsMarkedFailing(test); | |
| 104 } | |
| 105 | |
| 106 // static | |
| 107 bool TestSuite::NonIgnoredFailures(const testing::TestInfo& test) { | |
| 108 return test.should_run() && test.result()->Failed() && | |
| 109 !ShouldIgnoreFailure(test); | |
| 110 } | |
| 111 | |
| 112 int TestSuite::GetTestCount(TestMatch test_match) { | |
| 113 testing::UnitTest* instance = testing::UnitTest::GetInstance(); | |
| 114 int count = 0; | |
| 115 | |
| 116 for (int i = 0; i < instance->total_test_case_count(); ++i) { | |
| 117 const testing::TestCase& test_case = *instance->GetTestCase(i); | |
| 118 for (int j = 0; j < test_case.total_test_count(); ++j) { | |
| 119 if (test_match(*test_case.GetTestInfo(j))) { | |
| 120 count++; | |
| 121 } | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 return count; | |
| 126 } | |
| 127 | |
| 128 void TestSuite::EnforceTestIsolation() { | |
| 129 testing::TestEventListeners& listeners = | |
| 130 testing::UnitTest::GetInstance()->listeners(); | |
| 131 listeners.Append(new TestIsolationEnforcer); | |
| 132 } | |
| 133 | |
| 134 void TestSuite::CatchMaybeTests() { | |
| 135 testing::TestEventListeners& listeners = | |
| 136 testing::UnitTest::GetInstance()->listeners(); | |
| 137 listeners.Append(new MaybeTestDisabler); | |
| 138 } | |
| 139 | |
| 140 // Don't add additional code to this method. Instead add it to | |
| 141 // Initialize(). See bug 6436. | |
| 142 int TestSuite::Run() { | |
| 143 ScopedNSAutoreleasePool scoped_pool; | |
| 144 | |
| 145 Initialize(); | |
| 146 std::string client_func = | |
| 147 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 148 switches::kTestChildProcess); | |
| 149 // Check to see if we are being run as a client process. | |
| 150 if (!client_func.empty()) | |
| 151 return multi_process_function_list::InvokeChildProcessTest(client_func); | |
| 152 int result = RUN_ALL_TESTS(); | |
| 153 | |
| 154 // If there are failed tests, see if we should ignore the failures. | |
| 155 if (result != 0 && GetTestCount(&TestSuite::NonIgnoredFailures) == 0) | |
| 156 result = 0; | |
| 157 | |
| 158 // Display the number of flaky tests. | |
| 159 int flaky_count = GetTestCount(&TestSuite::IsMarkedFlaky); | |
| 160 if (flaky_count) { | |
| 161 printf(" YOU HAVE %d FLAKY %s\n\n", flaky_count, | |
| 162 flaky_count == 1 ? "TEST" : "TESTS"); | |
| 163 } | |
| 164 | |
| 165 // Display the number of tests with ignored failures (FAILS). | |
| 166 int failing_count = GetTestCount(&TestSuite::IsMarkedFailing); | |
| 167 if (failing_count) { | |
| 168 printf(" YOU HAVE %d %s with ignored failures (FAILS prefix)\n\n", | |
| 169 failing_count, failing_count == 1 ? "test" : "tests"); | |
| 170 } | |
| 171 | |
| 172 // This MUST happen before Shutdown() since Shutdown() tears down | |
| 173 // objects (such as NotificationService::current()) that Cocoa | |
| 174 // objects use to remove themselves as observers. | |
| 175 scoped_pool.Recycle(); | |
| 176 | |
| 177 Shutdown(); | |
| 178 | |
| 179 return result; | |
| 180 } | |
| 181 | |
| 182 // static | |
| 183 void TestSuite::UnitTestAssertHandler(const std::string& str) { | |
| 184 RAW_LOG(FATAL, str.c_str()); | |
| 185 } | |
| 186 | |
| 187 void TestSuite::SuppressErrorDialogs() { | |
| 188 #if defined(OS_WIN) | |
| 189 UINT new_flags = SEM_FAILCRITICALERRORS | | |
| 190 SEM_NOGPFAULTERRORBOX | | |
| 191 SEM_NOOPENFILEERRORBOX; | |
| 192 | |
| 193 // Preserve existing error mode, as discussed at | |
| 194 // http://blogs.msdn.com/oldnewthing/archive/2004/07/27/198410.aspx | |
| 195 UINT existing_flags = SetErrorMode(new_flags); | |
| 196 SetErrorMode(existing_flags | new_flags); | |
| 197 #endif // defined(OS_WIN) | |
| 198 } | |
| 199 | |
| 200 void TestSuite::Initialize() { | |
| 201 // Initialize logging. | |
| 202 FilePath exe; | |
| 203 PathService::Get(FILE_EXE, &exe); | |
| 204 FilePath log_filename = exe.ReplaceExtension(FILE_PATH_LITERAL("log")); | |
| 205 logging::InitLogging(log_filename.value().c_str(), | |
| 206 logging::LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG, | |
| 207 logging::LOCK_LOG_FILE, | |
| 208 logging::DELETE_OLD_LOG_FILE); | |
| 209 // We want process and thread IDs because we may have multiple processes. | |
| 210 // Note: temporarily enabled timestamps in an effort to catch bug 6361. | |
| 211 logging::SetLogItems(true, true, true, true); | |
| 212 | |
| 213 CHECK(EnableInProcessStackDumping()); | |
| 214 #if defined(OS_WIN) | |
| 215 // Make sure we run with high resolution timer to minimize differences | |
| 216 // between production code and test code. | |
| 217 Time::EnableHighResolutionTimer(true); | |
| 218 #endif // defined(OS_WIN) | |
| 219 | |
| 220 // In some cases, we do not want to see standard error dialogs. | |
| 221 if (!DebugUtil::BeingDebugged() && | |
| 222 !CommandLine::ForCurrentProcess()->HasSwitch("show-error-dialogs")) { | |
| 223 SuppressErrorDialogs(); | |
| 224 DebugUtil::SuppressDialogs(); | |
| 225 logging::SetLogAssertHandler(UnitTestAssertHandler); | |
| 226 } | |
| 227 | |
| 228 icu_util::Initialize(); | |
| 229 | |
| 230 #if defined(USE_NSS) | |
| 231 // Trying to repeatedly initialize and cleanup NSS and NSPR may result in | |
| 232 // a deadlock. Such repeated initialization will happen when using test | |
| 233 // isolation. Prevent problems by initializing NSS here, so that the cleanup | |
| 234 // will be done only on process exit. | |
| 235 EnsureNSSInit(); | |
| 236 #endif // defined(USE_NSS) | |
| 237 | |
| 238 CatchMaybeTests(); | |
| 239 } | |
| 240 | |
| 241 void TestSuite::Shutdown() { | |
| 242 } | |
| 243 | |
| 244 } // namespace base | |
| OLD | NEW |