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
3 // found in the LICENSE file.
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "content/browser/browser_thread_impl.h"
11 #include "content/browser/renderer_host/media/media_stream_manager.h"
12 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h"
13 #include "content/common/media/media_stream_options.h"
14 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "media/audio/audio_manager_base.h"
16 #if defined(OS_ANDROID)
17 #include "media/audio/android/audio_manager_android.h"
18 #elif defined(OS_LINUX) || defined(OS_OPENBSD)
19 #include "media/audio/linux/audio_manager_linux.h"
20 #elif defined(OS_MACOSX)
21 #include "media/audio/mac/audio_manager_mac.h"
23 #include "media/audio/win/audio_manager_win.h"
25 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h"
32 #if defined(OS_LINUX) || defined(OS_OPENBSD)
33 typedef media::AudioManagerLinux AudioManagerPlatform;
34 #elif defined(OS_MACOSX)
35 typedef media::AudioManagerMac AudioManagerPlatform;
37 typedef media::AudioManagerWin AudioManagerPlatform;
38 #elif defined(OS_ANDROID)
39 typedef media::AudioManagerAndroid AudioManagerPlatform;
43 // This class mocks the audio manager and overrides the
44 // GetAudioInputDeviceNames() method to ensure that we can run our tests on
45 // the buildbots. media::AudioManagerBase
46 class MockAudioManager : public AudioManagerPlatform {
49 virtual ~MockAudioManager() {}
51 virtual void GetAudioInputDeviceNames(
52 media::AudioDeviceNames* device_names) OVERRIDE {
53 DCHECK(device_names->empty());
54 if (HasAudioInputDevices()) {
55 AudioManagerBase::GetAudioInputDeviceNames(device_names);
57 device_names->push_back(media::AudioDeviceName("fake_device_name",
63 DISALLOW_COPY_AND_ASSIGN(MockAudioManager);
66 class MediaStreamManagerTest : public ::testing::Test {
68 MediaStreamManagerTest()
69 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
70 message_loop_(base::MessageLoopProxy::current()) {
71 // Create our own MediaStreamManager.
72 audio_manager_.reset(new MockAudioManager());
73 media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get()));
75 // Use fake devices in order to run on the bots.
76 media_stream_manager_->UseFakeDevice();
79 virtual ~MediaStreamManagerTest() {
80 media_stream_manager_->WillDestroyCurrentMessageLoop();
83 MOCK_METHOD1(Response, void(int index));
84 void ResponseCallback(int index,
85 const MediaStreamDevices& devices,
86 scoped_ptr<MediaStreamUIProxy> ui_proxy) {
88 message_loop_->PostTask(FROM_HERE, run_loop_.QuitClosure());
92 std::string MakeMediaAccessRequest(int index) {
93 const int render_process_id = 1;
94 const int render_view_id = 1;
95 const int page_request_id = 1;
96 StreamOptions components(MEDIA_DEVICE_AUDIO_CAPTURE,
97 MEDIA_DEVICE_VIDEO_CAPTURE);
98 const GURL security_origin;
99 MediaStreamManager::MediaRequestResponseCallback callback =
100 base::Bind(&MediaStreamManagerTest::ResponseCallback,
101 base::Unretained(this), index);
102 return media_stream_manager_->MakeMediaAccessRequest(render_process_id,
110 scoped_ptr<media::AudioManager> audio_manager_;
111 scoped_ptr<MediaStreamManager> media_stream_manager_;
112 content::TestBrowserThreadBundle thread_bundle_;
113 scoped_refptr<base::MessageLoopProxy> message_loop_;
114 base::RunLoop run_loop_;
117 DISALLOW_COPY_AND_ASSIGN(MediaStreamManagerTest);
120 TEST_F(MediaStreamManagerTest, MakeMediaAccessRequest) {
121 MakeMediaAccessRequest(0);
123 // Expecting the callback will be triggered and quit the test.
124 EXPECT_CALL(*this, Response(0));
128 TEST_F(MediaStreamManagerTest, MakeAndCancelMediaAccessRequest) {
129 std::string label = MakeMediaAccessRequest(0);
130 // No callback is expected.
131 media_stream_manager_->CancelRequest(label);
134 TEST_F(MediaStreamManagerTest, MakeMultipleRequests) {
136 std::string label1 = MakeMediaAccessRequest(0);
139 int render_process_id = 2;
140 int render_view_id = 2;
141 int page_request_id = 2;
142 StreamOptions components(MEDIA_DEVICE_AUDIO_CAPTURE,
143 MEDIA_DEVICE_VIDEO_CAPTURE);
144 GURL security_origin;
145 MediaStreamManager::MediaRequestResponseCallback callback =
146 base::Bind(&MediaStreamManagerTest::ResponseCallback,
147 base::Unretained(this), 1);
148 std::string label2 = media_stream_manager_->MakeMediaAccessRequest(
156 // Expecting the callbackS from requests will be triggered and quit the test.
157 // Note, the callbacks might come in a different order depending on the
159 EXPECT_CALL(*this, Response(0));
160 EXPECT_CALL(*this, Response(1));
164 TEST_F(MediaStreamManagerTest, MakeAndCancelMultipleRequests) {
165 std::string label1 = MakeMediaAccessRequest(0);
166 std::string label2 = MakeMediaAccessRequest(1);
167 media_stream_manager_->CancelRequest(label1);
169 // Expecting the callback from the second request will be triggered and
171 EXPECT_CALL(*this, Response(1));
175 } // namespace content