| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/macros.h" | 6 #include "base/macros.h" |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
| 9 #include "base/test/test_timeouts.h" | 9 #include "base/test/test_timeouts.h" |
| 10 #include "media/audio/audio_input_controller.h" | 10 #include "media/audio/audio_input_controller.h" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 void(AudioInputController* controller, | 60 void(AudioInputController* controller, |
| 61 const std::string& message)); | 61 const std::string& message)); |
| 62 | 62 |
| 63 private: | 63 private: |
| 64 DISALLOW_COPY_AND_ASSIGN(MockAudioInputControllerEventHandler); | 64 DISALLOW_COPY_AND_ASSIGN(MockAudioInputControllerEventHandler); |
| 65 }; | 65 }; |
| 66 | 66 |
| 67 // Test fixture. | 67 // Test fixture. |
| 68 class AudioInputControllerTest : public testing::Test { | 68 class AudioInputControllerTest : public testing::Test { |
| 69 public: | 69 public: |
| 70 AudioInputControllerTest() {} | 70 AudioInputControllerTest() |
| 71 : audio_manager_( |
| 72 AudioManager::CreateForTesting(message_loop_.task_runner())) {} |
| 71 ~AudioInputControllerTest() override {} | 73 ~AudioInputControllerTest() override {} |
| 72 | 74 |
| 73 protected: | 75 protected: |
| 74 base::MessageLoop message_loop_; | 76 base::MessageLoop message_loop_; |
| 77 ScopedAudioManagerPtr audio_manager_; |
| 75 | 78 |
| 76 private: | 79 private: |
| 77 DISALLOW_COPY_AND_ASSIGN(AudioInputControllerTest); | 80 DISALLOW_COPY_AND_ASSIGN(AudioInputControllerTest); |
| 78 }; | 81 }; |
| 79 | 82 |
| 80 // Test AudioInputController for create and close without recording audio. | 83 // Test AudioInputController for create and close without recording audio. |
| 81 TEST_F(AudioInputControllerTest, CreateAndClose) { | 84 TEST_F(AudioInputControllerTest, CreateAndClose) { |
| 82 MockAudioInputControllerEventHandler event_handler; | 85 MockAudioInputControllerEventHandler event_handler; |
| 83 | 86 |
| 84 // OnCreated() will be posted once. | 87 // OnCreated() will be posted once. |
| 85 EXPECT_CALL(event_handler, OnCreated(NotNull())) | 88 EXPECT_CALL(event_handler, OnCreated(NotNull())) |
| 86 .WillOnce(QuitMessageLoop(&message_loop_)); | 89 .WillOnce(QuitMessageLoop(&message_loop_)); |
| 87 | 90 |
| 88 scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting()); | |
| 89 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, | 91 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, |
| 90 kSampleRate, kBitsPerSample, kSamplesPerPacket); | 92 kSampleRate, kBitsPerSample, kSamplesPerPacket); |
| 91 | 93 |
| 92 scoped_refptr<AudioInputController> controller = | 94 scoped_refptr<AudioInputController> controller = |
| 93 AudioInputController::Create(audio_manager.get(), | 95 AudioInputController::Create(audio_manager_.get(), &event_handler, params, |
| 94 &event_handler, | 96 AudioManagerBase::kDefaultDeviceId, NULL); |
| 95 params, | |
| 96 AudioManagerBase::kDefaultDeviceId, | |
| 97 NULL); | |
| 98 ASSERT_TRUE(controller.get()); | 97 ASSERT_TRUE(controller.get()); |
| 99 | 98 |
| 100 // Wait for OnCreated() to fire. | 99 // Wait for OnCreated() to fire. |
| 101 message_loop_.Run(); | 100 message_loop_.Run(); |
| 102 | 101 |
| 103 // Close the AudioInputController synchronously. | 102 // Close the AudioInputController synchronously. |
| 104 CloseAudioController(controller.get()); | 103 CloseAudioController(controller.get()); |
| 105 } | 104 } |
| 106 | 105 |
| 107 // Test a normal call sequence of create, record and close. | 106 // Test a normal call sequence of create, record and close. |
| 108 TEST_F(AudioInputControllerTest, RecordAndClose) { | 107 TEST_F(AudioInputControllerTest, RecordAndClose) { |
| 109 MockAudioInputControllerEventHandler event_handler; | 108 MockAudioInputControllerEventHandler event_handler; |
| 110 int count = 0; | 109 int count = 0; |
| 111 | 110 |
| 112 // OnCreated() will be called once. | 111 // OnCreated() will be called once. |
| 113 EXPECT_CALL(event_handler, OnCreated(NotNull())) | 112 EXPECT_CALL(event_handler, OnCreated(NotNull())) |
| 114 .Times(Exactly(1)); | 113 .Times(Exactly(1)); |
| 115 | 114 |
| 116 // OnRecording() will be called only once. | 115 // OnRecording() will be called only once. |
| 117 EXPECT_CALL(event_handler, OnRecording(NotNull())) | 116 EXPECT_CALL(event_handler, OnRecording(NotNull())) |
| 118 .Times(Exactly(1)); | 117 .Times(Exactly(1)); |
| 119 | 118 |
| 120 // OnData() shall be called ten times. | 119 // OnData() shall be called ten times. |
| 121 EXPECT_CALL(event_handler, OnData(NotNull(), NotNull())) | 120 EXPECT_CALL(event_handler, OnData(NotNull(), NotNull())) |
| 122 .Times(AtLeast(10)) | 121 .Times(AtLeast(10)) |
| 123 .WillRepeatedly(CheckCountAndPostQuitTask( | 122 .WillRepeatedly(CheckCountAndPostQuitTask( |
| 124 &count, 10, message_loop_.task_runner())); | 123 &count, 10, message_loop_.task_runner())); |
| 125 | 124 |
| 126 scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting()); | |
| 127 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, | 125 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, |
| 128 kSampleRate, kBitsPerSample, kSamplesPerPacket); | 126 kSampleRate, kBitsPerSample, kSamplesPerPacket); |
| 129 | 127 |
| 130 // Creating the AudioInputController should render an OnCreated() call. | 128 // Creating the AudioInputController should render an OnCreated() call. |
| 131 scoped_refptr<AudioInputController> controller = | 129 scoped_refptr<AudioInputController> controller = |
| 132 AudioInputController::Create(audio_manager.get(), | 130 AudioInputController::Create(audio_manager_.get(), &event_handler, params, |
| 133 &event_handler, | 131 AudioManagerBase::kDefaultDeviceId, NULL); |
| 134 params, | |
| 135 AudioManagerBase::kDefaultDeviceId, | |
| 136 NULL); | |
| 137 ASSERT_TRUE(controller.get()); | 132 ASSERT_TRUE(controller.get()); |
| 138 | 133 |
| 139 // Start recording and trigger one OnRecording() call. | 134 // Start recording and trigger one OnRecording() call. |
| 140 controller->Record(); | 135 controller->Record(); |
| 141 | 136 |
| 142 // Record and wait until ten OnData() callbacks are received. | 137 // Record and wait until ten OnData() callbacks are received. |
| 143 message_loop_.Run(); | 138 message_loop_.Run(); |
| 144 | 139 |
| 145 // Close the AudioInputController synchronously. | 140 // Close the AudioInputController synchronously. |
| 146 CloseAudioController(controller.get()); | 141 CloseAudioController(controller.get()); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 169 .WillRepeatedly(CheckCountAndPostQuitTask( | 164 .WillRepeatedly(CheckCountAndPostQuitTask( |
| 170 &count, 10, message_loop_.task_runner())); | 165 &count, 10, message_loop_.task_runner())); |
| 171 | 166 |
| 172 // OnError() will be called after the data stream stops while the | 167 // OnError() will be called after the data stream stops while the |
| 173 // controller is in a recording state. | 168 // controller is in a recording state. |
| 174 EXPECT_CALL(event_handler, OnError(NotNull(), | 169 EXPECT_CALL(event_handler, OnError(NotNull(), |
| 175 AudioInputController::NO_DATA_ERROR)) | 170 AudioInputController::NO_DATA_ERROR)) |
| 176 .Times(Exactly(1)) | 171 .Times(Exactly(1)) |
| 177 .WillOnce(QuitMessageLoop(&message_loop_)); | 172 .WillOnce(QuitMessageLoop(&message_loop_)); |
| 178 | 173 |
| 179 scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting()); | |
| 180 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, | 174 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, |
| 181 kSampleRate, kBitsPerSample, kSamplesPerPacket); | 175 kSampleRate, kBitsPerSample, kSamplesPerPacket); |
| 182 | 176 |
| 183 // Creating the AudioInputController should render an OnCreated() call. | 177 // Creating the AudioInputController should render an OnCreated() call. |
| 184 scoped_refptr<AudioInputController> controller = | 178 scoped_refptr<AudioInputController> controller = |
| 185 AudioInputController::Create(audio_manager.get(), | 179 AudioInputController::Create(audio_manager_.get(), &event_handler, params, |
| 186 &event_handler, | 180 AudioManagerBase::kDefaultDeviceId, NULL); |
| 187 params, | |
| 188 AudioManagerBase::kDefaultDeviceId, | |
| 189 NULL); | |
| 190 ASSERT_TRUE(controller.get()); | 181 ASSERT_TRUE(controller.get()); |
| 191 | 182 |
| 192 // Start recording and trigger one OnRecording() call. | 183 // Start recording and trigger one OnRecording() call. |
| 193 controller->Record(); | 184 controller->Record(); |
| 194 | 185 |
| 195 // Record and wait until ten OnData() callbacks are received. | 186 // Record and wait until ten OnData() callbacks are received. |
| 196 message_loop_.Run(); | 187 message_loop_.Run(); |
| 197 | 188 |
| 198 // Stop the stream and verify that OnError() is posted. | 189 // Stop the stream and verify that OnError() is posted. |
| 199 AudioInputStream* stream = controller->stream_for_testing(); | 190 AudioInputStream* stream = controller->stream_for_testing(); |
| 200 stream->Stop(); | 191 stream->Stop(); |
| 201 message_loop_.Run(); | 192 message_loop_.Run(); |
| 202 | 193 |
| 203 // Close the AudioInputController synchronously. | 194 // Close the AudioInputController synchronously. |
| 204 CloseAudioController(controller.get()); | 195 CloseAudioController(controller.get()); |
| 205 } | 196 } |
| 206 | 197 |
| 207 // Test that AudioInputController rejects insanely large packet sizes. | 198 // Test that AudioInputController rejects insanely large packet sizes. |
| 208 TEST_F(AudioInputControllerTest, SamplesPerPacketTooLarge) { | 199 TEST_F(AudioInputControllerTest, SamplesPerPacketTooLarge) { |
| 209 // Create an audio device with a very large packet size. | 200 // Create an audio device with a very large packet size. |
| 210 MockAudioInputControllerEventHandler event_handler; | 201 MockAudioInputControllerEventHandler event_handler; |
| 211 | 202 |
| 212 // OnCreated() shall not be called in this test. | 203 // OnCreated() shall not be called in this test. |
| 213 EXPECT_CALL(event_handler, OnCreated(NotNull())) | 204 EXPECT_CALL(event_handler, OnCreated(NotNull())) |
| 214 .Times(Exactly(0)); | 205 .Times(Exactly(0)); |
| 215 | 206 |
| 216 scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting()); | |
| 217 AudioParameters params(AudioParameters::AUDIO_FAKE, | 207 AudioParameters params(AudioParameters::AUDIO_FAKE, |
| 218 kChannelLayout, | 208 kChannelLayout, |
| 219 kSampleRate, | 209 kSampleRate, |
| 220 kBitsPerSample, | 210 kBitsPerSample, |
| 221 kSamplesPerPacket * 1000); | 211 kSamplesPerPacket * 1000); |
| 222 scoped_refptr<AudioInputController> controller = | 212 scoped_refptr<AudioInputController> controller = |
| 223 AudioInputController::Create(audio_manager.get(), | 213 AudioInputController::Create(audio_manager_.get(), &event_handler, params, |
| 224 &event_handler, | 214 AudioManagerBase::kDefaultDeviceId, NULL); |
| 225 params, | |
| 226 AudioManagerBase::kDefaultDeviceId, | |
| 227 NULL); | |
| 228 ASSERT_FALSE(controller.get()); | 215 ASSERT_FALSE(controller.get()); |
| 229 } | 216 } |
| 230 | 217 |
| 231 // Test calling AudioInputController::Close multiple times. | 218 // Test calling AudioInputController::Close multiple times. |
| 232 TEST_F(AudioInputControllerTest, CloseTwice) { | 219 TEST_F(AudioInputControllerTest, CloseTwice) { |
| 233 MockAudioInputControllerEventHandler event_handler; | 220 MockAudioInputControllerEventHandler event_handler; |
| 234 | 221 |
| 235 // OnRecording() will be called only once. | 222 // OnRecording() will be called only once. |
| 236 EXPECT_CALL(event_handler, OnCreated(NotNull())); | 223 EXPECT_CALL(event_handler, OnCreated(NotNull())); |
| 237 | 224 |
| 238 // OnRecording() will be called only once. | 225 // OnRecording() will be called only once. |
| 239 EXPECT_CALL(event_handler, OnRecording(NotNull())) | 226 EXPECT_CALL(event_handler, OnRecording(NotNull())) |
| 240 .Times(Exactly(1)); | 227 .Times(Exactly(1)); |
| 241 | 228 |
| 242 scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting()); | |
| 243 AudioParameters params(AudioParameters::AUDIO_FAKE, | 229 AudioParameters params(AudioParameters::AUDIO_FAKE, |
| 244 kChannelLayout, | 230 kChannelLayout, |
| 245 kSampleRate, | 231 kSampleRate, |
| 246 kBitsPerSample, | 232 kBitsPerSample, |
| 247 kSamplesPerPacket); | 233 kSamplesPerPacket); |
| 248 scoped_refptr<AudioInputController> controller = | 234 scoped_refptr<AudioInputController> controller = |
| 249 AudioInputController::Create(audio_manager.get(), | 235 AudioInputController::Create(audio_manager_.get(), &event_handler, params, |
| 250 &event_handler, | 236 AudioManagerBase::kDefaultDeviceId, NULL); |
| 251 params, | |
| 252 AudioManagerBase::kDefaultDeviceId, | |
| 253 NULL); | |
| 254 ASSERT_TRUE(controller.get()); | 237 ASSERT_TRUE(controller.get()); |
| 255 | 238 |
| 256 controller->Record(); | 239 controller->Record(); |
| 257 | 240 |
| 258 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); | 241 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); |
| 259 base::MessageLoop::current()->Run(); | 242 base::MessageLoop::current()->Run(); |
| 260 | 243 |
| 261 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); | 244 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); |
| 262 base::MessageLoop::current()->Run(); | 245 base::MessageLoop::current()->Run(); |
| 263 } | 246 } |
| 264 | 247 |
| 265 } // namespace media | 248 } // namespace media |
| OLD | NEW |