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

Side by Side Diff: base/timer_unittest.cc

Issue 7529003: Make base::MessageLoops have names at construction time (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add name argument to MessageLoop Created 9 years, 4 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/memory/scoped_ptr.h" 5 #include "base/memory/scoped_ptr.h"
6 #include "base/message_loop.h" 6 #include "base/message_loop.h"
7 #include "base/task.h" 7 #include "base/task.h"
8 #include "base/timer.h" 8 #include "base/timer.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 10
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 *did_run_ = true; 68 *did_run_ = true;
69 MessageLoop::current()->Quit(); 69 MessageLoop::current()->Quit();
70 } 70 }
71 } 71 }
72 bool* did_run_; 72 bool* did_run_;
73 int counter_; 73 int counter_;
74 base::RepeatingTimer<RepeatingTimerTester> timer_; 74 base::RepeatingTimer<RepeatingTimerTester> timer_;
75 }; 75 };
76 76
77 void RunTest_OneShotTimer(MessageLoop::Type message_loop_type) { 77 void RunTest_OneShotTimer(MessageLoop::Type message_loop_type) {
78 MessageLoop loop(message_loop_type); 78 MessageLoop loop("TimerTest", message_loop_type);
79 79
80 bool did_run = false; 80 bool did_run = false;
81 OneShotTimerTester f(&did_run); 81 OneShotTimerTester f(&did_run);
82 f.Start(); 82 f.Start();
83 83
84 MessageLoop::current()->Run(); 84 MessageLoop::current()->Run();
85 85
86 EXPECT_TRUE(did_run); 86 EXPECT_TRUE(did_run);
87 } 87 }
88 88
89 void RunTest_OneShotTimer_Cancel(MessageLoop::Type message_loop_type) { 89 void RunTest_OneShotTimer_Cancel(MessageLoop::Type message_loop_type) {
90 MessageLoop loop(message_loop_type); 90 MessageLoop loop("TimerTest", message_loop_type);
91 91
92 bool did_run_a = false; 92 bool did_run_a = false;
93 OneShotTimerTester* a = new OneShotTimerTester(&did_run_a); 93 OneShotTimerTester* a = new OneShotTimerTester(&did_run_a);
94 94
95 // This should run before the timer expires. 95 // This should run before the timer expires.
96 MessageLoop::current()->DeleteSoon(FROM_HERE, a); 96 MessageLoop::current()->DeleteSoon(FROM_HERE, a);
97 97
98 // Now start the timer. 98 // Now start the timer.
99 a->Start(); 99 a->Start();
100 100
101 bool did_run_b = false; 101 bool did_run_b = false;
102 OneShotTimerTester b(&did_run_b); 102 OneShotTimerTester b(&did_run_b);
103 b.Start(); 103 b.Start();
104 104
105 MessageLoop::current()->Run(); 105 MessageLoop::current()->Run();
106 106
107 EXPECT_FALSE(did_run_a); 107 EXPECT_FALSE(did_run_a);
108 EXPECT_TRUE(did_run_b); 108 EXPECT_TRUE(did_run_b);
109 } 109 }
110 110
111 void RunTest_OneShotSelfDeletingTimer(MessageLoop::Type message_loop_type) { 111 void RunTest_OneShotSelfDeletingTimer(MessageLoop::Type message_loop_type) {
112 MessageLoop loop(message_loop_type); 112 MessageLoop loop("TimerTest", message_loop_type);
113 113
114 bool did_run = false; 114 bool did_run = false;
115 OneShotSelfDeletingTimerTester f(&did_run); 115 OneShotSelfDeletingTimerTester f(&did_run);
116 f.Start(); 116 f.Start();
117 117
118 MessageLoop::current()->Run(); 118 MessageLoop::current()->Run();
119 119
120 EXPECT_TRUE(did_run); 120 EXPECT_TRUE(did_run);
121 } 121 }
122 122
123 void RunTest_RepeatingTimer(MessageLoop::Type message_loop_type) { 123 void RunTest_RepeatingTimer(MessageLoop::Type message_loop_type) {
124 MessageLoop loop(message_loop_type); 124 MessageLoop loop("TimerTest", message_loop_type);
125 125
126 bool did_run = false; 126 bool did_run = false;
127 RepeatingTimerTester f(&did_run); 127 RepeatingTimerTester f(&did_run);
128 f.Start(); 128 f.Start();
129 129
130 MessageLoop::current()->Run(); 130 MessageLoop::current()->Run();
131 131
132 EXPECT_TRUE(did_run); 132 EXPECT_TRUE(did_run);
133 } 133 }
134 134
135 void RunTest_RepeatingTimer_Cancel(MessageLoop::Type message_loop_type) { 135 void RunTest_RepeatingTimer_Cancel(MessageLoop::Type message_loop_type) {
136 MessageLoop loop(message_loop_type); 136 MessageLoop loop("TimerTest", message_loop_type);
137 137
138 bool did_run_a = false; 138 bool did_run_a = false;
139 RepeatingTimerTester* a = new RepeatingTimerTester(&did_run_a); 139 RepeatingTimerTester* a = new RepeatingTimerTester(&did_run_a);
140 140
141 // This should run before the timer expires. 141 // This should run before the timer expires.
142 MessageLoop::current()->DeleteSoon(FROM_HERE, a); 142 MessageLoop::current()->DeleteSoon(FROM_HERE, a);
143 143
144 // Now start the timer. 144 // Now start the timer.
145 a->Start(); 145 a->Start();
146 146
(...skipping 18 matching lines...) Expand all
165 void Signal() { 165 void Signal() {
166 ASSERT_FALSE(signaled_); 166 ASSERT_FALSE(signaled_);
167 signaled_ = true; 167 signaled_ = true;
168 } 168 }
169 169
170 private: 170 private:
171 bool signaled_; 171 bool signaled_;
172 }; 172 };
173 173
174 void RunTest_DelayTimer_NoCall(MessageLoop::Type message_loop_type) { 174 void RunTest_DelayTimer_NoCall(MessageLoop::Type message_loop_type) {
175 MessageLoop loop(message_loop_type); 175 MessageLoop loop("TimerTest", message_loop_type);
176 176
177 // If Delay is never called, the timer shouldn't go off. 177 // If Delay is never called, the timer shouldn't go off.
178 DelayTimerTarget target; 178 DelayTimerTarget target;
179 base::DelayTimer<DelayTimerTarget> timer( 179 base::DelayTimer<DelayTimerTarget> timer(
180 TimeDelta::FromMilliseconds(1), &target, &DelayTimerTarget::Signal); 180 TimeDelta::FromMilliseconds(1), &target, &DelayTimerTarget::Signal);
181 181
182 bool did_run = false; 182 bool did_run = false;
183 OneShotTimerTester tester(&did_run); 183 OneShotTimerTester tester(&did_run);
184 tester.Start(); 184 tester.Start();
185 MessageLoop::current()->Run(); 185 MessageLoop::current()->Run();
186 186
187 ASSERT_FALSE(target.signaled()); 187 ASSERT_FALSE(target.signaled());
188 } 188 }
189 189
190 void RunTest_DelayTimer_OneCall(MessageLoop::Type message_loop_type) { 190 void RunTest_DelayTimer_OneCall(MessageLoop::Type message_loop_type) {
191 MessageLoop loop(message_loop_type); 191 MessageLoop loop("TimerTest", message_loop_type);
192 192
193 DelayTimerTarget target; 193 DelayTimerTarget target;
194 base::DelayTimer<DelayTimerTarget> timer( 194 base::DelayTimer<DelayTimerTarget> timer(
195 TimeDelta::FromMilliseconds(1), &target, &DelayTimerTarget::Signal); 195 TimeDelta::FromMilliseconds(1), &target, &DelayTimerTarget::Signal);
196 timer.Reset(); 196 timer.Reset();
197 197
198 bool did_run = false; 198 bool did_run = false;
199 OneShotTimerTester tester(&did_run, 100 /* milliseconds */); 199 OneShotTimerTester tester(&did_run, 100 /* milliseconds */);
200 tester.Start(); 200 tester.Start();
201 MessageLoop::current()->Run(); 201 MessageLoop::current()->Run();
(...skipping 12 matching lines...) Expand all
214 ASSERT_FALSE(target_->signaled()); 214 ASSERT_FALSE(target_->signaled());
215 timer_->Reset(); 215 timer_->Reset();
216 } 216 }
217 217
218 private: 218 private:
219 base::DelayTimer<DelayTimerTarget> *const timer_; 219 base::DelayTimer<DelayTimerTarget> *const timer_;
220 DelayTimerTarget *const target_; 220 DelayTimerTarget *const target_;
221 }; 221 };
222 222
223 void RunTest_DelayTimer_Reset(MessageLoop::Type message_loop_type) { 223 void RunTest_DelayTimer_Reset(MessageLoop::Type message_loop_type) {
224 MessageLoop loop(message_loop_type); 224 MessageLoop loop("TimerTest", message_loop_type);
225 225
226 // If Delay is never called, the timer shouldn't go off. 226 // If Delay is never called, the timer shouldn't go off.
227 DelayTimerTarget target; 227 DelayTimerTarget target;
228 base::DelayTimer<DelayTimerTarget> timer( 228 base::DelayTimer<DelayTimerTarget> timer(
229 TimeDelta::FromMilliseconds(50), &target, &DelayTimerTarget::Signal); 229 TimeDelta::FromMilliseconds(50), &target, &DelayTimerTarget::Signal);
230 timer.Reset(); 230 timer.Reset();
231 231
232 ResetHelper reset_helper(&timer, &target); 232 ResetHelper reset_helper(&timer, &target);
233 233
234 base::OneShotTimer<ResetHelper> timers[20]; 234 base::OneShotTimer<ResetHelper> timers[20];
(...skipping 12 matching lines...) Expand all
247 247
248 class DelayTimerFatalTarget { 248 class DelayTimerFatalTarget {
249 public: 249 public:
250 void Signal() { 250 void Signal() {
251 ASSERT_TRUE(false); 251 ASSERT_TRUE(false);
252 } 252 }
253 }; 253 };
254 254
255 255
256 void RunTest_DelayTimer_Deleted(MessageLoop::Type message_loop_type) { 256 void RunTest_DelayTimer_Deleted(MessageLoop::Type message_loop_type) {
257 MessageLoop loop(message_loop_type); 257 MessageLoop loop("TimerTest", message_loop_type);
258 258
259 DelayTimerFatalTarget target; 259 DelayTimerFatalTarget target;
260 260
261 { 261 {
262 base::DelayTimer<DelayTimerFatalTarget> timer( 262 base::DelayTimer<DelayTimerFatalTarget> timer(
263 TimeDelta::FromMilliseconds(50), &target, 263 TimeDelta::FromMilliseconds(50), &target,
264 &DelayTimerFatalTarget::Signal); 264 &DelayTimerFatalTarget::Signal);
265 timer.Reset(); 265 timer.Reset();
266 } 266 }
267 267
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 // message loop does not cause crashes if there were pending 338 // message loop does not cause crashes if there were pending
339 // timers not yet fired. It may only trigger exceptions 339 // timers not yet fired. It may only trigger exceptions
340 // if debug heap checking (or purify) is enabled. 340 // if debug heap checking (or purify) is enabled.
341 bool did_run = false; 341 bool did_run = false;
342 { 342 {
343 OneShotTimerTester a(&did_run); 343 OneShotTimerTester a(&did_run);
344 OneShotTimerTester b(&did_run); 344 OneShotTimerTester b(&did_run);
345 OneShotTimerTester c(&did_run); 345 OneShotTimerTester c(&did_run);
346 OneShotTimerTester d(&did_run); 346 OneShotTimerTester d(&did_run);
347 { 347 {
348 MessageLoop loop(MessageLoop::TYPE_DEFAULT); 348 MessageLoop loop("TimerTest", MessageLoop::TYPE_DEFAULT);
349 a.Start(); 349 a.Start();
350 b.Start(); 350 b.Start();
351 } // MessageLoop destructs by falling out of scope. 351 } // MessageLoop destructs by falling out of scope.
352 } // OneShotTimers destruct. SHOULD NOT CRASH, of course. 352 } // OneShotTimers destruct. SHOULD NOT CRASH, of course.
353 353
354 EXPECT_FALSE(did_run); 354 EXPECT_FALSE(did_run);
355 } 355 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698