OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include <string> |
| 6 |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/utf_string_conversions.h" |
| 9 #include "content/renderer/media/media_stream_extra_data.h" |
| 10 #include "content/renderer/media/mock_media_stream_dependency_factory.h" |
| 11 #include "content/renderer/media/mock_peer_connection_impl.h" |
| 12 #include "content/renderer/media/mock_web_rtc_peer_connection_handler_client.h" |
| 13 #include "content/renderer/media/rtc_peer_connection_handler.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "third_party/libjingle/source/talk/app/webrtc/peerconnectioninterface.h
" |
| 16 #include "third_party/WebKit/Source/Platform/chromium/public/WebMediaConstraints
.h" |
| 17 #include "third_party/WebKit/Source/Platform/chromium/public/WebMediaStreamDescr
iptor.h" |
| 18 #include "third_party/WebKit/Source/Platform/chromium/public/WebMediaStreamSourc
e.h" |
| 19 #include "third_party/WebKit/Source/Platform/chromium/public/WebRTCConfiguration
.h" |
| 20 #include "third_party/WebKit/Source/Platform/chromium/public/WebRTCICECandidateD
escriptor.h" |
| 21 #include "third_party/WebKit/Source/Platform/chromium/public/WebRTCPeerConnectio
nHandlerClient.h" |
| 22 #include "third_party/WebKit/Source/Platform/chromium/public/WebRTCSessionDescri
ptionDescriptor.h" |
| 23 #include "third_party/WebKit/Source/Platform/chromium/public/WebRTCSessionDescri
ptionRequest.h" |
| 24 #include "third_party/WebKit/Source/Platform/chromium/public/WebRTCVoidRequest.h
" |
| 25 #include "third_party/WebKit/Source/Platform/chromium/public/WebURL.h" |
| 26 |
| 27 static const char kDummySdp[] = "dummy sdp"; |
| 28 static const char kDummySdpType[] = "dummy type"; |
| 29 |
| 30 class RTCPeerConnectionHandlerUnderTest : public RTCPeerConnectionHandler { |
| 31 public: |
| 32 RTCPeerConnectionHandlerUnderTest( |
| 33 WebKit::WebRTCPeerConnectionHandlerClient* client, |
| 34 MediaStreamDependencyFactory* dependency_factory) |
| 35 : RTCPeerConnectionHandler(client, dependency_factory) { |
| 36 } |
| 37 |
| 38 webrtc::MockPeerConnectionImpl* native_peer_connection() { |
| 39 return static_cast<webrtc::MockPeerConnectionImpl*>( |
| 40 native_peer_connection_.get()); |
| 41 } |
| 42 }; |
| 43 |
| 44 class RTCPeerConnectionHandlerTest : public ::testing::Test { |
| 45 public: |
| 46 RTCPeerConnectionHandlerTest() : mock_peer_connection_(NULL) { |
| 47 } |
| 48 |
| 49 void SetUp() { |
| 50 mock_client_.reset(new WebKit::MockWebRTCPeerConnectionHandlerClient()); |
| 51 mock_dependency_factory_.reset( |
| 52 new MockMediaStreamDependencyFactory(NULL)); |
| 53 mock_dependency_factory_->CreatePeerConnectionFactory(NULL, |
| 54 NULL, |
| 55 NULL, |
| 56 NULL, |
| 57 NULL); |
| 58 pc_handler_.reset( |
| 59 new RTCPeerConnectionHandlerUnderTest(mock_client_.get(), |
| 60 mock_dependency_factory_.get())); |
| 61 |
| 62 WebKit::WebRTCConfiguration config; |
| 63 WebKit::WebMediaConstraints constraints; |
| 64 EXPECT_TRUE(pc_handler_->initialize(config, constraints)); |
| 65 |
| 66 mock_peer_connection_ = pc_handler_->native_peer_connection(); |
| 67 ASSERT_TRUE(mock_peer_connection_); |
| 68 } |
| 69 |
| 70 void Initialize(const std::string& server, const std::string& password) { |
| 71 WebKit::WebRTCConfiguration config; |
| 72 WebKit::WebMediaConstraints constraints; |
| 73 |
| 74 // TODO(perkj): Test that the parameters in |config| can be translated when |
| 75 // a WebRTCConfiguration can be constructed. It's WebKit class and can't be |
| 76 // initialized from a test. |
| 77 EXPECT_TRUE(pc_handler_->initialize(config, constraints)); |
| 78 |
| 79 mock_peer_connection_ = pc_handler_->native_peer_connection(); |
| 80 ASSERT_TRUE(mock_peer_connection_); |
| 81 } |
| 82 |
| 83 // Creates a WebKit local MediaStream. |
| 84 WebKit::WebMediaStreamDescriptor CreateLocalMediaStream( |
| 85 const std::string& stream_label) { |
| 86 std::string video_track_label("video-label"); |
| 87 std::string audio_track_label("audio-label"); |
| 88 |
| 89 talk_base::scoped_refptr<webrtc::LocalMediaStreamInterface> native_stream( |
| 90 mock_dependency_factory_->CreateLocalMediaStream(stream_label)); |
| 91 talk_base::scoped_refptr<webrtc::LocalAudioTrackInterface> audio_track( |
| 92 mock_dependency_factory_->CreateLocalAudioTrack(audio_track_label, |
| 93 NULL)); |
| 94 native_stream->AddTrack(audio_track); |
| 95 talk_base::scoped_refptr<webrtc::LocalVideoTrackInterface> video_track( |
| 96 mock_dependency_factory_->CreateLocalVideoTrack(video_track_label, 0)); |
| 97 native_stream->AddTrack(video_track); |
| 98 |
| 99 WebKit::WebVector<WebKit::WebMediaStreamSource> audio_sources( |
| 100 static_cast<size_t>(1)); |
| 101 audio_sources[0].initialize(WebKit::WebString::fromUTF8(video_track_label), |
| 102 WebKit::WebMediaStreamSource::TypeAudio, |
| 103 WebKit::WebString::fromUTF8("audio_track")); |
| 104 WebKit::WebVector<WebKit::WebMediaStreamSource> video_sources( |
| 105 static_cast<size_t>(1)); |
| 106 video_sources[0].initialize(WebKit::WebString::fromUTF8(video_track_label), |
| 107 WebKit::WebMediaStreamSource::TypeVideo, |
| 108 WebKit::WebString::fromUTF8("video_track")); |
| 109 WebKit::WebMediaStreamDescriptor local_stream; |
| 110 local_stream.initialize(UTF8ToUTF16(stream_label), audio_sources, |
| 111 video_sources); |
| 112 local_stream.setExtraData(new MediaStreamExtraData(native_stream)); |
| 113 return local_stream; |
| 114 } |
| 115 |
| 116 // Creates a remote MediaStream and adds it to the mocked native |
| 117 // peer connection. |
| 118 talk_base::scoped_refptr<webrtc::MediaStreamInterface> |
| 119 AddRemoteMockMediaStream(const std::string& stream_label, |
| 120 const std::string& video_track_label, |
| 121 const std::string& audio_track_label) { |
| 122 // We use a local stream as a remote since for testing purposes we really |
| 123 // only need the MediaStreamInterface. |
| 124 talk_base::scoped_refptr<webrtc::LocalMediaStreamInterface> stream( |
| 125 mock_dependency_factory_->CreateLocalMediaStream(stream_label)); |
| 126 if (!video_track_label.empty()) { |
| 127 talk_base::scoped_refptr<webrtc::LocalVideoTrackInterface> video_track( |
| 128 mock_dependency_factory_->CreateLocalVideoTrack(video_track_label, |
| 129 0)); |
| 130 stream->AddTrack(video_track); |
| 131 } |
| 132 if (!audio_track_label.empty()) { |
| 133 talk_base::scoped_refptr<webrtc::LocalAudioTrackInterface> audio_track( |
| 134 mock_dependency_factory_->CreateLocalAudioTrack(audio_track_label, |
| 135 NULL)); |
| 136 stream->AddTrack(audio_track); |
| 137 } |
| 138 mock_peer_connection_->AddRemoteStream(stream); |
| 139 return stream; |
| 140 } |
| 141 |
| 142 scoped_ptr<WebKit::MockWebRTCPeerConnectionHandlerClient> mock_client_; |
| 143 scoped_ptr<MockMediaStreamDependencyFactory> mock_dependency_factory_; |
| 144 scoped_ptr<RTCPeerConnectionHandlerUnderTest> pc_handler_; |
| 145 |
| 146 // Weak reference to the mocked native peer connection implementation. |
| 147 webrtc::MockPeerConnectionImpl* mock_peer_connection_; |
| 148 }; |
| 149 |
| 150 TEST_F(RTCPeerConnectionHandlerTest, Initialize) { |
| 151 Initialize("dummy", "dummy_pwd"); |
| 152 } |
| 153 |
| 154 TEST_F(RTCPeerConnectionHandlerTest, CreateOffer) { |
| 155 WebKit::WebRTCSessionDescriptionRequest request; |
| 156 WebKit::WebMediaConstraints options; |
| 157 // TODO(perkj): Can WebKit::WebRTCSessionDescriptionRequest be changed so |
| 158 // the |reqest| requestSucceeded can be tested? Currently the |request| object |
| 159 // can not be initialized from a unit test. |
| 160 EXPECT_FALSE(mock_peer_connection_->created_session_description() != NULL); |
| 161 pc_handler_->createOffer(request, options); |
| 162 EXPECT_TRUE(mock_peer_connection_->created_session_description() != NULL); |
| 163 } |
| 164 |
| 165 TEST_F(RTCPeerConnectionHandlerTest, CreateAnser) { |
| 166 WebKit::WebRTCSessionDescriptionRequest request; |
| 167 WebKit::WebMediaConstraints options; |
| 168 // TODO(perkj): Can WebKit::WebRTCSessionDescriptionRequest be changed so |
| 169 // the |reqest| requestSucceeded can be tested? Currently the |request| object |
| 170 // can not be initialized from a unit test. |
| 171 EXPECT_FALSE(mock_peer_connection_->created_session_description() != NULL); |
| 172 pc_handler_->createAnswer(request, options); |
| 173 EXPECT_TRUE(mock_peer_connection_->created_session_description() != NULL); |
| 174 } |
| 175 |
| 176 TEST_F(RTCPeerConnectionHandlerTest, setLocalDescription) { |
| 177 WebKit::WebRTCVoidRequest request; |
| 178 WebKit::WebRTCSessionDescriptionDescriptor description; |
| 179 description.initialize(kDummySdpType, kDummySdp); |
| 180 pc_handler_->setLocalDescription(request, description); |
| 181 EXPECT_EQ(description.type(), pc_handler_->localDescription().type()); |
| 182 EXPECT_EQ(description.sdp(), pc_handler_->localDescription().sdp()); |
| 183 |
| 184 std::string sdp_string; |
| 185 ASSERT_TRUE(mock_peer_connection_->local_description() != NULL); |
| 186 EXPECT_EQ(kDummySdpType, mock_peer_connection_->local_description()->type()); |
| 187 mock_peer_connection_->local_description()->ToString(&sdp_string); |
| 188 EXPECT_EQ(kDummySdp, sdp_string); |
| 189 } |
| 190 |
| 191 TEST_F(RTCPeerConnectionHandlerTest, setRemoteDescription) { |
| 192 WebKit::WebRTCVoidRequest request; |
| 193 WebKit::WebRTCSessionDescriptionDescriptor description; |
| 194 description.initialize(kDummySdpType, kDummySdp); |
| 195 pc_handler_->setRemoteDescription(request, description); |
| 196 EXPECT_EQ(description.type(), pc_handler_->remoteDescription().type()); |
| 197 EXPECT_EQ(description.sdp(), pc_handler_->remoteDescription().sdp()); |
| 198 |
| 199 std::string sdp_string; |
| 200 ASSERT_TRUE(mock_peer_connection_->remote_description() != NULL); |
| 201 EXPECT_EQ(kDummySdpType, mock_peer_connection_->remote_description()->type()); |
| 202 mock_peer_connection_->remote_description()->ToString(&sdp_string); |
| 203 EXPECT_EQ(kDummySdp, sdp_string); |
| 204 } |
| 205 |
| 206 TEST_F(RTCPeerConnectionHandlerTest, updateICE) { |
| 207 WebKit::WebRTCConfiguration config; |
| 208 WebKit::WebMediaConstraints constraints; |
| 209 |
| 210 // TODO(perkj): Test that the parameters in |config| can be translated when a |
| 211 // WebRTCConfiguration can be constructed. It's WebKit class and can't be |
| 212 // initialized from a test. |
| 213 EXPECT_TRUE(pc_handler_->updateICE(config, constraints)); |
| 214 } |
| 215 |
| 216 TEST_F(RTCPeerConnectionHandlerTest, addICECandidate) { |
| 217 WebKit::WebRTCICECandidateDescriptor candidate; |
| 218 candidate.initialize(kDummySdp, "mid", 1); |
| 219 EXPECT_TRUE(pc_handler_->addICECandidate(candidate)); |
| 220 EXPECT_EQ(kDummySdp, mock_peer_connection_->ice_sdp()); |
| 221 EXPECT_EQ(1, mock_peer_connection_->sdp_mline_index()); |
| 222 EXPECT_EQ("mid", mock_peer_connection_->sdp_mid()); |
| 223 } |
| 224 |
| 225 TEST_F(RTCPeerConnectionHandlerTest, addAndRemoveStream) { |
| 226 std::string stream_label = "local_stream"; |
| 227 WebKit::WebMediaStreamDescriptor local_stream( |
| 228 CreateLocalMediaStream(stream_label)); |
| 229 WebKit::WebMediaConstraints constraints; |
| 230 |
| 231 EXPECT_TRUE(pc_handler_->addStream(local_stream, constraints)); |
| 232 EXPECT_EQ(stream_label, mock_peer_connection_->stream_label()); |
| 233 EXPECT_EQ(1u, |
| 234 mock_peer_connection_->local_streams()->at(0)->audio_tracks()->count()); |
| 235 EXPECT_EQ(1u, |
| 236 mock_peer_connection_->local_streams()->at(0)->video_tracks()->count()); |
| 237 |
| 238 pc_handler_->removeStream(local_stream); |
| 239 EXPECT_EQ(0u, mock_peer_connection_->local_streams()->count()); |
| 240 } |
| 241 |
| 242 TEST_F(RTCPeerConnectionHandlerTest, OnStateChange) { |
| 243 // Ready states. |
| 244 webrtc::PeerConnectionObserver::StateType state = |
| 245 webrtc::PeerConnectionObserver::kReadyState; |
| 246 mock_peer_connection_->SetReadyState( |
| 247 webrtc::PeerConnectionInterface::kOpening); |
| 248 pc_handler_->OnStateChange(state); |
| 249 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ReadyStateOpening, |
| 250 mock_client_->ready_state()); |
| 251 mock_peer_connection_->SetReadyState( |
| 252 webrtc::PeerConnectionInterface::kActive); |
| 253 pc_handler_->OnStateChange(state); |
| 254 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ReadyStateActive, |
| 255 mock_client_->ready_state()); |
| 256 mock_peer_connection_->SetReadyState( |
| 257 webrtc::PeerConnectionInterface::kClosing); |
| 258 pc_handler_->OnStateChange(state); |
| 259 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ReadyStateClosing, |
| 260 mock_client_->ready_state()); |
| 261 mock_peer_connection_->SetReadyState( |
| 262 webrtc::PeerConnectionInterface::kClosed); |
| 263 pc_handler_->OnStateChange(state); |
| 264 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ReadyStateClosed, |
| 265 mock_client_->ready_state()); |
| 266 |
| 267 // Ice states. |
| 268 state = webrtc::PeerConnectionObserver::kIceState; |
| 269 mock_peer_connection_->SetIceState( |
| 270 webrtc::PeerConnectionInterface::kIceGathering); |
| 271 pc_handler_->OnStateChange(state); |
| 272 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ICEStateGathering, |
| 273 mock_client_->ice_state()); |
| 274 mock_peer_connection_->SetIceState( |
| 275 webrtc::PeerConnectionInterface::kIceWaiting); |
| 276 pc_handler_->OnStateChange(state); |
| 277 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ICEStateWaiting, |
| 278 mock_client_->ice_state()); |
| 279 mock_peer_connection_->SetIceState( |
| 280 webrtc::PeerConnectionInterface::kIceChecking); |
| 281 pc_handler_->OnStateChange(state); |
| 282 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ICEStateChecking, |
| 283 mock_client_->ice_state()); |
| 284 mock_peer_connection_->SetIceState( |
| 285 webrtc::PeerConnectionInterface::kIceConnected); |
| 286 pc_handler_->OnStateChange(state); |
| 287 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ICEStateConnected, |
| 288 mock_client_->ice_state()); |
| 289 mock_peer_connection_->SetIceState( |
| 290 webrtc::PeerConnectionInterface::kIceCompleted); |
| 291 pc_handler_->OnStateChange(state); |
| 292 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ICEStateCompleted, |
| 293 mock_client_->ice_state()); |
| 294 mock_peer_connection_->SetIceState( |
| 295 webrtc::PeerConnectionInterface::kIceFailed); |
| 296 pc_handler_->OnStateChange(state); |
| 297 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ICEStateFailed, |
| 298 mock_client_->ice_state()); |
| 299 mock_peer_connection_->SetIceState( |
| 300 webrtc::PeerConnectionInterface::kIceClosed); |
| 301 pc_handler_->OnStateChange(state); |
| 302 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ICEStateClosed, |
| 303 mock_client_->ice_state()); |
| 304 } |
| 305 |
| 306 TEST_F(RTCPeerConnectionHandlerTest, OnAddAndOnRemoveStream) { |
| 307 std::string remote_stream_label("remote_stream"); |
| 308 talk_base::scoped_refptr<webrtc::MediaStreamInterface> remote_stream( |
| 309 AddRemoteMockMediaStream(remote_stream_label, "video", "audio")); |
| 310 pc_handler_->OnAddStream(remote_stream); |
| 311 EXPECT_EQ(remote_stream_label, mock_client_->stream_label()); |
| 312 |
| 313 pc_handler_->OnRemoveStream(remote_stream); |
| 314 EXPECT_TRUE(mock_client_->stream_label().empty()); |
| 315 } |
| 316 |
| 317 TEST_F(RTCPeerConnectionHandlerTest, OnIceCandidateAndOnIceComplete) { |
| 318 scoped_ptr<webrtc::IceCandidateInterface> native_candidate( |
| 319 mock_dependency_factory_->CreateIceCandidate("mid", 1, kDummySdp)); |
| 320 pc_handler_->OnIceCandidate(native_candidate.get()); |
| 321 EXPECT_EQ("mid", mock_client_->candidate_mid()); |
| 322 EXPECT_EQ(1, mock_client_->candidate_mlineindex()); |
| 323 EXPECT_EQ(kDummySdp, mock_client_->candidate_sdp()); |
| 324 |
| 325 pc_handler_->OnIceComplete(); |
| 326 EXPECT_EQ("", mock_client_->candidate_mid()); |
| 327 EXPECT_EQ(-1, mock_client_->candidate_mlineindex()); |
| 328 EXPECT_EQ("", mock_client_->candidate_sdp()); |
| 329 } |
| 330 |
| 331 TEST_F(RTCPeerConnectionHandlerTest, OnRenegotiationNeeded) { |
| 332 EXPECT_FALSE(mock_client_->renegotiate()); |
| 333 pc_handler_->OnRenegotiationNeeded(); |
| 334 EXPECT_TRUE(mock_client_->renegotiate()); |
| 335 } |
OLD | NEW |