OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "modules/remoteplayback/RemotePlayback.h" | 5 #include "modules/remoteplayback/RemotePlayback.h" |
6 | 6 |
7 #include "bindings/core/v8/ExceptionStatePlaceholder.h" | 7 #include "bindings/core/v8/ExceptionStatePlaceholder.h" |
8 #include "bindings/core/v8/V8BindingForTesting.h" | 8 #include "bindings/core/v8/V8BindingForTesting.h" |
| 9 #include "bindings/modules/v8/RemotePlaybackAvailabilityCallback.h" |
9 #include "core/html/HTMLMediaElement.h" | 10 #include "core/html/HTMLMediaElement.h" |
10 #include "core/html/HTMLVideoElement.h" | 11 #include "core/html/HTMLVideoElement.h" |
11 #include "core/testing/DummyPageHolder.h" | 12 #include "core/testing/DummyPageHolder.h" |
| 13 #include "modules/remoteplayback/HTMLMediaElementRemotePlayback.h" |
| 14 #include "platform/UserGestureIndicator.h" |
12 #include "platform/testing/UnitTestHelpers.h" | 15 #include "platform/testing/UnitTestHelpers.h" |
13 #include "public/platform/modules/remoteplayback/WebRemotePlaybackState.h" | 16 #include "public/platform/modules/remoteplayback/WebRemotePlaybackState.h" |
14 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
16 | 19 |
17 namespace blink { | 20 namespace blink { |
18 | 21 |
19 class MockFunction : public ScriptFunction { | 22 class MockFunction : public ScriptFunction { |
20 public: | 23 public: |
21 static MockFunction* create(ScriptState* scriptState) { | 24 static MockFunction* create(ScriptState* scriptState) { |
(...skipping 22 matching lines...) Expand all Loading... |
44 | 47 |
45 class RemotePlaybackTest : public ::testing::Test { | 48 class RemotePlaybackTest : public ::testing::Test { |
46 protected: | 49 protected: |
47 void cancelPrompt(RemotePlayback* remotePlayback) { | 50 void cancelPrompt(RemotePlayback* remotePlayback) { |
48 remotePlayback->promptCancelled(); | 51 remotePlayback->promptCancelled(); |
49 } | 52 } |
50 | 53 |
51 void setState(RemotePlayback* remotePlayback, WebRemotePlaybackState state) { | 54 void setState(RemotePlayback* remotePlayback, WebRemotePlaybackState state) { |
52 remotePlayback->stateChanged(state); | 55 remotePlayback->stateChanged(state); |
53 } | 56 } |
| 57 |
| 58 void setAvailability(RemotePlayback* remotePlayback, bool available) { |
| 59 remotePlayback->availabilityChanged(available); |
| 60 } |
54 }; | 61 }; |
55 | 62 |
56 TEST_F(RemotePlaybackTest, PromptCancelledRejectsWithNotAllowedError) { | 63 TEST_F(RemotePlaybackTest, PromptCancelledRejectsWithNotAllowedError) { |
57 V8TestingScope scope; | 64 V8TestingScope scope; |
58 | 65 |
59 auto pageHolder = DummyPageHolder::create(); | 66 auto pageHolder = DummyPageHolder::create(); |
60 | 67 |
61 HTMLMediaElement* element = HTMLVideoElement::create(pageHolder->document()); | 68 HTMLMediaElement* element = HTMLVideoElement::create(pageHolder->document()); |
62 RemotePlayback* remotePlayback = | 69 RemotePlayback* remotePlayback = |
63 RemotePlayback::create(scope.getScriptState(), *element); | 70 HTMLMediaElementRemotePlayback::remote(scope.getScriptState(), *element); |
64 | 71 |
65 MockFunction* resolve = MockFunction::create(scope.getScriptState()); | 72 MockFunction* resolve = MockFunction::create(scope.getScriptState()); |
66 MockFunction* reject = MockFunction::create(scope.getScriptState()); | 73 MockFunction* reject = MockFunction::create(scope.getScriptState()); |
67 | 74 |
68 EXPECT_CALL(*resolve, call(::testing::_)).Times(0); | 75 EXPECT_CALL(*resolve, call(::testing::_)).Times(0); |
69 EXPECT_CALL(*reject, call(::testing::_)).Times(1); | 76 EXPECT_CALL(*reject, call(::testing::_)).Times(1); |
70 | 77 |
| 78 UserGestureIndicator indicator( |
| 79 UserGestureToken::create(UserGestureToken::NewGesture)); |
71 remotePlayback->prompt().then(resolve->bind(), reject->bind()); | 80 remotePlayback->prompt().then(resolve->bind(), reject->bind()); |
72 cancelPrompt(remotePlayback); | 81 cancelPrompt(remotePlayback); |
73 } | 82 } |
74 | 83 |
75 TEST_F(RemotePlaybackTest, StateChangeEvents) { | 84 TEST_F(RemotePlaybackTest, StateChangeEvents) { |
76 V8TestingScope scope; | 85 V8TestingScope scope; |
77 | 86 |
78 auto pageHolder = DummyPageHolder::create(); | 87 auto pageHolder = DummyPageHolder::create(); |
79 | 88 |
80 HTMLMediaElement* element = HTMLVideoElement::create(pageHolder->document()); | 89 HTMLMediaElement* element = HTMLVideoElement::create(pageHolder->document()); |
81 RemotePlayback* remotePlayback = | 90 RemotePlayback* remotePlayback = |
82 RemotePlayback::create(scope.getScriptState(), *element); | 91 HTMLMediaElementRemotePlayback::remote(scope.getScriptState(), *element); |
83 | 92 |
84 auto connectingHandler = new ::testing::StrictMock<MockEventListener>(); | 93 auto connectingHandler = new ::testing::StrictMock<MockEventListener>(); |
85 auto connectHandler = new ::testing::StrictMock<MockEventListener>(); | 94 auto connectHandler = new ::testing::StrictMock<MockEventListener>(); |
86 auto disconnectHandler = new ::testing::StrictMock<MockEventListener>(); | 95 auto disconnectHandler = new ::testing::StrictMock<MockEventListener>(); |
87 | 96 |
88 remotePlayback->addEventListener(EventTypeNames::connecting, | 97 remotePlayback->addEventListener(EventTypeNames::connecting, |
89 connectingHandler); | 98 connectingHandler); |
90 remotePlayback->addEventListener(EventTypeNames::connect, connectHandler); | 99 remotePlayback->addEventListener(EventTypeNames::connect, connectHandler); |
91 remotePlayback->addEventListener(EventTypeNames::disconnect, | 100 remotePlayback->addEventListener(EventTypeNames::disconnect, |
92 disconnectHandler); | 101 disconnectHandler); |
93 | 102 |
94 EXPECT_CALL(*connectingHandler, handleEvent(::testing::_, ::testing::_)) | 103 EXPECT_CALL(*connectingHandler, handleEvent(::testing::_, ::testing::_)) |
95 .Times(1); | 104 .Times(1); |
96 EXPECT_CALL(*connectHandler, handleEvent(::testing::_, ::testing::_)) | 105 EXPECT_CALL(*connectHandler, handleEvent(::testing::_, ::testing::_)) |
97 .Times(1); | 106 .Times(1); |
98 EXPECT_CALL(*disconnectHandler, handleEvent(::testing::_, ::testing::_)) | 107 EXPECT_CALL(*disconnectHandler, handleEvent(::testing::_, ::testing::_)) |
99 .Times(1); | 108 .Times(1); |
100 | 109 |
101 setState(remotePlayback, WebRemotePlaybackState::Connecting); | 110 setState(remotePlayback, WebRemotePlaybackState::Connecting); |
102 setState(remotePlayback, WebRemotePlaybackState::Connecting); | 111 setState(remotePlayback, WebRemotePlaybackState::Connecting); |
103 setState(remotePlayback, WebRemotePlaybackState::Connected); | 112 setState(remotePlayback, WebRemotePlaybackState::Connected); |
104 setState(remotePlayback, WebRemotePlaybackState::Connected); | 113 setState(remotePlayback, WebRemotePlaybackState::Connected); |
105 setState(remotePlayback, WebRemotePlaybackState::Disconnected); | 114 setState(remotePlayback, WebRemotePlaybackState::Disconnected); |
106 setState(remotePlayback, WebRemotePlaybackState::Disconnected); | 115 setState(remotePlayback, WebRemotePlaybackState::Disconnected); |
107 } | 116 } |
108 | 117 |
| 118 TEST_F(RemotePlaybackTest, |
| 119 DisableRemotePlaybackRejectsPromptWithInvalidStateError) { |
| 120 V8TestingScope scope; |
| 121 |
| 122 auto pageHolder = DummyPageHolder::create(); |
| 123 |
| 124 HTMLMediaElement* element = HTMLVideoElement::create(pageHolder->document()); |
| 125 RemotePlayback* remotePlayback = |
| 126 HTMLMediaElementRemotePlayback::remote(scope.getScriptState(), *element); |
| 127 |
| 128 MockFunction* resolve = MockFunction::create(scope.getScriptState()); |
| 129 MockFunction* reject = MockFunction::create(scope.getScriptState()); |
| 130 |
| 131 EXPECT_CALL(*resolve, call(::testing::_)).Times(0); |
| 132 EXPECT_CALL(*reject, call(::testing::_)).Times(1); |
| 133 |
| 134 UserGestureIndicator indicator( |
| 135 UserGestureToken::create(UserGestureToken::NewGesture)); |
| 136 remotePlayback->prompt().then(resolve->bind(), reject->bind()); |
| 137 HTMLMediaElementRemotePlayback::setBooleanAttribute( |
| 138 HTMLNames::disableremoteplaybackAttr, *element, true); |
| 139 } |
| 140 |
| 141 TEST_F(RemotePlaybackTest, DisableRemotePlaybackCancelsAvailabilityCallbacks) { |
| 142 V8TestingScope scope; |
| 143 |
| 144 auto pageHolder = DummyPageHolder::create(); |
| 145 |
| 146 HTMLMediaElement* element = HTMLVideoElement::create(pageHolder->document()); |
| 147 RemotePlayback* remotePlayback = |
| 148 HTMLMediaElementRemotePlayback::remote(scope.getScriptState(), *element); |
| 149 |
| 150 MockFunction* callbackFunction = MockFunction::create(scope.getScriptState()); |
| 151 RemotePlaybackAvailabilityCallback* availabilityCallback = |
| 152 RemotePlaybackAvailabilityCallback::create(scope.isolate(), |
| 153 callbackFunction->bind()); |
| 154 |
| 155 // The initial call upon registering will not happen as it's posted on the |
| 156 // message loop. |
| 157 EXPECT_CALL(*callbackFunction, call(::testing::_)).Times(0); |
| 158 |
| 159 MockFunction* watchResolve = MockFunction::create(scope.getScriptState()); |
| 160 MockFunction* watchReject = MockFunction::create(scope.getScriptState()); |
| 161 |
| 162 EXPECT_CALL(*watchResolve, call(::testing::_)).Times(1); |
| 163 EXPECT_CALL(*watchReject, call(::testing::_)).Times(0); |
| 164 |
| 165 remotePlayback->watchAvailability(availabilityCallback) |
| 166 .then(watchResolve->bind(), watchReject->bind()); |
| 167 |
| 168 HTMLMediaElementRemotePlayback::setBooleanAttribute( |
| 169 HTMLNames::disableremoteplaybackAttr, *element, true); |
| 170 setAvailability(remotePlayback, true); |
| 171 } |
| 172 |
109 } // namespace blink | 173 } // namespace blink |
OLD | NEW |