Roll leveldb 3f7758:803d69 (v1.17 -> v1.18)
[chromium-blink-merge.git] / media / audio / mac / audio_auhal_mac_unittest.cc
blob69179d56078a5178490b49a27a2b5c5d2cef4e1b
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"
16 using testing::_;
17 using testing::DoAll;
18 using testing::Return;
20 // TODO(crogers): Most of these tests can be made platform agnostic.
21 // http://crbug.com/223242
23 namespace media {
25 ACTION(ZeroBuffer) {
26 arg0->Zero();
29 ACTION_P(SignalEvent, event) {
30 event->Signal();
33 class 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();
42 virtual ~AUHALStreamTest() {
43 base::RunLoop().RunUntilIdle();
46 AudioOutputStream* Create() {
47 return manager_->MakeAudioOutputStream(
48 manager_->GetDefaultOutputStreamParameters(), "");
51 bool CanRunAudioTests() {
52 return manager_->HasAudioOutputDevices();
55 protected:
56 base::MessageLoop message_loop_;
57 scoped_ptr<AudioManager> manager_;
58 MockAudioSourceCallback source_;
60 private:
61 DISALLOW_COPY_AND_ASSIGN(AUHALStreamTest);
64 TEST_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);
73 TEST_F(AUHALStreamTest, CreateClose) {
74 if (!CanRunAudioTests())
75 return;
76 Create()->Close();
79 TEST_F(AUHALStreamTest, CreateOpenClose) {
80 if (!CanRunAudioTests())
81 return;
82 AudioOutputStream* stream = Create();
83 EXPECT_TRUE(stream->Open());
84 stream->Close();
87 TEST_F(AUHALStreamTest, CreateOpenStartStopClose) {
88 if (!CanRunAudioTests())
89 return;
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_, OnMoreData(_, _))
97 .WillOnce(DoAll(ZeroBuffer(), SignalEvent(&event), Return(0)));
98 EXPECT_CALL(source_, OnError(_)).Times(0);
99 stream->Start(&source_);
100 event.Wait();
102 stream->Stop();
103 stream->Close();
106 } // namespace media