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

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

Powered by Google App Engine
This is Rietveld 408576698