OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "core/dom/Document.h" |
| 6 #include "core/html/AutoplayExperimentHelper.h" |
| 7 #include "platform/UserGestureIndicator.h" |
| 8 #include "testing/gmock/include/gmock/gmock.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 // msvc refuses to compile if we use all of ::testing, due to a conflict with |
| 12 // WTF::NotNull. So, we just use what we need. |
| 13 using ::testing::Return; |
| 14 using ::testing::NiceMock; |
| 15 using ::testing::_; |
| 16 |
| 17 namespace blink { |
| 18 |
| 19 class MockAutoplayClient : public AutoplayExperimentHelper::Client { |
| 20 public: |
| 21 enum ElementType { Video, Audio }; |
| 22 |
| 23 MockAutoplayClient(const char* mode, ElementType type) |
| 24 : m_mode(mode) |
| 25 , m_duration(100) |
| 26 { |
| 27 // Set up default behaviors that the helper is allowed to use or cache |
| 28 // during construction. |
| 29 |
| 30 ON_CALL(*this, autoplayExperimentMode()) |
| 31 .WillByDefault(Return(m_mode)); |
| 32 |
| 33 // Use m_isVideo to answer these. |
| 34 ON_CALL(*this, isHTMLVideoElement()) |
| 35 .WillByDefault(Return(type == Video)); |
| 36 ON_CALL(*this, isHTMLAudioElement()) |
| 37 .WillByDefault(Return(type == Audio)); |
| 38 |
| 39 // Now set up some useful defaults. |
| 40 // Remember that this are only evaluated once. |
| 41 ON_CALL(*this, duration()) |
| 42 .WillByDefault(Return(m_duration)); |
| 43 ON_CALL(*this, currentTime()) |
| 44 .WillByDefault(Return(0)); |
| 45 |
| 46 // Default to "not optimized for mobile" page. |
| 47 ON_CALL(*this, isLegacyViewportType()) |
| 48 .WillByDefault(Return(false)); |
| 49 |
| 50 // Other handy defaults. |
| 51 ON_CALL(*this, ended()) |
| 52 .WillByDefault(Return(false)); |
| 53 ON_CALL(*this, pageVisibilityState()) |
| 54 .WillByDefault(Return(PageVisibilityStateVisible)); |
| 55 ON_CALL(*this, absoluteBoundingBoxRect()) |
| 56 .WillByDefault(Return( |
| 57 IntRect(10, 10, 100, 100))); |
| 58 |
| 59 // Normally, the autoplay experiment should not modify lots of other |
| 60 // state unless we explicitly expect it. |
| 61 EXPECT_CALL(*this, setMuted(_)) |
| 62 .Times(0); |
| 63 EXPECT_CALL(*this, removeUserGestureRequirement()) |
| 64 .Times(0); |
| 65 EXPECT_CALL(*this, setRequestPositionUpdates(true)) |
| 66 .Times(0); |
| 67 EXPECT_CALL(*this, recordAutoplayMetric(_)) |
| 68 .Times(0); |
| 69 } |
| 70 |
| 71 virtual ~MockAutoplayClient() {} |
| 72 |
| 73 MOCK_CONST_METHOD0(currentTime, double()); |
| 74 MOCK_CONST_METHOD0(duration, double()); |
| 75 MOCK_CONST_METHOD0(ended, bool()); |
| 76 MOCK_CONST_METHOD0(muted, bool()); |
| 77 MOCK_METHOD1(setMuted, void(bool)); |
| 78 MOCK_METHOD0(playInternal, void()); |
| 79 MOCK_CONST_METHOD0(isUserGestureRequiredForPlay, bool()); |
| 80 MOCK_METHOD0(removeUserGestureRequirement, void()); |
| 81 MOCK_METHOD1(recordAutoplayMetric, void(AutoplayMetrics)); |
| 82 MOCK_METHOD0(shouldAutoplay, bool()); |
| 83 MOCK_CONST_METHOD0(isHTMLVideoElement, bool()); |
| 84 MOCK_CONST_METHOD0(isHTMLAudioElement, bool()); |
| 85 MOCK_METHOD0(isLegacyViewportType, bool()); |
| 86 MOCK_CONST_METHOD0(pageVisibilityState, PageVisibilityState()); |
| 87 MOCK_CONST_METHOD0(autoplayExperimentMode, String()); |
| 88 MOCK_METHOD1(setRequestPositionUpdates, void(bool)); |
| 89 MOCK_CONST_METHOD0(absoluteBoundingBoxRect, IntRect()); |
| 90 |
| 91 const char* m_mode; |
| 92 // const since changes to it won't affect the mocked value. |
| 93 const double m_duration; |
| 94 }; |
| 95 |
| 96 class AutoplayExperimentTest : public ::testing::Test { |
| 97 public: |
| 98 AutoplayExperimentTest() {} |
| 99 |
| 100 ~AutoplayExperimentTest() {} |
| 101 |
| 102 bool isEligible() |
| 103 { |
| 104 return m_helper->isEligible(); |
| 105 } |
| 106 |
| 107 void setInterface(PassOwnPtrWillBeRawPtr<MockAutoplayClient> client) |
| 108 { |
| 109 m_client = client; |
| 110 |
| 111 // Set some defaults. |
| 112 setUserGestureRequiredForPlay(true); |
| 113 setShouldAutoplay(true); |
| 114 setIsMuted(false); |
| 115 |
| 116 m_helper = AutoplayExperimentHelper::create(m_client.get()); |
| 117 } |
| 118 |
| 119 #if ENABLE(OILPAN) |
| 120 void TearDown() |
| 121 { |
| 122 // Be sure that the mock is destructed before the test, so that any |
| 123 // missing expectations are noticed. Otherwise, the test will pass |
| 124 // and then missing expectations will show up in the log, without |
| 125 // causing a test failure. |
| 126 m_helper.clear(); |
| 127 m_client.clear(); |
| 128 Heap::collectAllGarbage(); |
| 129 } |
| 130 #endif |
| 131 |
| 132 OwnPtrWillBePersistent<MockAutoplayClient> m_client; |
| 133 OwnPtrWillBePersistent<AutoplayExperimentHelper> m_helper; |
| 134 |
| 135 // Mirror updatePlayState to transition to play. |
| 136 void startPlayback() |
| 137 { |
| 138 EXPECT_CALL(*m_client, recordAutoplayMetric(AnyPlaybackStarted)) |
| 139 .Times(1); |
| 140 m_helper->playbackStarted(); |
| 141 } |
| 142 |
| 143 void startPlaybackWithoutUserGesture() |
| 144 { |
| 145 EXPECT_FALSE(UserGestureIndicator::processingUserGesture()); |
| 146 startPlayback(); |
| 147 } |
| 148 |
| 149 void startPlaybackWithUserGesture() |
| 150 { |
| 151 UserGestureIndicator indicator(DefinitelyProcessingUserGesture); |
| 152 EXPECT_TRUE(UserGestureIndicator::processingUserGesture()); |
| 153 startPlayback(); |
| 154 } |
| 155 |
| 156 void setUserGestureRequiredForPlay(bool required) |
| 157 { |
| 158 ON_CALL(*m_client, isUserGestureRequiredForPlay()) |
| 159 .WillByDefault(Return(required)); |
| 160 } |
| 161 |
| 162 void setShouldAutoplay(bool should) |
| 163 { |
| 164 ON_CALL(*m_client, shouldAutoplay()) |
| 165 .WillByDefault(Return(should)); |
| 166 } |
| 167 |
| 168 void setIsMuted(bool isMuted) |
| 169 { |
| 170 ON_CALL(*m_client, muted()) |
| 171 .WillByDefault(Return(isMuted)); |
| 172 } |
| 173 |
| 174 void pausePlaybackExpectingBailout(bool expectingAutoplay) |
| 175 { |
| 176 EXPECT_CALL(*m_client, recordAutoplayMetric(AnyPlaybackPaused)) |
| 177 .Times(1); |
| 178 EXPECT_CALL(*m_client, recordAutoplayMetric(AnyPlaybackBailout)) |
| 179 .Times(1); |
| 180 if (expectingAutoplay) { |
| 181 EXPECT_CALL(*m_client, recordAutoplayMetric(AutoplayPaused)) |
| 182 .Times(1); |
| 183 EXPECT_CALL(*m_client, recordAutoplayMetric(AutoplayBailout)) |
| 184 .Times(1); |
| 185 } |
| 186 m_helper->pauseMethodCalled(); |
| 187 m_helper->playbackStopped(); |
| 188 } |
| 189 |
| 190 void pausePlaybackNotExpectingBailout(bool expectingAutoplay) |
| 191 { |
| 192 EXPECT_CALL(*m_client, recordAutoplayMetric(AnyPlaybackPaused)) |
| 193 .Times(1); |
| 194 if (expectingAutoplay) |
| 195 EXPECT_CALL(*m_client, recordAutoplayMetric(AutoplayPaused)).Times(1
); |
| 196 EXPECT_CALL(*m_client, recordAutoplayMetric(AnyPlaybackBailout)).Times(0
); |
| 197 EXPECT_CALL(*m_client, recordAutoplayMetric(AutoplayBailout)).Times(0); |
| 198 |
| 199 // Move past the bailout point, but not to the end. That way, we don't |
| 200 // have to set ended(). |
| 201 ON_CALL(*m_client, currentTime()) |
| 202 .WillByDefault(Return(m_client->m_duration-1)); |
| 203 |
| 204 m_helper->pauseMethodCalled(); |
| 205 m_helper->playbackStopped(); |
| 206 } |
| 207 |
| 208 void endPlayback(bool expectingAutoplay) |
| 209 { |
| 210 EXPECT_CALL(*m_client, recordAutoplayMetric(AnyPlaybackComplete)) |
| 211 .Times(1); |
| 212 if (expectingAutoplay) |
| 213 EXPECT_CALL(*m_client, recordAutoplayMetric(AutoplayComplete)).Times
(1); |
| 214 ON_CALL(*m_client, currentTime()) |
| 215 .WillByDefault(Return(m_client->m_duration)); |
| 216 ON_CALL(*m_client, ended()) |
| 217 .WillByDefault(Return(true)); |
| 218 m_helper->playbackStopped(); |
| 219 } |
| 220 |
| 221 void moveIntoViewport() |
| 222 { |
| 223 m_helper->positionChanged(IntRect(0, 0, 200, 200)); |
| 224 m_helper->triggerAutoplayViewportCheckForTesting(); |
| 225 } |
| 226 }; |
| 227 |
| 228 TEST_F(AutoplayExperimentTest, IsNotEligibleWithEmptyMode) |
| 229 { |
| 230 setInterface(adoptPtrWillBeNoop(new NiceMock<MockAutoplayClient>("", MockAut
oplayClient::Video))); |
| 231 EXPECT_FALSE(isEligible()); |
| 232 } |
| 233 |
| 234 TEST_F(AutoplayExperimentTest, IsVideoEligibleForVideoMode) |
| 235 { |
| 236 // Video should be eligible in "forvideo" mode. |
| 237 setInterface(adoptPtrWillBeNoop(new NiceMock<MockAutoplayClient>("enabled-fo
rvideo", MockAutoplayClient::Video))); |
| 238 EXPECT_TRUE(isEligible()); |
| 239 } |
| 240 |
| 241 TEST_F(AutoplayExperimentTest, IsAudioNotEligibleForVideoMode) |
| 242 { |
| 243 // Audio should not be eligible for video mode. |
| 244 setInterface(adoptPtrWillBeNoop(new NiceMock<MockAutoplayClient>("enabled-fo
rvideo", MockAutoplayClient::Audio))); |
| 245 EXPECT_FALSE(isEligible()); |
| 246 } |
| 247 |
| 248 TEST_F(AutoplayExperimentTest, IsEligibleRequiresUserGesture) |
| 249 { |
| 250 setInterface(adoptPtrWillBeNoop(new NiceMock<MockAutoplayClient>("enabled-fo
rvideo", MockAutoplayClient::Video))); |
| 251 // If a user gesture is not required, then we're not eligible. |
| 252 ON_CALL(*m_client, isUserGestureRequiredForPlay()) |
| 253 .WillByDefault(Return(false)); |
| 254 EXPECT_FALSE(isEligible()); |
| 255 } |
| 256 |
| 257 TEST_F(AutoplayExperimentTest, IsEligibleRequiresShouldAutoplay) |
| 258 { |
| 259 setInterface(adoptPtrWillBeNoop(new NiceMock<MockAutoplayClient>("enabled-fo
rvideo", MockAutoplayClient::Video))); |
| 260 // If we shouldn't autoplay, then we're not eligible. |
| 261 ON_CALL(*m_client, shouldAutoplay()) |
| 262 .WillByDefault(Return(false)); |
| 263 EXPECT_FALSE(isEligible()); |
| 264 } |
| 265 |
| 266 TEST_F(AutoplayExperimentTest, IsAudioEligibleForAudioMode) |
| 267 { |
| 268 setInterface(adoptPtrWillBeNoop(new NiceMock<MockAutoplayClient>("enabled-fo
raudio", MockAutoplayClient::Audio))); |
| 269 EXPECT_TRUE(isEligible()); |
| 270 } |
| 271 |
| 272 TEST_F(AutoplayExperimentTest, EligibleIfOptimizedForMobile) |
| 273 { |
| 274 setInterface(adoptPtrWillBeNoop(new NiceMock<MockAutoplayClient>("enabled-fo
rvideo-ifmobile", MockAutoplayClient::Video))); |
| 275 // Should not be eligible with our default of "not mobile". |
| 276 EXPECT_FALSE(isEligible()); |
| 277 |
| 278 ON_CALL(*m_client, isLegacyViewportType()) |
| 279 .WillByDefault(Return(true)); |
| 280 EXPECT_TRUE(isEligible()); |
| 281 } |
| 282 |
| 283 TEST_F(AutoplayExperimentTest, EligibleIfMuted) |
| 284 { |
| 285 setInterface(adoptPtrWillBeNoop(new NiceMock<MockAutoplayClient>("enabled-fo
rvideo-ifmuted", MockAutoplayClient::Video))); |
| 286 // Should not be eligible with our default of "not muted". |
| 287 EXPECT_FALSE(isEligible()); |
| 288 |
| 289 ON_CALL(*m_client, muted()) |
| 290 .WillByDefault(Return(true)); |
| 291 EXPECT_TRUE(isEligible()); |
| 292 } |
| 293 |
| 294 TEST_F(AutoplayExperimentTest, BecameReadyAutoplayThenBailout) |
| 295 { |
| 296 setInterface(adoptPtrWillBeNoop(new NiceMock<MockAutoplayClient>("enabled-fo
rvideo", MockAutoplayClient::Video))); |
| 297 |
| 298 EXPECT_CALL(*m_client, removeUserGestureRequirement()) |
| 299 .Times(1); |
| 300 EXPECT_CALL(*m_client, recordAutoplayMetric(AutoplayMediaFound)) |
| 301 .Times(1); |
| 302 m_helper->becameReadyToPlay(); |
| 303 |
| 304 EXPECT_CALL(*m_client, recordAutoplayMetric(GesturelessPlaybackStartedByAuto
playFlagImmediately)) |
| 305 .Times(1); |
| 306 startPlaybackWithoutUserGesture(); |
| 307 |
| 308 pausePlaybackExpectingBailout(true); |
| 309 } |
| 310 |
| 311 TEST_F(AutoplayExperimentTest, BecameReadyAutoplayThenPause) |
| 312 { |
| 313 setInterface(adoptPtrWillBeNoop(new NiceMock<MockAutoplayClient>("enabled-fo
rvideo", MockAutoplayClient::Video))); |
| 314 |
| 315 EXPECT_CALL(*m_client, removeUserGestureRequirement()) |
| 316 .Times(1); |
| 317 EXPECT_CALL(*m_client, recordAutoplayMetric(AutoplayMediaFound)) |
| 318 .Times(1); |
| 319 m_helper->becameReadyToPlay(); |
| 320 |
| 321 EXPECT_CALL(*m_client, recordAutoplayMetric(GesturelessPlaybackStartedByAuto
playFlagImmediately)) |
| 322 .Times(1); |
| 323 startPlaybackWithoutUserGesture(); |
| 324 |
| 325 pausePlaybackNotExpectingBailout(true); |
| 326 } |
| 327 |
| 328 TEST_F(AutoplayExperimentTest, BecameReadyAutoplayThenComplete) |
| 329 { |
| 330 setInterface(adoptPtrWillBeNoop(new NiceMock<MockAutoplayClient>("enabled-fo
rvideo", MockAutoplayClient::Video))); |
| 331 |
| 332 EXPECT_CALL(*m_client, removeUserGestureRequirement()) |
| 333 .Times(1); |
| 334 EXPECT_CALL(*m_client, recordAutoplayMetric(AutoplayMediaFound)) |
| 335 .Times(1); |
| 336 m_helper->becameReadyToPlay(); |
| 337 |
| 338 EXPECT_CALL(*m_client, recordAutoplayMetric(GesturelessPlaybackStartedByAuto
playFlagImmediately)) |
| 339 .Times(1); |
| 340 startPlaybackWithoutUserGesture(); |
| 341 |
| 342 // Now stop at the end. |
| 343 endPlayback(true); |
| 344 } |
| 345 |
| 346 TEST_F(AutoplayExperimentTest, NoUserGestureNeededShouldNotOverride) |
| 347 { |
| 348 // Make sure that we don't override the user gesture if it isn't needed. |
| 349 setInterface(adoptPtrWillBeNoop(new NiceMock<MockAutoplayClient>("enabled-fo
rvideo", MockAutoplayClient::Video))); |
| 350 setUserGestureRequiredForPlay(false); |
| 351 |
| 352 // It is still autoplay media, though. |
| 353 EXPECT_CALL(*m_client, recordAutoplayMetric(AutoplayMediaFound)) |
| 354 .Times(1); |
| 355 m_helper->becameReadyToPlay(); |
| 356 |
| 357 EXPECT_CALL(*m_client, recordAutoplayMetric(GesturelessPlaybackNotOverridden
)) |
| 358 .Times(1); |
| 359 startPlaybackWithoutUserGesture(); |
| 360 } |
| 361 |
| 362 TEST_F(AutoplayExperimentTest, NoAutoplayMetricsIfNoAutoplay) |
| 363 { |
| 364 // If playback is started while processing a user gesture, then nothing |
| 365 // should be overridden or logged about autoplay. |
| 366 setInterface(adoptPtrWillBeNoop(new NiceMock<MockAutoplayClient>("enabled-fo
rvideo", MockAutoplayClient::Video))); |
| 367 setUserGestureRequiredForPlay(false); |
| 368 setShouldAutoplay(false); |
| 369 startPlaybackWithUserGesture(); |
| 370 |
| 371 // Expect bailout, but not from autoplay. |
| 372 pausePlaybackExpectingBailout(false); |
| 373 } |
| 374 |
| 375 TEST_F(AutoplayExperimentTest, PlayMethodThenBailout) |
| 376 { |
| 377 setInterface(adoptPtrWillBeNoop(new NiceMock<MockAutoplayClient>("enabled-fo
rvideo", MockAutoplayClient::Video))); |
| 378 setShouldAutoplay(false); // No autoplay attribute. |
| 379 |
| 380 EXPECT_CALL(*m_client, removeUserGestureRequirement()) |
| 381 .Times(1); |
| 382 EXPECT_CALL(*m_client, recordAutoplayMetric(AutoplayMediaFound)) |
| 383 .Times(1); |
| 384 m_helper->playMethodCalled(); |
| 385 |
| 386 EXPECT_CALL(*m_client, recordAutoplayMetric(GesturelessPlaybackStartedByPlay
MethodImmediately)) |
| 387 .Times(1); |
| 388 startPlaybackWithoutUserGesture(); |
| 389 |
| 390 pausePlaybackExpectingBailout(true); |
| 391 } |
| 392 |
| 393 TEST_F(AutoplayExperimentTest, DeferAutoplayUntilMuted) |
| 394 { |
| 395 setInterface(adoptPtrWillBeNoop(new NiceMock<MockAutoplayClient>("enabled-fo
rvideo-ifmuted", MockAutoplayClient::Video))); |
| 396 |
| 397 // Should not override the gesture requirement yet. |
| 398 EXPECT_CALL(*m_client, recordAutoplayMetric(AutoplayMediaFound)) |
| 399 .Times(1); |
| 400 m_helper->becameReadyToPlay(); |
| 401 |
| 402 // When we toggle the muted attribute, it should start. |
| 403 EXPECT_CALL(*m_client, removeUserGestureRequirement()) |
| 404 .Times(1); |
| 405 EXPECT_CALL(*m_client, playInternal()) |
| 406 .Times(1); |
| 407 setIsMuted(true); |
| 408 m_helper->mutedChanged(); |
| 409 |
| 410 // When playback starts (in response to playInternal()), it should also |
| 411 // record why. 'After scroll' isn't the best name, but this isn't a common
case. |
| 412 EXPECT_CALL(*m_client, recordAutoplayMetric(GesturelessPlaybackStartedByAuto
playFlagAfterScroll)) |
| 413 .Times(1); |
| 414 startPlaybackWithoutUserGesture(); |
| 415 } |
| 416 |
| 417 TEST_F(AutoplayExperimentTest, DeferPlaybackUntilInViewport) |
| 418 { |
| 419 setInterface(adoptPtrWillBeNoop(new NiceMock<MockAutoplayClient>("enabled-fo
rvideo-ifviewport", MockAutoplayClient::Video))); |
| 420 |
| 421 // Should not override the gesture requirement yet. |
| 422 EXPECT_CALL(*m_client, recordAutoplayMetric(AutoplayMediaFound)) |
| 423 .Times(1); |
| 424 EXPECT_CALL(*m_client, setRequestPositionUpdates(true)) |
| 425 .Times(1); |
| 426 m_helper->becameReadyToPlay(); |
| 427 |
| 428 EXPECT_CALL(*m_client, removeUserGestureRequirement()) |
| 429 .Times(1); |
| 430 EXPECT_CALL(*m_client, playInternal()) |
| 431 .Times(1); |
| 432 EXPECT_CALL(*m_client, setRequestPositionUpdates(false)) |
| 433 .Times(1); |
| 434 moveIntoViewport(); |
| 435 } |
| 436 } |
OLD | NEW |