| OLD | NEW |
| 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 "config.h" | 5 #include "config.h" |
| 6 #include "platform/Timer.h" | 6 #include "platform/Timer.h" |
| 7 | 7 |
| 8 #include "platform/testing/TestingPlatformSupport.h" | |
| 9 #include "public/platform/Platform.h" | 8 #include "public/platform/Platform.h" |
| 10 #include "public/platform/WebScheduler.h" | 9 #include "public/platform/WebScheduler.h" |
| 11 #include "public/platform/WebThread.h" | 10 #include "public/platform/WebThread.h" |
| 12 #include "public/platform/WebViewScheduler.h" | 11 #include "public/platform/WebViewScheduler.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include <queue> | 14 #include <queue> |
| 16 | 15 |
| 17 using testing::ElementsAre; | 16 using testing::ElementsAre; |
| 18 | 17 |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 | 229 |
| 231 virtual void exitRunLoop() | 230 virtual void exitRunLoop() |
| 232 { | 231 { |
| 233 ASSERT_NOT_REACHED(); | 232 ASSERT_NOT_REACHED(); |
| 234 } | 233 } |
| 235 | 234 |
| 236 private: | 235 private: |
| 237 OwnPtr<MockWebScheduler> m_webScheduler; | 236 OwnPtr<MockWebScheduler> m_webScheduler; |
| 238 }; | 237 }; |
| 239 | 238 |
| 240 class TimerTestPlatform : public TestingPlatformSupport { | 239 class TimerTestPlatform : public Platform { |
| 241 public: | 240 public: |
| 242 TimerTestPlatform() | 241 TimerTestPlatform() |
| 243 : m_webThread(adoptPtr(new FakeWebThread())) { } | 242 : m_webThread(adoptPtr(new FakeWebThread())) { } |
| 244 ~TimerTestPlatform() override { } | 243 ~TimerTestPlatform() override { } |
| 245 | 244 |
| 246 WebThread* currentThread() override | 245 WebThread* currentThread() override |
| 247 { | 246 { |
| 248 return m_webThread.get(); | 247 return m_webThread.get(); |
| 249 } | 248 } |
| 250 | 249 |
| 250 void cryptographicallyRandomValues(unsigned char*, size_t) override |
| 251 { |
| 252 ASSERT_NOT_REACHED(); |
| 253 } |
| 254 |
| 255 const unsigned char* getTraceCategoryEnabledFlag(const char* categoryName) o
verride |
| 256 { |
| 257 static const unsigned char enabled[] = {0}; |
| 258 return enabled; |
| 259 } |
| 260 |
| 251 void runUntilIdle() | 261 void runUntilIdle() |
| 252 { | 262 { |
| 253 mockScheduler()->runUntilIdle(); | 263 mockScheduler()->runUntilIdle(); |
| 254 } | 264 } |
| 255 | 265 |
| 256 void runPendingTasks() | 266 void runPendingTasks() |
| 257 { | 267 { |
| 258 mockScheduler()->runPendingTasks(); | 268 mockScheduler()->runPendingTasks(); |
| 259 } | 269 } |
| 260 | 270 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 279 return static_cast<MockWebScheduler*>(m_webThread->scheduler()); | 289 return static_cast<MockWebScheduler*>(m_webThread->scheduler()); |
| 280 } | 290 } |
| 281 | 291 |
| 282 OwnPtr<FakeWebThread> m_webThread; | 292 OwnPtr<FakeWebThread> m_webThread; |
| 283 }; | 293 }; |
| 284 | 294 |
| 285 class TimerTest : public testing::Test { | 295 class TimerTest : public testing::Test { |
| 286 public: | 296 public: |
| 287 void SetUp() override | 297 void SetUp() override |
| 288 { | 298 { |
| 299 m_platform = adoptPtr(new TimerTestPlatform()); |
| 300 m_oldPlatform = Platform::current(); |
| 301 Platform::initialize(m_platform.get()); |
| 289 WTF::setMonotonicallyIncreasingTimeFunction(currentTime); | 302 WTF::setMonotonicallyIncreasingTimeFunction(currentTime); |
| 290 | 303 |
| 291 m_runTimes.clear(); | 304 m_runTimes.clear(); |
| 292 gCurrentTimeSecs = 10.0; | 305 gCurrentTimeSecs = 10.0; |
| 293 m_startTime = gCurrentTimeSecs; | 306 m_startTime = gCurrentTimeSecs; |
| 294 } | 307 } |
| 295 | 308 |
| 309 void TearDown() override |
| 310 { |
| 311 Platform::initialize(m_oldPlatform); |
| 312 } |
| 313 |
| 296 void countingTask(Timer<TimerTest>*) | 314 void countingTask(Timer<TimerTest>*) |
| 297 { | 315 { |
| 298 m_runTimes.append(monotonicallyIncreasingTime()); | 316 m_runTimes.append(monotonicallyIncreasingTime()); |
| 299 } | 317 } |
| 300 | 318 |
| 301 void recordNextFireTimeTask(Timer<TimerTest>* timer) | 319 void recordNextFireTimeTask(Timer<TimerTest>* timer) |
| 302 { | 320 { |
| 303 m_nextFireTimes.append(monotonicallyIncreasingTime() + timer->nextFireIn
terval()); | 321 m_nextFireTimes.append(monotonicallyIncreasingTime() + timer->nextFireIn
terval()); |
| 304 } | 322 } |
| 305 | 323 |
| 306 void advanceTimeBy(double timeSecs) | 324 void advanceTimeBy(double timeSecs) |
| 307 { | 325 { |
| 308 gCurrentTimeSecs += timeSecs; | 326 gCurrentTimeSecs += timeSecs; |
| 309 } | 327 } |
| 310 | 328 |
| 311 void runUntilIdle() | 329 void runUntilIdle() |
| 312 { | 330 { |
| 313 m_platform.runUntilIdle(); | 331 m_platform->runUntilIdle(); |
| 314 } | 332 } |
| 315 | 333 |
| 316 void runPendingTasks() | 334 void runPendingTasks() |
| 317 { | 335 { |
| 318 m_platform.runPendingTasks(); | 336 m_platform->runPendingTasks(); |
| 319 } | 337 } |
| 320 | 338 |
| 321 void runUntilIdleOrDeadlinePassed(double deadline) | 339 void runUntilIdleOrDeadlinePassed(double deadline) |
| 322 { | 340 { |
| 323 m_platform.runUntilIdleOrDeadlinePassed(deadline); | 341 m_platform->runUntilIdleOrDeadlinePassed(deadline); |
| 324 } | 342 } |
| 325 | 343 |
| 326 bool hasOneTimerTask() const | 344 bool hasOneTimerTask() const |
| 327 { | 345 { |
| 328 return m_platform.hasOneTimerTask(); | 346 return m_platform->hasOneTimerTask(); |
| 329 } | 347 } |
| 330 | 348 |
| 331 double nextTimerTaskDelaySecs() const | 349 double nextTimerTaskDelaySecs() const |
| 332 { | 350 { |
| 333 return m_platform.nextTimerTaskDelaySecs(); | 351 return m_platform->nextTimerTaskDelaySecs(); |
| 334 } | 352 } |
| 335 | 353 |
| 336 protected: | 354 protected: |
| 337 double m_startTime; | 355 double m_startTime; |
| 338 WTF::Vector<double> m_runTimes; | 356 WTF::Vector<double> m_runTimes; |
| 339 WTF::Vector<double> m_nextFireTimes; | 357 WTF::Vector<double> m_nextFireTimes; |
| 340 | 358 |
| 341 private: | 359 private: |
| 342 TimerTestPlatform m_platform; | 360 OwnPtr<TimerTestPlatform> m_platform; |
| 361 Platform* m_oldPlatform; |
| 343 }; | 362 }; |
| 344 | 363 |
| 345 TEST_F(TimerTest, StartOneShot_Zero) | 364 TEST_F(TimerTest, StartOneShot_Zero) |
| 346 { | 365 { |
| 347 Timer<TimerTest> timer(this, &TimerTest::countingTask); | 366 Timer<TimerTest> timer(this, &TimerTest::countingTask); |
| 348 timer.startOneShot(0, BLINK_FROM_HERE); | 367 timer.startOneShot(0, BLINK_FROM_HERE); |
| 349 | 368 |
| 350 ASSERT(hasOneTimerTask()); | 369 ASSERT(hasOneTimerTask()); |
| 351 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs()); | 370 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs()); |
| 352 | 371 |
| (...skipping 456 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 809 m_startTime + 6.0, | 828 m_startTime + 6.0, |
| 810 m_startTime + 8.0, | 829 m_startTime + 8.0, |
| 811 m_startTime + 10.0, | 830 m_startTime + 10.0, |
| 812 m_startTime + 14.0, | 831 m_startTime + 14.0, |
| 813 m_startTime + 18.0, | 832 m_startTime + 18.0, |
| 814 m_startTime + 28.0)); | 833 m_startTime + 28.0)); |
| 815 } | 834 } |
| 816 | 835 |
| 817 } // namespace | 836 } // namespace |
| 818 } // namespace blink | 837 } // namespace blink |
| OLD | NEW |