OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #ifndef BASE_TEST_SUITE_H_ | 5 #ifndef BASE_TEST_SUITE_H_ |
6 #define BASE_TEST_SUITE_H_ | 6 #define BASE_TEST_SUITE_H_ |
7 | 7 |
8 // Defines a basic test suite framework for running gtest based tests. You can | 8 // Defines a basic test suite framework for running gtest based tests. You can |
9 // instantiate this class in your main function and call its Run method to run | 9 // instantiate this class in your main function and call its Run method to run |
10 // any gtest based tests that are linked into your executable. | 10 // any gtest based tests that are linked into your executable. |
11 | 11 |
12 #include "base/at_exit.h" | 12 #include "base/at_exit.h" |
13 #include "base/base_paths.h" | 13 #include "base/base_paths.h" |
14 #include "base/debug_on_start.h" | 14 #include "base/debug_on_start.h" |
15 #include "base/i18n/icu_util.h" | 15 #include "base/i18n/icu_util.h" |
16 #include "base/multiprocess_test.h" | 16 #include "base/multiprocess_test.h" |
| 17 #include "base/nss_init.h" |
17 #include "base/process_util.h" | 18 #include "base/process_util.h" |
18 #include "base/scoped_nsautorelease_pool.h" | 19 #include "base/scoped_nsautorelease_pool.h" |
| 20 #include "base/scoped_ptr.h" |
19 #include "base/time.h" | 21 #include "base/time.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "testing/multiprocess_func_list.h" | 23 #include "testing/multiprocess_func_list.h" |
22 | 24 |
23 #if defined(OS_LINUX) | 25 #if defined(OS_LINUX) |
24 #include <gtk/gtk.h> | 26 #include <gtk/gtk.h> |
25 #endif | 27 #endif |
26 | 28 |
27 // Match function used by the GetTestCount method. | 29 // Match function used by the GetTestCount method. |
28 typedef bool (*TestMatch)(const testing::TestInfo&); | 30 typedef bool (*TestMatch)(const testing::TestInfo&); |
29 | 31 |
| 32 // By setting up a shadow AtExitManager, this test event listener ensures that |
| 33 // no state is carried between tests (like singletons, lazy instances, etc). |
| 34 // Of course it won't help if the code under test corrupts memory. |
| 35 class TestIsolationEnforcer : public testing::EmptyTestEventListener { |
| 36 public: |
| 37 virtual void OnTestStart(const testing::TestInfo& test_info) { |
| 38 ASSERT_FALSE(exit_manager_.get()); |
| 39 exit_manager_.reset(new base::ShadowingAtExitManager()); |
| 40 } |
| 41 |
| 42 virtual void OnTestEnd(const testing::TestInfo& test_info) { |
| 43 ASSERT_TRUE(exit_manager_.get()); |
| 44 exit_manager_.reset(); |
| 45 } |
| 46 |
| 47 private: |
| 48 scoped_ptr<base::ShadowingAtExitManager> exit_manager_; |
| 49 }; |
| 50 |
30 class TestSuite { | 51 class TestSuite { |
31 public: | 52 public: |
32 TestSuite(int argc, char** argv) { | 53 TestSuite(int argc, char** argv) { |
33 base::EnableTerminationOnHeapCorruption(); | 54 base::EnableTerminationOnHeapCorruption(); |
34 CommandLine::Init(argc, argv); | 55 CommandLine::Init(argc, argv); |
35 testing::InitGoogleTest(&argc, argv); | 56 testing::InitGoogleTest(&argc, argv); |
36 #if defined(OS_LINUX) | 57 #if defined(OS_LINUX) |
37 g_thread_init(NULL); | 58 g_thread_init(NULL); |
38 gtk_init_check(&argc, &argv); | 59 gtk_init_check(&argc, &argv); |
39 #endif | 60 #endif |
(...skipping 30 matching lines...) Expand all Loading... |
70 for (int j = 0; j < test_case.total_test_count(); ++j) { | 91 for (int j = 0; j < test_case.total_test_count(); ++j) { |
71 if (test_match(*test_case.GetTestInfo(j))) { | 92 if (test_match(*test_case.GetTestInfo(j))) { |
72 count++; | 93 count++; |
73 } | 94 } |
74 } | 95 } |
75 } | 96 } |
76 | 97 |
77 return count; | 98 return count; |
78 } | 99 } |
79 | 100 |
| 101 // TODO(phajdan.jr): Enforce isolation for all tests once it's stable. |
| 102 void EnforceTestIsolation() { |
| 103 testing::TestEventListeners& listeners = |
| 104 testing::UnitTest::GetInstance()->listeners(); |
| 105 listeners.Append(new TestIsolationEnforcer); |
| 106 } |
| 107 |
80 // Don't add additional code to this method. Instead add it to | 108 // Don't add additional code to this method. Instead add it to |
81 // Initialize(). See bug 6436. | 109 // Initialize(). See bug 6436. |
82 int Run() { | 110 int Run() { |
83 base::ScopedNSAutoreleasePool scoped_pool; | 111 base::ScopedNSAutoreleasePool scoped_pool; |
84 | 112 |
85 Initialize(); | 113 Initialize(); |
86 std::wstring client_func = | 114 std::wstring client_func = |
87 CommandLine::ForCurrentProcess()->GetSwitchValue(kRunClientProcess); | 115 CommandLine::ForCurrentProcess()->GetSwitchValue(kRunClientProcess); |
88 // Check to see if we are being run as a client process. | 116 // Check to see if we are being run as a client process. |
89 if (!client_func.empty()) { | 117 if (!client_func.empty()) { |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 SuppressErrorDialogs(); | 193 SuppressErrorDialogs(); |
166 #if !defined(PURIFY) | 194 #if !defined(PURIFY) |
167 // When the code in this file moved around, bug 6436 resurfaced. | 195 // When the code in this file moved around, bug 6436 resurfaced. |
168 // As a hack workaround, just #ifdef out this code for Purify builds. | 196 // As a hack workaround, just #ifdef out this code for Purify builds. |
169 logging::SetLogAssertHandler(UnitTestAssertHandler); | 197 logging::SetLogAssertHandler(UnitTestAssertHandler); |
170 #endif // !defined(PURIFY) | 198 #endif // !defined(PURIFY) |
171 } | 199 } |
172 #endif // defined(OS_WIN) | 200 #endif // defined(OS_WIN) |
173 | 201 |
174 icu_util::Initialize(); | 202 icu_util::Initialize(); |
| 203 |
| 204 #if defined(OS_LINUX) |
| 205 // Trying to repeatedly initialize and cleanup NSS and NSPR may result in |
| 206 // a deadlock. Such repeated initialization will happen when using test |
| 207 // isolation. Prevent problems by initializing NSS here, so that the cleanup |
| 208 // will be done only on process exit. |
| 209 base::EnsureNSSInit(); |
| 210 #endif // defined(OS_LINUX) |
175 } | 211 } |
176 | 212 |
177 virtual void Shutdown() { | 213 virtual void Shutdown() { |
178 } | 214 } |
179 | 215 |
180 // Make sure that we setup an AtExitManager so Singleton objects will be | 216 // Make sure that we setup an AtExitManager so Singleton objects will be |
181 // destroyed. | 217 // destroyed. |
182 base::AtExitManager at_exit_manager_; | 218 base::AtExitManager at_exit_manager_; |
183 }; | 219 }; |
184 | 220 |
185 #endif // BASE_TEST_SUITE_H_ | 221 #endif // BASE_TEST_SUITE_H_ |
OLD | NEW |