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 AudioDevice active_input
;
416 EXPECT_EQ(kUSBMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
417 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
420 TEST_F(CrasAudioHandlerTest
, InitializeWithKeyboardMic
) {
421 AudioNodeList audio_nodes
;
422 audio_nodes
.push_back(kInternalSpeaker
);
423 audio_nodes
.push_back(kInternalMic
);
424 audio_nodes
.push_back(kKeyboardMic
);
425 SetUpCrasAudioHandler(audio_nodes
);
427 // Verify the audio devices size.
428 AudioDeviceList audio_devices
;
429 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
430 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
431 EXPECT_TRUE(cras_audio_handler_
->HasKeyboardMic());
433 // Verify the internal speaker has been selected as the active output.
434 AudioDevice active_output
;
436 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
437 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
438 EXPECT_EQ(kInternalSpeaker
.id
,
439 cras_audio_handler_
->GetPrimaryActiveOutputNode());
440 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
442 // Ensure the internal microphone has been selected as the active input,
443 // not affected by keyboard mic.
444 AudioDevice active_input
;
445 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
446 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
447 const AudioDevice
* keyboard_mic
= GetDeviceFromId(kKeyboardMic
.id
);
448 EXPECT_FALSE(keyboard_mic
->active
);
451 TEST_F(CrasAudioHandlerTest
, SetKeyboardMicActive
) {
452 AudioNodeList audio_nodes
;
453 audio_nodes
.push_back(kInternalMic
);
454 audio_nodes
.push_back(kKeyboardMic
);
455 SetUpCrasAudioHandler(audio_nodes
);
457 // Verify the audio devices size.
458 AudioDeviceList audio_devices
;
459 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
460 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
461 EXPECT_TRUE(cras_audio_handler_
->HasKeyboardMic());
463 // Ensure the internal microphone has been selected as the active input,
464 // not affected by keyboard mic.
465 AudioDevice active_input
;
466 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
467 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
468 const AudioDevice
* keyboard_mic
= GetDeviceFromId(kKeyboardMic
.id
);
469 EXPECT_FALSE(keyboard_mic
->active
);
471 // Make keyboard mic active.
472 cras_audio_handler_
->SetKeyboardMicActive(true);
473 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
474 const AudioDevice
* active_keyboard_mic
= GetDeviceFromId(kKeyboardMic
.id
);
475 EXPECT_TRUE(active_keyboard_mic
->active
);
477 // Make keyboard mic inactive.
478 cras_audio_handler_
->SetKeyboardMicActive(false);
479 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
480 const AudioDevice
* inactive_keyboard_mic
= GetDeviceFromId(kKeyboardMic
.id
);
481 EXPECT_FALSE(inactive_keyboard_mic
->active
);
484 TEST_F(CrasAudioHandlerTest
, SwitchActiveOutputDevice
) {
485 AudioNodeList audio_nodes
;
486 audio_nodes
.push_back(kInternalSpeaker
);
487 audio_nodes
.push_back(kHeadphone
);
488 SetUpCrasAudioHandler(audio_nodes
);
489 AudioDeviceList audio_devices
;
490 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
491 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
493 // Verify the initial active output device is headphone.
494 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
495 AudioDevice active_output
;
497 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
498 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
499 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
501 // Switch the active output to internal speaker.
502 AudioDevice
internal_speaker(kInternalSpeaker
);
503 cras_audio_handler_
->SwitchToDevice(internal_speaker
, true);
505 // Verify the active output is switched to internal speaker, and the
506 // ActiveOutputNodeChanged event is fired.
507 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
509 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
510 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
511 EXPECT_EQ(kInternalSpeaker
.id
,
512 cras_audio_handler_
->GetPrimaryActiveOutputNode());
515 TEST_F(CrasAudioHandlerTest
, SwitchActiveInputDevice
) {
516 AudioNodeList audio_nodes
;
517 audio_nodes
.push_back(kInternalMic
);
518 audio_nodes
.push_back(kUSBMic
);
519 SetUpCrasAudioHandler(audio_nodes
);
520 AudioDeviceList audio_devices
;
521 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
522 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
524 // Verify the initial active input device is USB mic.
525 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
526 EXPECT_EQ(kUSBMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
528 // Switch the active input to internal mic.
529 AudioDevice
internal_mic(kInternalMic
);
530 cras_audio_handler_
->SwitchToDevice(internal_mic
, true);
532 // Verify the active output is switched to internal speaker, and the active
533 // ActiveInputNodeChanged event is fired.
534 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
535 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
538 TEST_F(CrasAudioHandlerTest
, PlugHeadphone
) {
539 // Set up initial audio devices, only with internal speaker.
540 AudioNodeList audio_nodes
;
541 audio_nodes
.push_back(kInternalSpeaker
);
542 SetUpCrasAudioHandler(audio_nodes
);
543 const size_t init_nodes_size
= audio_nodes
.size();
545 // Verify the audio devices size.
546 AudioDeviceList audio_devices
;
547 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
548 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
549 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
551 // Verify the internal speaker has been selected as the active output.
552 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
553 AudioDevice active_output
;
555 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
556 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
557 EXPECT_EQ(kInternalSpeaker
.id
,
558 cras_audio_handler_
->GetPrimaryActiveOutputNode());
559 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
561 // Plug the headphone.
563 AudioNode
internal_speaker(kInternalSpeaker
);
564 internal_speaker
.active
= true;
565 audio_nodes
.push_back(internal_speaker
);
566 audio_nodes
.push_back(kHeadphone
);
567 ChangeAudioNodes(audio_nodes
);
569 // Verify the AudioNodesChanged event is fired and new audio device is added.
570 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
571 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
572 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
574 // Verify the active output device is switched to headphone and
575 // ActiveOutputChanged event is fired.
576 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
578 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
579 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
580 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
581 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
584 TEST_F(CrasAudioHandlerTest
, UnplugHeadphone
) {
585 // Set up initial audio devices, with internal speaker and headphone.
586 AudioNodeList audio_nodes
;
587 audio_nodes
.push_back(kInternalSpeaker
);
588 audio_nodes
.push_back(kHeadphone
);
589 SetUpCrasAudioHandler(audio_nodes
);
590 const size_t init_nodes_size
= audio_nodes
.size();
592 // Verify the audio devices size.
593 AudioDeviceList audio_devices
;
594 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
595 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
596 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
598 // Verify the headphone has been selected as the active output.
599 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
600 AudioDevice active_output
;
602 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
603 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
604 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
605 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
607 // Unplug the headphone.
609 audio_nodes
.push_back(kInternalSpeaker
);
610 ChangeAudioNodes(audio_nodes
);
612 // Verify the AudioNodesChanged event is fired and one audio device is
614 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
615 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
616 EXPECT_EQ(init_nodes_size
- 1, audio_devices
.size());
618 // Verify the active output device is switched to internal speaker and
619 // ActiveOutputChanged event is fired.
620 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
622 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
623 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
624 EXPECT_EQ(kInternalSpeaker
.id
,
625 cras_audio_handler_
->GetPrimaryActiveOutputNode());
626 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
629 TEST_F(CrasAudioHandlerTest
, InitializeWithBluetoothHeadset
) {
630 AudioNodeList audio_nodes
;
631 audio_nodes
.push_back(kInternalSpeaker
);
632 audio_nodes
.push_back(kBluetoothHeadset
);
633 SetUpCrasAudioHandler(audio_nodes
);
635 // Verify the audio devices size.
636 AudioDeviceList audio_devices
;
637 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
638 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
639 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
641 // Verify the bluetooth headset has been selected as the active output.
642 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
643 AudioDevice active_output
;
645 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
646 EXPECT_EQ(kBluetoothHeadset
.id
, active_output
.id
);
647 EXPECT_EQ(kBluetoothHeadset
.id
,
648 cras_audio_handler_
->GetPrimaryActiveOutputNode());
649 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
652 TEST_F(CrasAudioHandlerTest
, ConnectAndDisconnectBluetoothHeadset
) {
653 // Initialize with internal speaker and headphone.
654 AudioNodeList audio_nodes
;
655 audio_nodes
.push_back(kInternalSpeaker
);
656 audio_nodes
.push_back(kHeadphone
);
657 SetUpCrasAudioHandler(audio_nodes
);
658 const size_t init_nodes_size
= audio_nodes
.size();
660 // Verify the audio devices size.
661 AudioDeviceList audio_devices
;
662 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
663 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
664 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
666 // Verify the headphone is selected as the active output initially.
667 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
668 AudioDevice active_output
;
670 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
671 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
672 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
673 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
675 // Connect to bluetooth headset. Since it is plugged in later than
676 // headphone, active output should be switched to it.
678 audio_nodes
.push_back(kInternalSpeaker
);
679 AudioNode
headphone(kHeadphone
);
680 headphone
.plugged_time
= 80000000;
681 headphone
.active
= true;
682 audio_nodes
.push_back(headphone
);
683 AudioNode
bluetooth_headset(kBluetoothHeadset
);
684 bluetooth_headset
.plugged_time
= 90000000;
685 audio_nodes
.push_back(bluetooth_headset
);
686 ChangeAudioNodes(audio_nodes
);
688 // Verify the AudioNodesChanged event is fired and new audio device is added.
689 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
690 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
691 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
693 // Verify the active output device is switched to bluetooth headset, and
694 // ActiveOutputChanged event is fired.
695 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
697 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
698 EXPECT_EQ(kBluetoothHeadset
.id
, active_output
.id
);
699 EXPECT_EQ(kBluetoothHeadset
.id
,
700 cras_audio_handler_
->GetPrimaryActiveOutputNode());
701 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
703 // Disconnect bluetooth headset.
705 audio_nodes
.push_back(kInternalSpeaker
);
706 headphone
.active
= false;
707 audio_nodes
.push_back(headphone
);
708 ChangeAudioNodes(audio_nodes
);
710 // Verify the AudioNodesChanged event is fired and one audio device is
712 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
713 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
714 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
716 // Verify the active output device is switched to headphone, and
717 // ActiveOutputChanged event is fired.
718 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
720 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
721 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
722 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
723 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
726 TEST_F(CrasAudioHandlerTest
, InitializeWithHDMIOutput
) {
727 AudioNodeList audio_nodes
;
728 audio_nodes
.push_back(kInternalSpeaker
);
729 audio_nodes
.push_back(kHDMIOutput
);
730 SetUpCrasAudioHandler(audio_nodes
);
732 // Verify the audio devices size.
733 AudioDeviceList audio_devices
;
734 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
735 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
736 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
738 // Verify the HDMI device has been selected as the active output.
739 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
740 AudioDevice active_output
;
742 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
743 EXPECT_EQ(kHDMIOutput
.id
, active_output
.id
);
744 EXPECT_EQ(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
745 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
748 TEST_F(CrasAudioHandlerTest
, ConnectAndDisconnectHDMIOutput
) {
749 // Initialize with internal speaker.
750 AudioNodeList audio_nodes
;
751 audio_nodes
.push_back(kInternalSpeaker
);
752 SetUpCrasAudioHandler(audio_nodes
);
753 const size_t init_nodes_size
= audio_nodes
.size();
755 // Verify the audio devices size.
756 AudioDeviceList audio_devices
;
757 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
758 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
759 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
761 // Verify the internal speaker is selected as the active output initially.
762 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
763 AudioDevice active_output
;
765 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
766 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
767 EXPECT_EQ(kInternalSpeaker
.id
,
768 cras_audio_handler_
->GetPrimaryActiveOutputNode());
769 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
771 // Connect to HDMI output.
773 AudioNode
internal_speaker(kInternalSpeaker
);
774 internal_speaker
.active
= true;
775 internal_speaker
.plugged_time
= 80000000;
776 audio_nodes
.push_back(internal_speaker
);
777 AudioNode
hdmi(kHDMIOutput
);
778 hdmi
.plugged_time
= 90000000;
779 audio_nodes
.push_back(hdmi
);
780 ChangeAudioNodes(audio_nodes
);
782 // Verify the AudioNodesChanged event is fired and new audio device is added.
783 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
784 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
785 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
787 // Verify the active output device is switched to hdmi output, and
788 // ActiveOutputChanged event is fired.
789 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
791 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
792 EXPECT_EQ(kHDMIOutput
.id
, active_output
.id
);
793 EXPECT_EQ(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
794 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
796 // Disconnect hdmi headset.
798 audio_nodes
.push_back(kInternalSpeaker
);
799 ChangeAudioNodes(audio_nodes
);
801 // Verify the AudioNodesChanged event is fired and one audio device is
803 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
804 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
805 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
807 // Verify the active output device is switched to internal speaker, and
808 // ActiveOutputChanged event is fired.
809 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
811 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
812 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
813 EXPECT_EQ(kInternalSpeaker
.id
,
814 cras_audio_handler_
->GetPrimaryActiveOutputNode());
815 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
818 TEST_F(CrasAudioHandlerTest
, HandleHeadphoneAndHDMIOutput
) {
819 // Initialize with internal speaker, headphone and HDMI output.
820 AudioNodeList audio_nodes
;
821 audio_nodes
.push_back(kInternalSpeaker
);
822 audio_nodes
.push_back(kHeadphone
);
823 audio_nodes
.push_back(kHDMIOutput
);
824 SetUpCrasAudioHandler(audio_nodes
);
825 const size_t init_nodes_size
= audio_nodes
.size();
827 // Verify the audio devices size.
828 AudioDeviceList audio_devices
;
829 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
830 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
831 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
833 // Verify the headphone is selected as the active output initially.
834 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
835 AudioDevice active_output
;
837 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
838 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
839 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
840 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
842 // Disconnect HDMI output.
844 audio_nodes
.push_back(kInternalSpeaker
);
845 audio_nodes
.push_back(kHDMIOutput
);
846 ChangeAudioNodes(audio_nodes
);
848 // Verify the AudioNodesChanged event is fired and one audio device is
850 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
851 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
852 EXPECT_EQ(init_nodes_size
- 1, audio_devices
.size());
854 // Verify the active output device is switched to HDMI output, and
855 // ActiveOutputChanged event is fired.
856 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
858 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
859 EXPECT_EQ(kHDMIOutput
.id
, active_output
.id
);
860 EXPECT_EQ(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
861 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
864 TEST_F(CrasAudioHandlerTest
, InitializeWithUSBHeadphone
) {
865 AudioNodeList audio_nodes
;
866 audio_nodes
.push_back(kInternalSpeaker
);
867 audio_nodes
.push_back(kUSBHeadphone1
);
868 SetUpCrasAudioHandler(audio_nodes
);
870 // Verify the audio devices size.
871 AudioDeviceList audio_devices
;
872 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
873 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
874 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
876 // Verify the usb headphone has been selected as the active output.
877 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
878 AudioDevice active_output
;
880 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
881 EXPECT_EQ(kUSBHeadphone1
.id
, active_output
.id
);
882 EXPECT_EQ(kUSBHeadphone1
.id
,
883 cras_audio_handler_
->GetPrimaryActiveOutputNode());
884 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
887 TEST_F(CrasAudioHandlerTest
, PlugAndUnplugUSBHeadphone
) {
888 // Initialize with internal speaker.
889 AudioNodeList audio_nodes
;
890 audio_nodes
.push_back(kInternalSpeaker
);
891 SetUpCrasAudioHandler(audio_nodes
);
892 const size_t init_nodes_size
= audio_nodes
.size();
894 // Verify the audio devices size.
895 AudioDeviceList audio_devices
;
896 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
897 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
898 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
900 // Verify the internal speaker is selected as the active output initially.
901 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
902 AudioDevice active_output
;
904 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
905 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
906 EXPECT_EQ(kInternalSpeaker
.id
,
907 cras_audio_handler_
->GetPrimaryActiveOutputNode());
908 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
910 // Plug in usb headphone
912 AudioNode
internal_speaker(kInternalSpeaker
);
913 internal_speaker
.active
= true;
914 internal_speaker
.plugged_time
= 80000000;
915 audio_nodes
.push_back(internal_speaker
);
916 AudioNode
usb_headphone(kUSBHeadphone1
);
917 usb_headphone
.plugged_time
= 90000000;
918 audio_nodes
.push_back(usb_headphone
);
919 ChangeAudioNodes(audio_nodes
);
921 // Verify the AudioNodesChanged event is fired and new audio device is added.
922 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
923 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
924 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
926 // Verify the active output device is switched to usb headphone, and
927 // ActiveOutputChanged event is fired.
928 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
930 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
931 EXPECT_EQ(kUSBHeadphone1
.id
, active_output
.id
);
932 EXPECT_EQ(kUSBHeadphone1
.id
,
933 cras_audio_handler_
->GetPrimaryActiveOutputNode());
934 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
936 // Unplug usb headphone.
938 audio_nodes
.push_back(kInternalSpeaker
);
939 ChangeAudioNodes(audio_nodes
);
941 // Verify the AudioNodesChanged event is fired and one audio device is
943 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
944 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
945 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
947 // Verify the active output device is switched to internal speaker, and
948 // ActiveOutputChanged event is fired.
949 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
951 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
952 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
953 EXPECT_EQ(kInternalSpeaker
.id
,
954 cras_audio_handler_
->GetPrimaryActiveOutputNode());
955 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
958 TEST_F(CrasAudioHandlerTest
, HandleMultipleUSBHeadphones
) {
959 // Initialize with internal speaker and one usb headphone.
960 AudioNodeList audio_nodes
;
961 audio_nodes
.push_back(kInternalSpeaker
);
962 audio_nodes
.push_back(kUSBHeadphone1
);
963 SetUpCrasAudioHandler(audio_nodes
);
964 const size_t init_nodes_size
= audio_nodes
.size();
966 // Verify the audio devices size.
967 AudioDeviceList audio_devices
;
968 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
969 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
970 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
972 // Verify the usb headphone is selected as the active output initially.
973 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
974 AudioDevice active_output
;
976 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
977 EXPECT_EQ(kUSBHeadphone1
.id
, active_output
.id
);
978 EXPECT_EQ(kUSBHeadphone1
.id
,
979 cras_audio_handler_
->GetPrimaryActiveOutputNode());
980 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
982 // Plug in another usb headphone.
984 audio_nodes
.push_back(kInternalSpeaker
);
985 AudioNode
usb_headphone_1(kUSBHeadphone1
);
986 usb_headphone_1
.active
= true;
987 usb_headphone_1
.plugged_time
= 80000000;
988 audio_nodes
.push_back(usb_headphone_1
);
989 AudioNode
usb_headphone_2(kUSBHeadphone2
);
990 usb_headphone_2
.plugged_time
= 90000000;
991 audio_nodes
.push_back(usb_headphone_2
);
992 ChangeAudioNodes(audio_nodes
);
994 // Verify the AudioNodesChanged event is fired and new audio device is added.
995 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
996 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
997 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
999 // Verify the active output device is switched to the 2nd usb headphone, which
1000 // is plugged later, and ActiveOutputChanged event is fired.
1001 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1003 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1004 EXPECT_EQ(kUSBHeadphone2
.id
, active_output
.id
);
1005 EXPECT_EQ(kUSBHeadphone2
.id
,
1006 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1007 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1009 // Unplug the 2nd usb headphone.
1010 audio_nodes
.clear();
1011 audio_nodes
.push_back(kInternalSpeaker
);
1012 audio_nodes
.push_back(kUSBHeadphone1
);
1013 ChangeAudioNodes(audio_nodes
);
1015 // Verify the AudioNodesChanged event is fired and one audio device is
1017 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1018 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1019 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1021 // Verify the active output device is switched to the first usb headphone, and
1022 // ActiveOutputChanged event is fired.
1023 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
1025 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1026 EXPECT_EQ(kUSBHeadphone1
.id
, active_output
.id
);
1027 EXPECT_EQ(kUSBHeadphone1
.id
,
1028 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1029 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1032 TEST_F(CrasAudioHandlerTest
, UnplugUSBHeadphonesWithActiveSpeaker
) {
1033 // Initialize with internal speaker and one usb headphone.
1034 AudioNodeList audio_nodes
;
1035 audio_nodes
.push_back(kInternalSpeaker
);
1036 audio_nodes
.push_back(kUSBHeadphone1
);
1037 SetUpCrasAudioHandler(audio_nodes
);
1038 const size_t init_nodes_size
= audio_nodes
.size();
1040 // Verify the audio devices size.
1041 AudioDeviceList audio_devices
;
1042 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1043 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1044 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1046 // Verify the usb headphone is selected as the active output initially.
1047 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1048 AudioDevice active_output
;
1050 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1051 EXPECT_EQ(kUSBHeadphone1
.id
, active_output
.id
);
1052 EXPECT_EQ(kUSBHeadphone1
.id
,
1053 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1054 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1056 // Plug in the headphone jack.
1057 audio_nodes
.clear();
1058 audio_nodes
.push_back(kInternalSpeaker
);
1059 AudioNode
usb_headphone_1(kUSBHeadphone1
);
1060 usb_headphone_1
.active
= true;
1061 usb_headphone_1
.plugged_time
= 80000000;
1062 audio_nodes
.push_back(usb_headphone_1
);
1063 AudioNode
headphone_jack(kHeadphone
);
1064 headphone_jack
.plugged_time
= 90000000;
1065 audio_nodes
.push_back(headphone_jack
);
1066 ChangeAudioNodes(audio_nodes
);
1068 // Verify the AudioNodesChanged event is fired and new audio device is added.
1069 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1070 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1071 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
1073 // Verify the active output device is switched to the headphone jack, which
1074 // is plugged later, and ActiveOutputChanged event is fired.
1075 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1077 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1078 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
1079 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1080 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1082 // Select the speaker to be the active output device.
1083 AudioDevice
internal_speaker(kInternalSpeaker
);
1084 cras_audio_handler_
->SwitchToDevice(internal_speaker
, true);
1086 // Verify the active output is switched to internal speaker, and the
1087 // ActiveOutputNodeChanged event is fired.
1088 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
1090 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1091 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
1092 EXPECT_EQ(kInternalSpeaker
.id
,
1093 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1095 // Unplug the usb headphone.
1096 audio_nodes
.clear();
1097 AudioNode
internal_speaker_node(kInternalSpeaker
);
1098 internal_speaker_node
.active
= true;
1099 internal_speaker_node
.plugged_time
= 70000000;
1100 audio_nodes
.push_back(internal_speaker_node
);
1101 headphone_jack
.active
= false;
1102 audio_nodes
.push_back(headphone_jack
);
1103 ChangeAudioNodes(audio_nodes
);
1105 // Verify the AudioNodesChanged event is fired and one audio device is
1107 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1108 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1109 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1111 // Verify the active output device remains to be speaker.
1112 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
1114 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1115 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
1116 EXPECT_EQ(kInternalSpeaker
.id
,
1117 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1118 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1121 TEST_F(CrasAudioHandlerTest
, OneActiveAudioOutputAfterLoginNewUserSession
) {
1122 // This tests the case found with crbug.com/273271.
1123 // Initialize with internal speaker, bluetooth headphone and headphone jack
1124 // for a new chrome session after user signs out from the previous session.
1125 // Headphone jack is plugged in later than bluetooth headphone, but bluetooth
1126 // headphone is selected as the active output by user from previous user
1128 AudioNodeList audio_nodes
;
1129 audio_nodes
.push_back(kInternalSpeaker
);
1130 AudioNode
bluetooth_headphone(kBluetoothHeadset
);
1131 bluetooth_headphone
.active
= true;
1132 bluetooth_headphone
.plugged_time
= 70000000;
1133 audio_nodes
.push_back(bluetooth_headphone
);
1134 AudioNode
headphone_jack(kHeadphone
);
1135 headphone_jack
.plugged_time
= 80000000;
1136 audio_nodes
.push_back(headphone_jack
);
1137 SetUpCrasAudioHandlerWithPrimaryActiveNode(audio_nodes
, bluetooth_headphone
);
1138 const size_t init_nodes_size
= audio_nodes
.size();
1140 // Verify the audio devices size.
1141 AudioDeviceList audio_devices
;
1142 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1143 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1144 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1146 // Verify the headphone jack is selected as the active output and all other
1147 // audio devices are not active.
1148 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1149 AudioDevice active_output
;
1151 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1152 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
1153 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1154 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1155 for (size_t i
= 0; i
< audio_devices
.size(); ++i
) {
1156 if (audio_devices
[i
].id
!= kHeadphone
.id
)
1157 EXPECT_FALSE(audio_devices
[i
].active
);
1161 TEST_F(CrasAudioHandlerTest
, BluetoothSpeakerIdChangedOnFly
) {
1162 // Initialize with internal speaker and bluetooth headset.
1163 AudioNodeList audio_nodes
;
1164 audio_nodes
.push_back(kInternalSpeaker
);
1165 audio_nodes
.push_back(kBluetoothHeadset
);
1166 SetUpCrasAudioHandler(audio_nodes
);
1167 const size_t init_nodes_size
= audio_nodes
.size();
1169 // Verify the audio devices size.
1170 AudioDeviceList audio_devices
;
1171 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1172 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1173 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1175 // Verify the bluetooth headset is selected as the active output and all other
1176 // audio devices are not active.
1177 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1178 AudioDevice active_output
;
1180 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1181 EXPECT_EQ(kBluetoothHeadset
.id
, active_output
.id
);
1182 EXPECT_EQ(kBluetoothHeadset
.id
,
1183 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1184 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1186 // Cras changes the bluetooth headset's id on the fly.
1187 audio_nodes
.clear();
1188 AudioNode
internal_speaker(kInternalSpeaker
);
1189 internal_speaker
.active
= false;
1190 audio_nodes
.push_back(internal_speaker
);
1191 AudioNode
bluetooth_headphone(kBluetoothHeadset
);
1192 // Change bluetooth headphone id.
1193 bluetooth_headphone
.id
= kBluetoothHeadsetId
+ 20000;
1194 bluetooth_headphone
.active
= false;
1195 audio_nodes
.push_back(bluetooth_headphone
);
1196 ChangeAudioNodes(audio_nodes
);
1198 // Verify NodesChanged event is fired, and the audio devices size is not
1200 audio_devices
.clear();
1201 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1202 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1203 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1205 // Verify ActiveOutputNodeChanged event is fired, and active device should be
1206 // bluetooth headphone.
1207 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1209 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1210 EXPECT_EQ(bluetooth_headphone
.id
, active_output
.id
);
1213 TEST_F(CrasAudioHandlerTest
, PlugUSBMic
) {
1214 // Set up initial audio devices, only with internal mic.
1215 AudioNodeList audio_nodes
;
1216 audio_nodes
.push_back(kInternalMic
);
1217 SetUpCrasAudioHandler(audio_nodes
);
1218 const size_t init_nodes_size
= audio_nodes
.size();
1220 // Verify the audio devices size.
1221 AudioDeviceList audio_devices
;
1222 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1223 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1224 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1226 // Verify the internal mic is selected as the active input.
1227 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1228 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1229 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
1231 // Plug the USB Mic.
1232 audio_nodes
.clear();
1233 AudioNode
internal_mic(kInternalMic
);
1234 internal_mic
.active
= true;
1235 audio_nodes
.push_back(internal_mic
);
1236 audio_nodes
.push_back(kUSBMic
);
1237 ChangeAudioNodes(audio_nodes
);
1239 // Verify the AudioNodesChanged event is fired and new audio device is added.
1240 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1241 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1242 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
1244 // Verify the active input device is switched to USB mic and
1245 // and ActiveInputChanged event is fired.
1246 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
1247 EXPECT_EQ(kUSBMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1248 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1251 TEST_F(CrasAudioHandlerTest
, UnplugUSBMic
) {
1252 // Set up initial audio devices, with internal mic and USB Mic.
1253 AudioNodeList audio_nodes
;
1254 audio_nodes
.push_back(kInternalMic
);
1255 audio_nodes
.push_back(kUSBMic
);
1256 SetUpCrasAudioHandler(audio_nodes
);
1257 const size_t init_nodes_size
= audio_nodes
.size();
1259 // Verify the audio devices size.
1260 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1261 AudioDeviceList audio_devices
;
1262 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1263 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1265 // Verify the USB mic is selected as the active output.
1266 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1267 EXPECT_EQ(kUSBMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1268 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1270 // Unplug the USB Mic.
1271 audio_nodes
.clear();
1272 audio_nodes
.push_back(kInternalMic
);
1273 ChangeAudioNodes(audio_nodes
);
1275 // Verify the AudioNodesChanged event is fired, and one audio device is
1277 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1278 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1279 EXPECT_EQ(init_nodes_size
- 1, audio_devices
.size());
1281 // Verify the active input device is switched to internal mic, and
1282 // and ActiveInputChanged event is fired.
1283 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
1284 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1285 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
1288 TEST_F(CrasAudioHandlerTest
, PlugUSBMicNotAffectActiveOutput
) {
1289 // Set up initial audio devices.
1290 AudioNodeList audio_nodes
;
1291 audio_nodes
.push_back(kInternalSpeaker
);
1292 audio_nodes
.push_back(kHeadphone
);
1293 audio_nodes
.push_back(kInternalMic
);
1294 SetUpCrasAudioHandler(audio_nodes
);
1295 const size_t init_nodes_size
= audio_nodes
.size();
1297 // Verify the audio devices size.
1298 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1299 AudioDeviceList audio_devices
;
1300 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1301 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1303 // Verify the internal mic is selected as the active input.
1304 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1305 EXPECT_EQ(kInternalMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1306 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
1308 // Verify the headphone is selected as the active output.
1309 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1310 EXPECT_EQ(kHeadphoneId
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1311 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1313 // Switch the active output to internal speaker.
1314 AudioDevice
internal_speaker(kInternalSpeaker
);
1315 cras_audio_handler_
->SwitchToDevice(internal_speaker
, true);
1317 // Verify the active output is switched to internal speaker, and the
1318 // ActiveOutputNodeChanged event is fired.
1319 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1320 AudioDevice active_output
;
1322 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1323 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
1324 EXPECT_EQ(kInternalSpeaker
.id
,
1325 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1327 // Plug the USB Mic.
1328 audio_nodes
.clear();
1329 AudioNode
internal_speaker_node(kInternalSpeaker
);
1330 internal_speaker_node
.active
= true;
1331 audio_nodes
.push_back(internal_speaker_node
);
1332 audio_nodes
.push_back(kHeadphone
);
1333 AudioNode
internal_mic(kInternalMic
);
1334 internal_mic
.active
= true;
1335 audio_nodes
.push_back(internal_mic
);
1336 audio_nodes
.push_back(kUSBMic
);
1337 ChangeAudioNodes(audio_nodes
);
1339 // Verify the AudioNodesChanged event is fired, one new device is added.
1340 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1341 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1342 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
1344 // Verify the active input device is switched to USB mic, and
1345 // and ActiveInputChanged event is fired.
1346 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
1347 EXPECT_EQ(kUSBMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1348 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1350 // Verify the active output device is not changed.
1351 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1353 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1354 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
1355 EXPECT_EQ(kInternalSpeaker
.id
,
1356 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1359 TEST_F(CrasAudioHandlerTest
, PlugHeadphoneAutoUnplugSpeakerWithActiveUSB
) {
1360 // Set up initial audio devices.
1361 AudioNodeList audio_nodes
;
1362 audio_nodes
.push_back(kUSBHeadphone1
);
1363 audio_nodes
.push_back(kInternalSpeaker
);
1364 audio_nodes
.push_back(kInternalMic
);
1365 SetUpCrasAudioHandler(audio_nodes
);
1366 const size_t init_nodes_size
= audio_nodes
.size();
1368 // Verify the audio devices size.
1369 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1370 AudioDeviceList audio_devices
;
1371 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1372 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1374 // Verify the internal mic is selected as the active input.
1375 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1376 EXPECT_EQ(kInternalMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1377 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
1379 // Verify the USB headphone is selected as the active output.
1380 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1381 EXPECT_EQ(kUSBHeadphoneId1
,
1382 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1383 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1385 // Plug the headphone and auto-unplug internal speaker.
1386 audio_nodes
.clear();
1387 AudioNode
usb_headphone_node(kUSBHeadphone1
);
1388 usb_headphone_node
.active
= true;
1389 audio_nodes
.push_back(usb_headphone_node
);
1390 AudioNode
headphone_node(kHeadphone
);
1391 headphone_node
.plugged_time
= 1000;
1392 audio_nodes
.push_back(headphone_node
);
1393 AudioNode
internal_mic(kInternalMic
);
1394 internal_mic
.active
= true;
1395 audio_nodes
.push_back(internal_mic
);
1396 ChangeAudioNodes(audio_nodes
);
1398 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1399 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1400 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1401 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1403 // Verify the active output device is switched to headphone, and
1404 // an ActiveOutputChanged event is fired.
1405 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1406 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1407 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1409 // Unplug the headphone and internal speaker auto-plugs back.
1410 audio_nodes
.clear();
1411 audio_nodes
.push_back(kUSBHeadphone1
);
1412 AudioNode
internal_speaker_node(kInternalSpeaker
);
1413 internal_speaker_node
.plugged_time
= 2000;
1414 audio_nodes
.push_back(internal_speaker_node
);
1415 audio_nodes
.push_back(internal_mic
);
1416 ChangeAudioNodes(audio_nodes
);
1418 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1419 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1420 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1421 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1423 // Verify the active output device is switched back to USB, and
1424 // an ActiveOutputChanged event is fired.
1425 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
1426 EXPECT_EQ(kUSBHeadphone1
.id
,
1427 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1428 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1430 // Verify the active input device is not changed.
1431 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1432 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1435 TEST_F(CrasAudioHandlerTest
, PlugMicAutoUnplugInternalMicWithActiveUSB
) {
1436 // Set up initial audio devices.
1437 AudioNodeList audio_nodes
;
1438 audio_nodes
.push_back(kUSBHeadphone1
);
1439 audio_nodes
.push_back(kInternalSpeaker
);
1440 audio_nodes
.push_back(kUSBMic
);
1441 audio_nodes
.push_back(kInternalMic
);
1442 SetUpCrasAudioHandler(audio_nodes
);
1443 const size_t init_nodes_size
= audio_nodes
.size();
1445 // Verify the audio devices size.
1446 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1447 AudioDeviceList audio_devices
;
1448 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1449 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1451 // Verify the internal mic is selected as the active input.
1452 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1453 EXPECT_EQ(kUSBMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1454 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1456 // Verify the internal speaker is selected as the active output.
1457 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1458 EXPECT_EQ(kUSBHeadphoneId1
,
1459 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1460 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1462 // Plug the headphone and mic, auto-unplug internal mic and speaker.
1463 audio_nodes
.clear();
1464 AudioNode
usb_headphone_node(kUSBHeadphone1
);
1465 usb_headphone_node
.active
= true;
1466 audio_nodes
.push_back(usb_headphone_node
);
1467 AudioNode
headphone_node(kHeadphone
);
1468 headphone_node
.plugged_time
= 1000;
1469 audio_nodes
.push_back(headphone_node
);
1470 AudioNode
usb_mic(kUSBMic
);
1471 usb_mic
.active
= true;
1472 audio_nodes
.push_back(usb_mic
);
1473 AudioNode
mic_jack(kMicJack
);
1474 mic_jack
.plugged_time
= 1000;
1475 audio_nodes
.push_back(mic_jack
);
1476 ChangeAudioNodes(audio_nodes
);
1478 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1479 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1480 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1481 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1483 // Verify the active output device is switched to headphone, and
1484 // an ActiveOutputChanged event is fired.
1485 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1486 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1487 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1489 // Verify the active input device is switched to mic jack, and
1490 // an ActiveInputChanged event is fired.
1491 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
1492 EXPECT_EQ(kMicJack
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1493 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1495 // Unplug the headphone and internal speaker auto-plugs back.
1496 audio_nodes
.clear();
1497 audio_nodes
.push_back(kUSBHeadphone1
);
1498 AudioNode
internal_speaker_node(kInternalSpeaker
);
1499 internal_speaker_node
.plugged_time
= 2000;
1500 audio_nodes
.push_back(internal_speaker_node
);
1501 audio_nodes
.push_back(kUSBMic
);
1502 AudioNode
internal_mic(kInternalMic
);
1503 internal_mic
.plugged_time
= 2000;
1504 audio_nodes
.push_back(internal_mic
);
1505 ChangeAudioNodes(audio_nodes
);
1507 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1508 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1509 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1510 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1512 // Verify the active output device is switched back to USB, and
1513 // an ActiveOutputChanged event is fired.
1514 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
1515 EXPECT_EQ(kUSBHeadphone1
.id
,
1516 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1517 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1519 // Verify the active input device is switched back to USB mic, and
1520 // an ActiveInputChanged event is fired.
1521 EXPECT_EQ(2, test_observer_
->active_input_node_changed_count());
1522 EXPECT_EQ(kUSBMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1523 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1526 TEST_F(CrasAudioHandlerTest
, MultipleNodesChangedSignalsOnPlugInHeadphone
) {
1527 // Set up initial audio devices.
1528 AudioNodeList audio_nodes
;
1529 audio_nodes
.push_back(kInternalSpeaker
);
1530 audio_nodes
.push_back(kBluetoothHeadset
);
1531 SetUpCrasAudioHandler(audio_nodes
);
1532 const size_t init_nodes_size
= audio_nodes
.size();
1534 // Verify the audio devices size.
1535 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1536 AudioDeviceList audio_devices
;
1537 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1538 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1540 // Verify the bluetooth headset is selected as the active output.
1541 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1542 EXPECT_EQ(kBluetoothHeadsetId
,
1543 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1544 AudioDevice active_output
;
1546 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1547 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1549 // Plug in headphone, but fire NodesChanged signal twice.
1550 audio_nodes
.clear();
1551 audio_nodes
.push_back(kInternalSpeaker
);
1552 AudioNode
bluetooth_headset(kBluetoothHeadset
);
1553 bluetooth_headset
.plugged_time
= 1000;
1554 bluetooth_headset
.active
= true;
1555 audio_nodes
.push_back(bluetooth_headset
);
1556 AudioNode
headphone(kHeadphone
);
1557 headphone
.active
= false;
1558 headphone
.plugged_time
= 2000;
1559 audio_nodes
.push_back(headphone
);
1560 ChangeAudioNodes(audio_nodes
);
1561 ChangeAudioNodes(audio_nodes
);
1563 // Verify the active output device is set to headphone.
1564 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1565 EXPECT_LE(1, test_observer_
->active_output_node_changed_count());
1566 EXPECT_EQ(headphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1568 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1569 EXPECT_EQ(headphone
.id
, active_output
.id
);
1571 // Verfiy the audio devices data is consistent, i.e., the active output device
1572 // should be headphone.
1573 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1574 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
1575 for (size_t i
= 0; i
< audio_devices
.size(); ++i
) {
1576 if (audio_devices
[i
].id
== kInternalSpeaker
.id
)
1577 EXPECT_FALSE(audio_devices
[i
].active
);
1578 else if (audio_devices
[i
].id
== bluetooth_headset
.id
)
1579 EXPECT_FALSE(audio_devices
[i
].active
);
1580 else if (audio_devices
[i
].id
== headphone
.id
)
1581 EXPECT_TRUE(audio_devices
[i
].active
);
1587 TEST_F(CrasAudioHandlerTest
, MultipleNodesChangedSignalsOnPlugInUSBMic
) {
1588 // Set up initial audio devices.
1589 AudioNodeList audio_nodes
;
1590 audio_nodes
.push_back(kInternalMic
);
1591 SetUpCrasAudioHandler(audio_nodes
);
1592 const size_t init_nodes_size
= audio_nodes
.size();
1594 // Verify the audio devices size.
1595 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1596 AudioDeviceList audio_devices
;
1597 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1598 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1600 // Verify the internal mic is selected as the active output.
1601 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1602 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1603 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
1604 EXPECT_TRUE(audio_devices
[0].active
);
1606 // Plug in usb mic, but fire NodesChanged signal twice.
1607 audio_nodes
.clear();
1608 AudioNode
internal_mic(kInternalMic
);
1609 internal_mic
.active
= true;
1610 internal_mic
.plugged_time
= 1000;
1611 audio_nodes
.push_back(internal_mic
);
1612 AudioNode
usb_mic(kUSBMic
);
1613 usb_mic
.active
= false;
1614 usb_mic
.plugged_time
= 2000;
1615 audio_nodes
.push_back(usb_mic
);
1616 ChangeAudioNodes(audio_nodes
);
1617 ChangeAudioNodes(audio_nodes
);
1619 // Verify the active output device is set to headphone.
1620 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1621 EXPECT_LE(1, test_observer_
->active_input_node_changed_count());
1622 EXPECT_EQ(usb_mic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1623 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1625 // Verfiy the audio devices data is consistent, i.e., the active input device
1626 // should be usb mic.
1627 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1628 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
1629 for (size_t i
= 0; i
< audio_devices
.size(); ++i
) {
1630 if (audio_devices
[i
].id
== kInternalMic
.id
)
1631 EXPECT_FALSE(audio_devices
[i
].active
);
1632 else if (audio_devices
[i
].id
== usb_mic
.id
)
1633 EXPECT_TRUE(audio_devices
[i
].active
);
1639 // This is the case of crbug.com/291303.
1640 TEST_F(CrasAudioHandlerTest
, MultipleNodesChangedSignalsOnSystemBoot
) {
1641 // Set up audio handler with empty audio_nodes.
1642 AudioNodeList audio_nodes
;
1643 SetUpCrasAudioHandler(audio_nodes
);
1645 AudioNode
internal_speaker(kInternalSpeaker
);
1646 internal_speaker
.active
= false;
1647 AudioNode
headphone(kHeadphone
);
1648 headphone
.active
= false;
1649 AudioNode
internal_mic(kInternalMic
);
1650 internal_mic
.active
= false;
1651 audio_nodes
.push_back(internal_speaker
);
1652 audio_nodes
.push_back(headphone
);
1653 audio_nodes
.push_back(internal_mic
);
1654 const size_t init_nodes_size
= audio_nodes
.size();
1656 // Simulate AudioNodesChanged signal being fired twice during system boot.
1657 ChangeAudioNodes(audio_nodes
);
1658 ChangeAudioNodes(audio_nodes
);
1660 // Verify the active output device is set to headphone.
1661 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1662 EXPECT_LE(1, test_observer_
->active_output_node_changed_count());
1663 EXPECT_EQ(headphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1664 AudioDevice active_output
;
1666 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1667 EXPECT_EQ(headphone
.id
, active_output
.id
);
1669 // Verify the active input device id is set to internal mic.
1670 EXPECT_EQ(internal_mic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1672 // Verfiy the audio devices data is consistent, i.e., the active output device
1673 // should be headphone, and the active input device should internal mic.
1674 AudioDeviceList audio_devices
;
1675 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1676 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1677 for (size_t i
= 0; i
< audio_devices
.size(); ++i
) {
1678 if (audio_devices
[i
].id
== internal_speaker
.id
)
1679 EXPECT_FALSE(audio_devices
[i
].active
);
1680 else if (audio_devices
[i
].id
== headphone
.id
)
1681 EXPECT_TRUE(audio_devices
[i
].active
);
1682 else if (audio_devices
[i
].id
== internal_mic
.id
)
1683 EXPECT_TRUE(audio_devices
[i
].active
);
1689 TEST_F(CrasAudioHandlerTest
, SetOutputMute
) {
1690 AudioNodeList audio_nodes
;
1691 audio_nodes
.push_back(kInternalSpeaker
);
1692 SetUpCrasAudioHandler(audio_nodes
);
1693 EXPECT_EQ(0, test_observer_
->output_mute_changed_count());
1696 cras_audio_handler_
->SetOutputMute(true);
1698 // Verify the output is muted, OnOutputMuteChanged event is fired,
1699 // and mute value is saved in the preferences.
1700 EXPECT_TRUE(cras_audio_handler_
->IsOutputMuted());
1701 EXPECT_EQ(1, test_observer_
->output_mute_changed_count());
1702 AudioDevice
speaker(kInternalSpeaker
);
1703 EXPECT_TRUE(audio_pref_handler_
->GetMuteValue(speaker
));
1705 // Unmute the device.
1706 cras_audio_handler_
->SetOutputMute(false);
1708 // Verify the output is unmuted, OnOutputMuteChanged event is fired,
1709 // and mute value is saved in the preferences.
1710 EXPECT_FALSE(cras_audio_handler_
->IsOutputMuted());
1711 EXPECT_EQ(2, test_observer_
->output_mute_changed_count());
1712 EXPECT_FALSE(audio_pref_handler_
->GetMuteValue(speaker
));
1715 TEST_F(CrasAudioHandlerTest
, SetInputMute
) {
1716 AudioNodeList audio_nodes
;
1717 audio_nodes
.push_back(kInternalMic
);
1718 SetUpCrasAudioHandler(audio_nodes
);
1719 EXPECT_EQ(0, test_observer_
->input_mute_changed_count());
1722 cras_audio_handler_
->SetInputMute(true);
1724 // Verify the input is muted, OnInputMuteChanged event is fired.
1725 EXPECT_TRUE(cras_audio_handler_
->IsInputMuted());
1726 EXPECT_EQ(1, test_observer_
->input_mute_changed_count());
1728 // Unmute the device.
1729 cras_audio_handler_
->SetInputMute(false);
1731 // Verify the input is unmuted, OnInputMuteChanged event is fired.
1732 EXPECT_FALSE(cras_audio_handler_
->IsInputMuted());
1733 EXPECT_EQ(2, test_observer_
->input_mute_changed_count());
1736 TEST_F(CrasAudioHandlerTest
, SetOutputVolumePercent
) {
1737 AudioNodeList audio_nodes
;
1738 audio_nodes
.push_back(kInternalSpeaker
);
1739 SetUpCrasAudioHandler(audio_nodes
);
1740 EXPECT_EQ(0, test_observer_
->output_volume_changed_count());
1742 cras_audio_handler_
->SetOutputVolumePercent(60);
1744 // Verify the output volume is changed to the designated value,
1745 // OnOutputVolumeChanged event is fired, and the device volume value
1746 // is saved the preferences.
1747 const int kVolume
= 60;
1748 EXPECT_EQ(kVolume
, cras_audio_handler_
->GetOutputVolumePercent());
1749 EXPECT_EQ(1, test_observer_
->output_volume_changed_count());
1751 EXPECT_TRUE(cras_audio_handler_
->GetPrimaryActiveOutputDevice(&device
));
1752 EXPECT_EQ(device
.id
, kInternalSpeaker
.id
);
1753 EXPECT_EQ(kVolume
, audio_pref_handler_
->GetOutputVolumeValue(&device
));
1756 TEST_F(CrasAudioHandlerTest
, SetInputGainPercent
) {
1757 AudioNodeList audio_nodes
;
1758 audio_nodes
.push_back(kInternalMic
);
1759 SetUpCrasAudioHandler(audio_nodes
);
1760 EXPECT_EQ(0, test_observer_
->input_gain_changed_count());
1762 cras_audio_handler_
->SetInputGainPercent(60);
1764 // Verify the input gain changed to the designated value,
1765 // OnInputGainChanged event is fired, and the device gain value
1766 // is saved in the preferences.
1767 const int kGain
= 60;
1768 EXPECT_EQ(kGain
, cras_audio_handler_
->GetInputGainPercent());
1769 EXPECT_EQ(1, test_observer_
->input_gain_changed_count());
1770 AudioDevice
internal_mic(kInternalMic
);
1771 EXPECT_EQ(kGain
, audio_pref_handler_
->GetInputGainValue(&internal_mic
));
1774 TEST_F(CrasAudioHandlerTest
, SetMuteForDevice
) {
1775 AudioNodeList audio_nodes
;
1776 audio_nodes
.push_back(kInternalSpeaker
);
1777 audio_nodes
.push_back(kHeadphone
);
1778 audio_nodes
.push_back(kInternalMic
);
1779 audio_nodes
.push_back(kUSBMic
);
1780 SetUpCrasAudioHandler(audio_nodes
);
1782 // Mute the active output device.
1783 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1784 cras_audio_handler_
->SetMuteForDevice(kHeadphone
.id
, true);
1786 // Verify the headphone is muted and mute value is saved in the preferences.
1787 EXPECT_TRUE(cras_audio_handler_
->IsOutputMutedForDevice(kHeadphone
.id
));
1788 AudioDevice
headphone(kHeadphone
);
1789 EXPECT_TRUE(audio_pref_handler_
->GetMuteValue(headphone
));
1791 // Mute the non-active output device.
1792 cras_audio_handler_
->SetMuteForDevice(kInternalSpeaker
.id
, true);
1794 // Verify the internal speaker is muted and mute value is saved in the
1796 EXPECT_TRUE(cras_audio_handler_
->IsOutputMutedForDevice(kInternalSpeaker
.id
));
1797 AudioDevice
internal_speaker(kInternalSpeaker
);
1798 EXPECT_TRUE(audio_pref_handler_
->GetMuteValue(internal_speaker
));
1800 // Mute the active input device.
1801 EXPECT_EQ(kUSBMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1802 cras_audio_handler_
->SetMuteForDevice(kUSBMic
.id
, true);
1804 // Verify the USB Mic is muted.
1805 EXPECT_TRUE(cras_audio_handler_
->IsInputMutedForDevice(kUSBMic
.id
));
1807 // Mute the non-active input device should be a no-op, see crbug.com/365050.
1808 cras_audio_handler_
->SetMuteForDevice(kInternalMic
.id
, true);
1810 // Verify IsInputMutedForDevice returns false for non-active input device.
1811 EXPECT_FALSE(cras_audio_handler_
->IsInputMutedForDevice(kInternalMic
.id
));
1814 TEST_F(CrasAudioHandlerTest
, SetVolumeGainPercentForDevice
) {
1815 AudioNodeList audio_nodes
;
1816 audio_nodes
.push_back(kInternalSpeaker
);
1817 audio_nodes
.push_back(kHeadphone
);
1818 audio_nodes
.push_back(kInternalMic
);
1819 audio_nodes
.push_back(kUSBMic
);
1820 SetUpCrasAudioHandler(audio_nodes
);
1822 // Set volume percent for active output device.
1823 const int kHeadphoneVolume
= 30;
1824 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1825 cras_audio_handler_
->SetVolumeGainPercentForDevice(kHeadphone
.id
,
1828 // Verify the volume percent of headphone is set, and saved in preferences.
1829 EXPECT_EQ(kHeadphoneVolume
,
1830 cras_audio_handler_
->GetOutputVolumePercentForDevice(
1832 AudioDevice
headphone(kHeadphone
);
1833 EXPECT_EQ(kHeadphoneVolume
,
1834 audio_pref_handler_
->GetOutputVolumeValue(&headphone
));
1836 // Set volume percent for non-active output device.
1837 const int kSpeakerVolume
= 60;
1838 cras_audio_handler_
->SetVolumeGainPercentForDevice(kInternalSpeaker
.id
,
1841 // Verify the volume percent of speaker is set, and saved in preferences.
1842 EXPECT_EQ(kSpeakerVolume
,
1843 cras_audio_handler_
->GetOutputVolumePercentForDevice(
1844 kInternalSpeaker
.id
));
1845 AudioDevice
speaker(kInternalSpeaker
);
1846 EXPECT_EQ(kSpeakerVolume
,
1847 audio_pref_handler_
->GetOutputVolumeValue(&speaker
));
1849 // Set gain percent for active input device.
1850 const int kUSBMicGain
= 30;
1851 EXPECT_EQ(kUSBMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1852 cras_audio_handler_
->SetVolumeGainPercentForDevice(kUSBMic
.id
,
1855 // Verify the gain percent of USB mic is set, and saved in preferences.
1856 EXPECT_EQ(kUSBMicGain
,
1857 cras_audio_handler_
->GetOutputVolumePercentForDevice(kUSBMic
.id
));
1858 AudioDevice
usb_mic(kHeadphone
);
1859 EXPECT_EQ(kUSBMicGain
,
1860 audio_pref_handler_
->GetInputGainValue(&usb_mic
));
1862 // Set gain percent for non-active input device.
1863 const int kInternalMicGain
= 60;
1864 cras_audio_handler_
->SetVolumeGainPercentForDevice(kInternalMic
.id
,
1867 // Verify the gain percent of internal mic is set, and saved in preferences.
1868 EXPECT_EQ(kInternalMicGain
,
1869 cras_audio_handler_
->GetOutputVolumePercentForDevice(
1871 AudioDevice
internal_mic(kInternalMic
);
1872 EXPECT_EQ(kInternalMicGain
,
1873 audio_pref_handler_
->GetInputGainValue(&internal_mic
));
1876 TEST_F(CrasAudioHandlerTest
, HandleOtherDeviceType
) {
1877 const size_t kNumValidAudioDevices
= 4;
1878 AudioNodeList audio_nodes
;
1879 audio_nodes
.push_back(kInternalSpeaker
);
1880 audio_nodes
.push_back(kOtherTypeOutput
);
1881 audio_nodes
.push_back(kInternalMic
);
1882 audio_nodes
.push_back(kOtherTypeInput
);
1883 SetUpCrasAudioHandler(audio_nodes
);
1885 // Verify the audio devices size.
1886 AudioDeviceList audio_devices
;
1887 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1888 EXPECT_EQ(kNumValidAudioDevices
, audio_devices
.size());
1890 // Verify the internal speaker has been selected as the active output,
1891 // and the output device with some randown unknown type is handled gracefully.
1892 AudioDevice active_output
;
1894 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1895 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
1896 EXPECT_EQ(kInternalSpeaker
.id
,
1897 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1898 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1900 // Ensure the internal microphone has been selected as the active input,
1901 // and the input device with some random unknown type is handled gracefully.
1902 AudioDevice active_input
;
1903 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1904 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1907 TEST_F(CrasAudioHandlerTest
, ChangeActiveNodesHotrodInit
) {
1908 AudioNodeList audio_nodes
;
1909 audio_nodes
.push_back(kHDMIOutput
);
1910 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
1911 audio_nodes
.push_back(kUSBJabraSpeakerOutput2
);
1912 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
1913 audio_nodes
.push_back(kUSBJabraSpeakerInput2
);
1914 audio_nodes
.push_back(kUSBCameraInput
);
1915 SetUpCrasAudioHandler(audio_nodes
);
1917 // Verify the audio devices size.
1918 AudioDeviceList audio_devices
;
1919 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1920 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
1922 // Verify only the 1st jabra speaker's output and input are selected as active
1923 // nodes by CrasAudioHandler.
1924 AudioDevice active_output
;
1926 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1927 EXPECT_EQ(2, GetActiveDeviceCount());
1928 AudioDevice primary_active_device
;
1929 EXPECT_TRUE(cras_audio_handler_
->GetPrimaryActiveOutputDevice(
1930 &primary_active_device
));
1931 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
, primary_active_device
.id
);
1932 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
1933 cras_audio_handler_
->GetPrimaryActiveInputNode());
1935 // Set both jabra speakers's input and output nodes to active, this simulate
1936 // the call sent by hotrod initialization process.
1937 test_observer_
->reset_active_output_node_changed_count();
1938 test_observer_
->reset_active_input_node_changed_count();
1939 CrasAudioHandler::NodeIdList active_nodes
;
1940 active_nodes
.push_back(kUSBJabraSpeakerOutput1
.id
);
1941 active_nodes
.push_back(kUSBJabraSpeakerOutput2
.id
);
1942 active_nodes
.push_back(kUSBJabraSpeakerInput1
.id
);
1943 active_nodes
.push_back(kUSBJabraSpeakerInput2
.id
);
1944 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
1946 // Verify both jabra speakers' input/output nodes are made active.
1947 // num_active_nodes = GetActiveDeviceCount();
1948 EXPECT_EQ(4, GetActiveDeviceCount());
1949 const AudioDevice
* active_output_1
=
1950 GetDeviceFromId(kUSBJabraSpeakerOutput1
.id
);
1951 EXPECT_TRUE(active_output_1
->active
);
1952 const AudioDevice
* active_output_2
=
1953 GetDeviceFromId(kUSBJabraSpeakerOutput2
.id
);
1954 EXPECT_TRUE(active_output_2
->active
);
1955 EXPECT_TRUE(cras_audio_handler_
->GetPrimaryActiveOutputDevice(
1956 &primary_active_device
));
1957 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
, primary_active_device
.id
);
1958 const AudioDevice
* active_input_1
=
1959 GetDeviceFromId(kUSBJabraSpeakerInput1
.id
);
1960 EXPECT_TRUE(active_input_1
->active
);
1961 const AudioDevice
* active_input_2
=
1962 GetDeviceFromId(kUSBJabraSpeakerInput2
.id
);
1963 EXPECT_TRUE(active_input_2
->active
);
1964 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
1965 cras_audio_handler_
->GetPrimaryActiveInputNode());
1967 // Verify only 1 ActiveOutputNodeChanged notification has been sent out
1968 // by calling ChangeActiveNodes.
1969 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1970 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
1972 // Verify all active devices are the not muted and their volume values are
1974 EXPECT_FALSE(cras_audio_handler_
->IsOutputMuted());
1976 cras_audio_handler_
->IsOutputMutedForDevice(kUSBJabraSpeakerOutput1
.id
));
1978 cras_audio_handler_
->IsOutputMutedForDevice(kUSBJabraSpeakerOutput2
.id
));
1979 EXPECT_EQ(cras_audio_handler_
->GetOutputVolumePercent(),
1980 cras_audio_handler_
->GetOutputVolumePercentForDevice(
1981 kUSBJabraSpeakerOutput1
.id
));
1982 EXPECT_EQ(cras_audio_handler_
->GetOutputVolumePercent(),
1983 cras_audio_handler_
->GetOutputVolumePercentForDevice(
1984 kUSBJabraSpeakerOutput2
.id
));
1986 // Adjust the volume of output devices, verify all active nodes are set to
1988 cras_audio_handler_
->SetOutputVolumePercent(25);
1989 EXPECT_EQ(25, cras_audio_handler_
->GetOutputVolumePercent());
1990 EXPECT_EQ(25, cras_audio_handler_
->GetOutputVolumePercentForDevice(
1991 kUSBJabraSpeakerOutput1
.id
));
1992 EXPECT_EQ(25, cras_audio_handler_
->GetOutputVolumePercentForDevice(
1993 kUSBJabraSpeakerOutput2
.id
));
1996 TEST_F(CrasAudioHandlerTest
, ChangeActiveNodesHotrodInitWithCameraInputActive
) {
1997 AudioNodeList audio_nodes
;
1998 audio_nodes
.push_back(kHDMIOutput
);
1999 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2000 audio_nodes
.push_back(kUSBJabraSpeakerOutput2
);
2001 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2002 audio_nodes
.push_back(kUSBJabraSpeakerInput2
);
2003 // Make the camera input to be plugged in later than jabra's input.
2004 AudioNode
usb_camera(kUSBCameraInput
);
2005 usb_camera
.plugged_time
= 10000000;
2006 audio_nodes
.push_back(usb_camera
);
2007 SetUpCrasAudioHandler(audio_nodes
);
2009 // Verify the audio devices size.
2010 AudioDeviceList audio_devices
;
2011 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2012 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2014 // Verify the 1st jabra speaker's output is selected as active output
2015 // node and camera's input is selected active input by CrasAudioHandler.
2016 EXPECT_EQ(2, GetActiveDeviceCount());
2017 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2018 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2019 EXPECT_EQ(kUSBCameraInput
.id
,
2020 cras_audio_handler_
->GetPrimaryActiveInputNode());
2022 // Set both jabra speakers's input and output nodes to active, this simulates
2023 // the call sent by hotrod initialization process.
2024 test_observer_
->reset_active_output_node_changed_count();
2025 test_observer_
->reset_active_input_node_changed_count();
2026 CrasAudioHandler::NodeIdList active_nodes
;
2027 active_nodes
.push_back(kUSBJabraSpeakerOutput1
.id
);
2028 active_nodes
.push_back(kUSBJabraSpeakerOutput2
.id
);
2029 active_nodes
.push_back(kUSBJabraSpeakerInput1
.id
);
2030 active_nodes
.push_back(kUSBJabraSpeakerInput2
.id
);
2031 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2033 // Verify both jabra speakers' input/output nodes are made active.
2034 // num_active_nodes = GetActiveDeviceCount();
2035 EXPECT_EQ(4, GetActiveDeviceCount());
2036 const AudioDevice
* active_output_1
=
2037 GetDeviceFromId(kUSBJabraSpeakerOutput1
.id
);
2038 EXPECT_TRUE(active_output_1
->active
);
2039 const AudioDevice
* active_output_2
=
2040 GetDeviceFromId(kUSBJabraSpeakerOutput2
.id
);
2041 EXPECT_TRUE(active_output_2
->active
);
2042 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2043 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2044 const AudioDevice
* active_input_1
=
2045 GetDeviceFromId(kUSBJabraSpeakerInput1
.id
);
2046 EXPECT_TRUE(active_input_1
->active
);
2047 const AudioDevice
* active_input_2
=
2048 GetDeviceFromId(kUSBJabraSpeakerInput2
.id
);
2049 EXPECT_TRUE(active_input_2
->active
);
2050 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2051 cras_audio_handler_
->GetPrimaryActiveInputNode());
2053 // Verify only 1 ActiveOutputNodeChanged notification has been sent out
2054 // by calling ChangeActiveNodes.
2055 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
2056 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
2059 TEST_F(CrasAudioHandlerTest
, ChangeActiveNodesWithFewerActives
) {
2060 AudioNodeList audio_nodes
;
2061 audio_nodes
.push_back(kHDMIOutput
);
2062 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2063 audio_nodes
.push_back(kUSBJabraSpeakerOutput2
);
2064 SetUpCrasAudioHandler(audio_nodes
);
2066 // Verify the audio devices size.
2067 AudioDeviceList audio_devices
;
2068 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2069 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2071 // Set all three nodes to be active.
2072 CrasAudioHandler::NodeIdList active_nodes
;
2073 active_nodes
.push_back(kHDMIOutput
.id
);
2074 active_nodes
.push_back(kUSBJabraSpeakerOutput1
.id
);
2075 active_nodes
.push_back(kUSBJabraSpeakerOutput2
.id
);
2076 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2078 // Verify all three nodes are active.
2079 EXPECT_EQ(3, GetActiveDeviceCount());
2080 const AudioDevice
* active_output_1
= GetDeviceFromId(kHDMIOutput
.id
);
2081 EXPECT_TRUE(active_output_1
->active
);
2082 const AudioDevice
* active_output_2
=
2083 GetDeviceFromId(kUSBJabraSpeakerOutput1
.id
);
2084 EXPECT_TRUE(active_output_2
->active
);
2085 const AudioDevice
* active_output_3
=
2086 GetDeviceFromId(kUSBJabraSpeakerOutput2
.id
);
2087 EXPECT_TRUE(active_output_3
->active
);
2089 // Now call ChangeActiveDevices with only 2 nodes.
2090 active_nodes
.clear();
2091 active_nodes
.push_back(kUSBJabraSpeakerOutput1
.id
);
2092 active_nodes
.push_back(kUSBJabraSpeakerOutput2
.id
);
2093 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2095 // Verify only 2 nodes are active.
2096 EXPECT_EQ(2, GetActiveDeviceCount());
2097 const AudioDevice
* output_1
= GetDeviceFromId(kHDMIOutput
.id
);
2098 EXPECT_FALSE(output_1
->active
);
2099 const AudioDevice
* output_2
= GetDeviceFromId(kUSBJabraSpeakerOutput1
.id
);
2100 EXPECT_TRUE(output_2
->active
);
2101 const AudioDevice
* output_3
= GetDeviceFromId(kUSBJabraSpeakerOutput2
.id
);
2102 EXPECT_TRUE(output_3
->active
);
2105 TEST_F(CrasAudioHandlerTest
, HotrodInitWithSingleJabra
) {
2106 // Simulates the hotrod initializated with a single jabra device and
2107 // CrasAudioHandler selected jabra input/output as active devices.
2108 AudioNodeList audio_nodes
;
2109 audio_nodes
.push_back(kHDMIOutput
);
2110 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2111 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2112 audio_nodes
.push_back(kUSBCameraInput
);
2113 SetUpCrasAudioHandler(audio_nodes
);
2115 // Verify the audio devices size.
2116 AudioDeviceList audio_devices
;
2117 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2118 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2120 // Verify the jabra speaker's output and input are selected as active nodes
2121 // by CrasAudioHandler.
2122 EXPECT_EQ(2, GetActiveDeviceCount());
2123 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2124 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2125 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2126 cras_audio_handler_
->GetPrimaryActiveInputNode());
2129 TEST_F(CrasAudioHandlerTest
,
2130 ChangeActiveNodesHotrodInitWithSingleJabraCameraPlugInLater
) {
2131 AudioNodeList audio_nodes
;
2132 audio_nodes
.push_back(kHDMIOutput
);
2133 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2134 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2135 AudioNode
usb_camera(kUSBCameraInput
);
2136 usb_camera
.plugged_time
= 10000000;
2137 audio_nodes
.push_back(usb_camera
);
2138 SetUpCrasAudioHandler(audio_nodes
);
2140 // Verify the audio devices size.
2141 AudioDeviceList audio_devices
;
2142 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2143 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2145 // Verify the jabra speaker's output is selected as active output, and
2146 // camera's input is selected as active input by CrasAudioHandler
2147 EXPECT_EQ(2, GetActiveDeviceCount());
2148 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2149 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2150 EXPECT_EQ(kUSBCameraInput
.id
,
2151 cras_audio_handler_
->GetPrimaryActiveInputNode());
2153 // Simulate hotrod app call to set jabra input as active device with only
2154 // jabra input node in the active node list, which does not conform to the
2155 // new SetActiveDevices protocol, but just show we can still handle it if
2157 CrasAudioHandler::NodeIdList active_nodes
;
2158 active_nodes
.push_back(kUSBJabraSpeakerOutput1
.id
);
2159 active_nodes
.push_back(kUSBJabraSpeakerInput1
.id
);
2160 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2162 // Verify the jabra speaker's output is selected as active output, and
2163 // jabra's input is selected as active input.
2164 EXPECT_EQ(2, GetActiveDeviceCount());
2165 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2166 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2167 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2168 cras_audio_handler_
->GetPrimaryActiveInputNode());
2171 TEST_F(CrasAudioHandlerTest
,
2172 ChangeActiveNodesHotrodInitWithSingleJabraCameraPlugInLaterOldCall
) {
2173 AudioNodeList audio_nodes
;
2174 audio_nodes
.push_back(kHDMIOutput
);
2175 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2176 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2177 AudioNode
usb_camera(kUSBCameraInput
);
2178 usb_camera
.plugged_time
= 10000000;
2179 audio_nodes
.push_back(usb_camera
);
2180 SetUpCrasAudioHandler(audio_nodes
);
2182 // Verify the audio devices size.
2183 AudioDeviceList audio_devices
;
2184 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2185 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2187 // Verify the jabra speaker's output is selected as active output, and
2188 // camera's input is selected as active input by CrasAudioHandler
2189 EXPECT_EQ(2, GetActiveDeviceCount());
2190 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2191 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2192 EXPECT_EQ(kUSBCameraInput
.id
,
2193 cras_audio_handler_
->GetPrimaryActiveInputNode());
2195 // Simulate hotrod app call to set jabra input as active device with only
2196 // jabra input node in the active node list, which does not conform to the
2197 // new SetActiveDevices protocol, but just show we can still handle it if
2199 CrasAudioHandler::NodeIdList active_nodes
;
2200 active_nodes
.push_back(kUSBJabraSpeakerInput1
.id
);
2201 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2203 // Verify the jabra speaker's output is selected as active output, and
2204 // jabra's input is selected as active input.
2205 EXPECT_EQ(2, GetActiveDeviceCount());
2206 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2207 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2208 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2209 cras_audio_handler_
->GetPrimaryActiveInputNode());
2212 TEST_F(CrasAudioHandlerTest
,
2213 ChangeActiveNodesHotrodInitWithSingleJabraChangeOutput
) {
2214 AudioNodeList audio_nodes
;
2215 audio_nodes
.push_back(kHDMIOutput
);
2216 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2217 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2218 audio_nodes
.push_back(kUSBCameraInput
);
2219 SetUpCrasAudioHandler(audio_nodes
);
2221 // Verify the audio devices size.
2222 AudioDeviceList audio_devices
;
2223 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2224 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2226 // Verify the jabra speaker's output and input are selected as active output
2227 // by CrasAudioHandler.
2228 EXPECT_EQ(2, GetActiveDeviceCount());
2229 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2230 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2231 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2232 cras_audio_handler_
->GetPrimaryActiveInputNode());
2234 // Simulate hotrod app call SetActiveDevices to change active output
2235 // with only complete list of active nodes passed in, which is the new
2236 // way of hotrod app.
2237 CrasAudioHandler::NodeIdList active_nodes
;
2238 active_nodes
.push_back(kHDMIOutput
.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(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2246 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2247 cras_audio_handler_
->GetPrimaryActiveInputNode());
2250 TEST_F(CrasAudioHandlerTest
,
2251 ChangeActiveNodesHotrodInitWithSingleJabraChangeOutputOldCall
) {
2252 AudioNodeList audio_nodes
;
2253 audio_nodes
.push_back(kHDMIOutput
);
2254 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2255 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2256 audio_nodes
.push_back(kUSBCameraInput
);
2257 SetUpCrasAudioHandler(audio_nodes
);
2259 // Verify the audio devices size.
2260 AudioDeviceList audio_devices
;
2261 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2262 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2264 // Verify the jabra speaker's output and input are selected as active output
2265 // by CrasAudioHandler.
2266 EXPECT_EQ(2, GetActiveDeviceCount());
2267 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2268 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2269 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2270 cras_audio_handler_
->GetPrimaryActiveInputNode());
2272 // Simulate hotrod app call SetActiveDevices to change active output
2273 // with only a single active output nodes passed in, which is the old
2274 // way of hotrod app.
2275 CrasAudioHandler::NodeIdList active_nodes
;
2276 active_nodes
.push_back(kHDMIOutput
.id
);
2277 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2279 // Verify the jabra speaker's output is selected as active output, and
2280 // jabra's input is selected as active input.
2281 EXPECT_EQ(2, GetActiveDeviceCount());
2282 EXPECT_EQ(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2283 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2284 cras_audio_handler_
->GetPrimaryActiveInputNode());
2287 TEST_F(CrasAudioHandlerTest
, NoMoreAudioInputDevices
) {
2288 // Some device like chromebox does not have the internal input device. The
2289 // active devices should be reset when the user plugs a device and then
2290 // unplugs it to such device.
2292 AudioNodeList audio_nodes
;
2293 audio_nodes
.push_back(kInternalSpeaker
);
2294 SetUpCrasAudioHandler(audio_nodes
);
2296 EXPECT_EQ(0ULL, cras_audio_handler_
->GetPrimaryActiveInputNode());
2298 audio_nodes
.push_back(kMicJack
);
2299 ChangeAudioNodes(audio_nodes
);
2301 EXPECT_EQ(kMicJack
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
2302 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
2303 test_observer_
->reset_active_input_node_changed_count();
2305 audio_nodes
.pop_back();
2306 ChangeAudioNodes(audio_nodes
);
2307 EXPECT_EQ(0ULL, cras_audio_handler_
->GetPrimaryActiveInputNode());
2308 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
2311 } // namespace chromeos