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 "core/html/HTMLMediaElement.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "core/dom/DocumentUserGestureToken.h" |
| 10 #include "core/dom/Fullscreen.h" |
| 11 #include "core/html/HTMLDivElement.h" |
| 12 #include "core/loader/EmptyClients.h" |
| 13 #include "core/testing/DummyPageHolder.h" |
| 14 #include "platform/UserGestureIndicator.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 namespace blink { |
| 19 |
| 20 namespace { |
| 21 |
| 22 class MockChromeClient : public EmptyChromeClient { |
| 23 public: |
| 24 MOCK_METHOD1(enterFullscreen, void(LocalFrame&)); |
| 25 MOCK_METHOD1(exitFullscreen, void(LocalFrame&)); |
| 26 }; |
| 27 |
| 28 using ::testing::_; |
| 29 using ::testing::Sequence; |
| 30 |
| 31 } // anonymous namespace |
| 32 |
| 33 class HTMLMediaElementPersistentVideoTest : public ::testing::Test { |
| 34 protected: |
| 35 void SetUp() override { |
| 36 m_chromeClient = new MockChromeClient(); |
| 37 |
| 38 Page::PageClients clients; |
| 39 fillWithEmptyClients(clients); |
| 40 clients.chromeClient = m_chromeClient.get(); |
| 41 |
| 42 m_pageHolder = DummyPageHolder::create(IntSize(800, 600), &clients); |
| 43 document().body()->setInnerHTML("<body><div><video></video></div></body>"); |
| 44 } |
| 45 |
| 46 Document& document() { return m_pageHolder->document(); } |
| 47 |
| 48 HTMLMediaElement* videoElement() { |
| 49 return toHTMLMediaElement(document().querySelector("video")); |
| 50 } |
| 51 |
| 52 HTMLDivElement* divElement() { |
| 53 return toHTMLDivElement(document().querySelector("div")); |
| 54 } |
| 55 |
| 56 Element* fullscreenElement() { |
| 57 return Fullscreen::currentFullScreenElementFrom(document()); |
| 58 } |
| 59 |
| 60 MockChromeClient& mockChromeClient() { return *m_chromeClient; } |
| 61 |
| 62 void simulateDidEnterFullscreen() { |
| 63 Fullscreen::fromIfExists(document())->didEnterFullscreen(); |
| 64 } |
| 65 |
| 66 void simulateDidExitFullscreen() { |
| 67 Fullscreen::fromIfExists(document())->didExitFullscreen(); |
| 68 } |
| 69 |
| 70 void simulateBecamePersistentVideo(bool value) { |
| 71 videoElement()->onBecamePersistentVideo(value); |
| 72 } |
| 73 |
| 74 bool isPersistentVideo() { return videoElement()->m_isPersistentVideo; } |
| 75 |
| 76 private: |
| 77 std::unique_ptr<DummyPageHolder> m_pageHolder; |
| 78 Persistent<MockChromeClient> m_chromeClient; |
| 79 }; |
| 80 |
| 81 TEST_F(HTMLMediaElementPersistentVideoTest, testWhenNothingIsFullscreen) { |
| 82 Sequence s; |
| 83 |
| 84 EXPECT_EQ(fullscreenElement(), nullptr); |
| 85 |
| 86 EXPECT_CALL(mockChromeClient(), enterFullscreen(_)).InSequence(s); |
| 87 |
| 88 // Make the video persistent. |
| 89 simulateBecamePersistentVideo(true); |
| 90 simulateDidEnterFullscreen(); |
| 91 EXPECT_EQ(fullscreenElement(), videoElement()); |
| 92 EXPECT_TRUE(isPersistentVideo()); |
| 93 |
| 94 // This should be no-op. |
| 95 simulateBecamePersistentVideo(true); |
| 96 EXPECT_EQ(fullscreenElement(), videoElement()); |
| 97 EXPECT_TRUE(isPersistentVideo()); |
| 98 |
| 99 // Make the video not persitent. |
| 100 EXPECT_CALL(mockChromeClient(), exitFullscreen(_)).InSequence(s); |
| 101 simulateBecamePersistentVideo(false); |
| 102 simulateDidExitFullscreen(); |
| 103 EXPECT_EQ(fullscreenElement(), nullptr); |
| 104 EXPECT_FALSE(isPersistentVideo()); |
| 105 |
| 106 // This should be no-op. |
| 107 simulateBecamePersistentVideo(false); |
| 108 EXPECT_EQ(fullscreenElement(), nullptr); |
| 109 EXPECT_FALSE(isPersistentVideo()); |
| 110 } |
| 111 |
| 112 TEST_F(HTMLMediaElementPersistentVideoTest, testWhenVideoIsFullscreen) { |
| 113 EXPECT_EQ(fullscreenElement(), nullptr); |
| 114 |
| 115 EXPECT_CALL(mockChromeClient(), enterFullscreen(_)).Times(1); |
| 116 EXPECT_CALL(mockChromeClient(), exitFullscreen(_)).Times(0); |
| 117 |
| 118 // Fullscreen the video in advance. |
| 119 UserGestureIndicator gestureIndicator( |
| 120 DocumentUserGestureToken::create(&document())); |
| 121 Fullscreen::requestFullscreen(*videoElement()); |
| 122 simulateDidEnterFullscreen(); |
| 123 EXPECT_EQ(fullscreenElement(), videoElement()); |
| 124 |
| 125 // This should be no-op. |
| 126 simulateBecamePersistentVideo(true); |
| 127 EXPECT_EQ(fullscreenElement(), videoElement()); |
| 128 EXPECT_FALSE(isPersistentVideo()); |
| 129 |
| 130 // This should be no-op. |
| 131 simulateBecamePersistentVideo(false); |
| 132 EXPECT_EQ(fullscreenElement(), videoElement()); |
| 133 EXPECT_FALSE(isPersistentVideo()); |
| 134 } |
| 135 |
| 136 TEST_F(HTMLMediaElementPersistentVideoTest, testWhenDivIsFullscreen) { |
| 137 EXPECT_EQ(fullscreenElement(), nullptr); |
| 138 |
| 139 EXPECT_CALL(mockChromeClient(), enterFullscreen(_)).Times(2); |
| 140 EXPECT_CALL(mockChromeClient(), exitFullscreen(_)).Times(0); |
| 141 |
| 142 // Fullscreen the video in advance. |
| 143 UserGestureIndicator gestureIndicator( |
| 144 DocumentUserGestureToken::create(&document())); |
| 145 Fullscreen::requestFullscreen(*divElement()); |
| 146 simulateDidEnterFullscreen(); |
| 147 EXPECT_EQ(fullscreenElement(), divElement()); |
| 148 |
| 149 // Make the video persistent. |
| 150 simulateBecamePersistentVideo(true); |
| 151 simulateDidEnterFullscreen(); |
| 152 EXPECT_EQ(fullscreenElement(), videoElement()); |
| 153 EXPECT_TRUE(isPersistentVideo()); |
| 154 |
| 155 // This should be no-op. |
| 156 simulateBecamePersistentVideo(true); |
| 157 EXPECT_EQ(fullscreenElement(), videoElement()); |
| 158 EXPECT_TRUE(isPersistentVideo()); |
| 159 |
| 160 // Make the video not persistent. Don't call simulateDidExitFullscreen as |
| 161 // there still a fullscreen element in stack. |
| 162 simulateBecamePersistentVideo(false); |
| 163 EXPECT_EQ(fullscreenElement(), divElement()); |
| 164 EXPECT_FALSE(isPersistentVideo()); |
| 165 } |
| 166 |
| 167 } // namespace blink |
OLD | NEW |