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

Unified Diff: content/browser/renderer_host/media/media_stream_dispatcher_host_unittest.cc

Issue 34393006: Refactor MediaStreamManager to never output real device id. It now always output sourceId in the fo… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased to latest code base. Created 7 years, 1 month 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 side-by-side diff with in-line comments
Download patch
Index: content/browser/renderer_host/media/media_stream_dispatcher_host_unittest.cc
diff --git a/content/browser/renderer_host/media/media_stream_dispatcher_host_unittest.cc b/content/browser/renderer_host/media/media_stream_dispatcher_host_unittest.cc
index 12f41fb1430fbb236354308394a367a8c02bc193..3e5bc349bc404176a720912a9c601015e09e40cf 100644
--- a/content/browser/renderer_host/media/media_stream_dispatcher_host_unittest.cc
+++ b/content/browser/renderer_host/media/media_stream_dispatcher_host_unittest.cc
@@ -15,12 +15,13 @@
#include "content/browser/renderer_host/media/media_stream_ui_proxy.h"
#include "content/common/media/media_stream_messages.h"
#include "content/common/media/media_stream_options.h"
+#include "content/public/browser/media_device_id.h"
#include "content/public/test/mock_resource_context.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/test/test_content_browser_client.h"
#include "content/test/test_content_client.h"
#include "ipc/ipc_message_macros.h"
-#include "media/audio/audio_manager.h"
+#include "media/audio/mock_audio_manager.h"
#include "media/video/capture/fake_video_capture_device.h"
#include "net/url_request/url_request_context.h"
#include "testing/gmock/include/gmock/gmock.h"
@@ -61,10 +62,11 @@ class MockMediaStreamDispatcherHost : public MediaStreamDispatcherHost,
void OnGenerateStream(int render_view_id,
int page_request_id,
const StreamOptions& components,
+ const GURL& security_origin,
const base::Closure& quit_closure) {
quit_closures_.push(quit_closure);
MediaStreamDispatcherHost::OnGenerateStream(
- render_view_id, page_request_id, components, GURL());
+ render_view_id, page_request_id, components, security_origin);
}
void OnStopStreamDevice(int render_view_id,
@@ -76,25 +78,28 @@ class MockMediaStreamDispatcherHost : public MediaStreamDispatcherHost,
int page_request_id,
const std::string& device_id,
MediaStreamType type,
+ const GURL& security_origin,
const base::Closure& quit_closure) {
quit_closures_.push(quit_closure);
MediaStreamDispatcherHost::OnOpenDevice(
- render_view_id, page_request_id, device_id, type, GURL());
+ render_view_id, page_request_id, device_id, type, security_origin);
}
- bool FindExistingRequestedDeviceInfo(const std::string& device_id,
- MediaStreamRequestType request_type,
- StreamDeviceInfo* device_info) {
- MediaRequestState request_state;
- return media_stream_manager_->FindExistingRequestedDeviceInfo(
- kProcessId, kRenderId, request_type, device_id, device_info,
- &request_state);
+ void OnEnumerateDevices(int render_view_id,
+ int page_request_id,
+ MediaStreamType type,
+ const GURL& security_origin,
+ const base::Closure& quit_closure) {
+ quit_closures_.push(quit_closure);
+ MediaStreamDispatcherHost::OnEnumerateDevices(
+ render_view_id, page_request_id, type, security_origin);
}
std::string label_;
StreamDeviceInfoArray audio_devices_;
StreamDeviceInfoArray video_devices_;
StreamDeviceInfo opened_device_;
+ StreamDeviceInfoArray enumerated_devices_;
private:
virtual ~MockMediaStreamDispatcherHost() {}
@@ -115,6 +120,8 @@ class MockMediaStreamDispatcherHost : public MediaStreamDispatcherHost,
IPC_MESSAGE_HANDLER(MediaStreamMsg_StopGeneratedStream,
OnStopGeneratedStreamFromBrowser)
IPC_MESSAGE_HANDLER(MediaStreamMsg_DeviceOpened, OnDeviceOpened)
+ IPC_MESSAGE_HANDLER(MediaStreamMsg_DevicesEnumerated,
+ OnDevicesEnumerated)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
EXPECT_TRUE(handled);
@@ -176,6 +183,17 @@ class MockMediaStreamDispatcherHost : public MediaStreamDispatcherHost,
opened_device_ = device;
}
+ void OnDevicesEnumerated(const IPC::Message& msg,
+ int request_id,
+ const std::string& label,
+ const StreamDeviceInfoArray& devices) {
+ base::Closure quit_closure = quit_closures_.front();
+ quit_closures_.pop();
+ message_loop_->PostTask(FROM_HERE, base::ResetAndReturn(&quit_closure));
+ label_ = label;
+ enumerated_devices_ = devices;
+ }
+
scoped_refptr<base::MessageLoopProxy> message_loop_;
std::queue<base::Closure> quit_closures_;
@@ -190,9 +208,11 @@ class MediaStreamDispatcherHostTest : public testing::Test {
public:
MediaStreamDispatcherHostTest()
: old_browser_client_(NULL),
- thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
+ thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
+ origin_("https://test.com") {
+ audio_manager_.reset(
+ new media::MockAudioManager(base::MessageLoopProxy::current()));
// Create our own MediaStreamManager.
- audio_manager_.reset(media::AudioManager::Create());
media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get()));
// Make sure we use fake devices to avoid long delays.
media_stream_manager_->UseFakeDevice();
@@ -207,10 +227,14 @@ class MediaStreamDispatcherHostTest : public testing::Test {
}
virtual ~MediaStreamDispatcherHostTest() {
- // Recover the old browser client and content client.
- SetBrowserClientForTesting(old_browser_client_);
- content_client_.reset();
- media_stream_manager_->WillDestroyCurrentMessageLoop();
+ }
+
+ virtual void SetUp() OVERRIDE {
+ media::FakeVideoCaptureDevice::GetDeviceNames(&physical_video_devices_);
+ ASSERT_GT(physical_video_devices_.size(), 0u);
+
+ audio_manager_->GetAudioInputDeviceNames(&physical_audio_devices_);
+ ASSERT_GT(physical_audio_devices_.size(), 0u);
}
virtual void TearDown() OVERRIDE {
@@ -231,9 +255,28 @@ class MediaStreamDispatcherHostTest : public testing::Test {
int page_request_id,
const StreamOptions& options) {
base::RunLoop run_loop;
- host_->OnGenerateStream(render_view_id, page_request_id, options,
+ EXPECT_CALL(*host_.get(), OnStreamGenerated(
+ render_view_id, page_request_id,
+ options.audio_type != MEDIA_NO_SERVICE ? 1 : 0,
+ options.video_type != MEDIA_NO_SERVICE ? 1 : 0));
+ host_->OnGenerateStream(render_view_id, page_request_id, options, origin_,
run_loop.QuitClosure());
run_loop.Run();
+ EXPECT_FALSE(DoesContainRawIds(host_->audio_devices_));
+ EXPECT_FALSE(DoesContainRawIds(host_->video_devices_));
+ EXPECT_TRUE(DoesEveryDeviceMapToRawId(host_->audio_devices_, origin_));
+ EXPECT_TRUE(DoesEveryDeviceMapToRawId(host_->video_devices_, origin_));
+ }
+
+ void GenerateStreamAndWaitForFailure(int render_view_id,
+ int page_request_id,
+ const StreamOptions& options) {
+ base::RunLoop run_loop;
+ EXPECT_CALL(*host_.get(),
+ OnStreamGenerationFailed(render_view_id, page_request_id));
+ host_->OnGenerateStream(render_view_id, page_request_id, options, origin_,
+ run_loop.QuitClosure());
+ run_loop.Run();
}
void OpenVideoDeviceAndWaitForResult(int render_view_id,
@@ -241,9 +284,96 @@ class MediaStreamDispatcherHostTest : public testing::Test {
const std::string& device_id) {
base::RunLoop run_loop;
host_->OnOpenDevice(render_view_id, page_request_id, device_id,
- MEDIA_DEVICE_VIDEO_CAPTURE,
+ MEDIA_DEVICE_VIDEO_CAPTURE, origin_,
run_loop.QuitClosure());
run_loop.Run();
+ EXPECT_FALSE(DoesContainRawIds(host_->video_devices_));
+ EXPECT_TRUE(DoesEveryDeviceMapToRawId(host_->video_devices_, origin_));
+ }
+
+ void StopStreamDeviceAndWait(const std::string& device_id) {
+ EXPECT_TRUE(FindRequestedDeviceInfo(device_id, MEDIA_GENERATE_STREAM,
+ NULL));
+ host_->OnStopStreamDevice(kRenderId, device_id);
+ base::RunLoop().RunUntilIdle();
+ EXPECT_FALSE(FindRequestedDeviceInfo(device_id, MEDIA_GENERATE_STREAM,
+ NULL));
+ }
+
+ void EnumerateDevicesAndWaitForResult(int render_view_id,
+ int page_request_id,
+ MediaStreamType type) {
+ base::RunLoop run_loop;
+ host_->OnEnumerateDevices(render_view_id, page_request_id, type, origin_,
+ run_loop.QuitClosure());
+ run_loop.Run();
+ ASSERT_FALSE(host_->enumerated_devices_.empty());
+ EXPECT_FALSE(DoesContainRawIds(host_->enumerated_devices_));
+ EXPECT_TRUE(DoesEveryDeviceMapToRawId(host_->enumerated_devices_, origin_));
+ }
+
+ bool FindRequestedDeviceInfo(const std::string& device_id,
+ MediaStreamRequestType request_type,
+ StreamDeviceInfo* device_info) {
+ const StreamDeviceInfo* found_device =
+ media_stream_manager_->FindRequestedDeviceInfoForTest(device_id,
+ kProcessId,
+ kRenderId,
+ request_type);
+ if (found_device && device_info) {
+ *device_info = *found_device;
+ }
+ return found_device;
+ }
+
+ bool DoesContainRawIds(const StreamDeviceInfoArray& devices) {
+ for (size_t i = 0; i < devices.size(); ++i) {
+ media::AudioDeviceNames::const_iterator audio_it =
+ physical_audio_devices_.begin();
+ for (; audio_it != physical_audio_devices_.end(); ++audio_it) {
+ if (audio_it->unique_id == devices[i].device.id)
+ return true;
+ }
+ media::VideoCaptureDevice::Names::const_iterator video_it =
+ physical_video_devices_.begin();
+ for (; video_it != physical_video_devices_.end(); ++video_it) {
+ if (video_it->id() == devices[i].device.id)
+ return true;
+ }
+ }
+ return false;
+ }
+
+ bool DoesEveryDeviceMapToRawId(const StreamDeviceInfoArray& devices,
+ const GURL& origin) {
+ for (size_t i = 0; i < devices.size(); ++i) {
+ bool found_match = false;
+ media::AudioDeviceNames::const_iterator audio_it =
+ physical_audio_devices_.begin();
+ for (; audio_it != physical_audio_devices_.end(); ++audio_it) {
+ if (content::DoesMediaDeviceIDMatchHMAC(
+ origin,
+ devices[i].device.id,
+ audio_it->unique_id)) {
+ EXPECT_FALSE(found_match);
+ found_match = true;
+ }
+ }
+ media::VideoCaptureDevice::Names::const_iterator video_it =
+ physical_video_devices_.begin();
+ for (; video_it != physical_video_devices_.end(); ++video_it) {
+ if (content::DoesMediaDeviceIDMatchHMAC(
+ origin,
+ devices[i].device.id,
+ video_it->id())) {
+ EXPECT_FALSE(found_match);
+ found_match = true;
+ }
+ }
+ if (!found_match)
+ return false;
+ }
+ return true;
}
scoped_refptr<MockMediaStreamDispatcherHost> host_;
@@ -252,19 +382,31 @@ class MediaStreamDispatcherHostTest : public testing::Test {
ContentBrowserClient* old_browser_client_;
scoped_ptr<ContentClient> content_client_;
content::TestBrowserThreadBundle thread_bundle_;
+ media::AudioDeviceNames physical_audio_devices_;
+ media::VideoCaptureDevice::Names physical_video_devices_;
+ GURL origin_;
};
TEST_F(MediaStreamDispatcherHostTest, GenerateStreamWithVideoOnly) {
StreamOptions options(MEDIA_NO_SERVICE, MEDIA_DEVICE_VIDEO_CAPTURE);
SetupFakeUI(true);
- EXPECT_CALL(*host_.get(), OnStreamGenerated(kRenderId, kPageRequestId, 0, 1));
GenerateStreamAndWaitForResult(kRenderId, kPageRequestId, options);
EXPECT_EQ(host_->audio_devices_.size(), 0u);
EXPECT_EQ(host_->video_devices_.size(), 1u);
}
+TEST_F(MediaStreamDispatcherHostTest, GenerateStreamWithAudioOnly) {
+ StreamOptions options(MEDIA_DEVICE_AUDIO_CAPTURE, MEDIA_NO_SERVICE);
+
+ SetupFakeUI(true);
+ GenerateStreamAndWaitForResult(kRenderId, kPageRequestId, options);
+
+ EXPECT_EQ(host_->audio_devices_.size(), 1u);
+ EXPECT_EQ(host_->video_devices_.size(), 0u);
+}
+
// This test generates two streams with video only using the same render view
// id. The same capture device with the same device and session id is expected
// to be used.
@@ -273,7 +415,6 @@ TEST_F(MediaStreamDispatcherHostTest, GenerateStreamsFromSameRenderId) {
// Generate first stream.
SetupFakeUI(true);
- EXPECT_CALL(*host_.get(), OnStreamGenerated(kRenderId, kPageRequestId, 0, 1));
GenerateStreamAndWaitForResult(kRenderId, kPageRequestId, options);
// Check the latest generated stream.
@@ -285,8 +426,6 @@ TEST_F(MediaStreamDispatcherHostTest, GenerateStreamsFromSameRenderId) {
// Generate second stream.
SetupFakeUI(true);
- EXPECT_CALL(*host_.get(),
- OnStreamGenerated(kRenderId, kPageRequestId + 1, 0, 1));
GenerateStreamAndWaitForResult(kRenderId, kPageRequestId + 1, options);
// Check the latest generated stream.
@@ -306,7 +445,6 @@ TEST_F(MediaStreamDispatcherHostTest,
// Generate first stream.
SetupFakeUI(true);
- EXPECT_CALL(*host_.get(), OnStreamGenerated(kRenderId, kPageRequestId, 0, 1));
GenerateStreamAndWaitForResult(kRenderId, kPageRequestId, options);
EXPECT_EQ(host_->audio_devices_.size(), 0u);
@@ -335,7 +473,6 @@ TEST_F(MediaStreamDispatcherHostTest, GenerateStreamsDifferentRenderId) {
// Generate first stream.
SetupFakeUI(true);
- EXPECT_CALL(*host_.get(), OnStreamGenerated(kRenderId, kPageRequestId, 0, 1));
GenerateStreamAndWaitForResult(kRenderId, kPageRequestId, options);
// Check the latest generated stream.
@@ -347,8 +484,6 @@ TEST_F(MediaStreamDispatcherHostTest, GenerateStreamsDifferentRenderId) {
// Generate second stream from another render view.
SetupFakeUI(true);
- EXPECT_CALL(*host_.get(),
- OnStreamGenerated(kRenderId+1, kPageRequestId + 1, 0, 1));
GenerateStreamAndWaitForResult(kRenderId+1, kPageRequestId + 1, options);
// Check the latest generated stream.
@@ -378,41 +513,93 @@ TEST_F(MediaStreamDispatcherHostTest, GenerateStreamsWithoutWaiting) {
base::RunLoop run_loop1;
base::RunLoop run_loop2;
- host_->OnGenerateStream(kRenderId, kPageRequestId, options,
+ host_->OnGenerateStream(kRenderId, kPageRequestId, options, origin_,
run_loop1.QuitClosure());
- host_->OnGenerateStream(kRenderId, kPageRequestId + 1, options,
+ host_->OnGenerateStream(kRenderId, kPageRequestId + 1, options, origin_,
run_loop2.QuitClosure());
run_loop1.Run();
run_loop2.Run();
}
+// Test that we can generate streams where a sourceId is specified in the
+// request.
+TEST_F(MediaStreamDispatcherHostTest, GenerateStreamsWithSourceId) {
+ ASSERT_GE(physical_audio_devices_.size(), 1u);
+ ASSERT_GE(physical_video_devices_.size(), 1u);
+
+ media::AudioDeviceNames::const_iterator audio_it =
+ physical_audio_devices_.begin();
+ for (; audio_it != physical_audio_devices_.end(); ++audio_it) {
+ StreamOptions options(MEDIA_DEVICE_AUDIO_CAPTURE,
+ MEDIA_DEVICE_VIDEO_CAPTURE);
+ options.audio_device_id = content::GetHMACForMediaDeviceID(
+ origin_,
+ audio_it->unique_id);
+ ASSERT_FALSE(options.audio_device_id.empty());
+
+ // Generate first stream.
+ SetupFakeUI(true);
+ GenerateStreamAndWaitForResult(kRenderId, kPageRequestId, options);
+ EXPECT_EQ(host_->audio_devices_[0].device.id, options.audio_device_id);
+ }
+
+ media::VideoCaptureDevice::Names::const_iterator video_it =
+ physical_video_devices_.begin();
+ for (; video_it != physical_video_devices_.end(); ++video_it) {
+ StreamOptions options(MEDIA_DEVICE_AUDIO_CAPTURE,
+ MEDIA_DEVICE_VIDEO_CAPTURE);
+ options.video_device_id = content::GetHMACForMediaDeviceID(
+ origin_,
+ video_it->id());
+ ASSERT_FALSE(options.video_device_id.empty());
+
+ // Generate first stream.
+ SetupFakeUI(true);
+ GenerateStreamAndWaitForResult(kRenderId, kPageRequestId, options);
+ EXPECT_EQ(host_->video_devices_[0].device.id, options.video_device_id);
+ }
+}
+
+// Test that generating a stream with an invalid video source id fail.
+TEST_F(MediaStreamDispatcherHostTest, GenerateStreamsWithInvalidVideoSourceId) {
+ StreamOptions options(MEDIA_DEVICE_AUDIO_CAPTURE,
+ MEDIA_DEVICE_VIDEO_CAPTURE);
+ std::string invalid_source_id = "invalid source id";
+ options.video_device_id = invalid_source_id;
+
+ GenerateStreamAndWaitForFailure(kRenderId, kRenderId, options);
+}
+
+// Test that generating a stream with an invalid audio source id fail.
+TEST_F(MediaStreamDispatcherHostTest, GenerateStreamsWithInvalidAudioSourceId) {
+ StreamOptions options(MEDIA_DEVICE_AUDIO_CAPTURE,
+ MEDIA_DEVICE_VIDEO_CAPTURE);
+ std::string invalid_source_id = "invalid source id";
+ options.audio_device_id = invalid_source_id;
+
+ GenerateStreamAndWaitForFailure(kRenderId, kRenderId, options);
+}
+
TEST_F(MediaStreamDispatcherHostTest, StopDeviceInStream) {
StreamOptions options(MEDIA_NO_SERVICE, MEDIA_DEVICE_VIDEO_CAPTURE);
SetupFakeUI(true);
- EXPECT_CALL(*host_.get(), OnStreamGenerated(kRenderId, kPageRequestId, 0, 1));
GenerateStreamAndWaitForResult(kRenderId, kPageRequestId, options);
const std::string device_id = host_->video_devices_.front().device.id;
const int session_id = host_->video_devices_.front().session_id;
StreamDeviceInfo video_device_info;
- EXPECT_TRUE(host_->FindExistingRequestedDeviceInfo(device_id,
- MEDIA_GENERATE_STREAM,
- &video_device_info));
+ EXPECT_TRUE(FindRequestedDeviceInfo(device_id, MEDIA_GENERATE_STREAM,
+ &video_device_info));
EXPECT_EQ(video_device_info.device.id, device_id);
EXPECT_EQ(video_device_info.session_id, session_id);
OpenVideoDeviceAndWaitForResult(kRenderId, kPageRequestId, device_id);
- host_->OnStopStreamDevice(kRenderId, device_id);
+ StopStreamDeviceAndWait(device_id);
- EXPECT_FALSE(host_->FindExistingRequestedDeviceInfo(device_id,
- MEDIA_GENERATE_STREAM,
- &video_device_info));
- EXPECT_TRUE(host_->FindExistingRequestedDeviceInfo(device_id,
- MEDIA_OPEN_DEVICE,
- &video_device_info));
+ EXPECT_TRUE(FindRequestedDeviceInfo(device_id, MEDIA_OPEN_DEVICE, NULL));
}
TEST_F(MediaStreamDispatcherHostTest, CancelPendingStreamsOnChannelClosing) {
@@ -423,13 +610,15 @@ TEST_F(MediaStreamDispatcherHostTest, CancelPendingStreamsOnChannelClosing) {
// Create multiple GenerateStream requests.
size_t streams = 5;
for (size_t i = 1; i <= streams; ++i) {
- host_->OnGenerateStream(kRenderId, kPageRequestId + i, options,
+ host_->OnGenerateStream(kRenderId, kPageRequestId + i, options, origin_,
run_loop.QuitClosure());
}
// Calling OnChannelClosing() to cancel all the pending requests.
host_->OnChannelClosing();
run_loop.RunUntilIdle();
+
+ media_stream_manager_->WillDestroyCurrentMessageLoop();
}
TEST_F(MediaStreamDispatcherHostTest, StopGeneratedStreamsOnChannelClosing) {
@@ -439,8 +628,6 @@ TEST_F(MediaStreamDispatcherHostTest, StopGeneratedStreamsOnChannelClosing) {
size_t generated_streams = 3;
for (size_t i = 0; i < generated_streams; ++i) {
SetupFakeUI(true);
- EXPECT_CALL(*host_.get(),
- OnStreamGenerated(kRenderId, kPageRequestId + i, 0, 1));
GenerateStreamAndWaitForResult(kRenderId, kPageRequestId + i, options);
}
@@ -458,9 +645,8 @@ TEST_F(MediaStreamDispatcherHostTest, CloseFromUI) {
.WillOnce(SaveArg<0>(&close_callback));
media_stream_manager_->UseFakeUI(stream_ui.PassAs<FakeMediaStreamUIProxy>());
- EXPECT_CALL(*host_.get(), OnStreamGenerated(kRenderId, kPageRequestId, 0, 1));
- EXPECT_CALL(*host_.get(), OnStopGeneratedStreamFromBrowser(kRenderId));
GenerateStreamAndWaitForResult(kRenderId, kPageRequestId, options);
+ EXPECT_CALL(*host_.get(), OnStopGeneratedStreamFromBrowser(kRenderId));
EXPECT_EQ(host_->audio_devices_.size(), 0u);
EXPECT_EQ(host_->video_devices_.size(), 1u);
@@ -470,4 +656,15 @@ TEST_F(MediaStreamDispatcherHostTest, CloseFromUI) {
base::RunLoop().RunUntilIdle();
}
+TEST_F(MediaStreamDispatcherHostTest, EnumerateAudioDevices) {
+ EnumerateDevicesAndWaitForResult(kRenderId, kPageRequestId,
+ MEDIA_DEVICE_AUDIO_CAPTURE);
+}
+
+TEST_F(MediaStreamDispatcherHostTest, EnumerateVideoDevices) {
+ EnumerateDevicesAndWaitForResult(kRenderId, kPageRequestId,
+ MEDIA_DEVICE_VIDEO_CAPTURE);
+}
+
+
}; // namespace content

Powered by Google App Engine
This is Rietveld 408576698