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 #include "media/audio/fake_audio_log_factory.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
21 #include "media/audio/alsa/audio_manager_alsa.h"
22 #elif defined(OS_ANDROID)
23 #include "media/audio/android/audio_manager_android.h"
24 #elif defined(OS_MACOSX)
25 #include "media/audio/mac/audio_manager_mac.h"
27 #include "media/audio/win/audio_manager_win.h"
29 #include "media/audio/fake_audio_manager.h"
37 typedef media::AudioManagerAlsa AudioManagerPlatform
;
38 #elif defined(OS_MACOSX)
39 typedef media::AudioManagerMac AudioManagerPlatform
;
41 typedef media::AudioManagerWin AudioManagerPlatform
;
42 #elif defined(OS_ANDROID)
43 typedef media::AudioManagerAndroid AudioManagerPlatform
;
45 typedef media::FakeAudioManager AudioManagerPlatform
;
49 // This class mocks the audio manager and overrides the
50 // GetAudioInputDeviceNames() method to ensure that we can run our tests on
51 // the buildbots. media::AudioManagerBase
52 class MockAudioManager
: public AudioManagerPlatform
{
54 MockAudioManager() : AudioManagerPlatform(&fake_audio_log_factory_
) {}
55 virtual ~MockAudioManager() {}
57 virtual void GetAudioInputDeviceNames(
58 media::AudioDeviceNames
* device_names
) OVERRIDE
{
59 DCHECK(device_names
->empty());
60 if (HasAudioInputDevices()) {
61 AudioManagerBase::GetAudioInputDeviceNames(device_names
);
63 device_names
->push_back(media::AudioDeviceName("fake_device_name",
69 media::FakeAudioLogFactory fake_audio_log_factory_
;
70 DISALLOW_COPY_AND_ASSIGN(MockAudioManager
);
73 class MediaStreamManagerTest
: public ::testing::Test
{
75 MediaStreamManagerTest()
76 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
),
77 message_loop_(base::MessageLoopProxy::current()) {
78 // Create our own MediaStreamManager.
79 audio_manager_
.reset(new MockAudioManager());
80 media_stream_manager_
.reset(new MediaStreamManager(audio_manager_
.get()));
82 // Use fake devices in order to run on the bots.
83 media_stream_manager_
->UseFakeDevice();
86 virtual ~MediaStreamManagerTest() {
89 MOCK_METHOD1(Response
, void(int index
));
90 void ResponseCallback(int index
,
91 const MediaStreamDevices
& devices
,
92 scoped_ptr
<MediaStreamUIProxy
> ui_proxy
) {
94 message_loop_
->PostTask(FROM_HERE
, run_loop_
.QuitClosure());
98 std::string
MakeMediaAccessRequest(int index
) {
99 const int render_process_id
= 1;
100 const int render_view_id
= 1;
101 const int page_request_id
= 1;
102 const GURL security_origin
;
103 MediaStreamManager::MediaRequestResponseCallback callback
=
104 base::Bind(&MediaStreamManagerTest::ResponseCallback
,
105 base::Unretained(this), index
);
106 StreamOptions
options(true, true);
107 return media_stream_manager_
->MakeMediaAccessRequest(render_process_id
,
115 scoped_ptr
<media::AudioManager
> audio_manager_
;
116 scoped_ptr
<MediaStreamManager
> media_stream_manager_
;
117 content::TestBrowserThreadBundle thread_bundle_
;
118 scoped_refptr
<base::MessageLoopProxy
> message_loop_
;
119 base::RunLoop run_loop_
;
122 DISALLOW_COPY_AND_ASSIGN(MediaStreamManagerTest
);
125 TEST_F(MediaStreamManagerTest
, MakeMediaAccessRequest
) {
126 MakeMediaAccessRequest(0);
128 // Expecting the callback will be triggered and quit the test.
129 EXPECT_CALL(*this, Response(0));
133 TEST_F(MediaStreamManagerTest
, MakeAndCancelMediaAccessRequest
) {
134 std::string label
= MakeMediaAccessRequest(0);
135 // No callback is expected.
136 media_stream_manager_
->CancelRequest(label
);
137 run_loop_
.RunUntilIdle();
138 media_stream_manager_
->WillDestroyCurrentMessageLoop();
141 TEST_F(MediaStreamManagerTest
, MakeMultipleRequests
) {
143 std::string label1
= MakeMediaAccessRequest(0);
146 int render_process_id
= 2;
147 int render_view_id
= 2;
148 int page_request_id
= 2;
149 GURL security_origin
;
150 StreamOptions
options(true, true);
151 MediaStreamManager::MediaRequestResponseCallback callback
=
152 base::Bind(&MediaStreamManagerTest::ResponseCallback
,
153 base::Unretained(this), 1);
154 std::string label2
= media_stream_manager_
->MakeMediaAccessRequest(
162 // Expecting the callbackS from requests will be triggered and quit the test.
163 // Note, the callbacks might come in a different order depending on the
165 EXPECT_CALL(*this, Response(0));
166 EXPECT_CALL(*this, Response(1));
170 TEST_F(MediaStreamManagerTest
, MakeAndCancelMultipleRequests
) {
171 std::string label1
= MakeMediaAccessRequest(0);
172 std::string label2
= MakeMediaAccessRequest(1);
173 media_stream_manager_
->CancelRequest(label1
);
175 // Expecting the callback from the second request will be triggered and
177 EXPECT_CALL(*this, Response(1));
181 } // namespace content