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

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

Issue 2813303003: Add AndroidVideoSurfaceChooser to manage overlays. (Closed)
Patch Set: from ps11 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.
watk 2017/04/27 20:33:10 I feel like you can delete the "One might argue" c
liberato (no reviews please) 2017/04/27 21:31:44 Done.
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 // We create a destruction observer. By default, the overlay must not be
102 // destroyed until the test completes. Of course, the test may ask the
103 // observer to expect something else.
104 destruction_observer_ = factory_->overlay()->CreateDestructionObserver();
105 destruction_observer_->DoNotAllowDestruction();
106 overlay_callbacks_ = factory_->overlay()->GetCallbacks();
107 }
108
109 void TearDown() override {
110 // If we get this far, the assume that whatever |destruction_observer_|
111 // was looking for should have already happened. We don't want the
112 // lifetime of the observer to matter with respect to the overlay when
113 // checking expectations.
114 // Note that it might already be null.
115 destruction_observer_ = nullptr;
116 }
117
118 void StartHelper(std::unique_ptr<MockAndroidOverlayFactory> factory) {
119 helper_->Initialize(
120 base::Bind(&MockClient::UseOverlayImpl, base::Unretained(&client_)),
121 base::Bind(&MockClient::UseSurfaceTexture, base::Unretained(&client_)),
122 base::Bind(&MockClient::StopUsingOverlayImmediately,
123 base::Unretained(&client_)),
124 std::move(factory));
125 }
126
127 std::unique_ptr<AVDAOverlayHelperImpl> helper_;
128 StrictMock<MockClient> client_;
129 std::unique_ptr<StrictMock<MockAndroidOverlayFactory>> factory_;
130 base::WeakPtr<MockAndroidOverlayFactory> factory_weak_;
131
132 // Callbacks to control the overlay that will be vended by |factory_|
133 MockAndroidOverlay::Callbacks overlay_callbacks_;
134
135 std::unique_ptr<MockAndroidOverlay::DestructionObserver>
136 destruction_observer_;
137 };
138
139 TEST_F(AVDAOverlayHelperImplTest, InitializeWithoutFactoryUsesSurfaceTexture) {
140 // Calling Initialize() with no factory should result in a callback to use
141 // surface texture.
142 EXPECT_CALL(client_, UseSurfaceTexture());
143 StartHelper(nullptr);
144 }
145
146 TEST_F(AVDAOverlayHelperImplTest, ProvideInitialOverlaySuccessfully) {
147 // Providing a factory at startup should result in a switch to overlay. It
148 // should not switch to SurfaceTexture initially, since pre-M requires it.
149 // Note that post-M (especially DS), it might be fine to start with ST. We
150 // just don't differentiate those cases yet in the impl.
151
152 // Initially, there should be no callback into |client_|, since we haven't
153 // told |helper_| that the overlay is ready. It should, however, request the
154 // overlay from |factory_|.
155 EXPECT_CALL(*factory_, MockCreateOverlay());
156 StartHelper(std::move(factory_));
watk 2017/04/27 20:33:10 Seems like these two lines are a separate test. An
liberato (no reviews please) 2017/04/27 21:31:44 Done.
157
158 // |factory_| should not have been deleted
159 ASSERT_TRUE(factory_weak_ != nullptr);
watk 2017/04/27 20:33:10 Not clear why this matters in this test
liberato (no reviews please) 2017/04/27 21:31:44 Done.
160
161 testing::Mock::VerifyAndClearExpectations(&client_);
162 testing::Mock::VerifyAndClearExpectations(factory_weak_.get());
163
164 // Notify the helper that the overlay is ready. Expect that |client_| will
165 // be told to use it.
166 EXPECT_CALL(client_, UseOverlay(NotNull()));
167 overlay_callbacks_.OverlayReady.Run();
168 }
169
170 TEST_F(AVDAOverlayHelperImplTest, NullInitialOverlayUsesSurfaceTexture) {
171 // If we provide a factory, but it fails to create an overlay, then |client_|
172 // should be notified to use a surface texture.
173
174 EXPECT_CALL(*factory_, MockCreateOverlay());
175 EXPECT_CALL(client_, UseSurfaceTexture());
176 // Replacing the overlay will null will destroy it.
watk 2017/04/27 20:33:10 s/will/with
liberato (no reviews please) 2017/04/27 21:31:44 Done.
177 destruction_observer_->ExpectDestruction();
178 factory_->SetOverlay(nullptr);
179 StartHelper(std::move(factory_));
180 }
181
182 TEST_F(AVDAOverlayHelperImplTest, FailedInitialOverlayUsesSurfaceTexture) {
183 // If we provide a factory, but the overlay that it provides returns 'failed',
184 // then |client_| should use surface texture.
185 EXPECT_CALL(*factory_, MockCreateOverlay());
186 StartHelper(std::move(factory_));
187
188 testing::Mock::VerifyAndClearExpectations(&client_);
189 testing::Mock::VerifyAndClearExpectations(factory_weak_.get());
190
191 // The overlay may be destroyed at any time after we send OverlayFailed. It
192 // doesn't have to be destroyed. We just care that it hasn't been destroyed
193 // before now.
194 destruction_observer_ = nullptr;
195 EXPECT_CALL(client_, UseSurfaceTexture());
196 overlay_callbacks_.OverlayFailed.Run();
197 }
198
199 TEST_F(AVDAOverlayHelperImplTest, OnSurfaceDestroyedSendsNotification) {
200 // If |helper_| is notified about OnSurfaceDestroyed, then |client_| should
201 // also be notified.
202
203 EXPECT_CALL(*factory_, MockCreateOverlay());
204 StartHelper(std::move(factory_));
205 EXPECT_CALL(client_, UseOverlay(NotNull()));
206 overlay_callbacks_.OverlayReady.Run();
207
208 testing::Mock::VerifyAndClearExpectations(&client_);
209 testing::Mock::VerifyAndClearExpectations(factory_weak_.get());
210
211 // Switch to a surface texture. OnSurfaceDestroyed should still be sent.
212 EXPECT_CALL(client_, UseSurfaceTexture());
213 helper_->OnOverlayFactory(nullptr);
214 testing::Mock::VerifyAndClearExpectations(&client_);
215
216 EXPECT_CALL(client_, StopUsingOverlayImmediately(NotNull()));
217 overlay_callbacks_.SurfaceDestroyed.Run();
218 }
219
220 TEST_F(AVDAOverlayHelperImplTest,
221 OnSurfaceDestroyedSendsNotificationAfterSwitch) {
222 // This tests two things. First:
223 // If |helper_| is notified about OnSurfaceDestroyed, then |client_| should
224 // also be notified even if |helper_| has already told |client_| to transition
225 // to SurfaceTexture. It has no idea if |client_| has actually transitioned,
226 // so it has to notify it to stop immediately, in case it hasn't.
227 // Second:
228 // |helper_| should notify |client_| to switch to surface texture if it
229 // provided an overlay, and the factory is changed. This indicates that
230 // whoever provided the factory is revoking it, so we shouldn't be using
231 // overlays from that factory anymore.
232 // We specifically test overlay => no factory, since |helper_| could elide
233 // multiple calls to switch to surface texture.
234 //
235 // We test these together, since switching the factory is the only way we have
236 // to make |helper_| transition to SurfaceTexture without sending destroyed.
237
238 EXPECT_CALL(*factory_, MockCreateOverlay());
239 StartHelper(std::move(factory_));
240 EXPECT_CALL(client_, UseOverlay(NotNull()));
241 overlay_callbacks_.OverlayReady.Run();
242
243 testing::Mock::VerifyAndClearExpectations(&client_);
244 testing::Mock::VerifyAndClearExpectations(factory_weak_.get());
245
246 // Switch factories, to notify the client back to switch to SurfaceTexture.
247 EXPECT_CALL(client_, UseSurfaceTexture());
248 helper_->OnOverlayFactory(nullptr);
249 testing::Mock::VerifyAndClearExpectations(&client_);
250
251 // Destroy the original surface.
252 EXPECT_CALL(client_, StopUsingOverlayImmediately(NotNull()));
253 overlay_callbacks_.SurfaceDestroyed.Run();
254 }
255
256 TEST_F(AVDAOverlayHelperImplTest, NullLaterOverlayUsesSurfaceTexture) {
257 // If an overlay factory is provided after startup that returns a null overlay
258 // from CreateOverlay, |helper_| should, at most, notify |client_| to use
259 // SurfaceTexture zero or more times.
260
261 // Start with SurfaceTexture.
262 EXPECT_CALL(client_, UseSurfaceTexture());
263 StartHelper(nullptr);
264 testing::Mock::VerifyAndClearExpectations(&client_);
265
266 // Provide a factory that will return a null overlay.
267 EXPECT_CALL(*factory_, MockCreateOverlay());
268 EXPECT_CALL(client_, UseSurfaceTexture()).Times(AnyNumber());
269 helper_->OnOverlayFactory(std::move(factory_));
270 factory_weak_->SetOverlay(nullptr);
271 }
272
273 TEST_F(AVDAOverlayHelperImplTest, FailedLaterOverlayDoesNothing) {
274 // If we send an overlay factory that returns an overlay, and that overlay
275 // fails, then the client should not be notified except for zero or more
276 // callbacks to switch to surface texture.
277
278 // Start with SurfaceTexture.
279 EXPECT_CALL(client_, UseSurfaceTexture());
280 StartHelper(nullptr);
281 testing::Mock::VerifyAndClearExpectations(&client_);
282
283 // Provide a factory.
284 EXPECT_CALL(*factory_, MockCreateOverlay());
285 EXPECT_CALL(client_, UseSurfaceTexture()).Times(AnyNumber());
286 helper_->OnOverlayFactory(std::move(factory_));
287 testing::Mock::VerifyAndClearExpectations(&client_);
288
289 // Fail the overlay. We don't care if it's destroyed after that, as long as
290 // it hasn't been destroyed yet.
291 destruction_observer_ = nullptr;
292 overlay_callbacks_.OverlayFailed.Run();
293 }
294
295 TEST_F(AVDAOverlayHelperImplTest, SuccessfulLaterOverlayNotifiesClient) {
296 // |client_| is notified if we provide a factory that gets an overlay.
297
298 // Start with SurfaceTexture.
299 EXPECT_CALL(client_, UseSurfaceTexture());
300 StartHelper(nullptr);
301 testing::Mock::VerifyAndClearExpectations(&client_);
302
303 // Provide a factory. |helper_| should try to create an overlay. We don't
304 // care if a call to UseSurfaceTexture is elided or not. Note that AVDA will
305 // ignore duplicate calls anyway (MultipleSurfaceTextureCallbacksAreIgnored).
306 EXPECT_CALL(*factory_, MockCreateOverlay());
307 EXPECT_CALL(client_, UseSurfaceTexture()).Times(AnyNumber());
308 helper_->OnOverlayFactory(std::move(factory_));
309 testing::Mock::VerifyAndClearExpectations(&client_);
310 testing::Mock::VerifyAndClearExpectations(factory_weak_.get());
311
312 // Notify |helper_| that the overlay is ready.
313 EXPECT_CALL(client_, UseOverlay(NotNull()));
314 overlay_callbacks_.OverlayReady.Run();
315 }
316
317 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698