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" | |
9 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
10 #include "base/test/test_message_loop.h" | |
11 #include "base/thread_task_runner_handle.h" | |
12 #include "media/audio/alsa/alsa_output.h" | 9 #include "media/audio/alsa/alsa_output.h" |
13 #include "media/audio/alsa/alsa_wrapper.h" | 10 #include "media/audio/alsa/alsa_wrapper.h" |
14 #include "media/audio/alsa/audio_manager_alsa.h" | 11 #include "media/audio/alsa/audio_manager_alsa.h" |
15 #include "media/audio/fake_audio_log_factory.h" | 12 #include "media/audio/fake_audio_log_factory.h" |
16 #include "media/audio/mock_audio_source_callback.h" | 13 #include "media/audio/mock_audio_source_callback.h" |
17 #include "media/base/data_buffer.h" | 14 #include "media/base/data_buffer.h" |
18 #include "media/base/seekable_buffer.h" | 15 #include "media/base/seekable_buffer.h" |
19 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
21 | 18 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 MOCK_METHOD1(PcmName, const char*(snd_pcm_t* handle)); | 65 MOCK_METHOD1(PcmName, const char*(snd_pcm_t* handle)); |
69 MOCK_METHOD1(PcmAvailUpdate, snd_pcm_sframes_t(snd_pcm_t* handle)); | 66 MOCK_METHOD1(PcmAvailUpdate, snd_pcm_sframes_t(snd_pcm_t* handle)); |
70 MOCK_METHOD1(PcmState, snd_pcm_state_t(snd_pcm_t* handle)); | 67 MOCK_METHOD1(PcmState, snd_pcm_state_t(snd_pcm_t* handle)); |
71 MOCK_METHOD1(PcmStart, int(snd_pcm_t* handle)); | 68 MOCK_METHOD1(PcmStart, int(snd_pcm_t* handle)); |
72 | 69 |
73 MOCK_METHOD1(StrError, const char*(int errnum)); | 70 MOCK_METHOD1(StrError, const char*(int errnum)); |
74 }; | 71 }; |
75 | 72 |
76 class MockAudioManagerAlsa : public AudioManagerAlsa { | 73 class MockAudioManagerAlsa : public AudioManagerAlsa { |
77 public: | 74 public: |
78 MockAudioManagerAlsa() | 75 MockAudioManagerAlsa() : AudioManagerAlsa(&fake_audio_log_factory_) {} |
79 : AudioManagerAlsa(base::ThreadTaskRunnerHandle::Get(), | |
80 base::ThreadTaskRunnerHandle::Get(), | |
81 &fake_audio_log_factory_) {} | |
82 MOCK_METHOD0(Init, void()); | 76 MOCK_METHOD0(Init, void()); |
83 MOCK_METHOD0(HasAudioOutputDevices, bool()); | 77 MOCK_METHOD0(HasAudioOutputDevices, bool()); |
84 MOCK_METHOD0(HasAudioInputDevices, bool()); | 78 MOCK_METHOD0(HasAudioInputDevices, bool()); |
85 MOCK_METHOD1(MakeLinearOutputStream, AudioOutputStream*( | 79 MOCK_METHOD1(MakeLinearOutputStream, AudioOutputStream*( |
86 const AudioParameters& params)); | 80 const AudioParameters& params)); |
87 MOCK_METHOD2(MakeLowLatencyOutputStream, AudioOutputStream*( | 81 MOCK_METHOD2(MakeLowLatencyOutputStream, AudioOutputStream*( |
88 const AudioParameters& params, | 82 const AudioParameters& params, |
89 const std::string& device_id)); | 83 const std::string& device_id)); |
90 MOCK_METHOD2(MakeLowLatencyInputStream, AudioInputStream*( | 84 MOCK_METHOD2(MakeLowLatencyInputStream, AudioInputStream*( |
91 const AudioParameters& params, const std::string& device_id)); | 85 const AudioParameters& params, const std::string& device_id)); |
92 | 86 |
93 // We need to override this function in order to skip the checking the number | 87 // We need to override this function in order to skip the checking the number |
94 // of active output streams. It is because the number of active streams | 88 // of active output streams. It is because the number of active streams |
95 // is managed inside MakeAudioOutputStream, and we don't use | 89 // is managed inside MakeAudioOutputStream, and we don't use |
96 // MakeAudioOutputStream to create the stream in the tests. | 90 // MakeAudioOutputStream to create the stream in the tests. |
97 void ReleaseOutputStream(AudioOutputStream* stream) override { | 91 void ReleaseOutputStream(AudioOutputStream* stream) override { |
98 DCHECK(stream); | 92 DCHECK(stream); |
99 delete stream; | 93 delete stream; |
100 } | 94 } |
101 | 95 |
| 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_; | |
172 StrictMock<MockAlsaWrapper> mock_alsa_wrapper_; | 171 StrictMock<MockAlsaWrapper> mock_alsa_wrapper_; |
173 std::unique_ptr<StrictMock<MockAudioManagerAlsa>, AudioManagerDeleter> | 172 scoped_ptr<StrictMock<MockAudioManagerAlsa> > mock_manager_; |
174 mock_manager_; | 173 base::MessageLoop message_loop_; |
175 scoped_refptr<media::DataBuffer> packet_; | 174 scoped_refptr<media::DataBuffer> packet_; |
176 | 175 |
177 private: | 176 private: |
178 DISALLOW_COPY_AND_ASSIGN(AlsaPcmOutputStreamTest); | 177 DISALLOW_COPY_AND_ASSIGN(AlsaPcmOutputStreamTest); |
179 }; | 178 }; |
180 | 179 |
181 const ChannelLayout AlsaPcmOutputStreamTest::kTestChannelLayout = | 180 const ChannelLayout AlsaPcmOutputStreamTest::kTestChannelLayout = |
182 CHANNEL_LAYOUT_STEREO; | 181 CHANNEL_LAYOUT_STEREO; |
183 const int AlsaPcmOutputStreamTest::kTestSampleRate = | 182 const int AlsaPcmOutputStreamTest::kTestSampleRate = |
184 AudioParameters::kAudioCDSampleRate; | 183 AudioParameters::kAudioCDSampleRate; |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 // Expect scheduling. | 435 // Expect scheduling. |
437 EXPECT_CALL(mock_alsa_wrapper_, PcmAvailUpdate(kFakeHandle)) | 436 EXPECT_CALL(mock_alsa_wrapper_, PcmAvailUpdate(kFakeHandle)) |
438 .Times(AtLeast(2)) | 437 .Times(AtLeast(2)) |
439 .WillRepeatedly(Return(kTestFramesPerPacket)); | 438 .WillRepeatedly(Return(kTestFramesPerPacket)); |
440 | 439 |
441 test_stream->Start(&mock_callback); | 440 test_stream->Start(&mock_callback); |
442 // Start() will issue a WriteTask() directly and then schedule the next one, | 441 // Start() will issue a WriteTask() directly and then schedule the next one, |
443 // call Stop() immediately after to ensure we don't run the message loop | 442 // call Stop() immediately after to ensure we don't run the message loop |
444 // forever. | 443 // forever. |
445 test_stream->Stop(); | 444 test_stream->Stop(); |
446 base::RunLoop().RunUntilIdle(); | 445 message_loop_.RunUntilIdle(); |
447 | 446 |
448 EXPECT_CALL(mock_alsa_wrapper_, PcmClose(kFakeHandle)) | 447 EXPECT_CALL(mock_alsa_wrapper_, PcmClose(kFakeHandle)) |
449 .WillOnce(Return(0)); | 448 .WillOnce(Return(0)); |
450 EXPECT_CALL(mock_alsa_wrapper_, PcmName(kFakeHandle)) | 449 EXPECT_CALL(mock_alsa_wrapper_, PcmName(kFakeHandle)) |
451 .WillOnce(Return(kTestDeviceName)); | 450 .WillOnce(Return(kTestDeviceName)); |
452 test_stream->Close(); | 451 test_stream->Close(); |
453 } | 452 } |
454 | 453 |
455 TEST_F(AlsaPcmOutputStreamTest, WritePacket_FinishedPacket) { | 454 TEST_F(AlsaPcmOutputStreamTest, WritePacket_FinishedPacket) { |
456 AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout); | 455 AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout); |
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
854 | 853 |
855 // TODO(ajwong): Find a way to test whether or not another task has been | 854 // TODO(ajwong): Find a way to test whether or not another task has been |
856 // posted so we can verify that the Alsa code will indeed break the task | 855 // posted so we can verify that the Alsa code will indeed break the task |
857 // posting loop. | 856 // posting loop. |
858 | 857 |
859 test_stream->TransitionTo(AlsaPcmOutputStream::kIsClosed); | 858 test_stream->TransitionTo(AlsaPcmOutputStream::kIsClosed); |
860 test_stream->Close(); | 859 test_stream->Close(); |
861 } | 860 } |
862 | 861 |
863 } // namespace media | 862 } // namespace media |
OLD | NEW |