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

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

Powered by Google App Engine
This is Rietveld 408576698