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

Side by Side Diff: ui/compositor/compositor_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698