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

Side by Side 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: address review comments 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/test/test_simple_task_runner.h" 5 #include "base/single_thread_task_runner.h"
6 #include "base/test/test_pending_task.h"
7 #include "base/threading/platform_thread.h"
6 #include "cc/output/begin_frame_args.h" 8 #include "cc/output/begin_frame_args.h"
7 #include "cc/test/begin_frame_args_test.h" 9 #include "cc/test/begin_frame_args_test.h"
8 #include "testing/gmock/include/gmock/gmock.h" 10 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/compositor/compositor.h" 12 #include "ui/compositor/compositor.h"
11 #include "ui/compositor/test/context_factories_for_test.h" 13 #include "ui/compositor/test/context_factories_for_test.h"
12 14
13 using testing::Mock; 15 using testing::Mock;
14 using testing::_; 16 using testing::_;
15 17
16 namespace ui { 18 namespace ui {
17 namespace { 19 namespace {
18 20
19 class MockCompositorBeginFrameObserver : public CompositorBeginFrameObserver { 21 class MockCompositorBeginFrameObserver : public CompositorBeginFrameObserver {
20 public: 22 public:
21 MOCK_METHOD1(OnSendBeginFrame, void(const cc::BeginFrameArgs&)); 23 MOCK_METHOD1(OnSendBeginFrame, void(const cc::BeginFrameArgs&));
22 }; 24 };
23 25
26 // Thread safe and slim version of base::TestSimpleTaskRunner. This is done by
27 // using
28 // mutex to protect pending_tasks_.
reveman 2015/04/14 22:42:10 nit: fix line wrapping
29 class TestThreadSafeSimpleTaskRunner : public base::SingleThreadTaskRunner {
30 public:
31 TestThreadSafeSimpleTaskRunner()
32 : thread_id_(base::PlatformThread::CurrentId()) {}
33
34 // SingleThreadTaskRunner implementation.
35 bool PostDelayedTask(const tracked_objects::Location& from_here,
36 const base::Closure& task,
37 base::TimeDelta delay) override {
38 base::AutoLock lock(pending_tasks_lock_);
39 pending_tasks_.push_back(
40 base::TestPendingTask(from_here, task, base::TimeTicks(), delay,
41 base::TestPendingTask::NESTABLE));
42 return true;
43 }
44
45 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
46 const base::Closure& task,
47 base::TimeDelta delay) override {
48 base::AutoLock lock(pending_tasks_lock_);
49 pending_tasks_.push_back(
50 base::TestPendingTask(from_here, task, base::TimeTicks(), delay,
51 base::TestPendingTask::NON_NESTABLE));
52 return true;
53 }
54
55 bool RunsTasksOnCurrentThread() const override {
56 return thread_id_ == base::PlatformThread::CurrentId();
57 }
58
59 // Runs each current pending task in order and clears the queue.
60 // Any tasks posted by the tasks are not run.
61 bool RunPendingTasks() {
62 std::deque<base::TestPendingTask> tasks_to_run;
63 {
64 base::AutoLock lock(pending_tasks_lock_);
65 tasks_to_run.swap(pending_tasks_);
66
67 if (tasks_to_run.empty())
reveman 2015/04/14 22:42:10 nit: you can remove this and just "return !tasks_t
68 return false;
69 }
70
71 for (std::deque<base::TestPendingTask>::iterator it = tasks_to_run.begin();
72 it != tasks_to_run.end(); ++it) {
73 it->task.Run();
74 }
75
76 return true;
77 }
78
79 // Runs pending tasks until the queue is empty.
80 void RunUntilIdle() {
81 while (RunPendingTasks())
82 continue;
83 }
84
85 protected:
86 ~TestThreadSafeSimpleTaskRunner() override {}
87
88 base::PlatformThreadId thread_id_;
89 std::deque<base::TestPendingTask> pending_tasks_;
90 // The lock that protects access to |pending_tasks_|.
91 base::Lock pending_tasks_lock_;
92
93 private:
94 DISALLOW_COPY_AND_ASSIGN(TestThreadSafeSimpleTaskRunner);
95 };
96
24 // Test fixture for tests that require a ui::Compositor with a real task 97 // Test fixture for tests that require a ui::Compositor with a real task
25 // runner. 98 // runner.
26 class CompositorTest : public testing::Test { 99 class CompositorTest : public testing::Test {
27 public: 100 public:
28 CompositorTest() {} 101 CompositorTest() {}
29 ~CompositorTest() override {} 102 ~CompositorTest() override {}
30 103
31 void SetUp() override { 104 void SetUp() override {
32 task_runner_ = new base::TestSimpleTaskRunner; 105 task_runner_ = new TestThreadSafeSimpleTaskRunner;
33 106
34 ui::ContextFactory* context_factory = 107 ui::ContextFactory* context_factory =
35 ui::InitializeContextFactoryForTests(false); 108 ui::InitializeContextFactoryForTests(false);
36 109
37 compositor_.reset(new ui::Compositor(gfx::kNullAcceleratedWidget, 110 compositor_.reset(new ui::Compositor(gfx::kNullAcceleratedWidget,
38 context_factory, task_runner_)); 111 context_factory, task_runner_));
39 } 112 }
40 void TearDown() override { 113 void TearDown() override {
41 compositor_.reset(); 114 compositor_.reset();
42 ui::TerminateContextFactoryForTests(); 115 ui::TerminateContextFactoryForTests();
43 } 116 }
44 117
45 protected: 118 protected:
46 base::TestSimpleTaskRunner* task_runner() { return task_runner_.get(); } 119 TestThreadSafeSimpleTaskRunner* task_runner() { return task_runner_.get(); }
47 ui::Compositor* compositor() { return compositor_.get(); } 120 ui::Compositor* compositor() { return compositor_.get(); }
48 121
49 private: 122 private:
50 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 123 scoped_refptr<TestThreadSafeSimpleTaskRunner> task_runner_;
51 scoped_ptr<ui::Compositor> compositor_; 124 scoped_ptr<ui::Compositor> compositor_;
52 125
53 DISALLOW_COPY_AND_ASSIGN(CompositorTest); 126 DISALLOW_COPY_AND_ASSIGN(CompositorTest);
54 }; 127 };
55 128
56 } // namespace 129 } // namespace
57 130
58 TEST_F(CompositorTest, LocksTimeOut) { 131 TEST_F(CompositorTest, LocksTimeOut) {
59 scoped_refptr<ui::CompositorLock> lock; 132 scoped_refptr<ui::CompositorLock> lock;
60 133
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 EXPECT_CALL(test_observer2, OnSendBeginFrame(_)).Times(0); 180 EXPECT_CALL(test_observer2, OnSendBeginFrame(_)).Times(0);
108 compositor()->RemoveBeginFrameObserver(&test_observer); 181 compositor()->RemoveBeginFrameObserver(&test_observer);
109 compositor()->RemoveBeginFrameObserver(&test_observer2); 182 compositor()->RemoveBeginFrameObserver(&test_observer2);
110 compositor()->AddBeginFrameObserver(&test_observer2); 183 compositor()->AddBeginFrameObserver(&test_observer2);
111 Mock::VerifyAndClearExpectations(&test_observer2); 184 Mock::VerifyAndClearExpectations(&test_observer2);
112 185
113 compositor()->RemoveBeginFrameObserver(&test_observer2); 186 compositor()->RemoveBeginFrameObserver(&test_observer2);
114 } 187 }
115 188
116 } // namespace ui 189 } // namespace ui
OLDNEW
« 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