[Android] Hide partner bookmark node if it's deleted.
[chromium-blink-merge.git] / chromeos / audio / cras_audio_handler_unittest.cc
blob574b421abf21e73121b6622b822034a75c38a080
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chromeos/audio/cras_audio_handler.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/values.h"
11 #include "chromeos/audio/audio_devices_pref_handler_stub.h"
12 #include "chromeos/dbus/audio_node.h"
13 #include "chromeos/dbus/dbus_thread_manager.h"
14 #include "chromeos/dbus/fake_cras_audio_client.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace chromeos {
19 const uint64 kInternalSpeakerId = 10001;
20 const uint64 kHeadphoneId = 10002;
21 const uint64 kInternalMicId = 10003;
22 const uint64 kUSBMicId = 10004;
23 const uint64 kBluetoothHeadsetId = 10005;
24 const uint64 kHDMIOutputId = 10006;
25 const uint64 kUSBHeadphoneId1 = 10007;
26 const uint64 kUSBHeadphoneId2 = 10008;
27 const uint64 kMicJackId = 10009;
28 const uint64 kKeyboardMicId = 10010;
29 const uint64 kOtherTypeOutputId = 90001;
30 const uint64 kOtherTypeInputId = 90002;
31 const uint64 kUSBJabraSpeakerOutputId1 = 90003;
32 const uint64 kUSBJabraSpeakerOutputId2 = 90004;
33 const uint64 kUSBJabraSpeakerInputId1 = 90005;
34 const uint64 kUSBJabraSpeakerInputId2 = 90006;
35 const uint64 kUSBCameraInputId = 90007;
37 const AudioNode kInternalSpeaker(
38 false,
39 kInternalSpeakerId,
40 "Fake Speaker",
41 "INTERNAL_SPEAKER",
42 "Speaker",
43 false,
47 const AudioNode kHeadphone(
48 false,
49 kHeadphoneId,
50 "Fake Headphone",
51 "HEADPHONE",
52 "Headphone",
53 false,
57 const AudioNode kInternalMic(
58 true,
59 kInternalMicId,
60 "Fake Mic",
61 "INTERNAL_MIC",
62 "Internal Mic",
63 false,
67 const AudioNode kMicJack(
68 true,
69 kMicJackId,
70 "Fake Mic Jack",
71 "MIC",
72 "Mic Jack",
73 false,
77 const AudioNode kUSBMic(
78 true,
79 kUSBMicId,
80 "Fake USB Mic",
81 "USB",
82 "USB Microphone",
83 false,
87 const AudioNode kKeyboardMic(
88 true,
89 kKeyboardMicId,
90 "Fake Keyboard Mic",
91 "KEYBOARD_MIC",
92 "Keyboard Mic",
93 false,
97 const AudioNode kOtherTypeOutput(
98 false,
99 kOtherTypeOutputId,
100 "Output Device",
101 "SOME_OTHER_TYPE",
102 "Other Type Output Device",
103 false,
107 const AudioNode kOtherTypeInput(
108 true,
109 kOtherTypeInputId,
110 "Input Device",
111 "SOME_OTHER_TYPE",
112 "Other Type Input Device",
113 false,
117 const AudioNode kBluetoothHeadset (
118 false,
119 kBluetoothHeadsetId,
120 "Bluetooth Headset",
121 "BLUETOOTH",
122 "Bluetooth Headset 1",
123 false,
127 const AudioNode kHDMIOutput (
128 false,
129 kHDMIOutputId,
130 "HDMI output",
131 "HDMI",
132 "HDMI output",
133 false,
137 const AudioNode kUSBHeadphone1 (
138 false,
139 kUSBHeadphoneId1,
140 "USB Headphone",
141 "USB",
142 "USB Headphone 1",
143 false,
147 const AudioNode kUSBHeadphone2 (
148 false,
149 kUSBHeadphoneId2,
150 "USB Headphone",
151 "USB",
152 "USB Headphone 1",
153 false,
157 const AudioNode kUSBJabraSpeakerOutput1(false,
158 kUSBJabraSpeakerOutputId1,
159 "Jabra Speaker 1",
160 "USB",
161 "Jabra Speaker 1",
162 false,
165 const AudioNode kUSBJabraSpeakerOutput2(false,
166 kUSBJabraSpeakerOutputId2,
167 "Jabra Speaker 2",
168 "USB",
169 "Jabra Speaker 2",
170 false,
173 const AudioNode kUSBJabraSpeakerInput1(true,
174 kUSBJabraSpeakerInputId1,
175 "Jabra Speaker 1",
176 "USB",
177 "Jabra Speaker",
178 false,
181 const AudioNode kUSBJabraSpeakerInput2(true,
182 kUSBJabraSpeakerInputId2,
183 "Jabra Speaker 2",
184 "USB",
185 "Jabra Speaker 2",
186 false,
189 const AudioNode kUSBCameraInput(true,
190 kUSBCameraInputId,
191 "USB Camera",
192 "USB",
193 "USB Camera",
194 false,
197 class TestObserver : public chromeos::CrasAudioHandler::AudioObserver {
198 public:
199 TestObserver() : active_output_node_changed_count_(0),
200 active_input_node_changed_count_(0),
201 audio_nodes_changed_count_(0),
202 output_mute_changed_count_(0),
203 input_mute_changed_count_(0),
204 output_volume_changed_count_(0),
205 input_gain_changed_count_(0) {
208 int active_output_node_changed_count() const {
209 return active_output_node_changed_count_;
212 void reset_active_output_node_changed_count() {
213 active_output_node_changed_count_ = 0;
216 int active_input_node_changed_count() const {
217 return active_input_node_changed_count_;
220 void reset_active_input_node_changed_count() {
221 active_input_node_changed_count_ = 0;
224 int audio_nodes_changed_count() const {
225 return audio_nodes_changed_count_;
228 int output_mute_changed_count() const {
229 return output_mute_changed_count_;
232 int input_mute_changed_count() const {
233 return input_mute_changed_count_;
236 int output_volume_changed_count() const {
237 return output_volume_changed_count_;
240 int input_gain_changed_count() const {
241 return input_gain_changed_count_;
244 ~TestObserver() override {}
246 protected:
247 // chromeos::CrasAudioHandler::AudioObserver overrides.
248 void OnActiveOutputNodeChanged() override {
249 ++active_output_node_changed_count_;
252 void OnActiveInputNodeChanged() override {
253 ++active_input_node_changed_count_;
256 void OnAudioNodesChanged() override { ++audio_nodes_changed_count_; }
258 void OnOutputMuteChanged() override { ++output_mute_changed_count_; }
260 void OnInputMuteChanged() override { ++input_mute_changed_count_; }
262 void OnOutputVolumeChanged() override { ++output_volume_changed_count_; }
264 void OnInputGainChanged() override { ++input_gain_changed_count_; }
266 private:
267 int active_output_node_changed_count_;
268 int active_input_node_changed_count_;
269 int audio_nodes_changed_count_;
270 int output_mute_changed_count_;
271 int input_mute_changed_count_;
272 int output_volume_changed_count_;
273 int input_gain_changed_count_;
275 DISALLOW_COPY_AND_ASSIGN(TestObserver);
278 class CrasAudioHandlerTest : public testing::Test {
279 public:
280 CrasAudioHandlerTest() : cras_audio_handler_(NULL),
281 fake_cras_audio_client_(NULL) {
283 ~CrasAudioHandlerTest() override {}
285 void SetUp() override {}
287 void TearDown() override {
288 cras_audio_handler_->RemoveAudioObserver(test_observer_.get());
289 test_observer_.reset();
290 CrasAudioHandler::Shutdown();
291 audio_pref_handler_ = NULL;
292 DBusThreadManager::Shutdown();
295 void SetUpCrasAudioHandler(const AudioNodeList& audio_nodes) {
296 DBusThreadManager::Initialize();
297 fake_cras_audio_client_ = static_cast<FakeCrasAudioClient*>(
298 DBusThreadManager::Get()->GetCrasAudioClient());
299 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes);
300 audio_pref_handler_ = new AudioDevicesPrefHandlerStub();
301 CrasAudioHandler::Initialize(audio_pref_handler_);
302 cras_audio_handler_ = CrasAudioHandler::Get();
303 test_observer_.reset(new TestObserver);
304 cras_audio_handler_->AddAudioObserver(test_observer_.get());
305 message_loop_.RunUntilIdle();
308 void SetUpCrasAudioHandlerWithPrimaryActiveNode(
309 const AudioNodeList& audio_nodes,
310 const AudioNode& primary_active_node) {
311 DBusThreadManager::Initialize();
312 fake_cras_audio_client_ = static_cast<FakeCrasAudioClient*>(
313 DBusThreadManager::Get()->GetCrasAudioClient());
314 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes);
315 fake_cras_audio_client_->SetActiveOutputNode(primary_active_node.id),
316 audio_pref_handler_ = new AudioDevicesPrefHandlerStub();
317 CrasAudioHandler::Initialize(audio_pref_handler_);
318 cras_audio_handler_ = CrasAudioHandler::Get();
319 test_observer_.reset(new TestObserver);
320 cras_audio_handler_->AddAudioObserver(test_observer_.get());
321 message_loop_.RunUntilIdle();
324 void ChangeAudioNodes(const AudioNodeList& audio_nodes) {
325 fake_cras_audio_client_->SetAudioNodesAndNotifyObserversForTesting(
326 audio_nodes);
327 message_loop_.RunUntilIdle();
330 const AudioDevice* GetDeviceFromId(uint64 id) {
331 return cras_audio_handler_->GetDeviceFromId(id);
334 int GetActiveDeviceCount() const {
335 int num_active_nodes = 0;
336 AudioDeviceList audio_devices;
337 cras_audio_handler_->GetAudioDevices(&audio_devices);
338 for (size_t i = 0; i < audio_devices.size(); ++i) {
339 if (audio_devices[i].active)
340 ++num_active_nodes;
342 return num_active_nodes;
345 protected:
346 base::MessageLoopForUI message_loop_;
347 CrasAudioHandler* cras_audio_handler_; // Not owned.
348 FakeCrasAudioClient* fake_cras_audio_client_; // Not owned.
349 scoped_ptr<TestObserver> test_observer_;
350 scoped_refptr<AudioDevicesPrefHandlerStub> audio_pref_handler_;
352 private:
353 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandlerTest);
356 TEST_F(CrasAudioHandlerTest, InitializeWithOnlyDefaultAudioDevices) {
357 AudioNodeList audio_nodes;
358 audio_nodes.push_back(kInternalSpeaker);
359 audio_nodes.push_back(kInternalMic);
360 SetUpCrasAudioHandler(audio_nodes);
362 // Verify the audio devices size.
363 AudioDeviceList audio_devices;
364 cras_audio_handler_->GetAudioDevices(&audio_devices);
365 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
367 // Verify the internal speaker has been selected as the active output.
368 AudioDevice active_output;
369 EXPECT_TRUE(
370 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
371 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
372 EXPECT_EQ(kInternalSpeaker.id,
373 cras_audio_handler_->GetPrimaryActiveOutputNode());
374 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
376 // Ensure the internal microphone has been selected as the active input.
377 AudioDevice active_input;
378 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
379 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
382 TEST_F(CrasAudioHandlerTest, InitializeWithAlternativeAudioDevices) {
383 AudioNodeList audio_nodes;
384 audio_nodes.push_back(kInternalSpeaker);
385 audio_nodes.push_back(kHeadphone);
386 audio_nodes.push_back(kInternalMic);
387 audio_nodes.push_back(kUSBMic);
388 SetUpCrasAudioHandler(audio_nodes);
390 // Verify the audio devices size.
391 AudioDeviceList audio_devices;
392 cras_audio_handler_->GetAudioDevices(&audio_devices);
393 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
395 // Verify the headphone has been selected as the active output.
396 AudioDevice active_output;
397 EXPECT_TRUE(
398 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
399 EXPECT_EQ(kHeadphone.id, active_output.id);
400 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
401 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
403 // Ensure the USB microphone has been selected as the active input.
404 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
405 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
408 TEST_F(CrasAudioHandlerTest, InitializeWithKeyboardMic) {
409 AudioNodeList audio_nodes;
410 audio_nodes.push_back(kInternalSpeaker);
411 audio_nodes.push_back(kInternalMic);
412 audio_nodes.push_back(kKeyboardMic);
413 SetUpCrasAudioHandler(audio_nodes);
415 // Verify the audio devices size.
416 AudioDeviceList audio_devices;
417 cras_audio_handler_->GetAudioDevices(&audio_devices);
418 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
419 EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic());
421 // Verify the internal speaker has been selected as the active output.
422 AudioDevice active_output;
423 EXPECT_TRUE(
424 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
425 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
426 EXPECT_EQ(kInternalSpeaker.id,
427 cras_audio_handler_->GetPrimaryActiveOutputNode());
428 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
430 // Ensure the internal microphone has been selected as the active input,
431 // not affected by keyboard mic.
432 AudioDevice active_input;
433 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
434 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
435 const AudioDevice* keyboard_mic = GetDeviceFromId(kKeyboardMic.id);
436 EXPECT_FALSE(keyboard_mic->active);
439 TEST_F(CrasAudioHandlerTest, SetKeyboardMicActive) {
440 AudioNodeList audio_nodes;
441 audio_nodes.push_back(kInternalMic);
442 audio_nodes.push_back(kKeyboardMic);
443 SetUpCrasAudioHandler(audio_nodes);
445 // Verify the audio devices size.
446 AudioDeviceList audio_devices;
447 cras_audio_handler_->GetAudioDevices(&audio_devices);
448 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
449 EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic());
451 // Ensure the internal microphone has been selected as the active input,
452 // not affected by keyboard mic.
453 AudioDevice active_input;
454 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
455 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
456 const AudioDevice* keyboard_mic = GetDeviceFromId(kKeyboardMic.id);
457 EXPECT_FALSE(keyboard_mic->active);
459 // Make keyboard mic active.
460 cras_audio_handler_->SetKeyboardMicActive(true);
461 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
462 const AudioDevice* active_keyboard_mic = GetDeviceFromId(kKeyboardMic.id);
463 EXPECT_TRUE(active_keyboard_mic->active);
465 // Make keyboard mic inactive.
466 cras_audio_handler_->SetKeyboardMicActive(false);
467 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
468 const AudioDevice* inactive_keyboard_mic = GetDeviceFromId(kKeyboardMic.id);
469 EXPECT_FALSE(inactive_keyboard_mic->active);
472 TEST_F(CrasAudioHandlerTest, SwitchActiveOutputDevice) {
473 AudioNodeList audio_nodes;
474 audio_nodes.push_back(kInternalSpeaker);
475 audio_nodes.push_back(kHeadphone);
476 SetUpCrasAudioHandler(audio_nodes);
477 AudioDeviceList audio_devices;
478 cras_audio_handler_->GetAudioDevices(&audio_devices);
479 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
481 // Verify the initial active output device is headphone.
482 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
483 AudioDevice active_output;
484 EXPECT_TRUE(
485 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
486 EXPECT_EQ(kHeadphone.id, active_output.id);
487 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
489 // Switch the active output to internal speaker.
490 AudioDevice internal_speaker(kInternalSpeaker);
491 cras_audio_handler_->SwitchToDevice(internal_speaker, true);
493 // Verify the active output is switched to internal speaker, and the
494 // ActiveOutputNodeChanged event is fired.
495 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
496 EXPECT_TRUE(
497 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
498 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
499 EXPECT_EQ(kInternalSpeaker.id,
500 cras_audio_handler_->GetPrimaryActiveOutputNode());
503 TEST_F(CrasAudioHandlerTest, SwitchActiveInputDevice) {
504 AudioNodeList audio_nodes;
505 audio_nodes.push_back(kInternalMic);
506 audio_nodes.push_back(kUSBMic);
507 SetUpCrasAudioHandler(audio_nodes);
508 AudioDeviceList audio_devices;
509 cras_audio_handler_->GetAudioDevices(&audio_devices);
510 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
512 // Verify the initial active input device is USB mic.
513 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
514 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
516 // Switch the active input to internal mic.
517 AudioDevice internal_mic(kInternalMic);
518 cras_audio_handler_->SwitchToDevice(internal_mic, true);
520 // Verify the active output is switched to internal speaker, and the active
521 // ActiveInputNodeChanged event is fired.
522 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
523 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
526 TEST_F(CrasAudioHandlerTest, PlugHeadphone) {
527 // Set up initial audio devices, only with internal speaker.
528 AudioNodeList audio_nodes;
529 audio_nodes.push_back(kInternalSpeaker);
530 SetUpCrasAudioHandler(audio_nodes);
531 const size_t init_nodes_size = audio_nodes.size();
533 // Verify the audio devices size.
534 AudioDeviceList audio_devices;
535 cras_audio_handler_->GetAudioDevices(&audio_devices);
536 EXPECT_EQ(init_nodes_size, audio_devices.size());
537 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
539 // Verify the internal speaker has been selected as the active output.
540 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
541 AudioDevice active_output;
542 EXPECT_TRUE(
543 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
544 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
545 EXPECT_EQ(kInternalSpeaker.id,
546 cras_audio_handler_->GetPrimaryActiveOutputNode());
547 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
549 // Plug the headphone.
550 audio_nodes.clear();
551 AudioNode internal_speaker(kInternalSpeaker);
552 internal_speaker.active = true;
553 audio_nodes.push_back(internal_speaker);
554 audio_nodes.push_back(kHeadphone);
555 ChangeAudioNodes(audio_nodes);
557 // Verify the AudioNodesChanged event is fired and new audio device is added.
558 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
559 cras_audio_handler_->GetAudioDevices(&audio_devices);
560 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
562 // Verify the active output device is switched to headphone and
563 // ActiveOutputChanged event is fired.
564 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
565 EXPECT_TRUE(
566 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
567 EXPECT_EQ(kHeadphone.id, active_output.id);
568 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
569 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
572 TEST_F(CrasAudioHandlerTest, UnplugHeadphone) {
573 // Set up initial audio devices, with internal speaker and headphone.
574 AudioNodeList audio_nodes;
575 audio_nodes.push_back(kInternalSpeaker);
576 audio_nodes.push_back(kHeadphone);
577 SetUpCrasAudioHandler(audio_nodes);
578 const size_t init_nodes_size = audio_nodes.size();
580 // Verify the audio devices size.
581 AudioDeviceList audio_devices;
582 cras_audio_handler_->GetAudioDevices(&audio_devices);
583 EXPECT_EQ(init_nodes_size, audio_devices.size());
584 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
586 // Verify the headphone has been selected as the active output.
587 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
588 AudioDevice active_output;
589 EXPECT_TRUE(
590 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
591 EXPECT_EQ(kHeadphone.id, active_output.id);
592 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
593 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
595 // Unplug the headphone.
596 audio_nodes.clear();
597 audio_nodes.push_back(kInternalSpeaker);
598 ChangeAudioNodes(audio_nodes);
600 // Verify the AudioNodesChanged event is fired and one audio device is
601 // removed.
602 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
603 cras_audio_handler_->GetAudioDevices(&audio_devices);
604 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
606 // Verify the active output device is switched to internal speaker and
607 // ActiveOutputChanged event is fired.
608 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
609 EXPECT_TRUE(
610 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
611 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
612 EXPECT_EQ(kInternalSpeaker.id,
613 cras_audio_handler_->GetPrimaryActiveOutputNode());
614 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
617 TEST_F(CrasAudioHandlerTest, InitializeWithBluetoothHeadset) {
618 AudioNodeList audio_nodes;
619 audio_nodes.push_back(kInternalSpeaker);
620 audio_nodes.push_back(kBluetoothHeadset);
621 SetUpCrasAudioHandler(audio_nodes);
623 // Verify the audio devices size.
624 AudioDeviceList audio_devices;
625 cras_audio_handler_->GetAudioDevices(&audio_devices);
626 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
627 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
629 // Verify the bluetooth headset has been selected as the active output.
630 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
631 AudioDevice active_output;
632 EXPECT_TRUE(
633 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
634 EXPECT_EQ(kBluetoothHeadset.id, active_output.id);
635 EXPECT_EQ(kBluetoothHeadset.id,
636 cras_audio_handler_->GetPrimaryActiveOutputNode());
637 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
640 TEST_F(CrasAudioHandlerTest, ConnectAndDisconnectBluetoothHeadset) {
641 // Initialize with internal speaker and headphone.
642 AudioNodeList audio_nodes;
643 audio_nodes.push_back(kInternalSpeaker);
644 audio_nodes.push_back(kHeadphone);
645 SetUpCrasAudioHandler(audio_nodes);
646 const size_t init_nodes_size = audio_nodes.size();
648 // Verify the audio devices size.
649 AudioDeviceList audio_devices;
650 cras_audio_handler_->GetAudioDevices(&audio_devices);
651 EXPECT_EQ(init_nodes_size, audio_devices.size());
652 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
654 // Verify the headphone is selected as the active output initially.
655 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
656 AudioDevice active_output;
657 EXPECT_TRUE(
658 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
659 EXPECT_EQ(kHeadphone.id, active_output.id);
660 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
661 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
663 // Connect to bluetooth headset. Since it is plugged in later than
664 // headphone, active output should be switched to it.
665 audio_nodes.clear();
666 audio_nodes.push_back(kInternalSpeaker);
667 AudioNode headphone(kHeadphone);
668 headphone.plugged_time = 80000000;
669 headphone.active = true;
670 audio_nodes.push_back(headphone);
671 AudioNode bluetooth_headset(kBluetoothHeadset);
672 bluetooth_headset.plugged_time = 90000000;
673 audio_nodes.push_back(bluetooth_headset);
674 ChangeAudioNodes(audio_nodes);
676 // Verify the AudioNodesChanged event is fired and new audio device is added.
677 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
678 cras_audio_handler_->GetAudioDevices(&audio_devices);
679 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
681 // Verify the active output device is switched to bluetooth headset, and
682 // ActiveOutputChanged event is fired.
683 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
684 EXPECT_TRUE(
685 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
686 EXPECT_EQ(kBluetoothHeadset.id, active_output.id);
687 EXPECT_EQ(kBluetoothHeadset.id,
688 cras_audio_handler_->GetPrimaryActiveOutputNode());
689 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
691 // Disconnect bluetooth headset.
692 audio_nodes.clear();
693 audio_nodes.push_back(kInternalSpeaker);
694 headphone.active = false;
695 audio_nodes.push_back(headphone);
696 ChangeAudioNodes(audio_nodes);
698 // Verify the AudioNodesChanged event is fired and one audio device is
699 // removed.
700 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
701 cras_audio_handler_->GetAudioDevices(&audio_devices);
702 EXPECT_EQ(init_nodes_size, audio_devices.size());
704 // Verify the active output device is switched to headphone, and
705 // ActiveOutputChanged event is fired.
706 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
707 EXPECT_TRUE(
708 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
709 EXPECT_EQ(kHeadphone.id, active_output.id);
710 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
711 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
714 TEST_F(CrasAudioHandlerTest, InitializeWithHDMIOutput) {
715 AudioNodeList audio_nodes;
716 audio_nodes.push_back(kInternalSpeaker);
717 audio_nodes.push_back(kHDMIOutput);
718 SetUpCrasAudioHandler(audio_nodes);
720 // Verify the audio devices size.
721 AudioDeviceList audio_devices;
722 cras_audio_handler_->GetAudioDevices(&audio_devices);
723 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
724 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
726 // Verify the HDMI device has been selected as the active output.
727 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
728 AudioDevice active_output;
729 EXPECT_TRUE(
730 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
731 EXPECT_EQ(kHDMIOutput.id, active_output.id);
732 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
733 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
736 TEST_F(CrasAudioHandlerTest, ConnectAndDisconnectHDMIOutput) {
737 // Initialize with internal speaker.
738 AudioNodeList audio_nodes;
739 audio_nodes.push_back(kInternalSpeaker);
740 SetUpCrasAudioHandler(audio_nodes);
741 const size_t init_nodes_size = audio_nodes.size();
743 // Verify the audio devices size.
744 AudioDeviceList audio_devices;
745 cras_audio_handler_->GetAudioDevices(&audio_devices);
746 EXPECT_EQ(init_nodes_size, audio_devices.size());
747 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
749 // Verify the internal speaker is selected as the active output initially.
750 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
751 AudioDevice active_output;
752 EXPECT_TRUE(
753 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
754 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
755 EXPECT_EQ(kInternalSpeaker.id,
756 cras_audio_handler_->GetPrimaryActiveOutputNode());
757 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
759 // Connect to HDMI output.
760 audio_nodes.clear();
761 AudioNode internal_speaker(kInternalSpeaker);
762 internal_speaker.active = true;
763 internal_speaker.plugged_time = 80000000;
764 audio_nodes.push_back(internal_speaker);
765 AudioNode hdmi(kHDMIOutput);
766 hdmi.plugged_time = 90000000;
767 audio_nodes.push_back(hdmi);
768 ChangeAudioNodes(audio_nodes);
770 // Verify the AudioNodesChanged event is fired and new audio device is added.
771 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
772 cras_audio_handler_->GetAudioDevices(&audio_devices);
773 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
775 // Verify the active output device is switched to hdmi output, and
776 // ActiveOutputChanged event is fired.
777 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
778 EXPECT_TRUE(
779 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
780 EXPECT_EQ(kHDMIOutput.id, active_output.id);
781 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
782 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
784 // Disconnect hdmi headset.
785 audio_nodes.clear();
786 audio_nodes.push_back(kInternalSpeaker);
787 ChangeAudioNodes(audio_nodes);
789 // Verify the AudioNodesChanged event is fired and one audio device is
790 // removed.
791 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
792 cras_audio_handler_->GetAudioDevices(&audio_devices);
793 EXPECT_EQ(init_nodes_size, audio_devices.size());
795 // Verify the active output device is switched to internal speaker, and
796 // ActiveOutputChanged event is fired.
797 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
798 EXPECT_TRUE(
799 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
800 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
801 EXPECT_EQ(kInternalSpeaker.id,
802 cras_audio_handler_->GetPrimaryActiveOutputNode());
803 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
806 TEST_F(CrasAudioHandlerTest, HandleHeadphoneAndHDMIOutput) {
807 // Initialize with internal speaker, headphone and HDMI output.
808 AudioNodeList audio_nodes;
809 audio_nodes.push_back(kInternalSpeaker);
810 audio_nodes.push_back(kHeadphone);
811 audio_nodes.push_back(kHDMIOutput);
812 SetUpCrasAudioHandler(audio_nodes);
813 const size_t init_nodes_size = audio_nodes.size();
815 // Verify the audio devices size.
816 AudioDeviceList audio_devices;
817 cras_audio_handler_->GetAudioDevices(&audio_devices);
818 EXPECT_EQ(init_nodes_size, audio_devices.size());
819 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
821 // Verify the headphone is selected as the active output initially.
822 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
823 AudioDevice active_output;
824 EXPECT_TRUE(
825 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
826 EXPECT_EQ(kHeadphone.id, active_output.id);
827 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
828 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
830 // Disconnect HDMI output.
831 audio_nodes.clear();
832 audio_nodes.push_back(kInternalSpeaker);
833 audio_nodes.push_back(kHDMIOutput);
834 ChangeAudioNodes(audio_nodes);
836 // Verify the AudioNodesChanged event is fired and one audio device is
837 // removed.
838 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
839 cras_audio_handler_->GetAudioDevices(&audio_devices);
840 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
842 // Verify the active output device is switched to HDMI output, and
843 // ActiveOutputChanged event is fired.
844 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
845 EXPECT_TRUE(
846 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
847 EXPECT_EQ(kHDMIOutput.id, active_output.id);
848 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
849 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
852 TEST_F(CrasAudioHandlerTest, InitializeWithUSBHeadphone) {
853 AudioNodeList audio_nodes;
854 audio_nodes.push_back(kInternalSpeaker);
855 audio_nodes.push_back(kUSBHeadphone1);
856 SetUpCrasAudioHandler(audio_nodes);
858 // Verify the audio devices size.
859 AudioDeviceList audio_devices;
860 cras_audio_handler_->GetAudioDevices(&audio_devices);
861 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
862 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
864 // Verify the usb headphone has been selected as the active output.
865 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
866 AudioDevice active_output;
867 EXPECT_TRUE(
868 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
869 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
870 EXPECT_EQ(kUSBHeadphone1.id,
871 cras_audio_handler_->GetPrimaryActiveOutputNode());
872 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
875 TEST_F(CrasAudioHandlerTest, PlugAndUnplugUSBHeadphone) {
876 // Initialize with internal speaker.
877 AudioNodeList audio_nodes;
878 audio_nodes.push_back(kInternalSpeaker);
879 SetUpCrasAudioHandler(audio_nodes);
880 const size_t init_nodes_size = audio_nodes.size();
882 // Verify the audio devices size.
883 AudioDeviceList audio_devices;
884 cras_audio_handler_->GetAudioDevices(&audio_devices);
885 EXPECT_EQ(init_nodes_size, audio_devices.size());
886 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
888 // Verify the internal speaker is selected as the active output initially.
889 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
890 AudioDevice active_output;
891 EXPECT_TRUE(
892 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
893 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
894 EXPECT_EQ(kInternalSpeaker.id,
895 cras_audio_handler_->GetPrimaryActiveOutputNode());
896 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
898 // Plug in usb headphone
899 audio_nodes.clear();
900 AudioNode internal_speaker(kInternalSpeaker);
901 internal_speaker.active = true;
902 internal_speaker.plugged_time = 80000000;
903 audio_nodes.push_back(internal_speaker);
904 AudioNode usb_headphone(kUSBHeadphone1);
905 usb_headphone.plugged_time = 90000000;
906 audio_nodes.push_back(usb_headphone);
907 ChangeAudioNodes(audio_nodes);
909 // Verify the AudioNodesChanged event is fired and new audio device is added.
910 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
911 cras_audio_handler_->GetAudioDevices(&audio_devices);
912 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
914 // Verify the active output device is switched to usb headphone, and
915 // ActiveOutputChanged event is fired.
916 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
917 EXPECT_TRUE(
918 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
919 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
920 EXPECT_EQ(kUSBHeadphone1.id,
921 cras_audio_handler_->GetPrimaryActiveOutputNode());
922 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
924 // Unplug usb headphone.
925 audio_nodes.clear();
926 audio_nodes.push_back(kInternalSpeaker);
927 ChangeAudioNodes(audio_nodes);
929 // Verify the AudioNodesChanged event is fired and one audio device is
930 // removed.
931 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
932 cras_audio_handler_->GetAudioDevices(&audio_devices);
933 EXPECT_EQ(init_nodes_size, audio_devices.size());
935 // Verify the active output device is switched to internal speaker, and
936 // ActiveOutputChanged event is fired.
937 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
938 EXPECT_TRUE(
939 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
940 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
941 EXPECT_EQ(kInternalSpeaker.id,
942 cras_audio_handler_->GetPrimaryActiveOutputNode());
943 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
946 TEST_F(CrasAudioHandlerTest, HandleMultipleUSBHeadphones) {
947 // Initialize with internal speaker and one usb headphone.
948 AudioNodeList audio_nodes;
949 audio_nodes.push_back(kInternalSpeaker);
950 audio_nodes.push_back(kUSBHeadphone1);
951 SetUpCrasAudioHandler(audio_nodes);
952 const size_t init_nodes_size = audio_nodes.size();
954 // Verify the audio devices size.
955 AudioDeviceList audio_devices;
956 cras_audio_handler_->GetAudioDevices(&audio_devices);
957 EXPECT_EQ(init_nodes_size, audio_devices.size());
958 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
960 // Verify the usb headphone is selected as the active output initially.
961 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
962 AudioDevice active_output;
963 EXPECT_TRUE(
964 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
965 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
966 EXPECT_EQ(kUSBHeadphone1.id,
967 cras_audio_handler_->GetPrimaryActiveOutputNode());
968 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
970 // Plug in another usb headphone.
971 audio_nodes.clear();
972 audio_nodes.push_back(kInternalSpeaker);
973 AudioNode usb_headphone_1(kUSBHeadphone1);
974 usb_headphone_1.active = true;
975 usb_headphone_1.plugged_time = 80000000;
976 audio_nodes.push_back(usb_headphone_1);
977 AudioNode usb_headphone_2(kUSBHeadphone2);
978 usb_headphone_2.plugged_time = 90000000;
979 audio_nodes.push_back(usb_headphone_2);
980 ChangeAudioNodes(audio_nodes);
982 // Verify the AudioNodesChanged event is fired and new audio device is added.
983 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
984 cras_audio_handler_->GetAudioDevices(&audio_devices);
985 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
987 // Verify the active output device is switched to the 2nd usb headphone, which
988 // is plugged later, and ActiveOutputChanged event is fired.
989 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
990 EXPECT_TRUE(
991 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
992 EXPECT_EQ(kUSBHeadphone2.id, active_output.id);
993 EXPECT_EQ(kUSBHeadphone2.id,
994 cras_audio_handler_->GetPrimaryActiveOutputNode());
995 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
997 // Unplug the 2nd usb headphone.
998 audio_nodes.clear();
999 audio_nodes.push_back(kInternalSpeaker);
1000 audio_nodes.push_back(kUSBHeadphone1);
1001 ChangeAudioNodes(audio_nodes);
1003 // Verify the AudioNodesChanged event is fired and one audio device is
1004 // removed.
1005 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1006 cras_audio_handler_->GetAudioDevices(&audio_devices);
1007 EXPECT_EQ(init_nodes_size, audio_devices.size());
1009 // Verify the active output device is switched to the first usb headphone, and
1010 // ActiveOutputChanged event is fired.
1011 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
1012 EXPECT_TRUE(
1013 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1014 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
1015 EXPECT_EQ(kUSBHeadphone1.id,
1016 cras_audio_handler_->GetPrimaryActiveOutputNode());
1017 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1020 TEST_F(CrasAudioHandlerTest, UnplugUSBHeadphonesWithActiveSpeaker) {
1021 // Initialize with internal speaker and one usb headphone.
1022 AudioNodeList audio_nodes;
1023 audio_nodes.push_back(kInternalSpeaker);
1024 audio_nodes.push_back(kUSBHeadphone1);
1025 SetUpCrasAudioHandler(audio_nodes);
1026 const size_t init_nodes_size = audio_nodes.size();
1028 // Verify the audio devices size.
1029 AudioDeviceList audio_devices;
1030 cras_audio_handler_->GetAudioDevices(&audio_devices);
1031 EXPECT_EQ(init_nodes_size, audio_devices.size());
1032 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1034 // Verify the usb headphone is selected as the active output initially.
1035 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1036 AudioDevice active_output;
1037 EXPECT_TRUE(
1038 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1039 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
1040 EXPECT_EQ(kUSBHeadphone1.id,
1041 cras_audio_handler_->GetPrimaryActiveOutputNode());
1042 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1044 // Plug in the headphone jack.
1045 audio_nodes.clear();
1046 audio_nodes.push_back(kInternalSpeaker);
1047 AudioNode usb_headphone_1(kUSBHeadphone1);
1048 usb_headphone_1.active = true;
1049 usb_headphone_1.plugged_time = 80000000;
1050 audio_nodes.push_back(usb_headphone_1);
1051 AudioNode headphone_jack(kHeadphone);
1052 headphone_jack.plugged_time = 90000000;
1053 audio_nodes.push_back(headphone_jack);
1054 ChangeAudioNodes(audio_nodes);
1056 // Verify the AudioNodesChanged event is fired and new audio device is added.
1057 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1058 cras_audio_handler_->GetAudioDevices(&audio_devices);
1059 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1061 // Verify the active output device is switched to the headphone jack, which
1062 // is plugged later, and ActiveOutputChanged event is fired.
1063 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1064 EXPECT_TRUE(
1065 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1066 EXPECT_EQ(kHeadphone.id, active_output.id);
1067 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1068 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1070 // Select the speaker to be the active output device.
1071 AudioDevice internal_speaker(kInternalSpeaker);
1072 cras_audio_handler_->SwitchToDevice(internal_speaker, true);
1074 // Verify the active output is switched to internal speaker, and the
1075 // ActiveOutputNodeChanged event is fired.
1076 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
1077 EXPECT_TRUE(
1078 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1079 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
1080 EXPECT_EQ(kInternalSpeaker.id,
1081 cras_audio_handler_->GetPrimaryActiveOutputNode());
1083 // Unplug the usb headphone.
1084 audio_nodes.clear();
1085 AudioNode internal_speaker_node(kInternalSpeaker);
1086 internal_speaker_node.active = true;
1087 internal_speaker_node.plugged_time = 70000000;
1088 audio_nodes.push_back(internal_speaker_node);
1089 headphone_jack.active = false;
1090 audio_nodes.push_back(headphone_jack);
1091 ChangeAudioNodes(audio_nodes);
1093 // Verify the AudioNodesChanged event is fired and one audio device is
1094 // removed.
1095 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1096 cras_audio_handler_->GetAudioDevices(&audio_devices);
1097 EXPECT_EQ(init_nodes_size, audio_devices.size());
1099 // Verify the active output device remains to be speaker.
1100 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
1101 EXPECT_TRUE(
1102 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1103 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
1104 EXPECT_EQ(kInternalSpeaker.id,
1105 cras_audio_handler_->GetPrimaryActiveOutputNode());
1106 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1109 TEST_F(CrasAudioHandlerTest, OneActiveAudioOutputAfterLoginNewUserSession) {
1110 // This tests the case found with crbug.com/273271.
1111 // Initialize with internal speaker, bluetooth headphone and headphone jack
1112 // for a new chrome session after user signs out from the previous session.
1113 // Headphone jack is plugged in later than bluetooth headphone, but bluetooth
1114 // headphone is selected as the active output by user from previous user
1115 // session.
1116 AudioNodeList audio_nodes;
1117 audio_nodes.push_back(kInternalSpeaker);
1118 AudioNode bluetooth_headphone(kBluetoothHeadset);
1119 bluetooth_headphone.active = true;
1120 bluetooth_headphone.plugged_time = 70000000;
1121 audio_nodes.push_back(bluetooth_headphone);
1122 AudioNode headphone_jack(kHeadphone);
1123 headphone_jack.plugged_time = 80000000;
1124 audio_nodes.push_back(headphone_jack);
1125 SetUpCrasAudioHandlerWithPrimaryActiveNode(audio_nodes, bluetooth_headphone);
1126 const size_t init_nodes_size = audio_nodes.size();
1128 // Verify the audio devices size.
1129 AudioDeviceList audio_devices;
1130 cras_audio_handler_->GetAudioDevices(&audio_devices);
1131 EXPECT_EQ(init_nodes_size, audio_devices.size());
1132 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1134 // Verify the headphone jack is selected as the active output and all other
1135 // audio devices are not active.
1136 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1137 AudioDevice active_output;
1138 EXPECT_TRUE(
1139 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1140 EXPECT_EQ(kHeadphone.id, active_output.id);
1141 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1142 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1143 for (size_t i = 0; i < audio_devices.size(); ++i) {
1144 if (audio_devices[i].id != kHeadphone.id)
1145 EXPECT_FALSE(audio_devices[i].active);
1149 TEST_F(CrasAudioHandlerTest, BluetoothSpeakerIdChangedOnFly) {
1150 // Initialize with internal speaker and bluetooth headset.
1151 AudioNodeList audio_nodes;
1152 audio_nodes.push_back(kInternalSpeaker);
1153 audio_nodes.push_back(kBluetoothHeadset);
1154 SetUpCrasAudioHandler(audio_nodes);
1155 const size_t init_nodes_size = audio_nodes.size();
1157 // Verify the audio devices size.
1158 AudioDeviceList audio_devices;
1159 cras_audio_handler_->GetAudioDevices(&audio_devices);
1160 EXPECT_EQ(init_nodes_size, audio_devices.size());
1161 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1163 // Verify the bluetooth headset is selected as the active output and all other
1164 // audio devices are not active.
1165 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1166 AudioDevice active_output;
1167 EXPECT_TRUE(
1168 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1169 EXPECT_EQ(kBluetoothHeadset.id, active_output.id);
1170 EXPECT_EQ(kBluetoothHeadset.id,
1171 cras_audio_handler_->GetPrimaryActiveOutputNode());
1172 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1174 // Cras changes the bluetooth headset's id on the fly.
1175 audio_nodes.clear();
1176 AudioNode internal_speaker(kInternalSpeaker);
1177 internal_speaker.active = false;
1178 audio_nodes.push_back(internal_speaker);
1179 AudioNode bluetooth_headphone(kBluetoothHeadset);
1180 // Change bluetooth headphone id.
1181 bluetooth_headphone.id = kBluetoothHeadsetId + 20000;
1182 bluetooth_headphone.active = false;
1183 audio_nodes.push_back(bluetooth_headphone);
1184 ChangeAudioNodes(audio_nodes);
1186 // Verify NodesChanged event is fired, and the audio devices size is not
1187 // changed.
1188 audio_devices.clear();
1189 cras_audio_handler_->GetAudioDevices(&audio_devices);
1190 EXPECT_EQ(init_nodes_size, audio_devices.size());
1191 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1193 // Verify ActiveOutputNodeChanged event is fired, and active device should be
1194 // bluetooth headphone.
1195 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1196 EXPECT_TRUE(
1197 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1198 EXPECT_EQ(bluetooth_headphone.id, active_output.id);
1201 TEST_F(CrasAudioHandlerTest, PlugUSBMic) {
1202 // Set up initial audio devices, only with internal mic.
1203 AudioNodeList audio_nodes;
1204 audio_nodes.push_back(kInternalMic);
1205 SetUpCrasAudioHandler(audio_nodes);
1206 const size_t init_nodes_size = audio_nodes.size();
1208 // Verify the audio devices size.
1209 AudioDeviceList audio_devices;
1210 cras_audio_handler_->GetAudioDevices(&audio_devices);
1211 EXPECT_EQ(init_nodes_size, audio_devices.size());
1212 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1214 // Verify the internal mic is selected as the active input.
1215 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1216 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1217 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1219 // Plug the USB Mic.
1220 audio_nodes.clear();
1221 AudioNode internal_mic(kInternalMic);
1222 internal_mic.active = true;
1223 audio_nodes.push_back(internal_mic);
1224 audio_nodes.push_back(kUSBMic);
1225 ChangeAudioNodes(audio_nodes);
1227 // Verify the AudioNodesChanged event is fired and new audio device is added.
1228 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1229 cras_audio_handler_->GetAudioDevices(&audio_devices);
1230 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1232 // Verify the active input device is switched to USB mic and
1233 // and ActiveInputChanged event is fired.
1234 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
1235 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
1236 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1239 TEST_F(CrasAudioHandlerTest, UnplugUSBMic) {
1240 // Set up initial audio devices, with internal mic and USB Mic.
1241 AudioNodeList audio_nodes;
1242 audio_nodes.push_back(kInternalMic);
1243 audio_nodes.push_back(kUSBMic);
1244 SetUpCrasAudioHandler(audio_nodes);
1245 const size_t init_nodes_size = audio_nodes.size();
1247 // Verify the audio devices size.
1248 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1249 AudioDeviceList audio_devices;
1250 cras_audio_handler_->GetAudioDevices(&audio_devices);
1251 EXPECT_EQ(init_nodes_size, audio_devices.size());
1253 // Verify the USB mic is selected as the active output.
1254 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1255 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
1256 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1258 // Unplug the USB Mic.
1259 audio_nodes.clear();
1260 audio_nodes.push_back(kInternalMic);
1261 ChangeAudioNodes(audio_nodes);
1263 // Verify the AudioNodesChanged event is fired, and one audio device is
1264 // removed.
1265 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1266 cras_audio_handler_->GetAudioDevices(&audio_devices);
1267 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
1269 // Verify the active input device is switched to internal mic, and
1270 // and ActiveInputChanged event is fired.
1271 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
1272 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1273 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1276 TEST_F(CrasAudioHandlerTest, PlugUSBMicNotAffectActiveOutput) {
1277 // Set up initial audio devices.
1278 AudioNodeList audio_nodes;
1279 audio_nodes.push_back(kInternalSpeaker);
1280 audio_nodes.push_back(kHeadphone);
1281 audio_nodes.push_back(kInternalMic);
1282 SetUpCrasAudioHandler(audio_nodes);
1283 const size_t init_nodes_size = audio_nodes.size();
1285 // Verify the audio devices size.
1286 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1287 AudioDeviceList audio_devices;
1288 cras_audio_handler_->GetAudioDevices(&audio_devices);
1289 EXPECT_EQ(init_nodes_size, audio_devices.size());
1291 // Verify the internal mic is selected as the active input.
1292 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1293 EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
1294 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1296 // Verify the headphone is selected as the active output.
1297 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1298 EXPECT_EQ(kHeadphoneId, cras_audio_handler_->GetPrimaryActiveOutputNode());
1299 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1301 // Switch the active output to internal speaker.
1302 AudioDevice internal_speaker(kInternalSpeaker);
1303 cras_audio_handler_->SwitchToDevice(internal_speaker, true);
1305 // Verify the active output is switched to internal speaker, and the
1306 // ActiveOutputNodeChanged event is fired.
1307 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1308 AudioDevice active_output;
1309 EXPECT_TRUE(
1310 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1311 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
1312 EXPECT_EQ(kInternalSpeaker.id,
1313 cras_audio_handler_->GetPrimaryActiveOutputNode());
1315 // Plug the USB Mic.
1316 audio_nodes.clear();
1317 AudioNode internal_speaker_node(kInternalSpeaker);
1318 internal_speaker_node.active = true;
1319 audio_nodes.push_back(internal_speaker_node);
1320 audio_nodes.push_back(kHeadphone);
1321 AudioNode internal_mic(kInternalMic);
1322 internal_mic.active = true;
1323 audio_nodes.push_back(internal_mic);
1324 audio_nodes.push_back(kUSBMic);
1325 ChangeAudioNodes(audio_nodes);
1327 // Verify the AudioNodesChanged event is fired, one new device is added.
1328 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1329 cras_audio_handler_->GetAudioDevices(&audio_devices);
1330 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1332 // Verify the active input device is switched to USB mic, and
1333 // and ActiveInputChanged event is fired.
1334 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
1335 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1336 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1338 // Verify the active output device is not changed.
1339 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1340 EXPECT_TRUE(
1341 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1342 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
1343 EXPECT_EQ(kInternalSpeaker.id,
1344 cras_audio_handler_->GetPrimaryActiveOutputNode());
1347 TEST_F(CrasAudioHandlerTest, PlugHeadphoneAutoUnplugSpeakerWithActiveUSB) {
1348 // Set up initial audio devices.
1349 AudioNodeList audio_nodes;
1350 audio_nodes.push_back(kUSBHeadphone1);
1351 audio_nodes.push_back(kInternalSpeaker);
1352 audio_nodes.push_back(kInternalMic);
1353 SetUpCrasAudioHandler(audio_nodes);
1354 const size_t init_nodes_size = audio_nodes.size();
1356 // Verify the audio devices size.
1357 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1358 AudioDeviceList audio_devices;
1359 cras_audio_handler_->GetAudioDevices(&audio_devices);
1360 EXPECT_EQ(init_nodes_size, audio_devices.size());
1362 // Verify the internal mic is selected as the active input.
1363 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1364 EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
1365 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1367 // Verify the USB headphone is selected as the active output.
1368 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1369 EXPECT_EQ(kUSBHeadphoneId1,
1370 cras_audio_handler_->GetPrimaryActiveOutputNode());
1371 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1373 // Plug the headphone and auto-unplug internal speaker.
1374 audio_nodes.clear();
1375 AudioNode usb_headphone_node(kUSBHeadphone1);
1376 usb_headphone_node.active = true;
1377 audio_nodes.push_back(usb_headphone_node);
1378 AudioNode headphone_node(kHeadphone);
1379 headphone_node.plugged_time = 1000;
1380 audio_nodes.push_back(headphone_node);
1381 AudioNode internal_mic(kInternalMic);
1382 internal_mic.active = true;
1383 audio_nodes.push_back(internal_mic);
1384 ChangeAudioNodes(audio_nodes);
1386 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1387 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1388 cras_audio_handler_->GetAudioDevices(&audio_devices);
1389 EXPECT_EQ(init_nodes_size, audio_devices.size());
1391 // Verify the active output device is switched to headphone, and
1392 // an ActiveOutputChanged event is fired.
1393 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1394 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1395 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1397 // Unplug the headphone and internal speaker auto-plugs back.
1398 audio_nodes.clear();
1399 audio_nodes.push_back(kUSBHeadphone1);
1400 AudioNode internal_speaker_node(kInternalSpeaker);
1401 internal_speaker_node.plugged_time = 2000;
1402 audio_nodes.push_back(internal_speaker_node);
1403 audio_nodes.push_back(internal_mic);
1404 ChangeAudioNodes(audio_nodes);
1406 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1407 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1408 cras_audio_handler_->GetAudioDevices(&audio_devices);
1409 EXPECT_EQ(init_nodes_size, audio_devices.size());
1411 // Verify the active output device is switched back to USB, and
1412 // an ActiveOutputChanged event is fired.
1413 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
1414 EXPECT_EQ(kUSBHeadphone1.id,
1415 cras_audio_handler_->GetPrimaryActiveOutputNode());
1416 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1418 // Verify the active input device is not changed.
1419 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1420 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1423 TEST_F(CrasAudioHandlerTest, PlugMicAutoUnplugInternalMicWithActiveUSB) {
1424 // Set up initial audio devices.
1425 AudioNodeList audio_nodes;
1426 audio_nodes.push_back(kUSBHeadphone1);
1427 audio_nodes.push_back(kInternalSpeaker);
1428 audio_nodes.push_back(kUSBMic);
1429 audio_nodes.push_back(kInternalMic);
1430 SetUpCrasAudioHandler(audio_nodes);
1431 const size_t init_nodes_size = audio_nodes.size();
1433 // Verify the audio devices size.
1434 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1435 AudioDeviceList audio_devices;
1436 cras_audio_handler_->GetAudioDevices(&audio_devices);
1437 EXPECT_EQ(init_nodes_size, audio_devices.size());
1439 // Verify the internal mic is selected as the active input.
1440 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1441 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
1442 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1444 // Verify the internal speaker is selected as the active output.
1445 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1446 EXPECT_EQ(kUSBHeadphoneId1,
1447 cras_audio_handler_->GetPrimaryActiveOutputNode());
1448 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1450 // Plug the headphone and mic, auto-unplug internal mic and speaker.
1451 audio_nodes.clear();
1452 AudioNode usb_headphone_node(kUSBHeadphone1);
1453 usb_headphone_node.active = true;
1454 audio_nodes.push_back(usb_headphone_node);
1455 AudioNode headphone_node(kHeadphone);
1456 headphone_node.plugged_time = 1000;
1457 audio_nodes.push_back(headphone_node);
1458 AudioNode usb_mic(kUSBMic);
1459 usb_mic.active = true;
1460 audio_nodes.push_back(usb_mic);
1461 AudioNode mic_jack(kMicJack);
1462 mic_jack.plugged_time = 1000;
1463 audio_nodes.push_back(mic_jack);
1464 ChangeAudioNodes(audio_nodes);
1466 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1467 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1468 cras_audio_handler_->GetAudioDevices(&audio_devices);
1469 EXPECT_EQ(init_nodes_size, audio_devices.size());
1471 // Verify the active output device is switched to headphone, and
1472 // an ActiveOutputChanged event is fired.
1473 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1474 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1475 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1477 // Verify the active input device is switched to mic jack, and
1478 // an ActiveInputChanged event is fired.
1479 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
1480 EXPECT_EQ(kMicJack.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1481 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1483 // Unplug the headphone and internal speaker auto-plugs back.
1484 audio_nodes.clear();
1485 audio_nodes.push_back(kUSBHeadphone1);
1486 AudioNode internal_speaker_node(kInternalSpeaker);
1487 internal_speaker_node.plugged_time = 2000;
1488 audio_nodes.push_back(internal_speaker_node);
1489 audio_nodes.push_back(kUSBMic);
1490 AudioNode internal_mic(kInternalMic);
1491 internal_mic.plugged_time = 2000;
1492 audio_nodes.push_back(internal_mic);
1493 ChangeAudioNodes(audio_nodes);
1495 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1496 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1497 cras_audio_handler_->GetAudioDevices(&audio_devices);
1498 EXPECT_EQ(init_nodes_size, audio_devices.size());
1500 // Verify the active output device is switched back to USB, and
1501 // an ActiveOutputChanged event is fired.
1502 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
1503 EXPECT_EQ(kUSBHeadphone1.id,
1504 cras_audio_handler_->GetPrimaryActiveOutputNode());
1505 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1507 // Verify the active input device is switched back to USB mic, and
1508 // an ActiveInputChanged event is fired.
1509 EXPECT_EQ(2, test_observer_->active_input_node_changed_count());
1510 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1511 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1514 TEST_F(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnPlugInHeadphone) {
1515 // Set up initial audio devices.
1516 AudioNodeList audio_nodes;
1517 audio_nodes.push_back(kInternalSpeaker);
1518 audio_nodes.push_back(kBluetoothHeadset);
1519 SetUpCrasAudioHandler(audio_nodes);
1520 const size_t init_nodes_size = audio_nodes.size();
1522 // Verify the audio devices size.
1523 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1524 AudioDeviceList audio_devices;
1525 cras_audio_handler_->GetAudioDevices(&audio_devices);
1526 EXPECT_EQ(init_nodes_size, audio_devices.size());
1528 // Verify the bluetooth headset is selected as the active output.
1529 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1530 EXPECT_EQ(kBluetoothHeadsetId,
1531 cras_audio_handler_->GetPrimaryActiveOutputNode());
1532 AudioDevice active_output;
1533 EXPECT_TRUE(
1534 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1535 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1537 // Plug in headphone, but fire NodesChanged signal twice.
1538 audio_nodes.clear();
1539 audio_nodes.push_back(kInternalSpeaker);
1540 AudioNode bluetooth_headset(kBluetoothHeadset);
1541 bluetooth_headset.plugged_time = 1000;
1542 bluetooth_headset.active = true;
1543 audio_nodes.push_back(bluetooth_headset);
1544 AudioNode headphone(kHeadphone);
1545 headphone.active = false;
1546 headphone.plugged_time = 2000;
1547 audio_nodes.push_back(headphone);
1548 ChangeAudioNodes(audio_nodes);
1549 ChangeAudioNodes(audio_nodes);
1551 // Verify the active output device is set to headphone.
1552 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1553 EXPECT_LE(1, test_observer_->active_output_node_changed_count());
1554 EXPECT_EQ(headphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1555 EXPECT_TRUE(
1556 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1557 EXPECT_EQ(headphone.id, active_output.id);
1559 // Verfiy the audio devices data is consistent, i.e., the active output device
1560 // should be headphone.
1561 cras_audio_handler_->GetAudioDevices(&audio_devices);
1562 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1563 for (size_t i = 0; i < audio_devices.size(); ++i) {
1564 if (audio_devices[i].id == kInternalSpeaker.id)
1565 EXPECT_FALSE(audio_devices[i].active);
1566 else if (audio_devices[i].id == bluetooth_headset.id)
1567 EXPECT_FALSE(audio_devices[i].active);
1568 else if (audio_devices[i].id == headphone.id)
1569 EXPECT_TRUE(audio_devices[i].active);
1570 else
1571 NOTREACHED();
1575 TEST_F(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnPlugInUSBMic) {
1576 // Set up initial audio devices.
1577 AudioNodeList audio_nodes;
1578 audio_nodes.push_back(kInternalMic);
1579 SetUpCrasAudioHandler(audio_nodes);
1580 const size_t init_nodes_size = audio_nodes.size();
1582 // Verify the audio devices size.
1583 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1584 AudioDeviceList audio_devices;
1585 cras_audio_handler_->GetAudioDevices(&audio_devices);
1586 EXPECT_EQ(init_nodes_size, audio_devices.size());
1588 // Verify the internal mic is selected as the active output.
1589 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1590 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1591 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
1592 EXPECT_TRUE(audio_devices[0].active);
1594 // Plug in usb mic, but fire NodesChanged signal twice.
1595 audio_nodes.clear();
1596 AudioNode internal_mic(kInternalMic);
1597 internal_mic.active = true;
1598 internal_mic.plugged_time = 1000;
1599 audio_nodes.push_back(internal_mic);
1600 AudioNode usb_mic(kUSBMic);
1601 usb_mic.active = false;
1602 usb_mic.plugged_time = 2000;
1603 audio_nodes.push_back(usb_mic);
1604 ChangeAudioNodes(audio_nodes);
1605 ChangeAudioNodes(audio_nodes);
1607 // Verify the active output device is set to headphone.
1608 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1609 EXPECT_LE(1, test_observer_->active_input_node_changed_count());
1610 EXPECT_EQ(usb_mic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1611 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1613 // Verfiy the audio devices data is consistent, i.e., the active input device
1614 // should be usb mic.
1615 cras_audio_handler_->GetAudioDevices(&audio_devices);
1616 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1617 for (size_t i = 0; i < audio_devices.size(); ++i) {
1618 if (audio_devices[i].id == kInternalMic.id)
1619 EXPECT_FALSE(audio_devices[i].active);
1620 else if (audio_devices[i].id == usb_mic.id)
1621 EXPECT_TRUE(audio_devices[i].active);
1622 else
1623 NOTREACHED();
1627 // This is the case of crbug.com/291303.
1628 TEST_F(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnSystemBoot) {
1629 // Set up audio handler with empty audio_nodes.
1630 AudioNodeList audio_nodes;
1631 SetUpCrasAudioHandler(audio_nodes);
1633 AudioNode internal_speaker(kInternalSpeaker);
1634 internal_speaker.active = false;
1635 AudioNode headphone(kHeadphone);
1636 headphone.active = false;
1637 AudioNode internal_mic(kInternalMic);
1638 internal_mic.active = false;
1639 audio_nodes.push_back(internal_speaker);
1640 audio_nodes.push_back(headphone);
1641 audio_nodes.push_back(internal_mic);
1642 const size_t init_nodes_size = audio_nodes.size();
1644 // Simulate AudioNodesChanged signal being fired twice during system boot.
1645 ChangeAudioNodes(audio_nodes);
1646 ChangeAudioNodes(audio_nodes);
1648 // Verify the active output device is set to headphone.
1649 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1650 EXPECT_LE(1, test_observer_->active_output_node_changed_count());
1651 EXPECT_EQ(headphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1652 AudioDevice active_output;
1653 EXPECT_TRUE(
1654 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1655 EXPECT_EQ(headphone.id, active_output.id);
1657 // Verify the active input device id is set to internal mic.
1658 EXPECT_EQ(internal_mic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1660 // Verfiy the audio devices data is consistent, i.e., the active output device
1661 // should be headphone, and the active input device should internal mic.
1662 AudioDeviceList audio_devices;
1663 cras_audio_handler_->GetAudioDevices(&audio_devices);
1664 EXPECT_EQ(init_nodes_size, audio_devices.size());
1665 for (size_t i = 0; i < audio_devices.size(); ++i) {
1666 if (audio_devices[i].id == internal_speaker.id)
1667 EXPECT_FALSE(audio_devices[i].active);
1668 else if (audio_devices[i].id == headphone.id)
1669 EXPECT_TRUE(audio_devices[i].active);
1670 else if (audio_devices[i].id == internal_mic.id)
1671 EXPECT_TRUE(audio_devices[i].active);
1672 else
1673 NOTREACHED();
1677 // This is the case of crbug.com/448924.
1678 TEST_F(CrasAudioHandlerTest,
1679 TwoNodesChangedSignalsForLosingTowNodesOnOneUnplug) {
1680 // Set up audio handler with 4 audio_nodes.
1681 AudioNodeList audio_nodes;
1682 AudioNode internal_speaker(kInternalSpeaker);
1683 internal_speaker.active = false;
1684 AudioNode headphone(kHeadphone);
1685 headphone.active = false;
1686 AudioNode internal_mic(kInternalMic);
1687 internal_mic.active = false;
1688 AudioNode micJack(kMicJack);
1689 micJack.active = false;
1690 audio_nodes.push_back(internal_speaker);
1691 audio_nodes.push_back(headphone);
1692 audio_nodes.push_back(internal_mic);
1693 audio_nodes.push_back(micJack);
1694 SetUpCrasAudioHandler(audio_nodes);
1696 // Verify the audio devices size.
1697 AudioDeviceList audio_devices;
1698 cras_audio_handler_->GetAudioDevices(&audio_devices);
1699 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
1701 // Verify the headphone has been selected as the active output.
1702 AudioDevice active_output;
1703 EXPECT_TRUE(
1704 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1705 EXPECT_EQ(kHeadphone.id, active_output.id);
1706 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1707 EXPECT_TRUE(active_output.active);
1708 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1710 // Verify the mic Jack has been selected as the active input.
1711 EXPECT_EQ(micJack.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1712 const AudioDevice* active_input = GetDeviceFromId(micJack.id);
1713 EXPECT_TRUE(active_input->active);
1714 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1716 // Simulate the nodes list in first NodesChanged signal, only headphone is
1717 // removed, other nodes remains the same.
1718 AudioNodeList changed_nodes_1;
1719 internal_speaker.active = false;
1720 changed_nodes_1.push_back(internal_speaker);
1721 internal_mic.active = false;
1722 changed_nodes_1.push_back(internal_mic);
1723 micJack.active = true;
1724 changed_nodes_1.push_back(micJack);
1726 // Simulate the nodes list in second NodesChanged signal, the micJac is
1727 // removed, but the internal_mic is inactive, which does not reflect the
1728 // active status set from the first NodesChanged signal since this was sent
1729 // before cras receives the SetActiveOutputNode from the first NodesChanged
1730 // handling.
1731 AudioNodeList changed_nodes_2;
1732 changed_nodes_2.push_back(internal_speaker);
1733 changed_nodes_2.push_back(internal_mic);
1735 // Simulate AudioNodesChanged signal being fired twice for unplug an audio
1736 // device with both input and output nodes on it.
1737 ChangeAudioNodes(changed_nodes_1);
1738 ChangeAudioNodes(changed_nodes_2);
1740 AudioDeviceList changed_devices;
1741 cras_audio_handler_->GetAudioDevices(&changed_devices);
1742 EXPECT_EQ(2u, changed_devices.size());
1744 // Verify the active output device is set to internal speaker.
1745 EXPECT_EQ(internal_speaker.id,
1746 cras_audio_handler_->GetPrimaryActiveOutputNode());
1747 EXPECT_TRUE(
1748 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1749 EXPECT_EQ(internal_speaker.id, active_output.id);
1750 EXPECT_TRUE(active_output.active);
1752 // Verify the active input device id is set to internal mic.
1753 EXPECT_EQ(internal_mic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1754 const AudioDevice* changed_active_input = GetDeviceFromId(internal_mic.id);
1755 EXPECT_TRUE(changed_active_input->active);
1758 TEST_F(CrasAudioHandlerTest, SetOutputMute) {
1759 AudioNodeList audio_nodes;
1760 audio_nodes.push_back(kInternalSpeaker);
1761 SetUpCrasAudioHandler(audio_nodes);
1762 EXPECT_EQ(0, test_observer_->output_mute_changed_count());
1764 // Mute the device.
1765 cras_audio_handler_->SetOutputMute(true);
1767 // Verify the output is muted, OnOutputMuteChanged event is fired,
1768 // and mute value is saved in the preferences.
1769 EXPECT_TRUE(cras_audio_handler_->IsOutputMuted());
1770 EXPECT_EQ(1, test_observer_->output_mute_changed_count());
1771 AudioDevice speaker(kInternalSpeaker);
1772 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(speaker));
1774 // Unmute the device.
1775 cras_audio_handler_->SetOutputMute(false);
1777 // Verify the output is unmuted, OnOutputMuteChanged event is fired,
1778 // and mute value is saved in the preferences.
1779 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
1780 EXPECT_EQ(2, test_observer_->output_mute_changed_count());
1781 EXPECT_FALSE(audio_pref_handler_->GetMuteValue(speaker));
1784 TEST_F(CrasAudioHandlerTest, SetInputMute) {
1785 AudioNodeList audio_nodes;
1786 audio_nodes.push_back(kInternalMic);
1787 SetUpCrasAudioHandler(audio_nodes);
1788 EXPECT_EQ(0, test_observer_->input_mute_changed_count());
1790 // Mute the device.
1791 cras_audio_handler_->SetInputMute(true);
1793 // Verify the input is muted, OnInputMuteChanged event is fired.
1794 EXPECT_TRUE(cras_audio_handler_->IsInputMuted());
1795 EXPECT_EQ(1, test_observer_->input_mute_changed_count());
1797 // Unmute the device.
1798 cras_audio_handler_->SetInputMute(false);
1800 // Verify the input is unmuted, OnInputMuteChanged event is fired.
1801 EXPECT_FALSE(cras_audio_handler_->IsInputMuted());
1802 EXPECT_EQ(2, test_observer_->input_mute_changed_count());
1805 TEST_F(CrasAudioHandlerTest, SetOutputVolumePercent) {
1806 AudioNodeList audio_nodes;
1807 audio_nodes.push_back(kInternalSpeaker);
1808 SetUpCrasAudioHandler(audio_nodes);
1809 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
1811 cras_audio_handler_->SetOutputVolumePercent(60);
1813 // Verify the output volume is changed to the designated value,
1814 // OnOutputVolumeChanged event is fired, and the device volume value
1815 // is saved the preferences.
1816 const int kVolume = 60;
1817 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent());
1818 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
1819 AudioDevice device;
1820 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device));
1821 EXPECT_EQ(device.id, kInternalSpeaker.id);
1822 EXPECT_EQ(kVolume, audio_pref_handler_->GetOutputVolumeValue(&device));
1825 TEST_F(CrasAudioHandlerTest, SetInputGainPercent) {
1826 AudioNodeList audio_nodes;
1827 audio_nodes.push_back(kInternalMic);
1828 SetUpCrasAudioHandler(audio_nodes);
1829 EXPECT_EQ(0, test_observer_->input_gain_changed_count());
1831 cras_audio_handler_->SetInputGainPercent(60);
1833 // Verify the input gain changed to the designated value,
1834 // OnInputGainChanged event is fired, and the device gain value
1835 // is saved in the preferences.
1836 const int kGain = 60;
1837 EXPECT_EQ(kGain, cras_audio_handler_->GetInputGainPercent());
1838 EXPECT_EQ(1, test_observer_->input_gain_changed_count());
1839 AudioDevice internal_mic(kInternalMic);
1840 EXPECT_EQ(kGain, audio_pref_handler_->GetInputGainValue(&internal_mic));
1843 TEST_F(CrasAudioHandlerTest, SetMuteForDevice) {
1844 AudioNodeList audio_nodes;
1845 audio_nodes.push_back(kInternalSpeaker);
1846 audio_nodes.push_back(kHeadphone);
1847 audio_nodes.push_back(kInternalMic);
1848 audio_nodes.push_back(kUSBMic);
1849 SetUpCrasAudioHandler(audio_nodes);
1851 // Mute the active output device.
1852 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1853 cras_audio_handler_->SetMuteForDevice(kHeadphone.id, true);
1855 // Verify the headphone is muted and mute value is saved in the preferences.
1856 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kHeadphone.id));
1857 AudioDevice headphone(kHeadphone);
1858 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(headphone));
1860 // Mute the non-active output device.
1861 cras_audio_handler_->SetMuteForDevice(kInternalSpeaker.id, true);
1863 // Verify the internal speaker is muted and mute value is saved in the
1864 // preferences.
1865 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kInternalSpeaker.id));
1866 AudioDevice internal_speaker(kInternalSpeaker);
1867 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(internal_speaker));
1869 // Mute the active input device.
1870 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1871 cras_audio_handler_->SetMuteForDevice(kUSBMic.id, true);
1873 // Verify the USB Mic is muted.
1874 EXPECT_TRUE(cras_audio_handler_->IsInputMutedForDevice(kUSBMic.id));
1876 // Mute the non-active input device should be a no-op, see crbug.com/365050.
1877 cras_audio_handler_->SetMuteForDevice(kInternalMic.id, true);
1879 // Verify IsInputMutedForDevice returns false for non-active input device.
1880 EXPECT_FALSE(cras_audio_handler_->IsInputMutedForDevice(kInternalMic.id));
1883 TEST_F(CrasAudioHandlerTest, SetVolumeGainPercentForDevice) {
1884 AudioNodeList audio_nodes;
1885 audio_nodes.push_back(kInternalSpeaker);
1886 audio_nodes.push_back(kHeadphone);
1887 audio_nodes.push_back(kInternalMic);
1888 audio_nodes.push_back(kUSBMic);
1889 SetUpCrasAudioHandler(audio_nodes);
1891 // Set volume percent for active output device.
1892 const int kHeadphoneVolume = 30;
1893 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1894 cras_audio_handler_->SetVolumeGainPercentForDevice(kHeadphone.id,
1895 kHeadphoneVolume);
1897 // Verify the volume percent of headphone is set, and saved in preferences.
1898 EXPECT_EQ(kHeadphoneVolume,
1899 cras_audio_handler_->GetOutputVolumePercentForDevice(
1900 kHeadphone.id));
1901 AudioDevice headphone(kHeadphone);
1902 EXPECT_EQ(kHeadphoneVolume,
1903 audio_pref_handler_->GetOutputVolumeValue(&headphone));
1905 // Set volume percent for non-active output device.
1906 const int kSpeakerVolume = 60;
1907 cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalSpeaker.id,
1908 kSpeakerVolume);
1910 // Verify the volume percent of speaker is set, and saved in preferences.
1911 EXPECT_EQ(kSpeakerVolume,
1912 cras_audio_handler_->GetOutputVolumePercentForDevice(
1913 kInternalSpeaker.id));
1914 AudioDevice speaker(kInternalSpeaker);
1915 EXPECT_EQ(kSpeakerVolume,
1916 audio_pref_handler_->GetOutputVolumeValue(&speaker));
1918 // Set gain percent for active input device.
1919 const int kUSBMicGain = 30;
1920 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1921 cras_audio_handler_->SetVolumeGainPercentForDevice(kUSBMic.id,
1922 kUSBMicGain);
1924 // Verify the gain percent of USB mic is set, and saved in preferences.
1925 EXPECT_EQ(kUSBMicGain,
1926 cras_audio_handler_->GetOutputVolumePercentForDevice(kUSBMic.id));
1927 AudioDevice usb_mic(kHeadphone);
1928 EXPECT_EQ(kUSBMicGain,
1929 audio_pref_handler_->GetInputGainValue(&usb_mic));
1931 // Set gain percent for non-active input device.
1932 const int kInternalMicGain = 60;
1933 cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalMic.id,
1934 kInternalMicGain);
1936 // Verify the gain percent of internal mic is set, and saved in preferences.
1937 EXPECT_EQ(kInternalMicGain,
1938 cras_audio_handler_->GetOutputVolumePercentForDevice(
1939 kInternalMic.id));
1940 AudioDevice internal_mic(kInternalMic);
1941 EXPECT_EQ(kInternalMicGain,
1942 audio_pref_handler_->GetInputGainValue(&internal_mic));
1945 TEST_F(CrasAudioHandlerTest, HandleOtherDeviceType) {
1946 const size_t kNumValidAudioDevices = 4;
1947 AudioNodeList audio_nodes;
1948 audio_nodes.push_back(kInternalSpeaker);
1949 audio_nodes.push_back(kOtherTypeOutput);
1950 audio_nodes.push_back(kInternalMic);
1951 audio_nodes.push_back(kOtherTypeInput);
1952 SetUpCrasAudioHandler(audio_nodes);
1954 // Verify the audio devices size.
1955 AudioDeviceList audio_devices;
1956 cras_audio_handler_->GetAudioDevices(&audio_devices);
1957 EXPECT_EQ(kNumValidAudioDevices, audio_devices.size());
1959 // Verify the internal speaker has been selected as the active output,
1960 // and the output device with some randown unknown type is handled gracefully.
1961 AudioDevice active_output;
1962 EXPECT_TRUE(
1963 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1964 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
1965 EXPECT_EQ(kInternalSpeaker.id,
1966 cras_audio_handler_->GetPrimaryActiveOutputNode());
1967 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1969 // Ensure the internal microphone has been selected as the active input,
1970 // and the input device with some random unknown type is handled gracefully.
1971 AudioDevice active_input;
1972 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1973 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1976 TEST_F(CrasAudioHandlerTest, ChangeActiveNodesHotrodInit) {
1977 AudioNodeList audio_nodes;
1978 audio_nodes.push_back(kHDMIOutput);
1979 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
1980 audio_nodes.push_back(kUSBJabraSpeakerOutput2);
1981 audio_nodes.push_back(kUSBJabraSpeakerInput1);
1982 audio_nodes.push_back(kUSBJabraSpeakerInput2);
1983 audio_nodes.push_back(kUSBCameraInput);
1984 SetUpCrasAudioHandler(audio_nodes);
1986 // Verify the audio devices size.
1987 AudioDeviceList audio_devices;
1988 cras_audio_handler_->GetAudioDevices(&audio_devices);
1989 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
1991 // Verify only the 1st jabra speaker's output and input are selected as active
1992 // nodes by CrasAudioHandler.
1993 AudioDevice active_output;
1994 EXPECT_TRUE(
1995 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1996 EXPECT_EQ(2, GetActiveDeviceCount());
1997 AudioDevice primary_active_device;
1998 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(
1999 &primary_active_device));
2000 EXPECT_EQ(kUSBJabraSpeakerOutput1.id, primary_active_device.id);
2001 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2002 cras_audio_handler_->GetPrimaryActiveInputNode());
2004 // Set both jabra speakers's input and output nodes to active, this simulate
2005 // the call sent by hotrod initialization process.
2006 test_observer_->reset_active_output_node_changed_count();
2007 test_observer_->reset_active_input_node_changed_count();
2008 CrasAudioHandler::NodeIdList active_nodes;
2009 active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2010 active_nodes.push_back(kUSBJabraSpeakerOutput2.id);
2011 active_nodes.push_back(kUSBJabraSpeakerInput1.id);
2012 active_nodes.push_back(kUSBJabraSpeakerInput2.id);
2013 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2015 // Verify both jabra speakers' input/output nodes are made active.
2016 // num_active_nodes = GetActiveDeviceCount();
2017 EXPECT_EQ(4, GetActiveDeviceCount());
2018 const AudioDevice* active_output_1 =
2019 GetDeviceFromId(kUSBJabraSpeakerOutput1.id);
2020 EXPECT_TRUE(active_output_1->active);
2021 const AudioDevice* active_output_2 =
2022 GetDeviceFromId(kUSBJabraSpeakerOutput2.id);
2023 EXPECT_TRUE(active_output_2->active);
2024 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(
2025 &primary_active_device));
2026 EXPECT_EQ(kUSBJabraSpeakerOutput1.id, primary_active_device.id);
2027 const AudioDevice* active_input_1 =
2028 GetDeviceFromId(kUSBJabraSpeakerInput1.id);
2029 EXPECT_TRUE(active_input_1->active);
2030 const AudioDevice* active_input_2 =
2031 GetDeviceFromId(kUSBJabraSpeakerInput2.id);
2032 EXPECT_TRUE(active_input_2->active);
2033 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2034 cras_audio_handler_->GetPrimaryActiveInputNode());
2036 // Verify only 1 ActiveOutputNodeChanged notification has been sent out
2037 // by calling ChangeActiveNodes.
2038 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
2039 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
2041 // Verify all active devices are the not muted and their volume values are
2042 // the same.
2043 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
2044 EXPECT_FALSE(
2045 cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput1.id));
2046 EXPECT_FALSE(
2047 cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput2.id));
2048 EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(),
2049 cras_audio_handler_->GetOutputVolumePercentForDevice(
2050 kUSBJabraSpeakerOutput1.id));
2051 EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(),
2052 cras_audio_handler_->GetOutputVolumePercentForDevice(
2053 kUSBJabraSpeakerOutput2.id));
2055 // Adjust the volume of output devices, verify all active nodes are set to
2056 // the same volume.
2057 cras_audio_handler_->SetOutputVolumePercent(25);
2058 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercent());
2059 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercentForDevice(
2060 kUSBJabraSpeakerOutput1.id));
2061 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercentForDevice(
2062 kUSBJabraSpeakerOutput2.id));
2065 TEST_F(CrasAudioHandlerTest, ChangeActiveNodesHotrodInitWithCameraInputActive) {
2066 AudioNodeList audio_nodes;
2067 audio_nodes.push_back(kHDMIOutput);
2068 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2069 audio_nodes.push_back(kUSBJabraSpeakerOutput2);
2070 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2071 audio_nodes.push_back(kUSBJabraSpeakerInput2);
2072 // Make the camera input to be plugged in later than jabra's input.
2073 AudioNode usb_camera(kUSBCameraInput);
2074 usb_camera.plugged_time = 10000000;
2075 audio_nodes.push_back(usb_camera);
2076 SetUpCrasAudioHandler(audio_nodes);
2078 // Verify the audio devices size.
2079 AudioDeviceList audio_devices;
2080 cras_audio_handler_->GetAudioDevices(&audio_devices);
2081 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2083 // Verify the 1st jabra speaker's output is selected as active output
2084 // node and camera's input is selected active input by CrasAudioHandler.
2085 EXPECT_EQ(2, GetActiveDeviceCount());
2086 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2087 cras_audio_handler_->GetPrimaryActiveOutputNode());
2088 EXPECT_EQ(kUSBCameraInput.id,
2089 cras_audio_handler_->GetPrimaryActiveInputNode());
2091 // Set both jabra speakers's input and output nodes to active, this simulates
2092 // the call sent by hotrod initialization process.
2093 test_observer_->reset_active_output_node_changed_count();
2094 test_observer_->reset_active_input_node_changed_count();
2095 CrasAudioHandler::NodeIdList active_nodes;
2096 active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2097 active_nodes.push_back(kUSBJabraSpeakerOutput2.id);
2098 active_nodes.push_back(kUSBJabraSpeakerInput1.id);
2099 active_nodes.push_back(kUSBJabraSpeakerInput2.id);
2100 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2102 // Verify both jabra speakers' input/output nodes are made active.
2103 // num_active_nodes = GetActiveDeviceCount();
2104 EXPECT_EQ(4, GetActiveDeviceCount());
2105 const AudioDevice* active_output_1 =
2106 GetDeviceFromId(kUSBJabraSpeakerOutput1.id);
2107 EXPECT_TRUE(active_output_1->active);
2108 const AudioDevice* active_output_2 =
2109 GetDeviceFromId(kUSBJabraSpeakerOutput2.id);
2110 EXPECT_TRUE(active_output_2->active);
2111 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2112 cras_audio_handler_->GetPrimaryActiveOutputNode());
2113 const AudioDevice* active_input_1 =
2114 GetDeviceFromId(kUSBJabraSpeakerInput1.id);
2115 EXPECT_TRUE(active_input_1->active);
2116 const AudioDevice* active_input_2 =
2117 GetDeviceFromId(kUSBJabraSpeakerInput2.id);
2118 EXPECT_TRUE(active_input_2->active);
2119 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2120 cras_audio_handler_->GetPrimaryActiveInputNode());
2122 // Verify only 1 ActiveOutputNodeChanged notification has been sent out
2123 // by calling ChangeActiveNodes.
2124 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
2125 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
2128 TEST_F(CrasAudioHandlerTest, ChangeActiveNodesWithFewerActives) {
2129 AudioNodeList audio_nodes;
2130 audio_nodes.push_back(kHDMIOutput);
2131 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2132 audio_nodes.push_back(kUSBJabraSpeakerOutput2);
2133 SetUpCrasAudioHandler(audio_nodes);
2135 // Verify the audio devices size.
2136 AudioDeviceList audio_devices;
2137 cras_audio_handler_->GetAudioDevices(&audio_devices);
2138 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2140 // Set all three nodes to be active.
2141 CrasAudioHandler::NodeIdList active_nodes;
2142 active_nodes.push_back(kHDMIOutput.id);
2143 active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2144 active_nodes.push_back(kUSBJabraSpeakerOutput2.id);
2145 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2147 // Verify all three nodes are active.
2148 EXPECT_EQ(3, GetActiveDeviceCount());
2149 const AudioDevice* active_output_1 = GetDeviceFromId(kHDMIOutput.id);
2150 EXPECT_TRUE(active_output_1->active);
2151 const AudioDevice* active_output_2 =
2152 GetDeviceFromId(kUSBJabraSpeakerOutput1.id);
2153 EXPECT_TRUE(active_output_2->active);
2154 const AudioDevice* active_output_3 =
2155 GetDeviceFromId(kUSBJabraSpeakerOutput2.id);
2156 EXPECT_TRUE(active_output_3->active);
2158 // Now call ChangeActiveDevices with only 2 nodes.
2159 active_nodes.clear();
2160 active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2161 active_nodes.push_back(kUSBJabraSpeakerOutput2.id);
2162 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2164 // Verify only 2 nodes are active.
2165 EXPECT_EQ(2, GetActiveDeviceCount());
2166 const AudioDevice* output_1 = GetDeviceFromId(kHDMIOutput.id);
2167 EXPECT_FALSE(output_1->active);
2168 const AudioDevice* output_2 = GetDeviceFromId(kUSBJabraSpeakerOutput1.id);
2169 EXPECT_TRUE(output_2->active);
2170 const AudioDevice* output_3 = GetDeviceFromId(kUSBJabraSpeakerOutput2.id);
2171 EXPECT_TRUE(output_3->active);
2174 TEST_F(CrasAudioHandlerTest, HotrodInitWithSingleJabra) {
2175 // Simulates the hotrod initializated with a single jabra device and
2176 // CrasAudioHandler selected jabra input/output as active devices.
2177 AudioNodeList audio_nodes;
2178 audio_nodes.push_back(kHDMIOutput);
2179 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2180 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2181 audio_nodes.push_back(kUSBCameraInput);
2182 SetUpCrasAudioHandler(audio_nodes);
2184 // Verify the audio devices size.
2185 AudioDeviceList audio_devices;
2186 cras_audio_handler_->GetAudioDevices(&audio_devices);
2187 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2189 // Verify the jabra speaker's output and input are selected as active nodes
2190 // by CrasAudioHandler.
2191 EXPECT_EQ(2, GetActiveDeviceCount());
2192 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2193 cras_audio_handler_->GetPrimaryActiveOutputNode());
2194 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2195 cras_audio_handler_->GetPrimaryActiveInputNode());
2198 TEST_F(CrasAudioHandlerTest,
2199 ChangeActiveNodesHotrodInitWithSingleJabraCameraPlugInLater) {
2200 AudioNodeList audio_nodes;
2201 audio_nodes.push_back(kHDMIOutput);
2202 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2203 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2204 AudioNode usb_camera(kUSBCameraInput);
2205 usb_camera.plugged_time = 10000000;
2206 audio_nodes.push_back(usb_camera);
2207 SetUpCrasAudioHandler(audio_nodes);
2209 // Verify the audio devices size.
2210 AudioDeviceList audio_devices;
2211 cras_audio_handler_->GetAudioDevices(&audio_devices);
2212 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2214 // Verify the jabra speaker's output is selected as active output, and
2215 // camera's input is selected as active input by CrasAudioHandler
2216 EXPECT_EQ(2, GetActiveDeviceCount());
2217 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2218 cras_audio_handler_->GetPrimaryActiveOutputNode());
2219 EXPECT_EQ(kUSBCameraInput.id,
2220 cras_audio_handler_->GetPrimaryActiveInputNode());
2222 // Simulate hotrod app call to set jabra input as active device with only
2223 // jabra input node in the active node list, which does not conform to the
2224 // new SetActiveDevices protocol, but just show we can still handle it if
2225 // this happens.
2226 CrasAudioHandler::NodeIdList active_nodes;
2227 active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2228 active_nodes.push_back(kUSBJabraSpeakerInput1.id);
2229 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2231 // Verify the jabra speaker's output is selected as active output, and
2232 // jabra's input is selected as active input.
2233 EXPECT_EQ(2, GetActiveDeviceCount());
2234 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2235 cras_audio_handler_->GetPrimaryActiveOutputNode());
2236 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2237 cras_audio_handler_->GetPrimaryActiveInputNode());
2240 TEST_F(CrasAudioHandlerTest,
2241 ChangeActiveNodesHotrodInitWithSingleJabraCameraPlugInLaterOldCall) {
2242 AudioNodeList audio_nodes;
2243 audio_nodes.push_back(kHDMIOutput);
2244 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2245 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2246 AudioNode usb_camera(kUSBCameraInput);
2247 usb_camera.plugged_time = 10000000;
2248 audio_nodes.push_back(usb_camera);
2249 SetUpCrasAudioHandler(audio_nodes);
2251 // Verify the audio devices size.
2252 AudioDeviceList audio_devices;
2253 cras_audio_handler_->GetAudioDevices(&audio_devices);
2254 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2256 // Verify the jabra speaker's output is selected as active output, and
2257 // camera's input is selected as active input by CrasAudioHandler
2258 EXPECT_EQ(2, GetActiveDeviceCount());
2259 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2260 cras_audio_handler_->GetPrimaryActiveOutputNode());
2261 EXPECT_EQ(kUSBCameraInput.id,
2262 cras_audio_handler_->GetPrimaryActiveInputNode());
2264 // Simulate hotrod app call to set jabra input as active device with only
2265 // jabra input node in the active node list, which does not conform to the
2266 // new SetActiveDevices protocol, but just show we can still handle it if
2267 // this happens.
2268 CrasAudioHandler::NodeIdList active_nodes;
2269 active_nodes.push_back(kUSBJabraSpeakerInput1.id);
2270 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2272 // Verify the jabra speaker's output is selected as active output, and
2273 // jabra's input is selected as active input.
2274 EXPECT_EQ(2, GetActiveDeviceCount());
2275 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2276 cras_audio_handler_->GetPrimaryActiveOutputNode());
2277 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2278 cras_audio_handler_->GetPrimaryActiveInputNode());
2281 TEST_F(CrasAudioHandlerTest,
2282 ChangeActiveNodesHotrodInitWithSingleJabraChangeOutput) {
2283 AudioNodeList audio_nodes;
2284 audio_nodes.push_back(kHDMIOutput);
2285 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2286 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2287 audio_nodes.push_back(kUSBCameraInput);
2288 SetUpCrasAudioHandler(audio_nodes);
2290 // Verify the audio devices size.
2291 AudioDeviceList audio_devices;
2292 cras_audio_handler_->GetAudioDevices(&audio_devices);
2293 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2295 // Verify the jabra speaker's output and input are selected as active output
2296 // by CrasAudioHandler.
2297 EXPECT_EQ(2, GetActiveDeviceCount());
2298 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2299 cras_audio_handler_->GetPrimaryActiveOutputNode());
2300 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2301 cras_audio_handler_->GetPrimaryActiveInputNode());
2303 // Simulate hotrod app call SetActiveDevices to change active output
2304 // with only complete list of active nodes passed in, which is the new
2305 // way of hotrod app.
2306 CrasAudioHandler::NodeIdList active_nodes;
2307 active_nodes.push_back(kHDMIOutput.id);
2308 active_nodes.push_back(kUSBJabraSpeakerInput1.id);
2309 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2311 // Verify the jabra speaker's output is selected as active output, and
2312 // jabra's input is selected as active input.
2313 EXPECT_EQ(2, GetActiveDeviceCount());
2314 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2315 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2316 cras_audio_handler_->GetPrimaryActiveInputNode());
2319 TEST_F(CrasAudioHandlerTest,
2320 ChangeActiveNodesHotrodInitWithSingleJabraChangeOutputOldCall) {
2321 AudioNodeList audio_nodes;
2322 audio_nodes.push_back(kHDMIOutput);
2323 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2324 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2325 audio_nodes.push_back(kUSBCameraInput);
2326 SetUpCrasAudioHandler(audio_nodes);
2328 // Verify the audio devices size.
2329 AudioDeviceList audio_devices;
2330 cras_audio_handler_->GetAudioDevices(&audio_devices);
2331 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2333 // Verify the jabra speaker's output and input are selected as active output
2334 // by CrasAudioHandler.
2335 EXPECT_EQ(2, GetActiveDeviceCount());
2336 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2337 cras_audio_handler_->GetPrimaryActiveOutputNode());
2338 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2339 cras_audio_handler_->GetPrimaryActiveInputNode());
2341 // Simulate hotrod app call SetActiveDevices to change active output
2342 // with only a single active output nodes passed in, which is the old
2343 // way of hotrod app.
2344 CrasAudioHandler::NodeIdList active_nodes;
2345 active_nodes.push_back(kHDMIOutput.id);
2346 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2348 // Verify the jabra speaker's output is selected as active output, and
2349 // jabra's input is selected as active input.
2350 EXPECT_EQ(2, GetActiveDeviceCount());
2351 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2352 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2353 cras_audio_handler_->GetPrimaryActiveInputNode());
2356 TEST_F(CrasAudioHandlerTest, NoMoreAudioInputDevices) {
2357 // Some device like chromebox does not have the internal input device. The
2358 // active devices should be reset when the user plugs a device and then
2359 // unplugs it to such device.
2361 AudioNodeList audio_nodes;
2362 audio_nodes.push_back(kInternalSpeaker);
2363 SetUpCrasAudioHandler(audio_nodes);
2365 EXPECT_EQ(0ULL, cras_audio_handler_->GetPrimaryActiveInputNode());
2367 audio_nodes.push_back(kMicJack);
2368 ChangeAudioNodes(audio_nodes);
2370 EXPECT_EQ(kMicJack.id, cras_audio_handler_->GetPrimaryActiveInputNode());
2371 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
2372 test_observer_->reset_active_input_node_changed_count();
2374 audio_nodes.pop_back();
2375 ChangeAudioNodes(audio_nodes);
2376 EXPECT_EQ(0ULL, cras_audio_handler_->GetPrimaryActiveInputNode());
2377 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
2380 // Test the case in which an HDMI output is plugged in with other higher
2381 // priority
2382 // output devices already plugged and user has manually selected an active
2383 // output.
2384 // The hotplug of hdmi output should not change user's selection of active
2385 // device.
2386 // crbug.com/447826.
2387 TEST_F(CrasAudioHandlerTest, HotPlugHDMINotChangeActiveOutput) {
2388 AudioNodeList audio_nodes;
2389 AudioNode internal_speaker(kInternalSpeaker);
2390 audio_nodes.push_back(internal_speaker);
2391 AudioNode usb_headset(kUSBHeadphone1);
2392 usb_headset.plugged_time = 80000000;
2393 audio_nodes.push_back(usb_headset);
2394 SetUpCrasAudioHandler(audio_nodes);
2396 // Verify the audio devices size.
2397 AudioDeviceList audio_devices;
2398 cras_audio_handler_->GetAudioDevices(&audio_devices);
2399 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2401 // Verify the USB headset is selected as active output by default.
2402 EXPECT_EQ(usb_headset.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2404 // Manually set the active output to internal speaker.
2405 AudioDevice internal_output(kInternalSpeaker);
2406 cras_audio_handler_->SwitchToDevice(internal_output, true);
2408 // Verify the active output is switched to internal speaker.
2409 EXPECT_EQ(internal_speaker.id,
2410 cras_audio_handler_->GetPrimaryActiveOutputNode());
2411 EXPECT_LT(kInternalSpeaker.plugged_time, usb_headset.plugged_time);
2412 const AudioDevice* usb_device = GetDeviceFromId(usb_headset.id);
2413 EXPECT_FALSE(usb_device->active);
2415 // Plug in HDMI output.
2416 audio_nodes.clear();
2417 internal_speaker.active = true;
2418 audio_nodes.push_back(internal_speaker);
2419 usb_headset.active = false;
2420 audio_nodes.push_back(usb_headset);
2421 AudioNode hdmi(kHDMIOutput);
2422 hdmi.plugged_time = 90000000;
2423 audio_nodes.push_back(hdmi);
2424 ChangeAudioNodes(audio_nodes);
2426 // The active output should not change.
2427 EXPECT_EQ(kInternalSpeaker.id,
2428 cras_audio_handler_->GetPrimaryActiveOutputNode());
2431 } // namespace chromeos