| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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/threading/thread.h" | |
| 8 #include "media/audio/cras/audio_manager_cras.h" | |
| 9 #include "media/audio/cras/cras_output.h" | |
| 10 #include "testing/gmock/include/gmock/gmock.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 | |
| 13 using testing::_; | |
| 14 using testing::DoAll; | |
| 15 using testing::Return; | |
| 16 using testing::SetArgumentPointee; | |
| 17 using testing::StrictMock; | |
| 18 | |
| 19 namespace media { | |
| 20 | |
| 21 class MockAudioSourceCallback : public AudioOutputStream::AudioSourceCallback { | |
| 22 public: | |
| 23 MOCK_METHOD2(OnMoreData, int(AudioBus* audio_bus, | |
| 24 AudioBuffersState buffers_state)); | |
| 25 MOCK_METHOD3(OnMoreIOData, int(AudioBus* source, | |
| 26 AudioBus* dest, | |
| 27 AudioBuffersState buffers_state)); | |
| 28 MOCK_METHOD1(OnError, void(AudioOutputStream* stream)); | |
| 29 }; | |
| 30 | |
| 31 class MockAudioManagerCras : public AudioManagerCras { | |
| 32 public: | |
| 33 MOCK_METHOD0(Init, void()); | |
| 34 MOCK_METHOD0(HasAudioOutputDevices, bool()); | |
| 35 MOCK_METHOD0(HasAudioInputDevices, bool()); | |
| 36 MOCK_METHOD1(MakeLinearOutputStream, AudioOutputStream*( | |
| 37 const AudioParameters& params)); | |
| 38 MOCK_METHOD1(MakeLowLatencyOutputStream, AudioOutputStream*( | |
| 39 const AudioParameters& params)); | |
| 40 MOCK_METHOD2(MakeLinearOutputStream, AudioInputStream*( | |
| 41 const AudioParameters& params, const std::string& device_id)); | |
| 42 MOCK_METHOD2(MakeLowLatencyInputStream, AudioInputStream*( | |
| 43 const AudioParameters& params, const std::string& device_id)); | |
| 44 | |
| 45 // We need to override this function in order to skip the checking the number | |
| 46 // of active output streams. It is because the number of active streams | |
| 47 // is managed inside MakeAudioOutputStream, and we don't use | |
| 48 // MakeAudioOutputStream to create the stream in the tests. | |
| 49 virtual void ReleaseOutputStream(AudioOutputStream* stream) OVERRIDE { | |
| 50 DCHECK(stream); | |
| 51 delete stream; | |
| 52 } | |
| 53 | |
| 54 // We don't mock this method since all tests will do the same thing | |
| 55 // and use the current message loop. | |
| 56 virtual scoped_refptr<base::MessageLoopProxy> GetMessageLoop() OVERRIDE { | |
| 57 return MessageLoop::current()->message_loop_proxy(); | |
| 58 } | |
| 59 }; | |
| 60 | |
| 61 class CrasOutputStreamTest : public testing::Test { | |
| 62 protected: | |
| 63 CrasOutputStreamTest() { | |
| 64 mock_manager_.reset(new StrictMock<MockAudioManagerCras>()); | |
| 65 } | |
| 66 | |
| 67 virtual ~CrasOutputStreamTest() { | |
| 68 } | |
| 69 | |
| 70 CrasOutputStream* CreateStream(ChannelLayout layout) { | |
| 71 return CreateStream(layout, kTestFramesPerPacket); | |
| 72 } | |
| 73 | |
| 74 CrasOutputStream* CreateStream(ChannelLayout layout, | |
| 75 int32 samples_per_packet) { | |
| 76 AudioParameters params(kTestFormat, layout, kTestSampleRate, | |
| 77 kTestBitsPerSample, samples_per_packet); | |
| 78 return new CrasOutputStream(params, | |
| 79 mock_manager_.get()); | |
| 80 } | |
| 81 | |
| 82 MockAudioManagerCras& mock_manager() { | |
| 83 return *(mock_manager_.get()); | |
| 84 } | |
| 85 | |
| 86 static const ChannelLayout kTestChannelLayout; | |
| 87 static const int kTestSampleRate; | |
| 88 static const int kTestBitsPerSample; | |
| 89 static const int kTestBytesPerFrame; | |
| 90 static const AudioParameters::Format kTestFormat; | |
| 91 static const uint32 kTestFramesPerPacket; | |
| 92 static const uint32 kTestPacketSize; | |
| 93 static struct cras_audio_format* const kFakeAudioFormat; | |
| 94 static struct cras_stream_params* const kFakeStreamParams; | |
| 95 static struct cras_client* const kFakeClient; | |
| 96 | |
| 97 scoped_ptr<StrictMock<MockAudioManagerCras> > mock_manager_; | |
| 98 | |
| 99 private: | |
| 100 DISALLOW_COPY_AND_ASSIGN(CrasOutputStreamTest); | |
| 101 }; | |
| 102 | |
| 103 const ChannelLayout CrasOutputStreamTest::kTestChannelLayout = | |
| 104 CHANNEL_LAYOUT_STEREO; | |
| 105 const int CrasOutputStreamTest::kTestSampleRate = | |
| 106 AudioParameters::kAudioCDSampleRate; | |
| 107 const int CrasOutputStreamTest::kTestBitsPerSample = 16; | |
| 108 const int CrasOutputStreamTest::kTestBytesPerFrame = | |
| 109 CrasOutputStreamTest::kTestBitsPerSample / 8 * | |
| 110 ChannelLayoutToChannelCount(CrasOutputStreamTest::kTestChannelLayout); | |
| 111 const AudioParameters::Format CrasOutputStreamTest::kTestFormat = | |
| 112 AudioParameters::AUDIO_PCM_LINEAR; | |
| 113 const uint32 CrasOutputStreamTest::kTestFramesPerPacket = 1000; | |
| 114 const uint32 CrasOutputStreamTest::kTestPacketSize = | |
| 115 CrasOutputStreamTest::kTestFramesPerPacket * | |
| 116 CrasOutputStreamTest::kTestBytesPerFrame; | |
| 117 struct cras_audio_format* const CrasOutputStreamTest::kFakeAudioFormat = | |
| 118 reinterpret_cast<struct cras_audio_format*>(1); | |
| 119 struct cras_stream_params* const CrasOutputStreamTest::kFakeStreamParams = | |
| 120 reinterpret_cast<struct cras_stream_params*>(1); | |
| 121 struct cras_client* const CrasOutputStreamTest::kFakeClient = | |
| 122 reinterpret_cast<struct cras_client*>(1); | |
| 123 | |
| 124 TEST_F(CrasOutputStreamTest, ConstructedState) { | |
| 125 // Should support mono. | |
| 126 CrasOutputStream* test_stream = CreateStream(CHANNEL_LAYOUT_MONO); | |
| 127 EXPECT_EQ(CrasOutputStream::kCreated, test_stream->state()); | |
| 128 test_stream->Close(); | |
| 129 | |
| 130 // Should support stereo. | |
| 131 test_stream = CreateStream(CHANNEL_LAYOUT_SURROUND); | |
| 132 EXPECT_EQ(CrasOutputStream::kCreated, test_stream->state()); | |
| 133 test_stream->Close(); | |
| 134 | |
| 135 // Bad bits per sample. | |
| 136 AudioParameters bad_bps_params(kTestFormat, kTestChannelLayout, | |
| 137 kTestSampleRate, kTestBitsPerSample - 1, | |
| 138 kTestFramesPerPacket); | |
| 139 test_stream = new CrasOutputStream(bad_bps_params, mock_manager_.get()); | |
| 140 EXPECT_EQ(CrasOutputStream::kInError, test_stream->state()); | |
| 141 test_stream->Close(); | |
| 142 | |
| 143 // Bad format. | |
| 144 AudioParameters bad_format_params(AudioParameters::AUDIO_LAST_FORMAT, | |
| 145 kTestChannelLayout, kTestSampleRate, | |
| 146 kTestBitsPerSample, kTestFramesPerPacket); | |
| 147 test_stream = new CrasOutputStream(bad_format_params, mock_manager_.get()); | |
| 148 EXPECT_EQ(CrasOutputStream::kInError, test_stream->state()); | |
| 149 test_stream->Close(); | |
| 150 | |
| 151 // Bad sample rate. | |
| 152 AudioParameters bad_rate_params(kTestFormat, kTestChannelLayout, | |
| 153 0, kTestBitsPerSample, kTestFramesPerPacket); | |
| 154 test_stream = new CrasOutputStream(bad_rate_params, mock_manager_.get()); | |
| 155 EXPECT_EQ(CrasOutputStream::kInError, test_stream->state()); | |
| 156 test_stream->Close(); | |
| 157 } | |
| 158 | |
| 159 TEST_F(CrasOutputStreamTest, OpenClose) { | |
| 160 CrasOutputStream* test_stream = CreateStream(CHANNEL_LAYOUT_MONO); | |
| 161 // Open the stream. | |
| 162 ASSERT_TRUE(test_stream->Open()); | |
| 163 EXPECT_EQ(CrasOutputStream::kIsOpened, test_stream->state()); | |
| 164 | |
| 165 // Close the stream. | |
| 166 test_stream->Close(); | |
| 167 } | |
| 168 | |
| 169 TEST_F(CrasOutputStreamTest, StartFailBeforeOpen) { | |
| 170 CrasOutputStream* test_stream = CreateStream(CHANNEL_LAYOUT_MONO); | |
| 171 MockAudioSourceCallback mock_callback; | |
| 172 | |
| 173 test_stream->Start(&mock_callback); | |
| 174 EXPECT_EQ(CrasOutputStream::kInError, test_stream->state()); | |
| 175 } | |
| 176 | |
| 177 TEST_F(CrasOutputStreamTest, StartStop) { | |
| 178 CrasOutputStream* test_stream = CreateStream(CHANNEL_LAYOUT_MONO); | |
| 179 MockAudioSourceCallback mock_callback; | |
| 180 | |
| 181 // Open the stream. | |
| 182 ASSERT_TRUE(test_stream->Open()); | |
| 183 EXPECT_EQ(CrasOutputStream::kIsOpened, test_stream->state()); | |
| 184 | |
| 185 // Start. | |
| 186 test_stream->Start(&mock_callback); | |
| 187 EXPECT_EQ(CrasOutputStream::kIsPlaying, test_stream->state()); | |
| 188 | |
| 189 // Stop. | |
| 190 test_stream->Stop(); | |
| 191 EXPECT_EQ(CrasOutputStream::kIsStopped, test_stream->state()); | |
| 192 | |
| 193 // Close the stream. | |
| 194 test_stream->Close(); | |
| 195 } | |
| 196 | |
| 197 TEST_F(CrasOutputStreamTest, RenderFrames) { | |
| 198 CrasOutputStream* test_stream = CreateStream(CHANNEL_LAYOUT_MONO); | |
| 199 MockAudioSourceCallback mock_callback; | |
| 200 | |
| 201 // Open the stream. | |
| 202 ASSERT_TRUE(test_stream->Open()); | |
| 203 EXPECT_EQ(CrasOutputStream::kIsOpened, test_stream->state()); | |
| 204 | |
| 205 // Render Callback. | |
| 206 EXPECT_CALL(mock_callback, OnMoreData(_, _)) | |
| 207 .WillRepeatedly(Return(kTestFramesPerPacket)); | |
| 208 | |
| 209 // Start. | |
| 210 test_stream->Start(&mock_callback); | |
| 211 EXPECT_EQ(CrasOutputStream::kIsPlaying, test_stream->state()); | |
| 212 | |
| 213 // Stop. | |
| 214 test_stream->Stop(); | |
| 215 EXPECT_EQ(CrasOutputStream::kIsStopped, test_stream->state()); | |
| 216 | |
| 217 // Close the stream. | |
| 218 test_stream->Close(); | |
| 219 } | |
| 220 | |
| 221 } // namespace media | |
| OLD | NEW |