Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / media / media_stream_manager_unittest.cc
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/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"
21
22 #if defined(USE_ALSA)
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"
28 #elif defined(OS_WIN)
29 #include "media/audio/win/audio_manager_win.h"
30 #else
31 #include "media/audio/fake_audio_manager.h"
32 #endif
33
34 using testing::_;
35
36 namespace content {
37
38 #if defined(USE_ALSA)
39 typedef media::AudioManagerAlsa AudioManagerPlatform;
40 #elif defined(OS_MACOSX)
41 typedef media::AudioManagerMac AudioManagerPlatform;
42 #elif defined(OS_WIN)
43 typedef media::AudioManagerWin AudioManagerPlatform;
44 #elif defined(OS_ANDROID)
45 typedef media::AudioManagerAndroid AudioManagerPlatform;
46 #else
47 typedef media::FakeAudioManager AudioManagerPlatform;
48 #endif
49
50
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 {
55  public:
56   MockAudioManager() : AudioManagerPlatform(&fake_audio_log_factory_) {}
57   virtual ~MockAudioManager() {}
58
59   virtual void GetAudioInputDeviceNames(
60       media::AudioDeviceNames* device_names) OVERRIDE {
61     DCHECK(device_names->empty());
62     if (HasAudioInputDevices()) {
63       AudioManagerBase::GetAudioInputDeviceNames(device_names);
64     } else {
65       device_names->push_back(media::AudioDeviceName("fake_device_name",
66                                                      "fake_device_id"));
67     }
68   }
69
70  private:
71   media::FakeAudioLogFactory fake_audio_log_factory_;
72   DISALLOW_COPY_AND_ASSIGN(MockAudioManager);
73 };
74
75 class MediaStreamManagerTest : public ::testing::Test {
76  public:
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()));
85 }
86
87   virtual ~MediaStreamManagerTest() {
88   }
89
90   MOCK_METHOD1(Response, void(int index));
91   void ResponseCallback(int index,
92                         const MediaStreamDevices& devices,
93                         scoped_ptr<MediaStreamUIProxy> ui_proxy) {
94     Response(index);
95     message_loop_->PostTask(FROM_HERE, run_loop_.QuitClosure());
96   }
97
98  protected:
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,
109                                                          render_view_id,
110                                                          page_request_id,
111                                                          options,
112                                                          security_origin,
113                                                          callback);
114   }
115
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_;
121
122  private:
123   DISALLOW_COPY_AND_ASSIGN(MediaStreamManagerTest);
124 };
125
126 TEST_F(MediaStreamManagerTest, MakeMediaAccessRequest) {
127   MakeMediaAccessRequest(0);
128
129   // Expecting the callback will be triggered and quit the test.
130   EXPECT_CALL(*this, Response(0));
131   run_loop_.Run();
132 }
133
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();
140 }
141
142 TEST_F(MediaStreamManagerTest, MakeMultipleRequests) {
143   // First request.
144   std::string label1 =  MakeMediaAccessRequest(0);
145
146   // Second request.
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(
156       render_process_id,
157       render_view_id,
158       page_request_id,
159       options,
160       security_origin,
161       callback);
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(0));
167   EXPECT_CALL(*this, Response(1));
168   run_loop_.Run();
169 }
170
171 TEST_F(MediaStreamManagerTest, MakeAndCancelMultipleRequests) {
172   std::string label1 = MakeMediaAccessRequest(0);
173   std::string label2 = MakeMediaAccessRequest(1);
174   media_stream_manager_->CancelRequest(label1);
175
176   // Expecting the callback from the second request will be triggered and
177   // quit the test.
178   EXPECT_CALL(*this, Response(1));
179   run_loop_.Run();
180 }
181
182 }  // namespace content