Chromium Code Reviews| 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 break; | |
| 161 gCurrentTimeSecs = newTime; | |
| 162 s_timerFunction(); | |
| 163 } | |
| 164 } | |
| 165 | |
| 166 bool hasOneTimerTask() const | |
| 167 { | |
| 168 return s_timerFunction && m_timerInterval >= 0; | |
| 169 } | |
| 170 | |
| 171 long nextTimerTaskDelayMillis() const | |
| 172 { | |
| 173 ASSERT(hasOneTimerTask()); | |
| 174 return static_cast<long>(m_timerInterval * 1000); | |
| 175 } | |
| 176 | |
| 177 private: | |
| 178 WebThread* m_webThread; | |
| 179 double m_timerInterval; | |
| 180 | |
| 181 static SharedTimerFunction s_timerFunction; | |
|
philipj_slow
2015/05/11 16:01:19
Is making this static important to the test? The "
Sami
2015/05/11 17:38:59
Unfortunately the way the shared timer function is
philipj_slow
2015/05/12 10:07:57
Acknowledged.
| |
| 182 }; | |
| 183 | |
| 184 Platform::SharedTimerFunction TimerTestPlatform::s_timerFunction; | |
| 185 | |
| 186 class TimerTest : public testing::Test { | |
| 187 public: | |
| 188 void SetUp() override | |
| 189 { | |
| 190 m_mockWebScheduler = adoptPtr(new MockWebScheduler()); | |
| 191 m_fakeWebThread = adoptPtr(new FakeWebThread(m_mockWebScheduler.get())); | |
| 192 m_platform = adoptPtr(new TimerTestPlatform(m_fakeWebThread.get())); | |
| 193 m_oldPlatform = Platform::current(); | |
| 194 Platform::initialize(m_platform.get()); | |
| 195 WTF::setMonotonicallyIncreasingTimeFunction(currentTime); | |
| 196 | |
| 197 m_runTimes.clear(); | |
| 198 gCurrentTimeSecs = 10.0; | |
| 199 m_startTime = gCurrentTimeSecs; | |
| 200 } | |
| 201 | |
| 202 void TearDown() override | |
| 203 { | |
| 204 Platform::initialize(m_oldPlatform); | |
| 205 } | |
| 206 | |
| 207 void countingTask(Timer<TimerTest>*) | |
| 208 { | |
| 209 m_runTimes.push_back(monotonicallyIncreasingTime()); | |
| 210 } | |
| 211 | |
| 212 void advanceTimeBy(double timeSecs) | |
| 213 { | |
| 214 gCurrentTimeSecs += timeSecs; | |
| 215 } | |
| 216 | |
| 217 void runUntilIdle() | |
| 218 { | |
| 219 m_platform->runUntilIdle(); | |
| 220 } | |
| 221 | |
| 222 void runUntilIdleOrDeadlinePassed(double deadline) | |
| 223 { | |
| 224 m_platform->runUntilIdleOrDeadlinePassed(deadline); | |
| 225 } | |
| 226 | |
| 227 bool hasOneTimerTask() const | |
| 228 { | |
| 229 return m_platform->hasOneTimerTask(); | |
| 230 } | |
| 231 | |
| 232 long nextTimerTaskDelayMillis() const | |
| 233 { | |
| 234 return m_platform->nextTimerTaskDelayMillis(); | |
| 235 } | |
| 236 | |
| 237 protected: | |
| 238 double m_startTime; | |
| 239 std::vector<double> m_runTimes; | |
| 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(this, &TimerTest::countingTask); | |
| 251 timer.startOneShot(0, FROM_HERE); | |
| 252 | |
| 253 ASSERT(hasOneTimerTask()); | |
| 254 EXPECT_EQ(0ll, nextTimerTaskDelayMillis()); | |
| 255 | |
| 256 runUntilIdle(); | |
| 257 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); | |
| 258 } | |
| 259 | |
| 260 TEST_F(TimerTest, StartOneShot_ZeroAndCancel) | |
| 261 { | |
| 262 Timer<TimerTest> timer(this, &TimerTest::countingTask); | |
| 263 timer.startOneShot(0, FROM_HERE); | |
| 264 | |
| 265 ASSERT(hasOneTimerTask()); | |
| 266 EXPECT_EQ(0ll, nextTimerTaskDelayMillis()); | |
| 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(this, &TimerTest::countingTask); | |
| 277 timer.startOneShot(0, FROM_HERE); | |
| 278 | |
| 279 ASSERT(hasOneTimerTask()); | |
| 280 EXPECT_EQ(0ll, nextTimerTaskDelayMillis()); | |
| 281 | |
| 282 timer.stop(); | |
| 283 | |
| 284 runUntilIdle(); | |
| 285 EXPECT_TRUE(m_runTimes.empty()); | |
| 286 | |
| 287 timer.startOneShot(0, FROM_HERE); | |
| 288 | |
| 289 ASSERT(hasOneTimerTask()); | |
| 290 EXPECT_EQ(0ll, nextTimerTaskDelayMillis()); | |
| 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(this, &TimerTest::countingTask); | |
| 299 timer.startOneShot(0, FROM_HERE); | |
| 300 | |
| 301 ASSERT(hasOneTimerTask()); | |
| 302 EXPECT_EQ(0ll, nextTimerTaskDelayMillis()); | |
| 303 | |
| 304 runUntilIdle(); | |
| 305 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); | |
| 306 | |
| 307 timer.startOneShot(0, FROM_HERE); | |
| 308 | |
| 309 ASSERT(hasOneTimerTask()); | |
| 310 EXPECT_EQ(0ll, nextTimerTaskDelayMillis()); | |
| 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(this, &TimerTest::countingTask); | |
| 319 timer.startOneShot(10.0, FROM_HERE); | |
| 320 | |
| 321 ASSERT(hasOneTimerTask()); | |
| 322 EXPECT_EQ(10000ll, nextTimerTaskDelayMillis()); | |
| 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(this, &TimerTest::countingTask); | |
| 331 timer.startOneShot(10, FROM_HERE); | |
| 332 | |
| 333 ASSERT(hasOneTimerTask()); | |
| 334 EXPECT_EQ(10000ll, nextTimerTaskDelayMillis()); | |
| 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(this, &TimerTest::countingTask); | |
| 345 timer.startOneShot(10, FROM_HERE); | |
| 346 | |
| 347 ASSERT(hasOneTimerTask()); | |
| 348 EXPECT_EQ(10000ll, nextTimerTaskDelayMillis()); | |
| 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(hasOneTimerTask()); | |
| 359 EXPECT_EQ(10000ll, nextTimerTaskDelayMillis()); | |
| 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(this, &TimerTest::countingTask); | |
| 368 timer.startOneShot(10, FROM_HERE); | |
| 369 | |
| 370 ASSERT(hasOneTimerTask()); | |
| 371 EXPECT_EQ(10000ll, nextTimerTaskDelayMillis()); | |
| 372 | |
| 373 runUntilIdle(); | |
| 374 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); | |
| 375 | |
| 376 timer.startOneShot(20, FROM_HERE); | |
| 377 | |
| 378 ASSERT(hasOneTimerTask()); | |
| 379 EXPECT_EQ(20000ll, nextTimerTaskDelayMillis()); | |
| 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(this, &TimerTest::countingTask); | |
| 388 timer.startOneShot(10, FROM_HERE); | |
| 389 timer.startOneShot(10, FROM_HERE); | |
| 390 | |
| 391 ASSERT(hasOneTimerTask()); | |
| 392 EXPECT_EQ(10000ll, nextTimerTaskDelayMillis()); | |
| 393 | |
| 394 runUntilIdle(); | |
| 395 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); | |
| 396 } | |
| 397 | |
| 398 TEST_F(TimerTest, PostingTimerTwiceWithNewerRunTimeCancelsOrigionalTask) | |
|
philipj_slow
2015/05/11 16:01:19
Typo "origional"
Sami
2015/05/11 17:38:59
Done.
| |
| 399 { | |
| 400 Timer<TimerTest> timer(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) | |
|
philipj_slow
2015/05/11 16:01:19
Dito
Sami
2015/05/11 17:38:59
Done.
| |
| 409 { | |
| 410 Timer<TimerTest> timer(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(this, &TimerTest::countingTask); | |
| 421 timer.startRepeating(1.0, FROM_HERE); | |
| 422 | |
| 423 ASSERT(hasOneTimerTask()); | |
| 424 EXPECT_EQ(1000ll, nextTimerTaskDelayMillis()); | |
| 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(this, &TimerTest::countingTask); | |
| 434 timer.startRepeating(1.0, FROM_HERE); | |
| 435 | |
| 436 ASSERT(hasOneTimerTask()); | |
| 437 EXPECT_EQ(1000ll, nextTimerTaskDelayMillis()); | |
| 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(this, &TimerTest::countingTask); | |
| 451 timer.startRepeating(1.0, FROM_HERE); | |
| 452 | |
| 453 ASSERT(hasOneTimerTask()); | |
| 454 EXPECT_EQ(1000ll, nextTimerTaskDelayMillis()); | |
| 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)); | |
|
philipj_slow
2015/05/11 16:01:19
It's not obvious to me why the last one will be ex
Sami
2015/05/11 17:38:59
Right, right now the timer implementation works by
philipj_slow
2015/05/12 10:07:57
Nice, that makes more sense to me.
| |
| 463 } | |
| 464 | |
| 465 TEST_F(TimerTest, IsActive_NeverPosted) | |
| 466 { | |
| 467 Timer<TimerTest> timer(this, &TimerTest::countingTask); | |
| 468 | |
| 469 EXPECT_FALSE(timer.isActive()); | |
| 470 } | |
| 471 | |
| 472 TEST_F(TimerTest, IsActive_AfterPosting_OneShotZero) | |
| 473 { | |
| 474 Timer<TimerTest> timer(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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); | |
| 595 timer.augmentRepeatInterval(10); | |
|
philipj_slow
2015/05/11 16:01:19
I couldn't guess what this was for and it's not do
Sami
2015/05/11 17:38:59
I believe your guess is as good as mine here -- II
philipj_slow
2015/05/12 10:07:57
OK, a test to verify the behavior is better than n
| |
| 596 | |
| 597 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval()); | |
| 598 EXPECT_FLOAT_EQ(18.0, timer.nextFireInterval()); | |
| 599 | |
| 600 runUntilIdleOrDeadlinePassed(m_startTime + 50.0); | |
| 601 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 20.0, m_startTime + 40.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(); | |
| 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 |