Upstream version 11.39.250.0
[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 kMicJackId = 10009;
28 const uint64 kKeyboardMicId = 10010;
29 const uint64 kOtherTypeOutputId = 90001;
30 const uint64 kOtherTypeInputId = 90002;
31 const uint64 kUSBJabraSpeakerOutputId1 = 90003;
32 const uint64 kUSBJabraSpeakerOutputId2 = 90004;
33 const uint64 kUSBJabraSpeakerInputId1 = 90005;
34 const uint64 kUSBJabraSpeakerInputId2 = 90006;
35 const uint64 kUSBCameraInputId = 90007;
36
37 const AudioNode kInternalSpeaker(
38     false,
39     kInternalSpeakerId,
40     "Fake Speaker",
41     "INTERNAL_SPEAKER",
42     "Speaker",
43     false,
44     0
45 );
46
47 const AudioNode kHeadphone(
48     false,
49     kHeadphoneId,
50     "Fake Headphone",
51     "HEADPHONE",
52     "Headphone",
53     false,
54     0
55 );
56
57 const AudioNode kInternalMic(
58     true,
59     kInternalMicId,
60     "Fake Mic",
61     "INTERNAL_MIC",
62     "Internal Mic",
63     false,
64     0
65 );
66
67 const AudioNode kMicJack(
68     true,
69     kMicJackId,
70     "Fake Mic Jack",
71     "MIC",
72     "Mic Jack",
73     false,
74     0
75 );
76
77 const AudioNode kUSBMic(
78     true,
79     kUSBMicId,
80     "Fake USB Mic",
81     "USB",
82     "USB Microphone",
83     false,
84     0
85 );
86
87 const AudioNode kKeyboardMic(
88     true,
89     kKeyboardMicId,
90     "Fake Keyboard Mic",
91     "KEYBOARD_MIC",
92     "Keyboard Mic",
93     false,
94     0
95 );
96
97 const AudioNode kOtherTypeOutput(
98     false,
99     kOtherTypeOutputId,
100     "Output Device",
101     "SOME_OTHER_TYPE",
102     "Other Type Output Device",
103     false,
104     0
105 );
106
107 const AudioNode kOtherTypeInput(
108     true,
109     kOtherTypeInputId,
110     "Input Device",
111     "SOME_OTHER_TYPE",
112     "Other Type Input Device",
113     false,
114     0
115 );
116
117 const AudioNode kBluetoothHeadset (
118     false,
119     kBluetoothHeadsetId,
120     "Bluetooth Headset",
121     "BLUETOOTH",
122     "Bluetooth Headset 1",
123     false,
124     0
125 );
126
127 const AudioNode kHDMIOutput (
128     false,
129     kHDMIOutputId,
130     "HDMI output",
131     "HDMI",
132     "HDMI output",
133     false,
134     0
135 );
136
137 const AudioNode kUSBHeadphone1 (
138     false,
139     kUSBHeadphoneId1,
140     "USB Headphone",
141     "USB",
142     "USB Headphone 1",
143     false,
144     0
145 );
146
147 const AudioNode kUSBHeadphone2 (
148     false,
149     kUSBHeadphoneId2,
150     "USB Headphone",
151     "USB",
152     "USB Headphone 1",
153     false,
154     0
155 );
156
157 const AudioNode kUSBJabraSpeakerOutput1(false,
158                                         kUSBJabraSpeakerOutputId1,
159                                         "Jabra Speaker 1",
160                                         "USB",
161                                         "Jabra Speaker 1",
162                                         false,
163                                         0);
164
165 const AudioNode kUSBJabraSpeakerOutput2(false,
166                                         kUSBJabraSpeakerOutputId2,
167                                         "Jabra Speaker 2",
168                                         "USB",
169                                         "Jabra Speaker 2",
170                                         false,
171                                         0);
172
173 const AudioNode kUSBJabraSpeakerInput1(true,
174                                        kUSBJabraSpeakerInputId1,
175                                        "Jabra Speaker 1",
176                                        "USB",
177                                        "Jabra Speaker",
178                                        false,
179                                        0);
180
181 const AudioNode kUSBJabraSpeakerInput2(true,
182                                        kUSBJabraSpeakerInputId2,
183                                        "Jabra Speaker 2",
184                                        "USB",
185                                        "Jabra Speaker 2",
186                                        false,
187                                        0);
188
189 const AudioNode kUSBCameraInput(true,
190                                 kUSBCameraInputId,
191                                 "USB Camera",
192                                 "USB",
193                                 "USB Camera",
194                                 false,
195                                 0);
196
197 class TestObserver : public chromeos::CrasAudioHandler::AudioObserver {
198  public:
199   TestObserver() : active_output_node_changed_count_(0),
200                    active_input_node_changed_count_(0),
201                    audio_nodes_changed_count_(0),
202                    output_mute_changed_count_(0),
203                    input_mute_changed_count_(0),
204                    output_volume_changed_count_(0),
205                    input_gain_changed_count_(0) {
206   }
207
208   int active_output_node_changed_count() const {
209     return active_output_node_changed_count_;
210   }
211
212   void reset_active_output_node_changed_count() {
213     active_output_node_changed_count_ = 0;
214   }
215
216   int active_input_node_changed_count() const {
217     return active_input_node_changed_count_;
218   }
219
220   void reset_active_input_node_changed_count() {
221     active_output_node_changed_count_ = 0;
222   }
223
224   int audio_nodes_changed_count() const {
225     return audio_nodes_changed_count_;
226   }
227
228   int output_mute_changed_count() const {
229     return output_mute_changed_count_;
230   }
231
232   int input_mute_changed_count() const {
233     return input_mute_changed_count_;
234   }
235
236   int output_volume_changed_count() const {
237     return output_volume_changed_count_;
238   }
239
240   int input_gain_changed_count() const {
241     return input_gain_changed_count_;
242   }
243
244   virtual ~TestObserver() {}
245
246  protected:
247   // chromeos::CrasAudioHandler::AudioObserver overrides.
248   virtual void OnActiveOutputNodeChanged() OVERRIDE {
249     ++active_output_node_changed_count_;
250   }
251
252   virtual void OnActiveInputNodeChanged() OVERRIDE {
253     ++active_input_node_changed_count_;
254   }
255
256   virtual void OnAudioNodesChanged() OVERRIDE {
257     ++audio_nodes_changed_count_;
258   }
259
260   virtual void OnOutputMuteChanged() OVERRIDE {
261     ++output_mute_changed_count_;
262   }
263
264   virtual void OnInputMuteChanged() OVERRIDE {
265     ++input_mute_changed_count_;
266   }
267
268   virtual void OnOutputVolumeChanged() OVERRIDE {
269     ++output_volume_changed_count_;
270   }
271
272   virtual void OnInputGainChanged() OVERRIDE {
273     ++input_gain_changed_count_;
274   }
275
276  private:
277   int active_output_node_changed_count_;
278   int active_input_node_changed_count_;
279   int audio_nodes_changed_count_;
280   int output_mute_changed_count_;
281   int input_mute_changed_count_;
282   int output_volume_changed_count_;
283   int input_gain_changed_count_;
284
285   DISALLOW_COPY_AND_ASSIGN(TestObserver);
286 };
287
288 class CrasAudioHandlerTest : public testing::Test {
289  public:
290   CrasAudioHandlerTest() : cras_audio_handler_(NULL),
291                            cras_audio_client_stub_(NULL) {
292   }
293   virtual ~CrasAudioHandlerTest() {}
294
295   virtual void SetUp() OVERRIDE {
296   }
297
298   virtual void TearDown() OVERRIDE {
299     cras_audio_handler_->RemoveAudioObserver(test_observer_.get());
300     test_observer_.reset();
301     CrasAudioHandler::Shutdown();
302     audio_pref_handler_ = NULL;
303     DBusThreadManager::Shutdown();
304   }
305
306   void SetUpCrasAudioHandler(const AudioNodeList& audio_nodes) {
307     DBusThreadManager::Initialize();
308     cras_audio_client_stub_ = static_cast<CrasAudioClientStubImpl*>(
309         DBusThreadManager::Get()->GetCrasAudioClient());
310     cras_audio_client_stub_->SetAudioNodesForTesting(audio_nodes);
311     audio_pref_handler_ = new AudioDevicesPrefHandlerStub();
312     CrasAudioHandler::Initialize(audio_pref_handler_);
313     cras_audio_handler_ = CrasAudioHandler::Get();
314     test_observer_.reset(new TestObserver);
315     cras_audio_handler_->AddAudioObserver(test_observer_.get());
316     message_loop_.RunUntilIdle();
317   }
318
319   void SetUpCrasAudioHandlerWithPrimaryActiveNode(
320       const AudioNodeList& audio_nodes,
321       const AudioNode& primary_active_node) {
322     DBusThreadManager::Initialize();
323     cras_audio_client_stub_ = static_cast<CrasAudioClientStubImpl*>(
324         DBusThreadManager::Get()->GetCrasAudioClient());
325     cras_audio_client_stub_->SetAudioNodesForTesting(audio_nodes);
326     cras_audio_client_stub_->SetActiveOutputNode(primary_active_node.id),
327         audio_pref_handler_ = new AudioDevicesPrefHandlerStub();
328     CrasAudioHandler::Initialize(audio_pref_handler_);
329     cras_audio_handler_ = CrasAudioHandler::Get();
330     test_observer_.reset(new TestObserver);
331     cras_audio_handler_->AddAudioObserver(test_observer_.get());
332     message_loop_.RunUntilIdle();
333   }
334
335   void ChangeAudioNodes(const AudioNodeList& audio_nodes) {
336     cras_audio_client_stub_->SetAudioNodesAndNotifyObserversForTesting(
337         audio_nodes);
338     message_loop_.RunUntilIdle();
339   }
340
341   const AudioDevice* GetDeviceFromId(uint64 id) {
342     return cras_audio_handler_->GetDeviceFromId(id);
343   }
344
345   int GetActiveDeviceCount() const {
346     int num_active_nodes = 0;
347     AudioDeviceList audio_devices;
348     cras_audio_handler_->GetAudioDevices(&audio_devices);
349     for (size_t i = 0; i < audio_devices.size(); ++i) {
350       if (audio_devices[i].active)
351         ++num_active_nodes;
352     }
353     return num_active_nodes;
354   }
355
356  protected:
357   base::MessageLoopForUI message_loop_;
358   CrasAudioHandler* cras_audio_handler_;  // Not owned.
359   CrasAudioClientStubImpl* cras_audio_client_stub_;  // Not owned.
360   scoped_ptr<TestObserver> test_observer_;
361   scoped_refptr<AudioDevicesPrefHandlerStub> audio_pref_handler_;
362
363  private:
364   DISALLOW_COPY_AND_ASSIGN(CrasAudioHandlerTest);
365 };
366
367 TEST_F(CrasAudioHandlerTest, InitializeWithOnlyDefaultAudioDevices) {
368   AudioNodeList audio_nodes;
369   audio_nodes.push_back(kInternalSpeaker);
370   audio_nodes.push_back(kInternalMic);
371   SetUpCrasAudioHandler(audio_nodes);
372
373   // Verify the audio devices size.
374   AudioDeviceList audio_devices;
375   cras_audio_handler_->GetAudioDevices(&audio_devices);
376   EXPECT_EQ(audio_nodes.size(), audio_devices.size());
377
378   // Verify the internal speaker has been selected as the active output.
379   AudioDevice active_output;
380   EXPECT_TRUE(
381       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
382   EXPECT_EQ(kInternalSpeaker.id, active_output.id);
383   EXPECT_EQ(kInternalSpeaker.id,
384             cras_audio_handler_->GetPrimaryActiveOutputNode());
385   EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
386
387   // Ensure the internal microphone has been selected as the active input.
388   AudioDevice active_input;
389   EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
390   EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
391 }
392
393 TEST_F(CrasAudioHandlerTest, InitializeWithAlternativeAudioDevices) {
394   AudioNodeList audio_nodes;
395   audio_nodes.push_back(kInternalSpeaker);
396   audio_nodes.push_back(kHeadphone);
397   audio_nodes.push_back(kInternalMic);
398   audio_nodes.push_back(kUSBMic);
399   SetUpCrasAudioHandler(audio_nodes);
400
401   // Verify the audio devices size.
402   AudioDeviceList audio_devices;
403   cras_audio_handler_->GetAudioDevices(&audio_devices);
404   EXPECT_EQ(audio_nodes.size(), audio_devices.size());
405
406   // Verify the headphone has been selected as the active output.
407   AudioDevice active_output;
408   EXPECT_TRUE(
409       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
410   EXPECT_EQ(kHeadphone.id, active_output.id);
411   EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
412   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
413
414   // Ensure the USB microphone has been selected as the active input.
415   AudioDevice active_input;
416   EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
417   EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
418 }
419
420 TEST_F(CrasAudioHandlerTest, InitializeWithKeyboardMic) {
421   AudioNodeList audio_nodes;
422   audio_nodes.push_back(kInternalSpeaker);
423   audio_nodes.push_back(kInternalMic);
424   audio_nodes.push_back(kKeyboardMic);
425   SetUpCrasAudioHandler(audio_nodes);
426
427   // Verify the audio devices size.
428   AudioDeviceList audio_devices;
429   cras_audio_handler_->GetAudioDevices(&audio_devices);
430   EXPECT_EQ(audio_nodes.size(), audio_devices.size());
431   EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic());
432
433   // Verify the internal speaker has been selected as the active output.
434   AudioDevice active_output;
435   EXPECT_TRUE(
436       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
437   EXPECT_EQ(kInternalSpeaker.id, active_output.id);
438   EXPECT_EQ(kInternalSpeaker.id,
439             cras_audio_handler_->GetPrimaryActiveOutputNode());
440   EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
441
442   // Ensure the internal microphone has been selected as the active input,
443   // not affected by keyboard mic.
444   AudioDevice active_input;
445   EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
446   EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
447   const AudioDevice* keyboard_mic = GetDeviceFromId(kKeyboardMic.id);
448   EXPECT_FALSE(keyboard_mic->active);
449 }
450
451 TEST_F(CrasAudioHandlerTest, SetKeyboardMicActive) {
452   AudioNodeList audio_nodes;
453   audio_nodes.push_back(kInternalMic);
454   audio_nodes.push_back(kKeyboardMic);
455   SetUpCrasAudioHandler(audio_nodes);
456
457   // Verify the audio devices size.
458   AudioDeviceList audio_devices;
459   cras_audio_handler_->GetAudioDevices(&audio_devices);
460   EXPECT_EQ(audio_nodes.size(), audio_devices.size());
461   EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic());
462
463   // Ensure the internal microphone has been selected as the active input,
464   // not affected by keyboard mic.
465   AudioDevice active_input;
466   EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
467   EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
468   const AudioDevice* keyboard_mic = GetDeviceFromId(kKeyboardMic.id);
469   EXPECT_FALSE(keyboard_mic->active);
470
471   // Make keyboard mic active.
472   cras_audio_handler_->SetKeyboardMicActive(true);
473   EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
474   const AudioDevice* active_keyboard_mic = GetDeviceFromId(kKeyboardMic.id);
475   EXPECT_TRUE(active_keyboard_mic->active);
476
477   // Make keyboard mic inactive.
478   cras_audio_handler_->SetKeyboardMicActive(false);
479   EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
480   const AudioDevice* inactive_keyboard_mic = GetDeviceFromId(kKeyboardMic.id);
481   EXPECT_FALSE(inactive_keyboard_mic->active);
482 }
483
484 TEST_F(CrasAudioHandlerTest, SwitchActiveOutputDevice) {
485   AudioNodeList audio_nodes;
486   audio_nodes.push_back(kInternalSpeaker);
487   audio_nodes.push_back(kHeadphone);
488   SetUpCrasAudioHandler(audio_nodes);
489   AudioDeviceList audio_devices;
490   cras_audio_handler_->GetAudioDevices(&audio_devices);
491   EXPECT_EQ(audio_nodes.size(), audio_devices.size());
492
493   // Verify the initial active output device is headphone.
494   EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
495   AudioDevice active_output;
496   EXPECT_TRUE(
497       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
498   EXPECT_EQ(kHeadphone.id, active_output.id);
499   EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
500
501   // Switch the active output to internal speaker.
502   AudioDevice internal_speaker(kInternalSpeaker);
503   cras_audio_handler_->SwitchToDevice(internal_speaker, true);
504
505   // Verify the active output is switched to internal speaker, and the
506   // ActiveOutputNodeChanged event is fired.
507   EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
508   EXPECT_TRUE(
509       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
510   EXPECT_EQ(kInternalSpeaker.id, active_output.id);
511   EXPECT_EQ(kInternalSpeaker.id,
512             cras_audio_handler_->GetPrimaryActiveOutputNode());
513 }
514
515 TEST_F(CrasAudioHandlerTest, SwitchActiveInputDevice) {
516   AudioNodeList audio_nodes;
517   audio_nodes.push_back(kInternalMic);
518   audio_nodes.push_back(kUSBMic);
519   SetUpCrasAudioHandler(audio_nodes);
520   AudioDeviceList audio_devices;
521   cras_audio_handler_->GetAudioDevices(&audio_devices);
522   EXPECT_EQ(audio_nodes.size(), audio_devices.size());
523
524   // Verify the initial active input device is USB mic.
525   EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
526   EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
527
528   // Switch the active input to internal mic.
529   AudioDevice internal_mic(kInternalMic);
530   cras_audio_handler_->SwitchToDevice(internal_mic, true);
531
532   // Verify the active output is switched to internal speaker, and the active
533   // ActiveInputNodeChanged event is fired.
534   EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
535   EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
536 }
537
538 TEST_F(CrasAudioHandlerTest, PlugHeadphone) {
539   // Set up initial audio devices, only with internal speaker.
540   AudioNodeList audio_nodes;
541   audio_nodes.push_back(kInternalSpeaker);
542   SetUpCrasAudioHandler(audio_nodes);
543   const size_t init_nodes_size = audio_nodes.size();
544
545   // Verify the audio devices size.
546   AudioDeviceList audio_devices;
547   cras_audio_handler_->GetAudioDevices(&audio_devices);
548   EXPECT_EQ(init_nodes_size, audio_devices.size());
549   EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
550
551   // Verify the internal speaker has been selected as the active output.
552   EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
553   AudioDevice active_output;
554   EXPECT_TRUE(
555       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
556   EXPECT_EQ(kInternalSpeaker.id, active_output.id);
557   EXPECT_EQ(kInternalSpeaker.id,
558             cras_audio_handler_->GetPrimaryActiveOutputNode());
559   EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
560
561   // Plug the headphone.
562   audio_nodes.clear();
563   AudioNode internal_speaker(kInternalSpeaker);
564   internal_speaker.active = true;
565   audio_nodes.push_back(internal_speaker);
566   audio_nodes.push_back(kHeadphone);
567   ChangeAudioNodes(audio_nodes);
568
569   // Verify the AudioNodesChanged event is fired and new audio device is added.
570   EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
571   cras_audio_handler_->GetAudioDevices(&audio_devices);
572   EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
573
574   // Verify the active output device is switched to headphone and
575   // ActiveOutputChanged event is fired.
576   EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
577   EXPECT_TRUE(
578       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
579   EXPECT_EQ(kHeadphone.id, active_output.id);
580   EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
581   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
582 }
583
584 TEST_F(CrasAudioHandlerTest, UnplugHeadphone) {
585   // Set up initial audio devices, with internal speaker and headphone.
586   AudioNodeList audio_nodes;
587   audio_nodes.push_back(kInternalSpeaker);
588   audio_nodes.push_back(kHeadphone);
589   SetUpCrasAudioHandler(audio_nodes);
590   const size_t init_nodes_size = audio_nodes.size();
591
592   // Verify the audio devices size.
593   AudioDeviceList audio_devices;
594   cras_audio_handler_->GetAudioDevices(&audio_devices);
595   EXPECT_EQ(init_nodes_size, audio_devices.size());
596   EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
597
598   // Verify the headphone has been selected as the active output.
599   EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
600   AudioDevice active_output;
601   EXPECT_TRUE(
602       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
603   EXPECT_EQ(kHeadphone.id, active_output.id);
604   EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
605   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
606
607   // Unplug the headphone.
608   audio_nodes.clear();
609   audio_nodes.push_back(kInternalSpeaker);
610   ChangeAudioNodes(audio_nodes);
611
612   // Verify the AudioNodesChanged event is fired and one audio device is
613   // removed.
614   EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
615   cras_audio_handler_->GetAudioDevices(&audio_devices);
616   EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
617
618   // Verify the active output device is switched to internal speaker and
619   // ActiveOutputChanged event is fired.
620   EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
621   EXPECT_TRUE(
622       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
623   EXPECT_EQ(kInternalSpeaker.id, active_output.id);
624   EXPECT_EQ(kInternalSpeaker.id,
625             cras_audio_handler_->GetPrimaryActiveOutputNode());
626   EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
627 }
628
629 TEST_F(CrasAudioHandlerTest, InitializeWithBluetoothHeadset) {
630   AudioNodeList audio_nodes;
631   audio_nodes.push_back(kInternalSpeaker);
632   audio_nodes.push_back(kBluetoothHeadset);
633   SetUpCrasAudioHandler(audio_nodes);
634
635   // Verify the audio devices size.
636   AudioDeviceList audio_devices;
637   cras_audio_handler_->GetAudioDevices(&audio_devices);
638   EXPECT_EQ(audio_nodes.size(), audio_devices.size());
639   EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
640
641   // Verify the bluetooth headset has been selected as the active output.
642   EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
643   AudioDevice active_output;
644   EXPECT_TRUE(
645       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
646   EXPECT_EQ(kBluetoothHeadset.id, active_output.id);
647   EXPECT_EQ(kBluetoothHeadset.id,
648             cras_audio_handler_->GetPrimaryActiveOutputNode());
649   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
650 }
651
652 TEST_F(CrasAudioHandlerTest, ConnectAndDisconnectBluetoothHeadset) {
653   // Initialize with internal speaker and headphone.
654   AudioNodeList audio_nodes;
655   audio_nodes.push_back(kInternalSpeaker);
656   audio_nodes.push_back(kHeadphone);
657   SetUpCrasAudioHandler(audio_nodes);
658   const size_t init_nodes_size = audio_nodes.size();
659
660   // Verify the audio devices size.
661   AudioDeviceList audio_devices;
662   cras_audio_handler_->GetAudioDevices(&audio_devices);
663   EXPECT_EQ(init_nodes_size, audio_devices.size());
664   EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
665
666   // Verify the headphone is selected as the active output initially.
667   EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
668   AudioDevice active_output;
669   EXPECT_TRUE(
670       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
671   EXPECT_EQ(kHeadphone.id, active_output.id);
672   EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
673   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
674
675   // Connect to bluetooth headset. Since it is plugged in later than
676   // headphone, active output should be switched to it.
677   audio_nodes.clear();
678   audio_nodes.push_back(kInternalSpeaker);
679   AudioNode headphone(kHeadphone);
680   headphone.plugged_time = 80000000;
681   headphone.active = true;
682   audio_nodes.push_back(headphone);
683   AudioNode bluetooth_headset(kBluetoothHeadset);
684   bluetooth_headset.plugged_time = 90000000;
685   audio_nodes.push_back(bluetooth_headset);
686   ChangeAudioNodes(audio_nodes);
687
688   // Verify the AudioNodesChanged event is fired and new audio device is added.
689   EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
690   cras_audio_handler_->GetAudioDevices(&audio_devices);
691   EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
692
693   // Verify the active output device is switched to bluetooth headset, and
694   // ActiveOutputChanged event is fired.
695   EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
696   EXPECT_TRUE(
697       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
698   EXPECT_EQ(kBluetoothHeadset.id, active_output.id);
699   EXPECT_EQ(kBluetoothHeadset.id,
700             cras_audio_handler_->GetPrimaryActiveOutputNode());
701   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
702
703   // Disconnect bluetooth headset.
704   audio_nodes.clear();
705   audio_nodes.push_back(kInternalSpeaker);
706   headphone.active = false;
707   audio_nodes.push_back(headphone);
708   ChangeAudioNodes(audio_nodes);
709
710   // Verify the AudioNodesChanged event is fired and one audio device is
711   // removed.
712   EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
713   cras_audio_handler_->GetAudioDevices(&audio_devices);
714   EXPECT_EQ(init_nodes_size, audio_devices.size());
715
716   // Verify the active output device is switched to headphone, and
717   // ActiveOutputChanged event is fired.
718   EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
719   EXPECT_TRUE(
720       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
721   EXPECT_EQ(kHeadphone.id, active_output.id);
722   EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
723   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
724 }
725
726 TEST_F(CrasAudioHandlerTest, InitializeWithHDMIOutput) {
727   AudioNodeList audio_nodes;
728   audio_nodes.push_back(kInternalSpeaker);
729   audio_nodes.push_back(kHDMIOutput);
730   SetUpCrasAudioHandler(audio_nodes);
731
732   // Verify the audio devices size.
733   AudioDeviceList audio_devices;
734   cras_audio_handler_->GetAudioDevices(&audio_devices);
735   EXPECT_EQ(audio_nodes.size(), audio_devices.size());
736   EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
737
738   // Verify the HDMI device has been selected as the active output.
739   EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
740   AudioDevice active_output;
741   EXPECT_TRUE(
742       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
743   EXPECT_EQ(kHDMIOutput.id, active_output.id);
744   EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
745   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
746 }
747
748 TEST_F(CrasAudioHandlerTest, ConnectAndDisconnectHDMIOutput) {
749   // Initialize with internal speaker.
750   AudioNodeList audio_nodes;
751   audio_nodes.push_back(kInternalSpeaker);
752   SetUpCrasAudioHandler(audio_nodes);
753   const size_t init_nodes_size = audio_nodes.size();
754
755   // Verify the audio devices size.
756   AudioDeviceList audio_devices;
757   cras_audio_handler_->GetAudioDevices(&audio_devices);
758   EXPECT_EQ(init_nodes_size, audio_devices.size());
759   EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
760
761   // Verify the internal speaker is selected as the active output initially.
762   EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
763   AudioDevice active_output;
764   EXPECT_TRUE(
765       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
766   EXPECT_EQ(kInternalSpeaker.id, active_output.id);
767   EXPECT_EQ(kInternalSpeaker.id,
768             cras_audio_handler_->GetPrimaryActiveOutputNode());
769   EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
770
771   // Connect to HDMI output.
772   audio_nodes.clear();
773   AudioNode internal_speaker(kInternalSpeaker);
774   internal_speaker.active = true;
775   internal_speaker.plugged_time = 80000000;
776   audio_nodes.push_back(internal_speaker);
777   AudioNode hdmi(kHDMIOutput);
778   hdmi.plugged_time = 90000000;
779   audio_nodes.push_back(hdmi);
780   ChangeAudioNodes(audio_nodes);
781
782   // Verify the AudioNodesChanged event is fired and new audio device is added.
783   EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
784   cras_audio_handler_->GetAudioDevices(&audio_devices);
785   EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
786
787   // Verify the active output device is switched to hdmi output, and
788   // ActiveOutputChanged event is fired.
789   EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
790   EXPECT_TRUE(
791       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
792   EXPECT_EQ(kHDMIOutput.id, active_output.id);
793   EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
794   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
795
796   // Disconnect hdmi headset.
797   audio_nodes.clear();
798   audio_nodes.push_back(kInternalSpeaker);
799   ChangeAudioNodes(audio_nodes);
800
801   // Verify the AudioNodesChanged event is fired and one audio device is
802   // removed.
803   EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
804   cras_audio_handler_->GetAudioDevices(&audio_devices);
805   EXPECT_EQ(init_nodes_size, audio_devices.size());
806
807   // Verify the active output device is switched to internal speaker, and
808   // ActiveOutputChanged event is fired.
809   EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
810   EXPECT_TRUE(
811       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
812   EXPECT_EQ(kInternalSpeaker.id, active_output.id);
813   EXPECT_EQ(kInternalSpeaker.id,
814             cras_audio_handler_->GetPrimaryActiveOutputNode());
815   EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
816 }
817
818 TEST_F(CrasAudioHandlerTest, HandleHeadphoneAndHDMIOutput) {
819   // Initialize with internal speaker, headphone and HDMI output.
820   AudioNodeList audio_nodes;
821   audio_nodes.push_back(kInternalSpeaker);
822   audio_nodes.push_back(kHeadphone);
823   audio_nodes.push_back(kHDMIOutput);
824   SetUpCrasAudioHandler(audio_nodes);
825   const size_t init_nodes_size = audio_nodes.size();
826
827   // Verify the audio devices size.
828   AudioDeviceList audio_devices;
829   cras_audio_handler_->GetAudioDevices(&audio_devices);
830   EXPECT_EQ(init_nodes_size, audio_devices.size());
831   EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
832
833   // Verify the headphone is selected as the active output initially.
834   EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
835   AudioDevice active_output;
836   EXPECT_TRUE(
837       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
838   EXPECT_EQ(kHeadphone.id, active_output.id);
839   EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
840   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
841
842   // Disconnect HDMI output.
843   audio_nodes.clear();
844   audio_nodes.push_back(kInternalSpeaker);
845   audio_nodes.push_back(kHDMIOutput);
846   ChangeAudioNodes(audio_nodes);
847
848   // Verify the AudioNodesChanged event is fired and one audio device is
849   // removed.
850   EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
851   cras_audio_handler_->GetAudioDevices(&audio_devices);
852   EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
853
854   // Verify the active output device is switched to HDMI output, and
855   // ActiveOutputChanged event is fired.
856   EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
857   EXPECT_TRUE(
858       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
859   EXPECT_EQ(kHDMIOutput.id, active_output.id);
860   EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
861   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
862 }
863
864 TEST_F(CrasAudioHandlerTest, InitializeWithUSBHeadphone) {
865   AudioNodeList audio_nodes;
866   audio_nodes.push_back(kInternalSpeaker);
867   audio_nodes.push_back(kUSBHeadphone1);
868   SetUpCrasAudioHandler(audio_nodes);
869
870   // Verify the audio devices size.
871   AudioDeviceList audio_devices;
872   cras_audio_handler_->GetAudioDevices(&audio_devices);
873   EXPECT_EQ(audio_nodes.size(), audio_devices.size());
874   EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
875
876   // Verify the usb headphone has been selected as the active output.
877   EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
878   AudioDevice active_output;
879   EXPECT_TRUE(
880       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
881   EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
882   EXPECT_EQ(kUSBHeadphone1.id,
883             cras_audio_handler_->GetPrimaryActiveOutputNode());
884   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
885 }
886
887 TEST_F(CrasAudioHandlerTest, PlugAndUnplugUSBHeadphone) {
888   // Initialize with internal speaker.
889   AudioNodeList audio_nodes;
890   audio_nodes.push_back(kInternalSpeaker);
891   SetUpCrasAudioHandler(audio_nodes);
892   const size_t init_nodes_size = audio_nodes.size();
893
894   // Verify the audio devices size.
895   AudioDeviceList audio_devices;
896   cras_audio_handler_->GetAudioDevices(&audio_devices);
897   EXPECT_EQ(init_nodes_size, audio_devices.size());
898   EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
899
900   // Verify the internal speaker is selected as the active output initially.
901   EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
902   AudioDevice active_output;
903   EXPECT_TRUE(
904       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
905   EXPECT_EQ(kInternalSpeaker.id, active_output.id);
906   EXPECT_EQ(kInternalSpeaker.id,
907             cras_audio_handler_->GetPrimaryActiveOutputNode());
908   EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
909
910   // Plug in usb headphone
911   audio_nodes.clear();
912   AudioNode internal_speaker(kInternalSpeaker);
913   internal_speaker.active = true;
914   internal_speaker.plugged_time = 80000000;
915   audio_nodes.push_back(internal_speaker);
916   AudioNode usb_headphone(kUSBHeadphone1);
917   usb_headphone.plugged_time = 90000000;
918   audio_nodes.push_back(usb_headphone);
919   ChangeAudioNodes(audio_nodes);
920
921   // Verify the AudioNodesChanged event is fired and new audio device is added.
922   EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
923   cras_audio_handler_->GetAudioDevices(&audio_devices);
924   EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
925
926   // Verify the active output device is switched to usb headphone, and
927   // ActiveOutputChanged event is fired.
928   EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
929   EXPECT_TRUE(
930       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
931   EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
932   EXPECT_EQ(kUSBHeadphone1.id,
933             cras_audio_handler_->GetPrimaryActiveOutputNode());
934   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
935
936   // Unplug usb headphone.
937   audio_nodes.clear();
938   audio_nodes.push_back(kInternalSpeaker);
939   ChangeAudioNodes(audio_nodes);
940
941   // Verify the AudioNodesChanged event is fired and one audio device is
942   // removed.
943   EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
944   cras_audio_handler_->GetAudioDevices(&audio_devices);
945   EXPECT_EQ(init_nodes_size, audio_devices.size());
946
947   // Verify the active output device is switched to internal speaker, and
948   // ActiveOutputChanged event is fired.
949   EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
950   EXPECT_TRUE(
951       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
952   EXPECT_EQ(kInternalSpeaker.id, active_output.id);
953   EXPECT_EQ(kInternalSpeaker.id,
954             cras_audio_handler_->GetPrimaryActiveOutputNode());
955   EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
956 }
957
958 TEST_F(CrasAudioHandlerTest, HandleMultipleUSBHeadphones) {
959   // Initialize with internal speaker and one usb headphone.
960   AudioNodeList audio_nodes;
961   audio_nodes.push_back(kInternalSpeaker);
962   audio_nodes.push_back(kUSBHeadphone1);
963   SetUpCrasAudioHandler(audio_nodes);
964   const size_t init_nodes_size = audio_nodes.size();
965
966   // Verify the audio devices size.
967   AudioDeviceList audio_devices;
968   cras_audio_handler_->GetAudioDevices(&audio_devices);
969   EXPECT_EQ(init_nodes_size, audio_devices.size());
970   EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
971
972   // Verify the usb headphone is selected as the active output initially.
973   EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
974   AudioDevice active_output;
975   EXPECT_TRUE(
976       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
977   EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
978   EXPECT_EQ(kUSBHeadphone1.id,
979             cras_audio_handler_->GetPrimaryActiveOutputNode());
980   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
981
982   // Plug in another usb headphone.
983   audio_nodes.clear();
984   audio_nodes.push_back(kInternalSpeaker);
985   AudioNode usb_headphone_1(kUSBHeadphone1);
986   usb_headphone_1.active = true;
987   usb_headphone_1.plugged_time = 80000000;
988   audio_nodes.push_back(usb_headphone_1);
989   AudioNode usb_headphone_2(kUSBHeadphone2);
990   usb_headphone_2.plugged_time = 90000000;
991   audio_nodes.push_back(usb_headphone_2);
992   ChangeAudioNodes(audio_nodes);
993
994   // Verify the AudioNodesChanged event is fired and new audio device is added.
995   EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
996   cras_audio_handler_->GetAudioDevices(&audio_devices);
997   EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
998
999   // Verify the active output device is switched to the 2nd usb headphone, which
1000   // is plugged later, and ActiveOutputChanged event is fired.
1001   EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1002   EXPECT_TRUE(
1003       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1004   EXPECT_EQ(kUSBHeadphone2.id, active_output.id);
1005   EXPECT_EQ(kUSBHeadphone2.id,
1006             cras_audio_handler_->GetPrimaryActiveOutputNode());
1007   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1008
1009   // Unplug the 2nd usb headphone.
1010   audio_nodes.clear();
1011   audio_nodes.push_back(kInternalSpeaker);
1012   audio_nodes.push_back(kUSBHeadphone1);
1013   ChangeAudioNodes(audio_nodes);
1014
1015   // Verify the AudioNodesChanged event is fired and one audio device is
1016   // removed.
1017   EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1018   cras_audio_handler_->GetAudioDevices(&audio_devices);
1019   EXPECT_EQ(init_nodes_size, audio_devices.size());
1020
1021   // Verify the active output device is switched to the first usb headphone, and
1022   // ActiveOutputChanged event is fired.
1023   EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
1024   EXPECT_TRUE(
1025       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1026   EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
1027   EXPECT_EQ(kUSBHeadphone1.id,
1028             cras_audio_handler_->GetPrimaryActiveOutputNode());
1029   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1030 }
1031
1032 TEST_F(CrasAudioHandlerTest, UnplugUSBHeadphonesWithActiveSpeaker) {
1033   // Initialize with internal speaker and one usb headphone.
1034   AudioNodeList audio_nodes;
1035   audio_nodes.push_back(kInternalSpeaker);
1036   audio_nodes.push_back(kUSBHeadphone1);
1037   SetUpCrasAudioHandler(audio_nodes);
1038   const size_t init_nodes_size = audio_nodes.size();
1039
1040   // Verify the audio devices size.
1041   AudioDeviceList audio_devices;
1042   cras_audio_handler_->GetAudioDevices(&audio_devices);
1043   EXPECT_EQ(init_nodes_size, audio_devices.size());
1044   EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1045
1046   // Verify the usb headphone is selected as the active output initially.
1047   EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1048   AudioDevice active_output;
1049   EXPECT_TRUE(
1050       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1051   EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
1052   EXPECT_EQ(kUSBHeadphone1.id,
1053             cras_audio_handler_->GetPrimaryActiveOutputNode());
1054   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1055
1056   // Plug in the headphone jack.
1057   audio_nodes.clear();
1058   audio_nodes.push_back(kInternalSpeaker);
1059   AudioNode usb_headphone_1(kUSBHeadphone1);
1060   usb_headphone_1.active = true;
1061   usb_headphone_1.plugged_time = 80000000;
1062   audio_nodes.push_back(usb_headphone_1);
1063   AudioNode headphone_jack(kHeadphone);
1064   headphone_jack.plugged_time = 90000000;
1065   audio_nodes.push_back(headphone_jack);
1066   ChangeAudioNodes(audio_nodes);
1067
1068   // Verify the AudioNodesChanged event is fired and new audio device is added.
1069   EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1070   cras_audio_handler_->GetAudioDevices(&audio_devices);
1071   EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1072
1073   // Verify the active output device is switched to the headphone jack, which
1074   // is plugged later, and ActiveOutputChanged event is fired.
1075   EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1076   EXPECT_TRUE(
1077       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1078   EXPECT_EQ(kHeadphone.id, active_output.id);
1079   EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1080   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1081
1082   // Select the speaker to be the active output device.
1083   AudioDevice internal_speaker(kInternalSpeaker);
1084   cras_audio_handler_->SwitchToDevice(internal_speaker, true);
1085
1086   // Verify the active output is switched to internal speaker, and the
1087   // ActiveOutputNodeChanged event is fired.
1088   EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
1089   EXPECT_TRUE(
1090       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1091   EXPECT_EQ(kInternalSpeaker.id, active_output.id);
1092   EXPECT_EQ(kInternalSpeaker.id,
1093             cras_audio_handler_->GetPrimaryActiveOutputNode());
1094
1095   // Unplug the usb headphone.
1096   audio_nodes.clear();
1097   AudioNode internal_speaker_node(kInternalSpeaker);
1098   internal_speaker_node.active = true;
1099   internal_speaker_node.plugged_time = 70000000;
1100   audio_nodes.push_back(internal_speaker_node);
1101   headphone_jack.active = false;
1102   audio_nodes.push_back(headphone_jack);
1103   ChangeAudioNodes(audio_nodes);
1104
1105   // Verify the AudioNodesChanged event is fired and one audio device is
1106   // removed.
1107   EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1108   cras_audio_handler_->GetAudioDevices(&audio_devices);
1109   EXPECT_EQ(init_nodes_size, audio_devices.size());
1110
1111   // Verify the active output device remains to be speaker.
1112   EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
1113   EXPECT_TRUE(
1114       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1115   EXPECT_EQ(kInternalSpeaker.id, active_output.id);
1116   EXPECT_EQ(kInternalSpeaker.id,
1117             cras_audio_handler_->GetPrimaryActiveOutputNode());
1118   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1119 }
1120
1121 TEST_F(CrasAudioHandlerTest, OneActiveAudioOutputAfterLoginNewUserSession) {
1122   // This tests the case found with crbug.com/273271.
1123   // Initialize with internal speaker, bluetooth headphone and headphone jack
1124   // for a new chrome session after user signs out from the previous session.
1125   // Headphone jack is plugged in later than bluetooth headphone, but bluetooth
1126   // headphone is selected as the active output by user from previous user
1127   // session.
1128   AudioNodeList audio_nodes;
1129   audio_nodes.push_back(kInternalSpeaker);
1130   AudioNode bluetooth_headphone(kBluetoothHeadset);
1131   bluetooth_headphone.active = true;
1132   bluetooth_headphone.plugged_time = 70000000;
1133   audio_nodes.push_back(bluetooth_headphone);
1134   AudioNode headphone_jack(kHeadphone);
1135   headphone_jack.plugged_time = 80000000;
1136   audio_nodes.push_back(headphone_jack);
1137   SetUpCrasAudioHandlerWithPrimaryActiveNode(audio_nodes, bluetooth_headphone);
1138   const size_t init_nodes_size = audio_nodes.size();
1139
1140   // Verify the audio devices size.
1141   AudioDeviceList audio_devices;
1142   cras_audio_handler_->GetAudioDevices(&audio_devices);
1143   EXPECT_EQ(init_nodes_size, audio_devices.size());
1144   EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1145
1146   // Verify the headphone jack is selected as the active output and all other
1147   // audio devices are not active.
1148   EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1149   AudioDevice active_output;
1150   EXPECT_TRUE(
1151       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1152   EXPECT_EQ(kHeadphone.id, active_output.id);
1153   EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1154   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1155   for (size_t i = 0; i < audio_devices.size(); ++i) {
1156     if (audio_devices[i].id != kHeadphone.id)
1157       EXPECT_FALSE(audio_devices[i].active);
1158   }
1159 }
1160
1161 TEST_F(CrasAudioHandlerTest, BluetoothSpeakerIdChangedOnFly) {
1162   // Initialize with internal speaker and bluetooth headset.
1163   AudioNodeList audio_nodes;
1164   audio_nodes.push_back(kInternalSpeaker);
1165   audio_nodes.push_back(kBluetoothHeadset);
1166   SetUpCrasAudioHandler(audio_nodes);
1167   const size_t init_nodes_size = audio_nodes.size();
1168
1169   // Verify the audio devices size.
1170   AudioDeviceList audio_devices;
1171   cras_audio_handler_->GetAudioDevices(&audio_devices);
1172   EXPECT_EQ(init_nodes_size, audio_devices.size());
1173   EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1174
1175   // Verify the bluetooth headset is selected as the active output and all other
1176   // audio devices are not active.
1177   EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1178   AudioDevice active_output;
1179   EXPECT_TRUE(
1180       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1181   EXPECT_EQ(kBluetoothHeadset.id, active_output.id);
1182   EXPECT_EQ(kBluetoothHeadset.id,
1183             cras_audio_handler_->GetPrimaryActiveOutputNode());
1184   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1185
1186   // Cras changes the bluetooth headset's id on the fly.
1187   audio_nodes.clear();
1188   AudioNode internal_speaker(kInternalSpeaker);
1189   internal_speaker.active = false;
1190   audio_nodes.push_back(internal_speaker);
1191   AudioNode bluetooth_headphone(kBluetoothHeadset);
1192   // Change bluetooth headphone id.
1193   bluetooth_headphone.id = kBluetoothHeadsetId + 20000;
1194   bluetooth_headphone.active = false;
1195   audio_nodes.push_back(bluetooth_headphone);
1196   ChangeAudioNodes(audio_nodes);
1197
1198   // Verify NodesChanged event is fired, and the audio devices size is not
1199   // changed.
1200   audio_devices.clear();
1201   cras_audio_handler_->GetAudioDevices(&audio_devices);
1202   EXPECT_EQ(init_nodes_size, audio_devices.size());
1203   EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1204
1205   // Verify ActiveOutputNodeChanged event is fired, and active device should be
1206   // bluetooth headphone.
1207   EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1208   EXPECT_TRUE(
1209       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1210   EXPECT_EQ(bluetooth_headphone.id, active_output.id);
1211 }
1212
1213 TEST_F(CrasAudioHandlerTest, PlugUSBMic) {
1214   // Set up initial audio devices, only with internal mic.
1215   AudioNodeList audio_nodes;
1216   audio_nodes.push_back(kInternalMic);
1217   SetUpCrasAudioHandler(audio_nodes);
1218   const size_t init_nodes_size = audio_nodes.size();
1219
1220   // Verify the audio devices size.
1221   AudioDeviceList audio_devices;
1222   cras_audio_handler_->GetAudioDevices(&audio_devices);
1223   EXPECT_EQ(init_nodes_size, audio_devices.size());
1224   EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1225
1226   // Verify the internal mic is selected as the active input.
1227   EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1228   EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1229   EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1230
1231   // Plug the USB Mic.
1232   audio_nodes.clear();
1233   AudioNode internal_mic(kInternalMic);
1234   internal_mic.active = true;
1235   audio_nodes.push_back(internal_mic);
1236   audio_nodes.push_back(kUSBMic);
1237   ChangeAudioNodes(audio_nodes);
1238
1239   // Verify the AudioNodesChanged event is fired and new audio device is added.
1240   EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1241   cras_audio_handler_->GetAudioDevices(&audio_devices);
1242   EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1243
1244   // Verify the active input device is switched to USB mic and
1245   // and ActiveInputChanged event is fired.
1246   EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
1247   EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
1248   EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1249 }
1250
1251 TEST_F(CrasAudioHandlerTest, UnplugUSBMic) {
1252   // Set up initial audio devices, with internal mic and USB Mic.
1253   AudioNodeList audio_nodes;
1254   audio_nodes.push_back(kInternalMic);
1255   audio_nodes.push_back(kUSBMic);
1256   SetUpCrasAudioHandler(audio_nodes);
1257   const size_t init_nodes_size = audio_nodes.size();
1258
1259   // Verify the audio devices size.
1260   EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1261   AudioDeviceList audio_devices;
1262   cras_audio_handler_->GetAudioDevices(&audio_devices);
1263   EXPECT_EQ(init_nodes_size, audio_devices.size());
1264
1265   // Verify the USB mic is selected as the active output.
1266   EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1267   EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
1268   EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1269
1270   // Unplug the USB Mic.
1271   audio_nodes.clear();
1272   audio_nodes.push_back(kInternalMic);
1273   ChangeAudioNodes(audio_nodes);
1274
1275   // Verify the AudioNodesChanged event is fired, and one audio device is
1276   // removed.
1277   EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1278   cras_audio_handler_->GetAudioDevices(&audio_devices);
1279   EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
1280
1281   // Verify the active input device is switched to internal mic, and
1282   // and ActiveInputChanged event is fired.
1283   EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
1284   EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1285   EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1286 }
1287
1288 TEST_F(CrasAudioHandlerTest, PlugUSBMicNotAffectActiveOutput) {
1289   // Set up initial audio devices.
1290   AudioNodeList audio_nodes;
1291   audio_nodes.push_back(kInternalSpeaker);
1292   audio_nodes.push_back(kHeadphone);
1293   audio_nodes.push_back(kInternalMic);
1294   SetUpCrasAudioHandler(audio_nodes);
1295   const size_t init_nodes_size = audio_nodes.size();
1296
1297   // Verify the audio devices size.
1298   EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1299   AudioDeviceList audio_devices;
1300   cras_audio_handler_->GetAudioDevices(&audio_devices);
1301   EXPECT_EQ(init_nodes_size, audio_devices.size());
1302
1303   // Verify the internal mic is selected as the active input.
1304   EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1305   EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
1306   EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1307
1308   // Verify the headphone is selected as the active output.
1309   EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1310   EXPECT_EQ(kHeadphoneId, cras_audio_handler_->GetPrimaryActiveOutputNode());
1311   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1312
1313   // Switch the active output to internal speaker.
1314   AudioDevice internal_speaker(kInternalSpeaker);
1315   cras_audio_handler_->SwitchToDevice(internal_speaker, true);
1316
1317   // Verify the active output is switched to internal speaker, and the
1318   // ActiveOutputNodeChanged event is fired.
1319   EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1320   AudioDevice active_output;
1321   EXPECT_TRUE(
1322       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1323   EXPECT_EQ(kInternalSpeaker.id, active_output.id);
1324   EXPECT_EQ(kInternalSpeaker.id,
1325             cras_audio_handler_->GetPrimaryActiveOutputNode());
1326
1327   // Plug the USB Mic.
1328   audio_nodes.clear();
1329   AudioNode internal_speaker_node(kInternalSpeaker);
1330   internal_speaker_node.active = true;
1331   audio_nodes.push_back(internal_speaker_node);
1332   audio_nodes.push_back(kHeadphone);
1333   AudioNode internal_mic(kInternalMic);
1334   internal_mic.active = true;
1335   audio_nodes.push_back(internal_mic);
1336   audio_nodes.push_back(kUSBMic);
1337   ChangeAudioNodes(audio_nodes);
1338
1339   // Verify the AudioNodesChanged event is fired, one new device is added.
1340   EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1341   cras_audio_handler_->GetAudioDevices(&audio_devices);
1342   EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1343
1344   // Verify the active input device is switched to USB mic, and
1345   // and ActiveInputChanged event is fired.
1346   EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
1347   EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1348   EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1349
1350   // Verify the active output device is not changed.
1351   EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1352   EXPECT_TRUE(
1353       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1354   EXPECT_EQ(kInternalSpeaker.id, active_output.id);
1355   EXPECT_EQ(kInternalSpeaker.id,
1356             cras_audio_handler_->GetPrimaryActiveOutputNode());
1357 }
1358
1359 TEST_F(CrasAudioHandlerTest, PlugHeadphoneAutoUnplugSpeakerWithActiveUSB) {
1360   // Set up initial audio devices.
1361   AudioNodeList audio_nodes;
1362   audio_nodes.push_back(kUSBHeadphone1);
1363   audio_nodes.push_back(kInternalSpeaker);
1364   audio_nodes.push_back(kInternalMic);
1365   SetUpCrasAudioHandler(audio_nodes);
1366   const size_t init_nodes_size = audio_nodes.size();
1367
1368   // Verify the audio devices size.
1369   EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1370   AudioDeviceList audio_devices;
1371   cras_audio_handler_->GetAudioDevices(&audio_devices);
1372   EXPECT_EQ(init_nodes_size, audio_devices.size());
1373
1374   // Verify the internal mic is selected as the active input.
1375   EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1376   EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
1377   EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1378
1379   // Verify the USB headphone is selected as the active output.
1380   EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1381   EXPECT_EQ(kUSBHeadphoneId1,
1382             cras_audio_handler_->GetPrimaryActiveOutputNode());
1383   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1384
1385   // Plug the headphone and auto-unplug internal speaker.
1386   audio_nodes.clear();
1387   AudioNode usb_headphone_node(kUSBHeadphone1);
1388   usb_headphone_node.active = true;
1389   audio_nodes.push_back(usb_headphone_node);
1390   AudioNode headphone_node(kHeadphone);
1391   headphone_node.plugged_time = 1000;
1392   audio_nodes.push_back(headphone_node);
1393   AudioNode internal_mic(kInternalMic);
1394   internal_mic.active = true;
1395   audio_nodes.push_back(internal_mic);
1396   ChangeAudioNodes(audio_nodes);
1397
1398   // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1399   EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1400   cras_audio_handler_->GetAudioDevices(&audio_devices);
1401   EXPECT_EQ(init_nodes_size, audio_devices.size());
1402
1403   // Verify the active output device is switched to headphone, and
1404   // an ActiveOutputChanged event is fired.
1405   EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1406   EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1407   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1408
1409   // Unplug the headphone and internal speaker auto-plugs back.
1410   audio_nodes.clear();
1411   audio_nodes.push_back(kUSBHeadphone1);
1412   AudioNode internal_speaker_node(kInternalSpeaker);
1413   internal_speaker_node.plugged_time = 2000;
1414   audio_nodes.push_back(internal_speaker_node);
1415   audio_nodes.push_back(internal_mic);
1416   ChangeAudioNodes(audio_nodes);
1417
1418   // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1419   EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1420   cras_audio_handler_->GetAudioDevices(&audio_devices);
1421   EXPECT_EQ(init_nodes_size, audio_devices.size());
1422
1423   // Verify the active output device is switched back to USB, and
1424   // an ActiveOutputChanged event is fired.
1425   EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
1426   EXPECT_EQ(kUSBHeadphone1.id,
1427             cras_audio_handler_->GetPrimaryActiveOutputNode());
1428   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1429
1430   // Verify the active input device is not changed.
1431   EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1432   EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1433 }
1434
1435 TEST_F(CrasAudioHandlerTest, PlugMicAutoUnplugInternalMicWithActiveUSB) {
1436   // Set up initial audio devices.
1437   AudioNodeList audio_nodes;
1438   audio_nodes.push_back(kUSBHeadphone1);
1439   audio_nodes.push_back(kInternalSpeaker);
1440   audio_nodes.push_back(kUSBMic);
1441   audio_nodes.push_back(kInternalMic);
1442   SetUpCrasAudioHandler(audio_nodes);
1443   const size_t init_nodes_size = audio_nodes.size();
1444
1445   // Verify the audio devices size.
1446   EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1447   AudioDeviceList audio_devices;
1448   cras_audio_handler_->GetAudioDevices(&audio_devices);
1449   EXPECT_EQ(init_nodes_size, audio_devices.size());
1450
1451   // Verify the internal mic is selected as the active input.
1452   EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
1453   EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
1454   EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1455
1456   // Verify the internal speaker is selected as the active output.
1457   EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1458   EXPECT_EQ(kUSBHeadphoneId1,
1459             cras_audio_handler_->GetPrimaryActiveOutputNode());
1460   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1461
1462   // Plug the headphone and mic, auto-unplug internal mic and speaker.
1463   audio_nodes.clear();
1464   AudioNode usb_headphone_node(kUSBHeadphone1);
1465   usb_headphone_node.active = true;
1466   audio_nodes.push_back(usb_headphone_node);
1467   AudioNode headphone_node(kHeadphone);
1468   headphone_node.plugged_time = 1000;
1469   audio_nodes.push_back(headphone_node);
1470   AudioNode usb_mic(kUSBMic);
1471   usb_mic.active = true;
1472   audio_nodes.push_back(usb_mic);
1473   AudioNode mic_jack(kMicJack);
1474   mic_jack.plugged_time = 1000;
1475   audio_nodes.push_back(mic_jack);
1476   ChangeAudioNodes(audio_nodes);
1477
1478   // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1479   EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1480   cras_audio_handler_->GetAudioDevices(&audio_devices);
1481   EXPECT_EQ(init_nodes_size, audio_devices.size());
1482
1483   // Verify the active output device is switched to headphone, and
1484   // an ActiveOutputChanged event is fired.
1485   EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1486   EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1487   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1488
1489   // Verify the active input device is switched to mic jack, and
1490   // an ActiveInputChanged event is fired.
1491   EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
1492   EXPECT_EQ(kMicJack.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1493   EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1494
1495   // Unplug the headphone and internal speaker auto-plugs back.
1496   audio_nodes.clear();
1497   audio_nodes.push_back(kUSBHeadphone1);
1498   AudioNode internal_speaker_node(kInternalSpeaker);
1499   internal_speaker_node.plugged_time = 2000;
1500   audio_nodes.push_back(internal_speaker_node);
1501   audio_nodes.push_back(kUSBMic);
1502   AudioNode internal_mic(kInternalMic);
1503   internal_mic.plugged_time = 2000;
1504   audio_nodes.push_back(internal_mic);
1505   ChangeAudioNodes(audio_nodes);
1506
1507   // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1508   EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1509   cras_audio_handler_->GetAudioDevices(&audio_devices);
1510   EXPECT_EQ(init_nodes_size, audio_devices.size());
1511
1512   // Verify the active output device is switched back to USB, and
1513   // an ActiveOutputChanged event is fired.
1514   EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
1515   EXPECT_EQ(kUSBHeadphone1.id,
1516             cras_audio_handler_->GetPrimaryActiveOutputNode());
1517   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1518
1519   // Verify the active input device is switched back to USB mic, and
1520   // an ActiveInputChanged event is fired.
1521   EXPECT_EQ(2, test_observer_->active_input_node_changed_count());
1522   EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1523   EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1524 }
1525
1526 TEST_F(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnPlugInHeadphone) {
1527   // Set up initial audio devices.
1528   AudioNodeList audio_nodes;
1529   audio_nodes.push_back(kInternalSpeaker);
1530   audio_nodes.push_back(kBluetoothHeadset);
1531   SetUpCrasAudioHandler(audio_nodes);
1532   const size_t init_nodes_size = audio_nodes.size();
1533
1534   // Verify the audio devices size.
1535   EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1536   AudioDeviceList audio_devices;
1537   cras_audio_handler_->GetAudioDevices(&audio_devices);
1538   EXPECT_EQ(init_nodes_size, audio_devices.size());
1539
1540   // Verify the bluetooth headset is selected as the active output.
1541   EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1542   EXPECT_EQ(kBluetoothHeadsetId,
1543             cras_audio_handler_->GetPrimaryActiveOutputNode());
1544   AudioDevice active_output;
1545   EXPECT_TRUE(
1546       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1547   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1548
1549   // Plug in headphone, but fire NodesChanged signal twice.
1550   audio_nodes.clear();
1551   audio_nodes.push_back(kInternalSpeaker);
1552   AudioNode bluetooth_headset(kBluetoothHeadset);
1553   bluetooth_headset.plugged_time = 1000;
1554   bluetooth_headset.active = true;
1555   audio_nodes.push_back(bluetooth_headset);
1556   AudioNode headphone(kHeadphone);
1557   headphone.active = false;
1558   headphone.plugged_time = 2000;
1559   audio_nodes.push_back(headphone);
1560   ChangeAudioNodes(audio_nodes);
1561   ChangeAudioNodes(audio_nodes);
1562
1563   // Verify the active output device is set to headphone.
1564   EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1565   EXPECT_LE(1, test_observer_->active_output_node_changed_count());
1566   EXPECT_EQ(headphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1567   EXPECT_TRUE(
1568       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1569   EXPECT_EQ(headphone.id, active_output.id);
1570
1571   // Verfiy the audio devices data is consistent, i.e., the active output device
1572   // should be headphone.
1573   cras_audio_handler_->GetAudioDevices(&audio_devices);
1574   EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1575   for (size_t i = 0; i < audio_devices.size(); ++i) {
1576     if (audio_devices[i].id == kInternalSpeaker.id)
1577       EXPECT_FALSE(audio_devices[i].active);
1578     else if (audio_devices[i].id == bluetooth_headset.id)
1579       EXPECT_FALSE(audio_devices[i].active);
1580     else if (audio_devices[i].id == headphone.id)
1581       EXPECT_TRUE(audio_devices[i].active);
1582     else
1583       NOTREACHED();
1584   }
1585 }
1586
1587 TEST_F(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnPlugInUSBMic) {
1588   // Set up initial audio devices.
1589   AudioNodeList audio_nodes;
1590   audio_nodes.push_back(kInternalMic);
1591   SetUpCrasAudioHandler(audio_nodes);
1592   const size_t init_nodes_size = audio_nodes.size();
1593
1594   // Verify the audio devices size.
1595   EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1596   AudioDeviceList audio_devices;
1597   cras_audio_handler_->GetAudioDevices(&audio_devices);
1598   EXPECT_EQ(init_nodes_size, audio_devices.size());
1599
1600   // Verify the internal mic is selected as the active output.
1601   EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1602   EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1603   EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
1604   EXPECT_TRUE(audio_devices[0].active);
1605
1606   // Plug in usb mic, but fire NodesChanged signal twice.
1607   audio_nodes.clear();
1608   AudioNode internal_mic(kInternalMic);
1609   internal_mic.active = true;
1610   internal_mic.plugged_time = 1000;
1611   audio_nodes.push_back(internal_mic);
1612   AudioNode usb_mic(kUSBMic);
1613   usb_mic.active = false;
1614   usb_mic.plugged_time = 2000;
1615   audio_nodes.push_back(usb_mic);
1616   ChangeAudioNodes(audio_nodes);
1617   ChangeAudioNodes(audio_nodes);
1618
1619   // Verify the active output device is set to headphone.
1620   EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1621   EXPECT_LE(1, test_observer_->active_input_node_changed_count());
1622   EXPECT_EQ(usb_mic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1623   EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1624
1625   // Verfiy the audio devices data is consistent, i.e., the active input device
1626   // should be usb mic.
1627   cras_audio_handler_->GetAudioDevices(&audio_devices);
1628   EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1629   for (size_t i = 0; i < audio_devices.size(); ++i) {
1630     if (audio_devices[i].id == kInternalMic.id)
1631       EXPECT_FALSE(audio_devices[i].active);
1632     else if (audio_devices[i].id == usb_mic.id)
1633       EXPECT_TRUE(audio_devices[i].active);
1634     else
1635       NOTREACHED();
1636   }
1637 }
1638
1639 // This is the case of crbug.com/291303.
1640 TEST_F(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnSystemBoot) {
1641   // Set up audio handler with empty audio_nodes.
1642   AudioNodeList audio_nodes;
1643   SetUpCrasAudioHandler(audio_nodes);
1644
1645   AudioNode internal_speaker(kInternalSpeaker);
1646   internal_speaker.active = false;
1647   AudioNode headphone(kHeadphone);
1648   headphone.active = false;
1649   AudioNode internal_mic(kInternalMic);
1650   internal_mic.active = false;
1651   audio_nodes.push_back(internal_speaker);
1652   audio_nodes.push_back(headphone);
1653   audio_nodes.push_back(internal_mic);
1654   const size_t init_nodes_size = audio_nodes.size();
1655
1656   // Simulate AudioNodesChanged signal being fired twice during system boot.
1657   ChangeAudioNodes(audio_nodes);
1658   ChangeAudioNodes(audio_nodes);
1659
1660   // Verify the active output device is set to headphone.
1661   EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1662   EXPECT_LE(1, test_observer_->active_output_node_changed_count());
1663   EXPECT_EQ(headphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1664   AudioDevice active_output;
1665   EXPECT_TRUE(
1666       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1667   EXPECT_EQ(headphone.id, active_output.id);
1668
1669   // Verify the active input device id is set to internal mic.
1670   EXPECT_EQ(internal_mic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1671
1672   // Verfiy the audio devices data is consistent, i.e., the active output device
1673   // should be headphone, and the active input device should internal mic.
1674   AudioDeviceList audio_devices;
1675   cras_audio_handler_->GetAudioDevices(&audio_devices);
1676   EXPECT_EQ(init_nodes_size, audio_devices.size());
1677   for (size_t i = 0; i < audio_devices.size(); ++i) {
1678     if (audio_devices[i].id == internal_speaker.id)
1679       EXPECT_FALSE(audio_devices[i].active);
1680     else if (audio_devices[i].id == headphone.id)
1681       EXPECT_TRUE(audio_devices[i].active);
1682     else if (audio_devices[i].id == internal_mic.id)
1683       EXPECT_TRUE(audio_devices[i].active);
1684     else
1685       NOTREACHED();
1686   }
1687 }
1688
1689 TEST_F(CrasAudioHandlerTest, SetOutputMute) {
1690   AudioNodeList audio_nodes;
1691   audio_nodes.push_back(kInternalSpeaker);
1692   SetUpCrasAudioHandler(audio_nodes);
1693   EXPECT_EQ(0, test_observer_->output_mute_changed_count());
1694
1695   // Mute the device.
1696   cras_audio_handler_->SetOutputMute(true);
1697
1698   // Verify the output is muted, OnOutputMuteChanged event is fired,
1699   // and mute value is saved in the preferences.
1700   EXPECT_TRUE(cras_audio_handler_->IsOutputMuted());
1701   EXPECT_EQ(1, test_observer_->output_mute_changed_count());
1702   AudioDevice speaker(kInternalSpeaker);
1703   EXPECT_TRUE(audio_pref_handler_->GetMuteValue(speaker));
1704
1705   // Unmute the device.
1706   cras_audio_handler_->SetOutputMute(false);
1707
1708   // Verify the output is unmuted, OnOutputMuteChanged event is fired,
1709   // and mute value is saved in the preferences.
1710   EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
1711   EXPECT_EQ(2, test_observer_->output_mute_changed_count());
1712   EXPECT_FALSE(audio_pref_handler_->GetMuteValue(speaker));
1713 }
1714
1715 TEST_F(CrasAudioHandlerTest, SetInputMute) {
1716   AudioNodeList audio_nodes;
1717   audio_nodes.push_back(kInternalMic);
1718   SetUpCrasAudioHandler(audio_nodes);
1719   EXPECT_EQ(0, test_observer_->input_mute_changed_count());
1720
1721   // Mute the device.
1722   cras_audio_handler_->SetInputMute(true);
1723
1724   // Verify the input is muted, OnInputMuteChanged event is fired.
1725   EXPECT_TRUE(cras_audio_handler_->IsInputMuted());
1726   EXPECT_EQ(1, test_observer_->input_mute_changed_count());
1727
1728   // Unmute the device.
1729   cras_audio_handler_->SetInputMute(false);
1730
1731   // Verify the input is unmuted, OnInputMuteChanged event is fired.
1732   EXPECT_FALSE(cras_audio_handler_->IsInputMuted());
1733   EXPECT_EQ(2, test_observer_->input_mute_changed_count());
1734 }
1735
1736 TEST_F(CrasAudioHandlerTest, SetOutputVolumePercent) {
1737   AudioNodeList audio_nodes;
1738   audio_nodes.push_back(kInternalSpeaker);
1739   SetUpCrasAudioHandler(audio_nodes);
1740   EXPECT_EQ(0, test_observer_->output_volume_changed_count());
1741
1742   cras_audio_handler_->SetOutputVolumePercent(60);
1743
1744   // Verify the output volume is changed to the designated value,
1745   // OnOutputVolumeChanged event is fired, and the device volume value
1746   // is saved the preferences.
1747   const int kVolume = 60;
1748   EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent());
1749   EXPECT_EQ(1, test_observer_->output_volume_changed_count());
1750   AudioDevice device;
1751   EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device));
1752   EXPECT_EQ(device.id, kInternalSpeaker.id);
1753   EXPECT_EQ(kVolume, audio_pref_handler_->GetOutputVolumeValue(&device));
1754 }
1755
1756 TEST_F(CrasAudioHandlerTest, SetInputGainPercent) {
1757   AudioNodeList audio_nodes;
1758   audio_nodes.push_back(kInternalMic);
1759   SetUpCrasAudioHandler(audio_nodes);
1760   EXPECT_EQ(0, test_observer_->input_gain_changed_count());
1761
1762   cras_audio_handler_->SetInputGainPercent(60);
1763
1764   // Verify the input gain changed to the designated value,
1765   // OnInputGainChanged event is fired, and the device gain value
1766   // is saved in the preferences.
1767   const int kGain = 60;
1768   EXPECT_EQ(kGain, cras_audio_handler_->GetInputGainPercent());
1769   EXPECT_EQ(1, test_observer_->input_gain_changed_count());
1770   AudioDevice internal_mic(kInternalMic);
1771   EXPECT_EQ(kGain, audio_pref_handler_->GetInputGainValue(&internal_mic));
1772 }
1773
1774 TEST_F(CrasAudioHandlerTest, SetMuteForDevice) {
1775   AudioNodeList audio_nodes;
1776   audio_nodes.push_back(kInternalSpeaker);
1777   audio_nodes.push_back(kHeadphone);
1778   audio_nodes.push_back(kInternalMic);
1779   audio_nodes.push_back(kUSBMic);
1780   SetUpCrasAudioHandler(audio_nodes);
1781
1782   // Mute the active output device.
1783   EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1784   cras_audio_handler_->SetMuteForDevice(kHeadphone.id, true);
1785
1786   // Verify the headphone is muted and mute value is saved in the preferences.
1787   EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kHeadphone.id));
1788   AudioDevice headphone(kHeadphone);
1789   EXPECT_TRUE(audio_pref_handler_->GetMuteValue(headphone));
1790
1791   // Mute the non-active output device.
1792   cras_audio_handler_->SetMuteForDevice(kInternalSpeaker.id, true);
1793
1794   // Verify the internal speaker is muted and mute value is saved in the
1795   // preferences.
1796   EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kInternalSpeaker.id));
1797   AudioDevice internal_speaker(kInternalSpeaker);
1798   EXPECT_TRUE(audio_pref_handler_->GetMuteValue(internal_speaker));
1799
1800   // Mute the active input device.
1801   EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1802   cras_audio_handler_->SetMuteForDevice(kUSBMic.id, true);
1803
1804   // Verify the USB Mic is muted.
1805   EXPECT_TRUE(cras_audio_handler_->IsInputMutedForDevice(kUSBMic.id));
1806
1807   // Mute the non-active input device should be a no-op, see crbug.com/365050.
1808   cras_audio_handler_->SetMuteForDevice(kInternalMic.id, true);
1809
1810   // Verify IsInputMutedForDevice returns false for non-active input device.
1811   EXPECT_FALSE(cras_audio_handler_->IsInputMutedForDevice(kInternalMic.id));
1812 }
1813
1814 TEST_F(CrasAudioHandlerTest, SetVolumeGainPercentForDevice) {
1815   AudioNodeList audio_nodes;
1816   audio_nodes.push_back(kInternalSpeaker);
1817   audio_nodes.push_back(kHeadphone);
1818   audio_nodes.push_back(kInternalMic);
1819   audio_nodes.push_back(kUSBMic);
1820   SetUpCrasAudioHandler(audio_nodes);
1821
1822   // Set volume percent for active output device.
1823   const int kHeadphoneVolume = 30;
1824   EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1825   cras_audio_handler_->SetVolumeGainPercentForDevice(kHeadphone.id,
1826                                                      kHeadphoneVolume);
1827
1828   // Verify the volume percent of headphone is set, and saved in preferences.
1829   EXPECT_EQ(kHeadphoneVolume,
1830             cras_audio_handler_->GetOutputVolumePercentForDevice(
1831                 kHeadphone.id));
1832   AudioDevice headphone(kHeadphone);
1833   EXPECT_EQ(kHeadphoneVolume,
1834             audio_pref_handler_->GetOutputVolumeValue(&headphone));
1835
1836   // Set volume percent for non-active output device.
1837   const int kSpeakerVolume = 60;
1838   cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalSpeaker.id,
1839                                                      kSpeakerVolume);
1840
1841   // Verify the volume percent of speaker is set, and saved in preferences.
1842   EXPECT_EQ(kSpeakerVolume,
1843             cras_audio_handler_->GetOutputVolumePercentForDevice(
1844                 kInternalSpeaker.id));
1845   AudioDevice speaker(kInternalSpeaker);
1846   EXPECT_EQ(kSpeakerVolume,
1847             audio_pref_handler_->GetOutputVolumeValue(&speaker));
1848
1849   // Set gain percent for active input device.
1850   const int kUSBMicGain = 30;
1851   EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1852   cras_audio_handler_->SetVolumeGainPercentForDevice(kUSBMic.id,
1853                                                      kUSBMicGain);
1854
1855   // Verify the gain percent of USB mic is set, and saved in preferences.
1856   EXPECT_EQ(kUSBMicGain,
1857             cras_audio_handler_->GetOutputVolumePercentForDevice(kUSBMic.id));
1858   AudioDevice usb_mic(kHeadphone);
1859   EXPECT_EQ(kUSBMicGain,
1860             audio_pref_handler_->GetInputGainValue(&usb_mic));
1861
1862   // Set gain percent for non-active input device.
1863   const int kInternalMicGain = 60;
1864   cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalMic.id,
1865                                                      kInternalMicGain);
1866
1867   // Verify the gain percent of internal mic is set, and saved in preferences.
1868   EXPECT_EQ(kInternalMicGain,
1869             cras_audio_handler_->GetOutputVolumePercentForDevice(
1870                 kInternalMic.id));
1871   AudioDevice internal_mic(kInternalMic);
1872   EXPECT_EQ(kInternalMicGain,
1873             audio_pref_handler_->GetInputGainValue(&internal_mic));
1874 }
1875
1876 TEST_F(CrasAudioHandlerTest, HandleOtherDeviceType) {
1877   const size_t kNumValidAudioDevices = 4;
1878   AudioNodeList audio_nodes;
1879   audio_nodes.push_back(kInternalSpeaker);
1880   audio_nodes.push_back(kOtherTypeOutput);
1881   audio_nodes.push_back(kInternalMic);
1882   audio_nodes.push_back(kOtherTypeInput);
1883   SetUpCrasAudioHandler(audio_nodes);
1884
1885   // Verify the audio devices size.
1886   AudioDeviceList audio_devices;
1887   cras_audio_handler_->GetAudioDevices(&audio_devices);
1888   EXPECT_EQ(kNumValidAudioDevices, audio_devices.size());
1889
1890   // Verify the internal speaker has been selected as the active output,
1891   // and the output device with some randown unknown type is handled gracefully.
1892   AudioDevice active_output;
1893   EXPECT_TRUE(
1894       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1895   EXPECT_EQ(kInternalSpeaker.id, active_output.id);
1896   EXPECT_EQ(kInternalSpeaker.id,
1897             cras_audio_handler_->GetPrimaryActiveOutputNode());
1898   EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1899
1900   // Ensure the internal microphone has been selected as the active input,
1901   // and the input device with some random unknown type is handled gracefully.
1902   AudioDevice active_input;
1903   EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1904   EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1905 }
1906
1907 TEST_F(CrasAudioHandlerTest, ChangeActiveNodesHotrodInit) {
1908   AudioNodeList audio_nodes;
1909   audio_nodes.push_back(kHDMIOutput);
1910   audio_nodes.push_back(kUSBJabraSpeakerOutput1);
1911   audio_nodes.push_back(kUSBJabraSpeakerOutput2);
1912   audio_nodes.push_back(kUSBJabraSpeakerInput1);
1913   audio_nodes.push_back(kUSBJabraSpeakerInput2);
1914   audio_nodes.push_back(kUSBCameraInput);
1915   SetUpCrasAudioHandler(audio_nodes);
1916
1917   // Verify the audio devices size.
1918   AudioDeviceList audio_devices;
1919   cras_audio_handler_->GetAudioDevices(&audio_devices);
1920   EXPECT_EQ(audio_nodes.size(), audio_devices.size());
1921
1922   // Verify only the 1st jabra speaker's output and input are selected as active
1923   // nodes by CrasAudioHandler.
1924   AudioDevice active_output;
1925   EXPECT_TRUE(
1926       cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1927   EXPECT_EQ(2, GetActiveDeviceCount());
1928   AudioDevice primary_active_device;
1929   EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(
1930       &primary_active_device));
1931   EXPECT_EQ(kUSBJabraSpeakerOutput1.id, primary_active_device.id);
1932   EXPECT_EQ(kUSBJabraSpeakerInput1.id,
1933             cras_audio_handler_->GetPrimaryActiveInputNode());
1934
1935   // Set both jabra speakers's input and output nodes to active, this simulate
1936   // the call sent by hotrod initialization process.
1937   test_observer_->reset_active_output_node_changed_count();
1938   test_observer_->reset_active_input_node_changed_count();
1939   CrasAudioHandler::NodeIdList active_nodes;
1940   active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
1941   active_nodes.push_back(kUSBJabraSpeakerOutput2.id);
1942   active_nodes.push_back(kUSBJabraSpeakerInput1.id);
1943   active_nodes.push_back(kUSBJabraSpeakerInput2.id);
1944   cras_audio_handler_->ChangeActiveNodes(active_nodes);
1945
1946   // Verify both jabra speakers' input/output nodes are made active.
1947   // num_active_nodes = GetActiveDeviceCount();
1948   EXPECT_EQ(4, GetActiveDeviceCount());
1949   const AudioDevice* active_output_1 =
1950       GetDeviceFromId(kUSBJabraSpeakerOutput1.id);
1951   EXPECT_TRUE(active_output_1->active);
1952   const AudioDevice* active_output_2 =
1953       GetDeviceFromId(kUSBJabraSpeakerOutput2.id);
1954   EXPECT_TRUE(active_output_2->active);
1955   EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(
1956       &primary_active_device));
1957   EXPECT_EQ(kUSBJabraSpeakerOutput1.id, primary_active_device.id);
1958   const AudioDevice* active_input_1 =
1959       GetDeviceFromId(kUSBJabraSpeakerInput1.id);
1960   EXPECT_TRUE(active_input_1->active);
1961   const AudioDevice* active_input_2 =
1962       GetDeviceFromId(kUSBJabraSpeakerInput2.id);
1963   EXPECT_TRUE(active_input_2->active);
1964   EXPECT_EQ(kUSBJabraSpeakerInput1.id,
1965             cras_audio_handler_->GetPrimaryActiveInputNode());
1966
1967   // Verify only 1 ActiveOutputNodeChanged notification has been sent out
1968   // by calling ChangeActiveNodes.
1969   EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1970   EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
1971
1972   // Verify all active devices are the not muted and their volume values are
1973   // the same.
1974   EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
1975   EXPECT_FALSE(
1976       cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput1.id));
1977   EXPECT_FALSE(
1978       cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput2.id));
1979   EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(),
1980             cras_audio_handler_->GetOutputVolumePercentForDevice(
1981                 kUSBJabraSpeakerOutput1.id));
1982   EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(),
1983             cras_audio_handler_->GetOutputVolumePercentForDevice(
1984                 kUSBJabraSpeakerOutput2.id));
1985
1986   // Adjust the volume of output devices, verify all active nodes are set to
1987   // the same volume.
1988   cras_audio_handler_->SetOutputVolumePercent(25);
1989   EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercent());
1990   EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercentForDevice(
1991                     kUSBJabraSpeakerOutput1.id));
1992   EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercentForDevice(
1993                     kUSBJabraSpeakerOutput2.id));
1994 }
1995
1996 TEST_F(CrasAudioHandlerTest, ChangeActiveNodesHotrodInitWithCameraInputActive) {
1997   AudioNodeList audio_nodes;
1998   audio_nodes.push_back(kHDMIOutput);
1999   audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2000   audio_nodes.push_back(kUSBJabraSpeakerOutput2);
2001   audio_nodes.push_back(kUSBJabraSpeakerInput1);
2002   audio_nodes.push_back(kUSBJabraSpeakerInput2);
2003   // Make the camera input to be plugged in later than jabra's input.
2004   AudioNode usb_camera(kUSBCameraInput);
2005   usb_camera.plugged_time = 10000000;
2006   audio_nodes.push_back(usb_camera);
2007   SetUpCrasAudioHandler(audio_nodes);
2008
2009   // Verify the audio devices size.
2010   AudioDeviceList audio_devices;
2011   cras_audio_handler_->GetAudioDevices(&audio_devices);
2012   EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2013
2014   // Verify the 1st jabra speaker's output is selected as active output
2015   // node and camera's input is selected active input by CrasAudioHandler.
2016   EXPECT_EQ(2, GetActiveDeviceCount());
2017   EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2018             cras_audio_handler_->GetPrimaryActiveOutputNode());
2019   EXPECT_EQ(kUSBCameraInput.id,
2020             cras_audio_handler_->GetPrimaryActiveInputNode());
2021
2022   // Set both jabra speakers's input and output nodes to active, this simulates
2023   // the call sent by hotrod initialization process.
2024   test_observer_->reset_active_output_node_changed_count();
2025   test_observer_->reset_active_input_node_changed_count();
2026   CrasAudioHandler::NodeIdList active_nodes;
2027   active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2028   active_nodes.push_back(kUSBJabraSpeakerOutput2.id);
2029   active_nodes.push_back(kUSBJabraSpeakerInput1.id);
2030   active_nodes.push_back(kUSBJabraSpeakerInput2.id);
2031   cras_audio_handler_->ChangeActiveNodes(active_nodes);
2032
2033   // Verify both jabra speakers' input/output nodes are made active.
2034   // num_active_nodes = GetActiveDeviceCount();
2035   EXPECT_EQ(4, GetActiveDeviceCount());
2036   const AudioDevice* active_output_1 =
2037       GetDeviceFromId(kUSBJabraSpeakerOutput1.id);
2038   EXPECT_TRUE(active_output_1->active);
2039   const AudioDevice* active_output_2 =
2040       GetDeviceFromId(kUSBJabraSpeakerOutput2.id);
2041   EXPECT_TRUE(active_output_2->active);
2042   EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2043             cras_audio_handler_->GetPrimaryActiveOutputNode());
2044   const AudioDevice* active_input_1 =
2045       GetDeviceFromId(kUSBJabraSpeakerInput1.id);
2046   EXPECT_TRUE(active_input_1->active);
2047   const AudioDevice* active_input_2 =
2048       GetDeviceFromId(kUSBJabraSpeakerInput2.id);
2049   EXPECT_TRUE(active_input_2->active);
2050   EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2051             cras_audio_handler_->GetPrimaryActiveInputNode());
2052
2053   // Verify only 1 ActiveOutputNodeChanged notification has been sent out
2054   // by calling ChangeActiveNodes.
2055   EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
2056   EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
2057 }
2058
2059 TEST_F(CrasAudioHandlerTest, ChangeActiveNodesWithFewerActives) {
2060   AudioNodeList audio_nodes;
2061   audio_nodes.push_back(kHDMIOutput);
2062   audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2063   audio_nodes.push_back(kUSBJabraSpeakerOutput2);
2064   SetUpCrasAudioHandler(audio_nodes);
2065
2066   // Verify the audio devices size.
2067   AudioDeviceList audio_devices;
2068   cras_audio_handler_->GetAudioDevices(&audio_devices);
2069   EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2070
2071   // Set all three nodes to be active.
2072   CrasAudioHandler::NodeIdList active_nodes;
2073   active_nodes.push_back(kHDMIOutput.id);
2074   active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2075   active_nodes.push_back(kUSBJabraSpeakerOutput2.id);
2076   cras_audio_handler_->ChangeActiveNodes(active_nodes);
2077
2078   // Verify all three nodes are active.
2079   EXPECT_EQ(3, GetActiveDeviceCount());
2080   const AudioDevice* active_output_1 = GetDeviceFromId(kHDMIOutput.id);
2081   EXPECT_TRUE(active_output_1->active);
2082   const AudioDevice* active_output_2 =
2083       GetDeviceFromId(kUSBJabraSpeakerOutput1.id);
2084   EXPECT_TRUE(active_output_2->active);
2085   const AudioDevice* active_output_3 =
2086       GetDeviceFromId(kUSBJabraSpeakerOutput2.id);
2087   EXPECT_TRUE(active_output_3->active);
2088
2089   // Now call ChangeActiveDevices with only 2 nodes.
2090   active_nodes.clear();
2091   active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2092   active_nodes.push_back(kUSBJabraSpeakerOutput2.id);
2093   cras_audio_handler_->ChangeActiveNodes(active_nodes);
2094
2095   // Verify only 2 nodes are active.
2096   EXPECT_EQ(2, GetActiveDeviceCount());
2097   const AudioDevice* output_1 = GetDeviceFromId(kHDMIOutput.id);
2098   EXPECT_FALSE(output_1->active);
2099   const AudioDevice* output_2 = GetDeviceFromId(kUSBJabraSpeakerOutput1.id);
2100   EXPECT_TRUE(output_2->active);
2101   const AudioDevice* output_3 = GetDeviceFromId(kUSBJabraSpeakerOutput2.id);
2102   EXPECT_TRUE(output_3->active);
2103 }
2104
2105 TEST_F(CrasAudioHandlerTest, HotrodInitWithSingleJabra) {
2106   // Simulates the hotrod initializated with a single jabra device and
2107   // CrasAudioHandler selected jabra input/output as active devices.
2108   AudioNodeList audio_nodes;
2109   audio_nodes.push_back(kHDMIOutput);
2110   audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2111   audio_nodes.push_back(kUSBJabraSpeakerInput1);
2112   audio_nodes.push_back(kUSBCameraInput);
2113   SetUpCrasAudioHandler(audio_nodes);
2114
2115   // Verify the audio devices size.
2116   AudioDeviceList audio_devices;
2117   cras_audio_handler_->GetAudioDevices(&audio_devices);
2118   EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2119
2120   // Verify the jabra speaker's output and input are selected as active nodes
2121   // by CrasAudioHandler.
2122   EXPECT_EQ(2, GetActiveDeviceCount());
2123   EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2124             cras_audio_handler_->GetPrimaryActiveOutputNode());
2125   EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2126             cras_audio_handler_->GetPrimaryActiveInputNode());
2127 }
2128
2129 TEST_F(CrasAudioHandlerTest,
2130        ChangeActiveNodesHotrodInitWithSingleJabraCameraPlugInLater) {
2131   AudioNodeList audio_nodes;
2132   audio_nodes.push_back(kHDMIOutput);
2133   audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2134   audio_nodes.push_back(kUSBJabraSpeakerInput1);
2135   AudioNode usb_camera(kUSBCameraInput);
2136   usb_camera.plugged_time = 10000000;
2137   audio_nodes.push_back(usb_camera);
2138   SetUpCrasAudioHandler(audio_nodes);
2139
2140   // Verify the audio devices size.
2141   AudioDeviceList audio_devices;
2142   cras_audio_handler_->GetAudioDevices(&audio_devices);
2143   EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2144
2145   // Verify the jabra speaker's output is selected as active output, and
2146   // camera's input is selected as active input by CrasAudioHandler
2147   EXPECT_EQ(2, GetActiveDeviceCount());
2148   EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2149             cras_audio_handler_->GetPrimaryActiveOutputNode());
2150   EXPECT_EQ(kUSBCameraInput.id,
2151             cras_audio_handler_->GetPrimaryActiveInputNode());
2152
2153   // Simulate hotrod app call to set jabra input as active device with only
2154   // jabra input node in the active node list, which does not conform to the
2155   // new SetActiveDevices protocol, but just show we can still handle it if
2156   // this happens.
2157   CrasAudioHandler::NodeIdList active_nodes;
2158   active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2159   active_nodes.push_back(kUSBJabraSpeakerInput1.id);
2160   cras_audio_handler_->ChangeActiveNodes(active_nodes);
2161
2162   // Verify the jabra speaker's output is selected as active output, and
2163   // jabra's input is selected as active input.
2164   EXPECT_EQ(2, GetActiveDeviceCount());
2165   EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2166             cras_audio_handler_->GetPrimaryActiveOutputNode());
2167   EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2168             cras_audio_handler_->GetPrimaryActiveInputNode());
2169 }
2170
2171 TEST_F(CrasAudioHandlerTest,
2172        ChangeActiveNodesHotrodInitWithSingleJabraCameraPlugInLaterOldCall) {
2173   AudioNodeList audio_nodes;
2174   audio_nodes.push_back(kHDMIOutput);
2175   audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2176   audio_nodes.push_back(kUSBJabraSpeakerInput1);
2177   AudioNode usb_camera(kUSBCameraInput);
2178   usb_camera.plugged_time = 10000000;
2179   audio_nodes.push_back(usb_camera);
2180   SetUpCrasAudioHandler(audio_nodes);
2181
2182   // Verify the audio devices size.
2183   AudioDeviceList audio_devices;
2184   cras_audio_handler_->GetAudioDevices(&audio_devices);
2185   EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2186
2187   // Verify the jabra speaker's output is selected as active output, and
2188   // camera's input is selected as active input by CrasAudioHandler
2189   EXPECT_EQ(2, GetActiveDeviceCount());
2190   EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2191             cras_audio_handler_->GetPrimaryActiveOutputNode());
2192   EXPECT_EQ(kUSBCameraInput.id,
2193             cras_audio_handler_->GetPrimaryActiveInputNode());
2194
2195   // Simulate hotrod app call to set jabra input as active device with only
2196   // jabra input node in the active node list, which does not conform to the
2197   // new SetActiveDevices protocol, but just show we can still handle it if
2198   // this happens.
2199   CrasAudioHandler::NodeIdList active_nodes;
2200   active_nodes.push_back(kUSBJabraSpeakerInput1.id);
2201   cras_audio_handler_->ChangeActiveNodes(active_nodes);
2202
2203   // Verify the jabra speaker's output is selected as active output, and
2204   // jabra's input is selected as active input.
2205   EXPECT_EQ(2, GetActiveDeviceCount());
2206   EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2207             cras_audio_handler_->GetPrimaryActiveOutputNode());
2208   EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2209             cras_audio_handler_->GetPrimaryActiveInputNode());
2210 }
2211
2212 TEST_F(CrasAudioHandlerTest,
2213        ChangeActiveNodesHotrodInitWithSingleJabraChangeOutput) {
2214   AudioNodeList audio_nodes;
2215   audio_nodes.push_back(kHDMIOutput);
2216   audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2217   audio_nodes.push_back(kUSBJabraSpeakerInput1);
2218   audio_nodes.push_back(kUSBCameraInput);
2219   SetUpCrasAudioHandler(audio_nodes);
2220
2221   // Verify the audio devices size.
2222   AudioDeviceList audio_devices;
2223   cras_audio_handler_->GetAudioDevices(&audio_devices);
2224   EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2225
2226   // Verify the jabra speaker's output and input are selected as active output
2227   // by CrasAudioHandler.
2228   EXPECT_EQ(2, GetActiveDeviceCount());
2229   EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2230             cras_audio_handler_->GetPrimaryActiveOutputNode());
2231   EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2232             cras_audio_handler_->GetPrimaryActiveInputNode());
2233
2234   // Simulate hotrod app call SetActiveDevices to change active output
2235   // with only complete list of active nodes passed in, which is the new
2236   // way of hotrod app.
2237   CrasAudioHandler::NodeIdList active_nodes;
2238   active_nodes.push_back(kHDMIOutput.id);
2239   active_nodes.push_back(kUSBJabraSpeakerInput1.id);
2240   cras_audio_handler_->ChangeActiveNodes(active_nodes);
2241
2242   // Verify the jabra speaker's output is selected as active output, and
2243   // jabra's input is selected as active input.
2244   EXPECT_EQ(2, GetActiveDeviceCount());
2245   EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2246   EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2247             cras_audio_handler_->GetPrimaryActiveInputNode());
2248 }
2249
2250 TEST_F(CrasAudioHandlerTest,
2251        ChangeActiveNodesHotrodInitWithSingleJabraChangeOutputOldCall) {
2252   AudioNodeList audio_nodes;
2253   audio_nodes.push_back(kHDMIOutput);
2254   audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2255   audio_nodes.push_back(kUSBJabraSpeakerInput1);
2256   audio_nodes.push_back(kUSBCameraInput);
2257   SetUpCrasAudioHandler(audio_nodes);
2258
2259   // Verify the audio devices size.
2260   AudioDeviceList audio_devices;
2261   cras_audio_handler_->GetAudioDevices(&audio_devices);
2262   EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2263
2264   // Verify the jabra speaker's output and input are selected as active output
2265   // by CrasAudioHandler.
2266   EXPECT_EQ(2, GetActiveDeviceCount());
2267   EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2268             cras_audio_handler_->GetPrimaryActiveOutputNode());
2269   EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2270             cras_audio_handler_->GetPrimaryActiveInputNode());
2271
2272   // Simulate hotrod app call SetActiveDevices to change active output
2273   // with only a single active output nodes passed in, which is the old
2274   // way of hotrod app.
2275   CrasAudioHandler::NodeIdList active_nodes;
2276   active_nodes.push_back(kHDMIOutput.id);
2277   cras_audio_handler_->ChangeActiveNodes(active_nodes);
2278
2279   // Verify the jabra speaker's output is selected as active output, and
2280   // jabra's input is selected as active input.
2281   EXPECT_EQ(2, GetActiveDeviceCount());
2282   EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2283   EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2284             cras_audio_handler_->GetPrimaryActiveInputNode());
2285 }
2286
2287 }  // namespace chromeos