OLD | NEW |
---|---|
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 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.
| |
27 // mutex to proect pending_tasks_. | |
reveman
2015/04/14 21:47:19
protect
weiliangc
2015/04/14 22:07:53
Done.
| |
28 class TestThreadSimpleTaskRunner : public base::SingleThreadTaskRunner { | |
reveman
2015/04/14 21:47:19
nit: s/TestThreadSimpleTaskRunner/TestThreadSafeSi
weiliangc
2015/04/14 22:07:53
Done.
| |
29 public: | |
30 TestThreadSimpleTaskRunner() | |
31 : thread_id_(base::PlatformThread::CurrentId()) {} | |
32 | |
33 // SingleThreadTaskRunner implementation. | |
34 bool PostDelayedTask(const tracked_objects::Location& from_here, | |
35 const base::Closure& task, | |
36 base::TimeDelta delay) override { | |
37 base::AutoLock lock(pending_tasks_lock_); | |
38 pending_tasks_.push_back( | |
39 base::TestPendingTask(from_here, task, base::TimeTicks(), delay, | |
40 base::TestPendingTask::NESTABLE)); | |
41 return true; | |
42 } | |
43 | |
44 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | |
45 const base::Closure& task, | |
46 base::TimeDelta delay) override { | |
47 base::AutoLock lock(pending_tasks_lock_); | |
48 pending_tasks_.push_back( | |
49 base::TestPendingTask(from_here, task, base::TimeTicks(), delay, | |
50 base::TestPendingTask::NON_NESTABLE)); | |
51 return true; | |
52 } | |
53 | |
54 bool RunsTasksOnCurrentThread() const override { | |
55 return thread_id_ == base::PlatformThread::CurrentId(); | |
56 } | |
57 | |
58 // Clears the queue of pending tasks without running them. | |
59 void ClearPendingTasks() { | |
reveman
2015/04/14 21:47:19
Do you need this function?
weiliangc
2015/04/14 22:07:53
Not really. Gone.
| |
60 base::AutoLock lock(pending_tasks_lock_); | |
61 | |
62 pending_tasks_.clear(); | |
63 } | |
64 | |
65 // Runs each current pending task in order and clears the queue. | |
66 // Any tasks posted by the tasks are not run. | |
67 void RunPendingTasks() { | |
68 std::deque<base::TestPendingTask> tasks_to_run; | |
69 { | |
70 base::AutoLock lock(pending_tasks_lock_); | |
71 tasks_to_run.swap(pending_tasks_); | |
72 } | |
73 | |
74 for (std::deque<base::TestPendingTask>::iterator it = tasks_to_run.begin(); | |
75 it != tasks_to_run.end(); ++it) { | |
76 it->task.Run(); | |
77 } | |
78 } | |
79 | |
80 // Runs pending tasks until the queue is empty. | |
81 void RunUntilIdle() { | |
82 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.
| |
83 RunPendingTasks(); | |
84 } | |
85 } | |
86 | |
87 protected: | |
88 ~TestThreadSimpleTaskRunner() override {} | |
89 | |
90 base::PlatformThreadId thread_id_; | |
91 std::deque<base::TestPendingTask> pending_tasks_; | |
92 // The lock that protects access to |pending_tasks_|. | |
93 base::Lock pending_tasks_lock_; | |
94 | |
95 private: | |
96 DISALLOW_COPY_AND_ASSIGN(TestThreadSimpleTaskRunner); | |
97 }; | |
98 | |
24 // Test fixture for tests that require a ui::Compositor with a real task | 99 // Test fixture for tests that require a ui::Compositor with a real task |
25 // runner. | 100 // runner. |
26 class CompositorTest : public testing::Test { | 101 class CompositorTest : public testing::Test { |
27 public: | 102 public: |
28 CompositorTest() {} | 103 CompositorTest() {} |
29 ~CompositorTest() override {} | 104 ~CompositorTest() override {} |
30 | 105 |
31 void SetUp() override { | 106 void SetUp() override { |
32 task_runner_ = new base::TestSimpleTaskRunner; | 107 task_runner_ = new TestThreadSimpleTaskRunner; |
33 | 108 |
34 ui::ContextFactory* context_factory = | 109 ui::ContextFactory* context_factory = |
35 ui::InitializeContextFactoryForTests(false); | 110 ui::InitializeContextFactoryForTests(false); |
36 | 111 |
37 compositor_.reset(new ui::Compositor(gfx::kNullAcceleratedWidget, | 112 compositor_.reset(new ui::Compositor(gfx::kNullAcceleratedWidget, |
38 context_factory, task_runner_)); | 113 context_factory, task_runner_)); |
39 } | 114 } |
40 void TearDown() override { | 115 void TearDown() override { |
41 compositor_.reset(); | 116 compositor_.reset(); |
42 ui::TerminateContextFactoryForTests(); | 117 ui::TerminateContextFactoryForTests(); |
43 } | 118 } |
44 | 119 |
45 protected: | 120 protected: |
46 base::TestSimpleTaskRunner* task_runner() { return task_runner_.get(); } | 121 TestThreadSimpleTaskRunner* task_runner() { return task_runner_.get(); } |
47 ui::Compositor* compositor() { return compositor_.get(); } | 122 ui::Compositor* compositor() { return compositor_.get(); } |
48 | 123 |
49 private: | 124 private: |
50 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 125 scoped_refptr<TestThreadSimpleTaskRunner> task_runner_; |
51 scoped_ptr<ui::Compositor> compositor_; | 126 scoped_ptr<ui::Compositor> compositor_; |
52 | 127 |
53 DISALLOW_COPY_AND_ASSIGN(CompositorTest); | 128 DISALLOW_COPY_AND_ASSIGN(CompositorTest); |
54 }; | 129 }; |
55 | 130 |
56 } // namespace | 131 } // namespace |
57 | 132 |
58 TEST_F(CompositorTest, LocksTimeOut) { | 133 TEST_F(CompositorTest, LocksTimeOut) { |
59 scoped_refptr<ui::CompositorLock> lock; | 134 scoped_refptr<ui::CompositorLock> lock; |
60 | 135 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
107 EXPECT_CALL(test_observer2, OnSendBeginFrame(_)).Times(0); | 182 EXPECT_CALL(test_observer2, OnSendBeginFrame(_)).Times(0); |
108 compositor()->RemoveBeginFrameObserver(&test_observer); | 183 compositor()->RemoveBeginFrameObserver(&test_observer); |
109 compositor()->RemoveBeginFrameObserver(&test_observer2); | 184 compositor()->RemoveBeginFrameObserver(&test_observer2); |
110 compositor()->AddBeginFrameObserver(&test_observer2); | 185 compositor()->AddBeginFrameObserver(&test_observer2); |
111 Mock::VerifyAndClearExpectations(&test_observer2); | 186 Mock::VerifyAndClearExpectations(&test_observer2); |
112 | 187 |
113 compositor()->RemoveBeginFrameObserver(&test_observer2); | 188 compositor()->RemoveBeginFrameObserver(&test_observer2); |
114 } | 189 } |
115 | 190 |
116 } // namespace ui | 191 } // namespace ui |
OLD | NEW |