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 <string> 6 7#include "base/synchronization/waitable_event.h" 8#include "base/test/test_timeouts.h" 9#include "base/time/time.h" 10#include "media/audio/cras/audio_manager_cras.h" 11#include "media/audio/fake_audio_log_factory.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 16// cras_util.h defines custom min/max macros which break compilation, so ensure 17// it's not included until last. #if avoids presubmit errors. 18#if defined(USE_CRAS) 19#include "media/audio/cras/cras_unified.h" 20#endif 21 22using testing::_; 23using testing::DoAll; 24using testing::InvokeWithoutArgs; 25using testing::Return; 26using testing::SetArgumentPointee; 27using testing::StrictMock; 28 29namespace media { 30 31class MockAudioManagerCras : public AudioManagerCras { 32 public: 33 MockAudioManagerCras() : AudioManagerCras(&fake_audio_log_factory_) {} 34 35 MOCK_METHOD0(Init, void()); 36 MOCK_METHOD0(HasAudioOutputDevices, bool()); 37 MOCK_METHOD0(HasAudioInputDevices, bool()); 38 MOCK_METHOD1(MakeLinearOutputStream, AudioOutputStream*( 39 const AudioParameters& params)); 40 MOCK_METHOD2(MakeLowLatencyOutputStream, 41 AudioOutputStream*(const AudioParameters& params, 42 const std::string& device_id)); 43 MOCK_METHOD2(MakeLinearOutputStream, AudioInputStream*( 44 const AudioParameters& params, const std::string& device_id)); 45 MOCK_METHOD2(MakeLowLatencyInputStream, AudioInputStream*( 46 const AudioParameters& params, const std::string& device_id)); 47 48 // We need to override this function in order to skip the checking the number 49 // of active output streams. It is because the number of active streams 50 // is managed inside MakeAudioOutputStream, and we don't use 51 // MakeAudioOutputStream to create the stream in the tests. 52 virtual void ReleaseOutputStream(AudioOutputStream* stream) OVERRIDE { 53 DCHECK(stream); 54 delete stream; 55 } 56 57 private: 58 FakeAudioLogFactory fake_audio_log_factory_; 59}; 60 61class CrasUnifiedStreamTest : public testing::Test { 62 protected: 63 CrasUnifiedStreamTest() { 64 mock_manager_.reset(new StrictMock<MockAudioManagerCras>()); 65 } 66 67 virtual ~CrasUnifiedStreamTest() { 68 } 69 70 CrasUnifiedStream* CreateStream(ChannelLayout layout) { 71 return CreateStream(layout, kTestFramesPerPacket); 72 } 73 74 CrasUnifiedStream* CreateStream(ChannelLayout layout, 75 int32 samples_per_packet) { 76 AudioParameters params(kTestFormat, layout, kTestSampleRate, 77 kTestBitsPerSample, samples_per_packet); 78 return new CrasUnifiedStream(params, mock_manager_.get()); 79 } 80 81 MockAudioManagerCras& mock_manager() { 82 return *(mock_manager_.get()); 83 } 84 85 static const ChannelLayout kTestChannelLayout; 86 static const int kTestSampleRate; 87 static const int kTestBitsPerSample; 88 static const AudioParameters::Format kTestFormat; 89 static const uint32 kTestFramesPerPacket; 90 91 scoped_ptr<StrictMock<MockAudioManagerCras> > mock_manager_; 92 93 private: 94 DISALLOW_COPY_AND_ASSIGN(CrasUnifiedStreamTest); 95}; 96 97const ChannelLayout CrasUnifiedStreamTest::kTestChannelLayout = 98 CHANNEL_LAYOUT_STEREO; 99const int CrasUnifiedStreamTest::kTestSampleRate = 100 AudioParameters::kAudioCDSampleRate; 101const int CrasUnifiedStreamTest::kTestBitsPerSample = 16; 102const AudioParameters::Format CrasUnifiedStreamTest::kTestFormat = 103 AudioParameters::AUDIO_PCM_LINEAR; 104const uint32 CrasUnifiedStreamTest::kTestFramesPerPacket = 1000; 105 106TEST_F(CrasUnifiedStreamTest, ConstructedState) { 107 // Should support mono. 108 CrasUnifiedStream* test_stream = CreateStream(CHANNEL_LAYOUT_MONO); 109 test_stream->Close(); 110 111 // Should support stereo. 112 test_stream = CreateStream(CHANNEL_LAYOUT_SURROUND); 113 EXPECT_TRUE(test_stream->Open()); 114 test_stream->Close(); 115 116 // Bad bits per sample. 117 AudioParameters bad_bps_params(kTestFormat, kTestChannelLayout, 118 kTestSampleRate, kTestBitsPerSample - 1, 119 kTestFramesPerPacket); 120 test_stream = new CrasUnifiedStream(bad_bps_params, mock_manager_.get()); 121 EXPECT_FALSE(test_stream->Open()); 122 test_stream->Close(); 123 124 // Bad sample rate. 125 AudioParameters bad_rate_params(kTestFormat, kTestChannelLayout, 126 0, kTestBitsPerSample, kTestFramesPerPacket); 127 test_stream = new CrasUnifiedStream(bad_rate_params, mock_manager_.get()); 128 EXPECT_FALSE(test_stream->Open()); 129 test_stream->Close(); 130 131 // Check that Mono works too. 132 test_stream = CreateStream(CHANNEL_LAYOUT_MONO); 133 ASSERT_TRUE(test_stream->Open()); 134 test_stream->Close(); 135} 136 137TEST_F(CrasUnifiedStreamTest, RenderFrames) { 138 CrasUnifiedStream* test_stream = CreateStream(CHANNEL_LAYOUT_MONO); 139 MockAudioSourceCallback mock_callback; 140 141 ASSERT_TRUE(test_stream->Open()); 142 143 base::WaitableEvent event(false, false); 144 145 EXPECT_CALL(mock_callback, OnMoreData(_, _)) 146 .WillRepeatedly(DoAll( 147 InvokeWithoutArgs(&event, &base::WaitableEvent::Signal), 148 Return(kTestFramesPerPacket))); 149 150 test_stream->Start(&mock_callback); 151 152 // Wait for samples to be captured. 153 EXPECT_TRUE(event.TimedWait(TestTimeouts::action_timeout())); 154 155 test_stream->Stop(); 156 157 test_stream->Close(); 158} 159 160} // namespace media 161