Fix build break
[chromium-blink-merge.git] / content / renderer / media / rtc_peer_connection_handler_unittest.cc
blobe3e66df64f4b8f9a9746d6dd59ef92b5f667d298
1 // Copyright (c) 2012 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 <string>
7 #include "base/memory/scoped_ptr.h"
8 #include "base/utf_string_conversions.h"
9 #include "base/values.h"
10 #include "content/renderer/media/media_stream_extra_data.h"
11 #include "content/renderer/media/mock_media_stream_dependency_factory.h"
12 #include "content/renderer/media/mock_peer_connection_impl.h"
13 #include "content/renderer/media/mock_web_rtc_peer_connection_handler_client.h"
14 #include "content/renderer/media/peer_connection_tracker.h"
15 #include "content/renderer/media/rtc_media_constraints.h"
16 #include "content/renderer/media/rtc_peer_connection_handler.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "third_party/WebKit/Source/Platform/chromium/public/WebMediaConstraints.h"
20 #include "third_party/WebKit/Source/Platform/chromium/public/WebMediaStream.h"
21 #include "third_party/WebKit/Source/Platform/chromium/public/WebMediaStreamSource.h"
22 #include "third_party/WebKit/Source/Platform/chromium/public/WebMediaStreamTrack.h"
23 #include "third_party/WebKit/Source/Platform/chromium/public/WebRTCConfiguration.h"
24 #include "third_party/WebKit/Source/Platform/chromium/public/WebRTCDTMFSenderHandler.h"
25 #include "third_party/WebKit/Source/Platform/chromium/public/WebRTCDataChannelHandler.h"
26 #include "third_party/WebKit/Source/Platform/chromium/public/WebRTCICECandidate.h"
27 #include "third_party/WebKit/Source/Platform/chromium/public/WebRTCPeerConnectionHandlerClient.h"
28 #include "third_party/WebKit/Source/Platform/chromium/public/WebRTCSessionDescription.h"
29 #include "third_party/WebKit/Source/Platform/chromium/public/WebRTCSessionDescriptionRequest.h"
30 #include "third_party/WebKit/Source/Platform/chromium/public/WebRTCStatsRequest.h"
31 #include "third_party/WebKit/Source/Platform/chromium/public/WebRTCVoidRequest.h"
32 #include "third_party/WebKit/Source/Platform/chromium/public/WebURL.h"
33 #include "third_party/libjingle/source/talk/app/webrtc/peerconnectioninterface.h"
35 static const char kDummySdp[] = "dummy sdp";
36 static const char kDummySdpType[] = "dummy type";
38 using WebKit::WebRTCPeerConnectionHandlerClient;
39 using testing::NiceMock;
40 using testing::_;
42 namespace content {
44 class MockRTCStatsResponse : public LocalRTCStatsResponse {
45 public:
46 MockRTCStatsResponse()
47 : report_count_(0),
48 statistic_count_(0) {
51 virtual size_t addReport(WebKit::WebString type,
52 WebKit::WebString id,
53 double timestamp) OVERRIDE {
54 ++report_count_;
55 return report_count_;
58 virtual void addStatistic(size_t report,
59 WebKit::WebString name, WebKit::WebString value)
60 OVERRIDE {
61 ++statistic_count_;
63 int report_count() const { return report_count_; }
65 private:
66 int report_count_;
67 int statistic_count_;
70 // Mocked wrapper for WebKit::WebRTCStatsRequest
71 class MockRTCStatsRequest : public LocalRTCStatsRequest {
72 public:
73 MockRTCStatsRequest()
74 : has_selector_(false),
75 stream_(),
76 request_succeeded_called_(false) {}
78 virtual bool hasSelector() const OVERRIDE {
79 return has_selector_;
81 virtual WebKit::WebMediaStream stream() const OVERRIDE {
82 return stream_;
84 virtual WebKit::WebMediaStreamTrack component() const OVERRIDE {
85 return component_;
87 virtual scoped_refptr<LocalRTCStatsResponse> createResponse() OVERRIDE {
88 DCHECK(!response_);
89 response_ = new talk_base::RefCountedObject<MockRTCStatsResponse>();
90 return response_;
93 virtual void requestSucceeded(const LocalRTCStatsResponse* response)
94 OVERRIDE {
95 EXPECT_EQ(response, response_.get());
96 request_succeeded_called_ = true;
99 // Function for setting whether or not a selector is available.
100 void setSelector(const WebKit::WebMediaStream& stream,
101 const WebKit::WebMediaStreamTrack& component) {
102 has_selector_ = true;
103 stream_ = stream;
104 component_ = component;
107 // Function for inspecting the result of a stats request.
108 MockRTCStatsResponse* result() {
109 if (request_succeeded_called_) {
110 return response_.get();
111 } else {
112 return NULL;
116 private:
117 bool has_selector_;
118 WebKit::WebMediaStream stream_;
119 WebKit::WebMediaStreamTrack component_;
120 scoped_refptr<MockRTCStatsResponse> response_;
121 bool request_succeeded_called_;
124 class MockPeerConnectionTracker : public PeerConnectionTracker {
125 public:
126 MOCK_METHOD1(UnregisterPeerConnection,
127 void(RTCPeerConnectionHandler* pc_handler));
128 // TODO (jiayl): add coverage for the following methods
129 MOCK_METHOD2(TrackCreateOffer,
130 void(RTCPeerConnectionHandler* pc_handler,
131 const RTCMediaConstraints& constraints));
132 MOCK_METHOD2(TrackCreateAnswer,
133 void(RTCPeerConnectionHandler* pc_handler,
134 const RTCMediaConstraints& constraints));
135 MOCK_METHOD3(TrackSetSessionDescription,
136 void(RTCPeerConnectionHandler* pc_handler,
137 const webrtc::SessionDescriptionInterface* desc,
138 Source source));
139 MOCK_METHOD3(
140 TrackUpdateIce,
141 void(RTCPeerConnectionHandler* pc_handler,
142 const std::vector<
143 webrtc::PeerConnectionInterface::IceServer>& servers,
144 const RTCMediaConstraints& options));
145 MOCK_METHOD3(TrackAddIceCandidate,
146 void(RTCPeerConnectionHandler* pc_handler,
147 const WebKit::WebRTCICECandidate& candidate,
148 Source source));
149 MOCK_METHOD3(TrackAddStream,
150 void(RTCPeerConnectionHandler* pc_handler,
151 const WebKit::WebMediaStream& stream,
152 Source source));
153 MOCK_METHOD3(TrackRemoveStream,
154 void(RTCPeerConnectionHandler* pc_handler,
155 const WebKit::WebMediaStream& stream,
156 Source source));
157 MOCK_METHOD1(TrackOnIceComplete,
158 void(RTCPeerConnectionHandler* pc_handler));
159 MOCK_METHOD3(TrackCreateDataChannel,
160 void(RTCPeerConnectionHandler* pc_handler,
161 const webrtc::DataChannelInterface* data_channel,
162 Source source));
163 MOCK_METHOD1(TrackStop, void(RTCPeerConnectionHandler* pc_handler));
164 MOCK_METHOD2(TrackSignalingStateChange,
165 void(RTCPeerConnectionHandler* pc_handler,
166 WebRTCPeerConnectionHandlerClient::SignalingState state));
167 MOCK_METHOD2(
168 TrackIceConnectionStateChange,
169 void(RTCPeerConnectionHandler* pc_handler,
170 WebRTCPeerConnectionHandlerClient::ICEConnectionState state));
171 MOCK_METHOD2(
172 TrackIceGatheringStateChange,
173 void(RTCPeerConnectionHandler* pc_handler,
174 WebRTCPeerConnectionHandlerClient::ICEGatheringState state));
175 MOCK_METHOD1(TrackOnRenegotiationNeeded,
176 void(RTCPeerConnectionHandler* pc_handler));
177 MOCK_METHOD2(TrackCreateDTMFSender,
178 void (RTCPeerConnectionHandler* pc_handler,
179 const WebKit::WebMediaStreamTrack& track));
182 class RTCPeerConnectionHandlerUnderTest : public RTCPeerConnectionHandler {
183 public:
184 RTCPeerConnectionHandlerUnderTest(
185 WebRTCPeerConnectionHandlerClient* client,
186 MediaStreamDependencyFactory* dependency_factory)
187 : RTCPeerConnectionHandler(client, dependency_factory) {
190 MockPeerConnectionImpl* native_peer_connection() {
191 return static_cast<MockPeerConnectionImpl*>(native_peer_connection_.get());
195 class RTCPeerConnectionHandlerTest : public ::testing::Test {
196 public:
197 RTCPeerConnectionHandlerTest() : mock_peer_connection_(NULL) {
200 virtual void SetUp() {
201 mock_client_.reset(new NiceMock<MockWebRTCPeerConnectionHandlerClient>());
202 mock_dependency_factory_.reset(new MockMediaStreamDependencyFactory());
203 mock_dependency_factory_->EnsurePeerConnectionFactory();
204 pc_handler_.reset(
205 new RTCPeerConnectionHandlerUnderTest(mock_client_.get(),
206 mock_dependency_factory_.get()));
207 mock_tracker_.reset(new NiceMock<MockPeerConnectionTracker>());
208 WebKit::WebRTCConfiguration config;
209 WebKit::WebMediaConstraints constraints;
210 EXPECT_TRUE(pc_handler_->InitializeForTest(config, constraints,
211 mock_tracker_.get()));
213 mock_peer_connection_ = pc_handler_->native_peer_connection();
214 ASSERT_TRUE(mock_peer_connection_);
217 // Creates a WebKit local MediaStream.
218 WebKit::WebMediaStream CreateLocalMediaStream(
219 const std::string& stream_label) {
220 std::string video_track_label("video-label");
221 std::string audio_track_label("audio-label");
223 WebKit::WebVector<WebKit::WebMediaStreamSource> audio_sources(
224 static_cast<size_t>(1));
225 audio_sources[0].initialize(WebKit::WebString::fromUTF8(audio_track_label),
226 WebKit::WebMediaStreamSource::TypeAudio,
227 WebKit::WebString::fromUTF8("audio_track"));
228 WebKit::WebVector<WebKit::WebMediaStreamSource> video_sources(
229 static_cast<size_t>(1));
230 video_sources[0].initialize(WebKit::WebString::fromUTF8(video_track_label),
231 WebKit::WebMediaStreamSource::TypeVideo,
232 WebKit::WebString::fromUTF8("video_track"));
233 WebKit::WebMediaStream local_stream;
234 local_stream.initialize(UTF8ToUTF16(stream_label), audio_sources,
235 video_sources);
237 scoped_refptr<webrtc::MediaStreamInterface> native_stream(
238 mock_dependency_factory_->CreateLocalMediaStream(stream_label));
239 WebKit::WebVector<WebKit::WebMediaStreamTrack> audio_tracks;
240 local_stream.audioSources(audio_tracks);
241 const std::string audio_track_id = UTF16ToUTF8(audio_tracks[0].id());
242 scoped_refptr<webrtc::AudioTrackInterface> audio_track(
243 mock_dependency_factory_->CreateLocalAudioTrack(audio_track_id,
244 NULL));
245 native_stream->AddTrack(audio_track);
246 WebKit::WebVector<WebKit::WebMediaStreamTrack> video_tracks;
247 local_stream.audioSources(video_tracks);
248 const std::string video_track_id = UTF16ToUTF8(video_tracks[0].id());
249 scoped_refptr<webrtc::VideoTrackInterface> video_track(
250 mock_dependency_factory_->CreateLocalVideoTrack(
251 video_track_id, 0));
252 native_stream->AddTrack(video_track);
254 local_stream.setExtraData(new MediaStreamExtraData(native_stream, true));
255 return local_stream;
258 // Creates a remote MediaStream and adds it to the mocked native
259 // peer connection.
260 scoped_refptr<webrtc::MediaStreamInterface>
261 AddRemoteMockMediaStream(const std::string& stream_label,
262 const std::string& video_track_label,
263 const std::string& audio_track_label) {
264 scoped_refptr<webrtc::MediaStreamInterface> stream(
265 mock_dependency_factory_->CreateLocalMediaStream(stream_label));
266 if (!video_track_label.empty()) {
267 scoped_refptr<webrtc::VideoTrackInterface> video_track(
268 mock_dependency_factory_->CreateLocalVideoTrack(
269 video_track_label, 0));
270 stream->AddTrack(video_track);
272 if (!audio_track_label.empty()) {
273 scoped_refptr<webrtc::AudioTrackInterface> audio_track(
274 mock_dependency_factory_->CreateLocalAudioTrack(audio_track_label,
275 NULL));
276 stream->AddTrack(audio_track);
278 mock_peer_connection_->AddRemoteStream(stream);
279 return stream;
282 scoped_ptr<MockWebRTCPeerConnectionHandlerClient> mock_client_;
283 scoped_ptr<MockMediaStreamDependencyFactory> mock_dependency_factory_;
284 scoped_ptr<NiceMock<MockPeerConnectionTracker> > mock_tracker_;
285 scoped_ptr<RTCPeerConnectionHandlerUnderTest> pc_handler_;
287 // Weak reference to the mocked native peer connection implementation.
288 MockPeerConnectionImpl* mock_peer_connection_;
291 TEST_F(RTCPeerConnectionHandlerTest, Destruct) {
292 EXPECT_CALL(*mock_tracker_.get(), UnregisterPeerConnection(pc_handler_.get()))
293 .Times(1);
294 pc_handler_.reset(NULL);
297 TEST_F(RTCPeerConnectionHandlerTest, CreateOffer) {
298 WebKit::WebRTCSessionDescriptionRequest request;
299 WebKit::WebMediaConstraints options;
300 EXPECT_CALL(*mock_tracker_.get(), TrackCreateOffer(pc_handler_.get(), _));
302 // TODO(perkj): Can WebKit::WebRTCSessionDescriptionRequest be changed so
303 // the |reqest| requestSucceeded can be tested? Currently the |request| object
304 // can not be initialized from a unit test.
305 EXPECT_FALSE(mock_peer_connection_->created_session_description() != NULL);
306 pc_handler_->createOffer(request, options);
307 EXPECT_TRUE(mock_peer_connection_->created_session_description() != NULL);
310 TEST_F(RTCPeerConnectionHandlerTest, CreateAnswer) {
311 WebKit::WebRTCSessionDescriptionRequest request;
312 WebKit::WebMediaConstraints options;
313 EXPECT_CALL(*mock_tracker_.get(), TrackCreateAnswer(pc_handler_.get(), _));
314 // TODO(perkj): Can WebKit::WebRTCSessionDescriptionRequest be changed so
315 // the |reqest| requestSucceeded can be tested? Currently the |request| object
316 // can not be initialized from a unit test.
317 EXPECT_FALSE(mock_peer_connection_->created_session_description() != NULL);
318 pc_handler_->createAnswer(request, options);
319 EXPECT_TRUE(mock_peer_connection_->created_session_description() != NULL);
322 TEST_F(RTCPeerConnectionHandlerTest, setLocalDescription) {
323 // PeerConnectionTracker::TrackSetSessionDescription is expected to be called
324 // before |mock_peer_connection| is called.
325 testing::InSequence sequence;
326 EXPECT_CALL(*mock_tracker_.get(),
327 TrackSetSessionDescription(pc_handler_.get(), testing::NotNull(),
328 PeerConnectionTracker::SOURCE_LOCAL));
329 EXPECT_CALL(*mock_peer_connection_, SetLocalDescription(_, _));
331 WebKit::WebRTCVoidRequest request;
332 WebKit::WebRTCSessionDescription description;
333 description.initialize(kDummySdpType, kDummySdp);
334 pc_handler_->setLocalDescription(request, description);
335 EXPECT_EQ(description.type(), pc_handler_->localDescription().type());
336 EXPECT_EQ(description.sdp(), pc_handler_->localDescription().sdp());
338 std::string sdp_string;
339 ASSERT_TRUE(mock_peer_connection_->local_description() != NULL);
340 EXPECT_EQ(kDummySdpType, mock_peer_connection_->local_description()->type());
341 mock_peer_connection_->local_description()->ToString(&sdp_string);
342 EXPECT_EQ(kDummySdp, sdp_string);
345 TEST_F(RTCPeerConnectionHandlerTest, setRemoteDescription) {
346 // PeerConnectionTracker::TrackSetSessionDescription is expected to be called
347 // before |mock_peer_connection| is called.
348 testing::InSequence sequence;
349 EXPECT_CALL(*mock_tracker_.get(),
350 TrackSetSessionDescription(pc_handler_.get(), testing::NotNull(),
351 PeerConnectionTracker::SOURCE_REMOTE));
352 EXPECT_CALL(*mock_peer_connection_, SetRemoteDescription(_, _));
354 WebKit::WebRTCVoidRequest request;
355 WebKit::WebRTCSessionDescription description;
356 description.initialize(kDummySdpType, kDummySdp);
357 pc_handler_->setRemoteDescription(request, description);
358 EXPECT_EQ(description.type(), pc_handler_->remoteDescription().type());
359 EXPECT_EQ(description.sdp(), pc_handler_->remoteDescription().sdp());
361 std::string sdp_string;
362 ASSERT_TRUE(mock_peer_connection_->remote_description() != NULL);
363 EXPECT_EQ(kDummySdpType, mock_peer_connection_->remote_description()->type());
364 mock_peer_connection_->remote_description()->ToString(&sdp_string);
365 EXPECT_EQ(kDummySdp, sdp_string);
368 TEST_F(RTCPeerConnectionHandlerTest, updateICE) {
369 WebKit::WebRTCConfiguration config;
370 WebKit::WebMediaConstraints constraints;
372 EXPECT_CALL(*mock_tracker_.get(), TrackUpdateIce(pc_handler_.get(), _, _));
373 // TODO(perkj): Test that the parameters in |config| can be translated when a
374 // WebRTCConfiguration can be constructed. It's WebKit class and can't be
375 // initialized from a test.
376 EXPECT_TRUE(pc_handler_->updateICE(config, constraints));
379 TEST_F(RTCPeerConnectionHandlerTest, addICECandidate) {
380 WebKit::WebRTCICECandidate candidate;
381 candidate.initialize(kDummySdp, "mid", 1);
383 EXPECT_CALL(*mock_tracker_.get(),
384 TrackAddIceCandidate(pc_handler_.get(),
385 testing::Ref(candidate),
386 PeerConnectionTracker::SOURCE_REMOTE));
387 EXPECT_TRUE(pc_handler_->addICECandidate(candidate));
388 EXPECT_EQ(kDummySdp, mock_peer_connection_->ice_sdp());
389 EXPECT_EQ(1, mock_peer_connection_->sdp_mline_index());
390 EXPECT_EQ("mid", mock_peer_connection_->sdp_mid());
393 TEST_F(RTCPeerConnectionHandlerTest, addAndRemoveStream) {
394 std::string stream_label = "local_stream";
395 WebKit::WebMediaStream local_stream(
396 CreateLocalMediaStream(stream_label));
397 WebKit::WebMediaConstraints constraints;
399 EXPECT_CALL(*mock_tracker_.get(),
400 TrackAddStream(pc_handler_.get(),
401 testing::Ref(local_stream),
402 PeerConnectionTracker::SOURCE_LOCAL));
403 EXPECT_CALL(*mock_tracker_.get(),
404 TrackRemoveStream(pc_handler_.get(),
405 testing::Ref(local_stream),
406 PeerConnectionTracker::SOURCE_LOCAL));
407 EXPECT_TRUE(pc_handler_->addStream(local_stream, constraints));
408 EXPECT_EQ(stream_label, mock_peer_connection_->stream_label());
409 EXPECT_EQ(1u,
410 mock_peer_connection_->local_streams()->at(0)->GetAudioTracks().size());
411 EXPECT_EQ(1u,
412 mock_peer_connection_->local_streams()->at(0)->GetVideoTracks().size());
414 pc_handler_->removeStream(local_stream);
415 EXPECT_EQ(0u, mock_peer_connection_->local_streams()->count());
418 TEST_F(RTCPeerConnectionHandlerTest, GetStatsNoSelector) {
419 scoped_refptr<MockRTCStatsRequest> request(
420 new talk_base::RefCountedObject<MockRTCStatsRequest>());
421 pc_handler_->getStats(request.get());
422 // Note that callback gets executed synchronously by mock.
423 ASSERT_TRUE(request->result());
424 EXPECT_LT(1, request->result()->report_count());
427 TEST_F(RTCPeerConnectionHandlerTest, GetStatsAfterClose) {
428 scoped_refptr<MockRTCStatsRequest> request(
429 new talk_base::RefCountedObject<MockRTCStatsRequest>());
430 pc_handler_->stop();
431 pc_handler_->getStats(request.get());
432 // Note that callback gets executed synchronously by mock.
433 ASSERT_TRUE(request->result());
434 EXPECT_LT(1, request->result()->report_count());
437 TEST_F(RTCPeerConnectionHandlerTest, GetStatsWithLocalSelector) {
438 WebKit::WebMediaStream local_stream(
439 CreateLocalMediaStream("local_stream"));
440 WebKit::WebMediaConstraints constraints;
441 pc_handler_->addStream(local_stream, constraints);
442 WebKit::WebVector<WebKit::WebMediaStreamTrack> tracks;
443 local_stream.audioSources(tracks);
444 ASSERT_LE(1ul, tracks.size());
446 scoped_refptr<MockRTCStatsRequest> request(
447 new talk_base::RefCountedObject<MockRTCStatsRequest>());
448 request->setSelector(local_stream, tracks[0]);
449 pc_handler_->getStats(request.get());
450 EXPECT_EQ(1, request->result()->report_count());
453 TEST_F(RTCPeerConnectionHandlerTest, GetStatsWithRemoteSelector) {
454 scoped_refptr<webrtc::MediaStreamInterface> stream(
455 AddRemoteMockMediaStream("remote_stream", "video", "audio"));
456 pc_handler_->OnAddStream(stream);
457 const WebKit::WebMediaStream& remote_stream = mock_client_->remote_stream();
459 WebKit::WebVector<WebKit::WebMediaStreamTrack> tracks;
460 remote_stream.audioSources(tracks);
461 ASSERT_LE(1ul, tracks.size());
463 scoped_refptr<MockRTCStatsRequest> request(
464 new talk_base::RefCountedObject<MockRTCStatsRequest>());
465 request->setSelector(remote_stream, tracks[0]);
466 pc_handler_->getStats(request.get());
467 EXPECT_EQ(1, request->result()->report_count());
470 TEST_F(RTCPeerConnectionHandlerTest, GetStatsWithBadSelector) {
471 // The setup is the same as GetStatsWithLocalSelector, but the stream is not
472 // added to the PeerConnection.
473 WebKit::WebMediaStream local_stream(
474 CreateLocalMediaStream("local_stream_2"));
475 WebKit::WebMediaConstraints constraints;
476 WebKit::WebVector<WebKit::WebMediaStreamTrack> tracks;
478 local_stream.audioSources(tracks);
479 WebKit::WebMediaStreamTrack component = tracks[0];
480 mock_peer_connection_->SetGetStatsResult(false);
482 scoped_refptr<MockRTCStatsRequest> request(
483 new talk_base::RefCountedObject<MockRTCStatsRequest>());
484 request->setSelector(local_stream, component);
485 pc_handler_->getStats(request.get());
486 EXPECT_EQ(0, request->result()->report_count());
489 TEST_F(RTCPeerConnectionHandlerTest, OnSignalingChange) {
490 testing::InSequence sequence;
492 webrtc::PeerConnectionInterface::SignalingState new_state =
493 webrtc::PeerConnectionInterface::kHaveRemoteOffer;
494 EXPECT_CALL(*mock_tracker_.get(), TrackSignalingStateChange(
495 pc_handler_.get(),
496 WebRTCPeerConnectionHandlerClient::SignalingStateHaveRemoteOffer));
497 EXPECT_CALL(*mock_client_.get(), didChangeSignalingState(
498 WebRTCPeerConnectionHandlerClient::SignalingStateHaveRemoteOffer));
499 pc_handler_->OnSignalingChange(new_state);
501 new_state = webrtc::PeerConnectionInterface::kHaveLocalPrAnswer;
502 EXPECT_CALL(*mock_tracker_.get(), TrackSignalingStateChange(
503 pc_handler_.get(),
504 WebRTCPeerConnectionHandlerClient::SignalingStateHaveLocalPrAnswer));
505 EXPECT_CALL(*mock_client_.get(), didChangeSignalingState(
506 WebRTCPeerConnectionHandlerClient::SignalingStateHaveLocalPrAnswer));
507 pc_handler_->OnSignalingChange(new_state);
509 new_state = webrtc::PeerConnectionInterface::kHaveLocalOffer;
510 EXPECT_CALL(*mock_tracker_.get(), TrackSignalingStateChange(
511 pc_handler_.get(),
512 WebRTCPeerConnectionHandlerClient::SignalingStateHaveLocalOffer));
513 EXPECT_CALL(*mock_client_.get(), didChangeSignalingState(
514 WebRTCPeerConnectionHandlerClient::SignalingStateHaveLocalOffer));
515 pc_handler_->OnSignalingChange(new_state);
517 new_state = webrtc::PeerConnectionInterface::kHaveRemotePrAnswer;
518 EXPECT_CALL(*mock_tracker_.get(), TrackSignalingStateChange(
519 pc_handler_.get(),
520 WebRTCPeerConnectionHandlerClient::SignalingStateHaveRemotePrAnswer));
521 EXPECT_CALL(*mock_client_.get(), didChangeSignalingState(
522 WebRTCPeerConnectionHandlerClient::SignalingStateHaveRemotePrAnswer));
523 pc_handler_->OnSignalingChange(new_state);
525 new_state = webrtc::PeerConnectionInterface::kClosed;
526 EXPECT_CALL(*mock_tracker_.get(), TrackSignalingStateChange(
527 pc_handler_.get(),
528 WebRTCPeerConnectionHandlerClient::SignalingStateClosed));
529 EXPECT_CALL(*mock_client_.get(), didChangeSignalingState(
530 WebRTCPeerConnectionHandlerClient::SignalingStateClosed));
531 pc_handler_->OnSignalingChange(new_state);
534 TEST_F(RTCPeerConnectionHandlerTest, OnIceConnectionChange) {
535 testing::InSequence sequence;
537 webrtc::PeerConnectionInterface::IceConnectionState new_state =
538 webrtc::PeerConnectionInterface::kIceConnectionNew;
539 EXPECT_CALL(*mock_tracker_.get(), TrackIceConnectionStateChange(
540 pc_handler_.get(),
541 WebRTCPeerConnectionHandlerClient::ICEConnectionStateStarting));
542 EXPECT_CALL(*mock_client_.get(), didChangeICEConnectionState(
543 WebRTCPeerConnectionHandlerClient::ICEConnectionStateStarting));
544 pc_handler_->OnIceConnectionChange(new_state);
546 new_state = webrtc::PeerConnectionInterface::kIceConnectionChecking;
547 EXPECT_CALL(*mock_tracker_.get(), TrackIceConnectionStateChange(
548 pc_handler_.get(),
549 WebRTCPeerConnectionHandlerClient::ICEConnectionStateChecking));
550 EXPECT_CALL(*mock_client_.get(), didChangeICEConnectionState(
551 WebRTCPeerConnectionHandlerClient::ICEConnectionStateChecking));
552 pc_handler_->OnIceConnectionChange(new_state);
554 new_state = webrtc::PeerConnectionInterface::kIceConnectionConnected;
555 EXPECT_CALL(*mock_tracker_.get(), TrackIceConnectionStateChange(
556 pc_handler_.get(),
557 WebRTCPeerConnectionHandlerClient::ICEConnectionStateConnected));
558 EXPECT_CALL(*mock_client_.get(), didChangeICEConnectionState(
559 WebRTCPeerConnectionHandlerClient::ICEConnectionStateConnected));
560 pc_handler_->OnIceConnectionChange(new_state);
562 new_state = webrtc::PeerConnectionInterface::kIceConnectionCompleted;
563 EXPECT_CALL(*mock_tracker_.get(), TrackIceConnectionStateChange(
564 pc_handler_.get(),
565 WebRTCPeerConnectionHandlerClient::ICEConnectionStateCompleted));
566 EXPECT_CALL(*mock_client_.get(), didChangeICEConnectionState(
567 WebRTCPeerConnectionHandlerClient::ICEConnectionStateCompleted));
568 pc_handler_->OnIceConnectionChange(new_state);
570 new_state = webrtc::PeerConnectionInterface::kIceConnectionFailed;
571 EXPECT_CALL(*mock_tracker_.get(), TrackIceConnectionStateChange(
572 pc_handler_.get(),
573 WebRTCPeerConnectionHandlerClient::ICEConnectionStateFailed));
574 EXPECT_CALL(*mock_client_.get(), didChangeICEConnectionState(
575 WebRTCPeerConnectionHandlerClient::ICEConnectionStateFailed));
576 pc_handler_->OnIceConnectionChange(new_state);
578 new_state = webrtc::PeerConnectionInterface::kIceConnectionDisconnected;
579 EXPECT_CALL(*mock_tracker_.get(), TrackIceConnectionStateChange(
580 pc_handler_.get(),
581 WebRTCPeerConnectionHandlerClient::ICEConnectionStateDisconnected));
582 EXPECT_CALL(*mock_client_.get(), didChangeICEConnectionState(
583 WebRTCPeerConnectionHandlerClient::ICEConnectionStateDisconnected));
584 pc_handler_->OnIceConnectionChange(new_state);
586 new_state = webrtc::PeerConnectionInterface::kIceConnectionClosed;
587 EXPECT_CALL(*mock_tracker_.get(), TrackIceConnectionStateChange(
588 pc_handler_.get(),
589 WebRTCPeerConnectionHandlerClient::ICEConnectionStateClosed));
590 EXPECT_CALL(*mock_client_.get(), didChangeICEConnectionState(
591 WebRTCPeerConnectionHandlerClient::ICEConnectionStateClosed));
592 pc_handler_->OnIceConnectionChange(new_state);
595 TEST_F(RTCPeerConnectionHandlerTest, OnIceGatheringChange) {
596 testing::InSequence sequence;
597 EXPECT_CALL(*mock_tracker_.get(), TrackIceGatheringStateChange(
598 pc_handler_.get(),
599 WebRTCPeerConnectionHandlerClient::ICEGatheringStateNew));
600 EXPECT_CALL(*mock_client_.get(), didChangeICEGatheringState(
601 WebRTCPeerConnectionHandlerClient::ICEGatheringStateNew));
602 EXPECT_CALL(*mock_tracker_.get(), TrackIceGatheringStateChange(
603 pc_handler_.get(),
604 WebRTCPeerConnectionHandlerClient::ICEGatheringStateGathering));
605 EXPECT_CALL(*mock_client_.get(), didChangeICEGatheringState(
606 WebRTCPeerConnectionHandlerClient::ICEGatheringStateGathering));
607 EXPECT_CALL(*mock_tracker_.get(), TrackIceGatheringStateChange(
608 pc_handler_.get(),
609 WebRTCPeerConnectionHandlerClient::ICEGatheringStateComplete));
610 EXPECT_CALL(*mock_client_.get(), didChangeICEGatheringState(
611 WebRTCPeerConnectionHandlerClient::ICEGatheringStateComplete));
613 webrtc::PeerConnectionInterface::IceGatheringState new_state =
614 webrtc::PeerConnectionInterface::kIceGatheringNew;
615 pc_handler_->OnIceGatheringChange(new_state);
617 new_state = webrtc::PeerConnectionInterface::kIceGatheringGathering;
618 pc_handler_->OnIceGatheringChange(new_state);
620 new_state = webrtc::PeerConnectionInterface::kIceGatheringComplete;
621 pc_handler_->OnIceGatheringChange(new_state);
623 // Check NULL candidate after ice gathering is completed.
624 EXPECT_EQ("", mock_client_->candidate_mid());
625 EXPECT_EQ(-1, mock_client_->candidate_mlineindex());
626 EXPECT_EQ("", mock_client_->candidate_sdp());
629 TEST_F(RTCPeerConnectionHandlerTest, OnAddAndOnRemoveStream) {
630 std::string remote_stream_label("remote_stream");
631 scoped_refptr<webrtc::MediaStreamInterface> remote_stream(
632 AddRemoteMockMediaStream(remote_stream_label, "video", "audio"));
634 testing::InSequence sequence;
635 EXPECT_CALL(*mock_tracker_.get(), TrackAddStream(
636 pc_handler_.get(),
637 testing::Property(&WebKit::WebMediaStream::label,
638 UTF8ToUTF16(remote_stream_label)),
639 PeerConnectionTracker::SOURCE_REMOTE));
640 EXPECT_CALL(*mock_client_.get(), didAddRemoteStream(
641 testing::Property(&WebKit::WebMediaStream::label,
642 UTF8ToUTF16(remote_stream_label))));
644 EXPECT_CALL(*mock_tracker_.get(), TrackRemoveStream(
645 pc_handler_.get(),
646 testing::Property(&WebKit::WebMediaStream::label,
647 UTF8ToUTF16(remote_stream_label)),
648 PeerConnectionTracker::SOURCE_REMOTE));
649 EXPECT_CALL(*mock_client_.get(), didRemoveRemoteStream(
650 testing::Property(&WebKit::WebMediaStream::label,
651 UTF8ToUTF16(remote_stream_label))));
653 pc_handler_->OnAddStream(remote_stream);
654 pc_handler_->OnRemoveStream(remote_stream);
657 TEST_F(RTCPeerConnectionHandlerTest, OnIceCandidate) {
658 testing::InSequence sequence;
659 EXPECT_CALL(*mock_tracker_.get(),
660 TrackAddIceCandidate(pc_handler_.get(), _,
661 PeerConnectionTracker::SOURCE_LOCAL));
662 EXPECT_CALL(*mock_client_.get(), didGenerateICECandidate(_));
664 scoped_ptr<webrtc::IceCandidateInterface> native_candidate(
665 mock_dependency_factory_->CreateIceCandidate("mid", 1, kDummySdp));
666 pc_handler_->OnIceCandidate(native_candidate.get());
667 EXPECT_EQ("mid", mock_client_->candidate_mid());
668 EXPECT_EQ(1, mock_client_->candidate_mlineindex());
669 EXPECT_EQ(kDummySdp, mock_client_->candidate_sdp());
672 TEST_F(RTCPeerConnectionHandlerTest, OnRenegotiationNeeded) {
673 testing::InSequence sequence;
674 EXPECT_CALL(*mock_tracker_.get(),
675 TrackOnRenegotiationNeeded(pc_handler_.get()));
676 EXPECT_CALL(*mock_client_.get(), negotiationNeeded());
677 pc_handler_->OnRenegotiationNeeded();
680 TEST_F(RTCPeerConnectionHandlerTest, CreateDataChannel) {
681 WebKit::WebString label = "d1";
682 EXPECT_CALL(*mock_tracker_.get(),
683 TrackCreateDataChannel(pc_handler_.get(),
684 testing::NotNull(),
685 PeerConnectionTracker::SOURCE_LOCAL));
686 scoped_ptr<WebKit::WebRTCDataChannelHandler> channel(
687 pc_handler_->createDataChannel("d1", true));
688 EXPECT_TRUE(channel.get() != NULL);
689 EXPECT_EQ(label, channel->label());
692 TEST_F(RTCPeerConnectionHandlerTest, CreateDtmfSender) {
693 std::string stream_label = "local_stream";
694 WebKit::WebMediaStream local_stream(CreateLocalMediaStream(stream_label));
695 WebKit::WebMediaConstraints constraints;
696 pc_handler_->addStream(local_stream, constraints);
698 WebKit::WebVector<WebKit::WebMediaStreamTrack> tracks;
699 local_stream.videoSources(tracks);
701 ASSERT_LE(1ul, tracks.size());
702 EXPECT_FALSE(pc_handler_->createDTMFSender(tracks[0]));
704 local_stream.audioSources(tracks);
705 ASSERT_LE(1ul, tracks.size());
707 EXPECT_CALL(*mock_tracker_.get(),
708 TrackCreateDTMFSender(pc_handler_.get(),
709 testing::Ref(tracks[0])));
711 scoped_ptr<WebKit::WebRTCDTMFSenderHandler> sender(
712 pc_handler_->createDTMFSender(tracks[0]));
713 EXPECT_TRUE(sender.get());
716 } // namespace content