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/mock_audio_source_callback.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 using testing::Return;
20 // TODO(crogers): Most of these tests can be made platform agnostic.
21 // http://crbug.com/223242
29 ACTION_P(SignalEvent, event) {
33 class AUHALStreamTest : public testing::Test {
36 : message_loop_(base::MessageLoop::TYPE_UI),
37 manager_(AudioManager::CreateForTesting()) {
38 // Wait for the AudioManager to finish any initialization on the audio loop.
39 base::RunLoop().RunUntilIdle();
42 virtual ~AUHALStreamTest() {
43 base::RunLoop().RunUntilIdle();
46 AudioOutputStream* Create() {
47 return manager_->MakeAudioOutputStream(
48 manager_->GetDefaultOutputStreamParameters(), "");
51 bool CanRunAudioTests() {
52 return manager_->HasAudioOutputDevices();
56 base::MessageLoop message_loop_;
57 scoped_ptr<AudioManager> manager_;
58 MockAudioSourceCallback source_;
61 DISALLOW_COPY_AND_ASSIGN(AUHALStreamTest);
64 TEST_F(AUHALStreamTest, HardwareSampleRate) {
65 if (!CanRunAudioTests())
67 const AudioParameters preferred_params =
68 manager_->GetDefaultOutputStreamParameters();
69 EXPECT_GE(preferred_params.sample_rate(), 16000);
70 EXPECT_LE(preferred_params.sample_rate(), 192000);
73 TEST_F(AUHALStreamTest, CreateClose) {
74 if (!CanRunAudioTests())
79 TEST_F(AUHALStreamTest, CreateOpenClose) {
80 if (!CanRunAudioTests())
82 AudioOutputStream* stream = Create();
83 EXPECT_TRUE(stream->Open());
87 TEST_F(AUHALStreamTest, CreateOpenStartStopClose) {
88 if (!CanRunAudioTests())
91 AudioOutputStream* stream = Create();
92 EXPECT_TRUE(stream->Open());
94 // Wait for the first data callback from the OS.
95 base::WaitableEvent event(false, false);
96 EXPECT_CALL(source_, OnMoreIOData(_, _, _))
97 .WillOnce(DoAll(ZeroBuffer(), SignalEvent(&event), Return(0)));
98 EXPECT_CALL(source_, OnError(_)).Times(0);
99 stream->Start(&source_);