audio_auhal_mac_unittest.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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.
4
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"
15
16using testing::_;
17using testing::DoAll;
18using testing::Return;
19
20// TODO(crogers): Most of these tests can be made platform agnostic.
21// http://crbug.com/223242
22
23namespace media {
24
25ACTION(ZeroBuffer) {
26  arg0->Zero();
27}
28
29ACTION_P(SignalEvent, event) {
30  event->Signal();
31}
32
33class AUHALStreamTest : public testing::Test {
34 public:
35  AUHALStreamTest()
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();
40  }
41
42  virtual ~AUHALStreamTest() {
43    base::RunLoop().RunUntilIdle();
44  }
45
46  AudioOutputStream* Create() {
47    return manager_->MakeAudioOutputStream(
48        manager_->GetDefaultOutputStreamParameters(), "");
49  }
50
51  bool CanRunAudioTests() {
52    return manager_->HasAudioOutputDevices();
53  }
54
55 protected:
56  base::MessageLoop message_loop_;
57  scoped_ptr<AudioManager> manager_;
58  MockAudioSourceCallback source_;
59
60 private:
61  DISALLOW_COPY_AND_ASSIGN(AUHALStreamTest);
62};
63
64TEST_F(AUHALStreamTest, HardwareSampleRate) {
65  if (!CanRunAudioTests())
66    return;
67  const AudioParameters preferred_params =
68      manager_->GetDefaultOutputStreamParameters();
69  EXPECT_GE(preferred_params.sample_rate(), 16000);
70  EXPECT_LE(preferred_params.sample_rate(), 192000);
71}
72
73TEST_F(AUHALStreamTest, CreateClose) {
74  if (!CanRunAudioTests())
75    return;
76  Create()->Close();
77}
78
79TEST_F(AUHALStreamTest, CreateOpenClose) {
80  if (!CanRunAudioTests())
81    return;
82  AudioOutputStream* stream = Create();
83  EXPECT_TRUE(stream->Open());
84  stream->Close();
85}
86
87TEST_F(AUHALStreamTest, CreateOpenStartStopClose) {
88  if (!CanRunAudioTests())
89    return;
90
91  AudioOutputStream* stream = Create();
92  EXPECT_TRUE(stream->Open());
93
94  // Wait for the first data callback from the OS.
95  base::WaitableEvent event(false, false);
96  EXPECT_CALL(source_, OnMoreData(_, _))
97      .WillOnce(DoAll(ZeroBuffer(), SignalEvent(&event), Return(0)));
98  EXPECT_CALL(source_, OnError(_)).Times(0);
99  stream->Start(&source_);
100  event.Wait();
101
102  stream->Stop();
103  stream->Close();
104}
105
106}  // namespace media
107