Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1014)

Side by Side Diff: third_party/WebKit/Source/platform/TimerTest.cpp

Issue 1646583002: [Reland] Per WebViewScheduler virtual time (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <queue> 14 #include <queue>
15 15
16 using testing::ElementsAre; 16 using testing::ElementsAre;
17 17
18 namespace blink { 18 namespace blink {
19 namespace { 19 namespace {
20 double gCurrentTimeSecs = 0.0; 20 double gCurrentTimeSecs = 0.0;
21 21
22 double currentTime()
23 {
24 return gCurrentTimeSecs;
25 }
26
27 // This class exists because gcc doesn't know how to move an OwnPtr. 22 // This class exists because gcc doesn't know how to move an OwnPtr.
28 class RefCountedTaskContainer : public RefCounted<RefCountedTaskContainer> { 23 class RefCountedTaskContainer : public RefCounted<RefCountedTaskContainer> {
29 public: 24 public:
30 explicit RefCountedTaskContainer(WebTaskRunner::Task* task) : m_task(adoptPt r(task)) { } 25 explicit RefCountedTaskContainer(WebTaskRunner::Task* task) : m_task(adoptPt r(task)) { }
31 26
32 ~RefCountedTaskContainer() { } 27 ~RefCountedTaskContainer() { }
33 28
34 void run() 29 void run()
35 { 30 {
36 m_task->run(); 31 m_task->run();
37 } 32 }
38 33
39 private: 34 private:
40 OwnPtr<WebTaskRunner::Task> m_task; 35 OwnPtr<WebTaskRunner::Task> m_task;
41 }; 36 };
42 37
43 class DelayedTask { 38 class DelayedTask {
44 public: 39 public:
45 DelayedTask(WebTaskRunner::Task* task, double delaySeconds) 40 DelayedTask(WebTaskRunner::Task* task, double delaySeconds)
46 : m_task(adoptRef(new RefCountedTaskContainer(task))) 41 : m_task(adoptRef(new RefCountedTaskContainer(task)))
47 , m_runTimeSeconds(monotonicallyIncreasingTime() + delaySeconds) 42 , m_runTimeSeconds(gCurrentTimeSecs + delaySeconds)
48 , m_delaySeconds(delaySeconds) { } 43 , m_delaySeconds(delaySeconds) { }
49 44
50 bool operator<(const DelayedTask& other) const 45 bool operator<(const DelayedTask& other) const
51 { 46 {
52 return m_runTimeSeconds > other.m_runTimeSeconds; 47 return m_runTimeSeconds > other.m_runTimeSeconds;
53 } 48 }
54 49
55 void run() const 50 void run() const
56 { 51 {
57 m_task->run(); 52 m_task->run();
(...skipping 29 matching lines...) Expand all
87 { 82 {
88 m_timerTasks->push(DelayedTask(task, delayMs * 0.001)); 83 m_timerTasks->push(DelayedTask(task, delayMs * 0.001));
89 } 84 }
90 85
91 WebTaskRunner* clone() override 86 WebTaskRunner* clone() override
92 { 87 {
93 ASSERT_NOT_REACHED(); 88 ASSERT_NOT_REACHED();
94 return nullptr; 89 return nullptr;
95 } 90 }
96 91
92 double virtualTimeSeconds() const override
93 {
94 ASSERT_NOT_REACHED();
95 return 0.0;
96 }
97
98 double monotonicallyIncreasingVirtualTimeSeconds() const override
99 {
100 return gCurrentTimeSecs;
101 }
102
97 std::priority_queue<DelayedTask>* m_timerTasks; // NOT OWNED 103 std::priority_queue<DelayedTask>* m_timerTasks; // NOT OWNED
98 }; 104 };
99 105
100 class MockWebScheduler : public WebScheduler { 106 class MockWebScheduler : public WebScheduler {
101 public: 107 public:
102 MockWebScheduler() : m_timerWebTaskRunner(&m_timerTasks) { } 108 MockWebScheduler() : m_timerWebTaskRunner(&m_timerTasks) { }
103 ~MockWebScheduler() override { } 109 ~MockWebScheduler() override { }
104 110
105 bool shouldYieldForHighPriorityWork() override 111 bool shouldYieldForHighPriorityWork() override
106 { 112 {
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 return static_cast<MockWebScheduler*>(m_webThread->scheduler()); 279 return static_cast<MockWebScheduler*>(m_webThread->scheduler());
274 } 280 }
275 281
276 OwnPtr<FakeWebThread> m_webThread; 282 OwnPtr<FakeWebThread> m_webThread;
277 }; 283 };
278 284
279 class TimerTest : public testing::Test { 285 class TimerTest : public testing::Test {
280 public: 286 public:
281 void SetUp() override 287 void SetUp() override
282 { 288 {
283 WTF::setMonotonicallyIncreasingTimeFunction(currentTime);
284
285 m_runTimes.clear(); 289 m_runTimes.clear();
286 gCurrentTimeSecs = 10.0; 290 gCurrentTimeSecs = 10.0;
287 m_startTime = gCurrentTimeSecs; 291 m_startTime = gCurrentTimeSecs;
288 } 292 }
289 293
290 void countingTask(Timer<TimerTest>*) 294 void countingTask(Timer<TimerTest>*)
291 { 295 {
292 m_runTimes.append(monotonicallyIncreasingTime()); 296 m_runTimes.append(gCurrentTimeSecs);
293 } 297 }
294 298
295 void recordNextFireTimeTask(Timer<TimerTest>* timer) 299 void recordNextFireTimeTask(Timer<TimerTest>* timer)
296 { 300 {
297 m_nextFireTimes.append(monotonicallyIncreasingTime() + timer->nextFireIn terval()); 301 m_nextFireTimes.append(gCurrentTimeSecs + timer->nextFireInterval());
298 } 302 }
299 303
300 void advanceTimeBy(double timeSecs) 304 void advanceTimeBy(double timeSecs)
301 { 305 {
302 gCurrentTimeSecs += timeSecs; 306 gCurrentTimeSecs += timeSecs;
303 } 307 }
304 308
305 void runUntilIdle() 309 void runUntilIdle()
306 { 310 {
307 m_platform.runUntilIdle(); 311 m_platform.runUntilIdle();
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
436 timer.startOneShot(10, BLINK_FROM_HERE); 440 timer.startOneShot(10, BLINK_FROM_HERE);
437 441
438 ASSERT(hasOneTimerTask()); 442 ASSERT(hasOneTimerTask());
439 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); 443 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs());
440 444
441 timer.stop(); 445 timer.stop();
442 446
443 runUntilIdle(); 447 runUntilIdle();
444 EXPECT_FALSE(m_runTimes.size()); 448 EXPECT_FALSE(m_runTimes.size());
445 449
446 double secondPostTime = monotonicallyIncreasingTime(); 450 double secondPostTime = gCurrentTimeSecs;
447 timer.startOneShot(10, BLINK_FROM_HERE); 451 timer.startOneShot(10, BLINK_FROM_HERE);
448 452
449 ASSERT(hasOneTimerTask()); 453 ASSERT(hasOneTimerTask());
450 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); 454 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs());
451 455
452 runUntilIdle(); 456 runUntilIdle();
453 EXPECT_THAT(m_runTimes, ElementsAre(secondPostTime + 10.0)); 457 EXPECT_THAT(m_runTimes, ElementsAre(secondPostTime + 10.0));
454 } 458 }
455 459
456 TEST_F(TimerTest, StartOneShot_NonZero_RepostingAfterRunning) 460 TEST_F(TimerTest, StartOneShot_NonZero_RepostingAfterRunning)
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
754 TimerForTest<TimerTest> timer(this, &TimerTest::countingTask, &taskRunner); 758 TimerForTest<TimerTest> timer(this, &TimerTest::countingTask, &taskRunner);
755 timer.startOneShot(0, BLINK_FROM_HERE); 759 timer.startOneShot(0, BLINK_FROM_HERE);
756 760
757 // Make sure the task was posted on taskRunner. 761 // Make sure the task was posted on taskRunner.
758 EXPECT_FALSE(timerTasks.empty()); 762 EXPECT_FALSE(timerTasks.empty());
759 } 763 }
760 764
761 765
762 } // namespace 766 } // namespace
763 } // namespace blink 767 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698