Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(368)

Side by Side Diff: base/test/test_suite.cc

Issue 10582012: For unit tests, track additions to AtExitManager and warn. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Additional fixes. Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 #include "base/test/test_suite.h" 5 #include "base/test/test_suite.h"
6 6
7 #include "base/at_exit.h" 7 #include "base/at_exit.h"
8 #include "base/base_paths.h" 8 #include "base/base_paths.h"
9 #include "base/base_switches.h" 9 #include "base/base_switches.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/debug/debug_on_start_win.h" 11 #include "base/debug/debug_on_start_win.h"
12 #include "base/debug/debugger.h" 12 #include "base/debug/debugger.h"
13 #include "base/file_path.h" 13 #include "base/file_path.h"
14 #include "base/i18n/icu_util.h" 14 #include "base/i18n/icu_util.h"
15 #include "base/logging.h" 15 #include "base/logging.h"
16 #include "base/memory/scoped_ptr.h" 16 #include "base/memory/scoped_ptr.h"
17 #include "base/path_service.h" 17 #include "base/path_service.h"
18 #include "base/process_util.h" 18 #include "base/process_util.h"
19 #include "base/test/multiprocess_test.h" 19 #include "base/test/multiprocess_test.h"
20 #include "base/test/test_switches.h"
20 #include "base/test/test_timeouts.h" 21 #include "base/test/test_timeouts.h"
21 #include "base/time.h" 22 #include "base/time.h"
22 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
23 #include "testing/multiprocess_func_list.h" 24 #include "testing/multiprocess_func_list.h"
24 25
25 #if defined(OS_MACOSX) 26 #if defined(OS_MACOSX)
26 #include "base/mac/scoped_nsautorelease_pool.h" 27 #include "base/mac/scoped_nsautorelease_pool.h"
27 #include "base/test/mock_chrome_application_mac.h" 28 #include "base/test/mock_chrome_application_mac.h"
28 #endif 29 #endif
29 30
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 } 63 }
63 64
64 private: 65 private:
65 CommandLine old_command_line_; 66 CommandLine old_command_line_;
66 67
67 DISALLOW_COPY_AND_ASSIGN(TestClientInitializer); 68 DISALLOW_COPY_AND_ASSIGN(TestClientInitializer);
68 }; 69 };
69 70
70 } // namespace 71 } // namespace
71 72
73 namespace base {
74
75 class TestWatchAtExitManager : public testing::EmptyTestEventListener {
76 public:
77 TestWatchAtExitManager() { }
78 ~TestWatchAtExitManager() { }
79
80 virtual void OnTestStart(const testing::TestInfo& test_info) OVERRIDE {
81 initial_top_manager_ = AtExitManager::current();
82 at_exit_stack_size_ = initial_top_manager_->CallbackStackSize();
83 }
84
85 virtual void OnTestEnd(const testing::TestInfo& test_info) OVERRIDE {
86 AtExitManager* new_top_manager = AtExitManager::current();
87 size_t new_stack_size = new_top_manager->CallbackStackSize();
88
89 if (initial_top_manager_ != new_top_manager) {
90 ADD_FAILURE() << "The current AtExitManager has changed across the "
91 "test " << test_info.test_case_name() << "." << test_info.name() <<
92 " most likely because one was created without being destroyed.";
93 } else if (new_stack_size != at_exit_stack_size_) {
94 // TODO(scottbyer): clean up all the errors that result from this and
95 // turn this into a test failure with
96 // ADD_FAILURE(). http://crbug.com/133403
97 LOG(WARNING) <<
98 "AtExitManager: items were added to the callback list by " <<
99 test_info.test_case_name() << "." << test_info.name() <<
100 ". Global state should be cleaned up before a test exits.";
101 }
102 }
103
104 private:
105 AtExitManager* initial_top_manager_;
106 size_t at_exit_stack_size_;
107
108 DISALLOW_COPY_AND_ASSIGN(TestWatchAtExitManager);
109 };
110
111 } // namespace base
112
72 const char TestSuite::kStrictFailureHandling[] = "strict_failure_handling"; 113 const char TestSuite::kStrictFailureHandling[] = "strict_failure_handling";
73 114
74 TestSuite::TestSuite(int argc, char** argv) : initialized_command_line_(false) { 115 TestSuite::TestSuite(int argc, char** argv) : initialized_command_line_(false) {
75 PreInitialize(argc, argv, true); 116 PreInitialize(argc, argv, true);
76 } 117 }
77 118
78 TestSuite::TestSuite(int argc, char** argv, bool create_at_exit_manager) 119 TestSuite::TestSuite(int argc, char** argv, bool create_at_exit_manager)
79 : initialized_command_line_(false) { 120 : initialized_command_line_(false) {
80 PreInitialize(argc, argv, create_at_exit_manager); 121 PreInitialize(argc, argv, create_at_exit_manager);
81 } 122 }
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 testing::UnitTest::GetInstance()->listeners(); 204 testing::UnitTest::GetInstance()->listeners();
164 listeners.Append(new MaybeTestDisabler); 205 listeners.Append(new MaybeTestDisabler);
165 } 206 }
166 207
167 void TestSuite::ResetCommandLine() { 208 void TestSuite::ResetCommandLine() {
168 testing::TestEventListeners& listeners = 209 testing::TestEventListeners& listeners =
169 testing::UnitTest::GetInstance()->listeners(); 210 testing::UnitTest::GetInstance()->listeners();
170 listeners.Append(new TestClientInitializer); 211 listeners.Append(new TestClientInitializer);
171 } 212 }
172 213
214 void TestSuite::WatchAtExitManager() {
215 testing::TestEventListeners& listeners =
216 testing::UnitTest::GetInstance()->listeners();
217 listeners.Append(new TestWatchAtExitManager);
218 }
219
173 // Don't add additional code to this method. Instead add it to 220 // Don't add additional code to this method. Instead add it to
174 // Initialize(). See bug 6436. 221 // Initialize(). See bug 6436.
175 int TestSuite::Run() { 222 int TestSuite::Run() {
176 #if defined(OS_MACOSX) 223 #if defined(OS_MACOSX)
177 base::mac::ScopedNSAutoreleasePool scoped_pool; 224 base::mac::ScopedNSAutoreleasePool scoped_pool;
178 #endif 225 #endif
179 226
180 Initialize(); 227 Initialize();
181 std::string client_func = 228 std::string client_func =
182 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 229 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 SuppressErrorDialogs(); 327 SuppressErrorDialogs();
281 base::debug::SetSuppressDebugUI(true); 328 base::debug::SetSuppressDebugUI(true);
282 logging::SetLogAssertHandler(UnitTestAssertHandler); 329 logging::SetLogAssertHandler(UnitTestAssertHandler);
283 } 330 }
284 331
285 icu_util::Initialize(); 332 icu_util::Initialize();
286 333
287 CatchMaybeTests(); 334 CatchMaybeTests();
288 ResetCommandLine(); 335 ResetCommandLine();
289 336
337 // Don't watch for AtExit items being added if we're running as a child
338 // process (e.g., browser_tests or interactive_ui_tests).
339 if (!CommandLine::ForCurrentProcess()->HasSwitch(
340 switches::kSingleProcessTestsFlag) &&
341 !CommandLine::ForCurrentProcess()->HasSwitch(
342 switches::kSingleProcessChromeFlag)) {
343 WatchAtExitManager();
344 }
345
290 TestTimeouts::Initialize(); 346 TestTimeouts::Initialize();
291 } 347 }
292 348
293 void TestSuite::Shutdown() { 349 void TestSuite::Shutdown() {
294 } 350 }
OLDNEW
« no previous file with comments | « base/test/test_suite.h ('k') | base/test/test_switches.h » ('j') | base/test/test_switches.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698