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.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 if (HasAudioInputDevices()) {
54 AudioManagerBase::GetAudioInputDeviceNames(device_names
);
56 device_names
->push_back(media::AudioDeviceName("fake_device_name",
62 DISALLOW_COPY_AND_ASSIGN(MockAudioManager
);
65 class MediaStreamManagerTest
: public ::testing::Test
{
67 MediaStreamManagerTest()
68 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
),
69 message_loop_(base::MessageLoopProxy::current()) {
70 // Create our own MediaStreamManager.
71 audio_manager_
.reset(new MockAudioManager());
72 media_stream_manager_
.reset(new MediaStreamManager(audio_manager_
.get()));
74 // Use fake devices in order to run on the bots.
75 media_stream_manager_
->UseFakeDevice();
78 virtual ~MediaStreamManagerTest() {}
80 MOCK_METHOD1(Response
, void(int index
));
81 void ResponseCallback(int index
,
82 const MediaStreamDevices
& devices
,
83 scoped_ptr
<MediaStreamUIProxy
> ui_proxy
) {
85 message_loop_
->PostTask(FROM_HERE
, run_loop_
.QuitClosure());
89 std::string
MakeMediaAccessRequest(int index
) {
90 const int render_process_id
= 1;
91 const int render_view_id
= 1;
92 StreamOptions
components(MEDIA_DEVICE_AUDIO_CAPTURE
,
93 MEDIA_DEVICE_VIDEO_CAPTURE
);
94 const GURL security_origin
;
95 MediaStreamManager::MediaRequestResponseCallback callback
=
96 base::Bind(&MediaStreamManagerTest::ResponseCallback
,
97 base::Unretained(this), index
);
98 return media_stream_manager_
->MakeMediaAccessRequest(render_process_id
,
105 scoped_ptr
<media::AudioManager
> audio_manager_
;
106 scoped_ptr
<MediaStreamManager
> media_stream_manager_
;
107 content::TestBrowserThreadBundle thread_bundle_
;
108 scoped_refptr
<base::MessageLoopProxy
> message_loop_
;
109 base::RunLoop run_loop_
;
112 DISALLOW_COPY_AND_ASSIGN(MediaStreamManagerTest
);
115 TEST_F(MediaStreamManagerTest
, MakeMediaAccessRequest
) {
116 MakeMediaAccessRequest(0);
118 // Expecting the callback will be triggered and quit the test.
119 EXPECT_CALL(*this, Response(0));
123 TEST_F(MediaStreamManagerTest
, MakeAndCancelMediaAccessRequest
) {
124 std::string label
= MakeMediaAccessRequest(0);
125 // No callback is expected.
126 media_stream_manager_
->CancelRequest(label
);
129 TEST_F(MediaStreamManagerTest
, MakeMultipleRequests
) {
131 std::string label1
= MakeMediaAccessRequest(0);
134 int render_process_id
= 2;
135 int render_view_id
= 2;
136 StreamOptions
components(MEDIA_DEVICE_AUDIO_CAPTURE
,
137 MEDIA_DEVICE_VIDEO_CAPTURE
);
138 GURL security_origin
;
139 MediaStreamManager::MediaRequestResponseCallback callback
=
140 base::Bind(&MediaStreamManagerTest::ResponseCallback
,
141 base::Unretained(this), 1);
142 std::string label2
= media_stream_manager_
->MakeMediaAccessRequest(
149 // Expecting the callbackS from requests will be triggered and quit the test.
150 // Note, the callbacks might come in a different order depending on the
152 EXPECT_CALL(*this, Response(0));
153 EXPECT_CALL(*this, Response(1));
157 TEST_F(MediaStreamManagerTest
, MakeAndCancelMultipleRequests
) {
158 std::string label1
= MakeMediaAccessRequest(0);
159 std::string label2
= MakeMediaAccessRequest(1);
160 media_stream_manager_
->CancelRequest(label1
);
162 // Expecting the callback from the second request will be triggered and
164 EXPECT_CALL(*this, Response(1));
168 } // namespace content