OLD | NEW |
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 |
11 using base::TimeDelta; | 11 using base::TimeDelta; |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 class OneShotTimerTester { | 15 class OneShotTimerTester { |
16 public: | 16 public: |
17 OneShotTimerTester(bool* did_run, unsigned milliseconds = 10) | 17 OneShotTimerTester(bool* did_run, unsigned milliseconds = 10) |
18 : did_run_(did_run), | 18 : did_run_(did_run), |
19 delay_ms_(milliseconds) { | 19 delay_ms_(milliseconds) { |
20 } | 20 } |
21 void Start() { | 21 void Start() { |
22 timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(delay_ms_), this, | 22 timer_.Start(TimeDelta::FromMilliseconds(delay_ms_), this, |
23 &OneShotTimerTester::Run); | 23 &OneShotTimerTester::Run); |
24 } | 24 } |
25 private: | 25 private: |
26 void Run() { | 26 void Run() { |
27 *did_run_ = true; | 27 *did_run_ = true; |
28 MessageLoop::current()->Quit(); | 28 MessageLoop::current()->Quit(); |
29 } | 29 } |
30 bool* did_run_; | 30 bool* did_run_; |
31 base::OneShotTimer<OneShotTimerTester> timer_; | 31 base::OneShotTimer<OneShotTimerTester> timer_; |
32 const unsigned delay_ms_; | 32 const unsigned delay_ms_; |
33 }; | 33 }; |
34 | 34 |
35 class OneShotSelfDeletingTimerTester { | 35 class OneShotSelfDeletingTimerTester { |
36 public: | 36 public: |
37 explicit OneShotSelfDeletingTimerTester(bool* did_run) : | 37 explicit OneShotSelfDeletingTimerTester(bool* did_run) : |
38 did_run_(did_run), | 38 did_run_(did_run), |
39 timer_(new base::OneShotTimer<OneShotSelfDeletingTimerTester>()) { | 39 timer_(new base::OneShotTimer<OneShotSelfDeletingTimerTester>()) { |
40 } | 40 } |
41 void Start() { | 41 void Start() { |
42 timer_->Start(FROM_HERE, TimeDelta::FromMilliseconds(10), this, | 42 timer_->Start(TimeDelta::FromMilliseconds(10), this, |
43 &OneShotSelfDeletingTimerTester::Run); | 43 &OneShotSelfDeletingTimerTester::Run); |
44 } | 44 } |
45 private: | 45 private: |
46 void Run() { | 46 void Run() { |
47 *did_run_ = true; | 47 *did_run_ = true; |
48 timer_.reset(); | 48 timer_.reset(); |
49 MessageLoop::current()->Quit(); | 49 MessageLoop::current()->Quit(); |
50 } | 50 } |
51 bool* did_run_; | 51 bool* did_run_; |
52 scoped_ptr<base::OneShotTimer<OneShotSelfDeletingTimerTester> > timer_; | 52 scoped_ptr<base::OneShotTimer<OneShotSelfDeletingTimerTester> > timer_; |
53 }; | 53 }; |
54 | 54 |
55 class RepeatingTimerTester { | 55 class RepeatingTimerTester { |
56 public: | 56 public: |
57 explicit RepeatingTimerTester(bool* did_run) | 57 explicit RepeatingTimerTester(bool* did_run) |
58 : did_run_(did_run), counter_(10) { | 58 : did_run_(did_run), counter_(10) { |
59 } | 59 } |
60 | 60 |
61 void Start() { | 61 void Start() { |
62 timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(10), this, | 62 timer_.Start(TimeDelta::FromMilliseconds(10), this, |
63 &RepeatingTimerTester::Run); | 63 &RepeatingTimerTester::Run); |
64 } | 64 } |
65 private: | 65 private: |
66 void Run() { | 66 void Run() { |
67 if (--counter_ == 0) { | 67 if (--counter_ == 0) { |
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_; |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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(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(FROM_HERE, | 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(message_loop_type); |
192 | 192 |
193 DelayTimerTarget target; | 193 DelayTimerTarget target; |
194 base::DelayTimer<DelayTimerTarget> timer(FROM_HERE, | 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(); |
202 | 202 |
203 ASSERT_TRUE(target.signaled()); | 203 ASSERT_TRUE(target.signaled()); |
204 } | 204 } |
(...skipping 13 matching lines...) Expand all Loading... |
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(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(FROM_HERE, | 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]; |
235 for (size_t i = 0; i < arraysize(timers); ++i) { | 235 for (size_t i = 0; i < arraysize(timers); ++i) { |
236 timers[i].Start(FROM_HERE, TimeDelta::FromMilliseconds(i * 10), | 236 timers[i].Start(TimeDelta::FromMilliseconds(i * 10), &reset_helper, |
237 &reset_helper, &ResetHelper::Reset); | 237 &ResetHelper::Reset); |
238 } | 238 } |
239 | 239 |
240 bool did_run = false; | 240 bool did_run = false; |
241 OneShotTimerTester tester(&did_run, 300); | 241 OneShotTimerTester tester(&did_run, 300); |
242 tester.Start(); | 242 tester.Start(); |
243 MessageLoop::current()->Run(); | 243 MessageLoop::current()->Run(); |
244 | 244 |
245 ASSERT_TRUE(target.signaled()); | 245 ASSERT_TRUE(target.signaled()); |
246 } | 246 } |
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(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 FROM_HERE, 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 |
268 // When the timer is deleted, the DelayTimerFatalTarget should never be | 268 // When the timer is deleted, the DelayTimerFatalTarget should never be |
269 // called. | 269 // called. |
270 base::PlatformThread::Sleep(100); | 270 base::PlatformThread::Sleep(100); |
271 } | 271 } |
272 | 272 |
273 } // namespace | 273 } // namespace |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 OneShotTimerTester d(&did_run); | 346 OneShotTimerTester d(&did_run); |
347 { | 347 { |
348 MessageLoop loop(MessageLoop::TYPE_DEFAULT); | 348 MessageLoop loop(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 } |
OLD | NEW |