IndexedDBFactory now ForceCloses databases.
[chromium-blink-merge.git] / content / browser / renderer_host / media / web_contents_audio_input_stream_unittest.cc
blob4a782aad12acaa3cedcc942761c926d3eb94c3ac
1 // Copyright (c) 2013 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 "content/browser/renderer_host/media/web_contents_audio_input_stream.h"
7 #include <list>
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/synchronization/waitable_event.h"
13 #include "base/threading/thread.h"
14 #include "content/browser/browser_thread_impl.h"
15 #include "content/browser/renderer_host/media/audio_mirroring_manager.h"
16 #include "content/browser/renderer_host/media/web_contents_tracker.h"
17 #include "media/audio/simple_sources.h"
18 #include "media/audio/virtual_audio_input_stream.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 using ::testing::_;
23 using ::testing::Assign;
24 using ::testing::DoAll;
25 using ::testing::Invoke;
26 using ::testing::InvokeWithoutArgs;
27 using ::testing::NotNull;
28 using ::testing::SaveArg;
29 using ::testing::WithArgs;
31 using media::AudioInputStream;
32 using media::AudioOutputStream;
33 using media::AudioParameters;
34 using media::SineWaveAudioSource;
35 using media::VirtualAudioInputStream;
36 using media::VirtualAudioOutputStream;
38 namespace content {
40 namespace {
42 const int kRenderProcessId = 123;
43 const int kRenderViewId = 456;
44 const int kAnotherRenderProcessId = 789;
45 const int kAnotherRenderViewId = 1;
47 const AudioParameters& TestAudioParameters() {
48 static const AudioParameters params(
49 AudioParameters::AUDIO_FAKE,
50 media::CHANNEL_LAYOUT_STEREO,
51 AudioParameters::kAudioCDSampleRate, 16,
52 AudioParameters::kAudioCDSampleRate / 100);
53 return params;
56 class MockAudioMirroringManager : public AudioMirroringManager {
57 public:
58 MockAudioMirroringManager() : AudioMirroringManager() {}
59 virtual ~MockAudioMirroringManager() {}
61 MOCK_METHOD3(StartMirroring,
62 void(int render_process_id, int render_view_id,
63 MirroringDestination* destination));
64 MOCK_METHOD3(StopMirroring,
65 void(int render_process_id, int render_view_id,
66 MirroringDestination* destination));
68 private:
69 DISALLOW_COPY_AND_ASSIGN(MockAudioMirroringManager);
72 class MockWebContentsTracker : public WebContentsTracker {
73 public:
74 MockWebContentsTracker() : WebContentsTracker() {}
76 MOCK_METHOD3(Start,
77 void(int render_process_id, int render_view_id,
78 const ChangeCallback& callback));
79 MOCK_METHOD0(Stop, void());
81 private:
82 virtual ~MockWebContentsTracker() {}
84 DISALLOW_COPY_AND_ASSIGN(MockWebContentsTracker);
87 // A fully-functional VirtualAudioInputStream, but methods are mocked to allow
88 // tests to check how/when they are invoked.
89 class MockVirtualAudioInputStream : public VirtualAudioInputStream {
90 public:
91 explicit MockVirtualAudioInputStream(
92 const scoped_refptr<base::MessageLoopProxy>& worker_loop)
93 : VirtualAudioInputStream(TestAudioParameters(), worker_loop,
94 VirtualAudioInputStream::AfterCloseCallback()),
95 real_(TestAudioParameters(), worker_loop,
96 base::Bind(&MockVirtualAudioInputStream::OnRealStreamHasClosed,
97 base::Unretained(this))),
98 real_stream_is_closed_(false) {
99 // Set default actions of mocked methods to delegate to the concrete
100 // implementation.
101 ON_CALL(*this, Open())
102 .WillByDefault(Invoke(&real_, &VirtualAudioInputStream::Open));
103 ON_CALL(*this, Start(_))
104 .WillByDefault(Invoke(&real_, &VirtualAudioInputStream::Start));
105 ON_CALL(*this, Stop())
106 .WillByDefault(Invoke(&real_, &VirtualAudioInputStream::Stop));
107 ON_CALL(*this, Close())
108 .WillByDefault(Invoke(&real_, &VirtualAudioInputStream::Close));
109 ON_CALL(*this, GetMaxVolume())
110 .WillByDefault(Invoke(&real_, &VirtualAudioInputStream::GetMaxVolume));
111 ON_CALL(*this, SetVolume(_))
112 .WillByDefault(Invoke(&real_, &VirtualAudioInputStream::SetVolume));
113 ON_CALL(*this, GetVolume())
114 .WillByDefault(Invoke(&real_, &VirtualAudioInputStream::GetVolume));
115 ON_CALL(*this, SetAutomaticGainControl(_))
116 .WillByDefault(
117 Invoke(&real_, &VirtualAudioInputStream::SetAutomaticGainControl));
118 ON_CALL(*this, GetAutomaticGainControl())
119 .WillByDefault(
120 Invoke(&real_, &VirtualAudioInputStream::GetAutomaticGainControl));
121 ON_CALL(*this, AddOutputStream(NotNull(), _))
122 .WillByDefault(
123 Invoke(&real_, &VirtualAudioInputStream::AddOutputStream));
124 ON_CALL(*this, RemoveOutputStream(NotNull(), _))
125 .WillByDefault(
126 Invoke(&real_, &VirtualAudioInputStream::RemoveOutputStream));
129 ~MockVirtualAudioInputStream() {
130 DCHECK(real_stream_is_closed_);
133 MOCK_METHOD0(Open, bool());
134 MOCK_METHOD1(Start, void(AudioInputStream::AudioInputCallback*));
135 MOCK_METHOD0(Stop, void());
136 MOCK_METHOD0(Close, void());
137 MOCK_METHOD0(GetMaxVolume, double());
138 MOCK_METHOD1(SetVolume, void(double));
139 MOCK_METHOD0(GetVolume, double());
140 MOCK_METHOD1(SetAutomaticGainControl, void(bool));
141 MOCK_METHOD0(GetAutomaticGainControl, bool());
142 MOCK_METHOD2(AddOutputStream, void(VirtualAudioOutputStream*,
143 const AudioParameters&));
144 MOCK_METHOD2(RemoveOutputStream, void(VirtualAudioOutputStream*,
145 const AudioParameters&));
147 private:
148 void OnRealStreamHasClosed(VirtualAudioInputStream* stream) {
149 DCHECK_EQ(&real_, stream);
150 DCHECK(!real_stream_is_closed_);
151 real_stream_is_closed_ = true;
154 VirtualAudioInputStream real_;
155 bool real_stream_is_closed_;
157 DISALLOW_COPY_AND_ASSIGN(MockVirtualAudioInputStream);
160 class MockAudioInputCallback : public AudioInputStream::AudioInputCallback {
161 public:
162 MockAudioInputCallback() {}
164 MOCK_METHOD5(OnData, void(AudioInputStream* stream, const uint8* src,
165 uint32 size, uint32 hardware_delay_bytes,
166 double volume));
167 MOCK_METHOD1(OnError, void(AudioInputStream* stream));
169 private:
170 DISALLOW_COPY_AND_ASSIGN(MockAudioInputCallback);
173 } // namespace
175 class WebContentsAudioInputStreamTest : public testing::Test {
176 public:
177 WebContentsAudioInputStreamTest()
178 : audio_thread_("Audio thread"),
179 io_thread_(BrowserThread::IO),
180 mock_mirroring_manager_(new MockAudioMirroringManager()),
181 mock_tracker_(new MockWebContentsTracker()),
182 mock_vais_(NULL),
183 wcais_(NULL),
184 destination_(NULL),
185 current_render_process_id_(kRenderProcessId),
186 current_render_view_id_(kRenderViewId),
187 on_data_event_(false, false) {
188 audio_thread_.Start();
189 io_thread_.Start();
192 virtual ~WebContentsAudioInputStreamTest() {
193 audio_thread_.Stop();
194 io_thread_.Stop();
196 DCHECK(!mock_vais_);
197 DCHECK(!wcais_);
198 EXPECT_FALSE(destination_);
199 DCHECK(streams_.empty());
200 DCHECK(sources_.empty());
203 void Open() {
204 mock_vais_ =
205 new MockVirtualAudioInputStream(audio_thread_.message_loop_proxy());
206 EXPECT_CALL(*mock_vais_, Open());
207 EXPECT_CALL(*mock_vais_, Close()); // At Close() time.
209 ASSERT_EQ(kRenderProcessId, current_render_process_id_);
210 ASSERT_EQ(kRenderViewId, current_render_view_id_);
211 EXPECT_CALL(*mock_tracker_.get(), Start(kRenderProcessId, kRenderViewId, _))
212 .WillOnce(DoAll(
213 SaveArg<2>(&change_callback_),
214 WithArgs<0, 1>(Invoke(&change_callback_,
215 &WebContentsTracker::ChangeCallback::Run))));
216 EXPECT_CALL(*mock_tracker_.get(), Stop()); // At Close() time.
218 wcais_ = new WebContentsAudioInputStream(
219 current_render_process_id_, current_render_view_id_,
220 mock_mirroring_manager_.get(),
221 mock_tracker_, mock_vais_);
222 wcais_->Open();
225 void Start() {
226 EXPECT_CALL(*mock_vais_, Start(&mock_input_callback_));
227 EXPECT_CALL(*mock_vais_, Stop()); // At Stop() time.
229 EXPECT_CALL(*mock_mirroring_manager_,
230 StartMirroring(kRenderProcessId, kRenderViewId, NotNull()))
231 .WillOnce(SaveArg<2>(&destination_))
232 .RetiresOnSaturation();
233 // At Stop() time, or when the mirroring target changes:
234 EXPECT_CALL(*mock_mirroring_manager_,
235 StopMirroring(kRenderProcessId, kRenderViewId, NotNull()))
236 .WillOnce(Assign(
237 &destination_,
238 static_cast<AudioMirroringManager::MirroringDestination*>(NULL)))
239 .RetiresOnSaturation();
241 EXPECT_CALL(mock_input_callback_, OnData(NotNull(), NotNull(), _, _, _))
242 .WillRepeatedly(
243 InvokeWithoutArgs(&on_data_event_, &base::WaitableEvent::Signal));
245 wcais_->Start(&mock_input_callback_);
247 // Test plumbing of volume controls and automatic gain controls. Calls to
248 // wcais_ methods should delegate directly to mock_vais_.
249 EXPECT_CALL(*mock_vais_, GetVolume());
250 double volume = wcais_->GetVolume();
251 EXPECT_CALL(*mock_vais_, GetMaxVolume());
252 const double max_volume = wcais_->GetMaxVolume();
253 volume *= 2.0;
254 if (volume < max_volume) {
255 volume = max_volume;
257 EXPECT_CALL(*mock_vais_, SetVolume(volume));
258 wcais_->SetVolume(volume);
259 EXPECT_CALL(*mock_vais_, GetAutomaticGainControl());
260 bool auto_gain = wcais_->GetAutomaticGainControl();
261 auto_gain = !auto_gain;
262 EXPECT_CALL(*mock_vais_, SetAutomaticGainControl(auto_gain));
263 wcais_->SetAutomaticGainControl(auto_gain);
266 void AddAnotherInput() {
267 // Note: WCAIS posts a task to invoke
268 // MockAudioMirroringManager::StartMirroring() on the IO thread, which
269 // causes our mock to set |destination_|. Block until that has happened.
270 base::WaitableEvent done(false, false);
271 BrowserThread::PostTask(
272 BrowserThread::IO, FROM_HERE, base::Bind(
273 &base::WaitableEvent::Signal, base::Unretained(&done)));
274 done.Wait();
275 ASSERT_TRUE(destination_);
277 EXPECT_CALL(*mock_vais_, AddOutputStream(NotNull(), _))
278 .RetiresOnSaturation();
279 // Later, when stream is closed:
280 EXPECT_CALL(*mock_vais_, RemoveOutputStream(NotNull(), _))
281 .RetiresOnSaturation();
283 const AudioParameters& params = TestAudioParameters();
284 AudioOutputStream* const out = destination_->AddInput(params);
285 ASSERT_TRUE(out);
286 streams_.push_back(out);
287 EXPECT_TRUE(out->Open());
288 SineWaveAudioSource* const source = new SineWaveAudioSource(
289 params.channel_layout(), 200.0, params.sample_rate());
290 sources_.push_back(source);
291 out->Start(source);
294 void RemoveOneInputInFIFOOrder() {
295 ASSERT_FALSE(streams_.empty());
296 AudioOutputStream* const out = streams_.front();
297 streams_.pop_front();
298 out->Stop();
299 out->Close(); // Self-deletes.
300 ASSERT_TRUE(!sources_.empty());
301 delete sources_.front();
302 sources_.pop_front();
305 void ChangeMirroringTarget() {
306 const int next_render_process_id =
307 current_render_process_id_ == kRenderProcessId ?
308 kAnotherRenderProcessId : kRenderProcessId;
309 const int next_render_view_id =
310 current_render_view_id_ == kRenderViewId ?
311 kAnotherRenderViewId : kRenderViewId;
313 EXPECT_CALL(*mock_mirroring_manager_,
314 StartMirroring(next_render_process_id, next_render_view_id,
315 NotNull()))
316 .WillOnce(SaveArg<2>(&destination_))
317 .RetiresOnSaturation();
318 // At Stop() time, or when the mirroring target changes:
319 EXPECT_CALL(*mock_mirroring_manager_,
320 StopMirroring(next_render_process_id, next_render_view_id,
321 NotNull()))
322 .WillOnce(Assign(
323 &destination_,
324 static_cast<AudioMirroringManager::MirroringDestination*>(NULL)))
325 .RetiresOnSaturation();
327 // Simulate OnTargetChange() callback from WebContentsTracker.
328 EXPECT_FALSE(change_callback_.is_null());
329 change_callback_.Run(next_render_process_id, next_render_view_id);
331 current_render_process_id_ = next_render_process_id;
332 current_render_view_id_ = next_render_view_id;
335 void LoseMirroringTarget() {
336 EXPECT_CALL(mock_input_callback_, OnError(_));
338 // Simulate OnTargetChange() callback from WebContentsTracker.
339 EXPECT_FALSE(change_callback_.is_null());
340 change_callback_.Run(-1, -1);
343 void Stop() {
344 wcais_->Stop();
347 void Close() {
348 // WebContentsAudioInputStream self-destructs on Close(). Its internal
349 // objects hang around until they are no longer referred to (e.g., as tasks
350 // on other threads shut things down).
351 wcais_->Close();
352 wcais_ = NULL;
353 mock_vais_ = NULL;
356 void RunOnAudioThread(const base::Closure& closure) {
357 audio_thread_.message_loop()->PostTask(FROM_HERE, closure);
360 // Block the calling thread until OnData() callbacks are being made.
361 void WaitForData() {
362 // Note: Arbitrarily chosen, but more iterations causes tests to take
363 // significantly more time.
364 static const int kNumIterations = 3;
365 for (int i = 0; i < kNumIterations; ++i)
366 on_data_event_.Wait();
369 private:
370 base::Thread audio_thread_;
371 BrowserThreadImpl io_thread_;
373 scoped_ptr<MockAudioMirroringManager> mock_mirroring_manager_;
374 scoped_refptr<MockWebContentsTracker> mock_tracker_;
376 MockVirtualAudioInputStream* mock_vais_; // Owned by wcais_.
377 WebContentsAudioInputStream* wcais_; // Self-destructs on Close().
379 // Mock consumer of audio data.
380 MockAudioInputCallback mock_input_callback_;
382 // Provided by WebContentsAudioInputStream to the mock WebContentsTracker.
383 // This callback is saved here, and test code will invoke it to simulate
384 // target change events.
385 WebContentsTracker::ChangeCallback change_callback_;
387 // Provided by WebContentsAudioInputStream to the mock AudioMirroringManager.
388 // A pointer to the implementation is saved here, and test code will invoke it
389 // to simulate: 1) calls to AddInput(); and 2) diverting audio data.
390 AudioMirroringManager::MirroringDestination* destination_;
392 // Current target RenderView. These get flipped in ChangedMirroringTarget().
393 int current_render_process_id_;
394 int current_render_view_id_;
396 // Streams provided by calls to WebContentsAudioInputStream::AddInput(). Each
397 // is started with a simulated source of audio data.
398 std::list<AudioOutputStream*> streams_;
399 std::list<SineWaveAudioSource*> sources_; // 1:1 with elements in streams_.
401 base::WaitableEvent on_data_event_;
403 DISALLOW_COPY_AND_ASSIGN(WebContentsAudioInputStreamTest);
406 #define RUN_ON_AUDIO_THREAD(method) \
407 RunOnAudioThread(base::Bind(&WebContentsAudioInputStreamTest::method, \
408 base::Unretained(this)))
410 TEST_F(WebContentsAudioInputStreamTest, OpenedButNeverStarted) {
411 RUN_ON_AUDIO_THREAD(Open);
412 RUN_ON_AUDIO_THREAD(Close);
415 TEST_F(WebContentsAudioInputStreamTest, MirroringNothing) {
416 RUN_ON_AUDIO_THREAD(Open);
417 RUN_ON_AUDIO_THREAD(Start);
418 WaitForData();
419 RUN_ON_AUDIO_THREAD(Stop);
420 RUN_ON_AUDIO_THREAD(Close);
423 TEST_F(WebContentsAudioInputStreamTest, MirroringOutputOutlivesSession) {
424 RUN_ON_AUDIO_THREAD(Open);
425 RUN_ON_AUDIO_THREAD(Start);
426 RUN_ON_AUDIO_THREAD(AddAnotherInput);
427 WaitForData();
428 RUN_ON_AUDIO_THREAD(Stop);
429 RUN_ON_AUDIO_THREAD(Close);
430 RUN_ON_AUDIO_THREAD(RemoveOneInputInFIFOOrder);
433 TEST_F(WebContentsAudioInputStreamTest, MirroringOutputWithinSession) {
434 RUN_ON_AUDIO_THREAD(Open);
435 RUN_ON_AUDIO_THREAD(Start);
436 RUN_ON_AUDIO_THREAD(AddAnotherInput);
437 WaitForData();
438 RUN_ON_AUDIO_THREAD(RemoveOneInputInFIFOOrder);
439 RUN_ON_AUDIO_THREAD(Stop);
440 RUN_ON_AUDIO_THREAD(Close);
443 TEST_F(WebContentsAudioInputStreamTest, MirroringNothingWithTargetChange) {
444 RUN_ON_AUDIO_THREAD(Open);
445 RUN_ON_AUDIO_THREAD(Start);
446 RUN_ON_AUDIO_THREAD(ChangeMirroringTarget);
447 RUN_ON_AUDIO_THREAD(Stop);
448 RUN_ON_AUDIO_THREAD(Close);
451 TEST_F(WebContentsAudioInputStreamTest, MirroringOneStreamAfterTargetChange) {
452 RUN_ON_AUDIO_THREAD(Open);
453 RUN_ON_AUDIO_THREAD(Start);
454 RUN_ON_AUDIO_THREAD(ChangeMirroringTarget);
455 RUN_ON_AUDIO_THREAD(AddAnotherInput);
456 WaitForData();
457 RUN_ON_AUDIO_THREAD(Stop);
458 RUN_ON_AUDIO_THREAD(Close);
459 RUN_ON_AUDIO_THREAD(RemoveOneInputInFIFOOrder);
462 TEST_F(WebContentsAudioInputStreamTest, MirroringOneStreamWithTargetChange) {
463 RUN_ON_AUDIO_THREAD(Open);
464 RUN_ON_AUDIO_THREAD(Start);
465 RUN_ON_AUDIO_THREAD(AddAnotherInput);
466 WaitForData();
467 RUN_ON_AUDIO_THREAD(ChangeMirroringTarget);
468 RUN_ON_AUDIO_THREAD(RemoveOneInputInFIFOOrder);
469 RUN_ON_AUDIO_THREAD(AddAnotherInput);
470 WaitForData();
471 RUN_ON_AUDIO_THREAD(Stop);
472 RUN_ON_AUDIO_THREAD(Close);
473 RUN_ON_AUDIO_THREAD(RemoveOneInputInFIFOOrder);
476 TEST_F(WebContentsAudioInputStreamTest, MirroringLostTarget) {
477 RUN_ON_AUDIO_THREAD(Open);
478 RUN_ON_AUDIO_THREAD(Start);
479 RUN_ON_AUDIO_THREAD(AddAnotherInput);
480 WaitForData();
481 RUN_ON_AUDIO_THREAD(LoseMirroringTarget);
482 RUN_ON_AUDIO_THREAD(RemoveOneInputInFIFOOrder);
483 RUN_ON_AUDIO_THREAD(Stop);
484 RUN_ON_AUDIO_THREAD(Close);
487 TEST_F(WebContentsAudioInputStreamTest, MirroringMultipleStreamsAndTargets) {
488 RUN_ON_AUDIO_THREAD(Open);
489 RUN_ON_AUDIO_THREAD(Start);
490 RUN_ON_AUDIO_THREAD(AddAnotherInput);
491 WaitForData();
492 RUN_ON_AUDIO_THREAD(AddAnotherInput);
493 RUN_ON_AUDIO_THREAD(AddAnotherInput);
494 RUN_ON_AUDIO_THREAD(AddAnotherInput);
495 WaitForData();
496 RUN_ON_AUDIO_THREAD(ChangeMirroringTarget);
497 RUN_ON_AUDIO_THREAD(RemoveOneInputInFIFOOrder);
498 WaitForData();
499 RUN_ON_AUDIO_THREAD(RemoveOneInputInFIFOOrder);
500 RUN_ON_AUDIO_THREAD(RemoveOneInputInFIFOOrder);
501 RUN_ON_AUDIO_THREAD(AddAnotherInput);
502 WaitForData();
503 RUN_ON_AUDIO_THREAD(RemoveOneInputInFIFOOrder);
504 WaitForData();
505 RUN_ON_AUDIO_THREAD(ChangeMirroringTarget);
506 RUN_ON_AUDIO_THREAD(RemoveOneInputInFIFOOrder);
507 RUN_ON_AUDIO_THREAD(Stop);
508 RUN_ON_AUDIO_THREAD(Close);
511 } // namespace content