Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / renderer_host / media / media_stream_manager_unittest.cc
blobfc0028c0a7669faf821b1907404c1b7a585304bc
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.
5 #include <string>
7 #include "base/bind.h"
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"
25 #if defined(USE_ALSA)
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"
31 #elif defined(OS_WIN)
32 #include "media/audio/win/audio_manager_win.h"
33 #else
34 #include "media/audio/fake_audio_manager.h"
35 #endif
37 using testing::_;
39 namespace content {
41 #if defined(USE_ALSA)
42 typedef media::AudioManagerAlsa AudioManagerPlatform;
43 #elif defined(OS_MACOSX)
44 typedef media::AudioManagerMac AudioManagerPlatform;
45 #elif defined(OS_WIN)
46 typedef media::AudioManagerWin AudioManagerPlatform;
47 #elif defined(OS_ANDROID)
48 typedef media::AudioManagerAndroid AudioManagerPlatform;
49 #else
50 typedef media::FakeAudioManager AudioManagerPlatform;
51 #endif
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 {
58 public:
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);
67 } else {
68 device_names->push_back(media::AudioDeviceName("fake_device_name",
69 "fake_device_id"));
73 private:
74 media::FakeAudioLogFactory fake_audio_log_factory_;
75 DISALLOW_COPY_AND_ASSIGN(MockAudioManager);
78 class MediaStreamManagerTest : public ::testing::Test {
79 public:
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) {
97 Response(index);
98 task_runner_->PostTask(FROM_HERE, run_loop_.QuitClosure());
101 protected:
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,
112 render_frame_id,
113 page_request_id,
114 options,
115 security_origin,
116 callback);
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_;
125 private:
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));
134 run_loop_.Run();
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) {
146 // First request.
147 std::string label1 = MakeMediaAccessRequest(0);
149 // Second request.
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(
159 render_process_id,
160 render_frame_id,
161 page_request_id,
162 options,
163 security_origin,
164 callback);
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
168 // value of labels.
169 EXPECT_CALL(*this, Response(0));
170 EXPECT_CALL(*this, Response(1));
171 run_loop_.Run();
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
180 // quit the test.
181 EXPECT_CALL(*this, Response(1));
182 run_loop_.Run();
185 } // namespace content