Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright (C) 2014 Google Inc. All rights reserved. | |
|
eseidel
2014/07/15 15:59:11
Wrong license block.
Sami
2014/07/15 19:20:06
Done.
| |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions are | |
| 6 * met: | |
| 7 * | |
| 8 * * Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * * Redistributions in binary form must reproduce the above | |
| 11 * copyright notice, this list of conditions and the following disclaimer | |
| 12 * in the documentation and/or other materials provided with the | |
| 13 * distribution. | |
| 14 * * Neither the name of Google Inc. nor the names of its | |
| 15 * contributors may be used to endorse or promote products derived from | |
| 16 * this software without specific prior written permission. | |
| 17 * | |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 */ | |
| 30 | |
| 31 #include "config.h" | |
| 32 #include "platform/scheduler/Scheduler.h" | |
| 33 | |
| 34 #include "platform/TestingPlatformSupport.h" | |
| 35 #include "public/platform/Platform.h" | |
| 36 | |
| 37 #include <gtest/gtest.h> | |
| 38 | |
| 39 using WebCore::Scheduler; | |
| 40 | |
| 41 namespace { | |
| 42 | |
| 43 class TestMainThread : public blink::WebThread { | |
| 44 public: | |
| 45 // blink::WebThread implementation. | |
| 46 virtual void postTask(Task* task) OVERRIDE | |
| 47 { | |
| 48 m_pendingTasks.append(adoptPtr(task)); | |
| 49 } | |
| 50 | |
| 51 virtual void postDelayedTask(Task* task, long long delayMs) OVERRIDE | |
| 52 { | |
| 53 ASSERT_NOT_REACHED(); | |
| 54 } | |
| 55 | |
| 56 virtual bool isCurrentThread() const OVERRIDE | |
| 57 { | |
| 58 return true; | |
| 59 } | |
| 60 | |
| 61 virtual void enterRunLoop() OVERRIDE | |
| 62 { | |
| 63 ASSERT_NOT_REACHED(); | |
| 64 } | |
| 65 | |
| 66 virtual void exitRunLoop() OVERRIDE | |
| 67 { | |
| 68 ASSERT_NOT_REACHED(); | |
| 69 } | |
| 70 | |
| 71 void runPendingTasks() | |
| 72 { | |
| 73 while (!m_pendingTasks.isEmpty()) | |
| 74 m_pendingTasks.takeFirst()->run(); | |
| 75 } | |
| 76 | |
| 77 private: | |
| 78 WTF::Deque<OwnPtr<Task> > m_pendingTasks; | |
| 79 }; | |
| 80 | |
| 81 class SchedulerTestingPlatformSupport : WebCore::TestingPlatformSupport { | |
| 82 public: | |
| 83 SchedulerTestingPlatformSupport() | |
| 84 : TestingPlatformSupport(TestingPlatformSupport::Config()) | |
| 85 , m_sharedTimerFunction(nullptr) | |
| 86 , m_sharedTimerRunning(false) | |
| 87 , m_sharedTimerFireInterval(0) | |
| 88 { | |
| 89 } | |
| 90 | |
| 91 // blink::Platform implementation. | |
| 92 virtual blink::WebThread* currentThread() OVERRIDE | |
| 93 { | |
| 94 return &m_mainThread; | |
| 95 } | |
| 96 | |
| 97 virtual void setSharedTimerFiredFunction(SharedTimerFunction timerFunction) OVERRIDE | |
| 98 { | |
| 99 m_sharedTimerFunction = timerFunction; | |
| 100 } | |
| 101 | |
| 102 virtual void setSharedTimerFireInterval(double) | |
| 103 { | |
| 104 m_sharedTimerFireInterval = 0; | |
| 105 m_sharedTimerRunning = true; | |
| 106 } | |
| 107 | |
| 108 virtual void stopSharedTimer() | |
| 109 { | |
| 110 m_sharedTimerRunning = false; | |
| 111 } | |
| 112 | |
| 113 void runPendingTasks() | |
| 114 { | |
| 115 m_mainThread.runPendingTasks(); | |
| 116 } | |
| 117 | |
| 118 bool sharedTimerRunning() const | |
| 119 { | |
| 120 return m_sharedTimerRunning; | |
| 121 } | |
| 122 | |
| 123 double sharedTimerFireInterval() const | |
| 124 { | |
| 125 return m_sharedTimerFireInterval; | |
| 126 } | |
| 127 | |
| 128 void triggerSharedTimer() | |
| 129 { | |
| 130 m_sharedTimerFunction(); | |
| 131 } | |
| 132 | |
| 133 private: | |
| 134 TestMainThread m_mainThread; | |
| 135 SharedTimerFunction m_sharedTimerFunction; | |
| 136 bool m_sharedTimerRunning; | |
| 137 double m_sharedTimerFireInterval; | |
| 138 }; | |
| 139 | |
| 140 class SchedulerTest : public testing::Test { | |
| 141 public: | |
| 142 SchedulerTest() | |
| 143 { | |
| 144 Scheduler::initializeOnMainThread(); | |
| 145 m_scheduler = Scheduler::current(); | |
| 146 } | |
| 147 | |
| 148 ~SchedulerTest() | |
| 149 { | |
| 150 Scheduler::shutdown(); | |
| 151 } | |
| 152 | |
| 153 void runPendingTasks() | |
| 154 { | |
| 155 m_platformSupport.runPendingTasks(); | |
| 156 } | |
| 157 | |
| 158 protected: | |
| 159 SchedulerTestingPlatformSupport m_platformSupport; | |
| 160 Scheduler* m_scheduler; | |
| 161 }; | |
| 162 | |
| 163 void orderedTestTask(int value, int* result) | |
| 164 { | |
| 165 *result = (*result << 4) | value; | |
| 166 } | |
| 167 | |
| 168 void unorderedTestTask(int value, int* result) | |
| 169 { | |
| 170 *result += value; | |
| 171 } | |
| 172 | |
| 173 TEST_F(SchedulerTest, TestPostTask) | |
| 174 { | |
| 175 int result = 0; | |
| 176 m_scheduler->postTask(bind(&orderedTestTask, 1, &result)); | |
| 177 m_scheduler->postTask(bind(&orderedTestTask, 2, &result)); | |
| 178 m_scheduler->postTask(bind(&orderedTestTask, 3, &result)); | |
| 179 m_scheduler->postTask(bind(&orderedTestTask, 4, &result)); | |
| 180 runPendingTasks(); | |
| 181 EXPECT_EQ(0x1234, result); | |
| 182 } | |
| 183 | |
| 184 TEST_F(SchedulerTest, TestPostMixedTaskTypes) | |
| 185 { | |
| 186 int result = 0; | |
| 187 m_scheduler->postTask(bind(&unorderedTestTask, 1, &result)); | |
| 188 m_scheduler->postInputTask(bind(&unorderedTestTask, 1, &result)); | |
| 189 m_scheduler->postCompositorTask(bind(&unorderedTestTask, 1, &result)); | |
| 190 m_scheduler->postTask(bind(&unorderedTestTask, 1, &result)); | |
| 191 runPendingTasks(); | |
| 192 EXPECT_EQ(4, result); | |
| 193 } | |
| 194 | |
| 195 int s_sharedTimerTickCount; | |
| 196 void sharedTimerFunction() | |
| 197 { | |
| 198 s_sharedTimerTickCount++; | |
| 199 } | |
| 200 | |
| 201 TEST_F(SchedulerTest, TestSharedTimer) | |
| 202 { | |
| 203 s_sharedTimerTickCount = 0; | |
| 204 m_scheduler->setSharedTimerFiredFunction(&sharedTimerFunction); | |
| 205 EXPECT_FALSE(m_platformSupport.sharedTimerRunning()); | |
| 206 m_scheduler->setSharedTimerFireInterval(0); | |
| 207 EXPECT_TRUE(m_platformSupport.sharedTimerRunning()); | |
| 208 | |
| 209 m_platformSupport.triggerSharedTimer(); | |
| 210 EXPECT_EQ(1, s_sharedTimerTickCount); | |
| 211 | |
| 212 m_scheduler->stopSharedTimer(); | |
| 213 EXPECT_FALSE(m_platformSupport.sharedTimerRunning()); | |
| 214 | |
| 215 m_scheduler->setSharedTimerFiredFunction(nullptr); | |
| 216 EXPECT_FALSE(m_platformSupport.sharedTimerRunning()); | |
| 217 } | |
| 218 | |
| 219 } // namespace | |
| OLD | NEW |