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/bind.h" | 6 #include "base/bind.h" |
6 #include "base/environment.h" | 7 #include "base/environment.h" |
7 #include "base/basictypes.h" | |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
10 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
11 #include "media/audio/audio_output_controller.h" | 13 #include "media/audio/audio_output_controller.h" |
| 14 #include "media/audio/audio_parameters.h" |
| 15 #include "media/base/audio_bus.h" |
12 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
14 | 18 |
15 // TODO(vrk): These tests need to be rewritten! (crbug.com/112500) | |
16 | |
17 using ::testing::_; | 19 using ::testing::_; |
18 using ::testing::AtLeast; | 20 using ::testing::AtLeast; |
19 using ::testing::DoAll; | 21 using ::testing::DoAll; |
20 using ::testing::Exactly; | 22 using ::testing::Invoke; |
21 using ::testing::InvokeWithoutArgs; | |
22 using ::testing::NotNull; | 23 using ::testing::NotNull; |
23 using ::testing::Return; | 24 using ::testing::Return; |
24 | 25 |
25 namespace media { | 26 namespace media { |
26 | 27 |
27 static const int kSampleRate = AudioParameters::kAudioCDSampleRate; | 28 static const int kSampleRate = AudioParameters::kAudioCDSampleRate; |
28 static const int kBitsPerSample = 16; | 29 static const int kBitsPerSample = 16; |
29 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO; | 30 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO; |
30 static const int kSamplesPerPacket = kSampleRate / 10; | 31 static const int kSamplesPerPacket = kSampleRate / 100; |
31 static const int kHardwareBufferSize = kSamplesPerPacket * | 32 static const int kHardwareBufferSize = kSamplesPerPacket * |
32 ChannelLayoutToChannelCount(kChannelLayout) * kBitsPerSample / 8; | 33 ChannelLayoutToChannelCount(kChannelLayout) * kBitsPerSample / 8; |
| 34 static const double kTestVolume = 0.25; |
33 | 35 |
34 class MockAudioOutputControllerEventHandler | 36 class MockAudioOutputControllerEventHandler |
35 : public AudioOutputController::EventHandler { | 37 : public AudioOutputController::EventHandler { |
36 public: | 38 public: |
37 MockAudioOutputControllerEventHandler() {} | 39 MockAudioOutputControllerEventHandler() {} |
38 | 40 |
39 MOCK_METHOD1(OnCreated, void(AudioOutputController* controller)); | 41 MOCK_METHOD1(OnCreated, void(AudioOutputController* controller)); |
40 MOCK_METHOD1(OnPlaying, void(AudioOutputController* controller)); | 42 MOCK_METHOD1(OnPlaying, void(AudioOutputController* controller)); |
41 MOCK_METHOD1(OnPaused, void(AudioOutputController* controller)); | 43 MOCK_METHOD1(OnPaused, void(AudioOutputController* controller)); |
42 MOCK_METHOD2(OnError, void(AudioOutputController* controller, | 44 MOCK_METHOD2(OnError, void(AudioOutputController* controller, |
(...skipping 10 matching lines...) Expand all Loading... |
53 | 55 |
54 MOCK_METHOD1(UpdatePendingBytes, void(uint32 bytes)); | 56 MOCK_METHOD1(UpdatePendingBytes, void(uint32 bytes)); |
55 MOCK_METHOD2(Read, int(AudioBus* source, AudioBus* dest)); | 57 MOCK_METHOD2(Read, int(AudioBus* source, AudioBus* dest)); |
56 MOCK_METHOD0(Close, void()); | 58 MOCK_METHOD0(Close, void()); |
57 MOCK_METHOD0(DataReady, bool()); | 59 MOCK_METHOD0(DataReady, bool()); |
58 | 60 |
59 private: | 61 private: |
60 DISALLOW_COPY_AND_ASSIGN(MockAudioOutputControllerSyncReader); | 62 DISALLOW_COPY_AND_ASSIGN(MockAudioOutputControllerSyncReader); |
61 }; | 63 }; |
62 | 64 |
| 65 class MockAudioOutputStream : public AudioOutputStream { |
| 66 public: |
| 67 MOCK_METHOD0(Open, bool()); |
| 68 MOCK_METHOD1(Start, void(AudioSourceCallback* callback)); |
| 69 MOCK_METHOD0(Stop, void()); |
| 70 MOCK_METHOD1(SetVolume, void(double volume)); |
| 71 MOCK_METHOD1(GetVolume, void(double* volume)); |
| 72 MOCK_METHOD0(Close, void()); |
| 73 |
| 74 // Set/get the callback passed to Start(). |
| 75 AudioSourceCallback* callback() const { return callback_; } |
| 76 void SetCallback(AudioSourceCallback* asc) { callback_ = asc; } |
| 77 |
| 78 private: |
| 79 AudioSourceCallback* callback_; |
| 80 }; |
| 81 |
63 ACTION_P(SignalEvent, event) { | 82 ACTION_P(SignalEvent, event) { |
64 event->Signal(); | 83 event->Signal(); |
65 } | 84 } |
66 | 85 |
67 // Custom action to clear a memory buffer. | 86 static const float kBufferNonZeroData = 1.0f; |
68 ACTION(ClearBuffer) { | 87 ACTION(PopulateBuffer) { |
69 arg1->Zero(); | 88 arg1->Zero(); |
70 } | 89 // Note: To confirm the buffer will be populated in these tests, it's |
71 | 90 // sufficient that only the first float in channel 0 is set to the value. |
72 // Closes AudioOutputController synchronously. | 91 arg1->channel(0)[0] = kBufferNonZeroData; |
73 static void CloseAudioController(AudioOutputController* controller) { | |
74 controller->Close(MessageLoop::QuitClosure()); | |
75 MessageLoop::current()->Run(); | |
76 } | 92 } |
77 | 93 |
78 class AudioOutputControllerTest : public testing::Test { | 94 class AudioOutputControllerTest : public testing::Test { |
79 public: | 95 public: |
80 AudioOutputControllerTest() {} | 96 AudioOutputControllerTest() |
81 virtual ~AudioOutputControllerTest() {} | 97 : audio_manager_(AudioManager::Create()), |
| 98 create_event_(false, false), |
| 99 play_event_(false, false), |
| 100 read_event_(false, false), |
| 101 pause_event_(false, false) { |
| 102 } |
| 103 |
| 104 virtual ~AudioOutputControllerTest() { |
| 105 } |
82 | 106 |
83 protected: | 107 protected: |
| 108 void Create(int samples_per_packet) { |
| 109 EXPECT_FALSE(create_event_.IsSignaled()); |
| 110 EXPECT_FALSE(play_event_.IsSignaled()); |
| 111 EXPECT_FALSE(read_event_.IsSignaled()); |
| 112 EXPECT_FALSE(pause_event_.IsSignaled()); |
| 113 |
| 114 params_ = AudioParameters( |
| 115 AudioParameters::AUDIO_FAKE, kChannelLayout, |
| 116 kSampleRate, kBitsPerSample, samples_per_packet); |
| 117 |
| 118 if (params_.IsValid()) { |
| 119 EXPECT_CALL(mock_event_handler_, OnCreated(NotNull())) |
| 120 .WillOnce(SignalEvent(&create_event_)); |
| 121 } |
| 122 |
| 123 controller_ = AudioOutputController::Create( |
| 124 audio_manager_.get(), &mock_event_handler_, params_, |
| 125 &mock_sync_reader_); |
| 126 if (controller_) |
| 127 controller_->SetVolume(kTestVolume); |
| 128 |
| 129 EXPECT_EQ(params_.IsValid(), controller_ != NULL); |
| 130 } |
| 131 |
| 132 void Play() { |
| 133 // Expect the event handler to receive one OnPlaying() call. |
| 134 EXPECT_CALL(mock_event_handler_, OnPlaying(NotNull())) |
| 135 .WillOnce(SignalEvent(&play_event_)); |
| 136 |
| 137 // During playback, the mock pretends to provide audio data rendered and |
| 138 // sent from the render process. |
| 139 EXPECT_CALL(mock_sync_reader_, UpdatePendingBytes(_)) |
| 140 .Times(AtLeast(2)); |
| 141 EXPECT_CALL(mock_sync_reader_, Read(_, _)) |
| 142 .WillRepeatedly(DoAll(PopulateBuffer(), |
| 143 SignalEvent(&read_event_), |
| 144 Return(params_.frames_per_buffer()))); |
| 145 EXPECT_CALL(mock_sync_reader_, DataReady()) |
| 146 .WillRepeatedly(Return(true)); |
| 147 |
| 148 controller_->Play(); |
| 149 } |
| 150 |
| 151 void Pause() { |
| 152 // Expect the event handler to receive one OnPaused() call. |
| 153 EXPECT_CALL(mock_event_handler_, OnPaused(NotNull())) |
| 154 .WillOnce(SignalEvent(&pause_event_)); |
| 155 |
| 156 controller_->Pause(); |
| 157 } |
| 158 |
| 159 void ChangeDevice() { |
| 160 // Expect the event handler to receive one OnPaying() call and no OnPaused() |
| 161 // call. |
| 162 EXPECT_CALL(mock_event_handler_, OnPlaying(NotNull())) |
| 163 .WillOnce(SignalEvent(&play_event_)); |
| 164 EXPECT_CALL(mock_event_handler_, OnPaused(NotNull())) |
| 165 .Times(0); |
| 166 |
| 167 // Simulate a device change event to AudioOutputController from the |
| 168 // AudioManager. |
| 169 audio_manager_->GetMessageLoop()->PostTask( |
| 170 FROM_HERE, |
| 171 base::Bind(&AudioOutputController::OnDeviceChange, controller_)); |
| 172 } |
| 173 |
| 174 void Divert(bool was_playing, bool will_be_playing) { |
| 175 if (was_playing) { |
| 176 // Expect the handler to receive one OnPlaying() call as a result of the |
| 177 // stream switching. |
| 178 EXPECT_CALL(mock_event_handler_, OnPlaying(NotNull())) |
| 179 .WillOnce(SignalEvent(&play_event_)); |
| 180 } |
| 181 |
| 182 EXPECT_CALL(mock_stream_, Open()) |
| 183 .WillOnce(Return(true)); |
| 184 EXPECT_CALL(mock_stream_, SetVolume(kTestVolume)); |
| 185 if (will_be_playing) { |
| 186 EXPECT_CALL(mock_stream_, Start(NotNull())) |
| 187 .Times(AtLeast(1)) |
| 188 .WillRepeatedly( |
| 189 Invoke(&mock_stream_, &MockAudioOutputStream::SetCallback)); |
| 190 } |
| 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 |
| 197 controller_->StartDiverting(&mock_stream_); |
| 198 } |
| 199 |
| 200 void ReadDivertedAudioData() { |
| 201 scoped_ptr<AudioBus> dest = AudioBus::Create(params_); |
| 202 ASSERT_TRUE(!!mock_stream_.callback()); |
| 203 const int frames_read = |
| 204 mock_stream_.callback()->OnMoreData(dest.get(), AudioBuffersState()); |
| 205 EXPECT_LT(0, frames_read); |
| 206 EXPECT_EQ(kBufferNonZeroData, dest->channel(0)[0]); |
| 207 } |
| 208 |
| 209 void Revert(bool was_playing) { |
| 210 if (was_playing) { |
| 211 // Expect the handler to receive one OnPlaying() call as a result of the |
| 212 // stream switching back. |
| 213 EXPECT_CALL(mock_event_handler_, OnPlaying(NotNull())) |
| 214 .WillOnce(SignalEvent(&play_event_)); |
| 215 } |
| 216 |
| 217 EXPECT_CALL(mock_stream_, Close()); |
| 218 |
| 219 controller_->StopDiverting(); |
| 220 } |
| 221 |
| 222 void Close() { |
| 223 EXPECT_CALL(mock_sync_reader_, Close()); |
| 224 |
| 225 controller_->Close(MessageLoop::QuitClosure()); |
| 226 MessageLoop::current()->Run(); |
| 227 } |
| 228 |
| 229 // These help make test sequences more readable. |
| 230 void DivertNeverPlaying() { Divert(false, false); } |
| 231 void DivertWillEventuallyBePlaying() { Divert(false, true); } |
| 232 void DivertWhilePlaying() { Divert(true, true); } |
| 233 void RevertWasNotPlaying() { Revert(false); } |
| 234 void RevertWhilePlaying() { Revert(true); } |
| 235 |
| 236 // These synchronize the main thread with key events taking place on other |
| 237 // threads. |
| 238 void WaitForCreate() { create_event_.Wait(); } |
| 239 void WaitForPlay() { play_event_.Wait(); } |
| 240 void WaitForReads() { |
| 241 // Note: Arbitrarily chosen, but more iterations causes tests to take |
| 242 // significantly more time. |
| 243 static const int kNumIterations = 3; |
| 244 for (int i = 0; i < kNumIterations; ++i) { |
| 245 read_event_.Wait(); |
| 246 } |
| 247 } |
| 248 void WaitForPause() { pause_event_.Wait(); } |
| 249 |
| 250 private: |
84 MessageLoopForIO message_loop_; | 251 MessageLoopForIO message_loop_; |
85 | 252 scoped_ptr<AudioManager> audio_manager_; |
86 private: | 253 MockAudioOutputControllerEventHandler mock_event_handler_; |
| 254 MockAudioOutputControllerSyncReader mock_sync_reader_; |
| 255 MockAudioOutputStream mock_stream_; |
| 256 base::WaitableEvent create_event_; |
| 257 base::WaitableEvent play_event_; |
| 258 base::WaitableEvent read_event_; |
| 259 base::WaitableEvent pause_event_; |
| 260 AudioParameters params_; |
| 261 scoped_refptr<AudioOutputController> controller_; |
| 262 |
87 DISALLOW_COPY_AND_ASSIGN(AudioOutputControllerTest); | 263 DISALLOW_COPY_AND_ASSIGN(AudioOutputControllerTest); |
88 }; | 264 }; |
89 | 265 |
90 TEST_F(AudioOutputControllerTest, CreateAndClose) { | 266 TEST_F(AudioOutputControllerTest, CreateAndClose) { |
91 scoped_ptr<AudioManager> audio_manager(AudioManager::Create()); | 267 Create(kSamplesPerPacket); |
92 if (!audio_manager->HasAudioOutputDevices()) | 268 Close(); |
93 return; | 269 } |
94 | 270 |
95 MockAudioOutputControllerEventHandler event_handler; | 271 TEST_F(AudioOutputControllerTest, HardwareBufferTooLarge) { |
96 | 272 Create(kSamplesPerPacket * 1000); |
97 EXPECT_CALL(event_handler, OnCreated(NotNull())) | 273 } |
98 .Times(1); | 274 |
99 | 275 TEST_F(AudioOutputControllerTest, PlayAndClose) { |
100 MockAudioOutputControllerSyncReader sync_reader; | 276 Create(kSamplesPerPacket); |
101 EXPECT_CALL(sync_reader, Close()); | 277 WaitForCreate(); |
102 | 278 Play(); |
103 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, | 279 WaitForPlay(); |
104 kSampleRate, kBitsPerSample, kSamplesPerPacket); | 280 WaitForReads(); |
105 scoped_refptr<AudioOutputController> controller = | 281 Close(); |
106 AudioOutputController::Create( | |
107 audio_manager.get(), &event_handler, params, &sync_reader); | |
108 ASSERT_TRUE(controller.get()); | |
109 | |
110 // Close the controller immediately. | |
111 CloseAudioController(controller); | |
112 } | 282 } |
113 | 283 |
114 TEST_F(AudioOutputControllerTest, PlayPauseClose) { | 284 TEST_F(AudioOutputControllerTest, PlayPauseClose) { |
115 scoped_ptr<AudioManager> audio_manager(AudioManager::Create()); | 285 Create(kSamplesPerPacket); |
116 if (!audio_manager->HasAudioOutputDevices()) | 286 WaitForCreate(); |
117 return; | 287 Play(); |
118 | 288 WaitForPlay(); |
119 MockAudioOutputControllerEventHandler event_handler; | 289 WaitForReads(); |
120 base::WaitableEvent event(false, false); | 290 Pause(); |
121 base::WaitableEvent pause_event(false, false); | 291 WaitForPause(); |
122 | 292 Close(); |
123 // If OnCreated is called then signal the event. | |
124 EXPECT_CALL(event_handler, OnCreated(NotNull())) | |
125 .WillOnce(InvokeWithoutArgs(&event, &base::WaitableEvent::Signal)); | |
126 | |
127 // OnPlaying() will be called only once. | |
128 EXPECT_CALL(event_handler, OnPlaying(NotNull())); | |
129 | |
130 MockAudioOutputControllerSyncReader sync_reader; | |
131 EXPECT_CALL(sync_reader, UpdatePendingBytes(_)) | |
132 .Times(AtLeast(2)); | |
133 EXPECT_CALL(sync_reader, Read(_, _)) | |
134 .WillRepeatedly(DoAll(ClearBuffer(), SignalEvent(&event), | |
135 Return(4))); | |
136 EXPECT_CALL(sync_reader, DataReady()) | |
137 .WillRepeatedly(Return(true)); | |
138 EXPECT_CALL(event_handler, OnPaused(NotNull())) | |
139 .WillOnce(InvokeWithoutArgs(&pause_event, &base::WaitableEvent::Signal)); | |
140 EXPECT_CALL(sync_reader, Close()); | |
141 | |
142 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, | |
143 kSampleRate, kBitsPerSample, kSamplesPerPacket); | |
144 scoped_refptr<AudioOutputController> controller = | |
145 AudioOutputController::Create( | |
146 audio_manager.get(), &event_handler, params, &sync_reader); | |
147 ASSERT_TRUE(controller.get()); | |
148 | |
149 // Wait for OnCreated() to be called. | |
150 event.Wait(); | |
151 | |
152 ASSERT_FALSE(pause_event.IsSignaled()); | |
153 controller->Play(); | |
154 controller->Pause(); | |
155 pause_event.Wait(); | |
156 | |
157 // Now stop the controller. | |
158 CloseAudioController(controller); | |
159 } | |
160 | |
161 TEST_F(AudioOutputControllerTest, HardwareBufferTooLarge) { | |
162 scoped_ptr<AudioManager> audio_manager(AudioManager::Create()); | |
163 if (!audio_manager->HasAudioOutputDevices()) | |
164 return; | |
165 | |
166 // Create an audio device with a very large hardware buffer size. | |
167 MockAudioOutputControllerEventHandler event_handler; | |
168 | |
169 MockAudioOutputControllerSyncReader sync_reader; | |
170 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, | |
171 kSampleRate, kBitsPerSample, | |
172 kSamplesPerPacket * 1000); | |
173 scoped_refptr<AudioOutputController> controller = | |
174 AudioOutputController::Create( | |
175 audio_manager.get(), &event_handler, params, &sync_reader); | |
176 | |
177 // Use assert because we don't stop the device and assume we can't | |
178 // create one. | |
179 ASSERT_FALSE(controller); | |
180 } | 293 } |
181 | 294 |
182 TEST_F(AudioOutputControllerTest, PlayPausePlayClose) { | 295 TEST_F(AudioOutputControllerTest, PlayPausePlayClose) { |
183 scoped_ptr<AudioManager> audio_manager(AudioManager::Create()); | 296 Create(kSamplesPerPacket); |
184 if (!audio_manager->HasAudioOutputDevices()) | 297 WaitForCreate(); |
185 return; | 298 Play(); |
186 | 299 WaitForPlay(); |
187 MockAudioOutputControllerEventHandler event_handler; | 300 WaitForReads(); |
188 base::WaitableEvent event(false, false); | 301 Pause(); |
189 EXPECT_CALL(event_handler, OnCreated(NotNull())) | 302 WaitForPause(); |
190 .WillOnce(InvokeWithoutArgs(&event, &base::WaitableEvent::Signal)); | 303 Play(); |
191 | 304 WaitForPlay(); |
192 // OnPlaying() will be called only once. | 305 Close(); |
193 base::WaitableEvent play_event(false, false); | 306 } |
194 EXPECT_CALL(event_handler, OnPlaying(NotNull())) | 307 |
195 .WillOnce(InvokeWithoutArgs(&play_event, &base::WaitableEvent::Signal)); | 308 TEST_F(AudioOutputControllerTest, PlayDeviceChangeClose) { |
196 | 309 Create(kSamplesPerPacket); |
197 // OnPaused() should never be called since the pause during kStarting is | 310 WaitForCreate(); |
198 // dropped when the second play comes in. | 311 Play(); |
199 EXPECT_CALL(event_handler, OnPaused(NotNull())) | 312 WaitForPlay(); |
200 .Times(0); | 313 WaitForReads(); |
201 | 314 ChangeDevice(); |
202 MockAudioOutputControllerSyncReader sync_reader; | 315 WaitForPlay(); |
203 EXPECT_CALL(sync_reader, UpdatePendingBytes(_)) | 316 WaitForReads(); |
204 .Times(AtLeast(1)); | 317 Close(); |
205 EXPECT_CALL(sync_reader, Read(_, _)) | 318 } |
206 .WillRepeatedly(DoAll(ClearBuffer(), SignalEvent(&event), Return(4))); | 319 |
207 EXPECT_CALL(sync_reader, DataReady()) | 320 TEST_F(AudioOutputControllerTest, PlayDivertRevertClose) { |
208 .WillRepeatedly(Return(true)); | 321 Create(kSamplesPerPacket); |
209 EXPECT_CALL(sync_reader, Close()); | 322 WaitForCreate(); |
210 | 323 Play(); |
211 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, | 324 WaitForPlay(); |
212 kSampleRate, kBitsPerSample, kSamplesPerPacket); | 325 WaitForReads(); |
213 scoped_refptr<AudioOutputController> controller = | 326 DivertWhilePlaying(); |
214 AudioOutputController::Create( | 327 WaitForPlay(); |
215 audio_manager.get(), &event_handler, params, &sync_reader); | 328 ReadDivertedAudioData(); |
216 ASSERT_TRUE(controller.get()); | 329 RevertWhilePlaying(); |
217 | 330 WaitForPlay(); |
218 // Wait for OnCreated() to be called. | 331 WaitForReads(); |
219 event.Wait(); | 332 Close(); |
220 | 333 } |
221 ASSERT_FALSE(play_event.IsSignaled()); | 334 |
222 controller->Play(); | 335 TEST_F(AudioOutputControllerTest, PlayDivertRevertDivertRevertClose) { |
223 controller->Pause(); | 336 Create(kSamplesPerPacket); |
224 controller->Play(); | 337 WaitForCreate(); |
225 play_event.Wait(); | 338 Play(); |
226 | 339 WaitForPlay(); |
227 // Now stop the controller. | 340 WaitForReads(); |
228 CloseAudioController(controller); | 341 DivertWhilePlaying(); |
229 } | 342 WaitForPlay(); |
230 | 343 ReadDivertedAudioData(); |
231 // Ensure state change events are handled. | 344 RevertWhilePlaying(); |
232 TEST_F(AudioOutputControllerTest, PlayStateChangeClose) { | 345 WaitForPlay(); |
233 scoped_ptr<AudioManager> audio_manager(AudioManager::Create()); | 346 WaitForReads(); |
234 if (!audio_manager->HasAudioOutputDevices()) | 347 DivertWhilePlaying(); |
235 return; | 348 WaitForPlay(); |
236 | 349 ReadDivertedAudioData(); |
237 MockAudioOutputControllerEventHandler event_handler; | 350 RevertWhilePlaying(); |
238 base::WaitableEvent event(false, false); | 351 WaitForPlay(); |
239 EXPECT_CALL(event_handler, OnCreated(NotNull())) | 352 WaitForReads(); |
240 .WillOnce(InvokeWithoutArgs(&event, &base::WaitableEvent::Signal)); | 353 Close(); |
241 | 354 } |
242 // OnPlaying() will be called once normally and once after being recreated. | 355 |
243 base::WaitableEvent play_event(false, false); | 356 TEST_F(AudioOutputControllerTest, DivertPlayPausePlayRevertClose) { |
244 EXPECT_CALL(event_handler, OnPlaying(NotNull())) | 357 Create(kSamplesPerPacket); |
245 .Times(2) | 358 WaitForCreate(); |
246 .WillRepeatedly(InvokeWithoutArgs( | 359 DivertWillEventuallyBePlaying(); |
247 &play_event, &base::WaitableEvent::Signal)); | 360 Play(); |
248 | 361 WaitForPlay(); |
249 // OnPaused() should not be called during the state change event. | 362 ReadDivertedAudioData(); |
250 EXPECT_CALL(event_handler, OnPaused(NotNull())) | 363 Pause(); |
251 .Times(0); | 364 WaitForPause(); |
252 | 365 Play(); |
253 MockAudioOutputControllerSyncReader sync_reader; | 366 WaitForPlay(); |
254 EXPECT_CALL(sync_reader, UpdatePendingBytes(_)) | 367 ReadDivertedAudioData(); |
255 .Times(AtLeast(1)); | 368 RevertWhilePlaying(); |
256 EXPECT_CALL(sync_reader, Read(_, _)) | 369 WaitForPlay(); |
257 .WillRepeatedly(DoAll(ClearBuffer(), SignalEvent(&event), Return(4))); | 370 WaitForReads(); |
258 EXPECT_CALL(sync_reader, DataReady()) | 371 Close(); |
259 .WillRepeatedly(Return(true)); | 372 } |
260 EXPECT_CALL(sync_reader, Close()); | 373 |
261 | 374 TEST_F(AudioOutputControllerTest, DivertRevertClose) { |
262 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, | 375 Create(kSamplesPerPacket); |
263 kSampleRate, kBitsPerSample, kSamplesPerPacket); | 376 WaitForCreate(); |
264 scoped_refptr<AudioOutputController> controller = | 377 DivertNeverPlaying(); |
265 AudioOutputController::Create( | 378 RevertWasNotPlaying(); |
266 audio_manager.get(), &event_handler, params, &sync_reader); | 379 Close(); |
267 ASSERT_TRUE(controller.get()); | |
268 | |
269 // Wait for OnCreated() to be called. | |
270 event.Wait(); | |
271 | |
272 ASSERT_FALSE(play_event.IsSignaled()); | |
273 controller->Play(); | |
274 play_event.Wait(); | |
275 | |
276 // Force a state change and wait for the stream to come back to playing state. | |
277 play_event.Reset(); | |
278 audio_manager->GetMessageLoop()->PostTask(FROM_HERE, | |
279 base::Bind(&AudioOutputController::OnDeviceChange, controller)); | |
280 play_event.Wait(); | |
281 | |
282 // Now stop the controller. | |
283 CloseAudioController(controller); | |
284 } | 380 } |
285 | 381 |
286 } // namespace media | 382 } // namespace media |
OLD | NEW |