| 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/run_loop.h" |
| 8 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
| 9 #include "base/test/test_timeouts.h" | 10 #include "base/test/test_timeouts.h" |
| 10 #include "media/audio/audio_device_description.h" | 11 #include "media/audio/audio_device_description.h" |
| 11 #include "media/audio/audio_input_controller.h" | 12 #include "media/audio/audio_input_controller.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 15 |
| 15 using ::testing::_; | 16 using ::testing::_; |
| 16 using ::testing::AtLeast; | 17 using ::testing::AtLeast; |
| 17 using ::testing::Exactly; | 18 using ::testing::Exactly; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 35 ACTION_P3(CheckCountAndPostQuitTask, count, limit, loop_or_proxy) { | 36 ACTION_P3(CheckCountAndPostQuitTask, count, limit, loop_or_proxy) { |
| 36 if (++*count >= limit) { | 37 if (++*count >= limit) { |
| 37 loop_or_proxy->PostTask(FROM_HERE, | 38 loop_or_proxy->PostTask(FROM_HERE, |
| 38 base::MessageLoop::QuitWhenIdleClosure()); | 39 base::MessageLoop::QuitWhenIdleClosure()); |
| 39 } | 40 } |
| 40 } | 41 } |
| 41 | 42 |
| 42 // Closes AudioOutputController synchronously. | 43 // Closes AudioOutputController synchronously. |
| 43 static void CloseAudioController(AudioInputController* controller) { | 44 static void CloseAudioController(AudioInputController* controller) { |
| 44 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); | 45 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); |
| 45 base::MessageLoop::current()->Run(); | 46 base::RunLoop().Run(); |
| 46 } | 47 } |
| 47 | 48 |
| 48 class MockAudioInputControllerEventHandler | 49 class MockAudioInputControllerEventHandler |
| 49 : public AudioInputController::EventHandler { | 50 : public AudioInputController::EventHandler { |
| 50 public: | 51 public: |
| 51 MockAudioInputControllerEventHandler() {} | 52 MockAudioInputControllerEventHandler() {} |
| 52 | 53 |
| 53 MOCK_METHOD1(OnCreated, void(AudioInputController* controller)); | 54 MOCK_METHOD1(OnCreated, void(AudioInputController* controller)); |
| 54 MOCK_METHOD1(OnRecording, void(AudioInputController* controller)); | 55 MOCK_METHOD1(OnRecording, void(AudioInputController* controller)); |
| 55 MOCK_METHOD2(OnError, void(AudioInputController* controller, | 56 MOCK_METHOD2(OnError, void(AudioInputController* controller, |
| 56 AudioInputController::ErrorCode error_code)); | 57 AudioInputController::ErrorCode error_code)); |
| 57 MOCK_METHOD2(OnData, | 58 MOCK_METHOD2(OnData, |
| 58 void(AudioInputController* controller, const AudioBus* data)); | 59 void(AudioInputController* controller, const AudioBus* data)); |
| 59 MOCK_METHOD2(OnLog, | 60 MOCK_METHOD2(OnLog, |
| 60 void(AudioInputController* controller, | 61 void(AudioInputController* controller, |
| 61 const std::string& message)); | 62 const std::string& message)); |
| 62 | 63 |
| 63 private: | 64 private: |
| 64 DISALLOW_COPY_AND_ASSIGN(MockAudioInputControllerEventHandler); | 65 DISALLOW_COPY_AND_ASSIGN(MockAudioInputControllerEventHandler); |
| 65 }; | 66 }; |
| 66 | 67 |
| 67 // Test fixture. | 68 // Test fixture. |
| 68 class AudioInputControllerTest : public testing::Test { | 69 class AudioInputControllerTest : public testing::Test { |
| 69 public: | 70 public: |
| 70 AudioInputControllerTest() | 71 AudioInputControllerTest() |
| 71 : audio_manager_( | 72 : audio_manager_( |
| 72 AudioManager::CreateForTesting(message_loop_.task_runner())) { | 73 AudioManager::CreateForTesting(message_loop_.task_runner())) { |
| 73 // Flush the message loop to ensure that AudioManager is fully initialized. | 74 // Flush the message loop to ensure that AudioManager is fully initialized. |
| 74 message_loop_.RunUntilIdle(); | 75 base::RunLoop().RunUntilIdle(); |
| 75 } | 76 } |
| 76 ~AudioInputControllerTest() override { | 77 ~AudioInputControllerTest() override { |
| 77 audio_manager_.reset(); | 78 audio_manager_.reset(); |
| 78 message_loop_.RunUntilIdle(); | 79 base::RunLoop().RunUntilIdle(); |
| 79 } | 80 } |
| 80 | 81 |
| 81 protected: | 82 protected: |
| 82 base::MessageLoop message_loop_; | 83 base::MessageLoop message_loop_; |
| 83 ScopedAudioManagerPtr audio_manager_; | 84 ScopedAudioManagerPtr audio_manager_; |
| 84 | 85 |
| 85 private: | 86 private: |
| 86 DISALLOW_COPY_AND_ASSIGN(AudioInputControllerTest); | 87 DISALLOW_COPY_AND_ASSIGN(AudioInputControllerTest); |
| 87 }; | 88 }; |
| 88 | 89 |
| 89 // Test AudioInputController for create and close without recording audio. | 90 // Test AudioInputController for create and close without recording audio. |
| 90 TEST_F(AudioInputControllerTest, CreateAndClose) { | 91 TEST_F(AudioInputControllerTest, CreateAndClose) { |
| 91 MockAudioInputControllerEventHandler event_handler; | 92 MockAudioInputControllerEventHandler event_handler; |
| 92 | 93 |
| 93 // OnCreated() will be posted once. | 94 // OnCreated() will be posted once. |
| 94 EXPECT_CALL(event_handler, OnCreated(NotNull())) | 95 EXPECT_CALL(event_handler, OnCreated(NotNull())) |
| 95 .WillOnce(QuitMessageLoop(&message_loop_)); | 96 .WillOnce(QuitMessageLoop(&message_loop_)); |
| 96 | 97 |
| 97 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, | 98 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, |
| 98 kSampleRate, kBitsPerSample, kSamplesPerPacket); | 99 kSampleRate, kBitsPerSample, kSamplesPerPacket); |
| 99 | 100 |
| 100 scoped_refptr<AudioInputController> controller = AudioInputController::Create( | 101 scoped_refptr<AudioInputController> controller = AudioInputController::Create( |
| 101 audio_manager_.get(), &event_handler, params, | 102 audio_manager_.get(), &event_handler, params, |
| 102 AudioDeviceDescription::kDefaultDeviceId, NULL); | 103 AudioDeviceDescription::kDefaultDeviceId, NULL); |
| 103 ASSERT_TRUE(controller.get()); | 104 ASSERT_TRUE(controller.get()); |
| 104 | 105 |
| 105 // Wait for OnCreated() to fire. | 106 // Wait for OnCreated() to fire. |
| 106 message_loop_.Run(); | 107 base::RunLoop().Run(); |
| 107 | 108 |
| 108 // Close the AudioInputController synchronously. | 109 // Close the AudioInputController synchronously. |
| 109 CloseAudioController(controller.get()); | 110 CloseAudioController(controller.get()); |
| 110 } | 111 } |
| 111 | 112 |
| 112 // Test a normal call sequence of create, record and close. | 113 // Test a normal call sequence of create, record and close. |
| 113 TEST_F(AudioInputControllerTest, RecordAndClose) { | 114 TEST_F(AudioInputControllerTest, RecordAndClose) { |
| 114 MockAudioInputControllerEventHandler event_handler; | 115 MockAudioInputControllerEventHandler event_handler; |
| 115 int count = 0; | 116 int count = 0; |
| 116 | 117 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 134 // Creating the AudioInputController should render an OnCreated() call. | 135 // Creating the AudioInputController should render an OnCreated() call. |
| 135 scoped_refptr<AudioInputController> controller = AudioInputController::Create( | 136 scoped_refptr<AudioInputController> controller = AudioInputController::Create( |
| 136 audio_manager_.get(), &event_handler, params, | 137 audio_manager_.get(), &event_handler, params, |
| 137 AudioDeviceDescription::kDefaultDeviceId, NULL); | 138 AudioDeviceDescription::kDefaultDeviceId, NULL); |
| 138 ASSERT_TRUE(controller.get()); | 139 ASSERT_TRUE(controller.get()); |
| 139 | 140 |
| 140 // Start recording and trigger one OnRecording() call. | 141 // Start recording and trigger one OnRecording() call. |
| 141 controller->Record(); | 142 controller->Record(); |
| 142 | 143 |
| 143 // Record and wait until ten OnData() callbacks are received. | 144 // Record and wait until ten OnData() callbacks are received. |
| 144 message_loop_.Run(); | 145 base::RunLoop().Run(); |
| 145 | 146 |
| 146 // Close the AudioInputController synchronously. | 147 // Close the AudioInputController synchronously. |
| 147 CloseAudioController(controller.get()); | 148 CloseAudioController(controller.get()); |
| 148 } | 149 } |
| 149 | 150 |
| 150 // Test that the AudioInputController reports an error when the input stream | 151 // Test that the AudioInputController reports an error when the input stream |
| 151 // stops. This can happen when the underlying audio layer stops feeding data as | 152 // stops. This can happen when the underlying audio layer stops feeding data as |
| 152 // a result of a removed microphone device. | 153 // a result of a removed microphone device. |
| 153 // Disabled due to crbug.com/357569 and crbug.com/357501. | 154 // Disabled due to crbug.com/357569 and crbug.com/357501. |
| 154 // TODO(henrika): Remove the test when the timer workaround has been removed. | 155 // TODO(henrika): Remove the test when the timer workaround has been removed. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 183 // Creating the AudioInputController should render an OnCreated() call. | 184 // Creating the AudioInputController should render an OnCreated() call. |
| 184 scoped_refptr<AudioInputController> controller = AudioInputController::Create( | 185 scoped_refptr<AudioInputController> controller = AudioInputController::Create( |
| 185 audio_manager_.get(), &event_handler, params, | 186 audio_manager_.get(), &event_handler, params, |
| 186 AudioDeviceDescription::kDefaultDeviceId, NULL); | 187 AudioDeviceDescription::kDefaultDeviceId, NULL); |
| 187 ASSERT_TRUE(controller.get()); | 188 ASSERT_TRUE(controller.get()); |
| 188 | 189 |
| 189 // Start recording and trigger one OnRecording() call. | 190 // Start recording and trigger one OnRecording() call. |
| 190 controller->Record(); | 191 controller->Record(); |
| 191 | 192 |
| 192 // Record and wait until ten OnData() callbacks are received. | 193 // Record and wait until ten OnData() callbacks are received. |
| 193 message_loop_.Run(); | 194 base::RunLoop().Run(); |
| 194 | 195 |
| 195 // Stop the stream and verify that OnError() is posted. | 196 // Stop the stream and verify that OnError() is posted. |
| 196 AudioInputStream* stream = controller->stream_for_testing(); | 197 AudioInputStream* stream = controller->stream_for_testing(); |
| 197 stream->Stop(); | 198 stream->Stop(); |
| 198 message_loop_.Run(); | 199 base::RunLoop().Run(); |
| 199 | 200 |
| 200 // Close the AudioInputController synchronously. | 201 // Close the AudioInputController synchronously. |
| 201 CloseAudioController(controller.get()); | 202 CloseAudioController(controller.get()); |
| 202 } | 203 } |
| 203 | 204 |
| 204 // Test that AudioInputController rejects insanely large packet sizes. | 205 // Test that AudioInputController rejects insanely large packet sizes. |
| 205 TEST_F(AudioInputControllerTest, SamplesPerPacketTooLarge) { | 206 TEST_F(AudioInputControllerTest, SamplesPerPacketTooLarge) { |
| 206 // Create an audio device with a very large packet size. | 207 // Create an audio device with a very large packet size. |
| 207 MockAudioInputControllerEventHandler event_handler; | 208 MockAudioInputControllerEventHandler event_handler; |
| 208 | 209 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 238 kBitsPerSample, | 239 kBitsPerSample, |
| 239 kSamplesPerPacket); | 240 kSamplesPerPacket); |
| 240 scoped_refptr<AudioInputController> controller = AudioInputController::Create( | 241 scoped_refptr<AudioInputController> controller = AudioInputController::Create( |
| 241 audio_manager_.get(), &event_handler, params, | 242 audio_manager_.get(), &event_handler, params, |
| 242 AudioDeviceDescription::kDefaultDeviceId, NULL); | 243 AudioDeviceDescription::kDefaultDeviceId, NULL); |
| 243 ASSERT_TRUE(controller.get()); | 244 ASSERT_TRUE(controller.get()); |
| 244 | 245 |
| 245 controller->Record(); | 246 controller->Record(); |
| 246 | 247 |
| 247 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); | 248 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); |
| 248 base::MessageLoop::current()->Run(); | 249 base::RunLoop().Run(); |
| 249 | 250 |
| 250 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); | 251 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); |
| 251 base::MessageLoop::current()->Run(); | 252 base::RunLoop().Run(); |
| 252 } | 253 } |
| 253 | 254 |
| 254 } // namespace media | 255 } // namespace media |
| OLD | NEW |