Add ICU message format support
[chromium-blink-merge.git] / chromeos / audio / cras_audio_handler_unittest.cc
blob4633d8773f0a4b19d76ab08866ea9b074d9662b8
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/bind.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "base/values.h"
14 #include "chromeos/audio/audio_devices_pref_handler_stub.h"
15 #include "chromeos/dbus/audio_node.h"
16 #include "chromeos/dbus/dbus_thread_manager.h"
17 #include "chromeos/dbus/fake_cras_audio_client.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 namespace chromeos {
21 namespace {
23 const uint64 kInternalSpeakerId = 10001;
24 const uint64 kHeadphoneId = 10002;
25 const uint64 kInternalMicId = 10003;
26 const uint64 kUSBMicId = 10004;
27 const uint64 kBluetoothHeadsetId = 10005;
28 const uint64 kHDMIOutputId = 10006;
29 const uint64 kUSBHeadphoneId1 = 10007;
30 const uint64 kUSBHeadphoneId2 = 10008;
31 const uint64 kMicJackId = 10009;
32 const uint64 kKeyboardMicId = 10010;
33 const uint64 kOtherTypeOutputId = 90001;
34 const uint64 kOtherTypeInputId = 90002;
35 const uint64 kUSBJabraSpeakerOutputId1 = 90003;
36 const uint64 kUSBJabraSpeakerOutputId2 = 90004;
37 const uint64 kUSBJabraSpeakerInputId1 = 90005;
38 const uint64 kUSBJabraSpeakerInputId2 = 90006;
39 const uint64 kUSBCameraInputId = 90007;
41 const AudioNode kInternalSpeaker(
42 false,
43 kInternalSpeakerId,
44 "Fake Speaker",
45 "INTERNAL_SPEAKER",
46 "Speaker",
47 false,
51 const AudioNode kHeadphone(
52 false,
53 kHeadphoneId,
54 "Fake Headphone",
55 "HEADPHONE",
56 "Headphone",
57 false,
61 const AudioNode kInternalMic(
62 true,
63 kInternalMicId,
64 "Fake Mic",
65 "INTERNAL_MIC",
66 "Internal Mic",
67 false,
71 const AudioNode kMicJack(
72 true,
73 kMicJackId,
74 "Fake Mic Jack",
75 "MIC",
76 "Mic Jack",
77 false,
81 const AudioNode kUSBMic(
82 true,
83 kUSBMicId,
84 "Fake USB Mic",
85 "USB",
86 "USB Microphone",
87 false,
91 const AudioNode kKeyboardMic(
92 true,
93 kKeyboardMicId,
94 "Fake Keyboard Mic",
95 "KEYBOARD_MIC",
96 "Keyboard Mic",
97 false,
101 const AudioNode kOtherTypeOutput(
102 false,
103 kOtherTypeOutputId,
104 "Output Device",
105 "SOME_OTHER_TYPE",
106 "Other Type Output Device",
107 false,
111 const AudioNode kOtherTypeInput(
112 true,
113 kOtherTypeInputId,
114 "Input Device",
115 "SOME_OTHER_TYPE",
116 "Other Type Input Device",
117 false,
121 const AudioNode kBluetoothHeadset(false,
122 kBluetoothHeadsetId,
123 "Bluetooth Headset",
124 "BLUETOOTH",
125 "Bluetooth Headset 1",
126 false,
129 const AudioNode kHDMIOutput(false,
130 kHDMIOutputId,
131 "HDMI output",
132 "HDMI",
133 "HDMI output",
134 false,
137 const AudioNode kUSBHeadphone1(false,
138 kUSBHeadphoneId1,
139 "USB Headphone",
140 "USB",
141 "USB Headphone 1",
142 false,
145 const AudioNode kUSBHeadphone2(false,
146 kUSBHeadphoneId2,
147 "USB Headphone",
148 "USB",
149 "USB Headphone 1",
150 false,
153 const AudioNode kUSBJabraSpeakerOutput1(false,
154 kUSBJabraSpeakerOutputId1,
155 "Jabra Speaker 1",
156 "USB",
157 "Jabra Speaker 1",
158 false,
161 const AudioNode kUSBJabraSpeakerOutput2(false,
162 kUSBJabraSpeakerOutputId2,
163 "Jabra Speaker 2",
164 "USB",
165 "Jabra Speaker 2",
166 false,
169 const AudioNode kUSBJabraSpeakerInput1(true,
170 kUSBJabraSpeakerInputId1,
171 "Jabra Speaker 1",
172 "USB",
173 "Jabra Speaker",
174 false,
177 const AudioNode kUSBJabraSpeakerInput2(true,
178 kUSBJabraSpeakerInputId2,
179 "Jabra Speaker 2",
180 "USB",
181 "Jabra Speaker 2",
182 false,
185 const AudioNode kUSBCameraInput(true,
186 kUSBCameraInputId,
187 "USB Camera",
188 "USB",
189 "USB Camera",
190 false,
193 class TestObserver : public chromeos::CrasAudioHandler::AudioObserver {
194 public:
195 TestObserver()
196 : active_output_node_changed_count_(0),
197 active_input_node_changed_count_(0),
198 audio_nodes_changed_count_(0),
199 output_mute_changed_count_(0),
200 input_mute_changed_count_(0),
201 output_volume_changed_count_(0),
202 input_gain_changed_count_(0),
203 output_mute_by_system_(false) {}
205 int active_output_node_changed_count() const {
206 return active_output_node_changed_count_;
209 void reset_active_output_node_changed_count() {
210 active_output_node_changed_count_ = 0;
213 int active_input_node_changed_count() const {
214 return active_input_node_changed_count_;
217 void reset_active_input_node_changed_count() {
218 active_input_node_changed_count_ = 0;
221 int audio_nodes_changed_count() const {
222 return audio_nodes_changed_count_;
225 int output_mute_changed_count() const {
226 return output_mute_changed_count_;
229 void reset_output_mute_changed_count() { input_mute_changed_count_ = 0; }
231 int input_mute_changed_count() const {
232 return input_mute_changed_count_;
235 int output_volume_changed_count() const {
236 return output_volume_changed_count_;
239 int input_gain_changed_count() const {
240 return input_gain_changed_count_;
243 bool output_mute_by_system() const { return output_mute_by_system_; }
245 ~TestObserver() override {}
247 protected:
248 // chromeos::CrasAudioHandler::AudioObserver overrides.
249 void OnActiveOutputNodeChanged() override {
250 ++active_output_node_changed_count_;
253 void OnActiveInputNodeChanged() override {
254 ++active_input_node_changed_count_;
257 void OnAudioNodesChanged() override { ++audio_nodes_changed_count_; }
259 void OnOutputMuteChanged(bool /* mute_on */, bool system_adjust) override {
260 ++output_mute_changed_count_;
261 output_mute_by_system_ = system_adjust;
264 void OnInputMuteChanged(bool /* mute_on */) override {
265 ++input_mute_changed_count_;
268 void OnOutputNodeVolumeChanged(uint64 /* node_id */,
269 int /* volume */) override {
270 ++output_volume_changed_count_;
273 void OnInputNodeGainChanged(uint64 /* node_id */, int /* gain */) override {
274 ++input_gain_changed_count_;
277 private:
278 int active_output_node_changed_count_;
279 int active_input_node_changed_count_;
280 int audio_nodes_changed_count_;
281 int output_mute_changed_count_;
282 int input_mute_changed_count_;
283 int output_volume_changed_count_;
284 int input_gain_changed_count_;
285 bool output_mute_by_system_; // output mute state adjusted by system.
287 DISALLOW_COPY_AND_ASSIGN(TestObserver);
290 } // namespace
292 class CrasAudioHandlerTest : public testing::Test {
293 public:
294 CrasAudioHandlerTest() : cras_audio_handler_(NULL),
295 fake_cras_audio_client_(NULL) {
297 ~CrasAudioHandlerTest() override {}
299 void SetUp() override {}
301 void TearDown() override {
302 cras_audio_handler_->RemoveAudioObserver(test_observer_.get());
303 test_observer_.reset();
304 CrasAudioHandler::Shutdown();
305 audio_pref_handler_ = NULL;
306 DBusThreadManager::Shutdown();
309 void SetUpCrasAudioHandler(const AudioNodeList& audio_nodes) {
310 DBusThreadManager::Initialize();
311 fake_cras_audio_client_ = static_cast<FakeCrasAudioClient*>(
312 DBusThreadManager::Get()->GetCrasAudioClient());
313 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes);
314 audio_pref_handler_ = new AudioDevicesPrefHandlerStub();
315 CrasAudioHandler::Initialize(audio_pref_handler_);
316 cras_audio_handler_ = CrasAudioHandler::Get();
317 test_observer_.reset(new TestObserver);
318 cras_audio_handler_->AddAudioObserver(test_observer_.get());
319 message_loop_.RunUntilIdle();
322 void SetUpCrasAudioHandlerWithPrimaryActiveNode(
323 const AudioNodeList& audio_nodes,
324 const AudioNode& primary_active_node) {
325 DBusThreadManager::Initialize();
326 fake_cras_audio_client_ = static_cast<FakeCrasAudioClient*>(
327 DBusThreadManager::Get()->GetCrasAudioClient());
328 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes);
329 fake_cras_audio_client_->SetActiveOutputNode(primary_active_node.id),
330 audio_pref_handler_ = new AudioDevicesPrefHandlerStub();
331 CrasAudioHandler::Initialize(audio_pref_handler_);
332 cras_audio_handler_ = CrasAudioHandler::Get();
333 test_observer_.reset(new TestObserver);
334 cras_audio_handler_->AddAudioObserver(test_observer_.get());
335 message_loop_.RunUntilIdle();
338 void ChangeAudioNodes(const AudioNodeList& audio_nodes) {
339 fake_cras_audio_client_->SetAudioNodesAndNotifyObserversForTesting(
340 audio_nodes);
341 message_loop_.RunUntilIdle();
344 const AudioDevice* GetDeviceFromId(uint64 id) {
345 return cras_audio_handler_->GetDeviceFromId(id);
348 int GetActiveDeviceCount() const {
349 int num_active_nodes = 0;
350 AudioDeviceList audio_devices;
351 cras_audio_handler_->GetAudioDevices(&audio_devices);
352 for (size_t i = 0; i < audio_devices.size(); ++i) {
353 if (audio_devices[i].active)
354 ++num_active_nodes;
356 return num_active_nodes;
359 void SetActiveHDMIRediscover() {
360 cras_audio_handler_->SetActiveHDMIOutoutRediscoveringIfNecessary(true);
363 void SetHDMIRediscoverGracePeriodDuration(int duration_in_ms) {
364 cras_audio_handler_->SetHDMIRediscoverGracePeriodForTesting(duration_in_ms);
367 bool IsDuringHDMIRediscoverGracePeriod() {
368 return cras_audio_handler_->hdmi_rediscovering();
371 protected:
372 base::MessageLoopForUI message_loop_;
373 CrasAudioHandler* cras_audio_handler_; // Not owned.
374 FakeCrasAudioClient* fake_cras_audio_client_; // Not owned.
375 scoped_ptr<TestObserver> test_observer_;
376 scoped_refptr<AudioDevicesPrefHandlerStub> audio_pref_handler_;
378 private:
379 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandlerTest);
382 class HDMIRediscoverWaiter {
383 public:
384 HDMIRediscoverWaiter(CrasAudioHandlerTest* cras_audio_handler_test,
385 int grace_period_duration_in_ms)
386 : cras_audio_handler_test_(cras_audio_handler_test),
387 grace_period_duration_in_ms_(grace_period_duration_in_ms) {}
389 void WaitUntilTimeOut(int wait_duration_in_ms) {
390 base::RunLoop run_loop;
391 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
392 FROM_HERE, run_loop.QuitClosure(),
393 base::TimeDelta::FromMilliseconds(wait_duration_in_ms));
394 run_loop.Run();
397 void CheckHDMIRediscoverGracePeriodEnd(const base::Closure& quit_loop_func) {
398 if (!cras_audio_handler_test_->IsDuringHDMIRediscoverGracePeriod()) {
399 quit_loop_func.Run();
400 return;
402 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
403 FROM_HERE,
404 base::Bind(&HDMIRediscoverWaiter::CheckHDMIRediscoverGracePeriodEnd,
405 base::Unretained(this), quit_loop_func),
406 base::TimeDelta::FromMilliseconds(grace_period_duration_in_ms_ / 4));
409 void WaitUntilHDMIRediscoverGracePeriodEnd() {
410 base::RunLoop run_loop;
411 CheckHDMIRediscoverGracePeriodEnd(run_loop.QuitClosure());
412 run_loop.Run();
415 private:
416 CrasAudioHandlerTest* cras_audio_handler_test_; // not owned
417 int grace_period_duration_in_ms_;
419 DISALLOW_COPY_AND_ASSIGN(HDMIRediscoverWaiter);
422 TEST_F(CrasAudioHandlerTest, InitializeWithOnlyDefaultAudioDevices) {
423 AudioNodeList audio_nodes;
424 audio_nodes.push_back(kInternalSpeaker);
425 audio_nodes.push_back(kInternalMic);
426 SetUpCrasAudioHandler(audio_nodes);
428 // Verify the audio devices size.
429 AudioDeviceList audio_devices;
430 cras_audio_handler_->GetAudioDevices(&audio_devices);
431 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
433 // Verify the internal speaker has been selected as the active output.
434 AudioDevice active_output;
435 EXPECT_TRUE(
436 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
437 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
438 EXPECT_EQ(kInternalSpeaker.id,
439 cras_audio_handler_->GetPrimaryActiveOutputNode());
440 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
442 // Ensure the internal microphone has been selected as the active input.
443 AudioDevice active_input;
444 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
445 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
448 TEST_F(CrasAudioHandlerTest, InitializeWithAlternativeAudioDevices) {
449 AudioNodeList audio_nodes;
450 audio_nodes.push_back(kInternalSpeaker);
451 audio_nodes.push_back(kHeadphone);
452 audio_nodes.push_back(kInternalMic);
453 audio_nodes.push_back(kUSBMic);
454 SetUpCrasAudioHandler(audio_nodes);
456 // Verify the audio devices size.
457 AudioDeviceList audio_devices;
458 cras_audio_handler_->GetAudioDevices(&audio_devices);
459 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
461 // Verify the headphone has been selected as the active output.
462 AudioDevice active_output;
463 EXPECT_TRUE(
464 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
465 EXPECT_EQ(kHeadphone.id, active_output.id);
466 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
467 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
469 // Ensure the USB microphone has been selected as the active input.
470 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
471 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
474 TEST_F(CrasAudioHandlerTest, InitializeWithKeyboardMic) {
475 AudioNodeList audio_nodes;
476 audio_nodes.push_back(kInternalSpeaker);
477 audio_nodes.push_back(kInternalMic);
478 audio_nodes.push_back(kKeyboardMic);
479 SetUpCrasAudioHandler(audio_nodes);
481 // Verify the audio devices size.
482 AudioDeviceList audio_devices;
483 cras_audio_handler_->GetAudioDevices(&audio_devices);
484 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
485 EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic());
487 // Verify the internal speaker has been selected as the active output.
488 AudioDevice active_output;
489 EXPECT_TRUE(
490 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
491 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
492 EXPECT_EQ(kInternalSpeaker.id,
493 cras_audio_handler_->GetPrimaryActiveOutputNode());
494 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
496 // Ensure the internal microphone has been selected as the active input,
497 // not affected by keyboard mic.
498 AudioDevice active_input;
499 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
500 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
501 const AudioDevice* keyboard_mic = GetDeviceFromId(kKeyboardMic.id);
502 EXPECT_FALSE(keyboard_mic->active);
505 TEST_F(CrasAudioHandlerTest, SetKeyboardMicActive) {
506 AudioNodeList audio_nodes;
507 audio_nodes.push_back(kInternalMic);
508 audio_nodes.push_back(kKeyboardMic);
509 SetUpCrasAudioHandler(audio_nodes);
511 // Verify the audio devices size.
512 AudioDeviceList audio_devices;
513 cras_audio_handler_->GetAudioDevices(&audio_devices);
514 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
515 EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic());
517 // Ensure the internal microphone has been selected as the active input,
518 // not affected by keyboard mic.
519 AudioDevice active_input;
520 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
521 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
522 const AudioDevice* keyboard_mic = GetDeviceFromId(kKeyboardMic.id);
523 EXPECT_FALSE(keyboard_mic->active);
525 // Make keyboard mic active.
526 cras_audio_handler_->SetKeyboardMicActive(true);
527 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
528 const AudioDevice* active_keyboard_mic = GetDeviceFromId(kKeyboardMic.id);
529 EXPECT_TRUE(active_keyboard_mic->active);
531 // Make keyboard mic inactive.
532 cras_audio_handler_->SetKeyboardMicActive(false);
533 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
534 const AudioDevice* inactive_keyboard_mic = GetDeviceFromId(kKeyboardMic.id);
535 EXPECT_FALSE(inactive_keyboard_mic->active);
538 TEST_F(CrasAudioHandlerTest, SwitchActiveOutputDevice) {
539 AudioNodeList audio_nodes;
540 audio_nodes.push_back(kInternalSpeaker);
541 audio_nodes.push_back(kHeadphone);
542 SetUpCrasAudioHandler(audio_nodes);
543 AudioDeviceList audio_devices;
544 cras_audio_handler_->GetAudioDevices(&audio_devices);
545 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
547 // Verify the initial active output device is headphone.
548 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
549 AudioDevice active_output;
550 EXPECT_TRUE(
551 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
552 EXPECT_EQ(kHeadphone.id, active_output.id);
553 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
555 // Switch the active output to internal speaker.
556 AudioDevice internal_speaker(kInternalSpeaker);
557 cras_audio_handler_->SwitchToDevice(internal_speaker, true);
559 // Verify the active output is switched to internal speaker, and the
560 // ActiveOutputNodeChanged event is fired.
561 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
562 EXPECT_TRUE(
563 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
564 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
565 EXPECT_EQ(kInternalSpeaker.id,
566 cras_audio_handler_->GetPrimaryActiveOutputNode());
569 TEST_F(CrasAudioHandlerTest, SwitchActiveInputDevice) {
570 AudioNodeList audio_nodes;
571 audio_nodes.push_back(kInternalMic);
572 audio_nodes.push_back(kUSBMic);
573 SetUpCrasAudioHandler(audio_nodes);
574 AudioDeviceList audio_devices;
575 cras_audio_handler_->GetAudioDevices(&audio_devices);
576 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
578 // Verify the initial active input device is USB mic.
579 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
580 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
582 // Switch the active input to internal mic.
583 AudioDevice internal_mic(kInternalMic);
584 cras_audio_handler_->SwitchToDevice(internal_mic, true);
586 // Verify the active output is switched to internal speaker, and the active
587 // ActiveInputNodeChanged event is fired.
588 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
589 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
592 TEST_F(CrasAudioHandlerTest, PlugHeadphone) {
593 // Set up initial audio devices, only with internal speaker.
594 AudioNodeList audio_nodes;
595 audio_nodes.push_back(kInternalSpeaker);
596 SetUpCrasAudioHandler(audio_nodes);
597 const size_t init_nodes_size = audio_nodes.size();
599 // Verify the audio devices size.
600 AudioDeviceList audio_devices;
601 cras_audio_handler_->GetAudioDevices(&audio_devices);
602 EXPECT_EQ(init_nodes_size, audio_devices.size());
603 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
605 // Verify the internal speaker has been selected as the active output.
606 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
607 AudioDevice active_output;
608 EXPECT_TRUE(
609 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
610 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
611 EXPECT_EQ(kInternalSpeaker.id,
612 cras_audio_handler_->GetPrimaryActiveOutputNode());
613 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
615 // Plug the headphone.
616 audio_nodes.clear();
617 AudioNode internal_speaker(kInternalSpeaker);
618 internal_speaker.active = true;
619 audio_nodes.push_back(internal_speaker);
620 audio_nodes.push_back(kHeadphone);
621 ChangeAudioNodes(audio_nodes);
623 // Verify the AudioNodesChanged event is fired and new audio device is added.
624 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
625 cras_audio_handler_->GetAudioDevices(&audio_devices);
626 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
628 // Verify the active output device is switched to headphone and
629 // ActiveOutputChanged event is fired.
630 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
631 EXPECT_TRUE(
632 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
633 EXPECT_EQ(kHeadphone.id, active_output.id);
634 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
635 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
638 TEST_F(CrasAudioHandlerTest, UnplugHeadphone) {
639 // Set up initial audio devices, with internal speaker and headphone.
640 AudioNodeList audio_nodes;
641 audio_nodes.push_back(kInternalSpeaker);
642 audio_nodes.push_back(kHeadphone);
643 SetUpCrasAudioHandler(audio_nodes);
644 const size_t init_nodes_size = audio_nodes.size();
646 // Verify the audio devices size.
647 AudioDeviceList audio_devices;
648 cras_audio_handler_->GetAudioDevices(&audio_devices);
649 EXPECT_EQ(init_nodes_size, audio_devices.size());
650 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
652 // Verify the headphone has been selected as the active output.
653 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
654 AudioDevice active_output;
655 EXPECT_TRUE(
656 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
657 EXPECT_EQ(kHeadphone.id, active_output.id);
658 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
659 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
661 // Unplug the headphone.
662 audio_nodes.clear();
663 audio_nodes.push_back(kInternalSpeaker);
664 ChangeAudioNodes(audio_nodes);
666 // Verify the AudioNodesChanged event is fired and one audio device is
667 // removed.
668 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
669 cras_audio_handler_->GetAudioDevices(&audio_devices);
670 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
672 // Verify the active output device is switched to internal speaker and
673 // ActiveOutputChanged event is fired.
674 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
675 EXPECT_TRUE(
676 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
677 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
678 EXPECT_EQ(kInternalSpeaker.id,
679 cras_audio_handler_->GetPrimaryActiveOutputNode());
680 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
683 TEST_F(CrasAudioHandlerTest, InitializeWithBluetoothHeadset) {
684 AudioNodeList audio_nodes;
685 audio_nodes.push_back(kInternalSpeaker);
686 audio_nodes.push_back(kBluetoothHeadset);
687 SetUpCrasAudioHandler(audio_nodes);
689 // Verify the audio devices size.
690 AudioDeviceList audio_devices;
691 cras_audio_handler_->GetAudioDevices(&audio_devices);
692 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
693 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
695 // Verify the bluetooth headset has been selected as the active output.
696 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
697 AudioDevice active_output;
698 EXPECT_TRUE(
699 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
700 EXPECT_EQ(kBluetoothHeadset.id, active_output.id);
701 EXPECT_EQ(kBluetoothHeadset.id,
702 cras_audio_handler_->GetPrimaryActiveOutputNode());
703 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
706 TEST_F(CrasAudioHandlerTest, ConnectAndDisconnectBluetoothHeadset) {
707 // Initialize with internal speaker and headphone.
708 AudioNodeList audio_nodes;
709 audio_nodes.push_back(kInternalSpeaker);
710 audio_nodes.push_back(kHeadphone);
711 SetUpCrasAudioHandler(audio_nodes);
712 const size_t init_nodes_size = audio_nodes.size();
714 // Verify the audio devices size.
715 AudioDeviceList audio_devices;
716 cras_audio_handler_->GetAudioDevices(&audio_devices);
717 EXPECT_EQ(init_nodes_size, audio_devices.size());
718 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
720 // Verify the headphone is selected as the active output initially.
721 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
722 AudioDevice active_output;
723 EXPECT_TRUE(
724 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
725 EXPECT_EQ(kHeadphone.id, active_output.id);
726 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
727 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
729 // Connect to bluetooth headset. Since it is plugged in later than
730 // headphone, active output should be switched to it.
731 audio_nodes.clear();
732 audio_nodes.push_back(kInternalSpeaker);
733 AudioNode headphone(kHeadphone);
734 headphone.plugged_time = 80000000;
735 headphone.active = true;
736 audio_nodes.push_back(headphone);
737 AudioNode bluetooth_headset(kBluetoothHeadset);
738 bluetooth_headset.plugged_time = 90000000;
739 audio_nodes.push_back(bluetooth_headset);
740 ChangeAudioNodes(audio_nodes);
742 // Verify the AudioNodesChanged event is fired and new audio device is added.
743 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
744 cras_audio_handler_->GetAudioDevices(&audio_devices);
745 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
747 // Verify the active output device is switched to bluetooth headset, and
748 // ActiveOutputChanged event is fired.
749 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
750 EXPECT_TRUE(
751 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
752 EXPECT_EQ(kBluetoothHeadset.id, active_output.id);
753 EXPECT_EQ(kBluetoothHeadset.id,
754 cras_audio_handler_->GetPrimaryActiveOutputNode());
755 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
757 // Disconnect bluetooth headset.
758 audio_nodes.clear();
759 audio_nodes.push_back(kInternalSpeaker);
760 headphone.active = false;
761 audio_nodes.push_back(headphone);
762 ChangeAudioNodes(audio_nodes);
764 // Verify the AudioNodesChanged event is fired and one audio device is
765 // removed.
766 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
767 cras_audio_handler_->GetAudioDevices(&audio_devices);
768 EXPECT_EQ(init_nodes_size, audio_devices.size());
770 // Verify the active output device is switched to headphone, and
771 // ActiveOutputChanged event is fired.
772 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
773 EXPECT_TRUE(
774 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
775 EXPECT_EQ(kHeadphone.id, active_output.id);
776 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
777 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
780 TEST_F(CrasAudioHandlerTest, InitializeWithHDMIOutput) {
781 AudioNodeList audio_nodes;
782 audio_nodes.push_back(kInternalSpeaker);
783 audio_nodes.push_back(kHDMIOutput);
784 SetUpCrasAudioHandler(audio_nodes);
786 // Verify the audio devices size.
787 AudioDeviceList audio_devices;
788 cras_audio_handler_->GetAudioDevices(&audio_devices);
789 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
790 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
792 // Verify the HDMI device has been selected as the active output.
793 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
794 AudioDevice active_output;
795 EXPECT_TRUE(
796 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
797 EXPECT_EQ(kHDMIOutput.id, active_output.id);
798 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
799 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
802 TEST_F(CrasAudioHandlerTest, ConnectAndDisconnectHDMIOutput) {
803 // Initialize with internal speaker.
804 AudioNodeList audio_nodes;
805 audio_nodes.push_back(kInternalSpeaker);
806 SetUpCrasAudioHandler(audio_nodes);
807 const size_t init_nodes_size = audio_nodes.size();
809 // Verify the audio devices size.
810 AudioDeviceList audio_devices;
811 cras_audio_handler_->GetAudioDevices(&audio_devices);
812 EXPECT_EQ(init_nodes_size, audio_devices.size());
813 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
815 // Verify the internal speaker is selected as the active output initially.
816 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
817 AudioDevice active_output;
818 EXPECT_TRUE(
819 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
820 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
821 EXPECT_EQ(kInternalSpeaker.id,
822 cras_audio_handler_->GetPrimaryActiveOutputNode());
823 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
825 // Connect to HDMI output.
826 audio_nodes.clear();
827 AudioNode internal_speaker(kInternalSpeaker);
828 internal_speaker.active = true;
829 internal_speaker.plugged_time = 80000000;
830 audio_nodes.push_back(internal_speaker);
831 AudioNode hdmi(kHDMIOutput);
832 hdmi.plugged_time = 90000000;
833 audio_nodes.push_back(hdmi);
834 ChangeAudioNodes(audio_nodes);
836 // Verify the AudioNodesChanged event is fired and new audio device is added.
837 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
838 cras_audio_handler_->GetAudioDevices(&audio_devices);
839 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
841 // Verify the active output device is switched to hdmi output, and
842 // ActiveOutputChanged event is fired.
843 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
844 EXPECT_TRUE(
845 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
846 EXPECT_EQ(kHDMIOutput.id, active_output.id);
847 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
848 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
850 // Disconnect hdmi headset.
851 audio_nodes.clear();
852 audio_nodes.push_back(kInternalSpeaker);
853 ChangeAudioNodes(audio_nodes);
855 // Verify the AudioNodesChanged event is fired and one audio device is
856 // removed.
857 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
858 cras_audio_handler_->GetAudioDevices(&audio_devices);
859 EXPECT_EQ(init_nodes_size, audio_devices.size());
861 // Verify the active output device is switched to internal speaker, and
862 // ActiveOutputChanged event is fired.
863 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
864 EXPECT_TRUE(
865 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
866 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
867 EXPECT_EQ(kInternalSpeaker.id,
868 cras_audio_handler_->GetPrimaryActiveOutputNode());
869 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
872 TEST_F(CrasAudioHandlerTest, HandleHeadphoneAndHDMIOutput) {
873 // Initialize with internal speaker, headphone and HDMI output.
874 AudioNodeList audio_nodes;
875 audio_nodes.push_back(kInternalSpeaker);
876 audio_nodes.push_back(kHeadphone);
877 audio_nodes.push_back(kHDMIOutput);
878 SetUpCrasAudioHandler(audio_nodes);
879 const size_t init_nodes_size = audio_nodes.size();
881 // Verify the audio devices size.
882 AudioDeviceList audio_devices;
883 cras_audio_handler_->GetAudioDevices(&audio_devices);
884 EXPECT_EQ(init_nodes_size, audio_devices.size());
885 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
887 // Verify the headphone is selected as the active output initially.
888 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
889 AudioDevice active_output;
890 EXPECT_TRUE(
891 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
892 EXPECT_EQ(kHeadphone.id, active_output.id);
893 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
894 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
896 // Disconnect HDMI output.
897 audio_nodes.clear();
898 audio_nodes.push_back(kInternalSpeaker);
899 audio_nodes.push_back(kHDMIOutput);
900 ChangeAudioNodes(audio_nodes);
902 // Verify the AudioNodesChanged event is fired and one audio device is
903 // removed.
904 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
905 cras_audio_handler_->GetAudioDevices(&audio_devices);
906 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
908 // Verify the active output device is switched to HDMI output, and
909 // ActiveOutputChanged event is fired.
910 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
911 EXPECT_TRUE(
912 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
913 EXPECT_EQ(kHDMIOutput.id, active_output.id);
914 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
915 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
918 TEST_F(CrasAudioHandlerTest, InitializeWithUSBHeadphone) {
919 AudioNodeList audio_nodes;
920 audio_nodes.push_back(kInternalSpeaker);
921 audio_nodes.push_back(kUSBHeadphone1);
922 SetUpCrasAudioHandler(audio_nodes);
924 // Verify the audio devices size.
925 AudioDeviceList audio_devices;
926 cras_audio_handler_->GetAudioDevices(&audio_devices);
927 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
928 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
930 // Verify the usb headphone has been selected as the active output.
931 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
932 AudioDevice active_output;
933 EXPECT_TRUE(
934 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
935 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
936 EXPECT_EQ(kUSBHeadphone1.id,
937 cras_audio_handler_->GetPrimaryActiveOutputNode());
938 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
941 TEST_F(CrasAudioHandlerTest, PlugAndUnplugUSBHeadphone) {
942 // Initialize with internal speaker.
943 AudioNodeList audio_nodes;
944 audio_nodes.push_back(kInternalSpeaker);
945 SetUpCrasAudioHandler(audio_nodes);
946 const size_t init_nodes_size = audio_nodes.size();
948 // Verify the audio devices size.
949 AudioDeviceList audio_devices;
950 cras_audio_handler_->GetAudioDevices(&audio_devices);
951 EXPECT_EQ(init_nodes_size, audio_devices.size());
952 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
954 // Verify the internal speaker is selected as the active output initially.
955 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
956 AudioDevice active_output;
957 EXPECT_TRUE(
958 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
959 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
960 EXPECT_EQ(kInternalSpeaker.id,
961 cras_audio_handler_->GetPrimaryActiveOutputNode());
962 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
964 // Plug in usb headphone
965 audio_nodes.clear();
966 AudioNode internal_speaker(kInternalSpeaker);
967 internal_speaker.active = true;
968 internal_speaker.plugged_time = 80000000;
969 audio_nodes.push_back(internal_speaker);
970 AudioNode usb_headphone(kUSBHeadphone1);
971 usb_headphone.plugged_time = 90000000;
972 audio_nodes.push_back(usb_headphone);
973 ChangeAudioNodes(audio_nodes);
975 // Verify the AudioNodesChanged event is fired and new audio device is added.
976 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
977 cras_audio_handler_->GetAudioDevices(&audio_devices);
978 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
980 // Verify the active output device is switched to usb headphone, and
981 // ActiveOutputChanged event is fired.
982 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
983 EXPECT_TRUE(
984 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
985 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
986 EXPECT_EQ(kUSBHeadphone1.id,
987 cras_audio_handler_->GetPrimaryActiveOutputNode());
988 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
990 // Unplug usb headphone.
991 audio_nodes.clear();
992 audio_nodes.push_back(kInternalSpeaker);
993 ChangeAudioNodes(audio_nodes);
995 // Verify the AudioNodesChanged event is fired and one audio device is
996 // removed.
997 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
998 cras_audio_handler_->GetAudioDevices(&audio_devices);
999 EXPECT_EQ(init_nodes_size, audio_devices.size());
1001 // Verify the active output device is switched to internal speaker, and
1002 // ActiveOutputChanged event is fired.
1003 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
1004 EXPECT_TRUE(
1005 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1006 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
1007 EXPECT_EQ(kInternalSpeaker.id,
1008 cras_audio_handler_->GetPrimaryActiveOutputNode());
1009 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
1012 TEST_F(CrasAudioHandlerTest, HandleMultipleUSBHeadphones) {
1013 // Initialize with internal speaker and one usb headphone.
1014 AudioNodeList audio_nodes;
1015 audio_nodes.push_back(kInternalSpeaker);
1016 audio_nodes.push_back(kUSBHeadphone1);
1017 SetUpCrasAudioHandler(audio_nodes);
1018 const size_t init_nodes_size = audio_nodes.size();
1020 // Verify the audio devices size.
1021 AudioDeviceList audio_devices;
1022 cras_audio_handler_->GetAudioDevices(&audio_devices);
1023 EXPECT_EQ(init_nodes_size, audio_devices.size());
1024 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1026 // Verify the usb headphone is selected as the active output initially.
1027 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1028 AudioDevice active_output;
1029 EXPECT_TRUE(
1030 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1031 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
1032 EXPECT_EQ(kUSBHeadphone1.id,
1033 cras_audio_handler_->GetPrimaryActiveOutputNode());
1034 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1036 // Plug in another usb headphone.
1037 audio_nodes.clear();
1038 audio_nodes.push_back(kInternalSpeaker);
1039 AudioNode usb_headphone_1(kUSBHeadphone1);
1040 usb_headphone_1.active = true;
1041 usb_headphone_1.plugged_time = 80000000;
1042 audio_nodes.push_back(usb_headphone_1);
1043 AudioNode usb_headphone_2(kUSBHeadphone2);
1044 usb_headphone_2.plugged_time = 90000000;
1045 audio_nodes.push_back(usb_headphone_2);
1046 ChangeAudioNodes(audio_nodes);
1048 // Verify the AudioNodesChanged event is fired and new audio device is added.
1049 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1050 cras_audio_handler_->GetAudioDevices(&audio_devices);
1051 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1053 // Verify the active output device is switched to the 2nd usb headphone, which
1054 // is plugged later, and ActiveOutputChanged event is fired.
1055 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1056 EXPECT_TRUE(
1057 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1058 EXPECT_EQ(kUSBHeadphone2.id, active_output.id);
1059 EXPECT_EQ(kUSBHeadphone2.id,
1060 cras_audio_handler_->GetPrimaryActiveOutputNode());
1061 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1063 // Unplug the 2nd usb headphone.
1064 audio_nodes.clear();
1065 audio_nodes.push_back(kInternalSpeaker);
1066 audio_nodes.push_back(kUSBHeadphone1);
1067 ChangeAudioNodes(audio_nodes);
1069 // Verify the AudioNodesChanged event is fired and one audio device is
1070 // removed.
1071 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1072 cras_audio_handler_->GetAudioDevices(&audio_devices);
1073 EXPECT_EQ(init_nodes_size, audio_devices.size());
1075 // Verify the active output device is switched to the first usb headphone, and
1076 // ActiveOutputChanged event is fired.
1077 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
1078 EXPECT_TRUE(
1079 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1080 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
1081 EXPECT_EQ(kUSBHeadphone1.id,
1082 cras_audio_handler_->GetPrimaryActiveOutputNode());
1083 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1086 TEST_F(CrasAudioHandlerTest, UnplugUSBHeadphonesWithActiveSpeaker) {
1087 // Initialize with internal speaker and one usb headphone.
1088 AudioNodeList audio_nodes;
1089 audio_nodes.push_back(kInternalSpeaker);
1090 audio_nodes.push_back(kUSBHeadphone1);
1091 SetUpCrasAudioHandler(audio_nodes);
1092 const size_t init_nodes_size = audio_nodes.size();
1094 // Verify the audio devices size.
1095 AudioDeviceList audio_devices;
1096 cras_audio_handler_->GetAudioDevices(&audio_devices);
1097 EXPECT_EQ(init_nodes_size, audio_devices.size());
1098 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1100 // Verify the usb headphone is selected as the active output initially.
1101 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1102 AudioDevice active_output;
1103 EXPECT_TRUE(
1104 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1105 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
1106 EXPECT_EQ(kUSBHeadphone1.id,
1107 cras_audio_handler_->GetPrimaryActiveOutputNode());
1108 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1110 // Plug in the headphone jack.
1111 audio_nodes.clear();
1112 audio_nodes.push_back(kInternalSpeaker);
1113 AudioNode usb_headphone_1(kUSBHeadphone1);
1114 usb_headphone_1.active = true;
1115 usb_headphone_1.plugged_time = 80000000;
1116 audio_nodes.push_back(usb_headphone_1);
1117 AudioNode headphone_jack(kHeadphone);
1118 headphone_jack.plugged_time = 90000000;
1119 audio_nodes.push_back(headphone_jack);
1120 ChangeAudioNodes(audio_nodes);
1122 // Verify the AudioNodesChanged event is fired and new audio device is added.
1123 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1124 cras_audio_handler_->GetAudioDevices(&audio_devices);
1125 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1127 // Verify the active output device is switched to the headphone jack, which
1128 // is plugged later, and ActiveOutputChanged event is fired.
1129 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1130 EXPECT_TRUE(
1131 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1132 EXPECT_EQ(kHeadphone.id, active_output.id);
1133 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1134 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1136 // Select the speaker to be the active output device.
1137 AudioDevice internal_speaker(kInternalSpeaker);
1138 cras_audio_handler_->SwitchToDevice(internal_speaker, true);
1140 // Verify the active output is switched to internal speaker, and the
1141 // ActiveOutputNodeChanged event is fired.
1142 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
1143 EXPECT_TRUE(
1144 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1145 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
1146 EXPECT_EQ(kInternalSpeaker.id,
1147 cras_audio_handler_->GetPrimaryActiveOutputNode());
1149 // Unplug the usb headphone.
1150 audio_nodes.clear();
1151 AudioNode internal_speaker_node(kInternalSpeaker);
1152 internal_speaker_node.active = true;
1153 internal_speaker_node.plugged_time = 70000000;
1154 audio_nodes.push_back(internal_speaker_node);
1155 headphone_jack.active = false;
1156 audio_nodes.push_back(headphone_jack);
1157 ChangeAudioNodes(audio_nodes);
1159 // Verify the AudioNodesChanged event is fired and one audio device is
1160 // removed.
1161 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1162 cras_audio_handler_->GetAudioDevices(&audio_devices);
1163 EXPECT_EQ(init_nodes_size, audio_devices.size());
1165 // Verify the active output device remains to be speaker.
1166 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
1167 EXPECT_TRUE(
1168 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1169 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
1170 EXPECT_EQ(kInternalSpeaker.id,
1171 cras_audio_handler_->GetPrimaryActiveOutputNode());
1172 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1175 TEST_F(CrasAudioHandlerTest, OneActiveAudioOutputAfterLoginNewUserSession) {
1176 // This tests the case found with crbug.com/273271.
1177 // Initialize with internal speaker, bluetooth headphone and headphone jack
1178 // for a new chrome session after user signs out from the previous session.
1179 // Headphone jack is plugged in later than bluetooth headphone, but bluetooth
1180 // headphone is selected as the active output by user from previous user
1181 // session.
1182 AudioNodeList audio_nodes;
1183 audio_nodes.push_back(kInternalSpeaker);
1184 AudioNode bluetooth_headphone(kBluetoothHeadset);
1185 bluetooth_headphone.active = true;
1186 bluetooth_headphone.plugged_time = 70000000;
1187 audio_nodes.push_back(bluetooth_headphone);
1188 AudioNode headphone_jack(kHeadphone);
1189 headphone_jack.plugged_time = 80000000;
1190 audio_nodes.push_back(headphone_jack);
1191 SetUpCrasAudioHandlerWithPrimaryActiveNode(audio_nodes, bluetooth_headphone);
1192 const size_t init_nodes_size = audio_nodes.size();
1194 // Verify the audio devices size.
1195 AudioDeviceList audio_devices;
1196 cras_audio_handler_->GetAudioDevices(&audio_devices);
1197 EXPECT_EQ(init_nodes_size, audio_devices.size());
1198 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1200 // Verify the headphone jack is selected as the active output and all other
1201 // audio devices are not active.
1202 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1203 AudioDevice active_output;
1204 EXPECT_TRUE(
1205 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1206 EXPECT_EQ(kHeadphone.id, active_output.id);
1207 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1208 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1209 for (size_t i = 0; i < audio_devices.size(); ++i) {
1210 if (audio_devices[i].id != kHeadphone.id)
1211 EXPECT_FALSE(audio_devices[i].active);
1215 TEST_F(CrasAudioHandlerTest, BluetoothSpeakerIdChangedOnFly) {
1216 // Initialize with internal speaker and bluetooth headset.
1217 AudioNodeList audio_nodes;
1218 audio_nodes.push_back(kInternalSpeaker);
1219 audio_nodes.push_back(kBluetoothHeadset);
1220 SetUpCrasAudioHandler(audio_nodes);
1221 const size_t init_nodes_size = audio_nodes.size();
1223 // Verify the audio devices size.
1224 AudioDeviceList audio_devices;
1225 cras_audio_handler_->GetAudioDevices(&audio_devices);
1226 EXPECT_EQ(init_nodes_size, audio_devices.size());
1227 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1229 // Verify the bluetooth headset is selected as the active output and all other
1230 // audio devices are not active.
1231 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1232 AudioDevice active_output;
1233 EXPECT_TRUE(
1234 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1235 EXPECT_EQ(kBluetoothHeadset.id, active_output.id);
1236 EXPECT_EQ(kBluetoothHeadset.id,
1237 cras_audio_handler_->GetPrimaryActiveOutputNode());
1238 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1240 // Cras changes the bluetooth headset's id on the fly.
1241 audio_nodes.clear();
1242 AudioNode internal_speaker(kInternalSpeaker);
1243 internal_speaker.active = false;
1244 audio_nodes.push_back(internal_speaker);
1245 AudioNode bluetooth_headphone(kBluetoothHeadset);
1246 // Change bluetooth headphone id.
1247 bluetooth_headphone.id = kBluetoothHeadsetId + 20000;
1248 bluetooth_headphone.active = false;
1249 audio_nodes.push_back(bluetooth_headphone);
1250 ChangeAudioNodes(audio_nodes);
1252 // Verify NodesChanged event is fired, and the audio devices size is not
1253 // changed.
1254 audio_devices.clear();
1255 cras_audio_handler_->GetAudioDevices(&audio_devices);
1256 EXPECT_EQ(init_nodes_size, audio_devices.size());
1257 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1259 // Verify ActiveOutputNodeChanged event is fired, and active device should be
1260 // bluetooth headphone.
1261 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1262 EXPECT_TRUE(
1263 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1264 EXPECT_EQ(bluetooth_headphone.id, active_output.id);
1267 TEST_F(CrasAudioHandlerTest, PlugUSBMic) {
1268 // Set up initial audio devices, only with internal mic.
1269 AudioNodeList audio_nodes;
1270 audio_nodes.push_back(kInternalMic);
1271 SetUpCrasAudioHandler(audio_nodes);
1272 const size_t init_nodes_size = audio_nodes.size();
1274 // Verify the audio devices size.
1275 AudioDeviceList audio_devices;
1276 cras_audio_handler_->GetAudioDevices(&audio_devices);
1277 EXPECT_EQ(init_nodes_size, audio_devices.size());
1278 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1280 // Verify the internal mic is selected as the active input.
1281 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1282 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1283 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1285 // Plug the USB Mic.
1286 audio_nodes.clear();
1287 AudioNode internal_mic(kInternalMic);
1288 internal_mic.active = true;
1289 audio_nodes.push_back(internal_mic);
1290 audio_nodes.push_back(kUSBMic);
1291 ChangeAudioNodes(audio_nodes);
1293 // Verify the AudioNodesChanged event is fired and new audio device is added.
1294 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1295 cras_audio_handler_->GetAudioDevices(&audio_devices);
1296 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1298 // Verify the active input device is switched to USB mic and
1299 // and ActiveInputChanged event is fired.
1300 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
1301 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
1302 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1305 TEST_F(CrasAudioHandlerTest, UnplugUSBMic) {
1306 // Set up initial audio devices, with internal mic and USB Mic.
1307 AudioNodeList audio_nodes;
1308 audio_nodes.push_back(kInternalMic);
1309 audio_nodes.push_back(kUSBMic);
1310 SetUpCrasAudioHandler(audio_nodes);
1311 const size_t init_nodes_size = audio_nodes.size();
1313 // Verify the audio devices size.
1314 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1315 AudioDeviceList audio_devices;
1316 cras_audio_handler_->GetAudioDevices(&audio_devices);
1317 EXPECT_EQ(init_nodes_size, audio_devices.size());
1319 // Verify the USB mic is selected as the active output.
1320 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1321 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
1322 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1324 // Unplug the USB Mic.
1325 audio_nodes.clear();
1326 audio_nodes.push_back(kInternalMic);
1327 ChangeAudioNodes(audio_nodes);
1329 // Verify the AudioNodesChanged event is fired, and one audio device is
1330 // removed.
1331 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1332 cras_audio_handler_->GetAudioDevices(&audio_devices);
1333 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
1335 // Verify the active input device is switched to internal mic, and
1336 // and ActiveInputChanged event is fired.
1337 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
1338 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1339 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1342 TEST_F(CrasAudioHandlerTest, PlugUSBMicNotAffectActiveOutput) {
1343 // Set up initial audio devices.
1344 AudioNodeList audio_nodes;
1345 audio_nodes.push_back(kInternalSpeaker);
1346 audio_nodes.push_back(kHeadphone);
1347 audio_nodes.push_back(kInternalMic);
1348 SetUpCrasAudioHandler(audio_nodes);
1349 const size_t init_nodes_size = audio_nodes.size();
1351 // Verify the audio devices size.
1352 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1353 AudioDeviceList audio_devices;
1354 cras_audio_handler_->GetAudioDevices(&audio_devices);
1355 EXPECT_EQ(init_nodes_size, audio_devices.size());
1357 // Verify the internal mic is selected as the active input.
1358 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1359 EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
1360 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1362 // Verify the headphone is selected as the active output.
1363 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1364 EXPECT_EQ(kHeadphoneId, cras_audio_handler_->GetPrimaryActiveOutputNode());
1365 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1367 // Switch the active output to internal speaker.
1368 AudioDevice internal_speaker(kInternalSpeaker);
1369 cras_audio_handler_->SwitchToDevice(internal_speaker, true);
1371 // Verify the active output is switched to internal speaker, and the
1372 // ActiveOutputNodeChanged event is fired.
1373 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1374 AudioDevice active_output;
1375 EXPECT_TRUE(
1376 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1377 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
1378 EXPECT_EQ(kInternalSpeaker.id,
1379 cras_audio_handler_->GetPrimaryActiveOutputNode());
1381 // Plug the USB Mic.
1382 audio_nodes.clear();
1383 AudioNode internal_speaker_node(kInternalSpeaker);
1384 internal_speaker_node.active = true;
1385 audio_nodes.push_back(internal_speaker_node);
1386 audio_nodes.push_back(kHeadphone);
1387 AudioNode internal_mic(kInternalMic);
1388 internal_mic.active = true;
1389 audio_nodes.push_back(internal_mic);
1390 audio_nodes.push_back(kUSBMic);
1391 ChangeAudioNodes(audio_nodes);
1393 // Verify the AudioNodesChanged event is fired, one new device is added.
1394 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1395 cras_audio_handler_->GetAudioDevices(&audio_devices);
1396 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1398 // Verify the active input device is switched to USB mic, and
1399 // and ActiveInputChanged event is fired.
1400 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
1401 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1402 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1404 // Verify the active output device is not changed.
1405 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1406 EXPECT_TRUE(
1407 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1408 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
1409 EXPECT_EQ(kInternalSpeaker.id,
1410 cras_audio_handler_->GetPrimaryActiveOutputNode());
1413 TEST_F(CrasAudioHandlerTest, PlugHeadphoneAutoUnplugSpeakerWithActiveUSB) {
1414 // Set up initial audio devices.
1415 AudioNodeList audio_nodes;
1416 audio_nodes.push_back(kUSBHeadphone1);
1417 audio_nodes.push_back(kInternalSpeaker);
1418 audio_nodes.push_back(kInternalMic);
1419 SetUpCrasAudioHandler(audio_nodes);
1420 const size_t init_nodes_size = audio_nodes.size();
1422 // Verify the audio devices size.
1423 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1424 AudioDeviceList audio_devices;
1425 cras_audio_handler_->GetAudioDevices(&audio_devices);
1426 EXPECT_EQ(init_nodes_size, audio_devices.size());
1428 // Verify the internal mic is selected as the active input.
1429 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1430 EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
1431 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1433 // Verify the USB headphone is selected as the active output.
1434 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1435 EXPECT_EQ(kUSBHeadphoneId1,
1436 cras_audio_handler_->GetPrimaryActiveOutputNode());
1437 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1439 // Plug the headphone and auto-unplug internal speaker.
1440 audio_nodes.clear();
1441 AudioNode usb_headphone_node(kUSBHeadphone1);
1442 usb_headphone_node.active = true;
1443 audio_nodes.push_back(usb_headphone_node);
1444 AudioNode headphone_node(kHeadphone);
1445 headphone_node.plugged_time = 1000;
1446 audio_nodes.push_back(headphone_node);
1447 AudioNode internal_mic(kInternalMic);
1448 internal_mic.active = true;
1449 audio_nodes.push_back(internal_mic);
1450 ChangeAudioNodes(audio_nodes);
1452 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1453 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1454 cras_audio_handler_->GetAudioDevices(&audio_devices);
1455 EXPECT_EQ(init_nodes_size, audio_devices.size());
1457 // Verify the active output device is switched to headphone, and
1458 // an ActiveOutputChanged event is fired.
1459 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1460 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1461 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1463 // Unplug the headphone and internal speaker auto-plugs back.
1464 audio_nodes.clear();
1465 audio_nodes.push_back(kUSBHeadphone1);
1466 AudioNode internal_speaker_node(kInternalSpeaker);
1467 internal_speaker_node.plugged_time = 2000;
1468 audio_nodes.push_back(internal_speaker_node);
1469 audio_nodes.push_back(internal_mic);
1470 ChangeAudioNodes(audio_nodes);
1472 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1473 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1474 cras_audio_handler_->GetAudioDevices(&audio_devices);
1475 EXPECT_EQ(init_nodes_size, audio_devices.size());
1477 // Verify the active output device is switched back to USB, and
1478 // an ActiveOutputChanged event is fired.
1479 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
1480 EXPECT_EQ(kUSBHeadphone1.id,
1481 cras_audio_handler_->GetPrimaryActiveOutputNode());
1482 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1484 // Verify the active input device is not changed.
1485 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1486 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1489 TEST_F(CrasAudioHandlerTest, PlugMicAutoUnplugInternalMicWithActiveUSB) {
1490 // Set up initial audio devices.
1491 AudioNodeList audio_nodes;
1492 audio_nodes.push_back(kUSBHeadphone1);
1493 audio_nodes.push_back(kInternalSpeaker);
1494 audio_nodes.push_back(kUSBMic);
1495 audio_nodes.push_back(kInternalMic);
1496 SetUpCrasAudioHandler(audio_nodes);
1497 const size_t init_nodes_size = audio_nodes.size();
1499 // Verify the audio devices size.
1500 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1501 AudioDeviceList audio_devices;
1502 cras_audio_handler_->GetAudioDevices(&audio_devices);
1503 EXPECT_EQ(init_nodes_size, audio_devices.size());
1505 // Verify the internal mic is selected as the active input.
1506 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1507 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
1508 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1510 // Verify the internal speaker is selected as the active output.
1511 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1512 EXPECT_EQ(kUSBHeadphoneId1,
1513 cras_audio_handler_->GetPrimaryActiveOutputNode());
1514 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1516 // Plug the headphone and mic, auto-unplug internal mic and speaker.
1517 audio_nodes.clear();
1518 AudioNode usb_headphone_node(kUSBHeadphone1);
1519 usb_headphone_node.active = true;
1520 audio_nodes.push_back(usb_headphone_node);
1521 AudioNode headphone_node(kHeadphone);
1522 headphone_node.plugged_time = 1000;
1523 audio_nodes.push_back(headphone_node);
1524 AudioNode usb_mic(kUSBMic);
1525 usb_mic.active = true;
1526 audio_nodes.push_back(usb_mic);
1527 AudioNode mic_jack(kMicJack);
1528 mic_jack.plugged_time = 1000;
1529 audio_nodes.push_back(mic_jack);
1530 ChangeAudioNodes(audio_nodes);
1532 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1533 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1534 cras_audio_handler_->GetAudioDevices(&audio_devices);
1535 EXPECT_EQ(init_nodes_size, audio_devices.size());
1537 // Verify the active output device is switched to headphone, and
1538 // an ActiveOutputChanged event is fired.
1539 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1540 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1541 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1543 // Verify the active input device is switched to mic jack, and
1544 // an ActiveInputChanged event is fired.
1545 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
1546 EXPECT_EQ(kMicJack.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1547 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1549 // Unplug the headphone and internal speaker auto-plugs back.
1550 audio_nodes.clear();
1551 audio_nodes.push_back(kUSBHeadphone1);
1552 AudioNode internal_speaker_node(kInternalSpeaker);
1553 internal_speaker_node.plugged_time = 2000;
1554 audio_nodes.push_back(internal_speaker_node);
1555 audio_nodes.push_back(kUSBMic);
1556 AudioNode internal_mic(kInternalMic);
1557 internal_mic.plugged_time = 2000;
1558 audio_nodes.push_back(internal_mic);
1559 ChangeAudioNodes(audio_nodes);
1561 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1562 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1563 cras_audio_handler_->GetAudioDevices(&audio_devices);
1564 EXPECT_EQ(init_nodes_size, audio_devices.size());
1566 // Verify the active output device is switched back to USB, and
1567 // an ActiveOutputChanged event is fired.
1568 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
1569 EXPECT_EQ(kUSBHeadphone1.id,
1570 cras_audio_handler_->GetPrimaryActiveOutputNode());
1571 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1573 // Verify the active input device is switched back to USB mic, and
1574 // an ActiveInputChanged event is fired.
1575 EXPECT_EQ(2, test_observer_->active_input_node_changed_count());
1576 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1577 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1580 TEST_F(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnPlugInHeadphone) {
1581 // Set up initial audio devices.
1582 AudioNodeList audio_nodes;
1583 audio_nodes.push_back(kInternalSpeaker);
1584 audio_nodes.push_back(kBluetoothHeadset);
1585 SetUpCrasAudioHandler(audio_nodes);
1586 const size_t init_nodes_size = audio_nodes.size();
1588 // Verify the audio devices size.
1589 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1590 AudioDeviceList audio_devices;
1591 cras_audio_handler_->GetAudioDevices(&audio_devices);
1592 EXPECT_EQ(init_nodes_size, audio_devices.size());
1594 // Verify the bluetooth headset is selected as the active output.
1595 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1596 EXPECT_EQ(kBluetoothHeadsetId,
1597 cras_audio_handler_->GetPrimaryActiveOutputNode());
1598 AudioDevice active_output;
1599 EXPECT_TRUE(
1600 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1601 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1603 // Plug in headphone, but fire NodesChanged signal twice.
1604 audio_nodes.clear();
1605 audio_nodes.push_back(kInternalSpeaker);
1606 AudioNode bluetooth_headset(kBluetoothHeadset);
1607 bluetooth_headset.plugged_time = 1000;
1608 bluetooth_headset.active = true;
1609 audio_nodes.push_back(bluetooth_headset);
1610 AudioNode headphone(kHeadphone);
1611 headphone.active = false;
1612 headphone.plugged_time = 2000;
1613 audio_nodes.push_back(headphone);
1614 ChangeAudioNodes(audio_nodes);
1615 ChangeAudioNodes(audio_nodes);
1617 // Verify the active output device is set to headphone.
1618 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1619 EXPECT_LE(1, test_observer_->active_output_node_changed_count());
1620 EXPECT_EQ(headphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1621 EXPECT_TRUE(
1622 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1623 EXPECT_EQ(headphone.id, active_output.id);
1625 // Verfiy the audio devices data is consistent, i.e., the active output device
1626 // should be headphone.
1627 cras_audio_handler_->GetAudioDevices(&audio_devices);
1628 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1629 for (size_t i = 0; i < audio_devices.size(); ++i) {
1630 if (audio_devices[i].id == kInternalSpeaker.id)
1631 EXPECT_FALSE(audio_devices[i].active);
1632 else if (audio_devices[i].id == bluetooth_headset.id)
1633 EXPECT_FALSE(audio_devices[i].active);
1634 else if (audio_devices[i].id == headphone.id)
1635 EXPECT_TRUE(audio_devices[i].active);
1636 else
1637 NOTREACHED();
1641 TEST_F(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnPlugInUSBMic) {
1642 // Set up initial audio devices.
1643 AudioNodeList audio_nodes;
1644 audio_nodes.push_back(kInternalMic);
1645 SetUpCrasAudioHandler(audio_nodes);
1646 const size_t init_nodes_size = audio_nodes.size();
1648 // Verify the audio devices size.
1649 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1650 AudioDeviceList audio_devices;
1651 cras_audio_handler_->GetAudioDevices(&audio_devices);
1652 EXPECT_EQ(init_nodes_size, audio_devices.size());
1654 // Verify the internal mic is selected as the active output.
1655 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1656 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1657 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
1658 EXPECT_TRUE(audio_devices[0].active);
1660 // Plug in usb mic, but fire NodesChanged signal twice.
1661 audio_nodes.clear();
1662 AudioNode internal_mic(kInternalMic);
1663 internal_mic.active = true;
1664 internal_mic.plugged_time = 1000;
1665 audio_nodes.push_back(internal_mic);
1666 AudioNode usb_mic(kUSBMic);
1667 usb_mic.active = false;
1668 usb_mic.plugged_time = 2000;
1669 audio_nodes.push_back(usb_mic);
1670 ChangeAudioNodes(audio_nodes);
1671 ChangeAudioNodes(audio_nodes);
1673 // Verify the active output device is set to headphone.
1674 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1675 EXPECT_LE(1, test_observer_->active_input_node_changed_count());
1676 EXPECT_EQ(usb_mic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1677 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1679 // Verfiy the audio devices data is consistent, i.e., the active input device
1680 // should be usb mic.
1681 cras_audio_handler_->GetAudioDevices(&audio_devices);
1682 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1683 for (size_t i = 0; i < audio_devices.size(); ++i) {
1684 if (audio_devices[i].id == kInternalMic.id)
1685 EXPECT_FALSE(audio_devices[i].active);
1686 else if (audio_devices[i].id == usb_mic.id)
1687 EXPECT_TRUE(audio_devices[i].active);
1688 else
1689 NOTREACHED();
1693 // This is the case of crbug.com/291303.
1694 TEST_F(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnSystemBoot) {
1695 // Set up audio handler with empty audio_nodes.
1696 AudioNodeList audio_nodes;
1697 SetUpCrasAudioHandler(audio_nodes);
1699 AudioNode internal_speaker(kInternalSpeaker);
1700 internal_speaker.active = false;
1701 AudioNode headphone(kHeadphone);
1702 headphone.active = false;
1703 AudioNode internal_mic(kInternalMic);
1704 internal_mic.active = false;
1705 audio_nodes.push_back(internal_speaker);
1706 audio_nodes.push_back(headphone);
1707 audio_nodes.push_back(internal_mic);
1708 const size_t init_nodes_size = audio_nodes.size();
1710 // Simulate AudioNodesChanged signal being fired twice during system boot.
1711 ChangeAudioNodes(audio_nodes);
1712 ChangeAudioNodes(audio_nodes);
1714 // Verify the active output device is set to headphone.
1715 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1716 EXPECT_LE(1, test_observer_->active_output_node_changed_count());
1717 EXPECT_EQ(headphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1718 AudioDevice active_output;
1719 EXPECT_TRUE(
1720 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1721 EXPECT_EQ(headphone.id, active_output.id);
1723 // Verify the active input device id is set to internal mic.
1724 EXPECT_EQ(internal_mic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1726 // Verfiy the audio devices data is consistent, i.e., the active output device
1727 // should be headphone, and the active input device should internal mic.
1728 AudioDeviceList audio_devices;
1729 cras_audio_handler_->GetAudioDevices(&audio_devices);
1730 EXPECT_EQ(init_nodes_size, audio_devices.size());
1731 for (size_t i = 0; i < audio_devices.size(); ++i) {
1732 if (audio_devices[i].id == internal_speaker.id)
1733 EXPECT_FALSE(audio_devices[i].active);
1734 else if (audio_devices[i].id == headphone.id)
1735 EXPECT_TRUE(audio_devices[i].active);
1736 else if (audio_devices[i].id == internal_mic.id)
1737 EXPECT_TRUE(audio_devices[i].active);
1738 else
1739 NOTREACHED();
1743 // This is the case of crbug.com/448924.
1744 TEST_F(CrasAudioHandlerTest,
1745 TwoNodesChangedSignalsForLosingTowNodesOnOneUnplug) {
1746 // Set up audio handler with 4 audio_nodes.
1747 AudioNodeList audio_nodes;
1748 AudioNode internal_speaker(kInternalSpeaker);
1749 internal_speaker.active = false;
1750 AudioNode headphone(kHeadphone);
1751 headphone.active = false;
1752 AudioNode internal_mic(kInternalMic);
1753 internal_mic.active = false;
1754 AudioNode micJack(kMicJack);
1755 micJack.active = false;
1756 audio_nodes.push_back(internal_speaker);
1757 audio_nodes.push_back(headphone);
1758 audio_nodes.push_back(internal_mic);
1759 audio_nodes.push_back(micJack);
1760 SetUpCrasAudioHandler(audio_nodes);
1762 // Verify the audio devices size.
1763 AudioDeviceList audio_devices;
1764 cras_audio_handler_->GetAudioDevices(&audio_devices);
1765 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
1767 // Verify the headphone has been selected as the active output.
1768 AudioDevice active_output;
1769 EXPECT_TRUE(
1770 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1771 EXPECT_EQ(kHeadphone.id, active_output.id);
1772 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1773 EXPECT_TRUE(active_output.active);
1774 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1776 // Verify the mic Jack has been selected as the active input.
1777 EXPECT_EQ(micJack.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1778 const AudioDevice* active_input = GetDeviceFromId(micJack.id);
1779 EXPECT_TRUE(active_input->active);
1780 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1782 // Simulate the nodes list in first NodesChanged signal, only headphone is
1783 // removed, other nodes remains the same.
1784 AudioNodeList changed_nodes_1;
1785 internal_speaker.active = false;
1786 changed_nodes_1.push_back(internal_speaker);
1787 internal_mic.active = false;
1788 changed_nodes_1.push_back(internal_mic);
1789 micJack.active = true;
1790 changed_nodes_1.push_back(micJack);
1792 // Simulate the nodes list in second NodesChanged signal, the micJac is
1793 // removed, but the internal_mic is inactive, which does not reflect the
1794 // active status set from the first NodesChanged signal since this was sent
1795 // before cras receives the SetActiveOutputNode from the first NodesChanged
1796 // handling.
1797 AudioNodeList changed_nodes_2;
1798 changed_nodes_2.push_back(internal_speaker);
1799 changed_nodes_2.push_back(internal_mic);
1801 // Simulate AudioNodesChanged signal being fired twice for unplug an audio
1802 // device with both input and output nodes on it.
1803 ChangeAudioNodes(changed_nodes_1);
1804 ChangeAudioNodes(changed_nodes_2);
1806 AudioDeviceList changed_devices;
1807 cras_audio_handler_->GetAudioDevices(&changed_devices);
1808 EXPECT_EQ(2u, changed_devices.size());
1810 // Verify the active output device is set to internal speaker.
1811 EXPECT_EQ(internal_speaker.id,
1812 cras_audio_handler_->GetPrimaryActiveOutputNode());
1813 EXPECT_TRUE(
1814 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1815 EXPECT_EQ(internal_speaker.id, active_output.id);
1816 EXPECT_TRUE(active_output.active);
1818 // Verify the active input device id is set to internal mic.
1819 EXPECT_EQ(internal_mic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1820 const AudioDevice* changed_active_input = GetDeviceFromId(internal_mic.id);
1821 EXPECT_TRUE(changed_active_input->active);
1824 TEST_F(CrasAudioHandlerTest, SetOutputMute) {
1825 AudioNodeList audio_nodes;
1826 audio_nodes.push_back(kInternalSpeaker);
1827 SetUpCrasAudioHandler(audio_nodes);
1828 EXPECT_EQ(0, test_observer_->output_mute_changed_count());
1830 // Mute the device.
1831 cras_audio_handler_->SetOutputMute(true);
1833 // Verify the output is muted, OnOutputMuteChanged event is fired,
1834 // and mute value is saved in the preferences.
1835 EXPECT_TRUE(cras_audio_handler_->IsOutputMuted());
1836 EXPECT_EQ(1, test_observer_->output_mute_changed_count());
1837 AudioDevice speaker(kInternalSpeaker);
1838 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(speaker));
1840 // Unmute the device.
1841 cras_audio_handler_->SetOutputMute(false);
1843 // Verify the output is unmuted, OnOutputMuteChanged event is fired,
1844 // and mute value is saved in the preferences.
1845 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
1846 EXPECT_EQ(2, test_observer_->output_mute_changed_count());
1847 EXPECT_FALSE(audio_pref_handler_->GetMuteValue(speaker));
1850 TEST_F(CrasAudioHandlerTest, SetInputMute) {
1851 AudioNodeList audio_nodes;
1852 audio_nodes.push_back(kInternalMic);
1853 SetUpCrasAudioHandler(audio_nodes);
1854 EXPECT_EQ(0, test_observer_->input_mute_changed_count());
1856 // Mute the device.
1857 cras_audio_handler_->SetInputMute(true);
1859 // Verify the input is muted, OnInputMuteChanged event is fired.
1860 EXPECT_TRUE(cras_audio_handler_->IsInputMuted());
1861 EXPECT_EQ(1, test_observer_->input_mute_changed_count());
1863 // Unmute the device.
1864 cras_audio_handler_->SetInputMute(false);
1866 // Verify the input is unmuted, OnInputMuteChanged event is fired.
1867 EXPECT_FALSE(cras_audio_handler_->IsInputMuted());
1868 EXPECT_EQ(2, test_observer_->input_mute_changed_count());
1871 TEST_F(CrasAudioHandlerTest, SetOutputVolumePercent) {
1872 AudioNodeList audio_nodes;
1873 audio_nodes.push_back(kInternalSpeaker);
1874 SetUpCrasAudioHandler(audio_nodes);
1875 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
1877 cras_audio_handler_->SetOutputVolumePercent(60);
1879 // Verify the output volume is changed to the designated value,
1880 // OnOutputNodeVolumeChanged event is fired, and the device volume value
1881 // is saved the preferences.
1882 const int kVolume = 60;
1883 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent());
1884 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
1885 AudioDevice device;
1886 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device));
1887 EXPECT_EQ(device.id, kInternalSpeaker.id);
1888 EXPECT_EQ(kVolume, audio_pref_handler_->GetOutputVolumeValue(&device));
1891 TEST_F(CrasAudioHandlerTest, SetInputGainPercent) {
1892 AudioNodeList audio_nodes;
1893 audio_nodes.push_back(kInternalMic);
1894 SetUpCrasAudioHandler(audio_nodes);
1895 EXPECT_EQ(0, test_observer_->input_gain_changed_count());
1897 cras_audio_handler_->SetInputGainPercent(60);
1899 // Verify the input gain changed to the designated value,
1900 // OnInputNodeGainChanged event is fired, and the device gain value
1901 // is saved in the preferences.
1902 const int kGain = 60;
1903 EXPECT_EQ(kGain, cras_audio_handler_->GetInputGainPercent());
1904 EXPECT_EQ(1, test_observer_->input_gain_changed_count());
1905 AudioDevice internal_mic(kInternalMic);
1906 EXPECT_EQ(kGain, audio_pref_handler_->GetInputGainValue(&internal_mic));
1909 TEST_F(CrasAudioHandlerTest, SetMuteForDevice) {
1910 AudioNodeList audio_nodes;
1911 audio_nodes.push_back(kInternalSpeaker);
1912 audio_nodes.push_back(kHeadphone);
1913 audio_nodes.push_back(kInternalMic);
1914 audio_nodes.push_back(kUSBMic);
1915 SetUpCrasAudioHandler(audio_nodes);
1917 // Mute the active output device.
1918 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1919 cras_audio_handler_->SetMuteForDevice(kHeadphone.id, true);
1921 // Verify the headphone is muted and mute value is saved in the preferences.
1922 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kHeadphone.id));
1923 AudioDevice headphone(kHeadphone);
1924 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(headphone));
1926 // Mute the non-active output device.
1927 cras_audio_handler_->SetMuteForDevice(kInternalSpeaker.id, true);
1929 // Verify the internal speaker is muted and mute value is saved in the
1930 // preferences.
1931 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kInternalSpeaker.id));
1932 AudioDevice internal_speaker(kInternalSpeaker);
1933 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(internal_speaker));
1935 // Mute the active input device.
1936 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1937 cras_audio_handler_->SetMuteForDevice(kUSBMic.id, true);
1939 // Verify the USB Mic is muted.
1940 EXPECT_TRUE(cras_audio_handler_->IsInputMutedForDevice(kUSBMic.id));
1942 // Mute the non-active input device should be a no-op, see crbug.com/365050.
1943 cras_audio_handler_->SetMuteForDevice(kInternalMic.id, true);
1945 // Verify IsInputMutedForDevice returns false for non-active input device.
1946 EXPECT_FALSE(cras_audio_handler_->IsInputMutedForDevice(kInternalMic.id));
1949 TEST_F(CrasAudioHandlerTest, SetVolumeGainPercentForDevice) {
1950 AudioNodeList audio_nodes;
1951 audio_nodes.push_back(kInternalSpeaker);
1952 audio_nodes.push_back(kHeadphone);
1953 audio_nodes.push_back(kInternalMic);
1954 audio_nodes.push_back(kUSBMic);
1955 SetUpCrasAudioHandler(audio_nodes);
1957 // Set volume percent for active output device.
1958 const int kHeadphoneVolume = 30;
1959 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1960 cras_audio_handler_->SetVolumeGainPercentForDevice(kHeadphone.id,
1961 kHeadphoneVolume);
1963 // Verify the volume percent of headphone is set, and saved in preferences.
1964 EXPECT_EQ(kHeadphoneVolume,
1965 cras_audio_handler_->GetOutputVolumePercentForDevice(
1966 kHeadphone.id));
1967 AudioDevice headphone(kHeadphone);
1968 EXPECT_EQ(kHeadphoneVolume,
1969 audio_pref_handler_->GetOutputVolumeValue(&headphone));
1971 // Set volume percent for non-active output device.
1972 const int kSpeakerVolume = 60;
1973 cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalSpeaker.id,
1974 kSpeakerVolume);
1976 // Verify the volume percent of speaker is set, and saved in preferences.
1977 EXPECT_EQ(kSpeakerVolume,
1978 cras_audio_handler_->GetOutputVolumePercentForDevice(
1979 kInternalSpeaker.id));
1980 AudioDevice speaker(kInternalSpeaker);
1981 EXPECT_EQ(kSpeakerVolume,
1982 audio_pref_handler_->GetOutputVolumeValue(&speaker));
1984 // Set gain percent for active input device.
1985 const int kUSBMicGain = 30;
1986 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1987 cras_audio_handler_->SetVolumeGainPercentForDevice(kUSBMic.id,
1988 kUSBMicGain);
1990 // Verify the gain percent of USB mic is set, and saved in preferences.
1991 EXPECT_EQ(kUSBMicGain,
1992 cras_audio_handler_->GetOutputVolumePercentForDevice(kUSBMic.id));
1993 AudioDevice usb_mic(kHeadphone);
1994 EXPECT_EQ(kUSBMicGain,
1995 audio_pref_handler_->GetInputGainValue(&usb_mic));
1997 // Set gain percent for non-active input device.
1998 const int kInternalMicGain = 60;
1999 cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalMic.id,
2000 kInternalMicGain);
2002 // Verify the gain percent of internal mic is set, and saved in preferences.
2003 EXPECT_EQ(kInternalMicGain,
2004 cras_audio_handler_->GetOutputVolumePercentForDevice(
2005 kInternalMic.id));
2006 AudioDevice internal_mic(kInternalMic);
2007 EXPECT_EQ(kInternalMicGain,
2008 audio_pref_handler_->GetInputGainValue(&internal_mic));
2011 TEST_F(CrasAudioHandlerTest, HandleOtherDeviceType) {
2012 const size_t kNumValidAudioDevices = 4;
2013 AudioNodeList audio_nodes;
2014 audio_nodes.push_back(kInternalSpeaker);
2015 audio_nodes.push_back(kOtherTypeOutput);
2016 audio_nodes.push_back(kInternalMic);
2017 audio_nodes.push_back(kOtherTypeInput);
2018 SetUpCrasAudioHandler(audio_nodes);
2020 // Verify the audio devices size.
2021 AudioDeviceList audio_devices;
2022 cras_audio_handler_->GetAudioDevices(&audio_devices);
2023 EXPECT_EQ(kNumValidAudioDevices, audio_devices.size());
2025 // Verify the internal speaker has been selected as the active output,
2026 // and the output device with some randown unknown type is handled gracefully.
2027 AudioDevice active_output;
2028 EXPECT_TRUE(
2029 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
2030 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
2031 EXPECT_EQ(kInternalSpeaker.id,
2032 cras_audio_handler_->GetPrimaryActiveOutputNode());
2033 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
2035 // Ensure the internal microphone has been selected as the active input,
2036 // and the input device with some random unknown type is handled gracefully.
2037 AudioDevice active_input;
2038 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
2039 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
2042 TEST_F(CrasAudioHandlerTest, ChangeActiveNodesHotrodInit) {
2043 AudioNodeList audio_nodes;
2044 audio_nodes.push_back(kHDMIOutput);
2045 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2046 audio_nodes.push_back(kUSBJabraSpeakerOutput2);
2047 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2048 audio_nodes.push_back(kUSBJabraSpeakerInput2);
2049 audio_nodes.push_back(kUSBCameraInput);
2050 SetUpCrasAudioHandler(audio_nodes);
2052 // Verify the audio devices size.
2053 AudioDeviceList audio_devices;
2054 cras_audio_handler_->GetAudioDevices(&audio_devices);
2055 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2057 // Verify only the 1st jabra speaker's output and input are selected as active
2058 // nodes by CrasAudioHandler.
2059 AudioDevice active_output;
2060 EXPECT_TRUE(
2061 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
2062 EXPECT_EQ(2, GetActiveDeviceCount());
2063 AudioDevice primary_active_device;
2064 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(
2065 &primary_active_device));
2066 EXPECT_EQ(kUSBJabraSpeakerOutput1.id, primary_active_device.id);
2067 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2068 cras_audio_handler_->GetPrimaryActiveInputNode());
2070 // Set both jabra speakers's input and output nodes to active, this simulate
2071 // the call sent by hotrod initialization process.
2072 test_observer_->reset_active_output_node_changed_count();
2073 test_observer_->reset_active_input_node_changed_count();
2074 CrasAudioHandler::NodeIdList active_nodes;
2075 active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2076 active_nodes.push_back(kUSBJabraSpeakerOutput2.id);
2077 active_nodes.push_back(kUSBJabraSpeakerInput1.id);
2078 active_nodes.push_back(kUSBJabraSpeakerInput2.id);
2079 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2081 // Verify both jabra speakers' input/output nodes are made active.
2082 // num_active_nodes = GetActiveDeviceCount();
2083 EXPECT_EQ(4, GetActiveDeviceCount());
2084 const AudioDevice* active_output_1 =
2085 GetDeviceFromId(kUSBJabraSpeakerOutput1.id);
2086 EXPECT_TRUE(active_output_1->active);
2087 const AudioDevice* active_output_2 =
2088 GetDeviceFromId(kUSBJabraSpeakerOutput2.id);
2089 EXPECT_TRUE(active_output_2->active);
2090 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(
2091 &primary_active_device));
2092 EXPECT_EQ(kUSBJabraSpeakerOutput1.id, primary_active_device.id);
2093 const AudioDevice* active_input_1 =
2094 GetDeviceFromId(kUSBJabraSpeakerInput1.id);
2095 EXPECT_TRUE(active_input_1->active);
2096 const AudioDevice* active_input_2 =
2097 GetDeviceFromId(kUSBJabraSpeakerInput2.id);
2098 EXPECT_TRUE(active_input_2->active);
2099 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2100 cras_audio_handler_->GetPrimaryActiveInputNode());
2102 // Verify only 1 ActiveOutputNodeChanged notification has been sent out
2103 // by calling ChangeActiveNodes.
2104 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
2105 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
2107 // Verify all active devices are the not muted and their volume values are
2108 // the same.
2109 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
2110 EXPECT_FALSE(
2111 cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput1.id));
2112 EXPECT_FALSE(
2113 cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput2.id));
2114 EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(),
2115 cras_audio_handler_->GetOutputVolumePercentForDevice(
2116 kUSBJabraSpeakerOutput1.id));
2117 EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(),
2118 cras_audio_handler_->GetOutputVolumePercentForDevice(
2119 kUSBJabraSpeakerOutput2.id));
2121 // Adjust the volume of output devices, verify all active nodes are set to
2122 // the same volume.
2123 cras_audio_handler_->SetOutputVolumePercent(25);
2124 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercent());
2125 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercentForDevice(
2126 kUSBJabraSpeakerOutput1.id));
2127 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercentForDevice(
2128 kUSBJabraSpeakerOutput2.id));
2131 TEST_F(CrasAudioHandlerTest, ChangeActiveNodesHotrodInitWithCameraInputActive) {
2132 AudioNodeList audio_nodes;
2133 audio_nodes.push_back(kHDMIOutput);
2134 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2135 audio_nodes.push_back(kUSBJabraSpeakerOutput2);
2136 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2137 audio_nodes.push_back(kUSBJabraSpeakerInput2);
2138 // Make the camera input to be plugged in later than jabra's input.
2139 AudioNode usb_camera(kUSBCameraInput);
2140 usb_camera.plugged_time = 10000000;
2141 audio_nodes.push_back(usb_camera);
2142 SetUpCrasAudioHandler(audio_nodes);
2144 // Verify the audio devices size.
2145 AudioDeviceList audio_devices;
2146 cras_audio_handler_->GetAudioDevices(&audio_devices);
2147 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2149 // Verify the 1st jabra speaker's output is selected as active output
2150 // node and camera's input is selected active input by CrasAudioHandler.
2151 EXPECT_EQ(2, GetActiveDeviceCount());
2152 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2153 cras_audio_handler_->GetPrimaryActiveOutputNode());
2154 EXPECT_EQ(kUSBCameraInput.id,
2155 cras_audio_handler_->GetPrimaryActiveInputNode());
2157 // Set both jabra speakers's input and output nodes to active, this simulates
2158 // the call sent by hotrod initialization process.
2159 test_observer_->reset_active_output_node_changed_count();
2160 test_observer_->reset_active_input_node_changed_count();
2161 CrasAudioHandler::NodeIdList active_nodes;
2162 active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2163 active_nodes.push_back(kUSBJabraSpeakerOutput2.id);
2164 active_nodes.push_back(kUSBJabraSpeakerInput1.id);
2165 active_nodes.push_back(kUSBJabraSpeakerInput2.id);
2166 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2168 // Verify both jabra speakers' input/output nodes are made active.
2169 // num_active_nodes = GetActiveDeviceCount();
2170 EXPECT_EQ(4, GetActiveDeviceCount());
2171 const AudioDevice* active_output_1 =
2172 GetDeviceFromId(kUSBJabraSpeakerOutput1.id);
2173 EXPECT_TRUE(active_output_1->active);
2174 const AudioDevice* active_output_2 =
2175 GetDeviceFromId(kUSBJabraSpeakerOutput2.id);
2176 EXPECT_TRUE(active_output_2->active);
2177 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2178 cras_audio_handler_->GetPrimaryActiveOutputNode());
2179 const AudioDevice* active_input_1 =
2180 GetDeviceFromId(kUSBJabraSpeakerInput1.id);
2181 EXPECT_TRUE(active_input_1->active);
2182 const AudioDevice* active_input_2 =
2183 GetDeviceFromId(kUSBJabraSpeakerInput2.id);
2184 EXPECT_TRUE(active_input_2->active);
2185 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2186 cras_audio_handler_->GetPrimaryActiveInputNode());
2188 // Verify only 1 ActiveOutputNodeChanged notification has been sent out
2189 // by calling ChangeActiveNodes.
2190 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
2191 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
2194 TEST_F(CrasAudioHandlerTest, ChangeActiveNodesWithFewerActives) {
2195 AudioNodeList audio_nodes;
2196 audio_nodes.push_back(kHDMIOutput);
2197 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2198 audio_nodes.push_back(kUSBJabraSpeakerOutput2);
2199 SetUpCrasAudioHandler(audio_nodes);
2201 // Verify the audio devices size.
2202 AudioDeviceList audio_devices;
2203 cras_audio_handler_->GetAudioDevices(&audio_devices);
2204 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2206 // Set all three nodes to be active.
2207 CrasAudioHandler::NodeIdList active_nodes;
2208 active_nodes.push_back(kHDMIOutput.id);
2209 active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2210 active_nodes.push_back(kUSBJabraSpeakerOutput2.id);
2211 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2213 // Verify all three nodes are active.
2214 EXPECT_EQ(3, GetActiveDeviceCount());
2215 const AudioDevice* active_output_1 = GetDeviceFromId(kHDMIOutput.id);
2216 EXPECT_TRUE(active_output_1->active);
2217 const AudioDevice* active_output_2 =
2218 GetDeviceFromId(kUSBJabraSpeakerOutput1.id);
2219 EXPECT_TRUE(active_output_2->active);
2220 const AudioDevice* active_output_3 =
2221 GetDeviceFromId(kUSBJabraSpeakerOutput2.id);
2222 EXPECT_TRUE(active_output_3->active);
2224 // Now call ChangeActiveDevices with only 2 nodes.
2225 active_nodes.clear();
2226 active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2227 active_nodes.push_back(kUSBJabraSpeakerOutput2.id);
2228 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2230 // Verify only 2 nodes are active.
2231 EXPECT_EQ(2, GetActiveDeviceCount());
2232 const AudioDevice* output_1 = GetDeviceFromId(kHDMIOutput.id);
2233 EXPECT_FALSE(output_1->active);
2234 const AudioDevice* output_2 = GetDeviceFromId(kUSBJabraSpeakerOutput1.id);
2235 EXPECT_TRUE(output_2->active);
2236 const AudioDevice* output_3 = GetDeviceFromId(kUSBJabraSpeakerOutput2.id);
2237 EXPECT_TRUE(output_3->active);
2240 TEST_F(CrasAudioHandlerTest, HotrodInitWithSingleJabra) {
2241 // Simulates the hotrod initializated with a single jabra device and
2242 // CrasAudioHandler selected jabra input/output as active devices.
2243 AudioNodeList audio_nodes;
2244 audio_nodes.push_back(kHDMIOutput);
2245 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2246 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2247 audio_nodes.push_back(kUSBCameraInput);
2248 SetUpCrasAudioHandler(audio_nodes);
2250 // Verify the audio devices size.
2251 AudioDeviceList audio_devices;
2252 cras_audio_handler_->GetAudioDevices(&audio_devices);
2253 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2255 // Verify the jabra speaker's output and input are selected as active nodes
2256 // by CrasAudioHandler.
2257 EXPECT_EQ(2, GetActiveDeviceCount());
2258 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2259 cras_audio_handler_->GetPrimaryActiveOutputNode());
2260 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2261 cras_audio_handler_->GetPrimaryActiveInputNode());
2264 TEST_F(CrasAudioHandlerTest,
2265 ChangeActiveNodesHotrodInitWithSingleJabraCameraPlugInLater) {
2266 AudioNodeList audio_nodes;
2267 audio_nodes.push_back(kHDMIOutput);
2268 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2269 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2270 AudioNode usb_camera(kUSBCameraInput);
2271 usb_camera.plugged_time = 10000000;
2272 audio_nodes.push_back(usb_camera);
2273 SetUpCrasAudioHandler(audio_nodes);
2275 // Verify the audio devices size.
2276 AudioDeviceList audio_devices;
2277 cras_audio_handler_->GetAudioDevices(&audio_devices);
2278 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2280 // Verify the jabra speaker's output is selected as active output, and
2281 // camera's input is selected as active input by CrasAudioHandler
2282 EXPECT_EQ(2, GetActiveDeviceCount());
2283 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2284 cras_audio_handler_->GetPrimaryActiveOutputNode());
2285 EXPECT_EQ(kUSBCameraInput.id,
2286 cras_audio_handler_->GetPrimaryActiveInputNode());
2288 // Simulate hotrod app call to set jabra input as active device with only
2289 // jabra input node in the active node list, which does not conform to the
2290 // new SetActiveDevices protocol, but just show we can still handle it if
2291 // this happens.
2292 CrasAudioHandler::NodeIdList active_nodes;
2293 active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2294 active_nodes.push_back(kUSBJabraSpeakerInput1.id);
2295 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2297 // Verify the jabra speaker's output is selected as active output, and
2298 // jabra's input is selected as active input.
2299 EXPECT_EQ(2, GetActiveDeviceCount());
2300 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2301 cras_audio_handler_->GetPrimaryActiveOutputNode());
2302 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2303 cras_audio_handler_->GetPrimaryActiveInputNode());
2306 TEST_F(CrasAudioHandlerTest,
2307 ChangeActiveNodesHotrodInitWithSingleJabraCameraPlugInLaterOldCall) {
2308 AudioNodeList audio_nodes;
2309 audio_nodes.push_back(kHDMIOutput);
2310 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2311 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2312 AudioNode usb_camera(kUSBCameraInput);
2313 usb_camera.plugged_time = 10000000;
2314 audio_nodes.push_back(usb_camera);
2315 SetUpCrasAudioHandler(audio_nodes);
2317 // Verify the audio devices size.
2318 AudioDeviceList audio_devices;
2319 cras_audio_handler_->GetAudioDevices(&audio_devices);
2320 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2322 // Verify the jabra speaker's output is selected as active output, and
2323 // camera's input is selected as active input by CrasAudioHandler
2324 EXPECT_EQ(2, GetActiveDeviceCount());
2325 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2326 cras_audio_handler_->GetPrimaryActiveOutputNode());
2327 EXPECT_EQ(kUSBCameraInput.id,
2328 cras_audio_handler_->GetPrimaryActiveInputNode());
2330 // Simulate hotrod app call to set jabra input as active device with only
2331 // jabra input node in the active node list, which does not conform to the
2332 // new SetActiveDevices protocol, but just show we can still handle it if
2333 // this happens.
2334 CrasAudioHandler::NodeIdList active_nodes;
2335 active_nodes.push_back(kUSBJabraSpeakerInput1.id);
2336 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2338 // Verify the jabra speaker's output is selected as active output, and
2339 // jabra's input is selected as active input.
2340 EXPECT_EQ(2, GetActiveDeviceCount());
2341 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2342 cras_audio_handler_->GetPrimaryActiveOutputNode());
2343 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2344 cras_audio_handler_->GetPrimaryActiveInputNode());
2347 TEST_F(CrasAudioHandlerTest,
2348 ChangeActiveNodesHotrodInitWithSingleJabraChangeOutput) {
2349 AudioNodeList audio_nodes;
2350 audio_nodes.push_back(kHDMIOutput);
2351 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2352 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2353 audio_nodes.push_back(kUSBCameraInput);
2354 SetUpCrasAudioHandler(audio_nodes);
2356 // Verify the audio devices size.
2357 AudioDeviceList audio_devices;
2358 cras_audio_handler_->GetAudioDevices(&audio_devices);
2359 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2361 // Verify the jabra speaker's output and input are selected as active output
2362 // by CrasAudioHandler.
2363 EXPECT_EQ(2, GetActiveDeviceCount());
2364 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2365 cras_audio_handler_->GetPrimaryActiveOutputNode());
2366 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2367 cras_audio_handler_->GetPrimaryActiveInputNode());
2369 // Simulate hotrod app call SetActiveDevices to change active output
2370 // with only complete list of active nodes passed in, which is the new
2371 // way of hotrod app.
2372 CrasAudioHandler::NodeIdList active_nodes;
2373 active_nodes.push_back(kHDMIOutput.id);
2374 active_nodes.push_back(kUSBJabraSpeakerInput1.id);
2375 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2377 // Verify the jabra speaker's output is selected as active output, and
2378 // jabra's input is selected as active input.
2379 EXPECT_EQ(2, GetActiveDeviceCount());
2380 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2381 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2382 cras_audio_handler_->GetPrimaryActiveInputNode());
2385 TEST_F(CrasAudioHandlerTest,
2386 ChangeActiveNodesHotrodInitWithSingleJabraChangeOutputOldCall) {
2387 AudioNodeList audio_nodes;
2388 audio_nodes.push_back(kHDMIOutput);
2389 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2390 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2391 audio_nodes.push_back(kUSBCameraInput);
2392 SetUpCrasAudioHandler(audio_nodes);
2394 // Verify the audio devices size.
2395 AudioDeviceList audio_devices;
2396 cras_audio_handler_->GetAudioDevices(&audio_devices);
2397 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2399 // Verify the jabra speaker's output and input are selected as active output
2400 // by CrasAudioHandler.
2401 EXPECT_EQ(2, GetActiveDeviceCount());
2402 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2403 cras_audio_handler_->GetPrimaryActiveOutputNode());
2404 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2405 cras_audio_handler_->GetPrimaryActiveInputNode());
2407 // Simulate hotrod app call SetActiveDevices to change active output
2408 // with only a single active output nodes passed in, which is the old
2409 // way of hotrod app.
2410 CrasAudioHandler::NodeIdList active_nodes;
2411 active_nodes.push_back(kHDMIOutput.id);
2412 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2414 // Verify the jabra speaker's output is selected as active output, and
2415 // jabra's input is selected as active input.
2416 EXPECT_EQ(2, GetActiveDeviceCount());
2417 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2418 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2419 cras_audio_handler_->GetPrimaryActiveInputNode());
2422 TEST_F(CrasAudioHandlerTest, NoMoreAudioInputDevices) {
2423 // Some device like chromebox does not have the internal input device. The
2424 // active devices should be reset when the user plugs a device and then
2425 // unplugs it to such device.
2427 AudioNodeList audio_nodes;
2428 audio_nodes.push_back(kInternalSpeaker);
2429 SetUpCrasAudioHandler(audio_nodes);
2431 EXPECT_EQ(0ULL, cras_audio_handler_->GetPrimaryActiveInputNode());
2433 audio_nodes.push_back(kMicJack);
2434 ChangeAudioNodes(audio_nodes);
2436 EXPECT_EQ(kMicJack.id, cras_audio_handler_->GetPrimaryActiveInputNode());
2437 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
2438 test_observer_->reset_active_input_node_changed_count();
2440 audio_nodes.pop_back();
2441 ChangeAudioNodes(audio_nodes);
2442 EXPECT_EQ(0ULL, cras_audio_handler_->GetPrimaryActiveInputNode());
2443 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
2446 // Test the case in which an HDMI output is plugged in with other higher
2447 // priority
2448 // output devices already plugged and user has manually selected an active
2449 // output.
2450 // The hotplug of hdmi output should not change user's selection of active
2451 // device.
2452 // crbug.com/447826.
2453 TEST_F(CrasAudioHandlerTest, HotPlugHDMINotChangeActiveOutput) {
2454 AudioNodeList audio_nodes;
2455 AudioNode internal_speaker(kInternalSpeaker);
2456 audio_nodes.push_back(internal_speaker);
2457 AudioNode usb_headset(kUSBHeadphone1);
2458 usb_headset.plugged_time = 80000000;
2459 audio_nodes.push_back(usb_headset);
2460 SetUpCrasAudioHandler(audio_nodes);
2462 // Verify the audio devices size.
2463 AudioDeviceList audio_devices;
2464 cras_audio_handler_->GetAudioDevices(&audio_devices);
2465 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2467 // Verify the USB headset is selected as active output by default.
2468 EXPECT_EQ(usb_headset.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2470 // Manually set the active output to internal speaker.
2471 AudioDevice internal_output(kInternalSpeaker);
2472 cras_audio_handler_->SwitchToDevice(internal_output, true);
2474 // Verify the active output is switched to internal speaker.
2475 EXPECT_EQ(internal_speaker.id,
2476 cras_audio_handler_->GetPrimaryActiveOutputNode());
2477 EXPECT_LT(kInternalSpeaker.plugged_time, usb_headset.plugged_time);
2478 const AudioDevice* usb_device = GetDeviceFromId(usb_headset.id);
2479 EXPECT_FALSE(usb_device->active);
2481 // Plug in HDMI output.
2482 audio_nodes.clear();
2483 internal_speaker.active = true;
2484 audio_nodes.push_back(internal_speaker);
2485 usb_headset.active = false;
2486 audio_nodes.push_back(usb_headset);
2487 AudioNode hdmi(kHDMIOutput);
2488 hdmi.plugged_time = 90000000;
2489 audio_nodes.push_back(hdmi);
2490 ChangeAudioNodes(audio_nodes);
2492 // The active output should not change.
2493 EXPECT_EQ(kInternalSpeaker.id,
2494 cras_audio_handler_->GetPrimaryActiveOutputNode());
2497 // Test the case in which the active device was set to inactive from cras after
2498 // resuming from suspension state. See crbug.com/478968.
2499 TEST_F(CrasAudioHandlerTest, ActiveNodeLostAfterResume) {
2500 AudioNodeList audio_nodes;
2501 EXPECT_FALSE(kHeadphone.active);
2502 audio_nodes.push_back(kHeadphone);
2503 EXPECT_FALSE(kHDMIOutput.active);
2504 audio_nodes.push_back(kHDMIOutput);
2505 SetUpCrasAudioHandler(audio_nodes);
2507 // Verify the headphone is selected as the active output.
2508 AudioDeviceList audio_devices;
2509 cras_audio_handler_->GetAudioDevices(&audio_devices);
2510 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2511 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2512 const AudioDevice* active_headphone = GetDeviceFromId(kHeadphone.id);
2513 EXPECT_EQ(kHeadphone.id, active_headphone->id);
2514 EXPECT_TRUE(active_headphone->active);
2516 // Simulate NodesChanged signal with headphone turning into inactive state,
2517 // and HDMI node removed.
2518 audio_nodes.clear();
2519 audio_nodes.push_back(kHeadphone);
2520 ChangeAudioNodes(audio_nodes);
2522 // Verify the headphone is set to active again.
2523 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2524 const AudioDevice* headphone_resumed = GetDeviceFromId(kHeadphone.id);
2525 EXPECT_EQ(kHeadphone.id, headphone_resumed->id);
2526 EXPECT_TRUE(headphone_resumed->active);
2529 // Test the case in which there are two NodesChanged signal for discovering
2530 // output devices, and there is race between NodesChange and SetActiveOutput
2531 // during this process. See crbug.com/478968.
2532 TEST_F(CrasAudioHandlerTest, ActiveNodeLostDuringLoginSession) {
2533 AudioNodeList audio_nodes;
2534 EXPECT_FALSE(kHeadphone.active);
2535 audio_nodes.push_back(kHeadphone);
2536 SetUpCrasAudioHandler(audio_nodes);
2538 // Verify the headphone is selected as the active output.
2539 AudioDeviceList audio_devices;
2540 cras_audio_handler_->GetAudioDevices(&audio_devices);
2541 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2542 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2543 const AudioDevice* active_headphone = GetDeviceFromId(kHeadphone.id);
2544 EXPECT_EQ(kHeadphone.id, active_headphone->id);
2545 EXPECT_TRUE(active_headphone->active);
2547 // Simulate NodesChanged signal with headphone turning into inactive state,
2548 // and add a new HDMI output node.
2549 audio_nodes.clear();
2550 audio_nodes.push_back(kHeadphone);
2551 audio_nodes.push_back(kHDMIOutput);
2552 ChangeAudioNodes(audio_nodes);
2554 // Verify the headphone is set to active again.
2555 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2556 const AudioDevice* headphone_resumed = GetDeviceFromId(kHeadphone.id);
2557 EXPECT_EQ(kHeadphone.id, headphone_resumed->id);
2558 EXPECT_TRUE(headphone_resumed->active);
2561 // This test HDMI output rediscovering case in crbug.com/503667.
2562 TEST_F(CrasAudioHandlerTest, HDMIOutputRediscover) {
2563 AudioNodeList audio_nodes;
2564 audio_nodes.push_back(kInternalSpeaker);
2565 audio_nodes.push_back(kHDMIOutput);
2566 SetUpCrasAudioHandler(audio_nodes);
2568 // Verify the HDMI device has been selected as the active output, and audio
2569 // output is not muted.
2570 AudioDevice active_output;
2571 EXPECT_TRUE(
2572 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
2573 EXPECT_EQ(kHDMIOutput.id, active_output.id);
2574 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2575 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
2576 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
2578 // Trigger HDMI rediscovering grace period, and remove the HDMI node.
2579 const int grace_period_in_ms = 200;
2580 SetHDMIRediscoverGracePeriodDuration(grace_period_in_ms);
2581 SetActiveHDMIRediscover();
2582 AudioNodeList audio_nodes_lost_hdmi;
2583 audio_nodes_lost_hdmi.push_back(kInternalSpeaker);
2584 ChangeAudioNodes(audio_nodes_lost_hdmi);
2586 // Verify the active output is switched to internal speaker, it is not muted
2587 // by preference, but the system output is muted during the grace period.
2588 EXPECT_TRUE(
2589 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
2590 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
2591 EXPECT_FALSE(
2592 cras_audio_handler_->IsOutputMutedForDevice(kInternalSpeaker.id));
2593 EXPECT_TRUE(cras_audio_handler_->IsOutputMuted());
2595 // Re-attach the HDMI device after a little delay.
2596 HDMIRediscoverWaiter waiter(this, grace_period_in_ms);
2597 waiter.WaitUntilTimeOut(grace_period_in_ms / 4);
2598 ChangeAudioNodes(audio_nodes);
2600 // After HDMI re-discover grace period, verify HDMI output is selected as the
2601 // active device and not muted.
2602 waiter.WaitUntilHDMIRediscoverGracePeriodEnd();
2603 EXPECT_TRUE(
2604 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
2605 EXPECT_EQ(kHDMIOutput.id, active_output.id);
2606 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2607 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
2610 // This tests the case of output unmuting event is notified after the hdmi
2611 // output re-discover grace period ends, see crbug.com/512601.
2612 TEST_F(CrasAudioHandlerTest, HDMIOutputUnplugDuringSuspension) {
2613 AudioNodeList audio_nodes;
2614 audio_nodes.push_back(kInternalSpeaker);
2615 audio_nodes.push_back(kHDMIOutput);
2616 SetUpCrasAudioHandler(audio_nodes);
2618 // Verify the HDMI device has been selected as the active output, and audio
2619 // output is not muted.
2620 AudioDevice active_output;
2621 EXPECT_TRUE(
2622 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
2623 EXPECT_EQ(kHDMIOutput.id, active_output.id);
2624 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2625 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
2626 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
2628 // Trigger HDMI rediscovering grace period, and remove the HDMI node.
2629 const int grace_period_in_ms = 200;
2630 SetHDMIRediscoverGracePeriodDuration(grace_period_in_ms);
2631 SetActiveHDMIRediscover();
2632 AudioNodeList audio_nodes_lost_hdmi;
2633 audio_nodes_lost_hdmi.push_back(kInternalSpeaker);
2634 ChangeAudioNodes(audio_nodes_lost_hdmi);
2636 // Verify the active output is switched to internal speaker, it is not muted
2637 // by preference, but the system output is muted during the grace period.
2638 EXPECT_TRUE(
2639 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
2640 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
2641 EXPECT_FALSE(
2642 cras_audio_handler_->IsOutputMutedForDevice(kInternalSpeaker.id));
2643 EXPECT_TRUE(cras_audio_handler_->IsOutputMuted());
2645 // After HDMI re-discover grace period, verify internal speaker is still the
2646 // active output and not muted, and unmute event by system is notified.
2647 test_observer_->reset_output_mute_changed_count();
2648 HDMIRediscoverWaiter waiter(this, grace_period_in_ms);
2649 waiter.WaitUntilHDMIRediscoverGracePeriodEnd();
2650 EXPECT_TRUE(
2651 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
2652 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
2653 EXPECT_EQ(kInternalSpeaker.id,
2654 cras_audio_handler_->GetPrimaryActiveOutputNode());
2655 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
2656 EXPECT_EQ(1, test_observer_->output_mute_changed_count());
2657 EXPECT_TRUE(test_observer_->output_mute_by_system());
2660 } // namespace chromeos