Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(286)

Side by Side Diff: chromeos/audio/cras_audio_handler_unittest.cc

Issue 19861002: Add test coverage for CrasAudioHandler. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix nits. Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chromeos/audio/cras_audio_handler.cc ('k') | chromeos/audio/mock_cras_audio_handler.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 using chromeos::AudioNode;
stevenjb 2013/07/26 21:03:17 Not needed
jennyz 2013/07/26 21:07:40 Done.
20
21 const uint64 kInternalSpeakerId = 10001;
22 const uint64 kHeadphoneId = 10002;
23 const uint64 kInternalMicId = 10003;
24 const uint64 kUSBMicId = 10004;
25 const uint64 kBluetoothHeadsetId = 10005;
26 const uint64 kHDMIOutputId = 10006;
27 const uint64 kUSBHeadphoneId1 = 10007;
28 const uint64 kUSBHeadphoneId2 = 10008;
29 const uint64 kOtherTypeOutputId = 90001;
30 const uint64 kOtherTypeInputId = 90002;
31
32 const AudioNode kInternalSpeaker(
33 false,
34 kInternalSpeakerId,
35 "Fake Speaker",
36 "INTERNAL_SPEAKER",
37 "Speaker",
38 false,
39 0
40 );
41
42 const AudioNode kHeadphone(
43 false,
44 kHeadphoneId,
45 "Fake Headphone",
46 "HEADPHONE",
47 "Headphone",
48 false,
49 0
50 );
51
52 const AudioNode kInternalMic(
53 true,
54 kInternalMicId,
55 "Fake Mic",
56 "INTERNAL_MIC",
57 "Internal Mic",
58 false,
59 0
60 );
61
62 const AudioNode kUSBMic(
63 true,
64 kUSBMicId,
65 "Fake USB Mic",
66 "USB",
67 "USB Microphone",
68 false,
69 0
70 );
71
72 const AudioNode kOtherTypeOutput(
73 false,
74 kOtherTypeOutputId,
75 "Output Device",
76 "SOME_OTHER_TYPE",
77 "Other Type Output Device",
78 false,
79 0
80 );
81
82 const AudioNode kOtherTypeInput(
83 true,
84 kOtherTypeInputId,
85 "Input Device",
86 "SOME_OTHER_TYPE",
87 "Other Type Input Device",
88 false,
89 0
90 );
91
92 const AudioNode kBluetoothHeadset (
93 false,
94 kBluetoothHeadsetId,
95 "Bluetooth Headset",
96 "BLUETOOTH",
97 "Bluetooth Headset 1",
98 false,
99 0
100 );
101
102 const AudioNode kHDMIOutput (
103 false,
104 kHDMIOutputId,
105 "HDMI output",
106 "HDMI",
107 "HDMI output",
108 false,
109 0
110 );
111
112 const AudioNode kUSBHeadphone1 (
113 false,
114 kUSBHeadphoneId1,
115 "USB Headphone",
116 "USB",
117 "USB Headphone 1",
118 false,
119 0
120 );
121
122 const AudioNode kUSBHeadphone2 (
123 false,
124 kUSBHeadphoneId2,
125 "USB Headphone",
126 "USB",
127 "USB Headphone 1",
128 false,
129 0
130 );
131
132
133 class TestObserver : public chromeos::CrasAudioHandler::AudioObserver {
134 public:
135 TestObserver() : active_output_node_changed_count_(0),
136 active_input_node_changed_count_(0),
137 audio_nodes_changed_count_(0),
138 output_mute_changed_count_(0),
139 input_mute_changed_count_(0),
140 output_volume_changed_count_(0),
141 input_gain_changed_count_(0) {
142 }
143
144 int active_output_node_changed_count() const {
145 return active_output_node_changed_count_;
146 }
147
148 int active_input_node_changed_count() const {
149 return active_input_node_changed_count_;
150 }
151
152 int audio_nodes_changed_count() const {
153 return audio_nodes_changed_count_;
154 }
155
156 int output_mute_changed_count() const {
157 return output_mute_changed_count_;
158 }
159
160 int input_mute_changed_count() const {
161 return input_mute_changed_count_;
162 }
163
164 int output_volume_changed_count() const {
165 return output_volume_changed_count_;
166 }
167
168 int input_gain_changed_count() const {
169 return input_gain_changed_count_;
170 }
171
172 virtual ~TestObserver() {}
173
174 protected:
175 // chromeos::CrasAudioHandler::AudioObserver overrides.
176 virtual void OnActiveOutputNodeChanged() OVERRIDE {
177 ++active_output_node_changed_count_;
178 }
179
180 virtual void OnActiveInputNodeChanged() OVERRIDE {
181 ++active_input_node_changed_count_;
182 }
183
184 virtual void OnAudioNodesChanged() OVERRIDE {
185 ++audio_nodes_changed_count_;
186 }
187
188 virtual void OnOutputMuteChanged() OVERRIDE {
189 ++output_mute_changed_count_;
190 }
191
192 virtual void OnInputMuteChanged() OVERRIDE {
193 ++input_mute_changed_count_;
194 }
195
196 virtual void OnOutputVolumeChanged() OVERRIDE {
197 ++output_volume_changed_count_;
198 }
199
200 virtual void OnInputGainChanged() OVERRIDE {
201 ++input_gain_changed_count_;
202 }
203
204 private:
205 int active_output_node_changed_count_;
206 int active_input_node_changed_count_;
207 int audio_nodes_changed_count_;
208 int output_mute_changed_count_;
209 int input_mute_changed_count_;
210 int output_volume_changed_count_;
211 int input_gain_changed_count_;
212
213 DISALLOW_COPY_AND_ASSIGN(TestObserver);
214 };
215
216 class CrasAudioHandlerTest : public testing::Test {
217 public:
218 CrasAudioHandlerTest() : cras_audio_handler_(NULL),
219 cras_audio_client_stub_(NULL) {
220 }
221 virtual ~CrasAudioHandlerTest() {}
222
223 virtual void SetUp() OVERRIDE {
224 }
225
226 virtual void TearDown() OVERRIDE {
227 cras_audio_handler_->RemoveAudioObserver(test_observer_.get());
228 test_observer_.reset();
229 CrasAudioHandler::Shutdown();
230 audio_pref_handler_ = NULL;
231 DBusThreadManager::Shutdown();
232 }
233
234 void SetUpCrasAudioHandler(const AudioNodeList& audio_nodes) {
235 DBusThreadManager::InitializeWithStub();
236 cras_audio_client_stub_ = static_cast<CrasAudioClientStubImpl*>(
237 DBusThreadManager::Get()->GetCrasAudioClient());
238 cras_audio_client_stub_->SetAudioDevices(audio_nodes);
239 audio_pref_handler_ = new AudioDevicesPrefHandlerStub();
240 CrasAudioHandler::Initialize(audio_pref_handler_);
241 cras_audio_handler_ = CrasAudioHandler::Get();
242 test_observer_.reset(new TestObserver);
243 cras_audio_handler_->AddAudioObserver(test_observer_.get());
244 message_loop_.RunUntilIdle();
245 }
246
247 void ChangeAudioNodes(const AudioNodeList& audio_nodes) {
248 cras_audio_client_stub_->ChangeAudioNodes(audio_nodes);
249 message_loop_.RunUntilIdle();
250 }
251
252 protected:
253 base::MessageLoopForUI message_loop_;
254 CrasAudioHandler* cras_audio_handler_; // Not owned.
255 CrasAudioClientStubImpl* cras_audio_client_stub_; // Not owned.
256 scoped_ptr<TestObserver> test_observer_;
257 scoped_refptr<AudioDevicesPrefHandlerStub> audio_pref_handler_;
258
259 private:
260 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandlerTest);
261 };
262
263 TEST_F(CrasAudioHandlerTest, InitializeWithOnlyDefaultAudioDevices) {
264 AudioNodeList audio_nodes;
265 audio_nodes.push_back(kInternalSpeaker);
266 audio_nodes.push_back(kInternalMic);
267 SetUpCrasAudioHandler(audio_nodes);
268
269 // Verify the audio devices size.
270 AudioDeviceList audio_devices;
271 cras_audio_handler_->GetAudioDevices(&audio_devices);
272 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
273
274 // Verify the internal speaker has been selected as the active output.
275 AudioDevice active_output;
276 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
277 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
278 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
279 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
280
281 // Ensure the internal microphone has been selected as the active input.
282 AudioDevice active_input;
283 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode());
284 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
285 }
286
287 TEST_F(CrasAudioHandlerTest, InitializeWithAlternativeAudioDevices) {
288 AudioNodeList audio_nodes;
289 audio_nodes.push_back(kInternalSpeaker);
290 audio_nodes.push_back(kHeadphone);
291 audio_nodes.push_back(kInternalMic);
292 audio_nodes.push_back(kUSBMic);
293 SetUpCrasAudioHandler(audio_nodes);
294
295 // Verify the audio devices size.
296 AudioDeviceList audio_devices;
297 cras_audio_handler_->GetAudioDevices(&audio_devices);
298 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
299
300 // Verify the headphone has been selected as the active output.
301 AudioDevice active_output;
302 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
303 EXPECT_EQ(kHeadphone.id, active_output.id);
304 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
305 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
306
307 // Ensure the USB microphone has been selected as the active input.
308 AudioDevice active_input;
309 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetActiveInputNode());
310 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
311 }
312
313 TEST_F(CrasAudioHandlerTest, SwitchActiveOutputDevice) {
314 AudioNodeList audio_nodes;
315 audio_nodes.push_back(kInternalSpeaker);
316 audio_nodes.push_back(kHeadphone);
317 SetUpCrasAudioHandler(audio_nodes);
318 AudioDeviceList audio_devices;
319 cras_audio_handler_->GetAudioDevices(&audio_devices);
320 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
321
322 // Verify the initial active output device is headphone.
323 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
324 AudioDevice active_output;
325 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
326 EXPECT_EQ(kHeadphone.id, active_output.id);
327 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
328
329 // Switch the active output to internal speaker.
330 AudioDevice internal_speaker(kInternalSpeaker);
331 cras_audio_handler_->SwitchToDevice(internal_speaker);
332
333 // Verify the active output is switched to internal speaker, and the
334 // ActiveOutputNodeChanged event is fired.
335 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
336 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
337 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
338 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
339 }
340
341 TEST_F(CrasAudioHandlerTest, SwitchActiveInputDevice) {
342 AudioNodeList audio_nodes;
343 audio_nodes.push_back(kInternalMic);
344 audio_nodes.push_back(kUSBMic);
345 SetUpCrasAudioHandler(audio_nodes);
346 AudioDeviceList audio_devices;
347 cras_audio_handler_->GetAudioDevices(&audio_devices);
348 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
349
350 // Verify the initial active input device is USB mic.
351 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
352 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetActiveInputNode());
353
354 // Switch the active input to internal mic.
355 AudioDevice internal_mic(kInternalMic);
356 cras_audio_handler_->SwitchToDevice(internal_mic);
357
358 // Verify the active output is switched to internal speaker, and the active
359 // ActiveInputNodeChanged event is fired.
360 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
361 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode());
362 }
363
364 TEST_F(CrasAudioHandlerTest, PlugHeadphone) {
365 // Set up initial audio devices, only with internal speaker.
366 AudioNodeList audio_nodes;
367 audio_nodes.push_back(kInternalSpeaker);
368 SetUpCrasAudioHandler(audio_nodes);
369 const size_t init_nodes_size = audio_nodes.size();
370
371 // Verify the audio devices size.
372 AudioDeviceList audio_devices;
373 cras_audio_handler_->GetAudioDevices(&audio_devices);
374 EXPECT_EQ(init_nodes_size, audio_devices.size());
375 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
376
377 // Verify the internal speaker has been selected as the active output.
378 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
379 AudioDevice active_output;
380 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
381 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
382 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
383 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
384
385 // Plug the headphone.
386 audio_nodes.push_back(kHeadphone);
387 ChangeAudioNodes(audio_nodes);
388
389 // Verify the AudioNodesChanged event is fired and new audio device is added.
390 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
391 cras_audio_handler_->GetAudioDevices(&audio_devices);
392 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
393
394 // Verify the active output device is switched to headphone and
395 // ActiveOutputChanged event is fired.
396 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
397 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
398 EXPECT_EQ(kHeadphone.id, active_output.id);
399 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
400 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
401 }
402
403 TEST_F(CrasAudioHandlerTest, UnplugHeadphone) {
404 // Set up initial audio devices, with internal speaker and headphone.
405 AudioNodeList audio_nodes;
406 audio_nodes.push_back(kInternalSpeaker);
407 audio_nodes.push_back(kHeadphone);
408 SetUpCrasAudioHandler(audio_nodes);
409 const size_t init_nodes_size = audio_nodes.size();
410
411 // Verify the audio devices size.
412 AudioDeviceList audio_devices;
413 cras_audio_handler_->GetAudioDevices(&audio_devices);
414 EXPECT_EQ(init_nodes_size, audio_devices.size());
415 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
416
417 // Verify the headphone has been selected as the active output.
418 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
419 AudioDevice active_output;
420 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
421 EXPECT_EQ(kHeadphone.id, active_output.id);
422 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
423 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
424
425 // Unplug the headphone.
426 audio_nodes.clear();
427 audio_nodes.push_back(kInternalSpeaker);
428 ChangeAudioNodes(audio_nodes);
429
430 // Verify the AudioNodesChanged event is fired and one audio device is
431 // removed.
432 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
433 cras_audio_handler_->GetAudioDevices(&audio_devices);
434 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
435
436 // Verify the active output device is switched to internal speaker and
437 // ActiveOutputChanged event is fired.
438 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
439 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
440 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
441 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
442 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
443 }
444
445 TEST_F(CrasAudioHandlerTest, InitializeWithBluetoothHeadset) {
446 AudioNodeList audio_nodes;
447 audio_nodes.push_back(kInternalSpeaker);
448 audio_nodes.push_back(kBluetoothHeadset);
449 SetUpCrasAudioHandler(audio_nodes);
450
451 // Verify the audio devices size.
452 AudioDeviceList audio_devices;
453 cras_audio_handler_->GetAudioDevices(&audio_devices);
454 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
455 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
456
457 // Verify the bluetooth headset has been selected as the active output.
458 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
459 AudioDevice active_output;
460 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
461 EXPECT_EQ(kBluetoothHeadset.id, active_output.id);
462 EXPECT_EQ(kBluetoothHeadset.id, cras_audio_handler_->GetActiveOutputNode());
463 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
464 }
465
466 TEST_F(CrasAudioHandlerTest, ConnectAndDisconnectBluetoothHeadset) {
467 // Initialize with internal speaker and headphone.
468 AudioNodeList audio_nodes;
469 audio_nodes.push_back(kInternalSpeaker);
470 audio_nodes.push_back(kHeadphone);
471 SetUpCrasAudioHandler(audio_nodes);
472 const size_t init_nodes_size = audio_nodes.size();
473
474 // Verify the audio devices size.
475 AudioDeviceList audio_devices;
476 cras_audio_handler_->GetAudioDevices(&audio_devices);
477 EXPECT_EQ(init_nodes_size, audio_devices.size());
478 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
479
480 // Verify the headphone is selected as the active output initially.
481 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
482 AudioDevice active_output;
483 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
484 EXPECT_EQ(kHeadphone.id, active_output.id);
485 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
486 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
487
488 // Connect to bluetooth headset. Since it is plugged in later than
489 // headphone, active output should be switched to it.
490 audio_nodes.clear();
491 audio_nodes.push_back(kInternalSpeaker);
492 AudioNode headphone(kHeadphone);
493 headphone.plugged_time = 80000000;
494 headphone.active = true;
495 audio_nodes.push_back(headphone);
496 AudioNode bluetooth_headset(kBluetoothHeadset);
497 bluetooth_headset.plugged_time = 90000000;
498 audio_nodes.push_back(bluetooth_headset);
499 ChangeAudioNodes(audio_nodes);
500
501 // Verify the AudioNodesChanged event is fired and new audio device is added.
502 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
503 cras_audio_handler_->GetAudioDevices(&audio_devices);
504 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
505
506 // Verify the active output device is switched to bluetooth headset, and
507 // ActiveOutputChanged event is fired.
508 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
509 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
510 EXPECT_EQ(kBluetoothHeadset.id, active_output.id);
511 EXPECT_EQ(kBluetoothHeadset.id, cras_audio_handler_->GetActiveOutputNode());
512 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
513
514 // Disconnect bluetooth headset.
515 audio_nodes.clear();
516 audio_nodes.push_back(kInternalSpeaker);
517 audio_nodes.push_back(headphone);
518 ChangeAudioNodes(audio_nodes);
519
520 // Verify the AudioNodesChanged event is fired and one audio device is
521 // removed.
522 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
523 cras_audio_handler_->GetAudioDevices(&audio_devices);
524 EXPECT_EQ(init_nodes_size, audio_devices.size());
525
526 // Verify the active output device is switched to headphone, and
527 // ActiveOutputChanged event is fired.
528 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
529 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
530 EXPECT_EQ(kHeadphone.id, active_output.id);
531 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
532 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
533 }
534
535 TEST_F(CrasAudioHandlerTest, InitializeWithHDMIOutput) {
536 AudioNodeList audio_nodes;
537 audio_nodes.push_back(kInternalSpeaker);
538 audio_nodes.push_back(kHDMIOutput);
539 SetUpCrasAudioHandler(audio_nodes);
540
541 // Verify the audio devices size.
542 AudioDeviceList audio_devices;
543 cras_audio_handler_->GetAudioDevices(&audio_devices);
544 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
545 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
546
547 // Verify the HDMI device has been selected as the active output.
548 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
549 AudioDevice active_output;
550 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
551 EXPECT_EQ(kHDMIOutput.id, active_output.id);
552 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetActiveOutputNode());
553 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
554 }
555
556 TEST_F(CrasAudioHandlerTest, ConnectAndDisconnectHDMIOutput) {
557 // Initialize with internal speaker.
558 AudioNodeList audio_nodes;
559 audio_nodes.push_back(kInternalSpeaker);
560 SetUpCrasAudioHandler(audio_nodes);
561 const size_t init_nodes_size = audio_nodes.size();
562
563 // Verify the audio devices size.
564 AudioDeviceList audio_devices;
565 cras_audio_handler_->GetAudioDevices(&audio_devices);
566 EXPECT_EQ(init_nodes_size, audio_devices.size());
567 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
568
569 // Verify the internal speaker is selected as the active output initially.
570 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
571 AudioDevice active_output;
572 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
573 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
574 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
575 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
576
577 // Connect to HDMI output.
578 audio_nodes.clear();
579 AudioNode internal_speaker(kInternalSpeaker);
580 internal_speaker.active = true;
581 internal_speaker.plugged_time = 80000000;
582 audio_nodes.push_back(internal_speaker);
583 AudioNode hdmi(kHDMIOutput);
584 hdmi.plugged_time = 90000000;
585 audio_nodes.push_back(hdmi);
586 ChangeAudioNodes(audio_nodes);
587
588 // Verify the AudioNodesChanged event is fired and new audio device is added.
589 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
590 cras_audio_handler_->GetAudioDevices(&audio_devices);
591 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
592
593 // Verify the active output device is switched to hdmi output, and
594 // ActiveOutputChanged event is fired.
595 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
596 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
597 EXPECT_EQ(kHDMIOutput.id, active_output.id);
598 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetActiveOutputNode());
599 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
600
601 // Disconnect hdmi headset.
602 audio_nodes.clear();
603 audio_nodes.push_back(kInternalSpeaker);
604 ChangeAudioNodes(audio_nodes);
605
606 // Verify the AudioNodesChanged event is fired and one audio device is
607 // removed.
608 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
609 cras_audio_handler_->GetAudioDevices(&audio_devices);
610 EXPECT_EQ(init_nodes_size, audio_devices.size());
611
612 // Verify the active output device is switched to internal speaker, and
613 // ActiveOutputChanged event is fired.
614 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
615 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
616 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
617 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
618 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
619 }
620
621 TEST_F(CrasAudioHandlerTest, HandleHeadphoneAndHDMIOutput) {
622 // Initialize with internal speaker, headphone and HDMI output.
623 AudioNodeList audio_nodes;
624 audio_nodes.push_back(kInternalSpeaker);
625 audio_nodes.push_back(kHeadphone);
626 audio_nodes.push_back(kHDMIOutput);
627 SetUpCrasAudioHandler(audio_nodes);
628 const size_t init_nodes_size = audio_nodes.size();
629
630 // Verify the audio devices size.
631 AudioDeviceList audio_devices;
632 cras_audio_handler_->GetAudioDevices(&audio_devices);
633 EXPECT_EQ(init_nodes_size, audio_devices.size());
634 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
635
636 // Verify the headphone is selected as the active output initially.
637 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
638 AudioDevice active_output;
639 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
640 EXPECT_EQ(kHeadphone.id, active_output.id);
641 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
642 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
643
644 // Disconnect HDMI output.
645 audio_nodes.clear();
646 audio_nodes.push_back(kInternalSpeaker);
647 audio_nodes.push_back(kHDMIOutput);
648 ChangeAudioNodes(audio_nodes);
649
650 // Verify the AudioNodesChanged event is fired and one audio device is
651 // removed.
652 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
653 cras_audio_handler_->GetAudioDevices(&audio_devices);
654 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
655
656 // Verify the active output device is switched to HDMI output, and
657 // ActiveOutputChanged event is fired.
658 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
659 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
660 EXPECT_EQ(kHDMIOutput.id, active_output.id);
661 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetActiveOutputNode());
662 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
663 }
664
665 TEST_F(CrasAudioHandlerTest, InitializeWithUSBHeadphone) {
666 AudioNodeList audio_nodes;
667 audio_nodes.push_back(kInternalSpeaker);
668 audio_nodes.push_back(kUSBHeadphone1);
669 SetUpCrasAudioHandler(audio_nodes);
670
671 // Verify the audio devices size.
672 AudioDeviceList audio_devices;
673 cras_audio_handler_->GetAudioDevices(&audio_devices);
674 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
675 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
676
677 // Verify the usb headphone has been selected as the active output.
678 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
679 AudioDevice active_output;
680 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
681 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
682 EXPECT_EQ(kUSBHeadphone1.id, cras_audio_handler_->GetActiveOutputNode());
683 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
684 }
685
686 TEST_F(CrasAudioHandlerTest, PlugAndUnplugUSBHeadphone) {
687 // Initialize with internal speaker.
688 AudioNodeList audio_nodes;
689 audio_nodes.push_back(kInternalSpeaker);
690 SetUpCrasAudioHandler(audio_nodes);
691 const size_t init_nodes_size = audio_nodes.size();
692
693 // Verify the audio devices size.
694 AudioDeviceList audio_devices;
695 cras_audio_handler_->GetAudioDevices(&audio_devices);
696 EXPECT_EQ(init_nodes_size, audio_devices.size());
697 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
698
699 // Verify the internal speaker is selected as the active output initially.
700 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
701 AudioDevice active_output;
702 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
703 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
704 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
705 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
706
707 // Plug in usb headphone
708 audio_nodes.clear();
709 AudioNode internal_speaker(kInternalSpeaker);
710 internal_speaker.active = true;
711 internal_speaker.plugged_time = 80000000;
712 audio_nodes.push_back(internal_speaker);
713 AudioNode usb_headphone(kUSBHeadphone1);
714 usb_headphone.plugged_time = 90000000;
715 audio_nodes.push_back(usb_headphone);
716 ChangeAudioNodes(audio_nodes);
717
718 // Verify the AudioNodesChanged event is fired and new audio device is added.
719 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
720 cras_audio_handler_->GetAudioDevices(&audio_devices);
721 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
722
723 // Verify the active output device is switched to usb headphone, and
724 // ActiveOutputChanged event is fired.
725 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
726 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
727 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
728 EXPECT_EQ(kUSBHeadphone1.id, cras_audio_handler_->GetActiveOutputNode());
729 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
730
731 // Unplug usb headphone.
732 audio_nodes.clear();
733 audio_nodes.push_back(kInternalSpeaker);
734 ChangeAudioNodes(audio_nodes);
735
736 // Verify the AudioNodesChanged event is fired and one audio device is
737 // removed.
738 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
739 cras_audio_handler_->GetAudioDevices(&audio_devices);
740 EXPECT_EQ(init_nodes_size, audio_devices.size());
741
742 // Verify the active output device is switched to internal speaker, and
743 // ActiveOutputChanged event is fired.
744 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
745 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
746 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
747 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
748 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
749 }
750
751 TEST_F(CrasAudioHandlerTest, HandleMultipleUSBHeadphones) {
752 // Initialize with internal speaker and one usb headphone.
753 AudioNodeList audio_nodes;
754 audio_nodes.push_back(kInternalSpeaker);
755 audio_nodes.push_back(kUSBHeadphone1);
756 SetUpCrasAudioHandler(audio_nodes);
757 const size_t init_nodes_size = audio_nodes.size();
758
759 // Verify the audio devices size.
760 AudioDeviceList audio_devices;
761 cras_audio_handler_->GetAudioDevices(&audio_devices);
762 EXPECT_EQ(init_nodes_size, audio_devices.size());
763 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
764
765 // Verify the usb headphone is selected as the active output initially.
766 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
767 AudioDevice active_output;
768 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
769 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
770 EXPECT_EQ(kUSBHeadphone1.id, cras_audio_handler_->GetActiveOutputNode());
771 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
772
773 // Plug in another usb headphone.
774 audio_nodes.clear();
775 audio_nodes.push_back(kInternalSpeaker);
776 AudioNode usb_headphone_1(kUSBHeadphone1);
777 usb_headphone_1.active = true;
778 usb_headphone_1.plugged_time = 80000000;
779 audio_nodes.push_back(usb_headphone_1);
780 AudioNode usb_headphone_2(kUSBHeadphone2);
781 usb_headphone_2.plugged_time = 90000000;
782 audio_nodes.push_back(usb_headphone_2);
783 ChangeAudioNodes(audio_nodes);
784
785 // Verify the AudioNodesChanged event is fired and new audio device is added.
786 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
787 cras_audio_handler_->GetAudioDevices(&audio_devices);
788 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
789
790 // Verify the active output device is switched to the 2nd usb headphone, which
791 // is plugged later, and ActiveOutputChanged event is fired.
792 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
793 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
794 EXPECT_EQ(kUSBHeadphone2.id, active_output.id);
795 EXPECT_EQ(kUSBHeadphone2.id, cras_audio_handler_->GetActiveOutputNode());
796 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
797
798 // Unplug the 2nd usb headphone.
799 audio_nodes.clear();
800 audio_nodes.push_back(kInternalSpeaker);
801 audio_nodes.push_back(kUSBHeadphone1);
802 ChangeAudioNodes(audio_nodes);
803
804 // Verify the AudioNodesChanged event is fired and one audio device is
805 // removed.
806 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
807 cras_audio_handler_->GetAudioDevices(&audio_devices);
808 EXPECT_EQ(init_nodes_size, audio_devices.size());
809
810 // Verify the active output device is switched to the first usb headphone, and
811 // ActiveOutputChanged event is fired.
812 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
813 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
814 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
815 EXPECT_EQ(kUSBHeadphone1.id, cras_audio_handler_->GetActiveOutputNode());
816 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
817 }
818
819 TEST_F(CrasAudioHandlerTest, PlugUSBMic) {
820 // Set up initial audio devices, only with internal mic.
821 AudioNodeList audio_nodes;
822 audio_nodes.push_back(kInternalMic);
823 SetUpCrasAudioHandler(audio_nodes);
824 const size_t init_nodes_size = audio_nodes.size();
825
826 // Verify the audio devices size.
827 AudioDeviceList audio_devices;
828 cras_audio_handler_->GetAudioDevices(&audio_devices);
829 EXPECT_EQ(init_nodes_size, audio_devices.size());
830 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
831
832 // Verify the internal mic is selected as the active output.
833 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
834 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode());
835 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
836
837 // Plug the USB Mic.
838 audio_nodes.push_back(kUSBMic);
839 ChangeAudioNodes(audio_nodes);
840
841 // Verify the AudioNodesChanged event is fired and new audio device is added.
842 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
843 cras_audio_handler_->GetAudioDevices(&audio_devices);
844 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
845
846 // Verify the active input device is switched to USB mic and
847 // and ActiveInputChanged event is fired.
848 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
849 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetActiveInputNode());
850 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
851 }
852
853 TEST_F(CrasAudioHandlerTest, UnplugUSBMic) {
854 // Set up initial audio devices, with internal mic and USB Mic.
855 AudioNodeList audio_nodes;
856 audio_nodes.push_back(kInternalMic);
857 audio_nodes.push_back(kUSBMic);
858 SetUpCrasAudioHandler(audio_nodes);
859 const size_t init_nodes_size = audio_nodes.size();
860
861 // Verify the audio devices size.
862 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
863 AudioDeviceList audio_devices;
864 cras_audio_handler_->GetAudioDevices(&audio_devices);
865 EXPECT_EQ(init_nodes_size, audio_devices.size());
866
867 // Verify the USB mic is selected as the active output.
868 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
869 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetActiveInputNode());
870 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
871
872 // Unplug the USB Mic.
873 audio_nodes.clear();
874 audio_nodes.push_back(kInternalMic);
875 ChangeAudioNodes(audio_nodes);
876
877 // Verify the AudioNodesChanged event is fired, and one audio device is
878 // removed.
879 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
880 cras_audio_handler_->GetAudioDevices(&audio_devices);
881 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
882
883 // Verify the active input device is switched to internal mic, and
884 // and ActiveInputChanged event is fired.
885 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
886 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode());
887 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
888 }
889
890 TEST_F(CrasAudioHandlerTest, SetOutputMute) {
891 AudioNodeList audio_nodes;
892 audio_nodes.push_back(kInternalSpeaker);
893 SetUpCrasAudioHandler(audio_nodes);
894 EXPECT_EQ(0, test_observer_->output_mute_changed_count());
895
896 // Mute the device.
897 cras_audio_handler_->SetOutputMute(true);
898
899 // Verify the output is muted, OnOutputMuteChanged event is fired,
900 // and mute value is saved in the preferences.
901 EXPECT_TRUE(cras_audio_handler_->IsOutputMuted());
902 EXPECT_EQ(1, test_observer_->output_mute_changed_count());
903 AudioDevice speaker(kInternalSpeaker);
904 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(speaker));
905
906 // Unmute the device.
907 cras_audio_handler_->SetOutputMute(false);
908
909 // Verify the output is unmuted, OnOutputMuteChanged event is fired,
910 // and mute value is saved in the preferences.
911 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
912 EXPECT_EQ(2, test_observer_->output_mute_changed_count());
913 EXPECT_FALSE(audio_pref_handler_->GetMuteValue(speaker));
914 }
915
916 TEST_F(CrasAudioHandlerTest, SetInputMute) {
917 AudioNodeList audio_nodes;
918 audio_nodes.push_back(kInternalMic);
919 SetUpCrasAudioHandler(audio_nodes);
920 EXPECT_EQ(0, test_observer_->input_mute_changed_count());
921
922 // Mute the device.
923 cras_audio_handler_->SetInputMute(true);
924
925 // Verify the input is muted, OnInputMuteChanged event is fired,
926 // and mute value is saved in the preferences.
927 EXPECT_TRUE(cras_audio_handler_->IsInputMuted());
928 EXPECT_EQ(1, test_observer_->input_mute_changed_count());
929 AudioDevice internal_mic(kInternalMic);
930 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(internal_mic));
931
932 // Unmute the device.
933 cras_audio_handler_->SetInputMute(false);
934
935 // Verify the input is unmuted, OnInputMuteChanged event is fired,
936 // and mute value is saved in the preferences.
937 EXPECT_FALSE(cras_audio_handler_->IsInputMuted());
938 EXPECT_EQ(2, test_observer_->input_mute_changed_count());
939 EXPECT_FALSE(audio_pref_handler_->GetMuteValue(internal_mic));
940 }
941
942 TEST_F(CrasAudioHandlerTest, SetOutputVolumePercent) {
943 AudioNodeList audio_nodes;
944 audio_nodes.push_back(kInternalSpeaker);
945 SetUpCrasAudioHandler(audio_nodes);
946 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
947
948 cras_audio_handler_->SetOutputVolumePercent(60);
949
950 // Verify the output volume is changed to the designated value,
951 // OnOutputVolumeChanged event is fired, and the device volume value
952 // is saved the preferences.
953 const int kVolume = 60;
954 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent());
955 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
956 AudioDevice device;
957 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&device));
958 EXPECT_EQ(device.id, kInternalSpeaker.id);
959 EXPECT_EQ(kVolume, audio_pref_handler_->GetVolumeGainValue(device));
960 }
961
962 TEST_F(CrasAudioHandlerTest, SetInputGainPercent) {
963 AudioNodeList audio_nodes;
964 audio_nodes.push_back(kInternalMic);
965 SetUpCrasAudioHandler(audio_nodes);
966 EXPECT_EQ(0, test_observer_->input_gain_changed_count());
967
968 cras_audio_handler_->SetInputGainPercent(60);
969
970 // Verify the input gain changed to the designated value,
971 // OnInputGainChanged event is fired, and the device gain value
972 // is saved in the preferences.
973 const int kGain = 60;
974 EXPECT_EQ(kGain, cras_audio_handler_->GetInputGainPercent());
975 EXPECT_EQ(1, test_observer_->input_gain_changed_count());
976 AudioDevice internal_mic(kInternalMic);
977 EXPECT_EQ(kGain, audio_pref_handler_->GetVolumeGainValue(internal_mic));
978 }
979
980 TEST_F(CrasAudioHandlerTest, SetMuteForDevice) {
981 AudioNodeList audio_nodes;
982 audio_nodes.push_back(kInternalSpeaker);
983 audio_nodes.push_back(kHeadphone);
984 audio_nodes.push_back(kInternalMic);
985 audio_nodes.push_back(kUSBMic);
986 SetUpCrasAudioHandler(audio_nodes);
987
988 // Mute the active output device.
989 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
990 cras_audio_handler_->SetMuteForDevice(kHeadphone.id, true);
991
992 // Verify the headphone is muted and mute value is saved in the preferences.
993 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kHeadphone.id));
994 AudioDevice headphone(kHeadphone);
995 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(headphone));
996
997 // Mute the non-active output device.
998 cras_audio_handler_->SetMuteForDevice(kInternalSpeaker.id, true);
999
1000 // Verify the internal speaker is muted and mute value is saved in the
1001 // preferences.
1002 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kInternalSpeaker.id));
1003 AudioDevice internal_speaker(kInternalSpeaker);
1004 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(internal_speaker));
1005
1006 // Mute the active input device.
1007 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetActiveInputNode());
1008 cras_audio_handler_->SetMuteForDevice(kUSBMic.id, true);
1009
1010 // Verify the USB Mic is muted and mute state is saved in the preferences.
1011 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kUSBMic.id));
1012 AudioDevice usb_mic(kUSBMic);
1013 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(usb_mic));
1014
1015 // Mute the non-active input device.
1016 cras_audio_handler_->SetMuteForDevice(kInternalMic.id, true);
1017
1018 // Verify the internal mic is muted and mute value is saved in the
1019 // preferences.
1020 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kInternalMic.id));
1021 AudioDevice internal_mic(kInternalMic);
1022 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(internal_mic));
1023 }
1024
1025 TEST_F(CrasAudioHandlerTest, SetVolumeGainPercentForDevice) {
1026 AudioNodeList audio_nodes;
1027 audio_nodes.push_back(kInternalSpeaker);
1028 audio_nodes.push_back(kHeadphone);
1029 audio_nodes.push_back(kInternalMic);
1030 audio_nodes.push_back(kUSBMic);
1031 SetUpCrasAudioHandler(audio_nodes);
1032
1033 // Set volume percent for active output device.
1034 const int kHeadphoneVolume = 30;
1035 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
1036 cras_audio_handler_->SetVolumeGainPercentForDevice(kHeadphone.id,
1037 kHeadphoneVolume);
1038
1039 // Verify the volume percent of headphone is set, and saved in preferences.
1040 EXPECT_EQ(kHeadphoneVolume,
1041 cras_audio_handler_->GetOutputVolumePercentForDevice(
1042 kHeadphone.id));
1043 AudioDevice headphone(kHeadphone);
1044 EXPECT_EQ(kHeadphoneVolume,
1045 audio_pref_handler_->GetVolumeGainValue(headphone));
1046
1047 // Set volume percent for non-active output device.
1048 const int kSpeakerVolume = 60;
1049 cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalSpeaker.id,
1050 kSpeakerVolume);
1051
1052 // Verify the volume percent of speaker is set, and saved in preferences.
1053 EXPECT_EQ(kSpeakerVolume,
1054 cras_audio_handler_->GetOutputVolumePercentForDevice(
1055 kInternalSpeaker.id));
1056 AudioDevice speaker(kInternalSpeaker);
1057 EXPECT_EQ(kSpeakerVolume,
1058 audio_pref_handler_->GetVolumeGainValue(speaker));
1059
1060 // Set gain percent for active input device.
1061 const int kUSBMicGain = 30;
1062 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetActiveInputNode());
1063 cras_audio_handler_->SetVolumeGainPercentForDevice(kUSBMic.id,
1064 kUSBMicGain);
1065
1066 // Verify the gain percent of USB mic is set, and saved in preferences.
1067 EXPECT_EQ(kUSBMicGain,
1068 cras_audio_handler_->GetOutputVolumePercentForDevice(kUSBMic.id));
1069 AudioDevice usb_mic(kHeadphone);
1070 EXPECT_EQ(kUSBMicGain,
1071 audio_pref_handler_->GetVolumeGainValue(usb_mic));
1072
1073 // Set gain percent for non-active input device.
1074 const int kInternalMicGain = 60;
1075 cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalMic.id,
1076 kInternalMicGain);
1077
1078 // Verify the gain percent of internal mic is set, and saved in preferences.
1079 EXPECT_EQ(kInternalMicGain,
1080 cras_audio_handler_->GetOutputVolumePercentForDevice(
1081 kInternalMic.id));
1082 AudioDevice internal_mic(kInternalMic);
1083 EXPECT_EQ(kInternalMicGain,
1084 audio_pref_handler_->GetVolumeGainValue(internal_mic));
1085 }
1086
1087 TEST_F(CrasAudioHandlerTest, HandleOtherDeviceType) {
1088 const size_t kNumValidAudioDevices = 4;
1089 AudioNodeList audio_nodes;
1090 audio_nodes.push_back(kInternalSpeaker);
1091 audio_nodes.push_back(kOtherTypeOutput);
1092 audio_nodes.push_back(kInternalMic);
1093 audio_nodes.push_back(kOtherTypeInput);
1094 SetUpCrasAudioHandler(audio_nodes);
1095
1096 // Verify the audio devices size.
1097 AudioDeviceList audio_devices;
1098 cras_audio_handler_->GetAudioDevices(&audio_devices);
1099 EXPECT_EQ(kNumValidAudioDevices, audio_devices.size());
1100
1101 // Verify the internal speaker has been selected as the active output,
1102 // and the output device with some randown unknown type is handled gracefully.
1103 AudioDevice active_output;
1104 EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
1105 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
1106 EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
1107 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1108
1109 // Ensure the internal microphone has been selected as the active input,
1110 // and the input device with some random unknown type is handled gracefully.
1111 AudioDevice active_input;
1112 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode());
1113 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1114 }
1115
1116 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/audio/cras_audio_handler.cc ('k') | chromeos/audio/mock_cras_audio_handler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698