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