OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "config.h" |
| 6 #include "platform/Timer.h" |
| 7 |
| 8 #include "public/platform/Platform.h" |
| 9 #include "public/platform/WebScheduler.h" |
| 10 #include "public/platform/WebThread.h" |
| 11 #include <gmock/gmock.h> |
| 12 #include <gtest/gtest.h> |
| 13 #include <queue> |
| 14 |
| 15 using testing::ElementsAre; |
| 16 |
| 17 namespace blink { |
| 18 namespace { |
| 19 double gCurrentTimeSecs = 0.0; |
| 20 |
| 21 double CurrentTime() |
| 22 { |
| 23 return gCurrentTimeSecs; |
| 24 } |
| 25 |
| 26 // This class exists because gcc doesn't know how to move an OwnPtr. |
| 27 class RefCountedTaskContainer : public RefCounted<RefCountedTaskContainer> { |
| 28 public: |
| 29 explicit RefCountedTaskContainer(WebThread::Task* task) : m_task(adoptPtr(ta
sk)) { } |
| 30 |
| 31 ~RefCountedTaskContainer() { } |
| 32 |
| 33 void run() |
| 34 { |
| 35 m_task->run(); |
| 36 } |
| 37 |
| 38 private: |
| 39 OwnPtr<WebThread::Task> m_task; |
| 40 }; |
| 41 |
| 42 class DelayedTask { |
| 43 public: |
| 44 DelayedTask(WebThread::Task* task, long long delayMs) |
| 45 : m_task(adoptRef(new RefCountedTaskContainer(task))) |
| 46 , m_runTimeSecs(monotonicallyIncreasingTime() + 0.001 * static_cast<doub
le>(delayMs)) |
| 47 , m_delayMs(delayMs) { } |
| 48 |
| 49 bool operator<(const DelayedTask& other) const |
| 50 { |
| 51 return m_runTimeSecs > other.m_runTimeSecs; |
| 52 } |
| 53 |
| 54 void run() const |
| 55 { |
| 56 m_task->run(); |
| 57 } |
| 58 |
| 59 double runTimeSecs() const |
| 60 { |
| 61 return m_runTimeSecs; |
| 62 } |
| 63 |
| 64 long long delayMs() const |
| 65 { |
| 66 return m_delayMs; |
| 67 } |
| 68 |
| 69 private: |
| 70 RefPtr<RefCountedTaskContainer> m_task; |
| 71 double m_runTimeSecs; |
| 72 long long m_delayMs; |
| 73 }; |
| 74 |
| 75 class MockWebScheduler : public WebScheduler { |
| 76 public: |
| 77 explicit MockWebScheduler(std::priority_queue<DelayedTask>* timerTasks) : m_
timerTasks(timerTasks) { } |
| 78 ~MockWebScheduler() override { } |
| 79 |
| 80 bool shouldYieldForHighPriorityWork() override |
| 81 { |
| 82 return false; |
| 83 } |
| 84 |
| 85 bool canExceedIdleDeadlineIfRequired() override |
| 86 { |
| 87 return false; |
| 88 } |
| 89 |
| 90 void postIdleTask(const WebTraceLocation&, WebThread::IdleTask*) override |
| 91 { |
| 92 } |
| 93 |
| 94 void postNonNestableIdleTask(const WebTraceLocation&, WebThread::IdleTask*)
override |
| 95 { |
| 96 } |
| 97 |
| 98 void postIdleTaskAfterWakeup(const WebTraceLocation&, WebThread::IdleTask*)
override |
| 99 { |
| 100 } |
| 101 |
| 102 void postLoadingTask(const WebTraceLocation&, WebThread::Task*) override |
| 103 { |
| 104 } |
| 105 |
| 106 void postTimerTask(const WebTraceLocation&, WebThread::Task* task, long long
delayMs) override |
| 107 { |
| 108 m_timerTasks->push(DelayedTask(task, delayMs)); |
| 109 } |
| 110 |
| 111 private: |
| 112 std::priority_queue<DelayedTask>* m_timerTasks; |
| 113 }; |
| 114 |
| 115 class FakeWebThread : public WebThread { |
| 116 public: |
| 117 explicit FakeWebThread(WebScheduler* webScheduler) : m_webScheduler(webSched
uler) { } |
| 118 ~FakeWebThread() override { } |
| 119 |
| 120 // WebThread implementation: |
| 121 void postTask(const WebTraceLocation&, Task*) |
| 122 { |
| 123 ASSERT_NOT_REACHED(); |
| 124 } |
| 125 |
| 126 virtual void postDelayedTask(const WebTraceLocation&, Task*, long long) |
| 127 { |
| 128 ASSERT_NOT_REACHED(); |
| 129 } |
| 130 |
| 131 virtual bool isCurrentThread() const |
| 132 { |
| 133 ASSERT_NOT_REACHED(); |
| 134 return true; |
| 135 } |
| 136 |
| 137 virtual PlatformThreadId threadId() const |
| 138 { |
| 139 ASSERT_NOT_REACHED(); |
| 140 return 0; |
| 141 } |
| 142 |
| 143 WebScheduler* scheduler() const override |
| 144 { |
| 145 return m_webScheduler; |
| 146 } |
| 147 |
| 148 virtual void enterRunLoop() |
| 149 { |
| 150 ASSERT_NOT_REACHED(); |
| 151 } |
| 152 |
| 153 virtual void exitRunLoop() |
| 154 { |
| 155 ASSERT_NOT_REACHED(); |
| 156 } |
| 157 |
| 158 private: |
| 159 WebScheduler* m_webScheduler; |
| 160 }; |
| 161 |
| 162 class TimerTestPlatform : public Platform { |
| 163 public: |
| 164 explicit TimerTestPlatform(WebThread* webThread) : m_webThread(webThread) {
} |
| 165 ~TimerTestPlatform() override { } |
| 166 |
| 167 WebThread* currentThread() override |
| 168 { |
| 169 return m_webThread; |
| 170 } |
| 171 |
| 172 void cryptographicallyRandomValues(unsigned char*, size_t) override |
| 173 { |
| 174 ASSERT_NOT_REACHED(); |
| 175 } |
| 176 |
| 177 private: |
| 178 WebThread* m_webThread; |
| 179 }; |
| 180 |
| 181 class TimerTest : public testing::Test { |
| 182 public: |
| 183 void SetUp() override |
| 184 { |
| 185 m_timerTasks = adoptPtr(new std::priority_queue<DelayedTask>); |
| 186 m_mockWebScheduler = adoptPtr(new MockWebScheduler(m_timerTasks.get())); |
| 187 m_fakeWebThread = adoptPtr(new FakeWebThread(m_mockWebScheduler.get())); |
| 188 m_platform = adoptPtr(new TimerTestPlatform(m_fakeWebThread.get())); |
| 189 m_oldPlatform = Platform::current(); |
| 190 Platform::initialize(m_platform.get()); |
| 191 WTF::setMonotonicallyIncreasingTimeFunction(CurrentTime); |
| 192 |
| 193 m_runTimes.clear(); |
| 194 gCurrentTimeSecs = 10.0; |
| 195 m_startTime = gCurrentTimeSecs; |
| 196 } |
| 197 |
| 198 void TearDown() override |
| 199 { |
| 200 Platform::initialize(m_oldPlatform); |
| 201 } |
| 202 |
| 203 void CountingTask(Timer<TimerTest>*) |
| 204 { |
| 205 m_runTimes.push_back(monotonicallyIncreasingTime()); |
| 206 } |
| 207 |
| 208 void AdvanceTimeTo(double timeSecs) |
| 209 { |
| 210 gCurrentTimeSecs = timeSecs; |
| 211 } |
| 212 |
| 213 void AdvanceTimeBy(double timeSecs) |
| 214 { |
| 215 gCurrentTimeSecs += timeSecs; |
| 216 } |
| 217 |
| 218 void RunUntilIdle() |
| 219 { |
| 220 while (!m_timerTasks->empty()) { |
| 221 AdvanceTimeTo(m_timerTasks->top().runTimeSecs()); |
| 222 m_timerTasks->top().run(); |
| 223 m_timerTasks->pop(); |
| 224 } |
| 225 } |
| 226 |
| 227 void RunUntilIdleOrDeadlinePassed(double deadline) |
| 228 { |
| 229 while (!m_timerTasks->empty() && m_timerTasks->top().runTimeSecs() < dea
dline) { |
| 230 AdvanceTimeTo(m_timerTasks->top().runTimeSecs()); |
| 231 m_timerTasks->top().run(); |
| 232 m_timerTasks->pop(); |
| 233 } |
| 234 } |
| 235 |
| 236 protected: |
| 237 double m_startTime; |
| 238 std::vector<double> m_runTimes; |
| 239 OwnPtr<std::priority_queue<DelayedTask>> m_timerTasks; |
| 240 |
| 241 private: |
| 242 OwnPtr<MockWebScheduler> m_mockWebScheduler; |
| 243 OwnPtr<FakeWebThread> m_fakeWebThread; |
| 244 OwnPtr<TimerTestPlatform> m_platform; |
| 245 Platform* m_oldPlatform; |
| 246 }; |
| 247 |
| 248 TEST_F(TimerTest, StartOneShot_Zero) |
| 249 { |
| 250 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 251 timer.startOneShot(0, FROM_HERE); |
| 252 |
| 253 ASSERT_EQ(1ul, m_timerTasks->size()); |
| 254 EXPECT_EQ(0ll, m_timerTasks->top().delayMs()); |
| 255 |
| 256 RunUntilIdle(); |
| 257 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); |
| 258 } |
| 259 |
| 260 TEST_F(TimerTest, StartOneShot_ZeroAndCancel) |
| 261 { |
| 262 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 263 timer.startOneShot(0, FROM_HERE); |
| 264 |
| 265 ASSERT_EQ(1ul, m_timerTasks->size()); |
| 266 EXPECT_EQ(0ll, m_timerTasks->top().delayMs()); |
| 267 |
| 268 timer.stop(); |
| 269 |
| 270 RunUntilIdle(); |
| 271 EXPECT_TRUE(m_runTimes.empty()); |
| 272 } |
| 273 |
| 274 TEST_F(TimerTest, StartOneShot_ZeroAndCancelThenRepost) |
| 275 { |
| 276 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 277 timer.startOneShot(0, FROM_HERE); |
| 278 |
| 279 ASSERT_EQ(1ul, m_timerTasks->size()); |
| 280 EXPECT_EQ(0ll, m_timerTasks->top().delayMs()); |
| 281 |
| 282 timer.stop(); |
| 283 |
| 284 RunUntilIdle(); |
| 285 EXPECT_TRUE(m_runTimes.empty()); |
| 286 |
| 287 timer.startOneShot(0, FROM_HERE); |
| 288 |
| 289 ASSERT_EQ(1ul, m_timerTasks->size()); |
| 290 EXPECT_EQ(0ll, m_timerTasks->top().delayMs()); |
| 291 |
| 292 RunUntilIdle(); |
| 293 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); |
| 294 } |
| 295 |
| 296 TEST_F(TimerTest, StartOneShot_Zero_RepostingAfterRunning) |
| 297 { |
| 298 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 299 timer.startOneShot(0, FROM_HERE); |
| 300 |
| 301 ASSERT_EQ(1ul, m_timerTasks->size()); |
| 302 EXPECT_EQ(0ll, m_timerTasks->top().delayMs()); |
| 303 |
| 304 RunUntilIdle(); |
| 305 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); |
| 306 |
| 307 timer.startOneShot(0, FROM_HERE); |
| 308 |
| 309 ASSERT_EQ(1ul, m_timerTasks->size()); |
| 310 EXPECT_EQ(0ll, m_timerTasks->top().delayMs()); |
| 311 |
| 312 RunUntilIdle(); |
| 313 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime, m_startTime)); |
| 314 } |
| 315 |
| 316 TEST_F(TimerTest, StartOneShot_NonZero) |
| 317 { |
| 318 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 319 timer.startOneShot(10.0, FROM_HERE); |
| 320 |
| 321 ASSERT_EQ(1ul, m_timerTasks->size()); |
| 322 EXPECT_EQ(10000ll, m_timerTasks->top().delayMs()); |
| 323 |
| 324 RunUntilIdle(); |
| 325 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); |
| 326 } |
| 327 |
| 328 TEST_F(TimerTest, StartOneShot_NonZeroAndCancel) |
| 329 { |
| 330 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 331 timer.startOneShot(10, FROM_HERE); |
| 332 |
| 333 ASSERT_EQ(1ul, m_timerTasks->size()); |
| 334 EXPECT_EQ(10000ll, m_timerTasks->top().delayMs()); |
| 335 |
| 336 timer.stop(); |
| 337 |
| 338 RunUntilIdle(); |
| 339 EXPECT_TRUE(m_runTimes.empty()); |
| 340 } |
| 341 |
| 342 TEST_F(TimerTest, StartOneShot_NonZeroAndCancelThenRepost) |
| 343 { |
| 344 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 345 timer.startOneShot(10, FROM_HERE); |
| 346 |
| 347 ASSERT_EQ(1ul, m_timerTasks->size()); |
| 348 EXPECT_EQ(10000ll, m_timerTasks->top().delayMs()); |
| 349 |
| 350 timer.stop(); |
| 351 |
| 352 RunUntilIdle(); |
| 353 EXPECT_TRUE(m_runTimes.empty()); |
| 354 |
| 355 double secondPostTime = monotonicallyIncreasingTime(); |
| 356 timer.startOneShot(10, FROM_HERE); |
| 357 |
| 358 ASSERT_EQ(1ul, m_timerTasks->size()); |
| 359 EXPECT_EQ(10000ll, m_timerTasks->top().delayMs()); |
| 360 |
| 361 RunUntilIdle(); |
| 362 EXPECT_THAT(m_runTimes, ElementsAre(secondPostTime + 10.0)); |
| 363 } |
| 364 |
| 365 TEST_F(TimerTest, StartOneShot_NonZero_RepostingAfterRunning) |
| 366 { |
| 367 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 368 timer.startOneShot(10, FROM_HERE); |
| 369 |
| 370 ASSERT_EQ(1ul, m_timerTasks->size()); |
| 371 EXPECT_EQ(10000ll, m_timerTasks->top().delayMs()); |
| 372 |
| 373 RunUntilIdle(); |
| 374 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); |
| 375 |
| 376 timer.startOneShot(20, FROM_HERE); |
| 377 |
| 378 ASSERT_EQ(1ul, m_timerTasks->size()); |
| 379 EXPECT_EQ(20000ll, m_timerTasks->top().delayMs()); |
| 380 |
| 381 RunUntilIdle(); |
| 382 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0, m_startTime + 30.0))
; |
| 383 } |
| 384 |
| 385 TEST_F(TimerTest, PostingTimerTwiceWithSameRunTimeDoesNothing) |
| 386 { |
| 387 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 388 timer.startOneShot(10, FROM_HERE); |
| 389 timer.startOneShot(10, FROM_HERE); |
| 390 |
| 391 ASSERT_EQ(1ul, m_timerTasks->size()); |
| 392 EXPECT_EQ(10000ll, m_timerTasks->top().delayMs()); |
| 393 |
| 394 RunUntilIdle(); |
| 395 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); |
| 396 } |
| 397 |
| 398 TEST_F(TimerTest, PostingTimerTwiceWithNewerRunTimeCancelsOrigionalTask) |
| 399 { |
| 400 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 401 timer.startOneShot(10, FROM_HERE); |
| 402 timer.startOneShot(0, FROM_HERE); |
| 403 |
| 404 RunUntilIdle(); |
| 405 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 0.0)); |
| 406 } |
| 407 |
| 408 TEST_F(TimerTest, PostingTimerTwiceWithLaterRunTimeCancelsOrigionalTask) |
| 409 { |
| 410 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 411 timer.startOneShot(0, FROM_HERE); |
| 412 timer.startOneShot(10, FROM_HERE); |
| 413 |
| 414 RunUntilIdle(); |
| 415 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); |
| 416 } |
| 417 |
| 418 TEST_F(TimerTest, StartRepeatingTask) |
| 419 { |
| 420 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 421 timer.startRepeating(1.0, FROM_HERE); |
| 422 |
| 423 ASSERT_EQ(1ul, m_timerTasks->size()); |
| 424 EXPECT_EQ(1000ll, m_timerTasks->top().delayMs()); |
| 425 |
| 426 RunUntilIdleOrDeadlinePassed(m_startTime + 5.5); |
| 427 EXPECT_THAT(m_runTimes, ElementsAre( |
| 428 m_startTime + 1.0, m_startTime + 2.0, m_startTime + 3.0, m_startTime + 4
.0, m_startTime + 5.0)); |
| 429 } |
| 430 |
| 431 TEST_F(TimerTest, StartRepeatingTask_ThenCancel) |
| 432 { |
| 433 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 434 timer.startRepeating(1.0, FROM_HERE); |
| 435 |
| 436 ASSERT_EQ(1ul, m_timerTasks->size()); |
| 437 EXPECT_EQ(1000ll, m_timerTasks->top().delayMs()); |
| 438 |
| 439 RunUntilIdleOrDeadlinePassed(m_startTime + 2.5); |
| 440 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0)); |
| 441 |
| 442 timer.stop(); |
| 443 RunUntilIdle(); |
| 444 |
| 445 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0)); |
| 446 } |
| 447 |
| 448 TEST_F(TimerTest, StartRepeatingTask_ThenPostOneShot) |
| 449 { |
| 450 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 451 timer.startRepeating(1.0, FROM_HERE); |
| 452 |
| 453 ASSERT_EQ(1ul, m_timerTasks->size()); |
| 454 EXPECT_EQ(1000ll, m_timerTasks->top().delayMs()); |
| 455 |
| 456 RunUntilIdleOrDeadlinePassed(m_startTime + 2.5); |
| 457 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0)); |
| 458 |
| 459 timer.startOneShot(0, FROM_HERE); |
| 460 RunUntilIdle(); |
| 461 |
| 462 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0, m_
startTime + 2.0)); |
| 463 } |
| 464 |
| 465 TEST_F(TimerTest, IsActive_NeverPosted) |
| 466 { |
| 467 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 468 |
| 469 EXPECT_FALSE(timer.isActive()); |
| 470 } |
| 471 |
| 472 TEST_F(TimerTest, IsActive_AfterPosting_OneShotZero) |
| 473 { |
| 474 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 475 timer.startOneShot(0, FROM_HERE); |
| 476 |
| 477 EXPECT_TRUE(timer.isActive()); |
| 478 } |
| 479 |
| 480 TEST_F(TimerTest, IsActive_AfterPosting_OneShotNonZero) |
| 481 { |
| 482 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 483 timer.startOneShot(10, FROM_HERE); |
| 484 |
| 485 EXPECT_TRUE(timer.isActive()); |
| 486 } |
| 487 |
| 488 TEST_F(TimerTest, IsActive_AfterPosting_Repeating) |
| 489 { |
| 490 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 491 timer.startRepeating(1.0, FROM_HERE); |
| 492 |
| 493 EXPECT_TRUE(timer.isActive()); |
| 494 } |
| 495 |
| 496 TEST_F(TimerTest, IsActive_AfterRunning_OneShotZero) |
| 497 { |
| 498 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 499 timer.startOneShot(0, FROM_HERE); |
| 500 |
| 501 RunUntilIdle(); |
| 502 EXPECT_FALSE(timer.isActive()); |
| 503 } |
| 504 |
| 505 TEST_F(TimerTest, IsActive_AfterRunning_OneShotNonZero) |
| 506 { |
| 507 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 508 timer.startOneShot(10, FROM_HERE); |
| 509 |
| 510 RunUntilIdle(); |
| 511 EXPECT_FALSE(timer.isActive()); |
| 512 } |
| 513 |
| 514 TEST_F(TimerTest, IsActive_AfterRunning_Repeating) |
| 515 { |
| 516 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 517 timer.startRepeating(1.0, FROM_HERE); |
| 518 |
| 519 RunUntilIdleOrDeadlinePassed(m_startTime + 10); |
| 520 EXPECT_TRUE(timer.isActive()); // It should run until cancelled. |
| 521 } |
| 522 |
| 523 TEST_F(TimerTest, NextFireInterval_OneShotZero) |
| 524 { |
| 525 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 526 timer.startOneShot(0, FROM_HERE); |
| 527 |
| 528 EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval()); |
| 529 } |
| 530 |
| 531 TEST_F(TimerTest, NextFireInterval_OneShotNonZero) |
| 532 { |
| 533 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 534 timer.startOneShot(10, FROM_HERE); |
| 535 |
| 536 EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval()); |
| 537 } |
| 538 |
| 539 TEST_F(TimerTest, NextFireInterval_OneShotNonZero_AfterAFewSeconds) |
| 540 { |
| 541 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 542 timer.startOneShot(10, FROM_HERE); |
| 543 |
| 544 AdvanceTimeBy(2.0); |
| 545 EXPECT_FLOAT_EQ(8.0, timer.nextFireInterval()); |
| 546 } |
| 547 |
| 548 TEST_F(TimerTest, NextFireInterval_Repeating) |
| 549 { |
| 550 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 551 timer.startRepeating(20, FROM_HERE); |
| 552 |
| 553 EXPECT_FLOAT_EQ(20.0, timer.nextFireInterval()); |
| 554 } |
| 555 |
| 556 TEST_F(TimerTest, RepeatInterval_NeverStarted) |
| 557 { |
| 558 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 559 |
| 560 EXPECT_FLOAT_EQ(0.0, timer.repeatInterval()); |
| 561 } |
| 562 |
| 563 TEST_F(TimerTest, RepeatInterval_OneShotZero) |
| 564 { |
| 565 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 566 timer.startOneShot(0, FROM_HERE); |
| 567 |
| 568 EXPECT_FLOAT_EQ(0.0, timer.repeatInterval()); |
| 569 } |
| 570 |
| 571 TEST_F(TimerTest, RepeatInterval_OneShotNonZero) |
| 572 { |
| 573 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 574 timer.startOneShot(10, FROM_HERE); |
| 575 |
| 576 EXPECT_FLOAT_EQ(0.0, timer.repeatInterval()); |
| 577 } |
| 578 |
| 579 TEST_F(TimerTest, RepeatInterval_Repeating) |
| 580 { |
| 581 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 582 timer.startRepeating(20, FROM_HERE); |
| 583 |
| 584 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval()); |
| 585 } |
| 586 |
| 587 TEST_F(TimerTest, AugmentRepeatInterval) |
| 588 { |
| 589 Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask); |
| 590 timer.startRepeating(10, FROM_HERE); |
| 591 EXPECT_FLOAT_EQ(10.0, timer.repeatInterval()); |
| 592 EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval()); |
| 593 |
| 594 AdvanceTimeBy(2.0); // augmentRepeatInterval reposts based off the current t
ime. |
| 595 timer.augmentRepeatInterval(10); |
| 596 |
| 597 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval()); |
| 598 EXPECT_FLOAT_EQ(20.0, timer.nextFireInterval()); |
| 599 |
| 600 RunUntilIdleOrDeadlinePassed(m_startTime + 50.0); |
| 601 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 22.0, m_startTime + 42.0))
; |
| 602 } |
| 603 |
| 604 class MockTimerWithAlignment : public TimerBase { |
| 605 public: |
| 606 MockTimerWithAlignment() : m_lastFireTime(0.0), m_alignedFireTime(0.0) { } |
| 607 |
| 608 virtual void fired() override |
| 609 { |
| 610 } |
| 611 |
| 612 double alignedFireTime(double fireTime) const override |
| 613 { |
| 614 m_lastFireTime = fireTime; |
| 615 return m_alignedFireTime; |
| 616 } |
| 617 |
| 618 void setAlignedFireTime(double alignedFireTime) |
| 619 { |
| 620 m_alignedFireTime = alignedFireTime; |
| 621 } |
| 622 |
| 623 double lastFireTime() const |
| 624 { |
| 625 return m_lastFireTime; |
| 626 } |
| 627 |
| 628 private: |
| 629 mutable double m_lastFireTime; |
| 630 double m_alignedFireTime; |
| 631 }; |
| 632 |
| 633 TEST_F(TimerTest, TimerAlignment_OneShotZero) |
| 634 { |
| 635 MockTimerWithAlignment timer; |
| 636 timer.setAlignedFireTime(m_startTime + 1.0); |
| 637 |
| 638 timer.start(0.0, 0.0, FROM_HERE); |
| 639 |
| 640 // The nextFireInterval gets overrriden. |
| 641 EXPECT_FLOAT_EQ(1.0, timer.nextFireInterval()); |
| 642 EXPECT_FLOAT_EQ(0.0, timer.nextUnalignedFireInterval()); |
| 643 EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime()); |
| 644 } |
| 645 |
| 646 TEST_F(TimerTest, TimerAlignment_OneShotNonZero) |
| 647 { |
| 648 MockTimerWithAlignment timer; |
| 649 timer.setAlignedFireTime(m_startTime + 1.0); |
| 650 |
| 651 timer.start(0.5, 0.0, FROM_HERE); |
| 652 |
| 653 // The nextFireInterval gets overrriden. |
| 654 EXPECT_FLOAT_EQ(1.0, timer.nextFireInterval()); |
| 655 EXPECT_FLOAT_EQ(0.5, timer.nextUnalignedFireInterval()); |
| 656 EXPECT_FLOAT_EQ(m_startTime + 0.5, timer.lastFireTime()); |
| 657 } |
| 658 |
| 659 TEST_F(TimerTest, DidChangeAlignmentInterval) |
| 660 { |
| 661 MockTimerWithAlignment timer; |
| 662 timer.setAlignedFireTime(m_startTime + 1.0); |
| 663 |
| 664 timer.start(0.0, 0.0, FROM_HERE); |
| 665 |
| 666 EXPECT_FLOAT_EQ(1.0, timer.nextFireInterval()); |
| 667 EXPECT_FLOAT_EQ(0.0, timer.nextUnalignedFireInterval()); |
| 668 EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime()); |
| 669 |
| 670 timer.setAlignedFireTime(m_startTime); |
| 671 timer.didChangeAlignmentInterval(monotonicallyIncreasingTime()); |
| 672 |
| 673 EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval()); |
| 674 EXPECT_FLOAT_EQ(0.0, timer.nextUnalignedFireInterval()); |
| 675 EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime()); |
| 676 } |
| 677 |
| 678 |
| 679 } // namespace |
| 680 } // namespace blink |
OLD | NEW |