Chromium Code Reviews| OLD | NEW |
|---|---|
| (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/android_video_surface_chooser_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 AndroidVideoSurfaceChooserImpl | |
| 29 class AndroidVideoSurfaceChooserImplTest : public testing::Test { | |
| 30 public: | |
| 31 class MockClient { | |
|
watk
2017/05/02 21:00:01
Same comment as above about these inner classes.
liberato (no reviews please)
2017/05/02 21:50:05
Done.
| |
| 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. | |
| 43 MOCK_METHOD0(UseSurfaceTexture, void(void)); | |
| 44 MOCK_METHOD1(StopUsingOverlayImmediately, void(AndroidOverlay*)); | |
| 45 | |
| 46 private: | |
| 47 std::unique_ptr<AndroidOverlay> overlay_; | |
| 48 }; | |
| 49 | |
| 50 // Mock factory which will return one overlay. | |
| 51 class MockAndroidOverlayFactory : public AndroidOverlayFactory { | |
| 52 public: | |
| 53 MockAndroidOverlayFactory() | |
| 54 : overlay_(base::MakeUnique<MockAndroidOverlay>()), | |
| 55 weak_this_factory_(this) {} | |
| 56 | |
| 57 // Called by CreateOverlay. | |
| 58 MOCK_METHOD0(MockCreateOverlay, void()); | |
| 59 | |
| 60 std::unique_ptr<AndroidOverlay> CreateOverlay( | |
| 61 const AndroidOverlay::Config& config) override { | |
| 62 MockCreateOverlay(); | |
| 63 | |
| 64 // Notify the overlay about the config that was used to create it. We | |
| 65 // can't do this during overlay construction since we might want the | |
| 66 // overlay to set test expectations. | |
| 67 if (overlay_) | |
| 68 overlay_->SetConfig(config); | |
| 69 | |
| 70 return std::move(overlay_); | |
| 71 } | |
| 72 | |
| 73 // Return the overlay, if we still own it. One the client creates an | |
| 74 // overlay, we'll re-assign ownership. | |
| 75 MockAndroidOverlay* overlay() { return overlay_.get(); } | |
| 76 | |
| 77 // Set the overlay that we'll provide next, or nullptr. | |
| 78 void SetOverlay(std::unique_ptr<MockAndroidOverlay> overlay) { | |
| 79 overlay_ = std::move(overlay); | |
| 80 } | |
| 81 | |
| 82 base::WeakPtr<MockAndroidOverlayFactory> GetWeakPtr() { | |
| 83 return weak_this_factory_.GetWeakPtr(); | |
| 84 } | |
| 85 | |
| 86 private: | |
| 87 std::unique_ptr<MockAndroidOverlay> overlay_; | |
| 88 | |
| 89 base::WeakPtrFactory<MockAndroidOverlayFactory> weak_this_factory_; | |
| 90 }; | |
| 91 | |
| 92 ~AndroidVideoSurfaceChooserImplTest() override {} | |
| 93 | |
| 94 void SetUp() override { | |
| 95 helper_ = base::MakeUnique<AndroidVideoSurfaceChooserImpl>(); | |
| 96 factory_ = base::MakeUnique<NiceMock<MockAndroidOverlayFactory>>(); | |
| 97 factory_weak_ = factory_->GetWeakPtr(); | |
| 98 | |
| 99 // We create a destruction observer. By default, the overlay must not be | |
| 100 // destroyed until the test completes. Of course, the test may ask the | |
| 101 // observer to expect something else. | |
| 102 destruction_observer_ = factory_->overlay()->CreateDestructionObserver(); | |
| 103 destruction_observer_->DoNotAllowDestruction(); | |
| 104 overlay_callbacks_ = factory_->overlay()->GetCallbacks(); | |
| 105 } | |
| 106 | |
| 107 void TearDown() override { | |
| 108 // If we get this far, the assume that whatever |destruction_observer_| | |
| 109 // was looking for should have already happened. We don't want the | |
| 110 // lifetime of the observer to matter with respect to the overlay when | |
| 111 // checking expectations. | |
| 112 // Note that it might already be null. | |
| 113 destruction_observer_ = nullptr; | |
| 114 } | |
| 115 | |
| 116 void StartHelper(std::unique_ptr<MockAndroidOverlayFactory> factory) { | |
| 117 helper_->Initialize( | |
| 118 base::Bind(&MockClient::UseOverlayImpl, base::Unretained(&client_)), | |
| 119 base::Bind(&MockClient::UseSurfaceTexture, base::Unretained(&client_)), | |
| 120 base::Bind(&MockClient::StopUsingOverlayImmediately, | |
| 121 base::Unretained(&client_)), | |
| 122 std::move(factory)); | |
| 123 } | |
| 124 | |
| 125 std::unique_ptr<AndroidVideoSurfaceChooserImpl> helper_; | |
| 126 StrictMock<MockClient> client_; | |
| 127 std::unique_ptr<NiceMock<MockAndroidOverlayFactory>> factory_; | |
| 128 base::WeakPtr<MockAndroidOverlayFactory> factory_weak_; | |
| 129 | |
| 130 // Callbacks to control the overlay that will be vended by |factory_| | |
| 131 MockAndroidOverlay::Callbacks overlay_callbacks_; | |
| 132 | |
| 133 std::unique_ptr<MockAndroidOverlay::DestructionObserver> | |
| 134 destruction_observer_; | |
| 135 }; | |
| 136 | |
| 137 TEST_F(AndroidVideoSurfaceChooserImplTest, | |
| 138 InitializeWithoutFactoryUsesSurfaceTexture) { | |
| 139 // Calling Initialize() with no factory should result in a callback to use | |
| 140 // surface texture. | |
| 141 EXPECT_CALL(client_, UseSurfaceTexture()); | |
| 142 StartHelper(nullptr); | |
| 143 } | |
| 144 | |
| 145 TEST_F(AndroidVideoSurfaceChooserImplTest, ProvideInitialOverlaySuccessfully) { | |
| 146 // Providing a factory at startup should result in a switch to overlay. It | |
| 147 // should not switch to SurfaceTexture initially, since pre-M requires it. | |
| 148 // Note that post-M (especially DS), it might be fine to start with ST. We | |
| 149 // just don't differentiate those cases yet in the impl. | |
| 150 | |
| 151 EXPECT_CALL(client_, UseSurfaceTexture()).Times(0); | |
| 152 StartHelper(std::move(factory_)); | |
| 153 | |
| 154 // Notify the helper that the overlay is ready. Expect that |client_| will | |
| 155 // be told to use it. | |
| 156 EXPECT_CALL(client_, UseOverlay(NotNull())); | |
| 157 overlay_callbacks_.OverlayReady.Run(); | |
| 158 } | |
| 159 | |
| 160 TEST_F(AndroidVideoSurfaceChooserImplTest, | |
| 161 InitializingWithFactoryCreatesOverlay) { | |
| 162 // Providing a factory at startup should result in a switch to overlay. It | |
| 163 // should not switch to SurfaceTexture initially, since pre-M requires it. | |
| 164 // Note that post-M (especially DS), it might be fine to start with ST. We | |
| 165 // just don't differentiate those cases yet in the impl. | |
| 166 | |
| 167 // Initially, there should be no callback into |client_|, since we haven't | |
| 168 // told |helper_| that the overlay is ready. It should, however, request the | |
| 169 // overlay from |factory_|. | |
| 170 EXPECT_CALL(*factory_, MockCreateOverlay()); | |
| 171 StartHelper(std::move(factory_)); | |
| 172 } | |
| 173 | |
| 174 TEST_F(AndroidVideoSurfaceChooserImplTest, | |
| 175 NullInitialOverlayUsesSurfaceTexture) { | |
| 176 // If we provide a factory, but it fails to create an overlay, then |client_| | |
| 177 // should be notified to use a surface texture. | |
| 178 | |
| 179 EXPECT_CALL(*factory_, MockCreateOverlay()); | |
| 180 EXPECT_CALL(client_, UseSurfaceTexture()); | |
| 181 // Replacing the overlay with null will destroy it. | |
| 182 destruction_observer_->ExpectDestruction(); | |
| 183 factory_->SetOverlay(nullptr); | |
| 184 StartHelper(std::move(factory_)); | |
| 185 } | |
| 186 | |
| 187 TEST_F(AndroidVideoSurfaceChooserImplTest, | |
| 188 FailedInitialOverlayUsesSurfaceTexture) { | |
| 189 // If we provide a factory, but the overlay that it provides returns 'failed', | |
| 190 // then |client_| should use surface texture. | |
| 191 EXPECT_CALL(*factory_, MockCreateOverlay()); | |
| 192 StartHelper(std::move(factory_)); | |
| 193 | |
| 194 testing::Mock::VerifyAndClearExpectations(&client_); | |
| 195 testing::Mock::VerifyAndClearExpectations(factory_weak_.get()); | |
| 196 | |
| 197 // The overlay may be destroyed at any time after we send OverlayFailed. It | |
| 198 // doesn't have to be destroyed. We just care that it hasn't been destroyed | |
| 199 // before now. | |
| 200 destruction_observer_ = nullptr; | |
| 201 EXPECT_CALL(client_, UseSurfaceTexture()); | |
| 202 overlay_callbacks_.OverlayFailed.Run(); | |
| 203 } | |
| 204 | |
| 205 TEST_F(AndroidVideoSurfaceChooserImplTest, | |
| 206 OnSurfaceDestroyedSendsNotification) { | |
| 207 // If |helper_| is notified about OnSurfaceDestroyed, then |client_| should | |
| 208 // also be notified. | |
| 209 | |
| 210 EXPECT_CALL(*factory_, MockCreateOverlay()); | |
| 211 StartHelper(std::move(factory_)); | |
| 212 EXPECT_CALL(client_, UseOverlay(NotNull())); | |
| 213 overlay_callbacks_.OverlayReady.Run(); | |
| 214 | |
| 215 testing::Mock::VerifyAndClearExpectations(&client_); | |
| 216 testing::Mock::VerifyAndClearExpectations(factory_weak_.get()); | |
| 217 | |
| 218 // Switch to a surface texture. OnSurfaceDestroyed should still be sent. | |
| 219 EXPECT_CALL(client_, UseSurfaceTexture()); | |
| 220 helper_->ReplaceOverlayFactory(nullptr); | |
| 221 testing::Mock::VerifyAndClearExpectations(&client_); | |
| 222 | |
| 223 EXPECT_CALL(client_, StopUsingOverlayImmediately(NotNull())); | |
| 224 overlay_callbacks_.SurfaceDestroyed.Run(); | |
| 225 } | |
| 226 | |
| 227 TEST_F(AndroidVideoSurfaceChooserImplTest, | |
| 228 OnSurfaceDestroyedSendsNotificationAfterSwitch) { | |
| 229 // This tests two things. First: | |
| 230 // If |helper_| is notified about OnSurfaceDestroyed, then |client_| should | |
| 231 // also be notified even if |helper_| has already told |client_| to transition | |
| 232 // to SurfaceTexture. It has no idea if |client_| has actually transitioned, | |
| 233 // so it has to notify it to stop immediately, in case it hasn't. | |
| 234 // Second: | |
| 235 // |helper_| should notify |client_| to switch to surface texture if it | |
| 236 // provided an overlay, and the factory is changed. This indicates that | |
| 237 // whoever provided the factory is revoking it, so we shouldn't be using | |
| 238 // overlays from that factory anymore. | |
| 239 // We specifically test overlay => no factory, since |helper_| could elide | |
| 240 // multiple calls to switch to surface texture. | |
| 241 // | |
| 242 // We test these together, since switching the factory is the only way we have | |
| 243 // to make |helper_| transition to SurfaceTexture without sending destroyed. | |
| 244 | |
| 245 EXPECT_CALL(*factory_, MockCreateOverlay()); | |
| 246 StartHelper(std::move(factory_)); | |
| 247 EXPECT_CALL(client_, UseOverlay(NotNull())); | |
| 248 overlay_callbacks_.OverlayReady.Run(); | |
| 249 | |
| 250 testing::Mock::VerifyAndClearExpectations(&client_); | |
| 251 testing::Mock::VerifyAndClearExpectations(factory_weak_.get()); | |
| 252 | |
| 253 // Switch factories, to notify the client back to switch to SurfaceTexture. | |
| 254 EXPECT_CALL(client_, UseSurfaceTexture()); | |
| 255 helper_->ReplaceOverlayFactory(nullptr); | |
| 256 testing::Mock::VerifyAndClearExpectations(&client_); | |
| 257 | |
| 258 // Destroy the original surface. | |
| 259 EXPECT_CALL(client_, StopUsingOverlayImmediately(NotNull())); | |
| 260 overlay_callbacks_.SurfaceDestroyed.Run(); | |
| 261 } | |
| 262 | |
| 263 TEST_F(AndroidVideoSurfaceChooserImplTest, NullLaterOverlayUsesSurfaceTexture) { | |
| 264 // If an overlay factory is provided after startup that returns a null overlay | |
| 265 // from CreateOverlay, |helper_| should, at most, notify |client_| to use | |
| 266 // SurfaceTexture zero or more times. | |
| 267 | |
| 268 // Start with SurfaceTexture. | |
| 269 EXPECT_CALL(client_, UseSurfaceTexture()); | |
| 270 StartHelper(nullptr); | |
| 271 testing::Mock::VerifyAndClearExpectations(&client_); | |
| 272 | |
| 273 // Provide a factory that will return a null overlay. | |
| 274 EXPECT_CALL(*factory_, MockCreateOverlay()); | |
| 275 EXPECT_CALL(client_, UseSurfaceTexture()).Times(AnyNumber()); | |
| 276 helper_->ReplaceOverlayFactory(std::move(factory_)); | |
| 277 factory_weak_->SetOverlay(nullptr); | |
| 278 } | |
| 279 | |
| 280 TEST_F(AndroidVideoSurfaceChooserImplTest, FailedLaterOverlayDoesNothing) { | |
| 281 // If we send an overlay factory that returns an overlay, and that overlay | |
| 282 // fails, then the client should not be notified except for zero or more | |
| 283 // callbacks to switch to surface texture. | |
| 284 | |
| 285 // Start with SurfaceTexture. | |
| 286 EXPECT_CALL(client_, UseSurfaceTexture()); | |
| 287 StartHelper(nullptr); | |
| 288 testing::Mock::VerifyAndClearExpectations(&client_); | |
| 289 | |
| 290 // Provide a factory. | |
| 291 EXPECT_CALL(*factory_, MockCreateOverlay()); | |
| 292 EXPECT_CALL(client_, UseSurfaceTexture()).Times(AnyNumber()); | |
| 293 helper_->ReplaceOverlayFactory(std::move(factory_)); | |
| 294 testing::Mock::VerifyAndClearExpectations(&client_); | |
| 295 | |
| 296 // Fail the overlay. We don't care if it's destroyed after that, as long as | |
| 297 // it hasn't been destroyed yet. | |
| 298 destruction_observer_ = nullptr; | |
| 299 overlay_callbacks_.OverlayFailed.Run(); | |
| 300 } | |
| 301 | |
| 302 TEST_F(AndroidVideoSurfaceChooserImplTest, | |
| 303 SuccessfulLaterOverlayNotifiesClient) { | |
| 304 // |client_| is notified if we provide a factory that gets an overlay. | |
| 305 | |
| 306 // Start with SurfaceTexture. | |
| 307 EXPECT_CALL(client_, UseSurfaceTexture()); | |
| 308 StartHelper(nullptr); | |
| 309 testing::Mock::VerifyAndClearExpectations(&client_); | |
| 310 | |
| 311 // Provide a factory. |helper_| should try to create an overlay. We don't | |
| 312 // care if a call to UseSurfaceTexture is elided or not. Note that AVDA will | |
| 313 // ignore duplicate calls anyway (MultipleSurfaceTextureCallbacksAreIgnored). | |
| 314 EXPECT_CALL(*factory_, MockCreateOverlay()); | |
| 315 EXPECT_CALL(client_, UseSurfaceTexture()).Times(AnyNumber()); | |
| 316 helper_->ReplaceOverlayFactory(std::move(factory_)); | |
| 317 testing::Mock::VerifyAndClearExpectations(&client_); | |
| 318 testing::Mock::VerifyAndClearExpectations(factory_weak_.get()); | |
| 319 | |
| 320 // Notify |helper_| that the overlay is ready. | |
| 321 EXPECT_CALL(client_, UseOverlay(NotNull())); | |
| 322 overlay_callbacks_.OverlayReady.Run(); | |
| 323 } | |
| 324 | |
| 325 } // namespace media | |
| OLD | NEW |