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

Side by Side Diff: base/timer/timer_unittest.cc

Issue 2103333006: Remove calls to deprecated MessageLoop methods in base. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 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
« no previous file with comments | « base/threading/thread.cc ('k') | base/trace_event/trace_event_system_stats_monitor_unittest.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/timer/timer.h" 5 #include "base/timer/timer.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 10
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
13 #include "base/run_loop.h"
13 #include "base/test/test_simple_task_runner.h" 14 #include "base/test/test_simple_task_runner.h"
14 #include "base/threading/thread_task_runner_handle.h" 15 #include "base/threading/thread_task_runner_handle.h"
15 #include "build/build_config.h" 16 #include "build/build_config.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
17 18
18 using base::TimeDelta; 19 using base::TimeDelta;
19 using base::SingleThreadTaskRunner; 20 using base::SingleThreadTaskRunner;
20 21
21 namespace { 22 namespace {
22 23
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 base::RepeatingTimer timer_; 110 base::RepeatingTimer timer_;
110 }; 111 };
111 112
112 void RunTest_OneShotTimer(base::MessageLoop::Type message_loop_type) { 113 void RunTest_OneShotTimer(base::MessageLoop::Type message_loop_type) {
113 base::MessageLoop loop(message_loop_type); 114 base::MessageLoop loop(message_loop_type);
114 115
115 bool did_run = false; 116 bool did_run = false;
116 OneShotTimerTester f(&did_run); 117 OneShotTimerTester f(&did_run);
117 f.Start(); 118 f.Start();
118 119
119 base::MessageLoop::current()->Run(); 120 base::RunLoop().Run();
120 121
121 EXPECT_TRUE(did_run); 122 EXPECT_TRUE(did_run);
122 } 123 }
123 124
124 void RunTest_OneShotTimer_Cancel(base::MessageLoop::Type message_loop_type) { 125 void RunTest_OneShotTimer_Cancel(base::MessageLoop::Type message_loop_type) {
125 base::MessageLoop loop(message_loop_type); 126 base::MessageLoop loop(message_loop_type);
126 127
127 bool did_run_a = false; 128 bool did_run_a = false;
128 OneShotTimerTester* a = new OneShotTimerTester(&did_run_a); 129 OneShotTimerTester* a = new OneShotTimerTester(&did_run_a);
129 130
130 // This should run before the timer expires. 131 // This should run before the timer expires.
131 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, a); 132 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, a);
132 133
133 // Now start the timer. 134 // Now start the timer.
134 a->Start(); 135 a->Start();
135 136
136 bool did_run_b = false; 137 bool did_run_b = false;
137 OneShotTimerTester b(&did_run_b); 138 OneShotTimerTester b(&did_run_b);
138 b.Start(); 139 b.Start();
139 140
140 base::MessageLoop::current()->Run(); 141 base::RunLoop().Run();
141 142
142 EXPECT_FALSE(did_run_a); 143 EXPECT_FALSE(did_run_a);
143 EXPECT_TRUE(did_run_b); 144 EXPECT_TRUE(did_run_b);
144 } 145 }
145 146
146 void RunTest_OneShotSelfDeletingTimer( 147 void RunTest_OneShotSelfDeletingTimer(
147 base::MessageLoop::Type message_loop_type) { 148 base::MessageLoop::Type message_loop_type) {
148 base::MessageLoop loop(message_loop_type); 149 base::MessageLoop loop(message_loop_type);
149 150
150 bool did_run = false; 151 bool did_run = false;
151 OneShotSelfDeletingTimerTester f(&did_run); 152 OneShotSelfDeletingTimerTester f(&did_run);
152 f.Start(); 153 f.Start();
153 154
154 base::MessageLoop::current()->Run(); 155 base::RunLoop().Run();
155 156
156 EXPECT_TRUE(did_run); 157 EXPECT_TRUE(did_run);
157 } 158 }
158 159
159 void RunTest_RepeatingTimer(base::MessageLoop::Type message_loop_type, 160 void RunTest_RepeatingTimer(base::MessageLoop::Type message_loop_type,
160 const TimeDelta& delay) { 161 const TimeDelta& delay) {
161 base::MessageLoop loop(message_loop_type); 162 base::MessageLoop loop(message_loop_type);
162 163
163 bool did_run = false; 164 bool did_run = false;
164 RepeatingTimerTester f(&did_run, delay); 165 RepeatingTimerTester f(&did_run, delay);
165 f.Start(); 166 f.Start();
166 167
167 base::MessageLoop::current()->Run(); 168 base::RunLoop().Run();
168 169
169 EXPECT_TRUE(did_run); 170 EXPECT_TRUE(did_run);
170 } 171 }
171 172
172 void RunTest_RepeatingTimer_Cancel(base::MessageLoop::Type message_loop_type, 173 void RunTest_RepeatingTimer_Cancel(base::MessageLoop::Type message_loop_type,
173 const TimeDelta& delay) { 174 const TimeDelta& delay) {
174 base::MessageLoop loop(message_loop_type); 175 base::MessageLoop loop(message_loop_type);
175 176
176 bool did_run_a = false; 177 bool did_run_a = false;
177 RepeatingTimerTester* a = new RepeatingTimerTester(&did_run_a, delay); 178 RepeatingTimerTester* a = new RepeatingTimerTester(&did_run_a, delay);
178 179
179 // This should run before the timer expires. 180 // This should run before the timer expires.
180 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, a); 181 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, a);
181 182
182 // Now start the timer. 183 // Now start the timer.
183 a->Start(); 184 a->Start();
184 185
185 bool did_run_b = false; 186 bool did_run_b = false;
186 RepeatingTimerTester b(&did_run_b, delay); 187 RepeatingTimerTester b(&did_run_b, delay);
187 b.Start(); 188 b.Start();
188 189
189 base::MessageLoop::current()->Run(); 190 base::RunLoop().Run();
190 191
191 EXPECT_FALSE(did_run_a); 192 EXPECT_FALSE(did_run_a);
192 EXPECT_TRUE(did_run_b); 193 EXPECT_TRUE(did_run_b);
193 } 194 }
194 195
195 class DelayTimerTarget { 196 class DelayTimerTarget {
196 public: 197 public:
197 bool signaled() const { return signaled_; } 198 bool signaled() const { return signaled_; }
198 199
199 void Signal() { 200 void Signal() {
200 ASSERT_FALSE(signaled_); 201 ASSERT_FALSE(signaled_);
201 signaled_ = true; 202 signaled_ = true;
202 } 203 }
203 204
204 private: 205 private:
205 bool signaled_ = false; 206 bool signaled_ = false;
206 }; 207 };
207 208
208 void RunTest_DelayTimer_NoCall(base::MessageLoop::Type message_loop_type) { 209 void RunTest_DelayTimer_NoCall(base::MessageLoop::Type message_loop_type) {
209 base::MessageLoop loop(message_loop_type); 210 base::MessageLoop loop(message_loop_type);
210 211
211 // If Delay is never called, the timer shouldn't go off. 212 // If Delay is never called, the timer shouldn't go off.
212 DelayTimerTarget target; 213 DelayTimerTarget target;
213 base::DelayTimer timer(FROM_HERE, TimeDelta::FromMilliseconds(1), &target, 214 base::DelayTimer timer(FROM_HERE, TimeDelta::FromMilliseconds(1), &target,
214 &DelayTimerTarget::Signal); 215 &DelayTimerTarget::Signal);
215 216
216 bool did_run = false; 217 bool did_run = false;
217 OneShotTimerTester tester(&did_run); 218 OneShotTimerTester tester(&did_run);
218 tester.Start(); 219 tester.Start();
219 base::MessageLoop::current()->Run(); 220 base::RunLoop().Run();
220 221
221 ASSERT_FALSE(target.signaled()); 222 ASSERT_FALSE(target.signaled());
222 } 223 }
223 224
224 void RunTest_DelayTimer_OneCall(base::MessageLoop::Type message_loop_type) { 225 void RunTest_DelayTimer_OneCall(base::MessageLoop::Type message_loop_type) {
225 base::MessageLoop loop(message_loop_type); 226 base::MessageLoop loop(message_loop_type);
226 227
227 DelayTimerTarget target; 228 DelayTimerTarget target;
228 base::DelayTimer timer(FROM_HERE, TimeDelta::FromMilliseconds(1), &target, 229 base::DelayTimer timer(FROM_HERE, TimeDelta::FromMilliseconds(1), &target,
229 &DelayTimerTarget::Signal); 230 &DelayTimerTarget::Signal);
230 timer.Reset(); 231 timer.Reset();
231 232
232 bool did_run = false; 233 bool did_run = false;
233 OneShotTimerTester tester(&did_run, 100 /* milliseconds */); 234 OneShotTimerTester tester(&did_run, 100 /* milliseconds */);
234 tester.Start(); 235 tester.Start();
235 base::MessageLoop::current()->Run(); 236 base::RunLoop().Run();
236 237
237 ASSERT_TRUE(target.signaled()); 238 ASSERT_TRUE(target.signaled());
238 } 239 }
239 240
240 struct ResetHelper { 241 struct ResetHelper {
241 ResetHelper(base::DelayTimer* timer, DelayTimerTarget* target) 242 ResetHelper(base::DelayTimer* timer, DelayTimerTarget* target)
242 : timer_(timer), target_(target) {} 243 : timer_(timer), target_(target) {}
243 244
244 void Reset() { 245 void Reset() {
245 ASSERT_FALSE(target_->signaled()); 246 ASSERT_FALSE(target_->signaled());
(...skipping 18 matching lines...) Expand all
264 265
265 base::OneShotTimer timers[20]; 266 base::OneShotTimer timers[20];
266 for (size_t i = 0; i < arraysize(timers); ++i) { 267 for (size_t i = 0; i < arraysize(timers); ++i) {
267 timers[i].Start(FROM_HERE, TimeDelta::FromMilliseconds(i * 10), 268 timers[i].Start(FROM_HERE, TimeDelta::FromMilliseconds(i * 10),
268 &reset_helper, &ResetHelper::Reset); 269 &reset_helper, &ResetHelper::Reset);
269 } 270 }
270 271
271 bool did_run = false; 272 bool did_run = false;
272 OneShotTimerTester tester(&did_run, 300); 273 OneShotTimerTester tester(&did_run, 300);
273 tester.Start(); 274 tester.Start();
274 base::MessageLoop::current()->Run(); 275 base::RunLoop().Run();
275 276
276 ASSERT_TRUE(target.signaled()); 277 ASSERT_TRUE(target.signaled());
277 } 278 }
278 279
279 class DelayTimerFatalTarget { 280 class DelayTimerFatalTarget {
280 public: 281 public:
281 void Signal() { 282 void Signal() {
282 ASSERT_TRUE(false); 283 ASSERT_TRUE(false);
283 } 284 }
284 }; 285 };
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
507 TEST(TimerTest, ContinuationStopStart) { 508 TEST(TimerTest, ContinuationStopStart) {
508 { 509 {
509 ClearAllCallbackHappened(); 510 ClearAllCallbackHappened();
510 base::MessageLoop loop; 511 base::MessageLoop loop;
511 base::Timer timer(false, false); 512 base::Timer timer(false, false);
512 timer.Start(FROM_HERE, TimeDelta::FromMilliseconds(10), 513 timer.Start(FROM_HERE, TimeDelta::FromMilliseconds(10),
513 base::Bind(&SetCallbackHappened1)); 514 base::Bind(&SetCallbackHappened1));
514 timer.Stop(); 515 timer.Stop();
515 timer.Start(FROM_HERE, TimeDelta::FromMilliseconds(40), 516 timer.Start(FROM_HERE, TimeDelta::FromMilliseconds(40),
516 base::Bind(&SetCallbackHappened2)); 517 base::Bind(&SetCallbackHappened2));
517 base::MessageLoop::current()->Run(); 518 base::RunLoop().Run();
518 EXPECT_FALSE(g_callback_happened1); 519 EXPECT_FALSE(g_callback_happened1);
519 EXPECT_TRUE(g_callback_happened2); 520 EXPECT_TRUE(g_callback_happened2);
520 } 521 }
521 } 522 }
522 523
523 TEST(TimerTest, ContinuationReset) { 524 TEST(TimerTest, ContinuationReset) {
524 { 525 {
525 ClearAllCallbackHappened(); 526 ClearAllCallbackHappened();
526 base::MessageLoop loop; 527 base::MessageLoop loop;
527 base::Timer timer(false, false); 528 base::Timer timer(false, false);
528 timer.Start(FROM_HERE, TimeDelta::FromMilliseconds(10), 529 timer.Start(FROM_HERE, TimeDelta::FromMilliseconds(10),
529 base::Bind(&SetCallbackHappened1)); 530 base::Bind(&SetCallbackHappened1));
530 timer.Reset(); 531 timer.Reset();
531 // Since Reset happened before task ran, the user_task must not be cleared: 532 // Since Reset happened before task ran, the user_task must not be cleared:
532 ASSERT_FALSE(timer.user_task().is_null()); 533 ASSERT_FALSE(timer.user_task().is_null());
533 base::MessageLoop::current()->Run(); 534 base::RunLoop().Run();
534 EXPECT_TRUE(g_callback_happened1); 535 EXPECT_TRUE(g_callback_happened1);
535 } 536 }
536 } 537 }
537 538
538 } // namespace 539 } // namespace
OLDNEW
« no previous file with comments | « base/threading/thread.cc ('k') | base/trace_event/trace_event_system_stats_monitor_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698