Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(104)

Side by Side Diff: content/browser/renderer_host/media/audio_renderer_host_unittest.cc

Issue 11413078: Tab Audio Capture: Browser-side connect/disconnect functionality. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Moved Glue to cc file, and renamed to SourceSharingCallback. Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/bind.h" 5 #include "base/bind.h"
6 #include "base/environment.h" 6 #include "base/environment.h"
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/message_loop.h" 8 #include "base/message_loop.h"
9 #include "base/process_util.h" 9 #include "base/process_util.h"
10 #include "base/sync_socket.h" 10 #include "base/sync_socket.h"
11 #include "content/browser/browser_thread_impl.h" 11 #include "content/browser/browser_thread_impl.h"
12 #include "content/browser/renderer_host/media/audio_renderer_host.h" 12 #include "content/browser/renderer_host/media/audio_renderer_host.h"
13 #include "content/browser/renderer_host/media/mock_media_observer.h" 13 #include "content/browser/renderer_host/media/mock_media_observer.h"
14 #include "content/common/media/audio_messages.h" 14 #include "content/common/media/audio_messages.h"
15 #include "ipc/ipc_message_utils.h" 15 #include "ipc/ipc_message_utils.h"
16 #include "media/audio/audio_manager.h" 16 #include "media/audio/audio_manager.h"
17 #include "media/audio/fake_audio_output_stream.h" 17 #include "media/audio/fake_audio_output_stream.h"
18 #include "net/url_request/url_request_context.h" 18 #include "net/url_request/url_request_context.h"
19 #include "testing/gmock/include/gmock/gmock.h" 19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
21 21
22 using ::testing::_; 22 using ::testing::_;
23 using ::testing::AtLeast; 23 using ::testing::AtLeast;
24 using ::testing::DoAll; 24 using ::testing::DoAll;
25 using ::testing::InSequence; 25 using ::testing::InSequence;
26 using ::testing::InvokeWithoutArgs; 26 using ::testing::Invoke;
27 using ::testing::NotNull;
27 using ::testing::Return; 28 using ::testing::Return;
28 using ::testing::SaveArg; 29 using ::testing::SaveArg;
29 using ::testing::SetArgumentPointee; 30 using ::testing::SetArgumentPointee;
30 31
31 namespace content { 32 namespace content {
32 33
34 static const int kRenderProcessId = 1;
35 static const int kRenderViewId = 4;
33 static const int kStreamId = 50; 36 static const int kStreamId = 50;
34 37
35 static bool IsRunningHeadless() { 38 static bool IsRunningHeadless() {
36 scoped_ptr<base::Environment> env(base::Environment::Create()); 39 scoped_ptr<base::Environment> env(base::Environment::Create());
37 if (env->HasVar("CHROME_HEADLESS")) 40 if (env->HasVar("CHROME_HEADLESS"))
38 return true; 41 return true;
39 return false; 42 return false;
40 } 43 }
41 44
42 class MockAudioRendererHost : public AudioRendererHost { 45 class MockAudioRendererHost : public AudioRendererHost {
43 public: 46 public:
44 explicit MockAudioRendererHost( 47 explicit MockAudioRendererHost(
45 media::AudioManager* audio_manager, 48 media::AudioManager* audio_manager,
46 MediaObserver* media_observer) 49 MediaObserver* media_observer)
47 : AudioRendererHost(audio_manager, media_observer), 50 : AudioRendererHost(kRenderProcessId, audio_manager, media_observer),
48 shared_memory_length_(0) { 51 shared_memory_length_(0) {
49 } 52 }
50 53
51 // A list of mock methods. 54 // A list of mock methods.
52 MOCK_METHOD2(OnStreamCreated, 55 MOCK_METHOD2(OnStreamCreated,
53 void(int stream_id, int length)); 56 void(int stream_id, int length));
54 MOCK_METHOD1(OnStreamPlaying, void(int stream_id)); 57 MOCK_METHOD1(OnStreamPlaying, void(int stream_id));
55 MOCK_METHOD1(OnStreamPaused, void(int stream_id)); 58 MOCK_METHOD1(OnStreamPaused, void(int stream_id));
56 MOCK_METHOD1(OnStreamError, void(int stream_id)); 59 MOCK_METHOD1(OnStreamError, void(int stream_id));
57 MOCK_METHOD2(OnStreamVolume, void(int stream_id, double volume)); 60 MOCK_METHOD2(OnStreamVolume, void(int stream_id, double volume));
58 61
59 private: 62 private:
60 virtual ~MockAudioRendererHost() { 63 virtual ~MockAudioRendererHost() {
61 // Make sure all audio streams have been deleted. 64 // Make sure all audio streams have been deleted.
62 EXPECT_TRUE(audio_entries_.empty()); 65 EXPECT_TRUE(audio_entries_.empty());
66 // Make sure no mirroring sessions are active.
67 EXPECT_TRUE(mirror_sessions_.empty());
63 } 68 }
64 69
65 // This method is used to dispatch IPC messages to the renderer. We intercept 70 // This method is used to dispatch IPC messages to the renderer. We intercept
66 // these messages here and dispatch to our mock methods to verify the 71 // these messages here and dispatch to our mock methods to verify the
67 // conversation between this object and the renderer. 72 // conversation between this object and the renderer.
68 virtual bool Send(IPC::Message* message) { 73 virtual bool Send(IPC::Message* message) {
69 CHECK(message); 74 CHECK(message);
70 75
71 // In this method we dispatch the messages to the according handlers as if 76 // In this method we dispatch the messages to the according handlers as if
72 // we are the renderer. 77 // we are the renderer.
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 } 134 }
130 } 135 }
131 136
132 scoped_ptr<base::SharedMemory> shared_memory_; 137 scoped_ptr<base::SharedMemory> shared_memory_;
133 scoped_ptr<base::SyncSocket> sync_socket_; 138 scoped_ptr<base::SyncSocket> sync_socket_;
134 uint32 shared_memory_length_; 139 uint32 shared_memory_length_;
135 140
136 DISALLOW_COPY_AND_ASSIGN(MockAudioRendererHost); 141 DISALLOW_COPY_AND_ASSIGN(MockAudioRendererHost);
137 }; 142 };
138 143
144 class MockMirroringDestination
145 : public AudioRendererHost::MirroringDestination {
146 public:
147 MockMirroringDestination() {}
148
149 MOCK_METHOD2(AddInput,
150 void(media::AudioOutputStream::AudioSourceCallback* callback,
151 const media::AudioParameters& params));
152 MOCK_METHOD2(RemoveInput,
153 void(media::AudioOutputStream::AudioSourceCallback* callback,
154 const base::Closure& done));
155
156 void SimulateRemoveInput(
157 media::AudioOutputStream::AudioSourceCallback* callback,
158 const base::Closure& done) {
159 done.Run();
160 }
161
162 private:
163 virtual ~MockMirroringDestination() {}
164
165 DISALLOW_COPY_AND_ASSIGN(MockMirroringDestination);
166 };
167
139 ACTION_P(QuitMessageLoop, message_loop) { 168 ACTION_P(QuitMessageLoop, message_loop) {
140 message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); 169 message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure());
141 } 170 }
142 171
143 class AudioRendererHostTest : public testing::Test { 172 class AudioRendererHostTest : public testing::Test {
144 public: 173 public:
145 AudioRendererHostTest() 174 AudioRendererHostTest()
146 : mock_stream_(true) { 175 : mock_stream_(true) {
147 } 176 }
148 177
149 protected: 178 protected:
150 virtual void SetUp() { 179 virtual void SetUp() {
151 // Create a message loop so AudioRendererHost can use it. 180 // Create a message loop so AudioRendererHost can use it.
152 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); 181 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO));
153 182
154 // Claim to be on both the UI and IO threads to pass all the DCHECKS. 183 // Claim to be on both the UI and IO threads to pass all the DCHECKS.
155 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO, 184 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO,
156 message_loop_.get())); 185 message_loop_.get()));
157 ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI, 186 ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI,
158 message_loop_.get())); 187 message_loop_.get()));
159 audio_manager_.reset(media::AudioManager::Create()); 188 audio_manager_.reset(media::AudioManager::Create());
160 observer_.reset(new MockMediaObserver()); 189 observer_.reset(new MockMediaObserver());
161 host_ = new MockAudioRendererHost(audio_manager_.get(), observer_.get()); 190 host_ = new MockAudioRendererHost(audio_manager_.get(), observer_.get());
162 191 destination_ = new MockMirroringDestination();
163 // Expect the audio stream will be deleted. 192 another_destination_ = new MockMirroringDestination();
164 EXPECT_CALL(*observer_, OnDeleteAudioStream(_, kStreamId));
165 193
166 // Simulate IPC channel connected. 194 // Simulate IPC channel connected.
167 host_->OnChannelConnected(base::GetCurrentProcId()); 195 host_->OnChannelConnected(base::GetCurrentProcId());
196
197 if (!IsRunningHeadless())
198 EnableRealDevice();
168 } 199 }
169 200
170 virtual void TearDown() { 201 virtual void TearDown() {
171 // Simulate closing the IPC channel. 202 // Simulate closing the IPC channel.
172 host_->OnChannelClosing(); 203 host_->OnChannelClosing();
173 204
174 // Release the reference to the mock object. The object will be destructed 205 // Release the reference to the mock objects. The MockAudioRendererHost
175 // on message_loop_. 206 // will be destroyed on message_loop_.
207 another_destination_ = NULL;
208 destination_ = NULL;
176 host_ = NULL; 209 host_ = NULL;
177 210
178 // We need to continue running message_loop_ to complete all destructions. 211 // We need to continue running message_loop_ to complete all destructions.
179 SyncWithAudioThread(); 212 SyncWithAudioThread();
180 213
181 audio_manager_.reset(); 214 audio_manager_.reset();
182 215
183 io_thread_.reset(); 216 io_thread_.reset();
184 ui_thread_.reset(); 217 ui_thread_.reset();
185 } 218 }
(...skipping 15 matching lines...) Expand all
201 234
202 media::AudioParameters params( 235 media::AudioParameters params(
203 format, media::CHANNEL_LAYOUT_STEREO, 236 format, media::CHANNEL_LAYOUT_STEREO,
204 media::AudioParameters::kAudioCDSampleRate, 16, 237 media::AudioParameters::kAudioCDSampleRate, 16,
205 media::AudioParameters::kAudioCDSampleRate / 10); 238 media::AudioParameters::kAudioCDSampleRate / 10);
206 239
207 // Send a create stream message to the audio output stream and wait until 240 // Send a create stream message to the audio output stream and wait until
208 // we receive the created message. 241 // we receive the created message.
209 host_->OnCreateStream(kStreamId, params, 0); 242 host_->OnCreateStream(kStreamId, params, 0);
210 message_loop_->Run(); 243 message_loop_->Run();
244
245 // Simulate the renderer process associating a stream with a render view.
246 host_->OnAssociateStreamWithProducer(kStreamId, kRenderViewId);
247 message_loop_->RunUntilIdle();
248
249 // Expect the audio stream will be deleted at some later point.
250 EXPECT_CALL(*observer_, OnDeleteAudioStream(_, kStreamId));
211 } 251 }
212 252
213 void Close() { 253 void Close() {
214 EXPECT_CALL(*observer_, 254 EXPECT_CALL(*observer_,
215 OnSetAudioStreamStatus(_, kStreamId, "closed")); 255 OnSetAudioStreamStatus(_, kStreamId, "closed"));
216 256
217 // Send a message to AudioRendererHost to tell it we want to close the 257 // Send a message to AudioRendererHost to tell it we want to close the
218 // stream. 258 // stream.
219 host_->OnCloseStream(kStreamId); 259 host_->OnCloseStream(kStreamId);
220 message_loop_->RunUntilIdle(); 260 message_loop_->RunUntilIdle();
(...skipping 20 matching lines...) Expand all
241 } 281 }
242 282
243 void SetVolume(double volume) { 283 void SetVolume(double volume) {
244 EXPECT_CALL(*observer_, 284 EXPECT_CALL(*observer_,
245 OnSetAudioStreamVolume(_, kStreamId, volume)); 285 OnSetAudioStreamVolume(_, kStreamId, volume));
246 286
247 host_->OnSetVolume(kStreamId, volume); 287 host_->OnSetVolume(kStreamId, volume);
248 message_loop_->RunUntilIdle(); 288 message_loop_->RunUntilIdle();
249 } 289 }
250 290
291 void StartMirroringTo(MockMirroringDestination* dest,
292 int expected_inputs_seen) {
293 if (expected_inputs_seen > 0) {
294 EXPECT_CALL(*dest, AddInput(NotNull(), _))
295 .Times(expected_inputs_seen);
296 EXPECT_CALL(*dest, RemoveInput(NotNull(), _))
297 .Times(expected_inputs_seen)
298 .WillRepeatedly(
299 Invoke(dest, &MockMirroringDestination::SimulateRemoveInput));
300 }
301
302 AudioRendererHost::StartMirroring(kRenderProcessId, kRenderViewId, dest);
303 message_loop_->RunUntilIdle();
304 }
305
306 void StopMirroringTo(MockMirroringDestination* dest) {
307 AudioRendererHost::StopMirroring(kRenderProcessId, kRenderViewId, dest);
308 message_loop_->RunUntilIdle();
309 }
310
251 void SimulateError() { 311 void SimulateError() {
252 EXPECT_CALL(*observer_, 312 EXPECT_CALL(*observer_,
253 OnSetAudioStreamStatus(_, kStreamId, "error")); 313 OnSetAudioStreamStatus(_, kStreamId, "error"));
254 // Find the first AudioOutputController in the AudioRendererHost. 314 // Find the first AudioOutputController in the AudioRendererHost.
255 CHECK(host_->audio_entries_.size()) 315 CHECK(host_->audio_entries_.size())
256 << "Calls Create() before calling this method"; 316 << "Calls Create() before calling this method";
257 media::AudioOutputController* controller = 317 media::AudioOutputController* controller =
258 host_->LookupControllerByIdForTesting(kStreamId); 318 host_->LookupControllerByIdForTesting(kStreamId);
259 CHECK(controller) << "AudioOutputController not found"; 319 CHECK(controller) << "AudioOutputController not found";
260 320
(...skipping 29 matching lines...) Expand all
290 // to the thread that itself owns. 350 // to the thread that itself owns.
291 message_loop_->PostTask( 351 message_loop_->PostTask(
292 FROM_HERE, base::Bind(&PostQuitOnAudioThread, 352 FROM_HERE, base::Bind(&PostQuitOnAudioThread,
293 base::Unretained(audio_manager_.get()), 353 base::Unretained(audio_manager_.get()),
294 message_loop_.get())); 354 message_loop_.get()));
295 message_loop_->Run(); 355 message_loop_->Run();
296 } 356 }
297 357
298 void EnableRealDevice() { mock_stream_ = false; } 358 void EnableRealDevice() { mock_stream_ = false; }
299 359
360 protected:
361 // Accessors for testing mirroring to one or two destinations.
362 MockMirroringDestination* destination() const {
363 return destination_;
364 }
365 MockMirroringDestination* another_destination() const {
366 return another_destination_;
367 }
368
300 private: 369 private:
301 bool mock_stream_; 370 bool mock_stream_;
302 scoped_ptr<MockMediaObserver> observer_; 371 scoped_ptr<MockMediaObserver> observer_;
303 scoped_refptr<MockAudioRendererHost> host_; 372 scoped_refptr<MockAudioRendererHost> host_;
373 scoped_refptr<MockMirroringDestination> destination_;
374 scoped_refptr<MockMirroringDestination> another_destination_;
304 scoped_ptr<MessageLoop> message_loop_; 375 scoped_ptr<MessageLoop> message_loop_;
305 scoped_ptr<BrowserThreadImpl> io_thread_; 376 scoped_ptr<BrowserThreadImpl> io_thread_;
306 scoped_ptr<BrowserThreadImpl> ui_thread_; 377 scoped_ptr<BrowserThreadImpl> ui_thread_;
307 scoped_ptr<media::AudioManager> audio_manager_; 378 scoped_ptr<media::AudioManager> audio_manager_;
308 379
309 DISALLOW_COPY_AND_ASSIGN(AudioRendererHostTest); 380 DISALLOW_COPY_AND_ASSIGN(AudioRendererHostTest);
310 }; 381 };
311 382
312 TEST_F(AudioRendererHostTest, CreateAndClose) { 383 TEST_F(AudioRendererHostTest, CreateAndClose) {
313 if (!IsRunningHeadless())
314 EnableRealDevice();
315
316 Create(); 384 Create();
317 Close(); 385 Close();
318 } 386 }
319 387
320 // Simulate the case where a stream is not properly closed. 388 // Simulate the case where a stream is not properly closed.
321 TEST_F(AudioRendererHostTest, CreateAndShutdown) { 389 TEST_F(AudioRendererHostTest, CreateAndShutdown) {
322 if (!IsRunningHeadless())
323 EnableRealDevice();
324
325 Create(); 390 Create();
326 } 391 }
327 392
328 TEST_F(AudioRendererHostTest, CreatePlayAndClose) { 393 TEST_F(AudioRendererHostTest, CreatePlayAndClose) {
329 if (!IsRunningHeadless())
330 EnableRealDevice();
331
332 Create(); 394 Create();
333 Play(); 395 Play();
334 Close(); 396 Close();
335 } 397 }
336 398
337 TEST_F(AudioRendererHostTest, CreatePlayPauseAndClose) { 399 TEST_F(AudioRendererHostTest, CreatePlayPauseAndClose) {
338 if (!IsRunningHeadless())
339 EnableRealDevice();
340
341 Create(); 400 Create();
342 Play(); 401 Play();
343 Pause(); 402 Pause();
344 Close(); 403 Close();
345 } 404 }
346 405
347 TEST_F(AudioRendererHostTest, SetVolume) { 406 TEST_F(AudioRendererHostTest, SetVolume) {
348 if (!IsRunningHeadless())
349 EnableRealDevice();
350
351 Create(); 407 Create();
352 SetVolume(0.5); 408 SetVolume(0.5);
353 Play(); 409 Play();
354 Pause(); 410 Pause();
355 Close(); 411 Close();
356 } 412 }
357 413
358 // Simulate the case where a stream is not properly closed. 414 // Simulate the case where a stream is not properly closed.
359 TEST_F(AudioRendererHostTest, CreatePlayAndShutdown) { 415 TEST_F(AudioRendererHostTest, CreatePlayAndShutdown) {
360 if (!IsRunningHeadless())
361 EnableRealDevice();
362
363 Create(); 416 Create();
364 Play(); 417 Play();
365 } 418 }
366 419
367 // Simulate the case where a stream is not properly closed. 420 // Simulate the case where a stream is not properly closed.
368 TEST_F(AudioRendererHostTest, CreatePlayPauseAndShutdown) { 421 TEST_F(AudioRendererHostTest, CreatePlayPauseAndShutdown) {
369 if (!IsRunningHeadless())
370 EnableRealDevice();
371
372 Create(); 422 Create();
373 Play(); 423 Play();
374 Pause(); 424 Pause();
375 } 425 }
376 426
427 TEST_F(AudioRendererHostTest, MirroringOfNothing) {
428 StartMirroringTo(destination(), 0);
429 StopMirroringTo(destination());
430 }
431
432 TEST_F(AudioRendererHostTest, StreamLifetimeAroundMirroringSession) {
433 Create();
434 Play();
435 StartMirroringTo(destination(), 1);
436 StopMirroringTo(destination());
437 Close();
438 }
439
440 TEST_F(AudioRendererHostTest, PauseStreamDuringMirroringSession) {
441 Create();
442 Play();
443 StartMirroringTo(destination(), 1);
444 Pause();
445 StopMirroringTo(destination());
446 Close();
447 }
448
449 TEST_F(AudioRendererHostTest, StreamLifetimeWithinMirroringSession) {
450 StartMirroringTo(destination(), 1);
451 Create();
452 Play();
453 Close();
454 StopMirroringTo(destination());
455 }
456
457 TEST_F(AudioRendererHostTest, StreamLifetimeAroundTwoMirroringSessions) {
458 Create();
459 Play();
460 StartMirroringTo(destination(), 1);
461 StopMirroringTo(destination());
462 StartMirroringTo(another_destination(), 1);
463 StopMirroringTo(another_destination());
464 Close();
465 }
466
467 TEST_F(AudioRendererHostTest, StreamLifetimeWithinTwoMirroringSessions) {
468 StartMirroringTo(destination(), 1);
469 Create();
470 Play();
471 StopMirroringTo(destination());
472 StartMirroringTo(another_destination(), 1);
473 Close();
474 StopMirroringTo(another_destination());
475 }
476
477 TEST_F(AudioRendererHostTest, StreamSwitchesMirroringSessions) {
478 StartMirroringTo(destination(), 1);
479 Create();
480 Play();
481 StartMirroringTo(another_destination(), 1);
482 Close();
483 StopMirroringTo(another_destination());
484 StopMirroringTo(destination()); // This should be a no-op.
485 }
486
487 TEST_F(AudioRendererHostTest, SwitchMirroringDestinationNoStreams) {
488 StartMirroringTo(destination(), 0);
489 StartMirroringTo(another_destination(), 0);
490 StopMirroringTo(another_destination());
491 }
492
493 // Simulate the case where mirroring was not stopped before shutdown.
494 TEST_F(AudioRendererHostTest, StartMirroringNothingAndShutdown) {
495 StartMirroringTo(destination(), 0);
496 }
497
498 // Simulate the case where mirroring was not stopped before shutdown, and a
499 // stream was being mirrored.
500 TEST_F(AudioRendererHostTest, StartMirroringSomethingAndShutdown) {
501 StartMirroringTo(destination(), 1);
502 Create();
503 Play();
504 }
505
377 TEST_F(AudioRendererHostTest, SimulateError) { 506 TEST_F(AudioRendererHostTest, SimulateError) {
378 if (!IsRunningHeadless())
379 EnableRealDevice();
380
381 Create(); 507 Create();
382 Play(); 508 Play();
383 SimulateError(); 509 SimulateError();
384 } 510 }
385 511
386 // Simulate the case when an error is generated on the browser process, 512 // Simulate the case when an error is generated on the browser process,
387 // the audio device is closed but the render process try to close the 513 // the audio device is closed but the render process try to close the
388 // audio stream again. 514 // audio stream again.
389 TEST_F(AudioRendererHostTest, SimulateErrorAndClose) { 515 TEST_F(AudioRendererHostTest, SimulateErrorAndClose) {
390 if (!IsRunningHeadless())
391 EnableRealDevice();
392
393 Create(); 516 Create();
394 Play(); 517 Play();
395 SimulateError(); 518 SimulateError();
396 Close(); 519 Close();
397 } 520 }
398 521
399 // TODO(hclam): Add tests for data conversation in low latency mode. 522 // TODO(hclam): Add tests for data conversation in low latency mode.
400 523
401 } // namespace content 524 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698