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/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/bind.h" | 6 #include "base/bind.h" |
7 #include "base/environment.h" | 7 #include "base/environment.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 EXPECT_CALL(mock_event_handler_, OnPaused(NotNull())) | 164 EXPECT_CALL(mock_event_handler_, OnPaused(NotNull())) |
165 .Times(0); | 165 .Times(0); |
166 | 166 |
167 // Simulate a device change event to AudioOutputController from the | 167 // Simulate a device change event to AudioOutputController from the |
168 // AudioManager. | 168 // AudioManager. |
169 audio_manager_->GetMessageLoop()->PostTask( | 169 audio_manager_->GetMessageLoop()->PostTask( |
170 FROM_HERE, | 170 FROM_HERE, |
171 base::Bind(&AudioOutputController::OnDeviceChange, controller_)); | 171 base::Bind(&AudioOutputController::OnDeviceChange, controller_)); |
172 } | 172 } |
173 | 173 |
174 void Divert(bool was_playing, bool will_be_playing) { | 174 void Divert(bool was_playing, int num_times_to_be_started) { |
175 if (was_playing) { | 175 if (was_playing) { |
176 // Expect the handler to receive one OnPlaying() call as a result of the | 176 // Expect the handler to receive one OnPlaying() call as a result of the |
177 // stream switching. | 177 // stream switching. |
178 EXPECT_CALL(mock_event_handler_, OnPlaying(NotNull())) | 178 EXPECT_CALL(mock_event_handler_, OnPlaying(NotNull())) |
179 .WillOnce(SignalEvent(&play_event_)); | 179 .WillOnce(SignalEvent(&play_event_)); |
180 } | 180 } |
181 | 181 |
182 EXPECT_CALL(mock_stream_, Open()) | 182 EXPECT_CALL(mock_stream_, Open()) |
183 .WillOnce(Return(true)); | 183 .WillOnce(Return(true)); |
184 EXPECT_CALL(mock_stream_, SetVolume(kTestVolume)); | 184 EXPECT_CALL(mock_stream_, SetVolume(kTestVolume)); |
185 if (will_be_playing) { | 185 if (num_times_to_be_started > 0) { |
186 EXPECT_CALL(mock_stream_, Start(NotNull())) | 186 EXPECT_CALL(mock_stream_, Start(NotNull())) |
187 .Times(AtLeast(1)) | 187 .Times(num_times_to_be_started) |
188 .WillRepeatedly( | 188 .WillRepeatedly( |
189 Invoke(&mock_stream_, &MockAudioOutputStream::SetCallback)); | 189 Invoke(&mock_stream_, &MockAudioOutputStream::SetCallback)); |
| 190 EXPECT_CALL(mock_stream_, Stop()) |
| 191 .Times(num_times_to_be_started); |
190 } | 192 } |
191 // Always expect a Stop() call--even without a Start() call--since | |
192 // AudioOutputController likes to play it safe and Stop() before any | |
193 // Close(). | |
194 EXPECT_CALL(mock_stream_, Stop()) | |
195 .Times(AtLeast(1)); | |
196 | 193 |
197 controller_->StartDiverting(&mock_stream_); | 194 controller_->StartDiverting(&mock_stream_); |
198 } | 195 } |
199 | 196 |
200 void ReadDivertedAudioData() { | 197 void ReadDivertedAudioData() { |
201 scoped_ptr<AudioBus> dest = AudioBus::Create(params_); | 198 scoped_ptr<AudioBus> dest = AudioBus::Create(params_); |
202 ASSERT_TRUE(!!mock_stream_.callback()); | 199 ASSERT_TRUE(!!mock_stream_.callback()); |
203 const int frames_read = | 200 const int frames_read = |
204 mock_stream_.callback()->OnMoreData(dest.get(), AudioBuffersState()); | 201 mock_stream_.callback()->OnMoreData(dest.get(), AudioBuffersState()); |
205 EXPECT_LT(0, frames_read); | 202 EXPECT_LT(0, frames_read); |
(...skipping 14 matching lines...) Expand all Loading... |
220 } | 217 } |
221 | 218 |
222 void Close() { | 219 void Close() { |
223 EXPECT_CALL(mock_sync_reader_, Close()); | 220 EXPECT_CALL(mock_sync_reader_, Close()); |
224 | 221 |
225 controller_->Close(MessageLoop::QuitClosure()); | 222 controller_->Close(MessageLoop::QuitClosure()); |
226 MessageLoop::current()->Run(); | 223 MessageLoop::current()->Run(); |
227 } | 224 } |
228 | 225 |
229 // These help make test sequences more readable. | 226 // These help make test sequences more readable. |
230 void DivertNeverPlaying() { Divert(false, false); } | 227 void DivertNeverPlaying() { Divert(false, 0); } |
231 void DivertWillEventuallyBePlaying() { Divert(false, true); } | 228 void DivertWillEventuallyBeTwicePlayed() { Divert(false, 2); } |
232 void DivertWhilePlaying() { Divert(true, true); } | 229 void DivertWhilePlaying() { Divert(true, 1); } |
233 void RevertWasNotPlaying() { Revert(false); } | 230 void RevertWasNotPlaying() { Revert(false); } |
234 void RevertWhilePlaying() { Revert(true); } | 231 void RevertWhilePlaying() { Revert(true); } |
235 | 232 |
236 // These synchronize the main thread with key events taking place on other | 233 // These synchronize the main thread with key events taking place on other |
237 // threads. | 234 // threads. |
238 void WaitForCreate() { create_event_.Wait(); } | 235 void WaitForCreate() { create_event_.Wait(); } |
239 void WaitForPlay() { play_event_.Wait(); } | 236 void WaitForPlay() { play_event_.Wait(); } |
240 void WaitForReads() { | 237 void WaitForReads() { |
241 // Note: Arbitrarily chosen, but more iterations causes tests to take | 238 // Note: Arbitrarily chosen, but more iterations causes tests to take |
242 // significantly more time. | 239 // significantly more time. |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 ReadDivertedAudioData(); | 346 ReadDivertedAudioData(); |
350 RevertWhilePlaying(); | 347 RevertWhilePlaying(); |
351 WaitForPlay(); | 348 WaitForPlay(); |
352 WaitForReads(); | 349 WaitForReads(); |
353 Close(); | 350 Close(); |
354 } | 351 } |
355 | 352 |
356 TEST_F(AudioOutputControllerTest, DivertPlayPausePlayRevertClose) { | 353 TEST_F(AudioOutputControllerTest, DivertPlayPausePlayRevertClose) { |
357 Create(kSamplesPerPacket); | 354 Create(kSamplesPerPacket); |
358 WaitForCreate(); | 355 WaitForCreate(); |
359 DivertWillEventuallyBePlaying(); | 356 DivertWillEventuallyBeTwicePlayed(); |
360 Play(); | 357 Play(); |
361 WaitForPlay(); | 358 WaitForPlay(); |
362 ReadDivertedAudioData(); | 359 ReadDivertedAudioData(); |
363 Pause(); | 360 Pause(); |
364 WaitForPause(); | 361 WaitForPause(); |
365 Play(); | 362 Play(); |
366 WaitForPlay(); | 363 WaitForPlay(); |
367 ReadDivertedAudioData(); | 364 ReadDivertedAudioData(); |
368 RevertWhilePlaying(); | 365 RevertWhilePlaying(); |
369 WaitForPlay(); | 366 WaitForPlay(); |
370 WaitForReads(); | 367 WaitForReads(); |
371 Close(); | 368 Close(); |
372 } | 369 } |
373 | 370 |
374 TEST_F(AudioOutputControllerTest, DivertRevertClose) { | 371 TEST_F(AudioOutputControllerTest, DivertRevertClose) { |
375 Create(kSamplesPerPacket); | 372 Create(kSamplesPerPacket); |
376 WaitForCreate(); | 373 WaitForCreate(); |
377 DivertNeverPlaying(); | 374 DivertNeverPlaying(); |
378 RevertWasNotPlaying(); | 375 RevertWasNotPlaying(); |
379 Close(); | 376 Close(); |
380 } | 377 } |
381 | 378 |
382 } // namespace media | 379 } // namespace media |
OLD | NEW |