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"
7 #include "base/memory/ref_counted.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/values.h"
11 #include "chromeos/audio/audio_devices_pref_handler_stub.h"
12 #include "chromeos/dbus/audio_node.h"
13 #include "chromeos/dbus/dbus_thread_manager.h"
14 #include "chromeos/dbus/fake_cras_audio_client.h"
15 #include "testing/gtest/include/gtest/gtest.h"
19 const uint64 kInternalSpeakerId
= 10001;
20 const uint64 kHeadphoneId
= 10002;
21 const uint64 kInternalMicId
= 10003;
22 const uint64 kUSBMicId
= 10004;
23 const uint64 kBluetoothHeadsetId
= 10005;
24 const uint64 kHDMIOutputId
= 10006;
25 const uint64 kUSBHeadphoneId1
= 10007;
26 const uint64 kUSBHeadphoneId2
= 10008;
27 const uint64 kMicJackId
= 10009;
28 const uint64 kKeyboardMicId
= 10010;
29 const uint64 kOtherTypeOutputId
= 90001;
30 const uint64 kOtherTypeInputId
= 90002;
31 const uint64 kUSBJabraSpeakerOutputId1
= 90003;
32 const uint64 kUSBJabraSpeakerOutputId2
= 90004;
33 const uint64 kUSBJabraSpeakerInputId1
= 90005;
34 const uint64 kUSBJabraSpeakerInputId2
= 90006;
35 const uint64 kUSBCameraInputId
= 90007;
37 const AudioNode
kInternalSpeaker(
47 const AudioNode
kHeadphone(
57 const AudioNode
kInternalMic(
67 const AudioNode
kMicJack(
77 const AudioNode
kUSBMic(
87 const AudioNode
kKeyboardMic(
97 const AudioNode
kOtherTypeOutput(
102 "Other Type Output Device",
107 const AudioNode
kOtherTypeInput(
112 "Other Type Input Device",
117 const AudioNode
kBluetoothHeadset (
122 "Bluetooth Headset 1",
127 const AudioNode
kHDMIOutput (
137 const AudioNode
kUSBHeadphone1 (
147 const AudioNode
kUSBHeadphone2 (
157 const AudioNode
kUSBJabraSpeakerOutput1(false,
158 kUSBJabraSpeakerOutputId1
,
165 const AudioNode
kUSBJabraSpeakerOutput2(false,
166 kUSBJabraSpeakerOutputId2
,
173 const AudioNode
kUSBJabraSpeakerInput1(true,
174 kUSBJabraSpeakerInputId1
,
181 const AudioNode
kUSBJabraSpeakerInput2(true,
182 kUSBJabraSpeakerInputId2
,
189 const AudioNode
kUSBCameraInput(true,
197 class TestObserver
: public chromeos::CrasAudioHandler::AudioObserver
{
199 TestObserver() : active_output_node_changed_count_(0),
200 active_input_node_changed_count_(0),
201 audio_nodes_changed_count_(0),
202 output_mute_changed_count_(0),
203 input_mute_changed_count_(0),
204 output_volume_changed_count_(0),
205 input_gain_changed_count_(0) {
208 int active_output_node_changed_count() const {
209 return active_output_node_changed_count_
;
212 void reset_active_output_node_changed_count() {
213 active_output_node_changed_count_
= 0;
216 int active_input_node_changed_count() const {
217 return active_input_node_changed_count_
;
220 void reset_active_input_node_changed_count() {
221 active_input_node_changed_count_
= 0;
224 int audio_nodes_changed_count() const {
225 return audio_nodes_changed_count_
;
228 int output_mute_changed_count() const {
229 return output_mute_changed_count_
;
232 int input_mute_changed_count() const {
233 return input_mute_changed_count_
;
236 int output_volume_changed_count() const {
237 return output_volume_changed_count_
;
240 int input_gain_changed_count() const {
241 return input_gain_changed_count_
;
244 virtual ~TestObserver() {}
247 // chromeos::CrasAudioHandler::AudioObserver overrides.
248 virtual void OnActiveOutputNodeChanged() override
{
249 ++active_output_node_changed_count_
;
252 virtual void OnActiveInputNodeChanged() override
{
253 ++active_input_node_changed_count_
;
256 virtual void OnAudioNodesChanged() override
{
257 ++audio_nodes_changed_count_
;
260 virtual void OnOutputMuteChanged() override
{
261 ++output_mute_changed_count_
;
264 virtual void OnInputMuteChanged() override
{
265 ++input_mute_changed_count_
;
268 virtual void OnOutputVolumeChanged() override
{
269 ++output_volume_changed_count_
;
272 virtual void OnInputGainChanged() override
{
273 ++input_gain_changed_count_
;
277 int active_output_node_changed_count_
;
278 int active_input_node_changed_count_
;
279 int audio_nodes_changed_count_
;
280 int output_mute_changed_count_
;
281 int input_mute_changed_count_
;
282 int output_volume_changed_count_
;
283 int input_gain_changed_count_
;
285 DISALLOW_COPY_AND_ASSIGN(TestObserver
);
288 class CrasAudioHandlerTest
: public testing::Test
{
290 CrasAudioHandlerTest() : cras_audio_handler_(NULL
),
291 fake_cras_audio_client_(NULL
) {
293 virtual ~CrasAudioHandlerTest() {}
295 virtual void SetUp() override
{
298 virtual void TearDown() override
{
299 cras_audio_handler_
->RemoveAudioObserver(test_observer_
.get());
300 test_observer_
.reset();
301 CrasAudioHandler::Shutdown();
302 audio_pref_handler_
= NULL
;
303 DBusThreadManager::Shutdown();
306 void SetUpCrasAudioHandler(const AudioNodeList
& audio_nodes
) {
307 DBusThreadManager::Initialize();
308 fake_cras_audio_client_
= static_cast<FakeCrasAudioClient
*>(
309 DBusThreadManager::Get()->GetCrasAudioClient());
310 fake_cras_audio_client_
->SetAudioNodesForTesting(audio_nodes
);
311 audio_pref_handler_
= new AudioDevicesPrefHandlerStub();
312 CrasAudioHandler::Initialize(audio_pref_handler_
);
313 cras_audio_handler_
= CrasAudioHandler::Get();
314 test_observer_
.reset(new TestObserver
);
315 cras_audio_handler_
->AddAudioObserver(test_observer_
.get());
316 message_loop_
.RunUntilIdle();
319 void SetUpCrasAudioHandlerWithPrimaryActiveNode(
320 const AudioNodeList
& audio_nodes
,
321 const AudioNode
& primary_active_node
) {
322 DBusThreadManager::Initialize();
323 fake_cras_audio_client_
= static_cast<FakeCrasAudioClient
*>(
324 DBusThreadManager::Get()->GetCrasAudioClient());
325 fake_cras_audio_client_
->SetAudioNodesForTesting(audio_nodes
);
326 fake_cras_audio_client_
->SetActiveOutputNode(primary_active_node
.id
),
327 audio_pref_handler_
= new AudioDevicesPrefHandlerStub();
328 CrasAudioHandler::Initialize(audio_pref_handler_
);
329 cras_audio_handler_
= CrasAudioHandler::Get();
330 test_observer_
.reset(new TestObserver
);
331 cras_audio_handler_
->AddAudioObserver(test_observer_
.get());
332 message_loop_
.RunUntilIdle();
335 void ChangeAudioNodes(const AudioNodeList
& audio_nodes
) {
336 fake_cras_audio_client_
->SetAudioNodesAndNotifyObserversForTesting(
338 message_loop_
.RunUntilIdle();
341 const AudioDevice
* GetDeviceFromId(uint64 id
) {
342 return cras_audio_handler_
->GetDeviceFromId(id
);
345 int GetActiveDeviceCount() const {
346 int num_active_nodes
= 0;
347 AudioDeviceList audio_devices
;
348 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
349 for (size_t i
= 0; i
< audio_devices
.size(); ++i
) {
350 if (audio_devices
[i
].active
)
353 return num_active_nodes
;
357 base::MessageLoopForUI message_loop_
;
358 CrasAudioHandler
* cras_audio_handler_
; // Not owned.
359 FakeCrasAudioClient
* fake_cras_audio_client_
; // Not owned.
360 scoped_ptr
<TestObserver
> test_observer_
;
361 scoped_refptr
<AudioDevicesPrefHandlerStub
> audio_pref_handler_
;
364 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandlerTest
);
367 TEST_F(CrasAudioHandlerTest
, InitializeWithOnlyDefaultAudioDevices
) {
368 AudioNodeList audio_nodes
;
369 audio_nodes
.push_back(kInternalSpeaker
);
370 audio_nodes
.push_back(kInternalMic
);
371 SetUpCrasAudioHandler(audio_nodes
);
373 // Verify the audio devices size.
374 AudioDeviceList audio_devices
;
375 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
376 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
378 // Verify the internal speaker has been selected as the active output.
379 AudioDevice active_output
;
381 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
382 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
383 EXPECT_EQ(kInternalSpeaker
.id
,
384 cras_audio_handler_
->GetPrimaryActiveOutputNode());
385 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
387 // Ensure the internal microphone has been selected as the active input.
388 AudioDevice active_input
;
389 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
390 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
393 TEST_F(CrasAudioHandlerTest
, InitializeWithAlternativeAudioDevices
) {
394 AudioNodeList audio_nodes
;
395 audio_nodes
.push_back(kInternalSpeaker
);
396 audio_nodes
.push_back(kHeadphone
);
397 audio_nodes
.push_back(kInternalMic
);
398 audio_nodes
.push_back(kUSBMic
);
399 SetUpCrasAudioHandler(audio_nodes
);
401 // Verify the audio devices size.
402 AudioDeviceList audio_devices
;
403 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
404 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
406 // Verify the headphone has been selected as the active output.
407 AudioDevice active_output
;
409 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
410 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
411 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
412 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
414 // Ensure the USB microphone has been selected as the active input.
415 EXPECT_EQ(kUSBMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
416 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
419 TEST_F(CrasAudioHandlerTest
, InitializeWithKeyboardMic
) {
420 AudioNodeList audio_nodes
;
421 audio_nodes
.push_back(kInternalSpeaker
);
422 audio_nodes
.push_back(kInternalMic
);
423 audio_nodes
.push_back(kKeyboardMic
);
424 SetUpCrasAudioHandler(audio_nodes
);
426 // Verify the audio devices size.
427 AudioDeviceList audio_devices
;
428 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
429 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
430 EXPECT_TRUE(cras_audio_handler_
->HasKeyboardMic());
432 // Verify the internal speaker has been selected as the active output.
433 AudioDevice active_output
;
435 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
436 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
437 EXPECT_EQ(kInternalSpeaker
.id
,
438 cras_audio_handler_
->GetPrimaryActiveOutputNode());
439 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
441 // Ensure the internal microphone has been selected as the active input,
442 // not affected by keyboard mic.
443 AudioDevice active_input
;
444 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
445 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
446 const AudioDevice
* keyboard_mic
= GetDeviceFromId(kKeyboardMic
.id
);
447 EXPECT_FALSE(keyboard_mic
->active
);
450 TEST_F(CrasAudioHandlerTest
, SetKeyboardMicActive
) {
451 AudioNodeList audio_nodes
;
452 audio_nodes
.push_back(kInternalMic
);
453 audio_nodes
.push_back(kKeyboardMic
);
454 SetUpCrasAudioHandler(audio_nodes
);
456 // Verify the audio devices size.
457 AudioDeviceList audio_devices
;
458 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
459 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
460 EXPECT_TRUE(cras_audio_handler_
->HasKeyboardMic());
462 // Ensure the internal microphone has been selected as the active input,
463 // not affected by keyboard mic.
464 AudioDevice active_input
;
465 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
466 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
467 const AudioDevice
* keyboard_mic
= GetDeviceFromId(kKeyboardMic
.id
);
468 EXPECT_FALSE(keyboard_mic
->active
);
470 // Make keyboard mic active.
471 cras_audio_handler_
->SetKeyboardMicActive(true);
472 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
473 const AudioDevice
* active_keyboard_mic
= GetDeviceFromId(kKeyboardMic
.id
);
474 EXPECT_TRUE(active_keyboard_mic
->active
);
476 // Make keyboard mic inactive.
477 cras_audio_handler_
->SetKeyboardMicActive(false);
478 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
479 const AudioDevice
* inactive_keyboard_mic
= GetDeviceFromId(kKeyboardMic
.id
);
480 EXPECT_FALSE(inactive_keyboard_mic
->active
);
483 TEST_F(CrasAudioHandlerTest
, SwitchActiveOutputDevice
) {
484 AudioNodeList audio_nodes
;
485 audio_nodes
.push_back(kInternalSpeaker
);
486 audio_nodes
.push_back(kHeadphone
);
487 SetUpCrasAudioHandler(audio_nodes
);
488 AudioDeviceList audio_devices
;
489 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
490 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
492 // Verify the initial active output device is headphone.
493 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
494 AudioDevice active_output
;
496 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
497 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
498 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
500 // Switch the active output to internal speaker.
501 AudioDevice
internal_speaker(kInternalSpeaker
);
502 cras_audio_handler_
->SwitchToDevice(internal_speaker
, true);
504 // Verify the active output is switched to internal speaker, and the
505 // ActiveOutputNodeChanged event is fired.
506 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
508 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
509 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
510 EXPECT_EQ(kInternalSpeaker
.id
,
511 cras_audio_handler_
->GetPrimaryActiveOutputNode());
514 TEST_F(CrasAudioHandlerTest
, SwitchActiveInputDevice
) {
515 AudioNodeList audio_nodes
;
516 audio_nodes
.push_back(kInternalMic
);
517 audio_nodes
.push_back(kUSBMic
);
518 SetUpCrasAudioHandler(audio_nodes
);
519 AudioDeviceList audio_devices
;
520 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
521 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
523 // Verify the initial active input device is USB mic.
524 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
525 EXPECT_EQ(kUSBMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
527 // Switch the active input to internal mic.
528 AudioDevice
internal_mic(kInternalMic
);
529 cras_audio_handler_
->SwitchToDevice(internal_mic
, true);
531 // Verify the active output is switched to internal speaker, and the active
532 // ActiveInputNodeChanged event is fired.
533 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
534 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
537 TEST_F(CrasAudioHandlerTest
, PlugHeadphone
) {
538 // Set up initial audio devices, only with internal speaker.
539 AudioNodeList audio_nodes
;
540 audio_nodes
.push_back(kInternalSpeaker
);
541 SetUpCrasAudioHandler(audio_nodes
);
542 const size_t init_nodes_size
= audio_nodes
.size();
544 // Verify the audio devices size.
545 AudioDeviceList audio_devices
;
546 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
547 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
548 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
550 // Verify the internal speaker has been selected as the active output.
551 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
552 AudioDevice active_output
;
554 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
555 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
556 EXPECT_EQ(kInternalSpeaker
.id
,
557 cras_audio_handler_
->GetPrimaryActiveOutputNode());
558 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
560 // Plug the headphone.
562 AudioNode
internal_speaker(kInternalSpeaker
);
563 internal_speaker
.active
= true;
564 audio_nodes
.push_back(internal_speaker
);
565 audio_nodes
.push_back(kHeadphone
);
566 ChangeAudioNodes(audio_nodes
);
568 // Verify the AudioNodesChanged event is fired and new audio device is added.
569 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
570 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
571 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
573 // Verify the active output device is switched to headphone and
574 // ActiveOutputChanged event is fired.
575 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
577 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
578 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
579 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
580 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
583 TEST_F(CrasAudioHandlerTest
, UnplugHeadphone
) {
584 // Set up initial audio devices, with internal speaker and headphone.
585 AudioNodeList audio_nodes
;
586 audio_nodes
.push_back(kInternalSpeaker
);
587 audio_nodes
.push_back(kHeadphone
);
588 SetUpCrasAudioHandler(audio_nodes
);
589 const size_t init_nodes_size
= audio_nodes
.size();
591 // Verify the audio devices size.
592 AudioDeviceList audio_devices
;
593 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
594 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
595 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
597 // Verify the headphone has been selected as the active output.
598 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
599 AudioDevice active_output
;
601 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
602 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
603 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
604 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
606 // Unplug the headphone.
608 audio_nodes
.push_back(kInternalSpeaker
);
609 ChangeAudioNodes(audio_nodes
);
611 // Verify the AudioNodesChanged event is fired and one audio device is
613 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
614 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
615 EXPECT_EQ(init_nodes_size
- 1, audio_devices
.size());
617 // Verify the active output device is switched to internal speaker and
618 // ActiveOutputChanged event is fired.
619 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
621 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
622 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
623 EXPECT_EQ(kInternalSpeaker
.id
,
624 cras_audio_handler_
->GetPrimaryActiveOutputNode());
625 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
628 TEST_F(CrasAudioHandlerTest
, InitializeWithBluetoothHeadset
) {
629 AudioNodeList audio_nodes
;
630 audio_nodes
.push_back(kInternalSpeaker
);
631 audio_nodes
.push_back(kBluetoothHeadset
);
632 SetUpCrasAudioHandler(audio_nodes
);
634 // Verify the audio devices size.
635 AudioDeviceList audio_devices
;
636 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
637 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
638 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
640 // Verify the bluetooth headset has been selected as the active output.
641 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
642 AudioDevice active_output
;
644 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
645 EXPECT_EQ(kBluetoothHeadset
.id
, active_output
.id
);
646 EXPECT_EQ(kBluetoothHeadset
.id
,
647 cras_audio_handler_
->GetPrimaryActiveOutputNode());
648 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
651 TEST_F(CrasAudioHandlerTest
, ConnectAndDisconnectBluetoothHeadset
) {
652 // Initialize with internal speaker and headphone.
653 AudioNodeList audio_nodes
;
654 audio_nodes
.push_back(kInternalSpeaker
);
655 audio_nodes
.push_back(kHeadphone
);
656 SetUpCrasAudioHandler(audio_nodes
);
657 const size_t init_nodes_size
= audio_nodes
.size();
659 // Verify the audio devices size.
660 AudioDeviceList audio_devices
;
661 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
662 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
663 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
665 // Verify the headphone is selected as the active output initially.
666 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
667 AudioDevice active_output
;
669 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
670 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
671 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
672 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
674 // Connect to bluetooth headset. Since it is plugged in later than
675 // headphone, active output should be switched to it.
677 audio_nodes
.push_back(kInternalSpeaker
);
678 AudioNode
headphone(kHeadphone
);
679 headphone
.plugged_time
= 80000000;
680 headphone
.active
= true;
681 audio_nodes
.push_back(headphone
);
682 AudioNode
bluetooth_headset(kBluetoothHeadset
);
683 bluetooth_headset
.plugged_time
= 90000000;
684 audio_nodes
.push_back(bluetooth_headset
);
685 ChangeAudioNodes(audio_nodes
);
687 // Verify the AudioNodesChanged event is fired and new audio device is added.
688 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
689 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
690 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
692 // Verify the active output device is switched to bluetooth headset, and
693 // ActiveOutputChanged event is fired.
694 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
696 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
697 EXPECT_EQ(kBluetoothHeadset
.id
, active_output
.id
);
698 EXPECT_EQ(kBluetoothHeadset
.id
,
699 cras_audio_handler_
->GetPrimaryActiveOutputNode());
700 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
702 // Disconnect bluetooth headset.
704 audio_nodes
.push_back(kInternalSpeaker
);
705 headphone
.active
= false;
706 audio_nodes
.push_back(headphone
);
707 ChangeAudioNodes(audio_nodes
);
709 // Verify the AudioNodesChanged event is fired and one audio device is
711 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
712 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
713 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
715 // Verify the active output device is switched to headphone, and
716 // ActiveOutputChanged event is fired.
717 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
719 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
720 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
721 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
722 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
725 TEST_F(CrasAudioHandlerTest
, InitializeWithHDMIOutput
) {
726 AudioNodeList audio_nodes
;
727 audio_nodes
.push_back(kInternalSpeaker
);
728 audio_nodes
.push_back(kHDMIOutput
);
729 SetUpCrasAudioHandler(audio_nodes
);
731 // Verify the audio devices size.
732 AudioDeviceList audio_devices
;
733 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
734 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
735 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
737 // Verify the HDMI device has been selected as the active output.
738 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
739 AudioDevice active_output
;
741 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
742 EXPECT_EQ(kHDMIOutput
.id
, active_output
.id
);
743 EXPECT_EQ(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
744 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
747 TEST_F(CrasAudioHandlerTest
, ConnectAndDisconnectHDMIOutput
) {
748 // Initialize with internal speaker.
749 AudioNodeList audio_nodes
;
750 audio_nodes
.push_back(kInternalSpeaker
);
751 SetUpCrasAudioHandler(audio_nodes
);
752 const size_t init_nodes_size
= audio_nodes
.size();
754 // Verify the audio devices size.
755 AudioDeviceList audio_devices
;
756 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
757 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
758 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
760 // Verify the internal speaker is selected as the active output initially.
761 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
762 AudioDevice active_output
;
764 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
765 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
766 EXPECT_EQ(kInternalSpeaker
.id
,
767 cras_audio_handler_
->GetPrimaryActiveOutputNode());
768 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
770 // Connect to HDMI output.
772 AudioNode
internal_speaker(kInternalSpeaker
);
773 internal_speaker
.active
= true;
774 internal_speaker
.plugged_time
= 80000000;
775 audio_nodes
.push_back(internal_speaker
);
776 AudioNode
hdmi(kHDMIOutput
);
777 hdmi
.plugged_time
= 90000000;
778 audio_nodes
.push_back(hdmi
);
779 ChangeAudioNodes(audio_nodes
);
781 // Verify the AudioNodesChanged event is fired and new audio device is added.
782 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
783 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
784 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
786 // Verify the active output device is switched to hdmi output, and
787 // ActiveOutputChanged event is fired.
788 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
790 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
791 EXPECT_EQ(kHDMIOutput
.id
, active_output
.id
);
792 EXPECT_EQ(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
793 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
795 // Disconnect hdmi headset.
797 audio_nodes
.push_back(kInternalSpeaker
);
798 ChangeAudioNodes(audio_nodes
);
800 // Verify the AudioNodesChanged event is fired and one audio device is
802 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
803 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
804 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
806 // Verify the active output device is switched to internal speaker, and
807 // ActiveOutputChanged event is fired.
808 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
810 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
811 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
812 EXPECT_EQ(kInternalSpeaker
.id
,
813 cras_audio_handler_
->GetPrimaryActiveOutputNode());
814 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
817 TEST_F(CrasAudioHandlerTest
, HandleHeadphoneAndHDMIOutput
) {
818 // Initialize with internal speaker, headphone and HDMI output.
819 AudioNodeList audio_nodes
;
820 audio_nodes
.push_back(kInternalSpeaker
);
821 audio_nodes
.push_back(kHeadphone
);
822 audio_nodes
.push_back(kHDMIOutput
);
823 SetUpCrasAudioHandler(audio_nodes
);
824 const size_t init_nodes_size
= audio_nodes
.size();
826 // Verify the audio devices size.
827 AudioDeviceList audio_devices
;
828 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
829 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
830 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
832 // Verify the headphone is selected as the active output initially.
833 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
834 AudioDevice active_output
;
836 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
837 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
838 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
839 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
841 // Disconnect HDMI output.
843 audio_nodes
.push_back(kInternalSpeaker
);
844 audio_nodes
.push_back(kHDMIOutput
);
845 ChangeAudioNodes(audio_nodes
);
847 // Verify the AudioNodesChanged event is fired and one audio device is
849 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
850 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
851 EXPECT_EQ(init_nodes_size
- 1, audio_devices
.size());
853 // Verify the active output device is switched to HDMI output, and
854 // ActiveOutputChanged event is fired.
855 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
857 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
858 EXPECT_EQ(kHDMIOutput
.id
, active_output
.id
);
859 EXPECT_EQ(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
860 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
863 TEST_F(CrasAudioHandlerTest
, InitializeWithUSBHeadphone
) {
864 AudioNodeList audio_nodes
;
865 audio_nodes
.push_back(kInternalSpeaker
);
866 audio_nodes
.push_back(kUSBHeadphone1
);
867 SetUpCrasAudioHandler(audio_nodes
);
869 // Verify the audio devices size.
870 AudioDeviceList audio_devices
;
871 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
872 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
873 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
875 // Verify the usb headphone has been selected as the active output.
876 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
877 AudioDevice active_output
;
879 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
880 EXPECT_EQ(kUSBHeadphone1
.id
, active_output
.id
);
881 EXPECT_EQ(kUSBHeadphone1
.id
,
882 cras_audio_handler_
->GetPrimaryActiveOutputNode());
883 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
886 TEST_F(CrasAudioHandlerTest
, PlugAndUnplugUSBHeadphone
) {
887 // Initialize with internal speaker.
888 AudioNodeList audio_nodes
;
889 audio_nodes
.push_back(kInternalSpeaker
);
890 SetUpCrasAudioHandler(audio_nodes
);
891 const size_t init_nodes_size
= audio_nodes
.size();
893 // Verify the audio devices size.
894 AudioDeviceList audio_devices
;
895 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
896 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
897 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
899 // Verify the internal speaker is selected as the active output initially.
900 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
901 AudioDevice active_output
;
903 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
904 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
905 EXPECT_EQ(kInternalSpeaker
.id
,
906 cras_audio_handler_
->GetPrimaryActiveOutputNode());
907 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
909 // Plug in usb headphone
911 AudioNode
internal_speaker(kInternalSpeaker
);
912 internal_speaker
.active
= true;
913 internal_speaker
.plugged_time
= 80000000;
914 audio_nodes
.push_back(internal_speaker
);
915 AudioNode
usb_headphone(kUSBHeadphone1
);
916 usb_headphone
.plugged_time
= 90000000;
917 audio_nodes
.push_back(usb_headphone
);
918 ChangeAudioNodes(audio_nodes
);
920 // Verify the AudioNodesChanged event is fired and new audio device is added.
921 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
922 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
923 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
925 // Verify the active output device is switched to usb headphone, and
926 // ActiveOutputChanged event is fired.
927 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
929 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
930 EXPECT_EQ(kUSBHeadphone1
.id
, active_output
.id
);
931 EXPECT_EQ(kUSBHeadphone1
.id
,
932 cras_audio_handler_
->GetPrimaryActiveOutputNode());
933 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
935 // Unplug usb headphone.
937 audio_nodes
.push_back(kInternalSpeaker
);
938 ChangeAudioNodes(audio_nodes
);
940 // Verify the AudioNodesChanged event is fired and one audio device is
942 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
943 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
944 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
946 // Verify the active output device is switched to internal speaker, and
947 // ActiveOutputChanged event is fired.
948 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
950 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
951 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
952 EXPECT_EQ(kInternalSpeaker
.id
,
953 cras_audio_handler_
->GetPrimaryActiveOutputNode());
954 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
957 TEST_F(CrasAudioHandlerTest
, HandleMultipleUSBHeadphones
) {
958 // Initialize with internal speaker and one usb headphone.
959 AudioNodeList audio_nodes
;
960 audio_nodes
.push_back(kInternalSpeaker
);
961 audio_nodes
.push_back(kUSBHeadphone1
);
962 SetUpCrasAudioHandler(audio_nodes
);
963 const size_t init_nodes_size
= audio_nodes
.size();
965 // Verify the audio devices size.
966 AudioDeviceList audio_devices
;
967 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
968 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
969 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
971 // Verify the usb headphone is selected as the active output initially.
972 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
973 AudioDevice active_output
;
975 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
976 EXPECT_EQ(kUSBHeadphone1
.id
, active_output
.id
);
977 EXPECT_EQ(kUSBHeadphone1
.id
,
978 cras_audio_handler_
->GetPrimaryActiveOutputNode());
979 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
981 // Plug in another usb headphone.
983 audio_nodes
.push_back(kInternalSpeaker
);
984 AudioNode
usb_headphone_1(kUSBHeadphone1
);
985 usb_headphone_1
.active
= true;
986 usb_headphone_1
.plugged_time
= 80000000;
987 audio_nodes
.push_back(usb_headphone_1
);
988 AudioNode
usb_headphone_2(kUSBHeadphone2
);
989 usb_headphone_2
.plugged_time
= 90000000;
990 audio_nodes
.push_back(usb_headphone_2
);
991 ChangeAudioNodes(audio_nodes
);
993 // Verify the AudioNodesChanged event is fired and new audio device is added.
994 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
995 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
996 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
998 // Verify the active output device is switched to the 2nd usb headphone, which
999 // is plugged later, and ActiveOutputChanged event is fired.
1000 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1002 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1003 EXPECT_EQ(kUSBHeadphone2
.id
, active_output
.id
);
1004 EXPECT_EQ(kUSBHeadphone2
.id
,
1005 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1006 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1008 // Unplug the 2nd usb headphone.
1009 audio_nodes
.clear();
1010 audio_nodes
.push_back(kInternalSpeaker
);
1011 audio_nodes
.push_back(kUSBHeadphone1
);
1012 ChangeAudioNodes(audio_nodes
);
1014 // Verify the AudioNodesChanged event is fired and one audio device is
1016 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1017 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1018 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1020 // Verify the active output device is switched to the first usb headphone, and
1021 // ActiveOutputChanged event is fired.
1022 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
1024 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1025 EXPECT_EQ(kUSBHeadphone1
.id
, active_output
.id
);
1026 EXPECT_EQ(kUSBHeadphone1
.id
,
1027 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1028 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1031 TEST_F(CrasAudioHandlerTest
, UnplugUSBHeadphonesWithActiveSpeaker
) {
1032 // Initialize with internal speaker and one usb headphone.
1033 AudioNodeList audio_nodes
;
1034 audio_nodes
.push_back(kInternalSpeaker
);
1035 audio_nodes
.push_back(kUSBHeadphone1
);
1036 SetUpCrasAudioHandler(audio_nodes
);
1037 const size_t init_nodes_size
= audio_nodes
.size();
1039 // Verify the audio devices size.
1040 AudioDeviceList audio_devices
;
1041 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1042 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1043 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1045 // Verify the usb headphone is selected as the active output initially.
1046 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1047 AudioDevice active_output
;
1049 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1050 EXPECT_EQ(kUSBHeadphone1
.id
, active_output
.id
);
1051 EXPECT_EQ(kUSBHeadphone1
.id
,
1052 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1053 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1055 // Plug in the headphone jack.
1056 audio_nodes
.clear();
1057 audio_nodes
.push_back(kInternalSpeaker
);
1058 AudioNode
usb_headphone_1(kUSBHeadphone1
);
1059 usb_headphone_1
.active
= true;
1060 usb_headphone_1
.plugged_time
= 80000000;
1061 audio_nodes
.push_back(usb_headphone_1
);
1062 AudioNode
headphone_jack(kHeadphone
);
1063 headphone_jack
.plugged_time
= 90000000;
1064 audio_nodes
.push_back(headphone_jack
);
1065 ChangeAudioNodes(audio_nodes
);
1067 // Verify the AudioNodesChanged event is fired and new audio device is added.
1068 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1069 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1070 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
1072 // Verify the active output device is switched to the headphone jack, which
1073 // is plugged later, and ActiveOutputChanged event is fired.
1074 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1076 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1077 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
1078 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1079 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1081 // Select the speaker to be the active output device.
1082 AudioDevice
internal_speaker(kInternalSpeaker
);
1083 cras_audio_handler_
->SwitchToDevice(internal_speaker
, true);
1085 // Verify the active output is switched to internal speaker, and the
1086 // ActiveOutputNodeChanged event is fired.
1087 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
1089 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1090 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
1091 EXPECT_EQ(kInternalSpeaker
.id
,
1092 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1094 // Unplug the usb headphone.
1095 audio_nodes
.clear();
1096 AudioNode
internal_speaker_node(kInternalSpeaker
);
1097 internal_speaker_node
.active
= true;
1098 internal_speaker_node
.plugged_time
= 70000000;
1099 audio_nodes
.push_back(internal_speaker_node
);
1100 headphone_jack
.active
= false;
1101 audio_nodes
.push_back(headphone_jack
);
1102 ChangeAudioNodes(audio_nodes
);
1104 // Verify the AudioNodesChanged event is fired and one audio device is
1106 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1107 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1108 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1110 // Verify the active output device remains to be speaker.
1111 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
1113 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1114 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
1115 EXPECT_EQ(kInternalSpeaker
.id
,
1116 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1117 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1120 TEST_F(CrasAudioHandlerTest
, OneActiveAudioOutputAfterLoginNewUserSession
) {
1121 // This tests the case found with crbug.com/273271.
1122 // Initialize with internal speaker, bluetooth headphone and headphone jack
1123 // for a new chrome session after user signs out from the previous session.
1124 // Headphone jack is plugged in later than bluetooth headphone, but bluetooth
1125 // headphone is selected as the active output by user from previous user
1127 AudioNodeList audio_nodes
;
1128 audio_nodes
.push_back(kInternalSpeaker
);
1129 AudioNode
bluetooth_headphone(kBluetoothHeadset
);
1130 bluetooth_headphone
.active
= true;
1131 bluetooth_headphone
.plugged_time
= 70000000;
1132 audio_nodes
.push_back(bluetooth_headphone
);
1133 AudioNode
headphone_jack(kHeadphone
);
1134 headphone_jack
.plugged_time
= 80000000;
1135 audio_nodes
.push_back(headphone_jack
);
1136 SetUpCrasAudioHandlerWithPrimaryActiveNode(audio_nodes
, bluetooth_headphone
);
1137 const size_t init_nodes_size
= audio_nodes
.size();
1139 // Verify the audio devices size.
1140 AudioDeviceList audio_devices
;
1141 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1142 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1143 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1145 // Verify the headphone jack is selected as the active output and all other
1146 // audio devices are not active.
1147 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1148 AudioDevice active_output
;
1150 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1151 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
1152 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1153 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1154 for (size_t i
= 0; i
< audio_devices
.size(); ++i
) {
1155 if (audio_devices
[i
].id
!= kHeadphone
.id
)
1156 EXPECT_FALSE(audio_devices
[i
].active
);
1160 TEST_F(CrasAudioHandlerTest
, BluetoothSpeakerIdChangedOnFly
) {
1161 // Initialize with internal speaker and bluetooth headset.
1162 AudioNodeList audio_nodes
;
1163 audio_nodes
.push_back(kInternalSpeaker
);
1164 audio_nodes
.push_back(kBluetoothHeadset
);
1165 SetUpCrasAudioHandler(audio_nodes
);
1166 const size_t init_nodes_size
= audio_nodes
.size();
1168 // Verify the audio devices size.
1169 AudioDeviceList audio_devices
;
1170 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1171 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1172 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1174 // Verify the bluetooth headset is selected as the active output and all other
1175 // audio devices are not active.
1176 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1177 AudioDevice active_output
;
1179 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1180 EXPECT_EQ(kBluetoothHeadset
.id
, active_output
.id
);
1181 EXPECT_EQ(kBluetoothHeadset
.id
,
1182 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1183 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1185 // Cras changes the bluetooth headset's id on the fly.
1186 audio_nodes
.clear();
1187 AudioNode
internal_speaker(kInternalSpeaker
);
1188 internal_speaker
.active
= false;
1189 audio_nodes
.push_back(internal_speaker
);
1190 AudioNode
bluetooth_headphone(kBluetoothHeadset
);
1191 // Change bluetooth headphone id.
1192 bluetooth_headphone
.id
= kBluetoothHeadsetId
+ 20000;
1193 bluetooth_headphone
.active
= false;
1194 audio_nodes
.push_back(bluetooth_headphone
);
1195 ChangeAudioNodes(audio_nodes
);
1197 // Verify NodesChanged event is fired, and the audio devices size is not
1199 audio_devices
.clear();
1200 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1201 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1202 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1204 // Verify ActiveOutputNodeChanged event is fired, and active device should be
1205 // bluetooth headphone.
1206 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1208 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1209 EXPECT_EQ(bluetooth_headphone
.id
, active_output
.id
);
1212 TEST_F(CrasAudioHandlerTest
, PlugUSBMic
) {
1213 // Set up initial audio devices, only with internal mic.
1214 AudioNodeList audio_nodes
;
1215 audio_nodes
.push_back(kInternalMic
);
1216 SetUpCrasAudioHandler(audio_nodes
);
1217 const size_t init_nodes_size
= audio_nodes
.size();
1219 // Verify the audio devices size.
1220 AudioDeviceList audio_devices
;
1221 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1222 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1223 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1225 // Verify the internal mic is selected as the active input.
1226 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1227 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1228 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
1230 // Plug the USB Mic.
1231 audio_nodes
.clear();
1232 AudioNode
internal_mic(kInternalMic
);
1233 internal_mic
.active
= true;
1234 audio_nodes
.push_back(internal_mic
);
1235 audio_nodes
.push_back(kUSBMic
);
1236 ChangeAudioNodes(audio_nodes
);
1238 // Verify the AudioNodesChanged event is fired and new audio device is added.
1239 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1240 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1241 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
1243 // Verify the active input device is switched to USB mic and
1244 // and ActiveInputChanged event is fired.
1245 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
1246 EXPECT_EQ(kUSBMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1247 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1250 TEST_F(CrasAudioHandlerTest
, UnplugUSBMic
) {
1251 // Set up initial audio devices, with internal mic and USB Mic.
1252 AudioNodeList audio_nodes
;
1253 audio_nodes
.push_back(kInternalMic
);
1254 audio_nodes
.push_back(kUSBMic
);
1255 SetUpCrasAudioHandler(audio_nodes
);
1256 const size_t init_nodes_size
= audio_nodes
.size();
1258 // Verify the audio devices size.
1259 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1260 AudioDeviceList audio_devices
;
1261 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1262 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1264 // Verify the USB mic is selected as the active output.
1265 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1266 EXPECT_EQ(kUSBMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1267 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1269 // Unplug the USB Mic.
1270 audio_nodes
.clear();
1271 audio_nodes
.push_back(kInternalMic
);
1272 ChangeAudioNodes(audio_nodes
);
1274 // Verify the AudioNodesChanged event is fired, and one audio device is
1276 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1277 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1278 EXPECT_EQ(init_nodes_size
- 1, audio_devices
.size());
1280 // Verify the active input device is switched to internal mic, and
1281 // and ActiveInputChanged event is fired.
1282 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
1283 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1284 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
1287 TEST_F(CrasAudioHandlerTest
, PlugUSBMicNotAffectActiveOutput
) {
1288 // Set up initial audio devices.
1289 AudioNodeList audio_nodes
;
1290 audio_nodes
.push_back(kInternalSpeaker
);
1291 audio_nodes
.push_back(kHeadphone
);
1292 audio_nodes
.push_back(kInternalMic
);
1293 SetUpCrasAudioHandler(audio_nodes
);
1294 const size_t init_nodes_size
= audio_nodes
.size();
1296 // Verify the audio devices size.
1297 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1298 AudioDeviceList audio_devices
;
1299 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1300 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1302 // Verify the internal mic is selected as the active input.
1303 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1304 EXPECT_EQ(kInternalMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1305 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
1307 // Verify the headphone is selected as the active output.
1308 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1309 EXPECT_EQ(kHeadphoneId
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1310 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1312 // Switch the active output to internal speaker.
1313 AudioDevice
internal_speaker(kInternalSpeaker
);
1314 cras_audio_handler_
->SwitchToDevice(internal_speaker
, true);
1316 // Verify the active output is switched to internal speaker, and the
1317 // ActiveOutputNodeChanged event is fired.
1318 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1319 AudioDevice active_output
;
1321 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1322 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
1323 EXPECT_EQ(kInternalSpeaker
.id
,
1324 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1326 // Plug the USB Mic.
1327 audio_nodes
.clear();
1328 AudioNode
internal_speaker_node(kInternalSpeaker
);
1329 internal_speaker_node
.active
= true;
1330 audio_nodes
.push_back(internal_speaker_node
);
1331 audio_nodes
.push_back(kHeadphone
);
1332 AudioNode
internal_mic(kInternalMic
);
1333 internal_mic
.active
= true;
1334 audio_nodes
.push_back(internal_mic
);
1335 audio_nodes
.push_back(kUSBMic
);
1336 ChangeAudioNodes(audio_nodes
);
1338 // Verify the AudioNodesChanged event is fired, one new device is added.
1339 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1340 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1341 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
1343 // Verify the active input device is switched to USB mic, and
1344 // and ActiveInputChanged event is fired.
1345 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
1346 EXPECT_EQ(kUSBMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1347 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1349 // Verify the active output device is not changed.
1350 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1352 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1353 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
1354 EXPECT_EQ(kInternalSpeaker
.id
,
1355 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1358 TEST_F(CrasAudioHandlerTest
, PlugHeadphoneAutoUnplugSpeakerWithActiveUSB
) {
1359 // Set up initial audio devices.
1360 AudioNodeList audio_nodes
;
1361 audio_nodes
.push_back(kUSBHeadphone1
);
1362 audio_nodes
.push_back(kInternalSpeaker
);
1363 audio_nodes
.push_back(kInternalMic
);
1364 SetUpCrasAudioHandler(audio_nodes
);
1365 const size_t init_nodes_size
= audio_nodes
.size();
1367 // Verify the audio devices size.
1368 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1369 AudioDeviceList audio_devices
;
1370 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1371 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1373 // Verify the internal mic is selected as the active input.
1374 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1375 EXPECT_EQ(kInternalMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1376 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
1378 // Verify the USB headphone is selected as the active output.
1379 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1380 EXPECT_EQ(kUSBHeadphoneId1
,
1381 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1382 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1384 // Plug the headphone and auto-unplug internal speaker.
1385 audio_nodes
.clear();
1386 AudioNode
usb_headphone_node(kUSBHeadphone1
);
1387 usb_headphone_node
.active
= true;
1388 audio_nodes
.push_back(usb_headphone_node
);
1389 AudioNode
headphone_node(kHeadphone
);
1390 headphone_node
.plugged_time
= 1000;
1391 audio_nodes
.push_back(headphone_node
);
1392 AudioNode
internal_mic(kInternalMic
);
1393 internal_mic
.active
= true;
1394 audio_nodes
.push_back(internal_mic
);
1395 ChangeAudioNodes(audio_nodes
);
1397 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1398 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1399 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1400 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1402 // Verify the active output device is switched to headphone, and
1403 // an ActiveOutputChanged event is fired.
1404 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1405 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1406 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1408 // Unplug the headphone and internal speaker auto-plugs back.
1409 audio_nodes
.clear();
1410 audio_nodes
.push_back(kUSBHeadphone1
);
1411 AudioNode
internal_speaker_node(kInternalSpeaker
);
1412 internal_speaker_node
.plugged_time
= 2000;
1413 audio_nodes
.push_back(internal_speaker_node
);
1414 audio_nodes
.push_back(internal_mic
);
1415 ChangeAudioNodes(audio_nodes
);
1417 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1418 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1419 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1420 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1422 // Verify the active output device is switched back to USB, and
1423 // an ActiveOutputChanged event is fired.
1424 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
1425 EXPECT_EQ(kUSBHeadphone1
.id
,
1426 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1427 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1429 // Verify the active input device is not changed.
1430 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1431 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1434 TEST_F(CrasAudioHandlerTest
, PlugMicAutoUnplugInternalMicWithActiveUSB
) {
1435 // Set up initial audio devices.
1436 AudioNodeList audio_nodes
;
1437 audio_nodes
.push_back(kUSBHeadphone1
);
1438 audio_nodes
.push_back(kInternalSpeaker
);
1439 audio_nodes
.push_back(kUSBMic
);
1440 audio_nodes
.push_back(kInternalMic
);
1441 SetUpCrasAudioHandler(audio_nodes
);
1442 const size_t init_nodes_size
= audio_nodes
.size();
1444 // Verify the audio devices size.
1445 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1446 AudioDeviceList audio_devices
;
1447 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1448 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1450 // Verify the internal mic is selected as the active input.
1451 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1452 EXPECT_EQ(kUSBMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1453 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1455 // Verify the internal speaker is selected as the active output.
1456 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1457 EXPECT_EQ(kUSBHeadphoneId1
,
1458 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1459 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1461 // Plug the headphone and mic, auto-unplug internal mic and speaker.
1462 audio_nodes
.clear();
1463 AudioNode
usb_headphone_node(kUSBHeadphone1
);
1464 usb_headphone_node
.active
= true;
1465 audio_nodes
.push_back(usb_headphone_node
);
1466 AudioNode
headphone_node(kHeadphone
);
1467 headphone_node
.plugged_time
= 1000;
1468 audio_nodes
.push_back(headphone_node
);
1469 AudioNode
usb_mic(kUSBMic
);
1470 usb_mic
.active
= true;
1471 audio_nodes
.push_back(usb_mic
);
1472 AudioNode
mic_jack(kMicJack
);
1473 mic_jack
.plugged_time
= 1000;
1474 audio_nodes
.push_back(mic_jack
);
1475 ChangeAudioNodes(audio_nodes
);
1477 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1478 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1479 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1480 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1482 // Verify the active output device is switched to headphone, and
1483 // an ActiveOutputChanged event is fired.
1484 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1485 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1486 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1488 // Verify the active input device is switched to mic jack, and
1489 // an ActiveInputChanged event is fired.
1490 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
1491 EXPECT_EQ(kMicJack
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1492 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1494 // Unplug the headphone and internal speaker auto-plugs back.
1495 audio_nodes
.clear();
1496 audio_nodes
.push_back(kUSBHeadphone1
);
1497 AudioNode
internal_speaker_node(kInternalSpeaker
);
1498 internal_speaker_node
.plugged_time
= 2000;
1499 audio_nodes
.push_back(internal_speaker_node
);
1500 audio_nodes
.push_back(kUSBMic
);
1501 AudioNode
internal_mic(kInternalMic
);
1502 internal_mic
.plugged_time
= 2000;
1503 audio_nodes
.push_back(internal_mic
);
1504 ChangeAudioNodes(audio_nodes
);
1506 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1507 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1508 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1509 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1511 // Verify the active output device is switched back to USB, and
1512 // an ActiveOutputChanged event is fired.
1513 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
1514 EXPECT_EQ(kUSBHeadphone1
.id
,
1515 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1516 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1518 // Verify the active input device is switched back to USB mic, and
1519 // an ActiveInputChanged event is fired.
1520 EXPECT_EQ(2, test_observer_
->active_input_node_changed_count());
1521 EXPECT_EQ(kUSBMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1522 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1525 TEST_F(CrasAudioHandlerTest
, MultipleNodesChangedSignalsOnPlugInHeadphone
) {
1526 // Set up initial audio devices.
1527 AudioNodeList audio_nodes
;
1528 audio_nodes
.push_back(kInternalSpeaker
);
1529 audio_nodes
.push_back(kBluetoothHeadset
);
1530 SetUpCrasAudioHandler(audio_nodes
);
1531 const size_t init_nodes_size
= audio_nodes
.size();
1533 // Verify the audio devices size.
1534 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1535 AudioDeviceList audio_devices
;
1536 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1537 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1539 // Verify the bluetooth headset is selected as the active output.
1540 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1541 EXPECT_EQ(kBluetoothHeadsetId
,
1542 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1543 AudioDevice active_output
;
1545 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1546 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1548 // Plug in headphone, but fire NodesChanged signal twice.
1549 audio_nodes
.clear();
1550 audio_nodes
.push_back(kInternalSpeaker
);
1551 AudioNode
bluetooth_headset(kBluetoothHeadset
);
1552 bluetooth_headset
.plugged_time
= 1000;
1553 bluetooth_headset
.active
= true;
1554 audio_nodes
.push_back(bluetooth_headset
);
1555 AudioNode
headphone(kHeadphone
);
1556 headphone
.active
= false;
1557 headphone
.plugged_time
= 2000;
1558 audio_nodes
.push_back(headphone
);
1559 ChangeAudioNodes(audio_nodes
);
1560 ChangeAudioNodes(audio_nodes
);
1562 // Verify the active output device is set to headphone.
1563 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1564 EXPECT_LE(1, test_observer_
->active_output_node_changed_count());
1565 EXPECT_EQ(headphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1567 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1568 EXPECT_EQ(headphone
.id
, active_output
.id
);
1570 // Verfiy the audio devices data is consistent, i.e., the active output device
1571 // should be headphone.
1572 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1573 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
1574 for (size_t i
= 0; i
< audio_devices
.size(); ++i
) {
1575 if (audio_devices
[i
].id
== kInternalSpeaker
.id
)
1576 EXPECT_FALSE(audio_devices
[i
].active
);
1577 else if (audio_devices
[i
].id
== bluetooth_headset
.id
)
1578 EXPECT_FALSE(audio_devices
[i
].active
);
1579 else if (audio_devices
[i
].id
== headphone
.id
)
1580 EXPECT_TRUE(audio_devices
[i
].active
);
1586 TEST_F(CrasAudioHandlerTest
, MultipleNodesChangedSignalsOnPlugInUSBMic
) {
1587 // Set up initial audio devices.
1588 AudioNodeList audio_nodes
;
1589 audio_nodes
.push_back(kInternalMic
);
1590 SetUpCrasAudioHandler(audio_nodes
);
1591 const size_t init_nodes_size
= audio_nodes
.size();
1593 // Verify the audio devices size.
1594 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1595 AudioDeviceList audio_devices
;
1596 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1597 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1599 // Verify the internal mic is selected as the active output.
1600 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1601 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1602 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
1603 EXPECT_TRUE(audio_devices
[0].active
);
1605 // Plug in usb mic, but fire NodesChanged signal twice.
1606 audio_nodes
.clear();
1607 AudioNode
internal_mic(kInternalMic
);
1608 internal_mic
.active
= true;
1609 internal_mic
.plugged_time
= 1000;
1610 audio_nodes
.push_back(internal_mic
);
1611 AudioNode
usb_mic(kUSBMic
);
1612 usb_mic
.active
= false;
1613 usb_mic
.plugged_time
= 2000;
1614 audio_nodes
.push_back(usb_mic
);
1615 ChangeAudioNodes(audio_nodes
);
1616 ChangeAudioNodes(audio_nodes
);
1618 // Verify the active output device is set to headphone.
1619 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1620 EXPECT_LE(1, test_observer_
->active_input_node_changed_count());
1621 EXPECT_EQ(usb_mic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1622 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1624 // Verfiy the audio devices data is consistent, i.e., the active input device
1625 // should be usb mic.
1626 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1627 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
1628 for (size_t i
= 0; i
< audio_devices
.size(); ++i
) {
1629 if (audio_devices
[i
].id
== kInternalMic
.id
)
1630 EXPECT_FALSE(audio_devices
[i
].active
);
1631 else if (audio_devices
[i
].id
== usb_mic
.id
)
1632 EXPECT_TRUE(audio_devices
[i
].active
);
1638 // This is the case of crbug.com/291303.
1639 TEST_F(CrasAudioHandlerTest
, MultipleNodesChangedSignalsOnSystemBoot
) {
1640 // Set up audio handler with empty audio_nodes.
1641 AudioNodeList audio_nodes
;
1642 SetUpCrasAudioHandler(audio_nodes
);
1644 AudioNode
internal_speaker(kInternalSpeaker
);
1645 internal_speaker
.active
= false;
1646 AudioNode
headphone(kHeadphone
);
1647 headphone
.active
= false;
1648 AudioNode
internal_mic(kInternalMic
);
1649 internal_mic
.active
= false;
1650 audio_nodes
.push_back(internal_speaker
);
1651 audio_nodes
.push_back(headphone
);
1652 audio_nodes
.push_back(internal_mic
);
1653 const size_t init_nodes_size
= audio_nodes
.size();
1655 // Simulate AudioNodesChanged signal being fired twice during system boot.
1656 ChangeAudioNodes(audio_nodes
);
1657 ChangeAudioNodes(audio_nodes
);
1659 // Verify the active output device is set to headphone.
1660 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1661 EXPECT_LE(1, test_observer_
->active_output_node_changed_count());
1662 EXPECT_EQ(headphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1663 AudioDevice active_output
;
1665 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1666 EXPECT_EQ(headphone
.id
, active_output
.id
);
1668 // Verify the active input device id is set to internal mic.
1669 EXPECT_EQ(internal_mic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1671 // Verfiy the audio devices data is consistent, i.e., the active output device
1672 // should be headphone, and the active input device should internal mic.
1673 AudioDeviceList audio_devices
;
1674 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1675 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1676 for (size_t i
= 0; i
< audio_devices
.size(); ++i
) {
1677 if (audio_devices
[i
].id
== internal_speaker
.id
)
1678 EXPECT_FALSE(audio_devices
[i
].active
);
1679 else if (audio_devices
[i
].id
== headphone
.id
)
1680 EXPECT_TRUE(audio_devices
[i
].active
);
1681 else if (audio_devices
[i
].id
== internal_mic
.id
)
1682 EXPECT_TRUE(audio_devices
[i
].active
);
1688 // This is the case of crbug.com/448924.
1689 TEST_F(CrasAudioHandlerTest
,
1690 TwoNodesChangedSignalsForLosingTowNodesOnOneUnplug
) {
1691 // Set up audio handler with 4 audio_nodes.
1692 AudioNodeList audio_nodes
;
1693 AudioNode
internal_speaker(kInternalSpeaker
);
1694 internal_speaker
.active
= false;
1695 AudioNode
headphone(kHeadphone
);
1696 headphone
.active
= false;
1697 AudioNode
internal_mic(kInternalMic
);
1698 internal_mic
.active
= false;
1699 AudioNode
micJack(kMicJack
);
1700 micJack
.active
= false;
1701 audio_nodes
.push_back(internal_speaker
);
1702 audio_nodes
.push_back(headphone
);
1703 audio_nodes
.push_back(internal_mic
);
1704 audio_nodes
.push_back(micJack
);
1705 SetUpCrasAudioHandler(audio_nodes
);
1707 // Verify the audio devices size.
1708 AudioDeviceList audio_devices
;
1709 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1710 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
1712 // Verify the headphone has been selected as the active output.
1713 AudioDevice active_output
;
1715 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1716 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
1717 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1718 EXPECT_TRUE(active_output
.active
);
1719 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1721 // Verify the mic Jack has been selected as the active input.
1722 EXPECT_EQ(micJack
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1723 const AudioDevice
* active_input
= GetDeviceFromId(micJack
.id
);
1724 EXPECT_TRUE(active_input
->active
);
1725 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1727 // Simulate the nodes list in first NodesChanged signal, only headphone is
1728 // removed, other nodes remains the same.
1729 AudioNodeList changed_nodes_1
;
1730 internal_speaker
.active
= false;
1731 changed_nodes_1
.push_back(internal_speaker
);
1732 internal_mic
.active
= false;
1733 changed_nodes_1
.push_back(internal_mic
);
1734 micJack
.active
= true;
1735 changed_nodes_1
.push_back(micJack
);
1737 // Simulate the nodes list in second NodesChanged signal, the micJac is
1738 // removed, but the internal_mic is inactive, which does not reflect the
1739 // active status set from the first NodesChanged signal since this was sent
1740 // before cras receives the SetActiveOutputNode from the first NodesChanged
1742 AudioNodeList changed_nodes_2
;
1743 changed_nodes_2
.push_back(internal_speaker
);
1744 changed_nodes_2
.push_back(internal_mic
);
1746 // Simulate AudioNodesChanged signal being fired twice for unplug an audio
1747 // device with both input and output nodes on it.
1748 ChangeAudioNodes(changed_nodes_1
);
1749 ChangeAudioNodes(changed_nodes_2
);
1751 AudioDeviceList changed_devices
;
1752 cras_audio_handler_
->GetAudioDevices(&changed_devices
);
1753 EXPECT_EQ(2u, changed_devices
.size());
1755 // Verify the active output device is set to internal speaker.
1756 EXPECT_EQ(internal_speaker
.id
,
1757 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1759 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1760 EXPECT_EQ(internal_speaker
.id
, active_output
.id
);
1761 EXPECT_TRUE(active_output
.active
);
1763 // Verify the active input device id is set to internal mic.
1764 EXPECT_EQ(internal_mic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1765 const AudioDevice
* changed_active_input
= GetDeviceFromId(internal_mic
.id
);
1766 EXPECT_TRUE(changed_active_input
->active
);
1769 TEST_F(CrasAudioHandlerTest
, SetOutputMute
) {
1770 AudioNodeList audio_nodes
;
1771 audio_nodes
.push_back(kInternalSpeaker
);
1772 SetUpCrasAudioHandler(audio_nodes
);
1773 EXPECT_EQ(0, test_observer_
->output_mute_changed_count());
1776 cras_audio_handler_
->SetOutputMute(true);
1778 // Verify the output is muted, OnOutputMuteChanged event is fired,
1779 // and mute value is saved in the preferences.
1780 EXPECT_TRUE(cras_audio_handler_
->IsOutputMuted());
1781 EXPECT_EQ(1, test_observer_
->output_mute_changed_count());
1782 AudioDevice
speaker(kInternalSpeaker
);
1783 EXPECT_TRUE(audio_pref_handler_
->GetMuteValue(speaker
));
1785 // Unmute the device.
1786 cras_audio_handler_
->SetOutputMute(false);
1788 // Verify the output is unmuted, OnOutputMuteChanged event is fired,
1789 // and mute value is saved in the preferences.
1790 EXPECT_FALSE(cras_audio_handler_
->IsOutputMuted());
1791 EXPECT_EQ(2, test_observer_
->output_mute_changed_count());
1792 EXPECT_FALSE(audio_pref_handler_
->GetMuteValue(speaker
));
1795 TEST_F(CrasAudioHandlerTest
, SetInputMute
) {
1796 AudioNodeList audio_nodes
;
1797 audio_nodes
.push_back(kInternalMic
);
1798 SetUpCrasAudioHandler(audio_nodes
);
1799 EXPECT_EQ(0, test_observer_
->input_mute_changed_count());
1802 cras_audio_handler_
->SetInputMute(true);
1804 // Verify the input is muted, OnInputMuteChanged event is fired.
1805 EXPECT_TRUE(cras_audio_handler_
->IsInputMuted());
1806 EXPECT_EQ(1, test_observer_
->input_mute_changed_count());
1808 // Unmute the device.
1809 cras_audio_handler_
->SetInputMute(false);
1811 // Verify the input is unmuted, OnInputMuteChanged event is fired.
1812 EXPECT_FALSE(cras_audio_handler_
->IsInputMuted());
1813 EXPECT_EQ(2, test_observer_
->input_mute_changed_count());
1816 TEST_F(CrasAudioHandlerTest
, SetOutputVolumePercent
) {
1817 AudioNodeList audio_nodes
;
1818 audio_nodes
.push_back(kInternalSpeaker
);
1819 SetUpCrasAudioHandler(audio_nodes
);
1820 EXPECT_EQ(0, test_observer_
->output_volume_changed_count());
1822 cras_audio_handler_
->SetOutputVolumePercent(60);
1824 // Verify the output volume is changed to the designated value,
1825 // OnOutputVolumeChanged event is fired, and the device volume value
1826 // is saved the preferences.
1827 const int kVolume
= 60;
1828 EXPECT_EQ(kVolume
, cras_audio_handler_
->GetOutputVolumePercent());
1829 EXPECT_EQ(1, test_observer_
->output_volume_changed_count());
1831 EXPECT_TRUE(cras_audio_handler_
->GetPrimaryActiveOutputDevice(&device
));
1832 EXPECT_EQ(device
.id
, kInternalSpeaker
.id
);
1833 EXPECT_EQ(kVolume
, audio_pref_handler_
->GetOutputVolumeValue(&device
));
1836 TEST_F(CrasAudioHandlerTest
, SetInputGainPercent
) {
1837 AudioNodeList audio_nodes
;
1838 audio_nodes
.push_back(kInternalMic
);
1839 SetUpCrasAudioHandler(audio_nodes
);
1840 EXPECT_EQ(0, test_observer_
->input_gain_changed_count());
1842 cras_audio_handler_
->SetInputGainPercent(60);
1844 // Verify the input gain changed to the designated value,
1845 // OnInputGainChanged event is fired, and the device gain value
1846 // is saved in the preferences.
1847 const int kGain
= 60;
1848 EXPECT_EQ(kGain
, cras_audio_handler_
->GetInputGainPercent());
1849 EXPECT_EQ(1, test_observer_
->input_gain_changed_count());
1850 AudioDevice
internal_mic(kInternalMic
);
1851 EXPECT_EQ(kGain
, audio_pref_handler_
->GetInputGainValue(&internal_mic
));
1854 TEST_F(CrasAudioHandlerTest
, SetMuteForDevice
) {
1855 AudioNodeList audio_nodes
;
1856 audio_nodes
.push_back(kInternalSpeaker
);
1857 audio_nodes
.push_back(kHeadphone
);
1858 audio_nodes
.push_back(kInternalMic
);
1859 audio_nodes
.push_back(kUSBMic
);
1860 SetUpCrasAudioHandler(audio_nodes
);
1862 // Mute the active output device.
1863 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1864 cras_audio_handler_
->SetMuteForDevice(kHeadphone
.id
, true);
1866 // Verify the headphone is muted and mute value is saved in the preferences.
1867 EXPECT_TRUE(cras_audio_handler_
->IsOutputMutedForDevice(kHeadphone
.id
));
1868 AudioDevice
headphone(kHeadphone
);
1869 EXPECT_TRUE(audio_pref_handler_
->GetMuteValue(headphone
));
1871 // Mute the non-active output device.
1872 cras_audio_handler_
->SetMuteForDevice(kInternalSpeaker
.id
, true);
1874 // Verify the internal speaker is muted and mute value is saved in the
1876 EXPECT_TRUE(cras_audio_handler_
->IsOutputMutedForDevice(kInternalSpeaker
.id
));
1877 AudioDevice
internal_speaker(kInternalSpeaker
);
1878 EXPECT_TRUE(audio_pref_handler_
->GetMuteValue(internal_speaker
));
1880 // Mute the active input device.
1881 EXPECT_EQ(kUSBMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1882 cras_audio_handler_
->SetMuteForDevice(kUSBMic
.id
, true);
1884 // Verify the USB Mic is muted.
1885 EXPECT_TRUE(cras_audio_handler_
->IsInputMutedForDevice(kUSBMic
.id
));
1887 // Mute the non-active input device should be a no-op, see crbug.com/365050.
1888 cras_audio_handler_
->SetMuteForDevice(kInternalMic
.id
, true);
1890 // Verify IsInputMutedForDevice returns false for non-active input device.
1891 EXPECT_FALSE(cras_audio_handler_
->IsInputMutedForDevice(kInternalMic
.id
));
1894 TEST_F(CrasAudioHandlerTest
, SetVolumeGainPercentForDevice
) {
1895 AudioNodeList audio_nodes
;
1896 audio_nodes
.push_back(kInternalSpeaker
);
1897 audio_nodes
.push_back(kHeadphone
);
1898 audio_nodes
.push_back(kInternalMic
);
1899 audio_nodes
.push_back(kUSBMic
);
1900 SetUpCrasAudioHandler(audio_nodes
);
1902 // Set volume percent for active output device.
1903 const int kHeadphoneVolume
= 30;
1904 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1905 cras_audio_handler_
->SetVolumeGainPercentForDevice(kHeadphone
.id
,
1908 // Verify the volume percent of headphone is set, and saved in preferences.
1909 EXPECT_EQ(kHeadphoneVolume
,
1910 cras_audio_handler_
->GetOutputVolumePercentForDevice(
1912 AudioDevice
headphone(kHeadphone
);
1913 EXPECT_EQ(kHeadphoneVolume
,
1914 audio_pref_handler_
->GetOutputVolumeValue(&headphone
));
1916 // Set volume percent for non-active output device.
1917 const int kSpeakerVolume
= 60;
1918 cras_audio_handler_
->SetVolumeGainPercentForDevice(kInternalSpeaker
.id
,
1921 // Verify the volume percent of speaker is set, and saved in preferences.
1922 EXPECT_EQ(kSpeakerVolume
,
1923 cras_audio_handler_
->GetOutputVolumePercentForDevice(
1924 kInternalSpeaker
.id
));
1925 AudioDevice
speaker(kInternalSpeaker
);
1926 EXPECT_EQ(kSpeakerVolume
,
1927 audio_pref_handler_
->GetOutputVolumeValue(&speaker
));
1929 // Set gain percent for active input device.
1930 const int kUSBMicGain
= 30;
1931 EXPECT_EQ(kUSBMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1932 cras_audio_handler_
->SetVolumeGainPercentForDevice(kUSBMic
.id
,
1935 // Verify the gain percent of USB mic is set, and saved in preferences.
1936 EXPECT_EQ(kUSBMicGain
,
1937 cras_audio_handler_
->GetOutputVolumePercentForDevice(kUSBMic
.id
));
1938 AudioDevice
usb_mic(kHeadphone
);
1939 EXPECT_EQ(kUSBMicGain
,
1940 audio_pref_handler_
->GetInputGainValue(&usb_mic
));
1942 // Set gain percent for non-active input device.
1943 const int kInternalMicGain
= 60;
1944 cras_audio_handler_
->SetVolumeGainPercentForDevice(kInternalMic
.id
,
1947 // Verify the gain percent of internal mic is set, and saved in preferences.
1948 EXPECT_EQ(kInternalMicGain
,
1949 cras_audio_handler_
->GetOutputVolumePercentForDevice(
1951 AudioDevice
internal_mic(kInternalMic
);
1952 EXPECT_EQ(kInternalMicGain
,
1953 audio_pref_handler_
->GetInputGainValue(&internal_mic
));
1956 TEST_F(CrasAudioHandlerTest
, HandleOtherDeviceType
) {
1957 const size_t kNumValidAudioDevices
= 4;
1958 AudioNodeList audio_nodes
;
1959 audio_nodes
.push_back(kInternalSpeaker
);
1960 audio_nodes
.push_back(kOtherTypeOutput
);
1961 audio_nodes
.push_back(kInternalMic
);
1962 audio_nodes
.push_back(kOtherTypeInput
);
1963 SetUpCrasAudioHandler(audio_nodes
);
1965 // Verify the audio devices size.
1966 AudioDeviceList audio_devices
;
1967 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1968 EXPECT_EQ(kNumValidAudioDevices
, audio_devices
.size());
1970 // Verify the internal speaker has been selected as the active output,
1971 // and the output device with some randown unknown type is handled gracefully.
1972 AudioDevice active_output
;
1974 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1975 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
1976 EXPECT_EQ(kInternalSpeaker
.id
,
1977 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1978 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1980 // Ensure the internal microphone has been selected as the active input,
1981 // and the input device with some random unknown type is handled gracefully.
1982 AudioDevice active_input
;
1983 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1984 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1987 TEST_F(CrasAudioHandlerTest
, ChangeActiveNodesHotrodInit
) {
1988 AudioNodeList audio_nodes
;
1989 audio_nodes
.push_back(kHDMIOutput
);
1990 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
1991 audio_nodes
.push_back(kUSBJabraSpeakerOutput2
);
1992 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
1993 audio_nodes
.push_back(kUSBJabraSpeakerInput2
);
1994 audio_nodes
.push_back(kUSBCameraInput
);
1995 SetUpCrasAudioHandler(audio_nodes
);
1997 // Verify the audio devices size.
1998 AudioDeviceList audio_devices
;
1999 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2000 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2002 // Verify only the 1st jabra speaker's output and input are selected as active
2003 // nodes by CrasAudioHandler.
2004 AudioDevice active_output
;
2006 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
2007 EXPECT_EQ(2, GetActiveDeviceCount());
2008 AudioDevice primary_active_device
;
2009 EXPECT_TRUE(cras_audio_handler_
->GetPrimaryActiveOutputDevice(
2010 &primary_active_device
));
2011 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
, primary_active_device
.id
);
2012 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2013 cras_audio_handler_
->GetPrimaryActiveInputNode());
2015 // Set both jabra speakers's input and output nodes to active, this simulate
2016 // the call sent by hotrod initialization process.
2017 test_observer_
->reset_active_output_node_changed_count();
2018 test_observer_
->reset_active_input_node_changed_count();
2019 CrasAudioHandler::NodeIdList active_nodes
;
2020 active_nodes
.push_back(kUSBJabraSpeakerOutput1
.id
);
2021 active_nodes
.push_back(kUSBJabraSpeakerOutput2
.id
);
2022 active_nodes
.push_back(kUSBJabraSpeakerInput1
.id
);
2023 active_nodes
.push_back(kUSBJabraSpeakerInput2
.id
);
2024 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2026 // Verify both jabra speakers' input/output nodes are made active.
2027 // num_active_nodes = GetActiveDeviceCount();
2028 EXPECT_EQ(4, GetActiveDeviceCount());
2029 const AudioDevice
* active_output_1
=
2030 GetDeviceFromId(kUSBJabraSpeakerOutput1
.id
);
2031 EXPECT_TRUE(active_output_1
->active
);
2032 const AudioDevice
* active_output_2
=
2033 GetDeviceFromId(kUSBJabraSpeakerOutput2
.id
);
2034 EXPECT_TRUE(active_output_2
->active
);
2035 EXPECT_TRUE(cras_audio_handler_
->GetPrimaryActiveOutputDevice(
2036 &primary_active_device
));
2037 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
, primary_active_device
.id
);
2038 const AudioDevice
* active_input_1
=
2039 GetDeviceFromId(kUSBJabraSpeakerInput1
.id
);
2040 EXPECT_TRUE(active_input_1
->active
);
2041 const AudioDevice
* active_input_2
=
2042 GetDeviceFromId(kUSBJabraSpeakerInput2
.id
);
2043 EXPECT_TRUE(active_input_2
->active
);
2044 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2045 cras_audio_handler_
->GetPrimaryActiveInputNode());
2047 // Verify only 1 ActiveOutputNodeChanged notification has been sent out
2048 // by calling ChangeActiveNodes.
2049 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
2050 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
2052 // Verify all active devices are the not muted and their volume values are
2054 EXPECT_FALSE(cras_audio_handler_
->IsOutputMuted());
2056 cras_audio_handler_
->IsOutputMutedForDevice(kUSBJabraSpeakerOutput1
.id
));
2058 cras_audio_handler_
->IsOutputMutedForDevice(kUSBJabraSpeakerOutput2
.id
));
2059 EXPECT_EQ(cras_audio_handler_
->GetOutputVolumePercent(),
2060 cras_audio_handler_
->GetOutputVolumePercentForDevice(
2061 kUSBJabraSpeakerOutput1
.id
));
2062 EXPECT_EQ(cras_audio_handler_
->GetOutputVolumePercent(),
2063 cras_audio_handler_
->GetOutputVolumePercentForDevice(
2064 kUSBJabraSpeakerOutput2
.id
));
2066 // Adjust the volume of output devices, verify all active nodes are set to
2068 cras_audio_handler_
->SetOutputVolumePercent(25);
2069 EXPECT_EQ(25, cras_audio_handler_
->GetOutputVolumePercent());
2070 EXPECT_EQ(25, cras_audio_handler_
->GetOutputVolumePercentForDevice(
2071 kUSBJabraSpeakerOutput1
.id
));
2072 EXPECT_EQ(25, cras_audio_handler_
->GetOutputVolumePercentForDevice(
2073 kUSBJabraSpeakerOutput2
.id
));
2076 TEST_F(CrasAudioHandlerTest
, ChangeActiveNodesHotrodInitWithCameraInputActive
) {
2077 AudioNodeList audio_nodes
;
2078 audio_nodes
.push_back(kHDMIOutput
);
2079 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2080 audio_nodes
.push_back(kUSBJabraSpeakerOutput2
);
2081 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2082 audio_nodes
.push_back(kUSBJabraSpeakerInput2
);
2083 // Make the camera input to be plugged in later than jabra's input.
2084 AudioNode
usb_camera(kUSBCameraInput
);
2085 usb_camera
.plugged_time
= 10000000;
2086 audio_nodes
.push_back(usb_camera
);
2087 SetUpCrasAudioHandler(audio_nodes
);
2089 // Verify the audio devices size.
2090 AudioDeviceList audio_devices
;
2091 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2092 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2094 // Verify the 1st jabra speaker's output is selected as active output
2095 // node and camera's input is selected active input by CrasAudioHandler.
2096 EXPECT_EQ(2, GetActiveDeviceCount());
2097 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2098 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2099 EXPECT_EQ(kUSBCameraInput
.id
,
2100 cras_audio_handler_
->GetPrimaryActiveInputNode());
2102 // Set both jabra speakers's input and output nodes to active, this simulates
2103 // the call sent by hotrod initialization process.
2104 test_observer_
->reset_active_output_node_changed_count();
2105 test_observer_
->reset_active_input_node_changed_count();
2106 CrasAudioHandler::NodeIdList active_nodes
;
2107 active_nodes
.push_back(kUSBJabraSpeakerOutput1
.id
);
2108 active_nodes
.push_back(kUSBJabraSpeakerOutput2
.id
);
2109 active_nodes
.push_back(kUSBJabraSpeakerInput1
.id
);
2110 active_nodes
.push_back(kUSBJabraSpeakerInput2
.id
);
2111 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2113 // Verify both jabra speakers' input/output nodes are made active.
2114 // num_active_nodes = GetActiveDeviceCount();
2115 EXPECT_EQ(4, GetActiveDeviceCount());
2116 const AudioDevice
* active_output_1
=
2117 GetDeviceFromId(kUSBJabraSpeakerOutput1
.id
);
2118 EXPECT_TRUE(active_output_1
->active
);
2119 const AudioDevice
* active_output_2
=
2120 GetDeviceFromId(kUSBJabraSpeakerOutput2
.id
);
2121 EXPECT_TRUE(active_output_2
->active
);
2122 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2123 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2124 const AudioDevice
* active_input_1
=
2125 GetDeviceFromId(kUSBJabraSpeakerInput1
.id
);
2126 EXPECT_TRUE(active_input_1
->active
);
2127 const AudioDevice
* active_input_2
=
2128 GetDeviceFromId(kUSBJabraSpeakerInput2
.id
);
2129 EXPECT_TRUE(active_input_2
->active
);
2130 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2131 cras_audio_handler_
->GetPrimaryActiveInputNode());
2133 // Verify only 1 ActiveOutputNodeChanged notification has been sent out
2134 // by calling ChangeActiveNodes.
2135 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
2136 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
2139 TEST_F(CrasAudioHandlerTest
, ChangeActiveNodesWithFewerActives
) {
2140 AudioNodeList audio_nodes
;
2141 audio_nodes
.push_back(kHDMIOutput
);
2142 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2143 audio_nodes
.push_back(kUSBJabraSpeakerOutput2
);
2144 SetUpCrasAudioHandler(audio_nodes
);
2146 // Verify the audio devices size.
2147 AudioDeviceList audio_devices
;
2148 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2149 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2151 // Set all three nodes to be active.
2152 CrasAudioHandler::NodeIdList active_nodes
;
2153 active_nodes
.push_back(kHDMIOutput
.id
);
2154 active_nodes
.push_back(kUSBJabraSpeakerOutput1
.id
);
2155 active_nodes
.push_back(kUSBJabraSpeakerOutput2
.id
);
2156 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2158 // Verify all three nodes are active.
2159 EXPECT_EQ(3, GetActiveDeviceCount());
2160 const AudioDevice
* active_output_1
= GetDeviceFromId(kHDMIOutput
.id
);
2161 EXPECT_TRUE(active_output_1
->active
);
2162 const AudioDevice
* active_output_2
=
2163 GetDeviceFromId(kUSBJabraSpeakerOutput1
.id
);
2164 EXPECT_TRUE(active_output_2
->active
);
2165 const AudioDevice
* active_output_3
=
2166 GetDeviceFromId(kUSBJabraSpeakerOutput2
.id
);
2167 EXPECT_TRUE(active_output_3
->active
);
2169 // Now call ChangeActiveDevices with only 2 nodes.
2170 active_nodes
.clear();
2171 active_nodes
.push_back(kUSBJabraSpeakerOutput1
.id
);
2172 active_nodes
.push_back(kUSBJabraSpeakerOutput2
.id
);
2173 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2175 // Verify only 2 nodes are active.
2176 EXPECT_EQ(2, GetActiveDeviceCount());
2177 const AudioDevice
* output_1
= GetDeviceFromId(kHDMIOutput
.id
);
2178 EXPECT_FALSE(output_1
->active
);
2179 const AudioDevice
* output_2
= GetDeviceFromId(kUSBJabraSpeakerOutput1
.id
);
2180 EXPECT_TRUE(output_2
->active
);
2181 const AudioDevice
* output_3
= GetDeviceFromId(kUSBJabraSpeakerOutput2
.id
);
2182 EXPECT_TRUE(output_3
->active
);
2185 TEST_F(CrasAudioHandlerTest
, HotrodInitWithSingleJabra
) {
2186 // Simulates the hotrod initializated with a single jabra device and
2187 // CrasAudioHandler selected jabra input/output as active devices.
2188 AudioNodeList audio_nodes
;
2189 audio_nodes
.push_back(kHDMIOutput
);
2190 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2191 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2192 audio_nodes
.push_back(kUSBCameraInput
);
2193 SetUpCrasAudioHandler(audio_nodes
);
2195 // Verify the audio devices size.
2196 AudioDeviceList audio_devices
;
2197 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2198 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2200 // Verify the jabra speaker's output and input are selected as active nodes
2201 // by CrasAudioHandler.
2202 EXPECT_EQ(2, GetActiveDeviceCount());
2203 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2204 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2205 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2206 cras_audio_handler_
->GetPrimaryActiveInputNode());
2209 TEST_F(CrasAudioHandlerTest
,
2210 ChangeActiveNodesHotrodInitWithSingleJabraCameraPlugInLater
) {
2211 AudioNodeList audio_nodes
;
2212 audio_nodes
.push_back(kHDMIOutput
);
2213 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2214 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2215 AudioNode
usb_camera(kUSBCameraInput
);
2216 usb_camera
.plugged_time
= 10000000;
2217 audio_nodes
.push_back(usb_camera
);
2218 SetUpCrasAudioHandler(audio_nodes
);
2220 // Verify the audio devices size.
2221 AudioDeviceList audio_devices
;
2222 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2223 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2225 // Verify the jabra speaker's output is selected as active output, and
2226 // camera's input is selected as active input by CrasAudioHandler
2227 EXPECT_EQ(2, GetActiveDeviceCount());
2228 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2229 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2230 EXPECT_EQ(kUSBCameraInput
.id
,
2231 cras_audio_handler_
->GetPrimaryActiveInputNode());
2233 // Simulate hotrod app call to set jabra input as active device with only
2234 // jabra input node in the active node list, which does not conform to the
2235 // new SetActiveDevices protocol, but just show we can still handle it if
2237 CrasAudioHandler::NodeIdList active_nodes
;
2238 active_nodes
.push_back(kUSBJabraSpeakerOutput1
.id
);
2239 active_nodes
.push_back(kUSBJabraSpeakerInput1
.id
);
2240 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2242 // Verify the jabra speaker's output is selected as active output, and
2243 // jabra's input is selected as active input.
2244 EXPECT_EQ(2, GetActiveDeviceCount());
2245 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2246 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2247 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2248 cras_audio_handler_
->GetPrimaryActiveInputNode());
2251 TEST_F(CrasAudioHandlerTest
,
2252 ChangeActiveNodesHotrodInitWithSingleJabraCameraPlugInLaterOldCall
) {
2253 AudioNodeList audio_nodes
;
2254 audio_nodes
.push_back(kHDMIOutput
);
2255 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2256 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2257 AudioNode
usb_camera(kUSBCameraInput
);
2258 usb_camera
.plugged_time
= 10000000;
2259 audio_nodes
.push_back(usb_camera
);
2260 SetUpCrasAudioHandler(audio_nodes
);
2262 // Verify the audio devices size.
2263 AudioDeviceList audio_devices
;
2264 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2265 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2267 // Verify the jabra speaker's output is selected as active output, and
2268 // camera's input is selected as active input by CrasAudioHandler
2269 EXPECT_EQ(2, GetActiveDeviceCount());
2270 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2271 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2272 EXPECT_EQ(kUSBCameraInput
.id
,
2273 cras_audio_handler_
->GetPrimaryActiveInputNode());
2275 // Simulate hotrod app call to set jabra input as active device with only
2276 // jabra input node in the active node list, which does not conform to the
2277 // new SetActiveDevices protocol, but just show we can still handle it if
2279 CrasAudioHandler::NodeIdList active_nodes
;
2280 active_nodes
.push_back(kUSBJabraSpeakerInput1
.id
);
2281 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2283 // Verify the jabra speaker's output is selected as active output, and
2284 // jabra's input is selected as active input.
2285 EXPECT_EQ(2, GetActiveDeviceCount());
2286 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2287 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2288 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2289 cras_audio_handler_
->GetPrimaryActiveInputNode());
2292 TEST_F(CrasAudioHandlerTest
,
2293 ChangeActiveNodesHotrodInitWithSingleJabraChangeOutput
) {
2294 AudioNodeList audio_nodes
;
2295 audio_nodes
.push_back(kHDMIOutput
);
2296 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2297 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2298 audio_nodes
.push_back(kUSBCameraInput
);
2299 SetUpCrasAudioHandler(audio_nodes
);
2301 // Verify the audio devices size.
2302 AudioDeviceList audio_devices
;
2303 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2304 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2306 // Verify the jabra speaker's output and input are selected as active output
2307 // by CrasAudioHandler.
2308 EXPECT_EQ(2, GetActiveDeviceCount());
2309 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2310 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2311 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2312 cras_audio_handler_
->GetPrimaryActiveInputNode());
2314 // Simulate hotrod app call SetActiveDevices to change active output
2315 // with only complete list of active nodes passed in, which is the new
2316 // way of hotrod app.
2317 CrasAudioHandler::NodeIdList active_nodes
;
2318 active_nodes
.push_back(kHDMIOutput
.id
);
2319 active_nodes
.push_back(kUSBJabraSpeakerInput1
.id
);
2320 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2322 // Verify the jabra speaker's output is selected as active output, and
2323 // jabra's input is selected as active input.
2324 EXPECT_EQ(2, GetActiveDeviceCount());
2325 EXPECT_EQ(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2326 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2327 cras_audio_handler_
->GetPrimaryActiveInputNode());
2330 TEST_F(CrasAudioHandlerTest
,
2331 ChangeActiveNodesHotrodInitWithSingleJabraChangeOutputOldCall
) {
2332 AudioNodeList audio_nodes
;
2333 audio_nodes
.push_back(kHDMIOutput
);
2334 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2335 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2336 audio_nodes
.push_back(kUSBCameraInput
);
2337 SetUpCrasAudioHandler(audio_nodes
);
2339 // Verify the audio devices size.
2340 AudioDeviceList audio_devices
;
2341 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2342 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2344 // Verify the jabra speaker's output and input are selected as active output
2345 // by CrasAudioHandler.
2346 EXPECT_EQ(2, GetActiveDeviceCount());
2347 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2348 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2349 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2350 cras_audio_handler_
->GetPrimaryActiveInputNode());
2352 // Simulate hotrod app call SetActiveDevices to change active output
2353 // with only a single active output nodes passed in, which is the old
2354 // way of hotrod app.
2355 CrasAudioHandler::NodeIdList active_nodes
;
2356 active_nodes
.push_back(kHDMIOutput
.id
);
2357 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2359 // Verify the jabra speaker's output is selected as active output, and
2360 // jabra's input is selected as active input.
2361 EXPECT_EQ(2, GetActiveDeviceCount());
2362 EXPECT_EQ(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2363 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2364 cras_audio_handler_
->GetPrimaryActiveInputNode());
2367 TEST_F(CrasAudioHandlerTest
, NoMoreAudioInputDevices
) {
2368 // Some device like chromebox does not have the internal input device. The
2369 // active devices should be reset when the user plugs a device and then
2370 // unplugs it to such device.
2372 AudioNodeList audio_nodes
;
2373 audio_nodes
.push_back(kInternalSpeaker
);
2374 SetUpCrasAudioHandler(audio_nodes
);
2376 EXPECT_EQ(0ULL, cras_audio_handler_
->GetPrimaryActiveInputNode());
2378 audio_nodes
.push_back(kMicJack
);
2379 ChangeAudioNodes(audio_nodes
);
2381 EXPECT_EQ(kMicJack
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
2382 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
2383 test_observer_
->reset_active_input_node_changed_count();
2385 audio_nodes
.pop_back();
2386 ChangeAudioNodes(audio_nodes
);
2387 EXPECT_EQ(0ULL, cras_audio_handler_
->GetPrimaryActiveInputNode());
2388 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
2391 } // namespace chromeos