| 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/macros.h" | 5 #include "base/macros.h" |
| 6 #include "base/run_loop.h" | 6 #include "base/run_loop.h" |
| 7 #include "base/thread_task_runner_handle.h" | 7 #include "base/thread_task_runner_handle.h" |
| 8 #include "cc/output/begin_frame_args.h" | 8 #include "cc/output/begin_frame_args.h" |
| 9 #include "cc/test/begin_frame_args_test.h" | 9 #include "cc/test/begin_frame_args_test.h" |
| 10 #include "testing/gmock/include/gmock/gmock.h" | 10 #include "testing/gmock/include/gmock/gmock.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "ui/compositor/compositor.h" | 12 #include "ui/compositor/compositor.h" |
| 13 #include "ui/compositor/layer.h" | 13 #include "ui/compositor/layer.h" |
| 14 #include "ui/compositor/test/context_factories_for_test.h" | 14 #include "ui/compositor/test/context_factories_for_test.h" |
| 15 #include "ui/compositor/test/draw_waiter_for_test.h" | 15 #include "ui/compositor/test/draw_waiter_for_test.h" |
| 16 | 16 |
| 17 using testing::Mock; | 17 using testing::Mock; |
| 18 using testing::_; | 18 using testing::_; |
| 19 | 19 |
| 20 namespace ui { | 20 namespace ui { |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 ACTION_P2(RemoveObserver, compositor, observer) { | 23 ACTION_P2(RemoveObserver, compositor, observer) { |
| 24 compositor->RemoveBeginFrameObserver(observer); | 24 compositor->RemoveBeginFrameObserver(observer); |
| 25 } | 25 } |
| 26 | 26 |
| 27 class MockCompositorBeginFrameObserver : public CompositorBeginFrameObserver { | |
| 28 public: | |
| 29 MOCK_METHOD1(OnSendBeginFrame, void(const cc::BeginFrameArgs&)); | |
| 30 }; | |
| 31 | |
| 32 // Test fixture for tests that require a ui::Compositor with a real task | 27 // Test fixture for tests that require a ui::Compositor with a real task |
| 33 // runner. | 28 // runner. |
| 34 class CompositorTest : public testing::Test { | 29 class CompositorTest : public testing::Test { |
| 35 public: | 30 public: |
| 36 CompositorTest() {} | 31 CompositorTest() {} |
| 37 ~CompositorTest() override {} | 32 ~CompositorTest() override {} |
| 38 | 33 |
| 39 void SetUp() override { | 34 void SetUp() override { |
| 40 task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 35 task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 41 | 36 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 lock = compositor()->GetCompositorLock(); | 79 lock = compositor()->GetCompositorLock(); |
| 85 EXPECT_TRUE(compositor()->IsLocked()); | 80 EXPECT_TRUE(compositor()->IsLocked()); |
| 86 task_runner()->PostDelayedTask( | 81 task_runner()->PostDelayedTask( |
| 87 FROM_HERE, run_loop.QuitClosure(), | 82 FROM_HERE, run_loop.QuitClosure(), |
| 88 base::TimeDelta::FromMilliseconds(kCompositorLockTimeoutMs)); | 83 base::TimeDelta::FromMilliseconds(kCompositorLockTimeoutMs)); |
| 89 run_loop.Run(); | 84 run_loop.Run(); |
| 90 EXPECT_TRUE(compositor()->IsLocked()); | 85 EXPECT_TRUE(compositor()->IsLocked()); |
| 91 } | 86 } |
| 92 } | 87 } |
| 93 | 88 |
| 94 TEST_F(CompositorTest, AddAndRemoveBeginFrameObserver) { | |
| 95 cc::BeginFrameArgs args = | |
| 96 cc::CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE, | |
| 97 base::TimeTicks::FromInternalValue(33)); | |
| 98 | |
| 99 MockCompositorBeginFrameObserver test_observer; | |
| 100 MockCompositorBeginFrameObserver test_observer2; | |
| 101 | |
| 102 // Add a single observer. | |
| 103 compositor()->AddBeginFrameObserver(&test_observer); | |
| 104 Mock::VerifyAndClearExpectations(&test_observer); | |
| 105 | |
| 106 // When |missed_begin_frame_args_| is sent, its type is set to MISSED. | |
| 107 cc::BeginFrameArgs expected_args(args); | |
| 108 cc::BeginFrameArgs expected_missed_args(args); | |
| 109 expected_missed_args.type = cc::BeginFrameArgs::MISSED; | |
| 110 | |
| 111 // Simulate to trigger new BeginFrame by using |args|. | |
| 112 EXPECT_CALL(test_observer, OnSendBeginFrame(expected_args)); | |
| 113 compositor()->SendBeginFramesToChildren(args); | |
| 114 Mock::VerifyAndClearExpectations(&test_observer); | |
| 115 | |
| 116 // When new observer is added, Compositor immediately calls OnSendBeginFrame | |
| 117 // with |missed_begin_frame_args_|. | |
| 118 EXPECT_CALL(test_observer2, OnSendBeginFrame(expected_missed_args)); | |
| 119 compositor()->AddBeginFrameObserver(&test_observer2); | |
| 120 Mock::VerifyAndClearExpectations(&test_observer); | |
| 121 Mock::VerifyAndClearExpectations(&test_observer2); | |
| 122 | |
| 123 // When |test_observer2| is removed and added again, it will be called again. | |
| 124 EXPECT_CALL(test_observer2, OnSendBeginFrame(expected_missed_args)); | |
| 125 compositor()->RemoveBeginFrameObserver(&test_observer2); | |
| 126 compositor()->AddBeginFrameObserver(&test_observer2); | |
| 127 Mock::VerifyAndClearExpectations(&test_observer2); | |
| 128 | |
| 129 // When all observer is removed, |missed_begin_frame_args_| is invalidated. | |
| 130 // So, it is not used for newly added observer. | |
| 131 EXPECT_CALL(test_observer2, OnSendBeginFrame(_)).Times(0); | |
| 132 compositor()->RemoveBeginFrameObserver(&test_observer); | |
| 133 compositor()->RemoveBeginFrameObserver(&test_observer2); | |
| 134 compositor()->SendBeginFramesToChildren(args); | |
| 135 compositor()->AddBeginFrameObserver(&test_observer2); | |
| 136 Mock::VerifyAndClearExpectations(&test_observer2); | |
| 137 | |
| 138 compositor()->RemoveBeginFrameObserver(&test_observer2); | |
| 139 } | |
| 140 | |
| 141 TEST_F(CompositorTest, RemoveBeginFrameObserverWhileSendingBeginFrame) { | |
| 142 cc::BeginFrameArgs args = cc::CreateBeginFrameArgsForTesting( | |
| 143 BEGINFRAME_FROM_HERE, base::TimeTicks::FromInternalValue(33)); | |
| 144 | |
| 145 cc::BeginFrameArgs expected_args(args); | |
| 146 cc::BeginFrameArgs expected_missed_args(args); | |
| 147 expected_missed_args.type = cc::BeginFrameArgs::MISSED; | |
| 148 | |
| 149 // Add both observers, and simulate removal of |test_observer2| during | |
| 150 // BeginFrame dispatch (implicitly triggered when the observer is added). | |
| 151 MockCompositorBeginFrameObserver test_observer; | |
| 152 MockCompositorBeginFrameObserver test_observer2; | |
| 153 EXPECT_CALL(test_observer, OnSendBeginFrame(expected_args)); | |
| 154 EXPECT_CALL(test_observer2, OnSendBeginFrame(expected_missed_args)) | |
| 155 .WillOnce(RemoveObserver(compositor(), &test_observer2)); | |
| 156 | |
| 157 // When a new observer is added, Compositor immediately calls OnSendBeginFrame | |
| 158 // with |missed_begin_frame_args_|. | |
| 159 compositor()->AddBeginFrameObserver(&test_observer); | |
| 160 compositor()->SendBeginFramesToChildren(args); | |
| 161 compositor()->AddBeginFrameObserver(&test_observer2); | |
| 162 Mock::VerifyAndClearExpectations(&test_observer); | |
| 163 Mock::VerifyAndClearExpectations(&test_observer2); | |
| 164 | |
| 165 // |test_observer2| was removed during the previous implicit BeginFrame | |
| 166 // dispatch, and should not get the new frame. | |
| 167 expected_args.type = cc::BeginFrameArgs::NORMAL; | |
| 168 EXPECT_CALL(test_observer, OnSendBeginFrame(expected_args)); | |
| 169 EXPECT_CALL(test_observer2, OnSendBeginFrame(_)).Times(0); | |
| 170 compositor()->SendBeginFramesToChildren(args); | |
| 171 Mock::VerifyAndClearExpectations(&test_observer); | |
| 172 Mock::VerifyAndClearExpectations(&test_observer2); | |
| 173 | |
| 174 // Now remove |test_observer| during explicit BeginFrame dispatch. | |
| 175 EXPECT_CALL(test_observer, OnSendBeginFrame(expected_args)) | |
| 176 .WillOnce(RemoveObserver(compositor(), &test_observer)); | |
| 177 EXPECT_CALL(test_observer2, OnSendBeginFrame(_)).Times(0); | |
| 178 compositor()->SendBeginFramesToChildren(args); | |
| 179 Mock::VerifyAndClearExpectations(&test_observer); | |
| 180 Mock::VerifyAndClearExpectations(&test_observer2); | |
| 181 | |
| 182 // No observers should get the new frame. | |
| 183 EXPECT_CALL(test_observer, OnSendBeginFrame(_)).Times(0); | |
| 184 EXPECT_CALL(test_observer2, OnSendBeginFrame(_)).Times(0); | |
| 185 compositor()->SendBeginFramesToChildren(args); | |
| 186 Mock::VerifyAndClearExpectations(&test_observer); | |
| 187 Mock::VerifyAndClearExpectations(&test_observer2); | |
| 188 | |
| 189 // Adding a new observer should not trigger a missed frame, as the | |
| 190 // previous frame had no observers. | |
| 191 EXPECT_CALL(test_observer, OnSendBeginFrame(_)).Times(0); | |
| 192 compositor()->AddBeginFrameObserver(&test_observer); | |
| 193 compositor()->RemoveBeginFrameObserver(&test_observer); | |
| 194 Mock::VerifyAndClearExpectations(&test_observer); | |
| 195 } | |
| 196 | |
| 197 TEST_F(CompositorTest, ReleaseWidgetWithOutputSurfaceNeverCreated) { | 89 TEST_F(CompositorTest, ReleaseWidgetWithOutputSurfaceNeverCreated) { |
| 198 compositor()->SetVisible(false); | 90 compositor()->SetVisible(false); |
| 199 EXPECT_EQ(gfx::kNullAcceleratedWidget, | 91 EXPECT_EQ(gfx::kNullAcceleratedWidget, |
| 200 compositor()->ReleaseAcceleratedWidget()); | 92 compositor()->ReleaseAcceleratedWidget()); |
| 201 compositor()->SetAcceleratedWidget(gfx::kNullAcceleratedWidget); | 93 compositor()->SetAcceleratedWidget(gfx::kNullAcceleratedWidget); |
| 202 compositor()->SetVisible(true); | 94 compositor()->SetVisible(true); |
| 203 } | 95 } |
| 204 | 96 |
| 205 TEST_F(CompositorTest, CreateAndReleaseOutputSurface) { | 97 TEST_F(CompositorTest, CreateAndReleaseOutputSurface) { |
| 206 scoped_ptr<Layer> root_layer(new Layer(ui::LAYER_SOLID_COLOR)); | 98 scoped_ptr<Layer> root_layer(new Layer(ui::LAYER_SOLID_COLOR)); |
| 207 root_layer->SetBounds(gfx::Rect(10, 10)); | 99 root_layer->SetBounds(gfx::Rect(10, 10)); |
| 208 compositor()->SetRootLayer(root_layer.get()); | 100 compositor()->SetRootLayer(root_layer.get()); |
| 209 compositor()->SetScaleAndSize(1.0f, gfx::Size(10, 10)); | 101 compositor()->SetScaleAndSize(1.0f, gfx::Size(10, 10)); |
| 210 DCHECK(compositor()->IsVisible()); | 102 DCHECK(compositor()->IsVisible()); |
| 211 compositor()->ScheduleDraw(); | 103 compositor()->ScheduleDraw(); |
| 212 DrawWaiterForTest::WaitForCompositingEnded(compositor()); | 104 DrawWaiterForTest::WaitForCompositingEnded(compositor()); |
| 213 compositor()->SetVisible(false); | 105 compositor()->SetVisible(false); |
| 214 EXPECT_EQ(gfx::kNullAcceleratedWidget, | 106 EXPECT_EQ(gfx::kNullAcceleratedWidget, |
| 215 compositor()->ReleaseAcceleratedWidget()); | 107 compositor()->ReleaseAcceleratedWidget()); |
| 216 compositor()->SetAcceleratedWidget(gfx::kNullAcceleratedWidget); | 108 compositor()->SetAcceleratedWidget(gfx::kNullAcceleratedWidget); |
| 217 compositor()->SetVisible(true); | 109 compositor()->SetVisible(true); |
| 218 compositor()->ScheduleDraw(); | 110 compositor()->ScheduleDraw(); |
| 219 DrawWaiterForTest::WaitForCompositingEnded(compositor()); | 111 DrawWaiterForTest::WaitForCompositingEnded(compositor()); |
| 220 compositor()->SetRootLayer(nullptr); | 112 compositor()->SetRootLayer(nullptr); |
| 221 } | 113 } |
| 222 | 114 |
| 223 } // namespace ui | 115 } // namespace ui |
| OLD | NEW |