media_stream_manager_unittest.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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. 4 5#include <string> 6 7#include "base/bind.h" 8#include "base/message_loop.h" 9#include "content/browser/browser_thread_impl.h" 10#include "content/browser/renderer_host/media/media_stream_manager.h" 11#include "content/common/media/media_stream_options.h" 12#include "media/audio/audio_manager_base.h" 13#if defined(OS_ANDROID) 14#include "media/audio/android/audio_manager_android.h" 15#elif defined(OS_LINUX) || defined(OS_OPENBSD) 16#include "media/audio/linux/audio_manager_linux.h" 17#elif defined(OS_MACOSX) 18#include "media/audio/mac/audio_manager_mac.h" 19#elif defined(OS_WIN) 20#include "media/audio/win/audio_manager_win.h" 21#endif 22#include "testing/gmock/include/gmock/gmock.h" 23#include "testing/gtest/include/gtest/gtest.h" 24 25using testing::_; 26 27namespace content { 28 29#if defined(OS_LINUX) || defined(OS_OPENBSD) 30typedef media::AudioManagerLinux AudioManagerPlatform; 31#elif defined(OS_MACOSX) 32typedef media::AudioManagerMac AudioManagerPlatform; 33#elif defined(OS_WIN) 34typedef media::AudioManagerWin AudioManagerPlatform; 35#elif defined(OS_ANDROID) 36typedef media::AudioManagerAndroid AudioManagerPlatform; 37#endif 38 39 40// This class mocks the audio manager and overrides the 41// GetAudioInputDeviceNames() method to ensure that we can run our tests on 42// the buildbots. media::AudioManagerBase 43class MockAudioManager : public AudioManagerPlatform { 44 public: 45 MockAudioManager() {} 46 virtual ~MockAudioManager() {} 47 48 virtual void GetAudioInputDeviceNames( 49 media::AudioDeviceNames* device_names) OVERRIDE { 50 if (HasAudioInputDevices()) { 51 AudioManagerBase::GetAudioInputDeviceNames(device_names); 52 } else { 53 device_names->push_back(media::AudioDeviceName("fake_device_name", 54 "fake_device_id")); 55 } 56 } 57 58 private: 59 DISALLOW_COPY_AND_ASSIGN(MockAudioManager); 60}; 61 62class MediaStreamManagerTest : public ::testing::Test { 63 public: 64 MediaStreamManagerTest() {} 65 66 MOCK_METHOD1(Response, void(const std::string&)); 67 void ResponseCallback(const std::string& label, 68 const MediaStreamDevices& devices) { 69 Response(label); 70 message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); 71 } 72 73 void WaitForResult() { message_loop_->Run(); } 74 75 protected: 76 virtual void SetUp() { 77 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); 78 ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI, 79 message_loop_.get())); 80 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO, 81 message_loop_.get())); 82 83 // Create our own MediaStreamManager. 84 audio_manager_.reset(new MockAudioManager()); 85 media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get())); 86 87 // Use fake devices in order to run on the bots. 88 media_stream_manager_->UseFakeDevice(); 89 } 90 91 virtual void TearDown() { 92 message_loop_->RunUntilIdle(); 93 94 // Delete the IO message loop to clean up the MediaStreamManager. 95 message_loop_.reset(); 96 } 97 98 void MakeMediaAccessRequest(std::string* label) { 99 const int render_process_id = 1; 100 const int render_view_id = 1; 101 StreamOptions components(MEDIA_DEVICE_AUDIO_CAPTURE, 102 MEDIA_DEVICE_VIDEO_CAPTURE); 103 const GURL security_origin; 104 MediaRequestResponseCallback callback = 105 base::Bind(&MediaStreamManagerTest::ResponseCallback, 106 base::Unretained(this)); 107 media_stream_manager_->MakeMediaAccessRequest(render_process_id, 108 render_view_id, 109 components, 110 security_origin, 111 callback, 112 label); 113 } 114 115 scoped_ptr<MessageLoop> message_loop_; 116 scoped_ptr<BrowserThreadImpl> ui_thread_; 117 scoped_ptr<BrowserThreadImpl> io_thread_; 118 scoped_ptr<media::AudioManager> audio_manager_; 119 scoped_ptr<MediaStreamManager> media_stream_manager_; 120 121 private: 122 DISALLOW_COPY_AND_ASSIGN(MediaStreamManagerTest); 123}; 124 125TEST_F(MediaStreamManagerTest, MakeMediaAccessRequest) { 126 std::string label; 127 MakeMediaAccessRequest(&label); 128 129 // Expecting the callback will be triggered and quit the test. 130 EXPECT_CALL(*this, Response(label)); 131 WaitForResult(); 132} 133 134TEST_F(MediaStreamManagerTest, MakeAndCancelMediaAccessRequest) { 135 std::string label; 136 MakeMediaAccessRequest(&label); 137 // No callback is expected. 138 media_stream_manager_->CancelRequest(label); 139} 140 141TEST_F(MediaStreamManagerTest, MakeMultipleRequests) { 142 // First request. 143 std::string label1; 144 MakeMediaAccessRequest(&label1); 145 146 // Second request. 147 std::string label2; 148 int render_process_id = 2; 149 int render_view_id = 2; 150 StreamOptions components(MEDIA_DEVICE_AUDIO_CAPTURE, 151 MEDIA_DEVICE_VIDEO_CAPTURE); 152 GURL security_origin; 153 MediaRequestResponseCallback callback = 154 base::Bind(&MediaStreamManagerTest::ResponseCallback, 155 base::Unretained(this)); 156 media_stream_manager_->MakeMediaAccessRequest(render_process_id, 157 render_view_id, 158 components, 159 security_origin, 160 callback, 161 &label2); 162 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 165 // value of labels. 166 EXPECT_CALL(*this, Response(_)).Times(2); 167 WaitForResult(); 168} 169 170TEST_F(MediaStreamManagerTest, MakeAndCancelMultipleRequests) { 171 std::string label1; 172 MakeMediaAccessRequest(&label1); 173 std::string label2; 174 MakeMediaAccessRequest(&label2); 175 media_stream_manager_->CancelRequest(label1); 176 177 // Expecting the callback from the second request will be triggered and 178 // quit the test. 179 EXPECT_CALL(*this, Response(label2)); 180 WaitForResult(); 181} 182 183} // namespace content 184