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

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

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