| 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 "platform/Timer.h" | 5 #include "platform/Timer.h" |
| 6 | 6 |
| 7 #include "platform/testing/TestingPlatformSupport.h" | 7 #include "platform/testing/TestingPlatformSupport.h" |
| 8 #include "public/platform/Platform.h" | 8 #include "public/platform/Platform.h" |
| 9 #include "public/platform/WebScheduler.h" | 9 #include "public/platform/WebScheduler.h" |
| 10 #include "public/platform/WebThread.h" | 10 #include "public/platform/WebThread.h" |
| 11 #include "public/platform/WebViewScheduler.h" | 11 #include "public/platform/WebViewScheduler.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "wtf/PtrUtil.h" |
| 14 #include "wtf/RefCounted.h" | 15 #include "wtf/RefCounted.h" |
| 16 #include <memory> |
| 15 #include <queue> | 17 #include <queue> |
| 16 | 18 |
| 17 using testing::ElementsAre; | 19 using testing::ElementsAre; |
| 18 | 20 |
| 19 namespace blink { | 21 namespace blink { |
| 20 namespace { | 22 namespace { |
| 21 double gCurrentTimeSecs = 0.0; | 23 double gCurrentTimeSecs = 0.0; |
| 22 | 24 |
| 23 // This class exists because gcc doesn't know how to move an OwnPtr. | 25 // This class exists because gcc doesn't know how to move an std::unique_ptr. |
| 24 class RefCountedTaskContainer : public RefCounted<RefCountedTaskContainer> { | 26 class RefCountedTaskContainer : public RefCounted<RefCountedTaskContainer> { |
| 25 public: | 27 public: |
| 26 explicit RefCountedTaskContainer(WebTaskRunner::Task* task) : m_task(adoptPt
r(task)) { } | 28 explicit RefCountedTaskContainer(WebTaskRunner::Task* task) : m_task(wrapUni
que(task)) { } |
| 27 | 29 |
| 28 ~RefCountedTaskContainer() { } | 30 ~RefCountedTaskContainer() { } |
| 29 | 31 |
| 30 void run() | 32 void run() |
| 31 { | 33 { |
| 32 m_task->run(); | 34 m_task->run(); |
| 33 } | 35 } |
| 34 | 36 |
| 35 private: | 37 private: |
| 36 OwnPtr<WebTaskRunner::Task> m_task; | 38 std::unique_ptr<WebTaskRunner::Task> m_task; |
| 37 }; | 39 }; |
| 38 | 40 |
| 39 class DelayedTask { | 41 class DelayedTask { |
| 40 public: | 42 public: |
| 41 DelayedTask(WebTaskRunner::Task* task, double delaySeconds) | 43 DelayedTask(WebTaskRunner::Task* task, double delaySeconds) |
| 42 : m_task(adoptRef(new RefCountedTaskContainer(task))) | 44 : m_task(adoptRef(new RefCountedTaskContainer(task))) |
| 43 , m_runTimeSeconds(gCurrentTimeSecs + delaySeconds) | 45 , m_runTimeSeconds(gCurrentTimeSecs + delaySeconds) |
| 44 , m_delaySeconds(delaySeconds) | 46 , m_delaySeconds(delaySeconds) |
| 45 { | 47 { |
| 46 } | 48 } |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 void removePendingNavigation(WebScheduler::NavigatingFrameType) override { } | 195 void removePendingNavigation(WebScheduler::NavigatingFrameType) override { } |
| 194 void onNavigationStarted() override { } | 196 void onNavigationStarted() override { } |
| 195 | 197 |
| 196 private: | 198 private: |
| 197 std::priority_queue<DelayedTask> m_timerTasks; | 199 std::priority_queue<DelayedTask> m_timerTasks; |
| 198 MockWebTaskRunner m_timerWebTaskRunner; | 200 MockWebTaskRunner m_timerWebTaskRunner; |
| 199 }; | 201 }; |
| 200 | 202 |
| 201 class FakeWebThread : public WebThread { | 203 class FakeWebThread : public WebThread { |
| 202 public: | 204 public: |
| 203 FakeWebThread() : m_webScheduler(adoptPtr(new MockWebScheduler())) { } | 205 FakeWebThread() : m_webScheduler(wrapUnique(new MockWebScheduler())) { } |
| 204 ~FakeWebThread() override { } | 206 ~FakeWebThread() override { } |
| 205 | 207 |
| 206 virtual bool isCurrentThread() const | 208 virtual bool isCurrentThread() const |
| 207 { | 209 { |
| 208 ASSERT_NOT_REACHED(); | 210 ASSERT_NOT_REACHED(); |
| 209 return true; | 211 return true; |
| 210 } | 212 } |
| 211 | 213 |
| 212 virtual PlatformThreadId threadId() const | 214 virtual PlatformThreadId threadId() const |
| 213 { | 215 { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 230 { | 232 { |
| 231 ASSERT_NOT_REACHED(); | 233 ASSERT_NOT_REACHED(); |
| 232 } | 234 } |
| 233 | 235 |
| 234 virtual void exitRunLoop() | 236 virtual void exitRunLoop() |
| 235 { | 237 { |
| 236 ASSERT_NOT_REACHED(); | 238 ASSERT_NOT_REACHED(); |
| 237 } | 239 } |
| 238 | 240 |
| 239 private: | 241 private: |
| 240 OwnPtr<MockWebScheduler> m_webScheduler; | 242 std::unique_ptr<MockWebScheduler> m_webScheduler; |
| 241 }; | 243 }; |
| 242 | 244 |
| 243 class TimerTestPlatform : public TestingPlatformSupport { | 245 class TimerTestPlatform : public TestingPlatformSupport { |
| 244 public: | 246 public: |
| 245 TimerTestPlatform() | 247 TimerTestPlatform() |
| 246 : m_webThread(adoptPtr(new FakeWebThread())) { } | 248 : m_webThread(wrapUnique(new FakeWebThread())) { } |
| 247 ~TimerTestPlatform() override { } | 249 ~TimerTestPlatform() override { } |
| 248 | 250 |
| 249 WebThread* currentThread() override | 251 WebThread* currentThread() override |
| 250 { | 252 { |
| 251 return m_webThread.get(); | 253 return m_webThread.get(); |
| 252 } | 254 } |
| 253 | 255 |
| 254 void runUntilIdle() | 256 void runUntilIdle() |
| 255 { | 257 { |
| 256 mockScheduler()->runUntilIdle(); | 258 mockScheduler()->runUntilIdle(); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 275 { | 277 { |
| 276 return mockScheduler()->nextTimerTaskDelaySecs(); | 278 return mockScheduler()->nextTimerTaskDelaySecs(); |
| 277 } | 279 } |
| 278 | 280 |
| 279 private: | 281 private: |
| 280 MockWebScheduler* mockScheduler() const | 282 MockWebScheduler* mockScheduler() const |
| 281 { | 283 { |
| 282 return static_cast<MockWebScheduler*>(m_webThread->scheduler()); | 284 return static_cast<MockWebScheduler*>(m_webThread->scheduler()); |
| 283 } | 285 } |
| 284 | 286 |
| 285 OwnPtr<FakeWebThread> m_webThread; | 287 std::unique_ptr<FakeWebThread> m_webThread; |
| 286 }; | 288 }; |
| 287 | 289 |
| 288 class TimerTest : public testing::Test { | 290 class TimerTest : public testing::Test { |
| 289 public: | 291 public: |
| 290 void SetUp() override | 292 void SetUp() override |
| 291 { | 293 { |
| 292 m_runTimes.clear(); | 294 m_runTimes.clear(); |
| 293 gCurrentTimeSecs = 10.0; | 295 gCurrentTimeSecs = 10.0; |
| 294 m_startTime = gCurrentTimeSecs; | 296 m_startTime = gCurrentTimeSecs; |
| 295 } | 297 } |
| (...skipping 480 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 776 TimerForTest<TimerTest> timer(this, &TimerTest::countingTask, &taskRunner); | 778 TimerForTest<TimerTest> timer(this, &TimerTest::countingTask, &taskRunner); |
| 777 timer.startOneShot(0, BLINK_FROM_HERE); | 779 timer.startOneShot(0, BLINK_FROM_HERE); |
| 778 | 780 |
| 779 // Make sure the task was posted on taskRunner. | 781 // Make sure the task was posted on taskRunner. |
| 780 EXPECT_FALSE(timerTasks.empty()); | 782 EXPECT_FALSE(timerTasks.empty()); |
| 781 } | 783 } |
| 782 | 784 |
| 783 | 785 |
| 784 } // namespace | 786 } // namespace |
| 785 } // namespace blink | 787 } // namespace blink |
| OLD | NEW |