1 // Copyright 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 "base/basictypes.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h"
9 #include "base/synchronization/waitable_event.h"
10 #include "media/audio/audio_io.h"
11 #include "media/audio/audio_manager.h"
12 #include "media/audio/audio_unittest_util.h"
13 #include "media/audio/mock_audio_source_callback.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
19 using testing::Return
;
21 // TODO(crogers): Most of these tests can be made platform agnostic.
22 // http://crbug.com/223242
30 ACTION_P(SignalEvent
, event
) {
34 class AUHALStreamTest
: public testing::Test
{
37 : message_loop_(base::MessageLoop::TYPE_UI
),
38 manager_(AudioManager::CreateForTesting()) {
39 // Wait for the AudioManager to finish any initialization on the audio loop.
40 base::RunLoop().RunUntilIdle();
43 ~AUHALStreamTest() override
{ base::RunLoop().RunUntilIdle(); }
45 AudioOutputStream
* Create() {
46 return manager_
->MakeAudioOutputStream(
47 manager_
->GetDefaultOutputStreamParameters(), "");
50 bool OutputDevicesAvailable() {
51 return manager_
->HasAudioOutputDevices();
55 base::MessageLoop message_loop_
;
56 scoped_ptr
<AudioManager
> manager_
;
57 MockAudioSourceCallback source_
;
60 DISALLOW_COPY_AND_ASSIGN(AUHALStreamTest
);
63 TEST_F(AUHALStreamTest
, HardwareSampleRate
) {
64 ABORT_AUDIO_TEST_IF_NOT(OutputDevicesAvailable());
65 const AudioParameters preferred_params
=
66 manager_
->GetDefaultOutputStreamParameters();
67 EXPECT_GE(preferred_params
.sample_rate(), 16000);
68 EXPECT_LE(preferred_params
.sample_rate(), 192000);
71 TEST_F(AUHALStreamTest
, CreateClose
) {
72 ABORT_AUDIO_TEST_IF_NOT(OutputDevicesAvailable());
76 TEST_F(AUHALStreamTest
, CreateOpenClose
) {
77 ABORT_AUDIO_TEST_IF_NOT(OutputDevicesAvailable());
78 AudioOutputStream
* stream
= Create();
79 EXPECT_TRUE(stream
->Open());
83 TEST_F(AUHALStreamTest
, CreateOpenStartStopClose
) {
84 ABORT_AUDIO_TEST_IF_NOT(OutputDevicesAvailable());
86 AudioOutputStream
* stream
= Create();
87 EXPECT_TRUE(stream
->Open());
89 // Wait for the first data callback from the OS.
90 base::WaitableEvent
event(false, false);
91 EXPECT_CALL(source_
, OnMoreData(_
, _
))
92 .WillOnce(DoAll(ZeroBuffer(), SignalEvent(&event
), Return(0)));
93 EXPECT_CALL(source_
, OnError(_
)).Times(0);
94 stream
->Start(&source_
);