Index: ui/compositor/compositor_unittest.cc |
diff --git a/ui/compositor/compositor_unittest.cc b/ui/compositor/compositor_unittest.cc |
index 6b42e62199f5505dd9ba90fc2efae6ea25044cec..5e204747eb62c589d013a986d284460a64514ecd 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,79 @@ class MockCompositorBeginFrameObserver : public CompositorBeginFrameObserver { |
MOCK_METHOD1(OnSendBeginFrame, void(const cc::BeginFrameArgs&)); |
}; |
+// Thread safe and slim version of TestSimpleTaskRunner. This is done by using |
reveman
2015/04/14 21:47:19
nit: s/TestSimpleTaskRunner/base::TestSimpleTaskRu
weiliangc
2015/04/14 22:07:53
Done.
|
+// mutex to proect pending_tasks_. |
reveman
2015/04/14 21:47:19
protect
weiliangc
2015/04/14 22:07:53
Done.
|
+class TestThreadSimpleTaskRunner : public base::SingleThreadTaskRunner { |
reveman
2015/04/14 21:47:19
nit: s/TestThreadSimpleTaskRunner/TestThreadSafeSi
weiliangc
2015/04/14 22:07:53
Done.
|
+ public: |
+ TestThreadSimpleTaskRunner() |
+ : 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(); |
+ } |
+ |
+ // Clears the queue of pending tasks without running them. |
+ void ClearPendingTasks() { |
reveman
2015/04/14 21:47:19
Do you need this function?
weiliangc
2015/04/14 22:07:53
Not really. Gone.
|
+ base::AutoLock lock(pending_tasks_lock_); |
+ |
+ pending_tasks_.clear(); |
+ } |
+ |
+ // Runs each current pending task in order and clears the queue. |
+ // Any tasks posted by the tasks are not run. |
+ void RunPendingTasks() { |
+ std::deque<base::TestPendingTask> tasks_to_run; |
+ { |
+ base::AutoLock lock(pending_tasks_lock_); |
+ tasks_to_run.swap(pending_tasks_); |
+ } |
+ |
+ for (std::deque<base::TestPendingTask>::iterator it = tasks_to_run.begin(); |
+ it != tasks_to_run.end(); ++it) { |
+ it->task.Run(); |
+ } |
+ } |
+ |
+ // Runs pending tasks until the queue is empty. |
+ void RunUntilIdle() { |
+ while (!pending_tasks_.empty()) { |
reveman
2015/04/14 21:47:19
This is not thread safe. You need to acquire the l
weiliangc
2015/04/14 22:07:53
Done.
|
+ RunPendingTasks(); |
+ } |
+ } |
+ |
+ protected: |
+ ~TestThreadSimpleTaskRunner() 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(TestThreadSimpleTaskRunner); |
+}; |
+ |
// Test fixture for tests that require a ui::Compositor with a real task |
// runner. |
class CompositorTest : public testing::Test { |
@@ -29,7 +104,7 @@ class CompositorTest : public testing::Test { |
~CompositorTest() override {} |
void SetUp() override { |
- task_runner_ = new base::TestSimpleTaskRunner; |
+ task_runner_ = new TestThreadSimpleTaskRunner; |
ui::ContextFactory* context_factory = |
ui::InitializeContextFactoryForTests(false); |
@@ -43,11 +118,11 @@ class CompositorTest : public testing::Test { |
} |
protected: |
- base::TestSimpleTaskRunner* task_runner() { return task_runner_.get(); } |
+ TestThreadSimpleTaskRunner* task_runner() { return task_runner_.get(); } |
ui::Compositor* compositor() { return compositor_.get(); } |
private: |
- scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
+ scoped_refptr<TestThreadSimpleTaskRunner> task_runner_; |
scoped_ptr<ui::Compositor> compositor_; |
DISALLOW_COPY_AND_ASSIGN(CompositorTest); |