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