[MacViews] Show comboboxes with a native NSMenu
[chromium-blink-merge.git] / content / browser / renderer_host / media / audio_renderer_host_unittest.cc
blob757771847bded34b268eb56071cd931ef459f868
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 "base/bind.h"
6 #include "base/command_line.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/run_loop.h"
9 #include "base/sync_socket.h"
10 #include "content/browser/media/capture/audio_mirroring_manager.h"
11 #include "content/browser/media/media_internals.h"
12 #include "content/browser/renderer_host/media/audio_input_device_manager.h"
13 #include "content/browser/renderer_host/media/audio_renderer_host.h"
14 #include "content/browser/renderer_host/media/media_stream_manager.h"
15 #include "content/common/media/audio_messages.h"
16 #include "content/public/common/content_switches.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "ipc/ipc_message_utils.h"
19 #include "media/audio/audio_manager.h"
20 #include "media/base/bind_to_current_loop.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 using ::testing::_;
26 using ::testing::Assign;
27 using ::testing::DoAll;
28 using ::testing::NotNull;
30 namespace {
31 const int kRenderProcessId = 1;
32 const int kRenderFrameId = 5;
33 const int kStreamId = 50;
34 const int kBadStreamId = 99;
35 const int kSwitchOutputDeviceRequestId = 1;
36 const GURL kSecurityOrigin("http://localhost");
37 const std::string kDefaultDeviceID = "";
38 const std::string kBadDeviceID = "bad-device-id";
39 } // namespace
41 namespace content {
43 class MockAudioMirroringManager : public AudioMirroringManager {
44 public:
45 MockAudioMirroringManager() {}
46 virtual ~MockAudioMirroringManager() {}
48 MOCK_METHOD3(AddDiverter,
49 void(int render_process_id,
50 int render_frame_id,
51 Diverter* diverter));
52 MOCK_METHOD1(RemoveDiverter, void(Diverter* diverter));
54 private:
55 DISALLOW_COPY_AND_ASSIGN(MockAudioMirroringManager);
58 class MockAudioRendererHost : public AudioRendererHost {
59 public:
60 MockAudioRendererHost(media::AudioManager* audio_manager,
61 AudioMirroringManager* mirroring_manager,
62 MediaInternals* media_internals,
63 MediaStreamManager* media_stream_manager,
64 const ResourceContext::SaltCallback& salt_callback)
65 : AudioRendererHost(kRenderProcessId,
66 audio_manager,
67 mirroring_manager,
68 media_internals,
69 media_stream_manager,
70 salt_callback),
71 shared_memory_length_(0) {}
73 // A list of mock methods.
74 MOCK_METHOD2(OnStreamCreated, void(int stream_id, int length));
75 MOCK_METHOD1(OnStreamPlaying, void(int stream_id));
76 MOCK_METHOD1(OnStreamPaused, void(int stream_id));
77 MOCK_METHOD1(OnStreamError, void(int stream_id));
78 MOCK_METHOD3(OnOutputDeviceSwitched,
79 void(int stream_id,
80 int request_id,
81 media::SwitchOutputDeviceResult result));
83 private:
84 virtual ~MockAudioRendererHost() {
85 // Make sure all audio streams have been deleted.
86 EXPECT_TRUE(audio_entries_.empty());
89 // This method is used to dispatch IPC messages to the renderer. We intercept
90 // these messages here and dispatch to our mock methods to verify the
91 // conversation between this object and the renderer.
92 virtual bool Send(IPC::Message* message) {
93 CHECK(message);
95 // In this method we dispatch the messages to the according handlers as if
96 // we are the renderer.
97 bool handled = true;
98 IPC_BEGIN_MESSAGE_MAP(MockAudioRendererHost, *message)
99 IPC_MESSAGE_HANDLER(AudioMsg_NotifyStreamCreated,
100 OnNotifyStreamCreated)
101 IPC_MESSAGE_HANDLER(AudioMsg_NotifyStreamStateChanged,
102 OnNotifyStreamStateChanged)
103 IPC_MESSAGE_HANDLER(AudioMsg_NotifyOutputDeviceSwitched,
104 OnNotifyOutputDeviceSwitched)
105 IPC_MESSAGE_UNHANDLED(handled = false)
106 IPC_END_MESSAGE_MAP()
107 EXPECT_TRUE(handled);
109 delete message;
110 return true;
113 void OnNotifyStreamCreated(
114 int stream_id, base::SharedMemoryHandle handle,
115 base::SyncSocket::TransitDescriptor socket_descriptor, uint32 length) {
116 // Maps the shared memory.
117 shared_memory_.reset(new base::SharedMemory(handle, false));
118 CHECK(shared_memory_->Map(length));
119 CHECK(shared_memory_->memory());
120 shared_memory_length_ = length;
122 // Create the SyncSocket using the handle.
123 base::SyncSocket::Handle sync_socket_handle =
124 base::SyncSocket::UnwrapHandle(socket_descriptor);
125 sync_socket_.reset(new base::SyncSocket(sync_socket_handle));
127 // And then delegate the call to the mock method.
128 OnStreamCreated(stream_id, length);
131 void OnNotifyStreamStateChanged(int stream_id,
132 media::AudioOutputIPCDelegateState state) {
133 switch (state) {
134 case media::AUDIO_OUTPUT_IPC_DELEGATE_STATE_PLAYING:
135 OnStreamPlaying(stream_id);
136 break;
137 case media::AUDIO_OUTPUT_IPC_DELEGATE_STATE_PAUSED:
138 OnStreamPaused(stream_id);
139 break;
140 case media::AUDIO_OUTPUT_IPC_DELEGATE_STATE_ERROR:
141 OnStreamError(stream_id);
142 break;
143 default:
144 FAIL() << "Unknown stream state";
145 break;
149 void OnNotifyOutputDeviceSwitched(int stream_id,
150 int request_id,
151 media::SwitchOutputDeviceResult result) {
152 switch (result) {
153 case media::SWITCH_OUTPUT_DEVICE_RESULT_SUCCESS:
154 case media::SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_FOUND:
155 case media::SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_AUTHORIZED:
156 case media::SWITCH_OUTPUT_DEVICE_RESULT_ERROR_OBSOLETE:
157 case media::SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_SUPPORTED:
158 OnOutputDeviceSwitched(stream_id, request_id, result);
159 break;
160 default:
161 FAIL() << "Unknown SwitchOutputDevice result";
162 break;
166 scoped_ptr<base::SharedMemory> shared_memory_;
167 scoped_ptr<base::SyncSocket> sync_socket_;
168 uint32 shared_memory_length_;
170 DISALLOW_COPY_AND_ASSIGN(MockAudioRendererHost);
173 namespace {
174 std::string ReturnMockSalt() {
175 return std::string();
178 ResourceContext::SaltCallback GetMockSaltCallback() {
179 return base::Bind(&ReturnMockSalt);
183 class AudioRendererHostTest : public testing::Test {
184 public:
185 AudioRendererHostTest() {
186 audio_manager_.reset(media::AudioManager::CreateForTesting());
187 base::CommandLine::ForCurrentProcess()->AppendSwitch(
188 switches::kUseFakeDeviceForMediaStream);
189 media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get()));
190 host_ = new MockAudioRendererHost(audio_manager_.get(), &mirroring_manager_,
191 MediaInternals::GetInstance(),
192 media_stream_manager_.get(),
193 GetMockSaltCallback());
195 // Simulate IPC channel connected.
196 host_->set_peer_process_for_testing(base::Process::Current());
199 ~AudioRendererHostTest() override {
200 // Simulate closing the IPC channel and give the audio thread time to close
201 // the underlying streams.
202 host_->OnChannelClosing();
203 SyncWithAudioThread();
205 // Release the reference to the mock object. The object will be destructed
206 // on message_loop_.
207 host_ = NULL;
210 protected:
211 void Create(bool unified_stream) {
212 EXPECT_CALL(*host_.get(), OnStreamCreated(kStreamId, _));
214 EXPECT_CALL(mirroring_manager_,
215 AddDiverter(kRenderProcessId, kRenderFrameId, NotNull()))
216 .RetiresOnSaturation();
218 // Send a create stream message to the audio output stream and wait until
219 // we receive the created message.
220 media::AudioParameters params(
221 media::AudioParameters::AUDIO_FAKE, media::CHANNEL_LAYOUT_STEREO,
222 media::AudioParameters::kAudioCDSampleRate, 16,
223 media::AudioParameters::kAudioCDSampleRate / 10);
224 int session_id = 0;
225 if (unified_stream) {
226 // Use AudioInputDeviceManager::kFakeOpenSessionId as the session id to
227 // pass the permission check.
228 session_id = AudioInputDeviceManager::kFakeOpenSessionId;
230 host_->OnCreateStream(kStreamId, kRenderFrameId, session_id, params);
232 // At some point in the future, a corresponding RemoveDiverter() call must
233 // be made.
234 EXPECT_CALL(mirroring_manager_, RemoveDiverter(NotNull()))
235 .RetiresOnSaturation();
236 SyncWithAudioThread();
239 void Close() {
240 // Send a message to AudioRendererHost to tell it we want to close the
241 // stream.
242 host_->OnCloseStream(kStreamId);
243 SyncWithAudioThread();
246 void Play() {
247 EXPECT_CALL(*host_.get(), OnStreamPlaying(kStreamId));
248 host_->OnPlayStream(kStreamId);
249 SyncWithAudioThread();
252 void Pause() {
253 EXPECT_CALL(*host_.get(), OnStreamPaused(kStreamId));
254 host_->OnPauseStream(kStreamId);
255 SyncWithAudioThread();
258 void SetVolume(double volume) {
259 host_->OnSetVolume(kStreamId, volume);
260 SyncWithAudioThread();
263 void SwitchOutputDevice(int stream_id,
264 std::string device_id,
265 media::SwitchOutputDeviceResult expected_result) {
266 EXPECT_CALL(*host_.get(),
267 OnOutputDeviceSwitched(stream_id, kSwitchOutputDeviceRequestId,
268 expected_result));
269 host_->OnSwitchOutputDevice(stream_id, kRenderFrameId, device_id,
270 kSecurityOrigin, kSwitchOutputDeviceRequestId);
271 SyncWithAudioThread();
274 void SimulateError() {
275 EXPECT_EQ(1u, host_->audio_entries_.size())
276 << "Calls Create() before calling this method";
278 // Expect an error signal sent through IPC.
279 EXPECT_CALL(*host_.get(), OnStreamError(kStreamId));
281 // Simulate an error sent from the audio device.
282 host_->ReportErrorAndClose(kStreamId);
283 SyncWithAudioThread();
285 // Expect the audio stream record is removed.
286 EXPECT_EQ(0u, host_->audio_entries_.size());
289 // SyncWithAudioThread() waits until all pending tasks on the audio thread
290 // are executed while also processing pending task in message_loop_ on the
291 // current thread. It is used to synchronize with the audio thread when we are
292 // closing an audio stream.
293 void SyncWithAudioThread() {
294 base::RunLoop().RunUntilIdle();
296 base::RunLoop run_loop;
297 audio_manager_->GetTaskRunner()->PostTask(
298 FROM_HERE, media::BindToCurrentLoop(run_loop.QuitClosure()));
299 run_loop.Run();
302 private:
303 // MediaStreamManager uses a DestructionObserver, so it must outlive the
304 // TestBrowserThreadBundle.
305 scoped_ptr<MediaStreamManager> media_stream_manager_;
306 TestBrowserThreadBundle thread_bundle_;
307 scoped_ptr<media::AudioManager> audio_manager_;
308 MockAudioMirroringManager mirroring_manager_;
309 scoped_refptr<MockAudioRendererHost> host_;
311 DISALLOW_COPY_AND_ASSIGN(AudioRendererHostTest);
314 TEST_F(AudioRendererHostTest, CreateAndClose) {
315 Create(false);
316 Close();
319 // Simulate the case where a stream is not properly closed.
320 TEST_F(AudioRendererHostTest, CreateAndShutdown) {
321 Create(false);
324 TEST_F(AudioRendererHostTest, CreatePlayAndClose) {
325 Create(false);
326 Play();
327 Close();
330 TEST_F(AudioRendererHostTest, CreatePlayPauseAndClose) {
331 Create(false);
332 Play();
333 Pause();
334 Close();
337 TEST_F(AudioRendererHostTest, SetVolume) {
338 Create(false);
339 SetVolume(0.5);
340 Play();
341 Pause();
342 Close();
345 TEST_F(AudioRendererHostTest, SwitchOutputDevice) {
346 Create(false);
347 SwitchOutputDevice(kStreamId, kDefaultDeviceID,
348 media::SWITCH_OUTPUT_DEVICE_RESULT_SUCCESS);
349 Close();
352 TEST_F(AudioRendererHostTest, SwitchOutputDeviceNotAuthorized) {
353 Create(false);
354 SwitchOutputDevice(kStreamId, kBadDeviceID,
355 media::SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_AUTHORIZED);
356 Close();
359 TEST_F(AudioRendererHostTest, SwitchOutputDeviceNoStream) {
360 Create(false);
361 SwitchOutputDevice(kBadStreamId, kDefaultDeviceID,
362 media::SWITCH_OUTPUT_DEVICE_RESULT_ERROR_OBSOLETE);
363 Close();
366 // Simulate the case where a stream is not properly closed.
367 TEST_F(AudioRendererHostTest, CreatePlayAndShutdown) {
368 Create(false);
369 Play();
372 // Simulate the case where a stream is not properly closed.
373 TEST_F(AudioRendererHostTest, CreatePlayPauseAndShutdown) {
374 Create(false);
375 Play();
376 Pause();
379 TEST_F(AudioRendererHostTest, SimulateError) {
380 Create(false);
381 Play();
382 SimulateError();
385 // Simulate the case when an error is generated on the browser process,
386 // the audio device is closed but the render process try to close the
387 // audio stream again.
388 TEST_F(AudioRendererHostTest, SimulateErrorAndClose) {
389 Create(false);
390 Play();
391 SimulateError();
392 Close();
395 TEST_F(AudioRendererHostTest, CreateUnifiedStreamAndClose) {
396 Create(true);
397 Close();
400 // TODO(hclam): Add tests for data conversation in low latency mode.
402 } // namespace content