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_web_rtc_peer_connection_handler_client.h" | |
12 #include "content/renderer/media/mock_peer_connection_impl.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/WebRTCICECandidateD escriptor.h" | |
17 #include "third_party/WebKit/Source/Platform/chromium/public/WebRTCConfiguration .h" | |
wjia(left Chromium)
2012/08/12 17:06:52
a-z order.
perkj_chrome
2012/08/13 07:35:46
Done.
| |
18 #include "third_party/WebKit/Source/Platform/chromium/public/WebMediaConstraints .h" | |
wjia(left Chromium)
2012/08/12 17:06:52
ditto.
perkj_chrome
2012/08/13 07:35:46
Done.
| |
19 #include "third_party/WebKit/Source/Platform/chromium/public/WebMediaStreamDescr iptor.h" | |
20 #include "third_party/WebKit/Source/Platform/chromium/public/WebMediaStreamSourc e.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())); | |
wjia(left Chromium)
2012/08/12 17:06:52
indent.
perkj_chrome
2012/08/13 07:35:46
Done.
| |
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 pc_handler_->createOffer(request, options); | |
158 } | |
159 | |
160 TEST_F(RTCPeerConnectionHandlerTest, setLocalDescription) { | |
161 WebKit::WebRTCVoidRequest request; | |
162 WebKit::WebRTCSessionDescriptionDescriptor description; | |
163 description.initialize(kDummySdpType, kDummySdp); | |
164 pc_handler_->setLocalDescription(request, description); | |
165 EXPECT_EQ(description.type(), pc_handler_->localDescription().type()); | |
166 EXPECT_EQ(description.sdp(), pc_handler_->localDescription().sdp()); | |
167 | |
168 std::string sdp_string; | |
169 ASSERT_TRUE(mock_peer_connection_->local_description() != NULL); | |
170 EXPECT_EQ(kDummySdpType, mock_peer_connection_->local_description()->type()); | |
171 mock_peer_connection_->local_description()->ToString(&sdp_string); | |
172 EXPECT_EQ(kDummySdp, sdp_string); | |
173 } | |
174 | |
175 TEST_F(RTCPeerConnectionHandlerTest, setRemoteDescription) { | |
176 WebKit::WebRTCVoidRequest request; | |
177 WebKit::WebRTCSessionDescriptionDescriptor description; | |
178 description.initialize(kDummySdpType, kDummySdp); | |
179 pc_handler_->setRemoteDescription(request, description); | |
180 EXPECT_EQ(description.type(), pc_handler_->remoteDescription().type()); | |
181 EXPECT_EQ(description.sdp(), pc_handler_->remoteDescription().sdp()); | |
182 | |
183 std::string sdp_string; | |
184 ASSERT_TRUE(mock_peer_connection_->remote_description() != NULL); | |
185 EXPECT_EQ(kDummySdpType, mock_peer_connection_->remote_description()->type()); | |
186 mock_peer_connection_->remote_description()->ToString(&sdp_string); | |
187 EXPECT_EQ(kDummySdp, sdp_string); | |
188 } | |
189 | |
190 TEST_F(RTCPeerConnectionHandlerTest, updateICE) { | |
191 WebKit::WebRTCConfiguration config; | |
192 WebKit::WebMediaConstraints constraints; | |
193 | |
194 // TODO(perkj): Test that the parameters in |config| can be translated when a | |
195 // WebRTCConfiguration can be constructed. It's WebKit class and can't be | |
196 // initialized from a test. | |
197 EXPECT_TRUE(pc_handler_->updateICE(config, constraints)); | |
198 } | |
199 | |
200 TEST_F(RTCPeerConnectionHandlerTest, addICECandidate) { | |
201 WebKit::WebRTCICECandidateDescriptor candidate; | |
202 candidate.initialize(kDummySdp, "mid", 1); | |
203 EXPECT_TRUE(pc_handler_->addICECandidate(candidate)); | |
204 EXPECT_EQ(kDummySdp, mock_peer_connection_->ice_sdp()); | |
205 EXPECT_EQ(1, mock_peer_connection_->sdp_mline_index()); | |
206 EXPECT_EQ("mid", mock_peer_connection_->sdp_mid()); | |
207 } | |
208 | |
209 TEST_F(RTCPeerConnectionHandlerTest, addAndRemoveStream) { | |
210 std::string stream_label = "local_stream"; | |
211 WebKit::WebMediaStreamDescriptor local_stream( | |
212 CreateLocalMediaStream(stream_label)); | |
213 WebKit::WebMediaConstraints constraints; | |
214 | |
215 EXPECT_TRUE(pc_handler_->addStream(local_stream, constraints)); | |
216 EXPECT_EQ(stream_label, mock_peer_connection_->stream_label()); | |
217 EXPECT_EQ(1u, | |
218 mock_peer_connection_->local_streams()->at(0)->audio_tracks()->count()); | |
219 EXPECT_EQ(1u, | |
220 mock_peer_connection_->local_streams()->at(0)->video_tracks()->count()); | |
221 | |
222 pc_handler_->removeStream(local_stream); | |
223 EXPECT_EQ("", mock_peer_connection_->stream_label()); | |
224 } | |
225 | |
226 TEST_F(RTCPeerConnectionHandlerTest, OnStateChange) { | |
227 webrtc::PeerConnectionObserver::StateType state = | |
228 webrtc::PeerConnectionObserver::kReadyState; | |
229 mock_peer_connection_->SetReadyState( | |
230 webrtc::PeerConnectionInterface::kOpening); | |
231 pc_handler_->OnStateChange(state); | |
232 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ReadyStateOpening, | |
233 mock_client_->ready_state()); | |
234 mock_peer_connection_->SetReadyState( | |
235 webrtc::PeerConnectionInterface::kActive); | |
wjia(left Chromium)
2012/08/12 17:06:52
indent.
perkj_chrome
2012/08/13 07:35:46
Done.
| |
236 pc_handler_->OnStateChange(state); | |
237 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ReadyStateActive, | |
238 mock_client_->ready_state()); | |
239 mock_peer_connection_->SetReadyState( | |
240 webrtc::PeerConnectionInterface::kClosing); | |
241 pc_handler_->OnStateChange(state); | |
242 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ReadyStateClosing, | |
243 mock_client_->ready_state()); | |
244 mock_peer_connection_->SetReadyState( | |
245 webrtc::PeerConnectionInterface::kClosed); | |
wjia(left Chromium)
2012/08/12 17:06:52
indent.
perkj_chrome
2012/08/13 07:35:46
Done.
| |
246 pc_handler_->OnStateChange(state); | |
247 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ReadyStateClosed, | |
248 mock_client_->ready_state()); | |
249 | |
250 // Ice states. | |
251 state = webrtc::PeerConnectionObserver::kIceState; | |
252 mock_peer_connection_->SetIceState( | |
253 webrtc::PeerConnectionInterface::kIceGathering); | |
254 pc_handler_->OnStateChange(state); | |
255 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ICEStateGathering, | |
256 mock_client_->ice_state()); | |
257 mock_peer_connection_->SetIceState( | |
258 webrtc::PeerConnectionInterface::kIceWaiting); | |
259 pc_handler_->OnStateChange(state); | |
260 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ICEStateWaiting, | |
261 mock_client_->ice_state()); | |
262 mock_peer_connection_->SetIceState( | |
263 webrtc::PeerConnectionInterface::kIceChecking); | |
264 pc_handler_->OnStateChange(state); | |
265 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ICEStateChecking, | |
266 mock_client_->ice_state()); | |
267 mock_peer_connection_->SetIceState( | |
268 webrtc::PeerConnectionInterface::kIceConnected); | |
269 pc_handler_->OnStateChange(state); | |
270 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ICEStateConnected, | |
271 mock_client_->ice_state()); | |
272 mock_peer_connection_->SetIceState( | |
273 webrtc::PeerConnectionInterface::kIceCompleted); | |
274 pc_handler_->OnStateChange(state); | |
275 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ICEStateCompleted, | |
276 mock_client_->ice_state()); | |
277 mock_peer_connection_->SetIceState( | |
278 webrtc::PeerConnectionInterface::kIceFailed); | |
279 pc_handler_->OnStateChange(state); | |
280 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ICEStateFailed, | |
281 mock_client_->ice_state()); | |
282 mock_peer_connection_->SetIceState( | |
283 webrtc::PeerConnectionInterface::kIceClosed); | |
284 pc_handler_->OnStateChange(state); | |
285 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ICEStateClosed, | |
286 mock_client_->ice_state()); | |
287 } | |
288 | |
289 TEST_F(RTCPeerConnectionHandlerTest, OnAddAndOnRemoveStream) { | |
290 std::string remote_stream_label("remote_stream"); | |
291 talk_base::scoped_refptr<webrtc::MediaStreamInterface> remote_stream( | |
292 AddRemoteMockMediaStream(remote_stream_label, "video", "audio")); | |
293 pc_handler_->OnAddStream(remote_stream); | |
294 EXPECT_EQ(remote_stream_label, mock_client_->stream_label()); | |
295 | |
296 pc_handler_->OnRemoveStream(remote_stream); | |
297 EXPECT_TRUE(mock_client_->stream_label().empty()); | |
298 } | |
299 | |
300 TEST_F(RTCPeerConnectionHandlerTest, OnIceCandidateAndOnIceComplete) { | |
301 scoped_ptr<webrtc::IceCandidateInterface> native_candidate( | |
302 mock_dependency_factory_->CreateIceCandidate("mid", 1, kDummySdp)); | |
303 pc_handler_->OnIceCandidate(native_candidate.get()); | |
304 EXPECT_EQ("mid", mock_client_->candidate_mid()); | |
305 EXPECT_EQ(1, mock_client_->candidate_mlineindex()); | |
306 EXPECT_EQ(kDummySdp, mock_client_->candidate_sdp()); | |
307 | |
308 pc_handler_->OnIceComplete(); | |
309 EXPECT_EQ("", mock_client_->candidate_mid()); | |
310 EXPECT_EQ(0, mock_client_->candidate_mlineindex()); | |
311 EXPECT_EQ("", mock_client_->candidate_sdp()); | |
312 } | |
313 | |
314 TEST_F(RTCPeerConnectionHandlerTest, OnRenegotiationNeeded) { | |
315 EXPECT_FALSE(mock_client_->renegotiate()); | |
316 pc_handler_->OnRenegotiationNeeded(); | |
317 EXPECT_TRUE(mock_client_->renegotiate()); | |
318 } | |
OLD | NEW |