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

Unified Diff: ui/compositor/compositor_unittest.cc

Issue 1081223002: Use MessageLoop for CompositorTest.LocksTimeOut (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@impl
Patch Set: thread safe task runner Created 5 years, 8 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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);
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698