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/command_line.h"
9 #include "base/location.h"
10 #include "base/run_loop.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "content/browser/browser_thread_impl.h"
14 #include "content/browser/renderer_host/media/media_stream_manager.h"
15 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h"
16 #include "content/common/media/media_stream_options.h"
17 #include "content/public/common/content_switches.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "media/audio/audio_manager_base.h"
20 #include "media/audio/fake_audio_log_factory.h"
21 #include "media/base/media_switches.h"
22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h"
26 #include "media/audio/alsa/audio_manager_alsa.h"
27 #elif defined(OS_ANDROID)
28 #include "media/audio/android/audio_manager_android.h"
29 #elif defined(OS_MACOSX)
30 #include "media/audio/mac/audio_manager_mac.h"
32 #include "media/audio/win/audio_manager_win.h"
34 #include "media/audio/fake_audio_manager.h"
42 typedef media::AudioManagerAlsa AudioManagerPlatform
;
43 #elif defined(OS_MACOSX)
44 typedef media::AudioManagerMac AudioManagerPlatform
;
46 typedef media::AudioManagerWin AudioManagerPlatform
;
47 #elif defined(OS_ANDROID)
48 typedef media::AudioManagerAndroid AudioManagerPlatform
;
50 typedef media::FakeAudioManager AudioManagerPlatform
;
54 // This class mocks the audio manager and overrides the
55 // GetAudioInputDeviceNames() method to ensure that we can run our tests on
56 // the buildbots. media::AudioManagerBase
57 class MockAudioManager
: public AudioManagerPlatform
{
59 MockAudioManager() : AudioManagerPlatform(&fake_audio_log_factory_
) {}
60 ~MockAudioManager() override
{}
62 void GetAudioInputDeviceNames(
63 media::AudioDeviceNames
* device_names
) override
{
64 DCHECK(device_names
->empty());
65 if (HasAudioInputDevices()) {
66 AudioManagerBase::GetAudioInputDeviceNames(device_names
);
68 device_names
->push_back(media::AudioDeviceName("fake_device_name",
74 media::FakeAudioLogFactory fake_audio_log_factory_
;
75 DISALLOW_COPY_AND_ASSIGN(MockAudioManager
);
78 class MediaStreamManagerTest
: public ::testing::Test
{
80 MediaStreamManagerTest()
81 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
),
82 task_runner_(base::ThreadTaskRunnerHandle::Get()) {
83 // Create our own MediaStreamManager. Use fake devices to run on the bots.
84 base::CommandLine::ForCurrentProcess()->AppendSwitch(
85 switches::kUseFakeDeviceForMediaStream
);
86 audio_manager_
.reset(new MockAudioManager());
87 media_stream_manager_
.reset(new MediaStreamManager(audio_manager_
.get()));
90 virtual ~MediaStreamManagerTest() {
93 MOCK_METHOD1(Response
, void(int index
));
94 void ResponseCallback(int index
,
95 const MediaStreamDevices
& devices
,
96 scoped_ptr
<MediaStreamUIProxy
> ui_proxy
) {
98 task_runner_
->PostTask(FROM_HERE
, run_loop_
.QuitClosure());
102 std::string
MakeMediaAccessRequest(int index
) {
103 const int render_process_id
= 1;
104 const int render_frame_id
= 1;
105 const int page_request_id
= 1;
106 const GURL security_origin
;
107 MediaStreamManager::MediaRequestResponseCallback callback
=
108 base::Bind(&MediaStreamManagerTest::ResponseCallback
,
109 base::Unretained(this), index
);
110 StreamOptions
options(true, true);
111 return media_stream_manager_
->MakeMediaAccessRequest(render_process_id
,
119 scoped_ptr
<media::AudioManager
> audio_manager_
;
120 scoped_ptr
<MediaStreamManager
> media_stream_manager_
;
121 content::TestBrowserThreadBundle thread_bundle_
;
122 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
123 base::RunLoop run_loop_
;
126 DISALLOW_COPY_AND_ASSIGN(MediaStreamManagerTest
);
129 TEST_F(MediaStreamManagerTest
, MakeMediaAccessRequest
) {
130 MakeMediaAccessRequest(0);
132 // Expecting the callback will be triggered and quit the test.
133 EXPECT_CALL(*this, Response(0));
137 TEST_F(MediaStreamManagerTest
, MakeAndCancelMediaAccessRequest
) {
138 std::string label
= MakeMediaAccessRequest(0);
139 // No callback is expected.
140 media_stream_manager_
->CancelRequest(label
);
141 run_loop_
.RunUntilIdle();
142 media_stream_manager_
->WillDestroyCurrentMessageLoop();
145 TEST_F(MediaStreamManagerTest
, MakeMultipleRequests
) {
147 std::string label1
= MakeMediaAccessRequest(0);
150 int render_process_id
= 2;
151 int render_frame_id
= 2;
152 int page_request_id
= 2;
153 GURL security_origin
;
154 StreamOptions
options(true, true);
155 MediaStreamManager::MediaRequestResponseCallback callback
=
156 base::Bind(&MediaStreamManagerTest::ResponseCallback
,
157 base::Unretained(this), 1);
158 std::string label2
= media_stream_manager_
->MakeMediaAccessRequest(
166 // Expecting the callbackS from requests will be triggered and quit the test.
167 // Note, the callbacks might come in a different order depending on the
169 EXPECT_CALL(*this, Response(0));
170 EXPECT_CALL(*this, Response(1));
174 TEST_F(MediaStreamManagerTest
, MakeAndCancelMultipleRequests
) {
175 std::string label1
= MakeMediaAccessRequest(0);
176 std::string label2
= MakeMediaAccessRequest(1);
177 media_stream_manager_
->CancelRequest(label1
);
179 // Expecting the callback from the second request will be triggered and
181 EXPECT_CALL(*this, Response(1));
185 } // namespace content