| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stdint.h> | 5 #include <stdint.h> |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/run_loop.h" |
| 8 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "base/test/test_message_loop.h" |
| 11 #include "base/thread_task_runner_handle.h" |
| 9 #include "media/audio/alsa/alsa_output.h" | 12 #include "media/audio/alsa/alsa_output.h" |
| 10 #include "media/audio/alsa/alsa_wrapper.h" | 13 #include "media/audio/alsa/alsa_wrapper.h" |
| 11 #include "media/audio/alsa/audio_manager_alsa.h" | 14 #include "media/audio/alsa/audio_manager_alsa.h" |
| 12 #include "media/audio/fake_audio_log_factory.h" | 15 #include "media/audio/fake_audio_log_factory.h" |
| 13 #include "media/audio/mock_audio_source_callback.h" | 16 #include "media/audio/mock_audio_source_callback.h" |
| 14 #include "media/base/data_buffer.h" | 17 #include "media/base/data_buffer.h" |
| 15 #include "media/base/seekable_buffer.h" | 18 #include "media/base/seekable_buffer.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 21 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 MOCK_METHOD1(PcmName, const char*(snd_pcm_t* handle)); | 68 MOCK_METHOD1(PcmName, const char*(snd_pcm_t* handle)); |
| 66 MOCK_METHOD1(PcmAvailUpdate, snd_pcm_sframes_t(snd_pcm_t* handle)); | 69 MOCK_METHOD1(PcmAvailUpdate, snd_pcm_sframes_t(snd_pcm_t* handle)); |
| 67 MOCK_METHOD1(PcmState, snd_pcm_state_t(snd_pcm_t* handle)); | 70 MOCK_METHOD1(PcmState, snd_pcm_state_t(snd_pcm_t* handle)); |
| 68 MOCK_METHOD1(PcmStart, int(snd_pcm_t* handle)); | 71 MOCK_METHOD1(PcmStart, int(snd_pcm_t* handle)); |
| 69 | 72 |
| 70 MOCK_METHOD1(StrError, const char*(int errnum)); | 73 MOCK_METHOD1(StrError, const char*(int errnum)); |
| 71 }; | 74 }; |
| 72 | 75 |
| 73 class MockAudioManagerAlsa : public AudioManagerAlsa { | 76 class MockAudioManagerAlsa : public AudioManagerAlsa { |
| 74 public: | 77 public: |
| 75 MockAudioManagerAlsa() : AudioManagerAlsa(&fake_audio_log_factory_) {} | 78 MockAudioManagerAlsa() |
| 79 : AudioManagerAlsa(base::ThreadTaskRunnerHandle::Get(), |
| 80 base::ThreadTaskRunnerHandle::Get(), |
| 81 &fake_audio_log_factory_) {} |
| 76 MOCK_METHOD0(Init, void()); | 82 MOCK_METHOD0(Init, void()); |
| 77 MOCK_METHOD0(HasAudioOutputDevices, bool()); | 83 MOCK_METHOD0(HasAudioOutputDevices, bool()); |
| 78 MOCK_METHOD0(HasAudioInputDevices, bool()); | 84 MOCK_METHOD0(HasAudioInputDevices, bool()); |
| 79 MOCK_METHOD1(MakeLinearOutputStream, AudioOutputStream*( | 85 MOCK_METHOD1(MakeLinearOutputStream, AudioOutputStream*( |
| 80 const AudioParameters& params)); | 86 const AudioParameters& params)); |
| 81 MOCK_METHOD2(MakeLowLatencyOutputStream, AudioOutputStream*( | 87 MOCK_METHOD2(MakeLowLatencyOutputStream, AudioOutputStream*( |
| 82 const AudioParameters& params, | 88 const AudioParameters& params, |
| 83 const std::string& device_id)); | 89 const std::string& device_id)); |
| 84 MOCK_METHOD2(MakeLowLatencyInputStream, AudioInputStream*( | 90 MOCK_METHOD2(MakeLowLatencyInputStream, AudioInputStream*( |
| 85 const AudioParameters& params, const std::string& device_id)); | 91 const AudioParameters& params, const std::string& device_id)); |
| 86 | 92 |
| 87 // We need to override this function in order to skip the checking the number | 93 // We need to override this function in order to skip the checking the number |
| 88 // of active output streams. It is because the number of active streams | 94 // of active output streams. It is because the number of active streams |
| 89 // is managed inside MakeAudioOutputStream, and we don't use | 95 // is managed inside MakeAudioOutputStream, and we don't use |
| 90 // MakeAudioOutputStream to create the stream in the tests. | 96 // MakeAudioOutputStream to create the stream in the tests. |
| 91 void ReleaseOutputStream(AudioOutputStream* stream) override { | 97 void ReleaseOutputStream(AudioOutputStream* stream) override { |
| 92 DCHECK(stream); | 98 DCHECK(stream); |
| 93 delete stream; | 99 delete stream; |
| 94 } | 100 } |
| 95 | 101 |
| 96 // We don't mock this method since all tests will do the same thing | |
| 97 // and use the current task runner. | |
| 98 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() override { | |
| 99 return base::MessageLoop::current()->task_runner(); | |
| 100 } | |
| 101 | |
| 102 private: | 102 private: |
| 103 FakeAudioLogFactory fake_audio_log_factory_; | 103 FakeAudioLogFactory fake_audio_log_factory_; |
| 104 }; | 104 }; |
| 105 | 105 |
| 106 class AlsaPcmOutputStreamTest : public testing::Test { | 106 class AlsaPcmOutputStreamTest : public testing::Test { |
| 107 protected: | 107 protected: |
| 108 AlsaPcmOutputStreamTest() { | 108 AlsaPcmOutputStreamTest() { |
| 109 mock_manager_.reset(new StrictMock<MockAudioManagerAlsa>()); | 109 mock_manager_.reset(new StrictMock<MockAudioManagerAlsa>()); |
| 110 } | 110 } |
| 111 | 111 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 // Used to simulate DeviceNameHint. | 161 // Used to simulate DeviceNameHint. |
| 162 static char kSurround40[]; | 162 static char kSurround40[]; |
| 163 static char kSurround41[]; | 163 static char kSurround41[]; |
| 164 static char kSurround50[]; | 164 static char kSurround50[]; |
| 165 static char kSurround51[]; | 165 static char kSurround51[]; |
| 166 static char kSurround70[]; | 166 static char kSurround70[]; |
| 167 static char kSurround71[]; | 167 static char kSurround71[]; |
| 168 static void* kFakeHints[]; | 168 static void* kFakeHints[]; |
| 169 static char kGenericSurround50[]; | 169 static char kGenericSurround50[]; |
| 170 | 170 |
| 171 base::TestMessageLoop message_loop_; |
| 171 StrictMock<MockAlsaWrapper> mock_alsa_wrapper_; | 172 StrictMock<MockAlsaWrapper> mock_alsa_wrapper_; |
| 172 scoped_ptr<StrictMock<MockAudioManagerAlsa> > mock_manager_; | 173 std::unique_ptr<StrictMock<MockAudioManagerAlsa>, AudioManagerDeleter> |
| 173 base::MessageLoop message_loop_; | 174 mock_manager_; |
| 174 scoped_refptr<media::DataBuffer> packet_; | 175 scoped_refptr<media::DataBuffer> packet_; |
| 175 | 176 |
| 176 private: | 177 private: |
| 177 DISALLOW_COPY_AND_ASSIGN(AlsaPcmOutputStreamTest); | 178 DISALLOW_COPY_AND_ASSIGN(AlsaPcmOutputStreamTest); |
| 178 }; | 179 }; |
| 179 | 180 |
| 180 const ChannelLayout AlsaPcmOutputStreamTest::kTestChannelLayout = | 181 const ChannelLayout AlsaPcmOutputStreamTest::kTestChannelLayout = |
| 181 CHANNEL_LAYOUT_STEREO; | 182 CHANNEL_LAYOUT_STEREO; |
| 182 const int AlsaPcmOutputStreamTest::kTestSampleRate = | 183 const int AlsaPcmOutputStreamTest::kTestSampleRate = |
| 183 AudioParameters::kAudioCDSampleRate; | 184 AudioParameters::kAudioCDSampleRate; |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 // Expect scheduling. | 436 // Expect scheduling. |
| 436 EXPECT_CALL(mock_alsa_wrapper_, PcmAvailUpdate(kFakeHandle)) | 437 EXPECT_CALL(mock_alsa_wrapper_, PcmAvailUpdate(kFakeHandle)) |
| 437 .Times(AtLeast(2)) | 438 .Times(AtLeast(2)) |
| 438 .WillRepeatedly(Return(kTestFramesPerPacket)); | 439 .WillRepeatedly(Return(kTestFramesPerPacket)); |
| 439 | 440 |
| 440 test_stream->Start(&mock_callback); | 441 test_stream->Start(&mock_callback); |
| 441 // Start() will issue a WriteTask() directly and then schedule the next one, | 442 // Start() will issue a WriteTask() directly and then schedule the next one, |
| 442 // call Stop() immediately after to ensure we don't run the message loop | 443 // call Stop() immediately after to ensure we don't run the message loop |
| 443 // forever. | 444 // forever. |
| 444 test_stream->Stop(); | 445 test_stream->Stop(); |
| 445 message_loop_.RunUntilIdle(); | 446 base::RunLoop().RunUntilIdle(); |
| 446 | 447 |
| 447 EXPECT_CALL(mock_alsa_wrapper_, PcmClose(kFakeHandle)) | 448 EXPECT_CALL(mock_alsa_wrapper_, PcmClose(kFakeHandle)) |
| 448 .WillOnce(Return(0)); | 449 .WillOnce(Return(0)); |
| 449 EXPECT_CALL(mock_alsa_wrapper_, PcmName(kFakeHandle)) | 450 EXPECT_CALL(mock_alsa_wrapper_, PcmName(kFakeHandle)) |
| 450 .WillOnce(Return(kTestDeviceName)); | 451 .WillOnce(Return(kTestDeviceName)); |
| 451 test_stream->Close(); | 452 test_stream->Close(); |
| 452 } | 453 } |
| 453 | 454 |
| 454 TEST_F(AlsaPcmOutputStreamTest, WritePacket_FinishedPacket) { | 455 TEST_F(AlsaPcmOutputStreamTest, WritePacket_FinishedPacket) { |
| 455 AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout); | 456 AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout); |
| (...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 853 | 854 |
| 854 // TODO(ajwong): Find a way to test whether or not another task has been | 855 // TODO(ajwong): Find a way to test whether or not another task has been |
| 855 // posted so we can verify that the Alsa code will indeed break the task | 856 // posted so we can verify that the Alsa code will indeed break the task |
| 856 // posting loop. | 857 // posting loop. |
| 857 | 858 |
| 858 test_stream->TransitionTo(AlsaPcmOutputStream::kIsClosed); | 859 test_stream->TransitionTo(AlsaPcmOutputStream::kIsClosed); |
| 859 test_stream->Close(); | 860 test_stream->Close(); |
| 860 } | 861 } |
| 861 | 862 |
| 862 } // namespace media | 863 } // namespace media |
| OLD | NEW |