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/cras_audio_client_stub_impl.h"
14 #include "chromeos/dbus/dbus_thread_manager.h"
15 #include "testing/gtest/include/gtest/gtest.h"
19 const uint64 kInternalSpeakerId = 10001;
20 const uint64 kHeadphoneId = 10002;
21 const uint64 kInternalMicId = 10003;
22 const uint64 kUSBMicId = 10004;
23 const uint64 kBluetoothHeadsetId = 10005;
24 const uint64 kHDMIOutputId = 10006;
25 const uint64 kUSBHeadphoneId1 = 10007;
26 const uint64 kUSBHeadphoneId2 = 10008;
27 const uint64 kOtherTypeOutputId = 90001;
28 const uint64 kOtherTypeInputId = 90002;
30 const AudioNode kInternalSpeaker(
40 const AudioNode kHeadphone(
50 const AudioNode kInternalMic(
60 const AudioNode kUSBMic(
70 const AudioNode kOtherTypeOutput(
75 "Other Type Output Device",
80 const AudioNode kOtherTypeInput(
85 "Other Type Input Device",
90 const AudioNode kBluetoothHeadset (
95 "Bluetooth Headset 1",
100 const AudioNode kHDMIOutput (
110 const AudioNode kUSBHeadphone1 (
120 const AudioNode kUSBHeadphone2 (
131 class TestObserver : public chromeos::CrasAudioHandler::AudioObserver {
133 TestObserver() : active_output_node_changed_count_(0),
134 active_input_node_changed_count_(0),
135 audio_nodes_changed_count_(0),
136 output_mute_changed_count_(0),
137 input_mute_changed_count_(0),
138 output_volume_changed_count_(0),
139 input_gain_changed_count_(0) {
142 int active_output_node_changed_count() const {
143 return active_output_node_changed_count_;
146 int active_input_node_changed_count() const {
147 return active_input_node_changed_count_;
150 int audio_nodes_changed_count() const {
151 return audio_nodes_changed_count_;
154 int output_mute_changed_count() const {
155 return output_mute_changed_count_;
158 int input_mute_changed_count() const {
159 return input_mute_changed_count_;
162 int output_volume_changed_count() const {
163 return output_volume_changed_count_;
166 int input_gain_changed_count() const {
167 return input_gain_changed_count_;
170 virtual ~TestObserver() {}
173 // chromeos::CrasAudioHandler::AudioObserver overrides.
174 virtual void OnActiveOutputNodeChanged() OVERRIDE {
175 ++active_output_node_changed_count_;
178 virtual void OnActiveInputNodeChanged() OVERRIDE {
179 ++active_input_node_changed_count_;
182 virtual void OnAudioNodesChanged() OVERRIDE {
183 ++audio_nodes_changed_count_;
186 virtual void OnOutputMuteChanged() OVERRIDE {
187 ++output_mute_changed_count_;
190 virtual void OnInputMuteChanged() OVERRIDE {
191 ++input_mute_changed_count_;
194 virtual void OnOutputVolumeChanged() OVERRIDE {
195 ++output_volume_changed_count_;
198 virtual void OnInputGainChanged() OVERRIDE {
199 ++input_gain_changed_count_;
203 int active_output_node_changed_count_;
204 int active_input_node_changed_count_;
205 int audio_nodes_changed_count_;
206 int output_mute_changed_count_;
207 int input_mute_changed_count_;
208 int output_volume_changed_count_;
209 int input_gain_changed_count_;
211 DISALLOW_COPY_AND_ASSIGN(TestObserver);
214 class CrasAudioHandlerTest : public testing::Test {
216 CrasAudioHandlerTest() : cras_audio_handler_(NULL),
217 cras_audio_client_stub_(NULL) {
219 virtual ~CrasAudioHandlerTest() {}
221 virtual void SetUp() OVERRIDE {
224 virtual void TearDown() OVERRIDE {
225 cras_audio_handler_->RemoveAudioObserver(test_observer_.get());
226 test_observer_.reset();
227 CrasAudioHandler::Shutdown();
228 audio_pref_handler_ = NULL;
229 DBusThreadManager::Shutdown();
232 void SetUpCrasAudioHandler(const AudioNodeList& audio_nodes) {
233 DBusThreadManager::InitializeWithStub();
234 cras_audio_client_stub_ = static_cast<CrasAudioClientStubImpl*>(
235 DBusThreadManager::Get()->GetCrasAudioClient());
236 cras_audio_client_stub_->SetAudioDevices(audio_nodes);
237 audio_pref_handler_ = new AudioDevicesPrefHandlerStub();
238 CrasAudioHandler::Initialize(audio_pref_handler_);
239 cras_audio_handler_ = CrasAudioHandler::Get();
240 test_observer_.reset(new TestObserver);
241 cras_audio_handler_->AddAudioObserver(test_observer_.get());
242 message_loop_.RunUntilIdle();
245 void ChangeAudioNodes(const AudioNodeList& audio_nodes) {
246 cras_audio_client_stub_->ChangeAudioNodes(audio_nodes);
247 message_loop_.RunUntilIdle();
251 base::MessageLoopForUI message_loop_;
252 CrasAudioHandler* cras_audio_handler_; // Not owned.
253 CrasAudioClientStubImpl* cras_audio_client_stub_; // Not owned.
254 scoped_ptr<TestObserver> test_observer_;
255 scoped_refptr<AudioDevicesPrefHandlerStub> audio_pref_handler_;
258 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandlerTest);
261 TEST_F(CrasAudioHandlerTest, InitializeWithOnlyDefaultAudioDevices) {
262 AudioNodeList audio_nodes;
263 audio_nodes.push_back(kInternalSpeaker);
264 audio_nodes.push_back(kInternalMic);
265 SetUpCrasAudioHandler(audio_nodes);
267 // Verify the audio devices size.
268 AudioDeviceList audio_devices;
269 cras_audio_handler_->GetAudioDevices(&audio_devices);
270 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
272 // Verify the internal speaker has been selected as the active output.
273 AudioDevice active_output;
274 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
275 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
276 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
277 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
279 // Ensure the internal microphone has been selected as the active input.
280 AudioDevice active_input;
281 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode());
282 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
285 TEST_F(CrasAudioHandlerTest, InitializeWithAlternativeAudioDevices) {
286 AudioNodeList audio_nodes;
287 audio_nodes.push_back(kInternalSpeaker);
288 audio_nodes.push_back(kHeadphone);
289 audio_nodes.push_back(kInternalMic);
290 audio_nodes.push_back(kUSBMic);
291 SetUpCrasAudioHandler(audio_nodes);
293 // Verify the audio devices size.
294 AudioDeviceList audio_devices;
295 cras_audio_handler_->GetAudioDevices(&audio_devices);
296 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
298 // Verify the headphone has been selected as the active output.
299 AudioDevice active_output;
300 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
301 EXPECT_EQ(kHeadphone.id, active_output.id);
302 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
303 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
305 // Ensure the USB microphone has been selected as the active input.
306 AudioDevice active_input;
307 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetActiveInputNode());
308 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
311 TEST_F(CrasAudioHandlerTest, SwitchActiveOutputDevice) {
312 AudioNodeList audio_nodes;
313 audio_nodes.push_back(kInternalSpeaker);
314 audio_nodes.push_back(kHeadphone);
315 SetUpCrasAudioHandler(audio_nodes);
316 AudioDeviceList audio_devices;
317 cras_audio_handler_->GetAudioDevices(&audio_devices);
318 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
320 // Verify the initial active output device is headphone.
321 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
322 AudioDevice active_output;
323 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
324 EXPECT_EQ(kHeadphone.id, active_output.id);
325 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
327 // Switch the active output to internal speaker.
328 AudioDevice internal_speaker(kInternalSpeaker);
329 cras_audio_handler_->SwitchToDevice(internal_speaker);
331 // Verify the active output is switched to internal speaker, and the
332 // ActiveOutputNodeChanged event is fired.
333 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
334 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
335 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
336 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
339 TEST_F(CrasAudioHandlerTest, SwitchActiveInputDevice) {
340 AudioNodeList audio_nodes;
341 audio_nodes.push_back(kInternalMic);
342 audio_nodes.push_back(kUSBMic);
343 SetUpCrasAudioHandler(audio_nodes);
344 AudioDeviceList audio_devices;
345 cras_audio_handler_->GetAudioDevices(&audio_devices);
346 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
348 // Verify the initial active input device is USB mic.
349 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
350 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetActiveInputNode());
352 // Switch the active input to internal mic.
353 AudioDevice internal_mic(kInternalMic);
354 cras_audio_handler_->SwitchToDevice(internal_mic);
356 // Verify the active output is switched to internal speaker, and the active
357 // ActiveInputNodeChanged event is fired.
358 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
359 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode());
362 TEST_F(CrasAudioHandlerTest, PlugHeadphone) {
363 // Set up initial audio devices, only with internal speaker.
364 AudioNodeList audio_nodes;
365 audio_nodes.push_back(kInternalSpeaker);
366 SetUpCrasAudioHandler(audio_nodes);
367 const size_t init_nodes_size = audio_nodes.size();
369 // Verify the audio devices size.
370 AudioDeviceList audio_devices;
371 cras_audio_handler_->GetAudioDevices(&audio_devices);
372 EXPECT_EQ(init_nodes_size, audio_devices.size());
373 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
375 // Verify the internal speaker has been selected as the active output.
376 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
377 AudioDevice active_output;
378 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
379 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
380 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
381 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
383 // Plug the headphone.
385 AudioNode internal_speaker(kInternalSpeaker);
386 internal_speaker.active = true;
387 audio_nodes.push_back(internal_speaker);
388 audio_nodes.push_back(kHeadphone);
389 ChangeAudioNodes(audio_nodes);
391 // Verify the AudioNodesChanged event is fired and new audio device is added.
392 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
393 cras_audio_handler_->GetAudioDevices(&audio_devices);
394 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
396 // Verify the active output device is switched to headphone and
397 // ActiveOutputChanged event is fired.
398 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
399 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
400 EXPECT_EQ(kHeadphone.id, active_output.id);
401 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
402 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
405 TEST_F(CrasAudioHandlerTest, UnplugHeadphone) {
406 // Set up initial audio devices, with internal speaker and headphone.
407 AudioNodeList audio_nodes;
408 audio_nodes.push_back(kInternalSpeaker);
409 audio_nodes.push_back(kHeadphone);
410 SetUpCrasAudioHandler(audio_nodes);
411 const size_t init_nodes_size = audio_nodes.size();
413 // Verify the audio devices size.
414 AudioDeviceList audio_devices;
415 cras_audio_handler_->GetAudioDevices(&audio_devices);
416 EXPECT_EQ(init_nodes_size, audio_devices.size());
417 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
419 // Verify the headphone has been selected as the active output.
420 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
421 AudioDevice active_output;
422 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
423 EXPECT_EQ(kHeadphone.id, active_output.id);
424 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
425 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
427 // Unplug the headphone.
429 audio_nodes.push_back(kInternalSpeaker);
430 ChangeAudioNodes(audio_nodes);
432 // Verify the AudioNodesChanged event is fired and one audio device is
434 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
435 cras_audio_handler_->GetAudioDevices(&audio_devices);
436 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
438 // Verify the active output device is switched to internal speaker and
439 // ActiveOutputChanged event is fired.
440 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
441 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
442 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
443 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
444 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
447 TEST_F(CrasAudioHandlerTest, InitializeWithBluetoothHeadset) {
448 AudioNodeList audio_nodes;
449 audio_nodes.push_back(kInternalSpeaker);
450 audio_nodes.push_back(kBluetoothHeadset);
451 SetUpCrasAudioHandler(audio_nodes);
453 // Verify the audio devices size.
454 AudioDeviceList audio_devices;
455 cras_audio_handler_->GetAudioDevices(&audio_devices);
456 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
457 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
459 // Verify the bluetooth headset has been selected as the active output.
460 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
461 AudioDevice active_output;
462 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
463 EXPECT_EQ(kBluetoothHeadset.id, active_output.id);
464 EXPECT_EQ(kBluetoothHeadset.id, cras_audio_handler_->GetActiveOutputNode());
465 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
468 TEST_F(CrasAudioHandlerTest, ConnectAndDisconnectBluetoothHeadset) {
469 // Initialize with internal speaker and headphone.
470 AudioNodeList audio_nodes;
471 audio_nodes.push_back(kInternalSpeaker);
472 audio_nodes.push_back(kHeadphone);
473 SetUpCrasAudioHandler(audio_nodes);
474 const size_t init_nodes_size = audio_nodes.size();
476 // Verify the audio devices size.
477 AudioDeviceList audio_devices;
478 cras_audio_handler_->GetAudioDevices(&audio_devices);
479 EXPECT_EQ(init_nodes_size, audio_devices.size());
480 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
482 // Verify the headphone is selected as the active output initially.
483 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
484 AudioDevice active_output;
485 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
486 EXPECT_EQ(kHeadphone.id, active_output.id);
487 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
488 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
490 // Connect to bluetooth headset. Since it is plugged in later than
491 // headphone, active output should be switched to it.
493 audio_nodes.push_back(kInternalSpeaker);
494 AudioNode headphone(kHeadphone);
495 headphone.plugged_time = 80000000;
496 headphone.active = true;
497 audio_nodes.push_back(headphone);
498 AudioNode bluetooth_headset(kBluetoothHeadset);
499 bluetooth_headset.plugged_time = 90000000;
500 audio_nodes.push_back(bluetooth_headset);
501 ChangeAudioNodes(audio_nodes);
503 // Verify the AudioNodesChanged event is fired and new audio device is added.
504 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
505 cras_audio_handler_->GetAudioDevices(&audio_devices);
506 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
508 // Verify the active output device is switched to bluetooth headset, and
509 // ActiveOutputChanged event is fired.
510 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
511 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
512 EXPECT_EQ(kBluetoothHeadset.id, active_output.id);
513 EXPECT_EQ(kBluetoothHeadset.id, cras_audio_handler_->GetActiveOutputNode());
514 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
516 // Disconnect bluetooth headset.
518 audio_nodes.push_back(kInternalSpeaker);
519 headphone.active = false;
520 audio_nodes.push_back(headphone);
521 ChangeAudioNodes(audio_nodes);
523 // Verify the AudioNodesChanged event is fired and one audio device is
525 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
526 cras_audio_handler_->GetAudioDevices(&audio_devices);
527 EXPECT_EQ(init_nodes_size, audio_devices.size());
529 // Verify the active output device is switched to headphone, and
530 // ActiveOutputChanged event is fired.
531 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
532 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
533 EXPECT_EQ(kHeadphone.id, active_output.id);
534 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
535 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
538 TEST_F(CrasAudioHandlerTest, InitializeWithHDMIOutput) {
539 AudioNodeList audio_nodes;
540 audio_nodes.push_back(kInternalSpeaker);
541 audio_nodes.push_back(kHDMIOutput);
542 SetUpCrasAudioHandler(audio_nodes);
544 // Verify the audio devices size.
545 AudioDeviceList audio_devices;
546 cras_audio_handler_->GetAudioDevices(&audio_devices);
547 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
548 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
550 // Verify the HDMI device has been selected as the active output.
551 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
552 AudioDevice active_output;
553 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
554 EXPECT_EQ(kHDMIOutput.id, active_output.id);
555 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetActiveOutputNode());
556 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
559 TEST_F(CrasAudioHandlerTest, ConnectAndDisconnectHDMIOutput) {
560 // Initialize with internal speaker.
561 AudioNodeList audio_nodes;
562 audio_nodes.push_back(kInternalSpeaker);
563 SetUpCrasAudioHandler(audio_nodes);
564 const size_t init_nodes_size = audio_nodes.size();
566 // Verify the audio devices size.
567 AudioDeviceList audio_devices;
568 cras_audio_handler_->GetAudioDevices(&audio_devices);
569 EXPECT_EQ(init_nodes_size, audio_devices.size());
570 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
572 // Verify the internal speaker is selected as the active output initially.
573 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
574 AudioDevice active_output;
575 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
576 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
577 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
578 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
580 // Connect to HDMI output.
582 AudioNode internal_speaker(kInternalSpeaker);
583 internal_speaker.active = true;
584 internal_speaker.plugged_time = 80000000;
585 audio_nodes.push_back(internal_speaker);
586 AudioNode hdmi(kHDMIOutput);
587 hdmi.plugged_time = 90000000;
588 audio_nodes.push_back(hdmi);
589 ChangeAudioNodes(audio_nodes);
591 // Verify the AudioNodesChanged event is fired and new audio device is added.
592 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
593 cras_audio_handler_->GetAudioDevices(&audio_devices);
594 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
596 // Verify the active output device is switched to hdmi output, and
597 // ActiveOutputChanged event is fired.
598 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
599 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
600 EXPECT_EQ(kHDMIOutput.id, active_output.id);
601 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetActiveOutputNode());
602 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
604 // Disconnect hdmi headset.
606 audio_nodes.push_back(kInternalSpeaker);
607 ChangeAudioNodes(audio_nodes);
609 // Verify the AudioNodesChanged event is fired and one audio device is
611 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
612 cras_audio_handler_->GetAudioDevices(&audio_devices);
613 EXPECT_EQ(init_nodes_size, audio_devices.size());
615 // Verify the active output device is switched to internal speaker, and
616 // ActiveOutputChanged event is fired.
617 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
618 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
619 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
620 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
621 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
624 TEST_F(CrasAudioHandlerTest, HandleHeadphoneAndHDMIOutput) {
625 // Initialize with internal speaker, headphone and HDMI output.
626 AudioNodeList audio_nodes;
627 audio_nodes.push_back(kInternalSpeaker);
628 audio_nodes.push_back(kHeadphone);
629 audio_nodes.push_back(kHDMIOutput);
630 SetUpCrasAudioHandler(audio_nodes);
631 const size_t init_nodes_size = audio_nodes.size();
633 // Verify the audio devices size.
634 AudioDeviceList audio_devices;
635 cras_audio_handler_->GetAudioDevices(&audio_devices);
636 EXPECT_EQ(init_nodes_size, audio_devices.size());
637 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
639 // Verify the headphone is selected as the active output initially.
640 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
641 AudioDevice active_output;
642 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
643 EXPECT_EQ(kHeadphone.id, active_output.id);
644 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
645 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
647 // Disconnect HDMI output.
649 audio_nodes.push_back(kInternalSpeaker);
650 audio_nodes.push_back(kHDMIOutput);
651 ChangeAudioNodes(audio_nodes);
653 // Verify the AudioNodesChanged event is fired and one audio device is
655 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
656 cras_audio_handler_->GetAudioDevices(&audio_devices);
657 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
659 // Verify the active output device is switched to HDMI output, and
660 // ActiveOutputChanged event is fired.
661 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
662 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
663 EXPECT_EQ(kHDMIOutput.id, active_output.id);
664 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetActiveOutputNode());
665 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
668 TEST_F(CrasAudioHandlerTest, InitializeWithUSBHeadphone) {
669 AudioNodeList audio_nodes;
670 audio_nodes.push_back(kInternalSpeaker);
671 audio_nodes.push_back(kUSBHeadphone1);
672 SetUpCrasAudioHandler(audio_nodes);
674 // Verify the audio devices size.
675 AudioDeviceList audio_devices;
676 cras_audio_handler_->GetAudioDevices(&audio_devices);
677 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
678 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
680 // Verify the usb headphone has been selected as the active output.
681 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
682 AudioDevice active_output;
683 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
684 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
685 EXPECT_EQ(kUSBHeadphone1.id, cras_audio_handler_->GetActiveOutputNode());
686 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
689 TEST_F(CrasAudioHandlerTest, PlugAndUnplugUSBHeadphone) {
690 // Initialize with internal speaker.
691 AudioNodeList audio_nodes;
692 audio_nodes.push_back(kInternalSpeaker);
693 SetUpCrasAudioHandler(audio_nodes);
694 const size_t init_nodes_size = audio_nodes.size();
696 // Verify the audio devices size.
697 AudioDeviceList audio_devices;
698 cras_audio_handler_->GetAudioDevices(&audio_devices);
699 EXPECT_EQ(init_nodes_size, audio_devices.size());
700 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
702 // Verify the internal speaker is selected as the active output initially.
703 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
704 AudioDevice active_output;
705 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
706 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
707 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
708 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
710 // Plug in usb headphone
712 AudioNode internal_speaker(kInternalSpeaker);
713 internal_speaker.active = true;
714 internal_speaker.plugged_time = 80000000;
715 audio_nodes.push_back(internal_speaker);
716 AudioNode usb_headphone(kUSBHeadphone1);
717 usb_headphone.plugged_time = 90000000;
718 audio_nodes.push_back(usb_headphone);
719 ChangeAudioNodes(audio_nodes);
721 // Verify the AudioNodesChanged event is fired and new audio device is added.
722 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
723 cras_audio_handler_->GetAudioDevices(&audio_devices);
724 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
726 // Verify the active output device is switched to usb headphone, and
727 // ActiveOutputChanged event is fired.
728 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
729 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
730 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
731 EXPECT_EQ(kUSBHeadphone1.id, cras_audio_handler_->GetActiveOutputNode());
732 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
734 // Unplug usb headphone.
736 audio_nodes.push_back(kInternalSpeaker);
737 ChangeAudioNodes(audio_nodes);
739 // Verify the AudioNodesChanged event is fired and one audio device is
741 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
742 cras_audio_handler_->GetAudioDevices(&audio_devices);
743 EXPECT_EQ(init_nodes_size, audio_devices.size());
745 // Verify the active output device is switched to internal speaker, and
746 // ActiveOutputChanged event is fired.
747 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
748 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
749 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
750 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
751 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
754 TEST_F(CrasAudioHandlerTest, HandleMultipleUSBHeadphones) {
755 // Initialize with internal speaker and one usb headphone.
756 AudioNodeList audio_nodes;
757 audio_nodes.push_back(kInternalSpeaker);
758 audio_nodes.push_back(kUSBHeadphone1);
759 SetUpCrasAudioHandler(audio_nodes);
760 const size_t init_nodes_size = audio_nodes.size();
762 // Verify the audio devices size.
763 AudioDeviceList audio_devices;
764 cras_audio_handler_->GetAudioDevices(&audio_devices);
765 EXPECT_EQ(init_nodes_size, audio_devices.size());
766 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
768 // Verify the usb headphone is selected as the active output initially.
769 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
770 AudioDevice active_output;
771 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
772 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
773 EXPECT_EQ(kUSBHeadphone1.id, cras_audio_handler_->GetActiveOutputNode());
774 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
776 // Plug in another usb headphone.
778 audio_nodes.push_back(kInternalSpeaker);
779 AudioNode usb_headphone_1(kUSBHeadphone1);
780 usb_headphone_1.active = true;
781 usb_headphone_1.plugged_time = 80000000;
782 audio_nodes.push_back(usb_headphone_1);
783 AudioNode usb_headphone_2(kUSBHeadphone2);
784 usb_headphone_2.plugged_time = 90000000;
785 audio_nodes.push_back(usb_headphone_2);
786 ChangeAudioNodes(audio_nodes);
788 // Verify the AudioNodesChanged event is fired and new audio device is added.
789 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
790 cras_audio_handler_->GetAudioDevices(&audio_devices);
791 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
793 // Verify the active output device is switched to the 2nd usb headphone, which
794 // is plugged later, and ActiveOutputChanged event is fired.
795 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
796 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
797 EXPECT_EQ(kUSBHeadphone2.id, active_output.id);
798 EXPECT_EQ(kUSBHeadphone2.id, cras_audio_handler_->GetActiveOutputNode());
799 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
801 // Unplug the 2nd usb headphone.
803 audio_nodes.push_back(kInternalSpeaker);
804 audio_nodes.push_back(kUSBHeadphone1);
805 ChangeAudioNodes(audio_nodes);
807 // Verify the AudioNodesChanged event is fired and one audio device is
809 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
810 cras_audio_handler_->GetAudioDevices(&audio_devices);
811 EXPECT_EQ(init_nodes_size, audio_devices.size());
813 // Verify the active output device is switched to the first usb headphone, and
814 // ActiveOutputChanged event is fired.
815 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
816 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
817 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
818 EXPECT_EQ(kUSBHeadphone1.id, cras_audio_handler_->GetActiveOutputNode());
819 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
822 TEST_F(CrasAudioHandlerTest, UnplugUSBHeadphonesWithActiveSpeaker) {
823 // Initialize with internal speaker and one usb headphone.
824 AudioNodeList audio_nodes;
825 audio_nodes.push_back(kInternalSpeaker);
826 audio_nodes.push_back(kUSBHeadphone1);
827 SetUpCrasAudioHandler(audio_nodes);
828 const size_t init_nodes_size = audio_nodes.size();
830 // Verify the audio devices size.
831 AudioDeviceList audio_devices;
832 cras_audio_handler_->GetAudioDevices(&audio_devices);
833 EXPECT_EQ(init_nodes_size, audio_devices.size());
834 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
836 // Verify the usb headphone is selected as the active output initially.
837 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
838 AudioDevice active_output;
839 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
840 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
841 EXPECT_EQ(kUSBHeadphone1.id, cras_audio_handler_->GetActiveOutputNode());
842 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
844 // Plug in the headphone jack.
846 audio_nodes.push_back(kInternalSpeaker);
847 AudioNode usb_headphone_1(kUSBHeadphone1);
848 usb_headphone_1.active = true;
849 usb_headphone_1.plugged_time = 80000000;
850 audio_nodes.push_back(usb_headphone_1);
851 AudioNode headphone_jack(kHeadphone);
852 headphone_jack.plugged_time = 90000000;
853 audio_nodes.push_back(headphone_jack);
854 ChangeAudioNodes(audio_nodes);
856 // Verify the AudioNodesChanged event is fired and new audio device is added.
857 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
858 cras_audio_handler_->GetAudioDevices(&audio_devices);
859 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
861 // Verify the active output device is switched to the headphone jack, which
862 // is plugged later, and ActiveOutputChanged event is fired.
863 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
864 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
865 EXPECT_EQ(kHeadphone.id, active_output.id);
866 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
867 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
869 // Select the speaker to be the active output device.
870 AudioDevice internal_speaker(kInternalSpeaker);
871 cras_audio_handler_->SwitchToDevice(internal_speaker);
873 // Verify the active output is switched to internal speaker, and the
874 // ActiveOutputNodeChanged event is fired.
875 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
876 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
877 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
878 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
880 // Unplug the usb headphone.
882 AudioNode internal_speaker_node(kInternalSpeaker);
883 internal_speaker_node.active = true;
884 internal_speaker_node.plugged_time = 70000000;
885 audio_nodes.push_back(internal_speaker_node);
886 headphone_jack.active = false;
887 audio_nodes.push_back(headphone_jack);
888 ChangeAudioNodes(audio_nodes);
890 // Verify the AudioNodesChanged event is fired and one audio device is
892 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
893 cras_audio_handler_->GetAudioDevices(&audio_devices);
894 EXPECT_EQ(init_nodes_size, audio_devices.size());
896 // Verify the active output device remains to be speaker.
897 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
898 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
899 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
900 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
901 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
904 TEST_F(CrasAudioHandlerTest, OneActiveAudioOutputAfterLoginNewUserSession) {
905 // This tests the case found with crbug.com/273271.
906 // Initialize with internal speaker, bluetooth headphone and headphone jack
907 // for a new chrome session after user signs out from the previous session.
908 // Headphone jack is plugged in later than bluetooth headphone, but bluetooth
909 // headphone is selected as the active output by user from previous user
911 AudioNodeList audio_nodes;
912 audio_nodes.push_back(kInternalSpeaker);
913 AudioNode bluetooth_headphone(kBluetoothHeadset);
914 bluetooth_headphone.active = true;
915 bluetooth_headphone.plugged_time = 70000000;
916 audio_nodes.push_back(bluetooth_headphone);
917 AudioNode headphone_jack(kHeadphone);
918 headphone_jack.plugged_time = 80000000;
919 audio_nodes.push_back(headphone_jack);
920 SetUpCrasAudioHandler(audio_nodes);
921 const size_t init_nodes_size = audio_nodes.size();
923 // Verify the audio devices size.
924 AudioDeviceList audio_devices;
925 cras_audio_handler_->GetAudioDevices(&audio_devices);
926 EXPECT_EQ(init_nodes_size, audio_devices.size());
927 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
929 // Verify the headphone jack is selected as the active output and all other
930 // audio devices are not active.
931 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
932 AudioDevice active_output;
933 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
934 EXPECT_EQ(kHeadphone.id, active_output.id);
935 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
936 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
937 for (size_t i = 0; i < audio_devices.size(); ++i) {
938 if (audio_devices[i].id != kHeadphone.id)
939 EXPECT_FALSE(audio_devices[i].active);
943 TEST_F(CrasAudioHandlerTest, BluetoothSpeakerIdChangedOnFly) {
944 // Initialize with internal speaker and bluetooth headset.
945 AudioNodeList audio_nodes;
946 audio_nodes.push_back(kInternalSpeaker);
947 audio_nodes.push_back(kBluetoothHeadset);
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 bluetooth headset is selected as the active output and all other
958 // audio devices are not active.
959 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
960 AudioDevice active_output;
961 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
962 EXPECT_EQ(kBluetoothHeadset.id, active_output.id);
963 EXPECT_EQ(kBluetoothHeadset.id, cras_audio_handler_->GetActiveOutputNode());
964 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
966 // Cras changes the bluetooth headset's id on the fly.
968 AudioNode internal_speaker(kInternalSpeaker);
969 internal_speaker.active = false;
970 audio_nodes.push_back(internal_speaker);
971 AudioNode bluetooth_headphone(kBluetoothHeadset);
972 // Change bluetooth headphone id.
973 bluetooth_headphone.id = kBluetoothHeadsetId + 20000;
974 bluetooth_headphone.active = false;
975 audio_nodes.push_back(bluetooth_headphone);
976 ChangeAudioNodes(audio_nodes);
978 // Verify NodesChanged event is fired, and the audio devices size is not
980 audio_devices.clear();
981 cras_audio_handler_->GetAudioDevices(&audio_devices);
982 EXPECT_EQ(init_nodes_size, audio_devices.size());
983 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
985 // Verify ActiveOutputNodeChanged event is fired, and active device should be
986 // bluetooth headphone.
987 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
988 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
989 EXPECT_EQ(bluetooth_headphone.id, active_output.id);
992 TEST_F(CrasAudioHandlerTest, PlugUSBMic) {
993 // Set up initial audio devices, only with internal mic.
994 AudioNodeList audio_nodes;
995 audio_nodes.push_back(kInternalMic);
996 SetUpCrasAudioHandler(audio_nodes);
997 const size_t init_nodes_size = audio_nodes.size();
999 // Verify the audio devices size.
1000 AudioDeviceList audio_devices;
1001 cras_audio_handler_->GetAudioDevices(&audio_devices);
1002 EXPECT_EQ(init_nodes_size, audio_devices.size());
1003 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1005 // Verify the internal mic is selected as the active input.
1006 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1007 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode());
1008 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1010 // Plug the USB Mic.
1011 audio_nodes.clear();
1012 AudioNode internal_mic(kInternalMic);
1013 internal_mic.active = true;
1014 audio_nodes.push_back(internal_mic);
1015 audio_nodes.push_back(kUSBMic);
1016 ChangeAudioNodes(audio_nodes);
1018 // Verify the AudioNodesChanged event is fired and new audio device is added.
1019 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1020 cras_audio_handler_->GetAudioDevices(&audio_devices);
1021 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1023 // Verify the active input device is switched to USB mic and
1024 // and ActiveInputChanged event is fired.
1025 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
1026 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetActiveInputNode());
1027 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1030 TEST_F(CrasAudioHandlerTest, UnplugUSBMic) {
1031 // Set up initial audio devices, with internal mic and USB Mic.
1032 AudioNodeList audio_nodes;
1033 audio_nodes.push_back(kInternalMic);
1034 audio_nodes.push_back(kUSBMic);
1035 SetUpCrasAudioHandler(audio_nodes);
1036 const size_t init_nodes_size = audio_nodes.size();
1038 // Verify the audio devices size.
1039 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1040 AudioDeviceList audio_devices;
1041 cras_audio_handler_->GetAudioDevices(&audio_devices);
1042 EXPECT_EQ(init_nodes_size, audio_devices.size());
1044 // Verify the USB mic is selected as the active output.
1045 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1046 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetActiveInputNode());
1047 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1049 // Unplug the USB Mic.
1050 audio_nodes.clear();
1051 audio_nodes.push_back(kInternalMic);
1052 ChangeAudioNodes(audio_nodes);
1054 // Verify the AudioNodesChanged event is fired, and one audio device is
1056 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1057 cras_audio_handler_->GetAudioDevices(&audio_devices);
1058 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
1060 // Verify the active input device is switched to internal mic, and
1061 // and ActiveInputChanged event is fired.
1062 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
1063 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode());
1064 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1067 TEST_F(CrasAudioHandlerTest, PlugUSBMicNotAffectActiveOutput) {
1068 // Set up initial audio devices.
1069 AudioNodeList audio_nodes;
1070 audio_nodes.push_back(kInternalSpeaker);
1071 audio_nodes.push_back(kHeadphone);
1072 audio_nodes.push_back(kInternalMic);
1073 SetUpCrasAudioHandler(audio_nodes);
1074 const size_t init_nodes_size = audio_nodes.size();
1076 // Verify the audio devices size.
1077 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1078 AudioDeviceList audio_devices;
1079 cras_audio_handler_->GetAudioDevices(&audio_devices);
1080 EXPECT_EQ(init_nodes_size, audio_devices.size());
1082 // Verify the internal mic is selected as the active input.
1083 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1084 EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetActiveInputNode());
1085 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1087 // Verify the headphone is selected as the active output.
1088 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1089 EXPECT_EQ(kHeadphoneId, cras_audio_handler_->GetActiveOutputNode());
1090 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1092 // Switch the active output to internal speaker.
1093 AudioDevice internal_speaker(kInternalSpeaker);
1094 cras_audio_handler_->SwitchToDevice(internal_speaker);
1096 // Verify the active output is switched to internal speaker, and the
1097 // ActiveOutputNodeChanged event is fired.
1098 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1099 AudioDevice active_output;
1100 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
1101 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
1102 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
1104 // Plug the USB Mic.
1105 audio_nodes.clear();
1106 AudioNode internal_speaker_node(kInternalSpeaker);
1107 internal_speaker_node.active = true;
1108 audio_nodes.push_back(internal_speaker_node);
1109 audio_nodes.push_back(kHeadphone);
1110 AudioNode internal_mic(kInternalMic);
1111 internal_mic.active = true;
1112 audio_nodes.push_back(internal_mic);
1113 audio_nodes.push_back(kUSBMic);
1114 ChangeAudioNodes(audio_nodes);
1116 // Verify the AudioNodesChanged event is fired, one new device is added.
1117 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1118 cras_audio_handler_->GetAudioDevices(&audio_devices);
1119 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1121 // Verify the active input device is switched to USB mic, and
1122 // and ActiveInputChanged event is fired.
1123 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
1124 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetActiveInputNode());
1125 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1127 // Verify the active output device is not changed.
1128 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1129 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
1130 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
1131 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
1134 TEST_F(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnPlugInHeadphone) {
1135 // Set up initial audio devices.
1136 AudioNodeList audio_nodes;
1137 audio_nodes.push_back(kInternalSpeaker);
1138 audio_nodes.push_back(kBluetoothHeadset);
1139 SetUpCrasAudioHandler(audio_nodes);
1140 const size_t init_nodes_size = audio_nodes.size();
1142 // Verify the audio devices size.
1143 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1144 AudioDeviceList audio_devices;
1145 cras_audio_handler_->GetAudioDevices(&audio_devices);
1146 EXPECT_EQ(init_nodes_size, audio_devices.size());
1148 // Verify the bluetooth headset is selected as the active output.
1149 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1150 EXPECT_EQ(kBluetoothHeadsetId, cras_audio_handler_->GetActiveOutputNode());
1151 AudioDevice active_output;
1152 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
1153 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1155 // Plug in headphone, but fire NodesChanged signal twice.
1156 audio_nodes.clear();
1157 audio_nodes.push_back(kInternalSpeaker);
1158 AudioNode bluetooth_headset(kBluetoothHeadset);
1159 bluetooth_headset.plugged_time = 1000;
1160 bluetooth_headset.active = true;
1161 audio_nodes.push_back(bluetooth_headset);
1162 AudioNode headphone(kHeadphone);
1163 headphone.active = false;
1164 headphone.plugged_time = 2000;
1165 audio_nodes.push_back(headphone);
1166 ChangeAudioNodes(audio_nodes);
1167 ChangeAudioNodes(audio_nodes);
1169 // Verify the active output device is set to headphone.
1170 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1171 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1172 EXPECT_EQ(headphone.id, cras_audio_handler_->GetActiveOutputNode());
1173 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
1174 EXPECT_EQ(headphone.id, active_output.id);
1176 // Verfiy the audio devices data is consistent, i.e., the active output device
1177 // should be headphone.
1178 cras_audio_handler_->GetAudioDevices(&audio_devices);
1179 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1180 for (size_t i = 0; i < audio_devices.size(); ++i) {
1181 if (audio_devices[i].id == kInternalSpeaker.id)
1182 EXPECT_FALSE(audio_devices[i].active);
1183 else if (audio_devices[i].id == bluetooth_headset.id)
1184 EXPECT_FALSE(audio_devices[i].active);
1185 else if (audio_devices[i].id == headphone.id)
1186 EXPECT_TRUE(audio_devices[i].active);
1192 TEST_F(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnPlugInUSBMic) {
1193 // Set up initial audio devices.
1194 AudioNodeList audio_nodes;
1195 audio_nodes.push_back(kInternalMic);
1196 SetUpCrasAudioHandler(audio_nodes);
1197 const size_t init_nodes_size = audio_nodes.size();
1199 // Verify the audio devices size.
1200 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1201 AudioDeviceList audio_devices;
1202 cras_audio_handler_->GetAudioDevices(&audio_devices);
1203 EXPECT_EQ(init_nodes_size, audio_devices.size());
1205 // Verify the internal mic is selected as the active output.
1206 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1207 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode());
1208 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
1209 EXPECT_TRUE(audio_devices[0].active);
1211 // Plug in usb mic, but fire NodesChanged signal twice.
1212 audio_nodes.clear();
1213 AudioNode internal_mic(kInternalMic);
1214 internal_mic.active = true;
1215 internal_mic.plugged_time = 1000;
1216 audio_nodes.push_back(internal_mic);
1217 AudioNode usb_mic(kUSBMic);
1218 usb_mic.active = false;
1219 usb_mic.plugged_time = 2000;
1220 audio_nodes.push_back(usb_mic);
1221 ChangeAudioNodes(audio_nodes);
1222 ChangeAudioNodes(audio_nodes);
1224 // Verify the active output device is set to headphone.
1225 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1226 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
1227 EXPECT_EQ(usb_mic.id, cras_audio_handler_->GetActiveInputNode());
1228 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1230 // Verfiy the audio devices data is consistent, i.e., the active input device
1231 // should be usb mic.
1232 cras_audio_handler_->GetAudioDevices(&audio_devices);
1233 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1234 for (size_t i = 0; i < audio_devices.size(); ++i) {
1235 if (audio_devices[i].id == kInternalMic.id)
1236 EXPECT_FALSE(audio_devices[i].active);
1237 else if (audio_devices[i].id == usb_mic.id)
1238 EXPECT_TRUE(audio_devices[i].active);
1244 // This is the case of crbug.com/291303.
1245 TEST_F(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnSystemBoot) {
1246 // Set up audio handler with empty audio_nodes.
1247 AudioNodeList audio_nodes;
1248 SetUpCrasAudioHandler(audio_nodes);
1250 AudioNode internal_speaker(kInternalSpeaker);
1251 internal_speaker.active = false;
1252 AudioNode headphone(kHeadphone);
1253 headphone.active = false;
1254 AudioNode internal_mic(kInternalMic);
1255 internal_mic.active = false;
1256 audio_nodes.push_back(internal_speaker);
1257 audio_nodes.push_back(headphone);
1258 audio_nodes.push_back(internal_mic);
1259 const size_t init_nodes_size = audio_nodes.size();
1261 // Simulate AudioNodesChanged signal being fired twice during system boot.
1262 ChangeAudioNodes(audio_nodes);
1263 ChangeAudioNodes(audio_nodes);
1265 // Verify the active output device is set to headphone.
1266 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1267 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1268 EXPECT_EQ(headphone.id, cras_audio_handler_->GetActiveOutputNode());
1269 AudioDevice active_output;
1270 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
1271 EXPECT_EQ(headphone.id, active_output.id);
1273 // Verify the active input device id is set to internal mic.
1274 EXPECT_EQ(internal_mic.id, cras_audio_handler_->GetActiveInputNode());
1276 // Verfiy the audio devices data is consistent, i.e., the active output device
1277 // should be headphone, and the active input device should internal mic.
1278 AudioDeviceList audio_devices;
1279 cras_audio_handler_->GetAudioDevices(&audio_devices);
1280 EXPECT_EQ(init_nodes_size, audio_devices.size());
1281 for (size_t i = 0; i < audio_devices.size(); ++i) {
1282 if (audio_devices[i].id == internal_speaker.id)
1283 EXPECT_FALSE(audio_devices[i].active);
1284 else if (audio_devices[i].id == headphone.id)
1285 EXPECT_TRUE(audio_devices[i].active);
1286 else if (audio_devices[i].id == internal_mic.id)
1287 EXPECT_TRUE(audio_devices[i].active);
1293 TEST_F(CrasAudioHandlerTest, SetOutputMute) {
1294 AudioNodeList audio_nodes;
1295 audio_nodes.push_back(kInternalSpeaker);
1296 SetUpCrasAudioHandler(audio_nodes);
1297 EXPECT_EQ(0, test_observer_->output_mute_changed_count());
1300 cras_audio_handler_->SetOutputMute(true);
1302 // Verify the output is muted, OnOutputMuteChanged event is fired,
1303 // and mute value is saved in the preferences.
1304 EXPECT_TRUE(cras_audio_handler_->IsOutputMuted());
1305 EXPECT_EQ(1, test_observer_->output_mute_changed_count());
1306 AudioDevice speaker(kInternalSpeaker);
1307 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(speaker));
1309 // Unmute the device.
1310 cras_audio_handler_->SetOutputMute(false);
1312 // Verify the output is unmuted, OnOutputMuteChanged event is fired,
1313 // and mute value is saved in the preferences.
1314 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
1315 EXPECT_EQ(2, test_observer_->output_mute_changed_count());
1316 EXPECT_FALSE(audio_pref_handler_->GetMuteValue(speaker));
1319 TEST_F(CrasAudioHandlerTest, SetInputMute) {
1320 AudioNodeList audio_nodes;
1321 audio_nodes.push_back(kInternalMic);
1322 SetUpCrasAudioHandler(audio_nodes);
1323 EXPECT_EQ(0, test_observer_->input_mute_changed_count());
1326 cras_audio_handler_->SetInputMute(true);
1328 // Verify the input is muted, OnInputMuteChanged event is fired,
1329 // and mute value is saved in the preferences.
1330 EXPECT_TRUE(cras_audio_handler_->IsInputMuted());
1331 EXPECT_EQ(1, test_observer_->input_mute_changed_count());
1332 AudioDevice internal_mic(kInternalMic);
1333 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(internal_mic));
1335 // Unmute the device.
1336 cras_audio_handler_->SetInputMute(false);
1338 // Verify the input is unmuted, OnInputMuteChanged event is fired,
1339 // and mute value is saved in the preferences.
1340 EXPECT_FALSE(cras_audio_handler_->IsInputMuted());
1341 EXPECT_EQ(2, test_observer_->input_mute_changed_count());
1342 EXPECT_FALSE(audio_pref_handler_->GetMuteValue(internal_mic));
1345 TEST_F(CrasAudioHandlerTest, SetOutputVolumePercent) {
1346 AudioNodeList audio_nodes;
1347 audio_nodes.push_back(kInternalSpeaker);
1348 SetUpCrasAudioHandler(audio_nodes);
1349 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
1351 cras_audio_handler_->SetOutputVolumePercent(60);
1353 // Verify the output volume is changed to the designated value,
1354 // OnOutputVolumeChanged event is fired, and the device volume value
1355 // is saved the preferences.
1356 const int kVolume = 60;
1357 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent());
1358 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
1360 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&device));
1361 EXPECT_EQ(device.id, kInternalSpeaker.id);
1362 EXPECT_EQ(kVolume, audio_pref_handler_->GetOutputVolumeValue(&device));
1365 TEST_F(CrasAudioHandlerTest, SetInputGainPercent) {
1366 AudioNodeList audio_nodes;
1367 audio_nodes.push_back(kInternalMic);
1368 SetUpCrasAudioHandler(audio_nodes);
1369 EXPECT_EQ(0, test_observer_->input_gain_changed_count());
1371 cras_audio_handler_->SetInputGainPercent(60);
1373 // Verify the input gain changed to the designated value,
1374 // OnInputGainChanged event is fired, and the device gain value
1375 // is saved in the preferences.
1376 const int kGain = 60;
1377 EXPECT_EQ(kGain, cras_audio_handler_->GetInputGainPercent());
1378 EXPECT_EQ(1, test_observer_->input_gain_changed_count());
1379 AudioDevice internal_mic(kInternalMic);
1380 EXPECT_EQ(kGain, audio_pref_handler_->GetInputGainValue(&internal_mic));
1383 TEST_F(CrasAudioHandlerTest, SetMuteForDevice) {
1384 AudioNodeList audio_nodes;
1385 audio_nodes.push_back(kInternalSpeaker);
1386 audio_nodes.push_back(kHeadphone);
1387 audio_nodes.push_back(kInternalMic);
1388 audio_nodes.push_back(kUSBMic);
1389 SetUpCrasAudioHandler(audio_nodes);
1391 // Mute the active output device.
1392 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
1393 cras_audio_handler_->SetMuteForDevice(kHeadphone.id, true);
1395 // Verify the headphone is muted and mute value is saved in the preferences.
1396 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kHeadphone.id));
1397 AudioDevice headphone(kHeadphone);
1398 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(headphone));
1400 // Mute the non-active output device.
1401 cras_audio_handler_->SetMuteForDevice(kInternalSpeaker.id, true);
1403 // Verify the internal speaker is muted and mute value is saved in the
1405 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kInternalSpeaker.id));
1406 AudioDevice internal_speaker(kInternalSpeaker);
1407 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(internal_speaker));
1409 // Mute the active input device.
1410 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetActiveInputNode());
1411 cras_audio_handler_->SetMuteForDevice(kUSBMic.id, true);
1413 // Verify the USB Mic is muted and mute state is saved in the preferences.
1414 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kUSBMic.id));
1415 AudioDevice usb_mic(kUSBMic);
1416 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(usb_mic));
1418 // Mute the non-active input device.
1419 cras_audio_handler_->SetMuteForDevice(kInternalMic.id, true);
1421 // Verify the internal mic is muted and mute value is saved in the
1423 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kInternalMic.id));
1424 AudioDevice internal_mic(kInternalMic);
1425 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(internal_mic));
1428 TEST_F(CrasAudioHandlerTest, SetVolumeGainPercentForDevice) {
1429 AudioNodeList audio_nodes;
1430 audio_nodes.push_back(kInternalSpeaker);
1431 audio_nodes.push_back(kHeadphone);
1432 audio_nodes.push_back(kInternalMic);
1433 audio_nodes.push_back(kUSBMic);
1434 SetUpCrasAudioHandler(audio_nodes);
1436 // Set volume percent for active output device.
1437 const int kHeadphoneVolume = 30;
1438 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
1439 cras_audio_handler_->SetVolumeGainPercentForDevice(kHeadphone.id,
1442 // Verify the volume percent of headphone is set, and saved in preferences.
1443 EXPECT_EQ(kHeadphoneVolume,
1444 cras_audio_handler_->GetOutputVolumePercentForDevice(
1446 AudioDevice headphone(kHeadphone);
1447 EXPECT_EQ(kHeadphoneVolume,
1448 audio_pref_handler_->GetOutputVolumeValue(&headphone));
1450 // Set volume percent for non-active output device.
1451 const int kSpeakerVolume = 60;
1452 cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalSpeaker.id,
1455 // Verify the volume percent of speaker is set, and saved in preferences.
1456 EXPECT_EQ(kSpeakerVolume,
1457 cras_audio_handler_->GetOutputVolumePercentForDevice(
1458 kInternalSpeaker.id));
1459 AudioDevice speaker(kInternalSpeaker);
1460 EXPECT_EQ(kSpeakerVolume,
1461 audio_pref_handler_->GetOutputVolumeValue(&speaker));
1463 // Set gain percent for active input device.
1464 const int kUSBMicGain = 30;
1465 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetActiveInputNode());
1466 cras_audio_handler_->SetVolumeGainPercentForDevice(kUSBMic.id,
1469 // Verify the gain percent of USB mic is set, and saved in preferences.
1470 EXPECT_EQ(kUSBMicGain,
1471 cras_audio_handler_->GetOutputVolumePercentForDevice(kUSBMic.id));
1472 AudioDevice usb_mic(kHeadphone);
1473 EXPECT_EQ(kUSBMicGain,
1474 audio_pref_handler_->GetInputGainValue(&usb_mic));
1476 // Set gain percent for non-active input device.
1477 const int kInternalMicGain = 60;
1478 cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalMic.id,
1481 // Verify the gain percent of internal mic is set, and saved in preferences.
1482 EXPECT_EQ(kInternalMicGain,
1483 cras_audio_handler_->GetOutputVolumePercentForDevice(
1485 AudioDevice internal_mic(kInternalMic);
1486 EXPECT_EQ(kInternalMicGain,
1487 audio_pref_handler_->GetInputGainValue(&internal_mic));
1490 TEST_F(CrasAudioHandlerTest, HandleOtherDeviceType) {
1491 const size_t kNumValidAudioDevices = 4;
1492 AudioNodeList audio_nodes;
1493 audio_nodes.push_back(kInternalSpeaker);
1494 audio_nodes.push_back(kOtherTypeOutput);
1495 audio_nodes.push_back(kInternalMic);
1496 audio_nodes.push_back(kOtherTypeInput);
1497 SetUpCrasAudioHandler(audio_nodes);
1499 // Verify the audio devices size.
1500 AudioDeviceList audio_devices;
1501 cras_audio_handler_->GetAudioDevices(&audio_devices);
1502 EXPECT_EQ(kNumValidAudioDevices, audio_devices.size());
1504 // Verify the internal speaker has been selected as the active output,
1505 // and the output device with some randown unknown type is handled gracefully.
1506 AudioDevice active_output;
1507 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
1508 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
1509 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
1510 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1512 // Ensure the internal microphone has been selected as the active input,
1513 // and the input device with some random unknown type is handled gracefully.
1514 AudioDevice active_input;
1515 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode());
1516 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1519 } // namespace chromeos