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

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: Test Created 5 years, 2 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
« ui/compositor/compositor.cc ('K') | « 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 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 // So, it is not used for newly added observer. 124 // So, it is not used for newly added observer.
121 EXPECT_CALL(test_observer2, OnSendBeginFrame(_)).Times(0); 125 EXPECT_CALL(test_observer2, OnSendBeginFrame(_)).Times(0);
122 compositor()->RemoveBeginFrameObserver(&test_observer); 126 compositor()->RemoveBeginFrameObserver(&test_observer);
123 compositor()->RemoveBeginFrameObserver(&test_observer2); 127 compositor()->RemoveBeginFrameObserver(&test_observer2);
124 compositor()->AddBeginFrameObserver(&test_observer2); 128 compositor()->AddBeginFrameObserver(&test_observer2);
125 Mock::VerifyAndClearExpectations(&test_observer2); 129 Mock::VerifyAndClearExpectations(&test_observer2);
126 130
127 compositor()->RemoveBeginFrameObserver(&test_observer2); 131 compositor()->RemoveBeginFrameObserver(&test_observer2);
128 } 132 }
129 133
134 TEST_F(CompositorTest, RemoveBeginFrameObserverWhileSendingBeginFrame) {
135 cc::BeginFrameArgs args = cc::CreateBeginFrameArgsForTesting(
136 BEGINFRAME_FROM_HERE, base::TimeTicks::FromInternalValue(33));
137
138 // Simulate to trigger new BeginFrame by using |args|.
139 compositor()->SendBeginFramesToChildren(args);
140
141 // When |missed_begin_frame_args_| is sent, its type is set to MISSED.
142 cc::BeginFrameArgs expected_args(args);
143 expected_args.type = cc::BeginFrameArgs::MISSED;
144
145 // Add both observers, and simulate removal of |test_observer2| during
146 // BeginFrame dispatch (implicitly triggered when the observer is added).
147 MockCompositorBeginFrameObserver test_observer;
148 MockCompositorBeginFrameObserver test_observer2;
149 EXPECT_CALL(test_observer, OnSendBeginFrame(expected_args));
150 EXPECT_CALL(test_observer2, OnSendBeginFrame(expected_args))
151 .WillOnce(RemoveObserver(compositor(), &test_observer2));
152
153 // When a new observer is added, Compositor immediately calls OnSendBeginFrame
154 // with |missed_begin_frame_args_|.
155 compositor()->AddBeginFrameObserver(&test_observer);
156 compositor()->AddBeginFrameObserver(&test_observer2);
157 Mock::VerifyAndClearExpectations(&test_observer);
158 Mock::VerifyAndClearExpectations(&test_observer2);
159
160 // |test_observer2| was removed during the previous implicit BeginFrame
161 // dispatch, and should not get the new frame.
162 expected_args.type = cc::BeginFrameArgs::NORMAL;
163 EXPECT_CALL(test_observer, OnSendBeginFrame(expected_args));
164 EXPECT_CALL(test_observer2, OnSendBeginFrame(expected_args)).Times(0);
165 compositor()->SendBeginFramesToChildren(args);
166 Mock::VerifyAndClearExpectations(&test_observer);
167 Mock::VerifyAndClearExpectations(&test_observer2);
168
169 // Now remove |test_observer| during explicit BeginFrame dispatch.
170 EXPECT_CALL(test_observer, OnSendBeginFrame(expected_args))
171 .WillOnce(RemoveObserver(compositor(), &test_observer));
172 EXPECT_CALL(test_observer2, OnSendBeginFrame(expected_args)).Times(0);
173 compositor()->SendBeginFramesToChildren(args);
174 Mock::VerifyAndClearExpectations(&test_observer);
175 Mock::VerifyAndClearExpectations(&test_observer2);
176
177 // No observers should get the new frame.
178 EXPECT_CALL(test_observer, OnSendBeginFrame(expected_args)).Times(0);
179 EXPECT_CALL(test_observer2, OnSendBeginFrame(expected_args)).Times(0);
180 compositor()->SendBeginFramesToChildren(args);
181 Mock::VerifyAndClearExpectations(&test_observer);
182 Mock::VerifyAndClearExpectations(&test_observer2);
183 }
184
130 TEST_F(CompositorTest, ReleaseWidgetWithOutputSurfaceNeverCreated) { 185 TEST_F(CompositorTest, ReleaseWidgetWithOutputSurfaceNeverCreated) {
131 compositor()->SetVisible(false); 186 compositor()->SetVisible(false);
132 EXPECT_EQ(gfx::kNullAcceleratedWidget, 187 EXPECT_EQ(gfx::kNullAcceleratedWidget,
133 compositor()->ReleaseAcceleratedWidget()); 188 compositor()->ReleaseAcceleratedWidget());
134 compositor()->SetAcceleratedWidget(gfx::kNullAcceleratedWidget); 189 compositor()->SetAcceleratedWidget(gfx::kNullAcceleratedWidget);
135 compositor()->SetVisible(true); 190 compositor()->SetVisible(true);
136 } 191 }
137 192
138 TEST_F(CompositorTest, CreateAndReleaseOutputSurface) { 193 TEST_F(CompositorTest, CreateAndReleaseOutputSurface) {
139 scoped_ptr<Layer> root_layer(new Layer(ui::LAYER_SOLID_COLOR)); 194 scoped_ptr<Layer> root_layer(new Layer(ui::LAYER_SOLID_COLOR));
140 root_layer->SetBounds(gfx::Rect(10, 10)); 195 root_layer->SetBounds(gfx::Rect(10, 10));
141 compositor()->SetRootLayer(root_layer.get()); 196 compositor()->SetRootLayer(root_layer.get());
142 compositor()->SetScaleAndSize(1.0f, gfx::Size(10, 10)); 197 compositor()->SetScaleAndSize(1.0f, gfx::Size(10, 10));
143 DCHECK(compositor()->IsVisible()); 198 DCHECK(compositor()->IsVisible());
144 compositor()->ScheduleDraw(); 199 compositor()->ScheduleDraw();
145 DrawWaiterForTest::WaitForCompositingEnded(compositor()); 200 DrawWaiterForTest::WaitForCompositingEnded(compositor());
146 compositor()->SetVisible(false); 201 compositor()->SetVisible(false);
147 EXPECT_EQ(gfx::kNullAcceleratedWidget, 202 EXPECT_EQ(gfx::kNullAcceleratedWidget,
148 compositor()->ReleaseAcceleratedWidget()); 203 compositor()->ReleaseAcceleratedWidget());
149 compositor()->SetAcceleratedWidget(gfx::kNullAcceleratedWidget); 204 compositor()->SetAcceleratedWidget(gfx::kNullAcceleratedWidget);
150 compositor()->SetVisible(true); 205 compositor()->SetVisible(true);
151 compositor()->ScheduleDraw(); 206 compositor()->ScheduleDraw();
152 DrawWaiterForTest::WaitForCompositingEnded(compositor()); 207 DrawWaiterForTest::WaitForCompositingEnded(compositor());
153 compositor()->SetRootLayer(nullptr); 208 compositor()->SetRootLayer(nullptr);
154 } 209 }
155 210
156 } // namespace ui 211 } // namespace ui
OLDNEW
« ui/compositor/compositor.cc ('K') | « ui/compositor/compositor.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698