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
3 // found in the LICENSE file.
5 #include "chromeos/audio/cras_audio_handler.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "base/values.h"
14 #include "chromeos/audio/audio_devices_pref_handler_stub.h"
15 #include "chromeos/dbus/audio_node.h"
16 #include "chromeos/dbus/dbus_thread_manager.h"
17 #include "chromeos/dbus/fake_cras_audio_client.h"
18 #include "testing/gtest/include/gtest/gtest.h"
23 const uint64 kInternalSpeakerId
= 10001;
24 const uint64 kHeadphoneId
= 10002;
25 const uint64 kInternalMicId
= 10003;
26 const uint64 kUSBMicId
= 10004;
27 const uint64 kBluetoothHeadsetId
= 10005;
28 const uint64 kHDMIOutputId
= 10006;
29 const uint64 kUSBHeadphoneId1
= 10007;
30 const uint64 kUSBHeadphoneId2
= 10008;
31 const uint64 kMicJackId
= 10009;
32 const uint64 kKeyboardMicId
= 10010;
33 const uint64 kOtherTypeOutputId
= 90001;
34 const uint64 kOtherTypeInputId
= 90002;
35 const uint64 kUSBJabraSpeakerOutputId1
= 90003;
36 const uint64 kUSBJabraSpeakerOutputId2
= 90004;
37 const uint64 kUSBJabraSpeakerInputId1
= 90005;
38 const uint64 kUSBJabraSpeakerInputId2
= 90006;
39 const uint64 kUSBCameraInputId
= 90007;
41 const AudioNode
kInternalSpeaker(
51 const AudioNode
kHeadphone(
61 const AudioNode
kInternalMic(
71 const AudioNode
kMicJack(
81 const AudioNode
kUSBMic(
91 const AudioNode
kKeyboardMic(
101 const AudioNode
kOtherTypeOutput(
106 "Other Type Output Device",
111 const AudioNode
kOtherTypeInput(
116 "Other Type Input Device",
121 const AudioNode
kBluetoothHeadset(false,
125 "Bluetooth Headset 1",
129 const AudioNode
kHDMIOutput(false,
137 const AudioNode
kUSBHeadphone1(false,
145 const AudioNode
kUSBHeadphone2(false,
153 const AudioNode
kUSBJabraSpeakerOutput1(false,
154 kUSBJabraSpeakerOutputId1
,
161 const AudioNode
kUSBJabraSpeakerOutput2(false,
162 kUSBJabraSpeakerOutputId2
,
169 const AudioNode
kUSBJabraSpeakerInput1(true,
170 kUSBJabraSpeakerInputId1
,
177 const AudioNode
kUSBJabraSpeakerInput2(true,
178 kUSBJabraSpeakerInputId2
,
185 const AudioNode
kUSBCameraInput(true,
193 class TestObserver
: public chromeos::CrasAudioHandler::AudioObserver
{
195 TestObserver() : active_output_node_changed_count_(0),
196 active_input_node_changed_count_(0),
197 audio_nodes_changed_count_(0),
198 output_mute_changed_count_(0),
199 input_mute_changed_count_(0),
200 output_volume_changed_count_(0),
201 input_gain_changed_count_(0) {
204 int active_output_node_changed_count() const {
205 return active_output_node_changed_count_
;
208 void reset_active_output_node_changed_count() {
209 active_output_node_changed_count_
= 0;
212 int active_input_node_changed_count() const {
213 return active_input_node_changed_count_
;
216 void reset_active_input_node_changed_count() {
217 active_input_node_changed_count_
= 0;
220 int audio_nodes_changed_count() const {
221 return audio_nodes_changed_count_
;
224 int output_mute_changed_count() const {
225 return output_mute_changed_count_
;
228 int input_mute_changed_count() const {
229 return input_mute_changed_count_
;
232 int output_volume_changed_count() const {
233 return output_volume_changed_count_
;
236 int input_gain_changed_count() const {
237 return input_gain_changed_count_
;
240 ~TestObserver() override
{}
243 // chromeos::CrasAudioHandler::AudioObserver overrides.
244 void OnActiveOutputNodeChanged() override
{
245 ++active_output_node_changed_count_
;
248 void OnActiveInputNodeChanged() override
{
249 ++active_input_node_changed_count_
;
252 void OnAudioNodesChanged() override
{ ++audio_nodes_changed_count_
; }
254 void OnOutputMuteChanged(bool /* mute_on */) override
{
255 ++output_mute_changed_count_
;
258 void OnInputMuteChanged(bool /* mute_on */) override
{
259 ++input_mute_changed_count_
;
262 void OnOutputNodeVolumeChanged(uint64
/* node_id */,
263 int /* volume */) override
{
264 ++output_volume_changed_count_
;
267 void OnInputNodeGainChanged(uint64
/* node_id */, int /* gain */) override
{
268 ++input_gain_changed_count_
;
272 int active_output_node_changed_count_
;
273 int active_input_node_changed_count_
;
274 int audio_nodes_changed_count_
;
275 int output_mute_changed_count_
;
276 int input_mute_changed_count_
;
277 int output_volume_changed_count_
;
278 int input_gain_changed_count_
;
280 DISALLOW_COPY_AND_ASSIGN(TestObserver
);
285 class CrasAudioHandlerTest
: public testing::Test
{
287 CrasAudioHandlerTest() : cras_audio_handler_(NULL
),
288 fake_cras_audio_client_(NULL
) {
290 ~CrasAudioHandlerTest() override
{}
292 void SetUp() override
{}
294 void TearDown() override
{
295 cras_audio_handler_
->RemoveAudioObserver(test_observer_
.get());
296 test_observer_
.reset();
297 CrasAudioHandler::Shutdown();
298 audio_pref_handler_
= NULL
;
299 DBusThreadManager::Shutdown();
302 void SetUpCrasAudioHandler(const AudioNodeList
& audio_nodes
) {
303 DBusThreadManager::Initialize();
304 fake_cras_audio_client_
= static_cast<FakeCrasAudioClient
*>(
305 DBusThreadManager::Get()->GetCrasAudioClient());
306 fake_cras_audio_client_
->SetAudioNodesForTesting(audio_nodes
);
307 audio_pref_handler_
= new AudioDevicesPrefHandlerStub();
308 CrasAudioHandler::Initialize(audio_pref_handler_
);
309 cras_audio_handler_
= CrasAudioHandler::Get();
310 test_observer_
.reset(new TestObserver
);
311 cras_audio_handler_
->AddAudioObserver(test_observer_
.get());
312 message_loop_
.RunUntilIdle();
315 void SetUpCrasAudioHandlerWithPrimaryActiveNode(
316 const AudioNodeList
& audio_nodes
,
317 const AudioNode
& primary_active_node
) {
318 DBusThreadManager::Initialize();
319 fake_cras_audio_client_
= static_cast<FakeCrasAudioClient
*>(
320 DBusThreadManager::Get()->GetCrasAudioClient());
321 fake_cras_audio_client_
->SetAudioNodesForTesting(audio_nodes
);
322 fake_cras_audio_client_
->SetActiveOutputNode(primary_active_node
.id
),
323 audio_pref_handler_
= new AudioDevicesPrefHandlerStub();
324 CrasAudioHandler::Initialize(audio_pref_handler_
);
325 cras_audio_handler_
= CrasAudioHandler::Get();
326 test_observer_
.reset(new TestObserver
);
327 cras_audio_handler_
->AddAudioObserver(test_observer_
.get());
328 message_loop_
.RunUntilIdle();
331 void ChangeAudioNodes(const AudioNodeList
& audio_nodes
) {
332 fake_cras_audio_client_
->SetAudioNodesAndNotifyObserversForTesting(
334 message_loop_
.RunUntilIdle();
337 const AudioDevice
* GetDeviceFromId(uint64 id
) {
338 return cras_audio_handler_
->GetDeviceFromId(id
);
341 int GetActiveDeviceCount() const {
342 int num_active_nodes
= 0;
343 AudioDeviceList audio_devices
;
344 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
345 for (size_t i
= 0; i
< audio_devices
.size(); ++i
) {
346 if (audio_devices
[i
].active
)
349 return num_active_nodes
;
352 void SetActiveHDMIRediscover() {
353 cras_audio_handler_
->SetActiveHDMIOutoutRediscoveringIfNecessary(true);
356 void SetHDMIRediscoverGracePeriodDuration(int duration_in_ms
) {
357 cras_audio_handler_
->SetHDMIRediscoverGracePeriodForTesting(duration_in_ms
);
360 bool IsDuringHDMIRediscoverGracePeriod() {
361 return cras_audio_handler_
->hdmi_rediscovering();
365 base::MessageLoopForUI message_loop_
;
366 CrasAudioHandler
* cras_audio_handler_
; // Not owned.
367 FakeCrasAudioClient
* fake_cras_audio_client_
; // Not owned.
368 scoped_ptr
<TestObserver
> test_observer_
;
369 scoped_refptr
<AudioDevicesPrefHandlerStub
> audio_pref_handler_
;
372 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandlerTest
);
375 class HDMIRediscoverWaiter
{
377 HDMIRediscoverWaiter(CrasAudioHandlerTest
* cras_audio_handler_test
,
378 int grace_period_duration_in_ms
)
379 : cras_audio_handler_test_(cras_audio_handler_test
),
380 grace_period_duration_in_ms_(grace_period_duration_in_ms
) {}
382 void WaitUntilTimeOut(int wait_duration_in_ms
) {
383 base::RunLoop run_loop
;
384 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
385 FROM_HERE
, run_loop
.QuitClosure(),
386 base::TimeDelta::FromMilliseconds(wait_duration_in_ms
));
390 void CheckHDMIRediscoverGracePeriodEnd(const base::Closure
& quit_loop_func
) {
391 if (!cras_audio_handler_test_
->IsDuringHDMIRediscoverGracePeriod()) {
392 quit_loop_func
.Run();
395 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
397 base::Bind(&HDMIRediscoverWaiter::CheckHDMIRediscoverGracePeriodEnd
,
398 base::Unretained(this), quit_loop_func
),
399 base::TimeDelta::FromMilliseconds(grace_period_duration_in_ms_
/ 4));
402 void WaitUntilHDMIRediscoverGracePeriodEnd() {
403 base::RunLoop run_loop
;
404 CheckHDMIRediscoverGracePeriodEnd(run_loop
.QuitClosure());
409 CrasAudioHandlerTest
* cras_audio_handler_test_
; // not owned
410 int grace_period_duration_in_ms_
;
412 DISALLOW_COPY_AND_ASSIGN(HDMIRediscoverWaiter
);
415 TEST_F(CrasAudioHandlerTest
, InitializeWithOnlyDefaultAudioDevices
) {
416 AudioNodeList audio_nodes
;
417 audio_nodes
.push_back(kInternalSpeaker
);
418 audio_nodes
.push_back(kInternalMic
);
419 SetUpCrasAudioHandler(audio_nodes
);
421 // Verify the audio devices size.
422 AudioDeviceList audio_devices
;
423 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
424 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
426 // Verify the internal speaker has been selected as the active output.
427 AudioDevice active_output
;
429 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
430 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
431 EXPECT_EQ(kInternalSpeaker
.id
,
432 cras_audio_handler_
->GetPrimaryActiveOutputNode());
433 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
435 // Ensure the internal microphone has been selected as the active input.
436 AudioDevice active_input
;
437 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
438 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
441 TEST_F(CrasAudioHandlerTest
, InitializeWithAlternativeAudioDevices
) {
442 AudioNodeList audio_nodes
;
443 audio_nodes
.push_back(kInternalSpeaker
);
444 audio_nodes
.push_back(kHeadphone
);
445 audio_nodes
.push_back(kInternalMic
);
446 audio_nodes
.push_back(kUSBMic
);
447 SetUpCrasAudioHandler(audio_nodes
);
449 // Verify the audio devices size.
450 AudioDeviceList audio_devices
;
451 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
452 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
454 // Verify the headphone has been selected as the active output.
455 AudioDevice active_output
;
457 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
458 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
459 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
460 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
462 // Ensure the USB microphone has been selected as the active input.
463 EXPECT_EQ(kUSBMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
464 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
467 TEST_F(CrasAudioHandlerTest
, InitializeWithKeyboardMic
) {
468 AudioNodeList audio_nodes
;
469 audio_nodes
.push_back(kInternalSpeaker
);
470 audio_nodes
.push_back(kInternalMic
);
471 audio_nodes
.push_back(kKeyboardMic
);
472 SetUpCrasAudioHandler(audio_nodes
);
474 // Verify the audio devices size.
475 AudioDeviceList audio_devices
;
476 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
477 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
478 EXPECT_TRUE(cras_audio_handler_
->HasKeyboardMic());
480 // Verify the internal speaker has been selected as the active output.
481 AudioDevice active_output
;
483 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
484 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
485 EXPECT_EQ(kInternalSpeaker
.id
,
486 cras_audio_handler_
->GetPrimaryActiveOutputNode());
487 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
489 // Ensure the internal microphone has been selected as the active input,
490 // not affected by keyboard mic.
491 AudioDevice active_input
;
492 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
493 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
494 const AudioDevice
* keyboard_mic
= GetDeviceFromId(kKeyboardMic
.id
);
495 EXPECT_FALSE(keyboard_mic
->active
);
498 TEST_F(CrasAudioHandlerTest
, SetKeyboardMicActive
) {
499 AudioNodeList audio_nodes
;
500 audio_nodes
.push_back(kInternalMic
);
501 audio_nodes
.push_back(kKeyboardMic
);
502 SetUpCrasAudioHandler(audio_nodes
);
504 // Verify the audio devices size.
505 AudioDeviceList audio_devices
;
506 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
507 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
508 EXPECT_TRUE(cras_audio_handler_
->HasKeyboardMic());
510 // Ensure the internal microphone has been selected as the active input,
511 // not affected by keyboard mic.
512 AudioDevice active_input
;
513 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
514 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
515 const AudioDevice
* keyboard_mic
= GetDeviceFromId(kKeyboardMic
.id
);
516 EXPECT_FALSE(keyboard_mic
->active
);
518 // Make keyboard mic active.
519 cras_audio_handler_
->SetKeyboardMicActive(true);
520 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
521 const AudioDevice
* active_keyboard_mic
= GetDeviceFromId(kKeyboardMic
.id
);
522 EXPECT_TRUE(active_keyboard_mic
->active
);
524 // Make keyboard mic inactive.
525 cras_audio_handler_
->SetKeyboardMicActive(false);
526 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
527 const AudioDevice
* inactive_keyboard_mic
= GetDeviceFromId(kKeyboardMic
.id
);
528 EXPECT_FALSE(inactive_keyboard_mic
->active
);
531 TEST_F(CrasAudioHandlerTest
, SwitchActiveOutputDevice
) {
532 AudioNodeList audio_nodes
;
533 audio_nodes
.push_back(kInternalSpeaker
);
534 audio_nodes
.push_back(kHeadphone
);
535 SetUpCrasAudioHandler(audio_nodes
);
536 AudioDeviceList audio_devices
;
537 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
538 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
540 // Verify the initial active output device is headphone.
541 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
542 AudioDevice active_output
;
544 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
545 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
546 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
548 // Switch the active output to internal speaker.
549 AudioDevice
internal_speaker(kInternalSpeaker
);
550 cras_audio_handler_
->SwitchToDevice(internal_speaker
, true);
552 // Verify the active output is switched to internal speaker, and the
553 // ActiveOutputNodeChanged event is fired.
554 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
556 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
557 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
558 EXPECT_EQ(kInternalSpeaker
.id
,
559 cras_audio_handler_
->GetPrimaryActiveOutputNode());
562 TEST_F(CrasAudioHandlerTest
, SwitchActiveInputDevice
) {
563 AudioNodeList audio_nodes
;
564 audio_nodes
.push_back(kInternalMic
);
565 audio_nodes
.push_back(kUSBMic
);
566 SetUpCrasAudioHandler(audio_nodes
);
567 AudioDeviceList audio_devices
;
568 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
569 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
571 // Verify the initial active input device is USB mic.
572 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
573 EXPECT_EQ(kUSBMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
575 // Switch the active input to internal mic.
576 AudioDevice
internal_mic(kInternalMic
);
577 cras_audio_handler_
->SwitchToDevice(internal_mic
, true);
579 // Verify the active output is switched to internal speaker, and the active
580 // ActiveInputNodeChanged event is fired.
581 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
582 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
585 TEST_F(CrasAudioHandlerTest
, PlugHeadphone
) {
586 // Set up initial audio devices, only with internal speaker.
587 AudioNodeList audio_nodes
;
588 audio_nodes
.push_back(kInternalSpeaker
);
589 SetUpCrasAudioHandler(audio_nodes
);
590 const size_t init_nodes_size
= audio_nodes
.size();
592 // Verify the audio devices size.
593 AudioDeviceList audio_devices
;
594 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
595 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
596 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
598 // Verify the internal speaker has been selected as the active output.
599 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
600 AudioDevice active_output
;
602 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
603 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
604 EXPECT_EQ(kInternalSpeaker
.id
,
605 cras_audio_handler_
->GetPrimaryActiveOutputNode());
606 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
608 // Plug the headphone.
610 AudioNode
internal_speaker(kInternalSpeaker
);
611 internal_speaker
.active
= true;
612 audio_nodes
.push_back(internal_speaker
);
613 audio_nodes
.push_back(kHeadphone
);
614 ChangeAudioNodes(audio_nodes
);
616 // Verify the AudioNodesChanged event is fired and new audio device is added.
617 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
618 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
619 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
621 // Verify the active output device is switched to headphone and
622 // ActiveOutputChanged event is fired.
623 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
625 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
626 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
627 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
628 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
631 TEST_F(CrasAudioHandlerTest
, UnplugHeadphone
) {
632 // Set up initial audio devices, with internal speaker and headphone.
633 AudioNodeList audio_nodes
;
634 audio_nodes
.push_back(kInternalSpeaker
);
635 audio_nodes
.push_back(kHeadphone
);
636 SetUpCrasAudioHandler(audio_nodes
);
637 const size_t init_nodes_size
= audio_nodes
.size();
639 // Verify the audio devices size.
640 AudioDeviceList audio_devices
;
641 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
642 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
643 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
645 // Verify the headphone has been selected as the active output.
646 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
647 AudioDevice active_output
;
649 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
650 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
651 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
652 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
654 // Unplug the headphone.
656 audio_nodes
.push_back(kInternalSpeaker
);
657 ChangeAudioNodes(audio_nodes
);
659 // Verify the AudioNodesChanged event is fired and one audio device is
661 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
662 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
663 EXPECT_EQ(init_nodes_size
- 1, audio_devices
.size());
665 // Verify the active output device is switched to internal speaker and
666 // ActiveOutputChanged event is fired.
667 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
669 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
670 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
671 EXPECT_EQ(kInternalSpeaker
.id
,
672 cras_audio_handler_
->GetPrimaryActiveOutputNode());
673 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
676 TEST_F(CrasAudioHandlerTest
, InitializeWithBluetoothHeadset
) {
677 AudioNodeList audio_nodes
;
678 audio_nodes
.push_back(kInternalSpeaker
);
679 audio_nodes
.push_back(kBluetoothHeadset
);
680 SetUpCrasAudioHandler(audio_nodes
);
682 // Verify the audio devices size.
683 AudioDeviceList audio_devices
;
684 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
685 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
686 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
688 // Verify the bluetooth headset has been selected as the active output.
689 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
690 AudioDevice active_output
;
692 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
693 EXPECT_EQ(kBluetoothHeadset
.id
, active_output
.id
);
694 EXPECT_EQ(kBluetoothHeadset
.id
,
695 cras_audio_handler_
->GetPrimaryActiveOutputNode());
696 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
699 TEST_F(CrasAudioHandlerTest
, ConnectAndDisconnectBluetoothHeadset
) {
700 // Initialize with internal speaker and headphone.
701 AudioNodeList audio_nodes
;
702 audio_nodes
.push_back(kInternalSpeaker
);
703 audio_nodes
.push_back(kHeadphone
);
704 SetUpCrasAudioHandler(audio_nodes
);
705 const size_t init_nodes_size
= audio_nodes
.size();
707 // Verify the audio devices size.
708 AudioDeviceList audio_devices
;
709 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
710 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
711 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
713 // Verify the headphone is selected as the active output initially.
714 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
715 AudioDevice active_output
;
717 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
718 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
719 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
720 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
722 // Connect to bluetooth headset. Since it is plugged in later than
723 // headphone, active output should be switched to it.
725 audio_nodes
.push_back(kInternalSpeaker
);
726 AudioNode
headphone(kHeadphone
);
727 headphone
.plugged_time
= 80000000;
728 headphone
.active
= true;
729 audio_nodes
.push_back(headphone
);
730 AudioNode
bluetooth_headset(kBluetoothHeadset
);
731 bluetooth_headset
.plugged_time
= 90000000;
732 audio_nodes
.push_back(bluetooth_headset
);
733 ChangeAudioNodes(audio_nodes
);
735 // Verify the AudioNodesChanged event is fired and new audio device is added.
736 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
737 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
738 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
740 // Verify the active output device is switched to bluetooth headset, and
741 // ActiveOutputChanged event is fired.
742 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
744 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
745 EXPECT_EQ(kBluetoothHeadset
.id
, active_output
.id
);
746 EXPECT_EQ(kBluetoothHeadset
.id
,
747 cras_audio_handler_
->GetPrimaryActiveOutputNode());
748 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
750 // Disconnect bluetooth headset.
752 audio_nodes
.push_back(kInternalSpeaker
);
753 headphone
.active
= false;
754 audio_nodes
.push_back(headphone
);
755 ChangeAudioNodes(audio_nodes
);
757 // Verify the AudioNodesChanged event is fired and one audio device is
759 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
760 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
761 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
763 // Verify the active output device is switched to headphone, and
764 // ActiveOutputChanged event is fired.
765 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
767 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
768 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
769 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
770 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
773 TEST_F(CrasAudioHandlerTest
, InitializeWithHDMIOutput
) {
774 AudioNodeList audio_nodes
;
775 audio_nodes
.push_back(kInternalSpeaker
);
776 audio_nodes
.push_back(kHDMIOutput
);
777 SetUpCrasAudioHandler(audio_nodes
);
779 // Verify the audio devices size.
780 AudioDeviceList audio_devices
;
781 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
782 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
783 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
785 // Verify the HDMI device has been selected as the active output.
786 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
787 AudioDevice active_output
;
789 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
790 EXPECT_EQ(kHDMIOutput
.id
, active_output
.id
);
791 EXPECT_EQ(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
792 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
795 TEST_F(CrasAudioHandlerTest
, ConnectAndDisconnectHDMIOutput
) {
796 // Initialize with internal speaker.
797 AudioNodeList audio_nodes
;
798 audio_nodes
.push_back(kInternalSpeaker
);
799 SetUpCrasAudioHandler(audio_nodes
);
800 const size_t init_nodes_size
= audio_nodes
.size();
802 // Verify the audio devices size.
803 AudioDeviceList audio_devices
;
804 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
805 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
806 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
808 // Verify the internal speaker is selected as the active output initially.
809 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
810 AudioDevice active_output
;
812 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
813 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
814 EXPECT_EQ(kInternalSpeaker
.id
,
815 cras_audio_handler_
->GetPrimaryActiveOutputNode());
816 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
818 // Connect to HDMI output.
820 AudioNode
internal_speaker(kInternalSpeaker
);
821 internal_speaker
.active
= true;
822 internal_speaker
.plugged_time
= 80000000;
823 audio_nodes
.push_back(internal_speaker
);
824 AudioNode
hdmi(kHDMIOutput
);
825 hdmi
.plugged_time
= 90000000;
826 audio_nodes
.push_back(hdmi
);
827 ChangeAudioNodes(audio_nodes
);
829 // Verify the AudioNodesChanged event is fired and new audio device is added.
830 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
831 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
832 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
834 // Verify the active output device is switched to hdmi output, and
835 // ActiveOutputChanged event is fired.
836 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
838 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
839 EXPECT_EQ(kHDMIOutput
.id
, active_output
.id
);
840 EXPECT_EQ(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
841 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
843 // Disconnect hdmi headset.
845 audio_nodes
.push_back(kInternalSpeaker
);
846 ChangeAudioNodes(audio_nodes
);
848 // Verify the AudioNodesChanged event is fired and one audio device is
850 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
851 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
852 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
854 // Verify the active output device is switched to internal speaker, and
855 // ActiveOutputChanged event is fired.
856 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
858 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
859 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
860 EXPECT_EQ(kInternalSpeaker
.id
,
861 cras_audio_handler_
->GetPrimaryActiveOutputNode());
862 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
865 TEST_F(CrasAudioHandlerTest
, HandleHeadphoneAndHDMIOutput
) {
866 // Initialize with internal speaker, headphone and HDMI output.
867 AudioNodeList audio_nodes
;
868 audio_nodes
.push_back(kInternalSpeaker
);
869 audio_nodes
.push_back(kHeadphone
);
870 audio_nodes
.push_back(kHDMIOutput
);
871 SetUpCrasAudioHandler(audio_nodes
);
872 const size_t init_nodes_size
= audio_nodes
.size();
874 // Verify the audio devices size.
875 AudioDeviceList audio_devices
;
876 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
877 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
878 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
880 // Verify the headphone is selected as the active output initially.
881 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
882 AudioDevice active_output
;
884 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
885 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
886 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
887 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
889 // Disconnect HDMI output.
891 audio_nodes
.push_back(kInternalSpeaker
);
892 audio_nodes
.push_back(kHDMIOutput
);
893 ChangeAudioNodes(audio_nodes
);
895 // Verify the AudioNodesChanged event is fired and one audio device is
897 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
898 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
899 EXPECT_EQ(init_nodes_size
- 1, audio_devices
.size());
901 // Verify the active output device is switched to HDMI output, and
902 // ActiveOutputChanged event is fired.
903 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
905 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
906 EXPECT_EQ(kHDMIOutput
.id
, active_output
.id
);
907 EXPECT_EQ(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
908 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
911 TEST_F(CrasAudioHandlerTest
, InitializeWithUSBHeadphone
) {
912 AudioNodeList audio_nodes
;
913 audio_nodes
.push_back(kInternalSpeaker
);
914 audio_nodes
.push_back(kUSBHeadphone1
);
915 SetUpCrasAudioHandler(audio_nodes
);
917 // Verify the audio devices size.
918 AudioDeviceList audio_devices
;
919 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
920 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
921 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
923 // Verify the usb headphone has been selected as the active output.
924 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
925 AudioDevice active_output
;
927 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
928 EXPECT_EQ(kUSBHeadphone1
.id
, active_output
.id
);
929 EXPECT_EQ(kUSBHeadphone1
.id
,
930 cras_audio_handler_
->GetPrimaryActiveOutputNode());
931 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
934 TEST_F(CrasAudioHandlerTest
, PlugAndUnplugUSBHeadphone
) {
935 // Initialize with internal speaker.
936 AudioNodeList audio_nodes
;
937 audio_nodes
.push_back(kInternalSpeaker
);
938 SetUpCrasAudioHandler(audio_nodes
);
939 const size_t init_nodes_size
= audio_nodes
.size();
941 // Verify the audio devices size.
942 AudioDeviceList audio_devices
;
943 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
944 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
945 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
947 // Verify the internal speaker is selected as the active output initially.
948 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
949 AudioDevice active_output
;
951 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
952 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
953 EXPECT_EQ(kInternalSpeaker
.id
,
954 cras_audio_handler_
->GetPrimaryActiveOutputNode());
955 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
957 // Plug in usb headphone
959 AudioNode
internal_speaker(kInternalSpeaker
);
960 internal_speaker
.active
= true;
961 internal_speaker
.plugged_time
= 80000000;
962 audio_nodes
.push_back(internal_speaker
);
963 AudioNode
usb_headphone(kUSBHeadphone1
);
964 usb_headphone
.plugged_time
= 90000000;
965 audio_nodes
.push_back(usb_headphone
);
966 ChangeAudioNodes(audio_nodes
);
968 // Verify the AudioNodesChanged event is fired and new audio device is added.
969 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
970 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
971 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
973 // Verify the active output device is switched to usb headphone, and
974 // ActiveOutputChanged event is fired.
975 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
977 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
978 EXPECT_EQ(kUSBHeadphone1
.id
, active_output
.id
);
979 EXPECT_EQ(kUSBHeadphone1
.id
,
980 cras_audio_handler_
->GetPrimaryActiveOutputNode());
981 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
983 // Unplug usb headphone.
985 audio_nodes
.push_back(kInternalSpeaker
);
986 ChangeAudioNodes(audio_nodes
);
988 // Verify the AudioNodesChanged event is fired and one audio device is
990 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
991 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
992 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
994 // Verify the active output device is switched to internal speaker, and
995 // ActiveOutputChanged event is fired.
996 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
998 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
999 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
1000 EXPECT_EQ(kInternalSpeaker
.id
,
1001 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1002 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
1005 TEST_F(CrasAudioHandlerTest
, HandleMultipleUSBHeadphones
) {
1006 // Initialize with internal speaker and one usb headphone.
1007 AudioNodeList audio_nodes
;
1008 audio_nodes
.push_back(kInternalSpeaker
);
1009 audio_nodes
.push_back(kUSBHeadphone1
);
1010 SetUpCrasAudioHandler(audio_nodes
);
1011 const size_t init_nodes_size
= audio_nodes
.size();
1013 // Verify the audio devices size.
1014 AudioDeviceList audio_devices
;
1015 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1016 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1017 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1019 // Verify the usb headphone is selected as the active output initially.
1020 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1021 AudioDevice active_output
;
1023 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1024 EXPECT_EQ(kUSBHeadphone1
.id
, active_output
.id
);
1025 EXPECT_EQ(kUSBHeadphone1
.id
,
1026 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1027 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1029 // Plug in another usb headphone.
1030 audio_nodes
.clear();
1031 audio_nodes
.push_back(kInternalSpeaker
);
1032 AudioNode
usb_headphone_1(kUSBHeadphone1
);
1033 usb_headphone_1
.active
= true;
1034 usb_headphone_1
.plugged_time
= 80000000;
1035 audio_nodes
.push_back(usb_headphone_1
);
1036 AudioNode
usb_headphone_2(kUSBHeadphone2
);
1037 usb_headphone_2
.plugged_time
= 90000000;
1038 audio_nodes
.push_back(usb_headphone_2
);
1039 ChangeAudioNodes(audio_nodes
);
1041 // Verify the AudioNodesChanged event is fired and new audio device is added.
1042 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1043 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1044 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
1046 // Verify the active output device is switched to the 2nd usb headphone, which
1047 // is plugged later, and ActiveOutputChanged event is fired.
1048 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1050 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1051 EXPECT_EQ(kUSBHeadphone2
.id
, active_output
.id
);
1052 EXPECT_EQ(kUSBHeadphone2
.id
,
1053 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1054 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1056 // Unplug the 2nd usb headphone.
1057 audio_nodes
.clear();
1058 audio_nodes
.push_back(kInternalSpeaker
);
1059 audio_nodes
.push_back(kUSBHeadphone1
);
1060 ChangeAudioNodes(audio_nodes
);
1062 // Verify the AudioNodesChanged event is fired and one audio device is
1064 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1065 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1066 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1068 // Verify the active output device is switched to the first usb headphone, and
1069 // ActiveOutputChanged event is fired.
1070 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
1072 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1073 EXPECT_EQ(kUSBHeadphone1
.id
, active_output
.id
);
1074 EXPECT_EQ(kUSBHeadphone1
.id
,
1075 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1076 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1079 TEST_F(CrasAudioHandlerTest
, UnplugUSBHeadphonesWithActiveSpeaker
) {
1080 // Initialize with internal speaker and one usb headphone.
1081 AudioNodeList audio_nodes
;
1082 audio_nodes
.push_back(kInternalSpeaker
);
1083 audio_nodes
.push_back(kUSBHeadphone1
);
1084 SetUpCrasAudioHandler(audio_nodes
);
1085 const size_t init_nodes_size
= audio_nodes
.size();
1087 // Verify the audio devices size.
1088 AudioDeviceList audio_devices
;
1089 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1090 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1091 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1093 // Verify the usb headphone is selected as the active output initially.
1094 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1095 AudioDevice active_output
;
1097 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1098 EXPECT_EQ(kUSBHeadphone1
.id
, active_output
.id
);
1099 EXPECT_EQ(kUSBHeadphone1
.id
,
1100 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1101 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1103 // Plug in the headphone jack.
1104 audio_nodes
.clear();
1105 audio_nodes
.push_back(kInternalSpeaker
);
1106 AudioNode
usb_headphone_1(kUSBHeadphone1
);
1107 usb_headphone_1
.active
= true;
1108 usb_headphone_1
.plugged_time
= 80000000;
1109 audio_nodes
.push_back(usb_headphone_1
);
1110 AudioNode
headphone_jack(kHeadphone
);
1111 headphone_jack
.plugged_time
= 90000000;
1112 audio_nodes
.push_back(headphone_jack
);
1113 ChangeAudioNodes(audio_nodes
);
1115 // Verify the AudioNodesChanged event is fired and new audio device is added.
1116 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1117 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1118 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
1120 // Verify the active output device is switched to the headphone jack, which
1121 // is plugged later, and ActiveOutputChanged event is fired.
1122 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1124 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1125 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
1126 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1127 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1129 // Select the speaker to be the active output device.
1130 AudioDevice
internal_speaker(kInternalSpeaker
);
1131 cras_audio_handler_
->SwitchToDevice(internal_speaker
, true);
1133 // Verify the active output is switched to internal speaker, and the
1134 // ActiveOutputNodeChanged event is fired.
1135 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
1137 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1138 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
1139 EXPECT_EQ(kInternalSpeaker
.id
,
1140 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1142 // Unplug the usb headphone.
1143 audio_nodes
.clear();
1144 AudioNode
internal_speaker_node(kInternalSpeaker
);
1145 internal_speaker_node
.active
= true;
1146 internal_speaker_node
.plugged_time
= 70000000;
1147 audio_nodes
.push_back(internal_speaker_node
);
1148 headphone_jack
.active
= false;
1149 audio_nodes
.push_back(headphone_jack
);
1150 ChangeAudioNodes(audio_nodes
);
1152 // Verify the AudioNodesChanged event is fired and one audio device is
1154 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1155 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1156 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1158 // Verify the active output device remains to be speaker.
1159 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
1161 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1162 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
1163 EXPECT_EQ(kInternalSpeaker
.id
,
1164 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1165 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1168 TEST_F(CrasAudioHandlerTest
, OneActiveAudioOutputAfterLoginNewUserSession
) {
1169 // This tests the case found with crbug.com/273271.
1170 // Initialize with internal speaker, bluetooth headphone and headphone jack
1171 // for a new chrome session after user signs out from the previous session.
1172 // Headphone jack is plugged in later than bluetooth headphone, but bluetooth
1173 // headphone is selected as the active output by user from previous user
1175 AudioNodeList audio_nodes
;
1176 audio_nodes
.push_back(kInternalSpeaker
);
1177 AudioNode
bluetooth_headphone(kBluetoothHeadset
);
1178 bluetooth_headphone
.active
= true;
1179 bluetooth_headphone
.plugged_time
= 70000000;
1180 audio_nodes
.push_back(bluetooth_headphone
);
1181 AudioNode
headphone_jack(kHeadphone
);
1182 headphone_jack
.plugged_time
= 80000000;
1183 audio_nodes
.push_back(headphone_jack
);
1184 SetUpCrasAudioHandlerWithPrimaryActiveNode(audio_nodes
, bluetooth_headphone
);
1185 const size_t init_nodes_size
= audio_nodes
.size();
1187 // Verify the audio devices size.
1188 AudioDeviceList audio_devices
;
1189 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1190 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1191 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1193 // Verify the headphone jack is selected as the active output and all other
1194 // audio devices are not active.
1195 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1196 AudioDevice active_output
;
1198 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1199 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
1200 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1201 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1202 for (size_t i
= 0; i
< audio_devices
.size(); ++i
) {
1203 if (audio_devices
[i
].id
!= kHeadphone
.id
)
1204 EXPECT_FALSE(audio_devices
[i
].active
);
1208 TEST_F(CrasAudioHandlerTest
, BluetoothSpeakerIdChangedOnFly
) {
1209 // Initialize with internal speaker and bluetooth headset.
1210 AudioNodeList audio_nodes
;
1211 audio_nodes
.push_back(kInternalSpeaker
);
1212 audio_nodes
.push_back(kBluetoothHeadset
);
1213 SetUpCrasAudioHandler(audio_nodes
);
1214 const size_t init_nodes_size
= audio_nodes
.size();
1216 // Verify the audio devices size.
1217 AudioDeviceList audio_devices
;
1218 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1219 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1220 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1222 // Verify the bluetooth headset is selected as the active output and all other
1223 // audio devices are not active.
1224 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1225 AudioDevice active_output
;
1227 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1228 EXPECT_EQ(kBluetoothHeadset
.id
, active_output
.id
);
1229 EXPECT_EQ(kBluetoothHeadset
.id
,
1230 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1231 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1233 // Cras changes the bluetooth headset's id on the fly.
1234 audio_nodes
.clear();
1235 AudioNode
internal_speaker(kInternalSpeaker
);
1236 internal_speaker
.active
= false;
1237 audio_nodes
.push_back(internal_speaker
);
1238 AudioNode
bluetooth_headphone(kBluetoothHeadset
);
1239 // Change bluetooth headphone id.
1240 bluetooth_headphone
.id
= kBluetoothHeadsetId
+ 20000;
1241 bluetooth_headphone
.active
= false;
1242 audio_nodes
.push_back(bluetooth_headphone
);
1243 ChangeAudioNodes(audio_nodes
);
1245 // Verify NodesChanged event is fired, and the audio devices size is not
1247 audio_devices
.clear();
1248 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1249 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1250 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1252 // Verify ActiveOutputNodeChanged event is fired, and active device should be
1253 // bluetooth headphone.
1254 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1256 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1257 EXPECT_EQ(bluetooth_headphone
.id
, active_output
.id
);
1260 TEST_F(CrasAudioHandlerTest
, PlugUSBMic
) {
1261 // Set up initial audio devices, only with internal mic.
1262 AudioNodeList audio_nodes
;
1263 audio_nodes
.push_back(kInternalMic
);
1264 SetUpCrasAudioHandler(audio_nodes
);
1265 const size_t init_nodes_size
= audio_nodes
.size();
1267 // Verify the audio devices size.
1268 AudioDeviceList audio_devices
;
1269 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1270 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1271 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1273 // Verify the internal mic is selected as the active input.
1274 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1275 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1276 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
1278 // Plug the USB Mic.
1279 audio_nodes
.clear();
1280 AudioNode
internal_mic(kInternalMic
);
1281 internal_mic
.active
= true;
1282 audio_nodes
.push_back(internal_mic
);
1283 audio_nodes
.push_back(kUSBMic
);
1284 ChangeAudioNodes(audio_nodes
);
1286 // Verify the AudioNodesChanged event is fired and new audio device is added.
1287 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1288 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1289 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
1291 // Verify the active input device is switched to USB mic and
1292 // and ActiveInputChanged event is fired.
1293 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
1294 EXPECT_EQ(kUSBMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1295 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1298 TEST_F(CrasAudioHandlerTest
, UnplugUSBMic
) {
1299 // Set up initial audio devices, with internal mic and USB Mic.
1300 AudioNodeList audio_nodes
;
1301 audio_nodes
.push_back(kInternalMic
);
1302 audio_nodes
.push_back(kUSBMic
);
1303 SetUpCrasAudioHandler(audio_nodes
);
1304 const size_t init_nodes_size
= audio_nodes
.size();
1306 // Verify the audio devices size.
1307 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1308 AudioDeviceList audio_devices
;
1309 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1310 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1312 // Verify the USB mic is selected as the active output.
1313 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1314 EXPECT_EQ(kUSBMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1315 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1317 // Unplug the USB Mic.
1318 audio_nodes
.clear();
1319 audio_nodes
.push_back(kInternalMic
);
1320 ChangeAudioNodes(audio_nodes
);
1322 // Verify the AudioNodesChanged event is fired, and one audio device is
1324 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1325 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1326 EXPECT_EQ(init_nodes_size
- 1, audio_devices
.size());
1328 // Verify the active input device is switched to internal mic, and
1329 // and ActiveInputChanged event is fired.
1330 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
1331 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1332 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
1335 TEST_F(CrasAudioHandlerTest
, PlugUSBMicNotAffectActiveOutput
) {
1336 // Set up initial audio devices.
1337 AudioNodeList audio_nodes
;
1338 audio_nodes
.push_back(kInternalSpeaker
);
1339 audio_nodes
.push_back(kHeadphone
);
1340 audio_nodes
.push_back(kInternalMic
);
1341 SetUpCrasAudioHandler(audio_nodes
);
1342 const size_t init_nodes_size
= audio_nodes
.size();
1344 // Verify the audio devices size.
1345 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1346 AudioDeviceList audio_devices
;
1347 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1348 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1350 // Verify the internal mic is selected as the active input.
1351 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1352 EXPECT_EQ(kInternalMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1353 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
1355 // Verify the headphone is selected as the active output.
1356 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1357 EXPECT_EQ(kHeadphoneId
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1358 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1360 // Switch the active output to internal speaker.
1361 AudioDevice
internal_speaker(kInternalSpeaker
);
1362 cras_audio_handler_
->SwitchToDevice(internal_speaker
, true);
1364 // Verify the active output is switched to internal speaker, and the
1365 // ActiveOutputNodeChanged event is fired.
1366 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1367 AudioDevice active_output
;
1369 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1370 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
1371 EXPECT_EQ(kInternalSpeaker
.id
,
1372 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1374 // Plug the USB Mic.
1375 audio_nodes
.clear();
1376 AudioNode
internal_speaker_node(kInternalSpeaker
);
1377 internal_speaker_node
.active
= true;
1378 audio_nodes
.push_back(internal_speaker_node
);
1379 audio_nodes
.push_back(kHeadphone
);
1380 AudioNode
internal_mic(kInternalMic
);
1381 internal_mic
.active
= true;
1382 audio_nodes
.push_back(internal_mic
);
1383 audio_nodes
.push_back(kUSBMic
);
1384 ChangeAudioNodes(audio_nodes
);
1386 // Verify the AudioNodesChanged event is fired, one new device is added.
1387 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1388 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1389 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
1391 // Verify the active input device is switched to USB mic, and
1392 // and ActiveInputChanged event is fired.
1393 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
1394 EXPECT_EQ(kUSBMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1395 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1397 // Verify the active output device is not changed.
1398 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1400 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1401 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
1402 EXPECT_EQ(kInternalSpeaker
.id
,
1403 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1406 TEST_F(CrasAudioHandlerTest
, PlugHeadphoneAutoUnplugSpeakerWithActiveUSB
) {
1407 // Set up initial audio devices.
1408 AudioNodeList audio_nodes
;
1409 audio_nodes
.push_back(kUSBHeadphone1
);
1410 audio_nodes
.push_back(kInternalSpeaker
);
1411 audio_nodes
.push_back(kInternalMic
);
1412 SetUpCrasAudioHandler(audio_nodes
);
1413 const size_t init_nodes_size
= audio_nodes
.size();
1415 // Verify the audio devices size.
1416 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1417 AudioDeviceList audio_devices
;
1418 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1419 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1421 // Verify the internal mic is selected as the active input.
1422 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1423 EXPECT_EQ(kInternalMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1424 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
1426 // Verify the USB headphone is selected as the active output.
1427 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1428 EXPECT_EQ(kUSBHeadphoneId1
,
1429 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1430 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1432 // Plug the headphone and auto-unplug internal speaker.
1433 audio_nodes
.clear();
1434 AudioNode
usb_headphone_node(kUSBHeadphone1
);
1435 usb_headphone_node
.active
= true;
1436 audio_nodes
.push_back(usb_headphone_node
);
1437 AudioNode
headphone_node(kHeadphone
);
1438 headphone_node
.plugged_time
= 1000;
1439 audio_nodes
.push_back(headphone_node
);
1440 AudioNode
internal_mic(kInternalMic
);
1441 internal_mic
.active
= true;
1442 audio_nodes
.push_back(internal_mic
);
1443 ChangeAudioNodes(audio_nodes
);
1445 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1446 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1447 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1448 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1450 // Verify the active output device is switched to headphone, and
1451 // an ActiveOutputChanged event is fired.
1452 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1453 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1454 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1456 // Unplug the headphone and internal speaker auto-plugs back.
1457 audio_nodes
.clear();
1458 audio_nodes
.push_back(kUSBHeadphone1
);
1459 AudioNode
internal_speaker_node(kInternalSpeaker
);
1460 internal_speaker_node
.plugged_time
= 2000;
1461 audio_nodes
.push_back(internal_speaker_node
);
1462 audio_nodes
.push_back(internal_mic
);
1463 ChangeAudioNodes(audio_nodes
);
1465 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1466 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1467 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1468 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1470 // Verify the active output device is switched back to USB, and
1471 // an ActiveOutputChanged event is fired.
1472 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
1473 EXPECT_EQ(kUSBHeadphone1
.id
,
1474 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1475 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1477 // Verify the active input device is not changed.
1478 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1479 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1482 TEST_F(CrasAudioHandlerTest
, PlugMicAutoUnplugInternalMicWithActiveUSB
) {
1483 // Set up initial audio devices.
1484 AudioNodeList audio_nodes
;
1485 audio_nodes
.push_back(kUSBHeadphone1
);
1486 audio_nodes
.push_back(kInternalSpeaker
);
1487 audio_nodes
.push_back(kUSBMic
);
1488 audio_nodes
.push_back(kInternalMic
);
1489 SetUpCrasAudioHandler(audio_nodes
);
1490 const size_t init_nodes_size
= audio_nodes
.size();
1492 // Verify the audio devices size.
1493 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1494 AudioDeviceList audio_devices
;
1495 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1496 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1498 // Verify the internal mic is selected as the active input.
1499 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1500 EXPECT_EQ(kUSBMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1501 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1503 // Verify the internal speaker is selected as the active output.
1504 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1505 EXPECT_EQ(kUSBHeadphoneId1
,
1506 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1507 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1509 // Plug the headphone and mic, auto-unplug internal mic and speaker.
1510 audio_nodes
.clear();
1511 AudioNode
usb_headphone_node(kUSBHeadphone1
);
1512 usb_headphone_node
.active
= true;
1513 audio_nodes
.push_back(usb_headphone_node
);
1514 AudioNode
headphone_node(kHeadphone
);
1515 headphone_node
.plugged_time
= 1000;
1516 audio_nodes
.push_back(headphone_node
);
1517 AudioNode
usb_mic(kUSBMic
);
1518 usb_mic
.active
= true;
1519 audio_nodes
.push_back(usb_mic
);
1520 AudioNode
mic_jack(kMicJack
);
1521 mic_jack
.plugged_time
= 1000;
1522 audio_nodes
.push_back(mic_jack
);
1523 ChangeAudioNodes(audio_nodes
);
1525 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1526 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1527 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1528 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1530 // Verify the active output device is switched to headphone, and
1531 // an ActiveOutputChanged event is fired.
1532 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1533 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1534 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1536 // Verify the active input device is switched to mic jack, and
1537 // an ActiveInputChanged event is fired.
1538 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
1539 EXPECT_EQ(kMicJack
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1540 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1542 // Unplug the headphone and internal speaker auto-plugs back.
1543 audio_nodes
.clear();
1544 audio_nodes
.push_back(kUSBHeadphone1
);
1545 AudioNode
internal_speaker_node(kInternalSpeaker
);
1546 internal_speaker_node
.plugged_time
= 2000;
1547 audio_nodes
.push_back(internal_speaker_node
);
1548 audio_nodes
.push_back(kUSBMic
);
1549 AudioNode
internal_mic(kInternalMic
);
1550 internal_mic
.plugged_time
= 2000;
1551 audio_nodes
.push_back(internal_mic
);
1552 ChangeAudioNodes(audio_nodes
);
1554 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1555 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1556 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1557 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1559 // Verify the active output device is switched back to USB, and
1560 // an ActiveOutputChanged event is fired.
1561 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
1562 EXPECT_EQ(kUSBHeadphone1
.id
,
1563 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1564 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1566 // Verify the active input device is switched back to USB mic, and
1567 // an ActiveInputChanged event is fired.
1568 EXPECT_EQ(2, test_observer_
->active_input_node_changed_count());
1569 EXPECT_EQ(kUSBMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1570 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1573 TEST_F(CrasAudioHandlerTest
, MultipleNodesChangedSignalsOnPlugInHeadphone
) {
1574 // Set up initial audio devices.
1575 AudioNodeList audio_nodes
;
1576 audio_nodes
.push_back(kInternalSpeaker
);
1577 audio_nodes
.push_back(kBluetoothHeadset
);
1578 SetUpCrasAudioHandler(audio_nodes
);
1579 const size_t init_nodes_size
= audio_nodes
.size();
1581 // Verify the audio devices size.
1582 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1583 AudioDeviceList audio_devices
;
1584 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1585 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1587 // Verify the bluetooth headset is selected as the active output.
1588 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1589 EXPECT_EQ(kBluetoothHeadsetId
,
1590 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1591 AudioDevice active_output
;
1593 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1594 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1596 // Plug in headphone, but fire NodesChanged signal twice.
1597 audio_nodes
.clear();
1598 audio_nodes
.push_back(kInternalSpeaker
);
1599 AudioNode
bluetooth_headset(kBluetoothHeadset
);
1600 bluetooth_headset
.plugged_time
= 1000;
1601 bluetooth_headset
.active
= true;
1602 audio_nodes
.push_back(bluetooth_headset
);
1603 AudioNode
headphone(kHeadphone
);
1604 headphone
.active
= false;
1605 headphone
.plugged_time
= 2000;
1606 audio_nodes
.push_back(headphone
);
1607 ChangeAudioNodes(audio_nodes
);
1608 ChangeAudioNodes(audio_nodes
);
1610 // Verify the active output device is set to headphone.
1611 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1612 EXPECT_LE(1, test_observer_
->active_output_node_changed_count());
1613 EXPECT_EQ(headphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1615 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1616 EXPECT_EQ(headphone
.id
, active_output
.id
);
1618 // Verfiy the audio devices data is consistent, i.e., the active output device
1619 // should be headphone.
1620 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1621 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
1622 for (size_t i
= 0; i
< audio_devices
.size(); ++i
) {
1623 if (audio_devices
[i
].id
== kInternalSpeaker
.id
)
1624 EXPECT_FALSE(audio_devices
[i
].active
);
1625 else if (audio_devices
[i
].id
== bluetooth_headset
.id
)
1626 EXPECT_FALSE(audio_devices
[i
].active
);
1627 else if (audio_devices
[i
].id
== headphone
.id
)
1628 EXPECT_TRUE(audio_devices
[i
].active
);
1634 TEST_F(CrasAudioHandlerTest
, MultipleNodesChangedSignalsOnPlugInUSBMic
) {
1635 // Set up initial audio devices.
1636 AudioNodeList audio_nodes
;
1637 audio_nodes
.push_back(kInternalMic
);
1638 SetUpCrasAudioHandler(audio_nodes
);
1639 const size_t init_nodes_size
= audio_nodes
.size();
1641 // Verify the audio devices size.
1642 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1643 AudioDeviceList audio_devices
;
1644 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1645 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1647 // Verify the internal mic is selected as the active output.
1648 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1649 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1650 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
1651 EXPECT_TRUE(audio_devices
[0].active
);
1653 // Plug in usb mic, but fire NodesChanged signal twice.
1654 audio_nodes
.clear();
1655 AudioNode
internal_mic(kInternalMic
);
1656 internal_mic
.active
= true;
1657 internal_mic
.plugged_time
= 1000;
1658 audio_nodes
.push_back(internal_mic
);
1659 AudioNode
usb_mic(kUSBMic
);
1660 usb_mic
.active
= false;
1661 usb_mic
.plugged_time
= 2000;
1662 audio_nodes
.push_back(usb_mic
);
1663 ChangeAudioNodes(audio_nodes
);
1664 ChangeAudioNodes(audio_nodes
);
1666 // Verify the active output device is set to headphone.
1667 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1668 EXPECT_LE(1, test_observer_
->active_input_node_changed_count());
1669 EXPECT_EQ(usb_mic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1670 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1672 // Verfiy the audio devices data is consistent, i.e., the active input device
1673 // should be usb mic.
1674 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1675 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
1676 for (size_t i
= 0; i
< audio_devices
.size(); ++i
) {
1677 if (audio_devices
[i
].id
== kInternalMic
.id
)
1678 EXPECT_FALSE(audio_devices
[i
].active
);
1679 else if (audio_devices
[i
].id
== usb_mic
.id
)
1680 EXPECT_TRUE(audio_devices
[i
].active
);
1686 // This is the case of crbug.com/291303.
1687 TEST_F(CrasAudioHandlerTest
, MultipleNodesChangedSignalsOnSystemBoot
) {
1688 // Set up audio handler with empty audio_nodes.
1689 AudioNodeList audio_nodes
;
1690 SetUpCrasAudioHandler(audio_nodes
);
1692 AudioNode
internal_speaker(kInternalSpeaker
);
1693 internal_speaker
.active
= false;
1694 AudioNode
headphone(kHeadphone
);
1695 headphone
.active
= false;
1696 AudioNode
internal_mic(kInternalMic
);
1697 internal_mic
.active
= false;
1698 audio_nodes
.push_back(internal_speaker
);
1699 audio_nodes
.push_back(headphone
);
1700 audio_nodes
.push_back(internal_mic
);
1701 const size_t init_nodes_size
= audio_nodes
.size();
1703 // Simulate AudioNodesChanged signal being fired twice during system boot.
1704 ChangeAudioNodes(audio_nodes
);
1705 ChangeAudioNodes(audio_nodes
);
1707 // Verify the active output device is set to headphone.
1708 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1709 EXPECT_LE(1, test_observer_
->active_output_node_changed_count());
1710 EXPECT_EQ(headphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1711 AudioDevice active_output
;
1713 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1714 EXPECT_EQ(headphone
.id
, active_output
.id
);
1716 // Verify the active input device id is set to internal mic.
1717 EXPECT_EQ(internal_mic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1719 // Verfiy the audio devices data is consistent, i.e., the active output device
1720 // should be headphone, and the active input device should internal mic.
1721 AudioDeviceList audio_devices
;
1722 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1723 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1724 for (size_t i
= 0; i
< audio_devices
.size(); ++i
) {
1725 if (audio_devices
[i
].id
== internal_speaker
.id
)
1726 EXPECT_FALSE(audio_devices
[i
].active
);
1727 else if (audio_devices
[i
].id
== headphone
.id
)
1728 EXPECT_TRUE(audio_devices
[i
].active
);
1729 else if (audio_devices
[i
].id
== internal_mic
.id
)
1730 EXPECT_TRUE(audio_devices
[i
].active
);
1736 // This is the case of crbug.com/448924.
1737 TEST_F(CrasAudioHandlerTest
,
1738 TwoNodesChangedSignalsForLosingTowNodesOnOneUnplug
) {
1739 // Set up audio handler with 4 audio_nodes.
1740 AudioNodeList audio_nodes
;
1741 AudioNode
internal_speaker(kInternalSpeaker
);
1742 internal_speaker
.active
= false;
1743 AudioNode
headphone(kHeadphone
);
1744 headphone
.active
= false;
1745 AudioNode
internal_mic(kInternalMic
);
1746 internal_mic
.active
= false;
1747 AudioNode
micJack(kMicJack
);
1748 micJack
.active
= false;
1749 audio_nodes
.push_back(internal_speaker
);
1750 audio_nodes
.push_back(headphone
);
1751 audio_nodes
.push_back(internal_mic
);
1752 audio_nodes
.push_back(micJack
);
1753 SetUpCrasAudioHandler(audio_nodes
);
1755 // Verify the audio devices size.
1756 AudioDeviceList audio_devices
;
1757 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1758 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
1760 // Verify the headphone has been selected as the active output.
1761 AudioDevice active_output
;
1763 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1764 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
1765 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1766 EXPECT_TRUE(active_output
.active
);
1767 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1769 // Verify the mic Jack has been selected as the active input.
1770 EXPECT_EQ(micJack
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1771 const AudioDevice
* active_input
= GetDeviceFromId(micJack
.id
);
1772 EXPECT_TRUE(active_input
->active
);
1773 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1775 // Simulate the nodes list in first NodesChanged signal, only headphone is
1776 // removed, other nodes remains the same.
1777 AudioNodeList changed_nodes_1
;
1778 internal_speaker
.active
= false;
1779 changed_nodes_1
.push_back(internal_speaker
);
1780 internal_mic
.active
= false;
1781 changed_nodes_1
.push_back(internal_mic
);
1782 micJack
.active
= true;
1783 changed_nodes_1
.push_back(micJack
);
1785 // Simulate the nodes list in second NodesChanged signal, the micJac is
1786 // removed, but the internal_mic is inactive, which does not reflect the
1787 // active status set from the first NodesChanged signal since this was sent
1788 // before cras receives the SetActiveOutputNode from the first NodesChanged
1790 AudioNodeList changed_nodes_2
;
1791 changed_nodes_2
.push_back(internal_speaker
);
1792 changed_nodes_2
.push_back(internal_mic
);
1794 // Simulate AudioNodesChanged signal being fired twice for unplug an audio
1795 // device with both input and output nodes on it.
1796 ChangeAudioNodes(changed_nodes_1
);
1797 ChangeAudioNodes(changed_nodes_2
);
1799 AudioDeviceList changed_devices
;
1800 cras_audio_handler_
->GetAudioDevices(&changed_devices
);
1801 EXPECT_EQ(2u, changed_devices
.size());
1803 // Verify the active output device is set to internal speaker.
1804 EXPECT_EQ(internal_speaker
.id
,
1805 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1807 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1808 EXPECT_EQ(internal_speaker
.id
, active_output
.id
);
1809 EXPECT_TRUE(active_output
.active
);
1811 // Verify the active input device id is set to internal mic.
1812 EXPECT_EQ(internal_mic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1813 const AudioDevice
* changed_active_input
= GetDeviceFromId(internal_mic
.id
);
1814 EXPECT_TRUE(changed_active_input
->active
);
1817 TEST_F(CrasAudioHandlerTest
, SetOutputMute
) {
1818 AudioNodeList audio_nodes
;
1819 audio_nodes
.push_back(kInternalSpeaker
);
1820 SetUpCrasAudioHandler(audio_nodes
);
1821 EXPECT_EQ(0, test_observer_
->output_mute_changed_count());
1824 cras_audio_handler_
->SetOutputMute(true);
1826 // Verify the output is muted, OnOutputMuteChanged event is fired,
1827 // and mute value is saved in the preferences.
1828 EXPECT_TRUE(cras_audio_handler_
->IsOutputMuted());
1829 EXPECT_EQ(1, test_observer_
->output_mute_changed_count());
1830 AudioDevice
speaker(kInternalSpeaker
);
1831 EXPECT_TRUE(audio_pref_handler_
->GetMuteValue(speaker
));
1833 // Unmute the device.
1834 cras_audio_handler_
->SetOutputMute(false);
1836 // Verify the output is unmuted, OnOutputMuteChanged event is fired,
1837 // and mute value is saved in the preferences.
1838 EXPECT_FALSE(cras_audio_handler_
->IsOutputMuted());
1839 EXPECT_EQ(2, test_observer_
->output_mute_changed_count());
1840 EXPECT_FALSE(audio_pref_handler_
->GetMuteValue(speaker
));
1843 TEST_F(CrasAudioHandlerTest
, SetInputMute
) {
1844 AudioNodeList audio_nodes
;
1845 audio_nodes
.push_back(kInternalMic
);
1846 SetUpCrasAudioHandler(audio_nodes
);
1847 EXPECT_EQ(0, test_observer_
->input_mute_changed_count());
1850 cras_audio_handler_
->SetInputMute(true);
1852 // Verify the input is muted, OnInputMuteChanged event is fired.
1853 EXPECT_TRUE(cras_audio_handler_
->IsInputMuted());
1854 EXPECT_EQ(1, test_observer_
->input_mute_changed_count());
1856 // Unmute the device.
1857 cras_audio_handler_
->SetInputMute(false);
1859 // Verify the input is unmuted, OnInputMuteChanged event is fired.
1860 EXPECT_FALSE(cras_audio_handler_
->IsInputMuted());
1861 EXPECT_EQ(2, test_observer_
->input_mute_changed_count());
1864 TEST_F(CrasAudioHandlerTest
, SetOutputVolumePercent
) {
1865 AudioNodeList audio_nodes
;
1866 audio_nodes
.push_back(kInternalSpeaker
);
1867 SetUpCrasAudioHandler(audio_nodes
);
1868 EXPECT_EQ(0, test_observer_
->output_volume_changed_count());
1870 cras_audio_handler_
->SetOutputVolumePercent(60);
1872 // Verify the output volume is changed to the designated value,
1873 // OnOutputNodeVolumeChanged event is fired, and the device volume value
1874 // is saved the preferences.
1875 const int kVolume
= 60;
1876 EXPECT_EQ(kVolume
, cras_audio_handler_
->GetOutputVolumePercent());
1877 EXPECT_EQ(1, test_observer_
->output_volume_changed_count());
1879 EXPECT_TRUE(cras_audio_handler_
->GetPrimaryActiveOutputDevice(&device
));
1880 EXPECT_EQ(device
.id
, kInternalSpeaker
.id
);
1881 EXPECT_EQ(kVolume
, audio_pref_handler_
->GetOutputVolumeValue(&device
));
1884 TEST_F(CrasAudioHandlerTest
, SetInputGainPercent
) {
1885 AudioNodeList audio_nodes
;
1886 audio_nodes
.push_back(kInternalMic
);
1887 SetUpCrasAudioHandler(audio_nodes
);
1888 EXPECT_EQ(0, test_observer_
->input_gain_changed_count());
1890 cras_audio_handler_
->SetInputGainPercent(60);
1892 // Verify the input gain changed to the designated value,
1893 // OnInputNodeGainChanged event is fired, and the device gain value
1894 // is saved in the preferences.
1895 const int kGain
= 60;
1896 EXPECT_EQ(kGain
, cras_audio_handler_
->GetInputGainPercent());
1897 EXPECT_EQ(1, test_observer_
->input_gain_changed_count());
1898 AudioDevice
internal_mic(kInternalMic
);
1899 EXPECT_EQ(kGain
, audio_pref_handler_
->GetInputGainValue(&internal_mic
));
1902 TEST_F(CrasAudioHandlerTest
, SetMuteForDevice
) {
1903 AudioNodeList audio_nodes
;
1904 audio_nodes
.push_back(kInternalSpeaker
);
1905 audio_nodes
.push_back(kHeadphone
);
1906 audio_nodes
.push_back(kInternalMic
);
1907 audio_nodes
.push_back(kUSBMic
);
1908 SetUpCrasAudioHandler(audio_nodes
);
1910 // Mute the active output device.
1911 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1912 cras_audio_handler_
->SetMuteForDevice(kHeadphone
.id
, true);
1914 // Verify the headphone is muted and mute value is saved in the preferences.
1915 EXPECT_TRUE(cras_audio_handler_
->IsOutputMutedForDevice(kHeadphone
.id
));
1916 AudioDevice
headphone(kHeadphone
);
1917 EXPECT_TRUE(audio_pref_handler_
->GetMuteValue(headphone
));
1919 // Mute the non-active output device.
1920 cras_audio_handler_
->SetMuteForDevice(kInternalSpeaker
.id
, true);
1922 // Verify the internal speaker is muted and mute value is saved in the
1924 EXPECT_TRUE(cras_audio_handler_
->IsOutputMutedForDevice(kInternalSpeaker
.id
));
1925 AudioDevice
internal_speaker(kInternalSpeaker
);
1926 EXPECT_TRUE(audio_pref_handler_
->GetMuteValue(internal_speaker
));
1928 // Mute the active input device.
1929 EXPECT_EQ(kUSBMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1930 cras_audio_handler_
->SetMuteForDevice(kUSBMic
.id
, true);
1932 // Verify the USB Mic is muted.
1933 EXPECT_TRUE(cras_audio_handler_
->IsInputMutedForDevice(kUSBMic
.id
));
1935 // Mute the non-active input device should be a no-op, see crbug.com/365050.
1936 cras_audio_handler_
->SetMuteForDevice(kInternalMic
.id
, true);
1938 // Verify IsInputMutedForDevice returns false for non-active input device.
1939 EXPECT_FALSE(cras_audio_handler_
->IsInputMutedForDevice(kInternalMic
.id
));
1942 TEST_F(CrasAudioHandlerTest
, SetVolumeGainPercentForDevice
) {
1943 AudioNodeList audio_nodes
;
1944 audio_nodes
.push_back(kInternalSpeaker
);
1945 audio_nodes
.push_back(kHeadphone
);
1946 audio_nodes
.push_back(kInternalMic
);
1947 audio_nodes
.push_back(kUSBMic
);
1948 SetUpCrasAudioHandler(audio_nodes
);
1950 // Set volume percent for active output device.
1951 const int kHeadphoneVolume
= 30;
1952 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1953 cras_audio_handler_
->SetVolumeGainPercentForDevice(kHeadphone
.id
,
1956 // Verify the volume percent of headphone is set, and saved in preferences.
1957 EXPECT_EQ(kHeadphoneVolume
,
1958 cras_audio_handler_
->GetOutputVolumePercentForDevice(
1960 AudioDevice
headphone(kHeadphone
);
1961 EXPECT_EQ(kHeadphoneVolume
,
1962 audio_pref_handler_
->GetOutputVolumeValue(&headphone
));
1964 // Set volume percent for non-active output device.
1965 const int kSpeakerVolume
= 60;
1966 cras_audio_handler_
->SetVolumeGainPercentForDevice(kInternalSpeaker
.id
,
1969 // Verify the volume percent of speaker is set, and saved in preferences.
1970 EXPECT_EQ(kSpeakerVolume
,
1971 cras_audio_handler_
->GetOutputVolumePercentForDevice(
1972 kInternalSpeaker
.id
));
1973 AudioDevice
speaker(kInternalSpeaker
);
1974 EXPECT_EQ(kSpeakerVolume
,
1975 audio_pref_handler_
->GetOutputVolumeValue(&speaker
));
1977 // Set gain percent for active input device.
1978 const int kUSBMicGain
= 30;
1979 EXPECT_EQ(kUSBMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1980 cras_audio_handler_
->SetVolumeGainPercentForDevice(kUSBMic
.id
,
1983 // Verify the gain percent of USB mic is set, and saved in preferences.
1984 EXPECT_EQ(kUSBMicGain
,
1985 cras_audio_handler_
->GetOutputVolumePercentForDevice(kUSBMic
.id
));
1986 AudioDevice
usb_mic(kHeadphone
);
1987 EXPECT_EQ(kUSBMicGain
,
1988 audio_pref_handler_
->GetInputGainValue(&usb_mic
));
1990 // Set gain percent for non-active input device.
1991 const int kInternalMicGain
= 60;
1992 cras_audio_handler_
->SetVolumeGainPercentForDevice(kInternalMic
.id
,
1995 // Verify the gain percent of internal mic is set, and saved in preferences.
1996 EXPECT_EQ(kInternalMicGain
,
1997 cras_audio_handler_
->GetOutputVolumePercentForDevice(
1999 AudioDevice
internal_mic(kInternalMic
);
2000 EXPECT_EQ(kInternalMicGain
,
2001 audio_pref_handler_
->GetInputGainValue(&internal_mic
));
2004 TEST_F(CrasAudioHandlerTest
, HandleOtherDeviceType
) {
2005 const size_t kNumValidAudioDevices
= 4;
2006 AudioNodeList audio_nodes
;
2007 audio_nodes
.push_back(kInternalSpeaker
);
2008 audio_nodes
.push_back(kOtherTypeOutput
);
2009 audio_nodes
.push_back(kInternalMic
);
2010 audio_nodes
.push_back(kOtherTypeInput
);
2011 SetUpCrasAudioHandler(audio_nodes
);
2013 // Verify the audio devices size.
2014 AudioDeviceList audio_devices
;
2015 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2016 EXPECT_EQ(kNumValidAudioDevices
, audio_devices
.size());
2018 // Verify the internal speaker has been selected as the active output,
2019 // and the output device with some randown unknown type is handled gracefully.
2020 AudioDevice active_output
;
2022 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
2023 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
2024 EXPECT_EQ(kInternalSpeaker
.id
,
2025 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2026 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
2028 // Ensure the internal microphone has been selected as the active input,
2029 // and the input device with some random unknown type is handled gracefully.
2030 AudioDevice active_input
;
2031 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
2032 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
2035 TEST_F(CrasAudioHandlerTest
, ChangeActiveNodesHotrodInit
) {
2036 AudioNodeList audio_nodes
;
2037 audio_nodes
.push_back(kHDMIOutput
);
2038 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2039 audio_nodes
.push_back(kUSBJabraSpeakerOutput2
);
2040 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2041 audio_nodes
.push_back(kUSBJabraSpeakerInput2
);
2042 audio_nodes
.push_back(kUSBCameraInput
);
2043 SetUpCrasAudioHandler(audio_nodes
);
2045 // Verify the audio devices size.
2046 AudioDeviceList audio_devices
;
2047 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2048 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2050 // Verify only the 1st jabra speaker's output and input are selected as active
2051 // nodes by CrasAudioHandler.
2052 AudioDevice active_output
;
2054 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
2055 EXPECT_EQ(2, GetActiveDeviceCount());
2056 AudioDevice primary_active_device
;
2057 EXPECT_TRUE(cras_audio_handler_
->GetPrimaryActiveOutputDevice(
2058 &primary_active_device
));
2059 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
, primary_active_device
.id
);
2060 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2061 cras_audio_handler_
->GetPrimaryActiveInputNode());
2063 // Set both jabra speakers's input and output nodes to active, this simulate
2064 // the call sent by hotrod initialization process.
2065 test_observer_
->reset_active_output_node_changed_count();
2066 test_observer_
->reset_active_input_node_changed_count();
2067 CrasAudioHandler::NodeIdList active_nodes
;
2068 active_nodes
.push_back(kUSBJabraSpeakerOutput1
.id
);
2069 active_nodes
.push_back(kUSBJabraSpeakerOutput2
.id
);
2070 active_nodes
.push_back(kUSBJabraSpeakerInput1
.id
);
2071 active_nodes
.push_back(kUSBJabraSpeakerInput2
.id
);
2072 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2074 // Verify both jabra speakers' input/output nodes are made active.
2075 // num_active_nodes = GetActiveDeviceCount();
2076 EXPECT_EQ(4, GetActiveDeviceCount());
2077 const AudioDevice
* active_output_1
=
2078 GetDeviceFromId(kUSBJabraSpeakerOutput1
.id
);
2079 EXPECT_TRUE(active_output_1
->active
);
2080 const AudioDevice
* active_output_2
=
2081 GetDeviceFromId(kUSBJabraSpeakerOutput2
.id
);
2082 EXPECT_TRUE(active_output_2
->active
);
2083 EXPECT_TRUE(cras_audio_handler_
->GetPrimaryActiveOutputDevice(
2084 &primary_active_device
));
2085 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
, primary_active_device
.id
);
2086 const AudioDevice
* active_input_1
=
2087 GetDeviceFromId(kUSBJabraSpeakerInput1
.id
);
2088 EXPECT_TRUE(active_input_1
->active
);
2089 const AudioDevice
* active_input_2
=
2090 GetDeviceFromId(kUSBJabraSpeakerInput2
.id
);
2091 EXPECT_TRUE(active_input_2
->active
);
2092 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2093 cras_audio_handler_
->GetPrimaryActiveInputNode());
2095 // Verify only 1 ActiveOutputNodeChanged notification has been sent out
2096 // by calling ChangeActiveNodes.
2097 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
2098 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
2100 // Verify all active devices are the not muted and their volume values are
2102 EXPECT_FALSE(cras_audio_handler_
->IsOutputMuted());
2104 cras_audio_handler_
->IsOutputMutedForDevice(kUSBJabraSpeakerOutput1
.id
));
2106 cras_audio_handler_
->IsOutputMutedForDevice(kUSBJabraSpeakerOutput2
.id
));
2107 EXPECT_EQ(cras_audio_handler_
->GetOutputVolumePercent(),
2108 cras_audio_handler_
->GetOutputVolumePercentForDevice(
2109 kUSBJabraSpeakerOutput1
.id
));
2110 EXPECT_EQ(cras_audio_handler_
->GetOutputVolumePercent(),
2111 cras_audio_handler_
->GetOutputVolumePercentForDevice(
2112 kUSBJabraSpeakerOutput2
.id
));
2114 // Adjust the volume of output devices, verify all active nodes are set to
2116 cras_audio_handler_
->SetOutputVolumePercent(25);
2117 EXPECT_EQ(25, cras_audio_handler_
->GetOutputVolumePercent());
2118 EXPECT_EQ(25, cras_audio_handler_
->GetOutputVolumePercentForDevice(
2119 kUSBJabraSpeakerOutput1
.id
));
2120 EXPECT_EQ(25, cras_audio_handler_
->GetOutputVolumePercentForDevice(
2121 kUSBJabraSpeakerOutput2
.id
));
2124 TEST_F(CrasAudioHandlerTest
, ChangeActiveNodesHotrodInitWithCameraInputActive
) {
2125 AudioNodeList audio_nodes
;
2126 audio_nodes
.push_back(kHDMIOutput
);
2127 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2128 audio_nodes
.push_back(kUSBJabraSpeakerOutput2
);
2129 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2130 audio_nodes
.push_back(kUSBJabraSpeakerInput2
);
2131 // Make the camera input to be plugged in later than jabra's input.
2132 AudioNode
usb_camera(kUSBCameraInput
);
2133 usb_camera
.plugged_time
= 10000000;
2134 audio_nodes
.push_back(usb_camera
);
2135 SetUpCrasAudioHandler(audio_nodes
);
2137 // Verify the audio devices size.
2138 AudioDeviceList audio_devices
;
2139 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2140 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2142 // Verify the 1st jabra speaker's output is selected as active output
2143 // node and camera's input is selected active input by CrasAudioHandler.
2144 EXPECT_EQ(2, GetActiveDeviceCount());
2145 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2146 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2147 EXPECT_EQ(kUSBCameraInput
.id
,
2148 cras_audio_handler_
->GetPrimaryActiveInputNode());
2150 // Set both jabra speakers's input and output nodes to active, this simulates
2151 // the call sent by hotrod initialization process.
2152 test_observer_
->reset_active_output_node_changed_count();
2153 test_observer_
->reset_active_input_node_changed_count();
2154 CrasAudioHandler::NodeIdList active_nodes
;
2155 active_nodes
.push_back(kUSBJabraSpeakerOutput1
.id
);
2156 active_nodes
.push_back(kUSBJabraSpeakerOutput2
.id
);
2157 active_nodes
.push_back(kUSBJabraSpeakerInput1
.id
);
2158 active_nodes
.push_back(kUSBJabraSpeakerInput2
.id
);
2159 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2161 // Verify both jabra speakers' input/output nodes are made active.
2162 // num_active_nodes = GetActiveDeviceCount();
2163 EXPECT_EQ(4, GetActiveDeviceCount());
2164 const AudioDevice
* active_output_1
=
2165 GetDeviceFromId(kUSBJabraSpeakerOutput1
.id
);
2166 EXPECT_TRUE(active_output_1
->active
);
2167 const AudioDevice
* active_output_2
=
2168 GetDeviceFromId(kUSBJabraSpeakerOutput2
.id
);
2169 EXPECT_TRUE(active_output_2
->active
);
2170 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2171 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2172 const AudioDevice
* active_input_1
=
2173 GetDeviceFromId(kUSBJabraSpeakerInput1
.id
);
2174 EXPECT_TRUE(active_input_1
->active
);
2175 const AudioDevice
* active_input_2
=
2176 GetDeviceFromId(kUSBJabraSpeakerInput2
.id
);
2177 EXPECT_TRUE(active_input_2
->active
);
2178 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2179 cras_audio_handler_
->GetPrimaryActiveInputNode());
2181 // Verify only 1 ActiveOutputNodeChanged notification has been sent out
2182 // by calling ChangeActiveNodes.
2183 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
2184 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
2187 TEST_F(CrasAudioHandlerTest
, ChangeActiveNodesWithFewerActives
) {
2188 AudioNodeList audio_nodes
;
2189 audio_nodes
.push_back(kHDMIOutput
);
2190 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2191 audio_nodes
.push_back(kUSBJabraSpeakerOutput2
);
2192 SetUpCrasAudioHandler(audio_nodes
);
2194 // Verify the audio devices size.
2195 AudioDeviceList audio_devices
;
2196 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2197 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2199 // Set all three nodes to be active.
2200 CrasAudioHandler::NodeIdList active_nodes
;
2201 active_nodes
.push_back(kHDMIOutput
.id
);
2202 active_nodes
.push_back(kUSBJabraSpeakerOutput1
.id
);
2203 active_nodes
.push_back(kUSBJabraSpeakerOutput2
.id
);
2204 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2206 // Verify all three nodes are active.
2207 EXPECT_EQ(3, GetActiveDeviceCount());
2208 const AudioDevice
* active_output_1
= GetDeviceFromId(kHDMIOutput
.id
);
2209 EXPECT_TRUE(active_output_1
->active
);
2210 const AudioDevice
* active_output_2
=
2211 GetDeviceFromId(kUSBJabraSpeakerOutput1
.id
);
2212 EXPECT_TRUE(active_output_2
->active
);
2213 const AudioDevice
* active_output_3
=
2214 GetDeviceFromId(kUSBJabraSpeakerOutput2
.id
);
2215 EXPECT_TRUE(active_output_3
->active
);
2217 // Now call ChangeActiveDevices with only 2 nodes.
2218 active_nodes
.clear();
2219 active_nodes
.push_back(kUSBJabraSpeakerOutput1
.id
);
2220 active_nodes
.push_back(kUSBJabraSpeakerOutput2
.id
);
2221 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2223 // Verify only 2 nodes are active.
2224 EXPECT_EQ(2, GetActiveDeviceCount());
2225 const AudioDevice
* output_1
= GetDeviceFromId(kHDMIOutput
.id
);
2226 EXPECT_FALSE(output_1
->active
);
2227 const AudioDevice
* output_2
= GetDeviceFromId(kUSBJabraSpeakerOutput1
.id
);
2228 EXPECT_TRUE(output_2
->active
);
2229 const AudioDevice
* output_3
= GetDeviceFromId(kUSBJabraSpeakerOutput2
.id
);
2230 EXPECT_TRUE(output_3
->active
);
2233 TEST_F(CrasAudioHandlerTest
, HotrodInitWithSingleJabra
) {
2234 // Simulates the hotrod initializated with a single jabra device and
2235 // CrasAudioHandler selected jabra input/output as active devices.
2236 AudioNodeList audio_nodes
;
2237 audio_nodes
.push_back(kHDMIOutput
);
2238 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2239 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2240 audio_nodes
.push_back(kUSBCameraInput
);
2241 SetUpCrasAudioHandler(audio_nodes
);
2243 // Verify the audio devices size.
2244 AudioDeviceList audio_devices
;
2245 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2246 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2248 // Verify the jabra speaker's output and input are selected as active nodes
2249 // by CrasAudioHandler.
2250 EXPECT_EQ(2, GetActiveDeviceCount());
2251 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2252 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2253 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2254 cras_audio_handler_
->GetPrimaryActiveInputNode());
2257 TEST_F(CrasAudioHandlerTest
,
2258 ChangeActiveNodesHotrodInitWithSingleJabraCameraPlugInLater
) {
2259 AudioNodeList audio_nodes
;
2260 audio_nodes
.push_back(kHDMIOutput
);
2261 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2262 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2263 AudioNode
usb_camera(kUSBCameraInput
);
2264 usb_camera
.plugged_time
= 10000000;
2265 audio_nodes
.push_back(usb_camera
);
2266 SetUpCrasAudioHandler(audio_nodes
);
2268 // Verify the audio devices size.
2269 AudioDeviceList audio_devices
;
2270 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2271 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2273 // Verify the jabra speaker's output is selected as active output, and
2274 // camera's input is selected as active input by CrasAudioHandler
2275 EXPECT_EQ(2, GetActiveDeviceCount());
2276 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2277 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2278 EXPECT_EQ(kUSBCameraInput
.id
,
2279 cras_audio_handler_
->GetPrimaryActiveInputNode());
2281 // Simulate hotrod app call to set jabra input as active device with only
2282 // jabra input node in the active node list, which does not conform to the
2283 // new SetActiveDevices protocol, but just show we can still handle it if
2285 CrasAudioHandler::NodeIdList active_nodes
;
2286 active_nodes
.push_back(kUSBJabraSpeakerOutput1
.id
);
2287 active_nodes
.push_back(kUSBJabraSpeakerInput1
.id
);
2288 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2290 // Verify the jabra speaker's output is selected as active output, and
2291 // jabra's input is selected as active input.
2292 EXPECT_EQ(2, GetActiveDeviceCount());
2293 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2294 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2295 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2296 cras_audio_handler_
->GetPrimaryActiveInputNode());
2299 TEST_F(CrasAudioHandlerTest
,
2300 ChangeActiveNodesHotrodInitWithSingleJabraCameraPlugInLaterOldCall
) {
2301 AudioNodeList audio_nodes
;
2302 audio_nodes
.push_back(kHDMIOutput
);
2303 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2304 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2305 AudioNode
usb_camera(kUSBCameraInput
);
2306 usb_camera
.plugged_time
= 10000000;
2307 audio_nodes
.push_back(usb_camera
);
2308 SetUpCrasAudioHandler(audio_nodes
);
2310 // Verify the audio devices size.
2311 AudioDeviceList audio_devices
;
2312 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2313 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2315 // Verify the jabra speaker's output is selected as active output, and
2316 // camera's input is selected as active input by CrasAudioHandler
2317 EXPECT_EQ(2, GetActiveDeviceCount());
2318 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2319 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2320 EXPECT_EQ(kUSBCameraInput
.id
,
2321 cras_audio_handler_
->GetPrimaryActiveInputNode());
2323 // Simulate hotrod app call to set jabra input as active device with only
2324 // jabra input node in the active node list, which does not conform to the
2325 // new SetActiveDevices protocol, but just show we can still handle it if
2327 CrasAudioHandler::NodeIdList active_nodes
;
2328 active_nodes
.push_back(kUSBJabraSpeakerInput1
.id
);
2329 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2331 // Verify the jabra speaker's output is selected as active output, and
2332 // jabra's input is selected as active input.
2333 EXPECT_EQ(2, GetActiveDeviceCount());
2334 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2335 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2336 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2337 cras_audio_handler_
->GetPrimaryActiveInputNode());
2340 TEST_F(CrasAudioHandlerTest
,
2341 ChangeActiveNodesHotrodInitWithSingleJabraChangeOutput
) {
2342 AudioNodeList audio_nodes
;
2343 audio_nodes
.push_back(kHDMIOutput
);
2344 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2345 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2346 audio_nodes
.push_back(kUSBCameraInput
);
2347 SetUpCrasAudioHandler(audio_nodes
);
2349 // Verify the audio devices size.
2350 AudioDeviceList audio_devices
;
2351 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2352 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2354 // Verify the jabra speaker's output and input are selected as active output
2355 // by CrasAudioHandler.
2356 EXPECT_EQ(2, GetActiveDeviceCount());
2357 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2358 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2359 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2360 cras_audio_handler_
->GetPrimaryActiveInputNode());
2362 // Simulate hotrod app call SetActiveDevices to change active output
2363 // with only complete list of active nodes passed in, which is the new
2364 // way of hotrod app.
2365 CrasAudioHandler::NodeIdList active_nodes
;
2366 active_nodes
.push_back(kHDMIOutput
.id
);
2367 active_nodes
.push_back(kUSBJabraSpeakerInput1
.id
);
2368 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2370 // Verify the jabra speaker's output is selected as active output, and
2371 // jabra's input is selected as active input.
2372 EXPECT_EQ(2, GetActiveDeviceCount());
2373 EXPECT_EQ(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2374 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2375 cras_audio_handler_
->GetPrimaryActiveInputNode());
2378 TEST_F(CrasAudioHandlerTest
,
2379 ChangeActiveNodesHotrodInitWithSingleJabraChangeOutputOldCall
) {
2380 AudioNodeList audio_nodes
;
2381 audio_nodes
.push_back(kHDMIOutput
);
2382 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2383 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2384 audio_nodes
.push_back(kUSBCameraInput
);
2385 SetUpCrasAudioHandler(audio_nodes
);
2387 // Verify the audio devices size.
2388 AudioDeviceList audio_devices
;
2389 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2390 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2392 // Verify the jabra speaker's output and input are selected as active output
2393 // by CrasAudioHandler.
2394 EXPECT_EQ(2, GetActiveDeviceCount());
2395 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2396 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2397 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2398 cras_audio_handler_
->GetPrimaryActiveInputNode());
2400 // Simulate hotrod app call SetActiveDevices to change active output
2401 // with only a single active output nodes passed in, which is the old
2402 // way of hotrod app.
2403 CrasAudioHandler::NodeIdList active_nodes
;
2404 active_nodes
.push_back(kHDMIOutput
.id
);
2405 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2407 // Verify the jabra speaker's output is selected as active output, and
2408 // jabra's input is selected as active input.
2409 EXPECT_EQ(2, GetActiveDeviceCount());
2410 EXPECT_EQ(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2411 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2412 cras_audio_handler_
->GetPrimaryActiveInputNode());
2415 TEST_F(CrasAudioHandlerTest
, NoMoreAudioInputDevices
) {
2416 // Some device like chromebox does not have the internal input device. The
2417 // active devices should be reset when the user plugs a device and then
2418 // unplugs it to such device.
2420 AudioNodeList audio_nodes
;
2421 audio_nodes
.push_back(kInternalSpeaker
);
2422 SetUpCrasAudioHandler(audio_nodes
);
2424 EXPECT_EQ(0ULL, cras_audio_handler_
->GetPrimaryActiveInputNode());
2426 audio_nodes
.push_back(kMicJack
);
2427 ChangeAudioNodes(audio_nodes
);
2429 EXPECT_EQ(kMicJack
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
2430 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
2431 test_observer_
->reset_active_input_node_changed_count();
2433 audio_nodes
.pop_back();
2434 ChangeAudioNodes(audio_nodes
);
2435 EXPECT_EQ(0ULL, cras_audio_handler_
->GetPrimaryActiveInputNode());
2436 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
2439 // Test the case in which an HDMI output is plugged in with other higher
2441 // output devices already plugged and user has manually selected an active
2443 // The hotplug of hdmi output should not change user's selection of active
2445 // crbug.com/447826.
2446 TEST_F(CrasAudioHandlerTest
, HotPlugHDMINotChangeActiveOutput
) {
2447 AudioNodeList audio_nodes
;
2448 AudioNode
internal_speaker(kInternalSpeaker
);
2449 audio_nodes
.push_back(internal_speaker
);
2450 AudioNode
usb_headset(kUSBHeadphone1
);
2451 usb_headset
.plugged_time
= 80000000;
2452 audio_nodes
.push_back(usb_headset
);
2453 SetUpCrasAudioHandler(audio_nodes
);
2455 // Verify the audio devices size.
2456 AudioDeviceList audio_devices
;
2457 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2458 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2460 // Verify the USB headset is selected as active output by default.
2461 EXPECT_EQ(usb_headset
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2463 // Manually set the active output to internal speaker.
2464 AudioDevice
internal_output(kInternalSpeaker
);
2465 cras_audio_handler_
->SwitchToDevice(internal_output
, true);
2467 // Verify the active output is switched to internal speaker.
2468 EXPECT_EQ(internal_speaker
.id
,
2469 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2470 EXPECT_LT(kInternalSpeaker
.plugged_time
, usb_headset
.plugged_time
);
2471 const AudioDevice
* usb_device
= GetDeviceFromId(usb_headset
.id
);
2472 EXPECT_FALSE(usb_device
->active
);
2474 // Plug in HDMI output.
2475 audio_nodes
.clear();
2476 internal_speaker
.active
= true;
2477 audio_nodes
.push_back(internal_speaker
);
2478 usb_headset
.active
= false;
2479 audio_nodes
.push_back(usb_headset
);
2480 AudioNode
hdmi(kHDMIOutput
);
2481 hdmi
.plugged_time
= 90000000;
2482 audio_nodes
.push_back(hdmi
);
2483 ChangeAudioNodes(audio_nodes
);
2485 // The active output should not change.
2486 EXPECT_EQ(kInternalSpeaker
.id
,
2487 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2490 // Test the case in which the active device was set to inactive from cras after
2491 // resuming from suspension state. See crbug.com/478968.
2492 TEST_F(CrasAudioHandlerTest
, ActiveNodeLostAfterResume
) {
2493 AudioNodeList audio_nodes
;
2494 EXPECT_FALSE(kHeadphone
.active
);
2495 audio_nodes
.push_back(kHeadphone
);
2496 EXPECT_FALSE(kHDMIOutput
.active
);
2497 audio_nodes
.push_back(kHDMIOutput
);
2498 SetUpCrasAudioHandler(audio_nodes
);
2500 // Verify the headphone is selected as the active output.
2501 AudioDeviceList audio_devices
;
2502 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2503 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2504 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2505 const AudioDevice
* active_headphone
= GetDeviceFromId(kHeadphone
.id
);
2506 EXPECT_EQ(kHeadphone
.id
, active_headphone
->id
);
2507 EXPECT_TRUE(active_headphone
->active
);
2509 // Simulate NodesChanged signal with headphone turning into inactive state,
2510 // and HDMI node removed.
2511 audio_nodes
.clear();
2512 audio_nodes
.push_back(kHeadphone
);
2513 ChangeAudioNodes(audio_nodes
);
2515 // Verify the headphone is set to active again.
2516 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2517 const AudioDevice
* headphone_resumed
= GetDeviceFromId(kHeadphone
.id
);
2518 EXPECT_EQ(kHeadphone
.id
, headphone_resumed
->id
);
2519 EXPECT_TRUE(headphone_resumed
->active
);
2522 // Test the case in which there are two NodesChanged signal for discovering
2523 // output devices, and there is race between NodesChange and SetActiveOutput
2524 // during this process. See crbug.com/478968.
2525 TEST_F(CrasAudioHandlerTest
, ActiveNodeLostDuringLoginSession
) {
2526 AudioNodeList audio_nodes
;
2527 EXPECT_FALSE(kHeadphone
.active
);
2528 audio_nodes
.push_back(kHeadphone
);
2529 SetUpCrasAudioHandler(audio_nodes
);
2531 // Verify the headphone is selected as the active output.
2532 AudioDeviceList audio_devices
;
2533 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2534 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2535 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2536 const AudioDevice
* active_headphone
= GetDeviceFromId(kHeadphone
.id
);
2537 EXPECT_EQ(kHeadphone
.id
, active_headphone
->id
);
2538 EXPECT_TRUE(active_headphone
->active
);
2540 // Simulate NodesChanged signal with headphone turning into inactive state,
2541 // and add a new HDMI output node.
2542 audio_nodes
.clear();
2543 audio_nodes
.push_back(kHeadphone
);
2544 audio_nodes
.push_back(kHDMIOutput
);
2545 ChangeAudioNodes(audio_nodes
);
2547 // Verify the headphone is set to active again.
2548 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2549 const AudioDevice
* headphone_resumed
= GetDeviceFromId(kHeadphone
.id
);
2550 EXPECT_EQ(kHeadphone
.id
, headphone_resumed
->id
);
2551 EXPECT_TRUE(headphone_resumed
->active
);
2554 // This test HDMI output rediscovering case in crbug.com/503667.
2555 TEST_F(CrasAudioHandlerTest
, HDMIOutputRediscover
) {
2556 AudioNodeList audio_nodes
;
2557 audio_nodes
.push_back(kInternalSpeaker
);
2558 audio_nodes
.push_back(kHDMIOutput
);
2559 SetUpCrasAudioHandler(audio_nodes
);
2561 // Verify the HDMI device has been selected as the active output, and audio
2562 // output is not muted.
2563 AudioDevice active_output
;
2565 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
2566 EXPECT_EQ(kHDMIOutput
.id
, active_output
.id
);
2567 EXPECT_EQ(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2568 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
2569 EXPECT_FALSE(cras_audio_handler_
->IsOutputMuted());
2571 // Trigger HDMI rediscovering grace period, and remove the HDMI node.
2572 const int grace_period_in_ms
= 200;
2573 SetHDMIRediscoverGracePeriodDuration(grace_period_in_ms
);
2574 SetActiveHDMIRediscover();
2575 AudioNodeList audio_nodes_lost_hdmi
;
2576 audio_nodes_lost_hdmi
.push_back(kInternalSpeaker
);
2577 ChangeAudioNodes(audio_nodes_lost_hdmi
);
2579 // Verify the active output is switched to internal speaker, it is not muted
2580 // by preference, but the system output is muted during the grace period.
2582 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
2583 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
2585 cras_audio_handler_
->IsOutputMutedForDevice(kInternalSpeaker
.id
));
2586 EXPECT_TRUE(cras_audio_handler_
->IsOutputMuted());
2588 // Re-attach the HDMI device after a little delay.
2589 HDMIRediscoverWaiter
waiter(this, grace_period_in_ms
);
2590 waiter
.WaitUntilTimeOut(grace_period_in_ms
/ 4);
2591 ChangeAudioNodes(audio_nodes
);
2593 // After HDMI re-discover grace period, verify HDMI output is selected as the
2594 // active device and not muted.
2595 waiter
.WaitUntilHDMIRediscoverGracePeriodEnd();
2597 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
2598 EXPECT_EQ(kHDMIOutput
.id
, active_output
.id
);
2599 EXPECT_EQ(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2600 EXPECT_FALSE(cras_audio_handler_
->IsOutputMuted());
2603 } // namespace chromeos