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

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

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

Powered by Google App Engine
This is Rietveld 408576698