OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/message_loop.h" | 5 #include "base/message_loop.h" |
6 #include "base/scoped_ptr.h" | 6 #include "base/scoped_ptr.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) : did_run_(did_run) { | 17 OneShotTimerTester(bool* did_run, unsigned milliseconds = 10) |
| 18 : did_run_(did_run), |
| 19 delay_ms_(milliseconds) { |
18 } | 20 } |
19 void Start() { | 21 void Start() { |
20 timer_.Start(TimeDelta::FromMilliseconds(10), this, | 22 timer_.Start(TimeDelta::FromMilliseconds(delay_ms_), this, |
21 &OneShotTimerTester::Run); | 23 &OneShotTimerTester::Run); |
22 } | 24 } |
23 private: | 25 private: |
24 void Run() { | 26 void Run() { |
25 *did_run_ = true; | 27 *did_run_ = true; |
26 MessageLoop::current()->Quit(); | 28 MessageLoop::current()->Quit(); |
27 } | 29 } |
28 bool* did_run_; | 30 bool* did_run_; |
29 base::OneShotTimer<OneShotTimerTester> timer_; | 31 base::OneShotTimer<OneShotTimerTester> timer_; |
| 32 const unsigned delay_ms_; |
30 }; | 33 }; |
31 | 34 |
32 class OneShotSelfDeletingTimerTester { | 35 class OneShotSelfDeletingTimerTester { |
33 public: | 36 public: |
34 OneShotSelfDeletingTimerTester(bool* did_run) : | 37 OneShotSelfDeletingTimerTester(bool* did_run) : |
35 did_run_(did_run), | 38 did_run_(did_run), |
36 timer_(new base::OneShotTimer<OneShotSelfDeletingTimerTester>()) { | 39 timer_(new base::OneShotTimer<OneShotSelfDeletingTimerTester>()) { |
37 } | 40 } |
38 void Start() { | 41 void Start() { |
39 timer_->Start(TimeDelta::FromMilliseconds(10), this, | 42 timer_->Start(TimeDelta::FromMilliseconds(10), this, |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 MessageLoop loop(message_loop_type); | 134 MessageLoop loop(message_loop_type); |
132 | 135 |
133 bool did_run_a = false; | 136 bool did_run_a = false; |
134 RepeatingTimerTester* a = new RepeatingTimerTester(&did_run_a); | 137 RepeatingTimerTester* a = new RepeatingTimerTester(&did_run_a); |
135 | 138 |
136 // This should run before the timer expires. | 139 // This should run before the timer expires. |
137 MessageLoop::current()->DeleteSoon(FROM_HERE, a); | 140 MessageLoop::current()->DeleteSoon(FROM_HERE, a); |
138 | 141 |
139 // Now start the timer. | 142 // Now start the timer. |
140 a->Start(); | 143 a->Start(); |
141 | 144 |
142 bool did_run_b = false; | 145 bool did_run_b = false; |
143 RepeatingTimerTester b(&did_run_b); | 146 RepeatingTimerTester b(&did_run_b); |
144 b.Start(); | 147 b.Start(); |
145 | 148 |
146 MessageLoop::current()->Run(); | 149 MessageLoop::current()->Run(); |
147 | 150 |
148 EXPECT_FALSE(did_run_a); | 151 EXPECT_FALSE(did_run_a); |
149 EXPECT_TRUE(did_run_b); | 152 EXPECT_TRUE(did_run_b); |
150 } | 153 } |
151 | 154 |
| 155 class DelayTimerTarget { |
| 156 public: |
| 157 DelayTimerTarget() |
| 158 : signaled_(false) { |
| 159 } |
| 160 |
| 161 bool signaled() const { return signaled_; } |
| 162 |
| 163 void Signal() { |
| 164 ASSERT_FALSE(signaled_); |
| 165 signaled_ = true; |
| 166 } |
| 167 |
| 168 private: |
| 169 bool signaled_; |
| 170 }; |
| 171 |
| 172 void RunTest_DelayTimer_NoCall(MessageLoop::Type message_loop_type) { |
| 173 MessageLoop loop(message_loop_type); |
| 174 |
| 175 // If Delay is never called, the timer shouldn't go off. |
| 176 DelayTimerTarget target; |
| 177 base::DelayTimer<DelayTimerTarget> timer( |
| 178 TimeDelta::FromMilliseconds(1), &target, &DelayTimerTarget::Signal); |
| 179 |
| 180 bool did_run = false; |
| 181 OneShotTimerTester tester(&did_run); |
| 182 tester.Start(); |
| 183 MessageLoop::current()->Run(); |
| 184 |
| 185 ASSERT_FALSE(target.signaled()); |
| 186 } |
| 187 |
| 188 void RunTest_DelayTimer_OneCall(MessageLoop::Type message_loop_type) { |
| 189 MessageLoop loop(message_loop_type); |
| 190 |
| 191 DelayTimerTarget target; |
| 192 base::DelayTimer<DelayTimerTarget> timer( |
| 193 TimeDelta::FromMilliseconds(1), &target, &DelayTimerTarget::Signal); |
| 194 timer.Reset(); |
| 195 |
| 196 bool did_run = false; |
| 197 OneShotTimerTester tester(&did_run, 100 /* milliseconds */); |
| 198 tester.Start(); |
| 199 MessageLoop::current()->Run(); |
| 200 |
| 201 ASSERT_TRUE(target.signaled()); |
| 202 } |
| 203 |
| 204 struct ResetHelper { |
| 205 ResetHelper(base::DelayTimer<DelayTimerTarget>* timer, |
| 206 DelayTimerTarget* target) |
| 207 : timer_(timer), |
| 208 target_(target) { |
| 209 } |
| 210 |
| 211 void Reset() { |
| 212 ASSERT_FALSE(target_->signaled()); |
| 213 timer_->Reset(); |
| 214 } |
| 215 |
| 216 private: |
| 217 base::DelayTimer<DelayTimerTarget> *const timer_; |
| 218 DelayTimerTarget *const target_; |
| 219 }; |
| 220 |
| 221 void RunTest_DelayTimer_Reset(MessageLoop::Type message_loop_type) { |
| 222 MessageLoop loop(message_loop_type); |
| 223 |
| 224 // If Delay is never called, the timer shouldn't go off. |
| 225 DelayTimerTarget target; |
| 226 base::DelayTimer<DelayTimerTarget> timer( |
| 227 TimeDelta::FromMilliseconds(1), &target, &DelayTimerTarget::Signal); |
| 228 timer.Reset(); |
| 229 |
| 230 ResetHelper reset_helper(&timer, &target); |
| 231 |
| 232 base::OneShotTimer<ResetHelper> timers[20]; |
| 233 for (size_t i = 0; i < arraysize(timers); ++i) { |
| 234 timers[i].Start(TimeDelta::FromMilliseconds(i * 10), &reset_helper, |
| 235 &ResetHelper::Reset); |
| 236 } |
| 237 |
| 238 bool did_run = false; |
| 239 OneShotTimerTester tester(&did_run, 300); |
| 240 tester.Start(); |
| 241 MessageLoop::current()->Run(); |
| 242 |
| 243 ASSERT_TRUE(target.signaled()); |
| 244 } |
| 245 |
152 } // namespace | 246 } // namespace |
153 | 247 |
154 //----------------------------------------------------------------------------- | 248 //----------------------------------------------------------------------------- |
155 // Each test is run against each type of MessageLoop. That way we are sure | 249 // Each test is run against each type of MessageLoop. That way we are sure |
156 // that timers work properly in all configurations. | 250 // that timers work properly in all configurations. |
157 | 251 |
158 TEST(TimerTest, OneShotTimer) { | 252 TEST(TimerTest, OneShotTimer) { |
159 RunTest_OneShotTimer(MessageLoop::TYPE_DEFAULT); | 253 RunTest_OneShotTimer(MessageLoop::TYPE_DEFAULT); |
160 RunTest_OneShotTimer(MessageLoop::TYPE_UI); | 254 RunTest_OneShotTimer(MessageLoop::TYPE_UI); |
161 RunTest_OneShotTimer(MessageLoop::TYPE_IO); | 255 RunTest_OneShotTimer(MessageLoop::TYPE_IO); |
(...skipping 18 matching lines...) Expand all Loading... |
180 RunTest_RepeatingTimer(MessageLoop::TYPE_UI); | 274 RunTest_RepeatingTimer(MessageLoop::TYPE_UI); |
181 RunTest_RepeatingTimer(MessageLoop::TYPE_IO); | 275 RunTest_RepeatingTimer(MessageLoop::TYPE_IO); |
182 } | 276 } |
183 | 277 |
184 TEST(TimerTest, RepeatingTimer_Cancel) { | 278 TEST(TimerTest, RepeatingTimer_Cancel) { |
185 RunTest_RepeatingTimer_Cancel(MessageLoop::TYPE_DEFAULT); | 279 RunTest_RepeatingTimer_Cancel(MessageLoop::TYPE_DEFAULT); |
186 RunTest_RepeatingTimer_Cancel(MessageLoop::TYPE_UI); | 280 RunTest_RepeatingTimer_Cancel(MessageLoop::TYPE_UI); |
187 RunTest_RepeatingTimer_Cancel(MessageLoop::TYPE_IO); | 281 RunTest_RepeatingTimer_Cancel(MessageLoop::TYPE_IO); |
188 } | 282 } |
189 | 283 |
| 284 TEST(TimerTest, DelayTimer_NoCall) { |
| 285 RunTest_DelayTimer_NoCall(MessageLoop::TYPE_DEFAULT); |
| 286 RunTest_DelayTimer_NoCall(MessageLoop::TYPE_UI); |
| 287 RunTest_DelayTimer_NoCall(MessageLoop::TYPE_IO); |
| 288 } |
| 289 |
| 290 TEST(TimerTest, DelayTimer_OneCall) { |
| 291 RunTest_DelayTimer_OneCall(MessageLoop::TYPE_DEFAULT); |
| 292 RunTest_DelayTimer_OneCall(MessageLoop::TYPE_UI); |
| 293 RunTest_DelayTimer_OneCall(MessageLoop::TYPE_IO); |
| 294 } |
| 295 |
| 296 TEST(TimerTest, DelayTimer_Reset) { |
| 297 RunTest_DelayTimer_Reset(MessageLoop::TYPE_DEFAULT); |
| 298 RunTest_DelayTimer_Reset(MessageLoop::TYPE_UI); |
| 299 RunTest_DelayTimer_Reset(MessageLoop::TYPE_IO); |
| 300 } |
| 301 |
190 TEST(TimerTest, MessageLoopShutdown) { | 302 TEST(TimerTest, MessageLoopShutdown) { |
191 // This test is designed to verify that shutdown of the | 303 // This test is designed to verify that shutdown of the |
192 // message loop does not cause crashes if there were pending | 304 // message loop does not cause crashes if there were pending |
193 // timers not yet fired. It may only trigger exceptions | 305 // timers not yet fired. It may only trigger exceptions |
194 // if debug heap checking (or purify) is enabled. | 306 // if debug heap checking (or purify) is enabled. |
195 bool did_run = false; | 307 bool did_run = false; |
196 { | 308 { |
197 OneShotTimerTester a(&did_run); | 309 OneShotTimerTester a(&did_run); |
198 OneShotTimerTester b(&did_run); | 310 OneShotTimerTester b(&did_run); |
199 OneShotTimerTester c(&did_run); | 311 OneShotTimerTester c(&did_run); |
200 OneShotTimerTester d(&did_run); | 312 OneShotTimerTester d(&did_run); |
201 { | 313 { |
202 MessageLoop loop(MessageLoop::TYPE_DEFAULT); | 314 MessageLoop loop(MessageLoop::TYPE_DEFAULT); |
203 a.Start(); | 315 a.Start(); |
204 b.Start(); | 316 b.Start(); |
205 } // MessageLoop destructs by falling out of scope. | 317 } // MessageLoop destructs by falling out of scope. |
206 } // OneShotTimers destruct. SHOULD NOT CRASH, of course. | 318 } // OneShotTimers destruct. SHOULD NOT CRASH, of course. |
207 | 319 |
208 EXPECT_EQ(false, did_run); | 320 EXPECT_EQ(false, did_run); |
209 } | 321 } |
OLD | NEW |