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

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

Issue 287383002: Implement getMediaDevices. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 6 years, 6 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/memory/scoped_ptr.h" 5 #include "base/memory/scoped_ptr.h"
6 #include "base/message_loop/message_loop.h" 6 #include "base/message_loop/message_loop.h"
7 #include "base/strings/utf_string_conversions.h" 7 #include "base/strings/utf_string_conversions.h"
8 #include "content/child/child_process.h" 8 #include "content/child/child_process.h"
9 #include "content/renderer/media/media_stream.h" 9 #include "content/renderer/media/media_stream.h"
10 #include "content/renderer/media/media_stream_impl.h" 10 #include "content/renderer/media/media_stream_impl.h"
11 #include "content/renderer/media/media_stream_track.h" 11 #include "content/renderer/media/media_stream_track.h"
12 #include "content/renderer/media/mock_media_stream_dispatcher.h" 12 #include "content/renderer/media/mock_media_stream_dispatcher.h"
13 #include "content/renderer/media/mock_media_stream_video_source.h" 13 #include "content/renderer/media/mock_media_stream_video_source.h"
14 #include "content/renderer/media/webrtc/mock_peer_connection_dependency_factory. h" 14 #include "content/renderer/media/webrtc/mock_peer_connection_dependency_factory. h"
15 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/WebKit/public/platform/WebMediaDeviceInfo.h"
16 #include "third_party/WebKit/public/platform/WebMediaStream.h" 17 #include "third_party/WebKit/public/platform/WebMediaStream.h"
17 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" 18 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
18 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" 19 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
19 #include "third_party/WebKit/public/platform/WebString.h" 20 #include "third_party/WebKit/public/platform/WebString.h"
20 #include "third_party/WebKit/public/platform/WebVector.h" 21 #include "third_party/WebKit/public/platform/WebVector.h"
21 22
22 namespace content { 23 namespace content {
23 24
24 class MockMediaStreamVideoCapturerSource : public MockMediaStreamVideoSource { 25 class MockMediaStreamVideoCapturerSource : public MockMediaStreamVideoSource {
25 public: 26 public:
(...skipping 24 matching lines...) Expand all
50 factory_(dependency_factory), 51 factory_(dependency_factory),
51 video_source_(NULL) { 52 video_source_(NULL) {
52 } 53 }
53 54
54 void RequestUserMedia() { 55 void RequestUserMedia() {
55 blink::WebUserMediaRequest user_media_request; 56 blink::WebUserMediaRequest user_media_request;
56 state_ = REQUEST_NOT_COMPLETE; 57 state_ = REQUEST_NOT_COMPLETE;
57 requestUserMedia(user_media_request); 58 requestUserMedia(user_media_request);
58 } 59 }
59 60
61 void RequestMediaDevices() {
62 blink::WebMediaDevicesRequest media_devices_request;
63 state_ = REQUEST_NOT_COMPLETE;
64 requestMediaDevices(media_devices_request);
65 }
66
60 virtual void GetUserMediaRequestSucceeded( 67 virtual void GetUserMediaRequestSucceeded(
61 const blink::WebMediaStream& stream, 68 const blink::WebMediaStream& stream,
62 blink::WebUserMediaRequest* request_info) OVERRIDE { 69 blink::WebUserMediaRequest* request_info) OVERRIDE {
63 last_generated_stream_ = stream; 70 last_generated_stream_ = stream;
64 state_ = REQUEST_SUCCEEDED; 71 state_ = REQUEST_SUCCEEDED;
65 } 72 }
66 73
67 virtual void GetUserMediaRequestFailed( 74 virtual void GetUserMediaRequestFailed(
68 blink::WebUserMediaRequest* request_info, 75 blink::WebUserMediaRequest* request_info,
69 content::MediaStreamRequestResult result) OVERRIDE { 76 content::MediaStreamRequestResult result) OVERRIDE {
70 last_generated_stream_.reset(); 77 last_generated_stream_.reset();
71 state_ = REQUEST_FAILED; 78 state_ = REQUEST_FAILED;
72 result_ = result; 79 result_ = result;
73 } 80 }
74 81
82 virtual void EnumerateDevicesSucceded(
83 blink::WebMediaDevicesRequest* request,
84 blink::WebVector<blink::WebMediaDeviceInfo>& devices) OVERRIDE {
85 state_ = REQUEST_SUCCEEDED;
86 last_devices_ = devices;
87 }
88
75 virtual MediaStreamVideoSource* CreateVideoSource( 89 virtual MediaStreamVideoSource* CreateVideoSource(
76 const StreamDeviceInfo& device, 90 const StreamDeviceInfo& device,
77 const MediaStreamSource::SourceStoppedCallback& stop_callback) OVERRIDE { 91 const MediaStreamSource::SourceStoppedCallback& stop_callback) OVERRIDE {
78 video_source_ = new MockMediaStreamVideoCapturerSource(device, 92 video_source_ = new MockMediaStreamVideoCapturerSource(device,
79 stop_callback, 93 stop_callback,
80 factory_); 94 factory_);
81 return video_source_; 95 return video_source_;
82 } 96 }
83 97
84 const blink::WebMediaStream& last_generated_stream() { 98 const blink::WebMediaStream& last_generated_stream() {
85 return last_generated_stream_; 99 return last_generated_stream_;
86 } 100 }
87 101
102 const blink::WebVector<blink::WebMediaDeviceInfo>& last_devices() {
103 return last_devices_;
104 }
105
88 void ClearLastGeneratedStream() { 106 void ClearLastGeneratedStream() {
89 last_generated_stream_.reset(); 107 last_generated_stream_.reset();
90 } 108 }
91 109
92 MockMediaStreamVideoCapturerSource* last_created_video_source() const { 110 MockMediaStreamVideoCapturerSource* last_created_video_source() const {
93 return video_source_; 111 return video_source_;
94 } 112 }
95 113
96 RequestState request_state() const { return state_; } 114 RequestState request_state() const { return state_; }
97 content::MediaStreamRequestResult error_reason() const { return result_; } 115 content::MediaStreamRequestResult error_reason() const { return result_; }
98 116
99 private: 117 private:
100 blink::WebMediaStream last_generated_stream_; 118 blink::WebMediaStream last_generated_stream_;
101 RequestState state_; 119 RequestState state_;
102 content::MediaStreamRequestResult result_; 120 content::MediaStreamRequestResult result_;
121 blink::WebVector<blink::WebMediaDeviceInfo> last_devices_;
103 PeerConnectionDependencyFactory* factory_; 122 PeerConnectionDependencyFactory* factory_;
104 MockMediaStreamVideoCapturerSource* video_source_; 123 MockMediaStreamVideoCapturerSource* video_source_;
105 }; 124 };
106 125
107 class MediaStreamImplTest : public ::testing::Test { 126 class MediaStreamImplTest : public ::testing::Test {
108 public: 127 public:
109 virtual void SetUp() { 128 virtual void SetUp() {
110 // Create our test object. 129 // Create our test object.
111 child_process_.reset(new ChildProcess()); 130 child_process_.reset(new ChildProcess());
112 ms_dispatcher_.reset(new MockMediaStreamDispatcher()); 131 ms_dispatcher_.reset(new MockMediaStreamDispatcher());
113 dependency_factory_.reset(new MockPeerConnectionDependencyFactory()); 132 dependency_factory_.reset(new MockPeerConnectionDependencyFactory());
114 ms_impl_.reset(new MediaStreamImplUnderTest(ms_dispatcher_.get(), 133 ms_impl_.reset(new MediaStreamImplUnderTest(ms_dispatcher_.get(),
115 dependency_factory_.get())); 134 dependency_factory_.get()));
116 } 135 }
117 136
118 blink::WebMediaStream RequestLocalMediaStream() { 137 blink::WebMediaStream RequestLocalMediaStream() {
119 ms_impl_->RequestUserMedia(); 138 ms_impl_->RequestUserMedia();
120 FakeMediaStreamDispatcherComplete(); 139 FakeMediaStreamDispatcherRequestUserMediaComplete();
121 StartMockedVideoSource(); 140 StartMockedVideoSource();
122 141
123 EXPECT_EQ(MediaStreamImplUnderTest::REQUEST_SUCCEEDED, 142 EXPECT_EQ(MediaStreamImplUnderTest::REQUEST_SUCCEEDED,
124 ms_impl_->request_state()); 143 ms_impl_->request_state());
125 144
126 blink::WebMediaStream desc = ms_impl_->last_generated_stream(); 145 blink::WebMediaStream desc = ms_impl_->last_generated_stream();
127 content::MediaStream* native_stream = 146 content::MediaStream* native_stream =
128 content::MediaStream::GetMediaStream(desc); 147 content::MediaStream::GetMediaStream(desc);
129 if (!native_stream) { 148 if (!native_stream) {
130 ADD_FAILURE(); 149 ADD_FAILURE();
131 return desc; 150 return desc;
132 } 151 }
133 152
134 blink::WebVector<blink::WebMediaStreamTrack> audio_tracks; 153 blink::WebVector<blink::WebMediaStreamTrack> audio_tracks;
135 desc.audioTracks(audio_tracks); 154 desc.audioTracks(audio_tracks);
136 blink::WebVector<blink::WebMediaStreamTrack> video_tracks; 155 blink::WebVector<blink::WebMediaStreamTrack> video_tracks;
137 desc.videoTracks(video_tracks); 156 desc.videoTracks(video_tracks);
138 157
139 EXPECT_EQ(1u, audio_tracks.size()); 158 EXPECT_EQ(1u, audio_tracks.size());
140 EXPECT_EQ(1u, video_tracks.size()); 159 EXPECT_EQ(1u, video_tracks.size());
141 EXPECT_NE(audio_tracks[0].id(), video_tracks[0].id()); 160 EXPECT_NE(audio_tracks[0].id(), video_tracks[0].id());
142 return desc; 161 return desc;
143 } 162 }
144 163
145 void FakeMediaStreamDispatcherComplete() { 164 void FakeMediaStreamDispatcherRequestUserMediaComplete() {
146 ms_impl_->OnStreamGenerated(ms_dispatcher_->request_id(), 165 // Audio request ID is used as the shared request ID.
166 ms_impl_->OnStreamGenerated(ms_dispatcher_->audio_request_id(),
147 ms_dispatcher_->stream_label(), 167 ms_dispatcher_->stream_label(),
148 ms_dispatcher_->audio_array(), 168 ms_dispatcher_->audio_array(),
149 ms_dispatcher_->video_array()); 169 ms_dispatcher_->video_array());
150 } 170 }
151 171
172 void FakeMediaStreamDispatcherRequestMediaDevicesComplete() {
173 ms_impl_->OnDevicesEnumerated(ms_dispatcher_->audio_request_id(),
174 ms_dispatcher_->audio_array());
175 ms_impl_->OnDevicesEnumerated(ms_dispatcher_->video_request_id(),
176 ms_dispatcher_->video_array());
177 }
178
152 void StartMockedVideoSource() { 179 void StartMockedVideoSource() {
153 MockMediaStreamVideoCapturerSource* video_source = 180 MockMediaStreamVideoCapturerSource* video_source =
154 ms_impl_->last_created_video_source(); 181 ms_impl_->last_created_video_source();
155 if (video_source->SourceHasAttemptedToStart()) 182 if (video_source->SourceHasAttemptedToStart())
156 video_source->StartMockedSource(); 183 video_source->StartMockedSource();
157 } 184 }
158 185
159 void FailToStartMockedVideoSource() { 186 void FailToStartMockedVideoSource() {
160 MockMediaStreamVideoCapturerSource* video_source = 187 MockMediaStreamVideoCapturerSource* video_source =
161 ms_impl_->last_created_video_source(); 188 ms_impl_->last_created_video_source();
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 // Test that the MediaStreams are deleted if the owning WebFrame is deleted. 337 // Test that the MediaStreams are deleted if the owning WebFrame is deleted.
311 // In the unit test the owning frame is NULL. 338 // In the unit test the owning frame is NULL.
312 ms_impl_->FrameWillClose(NULL); 339 ms_impl_->FrameWillClose(NULL);
313 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter()); 340 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter());
314 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter()); 341 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter());
315 } 342 }
316 343
317 // This test what happens if a video source to a MediaSteam fails to start. 344 // This test what happens if a video source to a MediaSteam fails to start.
318 TEST_F(MediaStreamImplTest, MediaVideoSourceFailToStart) { 345 TEST_F(MediaStreamImplTest, MediaVideoSourceFailToStart) {
319 ms_impl_->RequestUserMedia(); 346 ms_impl_->RequestUserMedia();
320 FakeMediaStreamDispatcherComplete(); 347 FakeMediaStreamDispatcherRequestUserMediaComplete();
321 FailToStartMockedVideoSource(); 348 FailToStartMockedVideoSource();
322 EXPECT_EQ(MediaStreamImplUnderTest::REQUEST_FAILED, 349 EXPECT_EQ(MediaStreamImplUnderTest::REQUEST_FAILED,
323 ms_impl_->request_state()); 350 ms_impl_->request_state());
324 EXPECT_EQ(MEDIA_DEVICE_TRACK_START_FAILURE, 351 EXPECT_EQ(MEDIA_DEVICE_TRACK_START_FAILURE,
325 ms_impl_->error_reason()); 352 ms_impl_->error_reason());
326 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter()); 353 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter());
327 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter()); 354 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter());
328 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter()); 355 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter());
329 } 356 }
330 357
331 // This test what happens if an audio source fail to initialize. 358 // This test what happens if an audio source fail to initialize.
332 TEST_F(MediaStreamImplTest, MediaAudioSourceFailToInitialize) { 359 TEST_F(MediaStreamImplTest, MediaAudioSourceFailToInitialize) {
333 FailToCreateNextAudioCapturer(); 360 FailToCreateNextAudioCapturer();
334 ms_impl_->RequestUserMedia(); 361 ms_impl_->RequestUserMedia();
335 FakeMediaStreamDispatcherComplete(); 362 FakeMediaStreamDispatcherRequestUserMediaComplete();
336 StartMockedVideoSource(); 363 StartMockedVideoSource();
337 EXPECT_EQ(MediaStreamImplUnderTest::REQUEST_FAILED, 364 EXPECT_EQ(MediaStreamImplUnderTest::REQUEST_FAILED,
338 ms_impl_->request_state()); 365 ms_impl_->request_state());
339 EXPECT_EQ(MEDIA_DEVICE_TRACK_START_FAILURE, 366 EXPECT_EQ(MEDIA_DEVICE_TRACK_START_FAILURE,
340 ms_impl_->error_reason()); 367 ms_impl_->error_reason());
341 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter()); 368 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter());
342 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter()); 369 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter());
343 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter()); 370 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter());
344 } 371 }
345 372
346 // This test what happens if MediaStreamImpl is deleted before a source has 373 // This test what happens if MediaStreamImpl is deleted before a source has
347 // started. 374 // started.
348 TEST_F(MediaStreamImplTest, MediaStreamImplShutDown) { 375 TEST_F(MediaStreamImplTest, MediaStreamImplShutDown) {
349 ms_impl_->RequestUserMedia(); 376 ms_impl_->RequestUserMedia();
350 FakeMediaStreamDispatcherComplete(); 377 FakeMediaStreamDispatcherRequestUserMediaComplete();
351 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter()); 378 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter());
352 EXPECT_EQ(MediaStreamImplUnderTest::REQUEST_NOT_COMPLETE, 379 EXPECT_EQ(MediaStreamImplUnderTest::REQUEST_NOT_COMPLETE,
353 ms_impl_->request_state()); 380 ms_impl_->request_state());
354 ms_impl_.reset(); 381 ms_impl_.reset();
355 } 382 }
356 383
357 // This test what happens if the WebFrame is closed while the MediaStream is 384 // This test what happens if the WebFrame is closed while the MediaStream is
358 // being generated by the MediaStreamDispatcher. 385 // being generated by the MediaStreamDispatcher.
359 TEST_F(MediaStreamImplTest, ReloadFrameWhileGeneratingStream) { 386 TEST_F(MediaStreamImplTest, ReloadFrameWhileGeneratingStream) {
360 ms_impl_->RequestUserMedia(); 387 ms_impl_->RequestUserMedia();
361 ms_impl_->FrameWillClose(NULL); 388 ms_impl_->FrameWillClose(NULL);
362 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter()); 389 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter());
363 EXPECT_EQ(0, ms_dispatcher_->stop_audio_device_counter()); 390 EXPECT_EQ(0, ms_dispatcher_->stop_audio_device_counter());
364 EXPECT_EQ(0, ms_dispatcher_->stop_video_device_counter()); 391 EXPECT_EQ(0, ms_dispatcher_->stop_video_device_counter());
365 EXPECT_EQ(MediaStreamImplUnderTest::REQUEST_NOT_COMPLETE, 392 EXPECT_EQ(MediaStreamImplUnderTest::REQUEST_NOT_COMPLETE,
366 ms_impl_->request_state()); 393 ms_impl_->request_state());
367 } 394 }
368 395
369 // This test what happens if the WebFrame is closed while the sources are being 396 // This test what happens if the WebFrame is closed while the sources are being
370 // started. 397 // started.
371 TEST_F(MediaStreamImplTest, ReloadFrameWhileGeneratingSources) { 398 TEST_F(MediaStreamImplTest, ReloadFrameWhileGeneratingSources) {
372 ms_impl_->RequestUserMedia(); 399 ms_impl_->RequestUserMedia();
373 FakeMediaStreamDispatcherComplete(); 400 FakeMediaStreamDispatcherRequestUserMediaComplete();
374 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter()); 401 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter());
375 ms_impl_->FrameWillClose(NULL); 402 ms_impl_->FrameWillClose(NULL);
376 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter()); 403 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter());
377 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter()); 404 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter());
378 EXPECT_EQ(MediaStreamImplUnderTest::REQUEST_NOT_COMPLETE, 405 EXPECT_EQ(MediaStreamImplUnderTest::REQUEST_NOT_COMPLETE,
379 ms_impl_->request_state()); 406 ms_impl_->request_state());
380 } 407 }
381 408
382 // This test what happens if stop is called on a track after the frame has 409 // This test what happens if stop is called on a track after the frame has
383 // been reloaded. 410 // been reloaded.
(...skipping 10 matching lines...) Expand all
394 audio_track->Stop(); 421 audio_track->Stop();
395 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter()); 422 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter());
396 423
397 blink::WebVector<blink::WebMediaStreamTrack> video_tracks; 424 blink::WebVector<blink::WebMediaStreamTrack> video_tracks;
398 mixed_desc.videoTracks(video_tracks); 425 mixed_desc.videoTracks(video_tracks);
399 MediaStreamTrack* video_track = MediaStreamTrack::GetTrack(video_tracks[0]); 426 MediaStreamTrack* video_track = MediaStreamTrack::GetTrack(video_tracks[0]);
400 video_track->Stop(); 427 video_track->Stop();
401 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter()); 428 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter());
402 } 429 }
403 430
431 TEST_F(MediaStreamImplTest, EnumerateMediaDevices) {
432 ms_impl_->RequestMediaDevices();
433 FakeMediaStreamDispatcherRequestMediaDevicesComplete();
434
435 EXPECT_EQ(MediaStreamImplUnderTest::REQUEST_SUCCEEDED,
436 ms_impl_->request_state());
437
438 EXPECT_FALSE(ms_impl_->last_devices()[0].deviceId().isEmpty());
439 EXPECT_EQ(blink::WebMediaDeviceInfo::MediaDeviceKindAudioInput,
440 ms_impl_->last_devices()[0].kind());
441 EXPECT_FALSE(ms_impl_->last_devices()[0].label().isEmpty());
442
443 EXPECT_FALSE(ms_impl_->last_devices()[1].deviceId().isEmpty());
444 EXPECT_EQ(blink::WebMediaDeviceInfo::MediaDeviceKindVideoInput,
445 ms_impl_->last_devices()[1].kind());
446 EXPECT_FALSE(ms_impl_->last_devices()[1].label().isEmpty());
447 }
448
404 } // namespace content 449 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/media/media_stream_impl.cc ('k') | content/renderer/media/mock_media_stream_dispatcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698