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