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

Side by Side Diff: base/timer_unittest.cc

Issue 16092013: Use base::MessageLoop in more files. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase again, sigh Created 7 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
« no previous file with comments | « base/threading/thread_unittest.cc ('k') | cc/test/fake_output_surface.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/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/timer.h" 7 #include "base/timer.h"
8 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
9 9
10 using base::TimeDelta; 10 using base::TimeDelta;
11 11
12 namespace { 12 namespace {
13 13
14 // The message loops on which each timer should be tested. 14 // The message loops on which each timer should be tested.
15 const MessageLoop::Type testing_message_loops[] = { 15 const base::MessageLoop::Type testing_message_loops[] = {
16 MessageLoop::TYPE_DEFAULT, 16 base::MessageLoop::TYPE_DEFAULT,
17 MessageLoop::TYPE_IO, 17 base::MessageLoop::TYPE_IO,
18 #if !defined(OS_IOS) // iOS does not allow direct running of the UI loop. 18 #if !defined(OS_IOS) // iOS does not allow direct running of the UI loop.
19 MessageLoop::TYPE_UI, 19 base::MessageLoop::TYPE_UI,
20 #endif 20 #endif
21 }; 21 };
22 22
23 const int kNumTestingMessageLoops = arraysize(testing_message_loops); 23 const int kNumTestingMessageLoops = arraysize(testing_message_loops);
24 24
25 class OneShotTimerTester { 25 class OneShotTimerTester {
26 public: 26 public:
27 explicit OneShotTimerTester(bool* did_run, unsigned milliseconds = 10) 27 explicit OneShotTimerTester(bool* did_run, unsigned milliseconds = 10)
28 : did_run_(did_run), 28 : did_run_(did_run),
29 delay_ms_(milliseconds) { 29 delay_ms_(milliseconds) {
30 } 30 }
31 void Start() { 31 void Start() {
32 timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(delay_ms_), this, 32 timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(delay_ms_), this,
33 &OneShotTimerTester::Run); 33 &OneShotTimerTester::Run);
34 } 34 }
35 private: 35 private:
36 void Run() { 36 void Run() {
37 *did_run_ = true; 37 *did_run_ = true;
38 MessageLoop::current()->QuitWhenIdle(); 38 base::MessageLoop::current()->QuitWhenIdle();
39 } 39 }
40 bool* did_run_; 40 bool* did_run_;
41 base::OneShotTimer<OneShotTimerTester> timer_; 41 base::OneShotTimer<OneShotTimerTester> timer_;
42 const unsigned delay_ms_; 42 const unsigned delay_ms_;
43 }; 43 };
44 44
45 class OneShotSelfDeletingTimerTester { 45 class OneShotSelfDeletingTimerTester {
46 public: 46 public:
47 explicit OneShotSelfDeletingTimerTester(bool* did_run) : 47 explicit OneShotSelfDeletingTimerTester(bool* did_run) :
48 did_run_(did_run), 48 did_run_(did_run),
49 timer_(new base::OneShotTimer<OneShotSelfDeletingTimerTester>()) { 49 timer_(new base::OneShotTimer<OneShotSelfDeletingTimerTester>()) {
50 } 50 }
51 void Start() { 51 void Start() {
52 timer_->Start(FROM_HERE, TimeDelta::FromMilliseconds(10), this, 52 timer_->Start(FROM_HERE, TimeDelta::FromMilliseconds(10), this,
53 &OneShotSelfDeletingTimerTester::Run); 53 &OneShotSelfDeletingTimerTester::Run);
54 } 54 }
55 private: 55 private:
56 void Run() { 56 void Run() {
57 *did_run_ = true; 57 *did_run_ = true;
58 timer_.reset(); 58 timer_.reset();
59 MessageLoop::current()->QuitWhenIdle(); 59 base::MessageLoop::current()->QuitWhenIdle();
60 } 60 }
61 bool* did_run_; 61 bool* did_run_;
62 scoped_ptr<base::OneShotTimer<OneShotSelfDeletingTimerTester> > timer_; 62 scoped_ptr<base::OneShotTimer<OneShotSelfDeletingTimerTester> > timer_;
63 }; 63 };
64 64
65 class RepeatingTimerTester { 65 class RepeatingTimerTester {
66 public: 66 public:
67 explicit RepeatingTimerTester(bool* did_run) 67 explicit RepeatingTimerTester(bool* did_run)
68 : did_run_(did_run), counter_(10) { 68 : did_run_(did_run), counter_(10) {
69 } 69 }
70 70
71 void Start() { 71 void Start() {
72 timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(10), this, 72 timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(10), this,
73 &RepeatingTimerTester::Run); 73 &RepeatingTimerTester::Run);
74 } 74 }
75 private: 75 private:
76 void Run() { 76 void Run() {
77 if (--counter_ == 0) { 77 if (--counter_ == 0) {
78 *did_run_ = true; 78 *did_run_ = true;
79 MessageLoop::current()->QuitWhenIdle(); 79 base::MessageLoop::current()->QuitWhenIdle();
80 } 80 }
81 } 81 }
82 bool* did_run_; 82 bool* did_run_;
83 int counter_; 83 int counter_;
84 base::RepeatingTimer<RepeatingTimerTester> timer_; 84 base::RepeatingTimer<RepeatingTimerTester> timer_;
85 }; 85 };
86 86
87 void RunTest_OneShotTimer(MessageLoop::Type message_loop_type) { 87 void RunTest_OneShotTimer(base::MessageLoop::Type message_loop_type) {
88 MessageLoop loop(message_loop_type); 88 base::MessageLoop loop(message_loop_type);
89 89
90 bool did_run = false; 90 bool did_run = false;
91 OneShotTimerTester f(&did_run); 91 OneShotTimerTester f(&did_run);
92 f.Start(); 92 f.Start();
93 93
94 MessageLoop::current()->Run(); 94 base::MessageLoop::current()->Run();
95 95
96 EXPECT_TRUE(did_run); 96 EXPECT_TRUE(did_run);
97 } 97 }
98 98
99 void RunTest_OneShotTimer_Cancel(MessageLoop::Type message_loop_type) { 99 void RunTest_OneShotTimer_Cancel(base::MessageLoop::Type message_loop_type) {
100 MessageLoop loop(message_loop_type); 100 base::MessageLoop loop(message_loop_type);
101 101
102 bool did_run_a = false; 102 bool did_run_a = false;
103 OneShotTimerTester* a = new OneShotTimerTester(&did_run_a); 103 OneShotTimerTester* a = new OneShotTimerTester(&did_run_a);
104 104
105 // This should run before the timer expires. 105 // This should run before the timer expires.
106 MessageLoop::current()->DeleteSoon(FROM_HERE, a); 106 base::MessageLoop::current()->DeleteSoon(FROM_HERE, a);
107 107
108 // Now start the timer. 108 // Now start the timer.
109 a->Start(); 109 a->Start();
110 110
111 bool did_run_b = false; 111 bool did_run_b = false;
112 OneShotTimerTester b(&did_run_b); 112 OneShotTimerTester b(&did_run_b);
113 b.Start(); 113 b.Start();
114 114
115 MessageLoop::current()->Run(); 115 base::MessageLoop::current()->Run();
116 116
117 EXPECT_FALSE(did_run_a); 117 EXPECT_FALSE(did_run_a);
118 EXPECT_TRUE(did_run_b); 118 EXPECT_TRUE(did_run_b);
119 } 119 }
120 120
121 void RunTest_OneShotSelfDeletingTimer(MessageLoop::Type message_loop_type) { 121 void RunTest_OneShotSelfDeletingTimer(
122 MessageLoop loop(message_loop_type); 122 base::MessageLoop::Type message_loop_type) {
123 base::MessageLoop loop(message_loop_type);
123 124
124 bool did_run = false; 125 bool did_run = false;
125 OneShotSelfDeletingTimerTester f(&did_run); 126 OneShotSelfDeletingTimerTester f(&did_run);
126 f.Start(); 127 f.Start();
127 128
128 MessageLoop::current()->Run(); 129 base::MessageLoop::current()->Run();
129 130
130 EXPECT_TRUE(did_run); 131 EXPECT_TRUE(did_run);
131 } 132 }
132 133
133 void RunTest_RepeatingTimer(MessageLoop::Type message_loop_type) { 134 void RunTest_RepeatingTimer(base::MessageLoop::Type message_loop_type) {
134 MessageLoop loop(message_loop_type); 135 base::MessageLoop loop(message_loop_type);
135 136
136 bool did_run = false; 137 bool did_run = false;
137 RepeatingTimerTester f(&did_run); 138 RepeatingTimerTester f(&did_run);
138 f.Start(); 139 f.Start();
139 140
140 MessageLoop::current()->Run(); 141 base::MessageLoop::current()->Run();
141 142
142 EXPECT_TRUE(did_run); 143 EXPECT_TRUE(did_run);
143 } 144 }
144 145
145 void RunTest_RepeatingTimer_Cancel(MessageLoop::Type message_loop_type) { 146 void RunTest_RepeatingTimer_Cancel(base::MessageLoop::Type message_loop_type) {
146 MessageLoop loop(message_loop_type); 147 base::MessageLoop loop(message_loop_type);
147 148
148 bool did_run_a = false; 149 bool did_run_a = false;
149 RepeatingTimerTester* a = new RepeatingTimerTester(&did_run_a); 150 RepeatingTimerTester* a = new RepeatingTimerTester(&did_run_a);
150 151
151 // This should run before the timer expires. 152 // This should run before the timer expires.
152 MessageLoop::current()->DeleteSoon(FROM_HERE, a); 153 base::MessageLoop::current()->DeleteSoon(FROM_HERE, a);
153 154
154 // Now start the timer. 155 // Now start the timer.
155 a->Start(); 156 a->Start();
156 157
157 bool did_run_b = false; 158 bool did_run_b = false;
158 RepeatingTimerTester b(&did_run_b); 159 RepeatingTimerTester b(&did_run_b);
159 b.Start(); 160 b.Start();
160 161
161 MessageLoop::current()->Run(); 162 base::MessageLoop::current()->Run();
162 163
163 EXPECT_FALSE(did_run_a); 164 EXPECT_FALSE(did_run_a);
164 EXPECT_TRUE(did_run_b); 165 EXPECT_TRUE(did_run_b);
165 } 166 }
166 167
167 class DelayTimerTarget { 168 class DelayTimerTarget {
168 public: 169 public:
169 DelayTimerTarget() 170 DelayTimerTarget()
170 : signaled_(false) { 171 : signaled_(false) {
171 } 172 }
172 173
173 bool signaled() const { return signaled_; } 174 bool signaled() const { return signaled_; }
174 175
175 void Signal() { 176 void Signal() {
176 ASSERT_FALSE(signaled_); 177 ASSERT_FALSE(signaled_);
177 signaled_ = true; 178 signaled_ = true;
178 } 179 }
179 180
180 private: 181 private:
181 bool signaled_; 182 bool signaled_;
182 }; 183 };
183 184
184 void RunTest_DelayTimer_NoCall(MessageLoop::Type message_loop_type) { 185 void RunTest_DelayTimer_NoCall(base::MessageLoop::Type message_loop_type) {
185 MessageLoop loop(message_loop_type); 186 base::MessageLoop loop(message_loop_type);
186 187
187 // If Delay is never called, the timer shouldn't go off. 188 // If Delay is never called, the timer shouldn't go off.
188 DelayTimerTarget target; 189 DelayTimerTarget target;
189 base::DelayTimer<DelayTimerTarget> timer(FROM_HERE, 190 base::DelayTimer<DelayTimerTarget> timer(FROM_HERE,
190 TimeDelta::FromMilliseconds(1), &target, &DelayTimerTarget::Signal); 191 TimeDelta::FromMilliseconds(1), &target, &DelayTimerTarget::Signal);
191 192
192 bool did_run = false; 193 bool did_run = false;
193 OneShotTimerTester tester(&did_run); 194 OneShotTimerTester tester(&did_run);
194 tester.Start(); 195 tester.Start();
195 MessageLoop::current()->Run(); 196 base::MessageLoop::current()->Run();
196 197
197 ASSERT_FALSE(target.signaled()); 198 ASSERT_FALSE(target.signaled());
198 } 199 }
199 200
200 void RunTest_DelayTimer_OneCall(MessageLoop::Type message_loop_type) { 201 void RunTest_DelayTimer_OneCall(base::MessageLoop::Type message_loop_type) {
201 MessageLoop loop(message_loop_type); 202 base::MessageLoop loop(message_loop_type);
202 203
203 DelayTimerTarget target; 204 DelayTimerTarget target;
204 base::DelayTimer<DelayTimerTarget> timer(FROM_HERE, 205 base::DelayTimer<DelayTimerTarget> timer(FROM_HERE,
205 TimeDelta::FromMilliseconds(1), &target, &DelayTimerTarget::Signal); 206 TimeDelta::FromMilliseconds(1), &target, &DelayTimerTarget::Signal);
206 timer.Reset(); 207 timer.Reset();
207 208
208 bool did_run = false; 209 bool did_run = false;
209 OneShotTimerTester tester(&did_run, 100 /* milliseconds */); 210 OneShotTimerTester tester(&did_run, 100 /* milliseconds */);
210 tester.Start(); 211 tester.Start();
211 MessageLoop::current()->Run(); 212 base::MessageLoop::current()->Run();
212 213
213 ASSERT_TRUE(target.signaled()); 214 ASSERT_TRUE(target.signaled());
214 } 215 }
215 216
216 struct ResetHelper { 217 struct ResetHelper {
217 ResetHelper(base::DelayTimer<DelayTimerTarget>* timer, 218 ResetHelper(base::DelayTimer<DelayTimerTarget>* timer,
218 DelayTimerTarget* target) 219 DelayTimerTarget* target)
219 : timer_(timer), 220 : timer_(timer),
220 target_(target) { 221 target_(target) {
221 } 222 }
222 223
223 void Reset() { 224 void Reset() {
224 ASSERT_FALSE(target_->signaled()); 225 ASSERT_FALSE(target_->signaled());
225 timer_->Reset(); 226 timer_->Reset();
226 } 227 }
227 228
228 private: 229 private:
229 base::DelayTimer<DelayTimerTarget> *const timer_; 230 base::DelayTimer<DelayTimerTarget> *const timer_;
230 DelayTimerTarget *const target_; 231 DelayTimerTarget *const target_;
231 }; 232 };
232 233
233 void RunTest_DelayTimer_Reset(MessageLoop::Type message_loop_type) { 234 void RunTest_DelayTimer_Reset(base::MessageLoop::Type message_loop_type) {
234 MessageLoop loop(message_loop_type); 235 base::MessageLoop loop(message_loop_type);
235 236
236 // If Delay is never called, the timer shouldn't go off. 237 // If Delay is never called, the timer shouldn't go off.
237 DelayTimerTarget target; 238 DelayTimerTarget target;
238 base::DelayTimer<DelayTimerTarget> timer(FROM_HERE, 239 base::DelayTimer<DelayTimerTarget> timer(FROM_HERE,
239 TimeDelta::FromMilliseconds(50), &target, &DelayTimerTarget::Signal); 240 TimeDelta::FromMilliseconds(50), &target, &DelayTimerTarget::Signal);
240 timer.Reset(); 241 timer.Reset();
241 242
242 ResetHelper reset_helper(&timer, &target); 243 ResetHelper reset_helper(&timer, &target);
243 244
244 base::OneShotTimer<ResetHelper> timers[20]; 245 base::OneShotTimer<ResetHelper> timers[20];
245 for (size_t i = 0; i < arraysize(timers); ++i) { 246 for (size_t i = 0; i < arraysize(timers); ++i) {
246 timers[i].Start(FROM_HERE, TimeDelta::FromMilliseconds(i * 10), 247 timers[i].Start(FROM_HERE, TimeDelta::FromMilliseconds(i * 10),
247 &reset_helper, &ResetHelper::Reset); 248 &reset_helper, &ResetHelper::Reset);
248 } 249 }
249 250
250 bool did_run = false; 251 bool did_run = false;
251 OneShotTimerTester tester(&did_run, 300); 252 OneShotTimerTester tester(&did_run, 300);
252 tester.Start(); 253 tester.Start();
253 MessageLoop::current()->Run(); 254 base::MessageLoop::current()->Run();
254 255
255 ASSERT_TRUE(target.signaled()); 256 ASSERT_TRUE(target.signaled());
256 } 257 }
257 258
258 class DelayTimerFatalTarget { 259 class DelayTimerFatalTarget {
259 public: 260 public:
260 void Signal() { 261 void Signal() {
261 ASSERT_TRUE(false); 262 ASSERT_TRUE(false);
262 } 263 }
263 }; 264 };
264 265
265 266
266 void RunTest_DelayTimer_Deleted(MessageLoop::Type message_loop_type) { 267 void RunTest_DelayTimer_Deleted(base::MessageLoop::Type message_loop_type) {
267 MessageLoop loop(message_loop_type); 268 base::MessageLoop loop(message_loop_type);
268 269
269 DelayTimerFatalTarget target; 270 DelayTimerFatalTarget target;
270 271
271 { 272 {
272 base::DelayTimer<DelayTimerFatalTarget> timer( 273 base::DelayTimer<DelayTimerFatalTarget> timer(
273 FROM_HERE, TimeDelta::FromMilliseconds(50), &target, 274 FROM_HERE, TimeDelta::FromMilliseconds(50), &target,
274 &DelayTimerFatalTarget::Signal); 275 &DelayTimerFatalTarget::Signal);
275 timer.Reset(); 276 timer.Reset();
276 } 277 }
277 278
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 // message loop does not cause crashes if there were pending 349 // message loop does not cause crashes if there were pending
349 // timers not yet fired. It may only trigger exceptions 350 // timers not yet fired. It may only trigger exceptions
350 // if debug heap checking is enabled. 351 // if debug heap checking is enabled.
351 bool did_run = false; 352 bool did_run = false;
352 { 353 {
353 OneShotTimerTester a(&did_run); 354 OneShotTimerTester a(&did_run);
354 OneShotTimerTester b(&did_run); 355 OneShotTimerTester b(&did_run);
355 OneShotTimerTester c(&did_run); 356 OneShotTimerTester c(&did_run);
356 OneShotTimerTester d(&did_run); 357 OneShotTimerTester d(&did_run);
357 { 358 {
358 MessageLoop loop(MessageLoop::TYPE_DEFAULT); 359 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT);
359 a.Start(); 360 a.Start();
360 b.Start(); 361 b.Start();
361 } // MessageLoop destructs by falling out of scope. 362 } // MessageLoop destructs by falling out of scope.
362 } // OneShotTimers destruct. SHOULD NOT CRASH, of course. 363 } // OneShotTimers destruct. SHOULD NOT CRASH, of course.
363 364
364 EXPECT_FALSE(did_run); 365 EXPECT_FALSE(did_run);
365 } 366 }
366 367
367 void TimerTestCallback() { 368 void TimerTestCallback() {
368 } 369 }
369 370
370 TEST(TimerTest, NonRepeatIsRunning) { 371 TEST(TimerTest, NonRepeatIsRunning) {
371 { 372 {
372 MessageLoop loop(MessageLoop::TYPE_DEFAULT); 373 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT);
373 base::Timer timer(false, false); 374 base::Timer timer(false, false);
374 EXPECT_FALSE(timer.IsRunning()); 375 EXPECT_FALSE(timer.IsRunning());
375 timer.Start(FROM_HERE, TimeDelta::FromDays(1), 376 timer.Start(FROM_HERE, TimeDelta::FromDays(1),
376 base::Bind(&TimerTestCallback)); 377 base::Bind(&TimerTestCallback));
377 EXPECT_TRUE(timer.IsRunning()); 378 EXPECT_TRUE(timer.IsRunning());
378 timer.Stop(); 379 timer.Stop();
379 EXPECT_FALSE(timer.IsRunning()); 380 EXPECT_FALSE(timer.IsRunning());
380 EXPECT_TRUE(timer.user_task().is_null()); 381 EXPECT_TRUE(timer.user_task().is_null());
381 } 382 }
382 383
383 { 384 {
384 base::Timer timer(true, false); 385 base::Timer timer(true, false);
385 MessageLoop loop(MessageLoop::TYPE_DEFAULT); 386 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT);
386 EXPECT_FALSE(timer.IsRunning()); 387 EXPECT_FALSE(timer.IsRunning());
387 timer.Start(FROM_HERE, TimeDelta::FromDays(1), 388 timer.Start(FROM_HERE, TimeDelta::FromDays(1),
388 base::Bind(&TimerTestCallback)); 389 base::Bind(&TimerTestCallback));
389 EXPECT_TRUE(timer.IsRunning()); 390 EXPECT_TRUE(timer.IsRunning());
390 timer.Stop(); 391 timer.Stop();
391 EXPECT_FALSE(timer.IsRunning()); 392 EXPECT_FALSE(timer.IsRunning());
392 ASSERT_FALSE(timer.user_task().is_null()); 393 ASSERT_FALSE(timer.user_task().is_null());
393 timer.Reset(); 394 timer.Reset();
394 EXPECT_TRUE(timer.IsRunning()); 395 EXPECT_TRUE(timer.IsRunning());
395 } 396 }
396 } 397 }
397 398
398 TEST(TimerTest, NonRepeatMessageLoopDeath) { 399 TEST(TimerTest, NonRepeatMessageLoopDeath) {
399 base::Timer timer(false, false); 400 base::Timer timer(false, false);
400 { 401 {
401 MessageLoop loop(MessageLoop::TYPE_DEFAULT); 402 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT);
402 EXPECT_FALSE(timer.IsRunning()); 403 EXPECT_FALSE(timer.IsRunning());
403 timer.Start(FROM_HERE, TimeDelta::FromDays(1), 404 timer.Start(FROM_HERE, TimeDelta::FromDays(1),
404 base::Bind(&TimerTestCallback)); 405 base::Bind(&TimerTestCallback));
405 EXPECT_TRUE(timer.IsRunning()); 406 EXPECT_TRUE(timer.IsRunning());
406 } 407 }
407 EXPECT_FALSE(timer.IsRunning()); 408 EXPECT_FALSE(timer.IsRunning());
408 EXPECT_TRUE(timer.user_task().is_null()); 409 EXPECT_TRUE(timer.user_task().is_null());
409 } 410 }
410 411
411 TEST(TimerTest, RetainRepeatIsRunning) { 412 TEST(TimerTest, RetainRepeatIsRunning) {
412 MessageLoop loop(MessageLoop::TYPE_DEFAULT); 413 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT);
413 base::Timer timer(FROM_HERE, TimeDelta::FromDays(1), 414 base::Timer timer(FROM_HERE, TimeDelta::FromDays(1),
414 base::Bind(&TimerTestCallback), true); 415 base::Bind(&TimerTestCallback), true);
415 EXPECT_FALSE(timer.IsRunning()); 416 EXPECT_FALSE(timer.IsRunning());
416 timer.Reset(); 417 timer.Reset();
417 EXPECT_TRUE(timer.IsRunning()); 418 EXPECT_TRUE(timer.IsRunning());
418 timer.Stop(); 419 timer.Stop();
419 EXPECT_FALSE(timer.IsRunning()); 420 EXPECT_FALSE(timer.IsRunning());
420 timer.Reset(); 421 timer.Reset();
421 EXPECT_TRUE(timer.IsRunning()); 422 EXPECT_TRUE(timer.IsRunning());
422 } 423 }
423 424
424 TEST(TimerTest, RetainNonRepeatIsRunning) { 425 TEST(TimerTest, RetainNonRepeatIsRunning) {
425 MessageLoop loop(MessageLoop::TYPE_DEFAULT); 426 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT);
426 base::Timer timer(FROM_HERE, TimeDelta::FromDays(1), 427 base::Timer timer(FROM_HERE, TimeDelta::FromDays(1),
427 base::Bind(&TimerTestCallback), false); 428 base::Bind(&TimerTestCallback), false);
428 EXPECT_FALSE(timer.IsRunning()); 429 EXPECT_FALSE(timer.IsRunning());
429 timer.Reset(); 430 timer.Reset();
430 EXPECT_TRUE(timer.IsRunning()); 431 EXPECT_TRUE(timer.IsRunning());
431 timer.Stop(); 432 timer.Stop();
432 EXPECT_FALSE(timer.IsRunning()); 433 EXPECT_FALSE(timer.IsRunning());
433 timer.Reset(); 434 timer.Reset();
434 EXPECT_TRUE(timer.IsRunning()); 435 EXPECT_TRUE(timer.IsRunning());
435 } 436 }
436 437
437 namespace { 438 namespace {
438 439
439 bool g_callback_happened1 = false; 440 bool g_callback_happened1 = false;
440 bool g_callback_happened2 = false; 441 bool g_callback_happened2 = false;
441 442
442 void ClearAllCallbackHappened() { 443 void ClearAllCallbackHappened() {
443 g_callback_happened1 = false; 444 g_callback_happened1 = false;
444 g_callback_happened2 = false; 445 g_callback_happened2 = false;
445 } 446 }
446 447
447 void SetCallbackHappened1() { 448 void SetCallbackHappened1() {
448 g_callback_happened1 = true; 449 g_callback_happened1 = true;
449 MessageLoop::current()->QuitWhenIdle(); 450 base::MessageLoop::current()->QuitWhenIdle();
450 } 451 }
451 452
452 void SetCallbackHappened2() { 453 void SetCallbackHappened2() {
453 g_callback_happened2 = true; 454 g_callback_happened2 = true;
454 MessageLoop::current()->QuitWhenIdle(); 455 base::MessageLoop::current()->QuitWhenIdle();
455 } 456 }
456 457
457 TEST(TimerTest, ContinuationStopStart) { 458 TEST(TimerTest, ContinuationStopStart) {
458 { 459 {
459 ClearAllCallbackHappened(); 460 ClearAllCallbackHappened();
460 MessageLoop loop(MessageLoop::TYPE_DEFAULT); 461 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT);
461 base::Timer timer(false, false); 462 base::Timer timer(false, false);
462 timer.Start(FROM_HERE, TimeDelta::FromMilliseconds(10), 463 timer.Start(FROM_HERE, TimeDelta::FromMilliseconds(10),
463 base::Bind(&SetCallbackHappened1)); 464 base::Bind(&SetCallbackHappened1));
464 timer.Stop(); 465 timer.Stop();
465 timer.Start(FROM_HERE, TimeDelta::FromMilliseconds(40), 466 timer.Start(FROM_HERE, TimeDelta::FromMilliseconds(40),
466 base::Bind(&SetCallbackHappened2)); 467 base::Bind(&SetCallbackHappened2));
467 MessageLoop::current()->Run(); 468 base::MessageLoop::current()->Run();
468 EXPECT_FALSE(g_callback_happened1); 469 EXPECT_FALSE(g_callback_happened1);
469 EXPECT_TRUE(g_callback_happened2); 470 EXPECT_TRUE(g_callback_happened2);
470 } 471 }
471 } 472 }
472 473
473 TEST(TimerTest, ContinuationReset) { 474 TEST(TimerTest, ContinuationReset) {
474 { 475 {
475 ClearAllCallbackHappened(); 476 ClearAllCallbackHappened();
476 MessageLoop loop(MessageLoop::TYPE_DEFAULT); 477 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT);
477 base::Timer timer(false, false); 478 base::Timer timer(false, false);
478 timer.Start(FROM_HERE, TimeDelta::FromMilliseconds(10), 479 timer.Start(FROM_HERE, TimeDelta::FromMilliseconds(10),
479 base::Bind(&SetCallbackHappened1)); 480 base::Bind(&SetCallbackHappened1));
480 timer.Reset(); 481 timer.Reset();
481 // Since Reset happened before task ran, the user_task must not be cleared: 482 // Since Reset happened before task ran, the user_task must not be cleared:
482 ASSERT_FALSE(timer.user_task().is_null()); 483 ASSERT_FALSE(timer.user_task().is_null());
483 MessageLoop::current()->Run(); 484 base::MessageLoop::current()->Run();
484 EXPECT_TRUE(g_callback_happened1); 485 EXPECT_TRUE(g_callback_happened1);
485 } 486 }
486 } 487 }
487 488
488 } // namespace 489 } // namespace
OLDNEW
« no previous file with comments | « base/threading/thread_unittest.cc ('k') | cc/test/fake_output_surface.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698