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" |
11 #include "media/audio/audio_device_description.h" | 11 #include "media/audio/audio_device_description.h" |
12 #include "media/audio/audio_input_controller.h" | 12 #include "media/audio/audio_input_controller.h" |
13 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 using ::testing::_; | 16 using ::testing::_; |
17 using ::testing::AtLeast; | 17 using ::testing::AtLeast; |
18 using ::testing::Exactly; | 18 using ::testing::Exactly; |
19 using ::testing::InvokeWithoutArgs; | 19 using ::testing::InvokeWithoutArgs; |
20 using ::testing::NotNull; | 20 using ::testing::NotNull; |
21 | 21 |
22 namespace media { | 22 namespace media { |
23 | 23 |
24 static const int kSampleRate = AudioParameters::kAudioCDSampleRate; | 24 static const int kSampleRate = AudioParameters::kAudioCDSampleRate; |
25 static const int kBitsPerSample = 16; | 25 static const int kBitsPerSample = 16; |
26 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO; | 26 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO; |
27 static const int kSamplesPerPacket = kSampleRate / 10; | 27 static const int kSamplesPerPacket = kSampleRate / 10; |
28 | 28 |
29 // Posts base::MessageLoop::QuitWhenIdleClosure() on specified message loop. | 29 ACTION_P(QuitRunLoop, run_loop) { |
30 ACTION_P(QuitMessageLoop, loop_or_proxy) { | 30 run_loop->QuitWhenIdle(); |
31 loop_or_proxy->PostTask(FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | |
32 } | 31 } |
33 | 32 |
34 // Posts base::MessageLoop::QuitWhenIdleClosure() on specified message loop | 33 // Posts base::MessageLoop::QuitWhenIdleClosure() on specified message loop |
35 // after a certain number of calls given by |limit|. | 34 // after a certain number of calls given by |limit|. |
36 ACTION_P3(CheckCountAndPostQuitTask, count, limit, loop_or_proxy) { | 35 ACTION_P3(CheckCountAndPostQuitTask, count, limit, loop_or_proxy) { |
37 if (++*count >= limit) { | 36 if (++*count >= limit) { |
38 loop_or_proxy->PostTask(FROM_HERE, | 37 loop_or_proxy->PostTask(FROM_HERE, |
39 base::MessageLoop::QuitWhenIdleClosure()); | 38 base::MessageLoop::QuitWhenIdleClosure()); |
40 } | 39 } |
41 } | 40 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 protected: | 81 protected: |
83 base::MessageLoop message_loop_; | 82 base::MessageLoop message_loop_; |
84 ScopedAudioManagerPtr audio_manager_; | 83 ScopedAudioManagerPtr audio_manager_; |
85 | 84 |
86 private: | 85 private: |
87 DISALLOW_COPY_AND_ASSIGN(AudioInputControllerTest); | 86 DISALLOW_COPY_AND_ASSIGN(AudioInputControllerTest); |
88 }; | 87 }; |
89 | 88 |
90 // Test AudioInputController for create and close without recording audio. | 89 // Test AudioInputController for create and close without recording audio. |
91 TEST_F(AudioInputControllerTest, CreateAndClose) { | 90 TEST_F(AudioInputControllerTest, CreateAndClose) { |
| 91 base::RunLoop run_loop; |
| 92 |
92 MockAudioInputControllerEventHandler event_handler; | 93 MockAudioInputControllerEventHandler event_handler; |
93 | 94 |
94 // OnCreated() will be posted once. | 95 // OnCreated() will be posted once. |
95 EXPECT_CALL(event_handler, OnCreated(NotNull())) | 96 EXPECT_CALL(event_handler, OnCreated(NotNull())) |
96 .WillOnce(QuitMessageLoop(&message_loop_)); | 97 .WillOnce(QuitRunLoop(&run_loop)); |
97 | 98 |
98 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, | 99 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, |
99 kSampleRate, kBitsPerSample, kSamplesPerPacket); | 100 kSampleRate, kBitsPerSample, kSamplesPerPacket); |
100 | 101 |
101 scoped_refptr<AudioInputController> controller = AudioInputController::Create( | 102 scoped_refptr<AudioInputController> controller = AudioInputController::Create( |
102 audio_manager_.get(), &event_handler, params, | 103 audio_manager_.get(), &event_handler, params, |
103 AudioDeviceDescription::kDefaultDeviceId, NULL); | 104 AudioDeviceDescription::kDefaultDeviceId, NULL); |
104 ASSERT_TRUE(controller.get()); | 105 ASSERT_TRUE(controller.get()); |
105 | 106 |
106 // Wait for OnCreated() to fire. | 107 // Wait for OnCreated() to fire. |
107 base::RunLoop().Run(); | 108 run_loop.Run(); |
108 | 109 |
109 // Close the AudioInputController synchronously. | 110 // Close the AudioInputController synchronously. |
110 CloseAudioController(controller.get()); | 111 CloseAudioController(controller.get()); |
111 } | 112 } |
112 | 113 |
113 // Test a normal call sequence of create, record and close. | 114 // Test a normal call sequence of create, record and close. |
114 TEST_F(AudioInputControllerTest, RecordAndClose) { | 115 TEST_F(AudioInputControllerTest, RecordAndClose) { |
115 MockAudioInputControllerEventHandler event_handler; | 116 MockAudioInputControllerEventHandler event_handler; |
116 int count = 0; | 117 int count = 0; |
117 | 118 |
(...skipping 29 matching lines...) Expand all Loading... |
147 // Close the AudioInputController synchronously. | 148 // Close the AudioInputController synchronously. |
148 CloseAudioController(controller.get()); | 149 CloseAudioController(controller.get()); |
149 } | 150 } |
150 | 151 |
151 // Test that the AudioInputController reports an error when the input stream | 152 // Test that the AudioInputController reports an error when the input stream |
152 // stops. This can happen when the underlying audio layer stops feeding data as | 153 // stops. This can happen when the underlying audio layer stops feeding data as |
153 // a result of a removed microphone device. | 154 // a result of a removed microphone device. |
154 // Disabled due to crbug.com/357569 and crbug.com/357501. | 155 // Disabled due to crbug.com/357569 and crbug.com/357501. |
155 // TODO(henrika): Remove the test when the timer workaround has been removed. | 156 // TODO(henrika): Remove the test when the timer workaround has been removed. |
156 TEST_F(AudioInputControllerTest, DISABLED_RecordAndError) { | 157 TEST_F(AudioInputControllerTest, DISABLED_RecordAndError) { |
| 158 base::RunLoop run_loop; |
| 159 |
157 MockAudioInputControllerEventHandler event_handler; | 160 MockAudioInputControllerEventHandler event_handler; |
158 int count = 0; | 161 int count = 0; |
159 | 162 |
160 // OnCreated() will be called once. | 163 // OnCreated() will be called once. |
161 EXPECT_CALL(event_handler, OnCreated(NotNull())) | 164 EXPECT_CALL(event_handler, OnCreated(NotNull())) |
162 .Times(Exactly(1)); | 165 .Times(Exactly(1)); |
163 | 166 |
164 // OnRecording() will be called only once. | 167 // OnRecording() will be called only once. |
165 EXPECT_CALL(event_handler, OnRecording(NotNull())) | 168 EXPECT_CALL(event_handler, OnRecording(NotNull())) |
166 .Times(Exactly(1)); | 169 .Times(Exactly(1)); |
167 | 170 |
168 // OnData() shall be called ten times. | 171 // OnData() shall be called ten times. |
169 EXPECT_CALL(event_handler, OnData(NotNull(), NotNull())) | 172 EXPECT_CALL(event_handler, OnData(NotNull(), NotNull())) |
170 .Times(AtLeast(10)) | 173 .Times(AtLeast(10)) |
171 .WillRepeatedly(CheckCountAndPostQuitTask( | 174 .WillRepeatedly(CheckCountAndPostQuitTask( |
172 &count, 10, message_loop_.task_runner())); | 175 &count, 10, message_loop_.task_runner())); |
173 | 176 |
174 // OnError() will be called after the data stream stops while the | 177 // OnError() will be called after the data stream stops while the |
175 // controller is in a recording state. | 178 // controller is in a recording state. |
176 EXPECT_CALL(event_handler, OnError(NotNull(), | 179 EXPECT_CALL(event_handler, |
177 AudioInputController::NO_DATA_ERROR)) | 180 OnError(NotNull(), AudioInputController::NO_DATA_ERROR)) |
178 .Times(Exactly(1)) | 181 .Times(Exactly(1)) |
179 .WillOnce(QuitMessageLoop(&message_loop_)); | 182 .WillOnce(QuitRunLoop(&run_loop)); |
180 | 183 |
181 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, | 184 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, |
182 kSampleRate, kBitsPerSample, kSamplesPerPacket); | 185 kSampleRate, kBitsPerSample, kSamplesPerPacket); |
183 | 186 |
184 // Creating the AudioInputController should render an OnCreated() call. | 187 // Creating the AudioInputController should render an OnCreated() call. |
185 scoped_refptr<AudioInputController> controller = AudioInputController::Create( | 188 scoped_refptr<AudioInputController> controller = AudioInputController::Create( |
186 audio_manager_.get(), &event_handler, params, | 189 audio_manager_.get(), &event_handler, params, |
187 AudioDeviceDescription::kDefaultDeviceId, NULL); | 190 AudioDeviceDescription::kDefaultDeviceId, NULL); |
188 ASSERT_TRUE(controller.get()); | 191 ASSERT_TRUE(controller.get()); |
189 | 192 |
190 // Start recording and trigger one OnRecording() call. | 193 // Start recording and trigger one OnRecording() call. |
191 controller->Record(); | 194 controller->Record(); |
192 | 195 |
193 // Record and wait until ten OnData() callbacks are received. | 196 // Record and wait until ten OnData() callbacks are received. |
194 base::RunLoop().Run(); | 197 run_loop.Run(); |
195 | 198 |
196 // Stop the stream and verify that OnError() is posted. | 199 // Stop the stream and verify that OnError() is posted. |
197 AudioInputStream* stream = controller->stream_for_testing(); | 200 AudioInputStream* stream = controller->stream_for_testing(); |
198 stream->Stop(); | 201 stream->Stop(); |
199 base::RunLoop().Run(); | 202 base::RunLoop().Run(); |
200 | 203 |
201 // Close the AudioInputController synchronously. | 204 // Close the AudioInputController synchronously. |
202 CloseAudioController(controller.get()); | 205 CloseAudioController(controller.get()); |
203 } | 206 } |
204 | 207 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 controller->Record(); | 249 controller->Record(); |
247 | 250 |
248 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); | 251 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); |
249 base::RunLoop().Run(); | 252 base::RunLoop().Run(); |
250 | 253 |
251 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); | 254 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); |
252 base::RunLoop().Run(); | 255 base::RunLoop().Run(); |
253 } | 256 } |
254 | 257 |
255 } // namespace media | 258 } // namespace media |
OLD | NEW |