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

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

Issue 2380793002: Migrate MediaDevices.enumerateDevices to Mojo (Closed)
Patch Set: rebase Created 4 years, 2 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
« no previous file with comments | « content/renderer/media/user_media_client_impl.cc ('k') | content/test/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "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>
11 #include <vector>
11 12
12 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
14 #include "base/run_loop.h"
13 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
14 #include "content/child/child_process.h" 16 #include "content/child/child_process.h"
17 #include "content/common/media/media_devices.h"
15 #include "content/renderer/media/media_stream.h" 18 #include "content/renderer/media/media_stream.h"
16 #include "content/renderer/media/media_stream_audio_source.h" 19 #include "content/renderer/media/media_stream_audio_source.h"
17 #include "content/renderer/media/media_stream_track.h" 20 #include "content/renderer/media/media_stream_track.h"
18 #include "content/renderer/media/mock_constraint_factory.h" 21 #include "content/renderer/media/mock_constraint_factory.h"
19 #include "content/renderer/media/mock_media_stream_dispatcher.h" 22 #include "content/renderer/media/mock_media_stream_dispatcher.h"
20 #include "content/renderer/media/mock_media_stream_video_source.h" 23 #include "content/renderer/media/mock_media_stream_video_source.h"
21 #include "content/renderer/media/webrtc/mock_peer_connection_dependency_factory. h" 24 #include "content/renderer/media/webrtc/mock_peer_connection_dependency_factory. h"
25 #include "mojo/public/cpp/bindings/binding.h"
22 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
23 #include "third_party/WebKit/public/platform/WebMediaDeviceInfo.h" 27 #include "third_party/WebKit/public/platform/WebMediaDeviceInfo.h"
24 #include "third_party/WebKit/public/platform/WebMediaStream.h" 28 #include "third_party/WebKit/public/platform/WebMediaStream.h"
25 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" 29 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
26 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" 30 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
27 #include "third_party/WebKit/public/platform/WebMediaStreamTrackSourcesRequest.h " 31 #include "third_party/WebKit/public/platform/WebMediaStreamTrackSourcesRequest.h "
28 #include "third_party/WebKit/public/platform/WebString.h" 32 #include "third_party/WebKit/public/platform/WebString.h"
29 #include "third_party/WebKit/public/platform/WebVector.h" 33 #include "third_party/WebKit/public/platform/WebVector.h"
30 #include "third_party/WebKit/public/web/WebHeap.h" 34 #include "third_party/WebKit/public/web/WebHeap.h"
31 35
32 namespace content { 36 namespace content {
33 37
34 class MockMediaStreamVideoCapturerSource : public MockMediaStreamVideoSource { 38 class MockMediaStreamVideoCapturerSource : public MockMediaStreamVideoSource {
35 public: 39 public:
36 MockMediaStreamVideoCapturerSource( 40 MockMediaStreamVideoCapturerSource(
37 const StreamDeviceInfo& device, 41 const StreamDeviceInfo& device,
38 const SourceStoppedCallback& stop_callback, 42 const SourceStoppedCallback& stop_callback,
39 PeerConnectionDependencyFactory* factory) 43 PeerConnectionDependencyFactory* factory)
40 : MockMediaStreamVideoSource(false) { 44 : MockMediaStreamVideoSource(false) {
41 SetDeviceInfo(device); 45 SetDeviceInfo(device);
42 SetStopCallback(stop_callback); 46 SetStopCallback(stop_callback);
43 } 47 }
44 }; 48 };
45 49
50 class MockMediaDevicesDispatcherHost
51 : public ::mojom::MediaDevicesDispatcherHost {
52 public:
53 MockMediaDevicesDispatcherHost() : binding_(this) {}
54 void EnumerateDevices(bool request_audio_input,
55 bool request_video_input,
56 bool request_audio_output,
57 const url::Origin& security_origin,
58 const EnumerateDevicesCallback& callback) override {
59 std::vector<std::vector<MediaDeviceInfo>> result(NUM_MEDIA_DEVICE_TYPES);
60 if (request_audio_input) {
61 result[MEDIA_DEVICE_TYPE_AUDIO_INPUT].push_back(MediaDeviceInfo(
62 "fake_audio_input 1", "Fake Audio Input 1", "fake_group 1"));
63 result[MEDIA_DEVICE_TYPE_AUDIO_INPUT].push_back(MediaDeviceInfo(
64 "fake_audio_input 2", "Fake Audio Input 2", "fake_group 2"));
65 }
66 if (request_video_input) {
67 result[MEDIA_DEVICE_TYPE_VIDEO_INPUT].push_back(
68 MediaDeviceInfo("fake_video_input 1", "Fake Video Input 1", ""));
69 result[MEDIA_DEVICE_TYPE_VIDEO_INPUT].push_back(
70 MediaDeviceInfo("fake_video_input 2", "Fake Video Input 2", ""));
71 }
72 if (request_audio_output) {
73 result[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT].push_back(MediaDeviceInfo(
74 "fake_audio_output 1", "Fake Audio Input 1", "fake_group 1"));
75 }
76 callback.Run(result);
77 }
78
79 mojom::MediaDevicesDispatcherHostPtr CreateInterfacePtrAndBind() {
80 return binding_.CreateInterfacePtrAndBind();
81 }
82
83 private:
84 mojo::Binding<mojom::MediaDevicesDispatcherHost> binding_;
85 };
86
46 class UserMediaClientImplUnderTest : public UserMediaClientImpl { 87 class UserMediaClientImplUnderTest : public UserMediaClientImpl {
47 public: 88 public:
48 enum RequestState { 89 enum RequestState {
49 REQUEST_NOT_STARTED, 90 REQUEST_NOT_STARTED,
50 REQUEST_NOT_COMPLETE, 91 REQUEST_NOT_COMPLETE,
51 REQUEST_SUCCEEDED, 92 REQUEST_SUCCEEDED,
52 REQUEST_FAILED, 93 REQUEST_FAILED,
53 }; 94 };
54 95
55 UserMediaClientImplUnderTest( 96 UserMediaClientImplUnderTest(
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 class UserMediaClientImplTest : public ::testing::Test { 254 class UserMediaClientImplTest : public ::testing::Test {
214 public: 255 public:
215 void SetUp() override { 256 void SetUp() override {
216 // Create our test object. 257 // Create our test object.
217 child_process_.reset(new ChildProcess()); 258 child_process_.reset(new ChildProcess());
218 dependency_factory_.reset(new MockPeerConnectionDependencyFactory()); 259 dependency_factory_.reset(new MockPeerConnectionDependencyFactory());
219 ms_dispatcher_ = new MockMediaStreamDispatcher(); 260 ms_dispatcher_ = new MockMediaStreamDispatcher();
220 used_media_impl_.reset(new UserMediaClientImplUnderTest( 261 used_media_impl_.reset(new UserMediaClientImplUnderTest(
221 dependency_factory_.get(), 262 dependency_factory_.get(),
222 std::unique_ptr<MediaStreamDispatcher>(ms_dispatcher_))); 263 std::unique_ptr<MediaStreamDispatcher>(ms_dispatcher_)));
264 used_media_impl_->SetMediaDevicesDispatcherForTesting(
265 media_devices_dispatcher_.CreateInterfacePtrAndBind());
223 } 266 }
224 267
225 void TearDown() override { 268 void TearDown() override {
226 used_media_impl_.reset(); 269 used_media_impl_.reset();
227 blink::WebHeap::collectAllGarbageForTesting(); 270 blink::WebHeap::collectAllGarbageForTesting();
228 } 271 }
229 272
230 void LoadNewDocumentInFrame() { 273 void LoadNewDocumentInFrame() {
231 used_media_impl_->WillCommitProvisionalLoad(); 274 used_media_impl_->WillCommitProvisionalLoad();
232 } 275 }
(...skipping 27 matching lines...) Expand all
260 303
261 void FakeMediaStreamDispatcherRequestUserMediaComplete() { 304 void FakeMediaStreamDispatcherRequestUserMediaComplete() {
262 // Audio request ID is used as the shared request ID. 305 // Audio request ID is used as the shared request ID.
263 used_media_impl_->OnStreamGenerated( 306 used_media_impl_->OnStreamGenerated(
264 ms_dispatcher_->audio_input_request_id(), 307 ms_dispatcher_->audio_input_request_id(),
265 ms_dispatcher_->stream_label(), 308 ms_dispatcher_->stream_label(),
266 ms_dispatcher_->audio_input_array(), 309 ms_dispatcher_->audio_input_array(),
267 ms_dispatcher_->video_array()); 310 ms_dispatcher_->video_array());
268 } 311 }
269 312
270 void FakeMediaStreamDispatcherRequestMediaDevicesComplete() {
271 // There may be repeated replies due to device or configuration changes.
272 used_media_impl_->OnDevicesEnumerated(
273 ms_dispatcher_->audio_input_request_id(),
274 ms_dispatcher_->audio_input_array());
275 used_media_impl_->OnDevicesEnumerated(
276 ms_dispatcher_->audio_input_request_id(),
277 ms_dispatcher_->audio_input_array());
278 used_media_impl_->OnDevicesEnumerated(
279 ms_dispatcher_->audio_output_request_id(),
280 ms_dispatcher_->audio_output_array());
281 used_media_impl_->OnDevicesEnumerated(
282 ms_dispatcher_->audio_output_request_id(),
283 ms_dispatcher_->audio_output_array());
284 used_media_impl_->OnDevicesEnumerated(
285 ms_dispatcher_->video_request_id(),
286 ms_dispatcher_->video_array());
287 used_media_impl_->OnDevicesEnumerated(
288 ms_dispatcher_->video_request_id(),
289 ms_dispatcher_->video_array());
290 }
291
292 void FakeMediaStreamDispatcherRequestSourcesComplete() {
293 used_media_impl_->OnDevicesEnumerated(
294 ms_dispatcher_->audio_input_request_id(),
295 ms_dispatcher_->audio_input_array());
296 used_media_impl_->OnDevicesEnumerated(
297 ms_dispatcher_->video_request_id(),
298 ms_dispatcher_->video_array());
299 }
300
301 void StartMockedVideoSource() { 313 void StartMockedVideoSource() {
302 MockMediaStreamVideoCapturerSource* video_source = 314 MockMediaStreamVideoCapturerSource* video_source =
303 used_media_impl_->last_created_video_source(); 315 used_media_impl_->last_created_video_source();
304 if (video_source->SourceHasAttemptedToStart()) 316 if (video_source->SourceHasAttemptedToStart())
305 video_source->StartMockedSource(); 317 video_source->StartMockedSource();
306 } 318 }
307 319
308 void FailToStartMockedVideoSource() { 320 void FailToStartMockedVideoSource() {
309 MockMediaStreamVideoCapturerSource* video_source = 321 MockMediaStreamVideoCapturerSource* video_source =
310 used_media_impl_->last_created_video_source(); 322 used_media_impl_->last_created_video_source();
(...skipping 12 matching lines...) Expand all
323 bool result = used_media_impl_->UserMediaRequestHasAutomaticDeviceSelection( 335 bool result = used_media_impl_->UserMediaRequestHasAutomaticDeviceSelection(
324 ms_dispatcher_->audio_input_request_id()); 336 ms_dispatcher_->audio_input_request_id());
325 used_media_impl_->DeleteRequest(ms_dispatcher_->audio_input_request_id()); 337 used_media_impl_->DeleteRequest(ms_dispatcher_->audio_input_request_id());
326 return result; 338 return result;
327 } 339 }
328 340
329 protected: 341 protected:
330 base::MessageLoop message_loop_; 342 base::MessageLoop message_loop_;
331 std::unique_ptr<ChildProcess> child_process_; 343 std::unique_ptr<ChildProcess> child_process_;
332 MockMediaStreamDispatcher* ms_dispatcher_; // Owned by |used_media_impl_|. 344 MockMediaStreamDispatcher* ms_dispatcher_; // Owned by |used_media_impl_|.
345 MockMediaDevicesDispatcherHost media_devices_dispatcher_;
333 std::unique_ptr<UserMediaClientImplUnderTest> used_media_impl_; 346 std::unique_ptr<UserMediaClientImplUnderTest> used_media_impl_;
334 std::unique_ptr<MockPeerConnectionDependencyFactory> dependency_factory_; 347 std::unique_ptr<MockPeerConnectionDependencyFactory> dependency_factory_;
335 }; 348 };
336 349
337 TEST_F(UserMediaClientImplTest, GenerateMediaStream) { 350 TEST_F(UserMediaClientImplTest, GenerateMediaStream) {
338 // Generate a stream with both audio and video. 351 // Generate a stream with both audio and video.
339 blink::WebMediaStream mixed_desc = RequestLocalMediaStream(); 352 blink::WebMediaStream mixed_desc = RequestLocalMediaStream();
340 } 353 }
341 354
342 // Test that the same source object is used if two MediaStreams are generated 355 // Test that the same source object is used if two MediaStreams are generated
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
557 570
558 blink::WebVector<blink::WebMediaStreamTrack> video_tracks; 571 blink::WebVector<blink::WebMediaStreamTrack> video_tracks;
559 mixed_desc.videoTracks(video_tracks); 572 mixed_desc.videoTracks(video_tracks);
560 MediaStreamTrack* video_track = MediaStreamTrack::GetTrack(video_tracks[0]); 573 MediaStreamTrack* video_track = MediaStreamTrack::GetTrack(video_tracks[0]);
561 video_track->Stop(); 574 video_track->Stop();
562 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter()); 575 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter());
563 } 576 }
564 577
565 TEST_F(UserMediaClientImplTest, EnumerateMediaDevices) { 578 TEST_F(UserMediaClientImplTest, EnumerateMediaDevices) {
566 used_media_impl_->RequestMediaDevices(); 579 used_media_impl_->RequestMediaDevices();
567 FakeMediaStreamDispatcherRequestMediaDevicesComplete(); 580 base::RunLoop().RunUntilIdle();
568 581
569 EXPECT_EQ(UserMediaClientImplUnderTest::REQUEST_SUCCEEDED, 582 EXPECT_EQ(UserMediaClientImplUnderTest::REQUEST_SUCCEEDED,
570 used_media_impl_->request_state()); 583 used_media_impl_->request_state());
571 EXPECT_EQ(static_cast<size_t>(5), used_media_impl_->last_devices().size()); 584 EXPECT_EQ(static_cast<size_t>(5), used_media_impl_->last_devices().size());
572 585
573 // Audio input device with matched output ID. 586 // Audio input device with matched output ID.
574 const blink::WebMediaDeviceInfo* device = 587 const blink::WebMediaDeviceInfo* device =
575 &used_media_impl_->last_devices()[0]; 588 &used_media_impl_->last_devices()[0];
576 EXPECT_FALSE(device->deviceId().isEmpty()); 589 EXPECT_FALSE(device->deviceId().isEmpty());
577 EXPECT_EQ(blink::WebMediaDeviceInfo::MediaDeviceKindAudioInput, 590 EXPECT_EQ(blink::WebMediaDeviceInfo::MediaDeviceKindAudioInput,
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
612 625
613 // Verfify group IDs. 626 // Verfify group IDs.
614 EXPECT_TRUE(used_media_impl_->last_devices()[0].groupId().equals( 627 EXPECT_TRUE(used_media_impl_->last_devices()[0].groupId().equals(
615 used_media_impl_->last_devices()[4].groupId())); 628 used_media_impl_->last_devices()[4].groupId()));
616 EXPECT_FALSE(used_media_impl_->last_devices()[1].groupId().equals( 629 EXPECT_FALSE(used_media_impl_->last_devices()[1].groupId().equals(
617 used_media_impl_->last_devices()[4].groupId())); 630 used_media_impl_->last_devices()[4].groupId()));
618 } 631 }
619 632
620 TEST_F(UserMediaClientImplTest, EnumerateSources) { 633 TEST_F(UserMediaClientImplTest, EnumerateSources) {
621 used_media_impl_->RequestSources(); 634 used_media_impl_->RequestSources();
622 FakeMediaStreamDispatcherRequestSourcesComplete(); 635 base::RunLoop().RunUntilIdle();
623 636
624 EXPECT_EQ(UserMediaClientImplUnderTest::REQUEST_SUCCEEDED, 637 EXPECT_EQ(UserMediaClientImplUnderTest::REQUEST_SUCCEEDED,
625 used_media_impl_->request_state()); 638 used_media_impl_->request_state());
626 EXPECT_EQ(static_cast<size_t>(4), used_media_impl_->last_sources().size()); 639 EXPECT_EQ(static_cast<size_t>(4), used_media_impl_->last_sources().size());
627 640
628 // Audio input devices. 641 // Audio input devices.
629 const blink::WebSourceInfo* source = &used_media_impl_->last_sources()[0]; 642 const blink::WebSourceInfo* source = &used_media_impl_->last_sources()[0];
630 EXPECT_FALSE(source->id().isEmpty()); 643 EXPECT_FALSE(source->id().isEmpty());
631 EXPECT_EQ(blink::WebSourceInfo::SourceKindAudio, source->kind()); 644 EXPECT_EQ(blink::WebSourceInfo::SourceKindAudio, source->kind());
632 EXPECT_FALSE(source->label().isEmpty()); 645 EXPECT_FALSE(source->label().isEmpty());
633 EXPECT_EQ(blink::WebSourceInfo::VideoFacingModeNone, source->facing()); 646 EXPECT_EQ(blink::WebSourceInfo::VideoFacingModeNone, source->facing());
634 647
635 source = &used_media_impl_->last_sources()[1]; 648 source = &used_media_impl_->last_sources()[1];
636 EXPECT_FALSE(source->id().isEmpty()); 649 EXPECT_FALSE(source->id().isEmpty());
637 EXPECT_EQ(blink::WebSourceInfo::SourceKindAudio, source->kind()); 650 EXPECT_EQ(blink::WebSourceInfo::SourceKindAudio, source->kind());
638 EXPECT_FALSE(source->label().isEmpty()); 651 EXPECT_FALSE(source->label().isEmpty());
639 EXPECT_EQ(blink::WebSourceInfo::VideoFacingModeNone, source->facing()); 652 EXPECT_EQ(blink::WebSourceInfo::VideoFacingModeNone, source->facing());
640 653
641 // Video input device user facing. 654 // Video input device user facing.
642 source = &used_media_impl_->last_sources()[2]; 655 source = &used_media_impl_->last_sources()[2];
643 EXPECT_FALSE(source->id().isEmpty()); 656 EXPECT_FALSE(source->id().isEmpty());
644 EXPECT_EQ(blink::WebSourceInfo::SourceKindVideo, source->kind()); 657 EXPECT_EQ(blink::WebSourceInfo::SourceKindVideo, source->kind());
645 EXPECT_FALSE(source->label().isEmpty()); 658 EXPECT_FALSE(source->label().isEmpty());
646 EXPECT_EQ(blink::WebSourceInfo::VideoFacingModeUser, source->facing()); 659 EXPECT_EQ(blink::WebSourceInfo::VideoFacingModeNone, source->facing());
647 660
648 // Video input device environment facing. 661 // Video input device environment facing.
649 source = &used_media_impl_->last_sources()[3]; 662 source = &used_media_impl_->last_sources()[3];
650 EXPECT_FALSE(source->id().isEmpty()); 663 EXPECT_FALSE(source->id().isEmpty());
651 EXPECT_EQ(blink::WebSourceInfo::SourceKindVideo, source->kind()); 664 EXPECT_EQ(blink::WebSourceInfo::SourceKindVideo, source->kind());
652 EXPECT_FALSE(source->label().isEmpty()); 665 EXPECT_FALSE(source->label().isEmpty());
653 EXPECT_EQ(blink::WebSourceInfo::VideoFacingModeEnvironment, source->facing()); 666 EXPECT_EQ(blink::WebSourceInfo::VideoFacingModeNone, source->facing());
654 } 667 }
655 668
656 TEST_F(UserMediaClientImplTest, RenderToAssociatedSinkConstraint) { 669 TEST_F(UserMediaClientImplTest, RenderToAssociatedSinkConstraint) {
657 // For a null UserMediaRequest (no audio requested), we expect false. 670 // For a null UserMediaRequest (no audio requested), we expect false.
658 used_media_impl_->RequestUserMedia(); 671 used_media_impl_->RequestUserMedia();
659 EXPECT_FALSE(used_media_impl_->UserMediaRequestHasAutomaticDeviceSelection( 672 EXPECT_FALSE(used_media_impl_->UserMediaRequestHasAutomaticDeviceSelection(
660 ms_dispatcher_->audio_input_request_id())); 673 ms_dispatcher_->audio_input_request_id()));
661 used_media_impl_->DeleteRequest(ms_dispatcher_->audio_input_request_id()); 674 used_media_impl_->DeleteRequest(ms_dispatcher_->audio_input_request_id());
662 675
663 // If audio is requested, but no constraint, it should be true. 676 // If audio is requested, but no constraint, it should be true.
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
718 // Now we load a new document in the web frame. If in the above Stop() call, 731 // Now we load a new document in the web frame. If in the above Stop() call,
719 // UserMediaClientImpl accidentally removed audio track, then video track will 732 // UserMediaClientImpl accidentally removed audio track, then video track will
720 // be removed again here, which is incorrect. 733 // be removed again here, which is incorrect.
721 LoadNewDocumentInFrame(); 734 LoadNewDocumentInFrame();
722 blink::WebHeap::collectAllGarbageForTesting(); 735 blink::WebHeap::collectAllGarbageForTesting();
723 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter()); 736 EXPECT_EQ(1, ms_dispatcher_->stop_video_device_counter());
724 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter()); 737 EXPECT_EQ(1, ms_dispatcher_->stop_audio_device_counter());
725 } 738 }
726 739
727 } // namespace content 740 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/media/user_media_client_impl.cc ('k') | content/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698