Adding yfriedman@ as owner for the enhanced_bookmarks component.
[chromium-blink-merge.git] / chromeos / audio / cras_audio_handler_unittest.cc
blob58dbe04c008cf753ddff3f02a20331796f0eb73e
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"
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;
32 const AudioNode kInternalSpeaker(
33 false,
34 kInternalSpeakerId,
35 "Fake Speaker",
36 "INTERNAL_SPEAKER",
37 "Speaker",
38 false,
42 const AudioNode kHeadphone(
43 false,
44 kHeadphoneId,
45 "Fake Headphone",
46 "HEADPHONE",
47 "Headphone",
48 false,
52 const AudioNode kInternalMic(
53 true,
54 kInternalMicId,
55 "Fake Mic",
56 "INTERNAL_MIC",
57 "Internal Mic",
58 false,
62 const AudioNode kMicJack(
63 true,
64 kMicJackId,
65 "Fake Mic Jack",
66 "MIC",
67 "Mic Jack",
68 false,
72 const AudioNode kUSBMic(
73 true,
74 kUSBMicId,
75 "Fake USB Mic",
76 "USB",
77 "USB Microphone",
78 false,
82 const AudioNode kKeyboardMic(
83 true,
84 kKeyboardMicId,
85 "Fake Keyboard Mic",
86 "KEYBOARD_MIC",
87 "Keyboard Mic",
88 false,
92 const AudioNode kOtherTypeOutput(
93 false,
94 kOtherTypeOutputId,
95 "Output Device",
96 "SOME_OTHER_TYPE",
97 "Other Type Output Device",
98 false,
102 const AudioNode kOtherTypeInput(
103 true,
104 kOtherTypeInputId,
105 "Input Device",
106 "SOME_OTHER_TYPE",
107 "Other Type Input Device",
108 false,
112 const AudioNode kBluetoothHeadset (
113 false,
114 kBluetoothHeadsetId,
115 "Bluetooth Headset",
116 "BLUETOOTH",
117 "Bluetooth Headset 1",
118 false,
122 const AudioNode kHDMIOutput (
123 false,
124 kHDMIOutputId,
125 "HDMI output",
126 "HDMI",
127 "HDMI output",
128 false,
132 const AudioNode kUSBHeadphone1 (
133 false,
134 kUSBHeadphoneId1,
135 "USB Headphone",
136 "USB",
137 "USB Headphone 1",
138 false,
142 const AudioNode kUSBHeadphone2 (
143 false,
144 kUSBHeadphoneId2,
145 "USB Headphone",
146 "USB",
147 "USB Headphone 1",
148 false,
153 class TestObserver : public chromeos::CrasAudioHandler::AudioObserver {
154 public:
155 TestObserver() : active_output_node_changed_count_(0),
156 active_input_node_changed_count_(0),
157 audio_nodes_changed_count_(0),
158 output_mute_changed_count_(0),
159 input_mute_changed_count_(0),
160 output_volume_changed_count_(0),
161 input_gain_changed_count_(0) {
164 int active_output_node_changed_count() const {
165 return active_output_node_changed_count_;
168 int active_input_node_changed_count() const {
169 return active_input_node_changed_count_;
172 int audio_nodes_changed_count() const {
173 return audio_nodes_changed_count_;
176 int output_mute_changed_count() const {
177 return output_mute_changed_count_;
180 int input_mute_changed_count() const {
181 return input_mute_changed_count_;
184 int output_volume_changed_count() const {
185 return output_volume_changed_count_;
188 int input_gain_changed_count() const {
189 return input_gain_changed_count_;
192 virtual ~TestObserver() {}
194 protected:
195 // chromeos::CrasAudioHandler::AudioObserver overrides.
196 virtual void OnActiveOutputNodeChanged() OVERRIDE {
197 ++active_output_node_changed_count_;
200 virtual void OnActiveInputNodeChanged() OVERRIDE {
201 ++active_input_node_changed_count_;
204 virtual void OnAudioNodesChanged() OVERRIDE {
205 ++audio_nodes_changed_count_;
208 virtual void OnOutputMuteChanged() OVERRIDE {
209 ++output_mute_changed_count_;
212 virtual void OnInputMuteChanged() OVERRIDE {
213 ++input_mute_changed_count_;
216 virtual void OnOutputVolumeChanged() OVERRIDE {
217 ++output_volume_changed_count_;
220 virtual void OnInputGainChanged() OVERRIDE {
221 ++input_gain_changed_count_;
224 private:
225 int active_output_node_changed_count_;
226 int active_input_node_changed_count_;
227 int audio_nodes_changed_count_;
228 int output_mute_changed_count_;
229 int input_mute_changed_count_;
230 int output_volume_changed_count_;
231 int input_gain_changed_count_;
233 DISALLOW_COPY_AND_ASSIGN(TestObserver);
236 class CrasAudioHandlerTest : public testing::Test {
237 public:
238 CrasAudioHandlerTest() : cras_audio_handler_(NULL),
239 cras_audio_client_stub_(NULL) {
241 virtual ~CrasAudioHandlerTest() {}
243 virtual void SetUp() OVERRIDE {
246 virtual void TearDown() OVERRIDE {
247 cras_audio_handler_->RemoveAudioObserver(test_observer_.get());
248 test_observer_.reset();
249 CrasAudioHandler::Shutdown();
250 audio_pref_handler_ = NULL;
251 DBusThreadManager::Shutdown();
254 void SetUpCrasAudioHandler(const AudioNodeList& audio_nodes) {
255 DBusThreadManager::InitializeWithStub();
256 cras_audio_client_stub_ = static_cast<CrasAudioClientStubImpl*>(
257 DBusThreadManager::Get()->GetCrasAudioClient());
258 cras_audio_client_stub_->SetAudioDevices(audio_nodes);
259 audio_pref_handler_ = new AudioDevicesPrefHandlerStub();
260 CrasAudioHandler::Initialize(audio_pref_handler_);
261 cras_audio_handler_ = CrasAudioHandler::Get();
262 test_observer_.reset(new TestObserver);
263 cras_audio_handler_->AddAudioObserver(test_observer_.get());
264 message_loop_.RunUntilIdle();
267 void ChangeAudioNodes(const AudioNodeList& audio_nodes) {
268 cras_audio_client_stub_->ChangeAudioNodes(audio_nodes);
269 message_loop_.RunUntilIdle();
272 protected:
273 base::MessageLoopForUI message_loop_;
274 CrasAudioHandler* cras_audio_handler_; // Not owned.
275 CrasAudioClientStubImpl* cras_audio_client_stub_; // Not owned.
276 scoped_ptr<TestObserver> test_observer_;
277 scoped_refptr<AudioDevicesPrefHandlerStub> audio_pref_handler_;
279 private:
280 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandlerTest);
283 TEST_F(CrasAudioHandlerTest, InitializeWithOnlyDefaultAudioDevices) {
284 AudioNodeList audio_nodes;
285 audio_nodes.push_back(kInternalSpeaker);
286 audio_nodes.push_back(kInternalMic);
287 SetUpCrasAudioHandler(audio_nodes);
289 // Verify the audio devices size.
290 AudioDeviceList audio_devices;
291 cras_audio_handler_->GetAudioDevices(&audio_devices);
292 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
294 // Verify the internal speaker has been selected as the active output.
295 AudioDevice active_output;
296 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
297 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
298 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
299 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
301 // Ensure the internal microphone has been selected as the active input.
302 AudioDevice active_input;
303 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode());
304 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
307 TEST_F(CrasAudioHandlerTest, InitializeWithAlternativeAudioDevices) {
308 AudioNodeList audio_nodes;
309 audio_nodes.push_back(kInternalSpeaker);
310 audio_nodes.push_back(kHeadphone);
311 audio_nodes.push_back(kInternalMic);
312 audio_nodes.push_back(kUSBMic);
313 SetUpCrasAudioHandler(audio_nodes);
315 // Verify the audio devices size.
316 AudioDeviceList audio_devices;
317 cras_audio_handler_->GetAudioDevices(&audio_devices);
318 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
320 // Verify the headphone has been selected as the active output.
321 AudioDevice active_output;
322 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
323 EXPECT_EQ(kHeadphone.id, active_output.id);
324 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
325 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
327 // Ensure the USB microphone has been selected as the active input.
328 AudioDevice active_input;
329 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetActiveInputNode());
330 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
333 TEST_F(CrasAudioHandlerTest, InitializeWithKeyboardMic) {
334 AudioNodeList audio_nodes;
335 audio_nodes.push_back(kInternalSpeaker);
336 audio_nodes.push_back(kInternalMic);
337 audio_nodes.push_back(kKeyboardMic);
338 SetUpCrasAudioHandler(audio_nodes);
340 // Verify the audio devices size.
341 AudioDeviceList audio_devices;
342 cras_audio_handler_->GetAudioDevices(&audio_devices);
343 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
345 // Verify the internal speaker has been selected as the active output.
346 AudioDevice active_output;
347 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
348 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
349 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
350 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
352 // Ensure the internal microphone has been selected as the active input,
353 // not affected by keyboard mic.
354 AudioDevice active_input;
355 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode());
356 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
359 TEST_F(CrasAudioHandlerTest, SwitchActiveOutputDevice) {
360 AudioNodeList audio_nodes;
361 audio_nodes.push_back(kInternalSpeaker);
362 audio_nodes.push_back(kHeadphone);
363 SetUpCrasAudioHandler(audio_nodes);
364 AudioDeviceList audio_devices;
365 cras_audio_handler_->GetAudioDevices(&audio_devices);
366 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
368 // Verify the initial active output device is headphone.
369 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
370 AudioDevice active_output;
371 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
372 EXPECT_EQ(kHeadphone.id, active_output.id);
373 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
375 // Switch the active output to internal speaker.
376 AudioDevice internal_speaker(kInternalSpeaker);
377 cras_audio_handler_->SwitchToDevice(internal_speaker);
379 // Verify the active output is switched to internal speaker, and the
380 // ActiveOutputNodeChanged event is fired.
381 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
382 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
383 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
384 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
387 TEST_F(CrasAudioHandlerTest, SwitchActiveInputDevice) {
388 AudioNodeList audio_nodes;
389 audio_nodes.push_back(kInternalMic);
390 audio_nodes.push_back(kUSBMic);
391 SetUpCrasAudioHandler(audio_nodes);
392 AudioDeviceList audio_devices;
393 cras_audio_handler_->GetAudioDevices(&audio_devices);
394 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
396 // Verify the initial active input device is USB mic.
397 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
398 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetActiveInputNode());
400 // Switch the active input to internal mic.
401 AudioDevice internal_mic(kInternalMic);
402 cras_audio_handler_->SwitchToDevice(internal_mic);
404 // Verify the active output is switched to internal speaker, and the active
405 // ActiveInputNodeChanged event is fired.
406 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
407 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode());
410 TEST_F(CrasAudioHandlerTest, PlugHeadphone) {
411 // Set up initial audio devices, only with internal speaker.
412 AudioNodeList audio_nodes;
413 audio_nodes.push_back(kInternalSpeaker);
414 SetUpCrasAudioHandler(audio_nodes);
415 const size_t init_nodes_size = audio_nodes.size();
417 // Verify the audio devices size.
418 AudioDeviceList audio_devices;
419 cras_audio_handler_->GetAudioDevices(&audio_devices);
420 EXPECT_EQ(init_nodes_size, audio_devices.size());
421 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
423 // Verify the internal speaker has been selected as the active output.
424 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
425 AudioDevice active_output;
426 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
427 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
428 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
429 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
431 // Plug the headphone.
432 audio_nodes.clear();
433 AudioNode internal_speaker(kInternalSpeaker);
434 internal_speaker.active = true;
435 audio_nodes.push_back(internal_speaker);
436 audio_nodes.push_back(kHeadphone);
437 ChangeAudioNodes(audio_nodes);
439 // Verify the AudioNodesChanged event is fired and new audio device is added.
440 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
441 cras_audio_handler_->GetAudioDevices(&audio_devices);
442 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
444 // Verify the active output device is switched to headphone and
445 // ActiveOutputChanged event is fired.
446 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
447 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
448 EXPECT_EQ(kHeadphone.id, active_output.id);
449 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
450 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
453 TEST_F(CrasAudioHandlerTest, UnplugHeadphone) {
454 // Set up initial audio devices, with internal speaker and headphone.
455 AudioNodeList audio_nodes;
456 audio_nodes.push_back(kInternalSpeaker);
457 audio_nodes.push_back(kHeadphone);
458 SetUpCrasAudioHandler(audio_nodes);
459 const size_t init_nodes_size = audio_nodes.size();
461 // Verify the audio devices size.
462 AudioDeviceList audio_devices;
463 cras_audio_handler_->GetAudioDevices(&audio_devices);
464 EXPECT_EQ(init_nodes_size, audio_devices.size());
465 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
467 // Verify the headphone has been selected as the active output.
468 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
469 AudioDevice active_output;
470 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
471 EXPECT_EQ(kHeadphone.id, active_output.id);
472 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
473 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
475 // Unplug the headphone.
476 audio_nodes.clear();
477 audio_nodes.push_back(kInternalSpeaker);
478 ChangeAudioNodes(audio_nodes);
480 // Verify the AudioNodesChanged event is fired and one audio device is
481 // removed.
482 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
483 cras_audio_handler_->GetAudioDevices(&audio_devices);
484 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
486 // Verify the active output device is switched to internal speaker and
487 // ActiveOutputChanged event is fired.
488 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
489 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
490 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
491 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
492 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
495 TEST_F(CrasAudioHandlerTest, InitializeWithBluetoothHeadset) {
496 AudioNodeList audio_nodes;
497 audio_nodes.push_back(kInternalSpeaker);
498 audio_nodes.push_back(kBluetoothHeadset);
499 SetUpCrasAudioHandler(audio_nodes);
501 // Verify the audio devices size.
502 AudioDeviceList audio_devices;
503 cras_audio_handler_->GetAudioDevices(&audio_devices);
504 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
505 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
507 // Verify the bluetooth headset has been selected as the active output.
508 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
509 AudioDevice active_output;
510 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
511 EXPECT_EQ(kBluetoothHeadset.id, active_output.id);
512 EXPECT_EQ(kBluetoothHeadset.id, cras_audio_handler_->GetActiveOutputNode());
513 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
516 TEST_F(CrasAudioHandlerTest, ConnectAndDisconnectBluetoothHeadset) {
517 // Initialize with internal speaker and headphone.
518 AudioNodeList audio_nodes;
519 audio_nodes.push_back(kInternalSpeaker);
520 audio_nodes.push_back(kHeadphone);
521 SetUpCrasAudioHandler(audio_nodes);
522 const size_t init_nodes_size = audio_nodes.size();
524 // Verify the audio devices size.
525 AudioDeviceList audio_devices;
526 cras_audio_handler_->GetAudioDevices(&audio_devices);
527 EXPECT_EQ(init_nodes_size, audio_devices.size());
528 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
530 // Verify the headphone is selected as the active output initially.
531 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
532 AudioDevice active_output;
533 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
534 EXPECT_EQ(kHeadphone.id, active_output.id);
535 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
536 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
538 // Connect to bluetooth headset. Since it is plugged in later than
539 // headphone, active output should be switched to it.
540 audio_nodes.clear();
541 audio_nodes.push_back(kInternalSpeaker);
542 AudioNode headphone(kHeadphone);
543 headphone.plugged_time = 80000000;
544 headphone.active = true;
545 audio_nodes.push_back(headphone);
546 AudioNode bluetooth_headset(kBluetoothHeadset);
547 bluetooth_headset.plugged_time = 90000000;
548 audio_nodes.push_back(bluetooth_headset);
549 ChangeAudioNodes(audio_nodes);
551 // Verify the AudioNodesChanged event is fired and new audio device is added.
552 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
553 cras_audio_handler_->GetAudioDevices(&audio_devices);
554 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
556 // Verify the active output device is switched to bluetooth headset, and
557 // ActiveOutputChanged event is fired.
558 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
559 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
560 EXPECT_EQ(kBluetoothHeadset.id, active_output.id);
561 EXPECT_EQ(kBluetoothHeadset.id, cras_audio_handler_->GetActiveOutputNode());
562 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
564 // Disconnect bluetooth headset.
565 audio_nodes.clear();
566 audio_nodes.push_back(kInternalSpeaker);
567 headphone.active = false;
568 audio_nodes.push_back(headphone);
569 ChangeAudioNodes(audio_nodes);
571 // Verify the AudioNodesChanged event is fired and one audio device is
572 // removed.
573 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
574 cras_audio_handler_->GetAudioDevices(&audio_devices);
575 EXPECT_EQ(init_nodes_size, audio_devices.size());
577 // Verify the active output device is switched to headphone, and
578 // ActiveOutputChanged event is fired.
579 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
580 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
581 EXPECT_EQ(kHeadphone.id, active_output.id);
582 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
583 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
586 TEST_F(CrasAudioHandlerTest, InitializeWithHDMIOutput) {
587 AudioNodeList audio_nodes;
588 audio_nodes.push_back(kInternalSpeaker);
589 audio_nodes.push_back(kHDMIOutput);
590 SetUpCrasAudioHandler(audio_nodes);
592 // Verify the audio devices size.
593 AudioDeviceList audio_devices;
594 cras_audio_handler_->GetAudioDevices(&audio_devices);
595 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
596 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
598 // Verify the HDMI device has been selected as the active output.
599 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
600 AudioDevice active_output;
601 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
602 EXPECT_EQ(kHDMIOutput.id, active_output.id);
603 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetActiveOutputNode());
604 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
607 TEST_F(CrasAudioHandlerTest, ConnectAndDisconnectHDMIOutput) {
608 // Initialize with internal speaker.
609 AudioNodeList audio_nodes;
610 audio_nodes.push_back(kInternalSpeaker);
611 SetUpCrasAudioHandler(audio_nodes);
612 const size_t init_nodes_size = audio_nodes.size();
614 // Verify the audio devices size.
615 AudioDeviceList audio_devices;
616 cras_audio_handler_->GetAudioDevices(&audio_devices);
617 EXPECT_EQ(init_nodes_size, audio_devices.size());
618 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
620 // Verify the internal speaker is selected as the active output initially.
621 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
622 AudioDevice active_output;
623 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
624 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
625 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
626 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
628 // Connect to HDMI output.
629 audio_nodes.clear();
630 AudioNode internal_speaker(kInternalSpeaker);
631 internal_speaker.active = true;
632 internal_speaker.plugged_time = 80000000;
633 audio_nodes.push_back(internal_speaker);
634 AudioNode hdmi(kHDMIOutput);
635 hdmi.plugged_time = 90000000;
636 audio_nodes.push_back(hdmi);
637 ChangeAudioNodes(audio_nodes);
639 // Verify the AudioNodesChanged event is fired and new audio device is added.
640 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
641 cras_audio_handler_->GetAudioDevices(&audio_devices);
642 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
644 // Verify the active output device is switched to hdmi output, and
645 // ActiveOutputChanged event is fired.
646 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
647 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
648 EXPECT_EQ(kHDMIOutput.id, active_output.id);
649 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetActiveOutputNode());
650 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
652 // Disconnect hdmi headset.
653 audio_nodes.clear();
654 audio_nodes.push_back(kInternalSpeaker);
655 ChangeAudioNodes(audio_nodes);
657 // Verify the AudioNodesChanged event is fired and one audio device is
658 // removed.
659 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
660 cras_audio_handler_->GetAudioDevices(&audio_devices);
661 EXPECT_EQ(init_nodes_size, audio_devices.size());
663 // Verify the active output device is switched to internal speaker, and
664 // ActiveOutputChanged event is fired.
665 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
666 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
667 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
668 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
669 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
672 TEST_F(CrasAudioHandlerTest, HandleHeadphoneAndHDMIOutput) {
673 // Initialize with internal speaker, headphone and HDMI output.
674 AudioNodeList audio_nodes;
675 audio_nodes.push_back(kInternalSpeaker);
676 audio_nodes.push_back(kHeadphone);
677 audio_nodes.push_back(kHDMIOutput);
678 SetUpCrasAudioHandler(audio_nodes);
679 const size_t init_nodes_size = audio_nodes.size();
681 // Verify the audio devices size.
682 AudioDeviceList audio_devices;
683 cras_audio_handler_->GetAudioDevices(&audio_devices);
684 EXPECT_EQ(init_nodes_size, audio_devices.size());
685 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
687 // Verify the headphone is selected as the active output initially.
688 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
689 AudioDevice active_output;
690 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
691 EXPECT_EQ(kHeadphone.id, active_output.id);
692 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
693 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
695 // Disconnect HDMI output.
696 audio_nodes.clear();
697 audio_nodes.push_back(kInternalSpeaker);
698 audio_nodes.push_back(kHDMIOutput);
699 ChangeAudioNodes(audio_nodes);
701 // Verify the AudioNodesChanged event is fired and one audio device is
702 // removed.
703 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
704 cras_audio_handler_->GetAudioDevices(&audio_devices);
705 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
707 // Verify the active output device is switched to HDMI output, and
708 // ActiveOutputChanged event is fired.
709 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
710 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
711 EXPECT_EQ(kHDMIOutput.id, active_output.id);
712 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetActiveOutputNode());
713 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
716 TEST_F(CrasAudioHandlerTest, InitializeWithUSBHeadphone) {
717 AudioNodeList audio_nodes;
718 audio_nodes.push_back(kInternalSpeaker);
719 audio_nodes.push_back(kUSBHeadphone1);
720 SetUpCrasAudioHandler(audio_nodes);
722 // Verify the audio devices size.
723 AudioDeviceList audio_devices;
724 cras_audio_handler_->GetAudioDevices(&audio_devices);
725 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
726 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
728 // Verify the usb headphone has been selected as the active output.
729 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
730 AudioDevice active_output;
731 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
732 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
733 EXPECT_EQ(kUSBHeadphone1.id, cras_audio_handler_->GetActiveOutputNode());
734 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
737 TEST_F(CrasAudioHandlerTest, PlugAndUnplugUSBHeadphone) {
738 // Initialize with internal speaker.
739 AudioNodeList audio_nodes;
740 audio_nodes.push_back(kInternalSpeaker);
741 SetUpCrasAudioHandler(audio_nodes);
742 const size_t init_nodes_size = audio_nodes.size();
744 // Verify the audio devices size.
745 AudioDeviceList audio_devices;
746 cras_audio_handler_->GetAudioDevices(&audio_devices);
747 EXPECT_EQ(init_nodes_size, audio_devices.size());
748 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
750 // Verify the internal speaker is selected as the active output initially.
751 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
752 AudioDevice active_output;
753 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
754 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
755 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
756 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
758 // Plug in usb headphone
759 audio_nodes.clear();
760 AudioNode internal_speaker(kInternalSpeaker);
761 internal_speaker.active = true;
762 internal_speaker.plugged_time = 80000000;
763 audio_nodes.push_back(internal_speaker);
764 AudioNode usb_headphone(kUSBHeadphone1);
765 usb_headphone.plugged_time = 90000000;
766 audio_nodes.push_back(usb_headphone);
767 ChangeAudioNodes(audio_nodes);
769 // Verify the AudioNodesChanged event is fired and new audio device is added.
770 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
771 cras_audio_handler_->GetAudioDevices(&audio_devices);
772 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
774 // Verify the active output device is switched to usb headphone, and
775 // ActiveOutputChanged event is fired.
776 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
777 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
778 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
779 EXPECT_EQ(kUSBHeadphone1.id, cras_audio_handler_->GetActiveOutputNode());
780 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
782 // Unplug usb headphone.
783 audio_nodes.clear();
784 audio_nodes.push_back(kInternalSpeaker);
785 ChangeAudioNodes(audio_nodes);
787 // Verify the AudioNodesChanged event is fired and one audio device is
788 // removed.
789 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
790 cras_audio_handler_->GetAudioDevices(&audio_devices);
791 EXPECT_EQ(init_nodes_size, audio_devices.size());
793 // Verify the active output device is switched to internal speaker, and
794 // ActiveOutputChanged event is fired.
795 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
796 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
797 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
798 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
799 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
802 TEST_F(CrasAudioHandlerTest, HandleMultipleUSBHeadphones) {
803 // Initialize with internal speaker and one usb headphone.
804 AudioNodeList audio_nodes;
805 audio_nodes.push_back(kInternalSpeaker);
806 audio_nodes.push_back(kUSBHeadphone1);
807 SetUpCrasAudioHandler(audio_nodes);
808 const size_t init_nodes_size = audio_nodes.size();
810 // Verify the audio devices size.
811 AudioDeviceList audio_devices;
812 cras_audio_handler_->GetAudioDevices(&audio_devices);
813 EXPECT_EQ(init_nodes_size, audio_devices.size());
814 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
816 // Verify the usb headphone is selected as the active output initially.
817 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
818 AudioDevice active_output;
819 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
820 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
821 EXPECT_EQ(kUSBHeadphone1.id, cras_audio_handler_->GetActiveOutputNode());
822 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
824 // Plug in another usb headphone.
825 audio_nodes.clear();
826 audio_nodes.push_back(kInternalSpeaker);
827 AudioNode usb_headphone_1(kUSBHeadphone1);
828 usb_headphone_1.active = true;
829 usb_headphone_1.plugged_time = 80000000;
830 audio_nodes.push_back(usb_headphone_1);
831 AudioNode usb_headphone_2(kUSBHeadphone2);
832 usb_headphone_2.plugged_time = 90000000;
833 audio_nodes.push_back(usb_headphone_2);
834 ChangeAudioNodes(audio_nodes);
836 // Verify the AudioNodesChanged event is fired and new audio device is added.
837 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
838 cras_audio_handler_->GetAudioDevices(&audio_devices);
839 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
841 // Verify the active output device is switched to the 2nd usb headphone, which
842 // is plugged later, and ActiveOutputChanged event is fired.
843 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
844 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
845 EXPECT_EQ(kUSBHeadphone2.id, active_output.id);
846 EXPECT_EQ(kUSBHeadphone2.id, cras_audio_handler_->GetActiveOutputNode());
847 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
849 // Unplug the 2nd usb headphone.
850 audio_nodes.clear();
851 audio_nodes.push_back(kInternalSpeaker);
852 audio_nodes.push_back(kUSBHeadphone1);
853 ChangeAudioNodes(audio_nodes);
855 // Verify the AudioNodesChanged event is fired and one audio device is
856 // removed.
857 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
858 cras_audio_handler_->GetAudioDevices(&audio_devices);
859 EXPECT_EQ(init_nodes_size, audio_devices.size());
861 // Verify the active output device is switched to the first usb headphone, and
862 // ActiveOutputChanged event is fired.
863 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
864 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
865 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
866 EXPECT_EQ(kUSBHeadphone1.id, cras_audio_handler_->GetActiveOutputNode());
867 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
870 TEST_F(CrasAudioHandlerTest, UnplugUSBHeadphonesWithActiveSpeaker) {
871 // Initialize with internal speaker and one usb headphone.
872 AudioNodeList audio_nodes;
873 audio_nodes.push_back(kInternalSpeaker);
874 audio_nodes.push_back(kUSBHeadphone1);
875 SetUpCrasAudioHandler(audio_nodes);
876 const size_t init_nodes_size = audio_nodes.size();
878 // Verify the audio devices size.
879 AudioDeviceList audio_devices;
880 cras_audio_handler_->GetAudioDevices(&audio_devices);
881 EXPECT_EQ(init_nodes_size, audio_devices.size());
882 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
884 // Verify the usb headphone is selected as the active output initially.
885 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
886 AudioDevice active_output;
887 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
888 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
889 EXPECT_EQ(kUSBHeadphone1.id, cras_audio_handler_->GetActiveOutputNode());
890 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
892 // Plug in the headphone jack.
893 audio_nodes.clear();
894 audio_nodes.push_back(kInternalSpeaker);
895 AudioNode usb_headphone_1(kUSBHeadphone1);
896 usb_headphone_1.active = true;
897 usb_headphone_1.plugged_time = 80000000;
898 audio_nodes.push_back(usb_headphone_1);
899 AudioNode headphone_jack(kHeadphone);
900 headphone_jack.plugged_time = 90000000;
901 audio_nodes.push_back(headphone_jack);
902 ChangeAudioNodes(audio_nodes);
904 // Verify the AudioNodesChanged event is fired and new audio device is added.
905 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
906 cras_audio_handler_->GetAudioDevices(&audio_devices);
907 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
909 // Verify the active output device is switched to the headphone jack, which
910 // is plugged later, and ActiveOutputChanged event is fired.
911 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
912 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
913 EXPECT_EQ(kHeadphone.id, active_output.id);
914 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
915 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
917 // Select the speaker to be the active output device.
918 AudioDevice internal_speaker(kInternalSpeaker);
919 cras_audio_handler_->SwitchToDevice(internal_speaker);
921 // Verify the active output is switched to internal speaker, and the
922 // ActiveOutputNodeChanged event is fired.
923 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
924 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
925 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
926 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
928 // Unplug the usb headphone.
929 audio_nodes.clear();
930 AudioNode internal_speaker_node(kInternalSpeaker);
931 internal_speaker_node.active = true;
932 internal_speaker_node.plugged_time = 70000000;
933 audio_nodes.push_back(internal_speaker_node);
934 headphone_jack.active = false;
935 audio_nodes.push_back(headphone_jack);
936 ChangeAudioNodes(audio_nodes);
938 // Verify the AudioNodesChanged event is fired and one audio device is
939 // removed.
940 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
941 cras_audio_handler_->GetAudioDevices(&audio_devices);
942 EXPECT_EQ(init_nodes_size, audio_devices.size());
944 // Verify the active output device remains to be speaker.
945 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
946 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
947 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
948 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
949 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
952 TEST_F(CrasAudioHandlerTest, OneActiveAudioOutputAfterLoginNewUserSession) {
953 // This tests the case found with crbug.com/273271.
954 // Initialize with internal speaker, bluetooth headphone and headphone jack
955 // for a new chrome session after user signs out from the previous session.
956 // Headphone jack is plugged in later than bluetooth headphone, but bluetooth
957 // headphone is selected as the active output by user from previous user
958 // session.
959 AudioNodeList audio_nodes;
960 audio_nodes.push_back(kInternalSpeaker);
961 AudioNode bluetooth_headphone(kBluetoothHeadset);
962 bluetooth_headphone.active = true;
963 bluetooth_headphone.plugged_time = 70000000;
964 audio_nodes.push_back(bluetooth_headphone);
965 AudioNode headphone_jack(kHeadphone);
966 headphone_jack.plugged_time = 80000000;
967 audio_nodes.push_back(headphone_jack);
968 SetUpCrasAudioHandler(audio_nodes);
969 const size_t init_nodes_size = audio_nodes.size();
971 // Verify the audio devices size.
972 AudioDeviceList audio_devices;
973 cras_audio_handler_->GetAudioDevices(&audio_devices);
974 EXPECT_EQ(init_nodes_size, audio_devices.size());
975 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
977 // Verify the headphone jack is selected as the active output and all other
978 // audio devices are not active.
979 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
980 AudioDevice active_output;
981 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
982 EXPECT_EQ(kHeadphone.id, active_output.id);
983 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
984 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
985 for (size_t i = 0; i < audio_devices.size(); ++i) {
986 if (audio_devices[i].id != kHeadphone.id)
987 EXPECT_FALSE(audio_devices[i].active);
991 TEST_F(CrasAudioHandlerTest, BluetoothSpeakerIdChangedOnFly) {
992 // Initialize with internal speaker and bluetooth headset.
993 AudioNodeList audio_nodes;
994 audio_nodes.push_back(kInternalSpeaker);
995 audio_nodes.push_back(kBluetoothHeadset);
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 bluetooth headset is selected as the active output and all other
1006 // audio devices are not active.
1007 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1008 AudioDevice active_output;
1009 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
1010 EXPECT_EQ(kBluetoothHeadset.id, active_output.id);
1011 EXPECT_EQ(kBluetoothHeadset.id, cras_audio_handler_->GetActiveOutputNode());
1012 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1014 // Cras changes the bluetooth headset's id on the fly.
1015 audio_nodes.clear();
1016 AudioNode internal_speaker(kInternalSpeaker);
1017 internal_speaker.active = false;
1018 audio_nodes.push_back(internal_speaker);
1019 AudioNode bluetooth_headphone(kBluetoothHeadset);
1020 // Change bluetooth headphone id.
1021 bluetooth_headphone.id = kBluetoothHeadsetId + 20000;
1022 bluetooth_headphone.active = false;
1023 audio_nodes.push_back(bluetooth_headphone);
1024 ChangeAudioNodes(audio_nodes);
1026 // Verify NodesChanged event is fired, and the audio devices size is not
1027 // changed.
1028 audio_devices.clear();
1029 cras_audio_handler_->GetAudioDevices(&audio_devices);
1030 EXPECT_EQ(init_nodes_size, audio_devices.size());
1031 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1033 // Verify ActiveOutputNodeChanged event is fired, and active device should be
1034 // bluetooth headphone.
1035 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1036 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
1037 EXPECT_EQ(bluetooth_headphone.id, active_output.id);
1040 TEST_F(CrasAudioHandlerTest, PlugUSBMic) {
1041 // Set up initial audio devices, only with internal mic.
1042 AudioNodeList audio_nodes;
1043 audio_nodes.push_back(kInternalMic);
1044 SetUpCrasAudioHandler(audio_nodes);
1045 const size_t init_nodes_size = audio_nodes.size();
1047 // Verify the audio devices size.
1048 AudioDeviceList audio_devices;
1049 cras_audio_handler_->GetAudioDevices(&audio_devices);
1050 EXPECT_EQ(init_nodes_size, audio_devices.size());
1051 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1053 // Verify the internal mic is selected as the active input.
1054 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1055 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode());
1056 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1058 // Plug the USB Mic.
1059 audio_nodes.clear();
1060 AudioNode internal_mic(kInternalMic);
1061 internal_mic.active = true;
1062 audio_nodes.push_back(internal_mic);
1063 audio_nodes.push_back(kUSBMic);
1064 ChangeAudioNodes(audio_nodes);
1066 // Verify the AudioNodesChanged event is fired and new audio device is added.
1067 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1068 cras_audio_handler_->GetAudioDevices(&audio_devices);
1069 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1071 // Verify the active input device is switched to USB mic and
1072 // and ActiveInputChanged event is fired.
1073 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
1074 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetActiveInputNode());
1075 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1078 TEST_F(CrasAudioHandlerTest, UnplugUSBMic) {
1079 // Set up initial audio devices, with internal mic and USB Mic.
1080 AudioNodeList audio_nodes;
1081 audio_nodes.push_back(kInternalMic);
1082 audio_nodes.push_back(kUSBMic);
1083 SetUpCrasAudioHandler(audio_nodes);
1084 const size_t init_nodes_size = audio_nodes.size();
1086 // Verify the audio devices size.
1087 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1088 AudioDeviceList audio_devices;
1089 cras_audio_handler_->GetAudioDevices(&audio_devices);
1090 EXPECT_EQ(init_nodes_size, audio_devices.size());
1092 // Verify the USB mic is selected as the active output.
1093 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1094 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetActiveInputNode());
1095 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1097 // Unplug the USB Mic.
1098 audio_nodes.clear();
1099 audio_nodes.push_back(kInternalMic);
1100 ChangeAudioNodes(audio_nodes);
1102 // Verify the AudioNodesChanged event is fired, and one audio device is
1103 // removed.
1104 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1105 cras_audio_handler_->GetAudioDevices(&audio_devices);
1106 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
1108 // Verify the active input device is switched to internal mic, and
1109 // and ActiveInputChanged event is fired.
1110 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
1111 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode());
1112 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1115 TEST_F(CrasAudioHandlerTest, PlugUSBMicNotAffectActiveOutput) {
1116 // Set up initial audio devices.
1117 AudioNodeList audio_nodes;
1118 audio_nodes.push_back(kInternalSpeaker);
1119 audio_nodes.push_back(kHeadphone);
1120 audio_nodes.push_back(kInternalMic);
1121 SetUpCrasAudioHandler(audio_nodes);
1122 const size_t init_nodes_size = audio_nodes.size();
1124 // Verify the audio devices size.
1125 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1126 AudioDeviceList audio_devices;
1127 cras_audio_handler_->GetAudioDevices(&audio_devices);
1128 EXPECT_EQ(init_nodes_size, audio_devices.size());
1130 // Verify the internal mic is selected as the active input.
1131 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1132 EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetActiveInputNode());
1133 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1135 // Verify the headphone is selected as the active output.
1136 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1137 EXPECT_EQ(kHeadphoneId, cras_audio_handler_->GetActiveOutputNode());
1138 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1140 // Switch the active output to internal speaker.
1141 AudioDevice internal_speaker(kInternalSpeaker);
1142 cras_audio_handler_->SwitchToDevice(internal_speaker);
1144 // Verify the active output is switched to internal speaker, and the
1145 // ActiveOutputNodeChanged event is fired.
1146 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1147 AudioDevice active_output;
1148 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
1149 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
1150 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
1152 // Plug the USB Mic.
1153 audio_nodes.clear();
1154 AudioNode internal_speaker_node(kInternalSpeaker);
1155 internal_speaker_node.active = true;
1156 audio_nodes.push_back(internal_speaker_node);
1157 audio_nodes.push_back(kHeadphone);
1158 AudioNode internal_mic(kInternalMic);
1159 internal_mic.active = true;
1160 audio_nodes.push_back(internal_mic);
1161 audio_nodes.push_back(kUSBMic);
1162 ChangeAudioNodes(audio_nodes);
1164 // Verify the AudioNodesChanged event is fired, one new device is added.
1165 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1166 cras_audio_handler_->GetAudioDevices(&audio_devices);
1167 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1169 // Verify the active input device is switched to USB mic, and
1170 // and ActiveInputChanged event is fired.
1171 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
1172 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetActiveInputNode());
1173 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1175 // Verify the active output device is not changed.
1176 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1177 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
1178 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
1179 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
1182 TEST_F(CrasAudioHandlerTest, PlugHeadphoneAutoUnplugSpeakerWithActiveUSB) {
1183 // Set up initial audio devices.
1184 AudioNodeList audio_nodes;
1185 audio_nodes.push_back(kUSBHeadphone1);
1186 audio_nodes.push_back(kInternalSpeaker);
1187 audio_nodes.push_back(kInternalMic);
1188 SetUpCrasAudioHandler(audio_nodes);
1189 const size_t init_nodes_size = audio_nodes.size();
1191 // Verify the audio devices size.
1192 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1193 AudioDeviceList audio_devices;
1194 cras_audio_handler_->GetAudioDevices(&audio_devices);
1195 EXPECT_EQ(init_nodes_size, audio_devices.size());
1197 // Verify the internal mic is selected as the active input.
1198 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1199 EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetActiveInputNode());
1200 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1202 // Verify the USB headphone is selected as the active output.
1203 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1204 EXPECT_EQ(kUSBHeadphoneId1, cras_audio_handler_->GetActiveOutputNode());
1205 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1207 // Plug the headphone and auto-unplug internal speaker.
1208 audio_nodes.clear();
1209 AudioNode usb_headphone_node(kUSBHeadphone1);
1210 usb_headphone_node.active = true;
1211 audio_nodes.push_back(usb_headphone_node);
1212 AudioNode headphone_node(kHeadphone);
1213 headphone_node.plugged_time = 1000;
1214 audio_nodes.push_back(headphone_node);
1215 AudioNode internal_mic(kInternalMic);
1216 internal_mic.active = true;
1217 audio_nodes.push_back(internal_mic);
1218 ChangeAudioNodes(audio_nodes);
1220 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1221 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1222 cras_audio_handler_->GetAudioDevices(&audio_devices);
1223 EXPECT_EQ(init_nodes_size, audio_devices.size());
1225 // Verify the active output device is switched to headphone, and
1226 // an ActiveOutputChanged event is fired.
1227 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1228 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
1229 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1231 // Unplug the headphone and internal speaker auto-plugs back.
1232 audio_nodes.clear();
1233 audio_nodes.push_back(kUSBHeadphone1);
1234 AudioNode internal_speaker_node(kInternalSpeaker);
1235 internal_speaker_node.plugged_time = 2000;
1236 audio_nodes.push_back(internal_speaker_node);
1237 audio_nodes.push_back(internal_mic);
1238 ChangeAudioNodes(audio_nodes);
1240 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1241 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1242 cras_audio_handler_->GetAudioDevices(&audio_devices);
1243 EXPECT_EQ(init_nodes_size, audio_devices.size());
1245 // Verify the active output device is switched back to USB, and
1246 // an ActiveOutputChanged event is fired.
1247 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
1248 EXPECT_EQ(kUSBHeadphone1.id, cras_audio_handler_->GetActiveOutputNode());
1249 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1251 // Verify the active input device is not changed.
1252 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1253 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode());
1256 TEST_F(CrasAudioHandlerTest, PlugMicAutoUnplugInternalMicWithActiveUSB) {
1257 // Set up initial audio devices.
1258 AudioNodeList audio_nodes;
1259 audio_nodes.push_back(kUSBHeadphone1);
1260 audio_nodes.push_back(kInternalSpeaker);
1261 audio_nodes.push_back(kUSBMic);
1262 audio_nodes.push_back(kInternalMic);
1263 SetUpCrasAudioHandler(audio_nodes);
1264 const size_t init_nodes_size = audio_nodes.size();
1266 // Verify the audio devices size.
1267 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1268 AudioDeviceList audio_devices;
1269 cras_audio_handler_->GetAudioDevices(&audio_devices);
1270 EXPECT_EQ(init_nodes_size, audio_devices.size());
1272 // Verify the internal mic is selected as the active input.
1273 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1274 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetActiveInputNode());
1275 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1277 // Verify the internal speaker is selected as the active output.
1278 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1279 EXPECT_EQ(kUSBHeadphoneId1, cras_audio_handler_->GetActiveOutputNode());
1280 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1282 // Plug the headphone and mic, auto-unplug internal mic and speaker.
1283 audio_nodes.clear();
1284 AudioNode usb_headphone_node(kUSBHeadphone1);
1285 usb_headphone_node.active = true;
1286 audio_nodes.push_back(usb_headphone_node);
1287 AudioNode headphone_node(kHeadphone);
1288 headphone_node.plugged_time = 1000;
1289 audio_nodes.push_back(headphone_node);
1290 AudioNode usb_mic(kUSBMic);
1291 usb_mic.active = true;
1292 audio_nodes.push_back(usb_mic);
1293 AudioNode mic_jack(kMicJack);
1294 mic_jack.plugged_time = 1000;
1295 audio_nodes.push_back(mic_jack);
1296 ChangeAudioNodes(audio_nodes);
1298 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1299 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1300 cras_audio_handler_->GetAudioDevices(&audio_devices);
1301 EXPECT_EQ(init_nodes_size, audio_devices.size());
1303 // Verify the active output device is switched to headphone, and
1304 // an ActiveOutputChanged event is fired.
1305 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1306 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
1307 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1309 // Verify the active input device is switched to mic jack, and
1310 // an ActiveInputChanged event is fired.
1311 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
1312 EXPECT_EQ(kMicJack.id, cras_audio_handler_->GetActiveInputNode());
1313 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1315 // Unplug the headphone and internal speaker auto-plugs back.
1316 audio_nodes.clear();
1317 audio_nodes.push_back(kUSBHeadphone1);
1318 AudioNode internal_speaker_node(kInternalSpeaker);
1319 internal_speaker_node.plugged_time = 2000;
1320 audio_nodes.push_back(internal_speaker_node);
1321 audio_nodes.push_back(kUSBMic);
1322 AudioNode internal_mic(kInternalMic);
1323 internal_mic.plugged_time = 2000;
1324 audio_nodes.push_back(internal_mic);
1325 ChangeAudioNodes(audio_nodes);
1327 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1328 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1329 cras_audio_handler_->GetAudioDevices(&audio_devices);
1330 EXPECT_EQ(init_nodes_size, audio_devices.size());
1332 // Verify the active output device is switched back to USB, and
1333 // an ActiveOutputChanged event is fired.
1334 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
1335 EXPECT_EQ(kUSBHeadphone1.id, cras_audio_handler_->GetActiveOutputNode());
1336 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1338 // Verify the active input device is switched back to USB mic, and
1339 // an ActiveInputChanged event is fired.
1340 EXPECT_EQ(2, test_observer_->active_input_node_changed_count());
1341 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetActiveInputNode());
1342 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1345 TEST_F(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnPlugInHeadphone) {
1346 // Set up initial audio devices.
1347 AudioNodeList audio_nodes;
1348 audio_nodes.push_back(kInternalSpeaker);
1349 audio_nodes.push_back(kBluetoothHeadset);
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 bluetooth headset is selected as the active output.
1360 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1361 EXPECT_EQ(kBluetoothHeadsetId, cras_audio_handler_->GetActiveOutputNode());
1362 AudioDevice active_output;
1363 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
1364 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1366 // Plug in headphone, but fire NodesChanged signal twice.
1367 audio_nodes.clear();
1368 audio_nodes.push_back(kInternalSpeaker);
1369 AudioNode bluetooth_headset(kBluetoothHeadset);
1370 bluetooth_headset.plugged_time = 1000;
1371 bluetooth_headset.active = true;
1372 audio_nodes.push_back(bluetooth_headset);
1373 AudioNode headphone(kHeadphone);
1374 headphone.active = false;
1375 headphone.plugged_time = 2000;
1376 audio_nodes.push_back(headphone);
1377 ChangeAudioNodes(audio_nodes);
1378 ChangeAudioNodes(audio_nodes);
1380 // Verify the active output device is set to headphone.
1381 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1382 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1383 EXPECT_EQ(headphone.id, cras_audio_handler_->GetActiveOutputNode());
1384 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
1385 EXPECT_EQ(headphone.id, active_output.id);
1387 // Verfiy the audio devices data is consistent, i.e., the active output device
1388 // should be headphone.
1389 cras_audio_handler_->GetAudioDevices(&audio_devices);
1390 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1391 for (size_t i = 0; i < audio_devices.size(); ++i) {
1392 if (audio_devices[i].id == kInternalSpeaker.id)
1393 EXPECT_FALSE(audio_devices[i].active);
1394 else if (audio_devices[i].id == bluetooth_headset.id)
1395 EXPECT_FALSE(audio_devices[i].active);
1396 else if (audio_devices[i].id == headphone.id)
1397 EXPECT_TRUE(audio_devices[i].active);
1398 else
1399 NOTREACHED();
1403 TEST_F(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnPlugInUSBMic) {
1404 // Set up initial audio devices.
1405 AudioNodeList audio_nodes;
1406 audio_nodes.push_back(kInternalMic);
1407 SetUpCrasAudioHandler(audio_nodes);
1408 const size_t init_nodes_size = audio_nodes.size();
1410 // Verify the audio devices size.
1411 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1412 AudioDeviceList audio_devices;
1413 cras_audio_handler_->GetAudioDevices(&audio_devices);
1414 EXPECT_EQ(init_nodes_size, audio_devices.size());
1416 // Verify the internal mic is selected as the active output.
1417 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1418 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode());
1419 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
1420 EXPECT_TRUE(audio_devices[0].active);
1422 // Plug in usb mic, but fire NodesChanged signal twice.
1423 audio_nodes.clear();
1424 AudioNode internal_mic(kInternalMic);
1425 internal_mic.active = true;
1426 internal_mic.plugged_time = 1000;
1427 audio_nodes.push_back(internal_mic);
1428 AudioNode usb_mic(kUSBMic);
1429 usb_mic.active = false;
1430 usb_mic.plugged_time = 2000;
1431 audio_nodes.push_back(usb_mic);
1432 ChangeAudioNodes(audio_nodes);
1433 ChangeAudioNodes(audio_nodes);
1435 // Verify the active output device is set to headphone.
1436 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1437 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
1438 EXPECT_EQ(usb_mic.id, cras_audio_handler_->GetActiveInputNode());
1439 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1441 // Verfiy the audio devices data is consistent, i.e., the active input device
1442 // should be usb mic.
1443 cras_audio_handler_->GetAudioDevices(&audio_devices);
1444 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1445 for (size_t i = 0; i < audio_devices.size(); ++i) {
1446 if (audio_devices[i].id == kInternalMic.id)
1447 EXPECT_FALSE(audio_devices[i].active);
1448 else if (audio_devices[i].id == usb_mic.id)
1449 EXPECT_TRUE(audio_devices[i].active);
1450 else
1451 NOTREACHED();
1455 // This is the case of crbug.com/291303.
1456 TEST_F(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnSystemBoot) {
1457 // Set up audio handler with empty audio_nodes.
1458 AudioNodeList audio_nodes;
1459 SetUpCrasAudioHandler(audio_nodes);
1461 AudioNode internal_speaker(kInternalSpeaker);
1462 internal_speaker.active = false;
1463 AudioNode headphone(kHeadphone);
1464 headphone.active = false;
1465 AudioNode internal_mic(kInternalMic);
1466 internal_mic.active = false;
1467 audio_nodes.push_back(internal_speaker);
1468 audio_nodes.push_back(headphone);
1469 audio_nodes.push_back(internal_mic);
1470 const size_t init_nodes_size = audio_nodes.size();
1472 // Simulate AudioNodesChanged signal being fired twice during system boot.
1473 ChangeAudioNodes(audio_nodes);
1474 ChangeAudioNodes(audio_nodes);
1476 // Verify the active output device is set to headphone.
1477 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1478 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1479 EXPECT_EQ(headphone.id, cras_audio_handler_->GetActiveOutputNode());
1480 AudioDevice active_output;
1481 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
1482 EXPECT_EQ(headphone.id, active_output.id);
1484 // Verify the active input device id is set to internal mic.
1485 EXPECT_EQ(internal_mic.id, cras_audio_handler_->GetActiveInputNode());
1487 // Verfiy the audio devices data is consistent, i.e., the active output device
1488 // should be headphone, and the active input device should internal mic.
1489 AudioDeviceList audio_devices;
1490 cras_audio_handler_->GetAudioDevices(&audio_devices);
1491 EXPECT_EQ(init_nodes_size, audio_devices.size());
1492 for (size_t i = 0; i < audio_devices.size(); ++i) {
1493 if (audio_devices[i].id == internal_speaker.id)
1494 EXPECT_FALSE(audio_devices[i].active);
1495 else if (audio_devices[i].id == headphone.id)
1496 EXPECT_TRUE(audio_devices[i].active);
1497 else if (audio_devices[i].id == internal_mic.id)
1498 EXPECT_TRUE(audio_devices[i].active);
1499 else
1500 NOTREACHED();
1504 TEST_F(CrasAudioHandlerTest, SetOutputMute) {
1505 AudioNodeList audio_nodes;
1506 audio_nodes.push_back(kInternalSpeaker);
1507 SetUpCrasAudioHandler(audio_nodes);
1508 EXPECT_EQ(0, test_observer_->output_mute_changed_count());
1510 // Mute the device.
1511 cras_audio_handler_->SetOutputMute(true);
1513 // Verify the output is muted, OnOutputMuteChanged event is fired,
1514 // and mute value is saved in the preferences.
1515 EXPECT_TRUE(cras_audio_handler_->IsOutputMuted());
1516 EXPECT_EQ(1, test_observer_->output_mute_changed_count());
1517 AudioDevice speaker(kInternalSpeaker);
1518 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(speaker));
1520 // Unmute the device.
1521 cras_audio_handler_->SetOutputMute(false);
1523 // Verify the output is unmuted, OnOutputMuteChanged event is fired,
1524 // and mute value is saved in the preferences.
1525 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
1526 EXPECT_EQ(2, test_observer_->output_mute_changed_count());
1527 EXPECT_FALSE(audio_pref_handler_->GetMuteValue(speaker));
1530 TEST_F(CrasAudioHandlerTest, SetInputMute) {
1531 AudioNodeList audio_nodes;
1532 audio_nodes.push_back(kInternalMic);
1533 SetUpCrasAudioHandler(audio_nodes);
1534 EXPECT_EQ(0, test_observer_->input_mute_changed_count());
1536 // Mute the device.
1537 cras_audio_handler_->SetInputMute(true);
1539 // Verify the input is muted, OnInputMuteChanged event is fired.
1540 EXPECT_TRUE(cras_audio_handler_->IsInputMuted());
1541 EXPECT_EQ(1, test_observer_->input_mute_changed_count());
1543 // Unmute the device.
1544 cras_audio_handler_->SetInputMute(false);
1546 // Verify the input is unmuted, OnInputMuteChanged event is fired.
1547 EXPECT_FALSE(cras_audio_handler_->IsInputMuted());
1548 EXPECT_EQ(2, test_observer_->input_mute_changed_count());
1551 TEST_F(CrasAudioHandlerTest, SetOutputVolumePercent) {
1552 AudioNodeList audio_nodes;
1553 audio_nodes.push_back(kInternalSpeaker);
1554 SetUpCrasAudioHandler(audio_nodes);
1555 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
1557 cras_audio_handler_->SetOutputVolumePercent(60);
1559 // Verify the output volume is changed to the designated value,
1560 // OnOutputVolumeChanged event is fired, and the device volume value
1561 // is saved the preferences.
1562 const int kVolume = 60;
1563 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent());
1564 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
1565 AudioDevice device;
1566 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&device));
1567 EXPECT_EQ(device.id, kInternalSpeaker.id);
1568 EXPECT_EQ(kVolume, audio_pref_handler_->GetOutputVolumeValue(&device));
1571 TEST_F(CrasAudioHandlerTest, SetInputGainPercent) {
1572 AudioNodeList audio_nodes;
1573 audio_nodes.push_back(kInternalMic);
1574 SetUpCrasAudioHandler(audio_nodes);
1575 EXPECT_EQ(0, test_observer_->input_gain_changed_count());
1577 cras_audio_handler_->SetInputGainPercent(60);
1579 // Verify the input gain changed to the designated value,
1580 // OnInputGainChanged event is fired, and the device gain value
1581 // is saved in the preferences.
1582 const int kGain = 60;
1583 EXPECT_EQ(kGain, cras_audio_handler_->GetInputGainPercent());
1584 EXPECT_EQ(1, test_observer_->input_gain_changed_count());
1585 AudioDevice internal_mic(kInternalMic);
1586 EXPECT_EQ(kGain, audio_pref_handler_->GetInputGainValue(&internal_mic));
1589 TEST_F(CrasAudioHandlerTest, SetMuteForDevice) {
1590 AudioNodeList audio_nodes;
1591 audio_nodes.push_back(kInternalSpeaker);
1592 audio_nodes.push_back(kHeadphone);
1593 audio_nodes.push_back(kInternalMic);
1594 audio_nodes.push_back(kUSBMic);
1595 SetUpCrasAudioHandler(audio_nodes);
1597 // Mute the active output device.
1598 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
1599 cras_audio_handler_->SetMuteForDevice(kHeadphone.id, true);
1601 // Verify the headphone is muted and mute value is saved in the preferences.
1602 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kHeadphone.id));
1603 AudioDevice headphone(kHeadphone);
1604 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(headphone));
1606 // Mute the non-active output device.
1607 cras_audio_handler_->SetMuteForDevice(kInternalSpeaker.id, true);
1609 // Verify the internal speaker is muted and mute value is saved in the
1610 // preferences.
1611 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kInternalSpeaker.id));
1612 AudioDevice internal_speaker(kInternalSpeaker);
1613 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(internal_speaker));
1615 // Mute the active input device.
1616 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetActiveInputNode());
1617 cras_audio_handler_->SetMuteForDevice(kUSBMic.id, true);
1619 // Verify the USB Mic is muted.
1620 EXPECT_TRUE(cras_audio_handler_->IsInputMutedForDevice(kUSBMic.id));
1622 // Mute the non-active input device should be a no-op, see crbug.com/365050.
1623 cras_audio_handler_->SetMuteForDevice(kInternalMic.id, true);
1625 // Verify IsInputMutedForDevice returns false for non-active input device.
1626 EXPECT_FALSE(cras_audio_handler_->IsInputMutedForDevice(kInternalMic.id));
1629 TEST_F(CrasAudioHandlerTest, SetVolumeGainPercentForDevice) {
1630 AudioNodeList audio_nodes;
1631 audio_nodes.push_back(kInternalSpeaker);
1632 audio_nodes.push_back(kHeadphone);
1633 audio_nodes.push_back(kInternalMic);
1634 audio_nodes.push_back(kUSBMic);
1635 SetUpCrasAudioHandler(audio_nodes);
1637 // Set volume percent for active output device.
1638 const int kHeadphoneVolume = 30;
1639 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
1640 cras_audio_handler_->SetVolumeGainPercentForDevice(kHeadphone.id,
1641 kHeadphoneVolume);
1643 // Verify the volume percent of headphone is set, and saved in preferences.
1644 EXPECT_EQ(kHeadphoneVolume,
1645 cras_audio_handler_->GetOutputVolumePercentForDevice(
1646 kHeadphone.id));
1647 AudioDevice headphone(kHeadphone);
1648 EXPECT_EQ(kHeadphoneVolume,
1649 audio_pref_handler_->GetOutputVolumeValue(&headphone));
1651 // Set volume percent for non-active output device.
1652 const int kSpeakerVolume = 60;
1653 cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalSpeaker.id,
1654 kSpeakerVolume);
1656 // Verify the volume percent of speaker is set, and saved in preferences.
1657 EXPECT_EQ(kSpeakerVolume,
1658 cras_audio_handler_->GetOutputVolumePercentForDevice(
1659 kInternalSpeaker.id));
1660 AudioDevice speaker(kInternalSpeaker);
1661 EXPECT_EQ(kSpeakerVolume,
1662 audio_pref_handler_->GetOutputVolumeValue(&speaker));
1664 // Set gain percent for active input device.
1665 const int kUSBMicGain = 30;
1666 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetActiveInputNode());
1667 cras_audio_handler_->SetVolumeGainPercentForDevice(kUSBMic.id,
1668 kUSBMicGain);
1670 // Verify the gain percent of USB mic is set, and saved in preferences.
1671 EXPECT_EQ(kUSBMicGain,
1672 cras_audio_handler_->GetOutputVolumePercentForDevice(kUSBMic.id));
1673 AudioDevice usb_mic(kHeadphone);
1674 EXPECT_EQ(kUSBMicGain,
1675 audio_pref_handler_->GetInputGainValue(&usb_mic));
1677 // Set gain percent for non-active input device.
1678 const int kInternalMicGain = 60;
1679 cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalMic.id,
1680 kInternalMicGain);
1682 // Verify the gain percent of internal mic is set, and saved in preferences.
1683 EXPECT_EQ(kInternalMicGain,
1684 cras_audio_handler_->GetOutputVolumePercentForDevice(
1685 kInternalMic.id));
1686 AudioDevice internal_mic(kInternalMic);
1687 EXPECT_EQ(kInternalMicGain,
1688 audio_pref_handler_->GetInputGainValue(&internal_mic));
1691 TEST_F(CrasAudioHandlerTest, HandleOtherDeviceType) {
1692 const size_t kNumValidAudioDevices = 4;
1693 AudioNodeList audio_nodes;
1694 audio_nodes.push_back(kInternalSpeaker);
1695 audio_nodes.push_back(kOtherTypeOutput);
1696 audio_nodes.push_back(kInternalMic);
1697 audio_nodes.push_back(kOtherTypeInput);
1698 SetUpCrasAudioHandler(audio_nodes);
1700 // Verify the audio devices size.
1701 AudioDeviceList audio_devices;
1702 cras_audio_handler_->GetAudioDevices(&audio_devices);
1703 EXPECT_EQ(kNumValidAudioDevices, audio_devices.size());
1705 // Verify the internal speaker has been selected as the active output,
1706 // and the output device with some randown unknown type is handled gracefully.
1707 AudioDevice active_output;
1708 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
1709 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
1710 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
1711 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1713 // Ensure the internal microphone has been selected as the active input,
1714 // and the input device with some random unknown type is handled gracefully.
1715 AudioDevice active_input;
1716 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode());
1717 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1720 } // namespace chromeos