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/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "content/browser/browser_thread_impl.h"
12 #include "content/browser/renderer_host/media/media_stream_manager.h"
13 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h"
14 #include "content/common/media/media_stream_options.h"
15 #include "content/public/common/content_switches.h"
16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "media/audio/audio_manager_base.h"
18 #include "media/audio/fake_audio_log_factory.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
23 #include "media/audio/alsa/audio_manager_alsa.h"
24 #elif defined(OS_ANDROID)
25 #include "media/audio/android/audio_manager_android.h"
26 #elif defined(OS_MACOSX)
27 #include "media/audio/mac/audio_manager_mac.h"
29 #include "media/audio/win/audio_manager_win.h"
31 #include "media/audio/fake_audio_manager.h"
39 typedef media::AudioManagerAlsa AudioManagerPlatform
;
40 #elif defined(OS_MACOSX)
41 typedef media::AudioManagerMac AudioManagerPlatform
;
43 typedef media::AudioManagerWin AudioManagerPlatform
;
44 #elif defined(OS_ANDROID)
45 typedef media::AudioManagerAndroid AudioManagerPlatform
;
47 typedef media::FakeAudioManager AudioManagerPlatform
;
51 // This class mocks the audio manager and overrides the
52 // GetAudioInputDeviceNames() method to ensure that we can run our tests on
53 // the buildbots. media::AudioManagerBase
54 class MockAudioManager
: public AudioManagerPlatform
{
56 MockAudioManager() : AudioManagerPlatform(&fake_audio_log_factory_
) {}
57 virtual ~MockAudioManager() {}
59 virtual void GetAudioInputDeviceNames(
60 media::AudioDeviceNames
* device_names
) OVERRIDE
{
61 DCHECK(device_names
->empty());
62 if (HasAudioInputDevices()) {
63 AudioManagerBase::GetAudioInputDeviceNames(device_names
);
65 device_names
->push_back(media::AudioDeviceName("fake_device_name",
71 media::FakeAudioLogFactory fake_audio_log_factory_
;
72 DISALLOW_COPY_AND_ASSIGN(MockAudioManager
);
75 class MediaStreamManagerTest
: public ::testing::Test
{
77 MediaStreamManagerTest()
78 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
),
79 message_loop_(base::MessageLoopProxy::current()) {
80 // Create our own MediaStreamManager. Use fake devices to run on the bots.
81 CommandLine::ForCurrentProcess()->AppendSwitch(
82 switches::kUseFakeDeviceForMediaStream
);
83 audio_manager_
.reset(new MockAudioManager());
84 media_stream_manager_
.reset(new MediaStreamManager(audio_manager_
.get()));
87 virtual ~MediaStreamManagerTest() {
90 MOCK_METHOD1(Response
, void(int index
));
91 void ResponseCallback(int index
,
92 const MediaStreamDevices
& devices
,
93 scoped_ptr
<MediaStreamUIProxy
> ui_proxy
) {
95 message_loop_
->PostTask(FROM_HERE
, run_loop_
.QuitClosure());
99 std::string
MakeMediaAccessRequest(int index
) {
100 const int render_process_id
= 1;
101 const int render_view_id
= 1;
102 const int page_request_id
= 1;
103 const GURL security_origin
;
104 MediaStreamManager::MediaRequestResponseCallback callback
=
105 base::Bind(&MediaStreamManagerTest::ResponseCallback
,
106 base::Unretained(this), index
);
107 StreamOptions
options(true, true);
108 return media_stream_manager_
->MakeMediaAccessRequest(render_process_id
,
116 scoped_ptr
<media::AudioManager
> audio_manager_
;
117 scoped_ptr
<MediaStreamManager
> media_stream_manager_
;
118 content::TestBrowserThreadBundle thread_bundle_
;
119 scoped_refptr
<base::MessageLoopProxy
> message_loop_
;
120 base::RunLoop run_loop_
;
123 DISALLOW_COPY_AND_ASSIGN(MediaStreamManagerTest
);
126 TEST_F(MediaStreamManagerTest
, MakeMediaAccessRequest
) {
127 MakeMediaAccessRequest(0);
129 // Expecting the callback will be triggered and quit the test.
130 EXPECT_CALL(*this, Response(0));
134 TEST_F(MediaStreamManagerTest
, MakeAndCancelMediaAccessRequest
) {
135 std::string label
= MakeMediaAccessRequest(0);
136 // No callback is expected.
137 media_stream_manager_
->CancelRequest(label
);
138 run_loop_
.RunUntilIdle();
139 media_stream_manager_
->WillDestroyCurrentMessageLoop();
142 TEST_F(MediaStreamManagerTest
, MakeMultipleRequests
) {
144 std::string label1
= MakeMediaAccessRequest(0);
147 int render_process_id
= 2;
148 int render_view_id
= 2;
149 int page_request_id
= 2;
150 GURL security_origin
;
151 StreamOptions
options(true, true);
152 MediaStreamManager::MediaRequestResponseCallback callback
=
153 base::Bind(&MediaStreamManagerTest::ResponseCallback
,
154 base::Unretained(this), 1);
155 std::string label2
= media_stream_manager_
->MakeMediaAccessRequest(
163 // Expecting the callbackS from requests will be triggered and quit the test.
164 // Note, the callbacks might come in a different order depending on the
166 EXPECT_CALL(*this, Response(0));
167 EXPECT_CALL(*this, Response(1));
171 TEST_F(MediaStreamManagerTest
, MakeAndCancelMultipleRequests
) {
172 std::string label1
= MakeMediaAccessRequest(0);
173 std::string label2
= MakeMediaAccessRequest(1);
174 media_stream_manager_
->CancelRequest(label1
);
176 // Expecting the callback from the second request will be triggered and
178 EXPECT_CALL(*this, Response(1));
182 } // namespace content