blob: 72eaaf38411d32874d19d5de6cd5c4dcdf6b102d [file] [log] [blame]
[email protected]ee38bc42013-07-29 21:41:321// 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"
[email protected]ee38bc42013-07-29 21:41:3213#include "chromeos/dbus/dbus_thread_manager.h"
satoruxcca5cc12014-10-27 16:33:5914#include "chromeos/dbus/fake_cras_audio_client.h"
[email protected]ee38bc42013-07-29 21:41:3215#include "testing/gtest/include/gtest/gtest.h"
16
17namespace chromeos {
18
19const uint64 kInternalSpeakerId = 10001;
20const uint64 kHeadphoneId = 10002;
21const uint64 kInternalMicId = 10003;
22const uint64 kUSBMicId = 10004;
23const uint64 kBluetoothHeadsetId = 10005;
24const uint64 kHDMIOutputId = 10006;
25const uint64 kUSBHeadphoneId1 = 10007;
26const uint64 kUSBHeadphoneId2 = 10008;
[email protected]ce631ee2014-01-23 03:37:0627const uint64 kMicJackId = 10009;
[email protected]f9a49d342014-07-29 23:47:2028const uint64 kKeyboardMicId = 10010;
[email protected]ee38bc42013-07-29 21:41:3229const uint64 kOtherTypeOutputId = 90001;
30const uint64 kOtherTypeInputId = 90002;
jennyz2cb9eb12014-11-05 19:21:0531const uint64 kUSBJabraSpeakerOutputId1 = 90003;
32const uint64 kUSBJabraSpeakerOutputId2 = 90004;
33const uint64 kUSBJabraSpeakerInputId1 = 90005;
34const uint64 kUSBJabraSpeakerInputId2 = 90006;
35const uint64 kUSBCameraInputId = 90007;
[email protected]ee38bc42013-07-29 21:41:3236
37const AudioNode kInternalSpeaker(
38 false,
39 kInternalSpeakerId,
40 "Fake Speaker",
41 "INTERNAL_SPEAKER",
42 "Speaker",
43 false,
44 0
45);
46
47const AudioNode kHeadphone(
48 false,
49 kHeadphoneId,
50 "Fake Headphone",
51 "HEADPHONE",
52 "Headphone",
53 false,
54 0
55);
56
57const AudioNode kInternalMic(
58 true,
59 kInternalMicId,
60 "Fake Mic",
61 "INTERNAL_MIC",
62 "Internal Mic",
63 false,
64 0
65);
66
[email protected]ce631ee2014-01-23 03:37:0667const AudioNode kMicJack(
68 true,
69 kMicJackId,
70 "Fake Mic Jack",
71 "MIC",
72 "Mic Jack",
73 false,
74 0
75);
76
[email protected]ee38bc42013-07-29 21:41:3277const AudioNode kUSBMic(
78 true,
79 kUSBMicId,
80 "Fake USB Mic",
81 "USB",
82 "USB Microphone",
83 false,
84 0
85);
86
[email protected]f9a49d342014-07-29 23:47:2087const AudioNode kKeyboardMic(
88 true,
89 kKeyboardMicId,
90 "Fake Keyboard Mic",
91 "KEYBOARD_MIC",
92 "Keyboard Mic",
93 false,
94 0
95);
96
[email protected]ee38bc42013-07-29 21:41:3297const AudioNode kOtherTypeOutput(
98 false,
99 kOtherTypeOutputId,
100 "Output Device",
101 "SOME_OTHER_TYPE",
102 "Other Type Output Device",
103 false,
104 0
105);
106
107const AudioNode kOtherTypeInput(
108 true,
109 kOtherTypeInputId,
110 "Input Device",
111 "SOME_OTHER_TYPE",
112 "Other Type Input Device",
113 false,
114 0
115);
116
jennyz481292c2015-03-21 01:49:29117const AudioNode kBluetoothHeadset(false,
118 kBluetoothHeadsetId,
119 "Bluetooth Headset",
120 "BLUETOOTH",
121 "Bluetooth Headset 1",
122 false,
123 0);
[email protected]ee38bc42013-07-29 21:41:32124
jennyz481292c2015-03-21 01:49:29125const AudioNode kHDMIOutput(false,
126 kHDMIOutputId,
127 "HDMI output",
128 "HDMI",
129 "HDMI output",
130 false,
131 0);
[email protected]ee38bc42013-07-29 21:41:32132
jennyz481292c2015-03-21 01:49:29133const AudioNode kUSBHeadphone1(false,
134 kUSBHeadphoneId1,
135 "USB Headphone",
136 "USB",
137 "USB Headphone 1",
138 false,
139 0);
[email protected]ee38bc42013-07-29 21:41:32140
jennyz481292c2015-03-21 01:49:29141const AudioNode kUSBHeadphone2(false,
142 kUSBHeadphoneId2,
143 "USB Headphone",
144 "USB",
145 "USB Headphone 1",
146 false,
147 0);
[email protected]ee38bc42013-07-29 21:41:32148
jennyz2cb9eb12014-11-05 19:21:05149const AudioNode kUSBJabraSpeakerOutput1(false,
150 kUSBJabraSpeakerOutputId1,
151 "Jabra Speaker 1",
152 "USB",
153 "Jabra Speaker 1",
154 false,
155 0);
156
157const AudioNode kUSBJabraSpeakerOutput2(false,
158 kUSBJabraSpeakerOutputId2,
159 "Jabra Speaker 2",
160 "USB",
161 "Jabra Speaker 2",
162 false,
163 0);
164
165const AudioNode kUSBJabraSpeakerInput1(true,
166 kUSBJabraSpeakerInputId1,
167 "Jabra Speaker 1",
168 "USB",
169 "Jabra Speaker",
170 false,
171 0);
172
173const AudioNode kUSBJabraSpeakerInput2(true,
174 kUSBJabraSpeakerInputId2,
175 "Jabra Speaker 2",
176 "USB",
177 "Jabra Speaker 2",
178 false,
179 0);
180
181const AudioNode kUSBCameraInput(true,
182 kUSBCameraInputId,
183 "USB Camera",
184 "USB",
185 "USB Camera",
186 false,
187 0);
[email protected]ee38bc42013-07-29 21:41:32188
189class TestObserver : public chromeos::CrasAudioHandler::AudioObserver {
190 public:
191 TestObserver() : active_output_node_changed_count_(0),
192 active_input_node_changed_count_(0),
193 audio_nodes_changed_count_(0),
194 output_mute_changed_count_(0),
195 input_mute_changed_count_(0),
196 output_volume_changed_count_(0),
197 input_gain_changed_count_(0) {
198 }
199
200 int active_output_node_changed_count() const {
201 return active_output_node_changed_count_;
202 }
203
jennyz2cb9eb12014-11-05 19:21:05204 void reset_active_output_node_changed_count() {
205 active_output_node_changed_count_ = 0;
206 }
207
[email protected]ee38bc42013-07-29 21:41:32208 int active_input_node_changed_count() const {
209 return active_input_node_changed_count_;
210 }
211
jennyz2cb9eb12014-11-05 19:21:05212 void reset_active_input_node_changed_count() {
mukai307596b62014-11-24 19:56:59213 active_input_node_changed_count_ = 0;
jennyz2cb9eb12014-11-05 19:21:05214 }
215
[email protected]ee38bc42013-07-29 21:41:32216 int audio_nodes_changed_count() const {
217 return audio_nodes_changed_count_;
218 }
219
220 int output_mute_changed_count() const {
221 return output_mute_changed_count_;
222 }
223
224 int input_mute_changed_count() const {
225 return input_mute_changed_count_;
226 }
227
228 int output_volume_changed_count() const {
229 return output_volume_changed_count_;
230 }
231
232 int input_gain_changed_count() const {
233 return input_gain_changed_count_;
234 }
235
dchengae98daa2015-01-21 20:30:49236 ~TestObserver() override {}
[email protected]ee38bc42013-07-29 21:41:32237
238 protected:
239 // chromeos::CrasAudioHandler::AudioObserver overrides.
dchengae98daa2015-01-21 20:30:49240 void OnActiveOutputNodeChanged() override {
[email protected]ee38bc42013-07-29 21:41:32241 ++active_output_node_changed_count_;
242 }
243
dchengae98daa2015-01-21 20:30:49244 void OnActiveInputNodeChanged() override {
[email protected]ee38bc42013-07-29 21:41:32245 ++active_input_node_changed_count_;
246 }
247
dchengae98daa2015-01-21 20:30:49248 void OnAudioNodesChanged() override { ++audio_nodes_changed_count_; }
[email protected]ee38bc42013-07-29 21:41:32249
jennyz170e7fb2015-03-30 23:37:39250 void OnOutputMuteChanged(bool /* mute_on */) override {
251 ++output_mute_changed_count_;
252 }
[email protected]ee38bc42013-07-29 21:41:32253
jennyz170e7fb2015-03-30 23:37:39254 void OnInputMuteChanged(bool /* mute_on */) override {
255 ++input_mute_changed_count_;
256 }
[email protected]ee38bc42013-07-29 21:41:32257
jennyz481292c2015-03-21 01:49:29258 void OnOutputNodeVolumeChanged(uint64 /* node_id */,
259 int /* volume */) override {
260 ++output_volume_changed_count_;
261 }
[email protected]ee38bc42013-07-29 21:41:32262
jennyz481292c2015-03-21 01:49:29263 void OnInputNodeGainChanged(uint64 /* node_id */, int /* gain */) override {
264 ++input_gain_changed_count_;
265 }
[email protected]ee38bc42013-07-29 21:41:32266
267 private:
268 int active_output_node_changed_count_;
269 int active_input_node_changed_count_;
270 int audio_nodes_changed_count_;
271 int output_mute_changed_count_;
272 int input_mute_changed_count_;
273 int output_volume_changed_count_;
274 int input_gain_changed_count_;
275
276 DISALLOW_COPY_AND_ASSIGN(TestObserver);
277};
278
279class CrasAudioHandlerTest : public testing::Test {
280 public:
281 CrasAudioHandlerTest() : cras_audio_handler_(NULL),
satoruxcca5cc12014-10-27 16:33:59282 fake_cras_audio_client_(NULL) {
[email protected]ee38bc42013-07-29 21:41:32283 }
dchengae98daa2015-01-21 20:30:49284 ~CrasAudioHandlerTest() override {}
[email protected]ee38bc42013-07-29 21:41:32285
dchengae98daa2015-01-21 20:30:49286 void SetUp() override {}
[email protected]ee38bc42013-07-29 21:41:32287
dchengae98daa2015-01-21 20:30:49288 void TearDown() override {
[email protected]ee38bc42013-07-29 21:41:32289 cras_audio_handler_->RemoveAudioObserver(test_observer_.get());
290 test_observer_.reset();
291 CrasAudioHandler::Shutdown();
292 audio_pref_handler_ = NULL;
293 DBusThreadManager::Shutdown();
294 }
295
296 void SetUpCrasAudioHandler(const AudioNodeList& audio_nodes) {
zelidrag29fe3382014-08-27 01:44:48297 DBusThreadManager::Initialize();
satoruxcca5cc12014-10-27 16:33:59298 fake_cras_audio_client_ = static_cast<FakeCrasAudioClient*>(
[email protected]ee38bc42013-07-29 21:41:32299 DBusThreadManager::Get()->GetCrasAudioClient());
satoruxcca5cc12014-10-27 16:33:59300 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes);
[email protected]ee38bc42013-07-29 21:41:32301 audio_pref_handler_ = new AudioDevicesPrefHandlerStub();
302 CrasAudioHandler::Initialize(audio_pref_handler_);
303 cras_audio_handler_ = CrasAudioHandler::Get();
304 test_observer_.reset(new TestObserver);
305 cras_audio_handler_->AddAudioObserver(test_observer_.get());
306 message_loop_.RunUntilIdle();
307 }
308
jennyzb47947f2014-09-25 00:42:04309 void SetUpCrasAudioHandlerWithPrimaryActiveNode(
310 const AudioNodeList& audio_nodes,
311 const AudioNode& primary_active_node) {
312 DBusThreadManager::Initialize();
satoruxcca5cc12014-10-27 16:33:59313 fake_cras_audio_client_ = static_cast<FakeCrasAudioClient*>(
jennyzb47947f2014-09-25 00:42:04314 DBusThreadManager::Get()->GetCrasAudioClient());
satoruxcca5cc12014-10-27 16:33:59315 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes);
316 fake_cras_audio_client_->SetActiveOutputNode(primary_active_node.id),
jennyzb47947f2014-09-25 00:42:04317 audio_pref_handler_ = new AudioDevicesPrefHandlerStub();
318 CrasAudioHandler::Initialize(audio_pref_handler_);
319 cras_audio_handler_ = CrasAudioHandler::Get();
320 test_observer_.reset(new TestObserver);
321 cras_audio_handler_->AddAudioObserver(test_observer_.get());
322 message_loop_.RunUntilIdle();
323 }
324
[email protected]ee38bc42013-07-29 21:41:32325 void ChangeAudioNodes(const AudioNodeList& audio_nodes) {
satoruxcca5cc12014-10-27 16:33:59326 fake_cras_audio_client_->SetAudioNodesAndNotifyObserversForTesting(
derat3698adff2014-08-29 16:10:43327 audio_nodes);
[email protected]ee38bc42013-07-29 21:41:32328 message_loop_.RunUntilIdle();
329 }
330
jennyzb47947f2014-09-25 00:42:04331 const AudioDevice* GetDeviceFromId(uint64 id) {
332 return cras_audio_handler_->GetDeviceFromId(id);
333 }
334
jennyz2cb9eb12014-11-05 19:21:05335 int GetActiveDeviceCount() const {
336 int num_active_nodes = 0;
337 AudioDeviceList audio_devices;
338 cras_audio_handler_->GetAudioDevices(&audio_devices);
339 for (size_t i = 0; i < audio_devices.size(); ++i) {
340 if (audio_devices[i].active)
341 ++num_active_nodes;
342 }
343 return num_active_nodes;
344 }
345
[email protected]ee38bc42013-07-29 21:41:32346 protected:
347 base::MessageLoopForUI message_loop_;
348 CrasAudioHandler* cras_audio_handler_; // Not owned.
satoruxcca5cc12014-10-27 16:33:59349 FakeCrasAudioClient* fake_cras_audio_client_; // Not owned.
[email protected]ee38bc42013-07-29 21:41:32350 scoped_ptr<TestObserver> test_observer_;
351 scoped_refptr<AudioDevicesPrefHandlerStub> audio_pref_handler_;
352
353 private:
354 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandlerTest);
355};
356
357TEST_F(CrasAudioHandlerTest, InitializeWithOnlyDefaultAudioDevices) {
358 AudioNodeList audio_nodes;
359 audio_nodes.push_back(kInternalSpeaker);
360 audio_nodes.push_back(kInternalMic);
361 SetUpCrasAudioHandler(audio_nodes);
362
363 // Verify the audio devices size.
364 AudioDeviceList audio_devices;
365 cras_audio_handler_->GetAudioDevices(&audio_devices);
366 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
367
368 // Verify the internal speaker has been selected as the active output.
369 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04370 EXPECT_TRUE(
371 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32372 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04373 EXPECT_EQ(kInternalSpeaker.id,
374 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32375 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
376
377 // Ensure the internal microphone has been selected as the active input.
378 AudioDevice active_input;
jennyzb47947f2014-09-25 00:42:04379 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:32380 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
381}
382
383TEST_F(CrasAudioHandlerTest, InitializeWithAlternativeAudioDevices) {
384 AudioNodeList audio_nodes;
385 audio_nodes.push_back(kInternalSpeaker);
386 audio_nodes.push_back(kHeadphone);
387 audio_nodes.push_back(kInternalMic);
388 audio_nodes.push_back(kUSBMic);
389 SetUpCrasAudioHandler(audio_nodes);
390
391 // Verify the audio devices size.
392 AudioDeviceList audio_devices;
393 cras_audio_handler_->GetAudioDevices(&audio_devices);
394 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
395
396 // Verify the headphone has been selected as the active output.
397 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04398 EXPECT_TRUE(
399 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32400 EXPECT_EQ(kHeadphone.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04401 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32402 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
403
404 // Ensure the USB microphone has been selected as the active input.
jennyzb47947f2014-09-25 00:42:04405 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:32406 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
407}
408
[email protected]f9a49d342014-07-29 23:47:20409TEST_F(CrasAudioHandlerTest, InitializeWithKeyboardMic) {
410 AudioNodeList audio_nodes;
411 audio_nodes.push_back(kInternalSpeaker);
412 audio_nodes.push_back(kInternalMic);
413 audio_nodes.push_back(kKeyboardMic);
414 SetUpCrasAudioHandler(audio_nodes);
415
416 // Verify the audio devices size.
417 AudioDeviceList audio_devices;
418 cras_audio_handler_->GetAudioDevices(&audio_devices);
419 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
jennyzb47947f2014-09-25 00:42:04420 EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic());
[email protected]f9a49d342014-07-29 23:47:20421
422 // Verify the internal speaker has been selected as the active output.
423 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04424 EXPECT_TRUE(
425 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]f9a49d342014-07-29 23:47:20426 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04427 EXPECT_EQ(kInternalSpeaker.id,
428 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]f9a49d342014-07-29 23:47:20429 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
430
431 // Ensure the internal microphone has been selected as the active input,
432 // not affected by keyboard mic.
433 AudioDevice active_input;
jennyzb47947f2014-09-25 00:42:04434 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]f9a49d342014-07-29 23:47:20435 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
jennyzb47947f2014-09-25 00:42:04436 const AudioDevice* keyboard_mic = GetDeviceFromId(kKeyboardMic.id);
437 EXPECT_FALSE(keyboard_mic->active);
438}
439
440TEST_F(CrasAudioHandlerTest, SetKeyboardMicActive) {
441 AudioNodeList audio_nodes;
442 audio_nodes.push_back(kInternalMic);
443 audio_nodes.push_back(kKeyboardMic);
444 SetUpCrasAudioHandler(audio_nodes);
445
446 // Verify the audio devices size.
447 AudioDeviceList audio_devices;
448 cras_audio_handler_->GetAudioDevices(&audio_devices);
449 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
450 EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic());
451
452 // Ensure the internal microphone has been selected as the active input,
453 // not affected by keyboard mic.
454 AudioDevice active_input;
455 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
456 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
457 const AudioDevice* keyboard_mic = GetDeviceFromId(kKeyboardMic.id);
458 EXPECT_FALSE(keyboard_mic->active);
459
460 // Make keyboard mic active.
461 cras_audio_handler_->SetKeyboardMicActive(true);
462 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
463 const AudioDevice* active_keyboard_mic = GetDeviceFromId(kKeyboardMic.id);
464 EXPECT_TRUE(active_keyboard_mic->active);
465
466 // Make keyboard mic inactive.
467 cras_audio_handler_->SetKeyboardMicActive(false);
468 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
469 const AudioDevice* inactive_keyboard_mic = GetDeviceFromId(kKeyboardMic.id);
470 EXPECT_FALSE(inactive_keyboard_mic->active);
[email protected]f9a49d342014-07-29 23:47:20471}
472
[email protected]ee38bc42013-07-29 21:41:32473TEST_F(CrasAudioHandlerTest, SwitchActiveOutputDevice) {
474 AudioNodeList audio_nodes;
475 audio_nodes.push_back(kInternalSpeaker);
476 audio_nodes.push_back(kHeadphone);
477 SetUpCrasAudioHandler(audio_nodes);
478 AudioDeviceList audio_devices;
479 cras_audio_handler_->GetAudioDevices(&audio_devices);
480 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
481
482 // Verify the initial active output device is headphone.
483 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
484 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04485 EXPECT_TRUE(
486 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32487 EXPECT_EQ(kHeadphone.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04488 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32489
490 // Switch the active output to internal speaker.
491 AudioDevice internal_speaker(kInternalSpeaker);
jennyz2cb9eb12014-11-05 19:21:05492 cras_audio_handler_->SwitchToDevice(internal_speaker, true);
[email protected]ee38bc42013-07-29 21:41:32493
494 // Verify the active output is switched to internal speaker, and the
495 // ActiveOutputNodeChanged event is fired.
496 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04497 EXPECT_TRUE(
498 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32499 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04500 EXPECT_EQ(kInternalSpeaker.id,
501 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32502}
503
504TEST_F(CrasAudioHandlerTest, SwitchActiveInputDevice) {
505 AudioNodeList audio_nodes;
506 audio_nodes.push_back(kInternalMic);
507 audio_nodes.push_back(kUSBMic);
508 SetUpCrasAudioHandler(audio_nodes);
509 AudioDeviceList audio_devices;
510 cras_audio_handler_->GetAudioDevices(&audio_devices);
511 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
512
513 // Verify the initial active input device is USB mic.
514 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:04515 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:32516
517 // Switch the active input to internal mic.
518 AudioDevice internal_mic(kInternalMic);
jennyz2cb9eb12014-11-05 19:21:05519 cras_audio_handler_->SwitchToDevice(internal_mic, true);
[email protected]ee38bc42013-07-29 21:41:32520
521 // Verify the active output is switched to internal speaker, and the active
522 // ActiveInputNodeChanged event is fired.
523 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:04524 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:32525}
526
527TEST_F(CrasAudioHandlerTest, PlugHeadphone) {
528 // Set up initial audio devices, only with internal speaker.
529 AudioNodeList audio_nodes;
530 audio_nodes.push_back(kInternalSpeaker);
531 SetUpCrasAudioHandler(audio_nodes);
532 const size_t init_nodes_size = audio_nodes.size();
533
534 // Verify the audio devices size.
535 AudioDeviceList audio_devices;
536 cras_audio_handler_->GetAudioDevices(&audio_devices);
537 EXPECT_EQ(init_nodes_size, audio_devices.size());
538 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
539
540 // Verify the internal speaker has been selected as the active output.
541 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
542 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04543 EXPECT_TRUE(
544 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32545 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04546 EXPECT_EQ(kInternalSpeaker.id,
547 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32548 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
549
550 // Plug the headphone.
[email protected]4642d7792013-09-18 20:27:17551 audio_nodes.clear();
552 AudioNode internal_speaker(kInternalSpeaker);
553 internal_speaker.active = true;
554 audio_nodes.push_back(internal_speaker);
[email protected]ee38bc42013-07-29 21:41:32555 audio_nodes.push_back(kHeadphone);
556 ChangeAudioNodes(audio_nodes);
557
558 // Verify the AudioNodesChanged event is fired and new audio device is added.
559 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
560 cras_audio_handler_->GetAudioDevices(&audio_devices);
561 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
562
563 // Verify the active output device is switched to headphone and
564 // ActiveOutputChanged event is fired.
565 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04566 EXPECT_TRUE(
567 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32568 EXPECT_EQ(kHeadphone.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04569 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32570 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
571}
572
573TEST_F(CrasAudioHandlerTest, UnplugHeadphone) {
574 // Set up initial audio devices, with internal speaker and headphone.
575 AudioNodeList audio_nodes;
576 audio_nodes.push_back(kInternalSpeaker);
577 audio_nodes.push_back(kHeadphone);
578 SetUpCrasAudioHandler(audio_nodes);
579 const size_t init_nodes_size = audio_nodes.size();
580
581 // Verify the audio devices size.
582 AudioDeviceList audio_devices;
583 cras_audio_handler_->GetAudioDevices(&audio_devices);
584 EXPECT_EQ(init_nodes_size, audio_devices.size());
585 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
586
587 // Verify the headphone has been selected as the active output.
588 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
589 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04590 EXPECT_TRUE(
591 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32592 EXPECT_EQ(kHeadphone.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04593 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32594 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
595
596 // Unplug the headphone.
597 audio_nodes.clear();
598 audio_nodes.push_back(kInternalSpeaker);
599 ChangeAudioNodes(audio_nodes);
600
601 // Verify the AudioNodesChanged event is fired and one audio device is
602 // removed.
603 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
604 cras_audio_handler_->GetAudioDevices(&audio_devices);
605 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
606
607 // Verify the active output device is switched to internal speaker and
608 // ActiveOutputChanged event is fired.
609 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04610 EXPECT_TRUE(
611 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32612 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04613 EXPECT_EQ(kInternalSpeaker.id,
614 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32615 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
616}
617
618TEST_F(CrasAudioHandlerTest, InitializeWithBluetoothHeadset) {
619 AudioNodeList audio_nodes;
620 audio_nodes.push_back(kInternalSpeaker);
621 audio_nodes.push_back(kBluetoothHeadset);
622 SetUpCrasAudioHandler(audio_nodes);
623
624 // Verify the audio devices size.
625 AudioDeviceList audio_devices;
626 cras_audio_handler_->GetAudioDevices(&audio_devices);
627 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
628 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
629
630 // Verify the bluetooth headset has been selected as the active output.
631 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
632 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04633 EXPECT_TRUE(
634 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32635 EXPECT_EQ(kBluetoothHeadset.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04636 EXPECT_EQ(kBluetoothHeadset.id,
637 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32638 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
639}
640
641TEST_F(CrasAudioHandlerTest, ConnectAndDisconnectBluetoothHeadset) {
642 // Initialize with internal speaker and headphone.
643 AudioNodeList audio_nodes;
644 audio_nodes.push_back(kInternalSpeaker);
645 audio_nodes.push_back(kHeadphone);
646 SetUpCrasAudioHandler(audio_nodes);
647 const size_t init_nodes_size = audio_nodes.size();
648
649 // Verify the audio devices size.
650 AudioDeviceList audio_devices;
651 cras_audio_handler_->GetAudioDevices(&audio_devices);
652 EXPECT_EQ(init_nodes_size, audio_devices.size());
653 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
654
655 // Verify the headphone is selected as the active output initially.
656 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
657 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04658 EXPECT_TRUE(
659 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32660 EXPECT_EQ(kHeadphone.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04661 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32662 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
663
664 // Connect to bluetooth headset. Since it is plugged in later than
665 // headphone, active output should be switched to it.
666 audio_nodes.clear();
667 audio_nodes.push_back(kInternalSpeaker);
668 AudioNode headphone(kHeadphone);
669 headphone.plugged_time = 80000000;
670 headphone.active = true;
671 audio_nodes.push_back(headphone);
672 AudioNode bluetooth_headset(kBluetoothHeadset);
673 bluetooth_headset.plugged_time = 90000000;
674 audio_nodes.push_back(bluetooth_headset);
675 ChangeAudioNodes(audio_nodes);
676
677 // Verify the AudioNodesChanged event is fired and new audio device is added.
678 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
679 cras_audio_handler_->GetAudioDevices(&audio_devices);
680 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
681
682 // Verify the active output device is switched to bluetooth headset, and
683 // ActiveOutputChanged event is fired.
684 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04685 EXPECT_TRUE(
686 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32687 EXPECT_EQ(kBluetoothHeadset.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04688 EXPECT_EQ(kBluetoothHeadset.id,
689 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32690 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
691
692 // Disconnect bluetooth headset.
693 audio_nodes.clear();
694 audio_nodes.push_back(kInternalSpeaker);
[email protected]4642d7792013-09-18 20:27:17695 headphone.active = false;
[email protected]ee38bc42013-07-29 21:41:32696 audio_nodes.push_back(headphone);
697 ChangeAudioNodes(audio_nodes);
698
699 // Verify the AudioNodesChanged event is fired and one audio device is
700 // removed.
701 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
702 cras_audio_handler_->GetAudioDevices(&audio_devices);
703 EXPECT_EQ(init_nodes_size, audio_devices.size());
704
705 // Verify the active output device is switched to headphone, and
706 // ActiveOutputChanged event is fired.
707 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04708 EXPECT_TRUE(
709 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32710 EXPECT_EQ(kHeadphone.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04711 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32712 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
713}
714
715TEST_F(CrasAudioHandlerTest, InitializeWithHDMIOutput) {
716 AudioNodeList audio_nodes;
717 audio_nodes.push_back(kInternalSpeaker);
718 audio_nodes.push_back(kHDMIOutput);
719 SetUpCrasAudioHandler(audio_nodes);
720
721 // Verify the audio devices size.
722 AudioDeviceList audio_devices;
723 cras_audio_handler_->GetAudioDevices(&audio_devices);
724 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
725 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
726
727 // Verify the HDMI device has been selected as the active output.
728 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
729 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04730 EXPECT_TRUE(
731 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32732 EXPECT_EQ(kHDMIOutput.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04733 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32734 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
735}
736
737TEST_F(CrasAudioHandlerTest, ConnectAndDisconnectHDMIOutput) {
738 // Initialize with internal speaker.
739 AudioNodeList audio_nodes;
740 audio_nodes.push_back(kInternalSpeaker);
741 SetUpCrasAudioHandler(audio_nodes);
742 const size_t init_nodes_size = audio_nodes.size();
743
744 // Verify the audio devices size.
745 AudioDeviceList audio_devices;
746 cras_audio_handler_->GetAudioDevices(&audio_devices);
747 EXPECT_EQ(init_nodes_size, audio_devices.size());
748 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
749
750 // Verify the internal speaker is selected as the active output initially.
751 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
752 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04753 EXPECT_TRUE(
754 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32755 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04756 EXPECT_EQ(kInternalSpeaker.id,
757 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32758 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
759
760 // Connect to HDMI output.
761 audio_nodes.clear();
762 AudioNode internal_speaker(kInternalSpeaker);
763 internal_speaker.active = true;
764 internal_speaker.plugged_time = 80000000;
765 audio_nodes.push_back(internal_speaker);
766 AudioNode hdmi(kHDMIOutput);
767 hdmi.plugged_time = 90000000;
768 audio_nodes.push_back(hdmi);
769 ChangeAudioNodes(audio_nodes);
770
771 // Verify the AudioNodesChanged event is fired and new audio device is added.
772 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
773 cras_audio_handler_->GetAudioDevices(&audio_devices);
774 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
775
776 // Verify the active output device is switched to hdmi output, and
777 // ActiveOutputChanged event is fired.
778 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04779 EXPECT_TRUE(
780 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32781 EXPECT_EQ(kHDMIOutput.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04782 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32783 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
784
785 // Disconnect hdmi headset.
786 audio_nodes.clear();
787 audio_nodes.push_back(kInternalSpeaker);
788 ChangeAudioNodes(audio_nodes);
789
790 // Verify the AudioNodesChanged event is fired and one audio device is
791 // removed.
792 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
793 cras_audio_handler_->GetAudioDevices(&audio_devices);
794 EXPECT_EQ(init_nodes_size, audio_devices.size());
795
796 // Verify the active output device is switched to internal speaker, and
797 // ActiveOutputChanged event is fired.
798 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04799 EXPECT_TRUE(
800 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32801 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04802 EXPECT_EQ(kInternalSpeaker.id,
803 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32804 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
805}
806
807TEST_F(CrasAudioHandlerTest, HandleHeadphoneAndHDMIOutput) {
808 // Initialize with internal speaker, headphone and HDMI output.
809 AudioNodeList audio_nodes;
810 audio_nodes.push_back(kInternalSpeaker);
811 audio_nodes.push_back(kHeadphone);
812 audio_nodes.push_back(kHDMIOutput);
813 SetUpCrasAudioHandler(audio_nodes);
814 const size_t init_nodes_size = audio_nodes.size();
815
816 // Verify the audio devices size.
817 AudioDeviceList audio_devices;
818 cras_audio_handler_->GetAudioDevices(&audio_devices);
819 EXPECT_EQ(init_nodes_size, audio_devices.size());
820 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
821
822 // Verify the headphone is selected as the active output initially.
823 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
824 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04825 EXPECT_TRUE(
826 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32827 EXPECT_EQ(kHeadphone.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04828 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32829 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
830
831 // Disconnect HDMI output.
832 audio_nodes.clear();
833 audio_nodes.push_back(kInternalSpeaker);
834 audio_nodes.push_back(kHDMIOutput);
835 ChangeAudioNodes(audio_nodes);
836
837 // Verify the AudioNodesChanged event is fired and one audio device is
838 // removed.
839 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
840 cras_audio_handler_->GetAudioDevices(&audio_devices);
841 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
842
843 // Verify the active output device is switched to HDMI output, and
844 // ActiveOutputChanged event is fired.
845 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04846 EXPECT_TRUE(
847 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32848 EXPECT_EQ(kHDMIOutput.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04849 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32850 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
851}
852
853TEST_F(CrasAudioHandlerTest, InitializeWithUSBHeadphone) {
854 AudioNodeList audio_nodes;
855 audio_nodes.push_back(kInternalSpeaker);
856 audio_nodes.push_back(kUSBHeadphone1);
857 SetUpCrasAudioHandler(audio_nodes);
858
859 // Verify the audio devices size.
860 AudioDeviceList audio_devices;
861 cras_audio_handler_->GetAudioDevices(&audio_devices);
862 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
863 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
864
865 // Verify the usb headphone has been selected as the active output.
866 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
867 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04868 EXPECT_TRUE(
869 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32870 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04871 EXPECT_EQ(kUSBHeadphone1.id,
872 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32873 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
874}
875
876TEST_F(CrasAudioHandlerTest, PlugAndUnplugUSBHeadphone) {
877 // Initialize with internal speaker.
878 AudioNodeList audio_nodes;
879 audio_nodes.push_back(kInternalSpeaker);
880 SetUpCrasAudioHandler(audio_nodes);
881 const size_t init_nodes_size = audio_nodes.size();
882
883 // Verify the audio devices size.
884 AudioDeviceList audio_devices;
885 cras_audio_handler_->GetAudioDevices(&audio_devices);
886 EXPECT_EQ(init_nodes_size, audio_devices.size());
887 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
888
889 // Verify the internal speaker is selected as the active output initially.
890 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
891 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04892 EXPECT_TRUE(
893 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32894 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04895 EXPECT_EQ(kInternalSpeaker.id,
896 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32897 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
898
899 // Plug in usb headphone
900 audio_nodes.clear();
901 AudioNode internal_speaker(kInternalSpeaker);
902 internal_speaker.active = true;
903 internal_speaker.plugged_time = 80000000;
904 audio_nodes.push_back(internal_speaker);
905 AudioNode usb_headphone(kUSBHeadphone1);
906 usb_headphone.plugged_time = 90000000;
907 audio_nodes.push_back(usb_headphone);
908 ChangeAudioNodes(audio_nodes);
909
910 // Verify the AudioNodesChanged event is fired and new audio device is added.
911 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
912 cras_audio_handler_->GetAudioDevices(&audio_devices);
913 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
914
915 // Verify the active output device is switched to usb headphone, and
916 // ActiveOutputChanged event is fired.
917 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04918 EXPECT_TRUE(
919 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32920 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04921 EXPECT_EQ(kUSBHeadphone1.id,
922 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32923 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
924
925 // Unplug usb headphone.
926 audio_nodes.clear();
927 audio_nodes.push_back(kInternalSpeaker);
928 ChangeAudioNodes(audio_nodes);
929
930 // Verify the AudioNodesChanged event is fired and one audio device is
931 // removed.
932 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
933 cras_audio_handler_->GetAudioDevices(&audio_devices);
934 EXPECT_EQ(init_nodes_size, audio_devices.size());
935
936 // Verify the active output device is switched to internal speaker, and
937 // ActiveOutputChanged event is fired.
938 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04939 EXPECT_TRUE(
940 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32941 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04942 EXPECT_EQ(kInternalSpeaker.id,
943 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32944 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
945}
946
947TEST_F(CrasAudioHandlerTest, HandleMultipleUSBHeadphones) {
948 // Initialize with internal speaker and one usb headphone.
949 AudioNodeList audio_nodes;
950 audio_nodes.push_back(kInternalSpeaker);
951 audio_nodes.push_back(kUSBHeadphone1);
952 SetUpCrasAudioHandler(audio_nodes);
953 const size_t init_nodes_size = audio_nodes.size();
954
955 // Verify the audio devices size.
956 AudioDeviceList audio_devices;
957 cras_audio_handler_->GetAudioDevices(&audio_devices);
958 EXPECT_EQ(init_nodes_size, audio_devices.size());
959 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
960
961 // Verify the usb headphone is selected as the active output initially.
962 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
963 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04964 EXPECT_TRUE(
965 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32966 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04967 EXPECT_EQ(kUSBHeadphone1.id,
968 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32969 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
970
971 // Plug in another usb headphone.
972 audio_nodes.clear();
973 audio_nodes.push_back(kInternalSpeaker);
974 AudioNode usb_headphone_1(kUSBHeadphone1);
975 usb_headphone_1.active = true;
976 usb_headphone_1.plugged_time = 80000000;
977 audio_nodes.push_back(usb_headphone_1);
978 AudioNode usb_headphone_2(kUSBHeadphone2);
979 usb_headphone_2.plugged_time = 90000000;
980 audio_nodes.push_back(usb_headphone_2);
981 ChangeAudioNodes(audio_nodes);
982
983 // Verify the AudioNodesChanged event is fired and new audio device is added.
984 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
985 cras_audio_handler_->GetAudioDevices(&audio_devices);
986 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
987
988 // Verify the active output device is switched to the 2nd usb headphone, which
989 // is plugged later, and ActiveOutputChanged event is fired.
990 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04991 EXPECT_TRUE(
992 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32993 EXPECT_EQ(kUSBHeadphone2.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04994 EXPECT_EQ(kUSBHeadphone2.id,
995 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32996 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
997
998 // Unplug the 2nd usb headphone.
999 audio_nodes.clear();
1000 audio_nodes.push_back(kInternalSpeaker);
1001 audio_nodes.push_back(kUSBHeadphone1);
1002 ChangeAudioNodes(audio_nodes);
1003
1004 // Verify the AudioNodesChanged event is fired and one audio device is
1005 // removed.
1006 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1007 cras_audio_handler_->GetAudioDevices(&audio_devices);
1008 EXPECT_EQ(init_nodes_size, audio_devices.size());
1009
1010 // Verify the active output device is switched to the first usb headphone, and
1011 // ActiveOutputChanged event is fired.
1012 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041013 EXPECT_TRUE(
1014 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:321015 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
jennyzb47947f2014-09-25 00:42:041016 EXPECT_EQ(kUSBHeadphone1.id,
1017 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:321018 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1019}
1020
[email protected]ca473252013-08-15 11:48:391021TEST_F(CrasAudioHandlerTest, UnplugUSBHeadphonesWithActiveSpeaker) {
1022 // Initialize with internal speaker and one usb headphone.
1023 AudioNodeList audio_nodes;
1024 audio_nodes.push_back(kInternalSpeaker);
1025 audio_nodes.push_back(kUSBHeadphone1);
1026 SetUpCrasAudioHandler(audio_nodes);
1027 const size_t init_nodes_size = audio_nodes.size();
1028
1029 // Verify the audio devices size.
1030 AudioDeviceList audio_devices;
1031 cras_audio_handler_->GetAudioDevices(&audio_devices);
1032 EXPECT_EQ(init_nodes_size, audio_devices.size());
1033 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1034
1035 // Verify the usb headphone is selected as the active output initially.
1036 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1037 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041038 EXPECT_TRUE(
1039 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ca473252013-08-15 11:48:391040 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
jennyzb47947f2014-09-25 00:42:041041 EXPECT_EQ(kUSBHeadphone1.id,
1042 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ca473252013-08-15 11:48:391043 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1044
1045 // Plug in the headphone jack.
1046 audio_nodes.clear();
1047 audio_nodes.push_back(kInternalSpeaker);
1048 AudioNode usb_headphone_1(kUSBHeadphone1);
1049 usb_headphone_1.active = true;
1050 usb_headphone_1.plugged_time = 80000000;
1051 audio_nodes.push_back(usb_headphone_1);
1052 AudioNode headphone_jack(kHeadphone);
1053 headphone_jack.plugged_time = 90000000;
1054 audio_nodes.push_back(headphone_jack);
1055 ChangeAudioNodes(audio_nodes);
1056
1057 // Verify the AudioNodesChanged event is fired and new audio device is added.
1058 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1059 cras_audio_handler_->GetAudioDevices(&audio_devices);
1060 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1061
1062 // Verify the active output device is switched to the headphone jack, which
1063 // is plugged later, and ActiveOutputChanged event is fired.
1064 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041065 EXPECT_TRUE(
1066 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ca473252013-08-15 11:48:391067 EXPECT_EQ(kHeadphone.id, active_output.id);
jennyzb47947f2014-09-25 00:42:041068 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ca473252013-08-15 11:48:391069 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1070
1071 // Select the speaker to be the active output device.
1072 AudioDevice internal_speaker(kInternalSpeaker);
jennyz2cb9eb12014-11-05 19:21:051073 cras_audio_handler_->SwitchToDevice(internal_speaker, true);
[email protected]ca473252013-08-15 11:48:391074
1075 // Verify the active output is switched to internal speaker, and the
1076 // ActiveOutputNodeChanged event is fired.
1077 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041078 EXPECT_TRUE(
1079 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ca473252013-08-15 11:48:391080 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
jennyzb47947f2014-09-25 00:42:041081 EXPECT_EQ(kInternalSpeaker.id,
1082 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ca473252013-08-15 11:48:391083
1084 // Unplug the usb headphone.
1085 audio_nodes.clear();
1086 AudioNode internal_speaker_node(kInternalSpeaker);
1087 internal_speaker_node.active = true;
1088 internal_speaker_node.plugged_time = 70000000;
1089 audio_nodes.push_back(internal_speaker_node);
1090 headphone_jack.active = false;
1091 audio_nodes.push_back(headphone_jack);
1092 ChangeAudioNodes(audio_nodes);
1093
1094 // Verify the AudioNodesChanged event is fired and one audio device is
1095 // removed.
1096 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1097 cras_audio_handler_->GetAudioDevices(&audio_devices);
1098 EXPECT_EQ(init_nodes_size, audio_devices.size());
1099
1100 // Verify the active output device remains to be speaker.
1101 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041102 EXPECT_TRUE(
1103 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ca473252013-08-15 11:48:391104 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
jennyzb47947f2014-09-25 00:42:041105 EXPECT_EQ(kInternalSpeaker.id,
1106 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ca473252013-08-15 11:48:391107 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1108}
1109
[email protected]d413fcc2013-08-16 23:20:031110TEST_F(CrasAudioHandlerTest, OneActiveAudioOutputAfterLoginNewUserSession) {
1111 // This tests the case found with crbug.com/273271.
1112 // Initialize with internal speaker, bluetooth headphone and headphone jack
1113 // for a new chrome session after user signs out from the previous session.
1114 // Headphone jack is plugged in later than bluetooth headphone, but bluetooth
1115 // headphone is selected as the active output by user from previous user
1116 // session.
1117 AudioNodeList audio_nodes;
1118 audio_nodes.push_back(kInternalSpeaker);
1119 AudioNode bluetooth_headphone(kBluetoothHeadset);
1120 bluetooth_headphone.active = true;
1121 bluetooth_headphone.plugged_time = 70000000;
1122 audio_nodes.push_back(bluetooth_headphone);
1123 AudioNode headphone_jack(kHeadphone);
1124 headphone_jack.plugged_time = 80000000;
1125 audio_nodes.push_back(headphone_jack);
jennyzb47947f2014-09-25 00:42:041126 SetUpCrasAudioHandlerWithPrimaryActiveNode(audio_nodes, bluetooth_headphone);
[email protected]d413fcc2013-08-16 23:20:031127 const size_t init_nodes_size = audio_nodes.size();
1128
1129 // Verify the audio devices size.
1130 AudioDeviceList audio_devices;
1131 cras_audio_handler_->GetAudioDevices(&audio_devices);
1132 EXPECT_EQ(init_nodes_size, audio_devices.size());
1133 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1134
1135 // Verify the headphone jack is selected as the active output and all other
1136 // audio devices are not active.
1137 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1138 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041139 EXPECT_TRUE(
1140 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]d413fcc2013-08-16 23:20:031141 EXPECT_EQ(kHeadphone.id, active_output.id);
jennyzb47947f2014-09-25 00:42:041142 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]d413fcc2013-08-16 23:20:031143 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1144 for (size_t i = 0; i < audio_devices.size(); ++i) {
1145 if (audio_devices[i].id != kHeadphone.id)
1146 EXPECT_FALSE(audio_devices[i].active);
1147 }
1148}
1149
[email protected]110babd2013-09-17 01:46:021150TEST_F(CrasAudioHandlerTest, BluetoothSpeakerIdChangedOnFly) {
1151 // Initialize with internal speaker and bluetooth headset.
1152 AudioNodeList audio_nodes;
1153 audio_nodes.push_back(kInternalSpeaker);
1154 audio_nodes.push_back(kBluetoothHeadset);
1155 SetUpCrasAudioHandler(audio_nodes);
1156 const size_t init_nodes_size = audio_nodes.size();
1157
1158 // Verify the audio devices size.
1159 AudioDeviceList audio_devices;
1160 cras_audio_handler_->GetAudioDevices(&audio_devices);
1161 EXPECT_EQ(init_nodes_size, audio_devices.size());
1162 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1163
1164 // Verify the bluetooth headset is selected as the active output and all other
1165 // audio devices are not active.
1166 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1167 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041168 EXPECT_TRUE(
1169 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]110babd2013-09-17 01:46:021170 EXPECT_EQ(kBluetoothHeadset.id, active_output.id);
jennyzb47947f2014-09-25 00:42:041171 EXPECT_EQ(kBluetoothHeadset.id,
1172 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]110babd2013-09-17 01:46:021173 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1174
1175 // Cras changes the bluetooth headset's id on the fly.
1176 audio_nodes.clear();
1177 AudioNode internal_speaker(kInternalSpeaker);
1178 internal_speaker.active = false;
1179 audio_nodes.push_back(internal_speaker);
1180 AudioNode bluetooth_headphone(kBluetoothHeadset);
1181 // Change bluetooth headphone id.
1182 bluetooth_headphone.id = kBluetoothHeadsetId + 20000;
1183 bluetooth_headphone.active = false;
1184 audio_nodes.push_back(bluetooth_headphone);
1185 ChangeAudioNodes(audio_nodes);
1186
1187 // Verify NodesChanged event is fired, and the audio devices size is not
1188 // changed.
1189 audio_devices.clear();
1190 cras_audio_handler_->GetAudioDevices(&audio_devices);
1191 EXPECT_EQ(init_nodes_size, audio_devices.size());
1192 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1193
1194 // Verify ActiveOutputNodeChanged event is fired, and active device should be
1195 // bluetooth headphone.
1196 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041197 EXPECT_TRUE(
1198 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]110babd2013-09-17 01:46:021199 EXPECT_EQ(bluetooth_headphone.id, active_output.id);
1200}
1201
[email protected]ee38bc42013-07-29 21:41:321202TEST_F(CrasAudioHandlerTest, PlugUSBMic) {
1203 // Set up initial audio devices, only with internal mic.
1204 AudioNodeList audio_nodes;
1205 audio_nodes.push_back(kInternalMic);
1206 SetUpCrasAudioHandler(audio_nodes);
1207 const size_t init_nodes_size = audio_nodes.size();
1208
1209 // Verify the audio devices size.
1210 AudioDeviceList audio_devices;
1211 cras_audio_handler_->GetAudioDevices(&audio_devices);
1212 EXPECT_EQ(init_nodes_size, audio_devices.size());
1213 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1214
[email protected]babc24b2013-09-09 23:50:231215 // Verify the internal mic is selected as the active input.
[email protected]ee38bc42013-07-29 21:41:321216 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041217 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:321218 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1219
1220 // Plug the USB Mic.
[email protected]4642d7792013-09-18 20:27:171221 audio_nodes.clear();
1222 AudioNode internal_mic(kInternalMic);
1223 internal_mic.active = true;
1224 audio_nodes.push_back(internal_mic);
[email protected]ee38bc42013-07-29 21:41:321225 audio_nodes.push_back(kUSBMic);
1226 ChangeAudioNodes(audio_nodes);
1227
1228 // Verify the AudioNodesChanged event is fired and new audio device is added.
1229 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1230 cras_audio_handler_->GetAudioDevices(&audio_devices);
1231 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1232
1233 // Verify the active input device is switched to USB mic and
1234 // and ActiveInputChanged event is fired.
1235 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041236 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:321237 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1238}
1239
1240TEST_F(CrasAudioHandlerTest, UnplugUSBMic) {
1241 // Set up initial audio devices, with internal mic and USB Mic.
1242 AudioNodeList audio_nodes;
1243 audio_nodes.push_back(kInternalMic);
1244 audio_nodes.push_back(kUSBMic);
1245 SetUpCrasAudioHandler(audio_nodes);
1246 const size_t init_nodes_size = audio_nodes.size();
1247
1248 // Verify the audio devices size.
1249 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1250 AudioDeviceList audio_devices;
1251 cras_audio_handler_->GetAudioDevices(&audio_devices);
1252 EXPECT_EQ(init_nodes_size, audio_devices.size());
1253
1254 // Verify the USB mic is selected as the active output.
1255 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041256 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:321257 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1258
1259 // Unplug the USB Mic.
1260 audio_nodes.clear();
1261 audio_nodes.push_back(kInternalMic);
1262 ChangeAudioNodes(audio_nodes);
1263
1264 // Verify the AudioNodesChanged event is fired, and one audio device is
1265 // removed.
1266 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1267 cras_audio_handler_->GetAudioDevices(&audio_devices);
1268 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
1269
1270 // Verify the active input device is switched to internal mic, and
1271 // and ActiveInputChanged event is fired.
1272 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041273 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:321274 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1275}
1276
[email protected]babc24b2013-09-09 23:50:231277TEST_F(CrasAudioHandlerTest, PlugUSBMicNotAffectActiveOutput) {
1278 // Set up initial audio devices.
1279 AudioNodeList audio_nodes;
1280 audio_nodes.push_back(kInternalSpeaker);
1281 audio_nodes.push_back(kHeadphone);
1282 audio_nodes.push_back(kInternalMic);
1283 SetUpCrasAudioHandler(audio_nodes);
1284 const size_t init_nodes_size = audio_nodes.size();
1285
1286 // Verify the audio devices size.
1287 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1288 AudioDeviceList audio_devices;
1289 cras_audio_handler_->GetAudioDevices(&audio_devices);
1290 EXPECT_EQ(init_nodes_size, audio_devices.size());
1291
1292 // Verify the internal mic is selected as the active input.
1293 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041294 EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]babc24b2013-09-09 23:50:231295 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1296
1297 // Verify the headphone is selected as the active output.
1298 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041299 EXPECT_EQ(kHeadphoneId, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]babc24b2013-09-09 23:50:231300 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1301
1302 // Switch the active output to internal speaker.
1303 AudioDevice internal_speaker(kInternalSpeaker);
jennyz2cb9eb12014-11-05 19:21:051304 cras_audio_handler_->SwitchToDevice(internal_speaker, true);
[email protected]babc24b2013-09-09 23:50:231305
1306 // Verify the active output is switched to internal speaker, and the
1307 // ActiveOutputNodeChanged event is fired.
1308 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1309 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041310 EXPECT_TRUE(
1311 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]babc24b2013-09-09 23:50:231312 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
jennyzb47947f2014-09-25 00:42:041313 EXPECT_EQ(kInternalSpeaker.id,
1314 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]babc24b2013-09-09 23:50:231315
1316 // Plug the USB Mic.
[email protected]4642d7792013-09-18 20:27:171317 audio_nodes.clear();
1318 AudioNode internal_speaker_node(kInternalSpeaker);
1319 internal_speaker_node.active = true;
1320 audio_nodes.push_back(internal_speaker_node);
1321 audio_nodes.push_back(kHeadphone);
1322 AudioNode internal_mic(kInternalMic);
1323 internal_mic.active = true;
1324 audio_nodes.push_back(internal_mic);
[email protected]babc24b2013-09-09 23:50:231325 audio_nodes.push_back(kUSBMic);
1326 ChangeAudioNodes(audio_nodes);
1327
1328 // Verify the AudioNodesChanged event is fired, one new device is added.
1329 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1330 cras_audio_handler_->GetAudioDevices(&audio_devices);
1331 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1332
1333 // Verify the active input device is switched to USB mic, and
1334 // and ActiveInputChanged event is fired.
1335 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041336 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]babc24b2013-09-09 23:50:231337 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1338
1339 // Verify the active output device is not changed.
1340 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041341 EXPECT_TRUE(
1342 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]babc24b2013-09-09 23:50:231343 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
jennyzb47947f2014-09-25 00:42:041344 EXPECT_EQ(kInternalSpeaker.id,
1345 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]babc24b2013-09-09 23:50:231346}
1347
[email protected]ce631ee2014-01-23 03:37:061348TEST_F(CrasAudioHandlerTest, PlugHeadphoneAutoUnplugSpeakerWithActiveUSB) {
1349 // Set up initial audio devices.
1350 AudioNodeList audio_nodes;
1351 audio_nodes.push_back(kUSBHeadphone1);
1352 audio_nodes.push_back(kInternalSpeaker);
1353 audio_nodes.push_back(kInternalMic);
1354 SetUpCrasAudioHandler(audio_nodes);
1355 const size_t init_nodes_size = audio_nodes.size();
1356
1357 // Verify the audio devices size.
1358 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1359 AudioDeviceList audio_devices;
1360 cras_audio_handler_->GetAudioDevices(&audio_devices);
1361 EXPECT_EQ(init_nodes_size, audio_devices.size());
1362
1363 // Verify the internal mic is selected as the active input.
1364 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041365 EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ce631ee2014-01-23 03:37:061366 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1367
1368 // Verify the USB headphone is selected as the active output.
1369 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041370 EXPECT_EQ(kUSBHeadphoneId1,
1371 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ce631ee2014-01-23 03:37:061372 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1373
1374 // Plug the headphone and auto-unplug internal speaker.
1375 audio_nodes.clear();
1376 AudioNode usb_headphone_node(kUSBHeadphone1);
1377 usb_headphone_node.active = true;
1378 audio_nodes.push_back(usb_headphone_node);
1379 AudioNode headphone_node(kHeadphone);
1380 headphone_node.plugged_time = 1000;
1381 audio_nodes.push_back(headphone_node);
1382 AudioNode internal_mic(kInternalMic);
1383 internal_mic.active = true;
1384 audio_nodes.push_back(internal_mic);
1385 ChangeAudioNodes(audio_nodes);
1386
1387 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1388 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1389 cras_audio_handler_->GetAudioDevices(&audio_devices);
1390 EXPECT_EQ(init_nodes_size, audio_devices.size());
1391
1392 // Verify the active output device is switched to headphone, and
1393 // an ActiveOutputChanged event is fired.
1394 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041395 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ce631ee2014-01-23 03:37:061396 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1397
1398 // Unplug the headphone and internal speaker auto-plugs back.
1399 audio_nodes.clear();
1400 audio_nodes.push_back(kUSBHeadphone1);
1401 AudioNode internal_speaker_node(kInternalSpeaker);
1402 internal_speaker_node.plugged_time = 2000;
1403 audio_nodes.push_back(internal_speaker_node);
1404 audio_nodes.push_back(internal_mic);
1405 ChangeAudioNodes(audio_nodes);
1406
1407 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1408 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1409 cras_audio_handler_->GetAudioDevices(&audio_devices);
1410 EXPECT_EQ(init_nodes_size, audio_devices.size());
1411
1412 // Verify the active output device is switched back to USB, and
1413 // an ActiveOutputChanged event is fired.
1414 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041415 EXPECT_EQ(kUSBHeadphone1.id,
1416 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ce631ee2014-01-23 03:37:061417 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1418
1419 // Verify the active input device is not changed.
1420 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041421 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ce631ee2014-01-23 03:37:061422}
1423
1424TEST_F(CrasAudioHandlerTest, PlugMicAutoUnplugInternalMicWithActiveUSB) {
1425 // Set up initial audio devices.
1426 AudioNodeList audio_nodes;
1427 audio_nodes.push_back(kUSBHeadphone1);
1428 audio_nodes.push_back(kInternalSpeaker);
1429 audio_nodes.push_back(kUSBMic);
1430 audio_nodes.push_back(kInternalMic);
1431 SetUpCrasAudioHandler(audio_nodes);
1432 const size_t init_nodes_size = audio_nodes.size();
1433
1434 // Verify the audio devices size.
1435 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1436 AudioDeviceList audio_devices;
1437 cras_audio_handler_->GetAudioDevices(&audio_devices);
1438 EXPECT_EQ(init_nodes_size, audio_devices.size());
1439
1440 // Verify the internal mic is selected as the active input.
1441 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041442 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ce631ee2014-01-23 03:37:061443 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1444
1445 // Verify the internal speaker is selected as the active output.
1446 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041447 EXPECT_EQ(kUSBHeadphoneId1,
1448 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ce631ee2014-01-23 03:37:061449 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1450
1451 // Plug the headphone and mic, auto-unplug internal mic and speaker.
1452 audio_nodes.clear();
1453 AudioNode usb_headphone_node(kUSBHeadphone1);
1454 usb_headphone_node.active = true;
1455 audio_nodes.push_back(usb_headphone_node);
1456 AudioNode headphone_node(kHeadphone);
1457 headphone_node.plugged_time = 1000;
1458 audio_nodes.push_back(headphone_node);
1459 AudioNode usb_mic(kUSBMic);
1460 usb_mic.active = true;
1461 audio_nodes.push_back(usb_mic);
1462 AudioNode mic_jack(kMicJack);
1463 mic_jack.plugged_time = 1000;
1464 audio_nodes.push_back(mic_jack);
1465 ChangeAudioNodes(audio_nodes);
1466
1467 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1468 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1469 cras_audio_handler_->GetAudioDevices(&audio_devices);
1470 EXPECT_EQ(init_nodes_size, audio_devices.size());
1471
1472 // Verify the active output device is switched to headphone, and
1473 // an ActiveOutputChanged event is fired.
1474 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041475 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ce631ee2014-01-23 03:37:061476 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1477
1478 // Verify the active input device is switched to mic jack, and
1479 // an ActiveInputChanged event is fired.
1480 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041481 EXPECT_EQ(kMicJack.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ce631ee2014-01-23 03:37:061482 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1483
1484 // Unplug the headphone and internal speaker auto-plugs back.
1485 audio_nodes.clear();
1486 audio_nodes.push_back(kUSBHeadphone1);
1487 AudioNode internal_speaker_node(kInternalSpeaker);
1488 internal_speaker_node.plugged_time = 2000;
1489 audio_nodes.push_back(internal_speaker_node);
1490 audio_nodes.push_back(kUSBMic);
1491 AudioNode internal_mic(kInternalMic);
1492 internal_mic.plugged_time = 2000;
1493 audio_nodes.push_back(internal_mic);
1494 ChangeAudioNodes(audio_nodes);
1495
1496 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1497 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1498 cras_audio_handler_->GetAudioDevices(&audio_devices);
1499 EXPECT_EQ(init_nodes_size, audio_devices.size());
1500
1501 // Verify the active output device is switched back to USB, and
1502 // an ActiveOutputChanged event is fired.
1503 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041504 EXPECT_EQ(kUSBHeadphone1.id,
1505 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ce631ee2014-01-23 03:37:061506 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1507
1508 // Verify the active input device is switched back to USB mic, and
1509 // an ActiveInputChanged event is fired.
1510 EXPECT_EQ(2, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041511 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ce631ee2014-01-23 03:37:061512 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1513}
1514
[email protected]4642d7792013-09-18 20:27:171515TEST_F(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnPlugInHeadphone) {
1516 // Set up initial audio devices.
1517 AudioNodeList audio_nodes;
1518 audio_nodes.push_back(kInternalSpeaker);
1519 audio_nodes.push_back(kBluetoothHeadset);
1520 SetUpCrasAudioHandler(audio_nodes);
1521 const size_t init_nodes_size = audio_nodes.size();
1522
1523 // Verify the audio devices size.
1524 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1525 AudioDeviceList audio_devices;
1526 cras_audio_handler_->GetAudioDevices(&audio_devices);
1527 EXPECT_EQ(init_nodes_size, audio_devices.size());
1528
1529 // Verify the bluetooth headset is selected as the active output.
1530 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041531 EXPECT_EQ(kBluetoothHeadsetId,
1532 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]4642d7792013-09-18 20:27:171533 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041534 EXPECT_TRUE(
1535 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]4642d7792013-09-18 20:27:171536 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1537
1538 // Plug in headphone, but fire NodesChanged signal twice.
1539 audio_nodes.clear();
1540 audio_nodes.push_back(kInternalSpeaker);
1541 AudioNode bluetooth_headset(kBluetoothHeadset);
1542 bluetooth_headset.plugged_time = 1000;
1543 bluetooth_headset.active = true;
1544 audio_nodes.push_back(bluetooth_headset);
1545 AudioNode headphone(kHeadphone);
1546 headphone.active = false;
1547 headphone.plugged_time = 2000;
1548 audio_nodes.push_back(headphone);
1549 ChangeAudioNodes(audio_nodes);
1550 ChangeAudioNodes(audio_nodes);
1551
1552 // Verify the active output device is set to headphone.
1553 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
jennyzb47947f2014-09-25 00:42:041554 EXPECT_LE(1, test_observer_->active_output_node_changed_count());
1555 EXPECT_EQ(headphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1556 EXPECT_TRUE(
1557 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]4642d7792013-09-18 20:27:171558 EXPECT_EQ(headphone.id, active_output.id);
1559
1560 // Verfiy the audio devices data is consistent, i.e., the active output device
1561 // should be headphone.
1562 cras_audio_handler_->GetAudioDevices(&audio_devices);
1563 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1564 for (size_t i = 0; i < audio_devices.size(); ++i) {
1565 if (audio_devices[i].id == kInternalSpeaker.id)
1566 EXPECT_FALSE(audio_devices[i].active);
1567 else if (audio_devices[i].id == bluetooth_headset.id)
1568 EXPECT_FALSE(audio_devices[i].active);
1569 else if (audio_devices[i].id == headphone.id)
1570 EXPECT_TRUE(audio_devices[i].active);
1571 else
1572 NOTREACHED();
1573 }
1574}
1575
1576TEST_F(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnPlugInUSBMic) {
1577 // Set up initial audio devices.
1578 AudioNodeList audio_nodes;
1579 audio_nodes.push_back(kInternalMic);
1580 SetUpCrasAudioHandler(audio_nodes);
1581 const size_t init_nodes_size = audio_nodes.size();
1582
1583 // Verify the audio devices size.
1584 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1585 AudioDeviceList audio_devices;
1586 cras_audio_handler_->GetAudioDevices(&audio_devices);
1587 EXPECT_EQ(init_nodes_size, audio_devices.size());
1588
1589 // Verify the internal mic is selected as the active output.
1590 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041591 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]4642d7792013-09-18 20:27:171592 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
1593 EXPECT_TRUE(audio_devices[0].active);
1594
1595 // Plug in usb mic, but fire NodesChanged signal twice.
1596 audio_nodes.clear();
1597 AudioNode internal_mic(kInternalMic);
1598 internal_mic.active = true;
1599 internal_mic.plugged_time = 1000;
1600 audio_nodes.push_back(internal_mic);
1601 AudioNode usb_mic(kUSBMic);
1602 usb_mic.active = false;
1603 usb_mic.plugged_time = 2000;
1604 audio_nodes.push_back(usb_mic);
1605 ChangeAudioNodes(audio_nodes);
1606 ChangeAudioNodes(audio_nodes);
1607
1608 // Verify the active output device is set to headphone.
1609 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
jennyzb47947f2014-09-25 00:42:041610 EXPECT_LE(1, test_observer_->active_input_node_changed_count());
1611 EXPECT_EQ(usb_mic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]4642d7792013-09-18 20:27:171612 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1613
1614 // Verfiy the audio devices data is consistent, i.e., the active input device
1615 // should be usb mic.
1616 cras_audio_handler_->GetAudioDevices(&audio_devices);
1617 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1618 for (size_t i = 0; i < audio_devices.size(); ++i) {
1619 if (audio_devices[i].id == kInternalMic.id)
1620 EXPECT_FALSE(audio_devices[i].active);
1621 else if (audio_devices[i].id == usb_mic.id)
1622 EXPECT_TRUE(audio_devices[i].active);
1623 else
1624 NOTREACHED();
1625 }
1626}
1627
1628// This is the case of crbug.com/291303.
1629TEST_F(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnSystemBoot) {
1630 // Set up audio handler with empty audio_nodes.
1631 AudioNodeList audio_nodes;
1632 SetUpCrasAudioHandler(audio_nodes);
1633
1634 AudioNode internal_speaker(kInternalSpeaker);
1635 internal_speaker.active = false;
1636 AudioNode headphone(kHeadphone);
1637 headphone.active = false;
1638 AudioNode internal_mic(kInternalMic);
1639 internal_mic.active = false;
1640 audio_nodes.push_back(internal_speaker);
1641 audio_nodes.push_back(headphone);
1642 audio_nodes.push_back(internal_mic);
1643 const size_t init_nodes_size = audio_nodes.size();
1644
1645 // Simulate AudioNodesChanged signal being fired twice during system boot.
1646 ChangeAudioNodes(audio_nodes);
1647 ChangeAudioNodes(audio_nodes);
1648
1649 // Verify the active output device is set to headphone.
1650 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
jennyzb47947f2014-09-25 00:42:041651 EXPECT_LE(1, test_observer_->active_output_node_changed_count());
1652 EXPECT_EQ(headphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]4642d7792013-09-18 20:27:171653 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041654 EXPECT_TRUE(
1655 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]4642d7792013-09-18 20:27:171656 EXPECT_EQ(headphone.id, active_output.id);
1657
1658 // Verify the active input device id is set to internal mic.
jennyzb47947f2014-09-25 00:42:041659 EXPECT_EQ(internal_mic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]4642d7792013-09-18 20:27:171660
1661 // Verfiy the audio devices data is consistent, i.e., the active output device
1662 // should be headphone, and the active input device should internal mic.
1663 AudioDeviceList audio_devices;
1664 cras_audio_handler_->GetAudioDevices(&audio_devices);
1665 EXPECT_EQ(init_nodes_size, audio_devices.size());
1666 for (size_t i = 0; i < audio_devices.size(); ++i) {
1667 if (audio_devices[i].id == internal_speaker.id)
1668 EXPECT_FALSE(audio_devices[i].active);
1669 else if (audio_devices[i].id == headphone.id)
1670 EXPECT_TRUE(audio_devices[i].active);
1671 else if (audio_devices[i].id == internal_mic.id)
1672 EXPECT_TRUE(audio_devices[i].active);
1673 else
1674 NOTREACHED();
1675 }
1676}
1677
jennyz2d8986d2015-01-15 18:34:071678// This is the case of crbug.com/448924.
1679TEST_F(CrasAudioHandlerTest,
1680 TwoNodesChangedSignalsForLosingTowNodesOnOneUnplug) {
1681 // Set up audio handler with 4 audio_nodes.
1682 AudioNodeList audio_nodes;
1683 AudioNode internal_speaker(kInternalSpeaker);
1684 internal_speaker.active = false;
1685 AudioNode headphone(kHeadphone);
1686 headphone.active = false;
1687 AudioNode internal_mic(kInternalMic);
1688 internal_mic.active = false;
1689 AudioNode micJack(kMicJack);
1690 micJack.active = false;
1691 audio_nodes.push_back(internal_speaker);
1692 audio_nodes.push_back(headphone);
1693 audio_nodes.push_back(internal_mic);
1694 audio_nodes.push_back(micJack);
1695 SetUpCrasAudioHandler(audio_nodes);
1696
1697 // Verify the audio devices size.
1698 AudioDeviceList audio_devices;
1699 cras_audio_handler_->GetAudioDevices(&audio_devices);
1700 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
1701
1702 // Verify the headphone has been selected as the active output.
1703 AudioDevice active_output;
1704 EXPECT_TRUE(
1705 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1706 EXPECT_EQ(kHeadphone.id, active_output.id);
1707 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1708 EXPECT_TRUE(active_output.active);
1709 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1710
1711 // Verify the mic Jack has been selected as the active input.
1712 EXPECT_EQ(micJack.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1713 const AudioDevice* active_input = GetDeviceFromId(micJack.id);
1714 EXPECT_TRUE(active_input->active);
1715 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1716
1717 // Simulate the nodes list in first NodesChanged signal, only headphone is
1718 // removed, other nodes remains the same.
1719 AudioNodeList changed_nodes_1;
1720 internal_speaker.active = false;
1721 changed_nodes_1.push_back(internal_speaker);
1722 internal_mic.active = false;
1723 changed_nodes_1.push_back(internal_mic);
1724 micJack.active = true;
1725 changed_nodes_1.push_back(micJack);
1726
1727 // Simulate the nodes list in second NodesChanged signal, the micJac is
1728 // removed, but the internal_mic is inactive, which does not reflect the
1729 // active status set from the first NodesChanged signal since this was sent
1730 // before cras receives the SetActiveOutputNode from the first NodesChanged
1731 // handling.
1732 AudioNodeList changed_nodes_2;
1733 changed_nodes_2.push_back(internal_speaker);
1734 changed_nodes_2.push_back(internal_mic);
1735
1736 // Simulate AudioNodesChanged signal being fired twice for unplug an audio
1737 // device with both input and output nodes on it.
1738 ChangeAudioNodes(changed_nodes_1);
1739 ChangeAudioNodes(changed_nodes_2);
1740
1741 AudioDeviceList changed_devices;
1742 cras_audio_handler_->GetAudioDevices(&changed_devices);
1743 EXPECT_EQ(2u, changed_devices.size());
1744
1745 // Verify the active output device is set to internal speaker.
1746 EXPECT_EQ(internal_speaker.id,
1747 cras_audio_handler_->GetPrimaryActiveOutputNode());
1748 EXPECT_TRUE(
1749 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1750 EXPECT_EQ(internal_speaker.id, active_output.id);
1751 EXPECT_TRUE(active_output.active);
1752
1753 // Verify the active input device id is set to internal mic.
1754 EXPECT_EQ(internal_mic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1755 const AudioDevice* changed_active_input = GetDeviceFromId(internal_mic.id);
1756 EXPECT_TRUE(changed_active_input->active);
1757}
1758
[email protected]ee38bc42013-07-29 21:41:321759TEST_F(CrasAudioHandlerTest, SetOutputMute) {
1760 AudioNodeList audio_nodes;
1761 audio_nodes.push_back(kInternalSpeaker);
1762 SetUpCrasAudioHandler(audio_nodes);
1763 EXPECT_EQ(0, test_observer_->output_mute_changed_count());
1764
1765 // Mute the device.
1766 cras_audio_handler_->SetOutputMute(true);
1767
1768 // Verify the output is muted, OnOutputMuteChanged event is fired,
1769 // and mute value is saved in the preferences.
1770 EXPECT_TRUE(cras_audio_handler_->IsOutputMuted());
1771 EXPECT_EQ(1, test_observer_->output_mute_changed_count());
1772 AudioDevice speaker(kInternalSpeaker);
1773 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(speaker));
1774
1775 // Unmute the device.
1776 cras_audio_handler_->SetOutputMute(false);
1777
1778 // Verify the output is unmuted, OnOutputMuteChanged event is fired,
1779 // and mute value is saved in the preferences.
1780 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
1781 EXPECT_EQ(2, test_observer_->output_mute_changed_count());
1782 EXPECT_FALSE(audio_pref_handler_->GetMuteValue(speaker));
1783}
1784
1785TEST_F(CrasAudioHandlerTest, SetInputMute) {
1786 AudioNodeList audio_nodes;
1787 audio_nodes.push_back(kInternalMic);
1788 SetUpCrasAudioHandler(audio_nodes);
1789 EXPECT_EQ(0, test_observer_->input_mute_changed_count());
1790
1791 // Mute the device.
1792 cras_audio_handler_->SetInputMute(true);
1793
[email protected]f3dd2962014-04-22 04:48:381794 // Verify the input is muted, OnInputMuteChanged event is fired.
[email protected]ee38bc42013-07-29 21:41:321795 EXPECT_TRUE(cras_audio_handler_->IsInputMuted());
1796 EXPECT_EQ(1, test_observer_->input_mute_changed_count());
[email protected]ee38bc42013-07-29 21:41:321797
1798 // Unmute the device.
1799 cras_audio_handler_->SetInputMute(false);
1800
[email protected]f3dd2962014-04-22 04:48:381801 // Verify the input is unmuted, OnInputMuteChanged event is fired.
[email protected]ee38bc42013-07-29 21:41:321802 EXPECT_FALSE(cras_audio_handler_->IsInputMuted());
1803 EXPECT_EQ(2, test_observer_->input_mute_changed_count());
[email protected]ee38bc42013-07-29 21:41:321804}
1805
1806TEST_F(CrasAudioHandlerTest, SetOutputVolumePercent) {
1807 AudioNodeList audio_nodes;
1808 audio_nodes.push_back(kInternalSpeaker);
1809 SetUpCrasAudioHandler(audio_nodes);
1810 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
1811
1812 cras_audio_handler_->SetOutputVolumePercent(60);
1813
1814 // Verify the output volume is changed to the designated value,
jennyz481292c2015-03-21 01:49:291815 // OnOutputNodeVolumeChanged event is fired, and the device volume value
[email protected]ee38bc42013-07-29 21:41:321816 // is saved the preferences.
1817 const int kVolume = 60;
1818 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent());
1819 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
1820 AudioDevice device;
jennyzb47947f2014-09-25 00:42:041821 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device));
[email protected]ee38bc42013-07-29 21:41:321822 EXPECT_EQ(device.id, kInternalSpeaker.id);
[email protected]140a31452013-09-09 18:54:371823 EXPECT_EQ(kVolume, audio_pref_handler_->GetOutputVolumeValue(&device));
[email protected]ee38bc42013-07-29 21:41:321824}
1825
1826TEST_F(CrasAudioHandlerTest, SetInputGainPercent) {
1827 AudioNodeList audio_nodes;
1828 audio_nodes.push_back(kInternalMic);
1829 SetUpCrasAudioHandler(audio_nodes);
1830 EXPECT_EQ(0, test_observer_->input_gain_changed_count());
1831
1832 cras_audio_handler_->SetInputGainPercent(60);
1833
1834 // Verify the input gain changed to the designated value,
jennyz481292c2015-03-21 01:49:291835 // OnInputNodeGainChanged event is fired, and the device gain value
[email protected]ee38bc42013-07-29 21:41:321836 // is saved in the preferences.
1837 const int kGain = 60;
1838 EXPECT_EQ(kGain, cras_audio_handler_->GetInputGainPercent());
1839 EXPECT_EQ(1, test_observer_->input_gain_changed_count());
1840 AudioDevice internal_mic(kInternalMic);
[email protected]140a31452013-09-09 18:54:371841 EXPECT_EQ(kGain, audio_pref_handler_->GetInputGainValue(&internal_mic));
[email protected]ee38bc42013-07-29 21:41:321842}
1843
1844TEST_F(CrasAudioHandlerTest, SetMuteForDevice) {
1845 AudioNodeList audio_nodes;
1846 audio_nodes.push_back(kInternalSpeaker);
1847 audio_nodes.push_back(kHeadphone);
1848 audio_nodes.push_back(kInternalMic);
1849 audio_nodes.push_back(kUSBMic);
1850 SetUpCrasAudioHandler(audio_nodes);
1851
1852 // Mute the active output device.
jennyzb47947f2014-09-25 00:42:041853 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:321854 cras_audio_handler_->SetMuteForDevice(kHeadphone.id, true);
1855
1856 // Verify the headphone is muted and mute value is saved in the preferences.
1857 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kHeadphone.id));
1858 AudioDevice headphone(kHeadphone);
1859 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(headphone));
1860
1861 // Mute the non-active output device.
1862 cras_audio_handler_->SetMuteForDevice(kInternalSpeaker.id, true);
1863
1864 // Verify the internal speaker is muted and mute value is saved in the
1865 // preferences.
1866 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kInternalSpeaker.id));
1867 AudioDevice internal_speaker(kInternalSpeaker);
1868 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(internal_speaker));
1869
1870 // Mute the active input device.
jennyzb47947f2014-09-25 00:42:041871 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:321872 cras_audio_handler_->SetMuteForDevice(kUSBMic.id, true);
1873
[email protected]f3dd2962014-04-22 04:48:381874 // Verify the USB Mic is muted.
1875 EXPECT_TRUE(cras_audio_handler_->IsInputMutedForDevice(kUSBMic.id));
[email protected]ee38bc42013-07-29 21:41:321876
[email protected]f3dd2962014-04-22 04:48:381877 // Mute the non-active input device should be a no-op, see crbug.com/365050.
[email protected]ee38bc42013-07-29 21:41:321878 cras_audio_handler_->SetMuteForDevice(kInternalMic.id, true);
1879
[email protected]f3dd2962014-04-22 04:48:381880 // Verify IsInputMutedForDevice returns false for non-active input device.
1881 EXPECT_FALSE(cras_audio_handler_->IsInputMutedForDevice(kInternalMic.id));
[email protected]ee38bc42013-07-29 21:41:321882}
1883
1884TEST_F(CrasAudioHandlerTest, SetVolumeGainPercentForDevice) {
1885 AudioNodeList audio_nodes;
1886 audio_nodes.push_back(kInternalSpeaker);
1887 audio_nodes.push_back(kHeadphone);
1888 audio_nodes.push_back(kInternalMic);
1889 audio_nodes.push_back(kUSBMic);
1890 SetUpCrasAudioHandler(audio_nodes);
1891
1892 // Set volume percent for active output device.
1893 const int kHeadphoneVolume = 30;
jennyzb47947f2014-09-25 00:42:041894 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:321895 cras_audio_handler_->SetVolumeGainPercentForDevice(kHeadphone.id,
1896 kHeadphoneVolume);
1897
1898 // Verify the volume percent of headphone is set, and saved in preferences.
1899 EXPECT_EQ(kHeadphoneVolume,
1900 cras_audio_handler_->GetOutputVolumePercentForDevice(
1901 kHeadphone.id));
1902 AudioDevice headphone(kHeadphone);
1903 EXPECT_EQ(kHeadphoneVolume,
[email protected]140a31452013-09-09 18:54:371904 audio_pref_handler_->GetOutputVolumeValue(&headphone));
[email protected]ee38bc42013-07-29 21:41:321905
1906 // Set volume percent for non-active output device.
1907 const int kSpeakerVolume = 60;
1908 cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalSpeaker.id,
1909 kSpeakerVolume);
1910
1911 // Verify the volume percent of speaker is set, and saved in preferences.
1912 EXPECT_EQ(kSpeakerVolume,
1913 cras_audio_handler_->GetOutputVolumePercentForDevice(
1914 kInternalSpeaker.id));
1915 AudioDevice speaker(kInternalSpeaker);
1916 EXPECT_EQ(kSpeakerVolume,
[email protected]140a31452013-09-09 18:54:371917 audio_pref_handler_->GetOutputVolumeValue(&speaker));
[email protected]ee38bc42013-07-29 21:41:321918
1919 // Set gain percent for active input device.
1920 const int kUSBMicGain = 30;
jennyzb47947f2014-09-25 00:42:041921 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:321922 cras_audio_handler_->SetVolumeGainPercentForDevice(kUSBMic.id,
1923 kUSBMicGain);
1924
1925 // Verify the gain percent of USB mic is set, and saved in preferences.
1926 EXPECT_EQ(kUSBMicGain,
1927 cras_audio_handler_->GetOutputVolumePercentForDevice(kUSBMic.id));
1928 AudioDevice usb_mic(kHeadphone);
1929 EXPECT_EQ(kUSBMicGain,
[email protected]140a31452013-09-09 18:54:371930 audio_pref_handler_->GetInputGainValue(&usb_mic));
[email protected]ee38bc42013-07-29 21:41:321931
1932 // Set gain percent for non-active input device.
1933 const int kInternalMicGain = 60;
1934 cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalMic.id,
1935 kInternalMicGain);
1936
1937 // Verify the gain percent of internal mic is set, and saved in preferences.
1938 EXPECT_EQ(kInternalMicGain,
1939 cras_audio_handler_->GetOutputVolumePercentForDevice(
1940 kInternalMic.id));
1941 AudioDevice internal_mic(kInternalMic);
1942 EXPECT_EQ(kInternalMicGain,
[email protected]140a31452013-09-09 18:54:371943 audio_pref_handler_->GetInputGainValue(&internal_mic));
[email protected]ee38bc42013-07-29 21:41:321944}
1945
1946TEST_F(CrasAudioHandlerTest, HandleOtherDeviceType) {
1947 const size_t kNumValidAudioDevices = 4;
1948 AudioNodeList audio_nodes;
1949 audio_nodes.push_back(kInternalSpeaker);
1950 audio_nodes.push_back(kOtherTypeOutput);
1951 audio_nodes.push_back(kInternalMic);
1952 audio_nodes.push_back(kOtherTypeInput);
1953 SetUpCrasAudioHandler(audio_nodes);
1954
1955 // Verify the audio devices size.
1956 AudioDeviceList audio_devices;
1957 cras_audio_handler_->GetAudioDevices(&audio_devices);
1958 EXPECT_EQ(kNumValidAudioDevices, audio_devices.size());
1959
1960 // Verify the internal speaker has been selected as the active output,
1961 // and the output device with some randown unknown type is handled gracefully.
1962 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041963 EXPECT_TRUE(
1964 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:321965 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
jennyzb47947f2014-09-25 00:42:041966 EXPECT_EQ(kInternalSpeaker.id,
1967 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:321968 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1969
1970 // Ensure the internal microphone has been selected as the active input,
1971 // and the input device with some random unknown type is handled gracefully.
1972 AudioDevice active_input;
jennyzb47947f2014-09-25 00:42:041973 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:321974 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1975}
1976
jennyz2cb9eb12014-11-05 19:21:051977TEST_F(CrasAudioHandlerTest, ChangeActiveNodesHotrodInit) {
jennyzb47947f2014-09-25 00:42:041978 AudioNodeList audio_nodes;
jennyz2cb9eb12014-11-05 19:21:051979 audio_nodes.push_back(kHDMIOutput);
1980 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
1981 audio_nodes.push_back(kUSBJabraSpeakerOutput2);
1982 audio_nodes.push_back(kUSBJabraSpeakerInput1);
1983 audio_nodes.push_back(kUSBJabraSpeakerInput2);
1984 audio_nodes.push_back(kUSBCameraInput);
jennyzb47947f2014-09-25 00:42:041985 SetUpCrasAudioHandler(audio_nodes);
1986
1987 // Verify the audio devices size.
1988 AudioDeviceList audio_devices;
1989 cras_audio_handler_->GetAudioDevices(&audio_devices);
1990 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
1991
jennyz2cb9eb12014-11-05 19:21:051992 // Verify only the 1st jabra speaker's output and input are selected as active
1993 // nodes by CrasAudioHandler.
jennyzb47947f2014-09-25 00:42:041994 AudioDevice active_output;
1995 EXPECT_TRUE(
1996 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
jennyz2cb9eb12014-11-05 19:21:051997 EXPECT_EQ(2, GetActiveDeviceCount());
jennyzb47947f2014-09-25 00:42:041998 AudioDevice primary_active_device;
1999 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(
2000 &primary_active_device));
jennyz2cb9eb12014-11-05 19:21:052001 EXPECT_EQ(kUSBJabraSpeakerOutput1.id, primary_active_device.id);
2002 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2003 cras_audio_handler_->GetPrimaryActiveInputNode());
2004
2005 // Set both jabra speakers's input and output nodes to active, this simulate
2006 // the call sent by hotrod initialization process.
2007 test_observer_->reset_active_output_node_changed_count();
2008 test_observer_->reset_active_input_node_changed_count();
2009 CrasAudioHandler::NodeIdList active_nodes;
2010 active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2011 active_nodes.push_back(kUSBJabraSpeakerOutput2.id);
2012 active_nodes.push_back(kUSBJabraSpeakerInput1.id);
2013 active_nodes.push_back(kUSBJabraSpeakerInput2.id);
2014 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2015
2016 // Verify both jabra speakers' input/output nodes are made active.
2017 // num_active_nodes = GetActiveDeviceCount();
2018 EXPECT_EQ(4, GetActiveDeviceCount());
2019 const AudioDevice* active_output_1 =
2020 GetDeviceFromId(kUSBJabraSpeakerOutput1.id);
2021 EXPECT_TRUE(active_output_1->active);
2022 const AudioDevice* active_output_2 =
2023 GetDeviceFromId(kUSBJabraSpeakerOutput2.id);
2024 EXPECT_TRUE(active_output_2->active);
2025 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(
2026 &primary_active_device));
2027 EXPECT_EQ(kUSBJabraSpeakerOutput1.id, primary_active_device.id);
2028 const AudioDevice* active_input_1 =
2029 GetDeviceFromId(kUSBJabraSpeakerInput1.id);
2030 EXPECT_TRUE(active_input_1->active);
2031 const AudioDevice* active_input_2 =
2032 GetDeviceFromId(kUSBJabraSpeakerInput2.id);
2033 EXPECT_TRUE(active_input_2->active);
2034 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2035 cras_audio_handler_->GetPrimaryActiveInputNode());
2036
2037 // Verify only 1 ActiveOutputNodeChanged notification has been sent out
2038 // by calling ChangeActiveNodes.
2039 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
2040 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:042041
2042 // Verify all active devices are the not muted and their volume values are
2043 // the same.
2044 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
jennyz2cb9eb12014-11-05 19:21:052045 EXPECT_FALSE(
2046 cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput1.id));
2047 EXPECT_FALSE(
2048 cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput2.id));
2049 EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(),
2050 cras_audio_handler_->GetOutputVolumePercentForDevice(
2051 kUSBJabraSpeakerOutput1.id));
2052 EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(),
2053 cras_audio_handler_->GetOutputVolumePercentForDevice(
2054 kUSBJabraSpeakerOutput2.id));
jennyzb47947f2014-09-25 00:42:042055
2056 // Adjust the volume of output devices, verify all active nodes are set to
2057 // the same volume.
2058 cras_audio_handler_->SetOutputVolumePercent(25);
2059 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercent());
jennyz2cb9eb12014-11-05 19:21:052060 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercentForDevice(
2061 kUSBJabraSpeakerOutput1.id));
2062 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercentForDevice(
2063 kUSBJabraSpeakerOutput2.id));
2064}
jennyzb47947f2014-09-25 00:42:042065
jennyz2cb9eb12014-11-05 19:21:052066TEST_F(CrasAudioHandlerTest, ChangeActiveNodesHotrodInitWithCameraInputActive) {
2067 AudioNodeList audio_nodes;
2068 audio_nodes.push_back(kHDMIOutput);
2069 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2070 audio_nodes.push_back(kUSBJabraSpeakerOutput2);
2071 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2072 audio_nodes.push_back(kUSBJabraSpeakerInput2);
2073 // Make the camera input to be plugged in later than jabra's input.
2074 AudioNode usb_camera(kUSBCameraInput);
2075 usb_camera.plugged_time = 10000000;
2076 audio_nodes.push_back(usb_camera);
2077 SetUpCrasAudioHandler(audio_nodes);
2078
2079 // Verify the audio devices size.
2080 AudioDeviceList audio_devices;
2081 cras_audio_handler_->GetAudioDevices(&audio_devices);
2082 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2083
2084 // Verify the 1st jabra speaker's output is selected as active output
2085 // node and camera's input is selected active input by CrasAudioHandler.
2086 EXPECT_EQ(2, GetActiveDeviceCount());
2087 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2088 cras_audio_handler_->GetPrimaryActiveOutputNode());
2089 EXPECT_EQ(kUSBCameraInput.id,
2090 cras_audio_handler_->GetPrimaryActiveInputNode());
2091
2092 // Set both jabra speakers's input and output nodes to active, this simulates
2093 // the call sent by hotrod initialization process.
2094 test_observer_->reset_active_output_node_changed_count();
2095 test_observer_->reset_active_input_node_changed_count();
2096 CrasAudioHandler::NodeIdList active_nodes;
2097 active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2098 active_nodes.push_back(kUSBJabraSpeakerOutput2.id);
2099 active_nodes.push_back(kUSBJabraSpeakerInput1.id);
2100 active_nodes.push_back(kUSBJabraSpeakerInput2.id);
2101 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2102
2103 // Verify both jabra speakers' input/output nodes are made active.
2104 // num_active_nodes = GetActiveDeviceCount();
2105 EXPECT_EQ(4, GetActiveDeviceCount());
2106 const AudioDevice* active_output_1 =
2107 GetDeviceFromId(kUSBJabraSpeakerOutput1.id);
2108 EXPECT_TRUE(active_output_1->active);
2109 const AudioDevice* active_output_2 =
2110 GetDeviceFromId(kUSBJabraSpeakerOutput2.id);
2111 EXPECT_TRUE(active_output_2->active);
2112 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2113 cras_audio_handler_->GetPrimaryActiveOutputNode());
2114 const AudioDevice* active_input_1 =
2115 GetDeviceFromId(kUSBJabraSpeakerInput1.id);
2116 EXPECT_TRUE(active_input_1->active);
2117 const AudioDevice* active_input_2 =
2118 GetDeviceFromId(kUSBJabraSpeakerInput2.id);
2119 EXPECT_TRUE(active_input_2->active);
2120 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2121 cras_audio_handler_->GetPrimaryActiveInputNode());
2122
2123 // Verify only 1 ActiveOutputNodeChanged notification has been sent out
2124 // by calling ChangeActiveNodes.
2125 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
2126 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
2127}
2128
2129TEST_F(CrasAudioHandlerTest, ChangeActiveNodesWithFewerActives) {
2130 AudioNodeList audio_nodes;
2131 audio_nodes.push_back(kHDMIOutput);
2132 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2133 audio_nodes.push_back(kUSBJabraSpeakerOutput2);
2134 SetUpCrasAudioHandler(audio_nodes);
2135
2136 // Verify the audio devices size.
2137 AudioDeviceList audio_devices;
2138 cras_audio_handler_->GetAudioDevices(&audio_devices);
2139 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2140
2141 // Set all three nodes to be active.
2142 CrasAudioHandler::NodeIdList active_nodes;
2143 active_nodes.push_back(kHDMIOutput.id);
2144 active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2145 active_nodes.push_back(kUSBJabraSpeakerOutput2.id);
2146 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2147
2148 // Verify all three nodes are active.
2149 EXPECT_EQ(3, GetActiveDeviceCount());
2150 const AudioDevice* active_output_1 = GetDeviceFromId(kHDMIOutput.id);
2151 EXPECT_TRUE(active_output_1->active);
2152 const AudioDevice* active_output_2 =
2153 GetDeviceFromId(kUSBJabraSpeakerOutput1.id);
2154 EXPECT_TRUE(active_output_2->active);
2155 const AudioDevice* active_output_3 =
2156 GetDeviceFromId(kUSBJabraSpeakerOutput2.id);
2157 EXPECT_TRUE(active_output_3->active);
2158
2159 // Now call ChangeActiveDevices with only 2 nodes.
2160 active_nodes.clear();
2161 active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2162 active_nodes.push_back(kUSBJabraSpeakerOutput2.id);
2163 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2164
2165 // Verify only 2 nodes are active.
2166 EXPECT_EQ(2, GetActiveDeviceCount());
2167 const AudioDevice* output_1 = GetDeviceFromId(kHDMIOutput.id);
2168 EXPECT_FALSE(output_1->active);
2169 const AudioDevice* output_2 = GetDeviceFromId(kUSBJabraSpeakerOutput1.id);
2170 EXPECT_TRUE(output_2->active);
2171 const AudioDevice* output_3 = GetDeviceFromId(kUSBJabraSpeakerOutput2.id);
2172 EXPECT_TRUE(output_3->active);
2173}
2174
2175TEST_F(CrasAudioHandlerTest, HotrodInitWithSingleJabra) {
2176 // Simulates the hotrod initializated with a single jabra device and
2177 // CrasAudioHandler selected jabra input/output as active devices.
2178 AudioNodeList audio_nodes;
2179 audio_nodes.push_back(kHDMIOutput);
2180 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2181 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2182 audio_nodes.push_back(kUSBCameraInput);
2183 SetUpCrasAudioHandler(audio_nodes);
2184
2185 // Verify the audio devices size.
2186 AudioDeviceList audio_devices;
2187 cras_audio_handler_->GetAudioDevices(&audio_devices);
2188 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2189
2190 // Verify the jabra speaker's output and input are selected as active nodes
2191 // by CrasAudioHandler.
2192 EXPECT_EQ(2, GetActiveDeviceCount());
2193 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2194 cras_audio_handler_->GetPrimaryActiveOutputNode());
2195 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2196 cras_audio_handler_->GetPrimaryActiveInputNode());
2197}
2198
2199TEST_F(CrasAudioHandlerTest,
2200 ChangeActiveNodesHotrodInitWithSingleJabraCameraPlugInLater) {
2201 AudioNodeList audio_nodes;
2202 audio_nodes.push_back(kHDMIOutput);
2203 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2204 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2205 AudioNode usb_camera(kUSBCameraInput);
2206 usb_camera.plugged_time = 10000000;
2207 audio_nodes.push_back(usb_camera);
2208 SetUpCrasAudioHandler(audio_nodes);
2209
2210 // Verify the audio devices size.
2211 AudioDeviceList audio_devices;
2212 cras_audio_handler_->GetAudioDevices(&audio_devices);
2213 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2214
2215 // Verify the jabra speaker's output is selected as active output, and
2216 // camera's input is selected as active input by CrasAudioHandler
2217 EXPECT_EQ(2, GetActiveDeviceCount());
2218 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2219 cras_audio_handler_->GetPrimaryActiveOutputNode());
2220 EXPECT_EQ(kUSBCameraInput.id,
2221 cras_audio_handler_->GetPrimaryActiveInputNode());
2222
2223 // Simulate hotrod app call to set jabra input as active device with only
2224 // jabra input node in the active node list, which does not conform to the
2225 // new SetActiveDevices protocol, but just show we can still handle it if
2226 // this happens.
2227 CrasAudioHandler::NodeIdList active_nodes;
2228 active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2229 active_nodes.push_back(kUSBJabraSpeakerInput1.id);
2230 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2231
2232 // Verify the jabra speaker's output is selected as active output, and
2233 // jabra's input is selected as active input.
2234 EXPECT_EQ(2, GetActiveDeviceCount());
2235 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2236 cras_audio_handler_->GetPrimaryActiveOutputNode());
2237 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2238 cras_audio_handler_->GetPrimaryActiveInputNode());
2239}
2240
2241TEST_F(CrasAudioHandlerTest,
2242 ChangeActiveNodesHotrodInitWithSingleJabraCameraPlugInLaterOldCall) {
2243 AudioNodeList audio_nodes;
2244 audio_nodes.push_back(kHDMIOutput);
2245 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2246 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2247 AudioNode usb_camera(kUSBCameraInput);
2248 usb_camera.plugged_time = 10000000;
2249 audio_nodes.push_back(usb_camera);
2250 SetUpCrasAudioHandler(audio_nodes);
2251
2252 // Verify the audio devices size.
2253 AudioDeviceList audio_devices;
2254 cras_audio_handler_->GetAudioDevices(&audio_devices);
2255 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2256
2257 // Verify the jabra speaker's output is selected as active output, and
2258 // camera's input is selected as active input by CrasAudioHandler
2259 EXPECT_EQ(2, GetActiveDeviceCount());
2260 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2261 cras_audio_handler_->GetPrimaryActiveOutputNode());
2262 EXPECT_EQ(kUSBCameraInput.id,
2263 cras_audio_handler_->GetPrimaryActiveInputNode());
2264
2265 // Simulate hotrod app call to set jabra input as active device with only
2266 // jabra input node in the active node list, which does not conform to the
2267 // new SetActiveDevices protocol, but just show we can still handle it if
2268 // this happens.
2269 CrasAudioHandler::NodeIdList active_nodes;
2270 active_nodes.push_back(kUSBJabraSpeakerInput1.id);
2271 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2272
2273 // Verify the jabra speaker's output is selected as active output, and
2274 // jabra's input is selected as active input.
2275 EXPECT_EQ(2, GetActiveDeviceCount());
2276 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2277 cras_audio_handler_->GetPrimaryActiveOutputNode());
2278 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2279 cras_audio_handler_->GetPrimaryActiveInputNode());
2280}
2281
2282TEST_F(CrasAudioHandlerTest,
2283 ChangeActiveNodesHotrodInitWithSingleJabraChangeOutput) {
2284 AudioNodeList audio_nodes;
2285 audio_nodes.push_back(kHDMIOutput);
2286 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2287 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2288 audio_nodes.push_back(kUSBCameraInput);
2289 SetUpCrasAudioHandler(audio_nodes);
2290
2291 // Verify the audio devices size.
2292 AudioDeviceList audio_devices;
2293 cras_audio_handler_->GetAudioDevices(&audio_devices);
2294 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2295
2296 // Verify the jabra speaker's output and input are selected as active output
2297 // by CrasAudioHandler.
2298 EXPECT_EQ(2, GetActiveDeviceCount());
2299 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2300 cras_audio_handler_->GetPrimaryActiveOutputNode());
2301 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2302 cras_audio_handler_->GetPrimaryActiveInputNode());
2303
2304 // Simulate hotrod app call SetActiveDevices to change active output
2305 // with only complete list of active nodes passed in, which is the new
2306 // way of hotrod app.
2307 CrasAudioHandler::NodeIdList active_nodes;
2308 active_nodes.push_back(kHDMIOutput.id);
2309 active_nodes.push_back(kUSBJabraSpeakerInput1.id);
2310 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2311
2312 // Verify the jabra speaker's output is selected as active output, and
2313 // jabra's input is selected as active input.
2314 EXPECT_EQ(2, GetActiveDeviceCount());
2315 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2316 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2317 cras_audio_handler_->GetPrimaryActiveInputNode());
2318}
2319
2320TEST_F(CrasAudioHandlerTest,
2321 ChangeActiveNodesHotrodInitWithSingleJabraChangeOutputOldCall) {
2322 AudioNodeList audio_nodes;
2323 audio_nodes.push_back(kHDMIOutput);
2324 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2325 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2326 audio_nodes.push_back(kUSBCameraInput);
2327 SetUpCrasAudioHandler(audio_nodes);
2328
2329 // Verify the audio devices size.
2330 AudioDeviceList audio_devices;
2331 cras_audio_handler_->GetAudioDevices(&audio_devices);
2332 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2333
2334 // Verify the jabra speaker's output and input are selected as active output
2335 // by CrasAudioHandler.
2336 EXPECT_EQ(2, GetActiveDeviceCount());
2337 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2338 cras_audio_handler_->GetPrimaryActiveOutputNode());
2339 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2340 cras_audio_handler_->GetPrimaryActiveInputNode());
2341
2342 // Simulate hotrod app call SetActiveDevices to change active output
2343 // with only a single active output nodes passed in, which is the old
2344 // way of hotrod app.
2345 CrasAudioHandler::NodeIdList active_nodes;
2346 active_nodes.push_back(kHDMIOutput.id);
2347 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2348
2349 // Verify the jabra speaker's output is selected as active output, and
2350 // jabra's input is selected as active input.
2351 EXPECT_EQ(2, GetActiveDeviceCount());
2352 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2353 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2354 cras_audio_handler_->GetPrimaryActiveInputNode());
jennyzb47947f2014-09-25 00:42:042355}
2356
mukai307596b62014-11-24 19:56:592357TEST_F(CrasAudioHandlerTest, NoMoreAudioInputDevices) {
2358 // Some device like chromebox does not have the internal input device. The
2359 // active devices should be reset when the user plugs a device and then
2360 // unplugs it to such device.
2361
2362 AudioNodeList audio_nodes;
2363 audio_nodes.push_back(kInternalSpeaker);
2364 SetUpCrasAudioHandler(audio_nodes);
2365
2366 EXPECT_EQ(0ULL, cras_audio_handler_->GetPrimaryActiveInputNode());
2367
2368 audio_nodes.push_back(kMicJack);
2369 ChangeAudioNodes(audio_nodes);
2370
2371 EXPECT_EQ(kMicJack.id, cras_audio_handler_->GetPrimaryActiveInputNode());
2372 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
2373 test_observer_->reset_active_input_node_changed_count();
2374
2375 audio_nodes.pop_back();
2376 ChangeAudioNodes(audio_nodes);
2377 EXPECT_EQ(0ULL, cras_audio_handler_->GetPrimaryActiveInputNode());
2378 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
2379}
2380
jennyz731ba262015-02-05 19:00:032381// Test the case in which an HDMI output is plugged in with other higher
2382// priority
2383// output devices already plugged and user has manually selected an active
2384// output.
2385// The hotplug of hdmi output should not change user's selection of active
2386// device.
2387// crbug.com/447826.
2388TEST_F(CrasAudioHandlerTest, HotPlugHDMINotChangeActiveOutput) {
2389 AudioNodeList audio_nodes;
2390 AudioNode internal_speaker(kInternalSpeaker);
2391 audio_nodes.push_back(internal_speaker);
2392 AudioNode usb_headset(kUSBHeadphone1);
2393 usb_headset.plugged_time = 80000000;
2394 audio_nodes.push_back(usb_headset);
2395 SetUpCrasAudioHandler(audio_nodes);
2396
2397 // Verify the audio devices size.
2398 AudioDeviceList audio_devices;
2399 cras_audio_handler_->GetAudioDevices(&audio_devices);
2400 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2401
2402 // Verify the USB headset is selected as active output by default.
2403 EXPECT_EQ(usb_headset.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2404
2405 // Manually set the active output to internal speaker.
2406 AudioDevice internal_output(kInternalSpeaker);
2407 cras_audio_handler_->SwitchToDevice(internal_output, true);
2408
2409 // Verify the active output is switched to internal speaker.
2410 EXPECT_EQ(internal_speaker.id,
2411 cras_audio_handler_->GetPrimaryActiveOutputNode());
2412 EXPECT_LT(kInternalSpeaker.plugged_time, usb_headset.plugged_time);
2413 const AudioDevice* usb_device = GetDeviceFromId(usb_headset.id);
2414 EXPECT_FALSE(usb_device->active);
2415
2416 // Plug in HDMI output.
2417 audio_nodes.clear();
2418 internal_speaker.active = true;
2419 audio_nodes.push_back(internal_speaker);
2420 usb_headset.active = false;
2421 audio_nodes.push_back(usb_headset);
2422 AudioNode hdmi(kHDMIOutput);
2423 hdmi.plugged_time = 90000000;
2424 audio_nodes.push_back(hdmi);
2425 ChangeAudioNodes(audio_nodes);
2426
2427 // The active output should not change.
2428 EXPECT_EQ(kInternalSpeaker.id,
2429 cras_audio_handler_->GetPrimaryActiveOutputNode());
2430}
2431
[email protected]ee38bc42013-07-29 21:41:322432} // namespace chromeos