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

Side by Side Diff: content/browser/renderer_host/media/audio_renderer_host_unittest.cc

Issue 14600025: Replace AudioSilenceDetector with an AudioPowerMonitor. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Use CancelableCallback instead of extra-task for close reply. Created 7 years, 5 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 | Annotate | Revision Log
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 "base/bind.h" 5 #include "base/bind.h"
6 #include "base/environment.h" 6 #include "base/environment.h"
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "base/process_util.h" 9 #include "base/process_util.h"
10 #include "base/sync_socket.h" 10 #include "base/sync_socket.h"
11 #include "content/browser/browser_thread_impl.h" 11 #include "content/browser/browser_thread_impl.h"
12 #include "content/browser/renderer_host/media/audio_input_device_manager.h" 12 #include "content/browser/renderer_host/media/audio_input_device_manager.h"
13 #include "content/browser/renderer_host/media/audio_mirroring_manager.h" 13 #include "content/browser/renderer_host/media/audio_mirroring_manager.h"
14 #include "content/browser/renderer_host/media/audio_renderer_host.h" 14 #include "content/browser/renderer_host/media/audio_renderer_host.h"
15 #include "content/browser/renderer_host/media/media_stream_manager.h" 15 #include "content/browser/renderer_host/media/media_stream_manager.h"
16 #include "content/browser/renderer_host/media/mock_media_observer.h" 16 #include "content/browser/renderer_host/media/mock_media_observer.h"
17 #include "content/common/media/audio_messages.h" 17 #include "content/common/media/audio_messages.h"
18 #include "content/common/media/media_stream_options.h" 18 #include "content/common/media/media_stream_options.h"
19 #include "ipc/ipc_message_utils.h" 19 #include "ipc/ipc_message_utils.h"
20 #include "media/audio/audio_manager.h" 20 #include "media/audio/audio_manager.h"
21 #include "media/audio/audio_manager_base.h" 21 #include "media/audio/audio_manager_base.h"
22 #include "media/audio/fake_audio_output_stream.h" 22 #include "media/audio/fake_audio_output_stream.h"
23 #include "net/url_request/url_request_context.h" 23 #include "net/url_request/url_request_context.h"
24 #include "testing/gmock/include/gmock/gmock.h" 24 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
26 26
27 using ::testing::_; 27 using ::testing::_;
28 using ::testing::AtLeast; 28 using ::testing::Assign;
29 using ::testing::DoAll; 29 using ::testing::DoAll;
30 using ::testing::InSequence;
31 using ::testing::NotNull; 30 using ::testing::NotNull;
32 using ::testing::Return;
33 using ::testing::SaveArg;
34 using ::testing::SetArgumentPointee;
35 31
36 namespace content { 32 namespace content {
37 33
38 static const int kRenderProcessId = 1; 34 static const int kRenderProcessId = 1;
39 static const int kRenderViewId = 4; 35 static const int kRenderViewId = 4;
40 static const int kStreamId = 50; 36 static const int kStreamId = 50;
41 37
42 class MockAudioMirroringManager : public AudioMirroringManager { 38 class MockAudioMirroringManager : public AudioMirroringManager {
43 public: 39 public:
44 MockAudioMirroringManager() {} 40 MockAudioMirroringManager() {}
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 152
157 DISALLOW_COPY_AND_ASSIGN(MockAudioRendererHost); 153 DISALLOW_COPY_AND_ASSIGN(MockAudioRendererHost);
158 }; 154 };
159 155
160 ACTION_P(QuitMessageLoop, message_loop) { 156 ACTION_P(QuitMessageLoop, message_loop) {
161 message_loop->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); 157 message_loop->PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
162 } 158 }
163 159
164 class AudioRendererHostTest : public testing::Test { 160 class AudioRendererHostTest : public testing::Test {
165 public: 161 public:
166 AudioRendererHostTest() {} 162 AudioRendererHostTest() : is_stream_active_(false) {}
167 163
168 protected: 164 protected:
169 virtual void SetUp() { 165 virtual void SetUp() {
170 // Create a message loop so AudioRendererHost can use it. 166 // Create a message loop so AudioRendererHost can use it.
171 message_loop_.reset(new base::MessageLoop(base::MessageLoop::TYPE_IO)); 167 message_loop_.reset(new base::MessageLoop(base::MessageLoop::TYPE_IO));
172 168
173 // Claim to be on both the UI and IO threads to pass all the DCHECKS. 169 // Claim to be on both the UI and IO threads to pass all the DCHECKS.
174 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO, 170 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO,
175 message_loop_.get())); 171 message_loop_.get()));
176 ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI, 172 ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI,
(...skipping 15 matching lines...) Expand all
192 host_->OnChannelClosing(); 188 host_->OnChannelClosing();
193 189
194 // Release the reference to the mock object. The object will be destructed 190 // Release the reference to the mock object. The object will be destructed
195 // on message_loop_. 191 // on message_loop_.
196 host_ = NULL; 192 host_ = NULL;
197 193
198 // We need to continue running message_loop_ to complete all destructions. 194 // We need to continue running message_loop_ to complete all destructions.
199 SyncWithAudioThread(); 195 SyncWithAudioThread();
200 audio_manager_.reset(); 196 audio_manager_.reset();
201 197
198 // Make sure the stream has been deleted before continuing.
199 while (is_stream_active_)
200 message_loop_->Run();
201
202 io_thread_.reset(); 202 io_thread_.reset();
203 ui_thread_.reset(); 203 ui_thread_.reset();
204 204
205 // Delete the IO message loop. This will cause the MediaStreamManager to be 205 // Delete the IO message loop. This will cause the MediaStreamManager to be
206 // notified so it will stop its device thread and device managers. 206 // notified so it will stop its device thread and device managers.
207 message_loop_.reset(); 207 message_loop_.reset();
208 } 208 }
209 209
210 void Create() { 210 void Create(bool unified_stream) {
211 EXPECT_CALL(*observer_, 211 EXPECT_CALL(*observer_,
212 OnSetAudioStreamStatus(_, kStreamId, "created")); 212 OnSetAudioStreamStatus(_, kStreamId, "created"));
213 EXPECT_CALL(*host_.get(), OnStreamCreated(kStreamId, _)) 213 EXPECT_CALL(*host_.get(), OnStreamCreated(kStreamId, _))
214 .WillOnce(QuitMessageLoop(message_loop_.get())); 214 .WillOnce(DoAll(Assign(&is_stream_active_, true),
215 QuitMessageLoop(message_loop_.get())));
215 EXPECT_CALL(mirroring_manager_, 216 EXPECT_CALL(mirroring_manager_,
216 AddDiverter(kRenderProcessId, kRenderViewId, NotNull())) 217 AddDiverter(kRenderProcessId, kRenderViewId, NotNull()))
217 .RetiresOnSaturation(); 218 .RetiresOnSaturation();
218 219
219 // Send a create stream message to the audio output stream and wait until 220 // Send a create stream message to the audio output stream and wait until
220 // we receive the created message. 221 // we receive the created message.
221 host_->OnCreateStream(kStreamId, 222 int session_id;
222 kRenderViewId, 223 media::AudioParameters params;
223 0, 224 if (unified_stream) {
224 media::AudioParameters( 225 // Use AudioInputDeviceManager::kFakeOpenSessionId as the session id to
225 media::AudioParameters::AUDIO_FAKE, 226 // pass the permission check.
226 media::CHANNEL_LAYOUT_STEREO, 227 session_id = AudioInputDeviceManager::kFakeOpenSessionId;
227 media::AudioParameters::kAudioCDSampleRate, 16, 228 params = media::AudioParameters(
228 media::AudioParameters::kAudioCDSampleRate / 10)); 229 media::AudioParameters::AUDIO_FAKE,
230 media::CHANNEL_LAYOUT_STEREO,
231 2,
232 media::AudioParameters::kAudioCDSampleRate, 16,
233 media::AudioParameters::kAudioCDSampleRate / 10);
234 } else {
235 session_id = 0;
236 params = media::AudioParameters(
237 media::AudioParameters::AUDIO_FAKE,
238 media::CHANNEL_LAYOUT_STEREO,
239 media::AudioParameters::kAudioCDSampleRate, 16,
240 media::AudioParameters::kAudioCDSampleRate / 10);
241 }
242 host_->OnCreateStream(kStreamId, kRenderViewId, session_id, params);
229 message_loop_->Run(); 243 message_loop_->Run();
230 244
231 // At some point in the future, a corresponding RemoveDiverter() call must 245 // At some point in the future, a corresponding RemoveDiverter() call must
232 // be made.
233 EXPECT_CALL(mirroring_manager_,
234 RemoveDiverter(kRenderProcessId, kRenderViewId, NotNull()))
235 .RetiresOnSaturation();
236
237 // All created streams should ultimately be closed.
238 EXPECT_CALL(*observer_,
239 OnSetAudioStreamStatus(_, kStreamId, "closed"));
240
241 // Expect the audio stream will be deleted at some later point.
242 EXPECT_CALL(*observer_, OnDeleteAudioStream(_, kStreamId));
243 }
244
245 void CreateUnifiedStream() {
246 EXPECT_CALL(*observer_,
247 OnSetAudioStreamStatus(_, kStreamId, "created"));
248 EXPECT_CALL(*host_.get(), OnStreamCreated(kStreamId, _))
249 .WillOnce(QuitMessageLoop(message_loop_.get()));
250 EXPECT_CALL(mirroring_manager_,
251 AddDiverter(kRenderProcessId, kRenderViewId, NotNull()))
252 .RetiresOnSaturation();
253 // Send a create stream message to the audio output stream and wait until
254 // we receive the created message.
255 // Use AudioInputDeviceManager::kFakeOpenSessionId as the session id to
256 // pass the permission check.
257 host_->OnCreateStream(kStreamId,
258 kRenderViewId,
259 AudioInputDeviceManager::kFakeOpenSessionId,
260 media::AudioParameters(
261 media::AudioParameters::AUDIO_FAKE,
262 media::CHANNEL_LAYOUT_STEREO,
263 2,
264 media::AudioParameters::kAudioCDSampleRate, 16,
265 media::AudioParameters::kAudioCDSampleRate / 10));
266 message_loop_->Run();
267
268 // At some point in the future, a corresponding RemoveDiverter() call must
269 // be made. 246 // be made.
270 EXPECT_CALL(mirroring_manager_, 247 EXPECT_CALL(mirroring_manager_,
271 RemoveDiverter(kRenderProcessId, kRenderViewId, NotNull())) 248 RemoveDiverter(kRenderProcessId, kRenderViewId, NotNull()))
272 .RetiresOnSaturation(); 249 .RetiresOnSaturation();
273 250
274 // All created streams should ultimately be closed. 251 // All created streams should ultimately be closed.
275 EXPECT_CALL(*observer_, 252 EXPECT_CALL(*observer_,
276 OnSetAudioStreamStatus(_, kStreamId, "closed")); 253 OnSetAudioStreamStatus(_, kStreamId, "closed"));
277 254
278 // Expect the audio stream will be deleted at some later point. 255 // Expect the audio stream will be deleted at some later point.
279 EXPECT_CALL(*observer_, OnDeleteAudioStream(_, kStreamId)); 256 EXPECT_CALL(*observer_, OnDeleteAudioStream(_, kStreamId))
257 .WillOnce(DoAll(Assign(&is_stream_active_, false),
258 QuitMessageLoop(message_loop_.get())));
280 } 259 }
281 260
282 void Close() { 261 void Close() {
283 // Send a message to AudioRendererHost to tell it we want to close the 262 // Send a message to AudioRendererHost to tell it we want to close the
284 // stream. 263 // stream.
285 host_->OnCloseStream(kStreamId); 264 host_->OnCloseStream(kStreamId);
286 message_loop_->RunUntilIdle(); 265 if (is_stream_active_)
266 message_loop_->Run();
267 else
268 message_loop_->RunUntilIdle();
287 } 269 }
288 270
289 void Play() { 271 void Play() {
290 EXPECT_CALL(*observer_, 272 EXPECT_CALL(*observer_,
291 OnSetAudioStreamPlaying(_, kStreamId, true)); 273 OnSetAudioStreamPlaying(_, kStreamId, true));
292 EXPECT_CALL(*host_.get(), OnStreamPlaying(kStreamId)) 274 EXPECT_CALL(*host_.get(), OnStreamPlaying(kStreamId))
293 .WillOnce(QuitMessageLoop(message_loop_.get())); 275 .WillOnce(QuitMessageLoop(message_loop_.get()));
294 276
295 host_->OnPlayStream(kStreamId); 277 host_->OnPlayStream(kStreamId);
296 message_loop_->Run(); 278 message_loop_->Run();
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 private: 342 private:
361 scoped_ptr<MockMediaInternals> observer_; 343 scoped_ptr<MockMediaInternals> observer_;
362 MockAudioMirroringManager mirroring_manager_; 344 MockAudioMirroringManager mirroring_manager_;
363 scoped_refptr<MockAudioRendererHost> host_; 345 scoped_refptr<MockAudioRendererHost> host_;
364 scoped_ptr<base::MessageLoop> message_loop_; 346 scoped_ptr<base::MessageLoop> message_loop_;
365 scoped_ptr<BrowserThreadImpl> io_thread_; 347 scoped_ptr<BrowserThreadImpl> io_thread_;
366 scoped_ptr<BrowserThreadImpl> ui_thread_; 348 scoped_ptr<BrowserThreadImpl> ui_thread_;
367 scoped_ptr<media::AudioManager> audio_manager_; 349 scoped_ptr<media::AudioManager> audio_manager_;
368 scoped_ptr<MediaStreamManager> media_stream_manager_; 350 scoped_ptr<MediaStreamManager> media_stream_manager_;
369 351
352 bool is_stream_active_;
353
370 DISALLOW_COPY_AND_ASSIGN(AudioRendererHostTest); 354 DISALLOW_COPY_AND_ASSIGN(AudioRendererHostTest);
371 }; 355 };
372 356
373 TEST_F(AudioRendererHostTest, CreateAndClose) { 357 TEST_F(AudioRendererHostTest, CreateAndClose) {
374 Create(); 358 Create(false);
375 Close(); 359 Close();
376 } 360 }
377 361
378 // Simulate the case where a stream is not properly closed. 362 // Simulate the case where a stream is not properly closed.
379 TEST_F(AudioRendererHostTest, CreateAndShutdown) { 363 TEST_F(AudioRendererHostTest, CreateAndShutdown) {
380 Create(); 364 Create(false);
381 } 365 }
382 366
383 TEST_F(AudioRendererHostTest, CreatePlayAndClose) { 367 TEST_F(AudioRendererHostTest, CreatePlayAndClose) {
384 Create(); 368 Create(false);
385 Play(); 369 Play();
386 Close(); 370 Close();
387 } 371 }
388 372
389 TEST_F(AudioRendererHostTest, CreatePlayPauseAndClose) { 373 TEST_F(AudioRendererHostTest, CreatePlayPauseAndClose) {
390 Create(); 374 Create(false);
391 Play(); 375 Play();
392 Pause(); 376 Pause();
393 Close(); 377 Close();
394 } 378 }
395 379
396 TEST_F(AudioRendererHostTest, SetVolume) { 380 TEST_F(AudioRendererHostTest, SetVolume) {
397 Create(); 381 Create(false);
398 SetVolume(0.5); 382 SetVolume(0.5);
399 Play(); 383 Play();
400 Pause(); 384 Pause();
401 Close(); 385 Close();
402 } 386 }
403 387
404 // Simulate the case where a stream is not properly closed. 388 // Simulate the case where a stream is not properly closed.
405 TEST_F(AudioRendererHostTest, CreatePlayAndShutdown) { 389 TEST_F(AudioRendererHostTest, CreatePlayAndShutdown) {
406 Create(); 390 Create(false);
407 Play(); 391 Play();
408 } 392 }
409 393
410 // Simulate the case where a stream is not properly closed. 394 // Simulate the case where a stream is not properly closed.
411 TEST_F(AudioRendererHostTest, CreatePlayPauseAndShutdown) { 395 TEST_F(AudioRendererHostTest, CreatePlayPauseAndShutdown) {
412 Create(); 396 Create(false);
413 Play(); 397 Play();
414 Pause(); 398 Pause();
415 } 399 }
416 400
417 TEST_F(AudioRendererHostTest, SimulateError) { 401 TEST_F(AudioRendererHostTest, SimulateError) {
418 Create(); 402 Create(false);
419 Play(); 403 Play();
420 SimulateError(); 404 SimulateError();
421 } 405 }
422 406
423 // Simulate the case when an error is generated on the browser process, 407 // Simulate the case when an error is generated on the browser process,
424 // the audio device is closed but the render process try to close the 408 // the audio device is closed but the render process try to close the
425 // audio stream again. 409 // audio stream again.
426 TEST_F(AudioRendererHostTest, SimulateErrorAndClose) { 410 TEST_F(AudioRendererHostTest, SimulateErrorAndClose) {
427 Create(); 411 Create(false);
428 Play(); 412 Play();
429 SimulateError(); 413 SimulateError();
430 Close(); 414 Close();
431 } 415 }
432 416
433 TEST_F(AudioRendererHostTest, CreateUnifiedStreamAndClose) { 417 TEST_F(AudioRendererHostTest, CreateUnifiedStreamAndClose) {
434 CreateUnifiedStream(); 418 Create(true);
435 Close(); 419 Close();
436 } 420 }
437 421
438 // TODO(hclam): Add tests for data conversation in low latency mode. 422 // TODO(hclam): Add tests for data conversation in low latency mode.
439 423
440 } // namespace content 424 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/renderer_host/media/audio_renderer_host.cc ('k') | content/browser/renderer_host/media/mock_media_observer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698