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

Side by Side Diff: third_party/WebKit/Source/platform/TimerTest.cpp

Issue 2588403002: TestingPlatformSupport: register Platform instance correctly (Closed)
Patch Set: review #32 Created 3 years, 11 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "platform/Timer.h" 5 #include "platform/Timer.h"
6 6
7 #include "platform/scheduler/base/task_queue_impl.h" 7 #include "platform/scheduler/base/task_queue_impl.h"
8 #include "platform/scheduler/child/web_task_runner_impl.h" 8 #include "platform/scheduler/child/web_task_runner_impl.h"
9 #include "platform/scheduler/renderer/renderer_scheduler_impl.h" 9 #include "platform/scheduler/renderer/renderer_scheduler_impl.h"
10 #include "platform/testing/TestingPlatformSupport.h" 10 #include "platform/testing/TestingPlatformSupport.h"
11 #include "public/platform/Platform.h" 11 #include "public/platform/Platform.h"
12 #include "public/platform/WebScheduler.h" 12 #include "public/platform/WebScheduler.h"
13 #include "public/platform/WebThread.h" 13 #include "public/platform/WebThread.h"
14 #include "public/platform/WebViewScheduler.h" 14 #include "public/platform/WebViewScheduler.h"
15 #include "testing/gmock/include/gmock/gmock.h" 15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "wtf/CurrentTime.h" 17 #include "wtf/CurrentTime.h"
18 #include "wtf/PtrUtil.h" 18 #include "wtf/PtrUtil.h"
19 #include "wtf/RefCounted.h" 19 #include "wtf/RefCounted.h"
20 #include <memory> 20 #include <memory>
21 #include <queue> 21 #include <queue>
22 22
23 using testing::ElementsAre; 23 using testing::ElementsAre;
24 24
25 namespace blink { 25 namespace blink {
26 namespace { 26 namespace {
27 27
28 class TimerTest : public testing::Test { 28 class TimerTest : public testing::Test {
29 public: 29 public:
30 void SetUp() override { 30 void initialize(
31 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>&
32 platform) {
31 m_runTimes.clear(); 33 m_runTimes.clear();
32 m_platform.advanceClockSeconds(10.0); 34 platform->advanceClockSeconds(10.0);
33 m_startTime = monotonicallyIncreasingTime(); 35 m_startTime = monotonicallyIncreasingTime();
34 } 36 }
35 37
36 void countingTask(TimerBase*) { 38 void countingTask(TimerBase*) {
37 m_runTimes.push_back(monotonicallyIncreasingTime()); 39 m_runTimes.push_back(monotonicallyIncreasingTime());
38 } 40 }
39 41
40 void recordNextFireTimeTask(TimerBase* timer) { 42 void recordNextFireTimeTask(TimerBase* timer) {
41 m_nextFireTimes.push_back(monotonicallyIncreasingTime() + 43 m_nextFireTimes.push_back(monotonicallyIncreasingTime() +
42 timer->nextFireInterval()); 44 timer->nextFireInterval());
43 } 45 }
44 46
45 void runUntilDeadline(double deadline) { 47 void runUntilDeadline(ScopedTestingPlatformSupport<
48 TestingPlatformSupportWithMockScheduler>& platform,
49 double deadline) {
46 double period = deadline - monotonicallyIncreasingTime(); 50 double period = deadline - monotonicallyIncreasingTime();
47 EXPECT_GE(period, 0.0); 51 EXPECT_GE(period, 0.0);
48 m_platform.runForPeriodSeconds(period); 52 platform->runForPeriodSeconds(period);
49 } 53 }
50 54
51 // Returns false if there are no pending delayed tasks, otherwise sets |time| 55 // Returns false if there are no pending delayed tasks, otherwise sets |time|
52 // to the delay in seconds till the next pending delayed task is scheduled to 56 // to the delay in seconds till the next pending delayed task is scheduled to
53 // fire. 57 // fire.
54 bool timeTillNextDelayedTask(double* time) const { 58 bool timeTillNextDelayedTask(
59 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>&
60 platform,
61 double* time) const {
55 base::TimeTicks nextRunTime; 62 base::TimeTicks nextRunTime;
56 if (!m_platform.rendererScheduler() 63 if (!platform->rendererScheduler()
57 ->TimerTaskRunner() 64 ->TimerTaskRunner()
58 ->GetTimeDomain() 65 ->GetTimeDomain()
59 ->NextScheduledRunTime(&nextRunTime)) 66 ->NextScheduledRunTime(&nextRunTime))
60 return false; 67 return false;
61 *time = (nextRunTime - 68 *time = (nextRunTime -
62 m_platform.rendererScheduler() 69 platform->rendererScheduler()
63 ->TimerTaskRunner() 70 ->TimerTaskRunner()
64 ->GetTimeDomain() 71 ->GetTimeDomain()
65 ->Now()) 72 ->Now())
66 .InSecondsF(); 73 .InSecondsF();
67 return true; 74 return true;
68 } 75 }
69 76
70 protected: 77 protected:
71 double m_startTime; 78 double m_startTime;
72 WTF::Vector<double> m_runTimes; 79 WTF::Vector<double> m_runTimes;
73 WTF::Vector<double> m_nextFireTimes; 80 WTF::Vector<double> m_nextFireTimes;
74 TestingPlatformSupportWithMockScheduler m_platform;
75 }; 81 };
76 82
77 class OnHeapTimerOwner final 83 class OnHeapTimerOwner final
78 : public GarbageCollectedFinalized<OnHeapTimerOwner> { 84 : public GarbageCollectedFinalized<OnHeapTimerOwner> {
79 public: 85 public:
80 class Record final : public RefCounted<Record> { 86 class Record final : public RefCounted<Record> {
81 public: 87 public:
82 static PassRefPtr<Record> create() { return adoptRef(new Record); } 88 static PassRefPtr<Record> create() { return adoptRef(new Record); }
83 89
84 bool timerHasFired() const { return m_timerHasFired; } 90 bool timerHasFired() const { return m_timerHasFired; }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 }; 123 };
118 124
119 class GCForbiddenScope final { 125 class GCForbiddenScope final {
120 public: 126 public:
121 STACK_ALLOCATED(); 127 STACK_ALLOCATED();
122 GCForbiddenScope() { ThreadState::current()->enterGCForbiddenScope(); } 128 GCForbiddenScope() { ThreadState::current()->enterGCForbiddenScope(); }
123 ~GCForbiddenScope() { ThreadState::current()->leaveGCForbiddenScope(); } 129 ~GCForbiddenScope() { ThreadState::current()->leaveGCForbiddenScope(); }
124 }; 130 };
125 131
126 TEST_F(TimerTest, StartOneShot_Zero) { 132 TEST_F(TimerTest, StartOneShot_Zero) {
127 Timer<TimerTest> timer(this, &TimerTest::countingTask); 133 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
128 timer.startOneShot(0, BLINK_FROM_HERE); 134 platform;
129 135 initialize(platform);
130 double runTime; 136
131 EXPECT_FALSE(timeTillNextDelayedTask(&runTime)); 137 Timer<TimerTest> timer(this, &TimerTest::countingTask);
132 138 timer.startOneShot(0, BLINK_FROM_HERE);
133 m_platform.runUntilIdle(); 139
140 double runTime;
141 EXPECT_FALSE(timeTillNextDelayedTask(platform, &runTime));
142
143 platform->runUntilIdle();
134 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); 144 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime));
135 } 145 }
136 146
137 TEST_F(TimerTest, StartOneShot_ZeroAndCancel) { 147 TEST_F(TimerTest, StartOneShot_ZeroAndCancel) {
138 Timer<TimerTest> timer(this, &TimerTest::countingTask); 148 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
139 timer.startOneShot(0, BLINK_FROM_HERE); 149 platform;
140 150 initialize(platform);
141 double runTime; 151
142 EXPECT_FALSE(timeTillNextDelayedTask(&runTime)); 152 Timer<TimerTest> timer(this, &TimerTest::countingTask);
143 153 timer.startOneShot(0, BLINK_FROM_HERE);
144 timer.stop(); 154
145 155 double runTime;
146 m_platform.runUntilIdle(); 156 EXPECT_FALSE(timeTillNextDelayedTask(platform, &runTime));
157
158 timer.stop();
159
160 platform->runUntilIdle();
147 EXPECT_FALSE(m_runTimes.size()); 161 EXPECT_FALSE(m_runTimes.size());
148 } 162 }
149 163
150 TEST_F(TimerTest, StartOneShot_ZeroAndCancelThenRepost) { 164 TEST_F(TimerTest, StartOneShot_ZeroAndCancelThenRepost) {
151 Timer<TimerTest> timer(this, &TimerTest::countingTask); 165 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
152 timer.startOneShot(0, BLINK_FROM_HERE); 166 platform;
153 167 initialize(platform);
154 double runTime; 168
155 EXPECT_FALSE(timeTillNextDelayedTask(&runTime)); 169 Timer<TimerTest> timer(this, &TimerTest::countingTask);
156 170 timer.startOneShot(0, BLINK_FROM_HERE);
157 timer.stop(); 171
158 172 double runTime;
159 m_platform.runUntilIdle(); 173 EXPECT_FALSE(timeTillNextDelayedTask(platform, &runTime));
160 EXPECT_FALSE(m_runTimes.size()); 174
161 175 timer.stop();
162 timer.startOneShot(0, BLINK_FROM_HERE); 176
163 177 platform->runUntilIdle();
164 EXPECT_FALSE(timeTillNextDelayedTask(&runTime)); 178 EXPECT_FALSE(m_runTimes.size());
165 179
166 m_platform.runUntilIdle(); 180 timer.startOneShot(0, BLINK_FROM_HERE);
181
182 EXPECT_FALSE(timeTillNextDelayedTask(platform, &runTime));
183
184 platform->runUntilIdle();
167 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); 185 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime));
168 } 186 }
169 187
170 TEST_F(TimerTest, StartOneShot_Zero_RepostingAfterRunning) { 188 TEST_F(TimerTest, StartOneShot_Zero_RepostingAfterRunning) {
171 Timer<TimerTest> timer(this, &TimerTest::countingTask); 189 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
172 timer.startOneShot(0, BLINK_FROM_HERE); 190 platform;
173 191 initialize(platform);
174 double runTime; 192
175 EXPECT_FALSE(timeTillNextDelayedTask(&runTime)); 193 Timer<TimerTest> timer(this, &TimerTest::countingTask);
176 194 timer.startOneShot(0, BLINK_FROM_HERE);
177 m_platform.runUntilIdle(); 195
196 double runTime;
197 EXPECT_FALSE(timeTillNextDelayedTask(platform, &runTime));
198
199 platform->runUntilIdle();
178 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); 200 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime));
179 201
180 timer.startOneShot(0, BLINK_FROM_HERE); 202 timer.startOneShot(0, BLINK_FROM_HERE);
181 203
182 EXPECT_FALSE(timeTillNextDelayedTask(&runTime)); 204 EXPECT_FALSE(timeTillNextDelayedTask(platform, &runTime));
183 205
184 m_platform.runUntilIdle(); 206 platform->runUntilIdle();
185 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime, m_startTime)); 207 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime, m_startTime));
186 } 208 }
187 209
188 TEST_F(TimerTest, StartOneShot_NonZero) { 210 TEST_F(TimerTest, StartOneShot_NonZero) {
211 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
212 platform;
213 initialize(platform);
214
189 Timer<TimerTest> timer(this, &TimerTest::countingTask); 215 Timer<TimerTest> timer(this, &TimerTest::countingTask);
190 timer.startOneShot(10.0, BLINK_FROM_HERE); 216 timer.startOneShot(10.0, BLINK_FROM_HERE);
191 217
192 double runTime; 218 double runTime;
193 EXPECT_TRUE(timeTillNextDelayedTask(&runTime)); 219 EXPECT_TRUE(timeTillNextDelayedTask(platform, &runTime));
194 EXPECT_FLOAT_EQ(10.0, runTime); 220 EXPECT_FLOAT_EQ(10.0, runTime);
195 221
196 m_platform.runUntilIdle(); 222 platform->runUntilIdle();
197 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); 223 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0));
198 } 224 }
199 225
200 TEST_F(TimerTest, StartOneShot_NonZeroAndCancel) { 226 TEST_F(TimerTest, StartOneShot_NonZeroAndCancel) {
201 Timer<TimerTest> timer(this, &TimerTest::countingTask); 227 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
202 timer.startOneShot(10, BLINK_FROM_HERE); 228 platform;
203 229 initialize(platform);
204 double runTime; 230
205 EXPECT_TRUE(timeTillNextDelayedTask(&runTime)); 231 Timer<TimerTest> timer(this, &TimerTest::countingTask);
206 EXPECT_FLOAT_EQ(10.0, runTime); 232 timer.startOneShot(10, BLINK_FROM_HERE);
207 233
208 timer.stop(); 234 double runTime;
209 EXPECT_TRUE(timeTillNextDelayedTask(&runTime)); 235 EXPECT_TRUE(timeTillNextDelayedTask(platform, &runTime));
210 236 EXPECT_FLOAT_EQ(10.0, runTime);
211 m_platform.runUntilIdle(); 237
238 timer.stop();
239 EXPECT_TRUE(timeTillNextDelayedTask(platform, &runTime));
240
241 platform->runUntilIdle();
212 EXPECT_FALSE(m_runTimes.size()); 242 EXPECT_FALSE(m_runTimes.size());
213 } 243 }
214 244
215 TEST_F(TimerTest, StartOneShot_NonZeroAndCancelThenRepost) { 245 TEST_F(TimerTest, StartOneShot_NonZeroAndCancelThenRepost) {
216 Timer<TimerTest> timer(this, &TimerTest::countingTask); 246 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
217 timer.startOneShot(10, BLINK_FROM_HERE); 247 platform;
218 248 initialize(platform);
219 double runTime; 249
220 EXPECT_TRUE(timeTillNextDelayedTask(&runTime)); 250 Timer<TimerTest> timer(this, &TimerTest::countingTask);
221 EXPECT_FLOAT_EQ(10.0, runTime); 251 timer.startOneShot(10, BLINK_FROM_HERE);
222 252
223 timer.stop(); 253 double runTime;
224 EXPECT_TRUE(timeTillNextDelayedTask(&runTime)); 254 EXPECT_TRUE(timeTillNextDelayedTask(platform, &runTime));
225 255 EXPECT_FLOAT_EQ(10.0, runTime);
226 m_platform.runUntilIdle(); 256
257 timer.stop();
258 EXPECT_TRUE(timeTillNextDelayedTask(platform, &runTime));
259
260 platform->runUntilIdle();
227 EXPECT_FALSE(m_runTimes.size()); 261 EXPECT_FALSE(m_runTimes.size());
228 262
229 double secondPostTime = monotonicallyIncreasingTime(); 263 double secondPostTime = monotonicallyIncreasingTime();
230 timer.startOneShot(10, BLINK_FROM_HERE); 264 timer.startOneShot(10, BLINK_FROM_HERE);
231 265
232 EXPECT_TRUE(timeTillNextDelayedTask(&runTime)); 266 EXPECT_TRUE(timeTillNextDelayedTask(platform, &runTime));
233 EXPECT_FLOAT_EQ(10.0, runTime); 267 EXPECT_FLOAT_EQ(10.0, runTime);
234 268
235 m_platform.runUntilIdle(); 269 platform->runUntilIdle();
236 EXPECT_THAT(m_runTimes, ElementsAre(secondPostTime + 10.0)); 270 EXPECT_THAT(m_runTimes, ElementsAre(secondPostTime + 10.0));
237 } 271 }
238 272
239 TEST_F(TimerTest, StartOneShot_NonZero_RepostingAfterRunning) { 273 TEST_F(TimerTest, StartOneShot_NonZero_RepostingAfterRunning) {
240 Timer<TimerTest> timer(this, &TimerTest::countingTask); 274 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
241 timer.startOneShot(10, BLINK_FROM_HERE); 275 platform;
242 276 initialize(platform);
243 double runTime; 277
244 EXPECT_TRUE(timeTillNextDelayedTask(&runTime)); 278 Timer<TimerTest> timer(this, &TimerTest::countingTask);
245 EXPECT_FLOAT_EQ(10.0, runTime); 279 timer.startOneShot(10, BLINK_FROM_HERE);
246 280
247 m_platform.runUntilIdle(); 281 double runTime;
282 EXPECT_TRUE(timeTillNextDelayedTask(platform, &runTime));
283 EXPECT_FLOAT_EQ(10.0, runTime);
284
285 platform->runUntilIdle();
248 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); 286 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0));
249 287
250 timer.startOneShot(20, BLINK_FROM_HERE); 288 timer.startOneShot(20, BLINK_FROM_HERE);
251 289
252 EXPECT_TRUE(timeTillNextDelayedTask(&runTime)); 290 EXPECT_TRUE(timeTillNextDelayedTask(platform, &runTime));
253 EXPECT_FLOAT_EQ(20.0, runTime); 291 EXPECT_FLOAT_EQ(20.0, runTime);
254 292
255 m_platform.runUntilIdle(); 293 platform->runUntilIdle();
256 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0, m_startTime + 30.0)); 294 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0, m_startTime + 30.0));
257 } 295 }
258 296
259 TEST_F(TimerTest, PostingTimerTwiceWithSameRunTimeDoesNothing) { 297 TEST_F(TimerTest, PostingTimerTwiceWithSameRunTimeDoesNothing) {
260 Timer<TimerTest> timer(this, &TimerTest::countingTask); 298 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
261 timer.startOneShot(10, BLINK_FROM_HERE); 299 platform;
262 timer.startOneShot(10, BLINK_FROM_HERE); 300 initialize(platform);
263 301
264 double runTime; 302 Timer<TimerTest> timer(this, &TimerTest::countingTask);
265 EXPECT_TRUE(timeTillNextDelayedTask(&runTime)); 303 timer.startOneShot(10, BLINK_FROM_HERE);
266 EXPECT_FLOAT_EQ(10.0, runTime); 304 timer.startOneShot(10, BLINK_FROM_HERE);
267 305
268 m_platform.runUntilIdle(); 306 double runTime;
307 EXPECT_TRUE(timeTillNextDelayedTask(platform, &runTime));
308 EXPECT_FLOAT_EQ(10.0, runTime);
309
310 platform->runUntilIdle();
269 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); 311 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0));
270 } 312 }
271 313
272 TEST_F(TimerTest, PostingTimerTwiceWithNewerRunTimeCancelsOriginalTask) { 314 TEST_F(TimerTest, PostingTimerTwiceWithNewerRunTimeCancelsOriginalTask) {
273 Timer<TimerTest> timer(this, &TimerTest::countingTask); 315 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
274 timer.startOneShot(10, BLINK_FROM_HERE); 316 platform;
275 timer.startOneShot(0, BLINK_FROM_HERE); 317 initialize(platform);
276 318
277 m_platform.runUntilIdle(); 319 Timer<TimerTest> timer(this, &TimerTest::countingTask);
320 timer.startOneShot(10, BLINK_FROM_HERE);
321 timer.startOneShot(0, BLINK_FROM_HERE);
322
323 platform->runUntilIdle();
278 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 0.0)); 324 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 0.0));
279 } 325 }
280 326
281 TEST_F(TimerTest, PostingTimerTwiceWithLaterRunTimeCancelsOriginalTask) { 327 TEST_F(TimerTest, PostingTimerTwiceWithLaterRunTimeCancelsOriginalTask) {
282 Timer<TimerTest> timer(this, &TimerTest::countingTask); 328 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
283 timer.startOneShot(0, BLINK_FROM_HERE); 329 platform;
284 timer.startOneShot(10, BLINK_FROM_HERE); 330 initialize(platform);
285 331
286 m_platform.runUntilIdle(); 332 Timer<TimerTest> timer(this, &TimerTest::countingTask);
333 timer.startOneShot(0, BLINK_FROM_HERE);
334 timer.startOneShot(10, BLINK_FROM_HERE);
335
336 platform->runUntilIdle();
287 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); 337 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0));
288 } 338 }
289 339
290 TEST_F(TimerTest, StartRepeatingTask) { 340 TEST_F(TimerTest, StartRepeatingTask) {
341 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
342 platform;
343 initialize(platform);
344
291 Timer<TimerTest> timer(this, &TimerTest::countingTask); 345 Timer<TimerTest> timer(this, &TimerTest::countingTask);
292 timer.startRepeating(1.0, BLINK_FROM_HERE); 346 timer.startRepeating(1.0, BLINK_FROM_HERE);
293 347
294 double runTime; 348 double runTime;
295 EXPECT_TRUE(timeTillNextDelayedTask(&runTime)); 349 EXPECT_TRUE(timeTillNextDelayedTask(platform, &runTime));
296 EXPECT_FLOAT_EQ(1.0, runTime); 350 EXPECT_FLOAT_EQ(1.0, runTime);
297 351
298 runUntilDeadline(m_startTime + 5.5); 352 runUntilDeadline(platform, m_startTime + 5.5);
299 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0, 353 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0,
300 m_startTime + 3.0, m_startTime + 4.0, 354 m_startTime + 3.0, m_startTime + 4.0,
301 m_startTime + 5.0)); 355 m_startTime + 5.0));
302 } 356 }
303 357
304 TEST_F(TimerTest, StartRepeatingTask_ThenCancel) { 358 TEST_F(TimerTest, StartRepeatingTask_ThenCancel) {
359 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
360 platform;
361 initialize(platform);
362
305 Timer<TimerTest> timer(this, &TimerTest::countingTask); 363 Timer<TimerTest> timer(this, &TimerTest::countingTask);
306 timer.startRepeating(1.0, BLINK_FROM_HERE); 364 timer.startRepeating(1.0, BLINK_FROM_HERE);
307 365
308 double runTime; 366 double runTime;
309 EXPECT_TRUE(timeTillNextDelayedTask(&runTime)); 367 EXPECT_TRUE(timeTillNextDelayedTask(platform, &runTime));
310 EXPECT_FLOAT_EQ(1.0, runTime); 368 EXPECT_FLOAT_EQ(1.0, runTime);
311 369
312 runUntilDeadline(m_startTime + 2.5); 370 runUntilDeadline(platform, m_startTime + 2.5);
313 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0)); 371 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0));
314 372
315 timer.stop(); 373 timer.stop();
316 m_platform.runUntilIdle(); 374 platform->runUntilIdle();
317 375
318 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0)); 376 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0));
319 } 377 }
320 378
321 TEST_F(TimerTest, StartRepeatingTask_ThenPostOneShot) { 379 TEST_F(TimerTest, StartRepeatingTask_ThenPostOneShot) {
380 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
381 platform;
382 initialize(platform);
383
322 Timer<TimerTest> timer(this, &TimerTest::countingTask); 384 Timer<TimerTest> timer(this, &TimerTest::countingTask);
323 timer.startRepeating(1.0, BLINK_FROM_HERE); 385 timer.startRepeating(1.0, BLINK_FROM_HERE);
324 386
325 double runTime; 387 double runTime;
326 EXPECT_TRUE(timeTillNextDelayedTask(&runTime)); 388 EXPECT_TRUE(timeTillNextDelayedTask(platform, &runTime));
327 EXPECT_FLOAT_EQ(1.0, runTime); 389 EXPECT_FLOAT_EQ(1.0, runTime);
328 390
329 runUntilDeadline(m_startTime + 2.5); 391 runUntilDeadline(platform, m_startTime + 2.5);
330 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0)); 392 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0));
331 393
332 timer.startOneShot(0, BLINK_FROM_HERE); 394 timer.startOneShot(0, BLINK_FROM_HERE);
333 m_platform.runUntilIdle(); 395 platform->runUntilIdle();
334 396
335 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0, 397 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0,
336 m_startTime + 2.5)); 398 m_startTime + 2.5));
337 } 399 }
338 400
339 TEST_F(TimerTest, IsActive_NeverPosted) { 401 TEST_F(TimerTest, IsActive_NeverPosted) {
402 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
403 platform;
404 initialize(platform);
405
340 Timer<TimerTest> timer(this, &TimerTest::countingTask); 406 Timer<TimerTest> timer(this, &TimerTest::countingTask);
341 407
342 EXPECT_FALSE(timer.isActive()); 408 EXPECT_FALSE(timer.isActive());
343 } 409 }
344 410
345 TEST_F(TimerTest, IsActive_AfterPosting_OneShotZero) { 411 TEST_F(TimerTest, IsActive_AfterPosting_OneShotZero) {
346 Timer<TimerTest> timer(this, &TimerTest::countingTask); 412 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
347 timer.startOneShot(0, BLINK_FROM_HERE); 413 platform;
348 414 initialize(platform);
415
416 Timer<TimerTest> timer(this, &TimerTest::countingTask);
417 timer.startOneShot(0, BLINK_FROM_HERE);
418
349 EXPECT_TRUE(timer.isActive()); 419 EXPECT_TRUE(timer.isActive());
350 } 420 }
351 421
352 TEST_F(TimerTest, IsActive_AfterPosting_OneShotNonZero) { 422 TEST_F(TimerTest, IsActive_AfterPosting_OneShotNonZero) {
423 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
424 platform;
425 initialize(platform);
426
353 Timer<TimerTest> timer(this, &TimerTest::countingTask); 427 Timer<TimerTest> timer(this, &TimerTest::countingTask);
354 timer.startOneShot(10, BLINK_FROM_HERE); 428 timer.startOneShot(10, BLINK_FROM_HERE);
355 429
356 EXPECT_TRUE(timer.isActive()); 430 EXPECT_TRUE(timer.isActive());
357 } 431 }
358 432
359 TEST_F(TimerTest, IsActive_AfterPosting_Repeating) { 433 TEST_F(TimerTest, IsActive_AfterPosting_Repeating) {
434 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
435 platform;
436 initialize(platform);
437
360 Timer<TimerTest> timer(this, &TimerTest::countingTask); 438 Timer<TimerTest> timer(this, &TimerTest::countingTask);
361 timer.startRepeating(1.0, BLINK_FROM_HERE); 439 timer.startRepeating(1.0, BLINK_FROM_HERE);
362 440
363 EXPECT_TRUE(timer.isActive()); 441 EXPECT_TRUE(timer.isActive());
364 } 442 }
365 443
366 TEST_F(TimerTest, IsActive_AfterRunning_OneShotZero) { 444 TEST_F(TimerTest, IsActive_AfterRunning_OneShotZero) {
445 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
446 platform;
447 initialize(platform);
448
367 Timer<TimerTest> timer(this, &TimerTest::countingTask); 449 Timer<TimerTest> timer(this, &TimerTest::countingTask);
368 timer.startOneShot(0, BLINK_FROM_HERE); 450 timer.startOneShot(0, BLINK_FROM_HERE);
369 451
370 m_platform.runUntilIdle(); 452 platform->runUntilIdle();
371 EXPECT_FALSE(timer.isActive()); 453 EXPECT_FALSE(timer.isActive());
372 } 454 }
373 455
374 TEST_F(TimerTest, IsActive_AfterRunning_OneShotNonZero) { 456 TEST_F(TimerTest, IsActive_AfterRunning_OneShotNonZero) {
457 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
458 platform;
459 initialize(platform);
460
375 Timer<TimerTest> timer(this, &TimerTest::countingTask); 461 Timer<TimerTest> timer(this, &TimerTest::countingTask);
376 timer.startOneShot(10, BLINK_FROM_HERE); 462 timer.startOneShot(10, BLINK_FROM_HERE);
377 463
378 m_platform.runUntilIdle(); 464 platform->runUntilIdle();
379 EXPECT_FALSE(timer.isActive()); 465 EXPECT_FALSE(timer.isActive());
380 } 466 }
381 467
382 TEST_F(TimerTest, IsActive_AfterRunning_Repeating) { 468 TEST_F(TimerTest, IsActive_AfterRunning_Repeating) {
469 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
470 platform;
471 initialize(platform);
472
383 Timer<TimerTest> timer(this, &TimerTest::countingTask); 473 Timer<TimerTest> timer(this, &TimerTest::countingTask);
384 timer.startRepeating(1.0, BLINK_FROM_HERE); 474 timer.startRepeating(1.0, BLINK_FROM_HERE);
385 475
386 runUntilDeadline(m_startTime + 10); 476 runUntilDeadline(platform, m_startTime + 10);
387 EXPECT_TRUE(timer.isActive()); // It should run until cancelled. 477 EXPECT_TRUE(timer.isActive()); // It should run until cancelled.
388 } 478 }
389 479
390 TEST_F(TimerTest, NextFireInterval_OneShotZero) { 480 TEST_F(TimerTest, NextFireInterval_OneShotZero) {
481 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
482 platform;
483 initialize(platform);
484
391 Timer<TimerTest> timer(this, &TimerTest::countingTask); 485 Timer<TimerTest> timer(this, &TimerTest::countingTask);
392 timer.startOneShot(0, BLINK_FROM_HERE); 486 timer.startOneShot(0, BLINK_FROM_HERE);
393 487
394 EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval()); 488 EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval());
395 } 489 }
396 490
397 TEST_F(TimerTest, NextFireInterval_OneShotNonZero) { 491 TEST_F(TimerTest, NextFireInterval_OneShotNonZero) {
492 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
493 platform;
494 initialize(platform);
495
398 Timer<TimerTest> timer(this, &TimerTest::countingTask); 496 Timer<TimerTest> timer(this, &TimerTest::countingTask);
399 timer.startOneShot(10, BLINK_FROM_HERE); 497 timer.startOneShot(10, BLINK_FROM_HERE);
400 498
401 EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval()); 499 EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval());
402 } 500 }
403 501
404 TEST_F(TimerTest, NextFireInterval_OneShotNonZero_AfterAFewSeconds) { 502 TEST_F(TimerTest, NextFireInterval_OneShotNonZero_AfterAFewSeconds) {
405 m_platform.setAutoAdvanceNowToPendingTasks(false); 503 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
504 platform;
505 initialize(platform);
506
507 platform->setAutoAdvanceNowToPendingTasks(false);
406 508
407 Timer<TimerTest> timer(this, &TimerTest::countingTask); 509 Timer<TimerTest> timer(this, &TimerTest::countingTask);
408 timer.startOneShot(10, BLINK_FROM_HERE); 510 timer.startOneShot(10, BLINK_FROM_HERE);
409 511
410 m_platform.advanceClockSeconds(2.0); 512 platform->advanceClockSeconds(2.0);
411 EXPECT_FLOAT_EQ(8.0, timer.nextFireInterval()); 513 EXPECT_FLOAT_EQ(8.0, timer.nextFireInterval());
412 } 514 }
413 515
414 TEST_F(TimerTest, NextFireInterval_Repeating) { 516 TEST_F(TimerTest, NextFireInterval_Repeating) {
517 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
518 platform;
519 initialize(platform);
520
415 Timer<TimerTest> timer(this, &TimerTest::countingTask); 521 Timer<TimerTest> timer(this, &TimerTest::countingTask);
416 timer.startRepeating(20, BLINK_FROM_HERE); 522 timer.startRepeating(20, BLINK_FROM_HERE);
417 523
418 EXPECT_FLOAT_EQ(20.0, timer.nextFireInterval()); 524 EXPECT_FLOAT_EQ(20.0, timer.nextFireInterval());
419 } 525 }
420 526
421 TEST_F(TimerTest, RepeatInterval_NeverStarted) { 527 TEST_F(TimerTest, RepeatInterval_NeverStarted) {
528 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
529 platform;
530 initialize(platform);
531
422 Timer<TimerTest> timer(this, &TimerTest::countingTask); 532 Timer<TimerTest> timer(this, &TimerTest::countingTask);
423 533
424 EXPECT_FLOAT_EQ(0.0, timer.repeatInterval()); 534 EXPECT_FLOAT_EQ(0.0, timer.repeatInterval());
425 } 535 }
426 536
427 TEST_F(TimerTest, RepeatInterval_OneShotZero) { 537 TEST_F(TimerTest, RepeatInterval_OneShotZero) {
538 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
539 platform;
540 initialize(platform);
541
428 Timer<TimerTest> timer(this, &TimerTest::countingTask); 542 Timer<TimerTest> timer(this, &TimerTest::countingTask);
429 timer.startOneShot(0, BLINK_FROM_HERE); 543 timer.startOneShot(0, BLINK_FROM_HERE);
430 544
431 EXPECT_FLOAT_EQ(0.0, timer.repeatInterval()); 545 EXPECT_FLOAT_EQ(0.0, timer.repeatInterval());
432 } 546 }
433 547
434 TEST_F(TimerTest, RepeatInterval_OneShotNonZero) { 548 TEST_F(TimerTest, RepeatInterval_OneShotNonZero) {
549 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
550 platform;
551 initialize(platform);
552
435 Timer<TimerTest> timer(this, &TimerTest::countingTask); 553 Timer<TimerTest> timer(this, &TimerTest::countingTask);
436 timer.startOneShot(10, BLINK_FROM_HERE); 554 timer.startOneShot(10, BLINK_FROM_HERE);
437 555
438 EXPECT_FLOAT_EQ(0.0, timer.repeatInterval()); 556 EXPECT_FLOAT_EQ(0.0, timer.repeatInterval());
439 } 557 }
440 558
441 TEST_F(TimerTest, RepeatInterval_Repeating) { 559 TEST_F(TimerTest, RepeatInterval_Repeating) {
560 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
561 platform;
562 initialize(platform);
563
442 Timer<TimerTest> timer(this, &TimerTest::countingTask); 564 Timer<TimerTest> timer(this, &TimerTest::countingTask);
443 timer.startRepeating(20, BLINK_FROM_HERE); 565 timer.startRepeating(20, BLINK_FROM_HERE);
444 566
445 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval()); 567 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval());
446 } 568 }
447 569
448 TEST_F(TimerTest, AugmentRepeatInterval) { 570 TEST_F(TimerTest, AugmentRepeatInterval) {
571 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
572 platform;
573 initialize(platform);
574
449 Timer<TimerTest> timer(this, &TimerTest::countingTask); 575 Timer<TimerTest> timer(this, &TimerTest::countingTask);
450 timer.startRepeating(10, BLINK_FROM_HERE); 576 timer.startRepeating(10, BLINK_FROM_HERE);
451 EXPECT_FLOAT_EQ(10.0, timer.repeatInterval()); 577 EXPECT_FLOAT_EQ(10.0, timer.repeatInterval());
452 EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval()); 578 EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval());
453 579
454 m_platform.advanceClockSeconds(2.0); 580 platform->advanceClockSeconds(2.0);
455 timer.augmentRepeatInterval(10); 581 timer.augmentRepeatInterval(10);
456 582
457 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval()); 583 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval());
458 EXPECT_FLOAT_EQ(18.0, timer.nextFireInterval()); 584 EXPECT_FLOAT_EQ(18.0, timer.nextFireInterval());
459 585
460 // NOTE setAutoAdvanceNowToPendingTasks(true) (which uses 586 // NOTE setAutoAdvanceNowToPendingTasks(true) (which uses
461 // cc::OrderedSimpleTaskRunner) results in somewhat strange behavior of the 587 // cc::OrderedSimpleTaskRunner) results in somewhat strange behavior of the
462 // test clock which breaks this test. Specifically the test clock advancing 588 // test clock which breaks this test. Specifically the test clock advancing
463 // logic ignores newly posted delayed tasks and advances too far. 589 // logic ignores newly posted delayed tasks and advances too far.
464 runUntilDeadline(m_startTime + 50.0); 590 runUntilDeadline(platform, m_startTime + 50.0);
465 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 20.0, m_startTime + 40.0)); 591 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 20.0, m_startTime + 40.0));
466 } 592 }
467 593
468 TEST_F(TimerTest, AugmentRepeatInterval_TimerFireDelayed) { 594 TEST_F(TimerTest, AugmentRepeatInterval_TimerFireDelayed) {
469 m_platform.setAutoAdvanceNowToPendingTasks(false); 595 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
596 platform;
597 initialize(platform);
598
599 platform->setAutoAdvanceNowToPendingTasks(false);
470 600
471 Timer<TimerTest> timer(this, &TimerTest::countingTask); 601 Timer<TimerTest> timer(this, &TimerTest::countingTask);
472 timer.startRepeating(10, BLINK_FROM_HERE); 602 timer.startRepeating(10, BLINK_FROM_HERE);
473 EXPECT_FLOAT_EQ(10.0, timer.repeatInterval()); 603 EXPECT_FLOAT_EQ(10.0, timer.repeatInterval());
474 EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval()); 604 EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval());
475 605
476 m_platform.advanceClockSeconds(123.0); // Make the timer long overdue. 606 platform->advanceClockSeconds(123.0); // Make the timer long overdue.
477 timer.augmentRepeatInterval(10); 607 timer.augmentRepeatInterval(10);
478 608
479 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval()); 609 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval());
480 // The timer is overdue so it should be scheduled to fire immediatly. 610 // The timer is overdue so it should be scheduled to fire immediatly.
481 EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval()); 611 EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval());
482 } 612 }
483 613
484 TEST_F(TimerTest, RepeatingTimerDoesNotDrift) { 614 TEST_F(TimerTest, RepeatingTimerDoesNotDrift) {
485 m_platform.setAutoAdvanceNowToPendingTasks(false); 615 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
616 platform;
617 initialize(platform);
618
619 platform->setAutoAdvanceNowToPendingTasks(false);
486 620
487 Timer<TimerTest> timer(this, &TimerTest::recordNextFireTimeTask); 621 Timer<TimerTest> timer(this, &TimerTest::recordNextFireTimeTask);
488 timer.startRepeating(2.0, BLINK_FROM_HERE); 622 timer.startRepeating(2.0, BLINK_FROM_HERE);
489 623
490 recordNextFireTimeTask( 624 recordNextFireTimeTask(
491 &timer); // Next scheduled task to run at m_startTime + 2.0 625 &timer); // Next scheduled task to run at m_startTime + 2.0
492 626
493 // Simulate timer firing early. Next scheduled task to run at 627 // Simulate timer firing early. Next scheduled task to run at
494 // m_startTime + 4.0 628 // m_startTime + 4.0
495 m_platform.advanceClockSeconds(1.9); 629 platform->advanceClockSeconds(1.9);
496 runUntilDeadline(monotonicallyIncreasingTime() + 0.2); 630 runUntilDeadline(platform, monotonicallyIncreasingTime() + 0.2);
497 631
498 // Next scheduled task to run at m_startTime + 6.0 632 // Next scheduled task to run at m_startTime + 6.0
499 m_platform.runForPeriodSeconds(2.0); 633 platform->runForPeriodSeconds(2.0);
500 // Next scheduled task to run at m_startTime + 8.0 634 // Next scheduled task to run at m_startTime + 8.0
501 m_platform.runForPeriodSeconds(2.1); 635 platform->runForPeriodSeconds(2.1);
502 // Next scheduled task to run at m_startTime + 10.0 636 // Next scheduled task to run at m_startTime + 10.0
503 m_platform.runForPeriodSeconds(2.9); 637 platform->runForPeriodSeconds(2.9);
504 // Next scheduled task to run at m_startTime + 14.0 (skips a beat) 638 // Next scheduled task to run at m_startTime + 14.0 (skips a beat)
505 m_platform.advanceClockSeconds(3.1); 639 platform->advanceClockSeconds(3.1);
506 m_platform.runUntilIdle(); 640 platform->runUntilIdle();
507 // Next scheduled task to run at m_startTime + 18.0 (skips a beat) 641 // Next scheduled task to run at m_startTime + 18.0 (skips a beat)
508 m_platform.advanceClockSeconds(4.0); 642 platform->advanceClockSeconds(4.0);
509 m_platform.runUntilIdle(); 643 platform->runUntilIdle();
510 // Next scheduled task to run at m_startTime + 28.0 (skips 5 beats) 644 // Next scheduled task to run at m_startTime + 28.0 (skips 5 beats)
511 m_platform.advanceClockSeconds(10.0); 645 platform->advanceClockSeconds(10.0);
512 m_platform.runUntilIdle(); 646 platform->runUntilIdle();
513 647
514 EXPECT_THAT( 648 EXPECT_THAT(
515 m_nextFireTimes, 649 m_nextFireTimes,
516 ElementsAre(m_startTime + 2.0, m_startTime + 4.0, m_startTime + 6.0, 650 ElementsAre(m_startTime + 2.0, m_startTime + 4.0, m_startTime + 6.0,
517 m_startTime + 8.0, m_startTime + 10.0, m_startTime + 14.0, 651 m_startTime + 8.0, m_startTime + 10.0, m_startTime + 14.0,
518 m_startTime + 18.0, m_startTime + 28.0)); 652 m_startTime + 18.0, m_startTime + 28.0));
519 } 653 }
520 654
521 template <typename TimerFiredClass> 655 template <typename TimerFiredClass>
522 class TimerForTest : public TaskRunnerTimer<TimerFiredClass> { 656 class TimerForTest : public TaskRunnerTimer<TimerFiredClass> {
523 public: 657 public:
524 using TimerFiredFunction = 658 using TimerFiredFunction =
525 typename TaskRunnerTimer<TimerFiredClass>::TimerFiredFunction; 659 typename TaskRunnerTimer<TimerFiredClass>::TimerFiredFunction;
526 660
527 ~TimerForTest() override {} 661 ~TimerForTest() override {}
528 662
529 TimerForTest(RefPtr<WebTaskRunner> webTaskRunner, 663 TimerForTest(RefPtr<WebTaskRunner> webTaskRunner,
530 TimerFiredClass* timerFiredClass, 664 TimerFiredClass* timerFiredClass,
531 TimerFiredFunction timerFiredFunction) 665 TimerFiredFunction timerFiredFunction)
532 : TaskRunnerTimer<TimerFiredClass>(std::move(webTaskRunner), 666 : TaskRunnerTimer<TimerFiredClass>(std::move(webTaskRunner),
533 timerFiredClass, 667 timerFiredClass,
534 timerFiredFunction) {} 668 timerFiredFunction) {}
535 }; 669 };
536 670
537 TEST_F(TimerTest, UserSuppliedWebTaskRunner) { 671 TEST_F(TimerTest, UserSuppliedWebTaskRunner) {
672 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
673 platform;
674 initialize(platform);
675
538 scoped_refptr<scheduler::TaskQueue> taskRunner( 676 scoped_refptr<scheduler::TaskQueue> taskRunner(
539 m_platform.rendererScheduler()->NewTimerTaskRunner( 677 platform->rendererScheduler()->NewTimerTaskRunner(
540 scheduler::TaskQueue::QueueType::TEST)); 678 scheduler::TaskQueue::QueueType::TEST));
541 RefPtr<scheduler::WebTaskRunnerImpl> webTaskRunner = 679 RefPtr<scheduler::WebTaskRunnerImpl> webTaskRunner =
542 scheduler::WebTaskRunnerImpl::create(taskRunner); 680 scheduler::WebTaskRunnerImpl::create(taskRunner);
543 TimerForTest<TimerTest> timer(webTaskRunner, this, &TimerTest::countingTask); 681 TimerForTest<TimerTest> timer(webTaskRunner, this, &TimerTest::countingTask);
544 timer.startOneShot(0, BLINK_FROM_HERE); 682 timer.startOneShot(0, BLINK_FROM_HERE);
545 683
546 // Make sure the task was posted on taskRunner. 684 // Make sure the task was posted on taskRunner.
547 EXPECT_FALSE(taskRunner->IsEmpty()); 685 EXPECT_FALSE(taskRunner->IsEmpty());
548 } 686 }
549 687
550 TEST_F(TimerTest, RunOnHeapTimer) { 688 TEST_F(TimerTest, RunOnHeapTimer) {
689 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
690 platform;
691 initialize(platform);
692
551 RefPtr<OnHeapTimerOwner::Record> record = OnHeapTimerOwner::Record::create(); 693 RefPtr<OnHeapTimerOwner::Record> record = OnHeapTimerOwner::Record::create();
552 Persistent<OnHeapTimerOwner> owner = new OnHeapTimerOwner(record); 694 Persistent<OnHeapTimerOwner> owner = new OnHeapTimerOwner(record);
553 695
554 owner->startOneShot(0, BLINK_FROM_HERE); 696 owner->startOneShot(0, BLINK_FROM_HERE);
555 697
556 EXPECT_FALSE(record->timerHasFired()); 698 EXPECT_FALSE(record->timerHasFired());
557 m_platform.runUntilIdle(); 699 platform->runUntilIdle();
558 EXPECT_TRUE(record->timerHasFired()); 700 EXPECT_TRUE(record->timerHasFired());
559 } 701 }
560 702
561 TEST_F(TimerTest, DestructOnHeapTimer) { 703 TEST_F(TimerTest, DestructOnHeapTimer) {
704 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
705 platform;
706 initialize(platform);
707
562 RefPtr<OnHeapTimerOwner::Record> record = OnHeapTimerOwner::Record::create(); 708 RefPtr<OnHeapTimerOwner::Record> record = OnHeapTimerOwner::Record::create();
563 Persistent<OnHeapTimerOwner> owner = new OnHeapTimerOwner(record); 709 Persistent<OnHeapTimerOwner> owner = new OnHeapTimerOwner(record);
564 710
565 record->dispose(); 711 record->dispose();
566 owner->startOneShot(0, BLINK_FROM_HERE); 712 owner->startOneShot(0, BLINK_FROM_HERE);
567 713
568 owner = nullptr; 714 owner = nullptr;
569 ThreadState::current()->collectGarbage( 715 ThreadState::current()->collectGarbage(
570 BlinkGC::NoHeapPointersOnStack, BlinkGC::GCWithSweep, BlinkGC::ForcedGC); 716 BlinkGC::NoHeapPointersOnStack, BlinkGC::GCWithSweep, BlinkGC::ForcedGC);
571 EXPECT_TRUE(record->ownerIsDestructed()); 717 EXPECT_TRUE(record->ownerIsDestructed());
572 718
573 EXPECT_FALSE(record->timerHasFired()); 719 EXPECT_FALSE(record->timerHasFired());
574 m_platform.runUntilIdle(); 720 platform->runUntilIdle();
575 EXPECT_FALSE(record->timerHasFired()); 721 EXPECT_FALSE(record->timerHasFired());
576 } 722 }
577 723
578 TEST_F(TimerTest, MarkOnHeapTimerAsUnreachable) { 724 TEST_F(TimerTest, MarkOnHeapTimerAsUnreachable) {
725 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
726 platform;
727 initialize(platform);
728
579 RefPtr<OnHeapTimerOwner::Record> record = OnHeapTimerOwner::Record::create(); 729 RefPtr<OnHeapTimerOwner::Record> record = OnHeapTimerOwner::Record::create();
580 Persistent<OnHeapTimerOwner> owner = new OnHeapTimerOwner(record); 730 Persistent<OnHeapTimerOwner> owner = new OnHeapTimerOwner(record);
581 731
582 record->dispose(); 732 record->dispose();
583 owner->startOneShot(0, BLINK_FROM_HERE); 733 owner->startOneShot(0, BLINK_FROM_HERE);
584 734
585 owner = nullptr; 735 owner = nullptr;
586 ThreadState::current()->collectGarbage(BlinkGC::NoHeapPointersOnStack, 736 ThreadState::current()->collectGarbage(BlinkGC::NoHeapPointersOnStack,
587 BlinkGC::GCWithoutSweep, 737 BlinkGC::GCWithoutSweep,
588 BlinkGC::ForcedGC); 738 BlinkGC::ForcedGC);
589 EXPECT_FALSE(record->ownerIsDestructed()); 739 EXPECT_FALSE(record->ownerIsDestructed());
590 740
591 { 741 {
592 GCForbiddenScope scope; 742 GCForbiddenScope scope;
593 EXPECT_FALSE(record->timerHasFired()); 743 EXPECT_FALSE(record->timerHasFired());
594 m_platform.runUntilIdle(); 744 platform->runUntilIdle();
595 EXPECT_FALSE(record->timerHasFired()); 745 EXPECT_FALSE(record->timerHasFired());
596 EXPECT_FALSE(record->ownerIsDestructed()); 746 EXPECT_FALSE(record->ownerIsDestructed());
597 } 747 }
598 } 748 }
599 749
600 namespace { 750 namespace {
601 751
602 class TaskObserver : public base::MessageLoop::TaskObserver { 752 class TaskObserver : public base::MessageLoop::TaskObserver {
603 public: 753 public:
604 TaskObserver(RefPtr<WebTaskRunner> task_runner, 754 TaskObserver(RefPtr<WebTaskRunner> task_runner,
605 std::vector<RefPtr<WebTaskRunner>>* runOrder) 755 std::vector<RefPtr<WebTaskRunner>>* runOrder)
606 : m_taskRunner(std::move(task_runner)), m_runOrder(runOrder) {} 756 : m_taskRunner(std::move(task_runner)), m_runOrder(runOrder) {}
607 757
608 void WillProcessTask(const base::PendingTask&) {} 758 void WillProcessTask(const base::PendingTask&) {}
609 759
610 void DidProcessTask(const base::PendingTask&) { 760 void DidProcessTask(const base::PendingTask&) {
611 m_runOrder->push_back(m_taskRunner); 761 m_runOrder->push_back(m_taskRunner);
612 } 762 }
613 763
614 private: 764 private:
615 RefPtr<WebTaskRunner> m_taskRunner; 765 RefPtr<WebTaskRunner> m_taskRunner;
616 std::vector<RefPtr<WebTaskRunner>>* m_runOrder; 766 std::vector<RefPtr<WebTaskRunner>>* m_runOrder;
617 }; 767 };
618 768
619 } // namespace 769 } // namespace
620 770
621 TEST_F(TimerTest, MoveToNewTaskRunnerOneShot) { 771 TEST_F(TimerTest, MoveToNewTaskRunnerOneShot) {
772 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
773 platform;
774 initialize(platform);
775
622 std::vector<RefPtr<WebTaskRunner>> runOrder; 776 std::vector<RefPtr<WebTaskRunner>> runOrder;
623 777
624 scoped_refptr<scheduler::TaskQueue> taskRunner1( 778 scoped_refptr<scheduler::TaskQueue> taskRunner1(
625 m_platform.rendererScheduler()->NewTimerTaskRunner( 779 platform->rendererScheduler()->NewTimerTaskRunner(
626 scheduler::TaskQueue::QueueType::TEST)); 780 scheduler::TaskQueue::QueueType::TEST));
627 RefPtr<scheduler::WebTaskRunnerImpl> webTaskRunner1 = 781 RefPtr<scheduler::WebTaskRunnerImpl> webTaskRunner1 =
628 scheduler::WebTaskRunnerImpl::create(taskRunner1); 782 scheduler::WebTaskRunnerImpl::create(taskRunner1);
629 TaskObserver taskObserver1(webTaskRunner1, &runOrder); 783 TaskObserver taskObserver1(webTaskRunner1, &runOrder);
630 taskRunner1->AddTaskObserver(&taskObserver1); 784 taskRunner1->AddTaskObserver(&taskObserver1);
631 785
632 scoped_refptr<scheduler::TaskQueue> taskRunner2( 786 scoped_refptr<scheduler::TaskQueue> taskRunner2(
633 m_platform.rendererScheduler()->NewTimerTaskRunner( 787 platform->rendererScheduler()->NewTimerTaskRunner(
634 scheduler::TaskQueue::QueueType::TEST)); 788 scheduler::TaskQueue::QueueType::TEST));
635 RefPtr<scheduler::WebTaskRunnerImpl> webTaskRunner2 = 789 RefPtr<scheduler::WebTaskRunnerImpl> webTaskRunner2 =
636 scheduler::WebTaskRunnerImpl::create(taskRunner2); 790 scheduler::WebTaskRunnerImpl::create(taskRunner2);
637 TaskObserver taskObserver2(webTaskRunner2, &runOrder); 791 TaskObserver taskObserver2(webTaskRunner2, &runOrder);
638 taskRunner2->AddTaskObserver(&taskObserver2); 792 taskRunner2->AddTaskObserver(&taskObserver2);
639 793
640 TimerForTest<TimerTest> timer(webTaskRunner1, this, &TimerTest::countingTask); 794 TimerForTest<TimerTest> timer(webTaskRunner1, this, &TimerTest::countingTask);
641 795
642 double startTime = monotonicallyIncreasingTime(); 796 double startTime = monotonicallyIncreasingTime();
643 797
644 timer.startOneShot(1, BLINK_FROM_HERE); 798 timer.startOneShot(1, BLINK_FROM_HERE);
645 799
646 m_platform.runForPeriodSeconds(0.5); 800 platform->runForPeriodSeconds(0.5);
647 801
648 timer.moveToNewTaskRunner(webTaskRunner2); 802 timer.moveToNewTaskRunner(webTaskRunner2);
649 803
650 m_platform.runUntilIdle(); 804 platform->runUntilIdle();
651 805
652 EXPECT_THAT(m_runTimes, ElementsAre(startTime + 1.0)); 806 EXPECT_THAT(m_runTimes, ElementsAre(startTime + 1.0));
653 807
654 EXPECT_THAT(runOrder, ElementsAre(webTaskRunner2)); 808 EXPECT_THAT(runOrder, ElementsAre(webTaskRunner2));
655 809
656 EXPECT_TRUE(taskRunner1->IsEmpty()); 810 EXPECT_TRUE(taskRunner1->IsEmpty());
657 EXPECT_TRUE(taskRunner2->IsEmpty()); 811 EXPECT_TRUE(taskRunner2->IsEmpty());
658 } 812 }
659 813
660 TEST_F(TimerTest, MoveToNewTaskRunnerRepeating) { 814 TEST_F(TimerTest, MoveToNewTaskRunnerRepeating) {
815 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
816 platform;
817 initialize(platform);
818
661 std::vector<RefPtr<WebTaskRunner>> runOrder; 819 std::vector<RefPtr<WebTaskRunner>> runOrder;
662 820
663 scoped_refptr<scheduler::TaskQueue> taskRunner1( 821 scoped_refptr<scheduler::TaskQueue> taskRunner1(
664 m_platform.rendererScheduler()->NewTimerTaskRunner( 822 platform->rendererScheduler()->NewTimerTaskRunner(
665 scheduler::TaskQueue::QueueType::TEST)); 823 scheduler::TaskQueue::QueueType::TEST));
666 RefPtr<scheduler::WebTaskRunnerImpl> webTaskRunner1 = 824 RefPtr<scheduler::WebTaskRunnerImpl> webTaskRunner1 =
667 scheduler::WebTaskRunnerImpl::create(taskRunner1); 825 scheduler::WebTaskRunnerImpl::create(taskRunner1);
668 TaskObserver taskObserver1(webTaskRunner1, &runOrder); 826 TaskObserver taskObserver1(webTaskRunner1, &runOrder);
669 taskRunner1->AddTaskObserver(&taskObserver1); 827 taskRunner1->AddTaskObserver(&taskObserver1);
670 828
671 scoped_refptr<scheduler::TaskQueue> taskRunner2( 829 scoped_refptr<scheduler::TaskQueue> taskRunner2(
672 m_platform.rendererScheduler()->NewTimerTaskRunner( 830 platform->rendererScheduler()->NewTimerTaskRunner(
673 scheduler::TaskQueue::QueueType::TEST)); 831 scheduler::TaskQueue::QueueType::TEST));
674 RefPtr<scheduler::WebTaskRunnerImpl> webTaskRunner2 = 832 RefPtr<scheduler::WebTaskRunnerImpl> webTaskRunner2 =
675 scheduler::WebTaskRunnerImpl::create(taskRunner2); 833 scheduler::WebTaskRunnerImpl::create(taskRunner2);
676 TaskObserver taskObserver2(webTaskRunner2, &runOrder); 834 TaskObserver taskObserver2(webTaskRunner2, &runOrder);
677 taskRunner2->AddTaskObserver(&taskObserver2); 835 taskRunner2->AddTaskObserver(&taskObserver2);
678 836
679 TimerForTest<TimerTest> timer(webTaskRunner1, this, &TimerTest::countingTask); 837 TimerForTest<TimerTest> timer(webTaskRunner1, this, &TimerTest::countingTask);
680 838
681 double startTime = monotonicallyIncreasingTime(); 839 double startTime = monotonicallyIncreasingTime();
682 840
683 timer.startRepeating(1, BLINK_FROM_HERE); 841 timer.startRepeating(1, BLINK_FROM_HERE);
684 842
685 m_platform.runForPeriodSeconds(2.5); 843 platform->runForPeriodSeconds(2.5);
686 844
687 timer.moveToNewTaskRunner(webTaskRunner2); 845 timer.moveToNewTaskRunner(webTaskRunner2);
688 846
689 m_platform.runForPeriodSeconds(2); 847 platform->runForPeriodSeconds(2);
690 848
691 EXPECT_THAT(m_runTimes, ElementsAre(startTime + 1.0, startTime + 2.0, 849 EXPECT_THAT(m_runTimes, ElementsAre(startTime + 1.0, startTime + 2.0,
692 startTime + 3.0, startTime + 4.0)); 850 startTime + 3.0, startTime + 4.0));
693 851
694 EXPECT_THAT(runOrder, ElementsAre(webTaskRunner1, webTaskRunner1, 852 EXPECT_THAT(runOrder, ElementsAre(webTaskRunner1, webTaskRunner1,
695 webTaskRunner2, webTaskRunner2)); 853 webTaskRunner2, webTaskRunner2));
696 854
697 EXPECT_TRUE(taskRunner1->IsEmpty()); 855 EXPECT_TRUE(taskRunner1->IsEmpty());
698 EXPECT_FALSE(taskRunner2->IsEmpty()); 856 EXPECT_FALSE(taskRunner2->IsEmpty());
699 } 857 }
700 858
701 // This test checks that when inactive timer is moved to a different task 859 // This test checks that when inactive timer is moved to a different task
702 // runner it isn't activated. 860 // runner it isn't activated.
703 TEST_F(TimerTest, MoveToNewTaskRunnerWithoutTasks) { 861 TEST_F(TimerTest, MoveToNewTaskRunnerWithoutTasks) {
862 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
863 platform;
864 initialize(platform);
865
704 scoped_refptr<scheduler::TaskQueue> taskRunner1( 866 scoped_refptr<scheduler::TaskQueue> taskRunner1(
705 m_platform.rendererScheduler()->NewTimerTaskRunner( 867 platform->rendererScheduler()->NewTimerTaskRunner(
706 scheduler::TaskQueue::QueueType::TEST)); 868 scheduler::TaskQueue::QueueType::TEST));
707 RefPtr<scheduler::WebTaskRunnerImpl> webTaskRunner1 = 869 RefPtr<scheduler::WebTaskRunnerImpl> webTaskRunner1 =
708 scheduler::WebTaskRunnerImpl::create(taskRunner1); 870 scheduler::WebTaskRunnerImpl::create(taskRunner1);
709 871
710 scoped_refptr<scheduler::TaskQueue> taskRunner2( 872 scoped_refptr<scheduler::TaskQueue> taskRunner2(
711 m_platform.rendererScheduler()->NewTimerTaskRunner( 873 platform->rendererScheduler()->NewTimerTaskRunner(
712 scheduler::TaskQueue::QueueType::TEST)); 874 scheduler::TaskQueue::QueueType::TEST));
713 RefPtr<scheduler::WebTaskRunnerImpl> webTaskRunner2 = 875 RefPtr<scheduler::WebTaskRunnerImpl> webTaskRunner2 =
714 scheduler::WebTaskRunnerImpl::create(taskRunner2); 876 scheduler::WebTaskRunnerImpl::create(taskRunner2);
715 877
716 TimerForTest<TimerTest> timer(webTaskRunner1, this, &TimerTest::countingTask); 878 TimerForTest<TimerTest> timer(webTaskRunner1, this, &TimerTest::countingTask);
717 879
718 m_platform.runUntilIdle(); 880 platform->runUntilIdle();
719 EXPECT_TRUE(!m_runTimes.size()); 881 EXPECT_TRUE(!m_runTimes.size());
720 EXPECT_TRUE(taskRunner1->IsEmpty()); 882 EXPECT_TRUE(taskRunner1->IsEmpty());
721 EXPECT_TRUE(taskRunner2->IsEmpty()); 883 EXPECT_TRUE(taskRunner2->IsEmpty());
722 } 884 }
723 885
724 } // namespace 886 } // namespace
725 } // namespace blink 887 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698