blob: 9c9ba3cd8b997fb05f656a4b9a160f3953008914 [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
avi6e1a22d2015-12-21 03:43:207#include <stddef.h>
8#include <stdint.h>
9
dcheng0a6e80c2016-04-08 18:37:3810#include <memory>
11
jennyzc770dc772015-06-29 23:46:0612#include "base/bind.h"
avi6e1a22d2015-12-21 03:43:2013#include "base/macros.h"
[email protected]ee38bc42013-07-29 21:41:3214#include "base/memory/ref_counted.h"
[email protected]ee38bc42013-07-29 21:41:3215#include "base/message_loop/message_loop.h"
jennyzc770dc772015-06-29 23:46:0616#include "base/run_loop.h"
gabd85cc46b2016-05-11 18:23:2017#include "base/threading/thread_task_runner_handle.h"
[email protected]ee38bc42013-07-29 21:41:3218#include "base/values.h"
jennyzbd236742016-03-02 20:15:5219#include "chromeos/audio/audio_devices_pref_handler.h"
[email protected]ee38bc42013-07-29 21:41:3220#include "chromeos/audio/audio_devices_pref_handler_stub.h"
21#include "chromeos/dbus/audio_node.h"
[email protected]ee38bc42013-07-29 21:41:3222#include "chromeos/dbus/dbus_thread_manager.h"
satoruxcca5cc12014-10-27 16:33:5923#include "chromeos/dbus/fake_cras_audio_client.h"
[email protected]ee38bc42013-07-29 21:41:3224#include "testing/gtest/include/gtest/gtest.h"
25
26namespace chromeos {
oshima2aaee552015-07-09 16:50:2627namespace {
[email protected]ee38bc42013-07-29 21:41:3228
avi6e1a22d2015-12-21 03:43:2029const uint64_t kInternalSpeakerId = 10001;
30const uint64_t kHeadphoneId = 10002;
31const uint64_t kInternalMicId = 10003;
32const uint64_t kUSBMicId = 10004;
33const uint64_t kBluetoothHeadsetId = 10005;
34const uint64_t kHDMIOutputId = 10006;
35const uint64_t kUSBHeadphoneId1 = 10007;
36const uint64_t kUSBHeadphoneId2 = 10008;
37const uint64_t kMicJackId = 10009;
38const uint64_t kKeyboardMicId = 10010;
39const uint64_t kOtherTypeOutputId = 90001;
40const uint64_t kOtherTypeInputId = 90002;
41const uint64_t kUSBJabraSpeakerOutputId1 = 90003;
42const uint64_t kUSBJabraSpeakerOutputId2 = 90004;
43const uint64_t kUSBJabraSpeakerInputId1 = 90005;
44const uint64_t kUSBJabraSpeakerInputId2 = 90006;
45const uint64_t kUSBCameraInputId = 90007;
[email protected]ee38bc42013-07-29 21:41:3246
hychaofe100d542016-01-12 10:17:5247const AudioNode kInternalSpeaker(false,
48 kInternalSpeakerId,
49 kInternalSpeakerId,
50 "Fake Speaker",
51 "INTERNAL_SPEAKER",
52 "Speaker",
53 false,
54 0);
[email protected]ee38bc42013-07-29 21:41:3255
hychaofe100d542016-01-12 10:17:5256const AudioNode kHeadphone(false,
57 kHeadphoneId,
58 kHeadphoneId,
59 "Fake Headphone",
60 "HEADPHONE",
61 "Headphone",
62 false,
63 0);
[email protected]ee38bc42013-07-29 21:41:3264
hychaofe100d542016-01-12 10:17:5265const AudioNode kInternalMic(true,
66 kInternalMicId,
67 kInternalMicId,
68 "Fake Mic",
69 "INTERNAL_MIC",
70 "Internal Mic",
71 false,
72 0);
[email protected]ee38bc42013-07-29 21:41:3273
hychaofe100d542016-01-12 10:17:5274const AudioNode kMicJack(true,
75 kMicJackId,
76 kMicJackId,
77 "Fake Mic Jack",
78 "MIC",
79 "Mic Jack",
80 false,
81 0);
[email protected]ce631ee2014-01-23 03:37:0682
hychaofe100d542016-01-12 10:17:5283const AudioNode kUSBMic(true,
84 kUSBMicId,
85 kUSBMicId,
86 "Fake USB Mic",
87 "USB",
88 "USB Microphone",
89 false,
90 0);
[email protected]ee38bc42013-07-29 21:41:3291
hychaofe100d542016-01-12 10:17:5292const AudioNode kKeyboardMic(true,
93 kKeyboardMicId,
94 kKeyboardMicId,
95 "Fake Keyboard Mic",
96 "KEYBOARD_MIC",
97 "Keyboard Mic",
98 false,
99 0);
[email protected]f9a49d342014-07-29 23:47:20100
hychaofe100d542016-01-12 10:17:52101const AudioNode kOtherTypeOutput(false,
102 kOtherTypeOutputId,
103 kOtherTypeOutputId,
104 "Output Device",
105 "SOME_OTHER_TYPE",
106 "Other Type Output Device",
107 false,
108 0);
[email protected]ee38bc42013-07-29 21:41:32109
hychaofe100d542016-01-12 10:17:52110const AudioNode kOtherTypeInput(true,
111 kOtherTypeInputId,
112 kOtherTypeInputId,
113 "Input Device",
114 "SOME_OTHER_TYPE",
115 "Other Type Input Device",
116 false,
117 0);
[email protected]ee38bc42013-07-29 21:41:32118
jennyz481292c2015-03-21 01:49:29119const AudioNode kBluetoothHeadset(false,
120 kBluetoothHeadsetId,
hychaofe100d542016-01-12 10:17:52121 kBluetoothHeadsetId,
jennyz481292c2015-03-21 01:49:29122 "Bluetooth Headset",
123 "BLUETOOTH",
124 "Bluetooth Headset 1",
125 false,
126 0);
[email protected]ee38bc42013-07-29 21:41:32127
jennyz481292c2015-03-21 01:49:29128const AudioNode kHDMIOutput(false,
129 kHDMIOutputId,
hychaofe100d542016-01-12 10:17:52130 kHDMIOutputId,
jennyz481292c2015-03-21 01:49:29131 "HDMI output",
132 "HDMI",
133 "HDMI output",
134 false,
135 0);
[email protected]ee38bc42013-07-29 21:41:32136
jennyz481292c2015-03-21 01:49:29137const AudioNode kUSBHeadphone1(false,
138 kUSBHeadphoneId1,
hychaofe100d542016-01-12 10:17:52139 kUSBHeadphoneId1,
jennyz481292c2015-03-21 01:49:29140 "USB Headphone",
141 "USB",
142 "USB Headphone 1",
143 false,
144 0);
[email protected]ee38bc42013-07-29 21:41:32145
jennyz481292c2015-03-21 01:49:29146const AudioNode kUSBHeadphone2(false,
147 kUSBHeadphoneId2,
hychaofe100d542016-01-12 10:17:52148 kUSBHeadphoneId2,
jennyz481292c2015-03-21 01:49:29149 "USB Headphone",
150 "USB",
151 "USB Headphone 1",
152 false,
153 0);
[email protected]ee38bc42013-07-29 21:41:32154
jennyz2cb9eb12014-11-05 19:21:05155const AudioNode kUSBJabraSpeakerOutput1(false,
156 kUSBJabraSpeakerOutputId1,
hychaofe100d542016-01-12 10:17:52157 kUSBJabraSpeakerOutputId1,
jennyz2cb9eb12014-11-05 19:21:05158 "Jabra Speaker 1",
159 "USB",
160 "Jabra Speaker 1",
161 false,
162 0);
163
164const AudioNode kUSBJabraSpeakerOutput2(false,
165 kUSBJabraSpeakerOutputId2,
hychaofe100d542016-01-12 10:17:52166 kUSBJabraSpeakerOutputId2,
jennyz2cb9eb12014-11-05 19:21:05167 "Jabra Speaker 2",
168 "USB",
169 "Jabra Speaker 2",
170 false,
171 0);
172
173const AudioNode kUSBJabraSpeakerInput1(true,
174 kUSBJabraSpeakerInputId1,
hychaofe100d542016-01-12 10:17:52175 kUSBJabraSpeakerInputId1,
jennyz2cb9eb12014-11-05 19:21:05176 "Jabra Speaker 1",
177 "USB",
178 "Jabra Speaker",
179 false,
180 0);
181
182const AudioNode kUSBJabraSpeakerInput2(true,
183 kUSBJabraSpeakerInputId2,
hychaofe100d542016-01-12 10:17:52184 kUSBJabraSpeakerInputId2,
jennyz2cb9eb12014-11-05 19:21:05185 "Jabra Speaker 2",
186 "USB",
187 "Jabra Speaker 2",
188 false,
189 0);
190
191const AudioNode kUSBCameraInput(true,
192 kUSBCameraInputId,
hychaofe100d542016-01-12 10:17:52193 kUSBCameraInputId,
jennyz2cb9eb12014-11-05 19:21:05194 "USB Camera",
195 "USB",
196 "USB Camera",
197 false,
198 0);
[email protected]ee38bc42013-07-29 21:41:32199
200class TestObserver : public chromeos::CrasAudioHandler::AudioObserver {
201 public:
jennyzc79ccda2015-07-28 23:04:35202 TestObserver()
203 : active_output_node_changed_count_(0),
204 active_input_node_changed_count_(0),
205 audio_nodes_changed_count_(0),
206 output_mute_changed_count_(0),
207 input_mute_changed_count_(0),
208 output_volume_changed_count_(0),
209 input_gain_changed_count_(0),
warx74524622016-03-31 01:07:21210 output_mute_by_system_(false),
211 output_channel_remixing_changed_count_(0) {}
[email protected]ee38bc42013-07-29 21:41:32212
213 int active_output_node_changed_count() const {
214 return active_output_node_changed_count_;
215 }
216
jennyz2cb9eb12014-11-05 19:21:05217 void reset_active_output_node_changed_count() {
218 active_output_node_changed_count_ = 0;
219 }
220
[email protected]ee38bc42013-07-29 21:41:32221 int active_input_node_changed_count() const {
222 return active_input_node_changed_count_;
223 }
224
jennyz2cb9eb12014-11-05 19:21:05225 void reset_active_input_node_changed_count() {
mukai307596b62014-11-24 19:56:59226 active_input_node_changed_count_ = 0;
jennyz2cb9eb12014-11-05 19:21:05227 }
228
[email protected]ee38bc42013-07-29 21:41:32229 int audio_nodes_changed_count() const {
230 return audio_nodes_changed_count_;
231 }
232
233 int output_mute_changed_count() const {
234 return output_mute_changed_count_;
235 }
236
jennyzc79ccda2015-07-28 23:04:35237 void reset_output_mute_changed_count() { input_mute_changed_count_ = 0; }
238
[email protected]ee38bc42013-07-29 21:41:32239 int input_mute_changed_count() const {
240 return input_mute_changed_count_;
241 }
242
243 int output_volume_changed_count() const {
244 return output_volume_changed_count_;
245 }
246
jennyz96526d02016-06-22 17:03:43247 void reset_output_volume_changed_count() { output_volume_changed_count_ = 0; }
248
[email protected]ee38bc42013-07-29 21:41:32249 int input_gain_changed_count() const {
250 return input_gain_changed_count_;
251 }
252
jennyzc79ccda2015-07-28 23:04:35253 bool output_mute_by_system() const { return output_mute_by_system_; }
254
warx74524622016-03-31 01:07:21255 int output_channel_remixing_changed_count() const {
256 return output_channel_remixing_changed_count_;
257 }
258
dchengae98daa2015-01-21 20:30:49259 ~TestObserver() override {}
[email protected]ee38bc42013-07-29 21:41:32260
261 protected:
262 // chromeos::CrasAudioHandler::AudioObserver overrides.
dchengae98daa2015-01-21 20:30:49263 void OnActiveOutputNodeChanged() override {
[email protected]ee38bc42013-07-29 21:41:32264 ++active_output_node_changed_count_;
265 }
266
dchengae98daa2015-01-21 20:30:49267 void OnActiveInputNodeChanged() override {
[email protected]ee38bc42013-07-29 21:41:32268 ++active_input_node_changed_count_;
269 }
270
dchengae98daa2015-01-21 20:30:49271 void OnAudioNodesChanged() override { ++audio_nodes_changed_count_; }
[email protected]ee38bc42013-07-29 21:41:32272
jennyzc79ccda2015-07-28 23:04:35273 void OnOutputMuteChanged(bool /* mute_on */, bool system_adjust) override {
jennyz170e7fb2015-03-30 23:37:39274 ++output_mute_changed_count_;
jennyzc79ccda2015-07-28 23:04:35275 output_mute_by_system_ = system_adjust;
jennyz170e7fb2015-03-30 23:37:39276 }
[email protected]ee38bc42013-07-29 21:41:32277
jennyz170e7fb2015-03-30 23:37:39278 void OnInputMuteChanged(bool /* mute_on */) override {
279 ++input_mute_changed_count_;
280 }
[email protected]ee38bc42013-07-29 21:41:32281
avi6e1a22d2015-12-21 03:43:20282 void OnOutputNodeVolumeChanged(uint64_t /* node_id */,
jennyz481292c2015-03-21 01:49:29283 int /* volume */) override {
284 ++output_volume_changed_count_;
285 }
[email protected]ee38bc42013-07-29 21:41:32286
avi6e1a22d2015-12-21 03:43:20287 void OnInputNodeGainChanged(uint64_t /* node_id */, int /* gain */) override {
jennyz481292c2015-03-21 01:49:29288 ++input_gain_changed_count_;
289 }
[email protected]ee38bc42013-07-29 21:41:32290
warx74524622016-03-31 01:07:21291 void OnOuputChannelRemixingChanged(bool /* mono_on */) override {
292 ++output_channel_remixing_changed_count_;
293 }
294
[email protected]ee38bc42013-07-29 21:41:32295 private:
296 int active_output_node_changed_count_;
297 int active_input_node_changed_count_;
298 int audio_nodes_changed_count_;
299 int output_mute_changed_count_;
300 int input_mute_changed_count_;
301 int output_volume_changed_count_;
302 int input_gain_changed_count_;
jennyzc79ccda2015-07-28 23:04:35303 bool output_mute_by_system_; // output mute state adjusted by system.
warx74524622016-03-31 01:07:21304 int output_channel_remixing_changed_count_;
[email protected]ee38bc42013-07-29 21:41:32305
306 DISALLOW_COPY_AND_ASSIGN(TestObserver);
307};
308
oshima2aaee552015-07-09 16:50:26309} // namespace
310
[email protected]ee38bc42013-07-29 21:41:32311class CrasAudioHandlerTest : public testing::Test {
312 public:
313 CrasAudioHandlerTest() : cras_audio_handler_(NULL),
satoruxcca5cc12014-10-27 16:33:59314 fake_cras_audio_client_(NULL) {
[email protected]ee38bc42013-07-29 21:41:32315 }
dchengae98daa2015-01-21 20:30:49316 ~CrasAudioHandlerTest() override {}
[email protected]ee38bc42013-07-29 21:41:32317
dchengae98daa2015-01-21 20:30:49318 void SetUp() override {}
[email protected]ee38bc42013-07-29 21:41:32319
dchengae98daa2015-01-21 20:30:49320 void TearDown() override {
[email protected]ee38bc42013-07-29 21:41:32321 cras_audio_handler_->RemoveAudioObserver(test_observer_.get());
322 test_observer_.reset();
323 CrasAudioHandler::Shutdown();
324 audio_pref_handler_ = NULL;
325 DBusThreadManager::Shutdown();
326 }
327
328 void SetUpCrasAudioHandler(const AudioNodeList& audio_nodes) {
zelidrag29fe3382014-08-27 01:44:48329 DBusThreadManager::Initialize();
satoruxcca5cc12014-10-27 16:33:59330 fake_cras_audio_client_ = static_cast<FakeCrasAudioClient*>(
[email protected]ee38bc42013-07-29 21:41:32331 DBusThreadManager::Get()->GetCrasAudioClient());
satoruxcca5cc12014-10-27 16:33:59332 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes);
[email protected]ee38bc42013-07-29 21:41:32333 audio_pref_handler_ = new AudioDevicesPrefHandlerStub();
334 CrasAudioHandler::Initialize(audio_pref_handler_);
335 cras_audio_handler_ = CrasAudioHandler::Get();
336 test_observer_.reset(new TestObserver);
337 cras_audio_handler_->AddAudioObserver(test_observer_.get());
fdorayf5b47fd12016-09-13 14:12:36338 base::RunLoop().RunUntilIdle();
[email protected]ee38bc42013-07-29 21:41:32339 }
340
jennyzbd236742016-03-02 20:15:52341 // Set up cras audio handlers with |audio_nodes| and set the active state of
342 // |active_device_in_pref| as active and |activate_by_user| in the pref,
343 // and rest of nodes in |audio_nodes_in_pref| as inactive.
344 void SetupCrasAudioHandlerWithActiveNodeInPref(
345 const AudioNodeList& audio_nodes,
346 const AudioNodeList& audio_nodes_in_pref,
347 const AudioDevice& active_device_in_pref,
348 bool activate_by_user) {
349 DBusThreadManager::Initialize();
350 fake_cras_audio_client_ = static_cast<FakeCrasAudioClient*>(
351 DBusThreadManager::Get()->GetCrasAudioClient());
352 audio_pref_handler_ = new AudioDevicesPrefHandlerStub();
353 bool active;
354 for (const AudioNode& node : audio_nodes_in_pref) {
355 active = node.id == active_device_in_pref.id;
356 audio_pref_handler_->SetDeviceActive(AudioDevice(node), active,
357 activate_by_user);
358 }
359
360 bool activate_by;
361 EXPECT_TRUE(audio_pref_handler_->GetDeviceActive(active_device_in_pref,
362 &active, &activate_by));
363 EXPECT_TRUE(active);
364 EXPECT_EQ(activate_by, activate_by_user);
365
366 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes);
367 CrasAudioHandler::Initialize(audio_pref_handler_);
368
369 cras_audio_handler_ = CrasAudioHandler::Get();
370 test_observer_.reset(new TestObserver);
371 cras_audio_handler_->AddAudioObserver(test_observer_.get());
fdorayf5b47fd12016-09-13 14:12:36372 base::RunLoop().RunUntilIdle();
jennyzbd236742016-03-02 20:15:52373 }
374
jennyzb47947f2014-09-25 00:42:04375 void SetUpCrasAudioHandlerWithPrimaryActiveNode(
376 const AudioNodeList& audio_nodes,
377 const AudioNode& primary_active_node) {
378 DBusThreadManager::Initialize();
satoruxcca5cc12014-10-27 16:33:59379 fake_cras_audio_client_ = static_cast<FakeCrasAudioClient*>(
jennyzb47947f2014-09-25 00:42:04380 DBusThreadManager::Get()->GetCrasAudioClient());
satoruxcca5cc12014-10-27 16:33:59381 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes);
382 fake_cras_audio_client_->SetActiveOutputNode(primary_active_node.id),
jennyzb47947f2014-09-25 00:42:04383 audio_pref_handler_ = new AudioDevicesPrefHandlerStub();
384 CrasAudioHandler::Initialize(audio_pref_handler_);
385 cras_audio_handler_ = CrasAudioHandler::Get();
386 test_observer_.reset(new TestObserver);
387 cras_audio_handler_->AddAudioObserver(test_observer_.get());
fdorayf5b47fd12016-09-13 14:12:36388 base::RunLoop().RunUntilIdle();
jennyzb47947f2014-09-25 00:42:04389 }
390
[email protected]ee38bc42013-07-29 21:41:32391 void ChangeAudioNodes(const AudioNodeList& audio_nodes) {
satoruxcca5cc12014-10-27 16:33:59392 fake_cras_audio_client_->SetAudioNodesAndNotifyObserversForTesting(
derat3698adff2014-08-29 16:10:43393 audio_nodes);
fdorayf5b47fd12016-09-13 14:12:36394 base::RunLoop().RunUntilIdle();
[email protected]ee38bc42013-07-29 21:41:32395 }
396
avi6e1a22d2015-12-21 03:43:20397 const AudioDevice* GetDeviceFromId(uint64_t id) {
jennyzb47947f2014-09-25 00:42:04398 return cras_audio_handler_->GetDeviceFromId(id);
399 }
400
jennyz2cb9eb12014-11-05 19:21:05401 int GetActiveDeviceCount() const {
402 int num_active_nodes = 0;
403 AudioDeviceList audio_devices;
404 cras_audio_handler_->GetAudioDevices(&audio_devices);
405 for (size_t i = 0; i < audio_devices.size(); ++i) {
406 if (audio_devices[i].active)
407 ++num_active_nodes;
408 }
409 return num_active_nodes;
410 }
411
jennyzc770dc772015-06-29 23:46:06412 void SetActiveHDMIRediscover() {
413 cras_audio_handler_->SetActiveHDMIOutoutRediscoveringIfNecessary(true);
414 }
415
416 void SetHDMIRediscoverGracePeriodDuration(int duration_in_ms) {
417 cras_audio_handler_->SetHDMIRediscoverGracePeriodForTesting(duration_in_ms);
418 }
419
420 bool IsDuringHDMIRediscoverGracePeriod() {
421 return cras_audio_handler_->hdmi_rediscovering();
422 }
423
warxfd7c37b2016-08-05 19:38:07424 void RestartAudioClient() {
425 cras_audio_handler_->AudioClientRestarted();
fdorayf5b47fd12016-09-13 14:12:36426 base::RunLoop().RunUntilIdle();
warxfd7c37b2016-08-05 19:38:07427 }
428
[email protected]ee38bc42013-07-29 21:41:32429 protected:
430 base::MessageLoopForUI message_loop_;
431 CrasAudioHandler* cras_audio_handler_; // Not owned.
satoruxcca5cc12014-10-27 16:33:59432 FakeCrasAudioClient* fake_cras_audio_client_; // Not owned.
dcheng0a6e80c2016-04-08 18:37:38433 std::unique_ptr<TestObserver> test_observer_;
[email protected]ee38bc42013-07-29 21:41:32434 scoped_refptr<AudioDevicesPrefHandlerStub> audio_pref_handler_;
435
436 private:
437 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandlerTest);
438};
439
jennyzc770dc772015-06-29 23:46:06440class HDMIRediscoverWaiter {
441 public:
442 HDMIRediscoverWaiter(CrasAudioHandlerTest* cras_audio_handler_test,
443 int grace_period_duration_in_ms)
444 : cras_audio_handler_test_(cras_audio_handler_test),
445 grace_period_duration_in_ms_(grace_period_duration_in_ms) {}
446
447 void WaitUntilTimeOut(int wait_duration_in_ms) {
448 base::RunLoop run_loop;
449 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
450 FROM_HERE, run_loop.QuitClosure(),
451 base::TimeDelta::FromMilliseconds(wait_duration_in_ms));
452 run_loop.Run();
453 }
454
455 void CheckHDMIRediscoverGracePeriodEnd(const base::Closure& quit_loop_func) {
456 if (!cras_audio_handler_test_->IsDuringHDMIRediscoverGracePeriod()) {
457 quit_loop_func.Run();
458 return;
459 }
460 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
461 FROM_HERE,
462 base::Bind(&HDMIRediscoverWaiter::CheckHDMIRediscoverGracePeriodEnd,
463 base::Unretained(this), quit_loop_func),
464 base::TimeDelta::FromMilliseconds(grace_period_duration_in_ms_ / 4));
465 }
466
467 void WaitUntilHDMIRediscoverGracePeriodEnd() {
468 base::RunLoop run_loop;
469 CheckHDMIRediscoverGracePeriodEnd(run_loop.QuitClosure());
470 run_loop.Run();
471 }
472
473 private:
474 CrasAudioHandlerTest* cras_audio_handler_test_; // not owned
475 int grace_period_duration_in_ms_;
476
477 DISALLOW_COPY_AND_ASSIGN(HDMIRediscoverWaiter);
478};
479
[email protected]ee38bc42013-07-29 21:41:32480TEST_F(CrasAudioHandlerTest, InitializeWithOnlyDefaultAudioDevices) {
481 AudioNodeList audio_nodes;
482 audio_nodes.push_back(kInternalSpeaker);
483 audio_nodes.push_back(kInternalMic);
484 SetUpCrasAudioHandler(audio_nodes);
485
486 // Verify the audio devices size.
487 AudioDeviceList audio_devices;
488 cras_audio_handler_->GetAudioDevices(&audio_devices);
489 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
490
491 // Verify the internal speaker has been selected as the active output.
492 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04493 EXPECT_TRUE(
494 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32495 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04496 EXPECT_EQ(kInternalSpeaker.id,
497 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32498 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
499
500 // Ensure the internal microphone has been selected as the active input.
501 AudioDevice active_input;
jennyzb47947f2014-09-25 00:42:04502 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:32503 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
504}
505
506TEST_F(CrasAudioHandlerTest, InitializeWithAlternativeAudioDevices) {
507 AudioNodeList audio_nodes;
508 audio_nodes.push_back(kInternalSpeaker);
509 audio_nodes.push_back(kHeadphone);
510 audio_nodes.push_back(kInternalMic);
511 audio_nodes.push_back(kUSBMic);
512 SetUpCrasAudioHandler(audio_nodes);
513
514 // Verify the audio devices size.
515 AudioDeviceList audio_devices;
516 cras_audio_handler_->GetAudioDevices(&audio_devices);
517 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
518
519 // Verify the headphone has been selected as the active output.
520 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04521 EXPECT_TRUE(
522 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32523 EXPECT_EQ(kHeadphone.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04524 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32525 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
526
527 // Ensure the USB microphone has been selected as the active input.
jennyzb47947f2014-09-25 00:42:04528 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:32529 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
530}
531
[email protected]f9a49d342014-07-29 23:47:20532TEST_F(CrasAudioHandlerTest, InitializeWithKeyboardMic) {
533 AudioNodeList audio_nodes;
534 audio_nodes.push_back(kInternalSpeaker);
535 audio_nodes.push_back(kInternalMic);
536 audio_nodes.push_back(kKeyboardMic);
537 SetUpCrasAudioHandler(audio_nodes);
538
539 // Verify the audio devices size.
540 AudioDeviceList audio_devices;
541 cras_audio_handler_->GetAudioDevices(&audio_devices);
542 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
jennyzb47947f2014-09-25 00:42:04543 EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic());
[email protected]f9a49d342014-07-29 23:47:20544
545 // Verify the internal speaker has been selected as the active output.
546 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04547 EXPECT_TRUE(
548 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]f9a49d342014-07-29 23:47:20549 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04550 EXPECT_EQ(kInternalSpeaker.id,
551 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]f9a49d342014-07-29 23:47:20552 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
553
554 // Ensure the internal microphone has been selected as the active input,
555 // not affected by keyboard mic.
556 AudioDevice active_input;
jennyzb47947f2014-09-25 00:42:04557 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]f9a49d342014-07-29 23:47:20558 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
jennyzb47947f2014-09-25 00:42:04559 const AudioDevice* keyboard_mic = GetDeviceFromId(kKeyboardMic.id);
560 EXPECT_FALSE(keyboard_mic->active);
561}
562
563TEST_F(CrasAudioHandlerTest, SetKeyboardMicActive) {
564 AudioNodeList audio_nodes;
565 audio_nodes.push_back(kInternalMic);
566 audio_nodes.push_back(kKeyboardMic);
567 SetUpCrasAudioHandler(audio_nodes);
568
569 // Verify the audio devices size.
570 AudioDeviceList audio_devices;
571 cras_audio_handler_->GetAudioDevices(&audio_devices);
572 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
573 EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic());
574
575 // Ensure the internal microphone has been selected as the active input,
576 // not affected by keyboard mic.
577 AudioDevice active_input;
578 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
579 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
580 const AudioDevice* keyboard_mic = GetDeviceFromId(kKeyboardMic.id);
581 EXPECT_FALSE(keyboard_mic->active);
582
583 // Make keyboard mic active.
584 cras_audio_handler_->SetKeyboardMicActive(true);
585 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
586 const AudioDevice* active_keyboard_mic = GetDeviceFromId(kKeyboardMic.id);
587 EXPECT_TRUE(active_keyboard_mic->active);
588
589 // Make keyboard mic inactive.
590 cras_audio_handler_->SetKeyboardMicActive(false);
591 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
592 const AudioDevice* inactive_keyboard_mic = GetDeviceFromId(kKeyboardMic.id);
593 EXPECT_FALSE(inactive_keyboard_mic->active);
[email protected]f9a49d342014-07-29 23:47:20594}
595
jennyzb46664f2016-03-08 18:18:10596TEST_F(CrasAudioHandlerTest, KeyboardMicNotSetAsPrimaryActive) {
597 AudioNodeList audio_nodes;
598 audio_nodes.push_back(kKeyboardMic);
599 SetUpCrasAudioHandler(audio_nodes);
600
601 // Verify keyboard mic is not set as primary active input.
602 AudioDeviceList audio_devices;
603 cras_audio_handler_->GetAudioDevices(&audio_devices);
604 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
605 EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic());
606 EXPECT_EQ(0u, cras_audio_handler_->GetPrimaryActiveInputNode());
607
608 // Verify the internal mic is set as primary input.
609 audio_nodes.push_back(kInternalMic);
610 ChangeAudioNodes(audio_nodes);
611 cras_audio_handler_->GetAudioDevices(&audio_devices);
612 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
613 EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic());
614 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
615}
616
[email protected]ee38bc42013-07-29 21:41:32617TEST_F(CrasAudioHandlerTest, SwitchActiveOutputDevice) {
618 AudioNodeList audio_nodes;
619 audio_nodes.push_back(kInternalSpeaker);
620 audio_nodes.push_back(kHeadphone);
621 SetUpCrasAudioHandler(audio_nodes);
622 AudioDeviceList audio_devices;
623 cras_audio_handler_->GetAudioDevices(&audio_devices);
624 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
625
626 // Verify the initial active output device is headphone.
627 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
628 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04629 EXPECT_TRUE(
630 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32631 EXPECT_EQ(kHeadphone.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04632 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32633
634 // Switch the active output to internal speaker.
635 AudioDevice internal_speaker(kInternalSpeaker);
jennyzbd236742016-03-02 20:15:52636 cras_audio_handler_->SwitchToDevice(internal_speaker, true,
637 CrasAudioHandler::ACTIVATE_BY_USER);
[email protected]ee38bc42013-07-29 21:41:32638
639 // Verify the active output is switched to internal speaker, and the
640 // ActiveOutputNodeChanged event is fired.
641 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04642 EXPECT_TRUE(
643 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32644 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04645 EXPECT_EQ(kInternalSpeaker.id,
646 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32647}
648
649TEST_F(CrasAudioHandlerTest, SwitchActiveInputDevice) {
650 AudioNodeList audio_nodes;
651 audio_nodes.push_back(kInternalMic);
652 audio_nodes.push_back(kUSBMic);
653 SetUpCrasAudioHandler(audio_nodes);
654 AudioDeviceList audio_devices;
655 cras_audio_handler_->GetAudioDevices(&audio_devices);
656 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
657
658 // Verify the initial active input device is USB mic.
659 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:04660 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:32661
662 // Switch the active input to internal mic.
663 AudioDevice internal_mic(kInternalMic);
jennyzbd236742016-03-02 20:15:52664 cras_audio_handler_->SwitchToDevice(internal_mic, true,
665 CrasAudioHandler::ACTIVATE_BY_USER);
[email protected]ee38bc42013-07-29 21:41:32666
667 // Verify the active output is switched to internal speaker, and the active
668 // ActiveInputNodeChanged event is fired.
669 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:04670 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:32671}
672
673TEST_F(CrasAudioHandlerTest, PlugHeadphone) {
674 // Set up initial audio devices, only with internal speaker.
675 AudioNodeList audio_nodes;
676 audio_nodes.push_back(kInternalSpeaker);
677 SetUpCrasAudioHandler(audio_nodes);
678 const size_t init_nodes_size = audio_nodes.size();
679
680 // Verify the audio devices size.
681 AudioDeviceList audio_devices;
682 cras_audio_handler_->GetAudioDevices(&audio_devices);
683 EXPECT_EQ(init_nodes_size, audio_devices.size());
684 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
685
686 // Verify the internal speaker has been selected as the active output.
687 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
688 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04689 EXPECT_TRUE(
690 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32691 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04692 EXPECT_EQ(kInternalSpeaker.id,
693 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32694 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
695
696 // Plug the headphone.
[email protected]4642d7792013-09-18 20:27:17697 audio_nodes.clear();
698 AudioNode internal_speaker(kInternalSpeaker);
699 internal_speaker.active = true;
700 audio_nodes.push_back(internal_speaker);
[email protected]ee38bc42013-07-29 21:41:32701 audio_nodes.push_back(kHeadphone);
702 ChangeAudioNodes(audio_nodes);
703
704 // Verify the AudioNodesChanged event is fired and new audio device is added.
705 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
706 cras_audio_handler_->GetAudioDevices(&audio_devices);
707 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
708
709 // Verify the active output device is switched to headphone and
710 // ActiveOutputChanged event is fired.
711 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04712 EXPECT_TRUE(
713 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32714 EXPECT_EQ(kHeadphone.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04715 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32716 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
717}
718
719TEST_F(CrasAudioHandlerTest, UnplugHeadphone) {
720 // Set up initial audio devices, with internal speaker and headphone.
721 AudioNodeList audio_nodes;
722 audio_nodes.push_back(kInternalSpeaker);
723 audio_nodes.push_back(kHeadphone);
724 SetUpCrasAudioHandler(audio_nodes);
725 const size_t init_nodes_size = audio_nodes.size();
726
727 // Verify the audio devices size.
728 AudioDeviceList audio_devices;
729 cras_audio_handler_->GetAudioDevices(&audio_devices);
730 EXPECT_EQ(init_nodes_size, audio_devices.size());
731 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
732
733 // Verify the headphone has been selected as the active output.
734 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
735 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04736 EXPECT_TRUE(
737 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32738 EXPECT_EQ(kHeadphone.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04739 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32740 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
741
742 // Unplug the headphone.
743 audio_nodes.clear();
744 audio_nodes.push_back(kInternalSpeaker);
745 ChangeAudioNodes(audio_nodes);
746
747 // Verify the AudioNodesChanged event is fired and one audio device is
748 // removed.
749 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
750 cras_audio_handler_->GetAudioDevices(&audio_devices);
751 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
752
753 // Verify the active output device is switched to internal speaker and
754 // ActiveOutputChanged event is fired.
755 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04756 EXPECT_TRUE(
757 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32758 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04759 EXPECT_EQ(kInternalSpeaker.id,
760 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32761 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
762}
763
764TEST_F(CrasAudioHandlerTest, InitializeWithBluetoothHeadset) {
765 AudioNodeList audio_nodes;
766 audio_nodes.push_back(kInternalSpeaker);
767 audio_nodes.push_back(kBluetoothHeadset);
768 SetUpCrasAudioHandler(audio_nodes);
769
770 // Verify the audio devices size.
771 AudioDeviceList audio_devices;
772 cras_audio_handler_->GetAudioDevices(&audio_devices);
773 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
774 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
775
776 // Verify the bluetooth headset has been selected as the active output.
777 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
778 AudioDevice active_output;
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(kBluetoothHeadset.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04782 EXPECT_EQ(kBluetoothHeadset.id,
783 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32784 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
785}
786
787TEST_F(CrasAudioHandlerTest, ConnectAndDisconnectBluetoothHeadset) {
788 // Initialize with internal speaker and headphone.
789 AudioNodeList audio_nodes;
790 audio_nodes.push_back(kInternalSpeaker);
791 audio_nodes.push_back(kHeadphone);
792 SetUpCrasAudioHandler(audio_nodes);
793 const size_t init_nodes_size = audio_nodes.size();
794
795 // Verify the audio devices size.
796 AudioDeviceList audio_devices;
797 cras_audio_handler_->GetAudioDevices(&audio_devices);
798 EXPECT_EQ(init_nodes_size, audio_devices.size());
799 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
800
801 // Verify the headphone is selected as the active output initially.
802 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
803 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04804 EXPECT_TRUE(
805 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32806 EXPECT_EQ(kHeadphone.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04807 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32808 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
809
810 // Connect to bluetooth headset. Since it is plugged in later than
811 // headphone, active output should be switched to it.
812 audio_nodes.clear();
813 audio_nodes.push_back(kInternalSpeaker);
814 AudioNode headphone(kHeadphone);
815 headphone.plugged_time = 80000000;
816 headphone.active = true;
817 audio_nodes.push_back(headphone);
818 AudioNode bluetooth_headset(kBluetoothHeadset);
819 bluetooth_headset.plugged_time = 90000000;
820 audio_nodes.push_back(bluetooth_headset);
821 ChangeAudioNodes(audio_nodes);
822
823 // Verify the AudioNodesChanged event is fired and new audio device is added.
824 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
825 cras_audio_handler_->GetAudioDevices(&audio_devices);
826 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
827
828 // Verify the active output device is switched to bluetooth headset, and
829 // ActiveOutputChanged event is fired.
830 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04831 EXPECT_TRUE(
832 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32833 EXPECT_EQ(kBluetoothHeadset.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04834 EXPECT_EQ(kBluetoothHeadset.id,
835 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32836 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
837
838 // Disconnect bluetooth headset.
839 audio_nodes.clear();
840 audio_nodes.push_back(kInternalSpeaker);
[email protected]4642d7792013-09-18 20:27:17841 headphone.active = false;
[email protected]ee38bc42013-07-29 21:41:32842 audio_nodes.push_back(headphone);
843 ChangeAudioNodes(audio_nodes);
844
845 // Verify the AudioNodesChanged event is fired and one audio device is
846 // removed.
847 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
848 cras_audio_handler_->GetAudioDevices(&audio_devices);
849 EXPECT_EQ(init_nodes_size, audio_devices.size());
850
851 // Verify the active output device is switched to headphone, and
852 // ActiveOutputChanged event is fired.
853 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04854 EXPECT_TRUE(
855 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32856 EXPECT_EQ(kHeadphone.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04857 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32858 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
859}
860
861TEST_F(CrasAudioHandlerTest, InitializeWithHDMIOutput) {
862 AudioNodeList audio_nodes;
863 audio_nodes.push_back(kInternalSpeaker);
864 audio_nodes.push_back(kHDMIOutput);
865 SetUpCrasAudioHandler(audio_nodes);
866
867 // Verify the audio devices size.
868 AudioDeviceList audio_devices;
869 cras_audio_handler_->GetAudioDevices(&audio_devices);
870 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
871 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
872
873 // Verify the HDMI device has been selected as the active output.
874 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
875 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04876 EXPECT_TRUE(
877 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32878 EXPECT_EQ(kHDMIOutput.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04879 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32880 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
881}
882
883TEST_F(CrasAudioHandlerTest, ConnectAndDisconnectHDMIOutput) {
884 // Initialize with internal speaker.
885 AudioNodeList audio_nodes;
886 audio_nodes.push_back(kInternalSpeaker);
887 SetUpCrasAudioHandler(audio_nodes);
888 const size_t init_nodes_size = audio_nodes.size();
889
890 // Verify the audio devices size.
891 AudioDeviceList audio_devices;
892 cras_audio_handler_->GetAudioDevices(&audio_devices);
893 EXPECT_EQ(init_nodes_size, audio_devices.size());
894 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
895
896 // Verify the internal speaker is selected as the active output initially.
897 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
898 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04899 EXPECT_TRUE(
900 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32901 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04902 EXPECT_EQ(kInternalSpeaker.id,
903 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32904 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
905
906 // Connect to HDMI output.
907 audio_nodes.clear();
908 AudioNode internal_speaker(kInternalSpeaker);
909 internal_speaker.active = true;
910 internal_speaker.plugged_time = 80000000;
911 audio_nodes.push_back(internal_speaker);
912 AudioNode hdmi(kHDMIOutput);
913 hdmi.plugged_time = 90000000;
914 audio_nodes.push_back(hdmi);
915 ChangeAudioNodes(audio_nodes);
916
917 // Verify the AudioNodesChanged event is fired and new audio device is added.
918 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
919 cras_audio_handler_->GetAudioDevices(&audio_devices);
920 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
921
922 // Verify the active output device is switched to hdmi output, and
923 // ActiveOutputChanged event is fired.
924 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04925 EXPECT_TRUE(
926 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32927 EXPECT_EQ(kHDMIOutput.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04928 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32929 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
930
931 // Disconnect hdmi headset.
932 audio_nodes.clear();
933 audio_nodes.push_back(kInternalSpeaker);
934 ChangeAudioNodes(audio_nodes);
935
936 // Verify the AudioNodesChanged event is fired and one audio device is
937 // removed.
938 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
939 cras_audio_handler_->GetAudioDevices(&audio_devices);
940 EXPECT_EQ(init_nodes_size, audio_devices.size());
941
942 // Verify the active output device is switched to internal speaker, and
943 // ActiveOutputChanged event is fired.
944 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04945 EXPECT_TRUE(
946 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32947 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04948 EXPECT_EQ(kInternalSpeaker.id,
949 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32950 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
951}
952
953TEST_F(CrasAudioHandlerTest, HandleHeadphoneAndHDMIOutput) {
954 // Initialize with internal speaker, headphone and HDMI output.
955 AudioNodeList audio_nodes;
956 audio_nodes.push_back(kInternalSpeaker);
957 audio_nodes.push_back(kHeadphone);
958 audio_nodes.push_back(kHDMIOutput);
959 SetUpCrasAudioHandler(audio_nodes);
960 const size_t init_nodes_size = audio_nodes.size();
961
962 // Verify the audio devices size.
963 AudioDeviceList audio_devices;
964 cras_audio_handler_->GetAudioDevices(&audio_devices);
965 EXPECT_EQ(init_nodes_size, audio_devices.size());
966 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
967
968 // Verify the headphone is selected as the active output initially.
969 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
970 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04971 EXPECT_TRUE(
972 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32973 EXPECT_EQ(kHeadphone.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04974 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32975 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
976
977 // Disconnect HDMI output.
978 audio_nodes.clear();
979 audio_nodes.push_back(kInternalSpeaker);
980 audio_nodes.push_back(kHDMIOutput);
981 ChangeAudioNodes(audio_nodes);
982
983 // Verify the AudioNodesChanged event is fired and one audio device is
984 // removed.
985 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
986 cras_audio_handler_->GetAudioDevices(&audio_devices);
987 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
988
989 // Verify the active output device is switched to HDMI output, and
990 // ActiveOutputChanged event is fired.
991 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04992 EXPECT_TRUE(
993 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:32994 EXPECT_EQ(kHDMIOutput.id, active_output.id);
jennyzb47947f2014-09-25 00:42:04995 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32996 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
997}
998
999TEST_F(CrasAudioHandlerTest, InitializeWithUSBHeadphone) {
1000 AudioNodeList audio_nodes;
1001 audio_nodes.push_back(kInternalSpeaker);
1002 audio_nodes.push_back(kUSBHeadphone1);
1003 SetUpCrasAudioHandler(audio_nodes);
1004
1005 // Verify the audio devices size.
1006 AudioDeviceList audio_devices;
1007 cras_audio_handler_->GetAudioDevices(&audio_devices);
1008 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
1009 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1010
1011 // Verify the usb headphone has been selected as the active output.
1012 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1013 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041014 EXPECT_TRUE(
1015 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:321016 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
jennyzb47947f2014-09-25 00:42:041017 EXPECT_EQ(kUSBHeadphone1.id,
1018 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:321019 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1020}
1021
1022TEST_F(CrasAudioHandlerTest, PlugAndUnplugUSBHeadphone) {
1023 // Initialize with internal speaker.
1024 AudioNodeList audio_nodes;
1025 audio_nodes.push_back(kInternalSpeaker);
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 internal speaker 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]ee38bc42013-07-29 21:41:321040 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
jennyzb47947f2014-09-25 00:42:041041 EXPECT_EQ(kInternalSpeaker.id,
1042 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:321043 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
1044
1045 // Plug in usb headphone
1046 audio_nodes.clear();
1047 AudioNode internal_speaker(kInternalSpeaker);
1048 internal_speaker.active = true;
1049 internal_speaker.plugged_time = 80000000;
1050 audio_nodes.push_back(internal_speaker);
1051 AudioNode usb_headphone(kUSBHeadphone1);
1052 usb_headphone.plugged_time = 90000000;
1053 audio_nodes.push_back(usb_headphone);
1054 ChangeAudioNodes(audio_nodes);
1055
1056 // Verify the AudioNodesChanged event is fired and new audio device is added.
1057 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1058 cras_audio_handler_->GetAudioDevices(&audio_devices);
1059 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1060
1061 // Verify the active output device is switched to usb headphone, and
1062 // ActiveOutputChanged event is fired.
1063 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041064 EXPECT_TRUE(
1065 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:321066 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
jennyzb47947f2014-09-25 00:42:041067 EXPECT_EQ(kUSBHeadphone1.id,
1068 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:321069 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1070
1071 // Unplug usb headphone.
1072 audio_nodes.clear();
1073 audio_nodes.push_back(kInternalSpeaker);
1074 ChangeAudioNodes(audio_nodes);
1075
1076 // Verify the AudioNodesChanged event is fired and one audio device is
1077 // removed.
1078 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1079 cras_audio_handler_->GetAudioDevices(&audio_devices);
1080 EXPECT_EQ(init_nodes_size, audio_devices.size());
1081
1082 // Verify the active output device is switched to internal speaker, and
1083 // ActiveOutputChanged event is fired.
1084 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041085 EXPECT_TRUE(
1086 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:321087 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
jennyzb47947f2014-09-25 00:42:041088 EXPECT_EQ(kInternalSpeaker.id,
1089 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:321090 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
1091}
1092
1093TEST_F(CrasAudioHandlerTest, HandleMultipleUSBHeadphones) {
1094 // Initialize with internal speaker and one usb headphone.
1095 AudioNodeList audio_nodes;
1096 audio_nodes.push_back(kInternalSpeaker);
1097 audio_nodes.push_back(kUSBHeadphone1);
1098 SetUpCrasAudioHandler(audio_nodes);
1099 const size_t init_nodes_size = audio_nodes.size();
1100
1101 // Verify the audio devices size.
1102 AudioDeviceList audio_devices;
1103 cras_audio_handler_->GetAudioDevices(&audio_devices);
1104 EXPECT_EQ(init_nodes_size, audio_devices.size());
1105 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1106
1107 // Verify the usb headphone is selected as the active output initially.
1108 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1109 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041110 EXPECT_TRUE(
1111 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:321112 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
jennyzb47947f2014-09-25 00:42:041113 EXPECT_EQ(kUSBHeadphone1.id,
1114 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:321115 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1116
1117 // Plug in another usb headphone.
1118 audio_nodes.clear();
1119 audio_nodes.push_back(kInternalSpeaker);
1120 AudioNode usb_headphone_1(kUSBHeadphone1);
1121 usb_headphone_1.active = true;
1122 usb_headphone_1.plugged_time = 80000000;
1123 audio_nodes.push_back(usb_headphone_1);
1124 AudioNode usb_headphone_2(kUSBHeadphone2);
1125 usb_headphone_2.plugged_time = 90000000;
1126 audio_nodes.push_back(usb_headphone_2);
1127 ChangeAudioNodes(audio_nodes);
1128
1129 // Verify the AudioNodesChanged event is fired and new audio device is added.
1130 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1131 cras_audio_handler_->GetAudioDevices(&audio_devices);
1132 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1133
1134 // Verify the active output device is switched to the 2nd usb headphone, which
1135 // is plugged later, and ActiveOutputChanged event is fired.
1136 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041137 EXPECT_TRUE(
1138 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:321139 EXPECT_EQ(kUSBHeadphone2.id, active_output.id);
jennyzb47947f2014-09-25 00:42:041140 EXPECT_EQ(kUSBHeadphone2.id,
1141 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:321142 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1143
1144 // Unplug the 2nd usb headphone.
1145 audio_nodes.clear();
1146 audio_nodes.push_back(kInternalSpeaker);
1147 audio_nodes.push_back(kUSBHeadphone1);
1148 ChangeAudioNodes(audio_nodes);
1149
1150 // Verify the AudioNodesChanged event is fired and one audio device is
1151 // removed.
1152 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1153 cras_audio_handler_->GetAudioDevices(&audio_devices);
1154 EXPECT_EQ(init_nodes_size, audio_devices.size());
1155
1156 // Verify the active output device is switched to the first usb headphone, and
1157 // ActiveOutputChanged event is fired.
1158 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041159 EXPECT_TRUE(
1160 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:321161 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
jennyzb47947f2014-09-25 00:42:041162 EXPECT_EQ(kUSBHeadphone1.id,
1163 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:321164 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1165}
1166
[email protected]ca473252013-08-15 11:48:391167TEST_F(CrasAudioHandlerTest, UnplugUSBHeadphonesWithActiveSpeaker) {
1168 // Initialize with internal speaker and one usb headphone.
1169 AudioNodeList audio_nodes;
1170 audio_nodes.push_back(kInternalSpeaker);
1171 audio_nodes.push_back(kUSBHeadphone1);
1172 SetUpCrasAudioHandler(audio_nodes);
1173 const size_t init_nodes_size = audio_nodes.size();
1174
1175 // Verify the audio devices size.
1176 AudioDeviceList audio_devices;
1177 cras_audio_handler_->GetAudioDevices(&audio_devices);
1178 EXPECT_EQ(init_nodes_size, audio_devices.size());
1179 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1180
1181 // Verify the usb headphone is selected as the active output initially.
1182 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1183 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041184 EXPECT_TRUE(
1185 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ca473252013-08-15 11:48:391186 EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
jennyzb47947f2014-09-25 00:42:041187 EXPECT_EQ(kUSBHeadphone1.id,
1188 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ca473252013-08-15 11:48:391189 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1190
1191 // Plug in the headphone jack.
1192 audio_nodes.clear();
1193 audio_nodes.push_back(kInternalSpeaker);
1194 AudioNode usb_headphone_1(kUSBHeadphone1);
1195 usb_headphone_1.active = true;
1196 usb_headphone_1.plugged_time = 80000000;
1197 audio_nodes.push_back(usb_headphone_1);
1198 AudioNode headphone_jack(kHeadphone);
1199 headphone_jack.plugged_time = 90000000;
1200 audio_nodes.push_back(headphone_jack);
1201 ChangeAudioNodes(audio_nodes);
1202
1203 // Verify the AudioNodesChanged event is fired and new audio device is added.
1204 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1205 cras_audio_handler_->GetAudioDevices(&audio_devices);
1206 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1207
1208 // Verify the active output device is switched to the headphone jack, which
1209 // is plugged later, and ActiveOutputChanged event is fired.
1210 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041211 EXPECT_TRUE(
1212 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ca473252013-08-15 11:48:391213 EXPECT_EQ(kHeadphone.id, active_output.id);
jennyzb47947f2014-09-25 00:42:041214 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ca473252013-08-15 11:48:391215 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1216
1217 // Select the speaker to be the active output device.
1218 AudioDevice internal_speaker(kInternalSpeaker);
jennyzbd236742016-03-02 20:15:521219 cras_audio_handler_->SwitchToDevice(internal_speaker, true,
1220 CrasAudioHandler::ACTIVATE_BY_USER);
[email protected]ca473252013-08-15 11:48:391221
1222 // Verify the active output is switched to internal speaker, and the
1223 // ActiveOutputNodeChanged event is fired.
1224 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041225 EXPECT_TRUE(
1226 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ca473252013-08-15 11:48:391227 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
jennyzb47947f2014-09-25 00:42:041228 EXPECT_EQ(kInternalSpeaker.id,
1229 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ca473252013-08-15 11:48:391230
1231 // Unplug the usb headphone.
1232 audio_nodes.clear();
1233 AudioNode internal_speaker_node(kInternalSpeaker);
1234 internal_speaker_node.active = true;
1235 internal_speaker_node.plugged_time = 70000000;
1236 audio_nodes.push_back(internal_speaker_node);
1237 headphone_jack.active = false;
1238 audio_nodes.push_back(headphone_jack);
1239 ChangeAudioNodes(audio_nodes);
1240
1241 // Verify the AudioNodesChanged event is fired and one audio device is
1242 // removed.
1243 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1244 cras_audio_handler_->GetAudioDevices(&audio_devices);
1245 EXPECT_EQ(init_nodes_size, audio_devices.size());
1246
1247 // Verify the active output device remains to be speaker.
1248 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041249 EXPECT_TRUE(
1250 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ca473252013-08-15 11:48:391251 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
jennyzb47947f2014-09-25 00:42:041252 EXPECT_EQ(kInternalSpeaker.id,
1253 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ca473252013-08-15 11:48:391254 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1255}
1256
[email protected]d413fcc2013-08-16 23:20:031257TEST_F(CrasAudioHandlerTest, OneActiveAudioOutputAfterLoginNewUserSession) {
1258 // This tests the case found with crbug.com/273271.
1259 // Initialize with internal speaker, bluetooth headphone and headphone jack
1260 // for a new chrome session after user signs out from the previous session.
1261 // Headphone jack is plugged in later than bluetooth headphone, but bluetooth
1262 // headphone is selected as the active output by user from previous user
1263 // session.
1264 AudioNodeList audio_nodes;
1265 audio_nodes.push_back(kInternalSpeaker);
1266 AudioNode bluetooth_headphone(kBluetoothHeadset);
1267 bluetooth_headphone.active = true;
1268 bluetooth_headphone.plugged_time = 70000000;
1269 audio_nodes.push_back(bluetooth_headphone);
1270 AudioNode headphone_jack(kHeadphone);
1271 headphone_jack.plugged_time = 80000000;
1272 audio_nodes.push_back(headphone_jack);
jennyzb47947f2014-09-25 00:42:041273 SetUpCrasAudioHandlerWithPrimaryActiveNode(audio_nodes, bluetooth_headphone);
[email protected]d413fcc2013-08-16 23:20:031274 const size_t init_nodes_size = audio_nodes.size();
1275
1276 // Verify the audio devices size.
1277 AudioDeviceList audio_devices;
1278 cras_audio_handler_->GetAudioDevices(&audio_devices);
1279 EXPECT_EQ(init_nodes_size, audio_devices.size());
1280 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1281
1282 // Verify the headphone jack is selected as the active output and all other
1283 // audio devices are not active.
1284 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1285 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041286 EXPECT_TRUE(
1287 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]d413fcc2013-08-16 23:20:031288 EXPECT_EQ(kHeadphone.id, active_output.id);
jennyzb47947f2014-09-25 00:42:041289 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]d413fcc2013-08-16 23:20:031290 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1291 for (size_t i = 0; i < audio_devices.size(); ++i) {
1292 if (audio_devices[i].id != kHeadphone.id)
1293 EXPECT_FALSE(audio_devices[i].active);
1294 }
1295}
1296
[email protected]110babd2013-09-17 01:46:021297TEST_F(CrasAudioHandlerTest, BluetoothSpeakerIdChangedOnFly) {
1298 // Initialize with internal speaker and bluetooth headset.
1299 AudioNodeList audio_nodes;
1300 audio_nodes.push_back(kInternalSpeaker);
1301 audio_nodes.push_back(kBluetoothHeadset);
1302 SetUpCrasAudioHandler(audio_nodes);
1303 const size_t init_nodes_size = audio_nodes.size();
1304
1305 // Verify the audio devices size.
1306 AudioDeviceList audio_devices;
1307 cras_audio_handler_->GetAudioDevices(&audio_devices);
1308 EXPECT_EQ(init_nodes_size, audio_devices.size());
1309 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1310
1311 // Verify the bluetooth headset is selected as the active output and all other
1312 // audio devices are not active.
1313 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1314 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041315 EXPECT_TRUE(
1316 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]110babd2013-09-17 01:46:021317 EXPECT_EQ(kBluetoothHeadset.id, active_output.id);
jennyzb47947f2014-09-25 00:42:041318 EXPECT_EQ(kBluetoothHeadset.id,
1319 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]110babd2013-09-17 01:46:021320 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1321
1322 // Cras changes the bluetooth headset's id on the fly.
1323 audio_nodes.clear();
1324 AudioNode internal_speaker(kInternalSpeaker);
1325 internal_speaker.active = false;
1326 audio_nodes.push_back(internal_speaker);
1327 AudioNode bluetooth_headphone(kBluetoothHeadset);
1328 // Change bluetooth headphone id.
1329 bluetooth_headphone.id = kBluetoothHeadsetId + 20000;
1330 bluetooth_headphone.active = false;
1331 audio_nodes.push_back(bluetooth_headphone);
1332 ChangeAudioNodes(audio_nodes);
1333
1334 // Verify NodesChanged event is fired, and the audio devices size is not
1335 // changed.
1336 audio_devices.clear();
1337 cras_audio_handler_->GetAudioDevices(&audio_devices);
1338 EXPECT_EQ(init_nodes_size, audio_devices.size());
1339 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1340
1341 // Verify ActiveOutputNodeChanged event is fired, and active device should be
1342 // bluetooth headphone.
1343 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041344 EXPECT_TRUE(
1345 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]110babd2013-09-17 01:46:021346 EXPECT_EQ(bluetooth_headphone.id, active_output.id);
1347}
1348
[email protected]ee38bc42013-07-29 21:41:321349TEST_F(CrasAudioHandlerTest, PlugUSBMic) {
1350 // Set up initial audio devices, only with internal mic.
1351 AudioNodeList audio_nodes;
1352 audio_nodes.push_back(kInternalMic);
1353 SetUpCrasAudioHandler(audio_nodes);
1354 const size_t init_nodes_size = audio_nodes.size();
1355
1356 // Verify the audio devices size.
1357 AudioDeviceList audio_devices;
1358 cras_audio_handler_->GetAudioDevices(&audio_devices);
1359 EXPECT_EQ(init_nodes_size, audio_devices.size());
1360 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1361
[email protected]babc24b2013-09-09 23:50:231362 // Verify the internal mic is selected as the active input.
[email protected]ee38bc42013-07-29 21:41:321363 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041364 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:321365 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1366
1367 // Plug the USB Mic.
[email protected]4642d7792013-09-18 20:27:171368 audio_nodes.clear();
1369 AudioNode internal_mic(kInternalMic);
1370 internal_mic.active = true;
1371 audio_nodes.push_back(internal_mic);
[email protected]ee38bc42013-07-29 21:41:321372 audio_nodes.push_back(kUSBMic);
1373 ChangeAudioNodes(audio_nodes);
1374
1375 // Verify the AudioNodesChanged event is fired and new audio device is added.
1376 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1377 cras_audio_handler_->GetAudioDevices(&audio_devices);
1378 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1379
1380 // Verify the active input device is switched to USB mic and
1381 // and ActiveInputChanged event is fired.
1382 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041383 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:321384 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1385}
1386
1387TEST_F(CrasAudioHandlerTest, UnplugUSBMic) {
1388 // Set up initial audio devices, with internal mic and USB Mic.
1389 AudioNodeList audio_nodes;
1390 audio_nodes.push_back(kInternalMic);
1391 audio_nodes.push_back(kUSBMic);
1392 SetUpCrasAudioHandler(audio_nodes);
1393 const size_t init_nodes_size = audio_nodes.size();
1394
1395 // Verify the audio devices size.
1396 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1397 AudioDeviceList audio_devices;
1398 cras_audio_handler_->GetAudioDevices(&audio_devices);
1399 EXPECT_EQ(init_nodes_size, audio_devices.size());
1400
1401 // Verify the USB mic is selected as the active output.
1402 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041403 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:321404 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1405
1406 // Unplug the USB Mic.
1407 audio_nodes.clear();
1408 audio_nodes.push_back(kInternalMic);
1409 ChangeAudioNodes(audio_nodes);
1410
1411 // Verify the AudioNodesChanged event is fired, and one audio device is
1412 // removed.
1413 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1414 cras_audio_handler_->GetAudioDevices(&audio_devices);
1415 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
1416
1417 // Verify the active input device is switched to internal mic, and
1418 // and ActiveInputChanged event is fired.
1419 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041420 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:321421 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1422}
1423
[email protected]babc24b2013-09-09 23:50:231424TEST_F(CrasAudioHandlerTest, PlugUSBMicNotAffectActiveOutput) {
1425 // Set up initial audio devices.
1426 AudioNodeList audio_nodes;
1427 audio_nodes.push_back(kInternalSpeaker);
1428 audio_nodes.push_back(kHeadphone);
1429 audio_nodes.push_back(kInternalMic);
1430 SetUpCrasAudioHandler(audio_nodes);
1431 const size_t init_nodes_size = audio_nodes.size();
1432
1433 // Verify the audio devices size.
1434 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1435 AudioDeviceList audio_devices;
1436 cras_audio_handler_->GetAudioDevices(&audio_devices);
1437 EXPECT_EQ(init_nodes_size, audio_devices.size());
1438
1439 // Verify the internal mic is selected as the active input.
1440 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041441 EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]babc24b2013-09-09 23:50:231442 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1443
1444 // Verify the headphone is selected as the active output.
1445 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041446 EXPECT_EQ(kHeadphoneId, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]babc24b2013-09-09 23:50:231447 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1448
1449 // Switch the active output to internal speaker.
1450 AudioDevice internal_speaker(kInternalSpeaker);
jennyzbd236742016-03-02 20:15:521451 cras_audio_handler_->SwitchToDevice(internal_speaker, true,
1452 CrasAudioHandler::ACTIVATE_BY_USER);
[email protected]babc24b2013-09-09 23:50:231453
1454 // Verify the active output is switched to internal speaker, and the
1455 // ActiveOutputNodeChanged event is fired.
1456 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1457 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041458 EXPECT_TRUE(
1459 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]babc24b2013-09-09 23:50:231460 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
jennyzb47947f2014-09-25 00:42:041461 EXPECT_EQ(kInternalSpeaker.id,
1462 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]babc24b2013-09-09 23:50:231463
1464 // Plug the USB Mic.
[email protected]4642d7792013-09-18 20:27:171465 audio_nodes.clear();
1466 AudioNode internal_speaker_node(kInternalSpeaker);
1467 internal_speaker_node.active = true;
1468 audio_nodes.push_back(internal_speaker_node);
1469 audio_nodes.push_back(kHeadphone);
1470 AudioNode internal_mic(kInternalMic);
1471 internal_mic.active = true;
1472 audio_nodes.push_back(internal_mic);
[email protected]babc24b2013-09-09 23:50:231473 audio_nodes.push_back(kUSBMic);
1474 ChangeAudioNodes(audio_nodes);
1475
1476 // Verify the AudioNodesChanged event is fired, one new device is added.
1477 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1478 cras_audio_handler_->GetAudioDevices(&audio_devices);
1479 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1480
1481 // Verify the active input device is switched to USB mic, and
1482 // and ActiveInputChanged event is fired.
1483 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041484 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]babc24b2013-09-09 23:50:231485 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1486
1487 // Verify the active output device is not changed.
1488 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041489 EXPECT_TRUE(
1490 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]babc24b2013-09-09 23:50:231491 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
jennyzb47947f2014-09-25 00:42:041492 EXPECT_EQ(kInternalSpeaker.id,
1493 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]babc24b2013-09-09 23:50:231494}
1495
[email protected]ce631ee2014-01-23 03:37:061496TEST_F(CrasAudioHandlerTest, PlugHeadphoneAutoUnplugSpeakerWithActiveUSB) {
1497 // Set up initial audio devices.
1498 AudioNodeList audio_nodes;
1499 audio_nodes.push_back(kUSBHeadphone1);
1500 audio_nodes.push_back(kInternalSpeaker);
1501 audio_nodes.push_back(kInternalMic);
1502 SetUpCrasAudioHandler(audio_nodes);
1503 const size_t init_nodes_size = audio_nodes.size();
1504
1505 // Verify the audio devices size.
1506 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1507 AudioDeviceList audio_devices;
1508 cras_audio_handler_->GetAudioDevices(&audio_devices);
1509 EXPECT_EQ(init_nodes_size, audio_devices.size());
1510
1511 // Verify the internal mic is selected as the active input.
1512 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041513 EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ce631ee2014-01-23 03:37:061514 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1515
1516 // Verify the USB headphone is selected as the active output.
1517 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041518 EXPECT_EQ(kUSBHeadphoneId1,
1519 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ce631ee2014-01-23 03:37:061520 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1521
1522 // Plug the headphone and auto-unplug internal speaker.
1523 audio_nodes.clear();
1524 AudioNode usb_headphone_node(kUSBHeadphone1);
1525 usb_headphone_node.active = true;
1526 audio_nodes.push_back(usb_headphone_node);
1527 AudioNode headphone_node(kHeadphone);
1528 headphone_node.plugged_time = 1000;
1529 audio_nodes.push_back(headphone_node);
1530 AudioNode internal_mic(kInternalMic);
1531 internal_mic.active = true;
1532 audio_nodes.push_back(internal_mic);
1533 ChangeAudioNodes(audio_nodes);
1534
1535 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1536 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1537 cras_audio_handler_->GetAudioDevices(&audio_devices);
1538 EXPECT_EQ(init_nodes_size, audio_devices.size());
1539
1540 // Verify the active output device is switched to headphone, and
1541 // an ActiveOutputChanged event is fired.
1542 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041543 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ce631ee2014-01-23 03:37:061544 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1545
1546 // Unplug the headphone and internal speaker auto-plugs back.
1547 audio_nodes.clear();
1548 audio_nodes.push_back(kUSBHeadphone1);
1549 AudioNode internal_speaker_node(kInternalSpeaker);
1550 internal_speaker_node.plugged_time = 2000;
1551 audio_nodes.push_back(internal_speaker_node);
1552 audio_nodes.push_back(internal_mic);
1553 ChangeAudioNodes(audio_nodes);
1554
1555 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1556 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1557 cras_audio_handler_->GetAudioDevices(&audio_devices);
1558 EXPECT_EQ(init_nodes_size, audio_devices.size());
1559
1560 // Verify the active output device is switched back to USB, and
1561 // an ActiveOutputChanged event is fired.
1562 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041563 EXPECT_EQ(kUSBHeadphone1.id,
1564 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ce631ee2014-01-23 03:37:061565 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1566
1567 // Verify the active input device is not changed.
1568 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041569 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ce631ee2014-01-23 03:37:061570}
1571
1572TEST_F(CrasAudioHandlerTest, PlugMicAutoUnplugInternalMicWithActiveUSB) {
1573 // Set up initial audio devices.
1574 AudioNodeList audio_nodes;
1575 audio_nodes.push_back(kUSBHeadphone1);
1576 audio_nodes.push_back(kInternalSpeaker);
1577 audio_nodes.push_back(kUSBMic);
1578 audio_nodes.push_back(kInternalMic);
1579 SetUpCrasAudioHandler(audio_nodes);
1580 const size_t init_nodes_size = audio_nodes.size();
1581
1582 // Verify the audio devices size.
1583 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1584 AudioDeviceList audio_devices;
1585 cras_audio_handler_->GetAudioDevices(&audio_devices);
1586 EXPECT_EQ(init_nodes_size, audio_devices.size());
1587
1588 // Verify the internal mic is selected as the active input.
1589 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041590 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ce631ee2014-01-23 03:37:061591 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1592
1593 // Verify the internal speaker is selected as the active output.
1594 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041595 EXPECT_EQ(kUSBHeadphoneId1,
1596 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ce631ee2014-01-23 03:37:061597 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1598
1599 // Plug the headphone and mic, auto-unplug internal mic and speaker.
1600 audio_nodes.clear();
1601 AudioNode usb_headphone_node(kUSBHeadphone1);
1602 usb_headphone_node.active = true;
1603 audio_nodes.push_back(usb_headphone_node);
1604 AudioNode headphone_node(kHeadphone);
1605 headphone_node.plugged_time = 1000;
1606 audio_nodes.push_back(headphone_node);
1607 AudioNode usb_mic(kUSBMic);
1608 usb_mic.active = true;
1609 audio_nodes.push_back(usb_mic);
1610 AudioNode mic_jack(kMicJack);
1611 mic_jack.plugged_time = 1000;
1612 audio_nodes.push_back(mic_jack);
1613 ChangeAudioNodes(audio_nodes);
1614
1615 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1616 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1617 cras_audio_handler_->GetAudioDevices(&audio_devices);
1618 EXPECT_EQ(init_nodes_size, audio_devices.size());
1619
1620 // Verify the active output device is switched to headphone, and
1621 // an ActiveOutputChanged event is fired.
1622 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041623 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ce631ee2014-01-23 03:37:061624 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1625
1626 // Verify the active input device is switched to mic jack, and
1627 // an ActiveInputChanged event is fired.
1628 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041629 EXPECT_EQ(kMicJack.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ce631ee2014-01-23 03:37:061630 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1631
1632 // Unplug the headphone and internal speaker auto-plugs back.
1633 audio_nodes.clear();
1634 audio_nodes.push_back(kUSBHeadphone1);
1635 AudioNode internal_speaker_node(kInternalSpeaker);
1636 internal_speaker_node.plugged_time = 2000;
1637 audio_nodes.push_back(internal_speaker_node);
1638 audio_nodes.push_back(kUSBMic);
1639 AudioNode internal_mic(kInternalMic);
1640 internal_mic.plugged_time = 2000;
1641 audio_nodes.push_back(internal_mic);
1642 ChangeAudioNodes(audio_nodes);
1643
1644 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1645 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1646 cras_audio_handler_->GetAudioDevices(&audio_devices);
1647 EXPECT_EQ(init_nodes_size, audio_devices.size());
1648
1649 // Verify the active output device is switched back to USB, and
1650 // an ActiveOutputChanged event is fired.
1651 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041652 EXPECT_EQ(kUSBHeadphone1.id,
1653 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ce631ee2014-01-23 03:37:061654 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1655
1656 // Verify the active input device is switched back to USB mic, and
1657 // an ActiveInputChanged event is fired.
1658 EXPECT_EQ(2, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041659 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ce631ee2014-01-23 03:37:061660 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1661}
1662
[email protected]4642d7792013-09-18 20:27:171663TEST_F(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnPlugInHeadphone) {
1664 // Set up initial audio devices.
1665 AudioNodeList audio_nodes;
1666 audio_nodes.push_back(kInternalSpeaker);
1667 audio_nodes.push_back(kBluetoothHeadset);
1668 SetUpCrasAudioHandler(audio_nodes);
1669 const size_t init_nodes_size = audio_nodes.size();
1670
1671 // Verify the audio devices size.
1672 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1673 AudioDeviceList audio_devices;
1674 cras_audio_handler_->GetAudioDevices(&audio_devices);
1675 EXPECT_EQ(init_nodes_size, audio_devices.size());
1676
1677 // Verify the bluetooth headset is selected as the active output.
1678 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041679 EXPECT_EQ(kBluetoothHeadsetId,
1680 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]4642d7792013-09-18 20:27:171681 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041682 EXPECT_TRUE(
1683 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]4642d7792013-09-18 20:27:171684 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1685
1686 // Plug in headphone, but fire NodesChanged signal twice.
1687 audio_nodes.clear();
1688 audio_nodes.push_back(kInternalSpeaker);
1689 AudioNode bluetooth_headset(kBluetoothHeadset);
1690 bluetooth_headset.plugged_time = 1000;
1691 bluetooth_headset.active = true;
1692 audio_nodes.push_back(bluetooth_headset);
1693 AudioNode headphone(kHeadphone);
1694 headphone.active = false;
1695 headphone.plugged_time = 2000;
1696 audio_nodes.push_back(headphone);
1697 ChangeAudioNodes(audio_nodes);
1698 ChangeAudioNodes(audio_nodes);
1699
1700 // Verify the active output device is set to headphone.
1701 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
jennyzb47947f2014-09-25 00:42:041702 EXPECT_LE(1, test_observer_->active_output_node_changed_count());
1703 EXPECT_EQ(headphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1704 EXPECT_TRUE(
1705 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]4642d7792013-09-18 20:27:171706 EXPECT_EQ(headphone.id, active_output.id);
1707
1708 // Verfiy the audio devices data is consistent, i.e., the active output device
1709 // should be headphone.
1710 cras_audio_handler_->GetAudioDevices(&audio_devices);
1711 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1712 for (size_t i = 0; i < audio_devices.size(); ++i) {
1713 if (audio_devices[i].id == kInternalSpeaker.id)
1714 EXPECT_FALSE(audio_devices[i].active);
1715 else if (audio_devices[i].id == bluetooth_headset.id)
1716 EXPECT_FALSE(audio_devices[i].active);
1717 else if (audio_devices[i].id == headphone.id)
1718 EXPECT_TRUE(audio_devices[i].active);
1719 else
1720 NOTREACHED();
1721 }
1722}
1723
1724TEST_F(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnPlugInUSBMic) {
1725 // Set up initial audio devices.
1726 AudioNodeList audio_nodes;
1727 audio_nodes.push_back(kInternalMic);
1728 SetUpCrasAudioHandler(audio_nodes);
1729 const size_t init_nodes_size = audio_nodes.size();
1730
1731 // Verify the audio devices size.
1732 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1733 AudioDeviceList audio_devices;
1734 cras_audio_handler_->GetAudioDevices(&audio_devices);
1735 EXPECT_EQ(init_nodes_size, audio_devices.size());
1736
1737 // Verify the internal mic is selected as the active output.
1738 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041739 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]4642d7792013-09-18 20:27:171740 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
1741 EXPECT_TRUE(audio_devices[0].active);
1742
1743 // Plug in usb mic, but fire NodesChanged signal twice.
1744 audio_nodes.clear();
1745 AudioNode internal_mic(kInternalMic);
1746 internal_mic.active = true;
1747 internal_mic.plugged_time = 1000;
1748 audio_nodes.push_back(internal_mic);
1749 AudioNode usb_mic(kUSBMic);
1750 usb_mic.active = false;
1751 usb_mic.plugged_time = 2000;
1752 audio_nodes.push_back(usb_mic);
1753 ChangeAudioNodes(audio_nodes);
1754 ChangeAudioNodes(audio_nodes);
1755
1756 // Verify the active output device is set to headphone.
1757 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
jennyzb47947f2014-09-25 00:42:041758 EXPECT_LE(1, test_observer_->active_input_node_changed_count());
1759 EXPECT_EQ(usb_mic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]4642d7792013-09-18 20:27:171760 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1761
1762 // Verfiy the audio devices data is consistent, i.e., the active input device
1763 // should be usb mic.
1764 cras_audio_handler_->GetAudioDevices(&audio_devices);
1765 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1766 for (size_t i = 0; i < audio_devices.size(); ++i) {
1767 if (audio_devices[i].id == kInternalMic.id)
1768 EXPECT_FALSE(audio_devices[i].active);
1769 else if (audio_devices[i].id == usb_mic.id)
1770 EXPECT_TRUE(audio_devices[i].active);
1771 else
1772 NOTREACHED();
1773 }
1774}
1775
1776// This is the case of crbug.com/291303.
1777TEST_F(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnSystemBoot) {
1778 // Set up audio handler with empty audio_nodes.
1779 AudioNodeList audio_nodes;
1780 SetUpCrasAudioHandler(audio_nodes);
1781
1782 AudioNode internal_speaker(kInternalSpeaker);
1783 internal_speaker.active = false;
1784 AudioNode headphone(kHeadphone);
1785 headphone.active = false;
1786 AudioNode internal_mic(kInternalMic);
1787 internal_mic.active = false;
1788 audio_nodes.push_back(internal_speaker);
1789 audio_nodes.push_back(headphone);
1790 audio_nodes.push_back(internal_mic);
1791 const size_t init_nodes_size = audio_nodes.size();
1792
1793 // Simulate AudioNodesChanged signal being fired twice during system boot.
1794 ChangeAudioNodes(audio_nodes);
1795 ChangeAudioNodes(audio_nodes);
1796
1797 // Verify the active output device is set to headphone.
1798 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
jennyzb47947f2014-09-25 00:42:041799 EXPECT_LE(1, test_observer_->active_output_node_changed_count());
1800 EXPECT_EQ(headphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]4642d7792013-09-18 20:27:171801 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041802 EXPECT_TRUE(
1803 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]4642d7792013-09-18 20:27:171804 EXPECT_EQ(headphone.id, active_output.id);
1805
1806 // Verify the active input device id is set to internal mic.
jennyzb47947f2014-09-25 00:42:041807 EXPECT_EQ(internal_mic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]4642d7792013-09-18 20:27:171808
1809 // Verfiy the audio devices data is consistent, i.e., the active output device
1810 // should be headphone, and the active input device should internal mic.
1811 AudioDeviceList audio_devices;
1812 cras_audio_handler_->GetAudioDevices(&audio_devices);
1813 EXPECT_EQ(init_nodes_size, audio_devices.size());
1814 for (size_t i = 0; i < audio_devices.size(); ++i) {
1815 if (audio_devices[i].id == internal_speaker.id)
1816 EXPECT_FALSE(audio_devices[i].active);
1817 else if (audio_devices[i].id == headphone.id)
1818 EXPECT_TRUE(audio_devices[i].active);
1819 else if (audio_devices[i].id == internal_mic.id)
1820 EXPECT_TRUE(audio_devices[i].active);
1821 else
1822 NOTREACHED();
1823 }
1824}
1825
jennyz2d8986d2015-01-15 18:34:071826// This is the case of crbug.com/448924.
1827TEST_F(CrasAudioHandlerTest,
1828 TwoNodesChangedSignalsForLosingTowNodesOnOneUnplug) {
1829 // Set up audio handler with 4 audio_nodes.
1830 AudioNodeList audio_nodes;
1831 AudioNode internal_speaker(kInternalSpeaker);
1832 internal_speaker.active = false;
1833 AudioNode headphone(kHeadphone);
1834 headphone.active = false;
1835 AudioNode internal_mic(kInternalMic);
1836 internal_mic.active = false;
1837 AudioNode micJack(kMicJack);
1838 micJack.active = false;
1839 audio_nodes.push_back(internal_speaker);
1840 audio_nodes.push_back(headphone);
1841 audio_nodes.push_back(internal_mic);
1842 audio_nodes.push_back(micJack);
1843 SetUpCrasAudioHandler(audio_nodes);
1844
1845 // Verify the audio devices size.
1846 AudioDeviceList audio_devices;
1847 cras_audio_handler_->GetAudioDevices(&audio_devices);
1848 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
1849
1850 // Verify the headphone has been selected as the active output.
1851 AudioDevice active_output;
1852 EXPECT_TRUE(
1853 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1854 EXPECT_EQ(kHeadphone.id, active_output.id);
1855 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1856 EXPECT_TRUE(active_output.active);
1857 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1858
1859 // Verify the mic Jack has been selected as the active input.
1860 EXPECT_EQ(micJack.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1861 const AudioDevice* active_input = GetDeviceFromId(micJack.id);
1862 EXPECT_TRUE(active_input->active);
1863 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1864
1865 // Simulate the nodes list in first NodesChanged signal, only headphone is
1866 // removed, other nodes remains the same.
1867 AudioNodeList changed_nodes_1;
1868 internal_speaker.active = false;
1869 changed_nodes_1.push_back(internal_speaker);
1870 internal_mic.active = false;
1871 changed_nodes_1.push_back(internal_mic);
1872 micJack.active = true;
1873 changed_nodes_1.push_back(micJack);
1874
1875 // Simulate the nodes list in second NodesChanged signal, the micJac is
1876 // removed, but the internal_mic is inactive, which does not reflect the
1877 // active status set from the first NodesChanged signal since this was sent
1878 // before cras receives the SetActiveOutputNode from the first NodesChanged
1879 // handling.
1880 AudioNodeList changed_nodes_2;
1881 changed_nodes_2.push_back(internal_speaker);
1882 changed_nodes_2.push_back(internal_mic);
1883
1884 // Simulate AudioNodesChanged signal being fired twice for unplug an audio
1885 // device with both input and output nodes on it.
1886 ChangeAudioNodes(changed_nodes_1);
1887 ChangeAudioNodes(changed_nodes_2);
1888
1889 AudioDeviceList changed_devices;
1890 cras_audio_handler_->GetAudioDevices(&changed_devices);
1891 EXPECT_EQ(2u, changed_devices.size());
1892
1893 // Verify the active output device is set to internal speaker.
1894 EXPECT_EQ(internal_speaker.id,
1895 cras_audio_handler_->GetPrimaryActiveOutputNode());
1896 EXPECT_TRUE(
1897 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1898 EXPECT_EQ(internal_speaker.id, active_output.id);
1899 EXPECT_TRUE(active_output.active);
1900
1901 // Verify the active input device id is set to internal mic.
1902 EXPECT_EQ(internal_mic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1903 const AudioDevice* changed_active_input = GetDeviceFromId(internal_mic.id);
1904 EXPECT_TRUE(changed_active_input->active);
1905}
1906
warx74524622016-03-31 01:07:211907TEST_F(CrasAudioHandlerTest, SetOutputMono) {
1908 AudioNodeList audio_nodes;
1909 audio_nodes.push_back(kHeadphone);
1910 SetUpCrasAudioHandler(audio_nodes);
1911 EXPECT_EQ(0, test_observer_->output_channel_remixing_changed_count());
1912
1913 // Set output mono
1914 cras_audio_handler_->SetOutputMono(true);
1915
1916 // Verify the output is in mono mode, OnOuputChannelRemixingChanged event
1917 // is fired.
1918 EXPECT_TRUE(cras_audio_handler_->IsOutputMonoEnabled());
1919 EXPECT_EQ(1, test_observer_->output_channel_remixing_changed_count());
1920
1921 // Set output stereo
1922 cras_audio_handler_->SetOutputMono(false);
1923 EXPECT_FALSE(cras_audio_handler_->IsOutputMonoEnabled());
1924 EXPECT_EQ(2, test_observer_->output_channel_remixing_changed_count());
1925}
1926
[email protected]ee38bc42013-07-29 21:41:321927TEST_F(CrasAudioHandlerTest, SetOutputMute) {
1928 AudioNodeList audio_nodes;
1929 audio_nodes.push_back(kInternalSpeaker);
1930 SetUpCrasAudioHandler(audio_nodes);
1931 EXPECT_EQ(0, test_observer_->output_mute_changed_count());
1932
1933 // Mute the device.
1934 cras_audio_handler_->SetOutputMute(true);
1935
1936 // Verify the output is muted, OnOutputMuteChanged event is fired,
1937 // and mute value is saved in the preferences.
1938 EXPECT_TRUE(cras_audio_handler_->IsOutputMuted());
1939 EXPECT_EQ(1, test_observer_->output_mute_changed_count());
1940 AudioDevice speaker(kInternalSpeaker);
1941 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(speaker));
1942
1943 // Unmute the device.
1944 cras_audio_handler_->SetOutputMute(false);
1945
1946 // Verify the output is unmuted, OnOutputMuteChanged event is fired,
1947 // and mute value is saved in the preferences.
1948 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
1949 EXPECT_EQ(2, test_observer_->output_mute_changed_count());
1950 EXPECT_FALSE(audio_pref_handler_->GetMuteValue(speaker));
1951}
1952
1953TEST_F(CrasAudioHandlerTest, SetInputMute) {
1954 AudioNodeList audio_nodes;
1955 audio_nodes.push_back(kInternalMic);
1956 SetUpCrasAudioHandler(audio_nodes);
1957 EXPECT_EQ(0, test_observer_->input_mute_changed_count());
1958
1959 // Mute the device.
1960 cras_audio_handler_->SetInputMute(true);
1961
[email protected]f3dd2962014-04-22 04:48:381962 // Verify the input is muted, OnInputMuteChanged event is fired.
[email protected]ee38bc42013-07-29 21:41:321963 EXPECT_TRUE(cras_audio_handler_->IsInputMuted());
1964 EXPECT_EQ(1, test_observer_->input_mute_changed_count());
[email protected]ee38bc42013-07-29 21:41:321965
1966 // Unmute the device.
1967 cras_audio_handler_->SetInputMute(false);
1968
[email protected]f3dd2962014-04-22 04:48:381969 // Verify the input is unmuted, OnInputMuteChanged event is fired.
[email protected]ee38bc42013-07-29 21:41:321970 EXPECT_FALSE(cras_audio_handler_->IsInputMuted());
1971 EXPECT_EQ(2, test_observer_->input_mute_changed_count());
[email protected]ee38bc42013-07-29 21:41:321972}
1973
1974TEST_F(CrasAudioHandlerTest, SetOutputVolumePercent) {
1975 AudioNodeList audio_nodes;
1976 audio_nodes.push_back(kInternalSpeaker);
1977 SetUpCrasAudioHandler(audio_nodes);
1978 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
1979
warxfd7c37b2016-08-05 19:38:071980 const int kVolume = 60;
1981 cras_audio_handler_->SetOutputVolumePercent(kVolume);
[email protected]ee38bc42013-07-29 21:41:321982
1983 // Verify the output volume is changed to the designated value,
jennyz481292c2015-03-21 01:49:291984 // OnOutputNodeVolumeChanged event is fired, and the device volume value
warxfd7c37b2016-08-05 19:38:071985 // is saved in the preferences.
[email protected]ee38bc42013-07-29 21:41:321986 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent());
1987 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
1988 AudioDevice device;
jennyzb47947f2014-09-25 00:42:041989 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device));
[email protected]ee38bc42013-07-29 21:41:321990 EXPECT_EQ(device.id, kInternalSpeaker.id);
[email protected]140a31452013-09-09 18:54:371991 EXPECT_EQ(kVolume, audio_pref_handler_->GetOutputVolumeValue(&device));
[email protected]ee38bc42013-07-29 21:41:321992}
1993
warxfd7c37b2016-08-05 19:38:071994TEST_F(CrasAudioHandlerTest, SetOutputVolumePercentWithoutNotifyingObservers) {
1995 AudioNodeList audio_nodes;
1996 audio_nodes.push_back(kInternalSpeaker);
1997 SetUpCrasAudioHandler(audio_nodes);
1998 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
1999
2000 const int kVolume1 = 60;
2001 const int kVolume2 = 80;
2002 cras_audio_handler_->SetOutputVolumePercentWithoutNotifyingObservers(
2003 kVolume1, CrasAudioHandler::VOLUME_CHANGE_MAXIMIZE_MODE_SCREENSHOT);
2004 // Verify the output volume is changed to the designated value,
2005 // OnOutputNodeVolumeChanged event is not fired, and the device volume value
2006 // is saved in the preferences.
2007 EXPECT_EQ(kVolume1, cras_audio_handler_->GetOutputVolumePercent());
2008 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2009 AudioDevice device;
2010 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device));
2011 EXPECT_EQ(device.id, kInternalSpeaker.id);
2012 EXPECT_EQ(kVolume1, audio_pref_handler_->GetOutputVolumeValue(&device));
2013
2014 // Make another SetOutputVolumePercentWithoutNotifyingObservers call to make
2015 // sure everything is right.
2016 cras_audio_handler_->SetOutputVolumePercentWithoutNotifyingObservers(
2017 kVolume2, CrasAudioHandler::VOLUME_CHANGE_MAXIMIZE_MODE_SCREENSHOT);
2018 EXPECT_EQ(kVolume2, cras_audio_handler_->GetOutputVolumePercent());
2019 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2020 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device));
2021 EXPECT_EQ(device.id, kInternalSpeaker.id);
2022 EXPECT_EQ(kVolume2, audio_pref_handler_->GetOutputVolumeValue(&device));
2023
2024 // Make a final SetOutputVolumePercent call to check if
2025 // SetOutputVolumePercentWithoutNotifyingObservers may block subsequent
2026 // notifying observers.
2027 cras_audio_handler_->SetOutputVolumePercent(kVolume1);
2028 EXPECT_EQ(kVolume1, cras_audio_handler_->GetOutputVolumePercent());
2029 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
2030 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device));
2031 EXPECT_EQ(device.id, kInternalSpeaker.id);
2032 EXPECT_EQ(kVolume1, audio_pref_handler_->GetOutputVolumeValue(&device));
2033}
2034
2035TEST_F(CrasAudioHandlerTest, RestartAudioClientWithCrasReady) {
2036 AudioNodeList audio_nodes;
2037 audio_nodes.push_back(kInternalSpeaker);
2038 SetUpCrasAudioHandler(audio_nodes);
2039 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2040
2041 const int kDefaultVolume = cras_audio_handler_->GetOutputVolumePercent();
2042 // Disable the auto OutputNodeVolumeChanged signal.
2043 fake_cras_audio_client_->set_notify_volume_change_with_delay(true);
2044
2045 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes);
2046 RestartAudioClient();
2047 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2048 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2049
2050 // The correct initialization OutputNodeVolumeChanged event is fired. We
2051 // should avoid notifying observers.
2052 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
2053 kInternalSpeaker.id, kDefaultVolume);
2054 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2055 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2056
2057 // The later OutputNodeVolumeChanged event after initialization should notify
2058 // observers.
2059 const int kVolume = 60;
2060 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
2061 kInternalSpeaker.id, kVolume);
2062 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
2063 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent());
2064}
2065
2066TEST_F(CrasAudioHandlerTest, RestartAudioClientWithCrasDropRequest) {
2067 AudioNodeList audio_nodes;
2068 audio_nodes.push_back(kInternalSpeaker);
2069 SetUpCrasAudioHandler(audio_nodes);
2070 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2071
2072 const int kDefaultVolume = cras_audio_handler_->GetOutputVolumePercent();
2073 // Disable the auto OutputNodeVolumeChanged signal.
2074 fake_cras_audio_client_->set_notify_volume_change_with_delay(true);
2075
2076 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes);
2077 RestartAudioClient();
2078 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2079 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2080
2081 // A wrong initialization OutputNodeVolumeChanged event is fired. This may
2082 // happen when Cras is not ready and drops request. The approach we use is
2083 // to log warning message, clear the pending automated volume change reasons,
2084 // and notify observers about this change.
2085 const int kVolume1 = 30;
2086 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
2087 kInternalSpeaker.id, kVolume1);
2088 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
2089 EXPECT_EQ(kVolume1, cras_audio_handler_->GetOutputVolumePercent());
2090
2091 // The later OutputNodeVolumeChanged event should notify observers.
2092 const int kVolume2 = 60;
2093 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
2094 kInternalSpeaker.id, kVolume2);
2095 EXPECT_EQ(2, test_observer_->output_volume_changed_count());
2096 EXPECT_EQ(kVolume2, cras_audio_handler_->GetOutputVolumePercent());
2097}
2098
jennyz96526d02016-06-22 17:03:432099TEST_F(CrasAudioHandlerTest, SetOutputVolumeWithDelayedSignal) {
2100 AudioNodeList audio_nodes;
2101 audio_nodes.push_back(kInternalSpeaker);
2102 SetUpCrasAudioHandler(audio_nodes);
2103 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2104
2105 const int kDefaultVolume = 75;
2106 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2107
2108 // Disable the auto OutputNodeVolumeChanged signal.
2109 fake_cras_audio_client_->set_notify_volume_change_with_delay(true);
2110
2111 // Verify the volume state is not changed before OutputNodeVolumeChanged
2112 // signal fires.
2113 const int kVolume = 60;
2114 cras_audio_handler_->SetOutputVolumePercent(kVolume);
2115 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2116 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2117
2118 // Verify the output volume is changed to the designated value after
2119 // OnOutputNodeVolumeChanged cras signal fires, and the volume change event
2120 // has been fired to notify the observers.
2121 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
2122 kInternalSpeaker.id, kVolume);
2123 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
2124 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent());
2125 AudioDevice device;
2126 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device));
2127 EXPECT_EQ(device.id, kInternalSpeaker.id);
2128 EXPECT_EQ(kVolume, audio_pref_handler_->GetOutputVolumeValue(&device));
2129}
2130
2131TEST_F(CrasAudioHandlerTest,
2132 ChangeOutputVolumesWithDelayedSignalForSingleActiveDevice) {
2133 AudioNodeList audio_nodes;
2134 audio_nodes.push_back(kInternalSpeaker);
2135 SetUpCrasAudioHandler(audio_nodes);
2136 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2137
2138 const int kDefaultVolume = 75;
2139 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2140
2141 // Disable the auto OutputNodeVolumeChanged signal.
2142 fake_cras_audio_client_->set_notify_volume_change_with_delay(true);
2143
2144 // Verify the volume state is not changed before OutputNodeVolumeChanged
2145 // signal fires.
2146 const int kVolume1 = 50;
2147 const int kVolume2 = 60;
2148 cras_audio_handler_->SetOutputVolumePercent(kVolume1);
2149 cras_audio_handler_->SetOutputVolumePercent(kVolume2);
2150 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2151 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2152
2153 // Simulate OutputNodeVolumeChanged signal fired with big latency that
2154 // it lags behind the SetOutputNodeVolume requests. Chrome sets the volume
2155 // to 50 then 60, but the volume changed signal for 50 comes back after
2156 // chrome sets the volume to 60. Verify chrome will sync to the designated
2157 // volume level after all signals arrive.
2158 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
2159 kInternalSpeaker.id, kVolume1);
2160 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
2161 EXPECT_EQ(kVolume1, cras_audio_handler_->GetOutputVolumePercent());
2162
2163 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
2164 kInternalSpeaker.id, kVolume2);
2165 EXPECT_EQ(2, test_observer_->output_volume_changed_count());
2166 EXPECT_EQ(kVolume2, cras_audio_handler_->GetOutputVolumePercent());
2167}
2168
2169TEST_F(CrasAudioHandlerTest,
2170 ChangeOutputVolumeFromNonChromeSourceSingleActiveDevice) {
2171 AudioNodeList audio_nodes;
2172 audio_nodes.push_back(kInternalSpeaker);
2173 SetUpCrasAudioHandler(audio_nodes);
2174 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2175
2176 const int kDefaultVolume = 75;
2177 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2178 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2179
2180 // Simulate OutputNodeVolumeChanged signal fired by a non-chrome source.
2181 // Verify chrome will sync its volume state to the volume from the signal,
2182 // and notify its observers for the volume change event.
2183 const int kVolume = 20;
2184 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
2185 kInternalSpeaker.id, kVolume);
2186 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
2187 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent());
2188 AudioDevice device;
2189 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device));
2190 EXPECT_EQ(device.id, kInternalSpeaker.id);
2191 EXPECT_EQ(kVolume, audio_pref_handler_->GetOutputVolumeValue(&device));
2192}
2193
[email protected]ee38bc42013-07-29 21:41:322194TEST_F(CrasAudioHandlerTest, SetInputGainPercent) {
2195 AudioNodeList audio_nodes;
2196 audio_nodes.push_back(kInternalMic);
2197 SetUpCrasAudioHandler(audio_nodes);
2198 EXPECT_EQ(0, test_observer_->input_gain_changed_count());
2199
2200 cras_audio_handler_->SetInputGainPercent(60);
2201
2202 // Verify the input gain changed to the designated value,
jennyz481292c2015-03-21 01:49:292203 // OnInputNodeGainChanged event is fired, and the device gain value
[email protected]ee38bc42013-07-29 21:41:322204 // is saved in the preferences.
2205 const int kGain = 60;
2206 EXPECT_EQ(kGain, cras_audio_handler_->GetInputGainPercent());
2207 EXPECT_EQ(1, test_observer_->input_gain_changed_count());
2208 AudioDevice internal_mic(kInternalMic);
[email protected]140a31452013-09-09 18:54:372209 EXPECT_EQ(kGain, audio_pref_handler_->GetInputGainValue(&internal_mic));
[email protected]ee38bc42013-07-29 21:41:322210}
2211
2212TEST_F(CrasAudioHandlerTest, SetMuteForDevice) {
2213 AudioNodeList audio_nodes;
2214 audio_nodes.push_back(kInternalSpeaker);
2215 audio_nodes.push_back(kHeadphone);
2216 audio_nodes.push_back(kInternalMic);
2217 audio_nodes.push_back(kUSBMic);
2218 SetUpCrasAudioHandler(audio_nodes);
2219
2220 // Mute the active output device.
jennyzb47947f2014-09-25 00:42:042221 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:322222 cras_audio_handler_->SetMuteForDevice(kHeadphone.id, true);
2223
2224 // Verify the headphone is muted and mute value is saved in the preferences.
2225 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kHeadphone.id));
2226 AudioDevice headphone(kHeadphone);
2227 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(headphone));
2228
2229 // Mute the non-active output device.
2230 cras_audio_handler_->SetMuteForDevice(kInternalSpeaker.id, true);
2231
2232 // Verify the internal speaker is muted and mute value is saved in the
2233 // preferences.
2234 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kInternalSpeaker.id));
2235 AudioDevice internal_speaker(kInternalSpeaker);
2236 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(internal_speaker));
2237
2238 // Mute the active input device.
jennyzb47947f2014-09-25 00:42:042239 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:322240 cras_audio_handler_->SetMuteForDevice(kUSBMic.id, true);
2241
[email protected]f3dd2962014-04-22 04:48:382242 // Verify the USB Mic is muted.
2243 EXPECT_TRUE(cras_audio_handler_->IsInputMutedForDevice(kUSBMic.id));
[email protected]ee38bc42013-07-29 21:41:322244
[email protected]f3dd2962014-04-22 04:48:382245 // Mute the non-active input device should be a no-op, see crbug.com/365050.
[email protected]ee38bc42013-07-29 21:41:322246 cras_audio_handler_->SetMuteForDevice(kInternalMic.id, true);
2247
[email protected]f3dd2962014-04-22 04:48:382248 // Verify IsInputMutedForDevice returns false for non-active input device.
2249 EXPECT_FALSE(cras_audio_handler_->IsInputMutedForDevice(kInternalMic.id));
[email protected]ee38bc42013-07-29 21:41:322250}
2251
2252TEST_F(CrasAudioHandlerTest, SetVolumeGainPercentForDevice) {
2253 AudioNodeList audio_nodes;
2254 audio_nodes.push_back(kInternalSpeaker);
2255 audio_nodes.push_back(kHeadphone);
2256 audio_nodes.push_back(kInternalMic);
2257 audio_nodes.push_back(kUSBMic);
2258 SetUpCrasAudioHandler(audio_nodes);
2259
2260 // Set volume percent for active output device.
2261 const int kHeadphoneVolume = 30;
jennyzb47947f2014-09-25 00:42:042262 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:322263 cras_audio_handler_->SetVolumeGainPercentForDevice(kHeadphone.id,
2264 kHeadphoneVolume);
2265
2266 // Verify the volume percent of headphone is set, and saved in preferences.
2267 EXPECT_EQ(kHeadphoneVolume,
2268 cras_audio_handler_->GetOutputVolumePercentForDevice(
2269 kHeadphone.id));
2270 AudioDevice headphone(kHeadphone);
2271 EXPECT_EQ(kHeadphoneVolume,
[email protected]140a31452013-09-09 18:54:372272 audio_pref_handler_->GetOutputVolumeValue(&headphone));
[email protected]ee38bc42013-07-29 21:41:322273
2274 // Set volume percent for non-active output device.
2275 const int kSpeakerVolume = 60;
2276 cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalSpeaker.id,
2277 kSpeakerVolume);
2278
2279 // Verify the volume percent of speaker is set, and saved in preferences.
2280 EXPECT_EQ(kSpeakerVolume,
2281 cras_audio_handler_->GetOutputVolumePercentForDevice(
2282 kInternalSpeaker.id));
2283 AudioDevice speaker(kInternalSpeaker);
2284 EXPECT_EQ(kSpeakerVolume,
[email protected]140a31452013-09-09 18:54:372285 audio_pref_handler_->GetOutputVolumeValue(&speaker));
[email protected]ee38bc42013-07-29 21:41:322286
2287 // Set gain percent for active input device.
2288 const int kUSBMicGain = 30;
jennyzb47947f2014-09-25 00:42:042289 EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:322290 cras_audio_handler_->SetVolumeGainPercentForDevice(kUSBMic.id,
2291 kUSBMicGain);
2292
2293 // Verify the gain percent of USB mic is set, and saved in preferences.
2294 EXPECT_EQ(kUSBMicGain,
2295 cras_audio_handler_->GetOutputVolumePercentForDevice(kUSBMic.id));
2296 AudioDevice usb_mic(kHeadphone);
2297 EXPECT_EQ(kUSBMicGain,
[email protected]140a31452013-09-09 18:54:372298 audio_pref_handler_->GetInputGainValue(&usb_mic));
[email protected]ee38bc42013-07-29 21:41:322299
2300 // Set gain percent for non-active input device.
2301 const int kInternalMicGain = 60;
2302 cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalMic.id,
2303 kInternalMicGain);
2304
2305 // Verify the gain percent of internal mic is set, and saved in preferences.
2306 EXPECT_EQ(kInternalMicGain,
2307 cras_audio_handler_->GetOutputVolumePercentForDevice(
2308 kInternalMic.id));
2309 AudioDevice internal_mic(kInternalMic);
2310 EXPECT_EQ(kInternalMicGain,
[email protected]140a31452013-09-09 18:54:372311 audio_pref_handler_->GetInputGainValue(&internal_mic));
[email protected]ee38bc42013-07-29 21:41:322312}
2313
2314TEST_F(CrasAudioHandlerTest, HandleOtherDeviceType) {
2315 const size_t kNumValidAudioDevices = 4;
2316 AudioNodeList audio_nodes;
2317 audio_nodes.push_back(kInternalSpeaker);
2318 audio_nodes.push_back(kOtherTypeOutput);
2319 audio_nodes.push_back(kInternalMic);
2320 audio_nodes.push_back(kOtherTypeInput);
2321 SetUpCrasAudioHandler(audio_nodes);
2322
2323 // Verify the audio devices size.
2324 AudioDeviceList audio_devices;
2325 cras_audio_handler_->GetAudioDevices(&audio_devices);
2326 EXPECT_EQ(kNumValidAudioDevices, audio_devices.size());
2327
2328 // Verify the internal speaker has been selected as the active output,
2329 // and the output device with some randown unknown type is handled gracefully.
2330 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:042331 EXPECT_TRUE(
2332 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]ee38bc42013-07-29 21:41:322333 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
jennyzb47947f2014-09-25 00:42:042334 EXPECT_EQ(kInternalSpeaker.id,
2335 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:322336 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
2337
2338 // Ensure the internal microphone has been selected as the active input,
2339 // and the input device with some random unknown type is handled gracefully.
2340 AudioDevice active_input;
jennyzb47947f2014-09-25 00:42:042341 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:322342 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
2343}
2344
hychaofe100d542016-01-12 10:17:522345TEST_F(CrasAudioHandlerTest, ActiveDeviceSelectionWithStableDeviceId) {
2346 AudioNodeList audio_nodes;
2347 AudioNode internal_speaker(kInternalSpeaker);
2348 audio_nodes.push_back(internal_speaker);
2349 AudioNode usb_headset(kUSBHeadphone1);
2350 usb_headset.plugged_time = 80000000;
2351 audio_nodes.push_back(usb_headset);
2352 SetUpCrasAudioHandler(audio_nodes);
2353
2354 // Verify the audio devices size.
2355 AudioDeviceList audio_devices;
2356 cras_audio_handler_->GetAudioDevices(&audio_devices);
2357 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2358
2359 // Initially active node is selected base on priority, so USB headphone
2360 // is selected.
2361 EXPECT_EQ(kUSBHeadphone1.id,
2362 cras_audio_handler_->GetPrimaryActiveOutputNode());
2363
2364 // Change the active device to internal speaker, now USB headphone becomes
2365 // inactive.
2366 AudioDevice speaker(kInternalSpeaker);
jennyzbd236742016-03-02 20:15:522367 cras_audio_handler_->SwitchToDevice(speaker, true,
2368 CrasAudioHandler::ACTIVATE_BY_USER);
hychaofe100d542016-01-12 10:17:522369 EXPECT_NE(kUSBHeadphone1.id,
2370 cras_audio_handler_->GetPrimaryActiveOutputNode());
2371
2372 // Unplug USB headset.
2373 audio_nodes.clear();
2374 internal_speaker.active = true;
2375 audio_nodes.push_back(internal_speaker);
2376 ChangeAudioNodes(audio_nodes);
2377 EXPECT_EQ(kInternalSpeaker.id,
2378 cras_audio_handler_->GetPrimaryActiveOutputNode());
2379
2380 // Plug the same USB headset back, id is different, but stable_device_id
2381 // remains the same.
2382 usb_headset.active = false;
2383 usb_headset.id = 98765;
2384 audio_nodes.push_back(usb_headset);
2385 ChangeAudioNodes(audio_nodes);
2386
2387 // Since USB headset was inactive before it was unplugged, it won't be
2388 // selected as active after it's plugged in again.
2389 EXPECT_EQ(kInternalSpeaker.id,
2390 cras_audio_handler_->GetPrimaryActiveOutputNode());
2391
2392 // Plug the second USB headset.
2393 AudioNode usb_headset2(kUSBHeadphone2);
2394 usb_headset2.plugged_time = 80000001;
2395 audio_nodes.push_back(usb_headset2);
2396 ChangeAudioNodes(audio_nodes);
2397
2398 // Since the second USB device is new, it's selected as the active device
2399 // by its priority.
2400 EXPECT_EQ(kUSBHeadphone2.id,
2401 cras_audio_handler_->GetPrimaryActiveOutputNode());
2402
2403 // Unplug the second USB headset.
2404 audio_nodes.clear();
2405 internal_speaker.active = false;
2406 audio_nodes.push_back(internal_speaker);
2407 audio_nodes.push_back(usb_headset);
2408 ChangeAudioNodes(audio_nodes);
2409
2410 // There is no active node after USB2 unplugged, the 1st USB got selected
2411 // by its priority.
2412 EXPECT_EQ(usb_headset.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2413
jennyzbd236742016-03-02 20:15:522414 audio_nodes.clear();
2415 internal_speaker.active = false;
2416 audio_nodes.push_back(internal_speaker);
2417 usb_headset.active = true;
2418 audio_nodes.push_back(usb_headset);
hychaofe100d542016-01-12 10:17:522419 usb_headset2.active = false;
2420 usb_headset2.plugged_time = 80000002;
2421 audio_nodes.push_back(usb_headset2);
2422 ChangeAudioNodes(audio_nodes);
2423
2424 // Plug the second USB again. Since it was the active node before it got
2425 // unplugged, it is now selected as the active node.
2426 EXPECT_EQ(usb_headset2.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2427}
2428
jennyzbd236742016-03-02 20:15:522429// Test the device new session case, either via reboot or logout, if there
2430// is an active device in the previous session, that device should still
2431// be set as active after the new session starts.
2432TEST_F(CrasAudioHandlerTest, PersistActiveDeviceAcrossSession) {
2433 // Set the active device to internal speaker before the session starts.
2434 AudioNodeList audio_nodes;
2435 audio_nodes.push_back(kInternalSpeaker);
2436 audio_nodes.push_back(kHeadphone);
2437 SetupCrasAudioHandlerWithActiveNodeInPref(
2438 audio_nodes, audio_nodes, AudioDevice(kInternalSpeaker), true);
2439
2440 // Verify the audio devices size.
2441 AudioDeviceList audio_devices;
2442 cras_audio_handler_->GetAudioDevices(&audio_devices);
2443 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2444
2445 // Verify the active device is the internal speaker, which is of a lower
2446 // priority, but selected as active since it was the active device previously.
2447 EXPECT_EQ(kInternalSpeaker.id,
2448 cras_audio_handler_->GetPrimaryActiveOutputNode());
2449}
2450
2451TEST_F(CrasAudioHandlerTest, PersistActiveSpeakerAcrossReboot) {
2452 // Simulates the device was shut down with three audio devices, and
2453 // internal speaker being the active one selected by user.
2454 AudioNodeList audio_nodes_in_pref;
2455 audio_nodes_in_pref.push_back(kInternalSpeaker);
2456 audio_nodes_in_pref.push_back(kHeadphone);
2457 audio_nodes_in_pref.push_back(kUSBHeadphone1);
2458
2459 // Simulate the first NodesChanged signal coming with only one node.
2460 AudioNodeList audio_nodes;
2461 audio_nodes.push_back(kUSBHeadphone1);
2462
2463 SetupCrasAudioHandlerWithActiveNodeInPref(
2464 audio_nodes, audio_nodes_in_pref, AudioDevice(kInternalSpeaker), true);
2465
2466 // Verify the usb headphone has been made active.
2467 AudioDeviceList audio_devices;
2468 cras_audio_handler_->GetAudioDevices(&audio_devices);
2469 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2470 EXPECT_EQ(kUSBHeadphone1.id,
2471 cras_audio_handler_->GetPrimaryActiveOutputNode());
2472
2473 // Simulate another NodesChanged signal coming later with all ndoes.
2474 audio_nodes.push_back(kInternalSpeaker);
2475 audio_nodes.push_back(kHeadphone);
2476 ChangeAudioNodes(audio_nodes);
2477
2478 // Verify the active output has been restored to internal speaker.
2479 cras_audio_handler_->GetAudioDevices(&audio_devices);
2480 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2481 EXPECT_EQ(kInternalSpeaker.id,
2482 cras_audio_handler_->GetPrimaryActiveOutputNode());
2483}
2484
jennyz6d5579b2016-07-28 17:49:112485// Test the corner case that headphone is plugged in for the first time on
2486// a cros device after the device is shutdown.
2487// crbug.com/622045.
2488TEST_F(CrasAudioHandlerTest, PlugInHeadphoneFirstTimeAfterPowerDown) {
2489 // Simulate plugging headphone for the first on a cros device after it is
2490 // powered down. Internal speaker is set up in audio prefs as active
2491 // before the new cros session starts.
2492 AudioNodeList audio_nodes_in_pref;
2493 audio_nodes_in_pref.push_back(kInternalSpeaker);
2494
2495 AudioNodeList audio_nodes;
2496 audio_nodes.push_back(kInternalSpeaker);
2497 audio_nodes.push_back(kHeadphone);
2498
2499 SetupCrasAudioHandlerWithActiveNodeInPref(
2500 audio_nodes, audio_nodes_in_pref, AudioDevice(kInternalSpeaker), false);
2501
2502 // Verify the audio devices size.
2503 AudioDeviceList audio_devices;
2504 cras_audio_handler_->GetAudioDevices(&audio_devices);
2505 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2506
2507 // Verify headphone becomes the active output.
2508 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2509}
2510
jennyzbd236742016-03-02 20:15:522511TEST_F(CrasAudioHandlerTest,
2512 PersistActiveUsbHeadphoneAcrossRebootUsbComeLater) {
2513 // Simulates the device was shut down with three audio devices, and
2514 // usb headphone being the active one selected by priority.
2515 AudioNodeList audio_nodes_in_pref;
2516 audio_nodes_in_pref.push_back(kInternalSpeaker);
2517 audio_nodes_in_pref.push_back(kHeadphone);
2518 audio_nodes_in_pref.push_back(kUSBHeadphone1);
2519
2520 // Simulate the first NodesChanged signal coming with only internal speaker
2521 // and the headphone.
2522 AudioNodeList audio_nodes;
2523 audio_nodes.push_back(kInternalSpeaker);
2524 audio_nodes.push_back(kHeadphone);
2525
2526 SetupCrasAudioHandlerWithActiveNodeInPref(audio_nodes, audio_nodes_in_pref,
2527 AudioDevice(kUSBHeadphone1), false);
2528
2529 // Verify the headphone has been made active.
2530 AudioDeviceList audio_devices;
2531 cras_audio_handler_->GetAudioDevices(&audio_devices);
2532 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2533 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2534
2535 // Simulate USB node comes later with all ndoes.
2536 AudioNode usb_node(kUSBHeadphone1);
2537 usb_node.plugged_time = 80000000;
2538 audio_nodes.push_back(usb_node);
2539 ChangeAudioNodes(audio_nodes);
2540
2541 // Verify the active output has been restored to usb headphone.
2542 cras_audio_handler_->GetAudioDevices(&audio_devices);
2543 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2544 EXPECT_EQ(kUSBHeadphone1.id,
2545 cras_audio_handler_->GetPrimaryActiveOutputNode());
2546}
2547
2548TEST_F(CrasAudioHandlerTest,
2549 PersistActiveUsbHeadphoneAcrossRebootUsbComeFirst) {
2550 // Simulates the device was shut down with three audio devices, and
2551 // usb headphone being the active one selected by priority.
2552 AudioNodeList audio_nodes_in_pref;
2553 audio_nodes_in_pref.push_back(kInternalSpeaker);
2554 audio_nodes_in_pref.push_back(kHeadphone);
2555 audio_nodes_in_pref.push_back(kUSBHeadphone1);
2556
2557 // Simulate the first NodesChanged signal coming with only internal speaker
2558 // and the USB headphone.
2559 AudioNodeList audio_nodes;
2560 audio_nodes.push_back(kInternalSpeaker);
2561 audio_nodes.push_back(kUSBHeadphone1);
2562
2563 SetupCrasAudioHandlerWithActiveNodeInPref(audio_nodes, audio_nodes_in_pref,
2564 AudioDevice(kUSBHeadphone1), false);
2565
2566 // Verify the USB headphone has been made active.
2567 AudioDeviceList audio_devices;
2568 cras_audio_handler_->GetAudioDevices(&audio_devices);
2569 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2570 EXPECT_EQ(kUSBHeadphone1.id,
2571 cras_audio_handler_->GetPrimaryActiveOutputNode());
2572
2573 // Simulate another NodesChanged signal coming later with all ndoes.
2574 AudioNode headphone_node(kHeadphone);
2575 headphone_node.plugged_time = 80000000;
2576 audio_nodes.push_back(headphone_node);
2577 ChangeAudioNodes(audio_nodes);
2578
2579 // Verify the active output has been restored to USB headphone.
2580 cras_audio_handler_->GetAudioDevices(&audio_devices);
2581 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2582 EXPECT_EQ(kUSBHeadphone1.id,
2583 cras_audio_handler_->GetPrimaryActiveOutputNode());
2584}
2585
2586// This covers the crbug.com/586026. Cras lost the active state of the internal
2587// speaker when user unplugs the headphone, which is a bug in cras. However,
2588// chrome code is still resilient and set the internal speaker back to active.
2589TEST_F(CrasAudioHandlerTest, UnplugHeadphoneLostActiveInternalSpeakerByCras) {
2590 // Set up with three nodes.
2591 AudioNodeList audio_nodes;
2592 audio_nodes.push_back(kInternalSpeaker);
2593 audio_nodes.push_back(kHeadphone);
2594 audio_nodes.push_back(kUSBHeadphone1);
2595 SetUpCrasAudioHandler(audio_nodes);
2596
2597 // Switch the active output to internal speaker.
2598 cras_audio_handler_->SwitchToDevice(AudioDevice(kInternalSpeaker), true,
2599 CrasAudioHandler::ACTIVATE_BY_USER);
2600
2601 // Verify internal speaker has been made active.
2602 AudioDeviceList audio_devices;
2603 cras_audio_handler_->GetAudioDevices(&audio_devices);
2604 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2605 EXPECT_EQ(kInternalSpeaker.id,
2606 cras_audio_handler_->GetPrimaryActiveOutputNode());
2607
2608 // Simulate unplug the headphone. Cras sends NodesChanged signal with
2609 // both internal speaker and usb headphone being inactive.
2610 audio_nodes.clear();
2611 audio_nodes.push_back(kInternalSpeaker);
2612 audio_nodes.push_back(kUSBHeadphone1);
2613 EXPECT_FALSE(kInternalSpeaker.active);
2614 EXPECT_FALSE(kUSBHeadphone1.active);
2615 ChangeAudioNodes(audio_nodes);
2616
2617 // Verify the active output is set back to internal speaker.
2618 cras_audio_handler_->GetAudioDevices(&audio_devices);
2619 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2620 EXPECT_EQ(kInternalSpeaker.id,
2621 cras_audio_handler_->GetPrimaryActiveOutputNode());
2622}
2623
2624TEST_F(CrasAudioHandlerTest, RemoveNonActiveDevice) {
2625 // Set up with three nodes.
2626 AudioNodeList audio_nodes;
2627 audio_nodes.push_back(kInternalSpeaker);
2628 audio_nodes.push_back(kHeadphone);
2629 audio_nodes.push_back(kUSBHeadphone1);
2630 SetUpCrasAudioHandler(audio_nodes);
2631
2632 // Switch the active output to internal speaker.
2633 cras_audio_handler_->SwitchToDevice(AudioDevice(kInternalSpeaker), true,
2634 CrasAudioHandler::ACTIVATE_BY_USER);
2635
2636 // Verify internal speaker has been made active.
2637 AudioDeviceList audio_devices;
2638 cras_audio_handler_->GetAudioDevices(&audio_devices);
2639 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2640 EXPECT_EQ(kInternalSpeaker.id,
2641 cras_audio_handler_->GetPrimaryActiveOutputNode());
2642
2643 // Remove headphone, which is an non-active device.
2644 audio_nodes.clear();
2645 AudioNode speaker(kInternalSpeaker);
2646 speaker.active = true;
2647 audio_nodes.push_back(speaker);
2648 AudioNode usb_headphone(kUSBHeadphone1);
2649 usb_headphone.active = false;
2650 audio_nodes.push_back(usb_headphone);
2651
2652 ChangeAudioNodes(audio_nodes);
2653
2654 // Verify the active output remains as internal speaker.
2655 cras_audio_handler_->GetAudioDevices(&audio_devices);
2656 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2657 EXPECT_EQ(kInternalSpeaker.id,
2658 cras_audio_handler_->GetPrimaryActiveOutputNode());
2659}
2660
jennyz2cb9eb12014-11-05 19:21:052661TEST_F(CrasAudioHandlerTest, ChangeActiveNodesHotrodInit) {
jennyzb47947f2014-09-25 00:42:042662 AudioNodeList audio_nodes;
jennyz2cb9eb12014-11-05 19:21:052663 audio_nodes.push_back(kHDMIOutput);
2664 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2665 audio_nodes.push_back(kUSBJabraSpeakerOutput2);
2666 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2667 audio_nodes.push_back(kUSBJabraSpeakerInput2);
2668 audio_nodes.push_back(kUSBCameraInput);
jennyzb47947f2014-09-25 00:42:042669 SetUpCrasAudioHandler(audio_nodes);
2670
2671 // Verify the audio devices size.
2672 AudioDeviceList audio_devices;
2673 cras_audio_handler_->GetAudioDevices(&audio_devices);
2674 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2675
jennyz2cb9eb12014-11-05 19:21:052676 // Verify only the 1st jabra speaker's output and input are selected as active
2677 // nodes by CrasAudioHandler.
jennyzb47947f2014-09-25 00:42:042678 AudioDevice active_output;
2679 EXPECT_TRUE(
2680 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
jennyz2cb9eb12014-11-05 19:21:052681 EXPECT_EQ(2, GetActiveDeviceCount());
jennyzb47947f2014-09-25 00:42:042682 AudioDevice primary_active_device;
2683 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(
2684 &primary_active_device));
jennyz2cb9eb12014-11-05 19:21:052685 EXPECT_EQ(kUSBJabraSpeakerOutput1.id, primary_active_device.id);
2686 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2687 cras_audio_handler_->GetPrimaryActiveInputNode());
2688
2689 // Set both jabra speakers's input and output nodes to active, this simulate
2690 // the call sent by hotrod initialization process.
2691 test_observer_->reset_active_output_node_changed_count();
2692 test_observer_->reset_active_input_node_changed_count();
2693 CrasAudioHandler::NodeIdList active_nodes;
2694 active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2695 active_nodes.push_back(kUSBJabraSpeakerOutput2.id);
2696 active_nodes.push_back(kUSBJabraSpeakerInput1.id);
2697 active_nodes.push_back(kUSBJabraSpeakerInput2.id);
2698 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2699
2700 // Verify both jabra speakers' input/output nodes are made active.
2701 // num_active_nodes = GetActiveDeviceCount();
2702 EXPECT_EQ(4, GetActiveDeviceCount());
2703 const AudioDevice* active_output_1 =
2704 GetDeviceFromId(kUSBJabraSpeakerOutput1.id);
2705 EXPECT_TRUE(active_output_1->active);
2706 const AudioDevice* active_output_2 =
2707 GetDeviceFromId(kUSBJabraSpeakerOutput2.id);
2708 EXPECT_TRUE(active_output_2->active);
2709 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(
2710 &primary_active_device));
2711 EXPECT_EQ(kUSBJabraSpeakerOutput1.id, primary_active_device.id);
2712 const AudioDevice* active_input_1 =
2713 GetDeviceFromId(kUSBJabraSpeakerInput1.id);
2714 EXPECT_TRUE(active_input_1->active);
2715 const AudioDevice* active_input_2 =
2716 GetDeviceFromId(kUSBJabraSpeakerInput2.id);
2717 EXPECT_TRUE(active_input_2->active);
2718 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2719 cras_audio_handler_->GetPrimaryActiveInputNode());
2720
2721 // Verify only 1 ActiveOutputNodeChanged notification has been sent out
2722 // by calling ChangeActiveNodes.
2723 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
2724 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:042725
2726 // Verify all active devices are the not muted and their volume values are
2727 // the same.
2728 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
jennyz2cb9eb12014-11-05 19:21:052729 EXPECT_FALSE(
2730 cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput1.id));
2731 EXPECT_FALSE(
2732 cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput2.id));
2733 EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(),
2734 cras_audio_handler_->GetOutputVolumePercentForDevice(
2735 kUSBJabraSpeakerOutput1.id));
2736 EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(),
2737 cras_audio_handler_->GetOutputVolumePercentForDevice(
2738 kUSBJabraSpeakerOutput2.id));
jennyzb47947f2014-09-25 00:42:042739
2740 // Adjust the volume of output devices, verify all active nodes are set to
2741 // the same volume.
2742 cras_audio_handler_->SetOutputVolumePercent(25);
2743 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercent());
jennyz2cb9eb12014-11-05 19:21:052744 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercentForDevice(
2745 kUSBJabraSpeakerOutput1.id));
2746 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercentForDevice(
2747 kUSBJabraSpeakerOutput2.id));
2748}
jennyzb47947f2014-09-25 00:42:042749
jennyz96526d02016-06-22 17:03:432750TEST_F(CrasAudioHandlerTest, ChangeVolumeHotrodDualSpeakersWithDelayedSignals) {
2751 AudioNodeList audio_nodes;
2752 audio_nodes.push_back(kHDMIOutput);
2753 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2754 audio_nodes.push_back(kUSBJabraSpeakerOutput2);
2755 SetUpCrasAudioHandler(audio_nodes);
2756
2757 // Verify the audio devices size.
2758 AudioDeviceList audio_devices;
2759 cras_audio_handler_->GetAudioDevices(&audio_devices);
2760 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2761
2762 // Set both jabra speakers nodes to active, this simulate
2763 // the call sent by hotrod initialization process.
2764 test_observer_->reset_active_output_node_changed_count();
2765 CrasAudioHandler::NodeIdList active_nodes;
2766 active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2767 active_nodes.push_back(kUSBJabraSpeakerOutput2.id);
2768 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2769
2770 // Verify both jabra speakers are made active.
2771 EXPECT_EQ(2, GetActiveDeviceCount());
2772 const AudioDevice* active_output_1 =
2773 GetDeviceFromId(kUSBJabraSpeakerOutput1.id);
2774 EXPECT_TRUE(active_output_1->active);
2775 const AudioDevice* active_output_2 =
2776 GetDeviceFromId(kUSBJabraSpeakerOutput2.id);
2777 EXPECT_TRUE(active_output_2->active);
2778
2779 // Verify all active devices are the not muted and their volume values are
2780 // the same.
2781 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
2782 EXPECT_FALSE(
2783 cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput1.id));
2784 EXPECT_FALSE(
2785 cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput2.id));
2786 EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(),
2787 cras_audio_handler_->GetOutputVolumePercentForDevice(
2788 kUSBJabraSpeakerOutput1.id));
2789 EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(),
2790 cras_audio_handler_->GetOutputVolumePercentForDevice(
2791 kUSBJabraSpeakerOutput2.id));
2792 const int kDefaultVolume = 75;
2793 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2794
2795 // Disable the auto OutputNodeVolumeChanged signal.
2796 fake_cras_audio_client_->set_notify_volume_change_with_delay(true);
2797 test_observer_->reset_output_volume_changed_count();
2798
2799 // Adjust the volume of output devices continuously.
2800 cras_audio_handler_->SetOutputVolumePercent(20);
2801 cras_audio_handler_->SetOutputVolumePercent(30);
2802
2803 // Sends delayed OutputNodeVolumeChanged signals.
2804 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
2805 kUSBJabraSpeakerOutput2.id, 20);
2806 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
2807 kUSBJabraSpeakerOutput1.id, 20);
2808 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
2809 kUSBJabraSpeakerOutput2.id, 30);
2810 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
2811 kUSBJabraSpeakerOutput1.id, 30);
2812
2813 // Verify that both speakers are set to the designated volume level after
2814 // receiving all delayed signals.
2815 EXPECT_EQ(4, test_observer_->output_volume_changed_count());
2816 EXPECT_EQ(30, cras_audio_handler_->GetOutputVolumePercent());
2817 EXPECT_EQ(30, cras_audio_handler_->GetOutputVolumePercentForDevice(
2818 kUSBJabraSpeakerOutput1.id));
2819 EXPECT_EQ(30, cras_audio_handler_->GetOutputVolumePercentForDevice(
2820 kUSBJabraSpeakerOutput2.id));
2821}
2822
jennyz2cb9eb12014-11-05 19:21:052823TEST_F(CrasAudioHandlerTest, ChangeActiveNodesHotrodInitWithCameraInputActive) {
2824 AudioNodeList audio_nodes;
2825 audio_nodes.push_back(kHDMIOutput);
2826 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2827 audio_nodes.push_back(kUSBJabraSpeakerOutput2);
2828 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2829 audio_nodes.push_back(kUSBJabraSpeakerInput2);
2830 // Make the camera input to be plugged in later than jabra's input.
2831 AudioNode usb_camera(kUSBCameraInput);
2832 usb_camera.plugged_time = 10000000;
2833 audio_nodes.push_back(usb_camera);
2834 SetUpCrasAudioHandler(audio_nodes);
2835
2836 // Verify the audio devices size.
2837 AudioDeviceList audio_devices;
2838 cras_audio_handler_->GetAudioDevices(&audio_devices);
2839 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2840
2841 // Verify the 1st jabra speaker's output is selected as active output
2842 // node and camera's input is selected active input by CrasAudioHandler.
2843 EXPECT_EQ(2, GetActiveDeviceCount());
2844 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2845 cras_audio_handler_->GetPrimaryActiveOutputNode());
2846 EXPECT_EQ(kUSBCameraInput.id,
2847 cras_audio_handler_->GetPrimaryActiveInputNode());
2848
2849 // Set both jabra speakers's input and output nodes to active, this simulates
2850 // the call sent by hotrod initialization process.
2851 test_observer_->reset_active_output_node_changed_count();
2852 test_observer_->reset_active_input_node_changed_count();
2853 CrasAudioHandler::NodeIdList active_nodes;
2854 active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2855 active_nodes.push_back(kUSBJabraSpeakerOutput2.id);
2856 active_nodes.push_back(kUSBJabraSpeakerInput1.id);
2857 active_nodes.push_back(kUSBJabraSpeakerInput2.id);
2858 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2859
2860 // Verify both jabra speakers' input/output nodes are made active.
2861 // num_active_nodes = GetActiveDeviceCount();
2862 EXPECT_EQ(4, GetActiveDeviceCount());
2863 const AudioDevice* active_output_1 =
2864 GetDeviceFromId(kUSBJabraSpeakerOutput1.id);
2865 EXPECT_TRUE(active_output_1->active);
2866 const AudioDevice* active_output_2 =
2867 GetDeviceFromId(kUSBJabraSpeakerOutput2.id);
2868 EXPECT_TRUE(active_output_2->active);
2869 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2870 cras_audio_handler_->GetPrimaryActiveOutputNode());
2871 const AudioDevice* active_input_1 =
2872 GetDeviceFromId(kUSBJabraSpeakerInput1.id);
2873 EXPECT_TRUE(active_input_1->active);
2874 const AudioDevice* active_input_2 =
2875 GetDeviceFromId(kUSBJabraSpeakerInput2.id);
2876 EXPECT_TRUE(active_input_2->active);
2877 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2878 cras_audio_handler_->GetPrimaryActiveInputNode());
2879
2880 // Verify only 1 ActiveOutputNodeChanged notification has been sent out
2881 // by calling ChangeActiveNodes.
2882 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
2883 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
2884}
2885
2886TEST_F(CrasAudioHandlerTest, ChangeActiveNodesWithFewerActives) {
2887 AudioNodeList audio_nodes;
2888 audio_nodes.push_back(kHDMIOutput);
2889 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2890 audio_nodes.push_back(kUSBJabraSpeakerOutput2);
2891 SetUpCrasAudioHandler(audio_nodes);
2892
2893 // Verify the audio devices size.
2894 AudioDeviceList audio_devices;
2895 cras_audio_handler_->GetAudioDevices(&audio_devices);
2896 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2897
2898 // Set all three nodes to be active.
2899 CrasAudioHandler::NodeIdList active_nodes;
2900 active_nodes.push_back(kHDMIOutput.id);
2901 active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2902 active_nodes.push_back(kUSBJabraSpeakerOutput2.id);
2903 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2904
2905 // Verify all three nodes are active.
2906 EXPECT_EQ(3, GetActiveDeviceCount());
2907 const AudioDevice* active_output_1 = GetDeviceFromId(kHDMIOutput.id);
2908 EXPECT_TRUE(active_output_1->active);
2909 const AudioDevice* active_output_2 =
2910 GetDeviceFromId(kUSBJabraSpeakerOutput1.id);
2911 EXPECT_TRUE(active_output_2->active);
2912 const AudioDevice* active_output_3 =
2913 GetDeviceFromId(kUSBJabraSpeakerOutput2.id);
2914 EXPECT_TRUE(active_output_3->active);
2915
2916 // Now call ChangeActiveDevices with only 2 nodes.
2917 active_nodes.clear();
2918 active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2919 active_nodes.push_back(kUSBJabraSpeakerOutput2.id);
2920 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2921
2922 // Verify only 2 nodes are active.
2923 EXPECT_EQ(2, GetActiveDeviceCount());
2924 const AudioDevice* output_1 = GetDeviceFromId(kHDMIOutput.id);
2925 EXPECT_FALSE(output_1->active);
2926 const AudioDevice* output_2 = GetDeviceFromId(kUSBJabraSpeakerOutput1.id);
2927 EXPECT_TRUE(output_2->active);
2928 const AudioDevice* output_3 = GetDeviceFromId(kUSBJabraSpeakerOutput2.id);
2929 EXPECT_TRUE(output_3->active);
2930}
2931
2932TEST_F(CrasAudioHandlerTest, HotrodInitWithSingleJabra) {
2933 // Simulates the hotrod initializated with a single jabra device and
2934 // CrasAudioHandler selected jabra input/output as active devices.
2935 AudioNodeList audio_nodes;
2936 audio_nodes.push_back(kHDMIOutput);
2937 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2938 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2939 audio_nodes.push_back(kUSBCameraInput);
2940 SetUpCrasAudioHandler(audio_nodes);
2941
2942 // Verify the audio devices size.
2943 AudioDeviceList audio_devices;
2944 cras_audio_handler_->GetAudioDevices(&audio_devices);
2945 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2946
2947 // Verify the jabra speaker's output and input are selected as active nodes
2948 // by CrasAudioHandler.
2949 EXPECT_EQ(2, GetActiveDeviceCount());
2950 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2951 cras_audio_handler_->GetPrimaryActiveOutputNode());
2952 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2953 cras_audio_handler_->GetPrimaryActiveInputNode());
2954}
2955
2956TEST_F(CrasAudioHandlerTest,
2957 ChangeActiveNodesHotrodInitWithSingleJabraCameraPlugInLater) {
2958 AudioNodeList audio_nodes;
2959 audio_nodes.push_back(kHDMIOutput);
2960 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2961 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2962 AudioNode usb_camera(kUSBCameraInput);
2963 usb_camera.plugged_time = 10000000;
2964 audio_nodes.push_back(usb_camera);
2965 SetUpCrasAudioHandler(audio_nodes);
2966
2967 // Verify the audio devices size.
2968 AudioDeviceList audio_devices;
2969 cras_audio_handler_->GetAudioDevices(&audio_devices);
2970 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2971
2972 // Verify the jabra speaker's output is selected as active output, and
2973 // camera's input is selected as active input by CrasAudioHandler
2974 EXPECT_EQ(2, GetActiveDeviceCount());
2975 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2976 cras_audio_handler_->GetPrimaryActiveOutputNode());
2977 EXPECT_EQ(kUSBCameraInput.id,
2978 cras_audio_handler_->GetPrimaryActiveInputNode());
2979
2980 // Simulate hotrod app call to set jabra input as active device with only
2981 // jabra input node in the active node list, which does not conform to the
2982 // new SetActiveDevices protocol, but just show we can still handle it if
2983 // this happens.
2984 CrasAudioHandler::NodeIdList active_nodes;
2985 active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2986 active_nodes.push_back(kUSBJabraSpeakerInput1.id);
2987 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2988
2989 // Verify the jabra speaker's output is selected as active output, and
2990 // jabra's input is selected as active input.
2991 EXPECT_EQ(2, GetActiveDeviceCount());
2992 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
2993 cras_audio_handler_->GetPrimaryActiveOutputNode());
2994 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
2995 cras_audio_handler_->GetPrimaryActiveInputNode());
2996}
2997
tbarzic96ea32052016-11-03 00:46:162998TEST_F(CrasAudioHandlerTest, ChangeActiveNodesDeactivatePrimaryActiveNode) {
2999 AudioNodeList audio_nodes;
3000 audio_nodes.push_back(kUSBJabraSpeakerInput1);
3001 audio_nodes.push_back(kUSBJabraSpeakerInput2);
3002 AudioNode usb_camera(kUSBCameraInput);
3003 usb_camera.plugged_time = 10000000;
3004 audio_nodes.push_back(usb_camera);
3005 SetUpCrasAudioHandler(audio_nodes);
3006
3007 // Verify the audio devices size.
3008 AudioDeviceList audio_devices;
3009 cras_audio_handler_->GetAudioDevices(&audio_devices);
3010 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3011
3012 // Verify the camera's input is selected as active input by CrasAudioHandler
3013 EXPECT_EQ(1, GetActiveDeviceCount());
3014 EXPECT_EQ(kUSBCameraInput.id,
3015 cras_audio_handler_->GetPrimaryActiveInputNode());
3016
3017 // Simulate hotrod app call to set jabra input as active device with only
3018 // jabra input node in the active node list, which does not conform to the
3019 // new SetActiveDevices protocol, but just show we can still handle it if
3020 // this happens.
3021 {
3022 CrasAudioHandler::NodeIdList active_nodes;
3023 active_nodes.push_back(kUSBJabraSpeakerInput1.id);
3024 active_nodes.push_back(kUSBCameraInput.id);
3025 cras_audio_handler_->ChangeActiveNodes(active_nodes);
3026 }
3027
3028 // Verify the jabra speaker's output is selected as active output, and
3029 // jabra's input is selected as active input.
3030 EXPECT_EQ(2, GetActiveDeviceCount());
3031 EXPECT_EQ(kUSBCameraInput.id,
3032 cras_audio_handler_->GetPrimaryActiveInputNode());
3033
3034 const AudioDevice* additional_speaker =
3035 cras_audio_handler_->GetDeviceFromId(kUSBJabraSpeakerInput1.id);
3036 ASSERT_TRUE(additional_speaker);
3037 EXPECT_TRUE(additional_speaker->active);
3038
3039 {
3040 CrasAudioHandler::NodeIdList active_nodes;
3041 active_nodes.push_back(kUSBJabraSpeakerInput1.id);
3042 active_nodes.push_back(kUSBJabraSpeakerInput2.id);
3043 cras_audio_handler_->ChangeActiveNodes(active_nodes);
3044 }
3045
3046 EXPECT_EQ(2, GetActiveDeviceCount());
3047 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
3048 cras_audio_handler_->GetPrimaryActiveInputNode());
3049
3050 additional_speaker =
3051 cras_audio_handler_->GetDeviceFromId(kUSBJabraSpeakerInput2.id);
3052 ASSERT_TRUE(additional_speaker);
3053 EXPECT_TRUE(additional_speaker->active);
3054}
3055
jennyz2cb9eb12014-11-05 19:21:053056TEST_F(CrasAudioHandlerTest,
3057 ChangeActiveNodesHotrodInitWithSingleJabraCameraPlugInLaterOldCall) {
3058 AudioNodeList audio_nodes;
3059 audio_nodes.push_back(kHDMIOutput);
3060 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
3061 audio_nodes.push_back(kUSBJabraSpeakerInput1);
3062 AudioNode usb_camera(kUSBCameraInput);
3063 usb_camera.plugged_time = 10000000;
3064 audio_nodes.push_back(usb_camera);
3065 SetUpCrasAudioHandler(audio_nodes);
3066
3067 // Verify the audio devices size.
3068 AudioDeviceList audio_devices;
3069 cras_audio_handler_->GetAudioDevices(&audio_devices);
3070 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3071
3072 // Verify the jabra speaker's output is selected as active output, and
3073 // camera's input is selected as active input by CrasAudioHandler
3074 EXPECT_EQ(2, GetActiveDeviceCount());
3075 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
3076 cras_audio_handler_->GetPrimaryActiveOutputNode());
3077 EXPECT_EQ(kUSBCameraInput.id,
3078 cras_audio_handler_->GetPrimaryActiveInputNode());
3079
3080 // Simulate hotrod app call to set jabra input as active device with only
3081 // jabra input node in the active node list, which does not conform to the
3082 // new SetActiveDevices protocol, but just show we can still handle it if
3083 // this happens.
3084 CrasAudioHandler::NodeIdList active_nodes;
3085 active_nodes.push_back(kUSBJabraSpeakerInput1.id);
3086 cras_audio_handler_->ChangeActiveNodes(active_nodes);
3087
3088 // Verify the jabra speaker's output is selected as active output, and
3089 // jabra's input is selected as active input.
3090 EXPECT_EQ(2, GetActiveDeviceCount());
3091 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
3092 cras_audio_handler_->GetPrimaryActiveOutputNode());
3093 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
3094 cras_audio_handler_->GetPrimaryActiveInputNode());
3095}
3096
3097TEST_F(CrasAudioHandlerTest,
3098 ChangeActiveNodesHotrodInitWithSingleJabraChangeOutput) {
3099 AudioNodeList audio_nodes;
3100 audio_nodes.push_back(kHDMIOutput);
3101 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
3102 audio_nodes.push_back(kUSBJabraSpeakerInput1);
3103 audio_nodes.push_back(kUSBCameraInput);
3104 SetUpCrasAudioHandler(audio_nodes);
3105
3106 // Verify the audio devices size.
3107 AudioDeviceList audio_devices;
3108 cras_audio_handler_->GetAudioDevices(&audio_devices);
3109 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3110
3111 // Verify the jabra speaker's output and input are selected as active output
3112 // by CrasAudioHandler.
3113 EXPECT_EQ(2, GetActiveDeviceCount());
3114 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
3115 cras_audio_handler_->GetPrimaryActiveOutputNode());
3116 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
3117 cras_audio_handler_->GetPrimaryActiveInputNode());
3118
3119 // Simulate hotrod app call SetActiveDevices to change active output
3120 // with only complete list of active nodes passed in, which is the new
3121 // way of hotrod app.
3122 CrasAudioHandler::NodeIdList active_nodes;
3123 active_nodes.push_back(kHDMIOutput.id);
3124 active_nodes.push_back(kUSBJabraSpeakerInput1.id);
3125 cras_audio_handler_->ChangeActiveNodes(active_nodes);
3126
3127 // Verify the jabra speaker's output is selected as active output, and
3128 // jabra's input is selected as active input.
3129 EXPECT_EQ(2, GetActiveDeviceCount());
3130 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3131 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
3132 cras_audio_handler_->GetPrimaryActiveInputNode());
3133}
3134
3135TEST_F(CrasAudioHandlerTest,
3136 ChangeActiveNodesHotrodInitWithSingleJabraChangeOutputOldCall) {
3137 AudioNodeList audio_nodes;
3138 audio_nodes.push_back(kHDMIOutput);
3139 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
3140 audio_nodes.push_back(kUSBJabraSpeakerInput1);
3141 audio_nodes.push_back(kUSBCameraInput);
3142 SetUpCrasAudioHandler(audio_nodes);
3143
3144 // Verify the audio devices size.
3145 AudioDeviceList audio_devices;
3146 cras_audio_handler_->GetAudioDevices(&audio_devices);
3147 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3148
3149 // Verify the jabra speaker's output and input are selected as active output
3150 // by CrasAudioHandler.
3151 EXPECT_EQ(2, GetActiveDeviceCount());
3152 EXPECT_EQ(kUSBJabraSpeakerOutput1.id,
3153 cras_audio_handler_->GetPrimaryActiveOutputNode());
3154 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
3155 cras_audio_handler_->GetPrimaryActiveInputNode());
3156
3157 // Simulate hotrod app call SetActiveDevices to change active output
3158 // with only a single active output nodes passed in, which is the old
3159 // way of hotrod app.
3160 CrasAudioHandler::NodeIdList active_nodes;
3161 active_nodes.push_back(kHDMIOutput.id);
3162 cras_audio_handler_->ChangeActiveNodes(active_nodes);
3163
3164 // Verify the jabra speaker's output is selected as active output, and
3165 // jabra's input is selected as active input.
3166 EXPECT_EQ(2, GetActiveDeviceCount());
3167 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3168 EXPECT_EQ(kUSBJabraSpeakerInput1.id,
3169 cras_audio_handler_->GetPrimaryActiveInputNode());
jennyzb47947f2014-09-25 00:42:043170}
3171
mukai307596b62014-11-24 19:56:593172TEST_F(CrasAudioHandlerTest, NoMoreAudioInputDevices) {
3173 // Some device like chromebox does not have the internal input device. The
3174 // active devices should be reset when the user plugs a device and then
3175 // unplugs it to such device.
3176
3177 AudioNodeList audio_nodes;
3178 audio_nodes.push_back(kInternalSpeaker);
3179 SetUpCrasAudioHandler(audio_nodes);
3180
3181 EXPECT_EQ(0ULL, cras_audio_handler_->GetPrimaryActiveInputNode());
3182
3183 audio_nodes.push_back(kMicJack);
3184 ChangeAudioNodes(audio_nodes);
3185
3186 EXPECT_EQ(kMicJack.id, cras_audio_handler_->GetPrimaryActiveInputNode());
3187 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
3188 test_observer_->reset_active_input_node_changed_count();
3189
3190 audio_nodes.pop_back();
3191 ChangeAudioNodes(audio_nodes);
3192 EXPECT_EQ(0ULL, cras_audio_handler_->GetPrimaryActiveInputNode());
3193 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
3194}
3195
jennyz731ba262015-02-05 19:00:033196// Test the case in which an HDMI output is plugged in with other higher
3197// priority
3198// output devices already plugged and user has manually selected an active
3199// output.
3200// The hotplug of hdmi output should not change user's selection of active
3201// device.
3202// crbug.com/447826.
3203TEST_F(CrasAudioHandlerTest, HotPlugHDMINotChangeActiveOutput) {
3204 AudioNodeList audio_nodes;
3205 AudioNode internal_speaker(kInternalSpeaker);
3206 audio_nodes.push_back(internal_speaker);
3207 AudioNode usb_headset(kUSBHeadphone1);
3208 usb_headset.plugged_time = 80000000;
3209 audio_nodes.push_back(usb_headset);
3210 SetUpCrasAudioHandler(audio_nodes);
3211
3212 // Verify the audio devices size.
3213 AudioDeviceList audio_devices;
3214 cras_audio_handler_->GetAudioDevices(&audio_devices);
3215 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3216
3217 // Verify the USB headset is selected as active output by default.
3218 EXPECT_EQ(usb_headset.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3219
3220 // Manually set the active output to internal speaker.
3221 AudioDevice internal_output(kInternalSpeaker);
jennyzbd236742016-03-02 20:15:523222 cras_audio_handler_->SwitchToDevice(internal_output, true,
3223 CrasAudioHandler::ACTIVATE_BY_USER);
jennyz731ba262015-02-05 19:00:033224
3225 // Verify the active output is switched to internal speaker.
3226 EXPECT_EQ(internal_speaker.id,
3227 cras_audio_handler_->GetPrimaryActiveOutputNode());
3228 EXPECT_LT(kInternalSpeaker.plugged_time, usb_headset.plugged_time);
3229 const AudioDevice* usb_device = GetDeviceFromId(usb_headset.id);
3230 EXPECT_FALSE(usb_device->active);
3231
3232 // Plug in HDMI output.
3233 audio_nodes.clear();
3234 internal_speaker.active = true;
3235 audio_nodes.push_back(internal_speaker);
3236 usb_headset.active = false;
3237 audio_nodes.push_back(usb_headset);
3238 AudioNode hdmi(kHDMIOutput);
3239 hdmi.plugged_time = 90000000;
3240 audio_nodes.push_back(hdmi);
3241 ChangeAudioNodes(audio_nodes);
3242
3243 // The active output should not change.
3244 EXPECT_EQ(kInternalSpeaker.id,
3245 cras_audio_handler_->GetPrimaryActiveOutputNode());
3246}
3247
jennyz3147abc72015-04-29 00:13:033248// Test the case in which the active device was set to inactive from cras after
3249// resuming from suspension state. See crbug.com/478968.
3250TEST_F(CrasAudioHandlerTest, ActiveNodeLostAfterResume) {
3251 AudioNodeList audio_nodes;
3252 EXPECT_FALSE(kHeadphone.active);
3253 audio_nodes.push_back(kHeadphone);
3254 EXPECT_FALSE(kHDMIOutput.active);
3255 audio_nodes.push_back(kHDMIOutput);
3256 SetUpCrasAudioHandler(audio_nodes);
3257
3258 // Verify the headphone is selected as the active output.
3259 AudioDeviceList audio_devices;
3260 cras_audio_handler_->GetAudioDevices(&audio_devices);
3261 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3262 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3263 const AudioDevice* active_headphone = GetDeviceFromId(kHeadphone.id);
3264 EXPECT_EQ(kHeadphone.id, active_headphone->id);
3265 EXPECT_TRUE(active_headphone->active);
3266
3267 // Simulate NodesChanged signal with headphone turning into inactive state,
3268 // and HDMI node removed.
3269 audio_nodes.clear();
3270 audio_nodes.push_back(kHeadphone);
3271 ChangeAudioNodes(audio_nodes);
3272
3273 // Verify the headphone is set to active again.
3274 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3275 const AudioDevice* headphone_resumed = GetDeviceFromId(kHeadphone.id);
3276 EXPECT_EQ(kHeadphone.id, headphone_resumed->id);
3277 EXPECT_TRUE(headphone_resumed->active);
3278}
3279
jennyz639ec6f2016-01-19 23:27:303280// In the mirror mode, when the device resumes after being suspended, the hmdi
3281// node will be lost first, then re-appear with a different node id, but with
3282// the same stable id. If it is set as the non-active node by user before
3283// suspend/resume, it should remain inactive after the device resumes even
3284// if it has a higher priority than the current active node.
3285// crbug.com/443014.
3286TEST_F(CrasAudioHandlerTest, HDMIRemainInactiveAfterSuspendResume) {
3287 AudioNodeList audio_nodes;
3288 AudioNode internal_speaker(kInternalSpeaker);
3289 audio_nodes.push_back(internal_speaker);
3290 AudioNode hdmi_output(kHDMIOutput);
3291 audio_nodes.push_back(hdmi_output);
3292 SetUpCrasAudioHandler(audio_nodes);
3293
3294 // Verify the hdmi is selected as the active output since it has a higher
3295 // priority.
3296 AudioDeviceList audio_devices;
3297 cras_audio_handler_->GetAudioDevices(&audio_devices);
3298 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3299 EXPECT_EQ(hdmi_output.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3300
3301 // Manually set the active output to internal speaker.
jennyzbd236742016-03-02 20:15:523302 cras_audio_handler_->SwitchToDevice(AudioDevice(internal_speaker), true,
3303 CrasAudioHandler::ACTIVATE_BY_USER);
jennyz639ec6f2016-01-19 23:27:303304 EXPECT_EQ(internal_speaker.id,
3305 cras_audio_handler_->GetPrimaryActiveOutputNode());
3306
3307 // Simulate the suspend and resume of the device during mirror mode. The HDMI
3308 // node will be lost first.
3309 audio_nodes.clear();
3310 internal_speaker.active = true;
3311 audio_nodes.push_back(internal_speaker);
3312 ChangeAudioNodes(audio_nodes);
3313
3314 // Verify the HDMI node is lost, and internal speaker is still the active
3315 // node.
3316 cras_audio_handler_->GetAudioDevices(&audio_devices);
3317 EXPECT_EQ(1u, audio_devices.size());
3318 EXPECT_EQ(internal_speaker.id,
3319 cras_audio_handler_->GetPrimaryActiveOutputNode());
3320
3321 // Simulate the re-appearing of the hdmi node, which comes with a new id,
3322 // but the same stable device id.
3323 AudioNode hdmi_output_2(hdmi_output);
3324 hdmi_output_2.id = 20006;
3325 hdmi_output_2.plugged_time = internal_speaker.plugged_time + 100;
3326 audio_nodes.push_back(hdmi_output_2);
3327 EXPECT_NE(hdmi_output.id, hdmi_output_2.id);
3328 EXPECT_EQ(hdmi_output.stable_device_id, hdmi_output_2.stable_device_id);
3329 ChangeAudioNodes(audio_nodes);
3330
3331 // Verify the hdmi node is not set the active, and the current active node
3332 // , the internal speaker, remains active.
3333 cras_audio_handler_->GetAudioDevices(&audio_devices);
3334 EXPECT_EQ(2u, audio_devices.size());
3335 EXPECT_EQ(internal_speaker.id,
3336 cras_audio_handler_->GetPrimaryActiveOutputNode());
3337}
3338
jennyz3147abc72015-04-29 00:13:033339// Test the case in which there are two NodesChanged signal for discovering
3340// output devices, and there is race between NodesChange and SetActiveOutput
3341// during this process. See crbug.com/478968.
3342TEST_F(CrasAudioHandlerTest, ActiveNodeLostDuringLoginSession) {
3343 AudioNodeList audio_nodes;
3344 EXPECT_FALSE(kHeadphone.active);
3345 audio_nodes.push_back(kHeadphone);
3346 SetUpCrasAudioHandler(audio_nodes);
3347
3348 // Verify the headphone is selected as the active output.
3349 AudioDeviceList audio_devices;
3350 cras_audio_handler_->GetAudioDevices(&audio_devices);
3351 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3352 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3353 const AudioDevice* active_headphone = GetDeviceFromId(kHeadphone.id);
3354 EXPECT_EQ(kHeadphone.id, active_headphone->id);
3355 EXPECT_TRUE(active_headphone->active);
3356
3357 // Simulate NodesChanged signal with headphone turning into inactive state,
3358 // and add a new HDMI output node.
3359 audio_nodes.clear();
3360 audio_nodes.push_back(kHeadphone);
3361 audio_nodes.push_back(kHDMIOutput);
3362 ChangeAudioNodes(audio_nodes);
3363
3364 // Verify the headphone is set to active again.
3365 EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3366 const AudioDevice* headphone_resumed = GetDeviceFromId(kHeadphone.id);
3367 EXPECT_EQ(kHeadphone.id, headphone_resumed->id);
3368 EXPECT_TRUE(headphone_resumed->active);
3369}
3370
jennyzc770dc772015-06-29 23:46:063371// This test HDMI output rediscovering case in crbug.com/503667.
3372TEST_F(CrasAudioHandlerTest, HDMIOutputRediscover) {
3373 AudioNodeList audio_nodes;
3374 audio_nodes.push_back(kInternalSpeaker);
3375 audio_nodes.push_back(kHDMIOutput);
3376 SetUpCrasAudioHandler(audio_nodes);
3377
3378 // Verify the HDMI device has been selected as the active output, and audio
3379 // output is not muted.
3380 AudioDevice active_output;
3381 EXPECT_TRUE(
3382 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
3383 EXPECT_EQ(kHDMIOutput.id, active_output.id);
3384 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3385 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
3386 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
3387
3388 // Trigger HDMI rediscovering grace period, and remove the HDMI node.
3389 const int grace_period_in_ms = 200;
3390 SetHDMIRediscoverGracePeriodDuration(grace_period_in_ms);
3391 SetActiveHDMIRediscover();
3392 AudioNodeList audio_nodes_lost_hdmi;
3393 audio_nodes_lost_hdmi.push_back(kInternalSpeaker);
3394 ChangeAudioNodes(audio_nodes_lost_hdmi);
3395
3396 // Verify the active output is switched to internal speaker, it is not muted
3397 // by preference, but the system output is muted during the grace period.
3398 EXPECT_TRUE(
3399 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
3400 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
3401 EXPECT_FALSE(
3402 cras_audio_handler_->IsOutputMutedForDevice(kInternalSpeaker.id));
3403 EXPECT_TRUE(cras_audio_handler_->IsOutputMuted());
3404
3405 // Re-attach the HDMI device after a little delay.
3406 HDMIRediscoverWaiter waiter(this, grace_period_in_ms);
3407 waiter.WaitUntilTimeOut(grace_period_in_ms / 4);
3408 ChangeAudioNodes(audio_nodes);
3409
3410 // After HDMI re-discover grace period, verify HDMI output is selected as the
3411 // active device and not muted.
3412 waiter.WaitUntilHDMIRediscoverGracePeriodEnd();
3413 EXPECT_TRUE(
3414 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
3415 EXPECT_EQ(kHDMIOutput.id, active_output.id);
3416 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3417 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
3418}
3419
jennyzc79ccda2015-07-28 23:04:353420// This tests the case of output unmuting event is notified after the hdmi
3421// output re-discover grace period ends, see crbug.com/512601.
3422TEST_F(CrasAudioHandlerTest, HDMIOutputUnplugDuringSuspension) {
3423 AudioNodeList audio_nodes;
3424 audio_nodes.push_back(kInternalSpeaker);
3425 audio_nodes.push_back(kHDMIOutput);
3426 SetUpCrasAudioHandler(audio_nodes);
3427
3428 // Verify the HDMI device has been selected as the active output, and audio
3429 // output is not muted.
3430 AudioDevice active_output;
3431 EXPECT_TRUE(
3432 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
3433 EXPECT_EQ(kHDMIOutput.id, active_output.id);
3434 EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3435 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
3436 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
3437
3438 // Trigger HDMI rediscovering grace period, and remove the HDMI node.
3439 const int grace_period_in_ms = 200;
3440 SetHDMIRediscoverGracePeriodDuration(grace_period_in_ms);
3441 SetActiveHDMIRediscover();
3442 AudioNodeList audio_nodes_lost_hdmi;
3443 audio_nodes_lost_hdmi.push_back(kInternalSpeaker);
3444 ChangeAudioNodes(audio_nodes_lost_hdmi);
3445
3446 // Verify the active output is switched to internal speaker, it is not muted
3447 // by preference, but the system output is muted during the grace period.
3448 EXPECT_TRUE(
3449 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
3450 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
3451 EXPECT_FALSE(
3452 cras_audio_handler_->IsOutputMutedForDevice(kInternalSpeaker.id));
3453 EXPECT_TRUE(cras_audio_handler_->IsOutputMuted());
3454
3455 // After HDMI re-discover grace period, verify internal speaker is still the
3456 // active output and not muted, and unmute event by system is notified.
3457 test_observer_->reset_output_mute_changed_count();
3458 HDMIRediscoverWaiter waiter(this, grace_period_in_ms);
3459 waiter.WaitUntilHDMIRediscoverGracePeriodEnd();
3460 EXPECT_TRUE(
3461 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
3462 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
3463 EXPECT_EQ(kInternalSpeaker.id,
3464 cras_audio_handler_->GetPrimaryActiveOutputNode());
3465 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
3466 EXPECT_EQ(1, test_observer_->output_mute_changed_count());
3467 EXPECT_TRUE(test_observer_->output_mute_by_system());
3468}
3469
[email protected]ee38bc42013-07-29 21:41:323470} // namespace chromeos