| 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/presentation/PresentationReceiver.h" | 5 #include "modules/presentation/PresentationReceiver.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ScriptPromiseResolver.h" | 7 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 8 #include "bindings/core/v8/V8BindingForTesting.h" | 8 #include "bindings/core/v8/V8BindingForTesting.h" |
| 9 #include "core/frame/LocalFrame.h" | 9 #include "core/frame/LocalFrame.h" |
| 10 #include "core/testing/DummyPageHolder.h" | 10 #include "core/testing/DummyPageHolder.h" |
| 11 #include "modules/presentation/PresentationConnectionList.h" | 11 #include "modules/presentation/PresentationConnectionList.h" |
| 12 #include "platform/testing/URLTestHelpers.h" | 12 #include "platform/testing/URLTestHelpers.h" |
| 13 #include "public/platform/modules/presentation/WebPresentationClient.h" |
| 13 #include "public/platform/modules/presentation/WebPresentationConnectionClient.h
" | 14 #include "public/platform/modules/presentation/WebPresentationConnectionClient.h
" |
| 14 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include <v8.h> | 17 #include <v8.h> |
| 17 | 18 |
| 18 namespace blink { | 19 namespace blink { |
| 19 | 20 |
| 20 class MockEventListener : public EventListener { | 21 class MockEventListener : public EventListener { |
| 21 public: | 22 public: |
| 22 MockEventListener() | 23 MockEventListener() |
| 23 : EventListener(CPPEventListenerType) | 24 : EventListener(CPPEventListenerType) |
| 24 { | 25 { |
| 25 } | 26 } |
| 26 | 27 |
| 27 bool operator==(const EventListener& other) const final | 28 bool operator==(const EventListener& other) const final |
| 28 { | 29 { |
| 29 return this == &other; | 30 return this == &other; |
| 30 } | 31 } |
| 31 | 32 |
| 32 MOCK_METHOD2(handleEvent, void(ExecutionContext* executionContext, Event*)); | 33 MOCK_METHOD2(handleEvent, void(ExecutionContext* executionContext, Event*)); |
| 33 }; | 34 }; |
| 34 | 35 |
| 36 class MockWebPresentationClient : public WebPresentationClient { |
| 37 public: |
| 38 MOCK_METHOD1(setController, |
| 39 void(WebPresentationController*)); |
| 40 |
| 41 MOCK_METHOD1(setReceiver, |
| 42 void(WebPresentationReceiver*)); |
| 43 |
| 44 MOCK_METHOD2(startSession, |
| 45 void(const WebVector<WebURL>& presentationUrls, WebPresentationConnectio
nClientCallbacks*)); |
| 46 |
| 47 MOCK_METHOD3(joinSession, |
| 48 void(const WebVector<WebURL>& presentationUrls, const WebString& present
ationId, WebPresentationConnectionClientCallbacks*)); |
| 49 |
| 50 MOCK_METHOD3(sendString, |
| 51 void(const WebURL& presentationUrl, const WebString& presentationId, con
st WebString& message)); |
| 52 |
| 53 MOCK_METHOD4(sendArrayBuffer, |
| 54 void(const WebURL& presentationUrl, const WebString& presentationId, con
st uint8_t* data, size_t length)); |
| 55 |
| 56 MOCK_METHOD4(sendBlobData, |
| 57 void(const WebURL& presentationUrl, const WebString& presentationId, con
st uint8_t* data, size_t length)); |
| 58 |
| 59 MOCK_METHOD2(closeSession, |
| 60 void(const WebURL& presentationUrl, const WebString& presentationId)); |
| 61 |
| 62 MOCK_METHOD2(terminateSession, |
| 63 void(const WebURL& presentationUrl, const WebString& presentationId)); |
| 64 |
| 65 MOCK_METHOD2(getAvailability, |
| 66 void(const WebURL& availabilityUrl, WebPresentationAvailabilityCallbacks
*)); |
| 67 |
| 68 MOCK_METHOD1(startListening, |
| 69 void(WebPresentationAvailabilityObserver*)); |
| 70 |
| 71 MOCK_METHOD1(stopListening, |
| 72 void(WebPresentationAvailabilityObserver*)); |
| 73 |
| 74 MOCK_METHOD1(setDefaultPresentationUrls, |
| 75 void(const WebVector<WebURL>&)); |
| 76 }; |
| 77 |
| 35 class TestWebPresentationConnectionClient : public WebPresentationConnectionClie
nt { | 78 class TestWebPresentationConnectionClient : public WebPresentationConnectionClie
nt { |
| 36 public: | 79 public: |
| 37 WebString getId() override { return WebString::fromUTF8("id"); } | 80 WebString getId() override { return WebString::fromUTF8("id"); } |
| 38 WebURL getUrl() override { return URLTestHelpers::toKURL("http://www.example
.com"); } | 81 WebURL getUrl() override { return URLTestHelpers::toKURL("http://www.example
.com"); } |
| 39 }; | 82 }; |
| 40 | 83 |
| 41 class PresentationReceiverTest : public ::testing::Test { | 84 class PresentationReceiverTest : public ::testing::Test { |
| 42 public: | 85 public: |
| 43 void addConnectionavailableEventListener( | 86 void addConnectionavailableEventListener( |
| 44 EventListener*, const PresentationReceiver*); | 87 EventListener*, const PresentationReceiver*); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 67 const PresentationReceiver* receiver) | 110 const PresentationReceiver* receiver) |
| 68 { | 111 { |
| 69 EXPECT_EQ(expectedSize, receiver->m_connectionList->m_connections.size()); | 112 EXPECT_EQ(expectedSize, receiver->m_connectionList->m_connections.size()); |
| 70 } | 113 } |
| 71 | 114 |
| 72 using ::testing::StrictMock; | 115 using ::testing::StrictMock; |
| 73 | 116 |
| 74 TEST_F(PresentationReceiverTest, NoConnectionUnresolvedConnectionList) | 117 TEST_F(PresentationReceiverTest, NoConnectionUnresolvedConnectionList) |
| 75 { | 118 { |
| 76 V8TestingScope scope; | 119 V8TestingScope scope; |
| 77 auto receiver = new PresentationReceiver(&scope.frame()); | 120 auto receiver = new PresentationReceiver(&scope.frame(), nullptr); |
| 78 | 121 |
| 79 auto eventHandler = new StrictMock<MockEventListener>(); | 122 auto eventHandler = new StrictMock<MockEventListener>(); |
| 80 addConnectionavailableEventListener(eventHandler, receiver); | 123 addConnectionavailableEventListener(eventHandler, receiver); |
| 81 EXPECT_CALL(*eventHandler, handleEvent(testing::_, testing::_)).Times(0); | 124 EXPECT_CALL(*eventHandler, handleEvent(testing::_, testing::_)).Times(0); |
| 82 | 125 |
| 83 receiver->connectionList(scope.getScriptState()); | 126 receiver->connectionList(scope.getScriptState()); |
| 84 | 127 |
| 85 verifyConnectionListPropertyState(ScriptPromisePropertyBase::Pending, receiv
er); | 128 verifyConnectionListPropertyState(ScriptPromisePropertyBase::Pending, receiv
er); |
| 86 verifyConnectionListSize(0, receiver); | 129 verifyConnectionListSize(0, receiver); |
| 87 } | 130 } |
| 88 | 131 |
| 89 TEST_F(PresentationReceiverTest, OneConnectionResolvedConnectionListNoEvent) | 132 TEST_F(PresentationReceiverTest, OneConnectionResolvedConnectionListNoEvent) |
| 90 { | 133 { |
| 91 V8TestingScope scope; | 134 V8TestingScope scope; |
| 92 auto receiver = new PresentationReceiver(&scope.frame()); | 135 auto receiver = new PresentationReceiver(&scope.frame(), nullptr); |
| 93 | 136 |
| 94 auto eventHandler = new StrictMock<MockEventListener>(); | 137 auto eventHandler = new StrictMock<MockEventListener>(); |
| 95 addConnectionavailableEventListener(eventHandler, receiver); | 138 addConnectionavailableEventListener(eventHandler, receiver); |
| 96 EXPECT_CALL(*eventHandler, handleEvent(testing::_, testing::_)).Times(0); | 139 EXPECT_CALL(*eventHandler, handleEvent(testing::_, testing::_)).Times(0); |
| 97 | 140 |
| 98 receiver->connectionList(scope.getScriptState()); | 141 receiver->connectionList(scope.getScriptState()); |
| 99 | 142 |
| 100 // Receive first connection. | 143 // Receive first connection. |
| 101 auto connectionClient = new TestWebPresentationConnectionClient(); | 144 auto connectionClient = new TestWebPresentationConnectionClient(); |
| 102 receiver->onConnectionReceived(connectionClient); | 145 receiver->onReceiverConnectionAvailable(connectionClient); |
| 103 | 146 |
| 104 verifyConnectionListPropertyState(ScriptPromisePropertyBase::Resolved, recei
ver); | 147 verifyConnectionListPropertyState(ScriptPromisePropertyBase::Resolved, recei
ver); |
| 105 verifyConnectionListSize(1, receiver); | 148 verifyConnectionListSize(1, receiver); |
| 106 } | 149 } |
| 107 | 150 |
| 108 TEST_F(PresentationReceiverTest, TwoConnectionsFireOnconnectionavailableEvent) | 151 TEST_F(PresentationReceiverTest, TwoConnectionsFireOnconnectionavailableEvent) |
| 109 { | 152 { |
| 110 V8TestingScope scope; | 153 V8TestingScope scope; |
| 111 auto receiver = new PresentationReceiver(&scope.frame()); | 154 auto receiver = new PresentationReceiver(&scope.frame(), nullptr); |
| 112 | 155 |
| 113 StrictMock<MockEventListener>* eventHandler = new StrictMock<MockEventListen
er>(); | 156 StrictMock<MockEventListener>* eventHandler = new StrictMock<MockEventListen
er>(); |
| 114 addConnectionavailableEventListener(eventHandler, receiver); | 157 addConnectionavailableEventListener(eventHandler, receiver); |
| 115 EXPECT_CALL(*eventHandler, handleEvent(testing::_, testing::_)).Times(1); | 158 EXPECT_CALL(*eventHandler, handleEvent(testing::_, testing::_)).Times(1); |
| 116 | 159 |
| 117 receiver->connectionList(scope.getScriptState()); | 160 receiver->connectionList(scope.getScriptState()); |
| 118 // Receive first connection. | 161 // Receive first connection. |
| 119 auto connectionClient1 = new TestWebPresentationConnectionClient(); | 162 auto connectionClient1 = new TestWebPresentationConnectionClient(); |
| 120 receiver->onConnectionReceived(connectionClient1); | 163 receiver->onReceiverConnectionAvailable(connectionClient1); |
| 121 | 164 |
| 122 // Receive second connection. | 165 // Receive second connection. |
| 123 auto connectionClient2 = new TestWebPresentationConnectionClient(); | 166 auto connectionClient2 = new TestWebPresentationConnectionClient(); |
| 124 receiver->onConnectionReceived(connectionClient2); | 167 receiver->onReceiverConnectionAvailable(connectionClient2); |
| 125 | 168 |
| 126 verifyConnectionListSize(2, receiver); | 169 verifyConnectionListSize(2, receiver); |
| 127 } | 170 } |
| 128 | 171 |
| 129 TEST_F(PresentationReceiverTest, TwoConnectionsNoEvent) | 172 TEST_F(PresentationReceiverTest, TwoConnectionsNoEvent) |
| 130 { | 173 { |
| 131 V8TestingScope scope; | 174 V8TestingScope scope; |
| 132 auto receiver = new PresentationReceiver(&scope.frame()); | 175 auto receiver = new PresentationReceiver(&scope.frame(), nullptr); |
| 133 | 176 |
| 134 StrictMock<MockEventListener>* eventHandler = new StrictMock<MockEventListen
er>(); | 177 StrictMock<MockEventListener>* eventHandler = new StrictMock<MockEventListen
er>(); |
| 135 addConnectionavailableEventListener(eventHandler, receiver); | 178 addConnectionavailableEventListener(eventHandler, receiver); |
| 136 EXPECT_CALL(*eventHandler, handleEvent(testing::_, testing::_)).Times(0); | 179 EXPECT_CALL(*eventHandler, handleEvent(testing::_, testing::_)).Times(0); |
| 137 | 180 |
| 138 // Receive first connection. | 181 // Receive first connection. |
| 139 auto connectionClient1 = new TestWebPresentationConnectionClient(); | 182 auto connectionClient1 = new TestWebPresentationConnectionClient(); |
| 140 receiver->onConnectionReceived(connectionClient1); | 183 receiver->onReceiverConnectionAvailable(connectionClient1); |
| 141 | 184 |
| 142 // Receive second connection. | 185 // Receive second connection. |
| 143 auto connectionClient2 = new TestWebPresentationConnectionClient(); | 186 auto connectionClient2 = new TestWebPresentationConnectionClient(); |
| 144 receiver->onConnectionReceived(connectionClient2); | 187 receiver->onReceiverConnectionAvailable(connectionClient2); |
| 145 | 188 |
| 146 receiver->connectionList(scope.getScriptState()); | 189 receiver->connectionList(scope.getScriptState()); |
| 147 verifyConnectionListPropertyState(ScriptPromisePropertyBase::Resolved, recei
ver); | 190 verifyConnectionListPropertyState(ScriptPromisePropertyBase::Resolved, recei
ver); |
| 148 verifyConnectionListSize(2, receiver); | 191 verifyConnectionListSize(2, receiver); |
| 149 } | 192 } |
| 150 | 193 |
| 194 TEST_F(PresentationReceiverTest, CreateReceiver) |
| 195 { |
| 196 MockWebPresentationClient client; |
| 197 EXPECT_CALL(client, setReceiver(testing::_)); |
| 198 |
| 199 V8TestingScope scope; |
| 200 new PresentationReceiver(&scope.frame(), &client); |
| 201 } |
| 202 |
| 151 } // namespace blink | 203 } // namespace blink |
| OLD | NEW |