[Cronet] Delay StartNetLog and StopNetLog until native request context is initialized
[chromium-blink-merge.git] / chromeos / audio / cras_audio_handler_unittest.cc
blobac9493c52d7d2e67e245a31d81f5072fe19d76ac
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(false,
118 kBluetoothHeadsetId,
119 "Bluetooth Headset",
120 "BLUETOOTH",
121 "Bluetooth Headset 1",
122 false,
125 const AudioNode kHDMIOutput(false,
126 kHDMIOutputId,
127 "HDMI output",
128 "HDMI",
129 "HDMI output",
130 false,
133 const AudioNode kUSBHeadphone1(false,
134 kUSBHeadphoneId1,
135 "USB Headphone",
136 "USB",
137 "USB Headphone 1",
138 false,
141 const AudioNode kUSBHeadphone2(false,
142 kUSBHeadphoneId2,
143 "USB Headphone",
144 "USB",
145 "USB Headphone 1",
146 false,
149 const AudioNode kUSBJabraSpeakerOutput1(false,
150 kUSBJabraSpeakerOutputId1,
151 "Jabra Speaker 1",
152 "USB",
153 "Jabra Speaker 1",
154 false,
157 const AudioNode kUSBJabraSpeakerOutput2(false,
158 kUSBJabraSpeakerOutputId2,
159 "Jabra Speaker 2",
160 "USB",
161 "Jabra Speaker 2",
162 false,
165 const AudioNode kUSBJabraSpeakerInput1(true,
166 kUSBJabraSpeakerInputId1,
167 "Jabra Speaker 1",
168 "USB",
169 "Jabra Speaker",
170 false,
173 const AudioNode kUSBJabraSpeakerInput2(true,
174 kUSBJabraSpeakerInputId2,
175 "Jabra Speaker 2",
176 "USB",
177 "Jabra Speaker 2",
178 false,
181 const AudioNode kUSBCameraInput(true,
182 kUSBCameraInputId,
183 "USB Camera",
184 "USB",
185 "USB Camera",
186 false,
189 class TestObserver : public chromeos::CrasAudioHandler::AudioObserver {
190 public:
191 TestObserver() : active_output_node_changed_count_(0),
192 active_input_node_changed_count_(0),
193 audio_nodes_changed_count_(0),
194 output_mute_changed_count_(0),
195 input_mute_changed_count_(0),
196 output_volume_changed_count_(0),
197 input_gain_changed_count_(0) {
200 int active_output_node_changed_count() const {
201 return active_output_node_changed_count_;
204 void reset_active_output_node_changed_count() {
205 active_output_node_changed_count_ = 0;
208 int active_input_node_changed_count() const {
209 return active_input_node_changed_count_;
212 void reset_active_input_node_changed_count() {
213 active_input_node_changed_count_ = 0;
216 int audio_nodes_changed_count() const {
217 return audio_nodes_changed_count_;
220 int output_mute_changed_count() const {
221 return output_mute_changed_count_;
224 int input_mute_changed_count() const {
225 return input_mute_changed_count_;
228 int output_volume_changed_count() const {
229 return output_volume_changed_count_;
232 int input_gain_changed_count() const {
233 return input_gain_changed_count_;
236 ~TestObserver() override {}
238 protected:
239 // chromeos::CrasAudioHandler::AudioObserver overrides.
240 void OnActiveOutputNodeChanged() override {
241 ++active_output_node_changed_count_;
244 void OnActiveInputNodeChanged() override {
245 ++active_input_node_changed_count_;
248 void OnAudioNodesChanged() override { ++audio_nodes_changed_count_; }
250 void OnOutputMuteChanged() override { ++output_mute_changed_count_; }
252 void OnInputMuteChanged() override { ++input_mute_changed_count_; }
254 void OnOutputNodeVolumeChanged(uint64 /* node_id */,
255 int /* volume */) override {
256 ++output_volume_changed_count_;
259 void OnInputNodeGainChanged(uint64 /* node_id */, int /* gain */) override {
260 ++input_gain_changed_count_;
263 private:
264 int active_output_node_changed_count_;
265 int active_input_node_changed_count_;
266 int audio_nodes_changed_count_;
267 int output_mute_changed_count_;
268 int input_mute_changed_count_;
269 int output_volume_changed_count_;
270 int input_gain_changed_count_;
272 DISALLOW_COPY_AND_ASSIGN(TestObserver);
275 class CrasAudioHandlerTest : public testing::Test {
276 public:
277 CrasAudioHandlerTest() : cras_audio_handler_(NULL),
278 fake_cras_audio_client_(NULL) {
280 ~CrasAudioHandlerTest() override {}
282 void SetUp() override {}
284 void TearDown() override {
285 cras_audio_handler_->RemoveAudioObserver(test_observer_.get());
286 test_observer_.reset();
287 CrasAudioHandler::Shutdown();
288 audio_pref_handler_ = NULL;
289 DBusThreadManager::Shutdown();
292 void SetUpCrasAudioHandler(const AudioNodeList& audio_nodes) {
293 DBusThreadManager::Initialize();
294 fake_cras_audio_client_ = static_cast<FakeCrasAudioClient*>(
295 DBusThreadManager::Get()->GetCrasAudioClient());
296 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes);
297 audio_pref_handler_ = new AudioDevicesPrefHandlerStub();
298 CrasAudioHandler::Initialize(audio_pref_handler_);
299 cras_audio_handler_ = CrasAudioHandler::Get();
300 test_observer_.reset(new TestObserver);
301 cras_audio_handler_->AddAudioObserver(test_observer_.get());
302 message_loop_.RunUntilIdle();
305 void SetUpCrasAudioHandlerWithPrimaryActiveNode(
306 const AudioNodeList& audio_nodes,
307 const AudioNode& primary_active_node) {
308 DBusThreadManager::Initialize();
309 fake_cras_audio_client_ = static_cast<FakeCrasAudioClient*>(
310 DBusThreadManager::Get()->GetCrasAudioClient());
311 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes);
312 fake_cras_audio_client_->SetActiveOutputNode(primary_active_node.id),
313 audio_pref_handler_ = new AudioDevicesPrefHandlerStub();
314 CrasAudioHandler::Initialize(audio_pref_handler_);
315 cras_audio_handler_ = CrasAudioHandler::Get();
316 test_observer_.reset(new TestObserver);
317 cras_audio_handler_->AddAudioObserver(test_observer_.get());
318 message_loop_.RunUntilIdle();
321 void ChangeAudioNodes(const AudioNodeList& audio_nodes) {
322 fake_cras_audio_client_->SetAudioNodesAndNotifyObserversForTesting(
323 audio_nodes);
324 message_loop_.RunUntilIdle();
327 const AudioDevice* GetDeviceFromId(uint64 id) {
328 return cras_audio_handler_->GetDeviceFromId(id);
331 int GetActiveDeviceCount() const {
332 int num_active_nodes = 0;
333 AudioDeviceList audio_devices;
334 cras_audio_handler_->GetAudioDevices(&audio_devices);
335 for (size_t i = 0; i < audio_devices.size(); ++i) {
336 if (audio_devices[i].active)
337 ++num_active_nodes;
339 return num_active_nodes;
342 protected:
343 base::MessageLoopForUI message_loop_;
344 CrasAudioHandler* cras_audio_handler_; // Not owned.
345 FakeCrasAudioClient* fake_cras_audio_client_; // Not owned.
346 scoped_ptr<TestObserver> test_observer_;
347 scoped_refptr<AudioDevicesPrefHandlerStub> audio_pref_handler_;
349 private:
350 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandlerTest);
353 TEST_F(CrasAudioHandlerTest, InitializeWithOnlyDefaultAudioDevices) {
354 AudioNodeList audio_nodes;
355 audio_nodes.push_back(kInternalSpeaker);
356 audio_nodes.push_back(kInternalMic);
357 SetUpCrasAudioHandler(audio_nodes);
359 // Verify the audio devices size.
360 AudioDeviceList audio_devices;
361 cras_audio_handler_->GetAudioDevices(&audio_devices);
362 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
364 // Verify the internal speaker has been selected as the active output.
365 AudioDevice active_output;
366 EXPECT_TRUE(
367 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
368 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
369 EXPECT_EQ(kInternalSpeaker.id,
370 cras_audio_handler_->GetPrimaryActiveOutputNode());
371 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
373 // Ensure the internal microphone has been selected as the active input.
374 AudioDevice active_input;
375 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
376 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
379 TEST_F(CrasAudioHandlerTest, InitializeWithAlternativeAudioDevices) {
380 AudioNodeList audio_nodes;
381 audio_nodes.push_back(kInternalSpeaker);
382 audio_nodes.push_back(kHeadphone);
383 audio_nodes.push_back(kInternalMic);
384 audio_nodes.push_back(kUSBMic);
385 SetUpCrasAudioHandler(audio_nodes);
387 // Verify the audio devices size.
388 AudioDeviceList audio_devices;
389 cras_audio_handler_->GetAudioDevices(&audio_devices);
390 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
392 // Verify the headphone has been selected as the active output.
393 AudioDevice active_output;
394 EXPECT_TRUE(
395 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
396 EXPECT_EQ(kHeadphone.id, active_output.id);
397 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
398 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
400 // Ensure the USB microphone has been selected as the active input.
401 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
402 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
405 TEST_F(CrasAudioHandlerTest, InitializeWithKeyboardMic) {
406 AudioNodeList audio_nodes;
407 audio_nodes.push_back(kInternalSpeaker);
408 audio_nodes.push_back(kInternalMic);
409 audio_nodes.push_back(kKeyboardMic);
410 SetUpCrasAudioHandler(audio_nodes);
412 // Verify the audio devices size.
413 AudioDeviceList audio_devices;
414 cras_audio_handler_->GetAudioDevices(&audio_devices);
415 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
416 EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic());
418 // Verify the internal speaker has been selected as the active output.
419 AudioDevice active_output;
420 EXPECT_TRUE(
421 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
422 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
423 EXPECT_EQ(kInternalSpeaker.id,
424 cras_audio_handler_->GetPrimaryActiveOutputNode());
425 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
427 // Ensure the internal microphone has been selected as the active input,
428 // not affected by keyboard mic.
429 AudioDevice active_input;
430 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
431 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
432 const AudioDevice* keyboard_mic = GetDeviceFromId(kKeyboardMic.id);
433 EXPECT_FALSE(keyboard_mic->active);
436 TEST_F(CrasAudioHandlerTest, SetKeyboardMicActive) {
437 AudioNodeList audio_nodes;
438 audio_nodes.push_back(kInternalMic);
439 audio_nodes.push_back(kKeyboardMic);
440 SetUpCrasAudioHandler(audio_nodes);
442 // Verify the audio devices size.
443 AudioDeviceList audio_devices;
444 cras_audio_handler_->GetAudioDevices(&audio_devices);
445 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
446 EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic());
448 // Ensure the internal microphone has been selected as the active input,
449 // not affected by keyboard mic.
450 AudioDevice active_input;
451 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
452 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
453 const AudioDevice* keyboard_mic = GetDeviceFromId(kKeyboardMic.id);
454 EXPECT_FALSE(keyboard_mic->active);
456 // Make keyboard mic active.
457 cras_audio_handler_->SetKeyboardMicActive(true);
458 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
459 const AudioDevice* active_keyboard_mic = GetDeviceFromId(kKeyboardMic.id);
460 EXPECT_TRUE(active_keyboard_mic->active);
462 // Make keyboard mic inactive.
463 cras_audio_handler_->SetKeyboardMicActive(false);
464 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
465 const AudioDevice* inactive_keyboard_mic = GetDeviceFromId(kKeyboardMic.id);
466 EXPECT_FALSE(inactive_keyboard_mic->active);
469 TEST_F(CrasAudioHandlerTest, SwitchActiveOutputDevice) {
470 AudioNodeList audio_nodes;
471 audio_nodes.push_back(kInternalSpeaker);
472 audio_nodes.push_back(kHeadphone);
473 SetUpCrasAudioHandler(audio_nodes);
474 AudioDeviceList audio_devices;
475 cras_audio_handler_->GetAudioDevices(&audio_devices);
476 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
478 // Verify the initial active output device is headphone.
479 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
480 AudioDevice active_output;
481 EXPECT_TRUE(
482 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
483 EXPECT_EQ(kHeadphone.id, active_output.id);
484 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
486 // Switch the active output to internal speaker.
487 AudioDevice internal_speaker(kInternalSpeaker);
488 cras_audio_handler_->SwitchToDevice(internal_speaker, true);
490 // Verify the active output is switched to internal speaker, and the
491 // ActiveOutputNodeChanged event is fired.
492 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
493 EXPECT_TRUE(
494 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
495 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
496 EXPECT_EQ(kInternalSpeaker.id,
497 cras_audio_handler_->GetPrimaryActiveOutputNode());
500 TEST_F(CrasAudioHandlerTest, SwitchActiveInputDevice) {
501 AudioNodeList audio_nodes;
502 audio_nodes.push_back(kInternalMic);
503 audio_nodes.push_back(kUSBMic);
504 SetUpCrasAudioHandler(audio_nodes);
505 AudioDeviceList audio_devices;
506 cras_audio_handler_->GetAudioDevices(&audio_devices);
507 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
509 // Verify the initial active input device is USB mic.
510 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
511 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
513 // Switch the active input to internal mic.
514 AudioDevice internal_mic(kInternalMic);
515 cras_audio_handler_->SwitchToDevice(internal_mic, true);
517 // Verify the active output is switched to internal speaker, and the active
518 // ActiveInputNodeChanged event is fired.
519 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
520 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
523 TEST_F(CrasAudioHandlerTest, PlugHeadphone) {
524 // Set up initial audio devices, only with internal speaker.
525 AudioNodeList audio_nodes;
526 audio_nodes.push_back(kInternalSpeaker);
527 SetUpCrasAudioHandler(audio_nodes);
528 const size_t init_nodes_size = audio_nodes.size();
530 // Verify the audio devices size.
531 AudioDeviceList audio_devices;
532 cras_audio_handler_->GetAudioDevices(&audio_devices);
533 EXPECT_EQ(init_nodes_size, audio_devices.size());
534 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
536 // Verify the internal speaker has been selected as the active output.
537 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
538 AudioDevice active_output;
539 EXPECT_TRUE(
540 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
541 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
542 EXPECT_EQ(kInternalSpeaker.id,
543 cras_audio_handler_->GetPrimaryActiveOutputNode());
544 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
546 // Plug the headphone.
547 audio_nodes.clear();
548 AudioNode internal_speaker(kInternalSpeaker);
549 internal_speaker.active = true;
550 audio_nodes.push_back(internal_speaker);
551 audio_nodes.push_back(kHeadphone);
552 ChangeAudioNodes(audio_nodes);
554 // Verify the AudioNodesChanged event is fired and new audio device is added.
555 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
556 cras_audio_handler_->GetAudioDevices(&audio_devices);
557 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
559 // Verify the active output device is switched to headphone and
560 // ActiveOutputChanged event is fired.
561 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
562 EXPECT_TRUE(
563 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
564 EXPECT_EQ(kHeadphone.id, active_output.id);
565 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
566 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
569 TEST_F(CrasAudioHandlerTest, UnplugHeadphone) {
570 // Set up initial audio devices, with internal speaker and headphone.
571 AudioNodeList audio_nodes;
572 audio_nodes.push_back(kInternalSpeaker);
573 audio_nodes.push_back(kHeadphone);
574 SetUpCrasAudioHandler(audio_nodes);
575 const size_t init_nodes_size = audio_nodes.size();
577 // Verify the audio devices size.
578 AudioDeviceList audio_devices;
579 cras_audio_handler_->GetAudioDevices(&audio_devices);
580 EXPECT_EQ(init_nodes_size, audio_devices.size());
581 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
583 // Verify the headphone has been selected as the active output.
584 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
585 AudioDevice active_output;
586 EXPECT_TRUE(
587 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
588 EXPECT_EQ(kHeadphone.id, active_output.id);
589 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
590 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
592 // Unplug the headphone.
593 audio_nodes.clear();
594 audio_nodes.push_back(kInternalSpeaker);
595 ChangeAudioNodes(audio_nodes);
597 // Verify the AudioNodesChanged event is fired and one audio device is
598 // removed.
599 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
600 cras_audio_handler_->GetAudioDevices(&audio_devices);
601 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
603 // Verify the active output device is switched to internal speaker and
604 // ActiveOutputChanged event is fired.
605 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
606 EXPECT_TRUE(
607 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
608 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
609 EXPECT_EQ(kInternalSpeaker.id,
610 cras_audio_handler_->GetPrimaryActiveOutputNode());
611 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
614 TEST_F(CrasAudioHandlerTest, InitializeWithBluetoothHeadset) {
615 AudioNodeList audio_nodes;
616 audio_nodes.push_back(kInternalSpeaker);
617 audio_nodes.push_back(kBluetoothHeadset);
618 SetUpCrasAudioHandler(audio_nodes);
620 // Verify the audio devices size.
621 AudioDeviceList audio_devices;
622 cras_audio_handler_->GetAudioDevices(&audio_devices);
623 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
624 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
626 // Verify the bluetooth headset has been selected as the active output.
627 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
628 AudioDevice active_output;
629 EXPECT_TRUE(
630 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
631 EXPECT_EQ(kBluetoothHeadset.id, active_output.id);
632 EXPECT_EQ(kBluetoothHeadset.id,
633 cras_audio_handler_->GetPrimaryActiveOutputNode());
634 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
637 TEST_F(CrasAudioHandlerTest, ConnectAndDisconnectBluetoothHeadset) {
638 // Initialize with internal speaker and headphone.
639 AudioNodeList audio_nodes;
640 audio_nodes.push_back(kInternalSpeaker);
641 audio_nodes.push_back(kHeadphone);
642 SetUpCrasAudioHandler(audio_nodes);
643 const size_t init_nodes_size = audio_nodes.size();
645 // Verify the audio devices size.
646 AudioDeviceList audio_devices;
647 cras_audio_handler_->GetAudioDevices(&audio_devices);
648 EXPECT_EQ(init_nodes_size, audio_devices.size());
649 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
651 // Verify the headphone is selected as the active output initially.
652 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
653 AudioDevice active_output;
654 EXPECT_TRUE(
655 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
656 EXPECT_EQ(kHeadphone.id, active_output.id);
657 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
658 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
660 // Connect to bluetooth headset. Since it is plugged in later than
661 // headphone, active output should be switched to it.
662 audio_nodes.clear();
663 audio_nodes.push_back(kInternalSpeaker);
664 AudioNode headphone(kHeadphone);
665 headphone.plugged_time = 80000000;
666 headphone.active = true;
667 audio_nodes.push_back(headphone);
668 AudioNode bluetooth_headset(kBluetoothHeadset);
669 bluetooth_headset.plugged_time = 90000000;
670 audio_nodes.push_back(bluetooth_headset);
671 ChangeAudioNodes(audio_nodes);
673 // Verify the AudioNodesChanged event is fired and new audio device is added.
674 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
675 cras_audio_handler_->GetAudioDevices(&audio_devices);
676 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
678 // Verify the active output device is switched to bluetooth headset, and
679 // ActiveOutputChanged event is fired.
680 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
681 EXPECT_TRUE(
682 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
683 EXPECT_EQ(kBluetoothHeadset.id, active_output.id);
684 EXPECT_EQ(kBluetoothHeadset.id,
685 cras_audio_handler_->GetPrimaryActiveOutputNode());
686 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
688 // Disconnect bluetooth headset.
689 audio_nodes.clear();
690 audio_nodes.push_back(kInternalSpeaker);
691 headphone.active = false;
692 audio_nodes.push_back(headphone);
693 ChangeAudioNodes(audio_nodes);
695 // Verify the AudioNodesChanged event is fired and one audio device is
696 // removed.
697 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
698 cras_audio_handler_->GetAudioDevices(&audio_devices);
699 EXPECT_EQ(init_nodes_size, audio_devices.size());
701 // Verify the active output device is switched to headphone, and
702 // ActiveOutputChanged event is fired.
703 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
704 EXPECT_TRUE(
705 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
706 EXPECT_EQ(kHeadphone.id, active_output.id);
707 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
708 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
711 TEST_F(CrasAudioHandlerTest, InitializeWithHDMIOutput) {
712 AudioNodeList audio_nodes;
713 audio_nodes.push_back(kInternalSpeaker);
714 audio_nodes.push_back(kHDMIOutput);
715 SetUpCrasAudioHandler(audio_nodes);
717 // Verify the audio devices size.
718 AudioDeviceList audio_devices;
719 cras_audio_handler_->GetAudioDevices(&audio_devices);
720 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
721 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
723 // Verify the HDMI device has been selected as the active output.
724 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
725 AudioDevice active_output;
726 EXPECT_TRUE(
727 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
728 EXPECT_EQ(kHDMIOutput.id, active_output.id);
729 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
730 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
733 TEST_F(CrasAudioHandlerTest, ConnectAndDisconnectHDMIOutput) {
734 // Initialize with internal speaker.
735 AudioNodeList audio_nodes;
736 audio_nodes.push_back(kInternalSpeaker);
737 SetUpCrasAudioHandler(audio_nodes);
738 const size_t init_nodes_size = audio_nodes.size();
740 // Verify the audio devices size.
741 AudioDeviceList audio_devices;
742 cras_audio_handler_->GetAudioDevices(&audio_devices);
743 EXPECT_EQ(init_nodes_size, audio_devices.size());
744 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
746 // Verify the internal speaker is selected as the active output initially.
747 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
748 AudioDevice active_output;
749 EXPECT_TRUE(
750 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
751 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
752 EXPECT_EQ(kInternalSpeaker.id,
753 cras_audio_handler_->GetPrimaryActiveOutputNode());
754 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
756 // Connect to HDMI output.
757 audio_nodes.clear();
758 AudioNode internal_speaker(kInternalSpeaker);
759 internal_speaker.active = true;
760 internal_speaker.plugged_time = 80000000;
761 audio_nodes.push_back(internal_speaker);
762 AudioNode hdmi(kHDMIOutput);
763 hdmi.plugged_time = 90000000;
764 audio_nodes.push_back(hdmi);
765 ChangeAudioNodes(audio_nodes);
767 // Verify the AudioNodesChanged event is fired and new audio device is added.
768 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
769 cras_audio_handler_->GetAudioDevices(&audio_devices);
770 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
772 // Verify the active output device is switched to hdmi output, and
773 // ActiveOutputChanged event is fired.
774 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
775 EXPECT_TRUE(
776 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
777 EXPECT_EQ(kHDMIOutput.id, active_output.id);
778 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
779 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
781 // Disconnect hdmi headset.
782 audio_nodes.clear();
783 audio_nodes.push_back(kInternalSpeaker);
784 ChangeAudioNodes(audio_nodes);
786 // Verify the AudioNodesChanged event is fired and one audio device is
787 // removed.
788 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
789 cras_audio_handler_->GetAudioDevices(&audio_devices);
790 EXPECT_EQ(init_nodes_size, audio_devices.size());
792 // Verify the active output device is switched to internal speaker, and
793 // ActiveOutputChanged event is fired.
794 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
795 EXPECT_TRUE(
796 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
797 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
798 EXPECT_EQ(kInternalSpeaker.id,
799 cras_audio_handler_->GetPrimaryActiveOutputNode());
800 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
803 TEST_F(CrasAudioHandlerTest, HandleHeadphoneAndHDMIOutput) {
804 // Initialize with internal speaker, headphone and HDMI output.
805 AudioNodeList audio_nodes;
806 audio_nodes.push_back(kInternalSpeaker);
807 audio_nodes.push_back(kHeadphone);
808 audio_nodes.push_back(kHDMIOutput);
809 SetUpCrasAudioHandler(audio_nodes);
810 const size_t init_nodes_size = audio_nodes.size();
812 // Verify the audio devices size.
813 AudioDeviceList audio_devices;
814 cras_audio_handler_->GetAudioDevices(&audio_devices);
815 EXPECT_EQ(init_nodes_size, audio_devices.size());
816 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
818 // Verify the headphone is selected as the active output initially.
819 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
820 AudioDevice active_output;
821 EXPECT_TRUE(
822 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
823 EXPECT_EQ(kHeadphone.id, active_output.id);
824 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
825 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
827 // Disconnect HDMI output.
828 audio_nodes.clear();
829 audio_nodes.push_back(kInternalSpeaker);
830 audio_nodes.push_back(kHDMIOutput);
831 ChangeAudioNodes(audio_nodes);
833 // Verify the AudioNodesChanged event is fired and one audio device is
834 // removed.
835 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
836 cras_audio_handler_->GetAudioDevices(&audio_devices);
837 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
839 // Verify the active output device is switched to HDMI output, and
840 // ActiveOutputChanged event is fired.
841 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
842 EXPECT_TRUE(
843 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
844 EXPECT_EQ(kHDMIOutput.id, active_output.id);
845 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
846 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
849 TEST_F(CrasAudioHandlerTest, InitializeWithUSBHeadphone) {
850 AudioNodeList audio_nodes;
851 audio_nodes.push_back(kInternalSpeaker);
852 audio_nodes.push_back(kUSBHeadphone1);
853 SetUpCrasAudioHandler(audio_nodes);
855 // Verify the audio devices size.
856 AudioDeviceList audio_devices;
857 cras_audio_handler_->GetAudioDevices(&audio_devices);
858 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
859 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
861 // Verify the usb headphone has been selected as the active output.
862 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
863 AudioDevice active_output;
864 EXPECT_TRUE(
865 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
866 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
867 EXPECT_EQ(kUSBHeadphone1.id,
868 cras_audio_handler_->GetPrimaryActiveOutputNode());
869 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
872 TEST_F(CrasAudioHandlerTest, PlugAndUnplugUSBHeadphone) {
873 // Initialize with internal speaker.
874 AudioNodeList audio_nodes;
875 audio_nodes.push_back(kInternalSpeaker);
876 SetUpCrasAudioHandler(audio_nodes);
877 const size_t init_nodes_size = audio_nodes.size();
879 // Verify the audio devices size.
880 AudioDeviceList audio_devices;
881 cras_audio_handler_->GetAudioDevices(&audio_devices);
882 EXPECT_EQ(init_nodes_size, audio_devices.size());
883 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
885 // Verify the internal speaker is selected as the active output initially.
886 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
887 AudioDevice active_output;
888 EXPECT_TRUE(
889 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
890 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
891 EXPECT_EQ(kInternalSpeaker.id,
892 cras_audio_handler_->GetPrimaryActiveOutputNode());
893 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
895 // Plug in usb headphone
896 audio_nodes.clear();
897 AudioNode internal_speaker(kInternalSpeaker);
898 internal_speaker.active = true;
899 internal_speaker.plugged_time = 80000000;
900 audio_nodes.push_back(internal_speaker);
901 AudioNode usb_headphone(kUSBHeadphone1);
902 usb_headphone.plugged_time = 90000000;
903 audio_nodes.push_back(usb_headphone);
904 ChangeAudioNodes(audio_nodes);
906 // Verify the AudioNodesChanged event is fired and new audio device is added.
907 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
908 cras_audio_handler_->GetAudioDevices(&audio_devices);
909 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
911 // Verify the active output device is switched to usb headphone, and
912 // ActiveOutputChanged event is fired.
913 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
914 EXPECT_TRUE(
915 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
916 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
917 EXPECT_EQ(kUSBHeadphone1.id,
918 cras_audio_handler_->GetPrimaryActiveOutputNode());
919 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
921 // Unplug usb headphone.
922 audio_nodes.clear();
923 audio_nodes.push_back(kInternalSpeaker);
924 ChangeAudioNodes(audio_nodes);
926 // Verify the AudioNodesChanged event is fired and one audio device is
927 // removed.
928 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
929 cras_audio_handler_->GetAudioDevices(&audio_devices);
930 EXPECT_EQ(init_nodes_size, audio_devices.size());
932 // Verify the active output device is switched to internal speaker, and
933 // ActiveOutputChanged event is fired.
934 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
935 EXPECT_TRUE(
936 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
937 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
938 EXPECT_EQ(kInternalSpeaker.id,
939 cras_audio_handler_->GetPrimaryActiveOutputNode());
940 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
943 TEST_F(CrasAudioHandlerTest, HandleMultipleUSBHeadphones) {
944 // Initialize with internal speaker and one usb headphone.
945 AudioNodeList audio_nodes;
946 audio_nodes.push_back(kInternalSpeaker);
947 audio_nodes.push_back(kUSBHeadphone1);
948 SetUpCrasAudioHandler(audio_nodes);
949 const size_t init_nodes_size = audio_nodes.size();
951 // Verify the audio devices size.
952 AudioDeviceList audio_devices;
953 cras_audio_handler_->GetAudioDevices(&audio_devices);
954 EXPECT_EQ(init_nodes_size, audio_devices.size());
955 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
957 // Verify the usb headphone is selected as the active output initially.
958 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
959 AudioDevice active_output;
960 EXPECT_TRUE(
961 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
962 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
963 EXPECT_EQ(kUSBHeadphone1.id,
964 cras_audio_handler_->GetPrimaryActiveOutputNode());
965 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
967 // Plug in another usb headphone.
968 audio_nodes.clear();
969 audio_nodes.push_back(kInternalSpeaker);
970 AudioNode usb_headphone_1(kUSBHeadphone1);
971 usb_headphone_1.active = true;
972 usb_headphone_1.plugged_time = 80000000;
973 audio_nodes.push_back(usb_headphone_1);
974 AudioNode usb_headphone_2(kUSBHeadphone2);
975 usb_headphone_2.plugged_time = 90000000;
976 audio_nodes.push_back(usb_headphone_2);
977 ChangeAudioNodes(audio_nodes);
979 // Verify the AudioNodesChanged event is fired and new audio device is added.
980 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
981 cras_audio_handler_->GetAudioDevices(&audio_devices);
982 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
984 // Verify the active output device is switched to the 2nd usb headphone, which
985 // is plugged later, and ActiveOutputChanged event is fired.
986 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
987 EXPECT_TRUE(
988 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
989 EXPECT_EQ(kUSBHeadphone2.id, active_output.id);
990 EXPECT_EQ(kUSBHeadphone2.id,
991 cras_audio_handler_->GetPrimaryActiveOutputNode());
992 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
994 // Unplug the 2nd usb headphone.
995 audio_nodes.clear();
996 audio_nodes.push_back(kInternalSpeaker);
997 audio_nodes.push_back(kUSBHeadphone1);
998 ChangeAudioNodes(audio_nodes);
1000 // Verify the AudioNodesChanged event is fired and one audio device is
1001 // removed.
1002 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1003 cras_audio_handler_->GetAudioDevices(&audio_devices);
1004 EXPECT_EQ(init_nodes_size, audio_devices.size());
1006 // Verify the active output device is switched to the first usb headphone, and
1007 // ActiveOutputChanged event is fired.
1008 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
1009 EXPECT_TRUE(
1010 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1011 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
1012 EXPECT_EQ(kUSBHeadphone1.id,
1013 cras_audio_handler_->GetPrimaryActiveOutputNode());
1014 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1017 TEST_F(CrasAudioHandlerTest, UnplugUSBHeadphonesWithActiveSpeaker) {
1018 // Initialize with internal speaker and one usb headphone.
1019 AudioNodeList audio_nodes;
1020 audio_nodes.push_back(kInternalSpeaker);
1021 audio_nodes.push_back(kUSBHeadphone1);
1022 SetUpCrasAudioHandler(audio_nodes);
1023 const size_t init_nodes_size = audio_nodes.size();
1025 // Verify the audio devices size.
1026 AudioDeviceList audio_devices;
1027 cras_audio_handler_->GetAudioDevices(&audio_devices);
1028 EXPECT_EQ(init_nodes_size, audio_devices.size());
1029 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1031 // Verify the usb headphone is selected as the active output initially.
1032 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1033 AudioDevice active_output;
1034 EXPECT_TRUE(
1035 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1036 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
1037 EXPECT_EQ(kUSBHeadphone1.id,
1038 cras_audio_handler_->GetPrimaryActiveOutputNode());
1039 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1041 // Plug in the headphone jack.
1042 audio_nodes.clear();
1043 audio_nodes.push_back(kInternalSpeaker);
1044 AudioNode usb_headphone_1(kUSBHeadphone1);
1045 usb_headphone_1.active = true;
1046 usb_headphone_1.plugged_time = 80000000;
1047 audio_nodes.push_back(usb_headphone_1);
1048 AudioNode headphone_jack(kHeadphone);
1049 headphone_jack.plugged_time = 90000000;
1050 audio_nodes.push_back(headphone_jack);
1051 ChangeAudioNodes(audio_nodes);
1053 // Verify the AudioNodesChanged event is fired and new audio device is added.
1054 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1055 cras_audio_handler_->GetAudioDevices(&audio_devices);
1056 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1058 // Verify the active output device is switched to the headphone jack, which
1059 // is plugged later, and ActiveOutputChanged event is fired.
1060 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1061 EXPECT_TRUE(
1062 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1063 EXPECT_EQ(kHeadphone.id, active_output.id);
1064 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1065 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1067 // Select the speaker to be the active output device.
1068 AudioDevice internal_speaker(kInternalSpeaker);
1069 cras_audio_handler_->SwitchToDevice(internal_speaker, true);
1071 // Verify the active output is switched to internal speaker, and the
1072 // ActiveOutputNodeChanged event is fired.
1073 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
1074 EXPECT_TRUE(
1075 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1076 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
1077 EXPECT_EQ(kInternalSpeaker.id,
1078 cras_audio_handler_->GetPrimaryActiveOutputNode());
1080 // Unplug the usb headphone.
1081 audio_nodes.clear();
1082 AudioNode internal_speaker_node(kInternalSpeaker);
1083 internal_speaker_node.active = true;
1084 internal_speaker_node.plugged_time = 70000000;
1085 audio_nodes.push_back(internal_speaker_node);
1086 headphone_jack.active = false;
1087 audio_nodes.push_back(headphone_jack);
1088 ChangeAudioNodes(audio_nodes);
1090 // Verify the AudioNodesChanged event is fired and one audio device is
1091 // removed.
1092 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1093 cras_audio_handler_->GetAudioDevices(&audio_devices);
1094 EXPECT_EQ(init_nodes_size, audio_devices.size());
1096 // Verify the active output device remains to be speaker.
1097 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
1098 EXPECT_TRUE(
1099 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1100 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
1101 EXPECT_EQ(kInternalSpeaker.id,
1102 cras_audio_handler_->GetPrimaryActiveOutputNode());
1103 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1106 TEST_F(CrasAudioHandlerTest, OneActiveAudioOutputAfterLoginNewUserSession) {
1107 // This tests the case found with crbug.com/273271.
1108 // Initialize with internal speaker, bluetooth headphone and headphone jack
1109 // for a new chrome session after user signs out from the previous session.
1110 // Headphone jack is plugged in later than bluetooth headphone, but bluetooth
1111 // headphone is selected as the active output by user from previous user
1112 // session.
1113 AudioNodeList audio_nodes;
1114 audio_nodes.push_back(kInternalSpeaker);
1115 AudioNode bluetooth_headphone(kBluetoothHeadset);
1116 bluetooth_headphone.active = true;
1117 bluetooth_headphone.plugged_time = 70000000;
1118 audio_nodes.push_back(bluetooth_headphone);
1119 AudioNode headphone_jack(kHeadphone);
1120 headphone_jack.plugged_time = 80000000;
1121 audio_nodes.push_back(headphone_jack);
1122 SetUpCrasAudioHandlerWithPrimaryActiveNode(audio_nodes, bluetooth_headphone);
1123 const size_t init_nodes_size = audio_nodes.size();
1125 // Verify the audio devices size.
1126 AudioDeviceList audio_devices;
1127 cras_audio_handler_->GetAudioDevices(&audio_devices);
1128 EXPECT_EQ(init_nodes_size, audio_devices.size());
1129 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1131 // Verify the headphone jack is selected as the active output and all other
1132 // audio devices are not active.
1133 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1134 AudioDevice active_output;
1135 EXPECT_TRUE(
1136 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1137 EXPECT_EQ(kHeadphone.id, active_output.id);
1138 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1139 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1140 for (size_t i = 0; i < audio_devices.size(); ++i) {
1141 if (audio_devices[i].id != kHeadphone.id)
1142 EXPECT_FALSE(audio_devices[i].active);
1146 TEST_F(CrasAudioHandlerTest, BluetoothSpeakerIdChangedOnFly) {
1147 // Initialize with internal speaker and bluetooth headset.
1148 AudioNodeList audio_nodes;
1149 audio_nodes.push_back(kInternalSpeaker);
1150 audio_nodes.push_back(kBluetoothHeadset);
1151 SetUpCrasAudioHandler(audio_nodes);
1152 const size_t init_nodes_size = audio_nodes.size();
1154 // Verify the audio devices size.
1155 AudioDeviceList audio_devices;
1156 cras_audio_handler_->GetAudioDevices(&audio_devices);
1157 EXPECT_EQ(init_nodes_size, audio_devices.size());
1158 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1160 // Verify the bluetooth headset is selected as the active output and all other
1161 // audio devices are not active.
1162 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1163 AudioDevice active_output;
1164 EXPECT_TRUE(
1165 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1166 EXPECT_EQ(kBluetoothHeadset.id, active_output.id);
1167 EXPECT_EQ(kBluetoothHeadset.id,
1168 cras_audio_handler_->GetPrimaryActiveOutputNode());
1169 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1171 // Cras changes the bluetooth headset's id on the fly.
1172 audio_nodes.clear();
1173 AudioNode internal_speaker(kInternalSpeaker);
1174 internal_speaker.active = false;
1175 audio_nodes.push_back(internal_speaker);
1176 AudioNode bluetooth_headphone(kBluetoothHeadset);
1177 // Change bluetooth headphone id.
1178 bluetooth_headphone.id = kBluetoothHeadsetId + 20000;
1179 bluetooth_headphone.active = false;
1180 audio_nodes.push_back(bluetooth_headphone);
1181 ChangeAudioNodes(audio_nodes);
1183 // Verify NodesChanged event is fired, and the audio devices size is not
1184 // changed.
1185 audio_devices.clear();
1186 cras_audio_handler_->GetAudioDevices(&audio_devices);
1187 EXPECT_EQ(init_nodes_size, audio_devices.size());
1188 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1190 // Verify ActiveOutputNodeChanged event is fired, and active device should be
1191 // bluetooth headphone.
1192 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1193 EXPECT_TRUE(
1194 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1195 EXPECT_EQ(bluetooth_headphone.id, active_output.id);
1198 TEST_F(CrasAudioHandlerTest, PlugUSBMic) {
1199 // Set up initial audio devices, only with internal mic.
1200 AudioNodeList audio_nodes;
1201 audio_nodes.push_back(kInternalMic);
1202 SetUpCrasAudioHandler(audio_nodes);
1203 const size_t init_nodes_size = audio_nodes.size();
1205 // Verify the audio devices size.
1206 AudioDeviceList audio_devices;
1207 cras_audio_handler_->GetAudioDevices(&audio_devices);
1208 EXPECT_EQ(init_nodes_size, audio_devices.size());
1209 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1211 // Verify the internal mic is selected as the active input.
1212 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1213 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1214 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1216 // Plug the USB Mic.
1217 audio_nodes.clear();
1218 AudioNode internal_mic(kInternalMic);
1219 internal_mic.active = true;
1220 audio_nodes.push_back(internal_mic);
1221 audio_nodes.push_back(kUSBMic);
1222 ChangeAudioNodes(audio_nodes);
1224 // Verify the AudioNodesChanged event is fired and new audio device is added.
1225 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1226 cras_audio_handler_->GetAudioDevices(&audio_devices);
1227 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1229 // Verify the active input device is switched to USB mic and
1230 // and ActiveInputChanged event is fired.
1231 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
1232 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
1233 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1236 TEST_F(CrasAudioHandlerTest, UnplugUSBMic) {
1237 // Set up initial audio devices, with internal mic and USB Mic.
1238 AudioNodeList audio_nodes;
1239 audio_nodes.push_back(kInternalMic);
1240 audio_nodes.push_back(kUSBMic);
1241 SetUpCrasAudioHandler(audio_nodes);
1242 const size_t init_nodes_size = audio_nodes.size();
1244 // Verify the audio devices size.
1245 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1246 AudioDeviceList audio_devices;
1247 cras_audio_handler_->GetAudioDevices(&audio_devices);
1248 EXPECT_EQ(init_nodes_size, audio_devices.size());
1250 // Verify the USB mic is selected as the active output.
1251 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1252 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
1253 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1255 // Unplug the USB Mic.
1256 audio_nodes.clear();
1257 audio_nodes.push_back(kInternalMic);
1258 ChangeAudioNodes(audio_nodes);
1260 // Verify the AudioNodesChanged event is fired, and one audio device is
1261 // removed.
1262 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1263 cras_audio_handler_->GetAudioDevices(&audio_devices);
1264 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
1266 // Verify the active input device is switched to internal mic, and
1267 // and ActiveInputChanged event is fired.
1268 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
1269 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1270 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1273 TEST_F(CrasAudioHandlerTest, PlugUSBMicNotAffectActiveOutput) {
1274 // Set up initial audio devices.
1275 AudioNodeList audio_nodes;
1276 audio_nodes.push_back(kInternalSpeaker);
1277 audio_nodes.push_back(kHeadphone);
1278 audio_nodes.push_back(kInternalMic);
1279 SetUpCrasAudioHandler(audio_nodes);
1280 const size_t init_nodes_size = audio_nodes.size();
1282 // Verify the audio devices size.
1283 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1284 AudioDeviceList audio_devices;
1285 cras_audio_handler_->GetAudioDevices(&audio_devices);
1286 EXPECT_EQ(init_nodes_size, audio_devices.size());
1288 // Verify the internal mic is selected as the active input.
1289 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1290 EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
1291 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1293 // Verify the headphone is selected as the active output.
1294 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1295 EXPECT_EQ(kHeadphoneId, cras_audio_handler_->GetPrimaryActiveOutputNode());
1296 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1298 // Switch the active output to internal speaker.
1299 AudioDevice internal_speaker(kInternalSpeaker);
1300 cras_audio_handler_->SwitchToDevice(internal_speaker, true);
1302 // Verify the active output is switched to internal speaker, and the
1303 // ActiveOutputNodeChanged event is fired.
1304 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1305 AudioDevice active_output;
1306 EXPECT_TRUE(
1307 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1308 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
1309 EXPECT_EQ(kInternalSpeaker.id,
1310 cras_audio_handler_->GetPrimaryActiveOutputNode());
1312 // Plug the USB Mic.
1313 audio_nodes.clear();
1314 AudioNode internal_speaker_node(kInternalSpeaker);
1315 internal_speaker_node.active = true;
1316 audio_nodes.push_back(internal_speaker_node);
1317 audio_nodes.push_back(kHeadphone);
1318 AudioNode internal_mic(kInternalMic);
1319 internal_mic.active = true;
1320 audio_nodes.push_back(internal_mic);
1321 audio_nodes.push_back(kUSBMic);
1322 ChangeAudioNodes(audio_nodes);
1324 // Verify the AudioNodesChanged event is fired, one new device is added.
1325 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1326 cras_audio_handler_->GetAudioDevices(&audio_devices);
1327 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1329 // Verify the active input device is switched to USB mic, and
1330 // and ActiveInputChanged event is fired.
1331 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
1332 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1333 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1335 // Verify the active output device is not changed.
1336 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1337 EXPECT_TRUE(
1338 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1339 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
1340 EXPECT_EQ(kInternalSpeaker.id,
1341 cras_audio_handler_->GetPrimaryActiveOutputNode());
1344 TEST_F(CrasAudioHandlerTest, PlugHeadphoneAutoUnplugSpeakerWithActiveUSB) {
1345 // Set up initial audio devices.
1346 AudioNodeList audio_nodes;
1347 audio_nodes.push_back(kUSBHeadphone1);
1348 audio_nodes.push_back(kInternalSpeaker);
1349 audio_nodes.push_back(kInternalMic);
1350 SetUpCrasAudioHandler(audio_nodes);
1351 const size_t init_nodes_size = audio_nodes.size();
1353 // Verify the audio devices size.
1354 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1355 AudioDeviceList audio_devices;
1356 cras_audio_handler_->GetAudioDevices(&audio_devices);
1357 EXPECT_EQ(init_nodes_size, audio_devices.size());
1359 // Verify the internal mic is selected as the active input.
1360 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1361 EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
1362 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1364 // Verify the USB headphone is selected as the active output.
1365 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1366 EXPECT_EQ(kUSBHeadphoneId1,
1367 cras_audio_handler_->GetPrimaryActiveOutputNode());
1368 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1370 // Plug the headphone and auto-unplug internal speaker.
1371 audio_nodes.clear();
1372 AudioNode usb_headphone_node(kUSBHeadphone1);
1373 usb_headphone_node.active = true;
1374 audio_nodes.push_back(usb_headphone_node);
1375 AudioNode headphone_node(kHeadphone);
1376 headphone_node.plugged_time = 1000;
1377 audio_nodes.push_back(headphone_node);
1378 AudioNode internal_mic(kInternalMic);
1379 internal_mic.active = true;
1380 audio_nodes.push_back(internal_mic);
1381 ChangeAudioNodes(audio_nodes);
1383 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1384 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1385 cras_audio_handler_->GetAudioDevices(&audio_devices);
1386 EXPECT_EQ(init_nodes_size, audio_devices.size());
1388 // Verify the active output device is switched to headphone, and
1389 // an ActiveOutputChanged event is fired.
1390 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1391 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1392 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1394 // Unplug the headphone and internal speaker auto-plugs back.
1395 audio_nodes.clear();
1396 audio_nodes.push_back(kUSBHeadphone1);
1397 AudioNode internal_speaker_node(kInternalSpeaker);
1398 internal_speaker_node.plugged_time = 2000;
1399 audio_nodes.push_back(internal_speaker_node);
1400 audio_nodes.push_back(internal_mic);
1401 ChangeAudioNodes(audio_nodes);
1403 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1404 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1405 cras_audio_handler_->GetAudioDevices(&audio_devices);
1406 EXPECT_EQ(init_nodes_size, audio_devices.size());
1408 // Verify the active output device is switched back to USB, and
1409 // an ActiveOutputChanged event is fired.
1410 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
1411 EXPECT_EQ(kUSBHeadphone1.id,
1412 cras_audio_handler_->GetPrimaryActiveOutputNode());
1413 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1415 // Verify the active input device is not changed.
1416 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1417 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1420 TEST_F(CrasAudioHandlerTest, PlugMicAutoUnplugInternalMicWithActiveUSB) {
1421 // Set up initial audio devices.
1422 AudioNodeList audio_nodes;
1423 audio_nodes.push_back(kUSBHeadphone1);
1424 audio_nodes.push_back(kInternalSpeaker);
1425 audio_nodes.push_back(kUSBMic);
1426 audio_nodes.push_back(kInternalMic);
1427 SetUpCrasAudioHandler(audio_nodes);
1428 const size_t init_nodes_size = audio_nodes.size();
1430 // Verify the audio devices size.
1431 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1432 AudioDeviceList audio_devices;
1433 cras_audio_handler_->GetAudioDevices(&audio_devices);
1434 EXPECT_EQ(init_nodes_size, audio_devices.size());
1436 // Verify the internal mic is selected as the active input.
1437 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1438 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
1439 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1441 // Verify the internal speaker is selected as the active output.
1442 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1443 EXPECT_EQ(kUSBHeadphoneId1,
1444 cras_audio_handler_->GetPrimaryActiveOutputNode());
1445 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1447 // Plug the headphone and mic, auto-unplug internal mic and speaker.
1448 audio_nodes.clear();
1449 AudioNode usb_headphone_node(kUSBHeadphone1);
1450 usb_headphone_node.active = true;
1451 audio_nodes.push_back(usb_headphone_node);
1452 AudioNode headphone_node(kHeadphone);
1453 headphone_node.plugged_time = 1000;
1454 audio_nodes.push_back(headphone_node);
1455 AudioNode usb_mic(kUSBMic);
1456 usb_mic.active = true;
1457 audio_nodes.push_back(usb_mic);
1458 AudioNode mic_jack(kMicJack);
1459 mic_jack.plugged_time = 1000;
1460 audio_nodes.push_back(mic_jack);
1461 ChangeAudioNodes(audio_nodes);
1463 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1464 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1465 cras_audio_handler_->GetAudioDevices(&audio_devices);
1466 EXPECT_EQ(init_nodes_size, audio_devices.size());
1468 // Verify the active output device is switched to headphone, and
1469 // an ActiveOutputChanged event is fired.
1470 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1471 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1472 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1474 // Verify the active input device is switched to mic jack, and
1475 // an ActiveInputChanged event is fired.
1476 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
1477 EXPECT_EQ(kMicJack.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1478 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1480 // Unplug the headphone and internal speaker auto-plugs back.
1481 audio_nodes.clear();
1482 audio_nodes.push_back(kUSBHeadphone1);
1483 AudioNode internal_speaker_node(kInternalSpeaker);
1484 internal_speaker_node.plugged_time = 2000;
1485 audio_nodes.push_back(internal_speaker_node);
1486 audio_nodes.push_back(kUSBMic);
1487 AudioNode internal_mic(kInternalMic);
1488 internal_mic.plugged_time = 2000;
1489 audio_nodes.push_back(internal_mic);
1490 ChangeAudioNodes(audio_nodes);
1492 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1493 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1494 cras_audio_handler_->GetAudioDevices(&audio_devices);
1495 EXPECT_EQ(init_nodes_size, audio_devices.size());
1497 // Verify the active output device is switched back to USB, and
1498 // an ActiveOutputChanged event is fired.
1499 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
1500 EXPECT_EQ(kUSBHeadphone1.id,
1501 cras_audio_handler_->GetPrimaryActiveOutputNode());
1502 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1504 // Verify the active input device is switched back to USB mic, and
1505 // an ActiveInputChanged event is fired.
1506 EXPECT_EQ(2, test_observer_->active_input_node_changed_count());
1507 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1508 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1511 TEST_F(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnPlugInHeadphone) {
1512 // Set up initial audio devices.
1513 AudioNodeList audio_nodes;
1514 audio_nodes.push_back(kInternalSpeaker);
1515 audio_nodes.push_back(kBluetoothHeadset);
1516 SetUpCrasAudioHandler(audio_nodes);
1517 const size_t init_nodes_size = audio_nodes.size();
1519 // Verify the audio devices size.
1520 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1521 AudioDeviceList audio_devices;
1522 cras_audio_handler_->GetAudioDevices(&audio_devices);
1523 EXPECT_EQ(init_nodes_size, audio_devices.size());
1525 // Verify the bluetooth headset is selected as the active output.
1526 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1527 EXPECT_EQ(kBluetoothHeadsetId,
1528 cras_audio_handler_->GetPrimaryActiveOutputNode());
1529 AudioDevice active_output;
1530 EXPECT_TRUE(
1531 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1532 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1534 // Plug in headphone, but fire NodesChanged signal twice.
1535 audio_nodes.clear();
1536 audio_nodes.push_back(kInternalSpeaker);
1537 AudioNode bluetooth_headset(kBluetoothHeadset);
1538 bluetooth_headset.plugged_time = 1000;
1539 bluetooth_headset.active = true;
1540 audio_nodes.push_back(bluetooth_headset);
1541 AudioNode headphone(kHeadphone);
1542 headphone.active = false;
1543 headphone.plugged_time = 2000;
1544 audio_nodes.push_back(headphone);
1545 ChangeAudioNodes(audio_nodes);
1546 ChangeAudioNodes(audio_nodes);
1548 // Verify the active output device is set to headphone.
1549 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1550 EXPECT_LE(1, test_observer_->active_output_node_changed_count());
1551 EXPECT_EQ(headphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1552 EXPECT_TRUE(
1553 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1554 EXPECT_EQ(headphone.id, active_output.id);
1556 // Verfiy the audio devices data is consistent, i.e., the active output device
1557 // should be headphone.
1558 cras_audio_handler_->GetAudioDevices(&audio_devices);
1559 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1560 for (size_t i = 0; i < audio_devices.size(); ++i) {
1561 if (audio_devices[i].id == kInternalSpeaker.id)
1562 EXPECT_FALSE(audio_devices[i].active);
1563 else if (audio_devices[i].id == bluetooth_headset.id)
1564 EXPECT_FALSE(audio_devices[i].active);
1565 else if (audio_devices[i].id == headphone.id)
1566 EXPECT_TRUE(audio_devices[i].active);
1567 else
1568 NOTREACHED();
1572 TEST_F(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnPlugInUSBMic) {
1573 // Set up initial audio devices.
1574 AudioNodeList audio_nodes;
1575 audio_nodes.push_back(kInternalMic);
1576 SetUpCrasAudioHandler(audio_nodes);
1577 const size_t init_nodes_size = audio_nodes.size();
1579 // Verify the audio devices size.
1580 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1581 AudioDeviceList audio_devices;
1582 cras_audio_handler_->GetAudioDevices(&audio_devices);
1583 EXPECT_EQ(init_nodes_size, audio_devices.size());
1585 // Verify the internal mic is selected as the active output.
1586 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1587 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1588 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
1589 EXPECT_TRUE(audio_devices[0].active);
1591 // Plug in usb mic, but fire NodesChanged signal twice.
1592 audio_nodes.clear();
1593 AudioNode internal_mic(kInternalMic);
1594 internal_mic.active = true;
1595 internal_mic.plugged_time = 1000;
1596 audio_nodes.push_back(internal_mic);
1597 AudioNode usb_mic(kUSBMic);
1598 usb_mic.active = false;
1599 usb_mic.plugged_time = 2000;
1600 audio_nodes.push_back(usb_mic);
1601 ChangeAudioNodes(audio_nodes);
1602 ChangeAudioNodes(audio_nodes);
1604 // Verify the active output device is set to headphone.
1605 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1606 EXPECT_LE(1, test_observer_->active_input_node_changed_count());
1607 EXPECT_EQ(usb_mic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1608 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1610 // Verfiy the audio devices data is consistent, i.e., the active input device
1611 // should be usb mic.
1612 cras_audio_handler_->GetAudioDevices(&audio_devices);
1613 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1614 for (size_t i = 0; i < audio_devices.size(); ++i) {
1615 if (audio_devices[i].id == kInternalMic.id)
1616 EXPECT_FALSE(audio_devices[i].active);
1617 else if (audio_devices[i].id == usb_mic.id)
1618 EXPECT_TRUE(audio_devices[i].active);
1619 else
1620 NOTREACHED();
1624 // This is the case of crbug.com/291303.
1625 TEST_F(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnSystemBoot) {
1626 // Set up audio handler with empty audio_nodes.
1627 AudioNodeList audio_nodes;
1628 SetUpCrasAudioHandler(audio_nodes);
1630 AudioNode internal_speaker(kInternalSpeaker);
1631 internal_speaker.active = false;
1632 AudioNode headphone(kHeadphone);
1633 headphone.active = false;
1634 AudioNode internal_mic(kInternalMic);
1635 internal_mic.active = false;
1636 audio_nodes.push_back(internal_speaker);
1637 audio_nodes.push_back(headphone);
1638 audio_nodes.push_back(internal_mic);
1639 const size_t init_nodes_size = audio_nodes.size();
1641 // Simulate AudioNodesChanged signal being fired twice during system boot.
1642 ChangeAudioNodes(audio_nodes);
1643 ChangeAudioNodes(audio_nodes);
1645 // Verify the active output device is set to headphone.
1646 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1647 EXPECT_LE(1, test_observer_->active_output_node_changed_count());
1648 EXPECT_EQ(headphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1649 AudioDevice active_output;
1650 EXPECT_TRUE(
1651 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1652 EXPECT_EQ(headphone.id, active_output.id);
1654 // Verify the active input device id is set to internal mic.
1655 EXPECT_EQ(internal_mic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1657 // Verfiy the audio devices data is consistent, i.e., the active output device
1658 // should be headphone, and the active input device should internal mic.
1659 AudioDeviceList audio_devices;
1660 cras_audio_handler_->GetAudioDevices(&audio_devices);
1661 EXPECT_EQ(init_nodes_size, audio_devices.size());
1662 for (size_t i = 0; i < audio_devices.size(); ++i) {
1663 if (audio_devices[i].id == internal_speaker.id)
1664 EXPECT_FALSE(audio_devices[i].active);
1665 else if (audio_devices[i].id == headphone.id)
1666 EXPECT_TRUE(audio_devices[i].active);
1667 else if (audio_devices[i].id == internal_mic.id)
1668 EXPECT_TRUE(audio_devices[i].active);
1669 else
1670 NOTREACHED();
1674 // This is the case of crbug.com/448924.
1675 TEST_F(CrasAudioHandlerTest,
1676 TwoNodesChangedSignalsForLosingTowNodesOnOneUnplug) {
1677 // Set up audio handler with 4 audio_nodes.
1678 AudioNodeList audio_nodes;
1679 AudioNode internal_speaker(kInternalSpeaker);
1680 internal_speaker.active = false;
1681 AudioNode headphone(kHeadphone);
1682 headphone.active = false;
1683 AudioNode internal_mic(kInternalMic);
1684 internal_mic.active = false;
1685 AudioNode micJack(kMicJack);
1686 micJack.active = false;
1687 audio_nodes.push_back(internal_speaker);
1688 audio_nodes.push_back(headphone);
1689 audio_nodes.push_back(internal_mic);
1690 audio_nodes.push_back(micJack);
1691 SetUpCrasAudioHandler(audio_nodes);
1693 // Verify the audio devices size.
1694 AudioDeviceList audio_devices;
1695 cras_audio_handler_->GetAudioDevices(&audio_devices);
1696 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
1698 // Verify the headphone has been selected as the active output.
1699 AudioDevice active_output;
1700 EXPECT_TRUE(
1701 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1702 EXPECT_EQ(kHeadphone.id, active_output.id);
1703 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1704 EXPECT_TRUE(active_output.active);
1705 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1707 // Verify the mic Jack has been selected as the active input.
1708 EXPECT_EQ(micJack.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1709 const AudioDevice* active_input = GetDeviceFromId(micJack.id);
1710 EXPECT_TRUE(active_input->active);
1711 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1713 // Simulate the nodes list in first NodesChanged signal, only headphone is
1714 // removed, other nodes remains the same.
1715 AudioNodeList changed_nodes_1;
1716 internal_speaker.active = false;
1717 changed_nodes_1.push_back(internal_speaker);
1718 internal_mic.active = false;
1719 changed_nodes_1.push_back(internal_mic);
1720 micJack.active = true;
1721 changed_nodes_1.push_back(micJack);
1723 // Simulate the nodes list in second NodesChanged signal, the micJac is
1724 // removed, but the internal_mic is inactive, which does not reflect the
1725 // active status set from the first NodesChanged signal since this was sent
1726 // before cras receives the SetActiveOutputNode from the first NodesChanged
1727 // handling.
1728 AudioNodeList changed_nodes_2;
1729 changed_nodes_2.push_back(internal_speaker);
1730 changed_nodes_2.push_back(internal_mic);
1732 // Simulate AudioNodesChanged signal being fired twice for unplug an audio
1733 // device with both input and output nodes on it.
1734 ChangeAudioNodes(changed_nodes_1);
1735 ChangeAudioNodes(changed_nodes_2);
1737 AudioDeviceList changed_devices;
1738 cras_audio_handler_->GetAudioDevices(&changed_devices);
1739 EXPECT_EQ(2u, changed_devices.size());
1741 // Verify the active output device is set to internal speaker.
1742 EXPECT_EQ(internal_speaker.id,
1743 cras_audio_handler_->GetPrimaryActiveOutputNode());
1744 EXPECT_TRUE(
1745 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1746 EXPECT_EQ(internal_speaker.id, active_output.id);
1747 EXPECT_TRUE(active_output.active);
1749 // Verify the active input device id is set to internal mic.
1750 EXPECT_EQ(internal_mic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1751 const AudioDevice* changed_active_input = GetDeviceFromId(internal_mic.id);
1752 EXPECT_TRUE(changed_active_input->active);
1755 TEST_F(CrasAudioHandlerTest, SetOutputMute) {
1756 AudioNodeList audio_nodes;
1757 audio_nodes.push_back(kInternalSpeaker);
1758 SetUpCrasAudioHandler(audio_nodes);
1759 EXPECT_EQ(0, test_observer_->output_mute_changed_count());
1761 // Mute the device.
1762 cras_audio_handler_->SetOutputMute(true);
1764 // Verify the output is muted, OnOutputMuteChanged event is fired,
1765 // and mute value is saved in the preferences.
1766 EXPECT_TRUE(cras_audio_handler_->IsOutputMuted());
1767 EXPECT_EQ(1, test_observer_->output_mute_changed_count());
1768 AudioDevice speaker(kInternalSpeaker);
1769 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(speaker));
1771 // Unmute the device.
1772 cras_audio_handler_->SetOutputMute(false);
1774 // Verify the output is unmuted, OnOutputMuteChanged event is fired,
1775 // and mute value is saved in the preferences.
1776 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
1777 EXPECT_EQ(2, test_observer_->output_mute_changed_count());
1778 EXPECT_FALSE(audio_pref_handler_->GetMuteValue(speaker));
1781 TEST_F(CrasAudioHandlerTest, SetInputMute) {
1782 AudioNodeList audio_nodes;
1783 audio_nodes.push_back(kInternalMic);
1784 SetUpCrasAudioHandler(audio_nodes);
1785 EXPECT_EQ(0, test_observer_->input_mute_changed_count());
1787 // Mute the device.
1788 cras_audio_handler_->SetInputMute(true);
1790 // Verify the input is muted, OnInputMuteChanged event is fired.
1791 EXPECT_TRUE(cras_audio_handler_->IsInputMuted());
1792 EXPECT_EQ(1, test_observer_->input_mute_changed_count());
1794 // Unmute the device.
1795 cras_audio_handler_->SetInputMute(false);
1797 // Verify the input is unmuted, OnInputMuteChanged event is fired.
1798 EXPECT_FALSE(cras_audio_handler_->IsInputMuted());
1799 EXPECT_EQ(2, test_observer_->input_mute_changed_count());
1802 TEST_F(CrasAudioHandlerTest, SetOutputVolumePercent) {
1803 AudioNodeList audio_nodes;
1804 audio_nodes.push_back(kInternalSpeaker);
1805 SetUpCrasAudioHandler(audio_nodes);
1806 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
1808 cras_audio_handler_->SetOutputVolumePercent(60);
1810 // Verify the output volume is changed to the designated value,
1811 // OnOutputNodeVolumeChanged event is fired, and the device volume value
1812 // is saved the preferences.
1813 const int kVolume = 60;
1814 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent());
1815 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
1816 AudioDevice device;
1817 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device));
1818 EXPECT_EQ(device.id, kInternalSpeaker.id);
1819 EXPECT_EQ(kVolume, audio_pref_handler_->GetOutputVolumeValue(&device));
1822 TEST_F(CrasAudioHandlerTest, SetInputGainPercent) {
1823 AudioNodeList audio_nodes;
1824 audio_nodes.push_back(kInternalMic);
1825 SetUpCrasAudioHandler(audio_nodes);
1826 EXPECT_EQ(0, test_observer_->input_gain_changed_count());
1828 cras_audio_handler_->SetInputGainPercent(60);
1830 // Verify the input gain changed to the designated value,
1831 // OnInputNodeGainChanged event is fired, and the device gain value
1832 // is saved in the preferences.
1833 const int kGain = 60;
1834 EXPECT_EQ(kGain, cras_audio_handler_->GetInputGainPercent());
1835 EXPECT_EQ(1, test_observer_->input_gain_changed_count());
1836 AudioDevice internal_mic(kInternalMic);
1837 EXPECT_EQ(kGain, audio_pref_handler_->GetInputGainValue(&internal_mic));
1840 TEST_F(CrasAudioHandlerTest, SetMuteForDevice) {
1841 AudioNodeList audio_nodes;
1842 audio_nodes.push_back(kInternalSpeaker);
1843 audio_nodes.push_back(kHeadphone);
1844 audio_nodes.push_back(kInternalMic);
1845 audio_nodes.push_back(kUSBMic);
1846 SetUpCrasAudioHandler(audio_nodes);
1848 // Mute the active output device.
1849 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1850 cras_audio_handler_->SetMuteForDevice(kHeadphone.id, true);
1852 // Verify the headphone is muted and mute value is saved in the preferences.
1853 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kHeadphone.id));
1854 AudioDevice headphone(kHeadphone);
1855 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(headphone));
1857 // Mute the non-active output device.
1858 cras_audio_handler_->SetMuteForDevice(kInternalSpeaker.id, true);
1860 // Verify the internal speaker is muted and mute value is saved in the
1861 // preferences.
1862 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kInternalSpeaker.id));
1863 AudioDevice internal_speaker(kInternalSpeaker);
1864 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(internal_speaker));
1866 // Mute the active input device.
1867 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1868 cras_audio_handler_->SetMuteForDevice(kUSBMic.id, true);
1870 // Verify the USB Mic is muted.
1871 EXPECT_TRUE(cras_audio_handler_->IsInputMutedForDevice(kUSBMic.id));
1873 // Mute the non-active input device should be a no-op, see crbug.com/365050.
1874 cras_audio_handler_->SetMuteForDevice(kInternalMic.id, true);
1876 // Verify IsInputMutedForDevice returns false for non-active input device.
1877 EXPECT_FALSE(cras_audio_handler_->IsInputMutedForDevice(kInternalMic.id));
1880 TEST_F(CrasAudioHandlerTest, SetVolumeGainPercentForDevice) {
1881 AudioNodeList audio_nodes;
1882 audio_nodes.push_back(kInternalSpeaker);
1883 audio_nodes.push_back(kHeadphone);
1884 audio_nodes.push_back(kInternalMic);
1885 audio_nodes.push_back(kUSBMic);
1886 SetUpCrasAudioHandler(audio_nodes);
1888 // Set volume percent for active output device.
1889 const int kHeadphoneVolume = 30;
1890 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1891 cras_audio_handler_->SetVolumeGainPercentForDevice(kHeadphone.id,
1892 kHeadphoneVolume);
1894 // Verify the volume percent of headphone is set, and saved in preferences.
1895 EXPECT_EQ(kHeadphoneVolume,
1896 cras_audio_handler_->GetOutputVolumePercentForDevice(
1897 kHeadphone.id));
1898 AudioDevice headphone(kHeadphone);
1899 EXPECT_EQ(kHeadphoneVolume,
1900 audio_pref_handler_->GetOutputVolumeValue(&headphone));
1902 // Set volume percent for non-active output device.
1903 const int kSpeakerVolume = 60;
1904 cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalSpeaker.id,
1905 kSpeakerVolume);
1907 // Verify the volume percent of speaker is set, and saved in preferences.
1908 EXPECT_EQ(kSpeakerVolume,
1909 cras_audio_handler_->GetOutputVolumePercentForDevice(
1910 kInternalSpeaker.id));
1911 AudioDevice speaker(kInternalSpeaker);
1912 EXPECT_EQ(kSpeakerVolume,
1913 audio_pref_handler_->GetOutputVolumeValue(&speaker));
1915 // Set gain percent for active input device.
1916 const int kUSBMicGain = 30;
1917 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1918 cras_audio_handler_->SetVolumeGainPercentForDevice(kUSBMic.id,
1919 kUSBMicGain);
1921 // Verify the gain percent of USB mic is set, and saved in preferences.
1922 EXPECT_EQ(kUSBMicGain,
1923 cras_audio_handler_->GetOutputVolumePercentForDevice(kUSBMic.id));
1924 AudioDevice usb_mic(kHeadphone);
1925 EXPECT_EQ(kUSBMicGain,
1926 audio_pref_handler_->GetInputGainValue(&usb_mic));
1928 // Set gain percent for non-active input device.
1929 const int kInternalMicGain = 60;
1930 cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalMic.id,
1931 kInternalMicGain);
1933 // Verify the gain percent of internal mic is set, and saved in preferences.
1934 EXPECT_EQ(kInternalMicGain,
1935 cras_audio_handler_->GetOutputVolumePercentForDevice(
1936 kInternalMic.id));
1937 AudioDevice internal_mic(kInternalMic);
1938 EXPECT_EQ(kInternalMicGain,
1939 audio_pref_handler_->GetInputGainValue(&internal_mic));
1942 TEST_F(CrasAudioHandlerTest, HandleOtherDeviceType) {
1943 const size_t kNumValidAudioDevices = 4;
1944 AudioNodeList audio_nodes;
1945 audio_nodes.push_back(kInternalSpeaker);
1946 audio_nodes.push_back(kOtherTypeOutput);
1947 audio_nodes.push_back(kInternalMic);
1948 audio_nodes.push_back(kOtherTypeInput);
1949 SetUpCrasAudioHandler(audio_nodes);
1951 // Verify the audio devices size.
1952 AudioDeviceList audio_devices;
1953 cras_audio_handler_->GetAudioDevices(&audio_devices);
1954 EXPECT_EQ(kNumValidAudioDevices, audio_devices.size());
1956 // Verify the internal speaker has been selected as the active output,
1957 // and the output device with some randown unknown type is handled gracefully.
1958 AudioDevice active_output;
1959 EXPECT_TRUE(
1960 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1961 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
1962 EXPECT_EQ(kInternalSpeaker.id,
1963 cras_audio_handler_->GetPrimaryActiveOutputNode());
1964 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1966 // Ensure the internal microphone has been selected as the active input,
1967 // and the input device with some random unknown type is handled gracefully.
1968 AudioDevice active_input;
1969 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1970 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1973 TEST_F(CrasAudioHandlerTest, ChangeActiveNodesHotrodInit) {
1974 AudioNodeList audio_nodes;
1975 audio_nodes.push_back(kHDMIOutput);
1976 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
1977 audio_nodes.push_back(kUSBJabraSpeakerOutput2);
1978 audio_nodes.push_back(kUSBJabraSpeakerInput1);
1979 audio_nodes.push_back(kUSBJabraSpeakerInput2);
1980 audio_nodes.push_back(kUSBCameraInput);
1981 SetUpCrasAudioHandler(audio_nodes);
1983 // Verify the audio devices size.
1984 AudioDeviceList audio_devices;
1985 cras_audio_handler_->GetAudioDevices(&audio_devices);
1986 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
1988 // Verify only the 1st jabra speaker's output and input are selected as active
1989 // nodes by CrasAudioHandler.
1990 AudioDevice active_output;
1991 EXPECT_TRUE(
1992 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1993 EXPECT_EQ(2, GetActiveDeviceCount());
1994 AudioDevice primary_active_device;
1995 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(
1996 &primary_active_device));
1997 EXPECT_EQ(kUSBJabraSpeakerOutput1.id, primary_active_device.id);
1998 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
1999 cras_audio_handler_->GetPrimaryActiveInputNode());
2001 // Set both jabra speakers's input and output nodes to active, this simulate
2002 // the call sent by hotrod initialization process.
2003 test_observer_->reset_active_output_node_changed_count();
2004 test_observer_->reset_active_input_node_changed_count();
2005 CrasAudioHandler::NodeIdList active_nodes;
2006 active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2007 active_nodes.push_back(kUSBJabraSpeakerOutput2.id);
2008 active_nodes.push_back(kUSBJabraSpeakerInput1.id);
2009 active_nodes.push_back(kUSBJabraSpeakerInput2.id);
2010 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2012 // Verify both jabra speakers' input/output nodes are made active.
2013 // num_active_nodes = GetActiveDeviceCount();
2014 EXPECT_EQ(4, GetActiveDeviceCount());
2015 const AudioDevice* active_output_1 =
2016 GetDeviceFromId(kUSBJabraSpeakerOutput1.id);
2017 EXPECT_TRUE(active_output_1->active);
2018 const AudioDevice* active_output_2 =
2019 GetDeviceFromId(kUSBJabraSpeakerOutput2.id);
2020 EXPECT_TRUE(active_output_2->active);
2021 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(
2022 &primary_active_device));
2023 EXPECT_EQ(kUSBJabraSpeakerOutput1.id, primary_active_device.id);
2024 const AudioDevice* active_input_1 =
2025 GetDeviceFromId(kUSBJabraSpeakerInput1.id);
2026 EXPECT_TRUE(active_input_1->active);
2027 const AudioDevice* active_input_2 =
2028 GetDeviceFromId(kUSBJabraSpeakerInput2.id);
2029 EXPECT_TRUE(active_input_2->active);
2030 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2031 cras_audio_handler_->GetPrimaryActiveInputNode());
2033 // Verify only 1 ActiveOutputNodeChanged notification has been sent out
2034 // by calling ChangeActiveNodes.
2035 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
2036 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
2038 // Verify all active devices are the not muted and their volume values are
2039 // the same.
2040 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
2041 EXPECT_FALSE(
2042 cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput1.id));
2043 EXPECT_FALSE(
2044 cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput2.id));
2045 EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(),
2046 cras_audio_handler_->GetOutputVolumePercentForDevice(
2047 kUSBJabraSpeakerOutput1.id));
2048 EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(),
2049 cras_audio_handler_->GetOutputVolumePercentForDevice(
2050 kUSBJabraSpeakerOutput2.id));
2052 // Adjust the volume of output devices, verify all active nodes are set to
2053 // the same volume.
2054 cras_audio_handler_->SetOutputVolumePercent(25);
2055 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercent());
2056 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercentForDevice(
2057 kUSBJabraSpeakerOutput1.id));
2058 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercentForDevice(
2059 kUSBJabraSpeakerOutput2.id));
2062 TEST_F(CrasAudioHandlerTest, ChangeActiveNodesHotrodInitWithCameraInputActive) {
2063 AudioNodeList audio_nodes;
2064 audio_nodes.push_back(kHDMIOutput);
2065 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2066 audio_nodes.push_back(kUSBJabraSpeakerOutput2);
2067 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2068 audio_nodes.push_back(kUSBJabraSpeakerInput2);
2069 // Make the camera input to be plugged in later than jabra's input.
2070 AudioNode usb_camera(kUSBCameraInput);
2071 usb_camera.plugged_time = 10000000;
2072 audio_nodes.push_back(usb_camera);
2073 SetUpCrasAudioHandler(audio_nodes);
2075 // Verify the audio devices size.
2076 AudioDeviceList audio_devices;
2077 cras_audio_handler_->GetAudioDevices(&audio_devices);
2078 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2080 // Verify the 1st jabra speaker's output is selected as active output
2081 // node and camera's input is selected active input by CrasAudioHandler.
2082 EXPECT_EQ(2, GetActiveDeviceCount());
2083 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2084 cras_audio_handler_->GetPrimaryActiveOutputNode());
2085 EXPECT_EQ(kUSBCameraInput.id,
2086 cras_audio_handler_->GetPrimaryActiveInputNode());
2088 // Set both jabra speakers's input and output nodes to active, this simulates
2089 // the call sent by hotrod initialization process.
2090 test_observer_->reset_active_output_node_changed_count();
2091 test_observer_->reset_active_input_node_changed_count();
2092 CrasAudioHandler::NodeIdList active_nodes;
2093 active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2094 active_nodes.push_back(kUSBJabraSpeakerOutput2.id);
2095 active_nodes.push_back(kUSBJabraSpeakerInput1.id);
2096 active_nodes.push_back(kUSBJabraSpeakerInput2.id);
2097 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2099 // Verify both jabra speakers' input/output nodes are made active.
2100 // num_active_nodes = GetActiveDeviceCount();
2101 EXPECT_EQ(4, GetActiveDeviceCount());
2102 const AudioDevice* active_output_1 =
2103 GetDeviceFromId(kUSBJabraSpeakerOutput1.id);
2104 EXPECT_TRUE(active_output_1->active);
2105 const AudioDevice* active_output_2 =
2106 GetDeviceFromId(kUSBJabraSpeakerOutput2.id);
2107 EXPECT_TRUE(active_output_2->active);
2108 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2109 cras_audio_handler_->GetPrimaryActiveOutputNode());
2110 const AudioDevice* active_input_1 =
2111 GetDeviceFromId(kUSBJabraSpeakerInput1.id);
2112 EXPECT_TRUE(active_input_1->active);
2113 const AudioDevice* active_input_2 =
2114 GetDeviceFromId(kUSBJabraSpeakerInput2.id);
2115 EXPECT_TRUE(active_input_2->active);
2116 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2117 cras_audio_handler_->GetPrimaryActiveInputNode());
2119 // Verify only 1 ActiveOutputNodeChanged notification has been sent out
2120 // by calling ChangeActiveNodes.
2121 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
2122 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
2125 TEST_F(CrasAudioHandlerTest, ChangeActiveNodesWithFewerActives) {
2126 AudioNodeList audio_nodes;
2127 audio_nodes.push_back(kHDMIOutput);
2128 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2129 audio_nodes.push_back(kUSBJabraSpeakerOutput2);
2130 SetUpCrasAudioHandler(audio_nodes);
2132 // Verify the audio devices size.
2133 AudioDeviceList audio_devices;
2134 cras_audio_handler_->GetAudioDevices(&audio_devices);
2135 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2137 // Set all three nodes to be active.
2138 CrasAudioHandler::NodeIdList active_nodes;
2139 active_nodes.push_back(kHDMIOutput.id);
2140 active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2141 active_nodes.push_back(kUSBJabraSpeakerOutput2.id);
2142 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2144 // Verify all three nodes are active.
2145 EXPECT_EQ(3, GetActiveDeviceCount());
2146 const AudioDevice* active_output_1 = GetDeviceFromId(kHDMIOutput.id);
2147 EXPECT_TRUE(active_output_1->active);
2148 const AudioDevice* active_output_2 =
2149 GetDeviceFromId(kUSBJabraSpeakerOutput1.id);
2150 EXPECT_TRUE(active_output_2->active);
2151 const AudioDevice* active_output_3 =
2152 GetDeviceFromId(kUSBJabraSpeakerOutput2.id);
2153 EXPECT_TRUE(active_output_3->active);
2155 // Now call ChangeActiveDevices with only 2 nodes.
2156 active_nodes.clear();
2157 active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2158 active_nodes.push_back(kUSBJabraSpeakerOutput2.id);
2159 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2161 // Verify only 2 nodes are active.
2162 EXPECT_EQ(2, GetActiveDeviceCount());
2163 const AudioDevice* output_1 = GetDeviceFromId(kHDMIOutput.id);
2164 EXPECT_FALSE(output_1->active);
2165 const AudioDevice* output_2 = GetDeviceFromId(kUSBJabraSpeakerOutput1.id);
2166 EXPECT_TRUE(output_2->active);
2167 const AudioDevice* output_3 = GetDeviceFromId(kUSBJabraSpeakerOutput2.id);
2168 EXPECT_TRUE(output_3->active);
2171 TEST_F(CrasAudioHandlerTest, HotrodInitWithSingleJabra) {
2172 // Simulates the hotrod initializated with a single jabra device and
2173 // CrasAudioHandler selected jabra input/output as active devices.
2174 AudioNodeList audio_nodes;
2175 audio_nodes.push_back(kHDMIOutput);
2176 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2177 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2178 audio_nodes.push_back(kUSBCameraInput);
2179 SetUpCrasAudioHandler(audio_nodes);
2181 // Verify the audio devices size.
2182 AudioDeviceList audio_devices;
2183 cras_audio_handler_->GetAudioDevices(&audio_devices);
2184 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2186 // Verify the jabra speaker's output and input are selected as active nodes
2187 // by CrasAudioHandler.
2188 EXPECT_EQ(2, GetActiveDeviceCount());
2189 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2190 cras_audio_handler_->GetPrimaryActiveOutputNode());
2191 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2192 cras_audio_handler_->GetPrimaryActiveInputNode());
2195 TEST_F(CrasAudioHandlerTest,
2196 ChangeActiveNodesHotrodInitWithSingleJabraCameraPlugInLater) {
2197 AudioNodeList audio_nodes;
2198 audio_nodes.push_back(kHDMIOutput);
2199 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2200 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2201 AudioNode usb_camera(kUSBCameraInput);
2202 usb_camera.plugged_time = 10000000;
2203 audio_nodes.push_back(usb_camera);
2204 SetUpCrasAudioHandler(audio_nodes);
2206 // Verify the audio devices size.
2207 AudioDeviceList audio_devices;
2208 cras_audio_handler_->GetAudioDevices(&audio_devices);
2209 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2211 // Verify the jabra speaker's output is selected as active output, and
2212 // camera's input is selected as active input by CrasAudioHandler
2213 EXPECT_EQ(2, GetActiveDeviceCount());
2214 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2215 cras_audio_handler_->GetPrimaryActiveOutputNode());
2216 EXPECT_EQ(kUSBCameraInput.id,
2217 cras_audio_handler_->GetPrimaryActiveInputNode());
2219 // Simulate hotrod app call to set jabra input as active device with only
2220 // jabra input node in the active node list, which does not conform to the
2221 // new SetActiveDevices protocol, but just show we can still handle it if
2222 // this happens.
2223 CrasAudioHandler::NodeIdList active_nodes;
2224 active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2225 active_nodes.push_back(kUSBJabraSpeakerInput1.id);
2226 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2228 // Verify the jabra speaker's output is selected as active output, and
2229 // jabra's input is selected as active input.
2230 EXPECT_EQ(2, GetActiveDeviceCount());
2231 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2232 cras_audio_handler_->GetPrimaryActiveOutputNode());
2233 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2234 cras_audio_handler_->GetPrimaryActiveInputNode());
2237 TEST_F(CrasAudioHandlerTest,
2238 ChangeActiveNodesHotrodInitWithSingleJabraCameraPlugInLaterOldCall) {
2239 AudioNodeList audio_nodes;
2240 audio_nodes.push_back(kHDMIOutput);
2241 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2242 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2243 AudioNode usb_camera(kUSBCameraInput);
2244 usb_camera.plugged_time = 10000000;
2245 audio_nodes.push_back(usb_camera);
2246 SetUpCrasAudioHandler(audio_nodes);
2248 // Verify the audio devices size.
2249 AudioDeviceList audio_devices;
2250 cras_audio_handler_->GetAudioDevices(&audio_devices);
2251 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2253 // Verify the jabra speaker's output is selected as active output, and
2254 // camera's input is selected as active input by CrasAudioHandler
2255 EXPECT_EQ(2, GetActiveDeviceCount());
2256 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2257 cras_audio_handler_->GetPrimaryActiveOutputNode());
2258 EXPECT_EQ(kUSBCameraInput.id,
2259 cras_audio_handler_->GetPrimaryActiveInputNode());
2261 // Simulate hotrod app call to set jabra input as active device with only
2262 // jabra input node in the active node list, which does not conform to the
2263 // new SetActiveDevices protocol, but just show we can still handle it if
2264 // this happens.
2265 CrasAudioHandler::NodeIdList active_nodes;
2266 active_nodes.push_back(kUSBJabraSpeakerInput1.id);
2267 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2269 // Verify the jabra speaker's output is selected as active output, and
2270 // jabra's input is selected as active input.
2271 EXPECT_EQ(2, GetActiveDeviceCount());
2272 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2273 cras_audio_handler_->GetPrimaryActiveOutputNode());
2274 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2275 cras_audio_handler_->GetPrimaryActiveInputNode());
2278 TEST_F(CrasAudioHandlerTest,
2279 ChangeActiveNodesHotrodInitWithSingleJabraChangeOutput) {
2280 AudioNodeList audio_nodes;
2281 audio_nodes.push_back(kHDMIOutput);
2282 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2283 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2284 audio_nodes.push_back(kUSBCameraInput);
2285 SetUpCrasAudioHandler(audio_nodes);
2287 // Verify the audio devices size.
2288 AudioDeviceList audio_devices;
2289 cras_audio_handler_->GetAudioDevices(&audio_devices);
2290 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2292 // Verify the jabra speaker's output and input are selected as active output
2293 // by CrasAudioHandler.
2294 EXPECT_EQ(2, GetActiveDeviceCount());
2295 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2296 cras_audio_handler_->GetPrimaryActiveOutputNode());
2297 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2298 cras_audio_handler_->GetPrimaryActiveInputNode());
2300 // Simulate hotrod app call SetActiveDevices to change active output
2301 // with only complete list of active nodes passed in, which is the new
2302 // way of hotrod app.
2303 CrasAudioHandler::NodeIdList active_nodes;
2304 active_nodes.push_back(kHDMIOutput.id);
2305 active_nodes.push_back(kUSBJabraSpeakerInput1.id);
2306 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2308 // Verify the jabra speaker's output is selected as active output, and
2309 // jabra's input is selected as active input.
2310 EXPECT_EQ(2, GetActiveDeviceCount());
2311 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2312 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2313 cras_audio_handler_->GetPrimaryActiveInputNode());
2316 TEST_F(CrasAudioHandlerTest,
2317 ChangeActiveNodesHotrodInitWithSingleJabraChangeOutputOldCall) {
2318 AudioNodeList audio_nodes;
2319 audio_nodes.push_back(kHDMIOutput);
2320 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2321 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2322 audio_nodes.push_back(kUSBCameraInput);
2323 SetUpCrasAudioHandler(audio_nodes);
2325 // Verify the audio devices size.
2326 AudioDeviceList audio_devices;
2327 cras_audio_handler_->GetAudioDevices(&audio_devices);
2328 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2330 // Verify the jabra speaker's output and input are selected as active output
2331 // by CrasAudioHandler.
2332 EXPECT_EQ(2, GetActiveDeviceCount());
2333 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2334 cras_audio_handler_->GetPrimaryActiveOutputNode());
2335 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2336 cras_audio_handler_->GetPrimaryActiveInputNode());
2338 // Simulate hotrod app call SetActiveDevices to change active output
2339 // with only a single active output nodes passed in, which is the old
2340 // way of hotrod app.
2341 CrasAudioHandler::NodeIdList active_nodes;
2342 active_nodes.push_back(kHDMIOutput.id);
2343 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2345 // Verify the jabra speaker's output is selected as active output, and
2346 // jabra's input is selected as active input.
2347 EXPECT_EQ(2, GetActiveDeviceCount());
2348 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2349 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2350 cras_audio_handler_->GetPrimaryActiveInputNode());
2353 TEST_F(CrasAudioHandlerTest, NoMoreAudioInputDevices) {
2354 // Some device like chromebox does not have the internal input device. The
2355 // active devices should be reset when the user plugs a device and then
2356 // unplugs it to such device.
2358 AudioNodeList audio_nodes;
2359 audio_nodes.push_back(kInternalSpeaker);
2360 SetUpCrasAudioHandler(audio_nodes);
2362 EXPECT_EQ(0ULL, cras_audio_handler_->GetPrimaryActiveInputNode());
2364 audio_nodes.push_back(kMicJack);
2365 ChangeAudioNodes(audio_nodes);
2367 EXPECT_EQ(kMicJack.id, cras_audio_handler_->GetPrimaryActiveInputNode());
2368 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
2369 test_observer_->reset_active_input_node_changed_count();
2371 audio_nodes.pop_back();
2372 ChangeAudioNodes(audio_nodes);
2373 EXPECT_EQ(0ULL, cras_audio_handler_->GetPrimaryActiveInputNode());
2374 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
2377 // Test the case in which an HDMI output is plugged in with other higher
2378 // priority
2379 // output devices already plugged and user has manually selected an active
2380 // output.
2381 // The hotplug of hdmi output should not change user's selection of active
2382 // device.
2383 // crbug.com/447826.
2384 TEST_F(CrasAudioHandlerTest, HotPlugHDMINotChangeActiveOutput) {
2385 AudioNodeList audio_nodes;
2386 AudioNode internal_speaker(kInternalSpeaker);
2387 audio_nodes.push_back(internal_speaker);
2388 AudioNode usb_headset(kUSBHeadphone1);
2389 usb_headset.plugged_time = 80000000;
2390 audio_nodes.push_back(usb_headset);
2391 SetUpCrasAudioHandler(audio_nodes);
2393 // Verify the audio devices size.
2394 AudioDeviceList audio_devices;
2395 cras_audio_handler_->GetAudioDevices(&audio_devices);
2396 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2398 // Verify the USB headset is selected as active output by default.
2399 EXPECT_EQ(usb_headset.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2401 // Manually set the active output to internal speaker.
2402 AudioDevice internal_output(kInternalSpeaker);
2403 cras_audio_handler_->SwitchToDevice(internal_output, true);
2405 // Verify the active output is switched to internal speaker.
2406 EXPECT_EQ(internal_speaker.id,
2407 cras_audio_handler_->GetPrimaryActiveOutputNode());
2408 EXPECT_LT(kInternalSpeaker.plugged_time, usb_headset.plugged_time);
2409 const AudioDevice* usb_device = GetDeviceFromId(usb_headset.id);
2410 EXPECT_FALSE(usb_device->active);
2412 // Plug in HDMI output.
2413 audio_nodes.clear();
2414 internal_speaker.active = true;
2415 audio_nodes.push_back(internal_speaker);
2416 usb_headset.active = false;
2417 audio_nodes.push_back(usb_headset);
2418 AudioNode hdmi(kHDMIOutput);
2419 hdmi.plugged_time = 90000000;
2420 audio_nodes.push_back(hdmi);
2421 ChangeAudioNodes(audio_nodes);
2423 // The active output should not change.
2424 EXPECT_EQ(kInternalSpeaker.id,
2425 cras_audio_handler_->GetPrimaryActiveOutputNode());
2428 } // namespace chromeos