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

Side by Side Diff: Source/platform/scheduler/SchedulerTest.cpp

Issue 364873002: Introduce a basic Blink Scheduler (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Review feedback. Created 6 years, 5 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2014 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/scheduler/Scheduler.h"
7
8 #include "platform/TestingPlatformSupport.h"
9 #include "public/platform/Platform.h"
10
11 #include <gtest/gtest.h>
12
13 using WebCore::Scheduler;
14
15 namespace {
16
17 class TestMainThread : public blink::WebThread {
18 public:
19 // blink::WebThread implementation.
20 virtual void postTask(Task* task) OVERRIDE
21 {
22 m_pendingTasks.append(adoptPtr(task));
23 }
24
25 virtual void postDelayedTask(Task* task, long long delayMs) OVERRIDE
26 {
27 ASSERT_NOT_REACHED();
28 }
29
30 virtual bool isCurrentThread() const OVERRIDE
31 {
32 return true;
33 }
34
35 virtual void enterRunLoop() OVERRIDE
36 {
37 ASSERT_NOT_REACHED();
38 }
39
40 virtual void exitRunLoop() OVERRIDE
41 {
42 ASSERT_NOT_REACHED();
43 }
44
45 void runPendingTasks()
46 {
47 while (!m_pendingTasks.isEmpty())
48 m_pendingTasks.takeFirst()->run();
49 }
50
51 private:
52 WTF::Deque<OwnPtr<Task> > m_pendingTasks;
53 };
54
55 class SchedulerTestingPlatformSupport : WebCore::TestingPlatformSupport {
56 public:
57 SchedulerTestingPlatformSupport()
58 : TestingPlatformSupport(TestingPlatformSupport::Config())
59 , m_sharedTimerFunction(nullptr)
60 , m_sharedTimerRunning(false)
61 , m_sharedTimerFireInterval(0)
62 {
63 }
64
65 // blink::Platform implementation.
66 virtual blink::WebThread* currentThread() OVERRIDE
67 {
68 return &m_mainThread;
69 }
70
71 virtual void setSharedTimerFiredFunction(SharedTimerFunction timerFunction) OVERRIDE
72 {
73 m_sharedTimerFunction = timerFunction;
74 }
75
76 virtual void setSharedTimerFireInterval(double)
77 {
78 m_sharedTimerFireInterval = 0;
79 m_sharedTimerRunning = true;
80 }
81
82 virtual void stopSharedTimer()
83 {
84 m_sharedTimerRunning = false;
85 }
86
87 void runPendingTasks()
88 {
89 m_mainThread.runPendingTasks();
90 }
91
92 bool sharedTimerRunning() const
93 {
94 return m_sharedTimerRunning;
95 }
96
97 double sharedTimerFireInterval() const
98 {
99 return m_sharedTimerFireInterval;
100 }
101
102 void triggerSharedTimer()
103 {
104 m_sharedTimerFunction();
105 }
106
107 private:
108 TestMainThread m_mainThread;
109 SharedTimerFunction m_sharedTimerFunction;
110 bool m_sharedTimerRunning;
111 double m_sharedTimerFireInterval;
112 };
113
114 class SchedulerTest : public testing::Test {
115 public:
116 SchedulerTest()
117 {
118 Scheduler::initializeOnMainThread();
119 m_scheduler = Scheduler::shared();
120 }
121
122 ~SchedulerTest()
123 {
124 Scheduler::shutdown();
125 }
126
127 void runPendingTasks()
128 {
129 m_platformSupport.runPendingTasks();
130 }
131
132 protected:
133 SchedulerTestingPlatformSupport m_platformSupport;
134 Scheduler* m_scheduler;
135 };
136
137 void orderedTestTask(int value, int* result)
138 {
139 *result = (*result << 4) | value;
140 }
141
142 void unorderedTestTask(int value, int* result)
143 {
144 *result += value;
145 }
146
147 TEST_F(SchedulerTest, TestPostTask)
148 {
149 int result = 0;
150 m_scheduler->postTask(bind(&orderedTestTask, 1, &result));
151 m_scheduler->postTask(bind(&orderedTestTask, 2, &result));
152 m_scheduler->postTask(bind(&orderedTestTask, 3, &result));
153 m_scheduler->postTask(bind(&orderedTestTask, 4, &result));
154 runPendingTasks();
155 EXPECT_EQ(0x1234, result);
156 }
157
158 TEST_F(SchedulerTest, TestPostMixedTaskTypes)
159 {
160 int result = 0;
161 m_scheduler->postTask(bind(&unorderedTestTask, 1, &result));
162 m_scheduler->postInputTask(bind(&unorderedTestTask, 1, &result));
163 m_scheduler->postCompositorTask(bind(&unorderedTestTask, 1, &result));
164 m_scheduler->postTask(bind(&unorderedTestTask, 1, &result));
165 runPendingTasks();
166 EXPECT_EQ(4, result);
167 }
168
169 int s_sharedTimerTickCount;
170 void sharedTimerFunction()
171 {
172 s_sharedTimerTickCount++;
173 }
174
175 TEST_F(SchedulerTest, TestSharedTimer)
176 {
177 s_sharedTimerTickCount = 0;
178 m_scheduler->setSharedTimerFiredFunction(&sharedTimerFunction);
179 EXPECT_FALSE(m_platformSupport.sharedTimerRunning());
180 m_scheduler->setSharedTimerFireInterval(0);
181 EXPECT_TRUE(m_platformSupport.sharedTimerRunning());
182
183 m_platformSupport.triggerSharedTimer();
184 EXPECT_EQ(1, s_sharedTimerTickCount);
185
186 m_scheduler->stopSharedTimer();
187 EXPECT_FALSE(m_platformSupport.sharedTimerRunning());
188
189 m_scheduler->setSharedTimerFiredFunction(nullptr);
190 EXPECT_FALSE(m_platformSupport.sharedTimerRunning());
191 }
192
193 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698