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

Side by Side Diff: media/gpu/avda_overlay_helper_impl_unittest.cc

Issue 2813303003: Add AndroidVideoSurfaceChooser to manage overlays. (Closed)
Patch Set: removed most weak factories Created 3 years, 7 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
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "media/gpu/avda_overlay_helper_impl.h"
6
7 #include <stdint.h>
8
9 #include <memory>
10
11 #include "base/bind.h"
12 #include "base/logging.h"
13 #include "base/memory/ptr_util.h"
14 #include "media/base/android/android_overlay_factory.h"
15 #include "media/base/android/mock_android_overlay.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 using ::testing::_;
20 using ::testing::AnyNumber;
21 using ::testing::NiceMock;
22 using ::testing::NotNull;
23 using ::testing::Return;
24 using ::testing::StrictMock;
25
26 namespace media {
27
28 // Unit tests for AVDAOverlayHelperImpl
29 class AVDAOverlayHelperImplTest : public testing::Test {
30 public:
31 class MockClient {
32 public:
33 MOCK_METHOD1(UseOverlay, void(AndroidOverlay*));
34
35 void UseOverlayImpl(std::unique_ptr<AndroidOverlay> overlay) {
36 UseOverlay(overlay.get());
37
38 // Also take ownership of the overlay, so that it's not destroyed.
39 overlay_ = std::move(overlay);
40 }
41
42 // Note that this won't clear |overlay_|, which is helpful. One might argue
43 // that we should drop it, and provide a separate TakeOverlay() method to
44 // hand off ownership to the caller.
45 MOCK_METHOD0(UseSurfaceTexture, void(void));
46 MOCK_METHOD1(StopUsingOverlayImmediately, void(AndroidOverlay*));
47
48 private:
49 std::unique_ptr<AndroidOverlay> overlay_;
50 };
51
52 // Mock factory which will return one overlay.
53 class MockAndroidOverlayFactory : public AndroidOverlayFactory {
54 public:
55 MockAndroidOverlayFactory()
56 : overlay_(base::MakeUnique<MockAndroidOverlay>()),
57 weak_this_factory_(this) {}
58
59 // Called by CreateOverlay.
60 MOCK_METHOD0(MockCreateOverlay, void());
61
62 std::unique_ptr<AndroidOverlay> CreateOverlay(
63 const AndroidOverlay::Config& config) override {
64 MockCreateOverlay();
65
66 // Notify the overlay about the config that was used to create it. We
67 // can't do this during overlay construction since we might want the
68 // overlay to set test expectations.
69 if (overlay_)
70 overlay_->SetConfig(config);
71
72 return std::move(overlay_);
73 }
74
75 // Return the overlay, if we still own it. One the client creates an
76 // overlay, we'll re-assign ownership.
77 MockAndroidOverlay* overlay() { return overlay_.get(); }
78
79 // Set the overlay that we'll provide next, or nullptr.
80 void SetOverlay(std::unique_ptr<MockAndroidOverlay> overlay) {
81 overlay_ = std::move(overlay);
82 }
83
84 base::WeakPtr<MockAndroidOverlayFactory> GetWeakPtr() {
85 return weak_this_factory_.GetWeakPtr();
86 }
87
88 private:
89 std::unique_ptr<MockAndroidOverlay> overlay_;
90
91 base::WeakPtrFactory<MockAndroidOverlayFactory> weak_this_factory_;
92 };
93
94 ~AVDAOverlayHelperImplTest() override {}
95
96 void SetUp() override {
97 helper_ = base::MakeUnique<AVDAOverlayHelperImpl>();
98 factory_ = base::MakeUnique<StrictMock<MockAndroidOverlayFactory>>();
99 factory_weak_ = factory_->GetWeakPtr();
100
101 overlay_callbacks_ = factory_->overlay()->GetCallbacks();
102 }
103
104 void StartHelper(std::unique_ptr<MockAndroidOverlayFactory> factory) {
105 // Before we hand off the factory (and the overlay with it), get the
106 // callbacks to control the overlay.
107 if (factory && factory->overlay()) {
108 // By default, destruction of the overlay is not okay.
109 destruction_observer_ = factory->overlay()->CreateDestructionObserver();
110 }
111
112 helper_->Startup(
113 base::Bind(&MockClient::UseOverlayImpl, base::Unretained(&client_)),
114 base::Bind(&MockClient::UseSurfaceTexture, base::Unretained(&client_)),
115 base::Bind(&MockClient::StopUsingOverlayImmediately,
116 base::Unretained(&client_)),
117 std::move(factory));
118 }
119
120 // Verify that the overlay hasn't been destroyed yet, and set an expectation
121 // that it will be.
122 void ExpectOverlayDestruction() {
123 VerifyDestructionObserver();
124 EXPECT_CALL(*destruction_observer_, OnOverlayDestroyed());
125 }
126
127 // If called without setting expectations, then this verifies that the overlay
128 // has not been destroyed.
129 void VerifyDestructionObserver() {
130 testing::Mock::VerifyAndClearExpectations(destruction_observer_.get());
131 }
132
133 std::unique_ptr<AVDAOverlayHelperImpl> helper_;
134 StrictMock<MockClient> client_;
135 std::unique_ptr<StrictMock<MockAndroidOverlayFactory>> factory_;
136 base::WeakPtr<MockAndroidOverlayFactory> factory_weak_;
137
138 // Callbacks to control the overlay that will be vended by |factory_|
139 MockAndroidOverlay::Callbacks overlay_callbacks_;
140
141 std::unique_ptr<MockAndroidOverlay::DestructionObserver>
142 destruction_observer_;
143 };
144
145 TEST_F(AVDAOverlayHelperImplTest, StartupWithoutFactoryUsesSurfaceTexture) {
146 // Calling Startup() with no factory should result in a callback to use
147 // surface texture.
148 EXPECT_CALL(client_, UseSurfaceTexture());
149 StartHelper(nullptr);
150 }
151
152 TEST_F(AVDAOverlayHelperImplTest, ProvideInitialOverlaySuccessfully) {
153 // Providing a factory at startup should result in a switch to overlay. It
154 // should not switch to SurfaceTexture initially, since pre-M requires it.
155 // Note that post-M (especially DS), it might be fine to start with ST. We
156 // just don't differentiate those cases yet in the impl.
157
158 // Initially, there should be no callback into |client_|, since we haven't
159 // told |helper_| that the overlay is ready. It should, however, request the
160 // overlay from |factory_|.
161 EXPECT_CALL(*factory_, MockCreateOverlay());
162 StartHelper(std::move(factory_));
163
164 // |factory_| should not have been deleted
165 ASSERT_TRUE(factory_weak_ != nullptr);
166
167 testing::Mock::VerifyAndClearExpectations(&client_);
168 testing::Mock::VerifyAndClearExpectations(factory_weak_.get());
169
170 // Notify the helper that the overlay is ready. Expect that |client_| will
171 // be told to use it.
172 EXPECT_CALL(client_, UseOverlay(NotNull()));
173 overlay_callbacks_.OverlayReady.Run();
174
175 // The overlay should be destroyed during cleanup, but not before now.
176 VerifyDestructionObserver();
177 }
178
179 TEST_F(AVDAOverlayHelperImplTest, NullInitialOverlayUsesSurfaceTexture) {
180 // If we provide a factory, but it fails to create an overlay, then |client_|
181 // should be notified to use a surface texture.
182
183 EXPECT_CALL(*factory_, MockCreateOverlay());
184 EXPECT_CALL(client_, UseSurfaceTexture());
185 factory_->SetOverlay(nullptr);
186 StartHelper(std::move(factory_));
187 }
188
189 TEST_F(AVDAOverlayHelperImplTest, FailedInitialOverlayUsesSurfaceTexture) {
190 // If we provide a factory, but the overlay that it provides returns 'failed',
191 // then |client_| should use surface texture.
192 EXPECT_CALL(*factory_, MockCreateOverlay());
193 StartHelper(std::move(factory_));
194
195 testing::Mock::VerifyAndClearExpectations(&client_);
196 testing::Mock::VerifyAndClearExpectations(factory_weak_.get());
197
198 // The overlay may be destroyed at any time after we send OverlayFailed.
199 ExpectOverlayDestruction();
200 EXPECT_CALL(client_, UseSurfaceTexture());
201 overlay_callbacks_.OverlayFailed.Run();
202 }
203
204 TEST_F(AVDAOverlayHelperImplTest, OnSurfaceDestroyedSendsNotification) {
205 // If |helper_| is notified about OnSurfaceDestroyed, then |client_| should
206 // also be notified.
207
208 EXPECT_CALL(*factory_, MockCreateOverlay());
209 StartHelper(std::move(factory_));
210 EXPECT_CALL(client_, UseOverlay(NotNull()));
211 overlay_callbacks_.OverlayReady.Run();
212
213 testing::Mock::VerifyAndClearExpectations(&client_);
214 testing::Mock::VerifyAndClearExpectations(factory_weak_.get());
215
216 // Switch to a surface texture. OnSurfaceDestroyed should still be sent.
217 EXPECT_CALL(client_, UseSurfaceTexture());
218 helper_->OnOverlayFactory(nullptr);
219 testing::Mock::VerifyAndClearExpectations(&client_);
220
221 EXPECT_CALL(client_, StopUsingOverlayImmediately(NotNull()));
222 overlay_callbacks_.SurfaceDestroyed.Run();
223 }
224
225 TEST_F(AVDAOverlayHelperImplTest,
226 OnSurfaceDestroyedSendsNotificationAfterSwitch) {
227 // This tests two things. First:
228 // If |helper_| is notified about OnSurfaceDestroyed, then |client_| should
229 // also be notified even if |helper_| has already told |client_| to transition
230 // to SurfaceTexture. It has no idea if |client_| has actually transitioned,
231 // so it has to notify it to stop immediately, in case it hasn't.
232 // Second:
233 // |helper_| should notify |client_| to switch to surface texture if it
234 // provided an overlay, and the factory is changed. This indicates that
235 // whoever provided the factory is revoking it, so we shouldn't be using
236 // overlays from that factory anymore.
237 // We specifically test overlay => no factory, since |helper_| could elide
238 // multiple calls to switch to surface texture.
239 //
240 // We test these together, since switching the factory is the only way we have
241 // to make |helper_| transition to SurfaceTexture without sending destroyed.
242
243 EXPECT_CALL(*factory_, MockCreateOverlay());
244 StartHelper(std::move(factory_));
245 EXPECT_CALL(client_, UseOverlay(NotNull()));
246 overlay_callbacks_.OverlayReady.Run();
247
248 testing::Mock::VerifyAndClearExpectations(&client_);
249 testing::Mock::VerifyAndClearExpectations(factory_weak_.get());
250
251 // Switch factories, to notify the client back to switch to SurfaceTexture.
252 EXPECT_CALL(client_, UseSurfaceTexture());
253 helper_->OnOverlayFactory(nullptr);
254 testing::Mock::VerifyAndClearExpectations(&client_);
255
256 // Destroy the original surface.
257 EXPECT_CALL(client_, StopUsingOverlayImmediately(NotNull()));
258 overlay_callbacks_.SurfaceDestroyed.Run();
259 }
260
261 TEST_F(AVDAOverlayHelperImplTest, NullLaterOverlayUsesSurfaceTexture) {
262 // If an overlay factory is provided after startup that returns a null overlay
263 // from CreateOverlay, |helper_| should, at most, notify |client_| to use
264 // SurfaceTexture zero or more times.
265
266 // Start with SurfaceTexture.
267 EXPECT_CALL(client_, UseSurfaceTexture());
268 StartHelper(nullptr);
269 testing::Mock::VerifyAndClearExpectations(&client_);
270
271 // Provide a factory that will return a null overlay.
272 EXPECT_CALL(*factory_, MockCreateOverlay());
273 EXPECT_CALL(client_, UseSurfaceTexture()).Times(AnyNumber());
274 helper_->OnOverlayFactory(std::move(factory_));
275 factory_weak_->SetOverlay(nullptr);
276 }
277
278 TEST_F(AVDAOverlayHelperImplTest, FailedLaterOverlayDoesNothing) {
279 // If we send an overlay factory that returns an overlay, and that overlay
280 // fails, then the client should not be notified except for zero or more
281 // callbacks to switch to surface texture.
282
283 // Start with SurfaceTexture.
284 EXPECT_CALL(client_, UseSurfaceTexture());
285 StartHelper(nullptr);
286 testing::Mock::VerifyAndClearExpectations(&client_);
287
288 // Provide a factory.
289 EXPECT_CALL(*factory_, MockCreateOverlay());
290 EXPECT_CALL(client_, UseSurfaceTexture()).Times(AnyNumber());
291 helper_->OnOverlayFactory(std::move(factory_));
292 testing::Mock::VerifyAndClearExpectations(&client_);
293
294 // Fail the overlay.
295 overlay_callbacks_.OverlayFailed.Run();
296 }
297
298 TEST_F(AVDAOverlayHelperImplTest, SuccessfulLaterOverlayNotifiesClient) {
299 // |client_| is notified if we provide a factory that gets an overlay.
300
301 // Start with SurfaceTexture.
302 EXPECT_CALL(client_, UseSurfaceTexture());
303 StartHelper(nullptr);
304 testing::Mock::VerifyAndClearExpectations(&client_);
305
306 // Provide a factory. |helper_| should try to create an overlay. We don't
307 // care if a call to UseSurfaceTexture is elided or not. Note that AVDA will
308 // ignore duplicate calls anyway (MultipleSurfaceTextureCallbacksAreIgnored).
309 EXPECT_CALL(*factory_, MockCreateOverlay());
310 EXPECT_CALL(client_, UseSurfaceTexture()).Times(AnyNumber());
311 helper_->OnOverlayFactory(std::move(factory_));
312 testing::Mock::VerifyAndClearExpectations(&client_);
313 testing::Mock::VerifyAndClearExpectations(factory_weak_.get());
314
315 // Notify |helper_| that the overlay is ready.
316 EXPECT_CALL(client_, UseOverlay(NotNull()));
317 overlay_callbacks_.OverlayReady.Run();
318 }
319
320 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698