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

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

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