Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / content / browser / renderer_host / media / media_stream_manager_unittest.cc
blobac6f722aa91ede2fb5ba4b352e041cc7a16555be
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/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"
22 #elif defined(OS_WIN)
23 #include "media/audio/win/audio_manager_win.h"
24 #endif
25 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h"
28 using testing::_;
30 namespace content {
32 #if defined(OS_LINUX) || defined(OS_OPENBSD)
33 typedef media::AudioManagerLinux AudioManagerPlatform;
34 #elif defined(OS_MACOSX)
35 typedef media::AudioManagerMac AudioManagerPlatform;
36 #elif defined(OS_WIN)
37 typedef media::AudioManagerWin AudioManagerPlatform;
38 #elif defined(OS_ANDROID)
39 typedef media::AudioManagerAndroid AudioManagerPlatform;
40 #endif
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 {
47 public:
48 MockAudioManager() {}
49 virtual ~MockAudioManager() {}
51 virtual void GetAudioInputDeviceNames(
52 media::AudioDeviceNames* device_names) OVERRIDE {
53 if (HasAudioInputDevices()) {
54 AudioManagerBase::GetAudioInputDeviceNames(device_names);
55 } else {
56 device_names->push_back(media::AudioDeviceName("fake_device_name",
57 "fake_device_id"));
61 private:
62 DISALLOW_COPY_AND_ASSIGN(MockAudioManager);
65 class MediaStreamManagerTest : public ::testing::Test {
66 public:
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) {
84 Response(index);
85 message_loop_->PostTask(FROM_HERE, run_loop_.QuitClosure());
88 protected:
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,
99 render_view_id,
100 components,
101 security_origin,
102 callback);
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_;
111 private:
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));
120 run_loop_.Run();
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) {
130 // First request.
131 std::string label1 = MakeMediaAccessRequest(0);
133 // Second request.
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(
143 render_process_id,
144 render_view_id,
145 components,
146 security_origin,
147 callback);
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
151 // value of labels.
152 EXPECT_CALL(*this, Response(0));
153 EXPECT_CALL(*this, Response(1));
154 run_loop_.Run();
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
163 // quit the test.
164 EXPECT_CALL(*this, Response(1));
165 run_loop_.Run();
168 } // namespace content