Chromium Code Reviews| Index: ui/compositor/compositor_unittest.cc |
| diff --git a/ui/compositor/compositor_unittest.cc b/ui/compositor/compositor_unittest.cc |
| index 6b42e62199f5505dd9ba90fc2efae6ea25044cec..756bfc3723dcd2d29fd9eb302ba032e83f165c38 100644 |
| --- a/ui/compositor/compositor_unittest.cc |
| +++ b/ui/compositor/compositor_unittest.cc |
| @@ -2,7 +2,9 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#include "base/test/test_simple_task_runner.h" |
| +#include "base/single_thread_task_runner.h" |
| +#include "base/test/test_pending_task.h" |
| +#include "base/threading/platform_thread.h" |
| #include "cc/output/begin_frame_args.h" |
| #include "cc/test/begin_frame_args_test.h" |
| #include "testing/gmock/include/gmock/gmock.h" |
| @@ -21,6 +23,77 @@ class MockCompositorBeginFrameObserver : public CompositorBeginFrameObserver { |
| MOCK_METHOD1(OnSendBeginFrame, void(const cc::BeginFrameArgs&)); |
| }; |
| +// Thread safe and slim version of base::TestSimpleTaskRunner. This is done by |
| +// using |
| +// mutex to protect pending_tasks_. |
|
reveman
2015/04/14 22:42:10
nit: fix line wrapping
|
| +class TestThreadSafeSimpleTaskRunner : public base::SingleThreadTaskRunner { |
| + public: |
| + TestThreadSafeSimpleTaskRunner() |
| + : thread_id_(base::PlatformThread::CurrentId()) {} |
| + |
| + // SingleThreadTaskRunner implementation. |
| + bool PostDelayedTask(const tracked_objects::Location& from_here, |
| + const base::Closure& task, |
| + base::TimeDelta delay) override { |
| + base::AutoLock lock(pending_tasks_lock_); |
| + pending_tasks_.push_back( |
| + base::TestPendingTask(from_here, task, base::TimeTicks(), delay, |
| + base::TestPendingTask::NESTABLE)); |
| + return true; |
| + } |
| + |
| + bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
| + const base::Closure& task, |
| + base::TimeDelta delay) override { |
| + base::AutoLock lock(pending_tasks_lock_); |
| + pending_tasks_.push_back( |
| + base::TestPendingTask(from_here, task, base::TimeTicks(), delay, |
| + base::TestPendingTask::NON_NESTABLE)); |
| + return true; |
| + } |
| + |
| + bool RunsTasksOnCurrentThread() const override { |
| + return thread_id_ == base::PlatformThread::CurrentId(); |
| + } |
| + |
| + // Runs each current pending task in order and clears the queue. |
| + // Any tasks posted by the tasks are not run. |
| + bool RunPendingTasks() { |
| + std::deque<base::TestPendingTask> tasks_to_run; |
| + { |
| + base::AutoLock lock(pending_tasks_lock_); |
| + tasks_to_run.swap(pending_tasks_); |
| + |
| + if (tasks_to_run.empty()) |
|
reveman
2015/04/14 22:42:10
nit: you can remove this and just "return !tasks_t
|
| + return false; |
| + } |
| + |
| + for (std::deque<base::TestPendingTask>::iterator it = tasks_to_run.begin(); |
| + it != tasks_to_run.end(); ++it) { |
| + it->task.Run(); |
| + } |
| + |
| + return true; |
| + } |
| + |
| + // Runs pending tasks until the queue is empty. |
| + void RunUntilIdle() { |
| + while (RunPendingTasks()) |
| + continue; |
| + } |
| + |
| + protected: |
| + ~TestThreadSafeSimpleTaskRunner() override {} |
| + |
| + base::PlatformThreadId thread_id_; |
| + std::deque<base::TestPendingTask> pending_tasks_; |
| + // The lock that protects access to |pending_tasks_|. |
| + base::Lock pending_tasks_lock_; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(TestThreadSafeSimpleTaskRunner); |
| +}; |
| + |
| // Test fixture for tests that require a ui::Compositor with a real task |
| // runner. |
| class CompositorTest : public testing::Test { |
| @@ -29,7 +102,7 @@ class CompositorTest : public testing::Test { |
| ~CompositorTest() override {} |
| void SetUp() override { |
| - task_runner_ = new base::TestSimpleTaskRunner; |
| + task_runner_ = new TestThreadSafeSimpleTaskRunner; |
| ui::ContextFactory* context_factory = |
| ui::InitializeContextFactoryForTests(false); |
| @@ -43,11 +116,11 @@ class CompositorTest : public testing::Test { |
| } |
| protected: |
| - base::TestSimpleTaskRunner* task_runner() { return task_runner_.get(); } |
| + TestThreadSafeSimpleTaskRunner* task_runner() { return task_runner_.get(); } |
| ui::Compositor* compositor() { return compositor_.get(); } |
| private: |
| - scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| + scoped_refptr<TestThreadSafeSimpleTaskRunner> task_runner_; |
| scoped_ptr<ui::Compositor> compositor_; |
| DISALLOW_COPY_AND_ASSIGN(CompositorTest); |