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.
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/values.h"
12 #include "content/child/child_process.h"
13 #include "content/renderer/media/media_stream.h"
14 #include "content/renderer/media/media_stream_audio_source.h"
15 #include "content/renderer/media/media_stream_source.h"
16 #include "content/renderer/media/media_stream_video_track.h"
17 #include "content/renderer/media/mock_media_stream_video_source.h"
18 #include "content/renderer/media/mock_peer_connection_impl.h"
19 #include "content/renderer/media/mock_web_rtc_peer_connection_handler_client.h"
20 #include "content/renderer/media/peer_connection_tracker.h"
21 #include "content/renderer/media/rtc_media_constraints.h"
22 #include "content/renderer/media/rtc_peer_connection_handler.h"
23 #include "content/renderer/media/webrtc/mock_peer_connection_dependency_factory.h"
24 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h"
25 #include "content/renderer/media/webrtc_audio_capturer.h"
26 #include "testing/gmock/include/gmock/gmock.h"
27 #include "testing/gtest/include/gtest/gtest.h"
28 #include "third_party/WebKit/public/platform/WebMediaConstraints.h"
29 #include "third_party/WebKit/public/platform/WebMediaStream.h"
30 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
31 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
32 #include "third_party/WebKit/public/platform/WebRTCConfiguration.h"
33 #include "third_party/WebKit/public/platform/WebRTCDTMFSenderHandler.h"
34 #include "third_party/WebKit/public/platform/WebRTCDataChannelHandler.h"
35 #include "third_party/WebKit/public/platform/WebRTCDataChannelInit.h"
36 #include "third_party/WebKit/public/platform/WebRTCICECandidate.h"
37 #include "third_party/WebKit/public/platform/WebRTCPeerConnectionHandlerClient.h"
38 #include "third_party/WebKit/public/platform/WebRTCSessionDescription.h"
39 #include "third_party/WebKit/public/platform/WebRTCSessionDescriptionRequest.h"
40 #include "third_party/WebKit/public/platform/WebRTCStatsRequest.h"
41 #include "third_party/WebKit/public/platform/WebRTCVoidRequest.h"
42 #include "third_party/WebKit/public/platform/WebURL.h"
43 #include "third_party/WebKit/public/web/WebHeap.h"
44 #include "third_party/libjingle/source/talk/app/webrtc/peerconnectioninterface.h"
46 static const char kDummySdp
[] = "dummy sdp";
47 static const char kDummySdpType
[] = "dummy type";
49 using blink::WebRTCPeerConnectionHandlerClient
;
50 using testing::NiceMock
;
56 class MockRTCStatsResponse
: public LocalRTCStatsResponse
{
58 MockRTCStatsResponse()
63 size_t addReport(blink::WebString type
,
65 double timestamp
) override
{
70 void addStatistic(size_t report
,
71 blink::WebString name
,
72 blink::WebString value
) override
{
75 int report_count() const { return report_count_
; }
82 // Mocked wrapper for blink::WebRTCStatsRequest
83 class MockRTCStatsRequest
: public LocalRTCStatsRequest
{
86 : has_selector_(false),
87 request_succeeded_called_(false) {}
89 bool hasSelector() const override
{ return has_selector_
; }
90 blink::WebMediaStreamTrack
component() const override
{ return component_
; }
91 scoped_refptr
<LocalRTCStatsResponse
> createResponse() override
{
92 DCHECK(!response_
.get());
93 response_
= new rtc::RefCountedObject
<MockRTCStatsResponse
>();
97 void requestSucceeded(const LocalRTCStatsResponse
* response
) override
{
98 EXPECT_EQ(response
, response_
.get());
99 request_succeeded_called_
= true;
102 // Function for setting whether or not a selector is available.
103 void setSelector(const blink::WebMediaStreamTrack
& component
) {
104 has_selector_
= true;
105 component_
= component
;
108 // Function for inspecting the result of a stats request.
109 MockRTCStatsResponse
* result() {
110 if (request_succeeded_called_
) {
111 return response_
.get();
119 blink::WebMediaStreamTrack component_
;
120 scoped_refptr
<MockRTCStatsResponse
> response_
;
121 bool request_succeeded_called_
;
124 class MockPeerConnectionTracker
: public PeerConnectionTracker
{
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 blink::WebRTCSessionDescription
& desc
,
141 void(RTCPeerConnectionHandler
* pc_handler
,
142 const webrtc::PeerConnectionInterface::RTCConfiguration
& config
,
143 const RTCMediaConstraints
& options
));
144 MOCK_METHOD4(TrackAddIceCandidate
,
145 void(RTCPeerConnectionHandler
* pc_handler
,
146 const blink::WebRTCICECandidate
& candidate
,
149 MOCK_METHOD3(TrackAddStream
,
150 void(RTCPeerConnectionHandler
* pc_handler
,
151 const blink::WebMediaStream
& stream
,
153 MOCK_METHOD3(TrackRemoveStream
,
154 void(RTCPeerConnectionHandler
* pc_handler
,
155 const blink::WebMediaStream
& stream
,
157 MOCK_METHOD1(TrackOnIceComplete
,
158 void(RTCPeerConnectionHandler
* pc_handler
));
159 MOCK_METHOD3(TrackCreateDataChannel
,
160 void(RTCPeerConnectionHandler
* pc_handler
,
161 const webrtc::DataChannelInterface
* data_channel
,
163 MOCK_METHOD1(TrackStop
, void(RTCPeerConnectionHandler
* pc_handler
));
164 MOCK_METHOD2(TrackSignalingStateChange
,
165 void(RTCPeerConnectionHandler
* pc_handler
,
166 WebRTCPeerConnectionHandlerClient::SignalingState state
));
168 TrackIceConnectionStateChange
,
169 void(RTCPeerConnectionHandler
* pc_handler
,
170 WebRTCPeerConnectionHandlerClient::ICEConnectionState state
));
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 blink::WebMediaStreamTrack
& track
));
182 class RTCPeerConnectionHandlerUnderTest
: public RTCPeerConnectionHandler
{
184 RTCPeerConnectionHandlerUnderTest(
185 WebRTCPeerConnectionHandlerClient
* client
,
186 PeerConnectionDependencyFactory
* dependency_factory
)
187 : RTCPeerConnectionHandler(client
, dependency_factory
) {
190 MockPeerConnectionImpl
* native_peer_connection() {
191 return static_cast<MockPeerConnectionImpl
*>(
192 RTCPeerConnectionHandler::native_peer_connection());
196 class RTCPeerConnectionHandlerTest
: public ::testing::Test
{
198 RTCPeerConnectionHandlerTest() : mock_peer_connection_(NULL
) {
199 child_process_
.reset(new ChildProcess());
202 virtual void SetUp() {
203 mock_client_
.reset(new NiceMock
<MockWebRTCPeerConnectionHandlerClient
>());
204 mock_dependency_factory_
.reset(new MockPeerConnectionDependencyFactory());
206 new RTCPeerConnectionHandlerUnderTest(mock_client_
.get(),
207 mock_dependency_factory_
.get()));
208 mock_tracker_
.reset(new NiceMock
<MockPeerConnectionTracker
>());
209 blink::WebRTCConfiguration config
;
210 blink::WebMediaConstraints constraints
;
211 EXPECT_TRUE(pc_handler_
->InitializeForTest(config
, constraints
,
212 mock_tracker_
.get()));
214 mock_peer_connection_
= pc_handler_
->native_peer_connection();
215 ASSERT_TRUE(mock_peer_connection_
);
218 virtual void TearDown() {
220 mock_tracker_
.reset();
221 mock_dependency_factory_
.reset();
222 mock_client_
.reset();
223 blink::WebHeap::collectAllGarbageForTesting();
226 // Creates a WebKit local MediaStream.
227 blink::WebMediaStream
CreateLocalMediaStream(
228 const std::string
& stream_label
) {
229 std::string
video_track_label("video-label");
230 std::string
audio_track_label("audio-label");
232 blink::WebMediaStreamSource audio_source
;
233 audio_source
.initialize(blink::WebString::fromUTF8(audio_track_label
),
234 blink::WebMediaStreamSource::TypeAudio
,
235 blink::WebString::fromUTF8("audio_track"));
236 audio_source
.setExtraData(new MediaStreamAudioSource());
237 blink::WebMediaStreamSource video_source
;
238 video_source
.initialize(blink::WebString::fromUTF8(video_track_label
),
239 blink::WebMediaStreamSource::TypeVideo
,
240 blink::WebString::fromUTF8("video_track"));
241 MockMediaStreamVideoSource
* native_video_source
=
242 new MockMediaStreamVideoSource(false);
243 video_source
.setExtraData(native_video_source
);
245 blink::WebVector
<blink::WebMediaStreamTrack
> audio_tracks(
246 static_cast<size_t>(1));
247 audio_tracks
[0].initialize(audio_source
.id(), audio_source
);
248 audio_tracks
[0].setExtraData(
249 new MediaStreamTrack(
250 WebRtcLocalAudioTrackAdapter::Create(audio_track_label
,
253 blink::WebVector
<blink::WebMediaStreamTrack
> video_tracks(
254 static_cast<size_t>(1));
255 blink::WebMediaConstraints constraints
;
256 constraints
.initialize();
257 video_tracks
[0] = MediaStreamVideoTrack::CreateVideoTrack(
258 native_video_source
, constraints
,
259 MediaStreamVideoSource::ConstraintsCallback(), true);
261 blink::WebMediaStream local_stream
;
262 local_stream
.initialize(base::UTF8ToUTF16(stream_label
), audio_tracks
,
264 local_stream
.setExtraData(
265 new MediaStream(local_stream
));
269 // Creates a remote MediaStream and adds it to the mocked native
271 scoped_refptr
<webrtc::MediaStreamInterface
>
272 AddRemoteMockMediaStream(const std::string
& stream_label
,
273 const std::string
& video_track_label
,
274 const std::string
& audio_track_label
) {
275 scoped_refptr
<webrtc::MediaStreamInterface
> stream(
276 mock_dependency_factory_
->CreateLocalMediaStream(stream_label
));
277 if (!video_track_label
.empty()) {
278 webrtc::VideoSourceInterface
* source
= NULL
;
279 scoped_refptr
<webrtc::VideoTrackInterface
> video_track(
280 mock_dependency_factory_
->CreateLocalVideoTrack(
281 video_track_label
, source
));
282 stream
->AddTrack(video_track
.get());
284 if (!audio_track_label
.empty()) {
285 scoped_refptr
<WebRtcAudioCapturer
> capturer
;
286 scoped_refptr
<webrtc::AudioTrackInterface
> audio_track(
287 WebRtcLocalAudioTrackAdapter::Create(audio_track_label
, NULL
));
288 stream
->AddTrack(audio_track
.get());
290 mock_peer_connection_
->AddRemoteStream(stream
.get());
294 base::MessageLoop message_loop_
;
295 scoped_ptr
<ChildProcess
> child_process_
;
296 scoped_ptr
<MockWebRTCPeerConnectionHandlerClient
> mock_client_
;
297 scoped_ptr
<MockPeerConnectionDependencyFactory
> mock_dependency_factory_
;
298 scoped_ptr
<NiceMock
<MockPeerConnectionTracker
> > mock_tracker_
;
299 scoped_ptr
<RTCPeerConnectionHandlerUnderTest
> pc_handler_
;
301 // Weak reference to the mocked native peer connection implementation.
302 MockPeerConnectionImpl
* mock_peer_connection_
;
305 TEST_F(RTCPeerConnectionHandlerTest
, Destruct
) {
306 EXPECT_CALL(*mock_tracker_
.get(), UnregisterPeerConnection(pc_handler_
.get()))
308 pc_handler_
.reset(NULL
);
311 TEST_F(RTCPeerConnectionHandlerTest
, CreateOffer
) {
312 blink::WebRTCSessionDescriptionRequest request
;
313 blink::WebMediaConstraints options
;
314 EXPECT_CALL(*mock_tracker_
.get(), TrackCreateOffer(pc_handler_
.get(), _
));
316 // TODO(perkj): Can blink::WebRTCSessionDescriptionRequest be changed so
317 // the |reqest| requestSucceeded can be tested? Currently the |request| object
318 // can not be initialized from a unit test.
319 EXPECT_FALSE(mock_peer_connection_
->created_session_description() != NULL
);
320 pc_handler_
->createOffer(request
, options
);
321 EXPECT_TRUE(mock_peer_connection_
->created_session_description() != NULL
);
324 TEST_F(RTCPeerConnectionHandlerTest
, CreateAnswer
) {
325 blink::WebRTCSessionDescriptionRequest request
;
326 blink::WebMediaConstraints options
;
327 EXPECT_CALL(*mock_tracker_
.get(), TrackCreateAnswer(pc_handler_
.get(), _
));
328 // TODO(perkj): Can blink::WebRTCSessionDescriptionRequest be changed so
329 // the |reqest| requestSucceeded can be tested? Currently the |request| object
330 // can not be initialized from a unit test.
331 EXPECT_FALSE(mock_peer_connection_
->created_session_description() != NULL
);
332 pc_handler_
->createAnswer(request
, options
);
333 EXPECT_TRUE(mock_peer_connection_
->created_session_description() != NULL
);
336 TEST_F(RTCPeerConnectionHandlerTest
, setLocalDescription
) {
337 blink::WebRTCVoidRequest request
;
338 blink::WebRTCSessionDescription description
;
339 description
.initialize(kDummySdpType
, kDummySdp
);
340 // PeerConnectionTracker::TrackSetSessionDescription is expected to be called
341 // before |mock_peer_connection| is called.
342 testing::InSequence sequence
;
343 EXPECT_CALL(*mock_tracker_
.get(),
344 TrackSetSessionDescription(pc_handler_
.get(), Ref(description
),
345 PeerConnectionTracker::SOURCE_LOCAL
));
346 EXPECT_CALL(*mock_peer_connection_
, SetLocalDescription(_
, _
));
348 pc_handler_
->setLocalDescription(request
, description
);
349 EXPECT_EQ(description
.type(), pc_handler_
->localDescription().type());
350 EXPECT_EQ(description
.sdp(), pc_handler_
->localDescription().sdp());
352 std::string sdp_string
;
353 ASSERT_TRUE(mock_peer_connection_
->local_description() != NULL
);
354 EXPECT_EQ(kDummySdpType
, mock_peer_connection_
->local_description()->type());
355 mock_peer_connection_
->local_description()->ToString(&sdp_string
);
356 EXPECT_EQ(kDummySdp
, sdp_string
);
359 TEST_F(RTCPeerConnectionHandlerTest
, setRemoteDescription
) {
360 blink::WebRTCVoidRequest request
;
361 blink::WebRTCSessionDescription description
;
362 description
.initialize(kDummySdpType
, kDummySdp
);
364 // PeerConnectionTracker::TrackSetSessionDescription is expected to be called
365 // before |mock_peer_connection| is called.
366 testing::InSequence sequence
;
367 EXPECT_CALL(*mock_tracker_
.get(),
368 TrackSetSessionDescription(pc_handler_
.get(), Ref(description
),
369 PeerConnectionTracker::SOURCE_REMOTE
));
370 EXPECT_CALL(*mock_peer_connection_
, SetRemoteDescription(_
, _
));
372 pc_handler_
->setRemoteDescription(request
, description
);
373 EXPECT_EQ(description
.type(), pc_handler_
->remoteDescription().type());
374 EXPECT_EQ(description
.sdp(), pc_handler_
->remoteDescription().sdp());
376 std::string sdp_string
;
377 ASSERT_TRUE(mock_peer_connection_
->remote_description() != NULL
);
378 EXPECT_EQ(kDummySdpType
, mock_peer_connection_
->remote_description()->type());
379 mock_peer_connection_
->remote_description()->ToString(&sdp_string
);
380 EXPECT_EQ(kDummySdp
, sdp_string
);
383 TEST_F(RTCPeerConnectionHandlerTest
, updateICE
) {
384 blink::WebRTCConfiguration config
;
385 blink::WebMediaConstraints constraints
;
387 EXPECT_CALL(*mock_tracker_
.get(), TrackUpdateIce(pc_handler_
.get(), _
, _
));
388 // TODO(perkj): Test that the parameters in |config| can be translated when a
389 // WebRTCConfiguration can be constructed. It's WebKit class and can't be
390 // initialized from a test.
391 EXPECT_TRUE(pc_handler_
->updateICE(config
, constraints
));
394 TEST_F(RTCPeerConnectionHandlerTest
, addICECandidate
) {
395 blink::WebRTCICECandidate candidate
;
396 candidate
.initialize(kDummySdp
, "sdpMid", 1);
398 EXPECT_CALL(*mock_tracker_
.get(),
399 TrackAddIceCandidate(pc_handler_
.get(),
400 testing::Ref(candidate
),
401 PeerConnectionTracker::SOURCE_REMOTE
,
403 EXPECT_TRUE(pc_handler_
->addICECandidate(candidate
));
404 EXPECT_EQ(kDummySdp
, mock_peer_connection_
->ice_sdp());
405 EXPECT_EQ(1, mock_peer_connection_
->sdp_mline_index());
406 EXPECT_EQ("sdpMid", mock_peer_connection_
->sdp_mid());
409 TEST_F(RTCPeerConnectionHandlerTest
, addAndRemoveStream
) {
410 std::string stream_label
= "local_stream";
411 blink::WebMediaStream
local_stream(
412 CreateLocalMediaStream(stream_label
));
413 blink::WebMediaConstraints constraints
;
415 EXPECT_CALL(*mock_tracker_
.get(),
416 TrackAddStream(pc_handler_
.get(),
417 testing::Ref(local_stream
),
418 PeerConnectionTracker::SOURCE_LOCAL
));
419 EXPECT_CALL(*mock_tracker_
.get(),
420 TrackRemoveStream(pc_handler_
.get(),
421 testing::Ref(local_stream
),
422 PeerConnectionTracker::SOURCE_LOCAL
));
423 EXPECT_TRUE(pc_handler_
->addStream(local_stream
, constraints
));
424 EXPECT_EQ(stream_label
, mock_peer_connection_
->stream_label());
426 mock_peer_connection_
->local_streams()->at(0)->GetAudioTracks().size());
428 mock_peer_connection_
->local_streams()->at(0)->GetVideoTracks().size());
430 EXPECT_FALSE(pc_handler_
->addStream(local_stream
, constraints
));
432 pc_handler_
->removeStream(local_stream
);
433 EXPECT_EQ(0u, mock_peer_connection_
->local_streams()->count());
436 TEST_F(RTCPeerConnectionHandlerTest
, addStreamWithStoppedAudioAndVideoTrack
) {
437 std::string stream_label
= "local_stream";
438 blink::WebMediaStream
local_stream(
439 CreateLocalMediaStream(stream_label
));
440 blink::WebMediaConstraints constraints
;
442 blink::WebVector
<blink::WebMediaStreamTrack
> audio_tracks
;
443 local_stream
.audioTracks(audio_tracks
);
444 MediaStreamAudioSource
* native_audio_source
=
445 static_cast<MediaStreamAudioSource
*>(
446 audio_tracks
[0].source().extraData());
447 native_audio_source
->StopSource();
449 blink::WebVector
<blink::WebMediaStreamTrack
> video_tracks
;
450 local_stream
.videoTracks(video_tracks
);
451 MediaStreamVideoSource
* native_video_source
=
452 static_cast<MediaStreamVideoSource
*>(
453 video_tracks
[0].source().extraData());
454 native_video_source
->StopSource();
456 EXPECT_TRUE(pc_handler_
->addStream(local_stream
, constraints
));
457 EXPECT_EQ(stream_label
, mock_peer_connection_
->stream_label());
460 mock_peer_connection_
->local_streams()->at(0)->GetAudioTracks().size());
463 mock_peer_connection_
->local_streams()->at(0)->GetVideoTracks().size());
466 TEST_F(RTCPeerConnectionHandlerTest
, GetStatsNoSelector
) {
467 scoped_refptr
<MockRTCStatsRequest
> request(
468 new rtc::RefCountedObject
<MockRTCStatsRequest
>());
469 pc_handler_
->getStats(request
.get());
470 // Note that callback gets executed synchronously by mock.
471 ASSERT_TRUE(request
->result());
472 EXPECT_LT(1, request
->result()->report_count());
475 TEST_F(RTCPeerConnectionHandlerTest
, GetStatsAfterClose
) {
476 scoped_refptr
<MockRTCStatsRequest
> request(
477 new rtc::RefCountedObject
<MockRTCStatsRequest
>());
479 pc_handler_
->getStats(request
.get());
480 // Note that callback gets executed synchronously by mock.
481 ASSERT_TRUE(request
->result());
482 EXPECT_LT(1, request
->result()->report_count());
485 TEST_F(RTCPeerConnectionHandlerTest
, GetStatsWithLocalSelector
) {
486 blink::WebMediaStream
local_stream(
487 CreateLocalMediaStream("local_stream"));
488 blink::WebMediaConstraints constraints
;
489 pc_handler_
->addStream(local_stream
, constraints
);
490 blink::WebVector
<blink::WebMediaStreamTrack
> tracks
;
491 local_stream
.audioTracks(tracks
);
492 ASSERT_LE(1ul, tracks
.size());
494 scoped_refptr
<MockRTCStatsRequest
> request(
495 new rtc::RefCountedObject
<MockRTCStatsRequest
>());
496 request
->setSelector(tracks
[0]);
497 pc_handler_
->getStats(request
.get());
498 EXPECT_EQ(1, request
->result()->report_count());
501 TEST_F(RTCPeerConnectionHandlerTest
, GetStatsWithRemoteSelector
) {
502 scoped_refptr
<webrtc::MediaStreamInterface
> stream(
503 AddRemoteMockMediaStream("remote_stream", "video", "audio"));
504 pc_handler_
->OnAddStream(stream
.get());
505 const blink::WebMediaStream
& remote_stream
= mock_client_
->remote_stream();
507 blink::WebVector
<blink::WebMediaStreamTrack
> tracks
;
508 remote_stream
.audioTracks(tracks
);
509 ASSERT_LE(1ul, tracks
.size());
511 scoped_refptr
<MockRTCStatsRequest
> request(
512 new rtc::RefCountedObject
<MockRTCStatsRequest
>());
513 request
->setSelector(tracks
[0]);
514 pc_handler_
->getStats(request
.get());
515 EXPECT_EQ(1, request
->result()->report_count());
518 TEST_F(RTCPeerConnectionHandlerTest
, GetStatsWithBadSelector
) {
519 // The setup is the same as GetStatsWithLocalSelector, but the stream is not
520 // added to the PeerConnection.
521 blink::WebMediaStream
local_stream(
522 CreateLocalMediaStream("local_stream_2"));
523 blink::WebMediaConstraints constraints
;
524 blink::WebVector
<blink::WebMediaStreamTrack
> tracks
;
526 local_stream
.audioTracks(tracks
);
527 blink::WebMediaStreamTrack component
= tracks
[0];
528 mock_peer_connection_
->SetGetStatsResult(false);
530 scoped_refptr
<MockRTCStatsRequest
> request(
531 new rtc::RefCountedObject
<MockRTCStatsRequest
>());
532 request
->setSelector(component
);
533 pc_handler_
->getStats(request
.get());
534 EXPECT_EQ(0, request
->result()->report_count());
537 TEST_F(RTCPeerConnectionHandlerTest
, OnSignalingChange
) {
538 testing::InSequence sequence
;
540 webrtc::PeerConnectionInterface::SignalingState new_state
=
541 webrtc::PeerConnectionInterface::kHaveRemoteOffer
;
542 EXPECT_CALL(*mock_tracker_
.get(), TrackSignalingStateChange(
544 WebRTCPeerConnectionHandlerClient::SignalingStateHaveRemoteOffer
));
545 EXPECT_CALL(*mock_client_
.get(), didChangeSignalingState(
546 WebRTCPeerConnectionHandlerClient::SignalingStateHaveRemoteOffer
));
547 pc_handler_
->OnSignalingChange(new_state
);
549 new_state
= webrtc::PeerConnectionInterface::kHaveLocalPrAnswer
;
550 EXPECT_CALL(*mock_tracker_
.get(), TrackSignalingStateChange(
552 WebRTCPeerConnectionHandlerClient::SignalingStateHaveLocalPrAnswer
));
553 EXPECT_CALL(*mock_client_
.get(), didChangeSignalingState(
554 WebRTCPeerConnectionHandlerClient::SignalingStateHaveLocalPrAnswer
));
555 pc_handler_
->OnSignalingChange(new_state
);
557 new_state
= webrtc::PeerConnectionInterface::kHaveLocalOffer
;
558 EXPECT_CALL(*mock_tracker_
.get(), TrackSignalingStateChange(
560 WebRTCPeerConnectionHandlerClient::SignalingStateHaveLocalOffer
));
561 EXPECT_CALL(*mock_client_
.get(), didChangeSignalingState(
562 WebRTCPeerConnectionHandlerClient::SignalingStateHaveLocalOffer
));
563 pc_handler_
->OnSignalingChange(new_state
);
565 new_state
= webrtc::PeerConnectionInterface::kHaveRemotePrAnswer
;
566 EXPECT_CALL(*mock_tracker_
.get(), TrackSignalingStateChange(
568 WebRTCPeerConnectionHandlerClient::SignalingStateHaveRemotePrAnswer
));
569 EXPECT_CALL(*mock_client_
.get(), didChangeSignalingState(
570 WebRTCPeerConnectionHandlerClient::SignalingStateHaveRemotePrAnswer
));
571 pc_handler_
->OnSignalingChange(new_state
);
573 new_state
= webrtc::PeerConnectionInterface::kClosed
;
574 EXPECT_CALL(*mock_tracker_
.get(), TrackSignalingStateChange(
576 WebRTCPeerConnectionHandlerClient::SignalingStateClosed
));
577 EXPECT_CALL(*mock_client_
.get(), didChangeSignalingState(
578 WebRTCPeerConnectionHandlerClient::SignalingStateClosed
));
579 pc_handler_
->OnSignalingChange(new_state
);
582 TEST_F(RTCPeerConnectionHandlerTest
, OnIceConnectionChange
) {
583 testing::InSequence sequence
;
585 webrtc::PeerConnectionInterface::IceConnectionState new_state
=
586 webrtc::PeerConnectionInterface::kIceConnectionNew
;
587 EXPECT_CALL(*mock_tracker_
.get(), TrackIceConnectionStateChange(
589 WebRTCPeerConnectionHandlerClient::ICEConnectionStateStarting
));
590 EXPECT_CALL(*mock_client_
.get(), didChangeICEConnectionState(
591 WebRTCPeerConnectionHandlerClient::ICEConnectionStateStarting
));
592 pc_handler_
->OnIceConnectionChange(new_state
);
594 new_state
= webrtc::PeerConnectionInterface::kIceConnectionChecking
;
595 EXPECT_CALL(*mock_tracker_
.get(), TrackIceConnectionStateChange(
597 WebRTCPeerConnectionHandlerClient::ICEConnectionStateChecking
));
598 EXPECT_CALL(*mock_client_
.get(), didChangeICEConnectionState(
599 WebRTCPeerConnectionHandlerClient::ICEConnectionStateChecking
));
600 pc_handler_
->OnIceConnectionChange(new_state
);
602 new_state
= webrtc::PeerConnectionInterface::kIceConnectionConnected
;
603 EXPECT_CALL(*mock_tracker_
.get(), TrackIceConnectionStateChange(
605 WebRTCPeerConnectionHandlerClient::ICEConnectionStateConnected
));
606 EXPECT_CALL(*mock_client_
.get(), didChangeICEConnectionState(
607 WebRTCPeerConnectionHandlerClient::ICEConnectionStateConnected
));
608 pc_handler_
->OnIceConnectionChange(new_state
);
610 new_state
= webrtc::PeerConnectionInterface::kIceConnectionCompleted
;
611 EXPECT_CALL(*mock_tracker_
.get(), TrackIceConnectionStateChange(
613 WebRTCPeerConnectionHandlerClient::ICEConnectionStateCompleted
));
614 EXPECT_CALL(*mock_client_
.get(), didChangeICEConnectionState(
615 WebRTCPeerConnectionHandlerClient::ICEConnectionStateCompleted
));
616 pc_handler_
->OnIceConnectionChange(new_state
);
618 new_state
= webrtc::PeerConnectionInterface::kIceConnectionFailed
;
619 EXPECT_CALL(*mock_tracker_
.get(), TrackIceConnectionStateChange(
621 WebRTCPeerConnectionHandlerClient::ICEConnectionStateFailed
));
622 EXPECT_CALL(*mock_client_
.get(), didChangeICEConnectionState(
623 WebRTCPeerConnectionHandlerClient::ICEConnectionStateFailed
));
624 pc_handler_
->OnIceConnectionChange(new_state
);
626 new_state
= webrtc::PeerConnectionInterface::kIceConnectionDisconnected
;
627 EXPECT_CALL(*mock_tracker_
.get(), TrackIceConnectionStateChange(
629 WebRTCPeerConnectionHandlerClient::ICEConnectionStateDisconnected
));
630 EXPECT_CALL(*mock_client_
.get(), didChangeICEConnectionState(
631 WebRTCPeerConnectionHandlerClient::ICEConnectionStateDisconnected
));
632 pc_handler_
->OnIceConnectionChange(new_state
);
634 new_state
= webrtc::PeerConnectionInterface::kIceConnectionClosed
;
635 EXPECT_CALL(*mock_tracker_
.get(), TrackIceConnectionStateChange(
637 WebRTCPeerConnectionHandlerClient::ICEConnectionStateClosed
));
638 EXPECT_CALL(*mock_client_
.get(), didChangeICEConnectionState(
639 WebRTCPeerConnectionHandlerClient::ICEConnectionStateClosed
));
640 pc_handler_
->OnIceConnectionChange(new_state
);
643 TEST_F(RTCPeerConnectionHandlerTest
, OnIceGatheringChange
) {
644 testing::InSequence sequence
;
645 EXPECT_CALL(*mock_tracker_
.get(), TrackIceGatheringStateChange(
647 WebRTCPeerConnectionHandlerClient::ICEGatheringStateNew
));
648 EXPECT_CALL(*mock_client_
.get(), didChangeICEGatheringState(
649 WebRTCPeerConnectionHandlerClient::ICEGatheringStateNew
));
650 EXPECT_CALL(*mock_tracker_
.get(), TrackIceGatheringStateChange(
652 WebRTCPeerConnectionHandlerClient::ICEGatheringStateGathering
));
653 EXPECT_CALL(*mock_client_
.get(), didChangeICEGatheringState(
654 WebRTCPeerConnectionHandlerClient::ICEGatheringStateGathering
));
655 EXPECT_CALL(*mock_tracker_
.get(), TrackIceGatheringStateChange(
657 WebRTCPeerConnectionHandlerClient::ICEGatheringStateComplete
));
658 EXPECT_CALL(*mock_client_
.get(), didChangeICEGatheringState(
659 WebRTCPeerConnectionHandlerClient::ICEGatheringStateComplete
));
661 webrtc::PeerConnectionInterface::IceGatheringState new_state
=
662 webrtc::PeerConnectionInterface::kIceGatheringNew
;
663 pc_handler_
->OnIceGatheringChange(new_state
);
665 new_state
= webrtc::PeerConnectionInterface::kIceGatheringGathering
;
666 pc_handler_
->OnIceGatheringChange(new_state
);
668 new_state
= webrtc::PeerConnectionInterface::kIceGatheringComplete
;
669 pc_handler_
->OnIceGatheringChange(new_state
);
671 // Check NULL candidate after ice gathering is completed.
672 EXPECT_EQ("", mock_client_
->candidate_mid());
673 EXPECT_EQ(-1, mock_client_
->candidate_mlineindex());
674 EXPECT_EQ("", mock_client_
->candidate_sdp());
677 TEST_F(RTCPeerConnectionHandlerTest
, OnAddAndOnRemoveStream
) {
678 std::string
remote_stream_label("remote_stream");
679 scoped_refptr
<webrtc::MediaStreamInterface
> remote_stream(
680 AddRemoteMockMediaStream(remote_stream_label
, "video", "audio"));
682 testing::InSequence sequence
;
683 EXPECT_CALL(*mock_tracker_
.get(), TrackAddStream(
685 testing::Property(&blink::WebMediaStream::id
,
686 base::UTF8ToUTF16(remote_stream_label
)),
687 PeerConnectionTracker::SOURCE_REMOTE
));
688 EXPECT_CALL(*mock_client_
.get(), didAddRemoteStream(
689 testing::Property(&blink::WebMediaStream::id
,
690 base::UTF8ToUTF16(remote_stream_label
))));
692 EXPECT_CALL(*mock_tracker_
.get(), TrackRemoveStream(
694 testing::Property(&blink::WebMediaStream::id
,
695 base::UTF8ToUTF16(remote_stream_label
)),
696 PeerConnectionTracker::SOURCE_REMOTE
));
697 EXPECT_CALL(*mock_client_
.get(), didRemoveRemoteStream(
698 testing::Property(&blink::WebMediaStream::id
,
699 base::UTF8ToUTF16(remote_stream_label
))));
701 pc_handler_
->OnAddStream(remote_stream
.get());
702 pc_handler_
->OnRemoveStream(remote_stream
.get());
705 // This test that WebKit is notified about remote track state changes.
706 TEST_F(RTCPeerConnectionHandlerTest
, RemoteTrackState
) {
707 std::string
remote_stream_label("remote_stream");
708 scoped_refptr
<webrtc::MediaStreamInterface
> remote_stream(
709 AddRemoteMockMediaStream(remote_stream_label
, "video", "audio"));
711 testing::InSequence sequence
;
712 EXPECT_CALL(*mock_client_
.get(), didAddRemoteStream(
713 testing::Property(&blink::WebMediaStream::id
,
714 base::UTF8ToUTF16(remote_stream_label
))));
715 pc_handler_
->OnAddStream(remote_stream
.get());
716 const blink::WebMediaStream
& webkit_stream
= mock_client_
->remote_stream();
718 blink::WebVector
<blink::WebMediaStreamTrack
> audio_tracks
;
719 webkit_stream
.audioTracks(audio_tracks
);
720 EXPECT_EQ(blink::WebMediaStreamSource::ReadyStateLive
,
721 audio_tracks
[0].source().readyState());
723 blink::WebVector
<blink::WebMediaStreamTrack
> video_tracks
;
724 webkit_stream
.videoTracks(video_tracks
);
725 EXPECT_EQ(blink::WebMediaStreamSource::ReadyStateLive
,
726 video_tracks
[0].source().readyState());
728 remote_stream
->GetAudioTracks()[0]->set_state(
729 webrtc::MediaStreamTrackInterface::kEnded
);
730 EXPECT_EQ(blink::WebMediaStreamSource::ReadyStateEnded
,
731 audio_tracks
[0].source().readyState());
733 remote_stream
->GetVideoTracks()[0]->set_state(
734 webrtc::MediaStreamTrackInterface::kEnded
);
735 EXPECT_EQ(blink::WebMediaStreamSource::ReadyStateEnded
,
736 video_tracks
[0].source().readyState());
739 TEST_F(RTCPeerConnectionHandlerTest
, RemoveAndAddAudioTrackFromRemoteStream
) {
740 std::string
remote_stream_label("remote_stream");
741 scoped_refptr
<webrtc::MediaStreamInterface
> remote_stream(
742 AddRemoteMockMediaStream(remote_stream_label
, "video", "audio"));
744 EXPECT_CALL(*mock_client_
.get(), didAddRemoteStream(
745 testing::Property(&blink::WebMediaStream::id
,
746 base::UTF8ToUTF16(remote_stream_label
))));
747 pc_handler_
->OnAddStream(remote_stream
.get());
748 const blink::WebMediaStream
& webkit_stream
= mock_client_
->remote_stream();
751 // Test in a small scope so that |audio_tracks| don't hold on to destroyed
753 blink::WebVector
<blink::WebMediaStreamTrack
> audio_tracks
;
754 webkit_stream
.audioTracks(audio_tracks
);
755 EXPECT_EQ(1u, audio_tracks
.size());
758 // Remove the Webrtc audio track from the Webrtc MediaStream.
759 scoped_refptr
<webrtc::AudioTrackInterface
> webrtc_track
=
760 remote_stream
->GetAudioTracks()[0].get();
761 remote_stream
->RemoveTrack(webrtc_track
.get());
764 blink::WebVector
<blink::WebMediaStreamTrack
> modified_audio_tracks1
;
765 webkit_stream
.audioTracks(modified_audio_tracks1
);
766 EXPECT_EQ(0u, modified_audio_tracks1
.size());
769 blink::WebHeap::collectGarbageForTesting();
771 // Add the WebRtc audio track again.
772 remote_stream
->AddTrack(webrtc_track
.get());
773 blink::WebVector
<blink::WebMediaStreamTrack
> modified_audio_tracks2
;
774 webkit_stream
.audioTracks(modified_audio_tracks2
);
775 EXPECT_EQ(1u, modified_audio_tracks2
.size());
778 TEST_F(RTCPeerConnectionHandlerTest
, RemoveAndAddVideoTrackFromRemoteStream
) {
779 std::string
remote_stream_label("remote_stream");
780 scoped_refptr
<webrtc::MediaStreamInterface
> remote_stream(
781 AddRemoteMockMediaStream(remote_stream_label
, "video", "video"));
783 EXPECT_CALL(*mock_client_
.get(), didAddRemoteStream(
784 testing::Property(&blink::WebMediaStream::id
,
785 base::UTF8ToUTF16(remote_stream_label
))));
786 pc_handler_
->OnAddStream(remote_stream
.get());
787 const blink::WebMediaStream
& webkit_stream
= mock_client_
->remote_stream();
790 // Test in a small scope so that |video_tracks| don't hold on to destroyed
792 blink::WebVector
<blink::WebMediaStreamTrack
> video_tracks
;
793 webkit_stream
.videoTracks(video_tracks
);
794 EXPECT_EQ(1u, video_tracks
.size());
797 // Remove the Webrtc video track from the Webrtc MediaStream.
798 scoped_refptr
<webrtc::VideoTrackInterface
> webrtc_track
=
799 remote_stream
->GetVideoTracks()[0].get();
800 remote_stream
->RemoveTrack(webrtc_track
.get());
802 blink::WebVector
<blink::WebMediaStreamTrack
> modified_video_tracks1
;
803 webkit_stream
.videoTracks(modified_video_tracks1
);
804 EXPECT_EQ(0u, modified_video_tracks1
.size());
807 blink::WebHeap::collectGarbageForTesting();
809 // Add the WebRtc video track again.
810 remote_stream
->AddTrack(webrtc_track
.get());
811 blink::WebVector
<blink::WebMediaStreamTrack
> modified_video_tracks2
;
812 webkit_stream
.videoTracks(modified_video_tracks2
);
813 EXPECT_EQ(1u, modified_video_tracks2
.size());
816 TEST_F(RTCPeerConnectionHandlerTest
, OnIceCandidate
) {
817 testing::InSequence sequence
;
818 EXPECT_CALL(*mock_tracker_
.get(),
819 TrackAddIceCandidate(pc_handler_
.get(), _
,
820 PeerConnectionTracker::SOURCE_LOCAL
, true));
821 EXPECT_CALL(*mock_client_
.get(), didGenerateICECandidate(_
));
823 scoped_ptr
<webrtc::IceCandidateInterface
> native_candidate(
824 mock_dependency_factory_
->CreateIceCandidate("sdpMid", 1, kDummySdp
));
825 pc_handler_
->OnIceCandidate(native_candidate
.get());
826 EXPECT_EQ("sdpMid", mock_client_
->candidate_mid());
827 EXPECT_EQ(1, mock_client_
->candidate_mlineindex());
828 EXPECT_EQ(kDummySdp
, mock_client_
->candidate_sdp());
831 TEST_F(RTCPeerConnectionHandlerTest
, OnRenegotiationNeeded
) {
832 testing::InSequence sequence
;
833 EXPECT_CALL(*mock_tracker_
.get(),
834 TrackOnRenegotiationNeeded(pc_handler_
.get()));
835 EXPECT_CALL(*mock_client_
.get(), negotiationNeeded());
836 pc_handler_
->OnRenegotiationNeeded();
839 TEST_F(RTCPeerConnectionHandlerTest
, CreateDataChannel
) {
840 blink::WebString label
= "d1";
841 EXPECT_CALL(*mock_tracker_
.get(),
842 TrackCreateDataChannel(pc_handler_
.get(),
844 PeerConnectionTracker::SOURCE_LOCAL
));
845 scoped_ptr
<blink::WebRTCDataChannelHandler
> channel(
846 pc_handler_
->createDataChannel("d1", blink::WebRTCDataChannelInit()));
847 EXPECT_TRUE(channel
.get() != NULL
);
848 EXPECT_EQ(label
, channel
->label());
851 TEST_F(RTCPeerConnectionHandlerTest
, CreateDtmfSender
) {
852 std::string stream_label
= "local_stream";
853 blink::WebMediaStream
local_stream(CreateLocalMediaStream(stream_label
));
854 blink::WebMediaConstraints constraints
;
855 pc_handler_
->addStream(local_stream
, constraints
);
857 blink::WebVector
<blink::WebMediaStreamTrack
> tracks
;
858 local_stream
.videoTracks(tracks
);
860 ASSERT_LE(1ul, tracks
.size());
861 EXPECT_FALSE(pc_handler_
->createDTMFSender(tracks
[0]));
863 local_stream
.audioTracks(tracks
);
864 ASSERT_LE(1ul, tracks
.size());
866 EXPECT_CALL(*mock_tracker_
.get(),
867 TrackCreateDTMFSender(pc_handler_
.get(),
868 testing::Ref(tracks
[0])));
870 scoped_ptr
<blink::WebRTCDTMFSenderHandler
> sender(
871 pc_handler_
->createDTMFSender(tracks
[0]));
872 EXPECT_TRUE(sender
.get());
875 } // namespace content