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

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

Issue 2813303003: Add AndroidVideoSurfaceChooser to manage overlays. (Closed)
Patch Set: cl feedback. Created 3 years, 8 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 overlay_weak_(overlay_->GetWeakPtr()),
58 weak_this_factory_(this) {}
59
60 // Called by CreateOverlay.
61 MOCK_METHOD0(MockCreateOverlay, void());
62
63 std::unique_ptr<AndroidOverlay> CreateOverlay(
64 const AndroidOverlay::Config& config) override {
65 MockCreateOverlay();
66
67 // Notify the overlay about the config that was used to create it. We
68 // can't do this during overlay construction since we might want the
69 // overlay to set test expectations.
70 if (overlay_)
71 overlay_->SetConfig(config);
72
73 most_recent_config_ = config;
74 return std::move(overlay_);
75 }
76
77 // Return a weak ptr to the overlay that we will / did create.
78 base::WeakPtr<MockAndroidOverlay> overlay() { return overlay_weak_; }
79
80 // Return the most recent config that we were given during CreateOverlay.
81 AndroidOverlay::Config& most_recent_config() { return most_recent_config_; }
82
83 // Set the overlay that we'll provide next, or nullptr.
84 void SetOverlay(std::unique_ptr<MockAndroidOverlay> overlay) {
85 overlay_ = std::move(overlay);
86 if (overlay_)
87 overlay_weak_ = overlay_->GetWeakPtr();
88 else
89 overlay_weak_ = nullptr;
90 }
91
92 base::WeakPtr<MockAndroidOverlayFactory> GetWeakPtr() {
93 return weak_this_factory_.GetWeakPtr();
94 }
95
96 // Shorthand for overlay()->On*() callbacks.
97 void OnOverlayReady() { overlay()->OnOverlayReady(); }
98
99 void OnOverlayFailed() { overlay()->OnOverlayFailed(); }
100
101 void OnSurfaceDestroyed() { overlay()->OnSurfaceDestroyed(); }
102
103 private:
104 AndroidOverlay::Config most_recent_config_;
105 std::unique_ptr<MockAndroidOverlay> overlay_;
106 base::WeakPtr<MockAndroidOverlay> overlay_weak_;
107
108 base::WeakPtrFactory<MockAndroidOverlayFactory> weak_this_factory_;
109 };
110
111 ~AVDAOverlayHelperImplTest() override {}
112
113 void SetUp() override {
114 helper_ = base::MakeUnique<AVDAOverlayHelperImpl>();
115 factory_ = base::MakeUnique<StrictMock<MockAndroidOverlayFactory>>();
116 factory_weak_ = factory_->GetWeakPtr();
117 }
118
119 void StartHelper(std::unique_ptr<MockAndroidOverlayFactory> factory) {
120 helper_->Startup(
121 base::Bind(&MockClient::UseOverlayImpl, base::Unretained(&client_)),
122 base::Bind(&MockClient::UseSurfaceTexture, base::Unretained(&client_)),
123 base::Bind(&MockClient::StopUsingOverlayImmediately,
124 base::Unretained(&client_)),
125 std::move(factory));
126 }
127
128 std::unique_ptr<AVDAOverlayHelperImpl> helper_;
129 StrictMock<MockClient> client_;
130 std::unique_ptr<StrictMock<MockAndroidOverlayFactory>> factory_;
131 base::WeakPtr<MockAndroidOverlayFactory> factory_weak_;
132 };
133
134 TEST_F(AVDAOverlayHelperImplTest, StartupWithoutFactoryUsesSurfaceTexture) {
135 // Calling Startup() with no factory should result in a callback to use
136 // surface texture.
137 EXPECT_CALL(client_, UseSurfaceTexture());
138 StartHelper(nullptr);
139 }
140
141 TEST_F(AVDAOverlayHelperImplTest, ProvideInitialOverlaySuccessfully) {
142 // Providing a factory at startup should result in a switch to overlay. It
143 // should not switch to SurfaceTexture initially, since pre-M requires it.
144 // Note that post-M (especially DS), it might be fine to start with ST. We
145 // just don't differentiate those cases yet in the impl.
146
147 // Initially, there should be no callback into |client_|, since we haven't
148 // told |helper_| that the overlay is ready. It should, however, request the
149 // overlay from |factory_|.
150 EXPECT_CALL(*factory_, MockCreateOverlay());
151 StartHelper(std::move(factory_));
152
153 // |factory_| should not have been deleted
154 ASSERT_TRUE(factory_weak_ != nullptr);
155
156 testing::Mock::VerifyAndClearExpectations(&client_);
157 testing::Mock::VerifyAndClearExpectations(factory_weak_.get());
158
159 // Notify the factory that the overlay is ready. Expect that |client_| will
160 // be told to use it.
161 EXPECT_CALL(client_, UseOverlay(factory_weak_->overlay().get()));
162 factory_weak_->OnOverlayReady();
163 }
164
165 TEST_F(AVDAOverlayHelperImplTest, NullInitialOverlayUsesSurfaceTexture) {
166 // If we provide a factory, but it fails to create an overlay, then |client_|
167 // should be notified to use a surface texture.
168
169 EXPECT_CALL(*factory_, MockCreateOverlay());
170 EXPECT_CALL(client_, UseSurfaceTexture());
171 factory_->SetOverlay(nullptr);
172 StartHelper(std::move(factory_));
173 }
174
175 TEST_F(AVDAOverlayHelperImplTest, FailedInitialOverlayUsesSurfaceTexture) {
176 // If we provide a factory, but the overlay that it provides returns 'failed',
177 // then |client_| should use surface texture.
178 EXPECT_CALL(*factory_, MockCreateOverlay());
179 StartHelper(std::move(factory_));
180
181 testing::Mock::VerifyAndClearExpectations(&client_);
182 testing::Mock::VerifyAndClearExpectations(factory_weak_.get());
183
184 EXPECT_CALL(client_, UseSurfaceTexture());
185 factory_weak_->OnOverlayFailed();
186 }
187
188 TEST_F(AVDAOverlayHelperImplTest, OnSurfaceDestroyedSendsNotification) {
189 // If |helper_| is notified about OnSurfaceDestroyed, then |client_| should
190 // also be notified.
191
192 EXPECT_CALL(*factory_, MockCreateOverlay());
193 StartHelper(std::move(factory_));
194 EXPECT_CALL(client_, UseOverlay(factory_weak_->overlay().get()));
195 factory_weak_->OnOverlayReady();
196
197 testing::Mock::VerifyAndClearExpectations(&client_);
198 testing::Mock::VerifyAndClearExpectations(factory_weak_.get());
199
200 EXPECT_CALL(client_, UseSurfaceTexture());
201 base::WeakPtr<MockAndroidOverlay> overlay = factory_weak_->overlay();
202 helper_->OnOverlayFactory(nullptr);
203 testing::Mock::VerifyAndClearExpectations(&client_);
204
205 EXPECT_CALL(client_, StopUsingOverlayImmediately(overlay.get()));
206 overlay->OnSurfaceDestroyed();
207 }
208
209 TEST_F(AVDAOverlayHelperImplTest,
210 OnSurfaceDestroyedSendsNotificationAfterSwitch) {
211 // This tests two things. First:
212 // If |helper_| is notified about OnSurfaceDestroyed, then |client_| should
213 // also be notified even if |helper_| has already told |client_| to transition
214 // to SurfaceTexture. It has no idea if |client_| has actually transitioned,
215 // so it has to notify it to stop immediately, in case it hasn't.
216 // Second:
217 // |helper_| should notify |client_| to switch to surface texture if it
218 // provided an overlay, and the factory is changed. This indicates that
219 // whoever provided the factory is revoking it, so we shouldn't be using
220 // overlays from that factory anymore.
221 // We specifically test overlay => no factory, since |helper_| could elide
222 // multiple calls to switch to surface texture.
223 //
224 // We test these together, since switching the factory is the only way we have
225 // to make |helper_| transition to SurfaceTexture without sending destroyed.
226
227 EXPECT_CALL(*factory_, MockCreateOverlay());
228 StartHelper(std::move(factory_));
229 EXPECT_CALL(client_, UseOverlay(factory_weak_->overlay().get()));
230 factory_weak_->OnOverlayReady();
231
232 testing::Mock::VerifyAndClearExpectations(&client_);
233 testing::Mock::VerifyAndClearExpectations(factory_weak_.get());
234
235 // Switch factories, to notify the client back to switch to SurfaceTexture.
236 // We save the overlay ptr, since |factory_weak_| may be destroyed.
237 EXPECT_CALL(client_, UseSurfaceTexture());
238 base::WeakPtr<MockAndroidOverlay> overlay = factory_weak_->overlay();
239 helper_->OnOverlayFactory(nullptr);
240 testing::Mock::VerifyAndClearExpectations(&client_);
241
242 // Destroy the original surface.
243 EXPECT_CALL(client_, StopUsingOverlayImmediately(overlay.get()));
244 overlay->OnSurfaceDestroyed();
245 }
246
247 TEST_F(AVDAOverlayHelperImplTest, NullLaterOverlayUsesSurfaceTexture) {
248 // If an overlay factory is provided after startup that returns a null overlay
249 // from CreateOverlay, |helper_| should, at most, notify |client_| to use
250 // SurfaceTexture zero or more times.
251
252 // Start with SurfaceTexture.
253 EXPECT_CALL(client_, UseSurfaceTexture());
254 StartHelper(nullptr);
255 testing::Mock::VerifyAndClearExpectations(&client_);
256
257 // Provide a factory that will return a null overlay.
258 EXPECT_CALL(*factory_, MockCreateOverlay());
259 EXPECT_CALL(client_, UseSurfaceTexture()).Times(AnyNumber());
260 helper_->OnOverlayFactory(std::move(factory_));
261 factory_weak_->SetOverlay(nullptr);
262 }
263
264 TEST_F(AVDAOverlayHelperImplTest, FailedLaterOverlayDoesNothing) {
265 // If we send an overlay factory that returns an overlay, and that overlay
266 // fails, then the client should not be notified except for zero or more
267 // callbacks to switch to surface texture.
268
269 // Start with SurfaceTexture.
270 EXPECT_CALL(client_, UseSurfaceTexture());
271 StartHelper(nullptr);
272 testing::Mock::VerifyAndClearExpectations(&client_);
273
274 // Provide a factory.
275 EXPECT_CALL(*factory_, MockCreateOverlay());
276 EXPECT_CALL(client_, UseSurfaceTexture()).Times(AnyNumber());
277 helper_->OnOverlayFactory(std::move(factory_));
278 testing::Mock::VerifyAndClearExpectations(&client_);
279
280 // Fail the overlay.
281 factory_weak_->OnOverlayFailed();
282 }
283
284 TEST_F(AVDAOverlayHelperImplTest, SuccessfulLaterOverlayNotifiesClient) {
285 // |client_| is notified if we provide a factory that gets an overlay.
286
287 // Start with SurfaceTexture.
288 EXPECT_CALL(client_, UseSurfaceTexture());
289 StartHelper(nullptr);
290 testing::Mock::VerifyAndClearExpectations(&client_);
291
292 // Provide a factory. |helper_| should try to create an overlay. We don't
293 // care if a call to UseSurfaceTexture is elided or not. Note that AVDA will
294 // ignore duplicate calls anyway (MultipleSurfaceTextureCallbacksAreIgnored).
295 EXPECT_CALL(*factory_, MockCreateOverlay());
296 EXPECT_CALL(client_, UseSurfaceTexture()).Times(AnyNumber());
297 helper_->OnOverlayFactory(std::move(factory_));
298 testing::Mock::VerifyAndClearExpectations(&client_);
299 testing::Mock::VerifyAndClearExpectations(factory_weak_.get());
300
301 // Notify |helper_| that the overlay is ready.
302 EXPECT_CALL(client_, UseOverlay(factory_weak_->overlay().get()));
303 factory_weak_->OnOverlayReady();
304 }
305
306 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698