1 // Copyright 2013 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.
5 #include "content/shell/renderer/test_runner/MockWebRTCPeerConnectionHandler.h"
7 #include "content/shell/renderer/test_runner/MockConstraints.h"
8 #include "content/shell/renderer/test_runner/MockWebRTCDTMFSenderHandler.h"
9 #include "content/shell/renderer/test_runner/MockWebRTCDataChannelHandler.h"
10 #include "content/shell/renderer/test_runner/TestInterfaces.h"
11 #include "content/shell/renderer/test_runner/WebTestDelegate.h"
12 #include "third_party/WebKit/public/platform/WebMediaConstraints.h"
13 #include "third_party/WebKit/public/platform/WebMediaStream.h"
14 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
15 #include "third_party/WebKit/public/platform/WebRTCDataChannelInit.h"
16 #include "third_party/WebKit/public/platform/WebRTCPeerConnectionHandlerClient.h"
17 #include "third_party/WebKit/public/platform/WebRTCStatsResponse.h"
18 #include "third_party/WebKit/public/platform/WebRTCVoidRequest.h"
19 #include "third_party/WebKit/public/platform/WebString.h"
20 #include "third_party/WebKit/public/platform/WebVector.h"
22 using namespace blink
;
23 using namespace content
;
25 namespace WebTestRunner
{
27 class RTCSessionDescriptionRequestSuccededTask
: public WebMethodTask
<MockWebRTCPeerConnectionHandler
> {
29 RTCSessionDescriptionRequestSuccededTask(MockWebRTCPeerConnectionHandler
* object
, const WebRTCSessionDescriptionRequest
& request
, const WebRTCSessionDescription
& result
)
30 : WebMethodTask
<MockWebRTCPeerConnectionHandler
>(object
)
36 virtual void runIfValid() OVERRIDE
38 m_request
.requestSucceeded(m_result
);
42 WebRTCSessionDescriptionRequest m_request
;
43 WebRTCSessionDescription m_result
;
46 class RTCSessionDescriptionRequestFailedTask
: public WebMethodTask
<MockWebRTCPeerConnectionHandler
> {
48 RTCSessionDescriptionRequestFailedTask(MockWebRTCPeerConnectionHandler
* object
, const WebRTCSessionDescriptionRequest
& request
)
49 : WebMethodTask
<MockWebRTCPeerConnectionHandler
>(object
)
54 virtual void runIfValid() OVERRIDE
56 m_request
.requestFailed("TEST_ERROR");
60 WebRTCSessionDescriptionRequest m_request
;
63 class RTCStatsRequestSucceededTask
: public WebMethodTask
<MockWebRTCPeerConnectionHandler
> {
65 RTCStatsRequestSucceededTask(MockWebRTCPeerConnectionHandler
* object
, const blink::WebRTCStatsRequest
& request
, const blink::WebRTCStatsResponse
& response
)
66 : WebMethodTask
<MockWebRTCPeerConnectionHandler
>(object
)
68 , m_response(response
)
72 virtual void runIfValid() OVERRIDE
74 m_request
.requestSucceeded(m_response
);
78 blink::WebRTCStatsRequest m_request
;
79 blink::WebRTCStatsResponse m_response
;
82 class RTCVoidRequestTask
: public WebMethodTask
<MockWebRTCPeerConnectionHandler
> {
84 RTCVoidRequestTask(MockWebRTCPeerConnectionHandler
* object
, const WebRTCVoidRequest
& request
, bool succeeded
)
85 : WebMethodTask
<MockWebRTCPeerConnectionHandler
>(object
)
87 , m_succeeded(succeeded
)
91 virtual void runIfValid() OVERRIDE
94 m_request
.requestSucceeded();
96 m_request
.requestFailed("TEST_ERROR");
100 WebRTCVoidRequest m_request
;
104 class RTCPeerConnectionStateTask
: public WebMethodTask
<MockWebRTCPeerConnectionHandler
> {
106 RTCPeerConnectionStateTask(MockWebRTCPeerConnectionHandler
* object
, WebRTCPeerConnectionHandlerClient
* client
, WebRTCPeerConnectionHandlerClient::ICEConnectionState connectionState
, WebRTCPeerConnectionHandlerClient::ICEGatheringState gatheringState
)
107 : WebMethodTask
<MockWebRTCPeerConnectionHandler
>(object
)
109 , m_connectionState(connectionState
)
110 , m_gatheringState(gatheringState
)
114 virtual void runIfValid() OVERRIDE
116 m_client
->didChangeICEGatheringState(m_gatheringState
);
117 m_client
->didChangeICEConnectionState(m_connectionState
);
121 WebRTCPeerConnectionHandlerClient
* m_client
;
122 WebRTCPeerConnectionHandlerClient::ICEConnectionState m_connectionState
;
123 WebRTCPeerConnectionHandlerClient::ICEGatheringState m_gatheringState
;
126 class RemoteDataChannelTask
: public WebMethodTask
<MockWebRTCPeerConnectionHandler
> {
128 RemoteDataChannelTask(MockWebRTCPeerConnectionHandler
* object
, WebRTCPeerConnectionHandlerClient
* client
, WebTestDelegate
* delegate
)
129 : WebMethodTask
<MockWebRTCPeerConnectionHandler
>(object
)
131 , m_delegate(delegate
)
135 virtual void runIfValid() OVERRIDE
137 WebRTCDataChannelInit init
;
138 WebRTCDataChannelHandler
* remoteDataChannel
= new MockWebRTCDataChannelHandler("MockRemoteDataChannel", init
, m_delegate
);
139 m_client
->didAddRemoteDataChannel(remoteDataChannel
);
143 WebRTCPeerConnectionHandlerClient
* m_client
;
144 WebTestDelegate
* m_delegate
;
147 /////////////////////
149 MockWebRTCPeerConnectionHandler::MockWebRTCPeerConnectionHandler()
153 MockWebRTCPeerConnectionHandler::MockWebRTCPeerConnectionHandler(WebRTCPeerConnectionHandlerClient
* client
, TestInterfaces
* interfaces
)
157 , m_interfaces(interfaces
)
161 bool MockWebRTCPeerConnectionHandler::initialize(const WebRTCConfiguration
&, const WebMediaConstraints
& constraints
)
163 if (MockConstraints::verifyConstraints(constraints
)) {
164 m_interfaces
->delegate()->postTask(new RTCPeerConnectionStateTask(this, m_client
, WebRTCPeerConnectionHandlerClient::ICEConnectionStateCompleted
, WebRTCPeerConnectionHandlerClient::ICEGatheringStateComplete
));
171 void MockWebRTCPeerConnectionHandler::createOffer(const WebRTCSessionDescriptionRequest
& request
, const WebMediaConstraints
& constraints
)
173 WebString shouldSucceed
;
174 if (constraints
.getMandatoryConstraintValue("succeed", shouldSucceed
) && shouldSucceed
== "true") {
175 WebRTCSessionDescription sessionDescription
;
176 sessionDescription
.initialize("offer", "local");
177 m_interfaces
->delegate()->postTask(new RTCSessionDescriptionRequestSuccededTask(this, request
, sessionDescription
));
179 m_interfaces
->delegate()->postTask(new RTCSessionDescriptionRequestFailedTask(this, request
));
182 void MockWebRTCPeerConnectionHandler::createAnswer(const WebRTCSessionDescriptionRequest
& request
, const WebMediaConstraints
&)
184 if (!m_remoteDescription
.isNull()) {
185 WebRTCSessionDescription sessionDescription
;
186 sessionDescription
.initialize("answer", "local");
187 m_interfaces
->delegate()->postTask(new RTCSessionDescriptionRequestSuccededTask(this, request
, sessionDescription
));
189 m_interfaces
->delegate()->postTask(new RTCSessionDescriptionRequestFailedTask(this, request
));
192 void MockWebRTCPeerConnectionHandler::setLocalDescription(const WebRTCVoidRequest
& request
, const WebRTCSessionDescription
& localDescription
)
194 if (!localDescription
.isNull() && localDescription
.sdp() == "local") {
195 m_localDescription
= localDescription
;
196 m_interfaces
->delegate()->postTask(new RTCVoidRequestTask(this, request
, true));
198 m_interfaces
->delegate()->postTask(new RTCVoidRequestTask(this, request
, false));
201 void MockWebRTCPeerConnectionHandler::setRemoteDescription(const WebRTCVoidRequest
& request
, const WebRTCSessionDescription
& remoteDescription
)
203 if (!remoteDescription
.isNull() && remoteDescription
.sdp() == "remote") {
204 m_remoteDescription
= remoteDescription
;
205 m_interfaces
->delegate()->postTask(new RTCVoidRequestTask(this, request
, true));
207 m_interfaces
->delegate()->postTask(new RTCVoidRequestTask(this, request
, false));
210 WebRTCSessionDescription
MockWebRTCPeerConnectionHandler::localDescription()
212 return m_localDescription
;
215 WebRTCSessionDescription
MockWebRTCPeerConnectionHandler::remoteDescription()
217 return m_remoteDescription
;
220 bool MockWebRTCPeerConnectionHandler::updateICE(const WebRTCConfiguration
&, const WebMediaConstraints
&)
225 bool MockWebRTCPeerConnectionHandler::addICECandidate(const WebRTCICECandidate
& iceCandidate
)
227 m_client
->didGenerateICECandidate(iceCandidate
);
231 bool MockWebRTCPeerConnectionHandler::addICECandidate(const WebRTCVoidRequest
& request
, const WebRTCICECandidate
& iceCandidate
)
233 m_interfaces
->delegate()->postTask(new RTCVoidRequestTask(this, request
, true));
237 bool MockWebRTCPeerConnectionHandler::addStream(const WebMediaStream
& stream
, const WebMediaConstraints
&)
240 m_client
->negotiationNeeded();
244 void MockWebRTCPeerConnectionHandler::removeStream(const WebMediaStream
& stream
)
247 m_client
->negotiationNeeded();
250 void MockWebRTCPeerConnectionHandler::getStats(const WebRTCStatsRequest
& request
)
252 WebRTCStatsResponse response
= request
.createResponse();
253 double currentDate
= m_interfaces
->delegate()->getCurrentTimeInMillisecond();
254 if (request
.hasSelector()) {
255 // FIXME: There is no check that the fetched values are valid.
256 size_t reportIndex
= response
.addReport("Mock video", "ssrc", currentDate
);
257 response
.addStatistic(reportIndex
, "type", "video");
259 for (int i
= 0; i
< m_streamCount
; ++i
) {
260 size_t reportIndex
= response
.addReport("Mock audio", "ssrc", currentDate
);
261 response
.addStatistic(reportIndex
, "type", "audio");
262 reportIndex
= response
.addReport("Mock video", "ssrc", currentDate
);
263 response
.addStatistic(reportIndex
, "type", "video");
266 m_interfaces
->delegate()->postTask(new RTCStatsRequestSucceededTask(this, request
, response
));
269 WebRTCDataChannelHandler
* MockWebRTCPeerConnectionHandler::createDataChannel(const WebString
& label
, const blink::WebRTCDataChannelInit
& init
)
271 m_interfaces
->delegate()->postTask(new RemoteDataChannelTask(this, m_client
, m_interfaces
->delegate()));
273 return new MockWebRTCDataChannelHandler(label
, init
, m_interfaces
->delegate());
276 WebRTCDTMFSenderHandler
* MockWebRTCPeerConnectionHandler::createDTMFSender(const WebMediaStreamTrack
& track
)
278 return new MockWebRTCDTMFSenderHandler(track
, m_interfaces
->delegate());
281 void MockWebRTCPeerConnectionHandler::stop()