OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "chromeos/audio/cras_audio_handler.h" | 5 #include "chromeos/audio/cras_audio_handler.h" |
6 | 6 |
| 7 #include "base/bind.h" |
7 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
8 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" |
| 12 #include "base/thread_task_runner_handle.h" |
10 #include "base/values.h" | 13 #include "base/values.h" |
11 #include "chromeos/audio/audio_devices_pref_handler_stub.h" | 14 #include "chromeos/audio/audio_devices_pref_handler_stub.h" |
12 #include "chromeos/dbus/audio_node.h" | 15 #include "chromeos/dbus/audio_node.h" |
13 #include "chromeos/dbus/dbus_thread_manager.h" | 16 #include "chromeos/dbus/dbus_thread_manager.h" |
14 #include "chromeos/dbus/fake_cras_audio_client.h" | 17 #include "chromeos/dbus/fake_cras_audio_client.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
16 | 19 |
17 namespace chromeos { | 20 namespace chromeos { |
18 | 21 |
19 const uint64 kInternalSpeakerId = 10001; | 22 const uint64 kInternalSpeakerId = 10001; |
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 int num_active_nodes = 0; | 339 int num_active_nodes = 0; |
337 AudioDeviceList audio_devices; | 340 AudioDeviceList audio_devices; |
338 cras_audio_handler_->GetAudioDevices(&audio_devices); | 341 cras_audio_handler_->GetAudioDevices(&audio_devices); |
339 for (size_t i = 0; i < audio_devices.size(); ++i) { | 342 for (size_t i = 0; i < audio_devices.size(); ++i) { |
340 if (audio_devices[i].active) | 343 if (audio_devices[i].active) |
341 ++num_active_nodes; | 344 ++num_active_nodes; |
342 } | 345 } |
343 return num_active_nodes; | 346 return num_active_nodes; |
344 } | 347 } |
345 | 348 |
| 349 void SetActiveHDMIRediscover() { |
| 350 cras_audio_handler_->SetActiveHDMIOutoutRediscoveringIfNecessary(true); |
| 351 } |
| 352 |
| 353 void SetHDMIRediscoverGracePeriodDuration(int duration_in_ms) { |
| 354 cras_audio_handler_->SetHDMIRediscoverGracePeriodForTesting(duration_in_ms); |
| 355 } |
| 356 |
| 357 bool IsDuringHDMIRediscoverGracePeriod() { |
| 358 return cras_audio_handler_->hdmi_rediscovering(); |
| 359 } |
| 360 |
346 protected: | 361 protected: |
347 base::MessageLoopForUI message_loop_; | 362 base::MessageLoopForUI message_loop_; |
348 CrasAudioHandler* cras_audio_handler_; // Not owned. | 363 CrasAudioHandler* cras_audio_handler_; // Not owned. |
349 FakeCrasAudioClient* fake_cras_audio_client_; // Not owned. | 364 FakeCrasAudioClient* fake_cras_audio_client_; // Not owned. |
350 scoped_ptr<TestObserver> test_observer_; | 365 scoped_ptr<TestObserver> test_observer_; |
351 scoped_refptr<AudioDevicesPrefHandlerStub> audio_pref_handler_; | 366 scoped_refptr<AudioDevicesPrefHandlerStub> audio_pref_handler_; |
352 | 367 |
353 private: | 368 private: |
354 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandlerTest); | 369 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandlerTest); |
355 }; | 370 }; |
356 | 371 |
| 372 class HDMIRediscoverWaiter { |
| 373 public: |
| 374 HDMIRediscoverWaiter(CrasAudioHandlerTest* cras_audio_handler_test, |
| 375 int grace_period_duration_in_ms) |
| 376 : cras_audio_handler_test_(cras_audio_handler_test), |
| 377 grace_period_duration_in_ms_(grace_period_duration_in_ms) {} |
| 378 |
| 379 void WaitUntilTimeOut(int wait_duration_in_ms) { |
| 380 base::RunLoop run_loop; |
| 381 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 382 FROM_HERE, run_loop.QuitClosure(), |
| 383 base::TimeDelta::FromMilliseconds(wait_duration_in_ms)); |
| 384 run_loop.Run(); |
| 385 } |
| 386 |
| 387 void CheckHDMIRediscoverGracePeriodEnd(const base::Closure& quit_loop_func) { |
| 388 if (!cras_audio_handler_test_->IsDuringHDMIRediscoverGracePeriod()) { |
| 389 quit_loop_func.Run(); |
| 390 return; |
| 391 } |
| 392 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 393 FROM_HERE, |
| 394 base::Bind(&HDMIRediscoverWaiter::CheckHDMIRediscoverGracePeriodEnd, |
| 395 base::Unretained(this), quit_loop_func), |
| 396 base::TimeDelta::FromMilliseconds(grace_period_duration_in_ms_ / 4)); |
| 397 } |
| 398 |
| 399 void WaitUntilHDMIRediscoverGracePeriodEnd() { |
| 400 base::RunLoop run_loop; |
| 401 CheckHDMIRediscoverGracePeriodEnd(run_loop.QuitClosure()); |
| 402 run_loop.Run(); |
| 403 } |
| 404 |
| 405 private: |
| 406 CrasAudioHandlerTest* cras_audio_handler_test_; // not owned |
| 407 int grace_period_duration_in_ms_; |
| 408 |
| 409 DISALLOW_COPY_AND_ASSIGN(HDMIRediscoverWaiter); |
| 410 }; |
| 411 |
357 TEST_F(CrasAudioHandlerTest, InitializeWithOnlyDefaultAudioDevices) { | 412 TEST_F(CrasAudioHandlerTest, InitializeWithOnlyDefaultAudioDevices) { |
358 AudioNodeList audio_nodes; | 413 AudioNodeList audio_nodes; |
359 audio_nodes.push_back(kInternalSpeaker); | 414 audio_nodes.push_back(kInternalSpeaker); |
360 audio_nodes.push_back(kInternalMic); | 415 audio_nodes.push_back(kInternalMic); |
361 SetUpCrasAudioHandler(audio_nodes); | 416 SetUpCrasAudioHandler(audio_nodes); |
362 | 417 |
363 // Verify the audio devices size. | 418 // Verify the audio devices size. |
364 AudioDeviceList audio_devices; | 419 AudioDeviceList audio_devices; |
365 cras_audio_handler_->GetAudioDevices(&audio_devices); | 420 cras_audio_handler_->GetAudioDevices(&audio_devices); |
366 EXPECT_EQ(audio_nodes.size(), audio_devices.size()); | 421 EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
(...skipping 2119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2486 audio_nodes.push_back(kHDMIOutput); | 2541 audio_nodes.push_back(kHDMIOutput); |
2487 ChangeAudioNodes(audio_nodes); | 2542 ChangeAudioNodes(audio_nodes); |
2488 | 2543 |
2489 // Verify the headphone is set to active again. | 2544 // Verify the headphone is set to active again. |
2490 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode()); | 2545 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
2491 const AudioDevice* headphone_resumed = GetDeviceFromId(kHeadphone.id); | 2546 const AudioDevice* headphone_resumed = GetDeviceFromId(kHeadphone.id); |
2492 EXPECT_EQ(kHeadphone.id, headphone_resumed->id); | 2547 EXPECT_EQ(kHeadphone.id, headphone_resumed->id); |
2493 EXPECT_TRUE(headphone_resumed->active); | 2548 EXPECT_TRUE(headphone_resumed->active); |
2494 } | 2549 } |
2495 | 2550 |
| 2551 // This test HDMI output rediscovering case in crbug.com/503667. |
| 2552 TEST_F(CrasAudioHandlerTest, HDMIOutputRediscover) { |
| 2553 AudioNodeList audio_nodes; |
| 2554 audio_nodes.push_back(kInternalSpeaker); |
| 2555 audio_nodes.push_back(kHDMIOutput); |
| 2556 SetUpCrasAudioHandler(audio_nodes); |
| 2557 |
| 2558 // Verify the HDMI device has been selected as the active output, and audio |
| 2559 // output is not muted. |
| 2560 AudioDevice active_output; |
| 2561 EXPECT_TRUE( |
| 2562 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
| 2563 EXPECT_EQ(kHDMIOutput.id, active_output.id); |
| 2564 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2565 EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 2566 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted()); |
| 2567 |
| 2568 // Trigger HDMI rediscovering grace period, and remove the HDMI node. |
| 2569 const int grace_period_in_ms = 200; |
| 2570 SetHDMIRediscoverGracePeriodDuration(grace_period_in_ms); |
| 2571 SetActiveHDMIRediscover(); |
| 2572 AudioNodeList audio_nodes_lost_hdmi; |
| 2573 audio_nodes_lost_hdmi.push_back(kInternalSpeaker); |
| 2574 ChangeAudioNodes(audio_nodes_lost_hdmi); |
| 2575 |
| 2576 // Verify the active output is switched to internal speaker, it is not muted |
| 2577 // by preference, but the system output is muted during the grace period. |
| 2578 EXPECT_TRUE( |
| 2579 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
| 2580 EXPECT_EQ(kInternalSpeaker.id, active_output.id); |
| 2581 EXPECT_FALSE( |
| 2582 cras_audio_handler_->IsOutputMutedForDevice(kInternalSpeaker.id)); |
| 2583 EXPECT_TRUE(cras_audio_handler_->IsOutputMuted()); |
| 2584 |
| 2585 // Re-attach the HDMI device after a little delay. |
| 2586 HDMIRediscoverWaiter waiter(this, grace_period_in_ms); |
| 2587 waiter.WaitUntilTimeOut(grace_period_in_ms / 4); |
| 2588 ChangeAudioNodes(audio_nodes); |
| 2589 |
| 2590 // After HDMI re-discover grace period, verify HDMI output is selected as the |
| 2591 // active device and not muted. |
| 2592 waiter.WaitUntilHDMIRediscoverGracePeriodEnd(); |
| 2593 EXPECT_TRUE( |
| 2594 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
| 2595 EXPECT_EQ(kHDMIOutput.id, active_output.id); |
| 2596 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2597 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted()); |
| 2598 } |
| 2599 |
2496 } // namespace chromeos | 2600 } // namespace chromeos |
OLD | NEW |