Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: media/audio/audio_output_controller_unittest.cc

Issue 1806313003: Pass task runners to AudioManager constructor. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: addressed comments Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <stdint.h> 5 #include <stdint.h>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/environment.h" 8 #include "base/environment.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop.h" 13 #include "base/run_loop.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "media/audio/audio_manager_base.h" 14 #include "media/audio/audio_manager_base.h"
16 #include "media/audio/audio_output_controller.h" 15 #include "media/audio/audio_output_controller.h"
17 #include "media/audio/audio_parameters.h" 16 #include "media/audio/audio_parameters.h"
18 #include "media/base/audio_bus.h" 17 #include "media/base/audio_bus.h"
19 #include "testing/gmock/include/gmock/gmock.h" 18 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
21 20
22 using ::testing::_; 21 using ::testing::_;
23 using ::testing::AtLeast; 22 using ::testing::AtLeast;
24 using ::testing::DoAll; 23 using ::testing::DoAll;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 MOCK_METHOD0(Close, void()); 71 MOCK_METHOD0(Close, void());
73 72
74 // Set/get the callback passed to Start(). 73 // Set/get the callback passed to Start().
75 AudioSourceCallback* callback() const { return callback_; } 74 AudioSourceCallback* callback() const { return callback_; }
76 void SetCallback(AudioSourceCallback* asc) { callback_ = asc; } 75 void SetCallback(AudioSourceCallback* asc) { callback_ = asc; }
77 76
78 private: 77 private:
79 AudioSourceCallback* callback_; 78 AudioSourceCallback* callback_;
80 }; 79 };
81 80
82 ACTION_P(SignalEvent, event) {
83 event->Signal();
84 }
85
86 static const float kBufferNonZeroData = 1.0f; 81 static const float kBufferNonZeroData = 1.0f;
87 ACTION(PopulateBuffer) { 82 ACTION(PopulateBuffer) {
88 arg0->Zero(); 83 arg0->Zero();
89 // Note: To confirm the buffer will be populated in these tests, it's 84 // Note: To confirm the buffer will be populated in these tests, it's
90 // sufficient that only the first float in channel 0 is set to the value. 85 // sufficient that only the first float in channel 0 is set to the value.
91 arg0->channel(0)[0] = kBufferNonZeroData; 86 arg0->channel(0)[0] = kBufferNonZeroData;
92 } 87 }
93 88
94 class AudioOutputControllerTest : public testing::Test { 89 class AudioOutputControllerTest : public testing::Test {
95 public: 90 public:
96 AudioOutputControllerTest() 91 AudioOutputControllerTest()
97 : audio_manager_(AudioManager::CreateForTesting()), 92 : audio_manager_(
98 create_event_(false, false), 93 AudioManager::CreateForTesting(message_loop_.task_runner())) {}
99 play_event_(false, false),
100 read_event_(false, false),
101 pause_event_(false, false) {
102 }
103 94
104 ~AudioOutputControllerTest() override {} 95 ~AudioOutputControllerTest() override {}
105 96
106 protected: 97 protected:
107 void Create(int samples_per_packet) { 98 void Create(int samples_per_packet) {
108 EXPECT_FALSE(create_event_.IsSignaled());
109 EXPECT_FALSE(play_event_.IsSignaled());
110 EXPECT_FALSE(read_event_.IsSignaled());
111 EXPECT_FALSE(pause_event_.IsSignaled());
112
113 params_ = AudioParameters( 99 params_ = AudioParameters(
114 AudioParameters::AUDIO_FAKE, kChannelLayout, 100 AudioParameters::AUDIO_FAKE, kChannelLayout,
115 kSampleRate, kBitsPerSample, samples_per_packet); 101 kSampleRate, kBitsPerSample, samples_per_packet);
116 102
117 if (params_.IsValid()) { 103 if (params_.IsValid()) {
118 EXPECT_CALL(mock_event_handler_, OnCreated()) 104 EXPECT_CALL(mock_event_handler_, OnCreated());
119 .WillOnce(SignalEvent(&create_event_));
120 } 105 }
121 106
122 controller_ = AudioOutputController::Create( 107 controller_ = AudioOutputController::Create(
123 audio_manager_.get(), &mock_event_handler_, params_, std::string(), 108 audio_manager_.get(), &mock_event_handler_, params_, std::string(),
124 &mock_sync_reader_); 109 &mock_sync_reader_);
125 if (controller_.get()) 110 if (controller_.get())
126 controller_->SetVolume(kTestVolume); 111 controller_->SetVolume(kTestVolume);
127 112
128 EXPECT_EQ(params_.IsValid(), controller_.get() != NULL); 113 EXPECT_EQ(params_.IsValid(), controller_.get() != NULL);
114 base::RunLoop().RunUntilIdle();
129 } 115 }
130 116
131 void Play() { 117 void Play() {
132 // Expect the event handler to receive one OnPlaying() call. 118 // Expect the event handler to receive one OnPlaying() call.
133 EXPECT_CALL(mock_event_handler_, OnPlaying()) 119 EXPECT_CALL(mock_event_handler_, OnPlaying());
134 .WillOnce(SignalEvent(&play_event_));
135 120
136 // During playback, the mock pretends to provide audio data rendered and 121 // During playback, the mock pretends to provide audio data rendered and
137 // sent from the render process. 122 // sent from the render process.
138 EXPECT_CALL(mock_sync_reader_, UpdatePendingBytes(_, _)).Times(AtLeast(1)); 123 EXPECT_CALL(mock_sync_reader_, UpdatePendingBytes(_, _)).Times(AtLeast(1));
139 EXPECT_CALL(mock_sync_reader_, Read(_)) 124 EXPECT_CALL(mock_sync_reader_, Read(_)).WillRepeatedly(PopulateBuffer());
140 .WillRepeatedly(DoAll(PopulateBuffer(),
141 SignalEvent(&read_event_)));
142 controller_->Play(); 125 controller_->Play();
126 base::RunLoop().RunUntilIdle();
143 } 127 }
144 128
145 void Pause() { 129 void Pause() {
146 // Expect the event handler to receive one OnPaused() call. 130 // Expect the event handler to receive one OnPaused() call.
147 EXPECT_CALL(mock_event_handler_, OnPaused()) 131 EXPECT_CALL(mock_event_handler_, OnPaused());
148 .WillOnce(SignalEvent(&pause_event_));
149 132
150 controller_->Pause(); 133 controller_->Pause();
134 base::RunLoop().RunUntilIdle();
151 } 135 }
152 136
153 void ChangeDevice() { 137 void ChangeDevice() {
154 // Expect the event handler to receive one OnPaying() call and no OnPaused() 138 // Expect the event handler to receive one OnPaying() call and no OnPaused()
155 // call. 139 // call.
156 EXPECT_CALL(mock_event_handler_, OnPlaying()) 140 EXPECT_CALL(mock_event_handler_, OnPlaying());
157 .WillOnce(SignalEvent(&play_event_));
158 EXPECT_CALL(mock_event_handler_, OnPaused()) 141 EXPECT_CALL(mock_event_handler_, OnPaused())
159 .Times(0); 142 .Times(0);
160 143
161 // Simulate a device change event to AudioOutputController from the 144 // Simulate a device change event to AudioOutputController from the
162 // AudioManager. 145 // AudioManager.
163 audio_manager_->GetTaskRunner()->PostTask( 146 audio_manager_->GetTaskRunner()->PostTask(
164 FROM_HERE, 147 FROM_HERE,
165 base::Bind(&AudioOutputController::OnDeviceChange, controller_)); 148 base::Bind(&AudioOutputController::OnDeviceChange, controller_));
166 } 149 }
167 150
168 void Divert(bool was_playing, int num_times_to_be_started) { 151 void Divert(bool was_playing, int num_times_to_be_started) {
169 if (was_playing) { 152 if (was_playing) {
170 // Expect the handler to receive one OnPlaying() call as a result of the 153 // Expect the handler to receive one OnPlaying() call as a result of the
171 // stream switching. 154 // stream switching.
172 EXPECT_CALL(mock_event_handler_, OnPlaying()) 155 EXPECT_CALL(mock_event_handler_, OnPlaying());
173 .WillOnce(SignalEvent(&play_event_));
174 } 156 }
175 157
176 EXPECT_CALL(mock_stream_, Open()) 158 EXPECT_CALL(mock_stream_, Open())
177 .WillOnce(Return(true)); 159 .WillOnce(Return(true));
178 EXPECT_CALL(mock_stream_, SetVolume(kTestVolume)); 160 EXPECT_CALL(mock_stream_, SetVolume(kTestVolume));
179 if (num_times_to_be_started > 0) { 161 if (num_times_to_be_started > 0) {
180 EXPECT_CALL(mock_stream_, Start(NotNull())) 162 EXPECT_CALL(mock_stream_, Start(NotNull()))
181 .Times(num_times_to_be_started) 163 .Times(num_times_to_be_started)
182 .WillRepeatedly( 164 .WillRepeatedly(
183 Invoke(&mock_stream_, &MockAudioOutputStream::SetCallback)); 165 Invoke(&mock_stream_, &MockAudioOutputStream::SetCallback));
184 EXPECT_CALL(mock_stream_, Stop()) 166 EXPECT_CALL(mock_stream_, Stop())
185 .Times(num_times_to_be_started); 167 .Times(num_times_to_be_started);
186 } 168 }
187 169
188 controller_->StartDiverting(&mock_stream_); 170 controller_->StartDiverting(&mock_stream_);
171 base::RunLoop().RunUntilIdle();
189 } 172 }
190 173
191 void ReadDivertedAudioData() { 174 void ReadDivertedAudioData() {
192 scoped_ptr<AudioBus> dest = AudioBus::Create(params_); 175 scoped_ptr<AudioBus> dest = AudioBus::Create(params_);
193 ASSERT_TRUE(mock_stream_.callback()); 176 ASSERT_TRUE(mock_stream_.callback());
194 const int frames_read = 177 const int frames_read =
195 mock_stream_.callback()->OnMoreData(dest.get(), 0, 0); 178 mock_stream_.callback()->OnMoreData(dest.get(), 0, 0);
196 EXPECT_LT(0, frames_read); 179 EXPECT_LT(0, frames_read);
197 EXPECT_EQ(kBufferNonZeroData, dest->channel(0)[0]); 180 EXPECT_EQ(kBufferNonZeroData, dest->channel(0)[0]);
198 } 181 }
199 182
200 void Revert(bool was_playing) { 183 void Revert(bool was_playing) {
201 if (was_playing) { 184 if (was_playing) {
202 // Expect the handler to receive one OnPlaying() call as a result of the 185 // Expect the handler to receive one OnPlaying() call as a result of the
203 // stream switching back. 186 // stream switching back.
204 EXPECT_CALL(mock_event_handler_, OnPlaying()) 187 EXPECT_CALL(mock_event_handler_, OnPlaying());
205 .WillOnce(SignalEvent(&play_event_));
206 } 188 }
207 189
208 EXPECT_CALL(mock_stream_, Close()); 190 EXPECT_CALL(mock_stream_, Close());
209 191
210 controller_->StopDiverting(); 192 controller_->StopDiverting();
193 base::RunLoop().RunUntilIdle();
211 } 194 }
212 195
213 void SwitchDevice(bool diverting) { 196 void SwitchDevice(bool diverting) {
214 if (!diverting) { 197 if (!diverting) {
215 // Expect the current stream to close and a new stream to start 198 // Expect the current stream to close and a new stream to start
216 // playing if not diverting. When diverting, nothing happens 199 // playing if not diverting. When diverting, nothing happens
217 // until diverting is stopped. 200 // until diverting is stopped.
218 EXPECT_CALL(mock_event_handler_, OnPlaying()) 201 EXPECT_CALL(mock_event_handler_, OnPlaying());
219 .WillOnce(SignalEvent(&play_event_));
220 } 202 }
221 203
222 controller_->SwitchOutputDevice(AudioManager::GetDefaultDeviceName(), 204 controller_->SwitchOutputDevice(AudioManager::GetDefaultDeviceName(),
223 base::Bind(&base::DoNothing)); 205 base::Bind(&base::DoNothing));
206 base::RunLoop().RunUntilIdle();
224 } 207 }
225 208
226 void Close() { 209 void Close() {
227 EXPECT_CALL(mock_sync_reader_, Close()); 210 EXPECT_CALL(mock_sync_reader_, Close());
228 211
229 controller_->Close(base::MessageLoop::QuitWhenIdleClosure()); 212 base::RunLoop run_loop;
230 base::MessageLoop::current()->Run(); 213 base::MessageLoop::current()->PostTask(
214 FROM_HERE, base::Bind(&AudioOutputController::Close, controller_,
215 run_loop.QuitClosure()));
216 run_loop.Run();
231 } 217 }
232 218
233 // These help make test sequences more readable. 219 // These help make test sequences more readable.
234 void DivertNeverPlaying() { Divert(false, 0); } 220 void DivertNeverPlaying() { Divert(false, 0); }
235 void DivertWillEventuallyBeTwicePlayed() { Divert(false, 2); } 221 void DivertWillEventuallyBeTwicePlayed() { Divert(false, 2); }
236 void DivertWhilePlaying() { Divert(true, 1); } 222 void DivertWhilePlaying() { Divert(true, 1); }
237 void RevertWasNotPlaying() { Revert(false); } 223 void RevertWasNotPlaying() { Revert(false); }
238 void RevertWhilePlaying() { Revert(true); } 224 void RevertWhilePlaying() { Revert(true); }
239 225
240 // These synchronize the main thread with key events taking place on other
241 // threads.
242 void WaitForCreate() { create_event_.Wait(); }
243 void WaitForPlay() { play_event_.Wait(); }
244 void WaitForReads() {
245 // Note: Arbitrarily chosen, but more iterations causes tests to take
246 // significantly more time.
247 static const int kNumIterations = 3;
248 for (int i = 0; i < kNumIterations; ++i) {
249 read_event_.Wait();
250 }
251 }
252 void WaitForPause() { pause_event_.Wait(); }
253
254 private: 226 private:
255 base::MessageLoopForIO message_loop_; 227 base::MessageLoopForIO message_loop_;
256 scoped_ptr<AudioManager> audio_manager_; 228 ScopedAudioManagerPtr audio_manager_;
257 MockAudioOutputControllerEventHandler mock_event_handler_; 229 MockAudioOutputControllerEventHandler mock_event_handler_;
258 MockAudioOutputControllerSyncReader mock_sync_reader_; 230 MockAudioOutputControllerSyncReader mock_sync_reader_;
259 MockAudioOutputStream mock_stream_; 231 MockAudioOutputStream mock_stream_;
260 base::WaitableEvent create_event_;
261 base::WaitableEvent play_event_;
262 base::WaitableEvent read_event_;
263 base::WaitableEvent pause_event_;
264 AudioParameters params_; 232 AudioParameters params_;
265 scoped_refptr<AudioOutputController> controller_; 233 scoped_refptr<AudioOutputController> controller_;
266 234
267 DISALLOW_COPY_AND_ASSIGN(AudioOutputControllerTest); 235 DISALLOW_COPY_AND_ASSIGN(AudioOutputControllerTest);
268 }; 236 };
269 237
270 TEST_F(AudioOutputControllerTest, CreateAndClose) { 238 TEST_F(AudioOutputControllerTest, CreateAndClose) {
271 Create(kSamplesPerPacket); 239 Create(kSamplesPerPacket);
272 Close(); 240 Close();
273 } 241 }
274 242
275 TEST_F(AudioOutputControllerTest, HardwareBufferTooLarge) { 243 TEST_F(AudioOutputControllerTest, HardwareBufferTooLarge) {
276 Create(kSamplesPerPacket * 1000); 244 Create(kSamplesPerPacket * 1000);
277 } 245 }
278 246
279 TEST_F(AudioOutputControllerTest, PlayAndClose) { 247 TEST_F(AudioOutputControllerTest, PlayAndClose) {
280 Create(kSamplesPerPacket); 248 Create(kSamplesPerPacket);
281 WaitForCreate();
282 Play(); 249 Play();
283 WaitForPlay();
284 WaitForReads();
285 Close(); 250 Close();
286 } 251 }
287 252
288 TEST_F(AudioOutputControllerTest, PlayPauseClose) { 253 TEST_F(AudioOutputControllerTest, PlayPauseClose) {
289 Create(kSamplesPerPacket); 254 Create(kSamplesPerPacket);
290 WaitForCreate();
291 Play(); 255 Play();
292 WaitForPlay();
293 WaitForReads();
294 Pause(); 256 Pause();
295 WaitForPause();
296 Close(); 257 Close();
297 } 258 }
298 259
299 TEST_F(AudioOutputControllerTest, PlayPausePlayClose) { 260 TEST_F(AudioOutputControllerTest, PlayPausePlayClose) {
300 Create(kSamplesPerPacket); 261 Create(kSamplesPerPacket);
301 WaitForCreate();
302 Play(); 262 Play();
303 WaitForPlay();
304 WaitForReads();
305 Pause(); 263 Pause();
306 WaitForPause();
307 Play(); 264 Play();
308 WaitForPlay();
309 Close(); 265 Close();
310 } 266 }
311 267
312 TEST_F(AudioOutputControllerTest, PlayDeviceChangeClose) { 268 TEST_F(AudioOutputControllerTest, PlayDeviceChangeClose) {
313 Create(kSamplesPerPacket); 269 Create(kSamplesPerPacket);
314 WaitForCreate();
315 Play(); 270 Play();
316 WaitForPlay();
317 WaitForReads();
318 ChangeDevice(); 271 ChangeDevice();
319 WaitForPlay();
320 WaitForReads();
321 Close(); 272 Close();
322 } 273 }
323 274
324 TEST_F(AudioOutputControllerTest, PlaySwitchDeviceClose) { 275 TEST_F(AudioOutputControllerTest, PlaySwitchDeviceClose) {
325 Create(kSamplesPerPacket); 276 Create(kSamplesPerPacket);
326 WaitForCreate();
327 Play(); 277 Play();
328 WaitForPlay();
329 WaitForReads();
330 SwitchDevice(false); 278 SwitchDevice(false);
331 WaitForPlay();
332 WaitForReads();
333 Close(); 279 Close();
334 } 280 }
335 281
336 TEST_F(AudioOutputControllerTest, PlayDivertRevertClose) { 282 TEST_F(AudioOutputControllerTest, PlayDivertRevertClose) {
337 Create(kSamplesPerPacket); 283 Create(kSamplesPerPacket);
338 WaitForCreate();
339 Play(); 284 Play();
340 WaitForPlay();
341 WaitForReads();
342 DivertWhilePlaying(); 285 DivertWhilePlaying();
343 WaitForPlay();
344 ReadDivertedAudioData(); 286 ReadDivertedAudioData();
345 RevertWhilePlaying(); 287 RevertWhilePlaying();
346 WaitForPlay();
347 WaitForReads();
348 Close(); 288 Close();
349 } 289 }
350 290
351 TEST_F(AudioOutputControllerTest, PlayDivertSwitchDeviceRevertClose) { 291 TEST_F(AudioOutputControllerTest, PlayDivertSwitchDeviceRevertClose) {
352 Create(kSamplesPerPacket); 292 Create(kSamplesPerPacket);
353 WaitForCreate();
354 Play(); 293 Play();
355 WaitForPlay();
356 WaitForReads();
357 DivertWhilePlaying(); 294 DivertWhilePlaying();
358 WaitForPlay();
359 SwitchDevice(true); 295 SwitchDevice(true);
360 ReadDivertedAudioData(); 296 ReadDivertedAudioData();
361 RevertWhilePlaying(); 297 RevertWhilePlaying();
362 WaitForPlay();
363 WaitForReads();
364 Close(); 298 Close();
365 } 299 }
366 300
367 TEST_F(AudioOutputControllerTest, PlayDivertRevertDivertRevertClose) { 301 TEST_F(AudioOutputControllerTest, PlayDivertRevertDivertRevertClose) {
368 Create(kSamplesPerPacket); 302 Create(kSamplesPerPacket);
369 WaitForCreate();
370 Play(); 303 Play();
371 WaitForPlay();
372 WaitForReads();
373 DivertWhilePlaying(); 304 DivertWhilePlaying();
374 WaitForPlay();
375 ReadDivertedAudioData(); 305 ReadDivertedAudioData();
376 RevertWhilePlaying(); 306 RevertWhilePlaying();
377 WaitForPlay();
378 WaitForReads();
379 DivertWhilePlaying(); 307 DivertWhilePlaying();
380 WaitForPlay();
381 ReadDivertedAudioData(); 308 ReadDivertedAudioData();
382 RevertWhilePlaying(); 309 RevertWhilePlaying();
383 WaitForPlay();
384 WaitForReads();
385 Close(); 310 Close();
386 } 311 }
387 312
388 TEST_F(AudioOutputControllerTest, DivertPlayPausePlayRevertClose) { 313 TEST_F(AudioOutputControllerTest, DivertPlayPausePlayRevertClose) {
389 Create(kSamplesPerPacket); 314 Create(kSamplesPerPacket);
390 WaitForCreate();
391 DivertWillEventuallyBeTwicePlayed(); 315 DivertWillEventuallyBeTwicePlayed();
392 Play(); 316 Play();
393 WaitForPlay();
394 ReadDivertedAudioData(); 317 ReadDivertedAudioData();
395 Pause(); 318 Pause();
396 WaitForPause();
397 Play(); 319 Play();
398 WaitForPlay();
399 ReadDivertedAudioData(); 320 ReadDivertedAudioData();
400 RevertWhilePlaying(); 321 RevertWhilePlaying();
401 WaitForPlay();
402 WaitForReads();
403 Close(); 322 Close();
404 } 323 }
405 324
406 TEST_F(AudioOutputControllerTest, DivertRevertClose) { 325 TEST_F(AudioOutputControllerTest, DivertRevertClose) {
407 Create(kSamplesPerPacket); 326 Create(kSamplesPerPacket);
408 WaitForCreate();
409 DivertNeverPlaying(); 327 DivertNeverPlaying();
410 RevertWasNotPlaying(); 328 RevertWasNotPlaying();
411 Close(); 329 Close();
412 } 330 }
413 331
414 } // namespace media 332 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698