Chromium Code Reviews| Index: Source/platform/scheduler/SchedulerTest.cpp |
| diff --git a/Source/platform/scheduler/SchedulerTest.cpp b/Source/platform/scheduler/SchedulerTest.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3ca0c3a1eb302f384050d5e84872ee8788969495 |
| --- /dev/null |
| +++ b/Source/platform/scheduler/SchedulerTest.cpp |
| @@ -0,0 +1,219 @@ |
| +/* |
| + * 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.
|
| + * |
| + * Redistribution and use in source and binary forms, with or without |
| + * modification, are permitted provided that the following conditions are |
| + * met: |
| + * |
| + * * Redistributions of source code must retain the above copyright |
| + * notice, this list of conditions and the following disclaimer. |
| + * * Redistributions in binary form must reproduce the above |
| + * copyright notice, this list of conditions and the following disclaimer |
| + * in the documentation and/or other materials provided with the |
| + * distribution. |
| + * * Neither the name of Google Inc. nor the names of its |
| + * contributors may be used to endorse or promote products derived from |
| + * this software without specific prior written permission. |
| + * |
| + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| + */ |
| + |
| +#include "config.h" |
| +#include "platform/scheduler/Scheduler.h" |
| + |
| +#include "platform/TestingPlatformSupport.h" |
| +#include "public/platform/Platform.h" |
| + |
| +#include <gtest/gtest.h> |
| + |
| +using WebCore::Scheduler; |
| + |
| +namespace { |
| + |
| +class TestMainThread : public blink::WebThread { |
| +public: |
| + // blink::WebThread implementation. |
| + virtual void postTask(Task* task) OVERRIDE |
| + { |
| + m_pendingTasks.append(adoptPtr(task)); |
| + } |
| + |
| + virtual void postDelayedTask(Task* task, long long delayMs) OVERRIDE |
| + { |
| + ASSERT_NOT_REACHED(); |
| + } |
| + |
| + virtual bool isCurrentThread() const OVERRIDE |
| + { |
| + return true; |
| + } |
| + |
| + virtual void enterRunLoop() OVERRIDE |
| + { |
| + ASSERT_NOT_REACHED(); |
| + } |
| + |
| + virtual void exitRunLoop() OVERRIDE |
| + { |
| + ASSERT_NOT_REACHED(); |
| + } |
| + |
| + void runPendingTasks() |
| + { |
| + while (!m_pendingTasks.isEmpty()) |
| + m_pendingTasks.takeFirst()->run(); |
| + } |
| + |
| +private: |
| + WTF::Deque<OwnPtr<Task> > m_pendingTasks; |
| +}; |
| + |
| +class SchedulerTestingPlatformSupport : WebCore::TestingPlatformSupport { |
| +public: |
| + SchedulerTestingPlatformSupport() |
| + : TestingPlatformSupport(TestingPlatformSupport::Config()) |
| + , m_sharedTimerFunction(nullptr) |
| + , m_sharedTimerRunning(false) |
| + , m_sharedTimerFireInterval(0) |
| + { |
| + } |
| + |
| + // blink::Platform implementation. |
| + virtual blink::WebThread* currentThread() OVERRIDE |
| + { |
| + return &m_mainThread; |
| + } |
| + |
| + virtual void setSharedTimerFiredFunction(SharedTimerFunction timerFunction) OVERRIDE |
| + { |
| + m_sharedTimerFunction = timerFunction; |
| + } |
| + |
| + virtual void setSharedTimerFireInterval(double) |
| + { |
| + m_sharedTimerFireInterval = 0; |
| + m_sharedTimerRunning = true; |
| + } |
| + |
| + virtual void stopSharedTimer() |
| + { |
| + m_sharedTimerRunning = false; |
| + } |
| + |
| + void runPendingTasks() |
| + { |
| + m_mainThread.runPendingTasks(); |
| + } |
| + |
| + bool sharedTimerRunning() const |
| + { |
| + return m_sharedTimerRunning; |
| + } |
| + |
| + double sharedTimerFireInterval() const |
| + { |
| + return m_sharedTimerFireInterval; |
| + } |
| + |
| + void triggerSharedTimer() |
| + { |
| + m_sharedTimerFunction(); |
| + } |
| + |
| +private: |
| + TestMainThread m_mainThread; |
| + SharedTimerFunction m_sharedTimerFunction; |
| + bool m_sharedTimerRunning; |
| + double m_sharedTimerFireInterval; |
| +}; |
| + |
| +class SchedulerTest : public testing::Test { |
| +public: |
| + SchedulerTest() |
| + { |
| + Scheduler::initializeOnMainThread(); |
| + m_scheduler = Scheduler::current(); |
| + } |
| + |
| + ~SchedulerTest() |
| + { |
| + Scheduler::shutdown(); |
| + } |
| + |
| + void runPendingTasks() |
| + { |
| + m_platformSupport.runPendingTasks(); |
| + } |
| + |
| +protected: |
| + SchedulerTestingPlatformSupport m_platformSupport; |
| + Scheduler* m_scheduler; |
| +}; |
| + |
| +void orderedTestTask(int value, int* result) |
| +{ |
| + *result = (*result << 4) | value; |
| +} |
| + |
| +void unorderedTestTask(int value, int* result) |
| +{ |
| + *result += value; |
| +} |
| + |
| +TEST_F(SchedulerTest, TestPostTask) |
| +{ |
| + int result = 0; |
| + m_scheduler->postTask(bind(&orderedTestTask, 1, &result)); |
| + m_scheduler->postTask(bind(&orderedTestTask, 2, &result)); |
| + m_scheduler->postTask(bind(&orderedTestTask, 3, &result)); |
| + m_scheduler->postTask(bind(&orderedTestTask, 4, &result)); |
| + runPendingTasks(); |
| + EXPECT_EQ(0x1234, result); |
| +} |
| + |
| +TEST_F(SchedulerTest, TestPostMixedTaskTypes) |
| +{ |
| + int result = 0; |
| + m_scheduler->postTask(bind(&unorderedTestTask, 1, &result)); |
| + m_scheduler->postInputTask(bind(&unorderedTestTask, 1, &result)); |
| + m_scheduler->postCompositorTask(bind(&unorderedTestTask, 1, &result)); |
| + m_scheduler->postTask(bind(&unorderedTestTask, 1, &result)); |
| + runPendingTasks(); |
| + EXPECT_EQ(4, result); |
| +} |
| + |
| +int s_sharedTimerTickCount; |
| +void sharedTimerFunction() |
| +{ |
| + s_sharedTimerTickCount++; |
| +} |
| + |
| +TEST_F(SchedulerTest, TestSharedTimer) |
| +{ |
| + s_sharedTimerTickCount = 0; |
| + m_scheduler->setSharedTimerFiredFunction(&sharedTimerFunction); |
| + EXPECT_FALSE(m_platformSupport.sharedTimerRunning()); |
| + m_scheduler->setSharedTimerFireInterval(0); |
| + EXPECT_TRUE(m_platformSupport.sharedTimerRunning()); |
| + |
| + m_platformSupport.triggerSharedTimer(); |
| + EXPECT_EQ(1, s_sharedTimerTickCount); |
| + |
| + m_scheduler->stopSharedTimer(); |
| + EXPECT_FALSE(m_platformSupport.sharedTimerRunning()); |
| + |
| + m_scheduler->setSharedTimerFiredFunction(nullptr); |
| + EXPECT_FALSE(m_platformSupport.sharedTimerRunning()); |
| +} |
| + |
| +} // namespace |