OLD | NEW |
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 "content/renderer/media/user_media_client_impl.h" | 5 #include "content/renderer/media/user_media_client_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 UserMediaClientImplUnderTest( | 55 UserMediaClientImplUnderTest( |
56 PeerConnectionDependencyFactory* dependency_factory, | 56 PeerConnectionDependencyFactory* dependency_factory, |
57 std::unique_ptr<MediaStreamDispatcher> media_stream_dispatcher) | 57 std::unique_ptr<MediaStreamDispatcher> media_stream_dispatcher) |
58 : UserMediaClientImpl(NULL, | 58 : UserMediaClientImpl(NULL, |
59 dependency_factory, | 59 dependency_factory, |
60 std::move(media_stream_dispatcher)), | 60 std::move(media_stream_dispatcher)), |
61 state_(REQUEST_NOT_STARTED), | 61 state_(REQUEST_NOT_STARTED), |
62 result_(NUM_MEDIA_REQUEST_RESULTS), | 62 result_(NUM_MEDIA_REQUEST_RESULTS), |
63 result_name_(""), | 63 result_name_(""), |
64 factory_(dependency_factory), | 64 factory_(dependency_factory), |
| 65 create_source_that_fails_(false), |
65 video_source_(NULL) {} | 66 video_source_(NULL) {} |
66 | 67 |
67 void RequestUserMedia(const blink::WebUserMediaRequest& user_media_request) { | 68 void RequestUserMedia(const blink::WebUserMediaRequest& user_media_request) { |
68 state_ = REQUEST_NOT_COMPLETE; | 69 state_ = REQUEST_NOT_COMPLETE; |
69 requestUserMedia(user_media_request); | 70 requestUserMedia(user_media_request); |
70 } | 71 } |
71 | 72 |
72 void RequestUserMedia() { | 73 void RequestUserMedia() { |
73 blink::WebUserMediaRequest user_media_request; | 74 blink::WebUserMediaRequest user_media_request; |
74 RequestUserMedia(user_media_request); | 75 RequestUserMedia(user_media_request); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 last_devices_ = devices; | 111 last_devices_ = devices; |
111 } | 112 } |
112 | 113 |
113 void EnumerateSourcesSucceded( | 114 void EnumerateSourcesSucceded( |
114 blink::WebMediaStreamTrackSourcesRequest* request, | 115 blink::WebMediaStreamTrackSourcesRequest* request, |
115 blink::WebVector<blink::WebSourceInfo>& sources) override { | 116 blink::WebVector<blink::WebSourceInfo>& sources) override { |
116 state_ = REQUEST_SUCCEEDED; | 117 state_ = REQUEST_SUCCEEDED; |
117 last_sources_ = sources; | 118 last_sources_ = sources; |
118 } | 119 } |
119 | 120 |
| 121 void SetCreateSourceThatFails(bool should_fail) { |
| 122 create_source_that_fails_ = should_fail; |
| 123 } |
| 124 |
| 125 MediaStreamAudioSource* CreateAudioSource( |
| 126 const StreamDeviceInfo& device, |
| 127 const blink::WebMediaConstraints& constraints) override { |
| 128 MediaStreamAudioSource* source; |
| 129 if (create_source_that_fails_) { |
| 130 class FailedAtLifeAudioSource : public MediaStreamAudioSource { |
| 131 public: |
| 132 FailedAtLifeAudioSource() : MediaStreamAudioSource(true) {} |
| 133 ~FailedAtLifeAudioSource() override {} |
| 134 protected: |
| 135 bool EnsureSourceIsStarted() override { |
| 136 return false; |
| 137 } |
| 138 }; |
| 139 source = new FailedAtLifeAudioSource(); |
| 140 } else { |
| 141 source = new MediaStreamAudioSource(true); |
| 142 } |
| 143 source->SetDeviceInfo(device); |
| 144 return source; |
| 145 } |
| 146 |
120 MediaStreamVideoSource* CreateVideoSource( | 147 MediaStreamVideoSource* CreateVideoSource( |
121 const StreamDeviceInfo& device, | 148 const StreamDeviceInfo& device, |
122 const MediaStreamSource::SourceStoppedCallback& stop_callback) override { | 149 const MediaStreamSource::SourceStoppedCallback& stop_callback) override { |
123 video_source_ = new MockMediaStreamVideoCapturerSource(device, | 150 video_source_ = new MockMediaStreamVideoCapturerSource(device, |
124 stop_callback, | 151 stop_callback, |
125 factory_); | 152 factory_); |
126 return video_source_; | 153 return video_source_; |
127 } | 154 } |
128 | 155 |
129 const blink::WebMediaStream& last_generated_stream() { | 156 const blink::WebMediaStream& last_generated_stream() { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 } | 190 } |
164 | 191 |
165 private: | 192 private: |
166 blink::WebMediaStream last_generated_stream_; | 193 blink::WebMediaStream last_generated_stream_; |
167 RequestState state_; | 194 RequestState state_; |
168 content::MediaStreamRequestResult result_; | 195 content::MediaStreamRequestResult result_; |
169 blink::WebString result_name_; | 196 blink::WebString result_name_; |
170 blink::WebVector<blink::WebMediaDeviceInfo> last_devices_; | 197 blink::WebVector<blink::WebMediaDeviceInfo> last_devices_; |
171 blink::WebVector<blink::WebSourceInfo> last_sources_; | 198 blink::WebVector<blink::WebSourceInfo> last_sources_; |
172 PeerConnectionDependencyFactory* factory_; | 199 PeerConnectionDependencyFactory* factory_; |
| 200 bool create_source_that_fails_; |
173 MockMediaStreamVideoCapturerSource* video_source_; | 201 MockMediaStreamVideoCapturerSource* video_source_; |
174 }; | 202 }; |
175 | 203 |
176 class UserMediaClientImplTest : public ::testing::Test { | 204 class UserMediaClientImplTest : public ::testing::Test { |
177 public: | 205 public: |
178 void SetUp() override { | 206 void SetUp() override { |
179 // Create our test object. | 207 // Create our test object. |
180 child_process_.reset(new ChildProcess()); | 208 child_process_.reset(new ChildProcess()); |
181 dependency_factory_.reset(new MockPeerConnectionDependencyFactory()); | 209 dependency_factory_.reset(new MockPeerConnectionDependencyFactory()); |
182 ms_dispatcher_ = new MockMediaStreamDispatcher(); | 210 ms_dispatcher_ = new MockMediaStreamDispatcher(); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 } | 283 } |
256 | 284 |
257 void FailToStartMockedVideoSource() { | 285 void FailToStartMockedVideoSource() { |
258 MockMediaStreamVideoCapturerSource* video_source = | 286 MockMediaStreamVideoCapturerSource* video_source = |
259 used_media_impl_->last_created_video_source(); | 287 used_media_impl_->last_created_video_source(); |
260 if (video_source->SourceHasAttemptedToStart()) | 288 if (video_source->SourceHasAttemptedToStart()) |
261 video_source->FailToStartMockedSource(); | 289 video_source->FailToStartMockedSource(); |
262 blink::WebHeap::collectGarbageForTesting(); | 290 blink::WebHeap::collectGarbageForTesting(); |
263 } | 291 } |
264 | 292 |
265 void FailToCreateNextAudioCapturer() { | |
266 dependency_factory_->FailToCreateNextAudioCapturer(); | |
267 blink::WebHeap::collectGarbageForTesting(); | |
268 } | |
269 | |
270 bool AudioRequestHasAutomaticDeviceSelection( | 293 bool AudioRequestHasAutomaticDeviceSelection( |
271 const blink::WebMediaConstraints& audio_constraints) { | 294 const blink::WebMediaConstraints& audio_constraints) { |
272 blink::WebMediaConstraints null_constraints; | 295 blink::WebMediaConstraints null_constraints; |
273 blink::WebUserMediaRequest request = | 296 blink::WebUserMediaRequest request = |
274 blink::WebUserMediaRequest::createForTesting(audio_constraints, | 297 blink::WebUserMediaRequest::createForTesting(audio_constraints, |
275 null_constraints); | 298 null_constraints); |
276 used_media_impl_->RequestUserMedia(request); | 299 used_media_impl_->RequestUserMedia(request); |
277 bool result = used_media_impl_->UserMediaRequestHasAutomaticDeviceSelection( | 300 bool result = used_media_impl_->UserMediaRequestHasAutomaticDeviceSelection( |
278 ms_dispatcher_->audio_input_request_id()); | 301 ms_dispatcher_->audio_input_request_id()); |
279 used_media_impl_->DeleteRequest(ms_dispatcher_->audio_input_request_id()); | 302 used_media_impl_->DeleteRequest(ms_dispatcher_->audio_input_request_id()); |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 EXPECT_EQ(MEDIA_DEVICE_TRACK_START_FAILURE, | 459 EXPECT_EQ(MEDIA_DEVICE_TRACK_START_FAILURE, |
437 used_media_impl_->error_reason()); | 460 used_media_impl_->error_reason()); |
438 blink::WebHeap::collectAllGarbageForTesting(); | 461 blink::WebHeap::collectAllGarbageForTesting(); |
439 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter()); | 462 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter()); |
440 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter()); | 463 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter()); |
441 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter()); | 464 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter()); |
442 } | 465 } |
443 | 466 |
444 // This test what happens if an audio source fail to initialize. | 467 // This test what happens if an audio source fail to initialize. |
445 TEST_F(UserMediaClientImplTest, MediaAudioSourceFailToInitialize) { | 468 TEST_F(UserMediaClientImplTest, MediaAudioSourceFailToInitialize) { |
446 FailToCreateNextAudioCapturer(); | 469 used_media_impl_->SetCreateSourceThatFails(true); |
447 used_media_impl_->RequestUserMedia(); | 470 used_media_impl_->RequestUserMedia(); |
448 FakeMediaStreamDispatcherRequestUserMediaComplete(); | 471 FakeMediaStreamDispatcherRequestUserMediaComplete(); |
449 StartMockedVideoSource(); | 472 StartMockedVideoSource(); |
450 EXPECT_EQ(UserMediaClientImplUnderTest::REQUEST_FAILED, | 473 EXPECT_EQ(UserMediaClientImplUnderTest::REQUEST_FAILED, |
451 used_media_impl_->request_state()); | 474 used_media_impl_->request_state()); |
452 EXPECT_EQ(MEDIA_DEVICE_TRACK_START_FAILURE, | 475 EXPECT_EQ(MEDIA_DEVICE_TRACK_START_FAILURE, |
453 used_media_impl_->error_reason()); | 476 used_media_impl_->error_reason()); |
454 blink::WebHeap::collectAllGarbageForTesting(); | 477 blink::WebHeap::collectAllGarbageForTesting(); |
455 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter()); | 478 EXPECT_EQ(1, ms_dispatcher_->request_stream_counter()); |
456 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter()); | 479 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter()); |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
658 // Now we close the web frame, if in the above Stop() call, | 681 // Now we close the web frame, if in the above Stop() call, |
659 // UserMediaClientImpl accidentally removed audio track, then video track will | 682 // UserMediaClientImpl accidentally removed audio track, then video track will |
660 // be removed again here, which is incorrect. | 683 // be removed again here, which is incorrect. |
661 used_media_impl_->FrameWillClose(); | 684 used_media_impl_->FrameWillClose(); |
662 blink::WebHeap::collectAllGarbageForTesting(); | 685 blink::WebHeap::collectAllGarbageForTesting(); |
663 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter()); | 686 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter()); |
664 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter()); | 687 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter()); |
665 } | 688 } |
666 | 689 |
667 } // namespace content | 690 } // namespace content |
OLD | NEW |