[safe-browsing] Database full hash matches like prefix match.
[chromium-blink-merge.git] / content / shell / renderer / test_runner / MockWebRTCPeerConnectionHandler.cpp
blobcfdbe5419d748b9ea0e70adbeb2670548e155c7d
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> {
28 public:
29 RTCSessionDescriptionRequestSuccededTask(MockWebRTCPeerConnectionHandler* object, const WebRTCSessionDescriptionRequest& request, const WebRTCSessionDescription& result)
30 : WebMethodTask<MockWebRTCPeerConnectionHandler>(object)
31 , m_request(request)
32 , m_result(result)
36 virtual void runIfValid() OVERRIDE
38 m_request.requestSucceeded(m_result);
41 private:
42 WebRTCSessionDescriptionRequest m_request;
43 WebRTCSessionDescription m_result;
46 class RTCSessionDescriptionRequestFailedTask : public WebMethodTask<MockWebRTCPeerConnectionHandler> {
47 public:
48 RTCSessionDescriptionRequestFailedTask(MockWebRTCPeerConnectionHandler* object, const WebRTCSessionDescriptionRequest& request)
49 : WebMethodTask<MockWebRTCPeerConnectionHandler>(object)
50 , m_request(request)
54 virtual void runIfValid() OVERRIDE
56 m_request.requestFailed("TEST_ERROR");
59 private:
60 WebRTCSessionDescriptionRequest m_request;
63 class RTCStatsRequestSucceededTask : public WebMethodTask<MockWebRTCPeerConnectionHandler> {
64 public:
65 RTCStatsRequestSucceededTask(MockWebRTCPeerConnectionHandler* object, const blink::WebRTCStatsRequest& request, const blink::WebRTCStatsResponse& response)
66 : WebMethodTask<MockWebRTCPeerConnectionHandler>(object)
67 , m_request(request)
68 , m_response(response)
72 virtual void runIfValid() OVERRIDE
74 m_request.requestSucceeded(m_response);
77 private:
78 blink::WebRTCStatsRequest m_request;
79 blink::WebRTCStatsResponse m_response;
82 class RTCVoidRequestTask : public WebMethodTask<MockWebRTCPeerConnectionHandler> {
83 public:
84 RTCVoidRequestTask(MockWebRTCPeerConnectionHandler* object, const WebRTCVoidRequest& request, bool succeeded)
85 : WebMethodTask<MockWebRTCPeerConnectionHandler>(object)
86 , m_request(request)
87 , m_succeeded(succeeded)
91 virtual void runIfValid() OVERRIDE
93 if (m_succeeded)
94 m_request.requestSucceeded();
95 else
96 m_request.requestFailed("TEST_ERROR");
99 private:
100 WebRTCVoidRequest m_request;
101 bool m_succeeded;
104 class RTCPeerConnectionStateTask : public WebMethodTask<MockWebRTCPeerConnectionHandler> {
105 public:
106 RTCPeerConnectionStateTask(MockWebRTCPeerConnectionHandler* object, WebRTCPeerConnectionHandlerClient* client, WebRTCPeerConnectionHandlerClient::ICEConnectionState connectionState, WebRTCPeerConnectionHandlerClient::ICEGatheringState gatheringState)
107 : WebMethodTask<MockWebRTCPeerConnectionHandler>(object)
108 , m_client(client)
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);
120 private:
121 WebRTCPeerConnectionHandlerClient* m_client;
122 WebRTCPeerConnectionHandlerClient::ICEConnectionState m_connectionState;
123 WebRTCPeerConnectionHandlerClient::ICEGatheringState m_gatheringState;
126 class RemoteDataChannelTask : public WebMethodTask<MockWebRTCPeerConnectionHandler> {
127 public:
128 RemoteDataChannelTask(MockWebRTCPeerConnectionHandler* object, WebRTCPeerConnectionHandlerClient* client, WebTestDelegate* delegate)
129 : WebMethodTask<MockWebRTCPeerConnectionHandler>(object)
130 , m_client(client)
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);
142 private:
143 WebRTCPeerConnectionHandlerClient* m_client;
144 WebTestDelegate* m_delegate;
147 /////////////////////
149 MockWebRTCPeerConnectionHandler::MockWebRTCPeerConnectionHandler()
153 MockWebRTCPeerConnectionHandler::MockWebRTCPeerConnectionHandler(WebRTCPeerConnectionHandlerClient* client, TestInterfaces* interfaces)
154 : m_client(client)
155 , m_stopped(false)
156 , m_streamCount(0)
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));
165 return true;
168 return false;
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));
178 } else
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));
188 } else
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));
197 } else
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));
206 } else
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&)
222 return true;
225 bool MockWebRTCPeerConnectionHandler::addICECandidate(const WebRTCICECandidate& iceCandidate)
227 m_client->didGenerateICECandidate(iceCandidate);
228 return true;
231 bool MockWebRTCPeerConnectionHandler::addICECandidate(const WebRTCVoidRequest& request, const WebRTCICECandidate& iceCandidate)
233 m_interfaces->delegate()->postTask(new RTCVoidRequestTask(this, request, true));
234 return true;
237 bool MockWebRTCPeerConnectionHandler::addStream(const WebMediaStream& stream, const WebMediaConstraints&)
239 ++m_streamCount;
240 m_client->negotiationNeeded();
241 return true;
244 void MockWebRTCPeerConnectionHandler::removeStream(const WebMediaStream& stream)
246 --m_streamCount;
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");
258 } else {
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()
283 m_stopped = true;