Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(377)

Side by Side Diff: content/renderer/media/rtc_peer_connection_handler_unittest.cc

Issue 10703095: New PeerConnection handler in Chrome to support latest PeerConnection draft (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Readd the UMA histogram for Deprecated PeerConnection to not screw up the stats. Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/WebRTCICECandidate. h"
21 #include "third_party/WebKit/Source/Platform/chromium/public/WebRTCPeerConnectio nHandlerClient.h"
22 #include "third_party/WebKit/Source/Platform/chromium/public/WebRTCSessionDescri ption.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(new MockMediaStreamDependencyFactory());
52 mock_dependency_factory_->EnsurePeerConnectionFactory();
53 pc_handler_.reset(
54 new RTCPeerConnectionHandlerUnderTest(mock_client_.get(),
55 mock_dependency_factory_.get()));
56
57 WebKit::WebRTCConfiguration config;
58 WebKit::WebMediaConstraints constraints;
59 EXPECT_TRUE(pc_handler_->initialize(config, constraints));
60
61 mock_peer_connection_ = pc_handler_->native_peer_connection();
62 ASSERT_TRUE(mock_peer_connection_);
63 }
64
65 void Initialize(const std::string& server, const std::string& password) {
66 WebKit::WebRTCConfiguration config;
67 WebKit::WebMediaConstraints constraints;
68
69 // TODO(perkj): Test that the parameters in |config| can be translated when
70 // a WebRTCConfiguration can be constructed. It's WebKit class and can't be
71 // initialized from a test.
72 EXPECT_TRUE(pc_handler_->initialize(config, constraints));
73
74 mock_peer_connection_ = pc_handler_->native_peer_connection();
75 ASSERT_TRUE(mock_peer_connection_);
76 }
77
78 // Creates a WebKit local MediaStream.
79 WebKit::WebMediaStreamDescriptor CreateLocalMediaStream(
80 const std::string& stream_label) {
81 std::string video_track_label("video-label");
82 std::string audio_track_label("audio-label");
83
84 scoped_refptr<webrtc::LocalMediaStreamInterface> native_stream(
85 mock_dependency_factory_->CreateLocalMediaStream(stream_label));
86 scoped_refptr<webrtc::LocalAudioTrackInterface> audio_track(
87 mock_dependency_factory_->CreateLocalAudioTrack(audio_track_label,
88 NULL));
89 native_stream->AddTrack(audio_track);
90 scoped_refptr<webrtc::LocalVideoTrackInterface> video_track(
91 mock_dependency_factory_->CreateLocalVideoTrack(video_track_label, 0));
92 native_stream->AddTrack(video_track);
93
94 WebKit::WebVector<WebKit::WebMediaStreamSource> audio_sources(
95 static_cast<size_t>(1));
96 audio_sources[0].initialize(WebKit::WebString::fromUTF8(video_track_label),
97 WebKit::WebMediaStreamSource::TypeAudio,
98 WebKit::WebString::fromUTF8("audio_track"));
99 WebKit::WebVector<WebKit::WebMediaStreamSource> video_sources(
100 static_cast<size_t>(1));
101 video_sources[0].initialize(WebKit::WebString::fromUTF8(video_track_label),
102 WebKit::WebMediaStreamSource::TypeVideo,
103 WebKit::WebString::fromUTF8("video_track"));
104 WebKit::WebMediaStreamDescriptor local_stream;
105 local_stream.initialize(UTF8ToUTF16(stream_label), audio_sources,
106 video_sources);
107 local_stream.setExtraData(new MediaStreamExtraData(native_stream));
108 return local_stream;
109 }
110
111 // Creates a remote MediaStream and adds it to the mocked native
112 // peer connection.
113 scoped_refptr<webrtc::MediaStreamInterface>
114 AddRemoteMockMediaStream(const std::string& stream_label,
115 const std::string& video_track_label,
116 const std::string& audio_track_label) {
117 // We use a local stream as a remote since for testing purposes we really
118 // only need the MediaStreamInterface.
119 scoped_refptr<webrtc::LocalMediaStreamInterface> stream(
120 mock_dependency_factory_->CreateLocalMediaStream(stream_label));
121 if (!video_track_label.empty()) {
122 scoped_refptr<webrtc::LocalVideoTrackInterface> video_track(
123 mock_dependency_factory_->CreateLocalVideoTrack(video_track_label,
124 0));
125 stream->AddTrack(video_track);
126 }
127 if (!audio_track_label.empty()) {
128 scoped_refptr<webrtc::LocalAudioTrackInterface> audio_track(
129 mock_dependency_factory_->CreateLocalAudioTrack(audio_track_label,
130 NULL));
131 stream->AddTrack(audio_track);
132 }
133 mock_peer_connection_->AddRemoteStream(stream);
134 return stream;
135 }
136
137 scoped_ptr<WebKit::MockWebRTCPeerConnectionHandlerClient> mock_client_;
138 scoped_ptr<MockMediaStreamDependencyFactory> mock_dependency_factory_;
139 scoped_ptr<RTCPeerConnectionHandlerUnderTest> pc_handler_;
140
141 // Weak reference to the mocked native peer connection implementation.
142 webrtc::MockPeerConnectionImpl* mock_peer_connection_;
143 };
144
145 TEST_F(RTCPeerConnectionHandlerTest, Initialize) {
146 Initialize("dummy", "dummy_pwd");
147 }
148
149 TEST_F(RTCPeerConnectionHandlerTest, CreateOffer) {
150 WebKit::WebRTCSessionDescriptionRequest request;
151 WebKit::WebMediaConstraints options;
152 // TODO(perkj): Can WebKit::WebRTCSessionDescriptionRequest be changed so
153 // the |reqest| requestSucceeded can be tested? Currently the |request| object
154 // can not be initialized from a unit test.
155 EXPECT_FALSE(mock_peer_connection_->created_session_description() != NULL);
156 pc_handler_->createOffer(request, options);
157 EXPECT_TRUE(mock_peer_connection_->created_session_description() != NULL);
158 }
159
160 TEST_F(RTCPeerConnectionHandlerTest, CreateAnser) {
161 WebKit::WebRTCSessionDescriptionRequest request;
162 WebKit::WebMediaConstraints options;
163 // TODO(perkj): Can WebKit::WebRTCSessionDescriptionRequest be changed so
164 // the |reqest| requestSucceeded can be tested? Currently the |request| object
165 // can not be initialized from a unit test.
166 EXPECT_FALSE(mock_peer_connection_->created_session_description() != NULL);
167 pc_handler_->createAnswer(request, options);
168 EXPECT_TRUE(mock_peer_connection_->created_session_description() != NULL);
169 }
170
171 TEST_F(RTCPeerConnectionHandlerTest, setLocalDescription) {
172 WebKit::WebRTCVoidRequest request;
173 WebKit::WebRTCSessionDescription description;
174 description.initialize(kDummySdpType, kDummySdp);
175 pc_handler_->setLocalDescription(request, description);
176 EXPECT_EQ(description.type(), pc_handler_->localDescription().type());
177 EXPECT_EQ(description.sdp(), pc_handler_->localDescription().sdp());
178
179 std::string sdp_string;
180 ASSERT_TRUE(mock_peer_connection_->local_description() != NULL);
181 EXPECT_EQ(kDummySdpType, mock_peer_connection_->local_description()->type());
182 mock_peer_connection_->local_description()->ToString(&sdp_string);
183 EXPECT_EQ(kDummySdp, sdp_string);
184 }
185
186 TEST_F(RTCPeerConnectionHandlerTest, setRemoteDescription) {
187 WebKit::WebRTCVoidRequest request;
188 WebKit::WebRTCSessionDescription description;
189 description.initialize(kDummySdpType, kDummySdp);
190 pc_handler_->setRemoteDescription(request, description);
191 EXPECT_EQ(description.type(), pc_handler_->remoteDescription().type());
192 EXPECT_EQ(description.sdp(), pc_handler_->remoteDescription().sdp());
193
194 std::string sdp_string;
195 ASSERT_TRUE(mock_peer_connection_->remote_description() != NULL);
196 EXPECT_EQ(kDummySdpType, mock_peer_connection_->remote_description()->type());
197 mock_peer_connection_->remote_description()->ToString(&sdp_string);
198 EXPECT_EQ(kDummySdp, sdp_string);
199 }
200
201 TEST_F(RTCPeerConnectionHandlerTest, updateICE) {
202 WebKit::WebRTCConfiguration config;
203 WebKit::WebMediaConstraints constraints;
204
205 // TODO(perkj): Test that the parameters in |config| can be translated when a
206 // WebRTCConfiguration can be constructed. It's WebKit class and can't be
207 // initialized from a test.
208 EXPECT_TRUE(pc_handler_->updateICE(config, constraints));
209 }
210
211 TEST_F(RTCPeerConnectionHandlerTest, addICECandidate) {
212 WebKit::WebRTCICECandidate candidate;
213 candidate.initialize(kDummySdp, "mid", 1);
214 EXPECT_TRUE(pc_handler_->addICECandidate(candidate));
215 EXPECT_EQ(kDummySdp, mock_peer_connection_->ice_sdp());
216 EXPECT_EQ(1, mock_peer_connection_->sdp_mline_index());
217 EXPECT_EQ("mid", mock_peer_connection_->sdp_mid());
218 }
219
220 TEST_F(RTCPeerConnectionHandlerTest, addAndRemoveStream) {
221 std::string stream_label = "local_stream";
222 WebKit::WebMediaStreamDescriptor local_stream(
223 CreateLocalMediaStream(stream_label));
224 WebKit::WebMediaConstraints constraints;
225
226 EXPECT_TRUE(pc_handler_->addStream(local_stream, constraints));
227 EXPECT_EQ(stream_label, mock_peer_connection_->stream_label());
228 EXPECT_EQ(1u,
229 mock_peer_connection_->local_streams()->at(0)->audio_tracks()->count());
230 EXPECT_EQ(1u,
231 mock_peer_connection_->local_streams()->at(0)->video_tracks()->count());
232
233 pc_handler_->removeStream(local_stream);
234 EXPECT_EQ(0u, mock_peer_connection_->local_streams()->count());
235 }
236
237 TEST_F(RTCPeerConnectionHandlerTest, OnStateChange) {
238 // Ready states.
239 webrtc::PeerConnectionObserver::StateType state =
240 webrtc::PeerConnectionObserver::kReadyState;
241 mock_peer_connection_->SetReadyState(
242 webrtc::PeerConnectionInterface::kOpening);
243 pc_handler_->OnStateChange(state);
244 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ReadyStateOpening,
245 mock_client_->ready_state());
246 mock_peer_connection_->SetReadyState(
247 webrtc::PeerConnectionInterface::kActive);
248 pc_handler_->OnStateChange(state);
249 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ReadyStateActive,
250 mock_client_->ready_state());
251 mock_peer_connection_->SetReadyState(
252 webrtc::PeerConnectionInterface::kClosing);
253 pc_handler_->OnStateChange(state);
254 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ReadyStateClosing,
255 mock_client_->ready_state());
256 mock_peer_connection_->SetReadyState(
257 webrtc::PeerConnectionInterface::kClosed);
258 pc_handler_->OnStateChange(state);
259 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ReadyStateClosed,
260 mock_client_->ready_state());
261
262 // Ice states.
263 state = webrtc::PeerConnectionObserver::kIceState;
264 mock_peer_connection_->SetIceState(
265 webrtc::PeerConnectionInterface::kIceGathering);
266 pc_handler_->OnStateChange(state);
267 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ICEStateGathering,
268 mock_client_->ice_state());
269 mock_peer_connection_->SetIceState(
270 webrtc::PeerConnectionInterface::kIceWaiting);
271 pc_handler_->OnStateChange(state);
272 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ICEStateWaiting,
273 mock_client_->ice_state());
274 mock_peer_connection_->SetIceState(
275 webrtc::PeerConnectionInterface::kIceChecking);
276 pc_handler_->OnStateChange(state);
277 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ICEStateChecking,
278 mock_client_->ice_state());
279 mock_peer_connection_->SetIceState(
280 webrtc::PeerConnectionInterface::kIceConnected);
281 pc_handler_->OnStateChange(state);
282 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ICEStateConnected,
283 mock_client_->ice_state());
284 mock_peer_connection_->SetIceState(
285 webrtc::PeerConnectionInterface::kIceCompleted);
286 pc_handler_->OnStateChange(state);
287 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ICEStateCompleted,
288 mock_client_->ice_state());
289 mock_peer_connection_->SetIceState(
290 webrtc::PeerConnectionInterface::kIceFailed);
291 pc_handler_->OnStateChange(state);
292 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ICEStateFailed,
293 mock_client_->ice_state());
294 mock_peer_connection_->SetIceState(
295 webrtc::PeerConnectionInterface::kIceClosed);
296 pc_handler_->OnStateChange(state);
297 EXPECT_EQ(WebKit::WebRTCPeerConnectionHandlerClient::ICEStateClosed,
298 mock_client_->ice_state());
299 }
300
301 TEST_F(RTCPeerConnectionHandlerTest, OnAddAndOnRemoveStream) {
302 std::string remote_stream_label("remote_stream");
303 scoped_refptr<webrtc::MediaStreamInterface> remote_stream(
304 AddRemoteMockMediaStream(remote_stream_label, "video", "audio"));
305 pc_handler_->OnAddStream(remote_stream);
306 EXPECT_EQ(remote_stream_label, mock_client_->stream_label());
307
308 pc_handler_->OnRemoveStream(remote_stream);
309 EXPECT_TRUE(mock_client_->stream_label().empty());
310 }
311
312 TEST_F(RTCPeerConnectionHandlerTest, OnIceCandidateAndOnIceComplete) {
313 scoped_ptr<webrtc::IceCandidateInterface> native_candidate(
314 mock_dependency_factory_->CreateIceCandidate("mid", 1, kDummySdp));
315 pc_handler_->OnIceCandidate(native_candidate.get());
316 EXPECT_EQ("mid", mock_client_->candidate_mid());
317 EXPECT_EQ(1, mock_client_->candidate_mlineindex());
318 EXPECT_EQ(kDummySdp, mock_client_->candidate_sdp());
319
320 pc_handler_->OnIceComplete();
321 EXPECT_EQ("", mock_client_->candidate_mid());
322 EXPECT_EQ(-1, mock_client_->candidate_mlineindex());
323 EXPECT_EQ("", mock_client_->candidate_sdp());
324 }
325
326 TEST_F(RTCPeerConnectionHandlerTest, OnRenegotiationNeeded) {
327 EXPECT_FALSE(mock_client_->renegotiate());
328 pc_handler_->OnRenegotiationNeeded();
329 EXPECT_TRUE(mock_client_->renegotiate());
330 }
OLDNEW
« no previous file with comments | « content/renderer/media/rtc_peer_connection_handler.cc ('k') | content/renderer/media/webrtc_uma_histograms.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698