| 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/run_loop.h" |
| 9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
| 10 #include "base/test/test_timeouts.h" | 10 #include "base/test/test_timeouts.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 } | 46 } |
| 47 | 47 |
| 48 class MockAudioInputControllerEventHandler | 48 class MockAudioInputControllerEventHandler |
| 49 : public AudioInputController::EventHandler { | 49 : public AudioInputController::EventHandler { |
| 50 public: | 50 public: |
| 51 MockAudioInputControllerEventHandler() {} | 51 MockAudioInputControllerEventHandler() {} |
| 52 | 52 |
| 53 MOCK_METHOD1(OnCreated, void(AudioInputController* controller)); | 53 MOCK_METHOD1(OnCreated, void(AudioInputController* controller)); |
| 54 MOCK_METHOD2(OnError, void(AudioInputController* controller, | 54 MOCK_METHOD2(OnError, void(AudioInputController* controller, |
| 55 AudioInputController::ErrorCode error_code)); | 55 AudioInputController::ErrorCode error_code)); |
| 56 MOCK_METHOD2(OnData, | |
| 57 void(AudioInputController* controller, const AudioBus* data)); | |
| 58 MOCK_METHOD2(OnLog, | 56 MOCK_METHOD2(OnLog, |
| 59 void(AudioInputController* controller, | 57 void(AudioInputController* controller, |
| 60 const std::string& message)); | 58 const std::string& message)); |
| 61 | 59 |
| 62 private: | 60 private: |
| 63 DISALLOW_COPY_AND_ASSIGN(MockAudioInputControllerEventHandler); | 61 DISALLOW_COPY_AND_ASSIGN(MockAudioInputControllerEventHandler); |
| 64 }; | 62 }; |
| 65 | 63 |
| 64 class MockSyncWriter : public AudioInputController::SyncWriter { |
| 65 public: |
| 66 MockSyncWriter() {} |
| 67 |
| 68 MOCK_METHOD4(Write, |
| 69 void(const AudioBus* data, |
| 70 double volume, |
| 71 bool key_pressed, |
| 72 uint32_t hardware_delay_bytes)); |
| 73 MOCK_METHOD0(Close, void()); |
| 74 }; |
| 75 |
| 66 // Test fixture. | 76 // Test fixture. |
| 67 class AudioInputControllerTest : public testing::Test { | 77 class AudioInputControllerTest : public testing::Test { |
| 68 public: | 78 public: |
| 69 AudioInputControllerTest() | 79 AudioInputControllerTest() |
| 70 : audio_manager_( | 80 : audio_manager_( |
| 71 AudioManager::CreateForTesting(message_loop_.task_runner())) { | 81 AudioManager::CreateForTesting(message_loop_.task_runner())) { |
| 72 // Flush the message loop to ensure that AudioManager is fully initialized. | 82 // Flush the message loop to ensure that AudioManager is fully initialized. |
| 73 base::RunLoop().RunUntilIdle(); | 83 base::RunLoop().RunUntilIdle(); |
| 74 } | 84 } |
| 75 ~AudioInputControllerTest() override { | 85 ~AudioInputControllerTest() override { |
| 76 audio_manager_.reset(); | 86 audio_manager_.reset(); |
| 77 base::RunLoop().RunUntilIdle(); | 87 base::RunLoop().RunUntilIdle(); |
| 78 } | 88 } |
| 79 | 89 |
| 80 protected: | 90 protected: |
| 81 base::MessageLoop message_loop_; | 91 base::MessageLoop message_loop_; |
| 82 ScopedAudioManagerPtr audio_manager_; | 92 ScopedAudioManagerPtr audio_manager_; |
| 83 | 93 |
| 84 private: | 94 private: |
| 85 DISALLOW_COPY_AND_ASSIGN(AudioInputControllerTest); | 95 DISALLOW_COPY_AND_ASSIGN(AudioInputControllerTest); |
| 86 }; | 96 }; |
| 87 | 97 |
| 88 // Test AudioInputController for create and close without recording audio. | 98 // Test AudioInputController for create and close without recording audio. |
| 89 TEST_F(AudioInputControllerTest, CreateAndClose) { | 99 TEST_F(AudioInputControllerTest, CreateAndClose) { |
| 90 base::RunLoop run_loop; | 100 base::RunLoop run_loop; |
| 91 | 101 |
| 92 MockAudioInputControllerEventHandler event_handler; | 102 MockAudioInputControllerEventHandler event_handler; |
| 103 MockSyncWriter sync_writer; |
| 93 | 104 |
| 94 // OnCreated() will be posted once. | 105 // OnCreated() will be posted once. |
| 95 EXPECT_CALL(event_handler, OnCreated(NotNull())) | 106 EXPECT_CALL(event_handler, OnCreated(NotNull())) |
| 96 .WillOnce(QuitRunLoop(&run_loop)); | 107 .WillOnce(QuitRunLoop(&run_loop)); |
| 97 | 108 |
| 98 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, | 109 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, |
| 99 kSampleRate, kBitsPerSample, kSamplesPerPacket); | 110 kSampleRate, kBitsPerSample, kSamplesPerPacket); |
| 100 | 111 |
| 101 scoped_refptr<AudioInputController> controller = AudioInputController::Create( | 112 scoped_refptr<AudioInputController> controller = AudioInputController::Create( |
| 102 audio_manager_.get(), &event_handler, params, | 113 audio_manager_.get(), &event_handler, &sync_writer, params, |
| 103 AudioDeviceDescription::kDefaultDeviceId, NULL); | 114 AudioDeviceDescription::kDefaultDeviceId, NULL); |
| 104 ASSERT_TRUE(controller.get()); | 115 ASSERT_TRUE(controller.get()); |
| 105 | 116 |
| 106 // Wait for OnCreated() to fire. | 117 // Wait for OnCreated() to fire. |
| 107 run_loop.Run(); | 118 run_loop.Run(); |
| 108 | 119 |
| 109 // Close the AudioInputController synchronously. | 120 // Close the AudioInputController synchronously. |
| 110 CloseAudioController(controller.get()); | 121 CloseAudioController(controller.get()); |
| 111 } | 122 } |
| 112 | 123 |
| 113 // Test a normal call sequence of create, record and close. | 124 // Test a normal call sequence of create, record and close. |
| 114 TEST_F(AudioInputControllerTest, RecordAndClose) { | 125 TEST_F(AudioInputControllerTest, RecordAndClose) { |
| 115 MockAudioInputControllerEventHandler event_handler; | 126 MockAudioInputControllerEventHandler event_handler; |
| 127 MockSyncWriter sync_writer; |
| 116 int count = 0; | 128 int count = 0; |
| 117 | 129 |
| 118 // OnCreated() will be called once. | 130 // OnCreated() will be called once. |
| 119 EXPECT_CALL(event_handler, OnCreated(NotNull())) | 131 EXPECT_CALL(event_handler, OnCreated(NotNull())) |
| 120 .Times(Exactly(1)); | 132 .Times(Exactly(1)); |
| 121 | 133 |
| 122 // OnData() shall be called ten times. | 134 // Write() should be called ten times. |
| 123 EXPECT_CALL(event_handler, OnData(NotNull(), NotNull())) | 135 EXPECT_CALL(sync_writer, Write(NotNull(), _, _, _)) |
| 124 .Times(AtLeast(10)) | 136 .Times(AtLeast(10)) |
| 125 .WillRepeatedly(CheckCountAndPostQuitTask( | 137 .WillRepeatedly( |
| 126 &count, 10, message_loop_.task_runner())); | 138 CheckCountAndPostQuitTask(&count, 10, message_loop_.task_runner())); |
| 127 | 139 |
| 128 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, | 140 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, |
| 129 kSampleRate, kBitsPerSample, kSamplesPerPacket); | 141 kSampleRate, kBitsPerSample, kSamplesPerPacket); |
| 130 | 142 |
| 131 // Creating the AudioInputController should render an OnCreated() call. | 143 // Creating the AudioInputController should render an OnCreated() call. |
| 132 scoped_refptr<AudioInputController> controller = AudioInputController::Create( | 144 scoped_refptr<AudioInputController> controller = AudioInputController::Create( |
| 133 audio_manager_.get(), &event_handler, params, | 145 audio_manager_.get(), &event_handler, &sync_writer, params, |
| 134 AudioDeviceDescription::kDefaultDeviceId, NULL); | 146 AudioDeviceDescription::kDefaultDeviceId, NULL); |
| 135 ASSERT_TRUE(controller.get()); | 147 ASSERT_TRUE(controller.get()); |
| 136 | 148 |
| 137 controller->Record(); | 149 controller->Record(); |
| 138 | 150 |
| 139 // Record and wait until ten OnData() callbacks are received. | 151 // Record and wait until ten Write() callbacks are received. |
| 140 base::RunLoop().Run(); | 152 base::RunLoop().Run(); |
| 141 | 153 |
| 142 // Close the AudioInputController synchronously. | 154 // Close the AudioInputController synchronously. |
| 143 CloseAudioController(controller.get()); | 155 CloseAudioController(controller.get()); |
| 144 } | 156 } |
| 145 | 157 |
| 146 // Test that AudioInputController rejects insanely large packet sizes. | 158 // Test that AudioInputController rejects insanely large packet sizes. |
| 147 TEST_F(AudioInputControllerTest, SamplesPerPacketTooLarge) { | 159 TEST_F(AudioInputControllerTest, SamplesPerPacketTooLarge) { |
| 148 // Create an audio device with a very large packet size. | 160 // Create an audio device with a very large packet size. |
| 149 MockAudioInputControllerEventHandler event_handler; | 161 MockAudioInputControllerEventHandler event_handler; |
| 162 MockSyncWriter sync_writer; |
| 150 | 163 |
| 151 // OnCreated() shall not be called in this test. | 164 // OnCreated() shall not be called in this test. |
| 152 EXPECT_CALL(event_handler, OnCreated(NotNull())) | 165 EXPECT_CALL(event_handler, OnCreated(NotNull())) |
| 153 .Times(Exactly(0)); | 166 .Times(Exactly(0)); |
| 154 | 167 |
| 155 AudioParameters params(AudioParameters::AUDIO_FAKE, | 168 AudioParameters params(AudioParameters::AUDIO_FAKE, |
| 156 kChannelLayout, | 169 kChannelLayout, |
| 157 kSampleRate, | 170 kSampleRate, |
| 158 kBitsPerSample, | 171 kBitsPerSample, |
| 159 kSamplesPerPacket * 1000); | 172 kSamplesPerPacket * 1000); |
| 160 scoped_refptr<AudioInputController> controller = AudioInputController::Create( | 173 scoped_refptr<AudioInputController> controller = AudioInputController::Create( |
| 161 audio_manager_.get(), &event_handler, params, | 174 audio_manager_.get(), &event_handler, &sync_writer, params, |
| 162 AudioDeviceDescription::kDefaultDeviceId, NULL); | 175 AudioDeviceDescription::kDefaultDeviceId, NULL); |
| 163 ASSERT_FALSE(controller.get()); | 176 ASSERT_FALSE(controller.get()); |
| 164 } | 177 } |
| 165 | 178 |
| 166 // Test calling AudioInputController::Close multiple times. | 179 // Test calling AudioInputController::Close multiple times. |
| 167 TEST_F(AudioInputControllerTest, CloseTwice) { | 180 TEST_F(AudioInputControllerTest, CloseTwice) { |
| 168 MockAudioInputControllerEventHandler event_handler; | 181 MockAudioInputControllerEventHandler event_handler; |
| 182 MockSyncWriter sync_writer; |
| 169 | 183 |
| 170 // OnCreated() will be called only once. | 184 // OnCreated() will be called only once. |
| 171 EXPECT_CALL(event_handler, OnCreated(NotNull())); | 185 EXPECT_CALL(event_handler, OnCreated(NotNull())); |
| 172 | 186 |
| 173 AudioParameters params(AudioParameters::AUDIO_FAKE, | 187 AudioParameters params(AudioParameters::AUDIO_FAKE, |
| 174 kChannelLayout, | 188 kChannelLayout, |
| 175 kSampleRate, | 189 kSampleRate, |
| 176 kBitsPerSample, | 190 kBitsPerSample, |
| 177 kSamplesPerPacket); | 191 kSamplesPerPacket); |
| 178 scoped_refptr<AudioInputController> controller = AudioInputController::Create( | 192 scoped_refptr<AudioInputController> controller = AudioInputController::Create( |
| 179 audio_manager_.get(), &event_handler, params, | 193 audio_manager_.get(), &event_handler, &sync_writer, params, |
| 180 AudioDeviceDescription::kDefaultDeviceId, NULL); | 194 AudioDeviceDescription::kDefaultDeviceId, NULL); |
| 181 ASSERT_TRUE(controller.get()); | 195 ASSERT_TRUE(controller.get()); |
| 182 | 196 |
| 183 controller->Record(); | 197 controller->Record(); |
| 184 | 198 |
| 185 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); | 199 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); |
| 186 base::RunLoop().Run(); | 200 base::RunLoop().Run(); |
| 187 | 201 |
| 188 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); | 202 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); |
| 189 base::RunLoop().Run(); | 203 base::RunLoop().Run(); |
| 190 } | 204 } |
| 191 | 205 |
| 192 } // namespace media | 206 } // namespace media |
| OLD | NEW |