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"
22 const uint64 kInternalSpeakerId
= 10001;
23 const uint64 kHeadphoneId
= 10002;
24 const uint64 kInternalMicId
= 10003;
25 const uint64 kUSBMicId
= 10004;
26 const uint64 kBluetoothHeadsetId
= 10005;
27 const uint64 kHDMIOutputId
= 10006;
28 const uint64 kUSBHeadphoneId1
= 10007;
29 const uint64 kUSBHeadphoneId2
= 10008;
30 const uint64 kMicJackId
= 10009;
31 const uint64 kKeyboardMicId
= 10010;
32 const uint64 kOtherTypeOutputId
= 90001;
33 const uint64 kOtherTypeInputId
= 90002;
34 const uint64 kUSBJabraSpeakerOutputId1
= 90003;
35 const uint64 kUSBJabraSpeakerOutputId2
= 90004;
36 const uint64 kUSBJabraSpeakerInputId1
= 90005;
37 const uint64 kUSBJabraSpeakerInputId2
= 90006;
38 const uint64 kUSBCameraInputId
= 90007;
40 const AudioNode
kInternalSpeaker(
50 const AudioNode
kHeadphone(
60 const AudioNode
kInternalMic(
70 const AudioNode
kMicJack(
80 const AudioNode
kUSBMic(
90 const AudioNode
kKeyboardMic(
100 const AudioNode
kOtherTypeOutput(
105 "Other Type Output Device",
110 const AudioNode
kOtherTypeInput(
115 "Other Type Input Device",
120 const AudioNode
kBluetoothHeadset(false,
124 "Bluetooth Headset 1",
128 const AudioNode
kHDMIOutput(false,
136 const AudioNode
kUSBHeadphone1(false,
144 const AudioNode
kUSBHeadphone2(false,
152 const AudioNode
kUSBJabraSpeakerOutput1(false,
153 kUSBJabraSpeakerOutputId1
,
160 const AudioNode
kUSBJabraSpeakerOutput2(false,
161 kUSBJabraSpeakerOutputId2
,
168 const AudioNode
kUSBJabraSpeakerInput1(true,
169 kUSBJabraSpeakerInputId1
,
176 const AudioNode
kUSBJabraSpeakerInput2(true,
177 kUSBJabraSpeakerInputId2
,
184 const AudioNode
kUSBCameraInput(true,
192 class TestObserver
: public chromeos::CrasAudioHandler::AudioObserver
{
194 TestObserver() : active_output_node_changed_count_(0),
195 active_input_node_changed_count_(0),
196 audio_nodes_changed_count_(0),
197 output_mute_changed_count_(0),
198 input_mute_changed_count_(0),
199 output_volume_changed_count_(0),
200 input_gain_changed_count_(0) {
203 int active_output_node_changed_count() const {
204 return active_output_node_changed_count_
;
207 void reset_active_output_node_changed_count() {
208 active_output_node_changed_count_
= 0;
211 int active_input_node_changed_count() const {
212 return active_input_node_changed_count_
;
215 void reset_active_input_node_changed_count() {
216 active_input_node_changed_count_
= 0;
219 int audio_nodes_changed_count() const {
220 return audio_nodes_changed_count_
;
223 int output_mute_changed_count() const {
224 return output_mute_changed_count_
;
227 int input_mute_changed_count() const {
228 return input_mute_changed_count_
;
231 int output_volume_changed_count() const {
232 return output_volume_changed_count_
;
235 int input_gain_changed_count() const {
236 return input_gain_changed_count_
;
239 ~TestObserver() override
{}
242 // chromeos::CrasAudioHandler::AudioObserver overrides.
243 void OnActiveOutputNodeChanged() override
{
244 ++active_output_node_changed_count_
;
247 void OnActiveInputNodeChanged() override
{
248 ++active_input_node_changed_count_
;
251 void OnAudioNodesChanged() override
{ ++audio_nodes_changed_count_
; }
253 void OnOutputMuteChanged(bool /* mute_on */) override
{
254 ++output_mute_changed_count_
;
257 void OnInputMuteChanged(bool /* mute_on */) override
{
258 ++input_mute_changed_count_
;
261 void OnOutputNodeVolumeChanged(uint64
/* node_id */,
262 int /* volume */) override
{
263 ++output_volume_changed_count_
;
266 void OnInputNodeGainChanged(uint64
/* node_id */, int /* gain */) override
{
267 ++input_gain_changed_count_
;
271 int active_output_node_changed_count_
;
272 int active_input_node_changed_count_
;
273 int audio_nodes_changed_count_
;
274 int output_mute_changed_count_
;
275 int input_mute_changed_count_
;
276 int output_volume_changed_count_
;
277 int input_gain_changed_count_
;
279 DISALLOW_COPY_AND_ASSIGN(TestObserver
);
282 class CrasAudioHandlerTest
: public testing::Test
{
284 CrasAudioHandlerTest() : cras_audio_handler_(NULL
),
285 fake_cras_audio_client_(NULL
) {
287 ~CrasAudioHandlerTest() override
{}
289 void SetUp() override
{}
291 void TearDown() override
{
292 cras_audio_handler_
->RemoveAudioObserver(test_observer_
.get());
293 test_observer_
.reset();
294 CrasAudioHandler::Shutdown();
295 audio_pref_handler_
= NULL
;
296 DBusThreadManager::Shutdown();
299 void SetUpCrasAudioHandler(const AudioNodeList
& audio_nodes
) {
300 DBusThreadManager::Initialize();
301 fake_cras_audio_client_
= static_cast<FakeCrasAudioClient
*>(
302 DBusThreadManager::Get()->GetCrasAudioClient());
303 fake_cras_audio_client_
->SetAudioNodesForTesting(audio_nodes
);
304 audio_pref_handler_
= new AudioDevicesPrefHandlerStub();
305 CrasAudioHandler::Initialize(audio_pref_handler_
);
306 cras_audio_handler_
= CrasAudioHandler::Get();
307 test_observer_
.reset(new TestObserver
);
308 cras_audio_handler_
->AddAudioObserver(test_observer_
.get());
309 message_loop_
.RunUntilIdle();
312 void SetUpCrasAudioHandlerWithPrimaryActiveNode(
313 const AudioNodeList
& audio_nodes
,
314 const AudioNode
& primary_active_node
) {
315 DBusThreadManager::Initialize();
316 fake_cras_audio_client_
= static_cast<FakeCrasAudioClient
*>(
317 DBusThreadManager::Get()->GetCrasAudioClient());
318 fake_cras_audio_client_
->SetAudioNodesForTesting(audio_nodes
);
319 fake_cras_audio_client_
->SetActiveOutputNode(primary_active_node
.id
),
320 audio_pref_handler_
= new AudioDevicesPrefHandlerStub();
321 CrasAudioHandler::Initialize(audio_pref_handler_
);
322 cras_audio_handler_
= CrasAudioHandler::Get();
323 test_observer_
.reset(new TestObserver
);
324 cras_audio_handler_
->AddAudioObserver(test_observer_
.get());
325 message_loop_
.RunUntilIdle();
328 void ChangeAudioNodes(const AudioNodeList
& audio_nodes
) {
329 fake_cras_audio_client_
->SetAudioNodesAndNotifyObserversForTesting(
331 message_loop_
.RunUntilIdle();
334 const AudioDevice
* GetDeviceFromId(uint64 id
) {
335 return cras_audio_handler_
->GetDeviceFromId(id
);
338 int GetActiveDeviceCount() const {
339 int num_active_nodes
= 0;
340 AudioDeviceList audio_devices
;
341 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
342 for (size_t i
= 0; i
< audio_devices
.size(); ++i
) {
343 if (audio_devices
[i
].active
)
346 return num_active_nodes
;
349 void SetActiveHDMIRediscover() {
350 cras_audio_handler_
->SetActiveHDMIOutoutRediscoveringIfNecessary(true);
353 void SetHDMIRediscoverGracePeriodDuration(int duration_in_ms
) {
354 cras_audio_handler_
->SetHDMIRediscoverGracePeriodForTesting(duration_in_ms
);
357 bool IsDuringHDMIRediscoverGracePeriod() {
358 return cras_audio_handler_
->hdmi_rediscovering();
362 base::MessageLoopForUI message_loop_
;
363 CrasAudioHandler
* cras_audio_handler_
; // Not owned.
364 FakeCrasAudioClient
* fake_cras_audio_client_
; // Not owned.
365 scoped_ptr
<TestObserver
> test_observer_
;
366 scoped_refptr
<AudioDevicesPrefHandlerStub
> audio_pref_handler_
;
369 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandlerTest
);
372 class HDMIRediscoverWaiter
{
374 HDMIRediscoverWaiter(CrasAudioHandlerTest
* cras_audio_handler_test
,
375 int grace_period_duration_in_ms
)
376 : cras_audio_handler_test_(cras_audio_handler_test
),
377 grace_period_duration_in_ms_(grace_period_duration_in_ms
) {}
379 void WaitUntilTimeOut(int wait_duration_in_ms
) {
380 base::RunLoop run_loop
;
381 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
382 FROM_HERE
, run_loop
.QuitClosure(),
383 base::TimeDelta::FromMilliseconds(wait_duration_in_ms
));
387 void CheckHDMIRediscoverGracePeriodEnd(const base::Closure
& quit_loop_func
) {
388 if (!cras_audio_handler_test_
->IsDuringHDMIRediscoverGracePeriod()) {
389 quit_loop_func
.Run();
392 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
394 base::Bind(&HDMIRediscoverWaiter::CheckHDMIRediscoverGracePeriodEnd
,
395 base::Unretained(this), quit_loop_func
),
396 base::TimeDelta::FromMilliseconds(grace_period_duration_in_ms_
/ 4));
399 void WaitUntilHDMIRediscoverGracePeriodEnd() {
400 base::RunLoop run_loop
;
401 CheckHDMIRediscoverGracePeriodEnd(run_loop
.QuitClosure());
406 CrasAudioHandlerTest
* cras_audio_handler_test_
; // not owned
407 int grace_period_duration_in_ms_
;
409 DISALLOW_COPY_AND_ASSIGN(HDMIRediscoverWaiter
);
412 TEST_F(CrasAudioHandlerTest
, InitializeWithOnlyDefaultAudioDevices
) {
413 AudioNodeList audio_nodes
;
414 audio_nodes
.push_back(kInternalSpeaker
);
415 audio_nodes
.push_back(kInternalMic
);
416 SetUpCrasAudioHandler(audio_nodes
);
418 // Verify the audio devices size.
419 AudioDeviceList audio_devices
;
420 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
421 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
423 // Verify the internal speaker has been selected as the active output.
424 AudioDevice active_output
;
426 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
427 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
428 EXPECT_EQ(kInternalSpeaker
.id
,
429 cras_audio_handler_
->GetPrimaryActiveOutputNode());
430 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
432 // Ensure the internal microphone has been selected as the active input.
433 AudioDevice active_input
;
434 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
435 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
438 TEST_F(CrasAudioHandlerTest
, InitializeWithAlternativeAudioDevices
) {
439 AudioNodeList audio_nodes
;
440 audio_nodes
.push_back(kInternalSpeaker
);
441 audio_nodes
.push_back(kHeadphone
);
442 audio_nodes
.push_back(kInternalMic
);
443 audio_nodes
.push_back(kUSBMic
);
444 SetUpCrasAudioHandler(audio_nodes
);
446 // Verify the audio devices size.
447 AudioDeviceList audio_devices
;
448 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
449 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
451 // Verify the headphone has been selected as the active output.
452 AudioDevice active_output
;
454 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
455 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
456 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
457 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
459 // Ensure the USB microphone has been selected as the active input.
460 EXPECT_EQ(kUSBMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
461 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
464 TEST_F(CrasAudioHandlerTest
, InitializeWithKeyboardMic
) {
465 AudioNodeList audio_nodes
;
466 audio_nodes
.push_back(kInternalSpeaker
);
467 audio_nodes
.push_back(kInternalMic
);
468 audio_nodes
.push_back(kKeyboardMic
);
469 SetUpCrasAudioHandler(audio_nodes
);
471 // Verify the audio devices size.
472 AudioDeviceList audio_devices
;
473 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
474 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
475 EXPECT_TRUE(cras_audio_handler_
->HasKeyboardMic());
477 // Verify the internal speaker has been selected as the active output.
478 AudioDevice active_output
;
480 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
481 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
482 EXPECT_EQ(kInternalSpeaker
.id
,
483 cras_audio_handler_
->GetPrimaryActiveOutputNode());
484 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
486 // Ensure the internal microphone has been selected as the active input,
487 // not affected by keyboard mic.
488 AudioDevice active_input
;
489 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
490 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
491 const AudioDevice
* keyboard_mic
= GetDeviceFromId(kKeyboardMic
.id
);
492 EXPECT_FALSE(keyboard_mic
->active
);
495 TEST_F(CrasAudioHandlerTest
, SetKeyboardMicActive
) {
496 AudioNodeList audio_nodes
;
497 audio_nodes
.push_back(kInternalMic
);
498 audio_nodes
.push_back(kKeyboardMic
);
499 SetUpCrasAudioHandler(audio_nodes
);
501 // Verify the audio devices size.
502 AudioDeviceList audio_devices
;
503 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
504 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
505 EXPECT_TRUE(cras_audio_handler_
->HasKeyboardMic());
507 // Ensure the internal microphone has been selected as the active input,
508 // not affected by keyboard mic.
509 AudioDevice active_input
;
510 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
511 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
512 const AudioDevice
* keyboard_mic
= GetDeviceFromId(kKeyboardMic
.id
);
513 EXPECT_FALSE(keyboard_mic
->active
);
515 // Make keyboard mic active.
516 cras_audio_handler_
->SetKeyboardMicActive(true);
517 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
518 const AudioDevice
* active_keyboard_mic
= GetDeviceFromId(kKeyboardMic
.id
);
519 EXPECT_TRUE(active_keyboard_mic
->active
);
521 // Make keyboard mic inactive.
522 cras_audio_handler_
->SetKeyboardMicActive(false);
523 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
524 const AudioDevice
* inactive_keyboard_mic
= GetDeviceFromId(kKeyboardMic
.id
);
525 EXPECT_FALSE(inactive_keyboard_mic
->active
);
528 TEST_F(CrasAudioHandlerTest
, SwitchActiveOutputDevice
) {
529 AudioNodeList audio_nodes
;
530 audio_nodes
.push_back(kInternalSpeaker
);
531 audio_nodes
.push_back(kHeadphone
);
532 SetUpCrasAudioHandler(audio_nodes
);
533 AudioDeviceList audio_devices
;
534 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
535 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
537 // Verify the initial active output device is headphone.
538 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
539 AudioDevice active_output
;
541 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
542 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
543 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
545 // Switch the active output to internal speaker.
546 AudioDevice
internal_speaker(kInternalSpeaker
);
547 cras_audio_handler_
->SwitchToDevice(internal_speaker
, true);
549 // Verify the active output is switched to internal speaker, and the
550 // ActiveOutputNodeChanged event is fired.
551 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
553 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
554 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
555 EXPECT_EQ(kInternalSpeaker
.id
,
556 cras_audio_handler_
->GetPrimaryActiveOutputNode());
559 TEST_F(CrasAudioHandlerTest
, SwitchActiveInputDevice
) {
560 AudioNodeList audio_nodes
;
561 audio_nodes
.push_back(kInternalMic
);
562 audio_nodes
.push_back(kUSBMic
);
563 SetUpCrasAudioHandler(audio_nodes
);
564 AudioDeviceList audio_devices
;
565 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
566 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
568 // Verify the initial active input device is USB mic.
569 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
570 EXPECT_EQ(kUSBMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
572 // Switch the active input to internal mic.
573 AudioDevice
internal_mic(kInternalMic
);
574 cras_audio_handler_
->SwitchToDevice(internal_mic
, true);
576 // Verify the active output is switched to internal speaker, and the active
577 // ActiveInputNodeChanged event is fired.
578 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
579 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
582 TEST_F(CrasAudioHandlerTest
, PlugHeadphone
) {
583 // Set up initial audio devices, only with internal speaker.
584 AudioNodeList audio_nodes
;
585 audio_nodes
.push_back(kInternalSpeaker
);
586 SetUpCrasAudioHandler(audio_nodes
);
587 const size_t init_nodes_size
= audio_nodes
.size();
589 // Verify the audio devices size.
590 AudioDeviceList audio_devices
;
591 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
592 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
593 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
595 // Verify the internal speaker has been selected as the active output.
596 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
597 AudioDevice active_output
;
599 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
600 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
601 EXPECT_EQ(kInternalSpeaker
.id
,
602 cras_audio_handler_
->GetPrimaryActiveOutputNode());
603 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
605 // Plug the headphone.
607 AudioNode
internal_speaker(kInternalSpeaker
);
608 internal_speaker
.active
= true;
609 audio_nodes
.push_back(internal_speaker
);
610 audio_nodes
.push_back(kHeadphone
);
611 ChangeAudioNodes(audio_nodes
);
613 // Verify the AudioNodesChanged event is fired and new audio device is added.
614 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
615 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
616 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
618 // Verify the active output device is switched to headphone and
619 // ActiveOutputChanged event is fired.
620 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
622 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
623 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
624 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
625 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
628 TEST_F(CrasAudioHandlerTest
, UnplugHeadphone
) {
629 // Set up initial audio devices, with internal speaker and headphone.
630 AudioNodeList audio_nodes
;
631 audio_nodes
.push_back(kInternalSpeaker
);
632 audio_nodes
.push_back(kHeadphone
);
633 SetUpCrasAudioHandler(audio_nodes
);
634 const size_t init_nodes_size
= audio_nodes
.size();
636 // Verify the audio devices size.
637 AudioDeviceList audio_devices
;
638 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
639 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
640 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
642 // Verify the headphone has been selected as the active output.
643 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
644 AudioDevice active_output
;
646 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
647 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
648 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
649 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
651 // Unplug the headphone.
653 audio_nodes
.push_back(kInternalSpeaker
);
654 ChangeAudioNodes(audio_nodes
);
656 // Verify the AudioNodesChanged event is fired and one audio device is
658 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
659 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
660 EXPECT_EQ(init_nodes_size
- 1, audio_devices
.size());
662 // Verify the active output device is switched to internal speaker and
663 // ActiveOutputChanged event is fired.
664 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
666 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
667 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
668 EXPECT_EQ(kInternalSpeaker
.id
,
669 cras_audio_handler_
->GetPrimaryActiveOutputNode());
670 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
673 TEST_F(CrasAudioHandlerTest
, InitializeWithBluetoothHeadset
) {
674 AudioNodeList audio_nodes
;
675 audio_nodes
.push_back(kInternalSpeaker
);
676 audio_nodes
.push_back(kBluetoothHeadset
);
677 SetUpCrasAudioHandler(audio_nodes
);
679 // Verify the audio devices size.
680 AudioDeviceList audio_devices
;
681 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
682 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
683 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
685 // Verify the bluetooth headset has been selected as the active output.
686 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
687 AudioDevice active_output
;
689 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
690 EXPECT_EQ(kBluetoothHeadset
.id
, active_output
.id
);
691 EXPECT_EQ(kBluetoothHeadset
.id
,
692 cras_audio_handler_
->GetPrimaryActiveOutputNode());
693 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
696 TEST_F(CrasAudioHandlerTest
, ConnectAndDisconnectBluetoothHeadset
) {
697 // Initialize with internal speaker and headphone.
698 AudioNodeList audio_nodes
;
699 audio_nodes
.push_back(kInternalSpeaker
);
700 audio_nodes
.push_back(kHeadphone
);
701 SetUpCrasAudioHandler(audio_nodes
);
702 const size_t init_nodes_size
= audio_nodes
.size();
704 // Verify the audio devices size.
705 AudioDeviceList audio_devices
;
706 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
707 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
708 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
710 // Verify the headphone is selected as the active output initially.
711 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
712 AudioDevice active_output
;
714 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
715 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
716 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
717 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
719 // Connect to bluetooth headset. Since it is plugged in later than
720 // headphone, active output should be switched to it.
722 audio_nodes
.push_back(kInternalSpeaker
);
723 AudioNode
headphone(kHeadphone
);
724 headphone
.plugged_time
= 80000000;
725 headphone
.active
= true;
726 audio_nodes
.push_back(headphone
);
727 AudioNode
bluetooth_headset(kBluetoothHeadset
);
728 bluetooth_headset
.plugged_time
= 90000000;
729 audio_nodes
.push_back(bluetooth_headset
);
730 ChangeAudioNodes(audio_nodes
);
732 // Verify the AudioNodesChanged event is fired and new audio device is added.
733 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
734 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
735 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
737 // Verify the active output device is switched to bluetooth headset, and
738 // ActiveOutputChanged event is fired.
739 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
741 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
742 EXPECT_EQ(kBluetoothHeadset
.id
, active_output
.id
);
743 EXPECT_EQ(kBluetoothHeadset
.id
,
744 cras_audio_handler_
->GetPrimaryActiveOutputNode());
745 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
747 // Disconnect bluetooth headset.
749 audio_nodes
.push_back(kInternalSpeaker
);
750 headphone
.active
= false;
751 audio_nodes
.push_back(headphone
);
752 ChangeAudioNodes(audio_nodes
);
754 // Verify the AudioNodesChanged event is fired and one audio device is
756 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
757 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
758 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
760 // Verify the active output device is switched to headphone, and
761 // ActiveOutputChanged event is fired.
762 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
764 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
765 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
766 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
767 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
770 TEST_F(CrasAudioHandlerTest
, InitializeWithHDMIOutput
) {
771 AudioNodeList audio_nodes
;
772 audio_nodes
.push_back(kInternalSpeaker
);
773 audio_nodes
.push_back(kHDMIOutput
);
774 SetUpCrasAudioHandler(audio_nodes
);
776 // Verify the audio devices size.
777 AudioDeviceList audio_devices
;
778 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
779 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
780 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
782 // Verify the HDMI device has been selected as the active output.
783 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
784 AudioDevice active_output
;
786 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
787 EXPECT_EQ(kHDMIOutput
.id
, active_output
.id
);
788 EXPECT_EQ(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
789 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
792 TEST_F(CrasAudioHandlerTest
, ConnectAndDisconnectHDMIOutput
) {
793 // Initialize with internal speaker.
794 AudioNodeList audio_nodes
;
795 audio_nodes
.push_back(kInternalSpeaker
);
796 SetUpCrasAudioHandler(audio_nodes
);
797 const size_t init_nodes_size
= audio_nodes
.size();
799 // Verify the audio devices size.
800 AudioDeviceList audio_devices
;
801 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
802 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
803 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
805 // Verify the internal speaker is selected as the active output initially.
806 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
807 AudioDevice active_output
;
809 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
810 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
811 EXPECT_EQ(kInternalSpeaker
.id
,
812 cras_audio_handler_
->GetPrimaryActiveOutputNode());
813 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
815 // Connect to HDMI output.
817 AudioNode
internal_speaker(kInternalSpeaker
);
818 internal_speaker
.active
= true;
819 internal_speaker
.plugged_time
= 80000000;
820 audio_nodes
.push_back(internal_speaker
);
821 AudioNode
hdmi(kHDMIOutput
);
822 hdmi
.plugged_time
= 90000000;
823 audio_nodes
.push_back(hdmi
);
824 ChangeAudioNodes(audio_nodes
);
826 // Verify the AudioNodesChanged event is fired and new audio device is added.
827 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
828 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
829 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
831 // Verify the active output device is switched to hdmi output, and
832 // ActiveOutputChanged event is fired.
833 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
835 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
836 EXPECT_EQ(kHDMIOutput
.id
, active_output
.id
);
837 EXPECT_EQ(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
838 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
840 // Disconnect hdmi headset.
842 audio_nodes
.push_back(kInternalSpeaker
);
843 ChangeAudioNodes(audio_nodes
);
845 // Verify the AudioNodesChanged event is fired and one audio device is
847 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
848 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
849 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
851 // Verify the active output device is switched to internal speaker, and
852 // ActiveOutputChanged event is fired.
853 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
855 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
856 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
857 EXPECT_EQ(kInternalSpeaker
.id
,
858 cras_audio_handler_
->GetPrimaryActiveOutputNode());
859 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
862 TEST_F(CrasAudioHandlerTest
, HandleHeadphoneAndHDMIOutput
) {
863 // Initialize with internal speaker, headphone and HDMI output.
864 AudioNodeList audio_nodes
;
865 audio_nodes
.push_back(kInternalSpeaker
);
866 audio_nodes
.push_back(kHeadphone
);
867 audio_nodes
.push_back(kHDMIOutput
);
868 SetUpCrasAudioHandler(audio_nodes
);
869 const size_t init_nodes_size
= audio_nodes
.size();
871 // Verify the audio devices size.
872 AudioDeviceList audio_devices
;
873 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
874 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
875 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
877 // Verify the headphone is selected as the active output initially.
878 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
879 AudioDevice active_output
;
881 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
882 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
883 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
884 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
886 // Disconnect HDMI output.
888 audio_nodes
.push_back(kInternalSpeaker
);
889 audio_nodes
.push_back(kHDMIOutput
);
890 ChangeAudioNodes(audio_nodes
);
892 // Verify the AudioNodesChanged event is fired and one audio device is
894 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
895 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
896 EXPECT_EQ(init_nodes_size
- 1, audio_devices
.size());
898 // Verify the active output device is switched to HDMI output, and
899 // ActiveOutputChanged event is fired.
900 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
902 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
903 EXPECT_EQ(kHDMIOutput
.id
, active_output
.id
);
904 EXPECT_EQ(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
905 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
908 TEST_F(CrasAudioHandlerTest
, InitializeWithUSBHeadphone
) {
909 AudioNodeList audio_nodes
;
910 audio_nodes
.push_back(kInternalSpeaker
);
911 audio_nodes
.push_back(kUSBHeadphone1
);
912 SetUpCrasAudioHandler(audio_nodes
);
914 // Verify the audio devices size.
915 AudioDeviceList audio_devices
;
916 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
917 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
918 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
920 // Verify the usb headphone has been selected as the active output.
921 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
922 AudioDevice active_output
;
924 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
925 EXPECT_EQ(kUSBHeadphone1
.id
, active_output
.id
);
926 EXPECT_EQ(kUSBHeadphone1
.id
,
927 cras_audio_handler_
->GetPrimaryActiveOutputNode());
928 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
931 TEST_F(CrasAudioHandlerTest
, PlugAndUnplugUSBHeadphone
) {
932 // Initialize with internal speaker.
933 AudioNodeList audio_nodes
;
934 audio_nodes
.push_back(kInternalSpeaker
);
935 SetUpCrasAudioHandler(audio_nodes
);
936 const size_t init_nodes_size
= audio_nodes
.size();
938 // Verify the audio devices size.
939 AudioDeviceList audio_devices
;
940 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
941 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
942 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
944 // Verify the internal speaker is selected as the active output initially.
945 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
946 AudioDevice active_output
;
948 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
949 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
950 EXPECT_EQ(kInternalSpeaker
.id
,
951 cras_audio_handler_
->GetPrimaryActiveOutputNode());
952 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
954 // Plug in usb headphone
956 AudioNode
internal_speaker(kInternalSpeaker
);
957 internal_speaker
.active
= true;
958 internal_speaker
.plugged_time
= 80000000;
959 audio_nodes
.push_back(internal_speaker
);
960 AudioNode
usb_headphone(kUSBHeadphone1
);
961 usb_headphone
.plugged_time
= 90000000;
962 audio_nodes
.push_back(usb_headphone
);
963 ChangeAudioNodes(audio_nodes
);
965 // Verify the AudioNodesChanged event is fired and new audio device is added.
966 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
967 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
968 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
970 // Verify the active output device is switched to usb headphone, and
971 // ActiveOutputChanged event is fired.
972 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
974 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
975 EXPECT_EQ(kUSBHeadphone1
.id
, active_output
.id
);
976 EXPECT_EQ(kUSBHeadphone1
.id
,
977 cras_audio_handler_
->GetPrimaryActiveOutputNode());
978 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
980 // Unplug usb headphone.
982 audio_nodes
.push_back(kInternalSpeaker
);
983 ChangeAudioNodes(audio_nodes
);
985 // Verify the AudioNodesChanged event is fired and one audio device is
987 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
988 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
989 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
991 // Verify the active output device is switched to internal speaker, and
992 // ActiveOutputChanged event is fired.
993 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
995 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
996 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
997 EXPECT_EQ(kInternalSpeaker
.id
,
998 cras_audio_handler_
->GetPrimaryActiveOutputNode());
999 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
1002 TEST_F(CrasAudioHandlerTest
, HandleMultipleUSBHeadphones
) {
1003 // Initialize with internal speaker and one usb headphone.
1004 AudioNodeList audio_nodes
;
1005 audio_nodes
.push_back(kInternalSpeaker
);
1006 audio_nodes
.push_back(kUSBHeadphone1
);
1007 SetUpCrasAudioHandler(audio_nodes
);
1008 const size_t init_nodes_size
= audio_nodes
.size();
1010 // Verify the audio devices size.
1011 AudioDeviceList audio_devices
;
1012 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1013 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1014 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1016 // Verify the usb headphone is selected as the active output initially.
1017 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1018 AudioDevice active_output
;
1020 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1021 EXPECT_EQ(kUSBHeadphone1
.id
, active_output
.id
);
1022 EXPECT_EQ(kUSBHeadphone1
.id
,
1023 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1024 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1026 // Plug in another usb headphone.
1027 audio_nodes
.clear();
1028 audio_nodes
.push_back(kInternalSpeaker
);
1029 AudioNode
usb_headphone_1(kUSBHeadphone1
);
1030 usb_headphone_1
.active
= true;
1031 usb_headphone_1
.plugged_time
= 80000000;
1032 audio_nodes
.push_back(usb_headphone_1
);
1033 AudioNode
usb_headphone_2(kUSBHeadphone2
);
1034 usb_headphone_2
.plugged_time
= 90000000;
1035 audio_nodes
.push_back(usb_headphone_2
);
1036 ChangeAudioNodes(audio_nodes
);
1038 // Verify the AudioNodesChanged event is fired and new audio device is added.
1039 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1040 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1041 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
1043 // Verify the active output device is switched to the 2nd usb headphone, which
1044 // is plugged later, and ActiveOutputChanged event is fired.
1045 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1047 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1048 EXPECT_EQ(kUSBHeadphone2
.id
, active_output
.id
);
1049 EXPECT_EQ(kUSBHeadphone2
.id
,
1050 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1051 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1053 // Unplug the 2nd usb headphone.
1054 audio_nodes
.clear();
1055 audio_nodes
.push_back(kInternalSpeaker
);
1056 audio_nodes
.push_back(kUSBHeadphone1
);
1057 ChangeAudioNodes(audio_nodes
);
1059 // Verify the AudioNodesChanged event is fired and one audio device is
1061 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1062 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1063 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1065 // Verify the active output device is switched to the first usb headphone, and
1066 // ActiveOutputChanged event is fired.
1067 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
1069 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1070 EXPECT_EQ(kUSBHeadphone1
.id
, active_output
.id
);
1071 EXPECT_EQ(kUSBHeadphone1
.id
,
1072 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1073 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1076 TEST_F(CrasAudioHandlerTest
, UnplugUSBHeadphonesWithActiveSpeaker
) {
1077 // Initialize with internal speaker and one usb headphone.
1078 AudioNodeList audio_nodes
;
1079 audio_nodes
.push_back(kInternalSpeaker
);
1080 audio_nodes
.push_back(kUSBHeadphone1
);
1081 SetUpCrasAudioHandler(audio_nodes
);
1082 const size_t init_nodes_size
= audio_nodes
.size();
1084 // Verify the audio devices size.
1085 AudioDeviceList audio_devices
;
1086 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1087 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1088 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1090 // Verify the usb headphone is selected as the active output initially.
1091 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1092 AudioDevice active_output
;
1094 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1095 EXPECT_EQ(kUSBHeadphone1
.id
, active_output
.id
);
1096 EXPECT_EQ(kUSBHeadphone1
.id
,
1097 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1098 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1100 // Plug in the headphone jack.
1101 audio_nodes
.clear();
1102 audio_nodes
.push_back(kInternalSpeaker
);
1103 AudioNode
usb_headphone_1(kUSBHeadphone1
);
1104 usb_headphone_1
.active
= true;
1105 usb_headphone_1
.plugged_time
= 80000000;
1106 audio_nodes
.push_back(usb_headphone_1
);
1107 AudioNode
headphone_jack(kHeadphone
);
1108 headphone_jack
.plugged_time
= 90000000;
1109 audio_nodes
.push_back(headphone_jack
);
1110 ChangeAudioNodes(audio_nodes
);
1112 // Verify the AudioNodesChanged event is fired and new audio device is added.
1113 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1114 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1115 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
1117 // Verify the active output device is switched to the headphone jack, which
1118 // is plugged later, and ActiveOutputChanged event is fired.
1119 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1121 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1122 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
1123 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1124 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1126 // Select the speaker to be the active output device.
1127 AudioDevice
internal_speaker(kInternalSpeaker
);
1128 cras_audio_handler_
->SwitchToDevice(internal_speaker
, true);
1130 // Verify the active output is switched to internal speaker, and the
1131 // ActiveOutputNodeChanged event is fired.
1132 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
1134 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1135 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
1136 EXPECT_EQ(kInternalSpeaker
.id
,
1137 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1139 // Unplug the usb headphone.
1140 audio_nodes
.clear();
1141 AudioNode
internal_speaker_node(kInternalSpeaker
);
1142 internal_speaker_node
.active
= true;
1143 internal_speaker_node
.plugged_time
= 70000000;
1144 audio_nodes
.push_back(internal_speaker_node
);
1145 headphone_jack
.active
= false;
1146 audio_nodes
.push_back(headphone_jack
);
1147 ChangeAudioNodes(audio_nodes
);
1149 // Verify the AudioNodesChanged event is fired and one audio device is
1151 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1152 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1153 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1155 // Verify the active output device remains to be speaker.
1156 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
1158 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1159 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
1160 EXPECT_EQ(kInternalSpeaker
.id
,
1161 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1162 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1165 TEST_F(CrasAudioHandlerTest
, OneActiveAudioOutputAfterLoginNewUserSession
) {
1166 // This tests the case found with crbug.com/273271.
1167 // Initialize with internal speaker, bluetooth headphone and headphone jack
1168 // for a new chrome session after user signs out from the previous session.
1169 // Headphone jack is plugged in later than bluetooth headphone, but bluetooth
1170 // headphone is selected as the active output by user from previous user
1172 AudioNodeList audio_nodes
;
1173 audio_nodes
.push_back(kInternalSpeaker
);
1174 AudioNode
bluetooth_headphone(kBluetoothHeadset
);
1175 bluetooth_headphone
.active
= true;
1176 bluetooth_headphone
.plugged_time
= 70000000;
1177 audio_nodes
.push_back(bluetooth_headphone
);
1178 AudioNode
headphone_jack(kHeadphone
);
1179 headphone_jack
.plugged_time
= 80000000;
1180 audio_nodes
.push_back(headphone_jack
);
1181 SetUpCrasAudioHandlerWithPrimaryActiveNode(audio_nodes
, bluetooth_headphone
);
1182 const size_t init_nodes_size
= audio_nodes
.size();
1184 // Verify the audio devices size.
1185 AudioDeviceList audio_devices
;
1186 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1187 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1188 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1190 // Verify the headphone jack is selected as the active output and all other
1191 // audio devices are not active.
1192 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1193 AudioDevice active_output
;
1195 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1196 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
1197 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1198 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1199 for (size_t i
= 0; i
< audio_devices
.size(); ++i
) {
1200 if (audio_devices
[i
].id
!= kHeadphone
.id
)
1201 EXPECT_FALSE(audio_devices
[i
].active
);
1205 TEST_F(CrasAudioHandlerTest
, BluetoothSpeakerIdChangedOnFly
) {
1206 // Initialize with internal speaker and bluetooth headset.
1207 AudioNodeList audio_nodes
;
1208 audio_nodes
.push_back(kInternalSpeaker
);
1209 audio_nodes
.push_back(kBluetoothHeadset
);
1210 SetUpCrasAudioHandler(audio_nodes
);
1211 const size_t init_nodes_size
= audio_nodes
.size();
1213 // Verify the audio devices size.
1214 AudioDeviceList audio_devices
;
1215 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1216 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1217 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1219 // Verify the bluetooth headset is selected as the active output and all other
1220 // audio devices are not active.
1221 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1222 AudioDevice active_output
;
1224 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1225 EXPECT_EQ(kBluetoothHeadset
.id
, active_output
.id
);
1226 EXPECT_EQ(kBluetoothHeadset
.id
,
1227 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1228 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1230 // Cras changes the bluetooth headset's id on the fly.
1231 audio_nodes
.clear();
1232 AudioNode
internal_speaker(kInternalSpeaker
);
1233 internal_speaker
.active
= false;
1234 audio_nodes
.push_back(internal_speaker
);
1235 AudioNode
bluetooth_headphone(kBluetoothHeadset
);
1236 // Change bluetooth headphone id.
1237 bluetooth_headphone
.id
= kBluetoothHeadsetId
+ 20000;
1238 bluetooth_headphone
.active
= false;
1239 audio_nodes
.push_back(bluetooth_headphone
);
1240 ChangeAudioNodes(audio_nodes
);
1242 // Verify NodesChanged event is fired, and the audio devices size is not
1244 audio_devices
.clear();
1245 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1246 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1247 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1249 // Verify ActiveOutputNodeChanged event is fired, and active device should be
1250 // bluetooth headphone.
1251 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1253 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1254 EXPECT_EQ(bluetooth_headphone
.id
, active_output
.id
);
1257 TEST_F(CrasAudioHandlerTest
, PlugUSBMic
) {
1258 // Set up initial audio devices, only with internal mic.
1259 AudioNodeList audio_nodes
;
1260 audio_nodes
.push_back(kInternalMic
);
1261 SetUpCrasAudioHandler(audio_nodes
);
1262 const size_t init_nodes_size
= audio_nodes
.size();
1264 // Verify the audio devices size.
1265 AudioDeviceList audio_devices
;
1266 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1267 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1268 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1270 // Verify the internal mic is selected as the active input.
1271 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1272 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1273 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
1275 // Plug the USB Mic.
1276 audio_nodes
.clear();
1277 AudioNode
internal_mic(kInternalMic
);
1278 internal_mic
.active
= true;
1279 audio_nodes
.push_back(internal_mic
);
1280 audio_nodes
.push_back(kUSBMic
);
1281 ChangeAudioNodes(audio_nodes
);
1283 // Verify the AudioNodesChanged event is fired and new audio device is added.
1284 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1285 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1286 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
1288 // Verify the active input device is switched to USB mic and
1289 // and ActiveInputChanged event is fired.
1290 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
1291 EXPECT_EQ(kUSBMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1292 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1295 TEST_F(CrasAudioHandlerTest
, UnplugUSBMic
) {
1296 // Set up initial audio devices, with internal mic and USB Mic.
1297 AudioNodeList audio_nodes
;
1298 audio_nodes
.push_back(kInternalMic
);
1299 audio_nodes
.push_back(kUSBMic
);
1300 SetUpCrasAudioHandler(audio_nodes
);
1301 const size_t init_nodes_size
= audio_nodes
.size();
1303 // Verify the audio devices size.
1304 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1305 AudioDeviceList audio_devices
;
1306 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1307 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1309 // Verify the USB mic is selected as the active output.
1310 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1311 EXPECT_EQ(kUSBMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1312 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1314 // Unplug the USB Mic.
1315 audio_nodes
.clear();
1316 audio_nodes
.push_back(kInternalMic
);
1317 ChangeAudioNodes(audio_nodes
);
1319 // Verify the AudioNodesChanged event is fired, and one audio device is
1321 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1322 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1323 EXPECT_EQ(init_nodes_size
- 1, audio_devices
.size());
1325 // Verify the active input device is switched to internal mic, and
1326 // and ActiveInputChanged event is fired.
1327 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
1328 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1329 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
1332 TEST_F(CrasAudioHandlerTest
, PlugUSBMicNotAffectActiveOutput
) {
1333 // Set up initial audio devices.
1334 AudioNodeList audio_nodes
;
1335 audio_nodes
.push_back(kInternalSpeaker
);
1336 audio_nodes
.push_back(kHeadphone
);
1337 audio_nodes
.push_back(kInternalMic
);
1338 SetUpCrasAudioHandler(audio_nodes
);
1339 const size_t init_nodes_size
= audio_nodes
.size();
1341 // Verify the audio devices size.
1342 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1343 AudioDeviceList audio_devices
;
1344 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1345 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1347 // Verify the internal mic is selected as the active input.
1348 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1349 EXPECT_EQ(kInternalMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1350 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
1352 // Verify the headphone is selected as the active output.
1353 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1354 EXPECT_EQ(kHeadphoneId
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1355 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1357 // Switch the active output to internal speaker.
1358 AudioDevice
internal_speaker(kInternalSpeaker
);
1359 cras_audio_handler_
->SwitchToDevice(internal_speaker
, true);
1361 // Verify the active output is switched to internal speaker, and the
1362 // ActiveOutputNodeChanged event is fired.
1363 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1364 AudioDevice active_output
;
1366 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1367 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
1368 EXPECT_EQ(kInternalSpeaker
.id
,
1369 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1371 // Plug the USB Mic.
1372 audio_nodes
.clear();
1373 AudioNode
internal_speaker_node(kInternalSpeaker
);
1374 internal_speaker_node
.active
= true;
1375 audio_nodes
.push_back(internal_speaker_node
);
1376 audio_nodes
.push_back(kHeadphone
);
1377 AudioNode
internal_mic(kInternalMic
);
1378 internal_mic
.active
= true;
1379 audio_nodes
.push_back(internal_mic
);
1380 audio_nodes
.push_back(kUSBMic
);
1381 ChangeAudioNodes(audio_nodes
);
1383 // Verify the AudioNodesChanged event is fired, one new device is added.
1384 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1385 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1386 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
1388 // Verify the active input device is switched to USB mic, and
1389 // and ActiveInputChanged event is fired.
1390 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
1391 EXPECT_EQ(kUSBMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1392 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1394 // Verify the active output device is not changed.
1395 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1397 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1398 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
1399 EXPECT_EQ(kInternalSpeaker
.id
,
1400 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1403 TEST_F(CrasAudioHandlerTest
, PlugHeadphoneAutoUnplugSpeakerWithActiveUSB
) {
1404 // Set up initial audio devices.
1405 AudioNodeList audio_nodes
;
1406 audio_nodes
.push_back(kUSBHeadphone1
);
1407 audio_nodes
.push_back(kInternalSpeaker
);
1408 audio_nodes
.push_back(kInternalMic
);
1409 SetUpCrasAudioHandler(audio_nodes
);
1410 const size_t init_nodes_size
= audio_nodes
.size();
1412 // Verify the audio devices size.
1413 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1414 AudioDeviceList audio_devices
;
1415 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1416 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1418 // Verify the internal mic is selected as the active input.
1419 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1420 EXPECT_EQ(kInternalMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1421 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
1423 // Verify the USB headphone is selected as the active output.
1424 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1425 EXPECT_EQ(kUSBHeadphoneId1
,
1426 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1427 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1429 // Plug the headphone and auto-unplug internal speaker.
1430 audio_nodes
.clear();
1431 AudioNode
usb_headphone_node(kUSBHeadphone1
);
1432 usb_headphone_node
.active
= true;
1433 audio_nodes
.push_back(usb_headphone_node
);
1434 AudioNode
headphone_node(kHeadphone
);
1435 headphone_node
.plugged_time
= 1000;
1436 audio_nodes
.push_back(headphone_node
);
1437 AudioNode
internal_mic(kInternalMic
);
1438 internal_mic
.active
= true;
1439 audio_nodes
.push_back(internal_mic
);
1440 ChangeAudioNodes(audio_nodes
);
1442 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1443 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1444 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1445 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1447 // Verify the active output device is switched to headphone, and
1448 // an ActiveOutputChanged event is fired.
1449 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1450 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1451 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1453 // Unplug the headphone and internal speaker auto-plugs back.
1454 audio_nodes
.clear();
1455 audio_nodes
.push_back(kUSBHeadphone1
);
1456 AudioNode
internal_speaker_node(kInternalSpeaker
);
1457 internal_speaker_node
.plugged_time
= 2000;
1458 audio_nodes
.push_back(internal_speaker_node
);
1459 audio_nodes
.push_back(internal_mic
);
1460 ChangeAudioNodes(audio_nodes
);
1462 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1463 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1464 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1465 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1467 // Verify the active output device is switched back to USB, and
1468 // an ActiveOutputChanged event is fired.
1469 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
1470 EXPECT_EQ(kUSBHeadphone1
.id
,
1471 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1472 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1474 // Verify the active input device is not changed.
1475 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1476 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1479 TEST_F(CrasAudioHandlerTest
, PlugMicAutoUnplugInternalMicWithActiveUSB
) {
1480 // Set up initial audio devices.
1481 AudioNodeList audio_nodes
;
1482 audio_nodes
.push_back(kUSBHeadphone1
);
1483 audio_nodes
.push_back(kInternalSpeaker
);
1484 audio_nodes
.push_back(kUSBMic
);
1485 audio_nodes
.push_back(kInternalMic
);
1486 SetUpCrasAudioHandler(audio_nodes
);
1487 const size_t init_nodes_size
= audio_nodes
.size();
1489 // Verify the audio devices size.
1490 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1491 AudioDeviceList audio_devices
;
1492 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1493 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1495 // Verify the internal mic is selected as the active input.
1496 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1497 EXPECT_EQ(kUSBMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1498 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1500 // Verify the internal speaker is selected as the active output.
1501 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1502 EXPECT_EQ(kUSBHeadphoneId1
,
1503 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1504 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1506 // Plug the headphone and mic, auto-unplug internal mic and speaker.
1507 audio_nodes
.clear();
1508 AudioNode
usb_headphone_node(kUSBHeadphone1
);
1509 usb_headphone_node
.active
= true;
1510 audio_nodes
.push_back(usb_headphone_node
);
1511 AudioNode
headphone_node(kHeadphone
);
1512 headphone_node
.plugged_time
= 1000;
1513 audio_nodes
.push_back(headphone_node
);
1514 AudioNode
usb_mic(kUSBMic
);
1515 usb_mic
.active
= true;
1516 audio_nodes
.push_back(usb_mic
);
1517 AudioNode
mic_jack(kMicJack
);
1518 mic_jack
.plugged_time
= 1000;
1519 audio_nodes
.push_back(mic_jack
);
1520 ChangeAudioNodes(audio_nodes
);
1522 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1523 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1524 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1525 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1527 // Verify the active output device is switched to headphone, and
1528 // an ActiveOutputChanged event is fired.
1529 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1530 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1531 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1533 // Verify the active input device is switched to mic jack, and
1534 // an ActiveInputChanged event is fired.
1535 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
1536 EXPECT_EQ(kMicJack
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1537 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1539 // Unplug the headphone and internal speaker auto-plugs back.
1540 audio_nodes
.clear();
1541 audio_nodes
.push_back(kUSBHeadphone1
);
1542 AudioNode
internal_speaker_node(kInternalSpeaker
);
1543 internal_speaker_node
.plugged_time
= 2000;
1544 audio_nodes
.push_back(internal_speaker_node
);
1545 audio_nodes
.push_back(kUSBMic
);
1546 AudioNode
internal_mic(kInternalMic
);
1547 internal_mic
.plugged_time
= 2000;
1548 audio_nodes
.push_back(internal_mic
);
1549 ChangeAudioNodes(audio_nodes
);
1551 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1552 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1553 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1554 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1556 // Verify the active output device is switched back to USB, and
1557 // an ActiveOutputChanged event is fired.
1558 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
1559 EXPECT_EQ(kUSBHeadphone1
.id
,
1560 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1561 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1563 // Verify the active input device is switched back to USB mic, and
1564 // an ActiveInputChanged event is fired.
1565 EXPECT_EQ(2, test_observer_
->active_input_node_changed_count());
1566 EXPECT_EQ(kUSBMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1567 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1570 TEST_F(CrasAudioHandlerTest
, MultipleNodesChangedSignalsOnPlugInHeadphone
) {
1571 // Set up initial audio devices.
1572 AudioNodeList audio_nodes
;
1573 audio_nodes
.push_back(kInternalSpeaker
);
1574 audio_nodes
.push_back(kBluetoothHeadset
);
1575 SetUpCrasAudioHandler(audio_nodes
);
1576 const size_t init_nodes_size
= audio_nodes
.size();
1578 // Verify the audio devices size.
1579 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1580 AudioDeviceList audio_devices
;
1581 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1582 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1584 // Verify the bluetooth headset is selected as the active output.
1585 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1586 EXPECT_EQ(kBluetoothHeadsetId
,
1587 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1588 AudioDevice active_output
;
1590 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1591 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1593 // Plug in headphone, but fire NodesChanged signal twice.
1594 audio_nodes
.clear();
1595 audio_nodes
.push_back(kInternalSpeaker
);
1596 AudioNode
bluetooth_headset(kBluetoothHeadset
);
1597 bluetooth_headset
.plugged_time
= 1000;
1598 bluetooth_headset
.active
= true;
1599 audio_nodes
.push_back(bluetooth_headset
);
1600 AudioNode
headphone(kHeadphone
);
1601 headphone
.active
= false;
1602 headphone
.plugged_time
= 2000;
1603 audio_nodes
.push_back(headphone
);
1604 ChangeAudioNodes(audio_nodes
);
1605 ChangeAudioNodes(audio_nodes
);
1607 // Verify the active output device is set to headphone.
1608 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1609 EXPECT_LE(1, test_observer_
->active_output_node_changed_count());
1610 EXPECT_EQ(headphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1612 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1613 EXPECT_EQ(headphone
.id
, active_output
.id
);
1615 // Verfiy the audio devices data is consistent, i.e., the active output device
1616 // should be headphone.
1617 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1618 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
1619 for (size_t i
= 0; i
< audio_devices
.size(); ++i
) {
1620 if (audio_devices
[i
].id
== kInternalSpeaker
.id
)
1621 EXPECT_FALSE(audio_devices
[i
].active
);
1622 else if (audio_devices
[i
].id
== bluetooth_headset
.id
)
1623 EXPECT_FALSE(audio_devices
[i
].active
);
1624 else if (audio_devices
[i
].id
== headphone
.id
)
1625 EXPECT_TRUE(audio_devices
[i
].active
);
1631 TEST_F(CrasAudioHandlerTest
, MultipleNodesChangedSignalsOnPlugInUSBMic
) {
1632 // Set up initial audio devices.
1633 AudioNodeList audio_nodes
;
1634 audio_nodes
.push_back(kInternalMic
);
1635 SetUpCrasAudioHandler(audio_nodes
);
1636 const size_t init_nodes_size
= audio_nodes
.size();
1638 // Verify the audio devices size.
1639 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1640 AudioDeviceList audio_devices
;
1641 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1642 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1644 // Verify the internal mic is selected as the active output.
1645 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1646 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1647 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
1648 EXPECT_TRUE(audio_devices
[0].active
);
1650 // Plug in usb mic, but fire NodesChanged signal twice.
1651 audio_nodes
.clear();
1652 AudioNode
internal_mic(kInternalMic
);
1653 internal_mic
.active
= true;
1654 internal_mic
.plugged_time
= 1000;
1655 audio_nodes
.push_back(internal_mic
);
1656 AudioNode
usb_mic(kUSBMic
);
1657 usb_mic
.active
= false;
1658 usb_mic
.plugged_time
= 2000;
1659 audio_nodes
.push_back(usb_mic
);
1660 ChangeAudioNodes(audio_nodes
);
1661 ChangeAudioNodes(audio_nodes
);
1663 // Verify the active output device is set to headphone.
1664 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1665 EXPECT_LE(1, test_observer_
->active_input_node_changed_count());
1666 EXPECT_EQ(usb_mic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1667 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1669 // Verfiy the audio devices data is consistent, i.e., the active input device
1670 // should be usb mic.
1671 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1672 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
1673 for (size_t i
= 0; i
< audio_devices
.size(); ++i
) {
1674 if (audio_devices
[i
].id
== kInternalMic
.id
)
1675 EXPECT_FALSE(audio_devices
[i
].active
);
1676 else if (audio_devices
[i
].id
== usb_mic
.id
)
1677 EXPECT_TRUE(audio_devices
[i
].active
);
1683 // This is the case of crbug.com/291303.
1684 TEST_F(CrasAudioHandlerTest
, MultipleNodesChangedSignalsOnSystemBoot
) {
1685 // Set up audio handler with empty audio_nodes.
1686 AudioNodeList audio_nodes
;
1687 SetUpCrasAudioHandler(audio_nodes
);
1689 AudioNode
internal_speaker(kInternalSpeaker
);
1690 internal_speaker
.active
= false;
1691 AudioNode
headphone(kHeadphone
);
1692 headphone
.active
= false;
1693 AudioNode
internal_mic(kInternalMic
);
1694 internal_mic
.active
= false;
1695 audio_nodes
.push_back(internal_speaker
);
1696 audio_nodes
.push_back(headphone
);
1697 audio_nodes
.push_back(internal_mic
);
1698 const size_t init_nodes_size
= audio_nodes
.size();
1700 // Simulate AudioNodesChanged signal being fired twice during system boot.
1701 ChangeAudioNodes(audio_nodes
);
1702 ChangeAudioNodes(audio_nodes
);
1704 // Verify the active output device is set to headphone.
1705 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1706 EXPECT_LE(1, test_observer_
->active_output_node_changed_count());
1707 EXPECT_EQ(headphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1708 AudioDevice active_output
;
1710 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1711 EXPECT_EQ(headphone
.id
, active_output
.id
);
1713 // Verify the active input device id is set to internal mic.
1714 EXPECT_EQ(internal_mic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1716 // Verfiy the audio devices data is consistent, i.e., the active output device
1717 // should be headphone, and the active input device should internal mic.
1718 AudioDeviceList audio_devices
;
1719 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1720 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1721 for (size_t i
= 0; i
< audio_devices
.size(); ++i
) {
1722 if (audio_devices
[i
].id
== internal_speaker
.id
)
1723 EXPECT_FALSE(audio_devices
[i
].active
);
1724 else if (audio_devices
[i
].id
== headphone
.id
)
1725 EXPECT_TRUE(audio_devices
[i
].active
);
1726 else if (audio_devices
[i
].id
== internal_mic
.id
)
1727 EXPECT_TRUE(audio_devices
[i
].active
);
1733 // This is the case of crbug.com/448924.
1734 TEST_F(CrasAudioHandlerTest
,
1735 TwoNodesChangedSignalsForLosingTowNodesOnOneUnplug
) {
1736 // Set up audio handler with 4 audio_nodes.
1737 AudioNodeList audio_nodes
;
1738 AudioNode
internal_speaker(kInternalSpeaker
);
1739 internal_speaker
.active
= false;
1740 AudioNode
headphone(kHeadphone
);
1741 headphone
.active
= false;
1742 AudioNode
internal_mic(kInternalMic
);
1743 internal_mic
.active
= false;
1744 AudioNode
micJack(kMicJack
);
1745 micJack
.active
= false;
1746 audio_nodes
.push_back(internal_speaker
);
1747 audio_nodes
.push_back(headphone
);
1748 audio_nodes
.push_back(internal_mic
);
1749 audio_nodes
.push_back(micJack
);
1750 SetUpCrasAudioHandler(audio_nodes
);
1752 // Verify the audio devices size.
1753 AudioDeviceList audio_devices
;
1754 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1755 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
1757 // Verify the headphone has been selected as the active output.
1758 AudioDevice active_output
;
1760 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1761 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
1762 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1763 EXPECT_TRUE(active_output
.active
);
1764 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1766 // Verify the mic Jack has been selected as the active input.
1767 EXPECT_EQ(micJack
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1768 const AudioDevice
* active_input
= GetDeviceFromId(micJack
.id
);
1769 EXPECT_TRUE(active_input
->active
);
1770 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1772 // Simulate the nodes list in first NodesChanged signal, only headphone is
1773 // removed, other nodes remains the same.
1774 AudioNodeList changed_nodes_1
;
1775 internal_speaker
.active
= false;
1776 changed_nodes_1
.push_back(internal_speaker
);
1777 internal_mic
.active
= false;
1778 changed_nodes_1
.push_back(internal_mic
);
1779 micJack
.active
= true;
1780 changed_nodes_1
.push_back(micJack
);
1782 // Simulate the nodes list in second NodesChanged signal, the micJac is
1783 // removed, but the internal_mic is inactive, which does not reflect the
1784 // active status set from the first NodesChanged signal since this was sent
1785 // before cras receives the SetActiveOutputNode from the first NodesChanged
1787 AudioNodeList changed_nodes_2
;
1788 changed_nodes_2
.push_back(internal_speaker
);
1789 changed_nodes_2
.push_back(internal_mic
);
1791 // Simulate AudioNodesChanged signal being fired twice for unplug an audio
1792 // device with both input and output nodes on it.
1793 ChangeAudioNodes(changed_nodes_1
);
1794 ChangeAudioNodes(changed_nodes_2
);
1796 AudioDeviceList changed_devices
;
1797 cras_audio_handler_
->GetAudioDevices(&changed_devices
);
1798 EXPECT_EQ(2u, changed_devices
.size());
1800 // Verify the active output device is set to internal speaker.
1801 EXPECT_EQ(internal_speaker
.id
,
1802 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1804 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1805 EXPECT_EQ(internal_speaker
.id
, active_output
.id
);
1806 EXPECT_TRUE(active_output
.active
);
1808 // Verify the active input device id is set to internal mic.
1809 EXPECT_EQ(internal_mic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1810 const AudioDevice
* changed_active_input
= GetDeviceFromId(internal_mic
.id
);
1811 EXPECT_TRUE(changed_active_input
->active
);
1814 TEST_F(CrasAudioHandlerTest
, SetOutputMute
) {
1815 AudioNodeList audio_nodes
;
1816 audio_nodes
.push_back(kInternalSpeaker
);
1817 SetUpCrasAudioHandler(audio_nodes
);
1818 EXPECT_EQ(0, test_observer_
->output_mute_changed_count());
1821 cras_audio_handler_
->SetOutputMute(true);
1823 // Verify the output is muted, OnOutputMuteChanged event is fired,
1824 // and mute value is saved in the preferences.
1825 EXPECT_TRUE(cras_audio_handler_
->IsOutputMuted());
1826 EXPECT_EQ(1, test_observer_
->output_mute_changed_count());
1827 AudioDevice
speaker(kInternalSpeaker
);
1828 EXPECT_TRUE(audio_pref_handler_
->GetMuteValue(speaker
));
1830 // Unmute the device.
1831 cras_audio_handler_
->SetOutputMute(false);
1833 // Verify the output is unmuted, OnOutputMuteChanged event is fired,
1834 // and mute value is saved in the preferences.
1835 EXPECT_FALSE(cras_audio_handler_
->IsOutputMuted());
1836 EXPECT_EQ(2, test_observer_
->output_mute_changed_count());
1837 EXPECT_FALSE(audio_pref_handler_
->GetMuteValue(speaker
));
1840 TEST_F(CrasAudioHandlerTest
, SetInputMute
) {
1841 AudioNodeList audio_nodes
;
1842 audio_nodes
.push_back(kInternalMic
);
1843 SetUpCrasAudioHandler(audio_nodes
);
1844 EXPECT_EQ(0, test_observer_
->input_mute_changed_count());
1847 cras_audio_handler_
->SetInputMute(true);
1849 // Verify the input is muted, OnInputMuteChanged event is fired.
1850 EXPECT_TRUE(cras_audio_handler_
->IsInputMuted());
1851 EXPECT_EQ(1, test_observer_
->input_mute_changed_count());
1853 // Unmute the device.
1854 cras_audio_handler_
->SetInputMute(false);
1856 // Verify the input is unmuted, OnInputMuteChanged event is fired.
1857 EXPECT_FALSE(cras_audio_handler_
->IsInputMuted());
1858 EXPECT_EQ(2, test_observer_
->input_mute_changed_count());
1861 TEST_F(CrasAudioHandlerTest
, SetOutputVolumePercent
) {
1862 AudioNodeList audio_nodes
;
1863 audio_nodes
.push_back(kInternalSpeaker
);
1864 SetUpCrasAudioHandler(audio_nodes
);
1865 EXPECT_EQ(0, test_observer_
->output_volume_changed_count());
1867 cras_audio_handler_
->SetOutputVolumePercent(60);
1869 // Verify the output volume is changed to the designated value,
1870 // OnOutputNodeVolumeChanged event is fired, and the device volume value
1871 // is saved the preferences.
1872 const int kVolume
= 60;
1873 EXPECT_EQ(kVolume
, cras_audio_handler_
->GetOutputVolumePercent());
1874 EXPECT_EQ(1, test_observer_
->output_volume_changed_count());
1876 EXPECT_TRUE(cras_audio_handler_
->GetPrimaryActiveOutputDevice(&device
));
1877 EXPECT_EQ(device
.id
, kInternalSpeaker
.id
);
1878 EXPECT_EQ(kVolume
, audio_pref_handler_
->GetOutputVolumeValue(&device
));
1881 TEST_F(CrasAudioHandlerTest
, SetInputGainPercent
) {
1882 AudioNodeList audio_nodes
;
1883 audio_nodes
.push_back(kInternalMic
);
1884 SetUpCrasAudioHandler(audio_nodes
);
1885 EXPECT_EQ(0, test_observer_
->input_gain_changed_count());
1887 cras_audio_handler_
->SetInputGainPercent(60);
1889 // Verify the input gain changed to the designated value,
1890 // OnInputNodeGainChanged event is fired, and the device gain value
1891 // is saved in the preferences.
1892 const int kGain
= 60;
1893 EXPECT_EQ(kGain
, cras_audio_handler_
->GetInputGainPercent());
1894 EXPECT_EQ(1, test_observer_
->input_gain_changed_count());
1895 AudioDevice
internal_mic(kInternalMic
);
1896 EXPECT_EQ(kGain
, audio_pref_handler_
->GetInputGainValue(&internal_mic
));
1899 TEST_F(CrasAudioHandlerTest
, SetMuteForDevice
) {
1900 AudioNodeList audio_nodes
;
1901 audio_nodes
.push_back(kInternalSpeaker
);
1902 audio_nodes
.push_back(kHeadphone
);
1903 audio_nodes
.push_back(kInternalMic
);
1904 audio_nodes
.push_back(kUSBMic
);
1905 SetUpCrasAudioHandler(audio_nodes
);
1907 // Mute the active output device.
1908 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1909 cras_audio_handler_
->SetMuteForDevice(kHeadphone
.id
, true);
1911 // Verify the headphone is muted and mute value is saved in the preferences.
1912 EXPECT_TRUE(cras_audio_handler_
->IsOutputMutedForDevice(kHeadphone
.id
));
1913 AudioDevice
headphone(kHeadphone
);
1914 EXPECT_TRUE(audio_pref_handler_
->GetMuteValue(headphone
));
1916 // Mute the non-active output device.
1917 cras_audio_handler_
->SetMuteForDevice(kInternalSpeaker
.id
, true);
1919 // Verify the internal speaker is muted and mute value is saved in the
1921 EXPECT_TRUE(cras_audio_handler_
->IsOutputMutedForDevice(kInternalSpeaker
.id
));
1922 AudioDevice
internal_speaker(kInternalSpeaker
);
1923 EXPECT_TRUE(audio_pref_handler_
->GetMuteValue(internal_speaker
));
1925 // Mute the active input device.
1926 EXPECT_EQ(kUSBMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1927 cras_audio_handler_
->SetMuteForDevice(kUSBMic
.id
, true);
1929 // Verify the USB Mic is muted.
1930 EXPECT_TRUE(cras_audio_handler_
->IsInputMutedForDevice(kUSBMic
.id
));
1932 // Mute the non-active input device should be a no-op, see crbug.com/365050.
1933 cras_audio_handler_
->SetMuteForDevice(kInternalMic
.id
, true);
1935 // Verify IsInputMutedForDevice returns false for non-active input device.
1936 EXPECT_FALSE(cras_audio_handler_
->IsInputMutedForDevice(kInternalMic
.id
));
1939 TEST_F(CrasAudioHandlerTest
, SetVolumeGainPercentForDevice
) {
1940 AudioNodeList audio_nodes
;
1941 audio_nodes
.push_back(kInternalSpeaker
);
1942 audio_nodes
.push_back(kHeadphone
);
1943 audio_nodes
.push_back(kInternalMic
);
1944 audio_nodes
.push_back(kUSBMic
);
1945 SetUpCrasAudioHandler(audio_nodes
);
1947 // Set volume percent for active output device.
1948 const int kHeadphoneVolume
= 30;
1949 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1950 cras_audio_handler_
->SetVolumeGainPercentForDevice(kHeadphone
.id
,
1953 // Verify the volume percent of headphone is set, and saved in preferences.
1954 EXPECT_EQ(kHeadphoneVolume
,
1955 cras_audio_handler_
->GetOutputVolumePercentForDevice(
1957 AudioDevice
headphone(kHeadphone
);
1958 EXPECT_EQ(kHeadphoneVolume
,
1959 audio_pref_handler_
->GetOutputVolumeValue(&headphone
));
1961 // Set volume percent for non-active output device.
1962 const int kSpeakerVolume
= 60;
1963 cras_audio_handler_
->SetVolumeGainPercentForDevice(kInternalSpeaker
.id
,
1966 // Verify the volume percent of speaker is set, and saved in preferences.
1967 EXPECT_EQ(kSpeakerVolume
,
1968 cras_audio_handler_
->GetOutputVolumePercentForDevice(
1969 kInternalSpeaker
.id
));
1970 AudioDevice
speaker(kInternalSpeaker
);
1971 EXPECT_EQ(kSpeakerVolume
,
1972 audio_pref_handler_
->GetOutputVolumeValue(&speaker
));
1974 // Set gain percent for active input device.
1975 const int kUSBMicGain
= 30;
1976 EXPECT_EQ(kUSBMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1977 cras_audio_handler_
->SetVolumeGainPercentForDevice(kUSBMic
.id
,
1980 // Verify the gain percent of USB mic is set, and saved in preferences.
1981 EXPECT_EQ(kUSBMicGain
,
1982 cras_audio_handler_
->GetOutputVolumePercentForDevice(kUSBMic
.id
));
1983 AudioDevice
usb_mic(kHeadphone
);
1984 EXPECT_EQ(kUSBMicGain
,
1985 audio_pref_handler_
->GetInputGainValue(&usb_mic
));
1987 // Set gain percent for non-active input device.
1988 const int kInternalMicGain
= 60;
1989 cras_audio_handler_
->SetVolumeGainPercentForDevice(kInternalMic
.id
,
1992 // Verify the gain percent of internal mic is set, and saved in preferences.
1993 EXPECT_EQ(kInternalMicGain
,
1994 cras_audio_handler_
->GetOutputVolumePercentForDevice(
1996 AudioDevice
internal_mic(kInternalMic
);
1997 EXPECT_EQ(kInternalMicGain
,
1998 audio_pref_handler_
->GetInputGainValue(&internal_mic
));
2001 TEST_F(CrasAudioHandlerTest
, HandleOtherDeviceType
) {
2002 const size_t kNumValidAudioDevices
= 4;
2003 AudioNodeList audio_nodes
;
2004 audio_nodes
.push_back(kInternalSpeaker
);
2005 audio_nodes
.push_back(kOtherTypeOutput
);
2006 audio_nodes
.push_back(kInternalMic
);
2007 audio_nodes
.push_back(kOtherTypeInput
);
2008 SetUpCrasAudioHandler(audio_nodes
);
2010 // Verify the audio devices size.
2011 AudioDeviceList audio_devices
;
2012 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2013 EXPECT_EQ(kNumValidAudioDevices
, audio_devices
.size());
2015 // Verify the internal speaker has been selected as the active output,
2016 // and the output device with some randown unknown type is handled gracefully.
2017 AudioDevice active_output
;
2019 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
2020 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
2021 EXPECT_EQ(kInternalSpeaker
.id
,
2022 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2023 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
2025 // Ensure the internal microphone has been selected as the active input,
2026 // and the input device with some random unknown type is handled gracefully.
2027 AudioDevice active_input
;
2028 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
2029 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
2032 TEST_F(CrasAudioHandlerTest
, ChangeActiveNodesHotrodInit
) {
2033 AudioNodeList audio_nodes
;
2034 audio_nodes
.push_back(kHDMIOutput
);
2035 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2036 audio_nodes
.push_back(kUSBJabraSpeakerOutput2
);
2037 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2038 audio_nodes
.push_back(kUSBJabraSpeakerInput2
);
2039 audio_nodes
.push_back(kUSBCameraInput
);
2040 SetUpCrasAudioHandler(audio_nodes
);
2042 // Verify the audio devices size.
2043 AudioDeviceList audio_devices
;
2044 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2045 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2047 // Verify only the 1st jabra speaker's output and input are selected as active
2048 // nodes by CrasAudioHandler.
2049 AudioDevice active_output
;
2051 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
2052 EXPECT_EQ(2, GetActiveDeviceCount());
2053 AudioDevice primary_active_device
;
2054 EXPECT_TRUE(cras_audio_handler_
->GetPrimaryActiveOutputDevice(
2055 &primary_active_device
));
2056 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
, primary_active_device
.id
);
2057 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2058 cras_audio_handler_
->GetPrimaryActiveInputNode());
2060 // Set both jabra speakers's input and output nodes to active, this simulate
2061 // the call sent by hotrod initialization process.
2062 test_observer_
->reset_active_output_node_changed_count();
2063 test_observer_
->reset_active_input_node_changed_count();
2064 CrasAudioHandler::NodeIdList active_nodes
;
2065 active_nodes
.push_back(kUSBJabraSpeakerOutput1
.id
);
2066 active_nodes
.push_back(kUSBJabraSpeakerOutput2
.id
);
2067 active_nodes
.push_back(kUSBJabraSpeakerInput1
.id
);
2068 active_nodes
.push_back(kUSBJabraSpeakerInput2
.id
);
2069 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2071 // Verify both jabra speakers' input/output nodes are made active.
2072 // num_active_nodes = GetActiveDeviceCount();
2073 EXPECT_EQ(4, GetActiveDeviceCount());
2074 const AudioDevice
* active_output_1
=
2075 GetDeviceFromId(kUSBJabraSpeakerOutput1
.id
);
2076 EXPECT_TRUE(active_output_1
->active
);
2077 const AudioDevice
* active_output_2
=
2078 GetDeviceFromId(kUSBJabraSpeakerOutput2
.id
);
2079 EXPECT_TRUE(active_output_2
->active
);
2080 EXPECT_TRUE(cras_audio_handler_
->GetPrimaryActiveOutputDevice(
2081 &primary_active_device
));
2082 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
, primary_active_device
.id
);
2083 const AudioDevice
* active_input_1
=
2084 GetDeviceFromId(kUSBJabraSpeakerInput1
.id
);
2085 EXPECT_TRUE(active_input_1
->active
);
2086 const AudioDevice
* active_input_2
=
2087 GetDeviceFromId(kUSBJabraSpeakerInput2
.id
);
2088 EXPECT_TRUE(active_input_2
->active
);
2089 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2090 cras_audio_handler_
->GetPrimaryActiveInputNode());
2092 // Verify only 1 ActiveOutputNodeChanged notification has been sent out
2093 // by calling ChangeActiveNodes.
2094 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
2095 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
2097 // Verify all active devices are the not muted and their volume values are
2099 EXPECT_FALSE(cras_audio_handler_
->IsOutputMuted());
2101 cras_audio_handler_
->IsOutputMutedForDevice(kUSBJabraSpeakerOutput1
.id
));
2103 cras_audio_handler_
->IsOutputMutedForDevice(kUSBJabraSpeakerOutput2
.id
));
2104 EXPECT_EQ(cras_audio_handler_
->GetOutputVolumePercent(),
2105 cras_audio_handler_
->GetOutputVolumePercentForDevice(
2106 kUSBJabraSpeakerOutput1
.id
));
2107 EXPECT_EQ(cras_audio_handler_
->GetOutputVolumePercent(),
2108 cras_audio_handler_
->GetOutputVolumePercentForDevice(
2109 kUSBJabraSpeakerOutput2
.id
));
2111 // Adjust the volume of output devices, verify all active nodes are set to
2113 cras_audio_handler_
->SetOutputVolumePercent(25);
2114 EXPECT_EQ(25, cras_audio_handler_
->GetOutputVolumePercent());
2115 EXPECT_EQ(25, cras_audio_handler_
->GetOutputVolumePercentForDevice(
2116 kUSBJabraSpeakerOutput1
.id
));
2117 EXPECT_EQ(25, cras_audio_handler_
->GetOutputVolumePercentForDevice(
2118 kUSBJabraSpeakerOutput2
.id
));
2121 TEST_F(CrasAudioHandlerTest
, ChangeActiveNodesHotrodInitWithCameraInputActive
) {
2122 AudioNodeList audio_nodes
;
2123 audio_nodes
.push_back(kHDMIOutput
);
2124 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2125 audio_nodes
.push_back(kUSBJabraSpeakerOutput2
);
2126 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2127 audio_nodes
.push_back(kUSBJabraSpeakerInput2
);
2128 // Make the camera input to be plugged in later than jabra's input.
2129 AudioNode
usb_camera(kUSBCameraInput
);
2130 usb_camera
.plugged_time
= 10000000;
2131 audio_nodes
.push_back(usb_camera
);
2132 SetUpCrasAudioHandler(audio_nodes
);
2134 // Verify the audio devices size.
2135 AudioDeviceList audio_devices
;
2136 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2137 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2139 // Verify the 1st jabra speaker's output is selected as active output
2140 // node and camera's input is selected active input by CrasAudioHandler.
2141 EXPECT_EQ(2, GetActiveDeviceCount());
2142 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2143 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2144 EXPECT_EQ(kUSBCameraInput
.id
,
2145 cras_audio_handler_
->GetPrimaryActiveInputNode());
2147 // Set both jabra speakers's input and output nodes to active, this simulates
2148 // the call sent by hotrod initialization process.
2149 test_observer_
->reset_active_output_node_changed_count();
2150 test_observer_
->reset_active_input_node_changed_count();
2151 CrasAudioHandler::NodeIdList active_nodes
;
2152 active_nodes
.push_back(kUSBJabraSpeakerOutput1
.id
);
2153 active_nodes
.push_back(kUSBJabraSpeakerOutput2
.id
);
2154 active_nodes
.push_back(kUSBJabraSpeakerInput1
.id
);
2155 active_nodes
.push_back(kUSBJabraSpeakerInput2
.id
);
2156 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2158 // Verify both jabra speakers' input/output nodes are made active.
2159 // num_active_nodes = GetActiveDeviceCount();
2160 EXPECT_EQ(4, GetActiveDeviceCount());
2161 const AudioDevice
* active_output_1
=
2162 GetDeviceFromId(kUSBJabraSpeakerOutput1
.id
);
2163 EXPECT_TRUE(active_output_1
->active
);
2164 const AudioDevice
* active_output_2
=
2165 GetDeviceFromId(kUSBJabraSpeakerOutput2
.id
);
2166 EXPECT_TRUE(active_output_2
->active
);
2167 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2168 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2169 const AudioDevice
* active_input_1
=
2170 GetDeviceFromId(kUSBJabraSpeakerInput1
.id
);
2171 EXPECT_TRUE(active_input_1
->active
);
2172 const AudioDevice
* active_input_2
=
2173 GetDeviceFromId(kUSBJabraSpeakerInput2
.id
);
2174 EXPECT_TRUE(active_input_2
->active
);
2175 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2176 cras_audio_handler_
->GetPrimaryActiveInputNode());
2178 // Verify only 1 ActiveOutputNodeChanged notification has been sent out
2179 // by calling ChangeActiveNodes.
2180 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
2181 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
2184 TEST_F(CrasAudioHandlerTest
, ChangeActiveNodesWithFewerActives
) {
2185 AudioNodeList audio_nodes
;
2186 audio_nodes
.push_back(kHDMIOutput
);
2187 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2188 audio_nodes
.push_back(kUSBJabraSpeakerOutput2
);
2189 SetUpCrasAudioHandler(audio_nodes
);
2191 // Verify the audio devices size.
2192 AudioDeviceList audio_devices
;
2193 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2194 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2196 // Set all three nodes to be active.
2197 CrasAudioHandler::NodeIdList active_nodes
;
2198 active_nodes
.push_back(kHDMIOutput
.id
);
2199 active_nodes
.push_back(kUSBJabraSpeakerOutput1
.id
);
2200 active_nodes
.push_back(kUSBJabraSpeakerOutput2
.id
);
2201 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2203 // Verify all three nodes are active.
2204 EXPECT_EQ(3, GetActiveDeviceCount());
2205 const AudioDevice
* active_output_1
= GetDeviceFromId(kHDMIOutput
.id
);
2206 EXPECT_TRUE(active_output_1
->active
);
2207 const AudioDevice
* active_output_2
=
2208 GetDeviceFromId(kUSBJabraSpeakerOutput1
.id
);
2209 EXPECT_TRUE(active_output_2
->active
);
2210 const AudioDevice
* active_output_3
=
2211 GetDeviceFromId(kUSBJabraSpeakerOutput2
.id
);
2212 EXPECT_TRUE(active_output_3
->active
);
2214 // Now call ChangeActiveDevices with only 2 nodes.
2215 active_nodes
.clear();
2216 active_nodes
.push_back(kUSBJabraSpeakerOutput1
.id
);
2217 active_nodes
.push_back(kUSBJabraSpeakerOutput2
.id
);
2218 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2220 // Verify only 2 nodes are active.
2221 EXPECT_EQ(2, GetActiveDeviceCount());
2222 const AudioDevice
* output_1
= GetDeviceFromId(kHDMIOutput
.id
);
2223 EXPECT_FALSE(output_1
->active
);
2224 const AudioDevice
* output_2
= GetDeviceFromId(kUSBJabraSpeakerOutput1
.id
);
2225 EXPECT_TRUE(output_2
->active
);
2226 const AudioDevice
* output_3
= GetDeviceFromId(kUSBJabraSpeakerOutput2
.id
);
2227 EXPECT_TRUE(output_3
->active
);
2230 TEST_F(CrasAudioHandlerTest
, HotrodInitWithSingleJabra
) {
2231 // Simulates the hotrod initializated with a single jabra device and
2232 // CrasAudioHandler selected jabra input/output as active devices.
2233 AudioNodeList audio_nodes
;
2234 audio_nodes
.push_back(kHDMIOutput
);
2235 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2236 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2237 audio_nodes
.push_back(kUSBCameraInput
);
2238 SetUpCrasAudioHandler(audio_nodes
);
2240 // Verify the audio devices size.
2241 AudioDeviceList audio_devices
;
2242 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2243 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2245 // Verify the jabra speaker's output and input are selected as active nodes
2246 // by CrasAudioHandler.
2247 EXPECT_EQ(2, GetActiveDeviceCount());
2248 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2249 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2250 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2251 cras_audio_handler_
->GetPrimaryActiveInputNode());
2254 TEST_F(CrasAudioHandlerTest
,
2255 ChangeActiveNodesHotrodInitWithSingleJabraCameraPlugInLater
) {
2256 AudioNodeList audio_nodes
;
2257 audio_nodes
.push_back(kHDMIOutput
);
2258 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2259 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2260 AudioNode
usb_camera(kUSBCameraInput
);
2261 usb_camera
.plugged_time
= 10000000;
2262 audio_nodes
.push_back(usb_camera
);
2263 SetUpCrasAudioHandler(audio_nodes
);
2265 // Verify the audio devices size.
2266 AudioDeviceList audio_devices
;
2267 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2268 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2270 // Verify the jabra speaker's output is selected as active output, and
2271 // camera's input is selected as active input by CrasAudioHandler
2272 EXPECT_EQ(2, GetActiveDeviceCount());
2273 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2274 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2275 EXPECT_EQ(kUSBCameraInput
.id
,
2276 cras_audio_handler_
->GetPrimaryActiveInputNode());
2278 // Simulate hotrod app call to set jabra input as active device with only
2279 // jabra input node in the active node list, which does not conform to the
2280 // new SetActiveDevices protocol, but just show we can still handle it if
2282 CrasAudioHandler::NodeIdList active_nodes
;
2283 active_nodes
.push_back(kUSBJabraSpeakerOutput1
.id
);
2284 active_nodes
.push_back(kUSBJabraSpeakerInput1
.id
);
2285 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2287 // Verify the jabra speaker's output is selected as active output, and
2288 // jabra's input is selected as active input.
2289 EXPECT_EQ(2, GetActiveDeviceCount());
2290 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2291 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2292 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2293 cras_audio_handler_
->GetPrimaryActiveInputNode());
2296 TEST_F(CrasAudioHandlerTest
,
2297 ChangeActiveNodesHotrodInitWithSingleJabraCameraPlugInLaterOldCall
) {
2298 AudioNodeList audio_nodes
;
2299 audio_nodes
.push_back(kHDMIOutput
);
2300 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2301 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2302 AudioNode
usb_camera(kUSBCameraInput
);
2303 usb_camera
.plugged_time
= 10000000;
2304 audio_nodes
.push_back(usb_camera
);
2305 SetUpCrasAudioHandler(audio_nodes
);
2307 // Verify the audio devices size.
2308 AudioDeviceList audio_devices
;
2309 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2310 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2312 // Verify the jabra speaker's output is selected as active output, and
2313 // camera's input is selected as active input by CrasAudioHandler
2314 EXPECT_EQ(2, GetActiveDeviceCount());
2315 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2316 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2317 EXPECT_EQ(kUSBCameraInput
.id
,
2318 cras_audio_handler_
->GetPrimaryActiveInputNode());
2320 // Simulate hotrod app call to set jabra input as active device with only
2321 // jabra input node in the active node list, which does not conform to the
2322 // new SetActiveDevices protocol, but just show we can still handle it if
2324 CrasAudioHandler::NodeIdList active_nodes
;
2325 active_nodes
.push_back(kUSBJabraSpeakerInput1
.id
);
2326 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2328 // Verify the jabra speaker's output is selected as active output, and
2329 // jabra's input is selected as active input.
2330 EXPECT_EQ(2, GetActiveDeviceCount());
2331 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2332 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2333 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2334 cras_audio_handler_
->GetPrimaryActiveInputNode());
2337 TEST_F(CrasAudioHandlerTest
,
2338 ChangeActiveNodesHotrodInitWithSingleJabraChangeOutput
) {
2339 AudioNodeList audio_nodes
;
2340 audio_nodes
.push_back(kHDMIOutput
);
2341 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2342 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2343 audio_nodes
.push_back(kUSBCameraInput
);
2344 SetUpCrasAudioHandler(audio_nodes
);
2346 // Verify the audio devices size.
2347 AudioDeviceList audio_devices
;
2348 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2349 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2351 // Verify the jabra speaker's output and input are selected as active output
2352 // by CrasAudioHandler.
2353 EXPECT_EQ(2, GetActiveDeviceCount());
2354 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2355 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2356 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2357 cras_audio_handler_
->GetPrimaryActiveInputNode());
2359 // Simulate hotrod app call SetActiveDevices to change active output
2360 // with only complete list of active nodes passed in, which is the new
2361 // way of hotrod app.
2362 CrasAudioHandler::NodeIdList active_nodes
;
2363 active_nodes
.push_back(kHDMIOutput
.id
);
2364 active_nodes
.push_back(kUSBJabraSpeakerInput1
.id
);
2365 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2367 // Verify the jabra speaker's output is selected as active output, and
2368 // jabra's input is selected as active input.
2369 EXPECT_EQ(2, GetActiveDeviceCount());
2370 EXPECT_EQ(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2371 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2372 cras_audio_handler_
->GetPrimaryActiveInputNode());
2375 TEST_F(CrasAudioHandlerTest
,
2376 ChangeActiveNodesHotrodInitWithSingleJabraChangeOutputOldCall
) {
2377 AudioNodeList audio_nodes
;
2378 audio_nodes
.push_back(kHDMIOutput
);
2379 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2380 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2381 audio_nodes
.push_back(kUSBCameraInput
);
2382 SetUpCrasAudioHandler(audio_nodes
);
2384 // Verify the audio devices size.
2385 AudioDeviceList audio_devices
;
2386 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2387 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2389 // Verify the jabra speaker's output and input are selected as active output
2390 // by CrasAudioHandler.
2391 EXPECT_EQ(2, GetActiveDeviceCount());
2392 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2393 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2394 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2395 cras_audio_handler_
->GetPrimaryActiveInputNode());
2397 // Simulate hotrod app call SetActiveDevices to change active output
2398 // with only a single active output nodes passed in, which is the old
2399 // way of hotrod app.
2400 CrasAudioHandler::NodeIdList active_nodes
;
2401 active_nodes
.push_back(kHDMIOutput
.id
);
2402 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2404 // Verify the jabra speaker's output is selected as active output, and
2405 // jabra's input is selected as active input.
2406 EXPECT_EQ(2, GetActiveDeviceCount());
2407 EXPECT_EQ(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2408 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2409 cras_audio_handler_
->GetPrimaryActiveInputNode());
2412 TEST_F(CrasAudioHandlerTest
, NoMoreAudioInputDevices
) {
2413 // Some device like chromebox does not have the internal input device. The
2414 // active devices should be reset when the user plugs a device and then
2415 // unplugs it to such device.
2417 AudioNodeList audio_nodes
;
2418 audio_nodes
.push_back(kInternalSpeaker
);
2419 SetUpCrasAudioHandler(audio_nodes
);
2421 EXPECT_EQ(0ULL, cras_audio_handler_
->GetPrimaryActiveInputNode());
2423 audio_nodes
.push_back(kMicJack
);
2424 ChangeAudioNodes(audio_nodes
);
2426 EXPECT_EQ(kMicJack
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
2427 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
2428 test_observer_
->reset_active_input_node_changed_count();
2430 audio_nodes
.pop_back();
2431 ChangeAudioNodes(audio_nodes
);
2432 EXPECT_EQ(0ULL, cras_audio_handler_
->GetPrimaryActiveInputNode());
2433 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
2436 // Test the case in which an HDMI output is plugged in with other higher
2438 // output devices already plugged and user has manually selected an active
2440 // The hotplug of hdmi output should not change user's selection of active
2442 // crbug.com/447826.
2443 TEST_F(CrasAudioHandlerTest
, HotPlugHDMINotChangeActiveOutput
) {
2444 AudioNodeList audio_nodes
;
2445 AudioNode
internal_speaker(kInternalSpeaker
);
2446 audio_nodes
.push_back(internal_speaker
);
2447 AudioNode
usb_headset(kUSBHeadphone1
);
2448 usb_headset
.plugged_time
= 80000000;
2449 audio_nodes
.push_back(usb_headset
);
2450 SetUpCrasAudioHandler(audio_nodes
);
2452 // Verify the audio devices size.
2453 AudioDeviceList audio_devices
;
2454 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2455 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2457 // Verify the USB headset is selected as active output by default.
2458 EXPECT_EQ(usb_headset
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2460 // Manually set the active output to internal speaker.
2461 AudioDevice
internal_output(kInternalSpeaker
);
2462 cras_audio_handler_
->SwitchToDevice(internal_output
, true);
2464 // Verify the active output is switched to internal speaker.
2465 EXPECT_EQ(internal_speaker
.id
,
2466 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2467 EXPECT_LT(kInternalSpeaker
.plugged_time
, usb_headset
.plugged_time
);
2468 const AudioDevice
* usb_device
= GetDeviceFromId(usb_headset
.id
);
2469 EXPECT_FALSE(usb_device
->active
);
2471 // Plug in HDMI output.
2472 audio_nodes
.clear();
2473 internal_speaker
.active
= true;
2474 audio_nodes
.push_back(internal_speaker
);
2475 usb_headset
.active
= false;
2476 audio_nodes
.push_back(usb_headset
);
2477 AudioNode
hdmi(kHDMIOutput
);
2478 hdmi
.plugged_time
= 90000000;
2479 audio_nodes
.push_back(hdmi
);
2480 ChangeAudioNodes(audio_nodes
);
2482 // The active output should not change.
2483 EXPECT_EQ(kInternalSpeaker
.id
,
2484 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2487 // Test the case in which the active device was set to inactive from cras after
2488 // resuming from suspension state. See crbug.com/478968.
2489 TEST_F(CrasAudioHandlerTest
, ActiveNodeLostAfterResume
) {
2490 AudioNodeList audio_nodes
;
2491 EXPECT_FALSE(kHeadphone
.active
);
2492 audio_nodes
.push_back(kHeadphone
);
2493 EXPECT_FALSE(kHDMIOutput
.active
);
2494 audio_nodes
.push_back(kHDMIOutput
);
2495 SetUpCrasAudioHandler(audio_nodes
);
2497 // Verify the headphone is selected as the active output.
2498 AudioDeviceList audio_devices
;
2499 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2500 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2501 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2502 const AudioDevice
* active_headphone
= GetDeviceFromId(kHeadphone
.id
);
2503 EXPECT_EQ(kHeadphone
.id
, active_headphone
->id
);
2504 EXPECT_TRUE(active_headphone
->active
);
2506 // Simulate NodesChanged signal with headphone turning into inactive state,
2507 // and HDMI node removed.
2508 audio_nodes
.clear();
2509 audio_nodes
.push_back(kHeadphone
);
2510 ChangeAudioNodes(audio_nodes
);
2512 // Verify the headphone is set to active again.
2513 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2514 const AudioDevice
* headphone_resumed
= GetDeviceFromId(kHeadphone
.id
);
2515 EXPECT_EQ(kHeadphone
.id
, headphone_resumed
->id
);
2516 EXPECT_TRUE(headphone_resumed
->active
);
2519 // Test the case in which there are two NodesChanged signal for discovering
2520 // output devices, and there is race between NodesChange and SetActiveOutput
2521 // during this process. See crbug.com/478968.
2522 TEST_F(CrasAudioHandlerTest
, ActiveNodeLostDuringLoginSession
) {
2523 AudioNodeList audio_nodes
;
2524 EXPECT_FALSE(kHeadphone
.active
);
2525 audio_nodes
.push_back(kHeadphone
);
2526 SetUpCrasAudioHandler(audio_nodes
);
2528 // Verify the headphone is selected as the active output.
2529 AudioDeviceList audio_devices
;
2530 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2531 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2532 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2533 const AudioDevice
* active_headphone
= GetDeviceFromId(kHeadphone
.id
);
2534 EXPECT_EQ(kHeadphone
.id
, active_headphone
->id
);
2535 EXPECT_TRUE(active_headphone
->active
);
2537 // Simulate NodesChanged signal with headphone turning into inactive state,
2538 // and add a new HDMI output node.
2539 audio_nodes
.clear();
2540 audio_nodes
.push_back(kHeadphone
);
2541 audio_nodes
.push_back(kHDMIOutput
);
2542 ChangeAudioNodes(audio_nodes
);
2544 // Verify the headphone is set to active again.
2545 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2546 const AudioDevice
* headphone_resumed
= GetDeviceFromId(kHeadphone
.id
);
2547 EXPECT_EQ(kHeadphone
.id
, headphone_resumed
->id
);
2548 EXPECT_TRUE(headphone_resumed
->active
);
2551 // This test HDMI output rediscovering case in crbug.com/503667.
2552 TEST_F(CrasAudioHandlerTest
, HDMIOutputRediscover
) {
2553 AudioNodeList audio_nodes
;
2554 audio_nodes
.push_back(kInternalSpeaker
);
2555 audio_nodes
.push_back(kHDMIOutput
);
2556 SetUpCrasAudioHandler(audio_nodes
);
2558 // Verify the HDMI device has been selected as the active output, and audio
2559 // output is not muted.
2560 AudioDevice active_output
;
2562 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
2563 EXPECT_EQ(kHDMIOutput
.id
, active_output
.id
);
2564 EXPECT_EQ(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2565 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
2566 EXPECT_FALSE(cras_audio_handler_
->IsOutputMuted());
2568 // Trigger HDMI rediscovering grace period, and remove the HDMI node.
2569 const int grace_period_in_ms
= 200;
2570 SetHDMIRediscoverGracePeriodDuration(grace_period_in_ms
);
2571 SetActiveHDMIRediscover();
2572 AudioNodeList audio_nodes_lost_hdmi
;
2573 audio_nodes_lost_hdmi
.push_back(kInternalSpeaker
);
2574 ChangeAudioNodes(audio_nodes_lost_hdmi
);
2576 // Verify the active output is switched to internal speaker, it is not muted
2577 // by preference, but the system output is muted during the grace period.
2579 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
2580 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
2582 cras_audio_handler_
->IsOutputMutedForDevice(kInternalSpeaker
.id
));
2583 EXPECT_TRUE(cras_audio_handler_
->IsOutputMuted());
2585 // Re-attach the HDMI device after a little delay.
2586 HDMIRediscoverWaiter
waiter(this, grace_period_in_ms
);
2587 waiter
.WaitUntilTimeOut(grace_period_in_ms
/ 4);
2588 ChangeAudioNodes(audio_nodes
);
2590 // After HDMI re-discover grace period, verify HDMI output is selected as the
2591 // active device and not muted.
2592 waiter
.WaitUntilHDMIRediscoverGracePeriodEnd();
2594 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
2595 EXPECT_EQ(kHDMIOutput
.id
, active_output
.id
);
2596 EXPECT_EQ(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2597 EXPECT_FALSE(cras_audio_handler_
->IsOutputMuted());
2600 } // namespace chromeos