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