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(false,
121 "Bluetooth Headset 1",
125 const AudioNode
kHDMIOutput(false,
133 const AudioNode
kUSBHeadphone1(false,
141 const AudioNode
kUSBHeadphone2(false,
149 const AudioNode
kUSBJabraSpeakerOutput1(false,
150 kUSBJabraSpeakerOutputId1
,
157 const AudioNode
kUSBJabraSpeakerOutput2(false,
158 kUSBJabraSpeakerOutputId2
,
165 const AudioNode
kUSBJabraSpeakerInput1(true,
166 kUSBJabraSpeakerInputId1
,
173 const AudioNode
kUSBJabraSpeakerInput2(true,
174 kUSBJabraSpeakerInputId2
,
181 const AudioNode
kUSBCameraInput(true,
189 class TestObserver
: public chromeos::CrasAudioHandler::AudioObserver
{
191 TestObserver() : active_output_node_changed_count_(0),
192 active_input_node_changed_count_(0),
193 audio_nodes_changed_count_(0),
194 output_mute_changed_count_(0),
195 input_mute_changed_count_(0),
196 output_volume_changed_count_(0),
197 input_gain_changed_count_(0) {
200 int active_output_node_changed_count() const {
201 return active_output_node_changed_count_
;
204 void reset_active_output_node_changed_count() {
205 active_output_node_changed_count_
= 0;
208 int active_input_node_changed_count() const {
209 return active_input_node_changed_count_
;
212 void reset_active_input_node_changed_count() {
213 active_input_node_changed_count_
= 0;
216 int audio_nodes_changed_count() const {
217 return audio_nodes_changed_count_
;
220 int output_mute_changed_count() const {
221 return output_mute_changed_count_
;
224 int input_mute_changed_count() const {
225 return input_mute_changed_count_
;
228 int output_volume_changed_count() const {
229 return output_volume_changed_count_
;
232 int input_gain_changed_count() const {
233 return input_gain_changed_count_
;
236 ~TestObserver() override
{}
239 // chromeos::CrasAudioHandler::AudioObserver overrides.
240 void OnActiveOutputNodeChanged() override
{
241 ++active_output_node_changed_count_
;
244 void OnActiveInputNodeChanged() override
{
245 ++active_input_node_changed_count_
;
248 void OnAudioNodesChanged() override
{ ++audio_nodes_changed_count_
; }
250 void OnOutputMuteChanged(bool /* mute_on */) override
{
251 ++output_mute_changed_count_
;
254 void OnInputMuteChanged(bool /* mute_on */) override
{
255 ++input_mute_changed_count_
;
258 void OnOutputNodeVolumeChanged(uint64
/* node_id */,
259 int /* volume */) override
{
260 ++output_volume_changed_count_
;
263 void OnInputNodeGainChanged(uint64
/* node_id */, int /* gain */) override
{
264 ++input_gain_changed_count_
;
268 int active_output_node_changed_count_
;
269 int active_input_node_changed_count_
;
270 int audio_nodes_changed_count_
;
271 int output_mute_changed_count_
;
272 int input_mute_changed_count_
;
273 int output_volume_changed_count_
;
274 int input_gain_changed_count_
;
276 DISALLOW_COPY_AND_ASSIGN(TestObserver
);
279 class CrasAudioHandlerTest
: public testing::Test
{
281 CrasAudioHandlerTest() : cras_audio_handler_(NULL
),
282 fake_cras_audio_client_(NULL
) {
284 ~CrasAudioHandlerTest() override
{}
286 void SetUp() override
{}
288 void TearDown() override
{
289 cras_audio_handler_
->RemoveAudioObserver(test_observer_
.get());
290 test_observer_
.reset();
291 CrasAudioHandler::Shutdown();
292 audio_pref_handler_
= NULL
;
293 DBusThreadManager::Shutdown();
296 void SetUpCrasAudioHandler(const AudioNodeList
& audio_nodes
) {
297 DBusThreadManager::Initialize();
298 fake_cras_audio_client_
= static_cast<FakeCrasAudioClient
*>(
299 DBusThreadManager::Get()->GetCrasAudioClient());
300 fake_cras_audio_client_
->SetAudioNodesForTesting(audio_nodes
);
301 audio_pref_handler_
= new AudioDevicesPrefHandlerStub();
302 CrasAudioHandler::Initialize(audio_pref_handler_
);
303 cras_audio_handler_
= CrasAudioHandler::Get();
304 test_observer_
.reset(new TestObserver
);
305 cras_audio_handler_
->AddAudioObserver(test_observer_
.get());
306 message_loop_
.RunUntilIdle();
309 void SetUpCrasAudioHandlerWithPrimaryActiveNode(
310 const AudioNodeList
& audio_nodes
,
311 const AudioNode
& primary_active_node
) {
312 DBusThreadManager::Initialize();
313 fake_cras_audio_client_
= static_cast<FakeCrasAudioClient
*>(
314 DBusThreadManager::Get()->GetCrasAudioClient());
315 fake_cras_audio_client_
->SetAudioNodesForTesting(audio_nodes
);
316 fake_cras_audio_client_
->SetActiveOutputNode(primary_active_node
.id
),
317 audio_pref_handler_
= new AudioDevicesPrefHandlerStub();
318 CrasAudioHandler::Initialize(audio_pref_handler_
);
319 cras_audio_handler_
= CrasAudioHandler::Get();
320 test_observer_
.reset(new TestObserver
);
321 cras_audio_handler_
->AddAudioObserver(test_observer_
.get());
322 message_loop_
.RunUntilIdle();
325 void ChangeAudioNodes(const AudioNodeList
& audio_nodes
) {
326 fake_cras_audio_client_
->SetAudioNodesAndNotifyObserversForTesting(
328 message_loop_
.RunUntilIdle();
331 const AudioDevice
* GetDeviceFromId(uint64 id
) {
332 return cras_audio_handler_
->GetDeviceFromId(id
);
335 int GetActiveDeviceCount() const {
336 int num_active_nodes
= 0;
337 AudioDeviceList audio_devices
;
338 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
339 for (size_t i
= 0; i
< audio_devices
.size(); ++i
) {
340 if (audio_devices
[i
].active
)
343 return num_active_nodes
;
347 base::MessageLoopForUI message_loop_
;
348 CrasAudioHandler
* cras_audio_handler_
; // Not owned.
349 FakeCrasAudioClient
* fake_cras_audio_client_
; // Not owned.
350 scoped_ptr
<TestObserver
> test_observer_
;
351 scoped_refptr
<AudioDevicesPrefHandlerStub
> audio_pref_handler_
;
354 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandlerTest
);
357 TEST_F(CrasAudioHandlerTest
, InitializeWithOnlyDefaultAudioDevices
) {
358 AudioNodeList audio_nodes
;
359 audio_nodes
.push_back(kInternalSpeaker
);
360 audio_nodes
.push_back(kInternalMic
);
361 SetUpCrasAudioHandler(audio_nodes
);
363 // Verify the audio devices size.
364 AudioDeviceList audio_devices
;
365 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
366 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
368 // Verify the internal speaker has been selected as the active output.
369 AudioDevice active_output
;
371 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
372 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
373 EXPECT_EQ(kInternalSpeaker
.id
,
374 cras_audio_handler_
->GetPrimaryActiveOutputNode());
375 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
377 // Ensure the internal microphone has been selected as the active input.
378 AudioDevice active_input
;
379 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
380 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
383 TEST_F(CrasAudioHandlerTest
, InitializeWithAlternativeAudioDevices
) {
384 AudioNodeList audio_nodes
;
385 audio_nodes
.push_back(kInternalSpeaker
);
386 audio_nodes
.push_back(kHeadphone
);
387 audio_nodes
.push_back(kInternalMic
);
388 audio_nodes
.push_back(kUSBMic
);
389 SetUpCrasAudioHandler(audio_nodes
);
391 // Verify the audio devices size.
392 AudioDeviceList audio_devices
;
393 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
394 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
396 // Verify the headphone has been selected as the active output.
397 AudioDevice active_output
;
399 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
400 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
401 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
402 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
404 // Ensure the USB microphone has been selected as the active input.
405 EXPECT_EQ(kUSBMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
406 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
409 TEST_F(CrasAudioHandlerTest
, InitializeWithKeyboardMic
) {
410 AudioNodeList audio_nodes
;
411 audio_nodes
.push_back(kInternalSpeaker
);
412 audio_nodes
.push_back(kInternalMic
);
413 audio_nodes
.push_back(kKeyboardMic
);
414 SetUpCrasAudioHandler(audio_nodes
);
416 // Verify the audio devices size.
417 AudioDeviceList audio_devices
;
418 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
419 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
420 EXPECT_TRUE(cras_audio_handler_
->HasKeyboardMic());
422 // Verify the internal speaker has been selected as the active output.
423 AudioDevice active_output
;
425 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
426 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
427 EXPECT_EQ(kInternalSpeaker
.id
,
428 cras_audio_handler_
->GetPrimaryActiveOutputNode());
429 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
431 // Ensure the internal microphone has been selected as the active input,
432 // not affected by keyboard mic.
433 AudioDevice active_input
;
434 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
435 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
436 const AudioDevice
* keyboard_mic
= GetDeviceFromId(kKeyboardMic
.id
);
437 EXPECT_FALSE(keyboard_mic
->active
);
440 TEST_F(CrasAudioHandlerTest
, SetKeyboardMicActive
) {
441 AudioNodeList audio_nodes
;
442 audio_nodes
.push_back(kInternalMic
);
443 audio_nodes
.push_back(kKeyboardMic
);
444 SetUpCrasAudioHandler(audio_nodes
);
446 // Verify the audio devices size.
447 AudioDeviceList audio_devices
;
448 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
449 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
450 EXPECT_TRUE(cras_audio_handler_
->HasKeyboardMic());
452 // Ensure the internal microphone has been selected as the active input,
453 // not affected by keyboard mic.
454 AudioDevice active_input
;
455 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
456 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
457 const AudioDevice
* keyboard_mic
= GetDeviceFromId(kKeyboardMic
.id
);
458 EXPECT_FALSE(keyboard_mic
->active
);
460 // Make keyboard mic active.
461 cras_audio_handler_
->SetKeyboardMicActive(true);
462 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
463 const AudioDevice
* active_keyboard_mic
= GetDeviceFromId(kKeyboardMic
.id
);
464 EXPECT_TRUE(active_keyboard_mic
->active
);
466 // Make keyboard mic inactive.
467 cras_audio_handler_
->SetKeyboardMicActive(false);
468 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
469 const AudioDevice
* inactive_keyboard_mic
= GetDeviceFromId(kKeyboardMic
.id
);
470 EXPECT_FALSE(inactive_keyboard_mic
->active
);
473 TEST_F(CrasAudioHandlerTest
, SwitchActiveOutputDevice
) {
474 AudioNodeList audio_nodes
;
475 audio_nodes
.push_back(kInternalSpeaker
);
476 audio_nodes
.push_back(kHeadphone
);
477 SetUpCrasAudioHandler(audio_nodes
);
478 AudioDeviceList audio_devices
;
479 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
480 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
482 // Verify the initial active output device is headphone.
483 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
484 AudioDevice active_output
;
486 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
487 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
488 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
490 // Switch the active output to internal speaker.
491 AudioDevice
internal_speaker(kInternalSpeaker
);
492 cras_audio_handler_
->SwitchToDevice(internal_speaker
, true);
494 // Verify the active output is switched to internal speaker, and the
495 // ActiveOutputNodeChanged event is fired.
496 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
498 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
499 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
500 EXPECT_EQ(kInternalSpeaker
.id
,
501 cras_audio_handler_
->GetPrimaryActiveOutputNode());
504 TEST_F(CrasAudioHandlerTest
, SwitchActiveInputDevice
) {
505 AudioNodeList audio_nodes
;
506 audio_nodes
.push_back(kInternalMic
);
507 audio_nodes
.push_back(kUSBMic
);
508 SetUpCrasAudioHandler(audio_nodes
);
509 AudioDeviceList audio_devices
;
510 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
511 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
513 // Verify the initial active input device is USB mic.
514 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
515 EXPECT_EQ(kUSBMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
517 // Switch the active input to internal mic.
518 AudioDevice
internal_mic(kInternalMic
);
519 cras_audio_handler_
->SwitchToDevice(internal_mic
, true);
521 // Verify the active output is switched to internal speaker, and the active
522 // ActiveInputNodeChanged event is fired.
523 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
524 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
527 TEST_F(CrasAudioHandlerTest
, PlugHeadphone
) {
528 // Set up initial audio devices, only with internal speaker.
529 AudioNodeList audio_nodes
;
530 audio_nodes
.push_back(kInternalSpeaker
);
531 SetUpCrasAudioHandler(audio_nodes
);
532 const size_t init_nodes_size
= audio_nodes
.size();
534 // Verify the audio devices size.
535 AudioDeviceList audio_devices
;
536 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
537 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
538 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
540 // Verify the internal speaker has been selected as the active output.
541 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
542 AudioDevice active_output
;
544 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
545 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
546 EXPECT_EQ(kInternalSpeaker
.id
,
547 cras_audio_handler_
->GetPrimaryActiveOutputNode());
548 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
550 // Plug the headphone.
552 AudioNode
internal_speaker(kInternalSpeaker
);
553 internal_speaker
.active
= true;
554 audio_nodes
.push_back(internal_speaker
);
555 audio_nodes
.push_back(kHeadphone
);
556 ChangeAudioNodes(audio_nodes
);
558 // Verify the AudioNodesChanged event is fired and new audio device is added.
559 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
560 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
561 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
563 // Verify the active output device is switched to headphone and
564 // ActiveOutputChanged event is fired.
565 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
567 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
568 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
569 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
570 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
573 TEST_F(CrasAudioHandlerTest
, UnplugHeadphone
) {
574 // Set up initial audio devices, with internal speaker and headphone.
575 AudioNodeList audio_nodes
;
576 audio_nodes
.push_back(kInternalSpeaker
);
577 audio_nodes
.push_back(kHeadphone
);
578 SetUpCrasAudioHandler(audio_nodes
);
579 const size_t init_nodes_size
= audio_nodes
.size();
581 // Verify the audio devices size.
582 AudioDeviceList audio_devices
;
583 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
584 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
585 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
587 // Verify the headphone has been selected as the active output.
588 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
589 AudioDevice active_output
;
591 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
592 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
593 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
594 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
596 // Unplug the headphone.
598 audio_nodes
.push_back(kInternalSpeaker
);
599 ChangeAudioNodes(audio_nodes
);
601 // Verify the AudioNodesChanged event is fired and one audio device is
603 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
604 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
605 EXPECT_EQ(init_nodes_size
- 1, audio_devices
.size());
607 // Verify the active output device is switched to internal speaker and
608 // ActiveOutputChanged event is fired.
609 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
611 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
612 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
613 EXPECT_EQ(kInternalSpeaker
.id
,
614 cras_audio_handler_
->GetPrimaryActiveOutputNode());
615 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
618 TEST_F(CrasAudioHandlerTest
, InitializeWithBluetoothHeadset
) {
619 AudioNodeList audio_nodes
;
620 audio_nodes
.push_back(kInternalSpeaker
);
621 audio_nodes
.push_back(kBluetoothHeadset
);
622 SetUpCrasAudioHandler(audio_nodes
);
624 // Verify the audio devices size.
625 AudioDeviceList audio_devices
;
626 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
627 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
628 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
630 // Verify the bluetooth headset has been selected as the active output.
631 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
632 AudioDevice active_output
;
634 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
635 EXPECT_EQ(kBluetoothHeadset
.id
, active_output
.id
);
636 EXPECT_EQ(kBluetoothHeadset
.id
,
637 cras_audio_handler_
->GetPrimaryActiveOutputNode());
638 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
641 TEST_F(CrasAudioHandlerTest
, ConnectAndDisconnectBluetoothHeadset
) {
642 // Initialize with internal speaker and headphone.
643 AudioNodeList audio_nodes
;
644 audio_nodes
.push_back(kInternalSpeaker
);
645 audio_nodes
.push_back(kHeadphone
);
646 SetUpCrasAudioHandler(audio_nodes
);
647 const size_t init_nodes_size
= audio_nodes
.size();
649 // Verify the audio devices size.
650 AudioDeviceList audio_devices
;
651 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
652 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
653 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
655 // Verify the headphone is selected as the active output initially.
656 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
657 AudioDevice active_output
;
659 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
660 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
661 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
662 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
664 // Connect to bluetooth headset. Since it is plugged in later than
665 // headphone, active output should be switched to it.
667 audio_nodes
.push_back(kInternalSpeaker
);
668 AudioNode
headphone(kHeadphone
);
669 headphone
.plugged_time
= 80000000;
670 headphone
.active
= true;
671 audio_nodes
.push_back(headphone
);
672 AudioNode
bluetooth_headset(kBluetoothHeadset
);
673 bluetooth_headset
.plugged_time
= 90000000;
674 audio_nodes
.push_back(bluetooth_headset
);
675 ChangeAudioNodes(audio_nodes
);
677 // Verify the AudioNodesChanged event is fired and new audio device is added.
678 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
679 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
680 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
682 // Verify the active output device is switched to bluetooth headset, and
683 // ActiveOutputChanged event is fired.
684 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
686 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
687 EXPECT_EQ(kBluetoothHeadset
.id
, active_output
.id
);
688 EXPECT_EQ(kBluetoothHeadset
.id
,
689 cras_audio_handler_
->GetPrimaryActiveOutputNode());
690 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
692 // Disconnect bluetooth headset.
694 audio_nodes
.push_back(kInternalSpeaker
);
695 headphone
.active
= false;
696 audio_nodes
.push_back(headphone
);
697 ChangeAudioNodes(audio_nodes
);
699 // Verify the AudioNodesChanged event is fired and one audio device is
701 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
702 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
703 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
705 // Verify the active output device is switched to headphone, and
706 // ActiveOutputChanged event is fired.
707 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
709 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
710 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
711 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
712 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
715 TEST_F(CrasAudioHandlerTest
, InitializeWithHDMIOutput
) {
716 AudioNodeList audio_nodes
;
717 audio_nodes
.push_back(kInternalSpeaker
);
718 audio_nodes
.push_back(kHDMIOutput
);
719 SetUpCrasAudioHandler(audio_nodes
);
721 // Verify the audio devices size.
722 AudioDeviceList audio_devices
;
723 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
724 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
725 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
727 // Verify the HDMI device has been selected as the active output.
728 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
729 AudioDevice active_output
;
731 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
732 EXPECT_EQ(kHDMIOutput
.id
, active_output
.id
);
733 EXPECT_EQ(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
734 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
737 TEST_F(CrasAudioHandlerTest
, ConnectAndDisconnectHDMIOutput
) {
738 // Initialize with internal speaker.
739 AudioNodeList audio_nodes
;
740 audio_nodes
.push_back(kInternalSpeaker
);
741 SetUpCrasAudioHandler(audio_nodes
);
742 const size_t init_nodes_size
= audio_nodes
.size();
744 // Verify the audio devices size.
745 AudioDeviceList audio_devices
;
746 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
747 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
748 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
750 // Verify the internal speaker is selected as the active output initially.
751 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
752 AudioDevice active_output
;
754 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
755 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
756 EXPECT_EQ(kInternalSpeaker
.id
,
757 cras_audio_handler_
->GetPrimaryActiveOutputNode());
758 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
760 // Connect to HDMI output.
762 AudioNode
internal_speaker(kInternalSpeaker
);
763 internal_speaker
.active
= true;
764 internal_speaker
.plugged_time
= 80000000;
765 audio_nodes
.push_back(internal_speaker
);
766 AudioNode
hdmi(kHDMIOutput
);
767 hdmi
.plugged_time
= 90000000;
768 audio_nodes
.push_back(hdmi
);
769 ChangeAudioNodes(audio_nodes
);
771 // Verify the AudioNodesChanged event is fired and new audio device is added.
772 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
773 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
774 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
776 // Verify the active output device is switched to hdmi output, and
777 // ActiveOutputChanged event is fired.
778 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
780 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
781 EXPECT_EQ(kHDMIOutput
.id
, active_output
.id
);
782 EXPECT_EQ(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
783 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
785 // Disconnect hdmi headset.
787 audio_nodes
.push_back(kInternalSpeaker
);
788 ChangeAudioNodes(audio_nodes
);
790 // Verify the AudioNodesChanged event is fired and one audio device is
792 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
793 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
794 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
796 // Verify the active output device is switched to internal speaker, and
797 // ActiveOutputChanged event is fired.
798 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
800 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
801 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
802 EXPECT_EQ(kInternalSpeaker
.id
,
803 cras_audio_handler_
->GetPrimaryActiveOutputNode());
804 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
807 TEST_F(CrasAudioHandlerTest
, HandleHeadphoneAndHDMIOutput
) {
808 // Initialize with internal speaker, headphone and HDMI output.
809 AudioNodeList audio_nodes
;
810 audio_nodes
.push_back(kInternalSpeaker
);
811 audio_nodes
.push_back(kHeadphone
);
812 audio_nodes
.push_back(kHDMIOutput
);
813 SetUpCrasAudioHandler(audio_nodes
);
814 const size_t init_nodes_size
= audio_nodes
.size();
816 // Verify the audio devices size.
817 AudioDeviceList audio_devices
;
818 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
819 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
820 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
822 // Verify the headphone is selected as the active output initially.
823 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
824 AudioDevice active_output
;
826 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
827 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
828 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
829 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
831 // Disconnect HDMI output.
833 audio_nodes
.push_back(kInternalSpeaker
);
834 audio_nodes
.push_back(kHDMIOutput
);
835 ChangeAudioNodes(audio_nodes
);
837 // Verify the AudioNodesChanged event is fired and one audio device is
839 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
840 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
841 EXPECT_EQ(init_nodes_size
- 1, audio_devices
.size());
843 // Verify the active output device is switched to HDMI output, and
844 // ActiveOutputChanged event is fired.
845 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
847 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
848 EXPECT_EQ(kHDMIOutput
.id
, active_output
.id
);
849 EXPECT_EQ(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
850 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
853 TEST_F(CrasAudioHandlerTest
, InitializeWithUSBHeadphone
) {
854 AudioNodeList audio_nodes
;
855 audio_nodes
.push_back(kInternalSpeaker
);
856 audio_nodes
.push_back(kUSBHeadphone1
);
857 SetUpCrasAudioHandler(audio_nodes
);
859 // Verify the audio devices size.
860 AudioDeviceList audio_devices
;
861 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
862 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
863 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
865 // Verify the usb headphone has been selected as the active output.
866 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
867 AudioDevice active_output
;
869 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
870 EXPECT_EQ(kUSBHeadphone1
.id
, active_output
.id
);
871 EXPECT_EQ(kUSBHeadphone1
.id
,
872 cras_audio_handler_
->GetPrimaryActiveOutputNode());
873 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
876 TEST_F(CrasAudioHandlerTest
, PlugAndUnplugUSBHeadphone
) {
877 // Initialize with internal speaker.
878 AudioNodeList audio_nodes
;
879 audio_nodes
.push_back(kInternalSpeaker
);
880 SetUpCrasAudioHandler(audio_nodes
);
881 const size_t init_nodes_size
= audio_nodes
.size();
883 // Verify the audio devices size.
884 AudioDeviceList audio_devices
;
885 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
886 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
887 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
889 // Verify the internal speaker is selected as the active output initially.
890 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
891 AudioDevice active_output
;
893 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
894 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
895 EXPECT_EQ(kInternalSpeaker
.id
,
896 cras_audio_handler_
->GetPrimaryActiveOutputNode());
897 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
899 // Plug in usb headphone
901 AudioNode
internal_speaker(kInternalSpeaker
);
902 internal_speaker
.active
= true;
903 internal_speaker
.plugged_time
= 80000000;
904 audio_nodes
.push_back(internal_speaker
);
905 AudioNode
usb_headphone(kUSBHeadphone1
);
906 usb_headphone
.plugged_time
= 90000000;
907 audio_nodes
.push_back(usb_headphone
);
908 ChangeAudioNodes(audio_nodes
);
910 // Verify the AudioNodesChanged event is fired and new audio device is added.
911 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
912 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
913 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
915 // Verify the active output device is switched to usb headphone, and
916 // ActiveOutputChanged event is fired.
917 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
919 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
920 EXPECT_EQ(kUSBHeadphone1
.id
, active_output
.id
);
921 EXPECT_EQ(kUSBHeadphone1
.id
,
922 cras_audio_handler_
->GetPrimaryActiveOutputNode());
923 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
925 // Unplug usb headphone.
927 audio_nodes
.push_back(kInternalSpeaker
);
928 ChangeAudioNodes(audio_nodes
);
930 // Verify the AudioNodesChanged event is fired and one audio device is
932 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
933 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
934 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
936 // Verify the active output device is switched to internal speaker, and
937 // ActiveOutputChanged event is fired.
938 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
940 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
941 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
942 EXPECT_EQ(kInternalSpeaker
.id
,
943 cras_audio_handler_
->GetPrimaryActiveOutputNode());
944 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
947 TEST_F(CrasAudioHandlerTest
, HandleMultipleUSBHeadphones
) {
948 // Initialize with internal speaker and one usb headphone.
949 AudioNodeList audio_nodes
;
950 audio_nodes
.push_back(kInternalSpeaker
);
951 audio_nodes
.push_back(kUSBHeadphone1
);
952 SetUpCrasAudioHandler(audio_nodes
);
953 const size_t init_nodes_size
= audio_nodes
.size();
955 // Verify the audio devices size.
956 AudioDeviceList audio_devices
;
957 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
958 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
959 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
961 // Verify the usb headphone is selected as the active output initially.
962 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
963 AudioDevice active_output
;
965 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
966 EXPECT_EQ(kUSBHeadphone1
.id
, active_output
.id
);
967 EXPECT_EQ(kUSBHeadphone1
.id
,
968 cras_audio_handler_
->GetPrimaryActiveOutputNode());
969 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
971 // Plug in another usb headphone.
973 audio_nodes
.push_back(kInternalSpeaker
);
974 AudioNode
usb_headphone_1(kUSBHeadphone1
);
975 usb_headphone_1
.active
= true;
976 usb_headphone_1
.plugged_time
= 80000000;
977 audio_nodes
.push_back(usb_headphone_1
);
978 AudioNode
usb_headphone_2(kUSBHeadphone2
);
979 usb_headphone_2
.plugged_time
= 90000000;
980 audio_nodes
.push_back(usb_headphone_2
);
981 ChangeAudioNodes(audio_nodes
);
983 // Verify the AudioNodesChanged event is fired and new audio device is added.
984 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
985 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
986 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
988 // Verify the active output device is switched to the 2nd usb headphone, which
989 // is plugged later, and ActiveOutputChanged event is fired.
990 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
992 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
993 EXPECT_EQ(kUSBHeadphone2
.id
, active_output
.id
);
994 EXPECT_EQ(kUSBHeadphone2
.id
,
995 cras_audio_handler_
->GetPrimaryActiveOutputNode());
996 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
998 // Unplug the 2nd usb headphone.
1000 audio_nodes
.push_back(kInternalSpeaker
);
1001 audio_nodes
.push_back(kUSBHeadphone1
);
1002 ChangeAudioNodes(audio_nodes
);
1004 // Verify the AudioNodesChanged event is fired and one audio device is
1006 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1007 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1008 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1010 // Verify the active output device is switched to the first usb headphone, and
1011 // ActiveOutputChanged event is fired.
1012 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
1014 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1015 EXPECT_EQ(kUSBHeadphone1
.id
, active_output
.id
);
1016 EXPECT_EQ(kUSBHeadphone1
.id
,
1017 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1018 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1021 TEST_F(CrasAudioHandlerTest
, UnplugUSBHeadphonesWithActiveSpeaker
) {
1022 // Initialize with internal speaker and one usb headphone.
1023 AudioNodeList audio_nodes
;
1024 audio_nodes
.push_back(kInternalSpeaker
);
1025 audio_nodes
.push_back(kUSBHeadphone1
);
1026 SetUpCrasAudioHandler(audio_nodes
);
1027 const size_t init_nodes_size
= audio_nodes
.size();
1029 // Verify the audio devices size.
1030 AudioDeviceList audio_devices
;
1031 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1032 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1033 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1035 // Verify the usb headphone is selected as the active output initially.
1036 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1037 AudioDevice active_output
;
1039 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1040 EXPECT_EQ(kUSBHeadphone1
.id
, active_output
.id
);
1041 EXPECT_EQ(kUSBHeadphone1
.id
,
1042 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1043 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1045 // Plug in the headphone jack.
1046 audio_nodes
.clear();
1047 audio_nodes
.push_back(kInternalSpeaker
);
1048 AudioNode
usb_headphone_1(kUSBHeadphone1
);
1049 usb_headphone_1
.active
= true;
1050 usb_headphone_1
.plugged_time
= 80000000;
1051 audio_nodes
.push_back(usb_headphone_1
);
1052 AudioNode
headphone_jack(kHeadphone
);
1053 headphone_jack
.plugged_time
= 90000000;
1054 audio_nodes
.push_back(headphone_jack
);
1055 ChangeAudioNodes(audio_nodes
);
1057 // Verify the AudioNodesChanged event is fired and new audio device is added.
1058 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1059 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1060 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
1062 // Verify the active output device is switched to the headphone jack, which
1063 // is plugged later, and ActiveOutputChanged event is fired.
1064 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1066 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1067 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
1068 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1069 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1071 // Select the speaker to be the active output device.
1072 AudioDevice
internal_speaker(kInternalSpeaker
);
1073 cras_audio_handler_
->SwitchToDevice(internal_speaker
, true);
1075 // Verify the active output is switched to internal speaker, and the
1076 // ActiveOutputNodeChanged event is fired.
1077 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
1079 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1080 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
1081 EXPECT_EQ(kInternalSpeaker
.id
,
1082 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1084 // Unplug the usb headphone.
1085 audio_nodes
.clear();
1086 AudioNode
internal_speaker_node(kInternalSpeaker
);
1087 internal_speaker_node
.active
= true;
1088 internal_speaker_node
.plugged_time
= 70000000;
1089 audio_nodes
.push_back(internal_speaker_node
);
1090 headphone_jack
.active
= false;
1091 audio_nodes
.push_back(headphone_jack
);
1092 ChangeAudioNodes(audio_nodes
);
1094 // Verify the AudioNodesChanged event is fired and one audio device is
1096 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1097 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1098 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1100 // Verify the active output device remains to be speaker.
1101 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
1103 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1104 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
1105 EXPECT_EQ(kInternalSpeaker
.id
,
1106 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1107 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1110 TEST_F(CrasAudioHandlerTest
, OneActiveAudioOutputAfterLoginNewUserSession
) {
1111 // This tests the case found with crbug.com/273271.
1112 // Initialize with internal speaker, bluetooth headphone and headphone jack
1113 // for a new chrome session after user signs out from the previous session.
1114 // Headphone jack is plugged in later than bluetooth headphone, but bluetooth
1115 // headphone is selected as the active output by user from previous user
1117 AudioNodeList audio_nodes
;
1118 audio_nodes
.push_back(kInternalSpeaker
);
1119 AudioNode
bluetooth_headphone(kBluetoothHeadset
);
1120 bluetooth_headphone
.active
= true;
1121 bluetooth_headphone
.plugged_time
= 70000000;
1122 audio_nodes
.push_back(bluetooth_headphone
);
1123 AudioNode
headphone_jack(kHeadphone
);
1124 headphone_jack
.plugged_time
= 80000000;
1125 audio_nodes
.push_back(headphone_jack
);
1126 SetUpCrasAudioHandlerWithPrimaryActiveNode(audio_nodes
, bluetooth_headphone
);
1127 const size_t init_nodes_size
= audio_nodes
.size();
1129 // Verify the audio devices size.
1130 AudioDeviceList audio_devices
;
1131 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1132 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1133 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1135 // Verify the headphone jack is selected as the active output and all other
1136 // audio devices are not active.
1137 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1138 AudioDevice active_output
;
1140 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1141 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
1142 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1143 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1144 for (size_t i
= 0; i
< audio_devices
.size(); ++i
) {
1145 if (audio_devices
[i
].id
!= kHeadphone
.id
)
1146 EXPECT_FALSE(audio_devices
[i
].active
);
1150 TEST_F(CrasAudioHandlerTest
, BluetoothSpeakerIdChangedOnFly
) {
1151 // Initialize with internal speaker and bluetooth headset.
1152 AudioNodeList audio_nodes
;
1153 audio_nodes
.push_back(kInternalSpeaker
);
1154 audio_nodes
.push_back(kBluetoothHeadset
);
1155 SetUpCrasAudioHandler(audio_nodes
);
1156 const size_t init_nodes_size
= audio_nodes
.size();
1158 // Verify the audio devices size.
1159 AudioDeviceList audio_devices
;
1160 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1161 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1162 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1164 // Verify the bluetooth headset is selected as the active output and all other
1165 // audio devices are not active.
1166 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1167 AudioDevice active_output
;
1169 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1170 EXPECT_EQ(kBluetoothHeadset
.id
, active_output
.id
);
1171 EXPECT_EQ(kBluetoothHeadset
.id
,
1172 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1173 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1175 // Cras changes the bluetooth headset's id on the fly.
1176 audio_nodes
.clear();
1177 AudioNode
internal_speaker(kInternalSpeaker
);
1178 internal_speaker
.active
= false;
1179 audio_nodes
.push_back(internal_speaker
);
1180 AudioNode
bluetooth_headphone(kBluetoothHeadset
);
1181 // Change bluetooth headphone id.
1182 bluetooth_headphone
.id
= kBluetoothHeadsetId
+ 20000;
1183 bluetooth_headphone
.active
= false;
1184 audio_nodes
.push_back(bluetooth_headphone
);
1185 ChangeAudioNodes(audio_nodes
);
1187 // Verify NodesChanged event is fired, and the audio devices size is not
1189 audio_devices
.clear();
1190 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1191 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1192 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1194 // Verify ActiveOutputNodeChanged event is fired, and active device should be
1195 // bluetooth headphone.
1196 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1198 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1199 EXPECT_EQ(bluetooth_headphone
.id
, active_output
.id
);
1202 TEST_F(CrasAudioHandlerTest
, PlugUSBMic
) {
1203 // Set up initial audio devices, only with internal mic.
1204 AudioNodeList audio_nodes
;
1205 audio_nodes
.push_back(kInternalMic
);
1206 SetUpCrasAudioHandler(audio_nodes
);
1207 const size_t init_nodes_size
= audio_nodes
.size();
1209 // Verify the audio devices size.
1210 AudioDeviceList audio_devices
;
1211 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1212 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1213 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1215 // Verify the internal mic is selected as the active input.
1216 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1217 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1218 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
1220 // Plug the USB Mic.
1221 audio_nodes
.clear();
1222 AudioNode
internal_mic(kInternalMic
);
1223 internal_mic
.active
= true;
1224 audio_nodes
.push_back(internal_mic
);
1225 audio_nodes
.push_back(kUSBMic
);
1226 ChangeAudioNodes(audio_nodes
);
1228 // Verify the AudioNodesChanged event is fired and new audio device is added.
1229 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1230 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1231 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
1233 // Verify the active input device is switched to USB mic and
1234 // and ActiveInputChanged event is fired.
1235 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
1236 EXPECT_EQ(kUSBMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1237 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1240 TEST_F(CrasAudioHandlerTest
, UnplugUSBMic
) {
1241 // Set up initial audio devices, with internal mic and USB Mic.
1242 AudioNodeList audio_nodes
;
1243 audio_nodes
.push_back(kInternalMic
);
1244 audio_nodes
.push_back(kUSBMic
);
1245 SetUpCrasAudioHandler(audio_nodes
);
1246 const size_t init_nodes_size
= audio_nodes
.size();
1248 // Verify the audio devices size.
1249 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1250 AudioDeviceList audio_devices
;
1251 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1252 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1254 // Verify the USB mic is selected as the active output.
1255 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1256 EXPECT_EQ(kUSBMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1257 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1259 // Unplug the USB Mic.
1260 audio_nodes
.clear();
1261 audio_nodes
.push_back(kInternalMic
);
1262 ChangeAudioNodes(audio_nodes
);
1264 // Verify the AudioNodesChanged event is fired, and one audio device is
1266 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1267 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1268 EXPECT_EQ(init_nodes_size
- 1, audio_devices
.size());
1270 // Verify the active input device is switched to internal mic, and
1271 // and ActiveInputChanged event is fired.
1272 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
1273 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1274 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
1277 TEST_F(CrasAudioHandlerTest
, PlugUSBMicNotAffectActiveOutput
) {
1278 // Set up initial audio devices.
1279 AudioNodeList audio_nodes
;
1280 audio_nodes
.push_back(kInternalSpeaker
);
1281 audio_nodes
.push_back(kHeadphone
);
1282 audio_nodes
.push_back(kInternalMic
);
1283 SetUpCrasAudioHandler(audio_nodes
);
1284 const size_t init_nodes_size
= audio_nodes
.size();
1286 // Verify the audio devices size.
1287 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1288 AudioDeviceList audio_devices
;
1289 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1290 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1292 // Verify the internal mic is selected as the active input.
1293 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1294 EXPECT_EQ(kInternalMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1295 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
1297 // Verify the headphone is selected as the active output.
1298 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1299 EXPECT_EQ(kHeadphoneId
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1300 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1302 // Switch the active output to internal speaker.
1303 AudioDevice
internal_speaker(kInternalSpeaker
);
1304 cras_audio_handler_
->SwitchToDevice(internal_speaker
, true);
1306 // Verify the active output is switched to internal speaker, and the
1307 // ActiveOutputNodeChanged event is fired.
1308 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1309 AudioDevice active_output
;
1311 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1312 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
1313 EXPECT_EQ(kInternalSpeaker
.id
,
1314 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1316 // Plug the USB Mic.
1317 audio_nodes
.clear();
1318 AudioNode
internal_speaker_node(kInternalSpeaker
);
1319 internal_speaker_node
.active
= true;
1320 audio_nodes
.push_back(internal_speaker_node
);
1321 audio_nodes
.push_back(kHeadphone
);
1322 AudioNode
internal_mic(kInternalMic
);
1323 internal_mic
.active
= true;
1324 audio_nodes
.push_back(internal_mic
);
1325 audio_nodes
.push_back(kUSBMic
);
1326 ChangeAudioNodes(audio_nodes
);
1328 // Verify the AudioNodesChanged event is fired, one new device is added.
1329 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1330 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1331 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
1333 // Verify the active input device is switched to USB mic, and
1334 // and ActiveInputChanged event is fired.
1335 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
1336 EXPECT_EQ(kUSBMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1337 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1339 // Verify the active output device is not changed.
1340 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1342 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1343 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
1344 EXPECT_EQ(kInternalSpeaker
.id
,
1345 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1348 TEST_F(CrasAudioHandlerTest
, PlugHeadphoneAutoUnplugSpeakerWithActiveUSB
) {
1349 // Set up initial audio devices.
1350 AudioNodeList audio_nodes
;
1351 audio_nodes
.push_back(kUSBHeadphone1
);
1352 audio_nodes
.push_back(kInternalSpeaker
);
1353 audio_nodes
.push_back(kInternalMic
);
1354 SetUpCrasAudioHandler(audio_nodes
);
1355 const size_t init_nodes_size
= audio_nodes
.size();
1357 // Verify the audio devices size.
1358 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1359 AudioDeviceList audio_devices
;
1360 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1361 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1363 // Verify the internal mic is selected as the active input.
1364 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1365 EXPECT_EQ(kInternalMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1366 EXPECT_FALSE(cras_audio_handler_
->has_alternative_input());
1368 // Verify the USB headphone is selected as the active output.
1369 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1370 EXPECT_EQ(kUSBHeadphoneId1
,
1371 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1372 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1374 // Plug the headphone and auto-unplug internal speaker.
1375 audio_nodes
.clear();
1376 AudioNode
usb_headphone_node(kUSBHeadphone1
);
1377 usb_headphone_node
.active
= true;
1378 audio_nodes
.push_back(usb_headphone_node
);
1379 AudioNode
headphone_node(kHeadphone
);
1380 headphone_node
.plugged_time
= 1000;
1381 audio_nodes
.push_back(headphone_node
);
1382 AudioNode
internal_mic(kInternalMic
);
1383 internal_mic
.active
= true;
1384 audio_nodes
.push_back(internal_mic
);
1385 ChangeAudioNodes(audio_nodes
);
1387 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1388 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1389 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1390 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1392 // Verify the active output device is switched to headphone, and
1393 // an ActiveOutputChanged event is fired.
1394 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1395 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1396 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1398 // Unplug the headphone and internal speaker auto-plugs back.
1399 audio_nodes
.clear();
1400 audio_nodes
.push_back(kUSBHeadphone1
);
1401 AudioNode
internal_speaker_node(kInternalSpeaker
);
1402 internal_speaker_node
.plugged_time
= 2000;
1403 audio_nodes
.push_back(internal_speaker_node
);
1404 audio_nodes
.push_back(internal_mic
);
1405 ChangeAudioNodes(audio_nodes
);
1407 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1408 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1409 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1410 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1412 // Verify the active output device is switched back to USB, and
1413 // an ActiveOutputChanged event is fired.
1414 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
1415 EXPECT_EQ(kUSBHeadphone1
.id
,
1416 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1417 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1419 // Verify the active input device is not changed.
1420 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1421 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1424 TEST_F(CrasAudioHandlerTest
, PlugMicAutoUnplugInternalMicWithActiveUSB
) {
1425 // Set up initial audio devices.
1426 AudioNodeList audio_nodes
;
1427 audio_nodes
.push_back(kUSBHeadphone1
);
1428 audio_nodes
.push_back(kInternalSpeaker
);
1429 audio_nodes
.push_back(kUSBMic
);
1430 audio_nodes
.push_back(kInternalMic
);
1431 SetUpCrasAudioHandler(audio_nodes
);
1432 const size_t init_nodes_size
= audio_nodes
.size();
1434 // Verify the audio devices size.
1435 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1436 AudioDeviceList audio_devices
;
1437 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1438 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1440 // Verify the internal mic is selected as the active input.
1441 EXPECT_EQ(0, test_observer_
->active_input_node_changed_count());
1442 EXPECT_EQ(kUSBMicId
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1443 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1445 // Verify the internal speaker is selected as the active output.
1446 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1447 EXPECT_EQ(kUSBHeadphoneId1
,
1448 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1449 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1451 // Plug the headphone and mic, auto-unplug internal mic and speaker.
1452 audio_nodes
.clear();
1453 AudioNode
usb_headphone_node(kUSBHeadphone1
);
1454 usb_headphone_node
.active
= true;
1455 audio_nodes
.push_back(usb_headphone_node
);
1456 AudioNode
headphone_node(kHeadphone
);
1457 headphone_node
.plugged_time
= 1000;
1458 audio_nodes
.push_back(headphone_node
);
1459 AudioNode
usb_mic(kUSBMic
);
1460 usb_mic
.active
= true;
1461 audio_nodes
.push_back(usb_mic
);
1462 AudioNode
mic_jack(kMicJack
);
1463 mic_jack
.plugged_time
= 1000;
1464 audio_nodes
.push_back(mic_jack
);
1465 ChangeAudioNodes(audio_nodes
);
1467 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1468 EXPECT_EQ(1, test_observer_
->audio_nodes_changed_count());
1469 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1470 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1472 // Verify the active output device is switched to headphone, and
1473 // an ActiveOutputChanged event is fired.
1474 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
1475 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1476 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1478 // Verify the active input device is switched to mic jack, and
1479 // an ActiveInputChanged event is fired.
1480 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
1481 EXPECT_EQ(kMicJack
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1482 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1484 // Unplug the headphone and internal speaker auto-plugs back.
1485 audio_nodes
.clear();
1486 audio_nodes
.push_back(kUSBHeadphone1
);
1487 AudioNode
internal_speaker_node(kInternalSpeaker
);
1488 internal_speaker_node
.plugged_time
= 2000;
1489 audio_nodes
.push_back(internal_speaker_node
);
1490 audio_nodes
.push_back(kUSBMic
);
1491 AudioNode
internal_mic(kInternalMic
);
1492 internal_mic
.plugged_time
= 2000;
1493 audio_nodes
.push_back(internal_mic
);
1494 ChangeAudioNodes(audio_nodes
);
1496 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1497 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1498 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1499 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1501 // Verify the active output device is switched back to USB, and
1502 // an ActiveOutputChanged event is fired.
1503 EXPECT_EQ(2, test_observer_
->active_output_node_changed_count());
1504 EXPECT_EQ(kUSBHeadphone1
.id
,
1505 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1506 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1508 // Verify the active input device is switched back to USB mic, and
1509 // an ActiveInputChanged event is fired.
1510 EXPECT_EQ(2, test_observer_
->active_input_node_changed_count());
1511 EXPECT_EQ(kUSBMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1512 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1515 TEST_F(CrasAudioHandlerTest
, MultipleNodesChangedSignalsOnPlugInHeadphone
) {
1516 // Set up initial audio devices.
1517 AudioNodeList audio_nodes
;
1518 audio_nodes
.push_back(kInternalSpeaker
);
1519 audio_nodes
.push_back(kBluetoothHeadset
);
1520 SetUpCrasAudioHandler(audio_nodes
);
1521 const size_t init_nodes_size
= audio_nodes
.size();
1523 // Verify the audio devices size.
1524 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1525 AudioDeviceList audio_devices
;
1526 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1527 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1529 // Verify the bluetooth headset is selected as the active output.
1530 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1531 EXPECT_EQ(kBluetoothHeadsetId
,
1532 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1533 AudioDevice active_output
;
1535 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1536 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1538 // Plug in headphone, but fire NodesChanged signal twice.
1539 audio_nodes
.clear();
1540 audio_nodes
.push_back(kInternalSpeaker
);
1541 AudioNode
bluetooth_headset(kBluetoothHeadset
);
1542 bluetooth_headset
.plugged_time
= 1000;
1543 bluetooth_headset
.active
= true;
1544 audio_nodes
.push_back(bluetooth_headset
);
1545 AudioNode
headphone(kHeadphone
);
1546 headphone
.active
= false;
1547 headphone
.plugged_time
= 2000;
1548 audio_nodes
.push_back(headphone
);
1549 ChangeAudioNodes(audio_nodes
);
1550 ChangeAudioNodes(audio_nodes
);
1552 // Verify the active output device is set to headphone.
1553 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1554 EXPECT_LE(1, test_observer_
->active_output_node_changed_count());
1555 EXPECT_EQ(headphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1557 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1558 EXPECT_EQ(headphone
.id
, active_output
.id
);
1560 // Verfiy the audio devices data is consistent, i.e., the active output device
1561 // should be headphone.
1562 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1563 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
1564 for (size_t i
= 0; i
< audio_devices
.size(); ++i
) {
1565 if (audio_devices
[i
].id
== kInternalSpeaker
.id
)
1566 EXPECT_FALSE(audio_devices
[i
].active
);
1567 else if (audio_devices
[i
].id
== bluetooth_headset
.id
)
1568 EXPECT_FALSE(audio_devices
[i
].active
);
1569 else if (audio_devices
[i
].id
== headphone
.id
)
1570 EXPECT_TRUE(audio_devices
[i
].active
);
1576 TEST_F(CrasAudioHandlerTest
, MultipleNodesChangedSignalsOnPlugInUSBMic
) {
1577 // Set up initial audio devices.
1578 AudioNodeList audio_nodes
;
1579 audio_nodes
.push_back(kInternalMic
);
1580 SetUpCrasAudioHandler(audio_nodes
);
1581 const size_t init_nodes_size
= audio_nodes
.size();
1583 // Verify the audio devices size.
1584 EXPECT_EQ(0, test_observer_
->audio_nodes_changed_count());
1585 AudioDeviceList audio_devices
;
1586 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1587 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1589 // Verify the internal mic is selected as the active output.
1590 EXPECT_EQ(0, test_observer_
->active_output_node_changed_count());
1591 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1592 EXPECT_FALSE(cras_audio_handler_
->has_alternative_output());
1593 EXPECT_TRUE(audio_devices
[0].active
);
1595 // Plug in usb mic, but fire NodesChanged signal twice.
1596 audio_nodes
.clear();
1597 AudioNode
internal_mic(kInternalMic
);
1598 internal_mic
.active
= true;
1599 internal_mic
.plugged_time
= 1000;
1600 audio_nodes
.push_back(internal_mic
);
1601 AudioNode
usb_mic(kUSBMic
);
1602 usb_mic
.active
= false;
1603 usb_mic
.plugged_time
= 2000;
1604 audio_nodes
.push_back(usb_mic
);
1605 ChangeAudioNodes(audio_nodes
);
1606 ChangeAudioNodes(audio_nodes
);
1608 // Verify the active output device is set to headphone.
1609 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1610 EXPECT_LE(1, test_observer_
->active_input_node_changed_count());
1611 EXPECT_EQ(usb_mic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1612 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1614 // Verfiy the audio devices data is consistent, i.e., the active input device
1615 // should be usb mic.
1616 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1617 EXPECT_EQ(init_nodes_size
+ 1, audio_devices
.size());
1618 for (size_t i
= 0; i
< audio_devices
.size(); ++i
) {
1619 if (audio_devices
[i
].id
== kInternalMic
.id
)
1620 EXPECT_FALSE(audio_devices
[i
].active
);
1621 else if (audio_devices
[i
].id
== usb_mic
.id
)
1622 EXPECT_TRUE(audio_devices
[i
].active
);
1628 // This is the case of crbug.com/291303.
1629 TEST_F(CrasAudioHandlerTest
, MultipleNodesChangedSignalsOnSystemBoot
) {
1630 // Set up audio handler with empty audio_nodes.
1631 AudioNodeList audio_nodes
;
1632 SetUpCrasAudioHandler(audio_nodes
);
1634 AudioNode
internal_speaker(kInternalSpeaker
);
1635 internal_speaker
.active
= false;
1636 AudioNode
headphone(kHeadphone
);
1637 headphone
.active
= false;
1638 AudioNode
internal_mic(kInternalMic
);
1639 internal_mic
.active
= false;
1640 audio_nodes
.push_back(internal_speaker
);
1641 audio_nodes
.push_back(headphone
);
1642 audio_nodes
.push_back(internal_mic
);
1643 const size_t init_nodes_size
= audio_nodes
.size();
1645 // Simulate AudioNodesChanged signal being fired twice during system boot.
1646 ChangeAudioNodes(audio_nodes
);
1647 ChangeAudioNodes(audio_nodes
);
1649 // Verify the active output device is set to headphone.
1650 EXPECT_EQ(2, test_observer_
->audio_nodes_changed_count());
1651 EXPECT_LE(1, test_observer_
->active_output_node_changed_count());
1652 EXPECT_EQ(headphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1653 AudioDevice active_output
;
1655 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1656 EXPECT_EQ(headphone
.id
, active_output
.id
);
1658 // Verify the active input device id is set to internal mic.
1659 EXPECT_EQ(internal_mic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1661 // Verfiy the audio devices data is consistent, i.e., the active output device
1662 // should be headphone, and the active input device should internal mic.
1663 AudioDeviceList audio_devices
;
1664 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1665 EXPECT_EQ(init_nodes_size
, audio_devices
.size());
1666 for (size_t i
= 0; i
< audio_devices
.size(); ++i
) {
1667 if (audio_devices
[i
].id
== internal_speaker
.id
)
1668 EXPECT_FALSE(audio_devices
[i
].active
);
1669 else if (audio_devices
[i
].id
== headphone
.id
)
1670 EXPECT_TRUE(audio_devices
[i
].active
);
1671 else if (audio_devices
[i
].id
== internal_mic
.id
)
1672 EXPECT_TRUE(audio_devices
[i
].active
);
1678 // This is the case of crbug.com/448924.
1679 TEST_F(CrasAudioHandlerTest
,
1680 TwoNodesChangedSignalsForLosingTowNodesOnOneUnplug
) {
1681 // Set up audio handler with 4 audio_nodes.
1682 AudioNodeList audio_nodes
;
1683 AudioNode
internal_speaker(kInternalSpeaker
);
1684 internal_speaker
.active
= false;
1685 AudioNode
headphone(kHeadphone
);
1686 headphone
.active
= false;
1687 AudioNode
internal_mic(kInternalMic
);
1688 internal_mic
.active
= false;
1689 AudioNode
micJack(kMicJack
);
1690 micJack
.active
= false;
1691 audio_nodes
.push_back(internal_speaker
);
1692 audio_nodes
.push_back(headphone
);
1693 audio_nodes
.push_back(internal_mic
);
1694 audio_nodes
.push_back(micJack
);
1695 SetUpCrasAudioHandler(audio_nodes
);
1697 // Verify the audio devices size.
1698 AudioDeviceList audio_devices
;
1699 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1700 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
1702 // Verify the headphone has been selected as the active output.
1703 AudioDevice active_output
;
1705 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1706 EXPECT_EQ(kHeadphone
.id
, active_output
.id
);
1707 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1708 EXPECT_TRUE(active_output
.active
);
1709 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1711 // Verify the mic Jack has been selected as the active input.
1712 EXPECT_EQ(micJack
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1713 const AudioDevice
* active_input
= GetDeviceFromId(micJack
.id
);
1714 EXPECT_TRUE(active_input
->active
);
1715 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1717 // Simulate the nodes list in first NodesChanged signal, only headphone is
1718 // removed, other nodes remains the same.
1719 AudioNodeList changed_nodes_1
;
1720 internal_speaker
.active
= false;
1721 changed_nodes_1
.push_back(internal_speaker
);
1722 internal_mic
.active
= false;
1723 changed_nodes_1
.push_back(internal_mic
);
1724 micJack
.active
= true;
1725 changed_nodes_1
.push_back(micJack
);
1727 // Simulate the nodes list in second NodesChanged signal, the micJac is
1728 // removed, but the internal_mic is inactive, which does not reflect the
1729 // active status set from the first NodesChanged signal since this was sent
1730 // before cras receives the SetActiveOutputNode from the first NodesChanged
1732 AudioNodeList changed_nodes_2
;
1733 changed_nodes_2
.push_back(internal_speaker
);
1734 changed_nodes_2
.push_back(internal_mic
);
1736 // Simulate AudioNodesChanged signal being fired twice for unplug an audio
1737 // device with both input and output nodes on it.
1738 ChangeAudioNodes(changed_nodes_1
);
1739 ChangeAudioNodes(changed_nodes_2
);
1741 AudioDeviceList changed_devices
;
1742 cras_audio_handler_
->GetAudioDevices(&changed_devices
);
1743 EXPECT_EQ(2u, changed_devices
.size());
1745 // Verify the active output device is set to internal speaker.
1746 EXPECT_EQ(internal_speaker
.id
,
1747 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1749 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1750 EXPECT_EQ(internal_speaker
.id
, active_output
.id
);
1751 EXPECT_TRUE(active_output
.active
);
1753 // Verify the active input device id is set to internal mic.
1754 EXPECT_EQ(internal_mic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1755 const AudioDevice
* changed_active_input
= GetDeviceFromId(internal_mic
.id
);
1756 EXPECT_TRUE(changed_active_input
->active
);
1759 TEST_F(CrasAudioHandlerTest
, SetOutputMute
) {
1760 AudioNodeList audio_nodes
;
1761 audio_nodes
.push_back(kInternalSpeaker
);
1762 SetUpCrasAudioHandler(audio_nodes
);
1763 EXPECT_EQ(0, test_observer_
->output_mute_changed_count());
1766 cras_audio_handler_
->SetOutputMute(true);
1768 // Verify the output is muted, OnOutputMuteChanged event is fired,
1769 // and mute value is saved in the preferences.
1770 EXPECT_TRUE(cras_audio_handler_
->IsOutputMuted());
1771 EXPECT_EQ(1, test_observer_
->output_mute_changed_count());
1772 AudioDevice
speaker(kInternalSpeaker
);
1773 EXPECT_TRUE(audio_pref_handler_
->GetMuteValue(speaker
));
1775 // Unmute the device.
1776 cras_audio_handler_
->SetOutputMute(false);
1778 // Verify the output is unmuted, OnOutputMuteChanged event is fired,
1779 // and mute value is saved in the preferences.
1780 EXPECT_FALSE(cras_audio_handler_
->IsOutputMuted());
1781 EXPECT_EQ(2, test_observer_
->output_mute_changed_count());
1782 EXPECT_FALSE(audio_pref_handler_
->GetMuteValue(speaker
));
1785 TEST_F(CrasAudioHandlerTest
, SetInputMute
) {
1786 AudioNodeList audio_nodes
;
1787 audio_nodes
.push_back(kInternalMic
);
1788 SetUpCrasAudioHandler(audio_nodes
);
1789 EXPECT_EQ(0, test_observer_
->input_mute_changed_count());
1792 cras_audio_handler_
->SetInputMute(true);
1794 // Verify the input is muted, OnInputMuteChanged event is fired.
1795 EXPECT_TRUE(cras_audio_handler_
->IsInputMuted());
1796 EXPECT_EQ(1, test_observer_
->input_mute_changed_count());
1798 // Unmute the device.
1799 cras_audio_handler_
->SetInputMute(false);
1801 // Verify the input is unmuted, OnInputMuteChanged event is fired.
1802 EXPECT_FALSE(cras_audio_handler_
->IsInputMuted());
1803 EXPECT_EQ(2, test_observer_
->input_mute_changed_count());
1806 TEST_F(CrasAudioHandlerTest
, SetOutputVolumePercent
) {
1807 AudioNodeList audio_nodes
;
1808 audio_nodes
.push_back(kInternalSpeaker
);
1809 SetUpCrasAudioHandler(audio_nodes
);
1810 EXPECT_EQ(0, test_observer_
->output_volume_changed_count());
1812 cras_audio_handler_
->SetOutputVolumePercent(60);
1814 // Verify the output volume is changed to the designated value,
1815 // OnOutputNodeVolumeChanged event is fired, and the device volume value
1816 // is saved the preferences.
1817 const int kVolume
= 60;
1818 EXPECT_EQ(kVolume
, cras_audio_handler_
->GetOutputVolumePercent());
1819 EXPECT_EQ(1, test_observer_
->output_volume_changed_count());
1821 EXPECT_TRUE(cras_audio_handler_
->GetPrimaryActiveOutputDevice(&device
));
1822 EXPECT_EQ(device
.id
, kInternalSpeaker
.id
);
1823 EXPECT_EQ(kVolume
, audio_pref_handler_
->GetOutputVolumeValue(&device
));
1826 TEST_F(CrasAudioHandlerTest
, SetInputGainPercent
) {
1827 AudioNodeList audio_nodes
;
1828 audio_nodes
.push_back(kInternalMic
);
1829 SetUpCrasAudioHandler(audio_nodes
);
1830 EXPECT_EQ(0, test_observer_
->input_gain_changed_count());
1832 cras_audio_handler_
->SetInputGainPercent(60);
1834 // Verify the input gain changed to the designated value,
1835 // OnInputNodeGainChanged event is fired, and the device gain value
1836 // is saved in the preferences.
1837 const int kGain
= 60;
1838 EXPECT_EQ(kGain
, cras_audio_handler_
->GetInputGainPercent());
1839 EXPECT_EQ(1, test_observer_
->input_gain_changed_count());
1840 AudioDevice
internal_mic(kInternalMic
);
1841 EXPECT_EQ(kGain
, audio_pref_handler_
->GetInputGainValue(&internal_mic
));
1844 TEST_F(CrasAudioHandlerTest
, SetMuteForDevice
) {
1845 AudioNodeList audio_nodes
;
1846 audio_nodes
.push_back(kInternalSpeaker
);
1847 audio_nodes
.push_back(kHeadphone
);
1848 audio_nodes
.push_back(kInternalMic
);
1849 audio_nodes
.push_back(kUSBMic
);
1850 SetUpCrasAudioHandler(audio_nodes
);
1852 // Mute the active output device.
1853 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1854 cras_audio_handler_
->SetMuteForDevice(kHeadphone
.id
, true);
1856 // Verify the headphone is muted and mute value is saved in the preferences.
1857 EXPECT_TRUE(cras_audio_handler_
->IsOutputMutedForDevice(kHeadphone
.id
));
1858 AudioDevice
headphone(kHeadphone
);
1859 EXPECT_TRUE(audio_pref_handler_
->GetMuteValue(headphone
));
1861 // Mute the non-active output device.
1862 cras_audio_handler_
->SetMuteForDevice(kInternalSpeaker
.id
, true);
1864 // Verify the internal speaker is muted and mute value is saved in the
1866 EXPECT_TRUE(cras_audio_handler_
->IsOutputMutedForDevice(kInternalSpeaker
.id
));
1867 AudioDevice
internal_speaker(kInternalSpeaker
);
1868 EXPECT_TRUE(audio_pref_handler_
->GetMuteValue(internal_speaker
));
1870 // Mute the active input device.
1871 EXPECT_EQ(kUSBMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1872 cras_audio_handler_
->SetMuteForDevice(kUSBMic
.id
, true);
1874 // Verify the USB Mic is muted.
1875 EXPECT_TRUE(cras_audio_handler_
->IsInputMutedForDevice(kUSBMic
.id
));
1877 // Mute the non-active input device should be a no-op, see crbug.com/365050.
1878 cras_audio_handler_
->SetMuteForDevice(kInternalMic
.id
, true);
1880 // Verify IsInputMutedForDevice returns false for non-active input device.
1881 EXPECT_FALSE(cras_audio_handler_
->IsInputMutedForDevice(kInternalMic
.id
));
1884 TEST_F(CrasAudioHandlerTest
, SetVolumeGainPercentForDevice
) {
1885 AudioNodeList audio_nodes
;
1886 audio_nodes
.push_back(kInternalSpeaker
);
1887 audio_nodes
.push_back(kHeadphone
);
1888 audio_nodes
.push_back(kInternalMic
);
1889 audio_nodes
.push_back(kUSBMic
);
1890 SetUpCrasAudioHandler(audio_nodes
);
1892 // Set volume percent for active output device.
1893 const int kHeadphoneVolume
= 30;
1894 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
1895 cras_audio_handler_
->SetVolumeGainPercentForDevice(kHeadphone
.id
,
1898 // Verify the volume percent of headphone is set, and saved in preferences.
1899 EXPECT_EQ(kHeadphoneVolume
,
1900 cras_audio_handler_
->GetOutputVolumePercentForDevice(
1902 AudioDevice
headphone(kHeadphone
);
1903 EXPECT_EQ(kHeadphoneVolume
,
1904 audio_pref_handler_
->GetOutputVolumeValue(&headphone
));
1906 // Set volume percent for non-active output device.
1907 const int kSpeakerVolume
= 60;
1908 cras_audio_handler_
->SetVolumeGainPercentForDevice(kInternalSpeaker
.id
,
1911 // Verify the volume percent of speaker is set, and saved in preferences.
1912 EXPECT_EQ(kSpeakerVolume
,
1913 cras_audio_handler_
->GetOutputVolumePercentForDevice(
1914 kInternalSpeaker
.id
));
1915 AudioDevice
speaker(kInternalSpeaker
);
1916 EXPECT_EQ(kSpeakerVolume
,
1917 audio_pref_handler_
->GetOutputVolumeValue(&speaker
));
1919 // Set gain percent for active input device.
1920 const int kUSBMicGain
= 30;
1921 EXPECT_EQ(kUSBMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1922 cras_audio_handler_
->SetVolumeGainPercentForDevice(kUSBMic
.id
,
1925 // Verify the gain percent of USB mic is set, and saved in preferences.
1926 EXPECT_EQ(kUSBMicGain
,
1927 cras_audio_handler_
->GetOutputVolumePercentForDevice(kUSBMic
.id
));
1928 AudioDevice
usb_mic(kHeadphone
);
1929 EXPECT_EQ(kUSBMicGain
,
1930 audio_pref_handler_
->GetInputGainValue(&usb_mic
));
1932 // Set gain percent for non-active input device.
1933 const int kInternalMicGain
= 60;
1934 cras_audio_handler_
->SetVolumeGainPercentForDevice(kInternalMic
.id
,
1937 // Verify the gain percent of internal mic is set, and saved in preferences.
1938 EXPECT_EQ(kInternalMicGain
,
1939 cras_audio_handler_
->GetOutputVolumePercentForDevice(
1941 AudioDevice
internal_mic(kInternalMic
);
1942 EXPECT_EQ(kInternalMicGain
,
1943 audio_pref_handler_
->GetInputGainValue(&internal_mic
));
1946 TEST_F(CrasAudioHandlerTest
, HandleOtherDeviceType
) {
1947 const size_t kNumValidAudioDevices
= 4;
1948 AudioNodeList audio_nodes
;
1949 audio_nodes
.push_back(kInternalSpeaker
);
1950 audio_nodes
.push_back(kOtherTypeOutput
);
1951 audio_nodes
.push_back(kInternalMic
);
1952 audio_nodes
.push_back(kOtherTypeInput
);
1953 SetUpCrasAudioHandler(audio_nodes
);
1955 // Verify the audio devices size.
1956 AudioDeviceList audio_devices
;
1957 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1958 EXPECT_EQ(kNumValidAudioDevices
, audio_devices
.size());
1960 // Verify the internal speaker has been selected as the active output,
1961 // and the output device with some randown unknown type is handled gracefully.
1962 AudioDevice active_output
;
1964 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1965 EXPECT_EQ(kInternalSpeaker
.id
, active_output
.id
);
1966 EXPECT_EQ(kInternalSpeaker
.id
,
1967 cras_audio_handler_
->GetPrimaryActiveOutputNode());
1968 EXPECT_TRUE(cras_audio_handler_
->has_alternative_output());
1970 // Ensure the internal microphone has been selected as the active input,
1971 // and the input device with some random unknown type is handled gracefully.
1972 AudioDevice active_input
;
1973 EXPECT_EQ(kInternalMic
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
1974 EXPECT_TRUE(cras_audio_handler_
->has_alternative_input());
1977 TEST_F(CrasAudioHandlerTest
, ChangeActiveNodesHotrodInit
) {
1978 AudioNodeList audio_nodes
;
1979 audio_nodes
.push_back(kHDMIOutput
);
1980 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
1981 audio_nodes
.push_back(kUSBJabraSpeakerOutput2
);
1982 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
1983 audio_nodes
.push_back(kUSBJabraSpeakerInput2
);
1984 audio_nodes
.push_back(kUSBCameraInput
);
1985 SetUpCrasAudioHandler(audio_nodes
);
1987 // Verify the audio devices size.
1988 AudioDeviceList audio_devices
;
1989 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
1990 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
1992 // Verify only the 1st jabra speaker's output and input are selected as active
1993 // nodes by CrasAudioHandler.
1994 AudioDevice active_output
;
1996 cras_audio_handler_
->GetPrimaryActiveOutputDevice(&active_output
));
1997 EXPECT_EQ(2, GetActiveDeviceCount());
1998 AudioDevice primary_active_device
;
1999 EXPECT_TRUE(cras_audio_handler_
->GetPrimaryActiveOutputDevice(
2000 &primary_active_device
));
2001 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
, primary_active_device
.id
);
2002 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2003 cras_audio_handler_
->GetPrimaryActiveInputNode());
2005 // Set both jabra speakers's input and output nodes to active, this simulate
2006 // the call sent by hotrod initialization process.
2007 test_observer_
->reset_active_output_node_changed_count();
2008 test_observer_
->reset_active_input_node_changed_count();
2009 CrasAudioHandler::NodeIdList active_nodes
;
2010 active_nodes
.push_back(kUSBJabraSpeakerOutput1
.id
);
2011 active_nodes
.push_back(kUSBJabraSpeakerOutput2
.id
);
2012 active_nodes
.push_back(kUSBJabraSpeakerInput1
.id
);
2013 active_nodes
.push_back(kUSBJabraSpeakerInput2
.id
);
2014 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2016 // Verify both jabra speakers' input/output nodes are made active.
2017 // num_active_nodes = GetActiveDeviceCount();
2018 EXPECT_EQ(4, GetActiveDeviceCount());
2019 const AudioDevice
* active_output_1
=
2020 GetDeviceFromId(kUSBJabraSpeakerOutput1
.id
);
2021 EXPECT_TRUE(active_output_1
->active
);
2022 const AudioDevice
* active_output_2
=
2023 GetDeviceFromId(kUSBJabraSpeakerOutput2
.id
);
2024 EXPECT_TRUE(active_output_2
->active
);
2025 EXPECT_TRUE(cras_audio_handler_
->GetPrimaryActiveOutputDevice(
2026 &primary_active_device
));
2027 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
, primary_active_device
.id
);
2028 const AudioDevice
* active_input_1
=
2029 GetDeviceFromId(kUSBJabraSpeakerInput1
.id
);
2030 EXPECT_TRUE(active_input_1
->active
);
2031 const AudioDevice
* active_input_2
=
2032 GetDeviceFromId(kUSBJabraSpeakerInput2
.id
);
2033 EXPECT_TRUE(active_input_2
->active
);
2034 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2035 cras_audio_handler_
->GetPrimaryActiveInputNode());
2037 // Verify only 1 ActiveOutputNodeChanged notification has been sent out
2038 // by calling ChangeActiveNodes.
2039 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
2040 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
2042 // Verify all active devices are the not muted and their volume values are
2044 EXPECT_FALSE(cras_audio_handler_
->IsOutputMuted());
2046 cras_audio_handler_
->IsOutputMutedForDevice(kUSBJabraSpeakerOutput1
.id
));
2048 cras_audio_handler_
->IsOutputMutedForDevice(kUSBJabraSpeakerOutput2
.id
));
2049 EXPECT_EQ(cras_audio_handler_
->GetOutputVolumePercent(),
2050 cras_audio_handler_
->GetOutputVolumePercentForDevice(
2051 kUSBJabraSpeakerOutput1
.id
));
2052 EXPECT_EQ(cras_audio_handler_
->GetOutputVolumePercent(),
2053 cras_audio_handler_
->GetOutputVolumePercentForDevice(
2054 kUSBJabraSpeakerOutput2
.id
));
2056 // Adjust the volume of output devices, verify all active nodes are set to
2058 cras_audio_handler_
->SetOutputVolumePercent(25);
2059 EXPECT_EQ(25, cras_audio_handler_
->GetOutputVolumePercent());
2060 EXPECT_EQ(25, cras_audio_handler_
->GetOutputVolumePercentForDevice(
2061 kUSBJabraSpeakerOutput1
.id
));
2062 EXPECT_EQ(25, cras_audio_handler_
->GetOutputVolumePercentForDevice(
2063 kUSBJabraSpeakerOutput2
.id
));
2066 TEST_F(CrasAudioHandlerTest
, ChangeActiveNodesHotrodInitWithCameraInputActive
) {
2067 AudioNodeList audio_nodes
;
2068 audio_nodes
.push_back(kHDMIOutput
);
2069 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2070 audio_nodes
.push_back(kUSBJabraSpeakerOutput2
);
2071 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2072 audio_nodes
.push_back(kUSBJabraSpeakerInput2
);
2073 // Make the camera input to be plugged in later than jabra's input.
2074 AudioNode
usb_camera(kUSBCameraInput
);
2075 usb_camera
.plugged_time
= 10000000;
2076 audio_nodes
.push_back(usb_camera
);
2077 SetUpCrasAudioHandler(audio_nodes
);
2079 // Verify the audio devices size.
2080 AudioDeviceList audio_devices
;
2081 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2082 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2084 // Verify the 1st jabra speaker's output is selected as active output
2085 // node and camera's input is selected active input by CrasAudioHandler.
2086 EXPECT_EQ(2, GetActiveDeviceCount());
2087 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2088 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2089 EXPECT_EQ(kUSBCameraInput
.id
,
2090 cras_audio_handler_
->GetPrimaryActiveInputNode());
2092 // Set both jabra speakers's input and output nodes to active, this simulates
2093 // the call sent by hotrod initialization process.
2094 test_observer_
->reset_active_output_node_changed_count();
2095 test_observer_
->reset_active_input_node_changed_count();
2096 CrasAudioHandler::NodeIdList active_nodes
;
2097 active_nodes
.push_back(kUSBJabraSpeakerOutput1
.id
);
2098 active_nodes
.push_back(kUSBJabraSpeakerOutput2
.id
);
2099 active_nodes
.push_back(kUSBJabraSpeakerInput1
.id
);
2100 active_nodes
.push_back(kUSBJabraSpeakerInput2
.id
);
2101 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2103 // Verify both jabra speakers' input/output nodes are made active.
2104 // num_active_nodes = GetActiveDeviceCount();
2105 EXPECT_EQ(4, GetActiveDeviceCount());
2106 const AudioDevice
* active_output_1
=
2107 GetDeviceFromId(kUSBJabraSpeakerOutput1
.id
);
2108 EXPECT_TRUE(active_output_1
->active
);
2109 const AudioDevice
* active_output_2
=
2110 GetDeviceFromId(kUSBJabraSpeakerOutput2
.id
);
2111 EXPECT_TRUE(active_output_2
->active
);
2112 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2113 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2114 const AudioDevice
* active_input_1
=
2115 GetDeviceFromId(kUSBJabraSpeakerInput1
.id
);
2116 EXPECT_TRUE(active_input_1
->active
);
2117 const AudioDevice
* active_input_2
=
2118 GetDeviceFromId(kUSBJabraSpeakerInput2
.id
);
2119 EXPECT_TRUE(active_input_2
->active
);
2120 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2121 cras_audio_handler_
->GetPrimaryActiveInputNode());
2123 // Verify only 1 ActiveOutputNodeChanged notification has been sent out
2124 // by calling ChangeActiveNodes.
2125 EXPECT_EQ(1, test_observer_
->active_output_node_changed_count());
2126 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
2129 TEST_F(CrasAudioHandlerTest
, ChangeActiveNodesWithFewerActives
) {
2130 AudioNodeList audio_nodes
;
2131 audio_nodes
.push_back(kHDMIOutput
);
2132 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2133 audio_nodes
.push_back(kUSBJabraSpeakerOutput2
);
2134 SetUpCrasAudioHandler(audio_nodes
);
2136 // Verify the audio devices size.
2137 AudioDeviceList audio_devices
;
2138 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2139 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2141 // Set all three nodes to be active.
2142 CrasAudioHandler::NodeIdList active_nodes
;
2143 active_nodes
.push_back(kHDMIOutput
.id
);
2144 active_nodes
.push_back(kUSBJabraSpeakerOutput1
.id
);
2145 active_nodes
.push_back(kUSBJabraSpeakerOutput2
.id
);
2146 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2148 // Verify all three nodes are active.
2149 EXPECT_EQ(3, GetActiveDeviceCount());
2150 const AudioDevice
* active_output_1
= GetDeviceFromId(kHDMIOutput
.id
);
2151 EXPECT_TRUE(active_output_1
->active
);
2152 const AudioDevice
* active_output_2
=
2153 GetDeviceFromId(kUSBJabraSpeakerOutput1
.id
);
2154 EXPECT_TRUE(active_output_2
->active
);
2155 const AudioDevice
* active_output_3
=
2156 GetDeviceFromId(kUSBJabraSpeakerOutput2
.id
);
2157 EXPECT_TRUE(active_output_3
->active
);
2159 // Now call ChangeActiveDevices with only 2 nodes.
2160 active_nodes
.clear();
2161 active_nodes
.push_back(kUSBJabraSpeakerOutput1
.id
);
2162 active_nodes
.push_back(kUSBJabraSpeakerOutput2
.id
);
2163 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2165 // Verify only 2 nodes are active.
2166 EXPECT_EQ(2, GetActiveDeviceCount());
2167 const AudioDevice
* output_1
= GetDeviceFromId(kHDMIOutput
.id
);
2168 EXPECT_FALSE(output_1
->active
);
2169 const AudioDevice
* output_2
= GetDeviceFromId(kUSBJabraSpeakerOutput1
.id
);
2170 EXPECT_TRUE(output_2
->active
);
2171 const AudioDevice
* output_3
= GetDeviceFromId(kUSBJabraSpeakerOutput2
.id
);
2172 EXPECT_TRUE(output_3
->active
);
2175 TEST_F(CrasAudioHandlerTest
, HotrodInitWithSingleJabra
) {
2176 // Simulates the hotrod initializated with a single jabra device and
2177 // CrasAudioHandler selected jabra input/output as active devices.
2178 AudioNodeList audio_nodes
;
2179 audio_nodes
.push_back(kHDMIOutput
);
2180 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2181 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2182 audio_nodes
.push_back(kUSBCameraInput
);
2183 SetUpCrasAudioHandler(audio_nodes
);
2185 // Verify the audio devices size.
2186 AudioDeviceList audio_devices
;
2187 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2188 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2190 // Verify the jabra speaker's output and input are selected as active nodes
2191 // by CrasAudioHandler.
2192 EXPECT_EQ(2, GetActiveDeviceCount());
2193 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2194 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2195 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2196 cras_audio_handler_
->GetPrimaryActiveInputNode());
2199 TEST_F(CrasAudioHandlerTest
,
2200 ChangeActiveNodesHotrodInitWithSingleJabraCameraPlugInLater
) {
2201 AudioNodeList audio_nodes
;
2202 audio_nodes
.push_back(kHDMIOutput
);
2203 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2204 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2205 AudioNode
usb_camera(kUSBCameraInput
);
2206 usb_camera
.plugged_time
= 10000000;
2207 audio_nodes
.push_back(usb_camera
);
2208 SetUpCrasAudioHandler(audio_nodes
);
2210 // Verify the audio devices size.
2211 AudioDeviceList audio_devices
;
2212 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2213 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2215 // Verify the jabra speaker's output is selected as active output, and
2216 // camera's input is selected as active input by CrasAudioHandler
2217 EXPECT_EQ(2, GetActiveDeviceCount());
2218 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2219 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2220 EXPECT_EQ(kUSBCameraInput
.id
,
2221 cras_audio_handler_
->GetPrimaryActiveInputNode());
2223 // Simulate hotrod app call to set jabra input as active device with only
2224 // jabra input node in the active node list, which does not conform to the
2225 // new SetActiveDevices protocol, but just show we can still handle it if
2227 CrasAudioHandler::NodeIdList active_nodes
;
2228 active_nodes
.push_back(kUSBJabraSpeakerOutput1
.id
);
2229 active_nodes
.push_back(kUSBJabraSpeakerInput1
.id
);
2230 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2232 // Verify the jabra speaker's output is selected as active output, and
2233 // jabra's input is selected as active input.
2234 EXPECT_EQ(2, GetActiveDeviceCount());
2235 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2236 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2237 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2238 cras_audio_handler_
->GetPrimaryActiveInputNode());
2241 TEST_F(CrasAudioHandlerTest
,
2242 ChangeActiveNodesHotrodInitWithSingleJabraCameraPlugInLaterOldCall
) {
2243 AudioNodeList audio_nodes
;
2244 audio_nodes
.push_back(kHDMIOutput
);
2245 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2246 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2247 AudioNode
usb_camera(kUSBCameraInput
);
2248 usb_camera
.plugged_time
= 10000000;
2249 audio_nodes
.push_back(usb_camera
);
2250 SetUpCrasAudioHandler(audio_nodes
);
2252 // Verify the audio devices size.
2253 AudioDeviceList audio_devices
;
2254 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2255 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2257 // Verify the jabra speaker's output is selected as active output, and
2258 // camera's input is selected as active input by CrasAudioHandler
2259 EXPECT_EQ(2, GetActiveDeviceCount());
2260 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2261 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2262 EXPECT_EQ(kUSBCameraInput
.id
,
2263 cras_audio_handler_
->GetPrimaryActiveInputNode());
2265 // Simulate hotrod app call to set jabra input as active device with only
2266 // jabra input node in the active node list, which does not conform to the
2267 // new SetActiveDevices protocol, but just show we can still handle it if
2269 CrasAudioHandler::NodeIdList active_nodes
;
2270 active_nodes
.push_back(kUSBJabraSpeakerInput1
.id
);
2271 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2273 // Verify the jabra speaker's output is selected as active output, and
2274 // jabra's input is selected as active input.
2275 EXPECT_EQ(2, GetActiveDeviceCount());
2276 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2277 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2278 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2279 cras_audio_handler_
->GetPrimaryActiveInputNode());
2282 TEST_F(CrasAudioHandlerTest
,
2283 ChangeActiveNodesHotrodInitWithSingleJabraChangeOutput
) {
2284 AudioNodeList audio_nodes
;
2285 audio_nodes
.push_back(kHDMIOutput
);
2286 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2287 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2288 audio_nodes
.push_back(kUSBCameraInput
);
2289 SetUpCrasAudioHandler(audio_nodes
);
2291 // Verify the audio devices size.
2292 AudioDeviceList audio_devices
;
2293 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2294 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2296 // Verify the jabra speaker's output and input are selected as active output
2297 // by CrasAudioHandler.
2298 EXPECT_EQ(2, GetActiveDeviceCount());
2299 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2300 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2301 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2302 cras_audio_handler_
->GetPrimaryActiveInputNode());
2304 // Simulate hotrod app call SetActiveDevices to change active output
2305 // with only complete list of active nodes passed in, which is the new
2306 // way of hotrod app.
2307 CrasAudioHandler::NodeIdList active_nodes
;
2308 active_nodes
.push_back(kHDMIOutput
.id
);
2309 active_nodes
.push_back(kUSBJabraSpeakerInput1
.id
);
2310 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2312 // Verify the jabra speaker's output is selected as active output, and
2313 // jabra's input is selected as active input.
2314 EXPECT_EQ(2, GetActiveDeviceCount());
2315 EXPECT_EQ(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2316 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2317 cras_audio_handler_
->GetPrimaryActiveInputNode());
2320 TEST_F(CrasAudioHandlerTest
,
2321 ChangeActiveNodesHotrodInitWithSingleJabraChangeOutputOldCall
) {
2322 AudioNodeList audio_nodes
;
2323 audio_nodes
.push_back(kHDMIOutput
);
2324 audio_nodes
.push_back(kUSBJabraSpeakerOutput1
);
2325 audio_nodes
.push_back(kUSBJabraSpeakerInput1
);
2326 audio_nodes
.push_back(kUSBCameraInput
);
2327 SetUpCrasAudioHandler(audio_nodes
);
2329 // Verify the audio devices size.
2330 AudioDeviceList audio_devices
;
2331 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2332 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2334 // Verify the jabra speaker's output and input are selected as active output
2335 // by CrasAudioHandler.
2336 EXPECT_EQ(2, GetActiveDeviceCount());
2337 EXPECT_EQ(kUSBJabraSpeakerOutput1
.id
,
2338 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2339 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2340 cras_audio_handler_
->GetPrimaryActiveInputNode());
2342 // Simulate hotrod app call SetActiveDevices to change active output
2343 // with only a single active output nodes passed in, which is the old
2344 // way of hotrod app.
2345 CrasAudioHandler::NodeIdList active_nodes
;
2346 active_nodes
.push_back(kHDMIOutput
.id
);
2347 cras_audio_handler_
->ChangeActiveNodes(active_nodes
);
2349 // Verify the jabra speaker's output is selected as active output, and
2350 // jabra's input is selected as active input.
2351 EXPECT_EQ(2, GetActiveDeviceCount());
2352 EXPECT_EQ(kHDMIOutput
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2353 EXPECT_EQ(kUSBJabraSpeakerInput1
.id
,
2354 cras_audio_handler_
->GetPrimaryActiveInputNode());
2357 TEST_F(CrasAudioHandlerTest
, NoMoreAudioInputDevices
) {
2358 // Some device like chromebox does not have the internal input device. The
2359 // active devices should be reset when the user plugs a device and then
2360 // unplugs it to such device.
2362 AudioNodeList audio_nodes
;
2363 audio_nodes
.push_back(kInternalSpeaker
);
2364 SetUpCrasAudioHandler(audio_nodes
);
2366 EXPECT_EQ(0ULL, cras_audio_handler_
->GetPrimaryActiveInputNode());
2368 audio_nodes
.push_back(kMicJack
);
2369 ChangeAudioNodes(audio_nodes
);
2371 EXPECT_EQ(kMicJack
.id
, cras_audio_handler_
->GetPrimaryActiveInputNode());
2372 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
2373 test_observer_
->reset_active_input_node_changed_count();
2375 audio_nodes
.pop_back();
2376 ChangeAudioNodes(audio_nodes
);
2377 EXPECT_EQ(0ULL, cras_audio_handler_
->GetPrimaryActiveInputNode());
2378 EXPECT_EQ(1, test_observer_
->active_input_node_changed_count());
2381 // Test the case in which an HDMI output is plugged in with other higher
2383 // output devices already plugged and user has manually selected an active
2385 // The hotplug of hdmi output should not change user's selection of active
2387 // crbug.com/447826.
2388 TEST_F(CrasAudioHandlerTest
, HotPlugHDMINotChangeActiveOutput
) {
2389 AudioNodeList audio_nodes
;
2390 AudioNode
internal_speaker(kInternalSpeaker
);
2391 audio_nodes
.push_back(internal_speaker
);
2392 AudioNode
usb_headset(kUSBHeadphone1
);
2393 usb_headset
.plugged_time
= 80000000;
2394 audio_nodes
.push_back(usb_headset
);
2395 SetUpCrasAudioHandler(audio_nodes
);
2397 // Verify the audio devices size.
2398 AudioDeviceList audio_devices
;
2399 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2400 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2402 // Verify the USB headset is selected as active output by default.
2403 EXPECT_EQ(usb_headset
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2405 // Manually set the active output to internal speaker.
2406 AudioDevice
internal_output(kInternalSpeaker
);
2407 cras_audio_handler_
->SwitchToDevice(internal_output
, true);
2409 // Verify the active output is switched to internal speaker.
2410 EXPECT_EQ(internal_speaker
.id
,
2411 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2412 EXPECT_LT(kInternalSpeaker
.plugged_time
, usb_headset
.plugged_time
);
2413 const AudioDevice
* usb_device
= GetDeviceFromId(usb_headset
.id
);
2414 EXPECT_FALSE(usb_device
->active
);
2416 // Plug in HDMI output.
2417 audio_nodes
.clear();
2418 internal_speaker
.active
= true;
2419 audio_nodes
.push_back(internal_speaker
);
2420 usb_headset
.active
= false;
2421 audio_nodes
.push_back(usb_headset
);
2422 AudioNode
hdmi(kHDMIOutput
);
2423 hdmi
.plugged_time
= 90000000;
2424 audio_nodes
.push_back(hdmi
);
2425 ChangeAudioNodes(audio_nodes
);
2427 // The active output should not change.
2428 EXPECT_EQ(kInternalSpeaker
.id
,
2429 cras_audio_handler_
->GetPrimaryActiveOutputNode());
2432 // Test the case in which the active device was set to inactive from cras after
2433 // resuming from suspension state. See crbug.com/478968.
2434 TEST_F(CrasAudioHandlerTest
, ActiveNodeLostAfterResume
) {
2435 AudioNodeList audio_nodes
;
2436 EXPECT_FALSE(kHeadphone
.active
);
2437 audio_nodes
.push_back(kHeadphone
);
2438 EXPECT_FALSE(kHDMIOutput
.active
);
2439 audio_nodes
.push_back(kHDMIOutput
);
2440 SetUpCrasAudioHandler(audio_nodes
);
2442 // Verify the headphone is selected as the active output.
2443 AudioDeviceList audio_devices
;
2444 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2445 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2446 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2447 const AudioDevice
* active_headphone
= GetDeviceFromId(kHeadphone
.id
);
2448 EXPECT_EQ(kHeadphone
.id
, active_headphone
->id
);
2449 EXPECT_TRUE(active_headphone
->active
);
2451 // Simulate NodesChanged signal with headphone turning into inactive state,
2452 // and HDMI node removed.
2453 audio_nodes
.clear();
2454 audio_nodes
.push_back(kHeadphone
);
2455 ChangeAudioNodes(audio_nodes
);
2457 // Verify the headphone is set to active again.
2458 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2459 const AudioDevice
* headphone_resumed
= GetDeviceFromId(kHeadphone
.id
);
2460 EXPECT_EQ(kHeadphone
.id
, headphone_resumed
->id
);
2461 EXPECT_TRUE(headphone_resumed
->active
);
2464 // Test the case in which there are two NodesChanged signal for discovering
2465 // output devices, and there is race between NodesChange and SetActiveOutput
2466 // during this process. See crbug.com/478968.
2467 TEST_F(CrasAudioHandlerTest
, ActiveNodeLostDuringLoginSession
) {
2468 AudioNodeList audio_nodes
;
2469 EXPECT_FALSE(kHeadphone
.active
);
2470 audio_nodes
.push_back(kHeadphone
);
2471 SetUpCrasAudioHandler(audio_nodes
);
2473 // Verify the headphone is selected as the active output.
2474 AudioDeviceList audio_devices
;
2475 cras_audio_handler_
->GetAudioDevices(&audio_devices
);
2476 EXPECT_EQ(audio_nodes
.size(), audio_devices
.size());
2477 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2478 const AudioDevice
* active_headphone
= GetDeviceFromId(kHeadphone
.id
);
2479 EXPECT_EQ(kHeadphone
.id
, active_headphone
->id
);
2480 EXPECT_TRUE(active_headphone
->active
);
2482 // Simulate NodesChanged signal with headphone turning into inactive state,
2483 // and add a new HDMI output node.
2484 audio_nodes
.clear();
2485 audio_nodes
.push_back(kHeadphone
);
2486 audio_nodes
.push_back(kHDMIOutput
);
2487 ChangeAudioNodes(audio_nodes
);
2489 // Verify the headphone is set to active again.
2490 EXPECT_EQ(kHeadphone
.id
, cras_audio_handler_
->GetPrimaryActiveOutputNode());
2491 const AudioDevice
* headphone_resumed
= GetDeviceFromId(kHeadphone
.id
);
2492 EXPECT_EQ(kHeadphone
.id
, headphone_resumed
->id
);
2493 EXPECT_TRUE(headphone_resumed
->active
);
2496 } // namespace chromeos