blob: 342aa5b29b9c5ee5780bd5801e9bddcc8ae526f6 [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"
jennyzc770dc772015-06-29 23:46:0615#include "base/run_loop.h"
Max Morin7a2d72a2017-07-20 19:06:2516#include "base/system_monitor/system_monitor.h"
fdoray822e6b22017-04-27 20:47:0317#include "base/test/scoped_task_environment.h"
gabd85cc46b2016-05-11 18:23:2018#include "base/threading/thread_task_runner_handle.h"
[email protected]ee38bc42013-07-29 21:41:3219#include "base/values.h"
jennyzbd236742016-03-02 20:15:5220#include "chromeos/audio/audio_devices_pref_handler.h"
[email protected]ee38bc42013-07-29 21:41:3221#include "chromeos/audio/audio_devices_pref_handler_stub.h"
22#include "chromeos/dbus/audio_node.h"
[email protected]ee38bc42013-07-29 21:41:3223#include "chromeos/dbus/dbus_thread_manager.h"
satoruxcca5cc12014-10-27 16:33:5924#include "chromeos/dbus/fake_cras_audio_client.h"
jennyzfbaa3e62017-03-06 20:14:0225#include "media/base/video_facing.h"
[email protected]ee38bc42013-07-29 21:41:3226#include "testing/gtest/include/gtest/gtest.h"
27
28namespace chromeos {
oshima2aaee552015-07-09 16:50:2629namespace {
[email protected]ee38bc42013-07-29 21:41:3230
avi6e1a22d2015-12-21 03:43:2031const uint64_t kInternalSpeakerId = 10001;
32const uint64_t kHeadphoneId = 10002;
33const uint64_t kInternalMicId = 10003;
34const uint64_t kUSBMicId = 10004;
35const uint64_t kBluetoothHeadsetId = 10005;
36const uint64_t kHDMIOutputId = 10006;
37const uint64_t kUSBHeadphoneId1 = 10007;
38const uint64_t kUSBHeadphoneId2 = 10008;
39const uint64_t kMicJackId = 10009;
40const uint64_t kKeyboardMicId = 10010;
jennyzfbaa3e62017-03-06 20:14:0241const uint64_t kFrontMicId = 10011;
42const uint64_t kRearMicId = 10012;
avi6e1a22d2015-12-21 03:43:2043const uint64_t kOtherTypeOutputId = 90001;
44const uint64_t kOtherTypeInputId = 90002;
45const uint64_t kUSBJabraSpeakerOutputId1 = 90003;
46const uint64_t kUSBJabraSpeakerOutputId2 = 90004;
47const uint64_t kUSBJabraSpeakerInputId1 = 90005;
48const uint64_t kUSBJabraSpeakerInputId2 = 90006;
49const uint64_t kUSBCameraInputId = 90007;
[email protected]ee38bc42013-07-29 21:41:3250
tbarzic3ab01a22016-12-17 03:12:5351struct AudioNodeInfo {
52 bool is_input;
53 uint64_t id;
54 const char* const device_name;
55 const char* const type;
56 const char* const name;
57};
[email protected]ee38bc42013-07-29 21:41:3258
tbarzic3ab01a22016-12-17 03:12:5359const AudioNodeInfo kInternalSpeaker[] = {
60 {false, kInternalSpeakerId, "Fake Speaker", "INTERNAL_SPEAKER", "Speaker"}};
[email protected]ee38bc42013-07-29 21:41:3261
tbarzic3ab01a22016-12-17 03:12:5362const AudioNodeInfo kHeadphone[] = {
63 {false, kHeadphoneId, "Fake Headphone", "HEADPHONE", "Headphone"}};
[email protected]ee38bc42013-07-29 21:41:3264
tbarzic3ab01a22016-12-17 03:12:5365const AudioNodeInfo kInternalMic[] = {
66 {true, kInternalMicId, "Fake Mic", "INTERNAL_MIC", "Internal Mic"}};
[email protected]ce631ee2014-01-23 03:37:0667
tbarzic3ab01a22016-12-17 03:12:5368const AudioNodeInfo kMicJack[] = {
69 {true, kMicJackId, "Fake Mic Jack", "MIC", "Mic Jack"}};
[email protected]ee38bc42013-07-29 21:41:3270
tbarzic3ab01a22016-12-17 03:12:5371const AudioNodeInfo kUSBMic[] = {
72 {true, kUSBMicId, "Fake USB Mic", "USB", "USB Microphone"}};
[email protected]f9a49d342014-07-29 23:47:2073
tbarzic3ab01a22016-12-17 03:12:5374const AudioNodeInfo kKeyboardMic[] = {{true, kKeyboardMicId,
75 "Fake Keyboard Mic", "KEYBOARD_MIC",
76 "Keyboard Mic"}};
[email protected]ee38bc42013-07-29 21:41:3277
jennyzfbaa3e62017-03-06 20:14:0278const AudioNodeInfo kFrontMic[] = {
79 {true, kFrontMicId, "Fake Front Mic", "FRONT_MIC", "Front Mic"}};
80
81const AudioNodeInfo kRearMic[] = {
82 {true, kRearMicId, "Fake Rear Mic", "REAR_MIC", "Rear Mic"}};
83
tbarzic3ab01a22016-12-17 03:12:5384const AudioNodeInfo kOtherTypeOutput[] = {{false, kOtherTypeOutputId,
85 "Output Device", "SOME_OTHER_TYPE",
86 "Other Type Output Device"}};
[email protected]ee38bc42013-07-29 21:41:3287
tbarzic3ab01a22016-12-17 03:12:5388const AudioNodeInfo kOtherTypeInput[] = {{true, kOtherTypeInputId,
89 "Input Device", "SOME_OTHER_TYPE",
90 "Other Type Input Device"}};
[email protected]ee38bc42013-07-29 21:41:3291
tbarzic3ab01a22016-12-17 03:12:5392const AudioNodeInfo kBluetoothHeadset[] = {{false, kBluetoothHeadsetId,
93 "Bluetooth Headset", "BLUETOOTH",
94 "Bluetooth Headset 1"}};
[email protected]ee38bc42013-07-29 21:41:3295
tbarzic3ab01a22016-12-17 03:12:5396const AudioNodeInfo kHDMIOutput[] = {
97 {false, kHDMIOutputId, "HDMI output", "HDMI", "HDMI output"}};
[email protected]ee38bc42013-07-29 21:41:3298
tbarzic3ab01a22016-12-17 03:12:5399const AudioNodeInfo kUSBHeadphone1[] = {
100 {false, kUSBHeadphoneId1, "USB Headphone", "USB", "USB Headphone 1"}};
[email protected]ee38bc42013-07-29 21:41:32101
tbarzic3ab01a22016-12-17 03:12:53102const AudioNodeInfo kUSBHeadphone2[] = {
103 {false, kUSBHeadphoneId2, "USB Headphone", "USB", "USB Headphone 1"}};
jennyz2cb9eb12014-11-05 19:21:05104
tbarzic3ab01a22016-12-17 03:12:53105const AudioNodeInfo kUSBJabraSpeakerOutput1[] = {
106 {false, kUSBJabraSpeakerOutputId1, "Jabra Speaker 1", "USB",
107 "Jabra Speaker 1"}};
jennyz2cb9eb12014-11-05 19:21:05108
tbarzic3ab01a22016-12-17 03:12:53109const AudioNodeInfo kUSBJabraSpeakerOutput2[] = {
110 {false, kUSBJabraSpeakerOutputId2, "Jabra Speaker 2", "USB",
111 "Jabra Speaker 2"}};
jennyz2cb9eb12014-11-05 19:21:05112
tbarzic3ab01a22016-12-17 03:12:53113const AudioNodeInfo kUSBJabraSpeakerInput1[] = {{true, kUSBJabraSpeakerInputId1,
114 "Jabra Speaker 1", "USB",
115 "Jabra Speaker"}};
jennyz2cb9eb12014-11-05 19:21:05116
tbarzic3ab01a22016-12-17 03:12:53117const AudioNodeInfo kUSBJabraSpeakerInput2[] = {{true, kUSBJabraSpeakerInputId2,
118 "Jabra Speaker 2", "USB",
119 "Jabra Speaker 2"}};
120
121const AudioNodeInfo kUSBCameraInput[] = {
122 {true, kUSBCameraInputId, "USB Camera", "USB", "USB Camera"}};
[email protected]ee38bc42013-07-29 21:41:32123
124class TestObserver : public chromeos::CrasAudioHandler::AudioObserver {
125 public:
tbarzic3ab01a22016-12-17 03:12:53126 TestObserver() {}
[email protected]ee38bc42013-07-29 21:41:32127
128 int active_output_node_changed_count() const {
129 return active_output_node_changed_count_;
130 }
131
jennyz2cb9eb12014-11-05 19:21:05132 void reset_active_output_node_changed_count() {
133 active_output_node_changed_count_ = 0;
134 }
135
[email protected]ee38bc42013-07-29 21:41:32136 int active_input_node_changed_count() const {
137 return active_input_node_changed_count_;
138 }
139
jennyz2cb9eb12014-11-05 19:21:05140 void reset_active_input_node_changed_count() {
mukai307596b62014-11-24 19:56:59141 active_input_node_changed_count_ = 0;
jennyz2cb9eb12014-11-05 19:21:05142 }
143
[email protected]ee38bc42013-07-29 21:41:32144 int audio_nodes_changed_count() const {
145 return audio_nodes_changed_count_;
146 }
147
148 int output_mute_changed_count() const {
149 return output_mute_changed_count_;
150 }
151
jennyzc79ccda2015-07-28 23:04:35152 void reset_output_mute_changed_count() { input_mute_changed_count_ = 0; }
153
[email protected]ee38bc42013-07-29 21:41:32154 int input_mute_changed_count() const {
155 return input_mute_changed_count_;
156 }
157
158 int output_volume_changed_count() const {
159 return output_volume_changed_count_;
160 }
161
jennyz96526d02016-06-22 17:03:43162 void reset_output_volume_changed_count() { output_volume_changed_count_ = 0; }
163
[email protected]ee38bc42013-07-29 21:41:32164 int input_gain_changed_count() const {
165 return input_gain_changed_count_;
166 }
167
jennyzc79ccda2015-07-28 23:04:35168 bool output_mute_by_system() const { return output_mute_by_system_; }
169
warx74524622016-03-31 01:07:21170 int output_channel_remixing_changed_count() const {
171 return output_channel_remixing_changed_count_;
172 }
173
dchengae98daa2015-01-21 20:30:49174 ~TestObserver() override {}
[email protected]ee38bc42013-07-29 21:41:32175
176 protected:
177 // chromeos::CrasAudioHandler::AudioObserver overrides.
dchengae98daa2015-01-21 20:30:49178 void OnActiveOutputNodeChanged() override {
[email protected]ee38bc42013-07-29 21:41:32179 ++active_output_node_changed_count_;
180 }
181
dchengae98daa2015-01-21 20:30:49182 void OnActiveInputNodeChanged() override {
[email protected]ee38bc42013-07-29 21:41:32183 ++active_input_node_changed_count_;
184 }
185
dchengae98daa2015-01-21 20:30:49186 void OnAudioNodesChanged() override { ++audio_nodes_changed_count_; }
[email protected]ee38bc42013-07-29 21:41:32187
jennyzc79ccda2015-07-28 23:04:35188 void OnOutputMuteChanged(bool /* mute_on */, bool system_adjust) override {
jennyz170e7fb2015-03-30 23:37:39189 ++output_mute_changed_count_;
jennyzc79ccda2015-07-28 23:04:35190 output_mute_by_system_ = system_adjust;
jennyz170e7fb2015-03-30 23:37:39191 }
[email protected]ee38bc42013-07-29 21:41:32192
jennyz170e7fb2015-03-30 23:37:39193 void OnInputMuteChanged(bool /* mute_on */) override {
194 ++input_mute_changed_count_;
195 }
[email protected]ee38bc42013-07-29 21:41:32196
avi6e1a22d2015-12-21 03:43:20197 void OnOutputNodeVolumeChanged(uint64_t /* node_id */,
jennyz481292c2015-03-21 01:49:29198 int /* volume */) override {
199 ++output_volume_changed_count_;
200 }
[email protected]ee38bc42013-07-29 21:41:32201
avi6e1a22d2015-12-21 03:43:20202 void OnInputNodeGainChanged(uint64_t /* node_id */, int /* gain */) override {
jennyz481292c2015-03-21 01:49:29203 ++input_gain_changed_count_;
204 }
[email protected]ee38bc42013-07-29 21:41:32205
warx74524622016-03-31 01:07:21206 void OnOuputChannelRemixingChanged(bool /* mono_on */) override {
207 ++output_channel_remixing_changed_count_;
208 }
209
[email protected]ee38bc42013-07-29 21:41:32210 private:
tbarzic3ab01a22016-12-17 03:12:53211 int active_output_node_changed_count_ = 0;
212 int active_input_node_changed_count_ = 0;
213 int audio_nodes_changed_count_ = 0;
214 int output_mute_changed_count_ = 0;
215 int input_mute_changed_count_ = 0;
216 int output_volume_changed_count_ = 0;
217 int input_gain_changed_count_ = 0;
218 bool output_mute_by_system_ = false; // output mute state adjusted by system.
219 int output_channel_remixing_changed_count_ = 0;
[email protected]ee38bc42013-07-29 21:41:32220
221 DISALLOW_COPY_AND_ASSIGN(TestObserver);
222};
223
Max Morin7a2d72a2017-07-20 19:06:25224class SystemMonitorObserver
225 : public base::SystemMonitor::DevicesChangedObserver {
226 public:
227 SystemMonitorObserver() : device_changes_received_(0) {}
228 ~SystemMonitorObserver() override {}
229
230 void OnDevicesChanged(base::SystemMonitor::DeviceType device_type) override {
231 if (device_type == base::SystemMonitor::DeviceType::DEVTYPE_AUDIO)
232 device_changes_received_++;
233 }
234
235 int device_changes_received() const { return device_changes_received_; }
236
237 void reset_count() { device_changes_received_ = 0; }
238
239 private:
240 int device_changes_received_;
241};
242
jennyzfbaa3e62017-03-06 20:14:02243class FakeVideoCaptureManager {
244 public:
245 FakeVideoCaptureManager() {}
246 virtual ~FakeVideoCaptureManager() {}
247
248 void AddObserver(media::VideoCaptureObserver* observer) {
249 observers_.AddObserver(observer);
250 }
251
252 void RemoveAllObservers() { observers_.Clear(); }
253
254 void NotifyVideoCaptureStarted(media::VideoFacingMode facing) {
255 for (auto& observer : observers_)
256 observer.OnVideoCaptureStarted(facing);
257 }
258
259 void NotifyVideoCaptureStopped(media::VideoFacingMode facing) {
260 for (auto& observer : observers_)
261 observer.OnVideoCaptureStopped(facing);
262 }
263
264 private:
265 base::ObserverList<media::VideoCaptureObserver> observers_;
266 DISALLOW_COPY_AND_ASSIGN(FakeVideoCaptureManager);
267};
268
oshima2aaee552015-07-09 16:50:26269} // namespace
270
tbarzic3ab01a22016-12-17 03:12:53271// Test param is the version of stabel device id used by audio node.
272class CrasAudioHandlerTest : public testing::TestWithParam<int> {
[email protected]ee38bc42013-07-29 21:41:32273 public:
fdoray822e6b22017-04-27 20:47:03274 CrasAudioHandlerTest()
275 : scoped_task_environment_(
276 base::test::ScopedTaskEnvironment::MainThreadType::UI) {}
dchengae98daa2015-01-21 20:30:49277 ~CrasAudioHandlerTest() override {}
[email protected]ee38bc42013-07-29 21:41:32278
jennyzfbaa3e62017-03-06 20:14:02279 void SetUp() override {
Max Morin7a2d72a2017-07-20 19:06:25280 system_monitor_.AddDevicesChangedObserver(&system_monitor_observer_);
jennyzfbaa3e62017-03-06 20:14:02281 video_capture_manager_.reset(new FakeVideoCaptureManager);
282 }
[email protected]ee38bc42013-07-29 21:41:32283
dchengae98daa2015-01-21 20:30:49284 void TearDown() override {
Max Morin7a2d72a2017-07-20 19:06:25285 system_monitor_.RemoveDevicesChangedObserver(&system_monitor_observer_);
[email protected]ee38bc42013-07-29 21:41:32286 cras_audio_handler_->RemoveAudioObserver(test_observer_.get());
287 test_observer_.reset();
jennyzfbaa3e62017-03-06 20:14:02288 video_capture_manager_->RemoveAllObservers();
289 video_capture_manager_.reset();
[email protected]ee38bc42013-07-29 21:41:32290 CrasAudioHandler::Shutdown();
tbarzic3ab01a22016-12-17 03:12:53291 audio_pref_handler_ = nullptr;
[email protected]ee38bc42013-07-29 21:41:32292 DBusThreadManager::Shutdown();
293 }
294
tbarzic3ab01a22016-12-17 03:12:53295 AudioNode GenerateAudioNode(const AudioNodeInfo* node_info) {
296 uint64_t stable_device_id_v2 = GetParam() == 1 ? 0 : (node_info->id ^ 0xFF);
297 uint64_t stable_device_id_v1 = node_info->id;
298 return AudioNode(node_info->is_input, node_info->id, GetParam() == 2,
299 stable_device_id_v1, stable_device_id_v2,
300 node_info->device_name, node_info->type, node_info->name,
301 false /* is_active*/, 0 /* pluged_time */);
302 }
303
304 AudioNodeList GenerateAudioNodeList(
305 const std::vector<const AudioNodeInfo*> nodes) {
306 AudioNodeList node_list;
vmpstr843b41a2017-03-01 21:15:03307 for (auto* node_info : nodes) {
tbarzic3ab01a22016-12-17 03:12:53308 node_list.push_back(GenerateAudioNode(node_info));
309 }
310 return node_list;
311 }
312
[email protected]ee38bc42013-07-29 21:41:32313 void SetUpCrasAudioHandler(const AudioNodeList& audio_nodes) {
zelidrag29fe3382014-08-27 01:44:48314 DBusThreadManager::Initialize();
satoruxcca5cc12014-10-27 16:33:59315 fake_cras_audio_client_ = static_cast<FakeCrasAudioClient*>(
[email protected]ee38bc42013-07-29 21:41:32316 DBusThreadManager::Get()->GetCrasAudioClient());
satoruxcca5cc12014-10-27 16:33:59317 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes);
[email protected]ee38bc42013-07-29 21:41:32318 audio_pref_handler_ = new AudioDevicesPrefHandlerStub();
319 CrasAudioHandler::Initialize(audio_pref_handler_);
320 cras_audio_handler_ = CrasAudioHandler::Get();
321 test_observer_.reset(new TestObserver);
322 cras_audio_handler_->AddAudioObserver(test_observer_.get());
jennyzfbaa3e62017-03-06 20:14:02323 video_capture_manager_->AddObserver(cras_audio_handler_);
fdorayf5b47fd12016-09-13 14:12:36324 base::RunLoop().RunUntilIdle();
[email protected]ee38bc42013-07-29 21:41:32325 }
326
jennyzbd236742016-03-02 20:15:52327 // Set up cras audio handlers with |audio_nodes| and set the active state of
328 // |active_device_in_pref| as active and |activate_by_user| in the pref,
329 // and rest of nodes in |audio_nodes_in_pref| as inactive.
330 void SetupCrasAudioHandlerWithActiveNodeInPref(
331 const AudioNodeList& audio_nodes,
332 const AudioNodeList& audio_nodes_in_pref,
333 const AudioDevice& active_device_in_pref,
334 bool activate_by_user) {
335 DBusThreadManager::Initialize();
336 fake_cras_audio_client_ = static_cast<FakeCrasAudioClient*>(
337 DBusThreadManager::Get()->GetCrasAudioClient());
338 audio_pref_handler_ = new AudioDevicesPrefHandlerStub();
339 bool active;
340 for (const AudioNode& node : audio_nodes_in_pref) {
341 active = node.id == active_device_in_pref.id;
342 audio_pref_handler_->SetDeviceActive(AudioDevice(node), active,
343 activate_by_user);
344 }
345
346 bool activate_by;
347 EXPECT_TRUE(audio_pref_handler_->GetDeviceActive(active_device_in_pref,
348 &active, &activate_by));
349 EXPECT_TRUE(active);
350 EXPECT_EQ(activate_by, activate_by_user);
351
352 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes);
353 CrasAudioHandler::Initialize(audio_pref_handler_);
354
355 cras_audio_handler_ = CrasAudioHandler::Get();
356 test_observer_.reset(new TestObserver);
357 cras_audio_handler_->AddAudioObserver(test_observer_.get());
fdorayf5b47fd12016-09-13 14:12:36358 base::RunLoop().RunUntilIdle();
jennyzbd236742016-03-02 20:15:52359 }
360
jennyzb47947f2014-09-25 00:42:04361 void SetUpCrasAudioHandlerWithPrimaryActiveNode(
362 const AudioNodeList& audio_nodes,
363 const AudioNode& primary_active_node) {
364 DBusThreadManager::Initialize();
satoruxcca5cc12014-10-27 16:33:59365 fake_cras_audio_client_ = static_cast<FakeCrasAudioClient*>(
jennyzb47947f2014-09-25 00:42:04366 DBusThreadManager::Get()->GetCrasAudioClient());
satoruxcca5cc12014-10-27 16:33:59367 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes);
Max Morin7a2d72a2017-07-20 19:06:25368 fake_cras_audio_client_->SetActiveOutputNode(primary_active_node.id);
369 audio_pref_handler_ = new AudioDevicesPrefHandlerStub();
jennyzb47947f2014-09-25 00:42:04370 CrasAudioHandler::Initialize(audio_pref_handler_);
371 cras_audio_handler_ = CrasAudioHandler::Get();
372 test_observer_.reset(new TestObserver);
373 cras_audio_handler_->AddAudioObserver(test_observer_.get());
fdorayf5b47fd12016-09-13 14:12:36374 base::RunLoop().RunUntilIdle();
jennyzb47947f2014-09-25 00:42:04375 }
376
[email protected]ee38bc42013-07-29 21:41:32377 void ChangeAudioNodes(const AudioNodeList& audio_nodes) {
satoruxcca5cc12014-10-27 16:33:59378 fake_cras_audio_client_->SetAudioNodesAndNotifyObserversForTesting(
derat3698adff2014-08-29 16:10:43379 audio_nodes);
fdorayf5b47fd12016-09-13 14:12:36380 base::RunLoop().RunUntilIdle();
[email protected]ee38bc42013-07-29 21:41:32381 }
382
Max Morin7a2d72a2017-07-20 19:06:25383 void VerifySystemMonitorWasCalled() {
384 // System monitor uses PostTask internally, so we have to process messages
385 // before verifying the expectation.
386 base::RunLoop().RunUntilIdle();
387 EXPECT_EQ(1, system_monitor_observer_.device_changes_received());
388 }
389
avi6e1a22d2015-12-21 03:43:20390 const AudioDevice* GetDeviceFromId(uint64_t id) {
jennyzb47947f2014-09-25 00:42:04391 return cras_audio_handler_->GetDeviceFromId(id);
392 }
393
jennyz2cb9eb12014-11-05 19:21:05394 int GetActiveDeviceCount() const {
395 int num_active_nodes = 0;
396 AudioDeviceList audio_devices;
397 cras_audio_handler_->GetAudioDevices(&audio_devices);
398 for (size_t i = 0; i < audio_devices.size(); ++i) {
399 if (audio_devices[i].active)
400 ++num_active_nodes;
401 }
402 return num_active_nodes;
403 }
404
jennyzc770dc772015-06-29 23:46:06405 void SetActiveHDMIRediscover() {
406 cras_audio_handler_->SetActiveHDMIOutoutRediscoveringIfNecessary(true);
407 }
408
409 void SetHDMIRediscoverGracePeriodDuration(int duration_in_ms) {
410 cras_audio_handler_->SetHDMIRediscoverGracePeriodForTesting(duration_in_ms);
411 }
412
413 bool IsDuringHDMIRediscoverGracePeriod() {
414 return cras_audio_handler_->hdmi_rediscovering();
415 }
416
warxfd7c37b2016-08-05 19:38:07417 void RestartAudioClient() {
418 cras_audio_handler_->AudioClientRestarted();
fdorayf5b47fd12016-09-13 14:12:36419 base::RunLoop().RunUntilIdle();
warxfd7c37b2016-08-05 19:38:07420 }
421
jennyzfbaa3e62017-03-06 20:14:02422 void StartFrontFacingCamera() {
423 video_capture_manager_->NotifyVideoCaptureStarted(
424 media::MEDIA_VIDEO_FACING_USER);
425 }
426
427 void StopFrontFacingCamera() {
428 video_capture_manager_->NotifyVideoCaptureStopped(
429 media::MEDIA_VIDEO_FACING_USER);
430 }
431
432 void StartRearFacingCamera() {
433 video_capture_manager_->NotifyVideoCaptureStarted(
434 media::MEDIA_VIDEO_FACING_ENVIRONMENT);
435 }
436
437 void StopRearFacingCamera() {
438 video_capture_manager_->NotifyVideoCaptureStopped(
439 media::MEDIA_VIDEO_FACING_ENVIRONMENT);
440 }
441
[email protected]ee38bc42013-07-29 21:41:32442 protected:
fdoray822e6b22017-04-27 20:47:03443 base::test::ScopedTaskEnvironment scoped_task_environment_;
Max Morin7a2d72a2017-07-20 19:06:25444 base::SystemMonitor system_monitor_;
445 SystemMonitorObserver system_monitor_observer_;
tbarzic3ab01a22016-12-17 03:12:53446 CrasAudioHandler* cras_audio_handler_ = nullptr; // Not owned.
447 FakeCrasAudioClient* fake_cras_audio_client_ = nullptr; // Not owned.
dcheng0a6e80c2016-04-08 18:37:38448 std::unique_ptr<TestObserver> test_observer_;
[email protected]ee38bc42013-07-29 21:41:32449 scoped_refptr<AudioDevicesPrefHandlerStub> audio_pref_handler_;
jennyzfbaa3e62017-03-06 20:14:02450 std::unique_ptr<FakeVideoCaptureManager> video_capture_manager_;
[email protected]ee38bc42013-07-29 21:41:32451
452 private:
453 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandlerTest);
454};
455
jennyzc770dc772015-06-29 23:46:06456class HDMIRediscoverWaiter {
457 public:
458 HDMIRediscoverWaiter(CrasAudioHandlerTest* cras_audio_handler_test,
459 int grace_period_duration_in_ms)
460 : cras_audio_handler_test_(cras_audio_handler_test),
461 grace_period_duration_in_ms_(grace_period_duration_in_ms) {}
462
463 void WaitUntilTimeOut(int wait_duration_in_ms) {
464 base::RunLoop run_loop;
465 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
466 FROM_HERE, run_loop.QuitClosure(),
467 base::TimeDelta::FromMilliseconds(wait_duration_in_ms));
468 run_loop.Run();
469 }
470
471 void CheckHDMIRediscoverGracePeriodEnd(const base::Closure& quit_loop_func) {
472 if (!cras_audio_handler_test_->IsDuringHDMIRediscoverGracePeriod()) {
473 quit_loop_func.Run();
474 return;
475 }
476 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
477 FROM_HERE,
478 base::Bind(&HDMIRediscoverWaiter::CheckHDMIRediscoverGracePeriodEnd,
479 base::Unretained(this), quit_loop_func),
480 base::TimeDelta::FromMilliseconds(grace_period_duration_in_ms_ / 4));
481 }
482
483 void WaitUntilHDMIRediscoverGracePeriodEnd() {
484 base::RunLoop run_loop;
485 CheckHDMIRediscoverGracePeriodEnd(run_loop.QuitClosure());
486 run_loop.Run();
487 }
488
489 private:
490 CrasAudioHandlerTest* cras_audio_handler_test_; // not owned
491 int grace_period_duration_in_ms_;
492
493 DISALLOW_COPY_AND_ASSIGN(HDMIRediscoverWaiter);
494};
495
tbarzic3ab01a22016-12-17 03:12:53496INSTANTIATE_TEST_CASE_P(StableIdV1, CrasAudioHandlerTest, testing::Values(1));
Max Morin7a2d72a2017-07-20 19:06:25497INSTANTIATE_TEST_CASE_P(StableIdV2, CrasAudioHandlerTest, testing::Values(2));
tbarzic3ab01a22016-12-17 03:12:53498
499TEST_P(CrasAudioHandlerTest, InitializeWithOnlyDefaultAudioDevices) {
500 AudioNodeList audio_nodes =
501 GenerateAudioNodeList({kInternalSpeaker, kInternalMic});
[email protected]ee38bc42013-07-29 21:41:32502 SetUpCrasAudioHandler(audio_nodes);
503
504 // Verify the audio devices size.
505 AudioDeviceList audio_devices;
506 cras_audio_handler_->GetAudioDevices(&audio_devices);
507 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
508
509 // Verify the internal speaker has been selected as the active output.
510 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04511 EXPECT_TRUE(
512 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53513 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
514 EXPECT_EQ(kInternalSpeaker->id,
jennyzb47947f2014-09-25 00:42:04515 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32516 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
517
518 // Ensure the internal microphone has been selected as the active input.
519 AudioDevice active_input;
tbarzic3ab01a22016-12-17 03:12:53520 EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:32521 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
522}
523
tbarzic3ab01a22016-12-17 03:12:53524TEST_P(CrasAudioHandlerTest, InitializeWithAlternativeAudioDevices) {
525 AudioNodeList audio_nodes = GenerateAudioNodeList(
526 {kInternalSpeaker, kHeadphone, kInternalMic, kUSBMic});
[email protected]ee38bc42013-07-29 21:41:32527 SetUpCrasAudioHandler(audio_nodes);
528
529 // Verify the audio devices size.
530 AudioDeviceList audio_devices;
531 cras_audio_handler_->GetAudioDevices(&audio_devices);
532 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
533
534 // Verify the headphone has been selected as the active output.
535 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04536 EXPECT_TRUE(
537 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53538 EXPECT_EQ(kHeadphone->id, active_output.id);
539 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32540 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
541
542 // Ensure the USB microphone has been selected as the active input.
jennyzb47947f2014-09-25 00:42:04543 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:32544 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
545}
546
tbarzic3ab01a22016-12-17 03:12:53547TEST_P(CrasAudioHandlerTest, InitializeWithKeyboardMic) {
548 AudioNodeList audio_nodes =
549 GenerateAudioNodeList({kInternalSpeaker, kInternalMic, kKeyboardMic});
[email protected]f9a49d342014-07-29 23:47:20550 SetUpCrasAudioHandler(audio_nodes);
551
552 // Verify the audio devices size.
553 AudioDeviceList audio_devices;
554 cras_audio_handler_->GetAudioDevices(&audio_devices);
555 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
jennyzb47947f2014-09-25 00:42:04556 EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic());
[email protected]f9a49d342014-07-29 23:47:20557
558 // Verify the internal speaker has been selected as the active output.
559 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04560 EXPECT_TRUE(
561 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53562 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
563 EXPECT_EQ(kInternalSpeaker->id,
jennyzb47947f2014-09-25 00:42:04564 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]f9a49d342014-07-29 23:47:20565 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
566
567 // Ensure the internal microphone has been selected as the active input,
568 // not affected by keyboard mic.
569 AudioDevice active_input;
tbarzic3ab01a22016-12-17 03:12:53570 EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]f9a49d342014-07-29 23:47:20571 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
tbarzic3ab01a22016-12-17 03:12:53572 const AudioDevice* keyboard_mic = GetDeviceFromId(kKeyboardMic->id);
jennyzb47947f2014-09-25 00:42:04573 EXPECT_FALSE(keyboard_mic->active);
574}
575
tbarzic3ab01a22016-12-17 03:12:53576TEST_P(CrasAudioHandlerTest, SetKeyboardMicActive) {
577 AudioNodeList audio_nodes =
578 GenerateAudioNodeList({kInternalMic, kKeyboardMic});
jennyzb47947f2014-09-25 00:42:04579 SetUpCrasAudioHandler(audio_nodes);
580
581 // Verify the audio devices size.
582 AudioDeviceList audio_devices;
583 cras_audio_handler_->GetAudioDevices(&audio_devices);
584 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
585 EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic());
586
587 // Ensure the internal microphone has been selected as the active input,
588 // not affected by keyboard mic.
589 AudioDevice active_input;
tbarzic3ab01a22016-12-17 03:12:53590 EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
jennyzb47947f2014-09-25 00:42:04591 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
tbarzic3ab01a22016-12-17 03:12:53592 const AudioDevice* keyboard_mic = GetDeviceFromId(kKeyboardMic->id);
jennyzb47947f2014-09-25 00:42:04593 EXPECT_FALSE(keyboard_mic->active);
594
595 // Make keyboard mic active.
596 cras_audio_handler_->SetKeyboardMicActive(true);
tbarzic3ab01a22016-12-17 03:12:53597 EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
598 const AudioDevice* active_keyboard_mic = GetDeviceFromId(kKeyboardMic->id);
jennyzb47947f2014-09-25 00:42:04599 EXPECT_TRUE(active_keyboard_mic->active);
600
601 // Make keyboard mic inactive.
602 cras_audio_handler_->SetKeyboardMicActive(false);
tbarzic3ab01a22016-12-17 03:12:53603 EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
604 const AudioDevice* inactive_keyboard_mic = GetDeviceFromId(kKeyboardMic->id);
jennyzb47947f2014-09-25 00:42:04605 EXPECT_FALSE(inactive_keyboard_mic->active);
[email protected]f9a49d342014-07-29 23:47:20606}
607
tbarzic3ab01a22016-12-17 03:12:53608TEST_P(CrasAudioHandlerTest, KeyboardMicNotSetAsPrimaryActive) {
609 AudioNodeList audio_nodes = GenerateAudioNodeList({kKeyboardMic});
jennyzb46664f2016-03-08 18:18:10610 SetUpCrasAudioHandler(audio_nodes);
611
612 // Verify keyboard mic is not set as primary active input.
613 AudioDeviceList audio_devices;
614 cras_audio_handler_->GetAudioDevices(&audio_devices);
615 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
616 EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic());
617 EXPECT_EQ(0u, cras_audio_handler_->GetPrimaryActiveInputNode());
618
619 // Verify the internal mic is set as primary input.
tbarzic3ab01a22016-12-17 03:12:53620 audio_nodes.push_back(GenerateAudioNode(kInternalMic));
jennyzb46664f2016-03-08 18:18:10621 ChangeAudioNodes(audio_nodes);
622 cras_audio_handler_->GetAudioDevices(&audio_devices);
623 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
624 EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic());
tbarzic3ab01a22016-12-17 03:12:53625 EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
jennyzb46664f2016-03-08 18:18:10626}
627
tbarzic3ab01a22016-12-17 03:12:53628TEST_P(CrasAudioHandlerTest, SwitchActiveOutputDevice) {
629 AudioNodeList audio_nodes =
630 GenerateAudioNodeList({kInternalSpeaker, kHeadphone});
[email protected]ee38bc42013-07-29 21:41:32631 SetUpCrasAudioHandler(audio_nodes);
tbarzic3ab01a22016-12-17 03:12:53632
[email protected]ee38bc42013-07-29 21:41:32633 AudioDeviceList audio_devices;
634 cras_audio_handler_->GetAudioDevices(&audio_devices);
635 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
Max Morin7a2d72a2017-07-20 19:06:25636 system_monitor_observer_.reset_count();
[email protected]ee38bc42013-07-29 21:41:32637
638 // Verify the initial active output device is headphone.
639 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
640 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04641 EXPECT_TRUE(
642 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53643 EXPECT_EQ(kHeadphone->id, active_output.id);
644 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32645
646 // Switch the active output to internal speaker.
tbarzic3ab01a22016-12-17 03:12:53647 AudioDevice internal_speaker(GenerateAudioNode(kInternalSpeaker));
jennyzbd236742016-03-02 20:15:52648 cras_audio_handler_->SwitchToDevice(internal_speaker, true,
649 CrasAudioHandler::ACTIVATE_BY_USER);
[email protected]ee38bc42013-07-29 21:41:32650
651 // Verify the active output is switched to internal speaker, and the
652 // ActiveOutputNodeChanged event is fired.
653 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
Max Morin7a2d72a2017-07-20 19:06:25654 VerifySystemMonitorWasCalled();
jennyzb47947f2014-09-25 00:42:04655 EXPECT_TRUE(
656 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53657 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
658 EXPECT_EQ(kInternalSpeaker->id,
jennyzb47947f2014-09-25 00:42:04659 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32660}
661
tbarzic3ab01a22016-12-17 03:12:53662TEST_P(CrasAudioHandlerTest, SwitchActiveInputDevice) {
663 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalMic, kUSBMic});
[email protected]ee38bc42013-07-29 21:41:32664 SetUpCrasAudioHandler(audio_nodes);
tbarzic3ab01a22016-12-17 03:12:53665
[email protected]ee38bc42013-07-29 21:41:32666 AudioDeviceList audio_devices;
667 cras_audio_handler_->GetAudioDevices(&audio_devices);
668 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
669
670 // Verify the initial active input device is USB mic.
671 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:04672 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:32673
674 // Switch the active input to internal mic.
tbarzic3ab01a22016-12-17 03:12:53675 AudioDevice internal_mic(GenerateAudioNode(kInternalMic));
jennyzbd236742016-03-02 20:15:52676 cras_audio_handler_->SwitchToDevice(internal_mic, true,
677 CrasAudioHandler::ACTIVATE_BY_USER);
[email protected]ee38bc42013-07-29 21:41:32678
679 // Verify the active output is switched to internal speaker, and the active
680 // ActiveInputNodeChanged event is fired.
681 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
tbarzic3ab01a22016-12-17 03:12:53682 EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:32683}
684
tbarzic3ab01a22016-12-17 03:12:53685TEST_P(CrasAudioHandlerTest, PlugHeadphone) {
[email protected]ee38bc42013-07-29 21:41:32686 // Set up initial audio devices, only with internal speaker.
tbarzic3ab01a22016-12-17 03:12:53687 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker});
[email protected]ee38bc42013-07-29 21:41:32688 SetUpCrasAudioHandler(audio_nodes);
689 const size_t init_nodes_size = audio_nodes.size();
690
691 // Verify the audio devices size.
692 AudioDeviceList audio_devices;
693 cras_audio_handler_->GetAudioDevices(&audio_devices);
694 EXPECT_EQ(init_nodes_size, audio_devices.size());
695 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
696
697 // Verify the internal speaker has been selected as the active output.
698 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
699 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04700 EXPECT_TRUE(
701 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53702 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
703 EXPECT_EQ(kInternalSpeaker->id,
jennyzb47947f2014-09-25 00:42:04704 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32705 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
Max Morin7a2d72a2017-07-20 19:06:25706 system_monitor_observer_.reset_count();
[email protected]ee38bc42013-07-29 21:41:32707
708 // Plug the headphone.
[email protected]4642d7792013-09-18 20:27:17709 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:53710 AudioNode internal_speaker = GenerateAudioNode(kInternalSpeaker);
[email protected]4642d7792013-09-18 20:27:17711 internal_speaker.active = true;
712 audio_nodes.push_back(internal_speaker);
tbarzic3ab01a22016-12-17 03:12:53713 audio_nodes.push_back(GenerateAudioNode(kHeadphone));
[email protected]ee38bc42013-07-29 21:41:32714 ChangeAudioNodes(audio_nodes);
715
716 // Verify the AudioNodesChanged event is fired and new audio device is added.
717 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
Max Morin7a2d72a2017-07-20 19:06:25718 VerifySystemMonitorWasCalled();
[email protected]ee38bc42013-07-29 21:41:32719 cras_audio_handler_->GetAudioDevices(&audio_devices);
720 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
721
722 // Verify the active output device is switched to headphone and
723 // ActiveOutputChanged event is fired.
724 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04725 EXPECT_TRUE(
726 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53727 EXPECT_EQ(kHeadphone->id, active_output.id);
728 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32729 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
730}
731
tbarzic3ab01a22016-12-17 03:12:53732TEST_P(CrasAudioHandlerTest, UnplugHeadphone) {
[email protected]ee38bc42013-07-29 21:41:32733 // Set up initial audio devices, with internal speaker and headphone.
tbarzic3ab01a22016-12-17 03:12:53734 AudioNodeList audio_nodes =
735 GenerateAudioNodeList({kInternalSpeaker, kHeadphone});
[email protected]ee38bc42013-07-29 21:41:32736 SetUpCrasAudioHandler(audio_nodes);
737 const size_t init_nodes_size = audio_nodes.size();
738
739 // Verify the audio devices size.
740 AudioDeviceList audio_devices;
741 cras_audio_handler_->GetAudioDevices(&audio_devices);
742 EXPECT_EQ(init_nodes_size, audio_devices.size());
743 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
744
745 // Verify the headphone has been selected as the active output.
746 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
747 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04748 EXPECT_TRUE(
749 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53750 EXPECT_EQ(kHeadphone->id, active_output.id);
751 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32752 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
Max Morin7a2d72a2017-07-20 19:06:25753 system_monitor_observer_.reset_count();
[email protected]ee38bc42013-07-29 21:41:32754
755 // Unplug the headphone.
756 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:53757 audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
[email protected]ee38bc42013-07-29 21:41:32758 ChangeAudioNodes(audio_nodes);
759
760 // Verify the AudioNodesChanged event is fired and one audio device is
761 // removed.
762 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
Max Morin7a2d72a2017-07-20 19:06:25763 VerifySystemMonitorWasCalled();
[email protected]ee38bc42013-07-29 21:41:32764 cras_audio_handler_->GetAudioDevices(&audio_devices);
765 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
766
767 // Verify the active output device is switched to internal speaker and
768 // ActiveOutputChanged event is fired.
769 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04770 EXPECT_TRUE(
771 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53772 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
773 EXPECT_EQ(kInternalSpeaker->id,
jennyzb47947f2014-09-25 00:42:04774 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32775 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
776}
777
tbarzic3ab01a22016-12-17 03:12:53778TEST_P(CrasAudioHandlerTest, InitializeWithBluetoothHeadset) {
779 AudioNodeList audio_nodes =
780 GenerateAudioNodeList({kInternalSpeaker, kBluetoothHeadset});
[email protected]ee38bc42013-07-29 21:41:32781 SetUpCrasAudioHandler(audio_nodes);
782
783 // Verify the audio devices size.
784 AudioDeviceList audio_devices;
785 cras_audio_handler_->GetAudioDevices(&audio_devices);
786 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
787 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
788
789 // Verify the bluetooth headset has been selected as the active output.
790 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
791 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04792 EXPECT_TRUE(
793 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53794 EXPECT_EQ(kBluetoothHeadset->id, active_output.id);
795 EXPECT_EQ(kBluetoothHeadset->id,
jennyzb47947f2014-09-25 00:42:04796 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32797 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
798}
799
tbarzic3ab01a22016-12-17 03:12:53800TEST_P(CrasAudioHandlerTest, ConnectAndDisconnectBluetoothHeadset) {
[email protected]ee38bc42013-07-29 21:41:32801 // Initialize with internal speaker and headphone.
tbarzic3ab01a22016-12-17 03:12:53802 AudioNodeList audio_nodes =
803 GenerateAudioNodeList({kInternalSpeaker, kHeadphone});
[email protected]ee38bc42013-07-29 21:41:32804 SetUpCrasAudioHandler(audio_nodes);
805 const size_t init_nodes_size = audio_nodes.size();
806
807 // Verify the audio devices size.
808 AudioDeviceList audio_devices;
809 cras_audio_handler_->GetAudioDevices(&audio_devices);
810 EXPECT_EQ(init_nodes_size, audio_devices.size());
811 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
812
813 // Verify the headphone is selected as the active output initially.
814 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
815 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04816 EXPECT_TRUE(
817 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53818 EXPECT_EQ(kHeadphone->id, active_output.id);
819 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32820 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
821
822 // Connect to bluetooth headset. Since it is plugged in later than
823 // headphone, active output should be switched to it.
Max Morin7a2d72a2017-07-20 19:06:25824 system_monitor_observer_.reset_count();
[email protected]ee38bc42013-07-29 21:41:32825 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:53826 audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
827 AudioNode headphone = GenerateAudioNode(kHeadphone);
[email protected]ee38bc42013-07-29 21:41:32828 headphone.plugged_time = 80000000;
829 headphone.active = true;
830 audio_nodes.push_back(headphone);
tbarzic3ab01a22016-12-17 03:12:53831 AudioNode bluetooth_headset = GenerateAudioNode(kBluetoothHeadset);
[email protected]ee38bc42013-07-29 21:41:32832 bluetooth_headset.plugged_time = 90000000;
833 audio_nodes.push_back(bluetooth_headset);
834 ChangeAudioNodes(audio_nodes);
835
836 // Verify the AudioNodesChanged event is fired and new audio device is added.
837 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
Max Morin7a2d72a2017-07-20 19:06:25838 VerifySystemMonitorWasCalled();
[email protected]ee38bc42013-07-29 21:41:32839 cras_audio_handler_->GetAudioDevices(&audio_devices);
840 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
841
842 // Verify the active output device is switched to bluetooth headset, and
843 // ActiveOutputChanged event is fired.
844 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04845 EXPECT_TRUE(
846 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53847 EXPECT_EQ(kBluetoothHeadset->id, active_output.id);
848 EXPECT_EQ(kBluetoothHeadset->id,
jennyzb47947f2014-09-25 00:42:04849 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32850 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
851
852 // Disconnect bluetooth headset.
Max Morin7a2d72a2017-07-20 19:06:25853 system_monitor_observer_.reset_count();
[email protected]ee38bc42013-07-29 21:41:32854 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:53855 audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
[email protected]4642d7792013-09-18 20:27:17856 headphone.active = false;
[email protected]ee38bc42013-07-29 21:41:32857 audio_nodes.push_back(headphone);
858 ChangeAudioNodes(audio_nodes);
859
860 // Verify the AudioNodesChanged event is fired and one audio device is
861 // removed.
862 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
Max Morin7a2d72a2017-07-20 19:06:25863 VerifySystemMonitorWasCalled();
[email protected]ee38bc42013-07-29 21:41:32864 cras_audio_handler_->GetAudioDevices(&audio_devices);
865 EXPECT_EQ(init_nodes_size, audio_devices.size());
866
867 // Verify the active output device is switched to headphone, and
868 // ActiveOutputChanged event is fired.
869 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04870 EXPECT_TRUE(
871 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53872 EXPECT_EQ(kHeadphone->id, active_output.id);
873 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32874 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
875}
876
tbarzic3ab01a22016-12-17 03:12:53877TEST_P(CrasAudioHandlerTest, InitializeWithHDMIOutput) {
878 AudioNodeList audio_nodes =
879 GenerateAudioNodeList({kInternalSpeaker, kHDMIOutput});
[email protected]ee38bc42013-07-29 21:41:32880 SetUpCrasAudioHandler(audio_nodes);
881
882 // Verify the audio devices size.
883 AudioDeviceList audio_devices;
884 cras_audio_handler_->GetAudioDevices(&audio_devices);
885 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
886 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
887
888 // Verify the HDMI device has been selected as the active output.
889 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
890 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04891 EXPECT_TRUE(
892 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53893 EXPECT_EQ(kHDMIOutput->id, active_output.id);
894 EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32895 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
896}
897
tbarzic3ab01a22016-12-17 03:12:53898TEST_P(CrasAudioHandlerTest, ConnectAndDisconnectHDMIOutput) {
[email protected]ee38bc42013-07-29 21:41:32899 // Initialize with internal speaker.
tbarzic3ab01a22016-12-17 03:12:53900 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker});
[email protected]ee38bc42013-07-29 21:41:32901 SetUpCrasAudioHandler(audio_nodes);
902 const size_t init_nodes_size = audio_nodes.size();
903
904 // Verify the audio devices size.
905 AudioDeviceList audio_devices;
906 cras_audio_handler_->GetAudioDevices(&audio_devices);
907 EXPECT_EQ(init_nodes_size, audio_devices.size());
908 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
909
910 // Verify the internal speaker is selected as the active output initially.
911 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
912 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04913 EXPECT_TRUE(
914 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53915 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
916 EXPECT_EQ(kInternalSpeaker->id,
jennyzb47947f2014-09-25 00:42:04917 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32918 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
Max Morin7a2d72a2017-07-20 19:06:25919 system_monitor_observer_.reset_count();
[email protected]ee38bc42013-07-29 21:41:32920
921 // Connect to HDMI output.
922 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:53923 AudioNode internal_speaker = GenerateAudioNode(kInternalSpeaker);
[email protected]ee38bc42013-07-29 21:41:32924 internal_speaker.active = true;
925 internal_speaker.plugged_time = 80000000;
926 audio_nodes.push_back(internal_speaker);
tbarzic3ab01a22016-12-17 03:12:53927 AudioNode hdmi = GenerateAudioNode(kHDMIOutput);
[email protected]ee38bc42013-07-29 21:41:32928 hdmi.plugged_time = 90000000;
929 audio_nodes.push_back(hdmi);
930 ChangeAudioNodes(audio_nodes);
931
932 // Verify the AudioNodesChanged event is fired and new audio device is added.
933 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
Max Morin7a2d72a2017-07-20 19:06:25934 VerifySystemMonitorWasCalled();
[email protected]ee38bc42013-07-29 21:41:32935 cras_audio_handler_->GetAudioDevices(&audio_devices);
936 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
937
938 // Verify the active output device is switched to hdmi output, and
939 // ActiveOutputChanged event is fired.
940 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04941 EXPECT_TRUE(
942 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53943 EXPECT_EQ(kHDMIOutput->id, active_output.id);
944 EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32945 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
Max Morin7a2d72a2017-07-20 19:06:25946 system_monitor_observer_.reset_count();
[email protected]ee38bc42013-07-29 21:41:32947
948 // Disconnect hdmi headset.
949 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:53950 audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
[email protected]ee38bc42013-07-29 21:41:32951 ChangeAudioNodes(audio_nodes);
952
953 // Verify the AudioNodesChanged event is fired and one audio device is
954 // removed.
955 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
Max Morin7a2d72a2017-07-20 19:06:25956 VerifySystemMonitorWasCalled();
[email protected]ee38bc42013-07-29 21:41:32957 cras_audio_handler_->GetAudioDevices(&audio_devices);
958 EXPECT_EQ(init_nodes_size, audio_devices.size());
959
960 // Verify the active output device is switched to internal speaker, and
961 // ActiveOutputChanged event is fired.
962 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04963 EXPECT_TRUE(
964 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53965 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
966 EXPECT_EQ(kInternalSpeaker->id,
jennyzb47947f2014-09-25 00:42:04967 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32968 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
969}
970
tbarzic3ab01a22016-12-17 03:12:53971TEST_P(CrasAudioHandlerTest, HandleHeadphoneAndHDMIOutput) {
[email protected]ee38bc42013-07-29 21:41:32972 // Initialize with internal speaker, headphone and HDMI output.
tbarzic3ab01a22016-12-17 03:12:53973 AudioNodeList audio_nodes =
974 GenerateAudioNodeList({kInternalSpeaker, kHeadphone, kHDMIOutput});
[email protected]ee38bc42013-07-29 21:41:32975 SetUpCrasAudioHandler(audio_nodes);
976 const size_t init_nodes_size = audio_nodes.size();
977
978 // Verify the audio devices size.
979 AudioDeviceList audio_devices;
980 cras_audio_handler_->GetAudioDevices(&audio_devices);
981 EXPECT_EQ(init_nodes_size, audio_devices.size());
982 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
983
984 // Verify the headphone is selected as the active output initially.
985 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
986 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04987 EXPECT_TRUE(
988 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53989 EXPECT_EQ(kHeadphone->id, active_output.id);
990 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32991 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
992
993 // Disconnect HDMI output.
994 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:53995 audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
996 audio_nodes.push_back(GenerateAudioNode(kHDMIOutput));
[email protected]ee38bc42013-07-29 21:41:32997 ChangeAudioNodes(audio_nodes);
998
999 // Verify the AudioNodesChanged event is fired and one audio device is
1000 // removed.
1001 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1002 cras_audio_handler_->GetAudioDevices(&audio_devices);
1003 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
1004
1005 // Verify the active output device is switched to HDMI output, and
1006 // ActiveOutputChanged event is fired.
1007 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041008 EXPECT_TRUE(
1009 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531010 EXPECT_EQ(kHDMIOutput->id, active_output.id);
1011 EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:321012 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1013}
1014
tbarzic3ab01a22016-12-17 03:12:531015TEST_P(CrasAudioHandlerTest, InitializeWithUSBHeadphone) {
1016 AudioNodeList audio_nodes =
1017 GenerateAudioNodeList({kInternalSpeaker, kUSBHeadphone1});
[email protected]ee38bc42013-07-29 21:41:321018 SetUpCrasAudioHandler(audio_nodes);
1019
1020 // Verify the audio devices size.
1021 AudioDeviceList audio_devices;
1022 cras_audio_handler_->GetAudioDevices(&audio_devices);
1023 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
1024 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1025
1026 // Verify the usb headphone has been selected as the active output.
1027 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1028 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041029 EXPECT_TRUE(
1030 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531031 EXPECT_EQ(kUSBHeadphone1->id, active_output.id);
1032 EXPECT_EQ(kUSBHeadphone1->id,
jennyzb47947f2014-09-25 00:42:041033 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:321034 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1035}
1036
tbarzic3ab01a22016-12-17 03:12:531037TEST_P(CrasAudioHandlerTest, PlugAndUnplugUSBHeadphone) {
[email protected]ee38bc42013-07-29 21:41:321038 // Initialize with internal speaker.
tbarzic3ab01a22016-12-17 03:12:531039 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker});
[email protected]ee38bc42013-07-29 21:41:321040 SetUpCrasAudioHandler(audio_nodes);
1041 const size_t init_nodes_size = audio_nodes.size();
1042
1043 // Verify the audio devices size.
1044 AudioDeviceList audio_devices;
1045 cras_audio_handler_->GetAudioDevices(&audio_devices);
1046 EXPECT_EQ(init_nodes_size, audio_devices.size());
1047 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1048
1049 // Verify the internal speaker is selected as the active output initially.
1050 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1051 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041052 EXPECT_TRUE(
1053 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531054 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
1055 EXPECT_EQ(kInternalSpeaker->id,
jennyzb47947f2014-09-25 00:42:041056 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:321057 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
Max Morin7a2d72a2017-07-20 19:06:251058 system_monitor_observer_.reset_count();
[email protected]ee38bc42013-07-29 21:41:321059
1060 // Plug in usb headphone
1061 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531062 AudioNode internal_speaker = GenerateAudioNode(kInternalSpeaker);
[email protected]ee38bc42013-07-29 21:41:321063 internal_speaker.active = true;
1064 internal_speaker.plugged_time = 80000000;
1065 audio_nodes.push_back(internal_speaker);
tbarzic3ab01a22016-12-17 03:12:531066 AudioNode usb_headphone = GenerateAudioNode(kUSBHeadphone1);
[email protected]ee38bc42013-07-29 21:41:321067 usb_headphone.plugged_time = 90000000;
1068 audio_nodes.push_back(usb_headphone);
1069 ChangeAudioNodes(audio_nodes);
1070
1071 // Verify the AudioNodesChanged event is fired and new audio device is added.
1072 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
Max Morin7a2d72a2017-07-20 19:06:251073 VerifySystemMonitorWasCalled();
[email protected]ee38bc42013-07-29 21:41:321074 cras_audio_handler_->GetAudioDevices(&audio_devices);
1075 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1076
1077 // Verify the active output device is switched to usb headphone, and
1078 // ActiveOutputChanged event is fired.
1079 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041080 EXPECT_TRUE(
1081 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531082 EXPECT_EQ(kUSBHeadphone1->id, active_output.id);
1083 EXPECT_EQ(kUSBHeadphone1->id,
jennyzb47947f2014-09-25 00:42:041084 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:321085 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
Max Morin7a2d72a2017-07-20 19:06:251086 system_monitor_observer_.reset_count();
[email protected]ee38bc42013-07-29 21:41:321087
1088 // Unplug usb headphone.
1089 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531090 audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
[email protected]ee38bc42013-07-29 21:41:321091 ChangeAudioNodes(audio_nodes);
1092
1093 // Verify the AudioNodesChanged event is fired and one audio device is
1094 // removed.
1095 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
Max Morin7a2d72a2017-07-20 19:06:251096 VerifySystemMonitorWasCalled();
[email protected]ee38bc42013-07-29 21:41:321097 cras_audio_handler_->GetAudioDevices(&audio_devices);
1098 EXPECT_EQ(init_nodes_size, audio_devices.size());
1099
1100 // Verify the active output device is switched to internal speaker, and
1101 // ActiveOutputChanged event is fired.
1102 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041103 EXPECT_TRUE(
1104 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531105 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
1106 EXPECT_EQ(kInternalSpeaker->id,
jennyzb47947f2014-09-25 00:42:041107 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:321108 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
1109}
1110
tbarzic3ab01a22016-12-17 03:12:531111TEST_P(CrasAudioHandlerTest, HandleMultipleUSBHeadphones) {
[email protected]ee38bc42013-07-29 21:41:321112 // Initialize with internal speaker and one usb headphone.
tbarzic3ab01a22016-12-17 03:12:531113 AudioNodeList audio_nodes =
1114 GenerateAudioNodeList({kInternalSpeaker, kUSBHeadphone1});
[email protected]ee38bc42013-07-29 21:41:321115 SetUpCrasAudioHandler(audio_nodes);
1116 const size_t init_nodes_size = audio_nodes.size();
1117
1118 // Verify the audio devices size.
1119 AudioDeviceList audio_devices;
1120 cras_audio_handler_->GetAudioDevices(&audio_devices);
1121 EXPECT_EQ(init_nodes_size, audio_devices.size());
1122 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1123
1124 // Verify the usb headphone is selected as the active output initially.
1125 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1126 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041127 EXPECT_TRUE(
1128 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531129 EXPECT_EQ(kUSBHeadphone1->id, active_output.id);
1130 EXPECT_EQ(kUSBHeadphone1->id,
jennyzb47947f2014-09-25 00:42:041131 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:321132 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1133
1134 // Plug in another usb headphone.
1135 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531136 audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
1137 AudioNode usb_headphone_1 = GenerateAudioNode(kUSBHeadphone1);
[email protected]ee38bc42013-07-29 21:41:321138 usb_headphone_1.active = true;
1139 usb_headphone_1.plugged_time = 80000000;
1140 audio_nodes.push_back(usb_headphone_1);
tbarzic3ab01a22016-12-17 03:12:531141 AudioNode usb_headphone_2 = GenerateAudioNode(kUSBHeadphone2);
[email protected]ee38bc42013-07-29 21:41:321142 usb_headphone_2.plugged_time = 90000000;
1143 audio_nodes.push_back(usb_headphone_2);
1144 ChangeAudioNodes(audio_nodes);
1145
1146 // Verify the AudioNodesChanged event is fired and new audio device is added.
1147 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1148 cras_audio_handler_->GetAudioDevices(&audio_devices);
1149 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1150
1151 // Verify the active output device is switched to the 2nd usb headphone, which
1152 // is plugged later, and ActiveOutputChanged event is fired.
1153 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041154 EXPECT_TRUE(
1155 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531156 EXPECT_EQ(kUSBHeadphone2->id, active_output.id);
1157 EXPECT_EQ(kUSBHeadphone2->id,
jennyzb47947f2014-09-25 00:42:041158 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:321159 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1160
1161 // Unplug the 2nd usb headphone.
1162 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531163 audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
1164 audio_nodes.push_back(GenerateAudioNode(kUSBHeadphone1));
[email protected]ee38bc42013-07-29 21:41:321165 ChangeAudioNodes(audio_nodes);
1166
1167 // Verify the AudioNodesChanged event is fired and one audio device is
1168 // removed.
1169 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1170 cras_audio_handler_->GetAudioDevices(&audio_devices);
1171 EXPECT_EQ(init_nodes_size, audio_devices.size());
1172
1173 // Verify the active output device is switched to the first usb headphone, and
1174 // ActiveOutputChanged event is fired.
1175 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041176 EXPECT_TRUE(
1177 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531178 EXPECT_EQ(kUSBHeadphone1->id, active_output.id);
1179 EXPECT_EQ(kUSBHeadphone1->id,
jennyzb47947f2014-09-25 00:42:041180 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:321181 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1182}
1183
tbarzic3ab01a22016-12-17 03:12:531184TEST_P(CrasAudioHandlerTest, UnplugUSBHeadphonesWithActiveSpeaker) {
[email protected]ca473252013-08-15 11:48:391185 // Initialize with internal speaker and one usb headphone.
tbarzic3ab01a22016-12-17 03:12:531186 AudioNodeList audio_nodes =
1187 GenerateAudioNodeList({kInternalSpeaker, kUSBHeadphone1});
[email protected]ca473252013-08-15 11:48:391188 SetUpCrasAudioHandler(audio_nodes);
1189 const size_t init_nodes_size = audio_nodes.size();
1190
1191 // Verify the audio devices size.
1192 AudioDeviceList audio_devices;
1193 cras_audio_handler_->GetAudioDevices(&audio_devices);
1194 EXPECT_EQ(init_nodes_size, audio_devices.size());
1195 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1196
1197 // Verify the usb headphone is selected as the active output initially.
1198 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1199 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041200 EXPECT_TRUE(
1201 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531202 EXPECT_EQ(kUSBHeadphone1->id, active_output.id);
1203 EXPECT_EQ(kUSBHeadphone1->id,
jennyzb47947f2014-09-25 00:42:041204 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ca473252013-08-15 11:48:391205 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1206
1207 // Plug in the headphone jack.
1208 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531209 audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
1210 AudioNode usb_headphone_1 = GenerateAudioNode(kUSBHeadphone1);
[email protected]ca473252013-08-15 11:48:391211 usb_headphone_1.active = true;
1212 usb_headphone_1.plugged_time = 80000000;
1213 audio_nodes.push_back(usb_headphone_1);
tbarzic3ab01a22016-12-17 03:12:531214 AudioNode headphone_jack = GenerateAudioNode(kHeadphone);
[email protected]ca473252013-08-15 11:48:391215 headphone_jack.plugged_time = 90000000;
1216 audio_nodes.push_back(headphone_jack);
1217 ChangeAudioNodes(audio_nodes);
1218
1219 // Verify the AudioNodesChanged event is fired and new audio device is added.
1220 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1221 cras_audio_handler_->GetAudioDevices(&audio_devices);
1222 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1223
1224 // Verify the active output device is switched to the headphone jack, which
1225 // is plugged later, and ActiveOutputChanged event is fired.
1226 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041227 EXPECT_TRUE(
1228 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531229 EXPECT_EQ(kHeadphone->id, active_output.id);
1230 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ca473252013-08-15 11:48:391231 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1232
1233 // Select the speaker to be the active output device.
tbarzic3ab01a22016-12-17 03:12:531234 AudioDevice internal_speaker(GenerateAudioNode(kInternalSpeaker));
jennyzbd236742016-03-02 20:15:521235 cras_audio_handler_->SwitchToDevice(internal_speaker, true,
1236 CrasAudioHandler::ACTIVATE_BY_USER);
[email protected]ca473252013-08-15 11:48:391237
1238 // Verify the active output is switched to internal speaker, and the
1239 // ActiveOutputNodeChanged event is fired.
1240 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041241 EXPECT_TRUE(
1242 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531243 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
1244 EXPECT_EQ(kInternalSpeaker->id,
jennyzb47947f2014-09-25 00:42:041245 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ca473252013-08-15 11:48:391246
1247 // Unplug the usb headphone.
1248 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531249 AudioNode internal_speaker_node(GenerateAudioNode(kInternalSpeaker));
[email protected]ca473252013-08-15 11:48:391250 internal_speaker_node.active = true;
1251 internal_speaker_node.plugged_time = 70000000;
1252 audio_nodes.push_back(internal_speaker_node);
1253 headphone_jack.active = false;
1254 audio_nodes.push_back(headphone_jack);
1255 ChangeAudioNodes(audio_nodes);
1256
1257 // Verify the AudioNodesChanged event is fired and one audio device is
1258 // removed.
1259 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1260 cras_audio_handler_->GetAudioDevices(&audio_devices);
1261 EXPECT_EQ(init_nodes_size, audio_devices.size());
1262
1263 // Verify the active output device remains to be speaker.
1264 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041265 EXPECT_TRUE(
1266 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531267 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
1268 EXPECT_EQ(kInternalSpeaker->id,
jennyzb47947f2014-09-25 00:42:041269 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ca473252013-08-15 11:48:391270 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1271}
1272
tbarzic3ab01a22016-12-17 03:12:531273TEST_P(CrasAudioHandlerTest, OneActiveAudioOutputAfterLoginNewUserSession) {
[email protected]d413fcc2013-08-16 23:20:031274 // This tests the case found with crbug.com/273271.
1275 // Initialize with internal speaker, bluetooth headphone and headphone jack
1276 // for a new chrome session after user signs out from the previous session.
1277 // Headphone jack is plugged in later than bluetooth headphone, but bluetooth
1278 // headphone is selected as the active output by user from previous user
1279 // session.
tbarzic3ab01a22016-12-17 03:12:531280 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker});
1281 AudioNode bluetooth_headphone = GenerateAudioNode(kBluetoothHeadset);
[email protected]d413fcc2013-08-16 23:20:031282 bluetooth_headphone.active = true;
1283 bluetooth_headphone.plugged_time = 70000000;
1284 audio_nodes.push_back(bluetooth_headphone);
tbarzic3ab01a22016-12-17 03:12:531285 AudioNode headphone_jack = GenerateAudioNode(kHeadphone);
[email protected]d413fcc2013-08-16 23:20:031286 headphone_jack.plugged_time = 80000000;
1287 audio_nodes.push_back(headphone_jack);
jennyzb47947f2014-09-25 00:42:041288 SetUpCrasAudioHandlerWithPrimaryActiveNode(audio_nodes, bluetooth_headphone);
[email protected]d413fcc2013-08-16 23:20:031289 const size_t init_nodes_size = audio_nodes.size();
1290
1291 // Verify the audio devices size.
1292 AudioDeviceList audio_devices;
1293 cras_audio_handler_->GetAudioDevices(&audio_devices);
1294 EXPECT_EQ(init_nodes_size, audio_devices.size());
1295 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1296
1297 // Verify the headphone jack is selected as the active output and all other
1298 // audio devices are not active.
1299 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1300 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041301 EXPECT_TRUE(
1302 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531303 EXPECT_EQ(kHeadphone->id, active_output.id);
1304 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]d413fcc2013-08-16 23:20:031305 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1306 for (size_t i = 0; i < audio_devices.size(); ++i) {
tbarzic3ab01a22016-12-17 03:12:531307 if (audio_devices[i].id != kHeadphone->id)
[email protected]d413fcc2013-08-16 23:20:031308 EXPECT_FALSE(audio_devices[i].active);
1309 }
1310}
1311
tbarzic3ab01a22016-12-17 03:12:531312TEST_P(CrasAudioHandlerTest, BluetoothSpeakerIdChangedOnFly) {
[email protected]110babd2013-09-17 01:46:021313 // Initialize with internal speaker and bluetooth headset.
tbarzic3ab01a22016-12-17 03:12:531314 AudioNodeList audio_nodes =
1315 GenerateAudioNodeList({kInternalSpeaker, kBluetoothHeadset});
[email protected]110babd2013-09-17 01:46:021316 SetUpCrasAudioHandler(audio_nodes);
1317 const size_t init_nodes_size = audio_nodes.size();
1318
1319 // Verify the audio devices size.
1320 AudioDeviceList audio_devices;
1321 cras_audio_handler_->GetAudioDevices(&audio_devices);
1322 EXPECT_EQ(init_nodes_size, audio_devices.size());
1323 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1324
1325 // Verify the bluetooth headset is selected as the active output and all other
1326 // audio devices are not active.
1327 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1328 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041329 EXPECT_TRUE(
1330 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531331 EXPECT_EQ(kBluetoothHeadset->id, active_output.id);
1332 EXPECT_EQ(kBluetoothHeadset->id,
jennyzb47947f2014-09-25 00:42:041333 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]110babd2013-09-17 01:46:021334 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1335
1336 // Cras changes the bluetooth headset's id on the fly.
1337 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531338 AudioNode internal_speaker(GenerateAudioNode(kInternalSpeaker));
[email protected]110babd2013-09-17 01:46:021339 internal_speaker.active = false;
1340 audio_nodes.push_back(internal_speaker);
tbarzic3ab01a22016-12-17 03:12:531341 AudioNode bluetooth_headphone = GenerateAudioNode(kBluetoothHeadset);
[email protected]110babd2013-09-17 01:46:021342 // Change bluetooth headphone id.
1343 bluetooth_headphone.id = kBluetoothHeadsetId + 20000;
1344 bluetooth_headphone.active = false;
1345 audio_nodes.push_back(bluetooth_headphone);
1346 ChangeAudioNodes(audio_nodes);
1347
1348 // Verify NodesChanged event is fired, and the audio devices size is not
1349 // changed.
1350 audio_devices.clear();
1351 cras_audio_handler_->GetAudioDevices(&audio_devices);
1352 EXPECT_EQ(init_nodes_size, audio_devices.size());
1353 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1354
1355 // Verify ActiveOutputNodeChanged event is fired, and active device should be
1356 // bluetooth headphone.
1357 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041358 EXPECT_TRUE(
1359 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]110babd2013-09-17 01:46:021360 EXPECT_EQ(bluetooth_headphone.id, active_output.id);
1361}
1362
tbarzic3ab01a22016-12-17 03:12:531363TEST_P(CrasAudioHandlerTest, PlugUSBMic) {
[email protected]ee38bc42013-07-29 21:41:321364 // Set up initial audio devices, only with internal mic.
tbarzic3ab01a22016-12-17 03:12:531365 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalMic});
[email protected]ee38bc42013-07-29 21:41:321366 SetUpCrasAudioHandler(audio_nodes);
1367 const size_t init_nodes_size = audio_nodes.size();
1368
1369 // Verify the audio devices size.
1370 AudioDeviceList audio_devices;
1371 cras_audio_handler_->GetAudioDevices(&audio_devices);
1372 EXPECT_EQ(init_nodes_size, audio_devices.size());
1373 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1374
[email protected]babc24b2013-09-09 23:50:231375 // Verify the internal mic is selected as the active input.
[email protected]ee38bc42013-07-29 21:41:321376 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
tbarzic3ab01a22016-12-17 03:12:531377 EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:321378 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
Max Morin7a2d72a2017-07-20 19:06:251379 system_monitor_observer_.reset_count();
[email protected]ee38bc42013-07-29 21:41:321380
1381 // Plug the USB Mic.
[email protected]4642d7792013-09-18 20:27:171382 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531383 AudioNode internal_mic(GenerateAudioNode(kInternalMic));
[email protected]4642d7792013-09-18 20:27:171384 internal_mic.active = true;
1385 audio_nodes.push_back(internal_mic);
tbarzic3ab01a22016-12-17 03:12:531386 audio_nodes.push_back(GenerateAudioNode(kUSBMic));
[email protected]ee38bc42013-07-29 21:41:321387 ChangeAudioNodes(audio_nodes);
1388
1389 // Verify the AudioNodesChanged event is fired and new audio device is added.
1390 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
Max Morin7a2d72a2017-07-20 19:06:251391 VerifySystemMonitorWasCalled();
[email protected]ee38bc42013-07-29 21:41:321392 cras_audio_handler_->GetAudioDevices(&audio_devices);
1393 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1394
1395 // Verify the active input device is switched to USB mic and
1396 // and ActiveInputChanged event is fired.
1397 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041398 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:321399 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1400}
1401
tbarzic3ab01a22016-12-17 03:12:531402TEST_P(CrasAudioHandlerTest, UnplugUSBMic) {
[email protected]ee38bc42013-07-29 21:41:321403 // Set up initial audio devices, with internal mic and USB Mic.
tbarzic3ab01a22016-12-17 03:12:531404 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalMic, kUSBMic});
[email protected]ee38bc42013-07-29 21:41:321405 SetUpCrasAudioHandler(audio_nodes);
1406 const size_t init_nodes_size = audio_nodes.size();
1407
1408 // Verify the audio devices size.
1409 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1410 AudioDeviceList audio_devices;
1411 cras_audio_handler_->GetAudioDevices(&audio_devices);
1412 EXPECT_EQ(init_nodes_size, audio_devices.size());
1413
1414 // Verify the USB mic is selected as the active output.
1415 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041416 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:321417 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
Max Morin7a2d72a2017-07-20 19:06:251418 system_monitor_observer_.reset_count();
[email protected]ee38bc42013-07-29 21:41:321419
1420 // Unplug the USB Mic.
1421 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531422 audio_nodes.push_back(GenerateAudioNode(kInternalMic));
[email protected]ee38bc42013-07-29 21:41:321423 ChangeAudioNodes(audio_nodes);
1424
1425 // Verify the AudioNodesChanged event is fired, and one audio device is
1426 // removed.
1427 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
Max Morin7a2d72a2017-07-20 19:06:251428 VerifySystemMonitorWasCalled();
[email protected]ee38bc42013-07-29 21:41:321429 cras_audio_handler_->GetAudioDevices(&audio_devices);
1430 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
1431
1432 // Verify the active input device is switched to internal mic, and
1433 // and ActiveInputChanged event is fired.
1434 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
tbarzic3ab01a22016-12-17 03:12:531435 EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:321436 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1437}
1438
tbarzic3ab01a22016-12-17 03:12:531439TEST_P(CrasAudioHandlerTest, PlugUSBMicNotAffectActiveOutput) {
[email protected]babc24b2013-09-09 23:50:231440 // Set up initial audio devices.
tbarzic3ab01a22016-12-17 03:12:531441 AudioNodeList audio_nodes =
1442 GenerateAudioNodeList({kInternalSpeaker, kHeadphone, kInternalMic});
[email protected]babc24b2013-09-09 23:50:231443 SetUpCrasAudioHandler(audio_nodes);
1444 const size_t init_nodes_size = audio_nodes.size();
1445
1446 // Verify the audio devices size.
1447 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1448 AudioDeviceList audio_devices;
1449 cras_audio_handler_->GetAudioDevices(&audio_devices);
1450 EXPECT_EQ(init_nodes_size, audio_devices.size());
1451
1452 // Verify the internal mic is selected as the active input.
1453 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041454 EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]babc24b2013-09-09 23:50:231455 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1456
1457 // Verify the headphone is selected as the active output.
1458 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041459 EXPECT_EQ(kHeadphoneId, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]babc24b2013-09-09 23:50:231460 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1461
1462 // Switch the active output to internal speaker.
tbarzic3ab01a22016-12-17 03:12:531463 AudioDevice internal_speaker(GenerateAudioNode(kInternalSpeaker));
jennyzbd236742016-03-02 20:15:521464 cras_audio_handler_->SwitchToDevice(internal_speaker, true,
1465 CrasAudioHandler::ACTIVATE_BY_USER);
[email protected]babc24b2013-09-09 23:50:231466
1467 // Verify the active output is switched to internal speaker, and the
1468 // ActiveOutputNodeChanged event is fired.
1469 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1470 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041471 EXPECT_TRUE(
1472 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531473 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
1474 EXPECT_EQ(kInternalSpeaker->id,
jennyzb47947f2014-09-25 00:42:041475 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]babc24b2013-09-09 23:50:231476
1477 // Plug the USB Mic.
[email protected]4642d7792013-09-18 20:27:171478 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531479 AudioNode internal_speaker_node = GenerateAudioNode(kInternalSpeaker);
[email protected]4642d7792013-09-18 20:27:171480 internal_speaker_node.active = true;
1481 audio_nodes.push_back(internal_speaker_node);
tbarzic3ab01a22016-12-17 03:12:531482 audio_nodes.push_back(GenerateAudioNode(kHeadphone));
1483 AudioNode internal_mic = GenerateAudioNode(kInternalMic);
[email protected]4642d7792013-09-18 20:27:171484 internal_mic.active = true;
1485 audio_nodes.push_back(internal_mic);
tbarzic3ab01a22016-12-17 03:12:531486 audio_nodes.push_back(GenerateAudioNode(kUSBMic));
[email protected]babc24b2013-09-09 23:50:231487 ChangeAudioNodes(audio_nodes);
1488
1489 // Verify the AudioNodesChanged event is fired, one new device is added.
1490 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1491 cras_audio_handler_->GetAudioDevices(&audio_devices);
1492 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1493
1494 // Verify the active input device is switched to USB mic, and
1495 // and ActiveInputChanged event is fired.
1496 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
tbarzic3ab01a22016-12-17 03:12:531497 EXPECT_EQ(kUSBMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]babc24b2013-09-09 23:50:231498 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1499
1500 // Verify the active output device is not changed.
1501 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041502 EXPECT_TRUE(
1503 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531504 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
1505 EXPECT_EQ(kInternalSpeaker->id,
jennyzb47947f2014-09-25 00:42:041506 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]babc24b2013-09-09 23:50:231507}
1508
tbarzic3ab01a22016-12-17 03:12:531509TEST_P(CrasAudioHandlerTest, PlugHeadphoneAutoUnplugSpeakerWithActiveUSB) {
[email protected]ce631ee2014-01-23 03:37:061510 // Set up initial audio devices.
tbarzic3ab01a22016-12-17 03:12:531511 AudioNodeList audio_nodes =
1512 GenerateAudioNodeList({kUSBHeadphone1, kInternalSpeaker, kInternalMic});
[email protected]ce631ee2014-01-23 03:37:061513 SetUpCrasAudioHandler(audio_nodes);
1514 const size_t init_nodes_size = audio_nodes.size();
1515
1516 // Verify the audio devices size.
1517 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1518 AudioDeviceList audio_devices;
1519 cras_audio_handler_->GetAudioDevices(&audio_devices);
1520 EXPECT_EQ(init_nodes_size, audio_devices.size());
1521
1522 // Verify the internal mic is selected as the active input.
1523 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041524 EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ce631ee2014-01-23 03:37:061525 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1526
1527 // Verify the USB headphone is selected as the active output.
1528 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041529 EXPECT_EQ(kUSBHeadphoneId1,
1530 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ce631ee2014-01-23 03:37:061531 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1532
1533 // Plug the headphone and auto-unplug internal speaker.
1534 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531535 AudioNode usb_headphone_node = GenerateAudioNode(kUSBHeadphone1);
[email protected]ce631ee2014-01-23 03:37:061536 usb_headphone_node.active = true;
1537 audio_nodes.push_back(usb_headphone_node);
tbarzic3ab01a22016-12-17 03:12:531538 AudioNode headphone_node = GenerateAudioNode(kHeadphone);
[email protected]ce631ee2014-01-23 03:37:061539 headphone_node.plugged_time = 1000;
1540 audio_nodes.push_back(headphone_node);
tbarzic3ab01a22016-12-17 03:12:531541 AudioNode internal_mic = GenerateAudioNode(kInternalMic);
[email protected]ce631ee2014-01-23 03:37:061542 internal_mic.active = true;
1543 audio_nodes.push_back(internal_mic);
1544 ChangeAudioNodes(audio_nodes);
1545
1546 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1547 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1548 cras_audio_handler_->GetAudioDevices(&audio_devices);
1549 EXPECT_EQ(init_nodes_size, audio_devices.size());
1550
1551 // Verify the active output device is switched to headphone, and
1552 // an ActiveOutputChanged event is fired.
1553 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
tbarzic3ab01a22016-12-17 03:12:531554 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ce631ee2014-01-23 03:37:061555 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1556
1557 // Unplug the headphone and internal speaker auto-plugs back.
1558 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531559 audio_nodes.push_back(GenerateAudioNode(kUSBHeadphone1));
1560 AudioNode internal_speaker_node = GenerateAudioNode(kInternalSpeaker);
[email protected]ce631ee2014-01-23 03:37:061561 internal_speaker_node.plugged_time = 2000;
1562 audio_nodes.push_back(internal_speaker_node);
1563 audio_nodes.push_back(internal_mic);
1564 ChangeAudioNodes(audio_nodes);
1565
1566 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1567 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1568 cras_audio_handler_->GetAudioDevices(&audio_devices);
1569 EXPECT_EQ(init_nodes_size, audio_devices.size());
1570
1571 // Verify the active output device is switched back to USB, and
1572 // an ActiveOutputChanged event is fired.
1573 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
tbarzic3ab01a22016-12-17 03:12:531574 EXPECT_EQ(kUSBHeadphone1->id,
jennyzb47947f2014-09-25 00:42:041575 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ce631ee2014-01-23 03:37:061576 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1577
1578 // Verify the active input device is not changed.
1579 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
tbarzic3ab01a22016-12-17 03:12:531580 EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ce631ee2014-01-23 03:37:061581}
1582
tbarzic3ab01a22016-12-17 03:12:531583TEST_P(CrasAudioHandlerTest, PlugMicAutoUnplugInternalMicWithActiveUSB) {
[email protected]ce631ee2014-01-23 03:37:061584 // Set up initial audio devices.
tbarzic3ab01a22016-12-17 03:12:531585 AudioNodeList audio_nodes = GenerateAudioNodeList(
1586 {kUSBHeadphone1, kInternalSpeaker, kUSBMic, kInternalMic});
[email protected]ce631ee2014-01-23 03:37:061587 SetUpCrasAudioHandler(audio_nodes);
1588 const size_t init_nodes_size = audio_nodes.size();
1589
1590 // Verify the audio devices size.
1591 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1592 AudioDeviceList audio_devices;
1593 cras_audio_handler_->GetAudioDevices(&audio_devices);
1594 EXPECT_EQ(init_nodes_size, audio_devices.size());
1595
1596 // Verify the internal mic is selected as the active input.
1597 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041598 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ce631ee2014-01-23 03:37:061599 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1600
1601 // Verify the internal speaker is selected as the active output.
1602 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041603 EXPECT_EQ(kUSBHeadphoneId1,
1604 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ce631ee2014-01-23 03:37:061605 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1606
1607 // Plug the headphone and mic, auto-unplug internal mic and speaker.
1608 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531609 AudioNode usb_headphone_node = GenerateAudioNode(kUSBHeadphone1);
[email protected]ce631ee2014-01-23 03:37:061610 usb_headphone_node.active = true;
1611 audio_nodes.push_back(usb_headphone_node);
tbarzic3ab01a22016-12-17 03:12:531612 AudioNode headphone_node = GenerateAudioNode(kHeadphone);
[email protected]ce631ee2014-01-23 03:37:061613 headphone_node.plugged_time = 1000;
1614 audio_nodes.push_back(headphone_node);
tbarzic3ab01a22016-12-17 03:12:531615 AudioNode usb_mic = GenerateAudioNode(kUSBMic);
[email protected]ce631ee2014-01-23 03:37:061616 usb_mic.active = true;
1617 audio_nodes.push_back(usb_mic);
tbarzic3ab01a22016-12-17 03:12:531618 AudioNode mic_jack = GenerateAudioNode(kMicJack);
[email protected]ce631ee2014-01-23 03:37:061619 mic_jack.plugged_time = 1000;
1620 audio_nodes.push_back(mic_jack);
1621 ChangeAudioNodes(audio_nodes);
1622
1623 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1624 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1625 cras_audio_handler_->GetAudioDevices(&audio_devices);
1626 EXPECT_EQ(init_nodes_size, audio_devices.size());
1627
1628 // Verify the active output device is switched to headphone, and
1629 // an ActiveOutputChanged event is fired.
1630 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
tbarzic3ab01a22016-12-17 03:12:531631 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ce631ee2014-01-23 03:37:061632 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1633
1634 // Verify the active input device is switched to mic jack, and
1635 // an ActiveInputChanged event is fired.
1636 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
tbarzic3ab01a22016-12-17 03:12:531637 EXPECT_EQ(kMicJack->id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ce631ee2014-01-23 03:37:061638 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1639
1640 // Unplug the headphone and internal speaker auto-plugs back.
1641 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531642 audio_nodes.push_back(GenerateAudioNode(kUSBHeadphone1));
1643 AudioNode internal_speaker_node = GenerateAudioNode(kInternalSpeaker);
[email protected]ce631ee2014-01-23 03:37:061644 internal_speaker_node.plugged_time = 2000;
1645 audio_nodes.push_back(internal_speaker_node);
tbarzic3ab01a22016-12-17 03:12:531646 audio_nodes.push_back(GenerateAudioNode(kUSBMic));
1647 AudioNode internal_mic = GenerateAudioNode(kInternalMic);
[email protected]ce631ee2014-01-23 03:37:061648 internal_mic.plugged_time = 2000;
1649 audio_nodes.push_back(internal_mic);
1650 ChangeAudioNodes(audio_nodes);
1651
1652 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1653 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1654 cras_audio_handler_->GetAudioDevices(&audio_devices);
1655 EXPECT_EQ(init_nodes_size, audio_devices.size());
1656
1657 // Verify the active output device is switched back to USB, and
1658 // an ActiveOutputChanged event is fired.
1659 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
tbarzic3ab01a22016-12-17 03:12:531660 EXPECT_EQ(kUSBHeadphone1->id,
jennyzb47947f2014-09-25 00:42:041661 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ce631ee2014-01-23 03:37:061662 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1663
1664 // Verify the active input device is switched back to USB mic, and
1665 // an ActiveInputChanged event is fired.
1666 EXPECT_EQ(2, test_observer_->active_input_node_changed_count());
tbarzic3ab01a22016-12-17 03:12:531667 EXPECT_EQ(kUSBMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ce631ee2014-01-23 03:37:061668 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1669}
1670
tbarzic3ab01a22016-12-17 03:12:531671TEST_P(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnPlugInHeadphone) {
[email protected]4642d7792013-09-18 20:27:171672 // Set up initial audio devices.
tbarzic3ab01a22016-12-17 03:12:531673 AudioNodeList audio_nodes =
1674 GenerateAudioNodeList({kInternalSpeaker, kBluetoothHeadset});
[email protected]4642d7792013-09-18 20:27:171675 SetUpCrasAudioHandler(audio_nodes);
1676 const size_t init_nodes_size = audio_nodes.size();
1677
1678 // Verify the audio devices size.
1679 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1680 AudioDeviceList audio_devices;
1681 cras_audio_handler_->GetAudioDevices(&audio_devices);
1682 EXPECT_EQ(init_nodes_size, audio_devices.size());
1683
1684 // Verify the bluetooth headset is selected as the active output.
1685 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041686 EXPECT_EQ(kBluetoothHeadsetId,
1687 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]4642d7792013-09-18 20:27:171688 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041689 EXPECT_TRUE(
1690 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]4642d7792013-09-18 20:27:171691 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1692
1693 // Plug in headphone, but fire NodesChanged signal twice.
1694 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531695 audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
1696 AudioNode bluetooth_headset = GenerateAudioNode(kBluetoothHeadset);
[email protected]4642d7792013-09-18 20:27:171697 bluetooth_headset.plugged_time = 1000;
1698 bluetooth_headset.active = true;
1699 audio_nodes.push_back(bluetooth_headset);
tbarzic3ab01a22016-12-17 03:12:531700 AudioNode headphone = GenerateAudioNode(kHeadphone);
[email protected]4642d7792013-09-18 20:27:171701 headphone.active = false;
1702 headphone.plugged_time = 2000;
1703 audio_nodes.push_back(headphone);
1704 ChangeAudioNodes(audio_nodes);
1705 ChangeAudioNodes(audio_nodes);
1706
1707 // Verify the active output device is set to headphone.
1708 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
jennyzb47947f2014-09-25 00:42:041709 EXPECT_LE(1, test_observer_->active_output_node_changed_count());
1710 EXPECT_EQ(headphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1711 EXPECT_TRUE(
1712 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]4642d7792013-09-18 20:27:171713 EXPECT_EQ(headphone.id, active_output.id);
1714
1715 // Verfiy the audio devices data is consistent, i.e., the active output device
1716 // should be headphone.
1717 cras_audio_handler_->GetAudioDevices(&audio_devices);
1718 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1719 for (size_t i = 0; i < audio_devices.size(); ++i) {
tbarzic3ab01a22016-12-17 03:12:531720 if (audio_devices[i].id == kInternalSpeaker->id)
[email protected]4642d7792013-09-18 20:27:171721 EXPECT_FALSE(audio_devices[i].active);
1722 else if (audio_devices[i].id == bluetooth_headset.id)
1723 EXPECT_FALSE(audio_devices[i].active);
1724 else if (audio_devices[i].id == headphone.id)
1725 EXPECT_TRUE(audio_devices[i].active);
1726 else
1727 NOTREACHED();
1728 }
1729}
1730
tbarzic3ab01a22016-12-17 03:12:531731TEST_P(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnPlugInUSBMic) {
[email protected]4642d7792013-09-18 20:27:171732 // Set up initial audio devices.
tbarzic3ab01a22016-12-17 03:12:531733 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalMic});
[email protected]4642d7792013-09-18 20:27:171734 SetUpCrasAudioHandler(audio_nodes);
1735 const size_t init_nodes_size = audio_nodes.size();
1736
1737 // Verify the audio devices size.
1738 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1739 AudioDeviceList audio_devices;
1740 cras_audio_handler_->GetAudioDevices(&audio_devices);
1741 EXPECT_EQ(init_nodes_size, audio_devices.size());
1742
1743 // Verify the internal mic is selected as the active output.
1744 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
tbarzic3ab01a22016-12-17 03:12:531745 EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]4642d7792013-09-18 20:27:171746 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
1747 EXPECT_TRUE(audio_devices[0].active);
1748
1749 // Plug in usb mic, but fire NodesChanged signal twice.
1750 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531751 AudioNode internal_mic = GenerateAudioNode(kInternalMic);
[email protected]4642d7792013-09-18 20:27:171752 internal_mic.active = true;
1753 internal_mic.plugged_time = 1000;
1754 audio_nodes.push_back(internal_mic);
tbarzic3ab01a22016-12-17 03:12:531755 AudioNode usb_mic = GenerateAudioNode(kUSBMic);
[email protected]4642d7792013-09-18 20:27:171756 usb_mic.active = false;
1757 usb_mic.plugged_time = 2000;
1758 audio_nodes.push_back(usb_mic);
1759 ChangeAudioNodes(audio_nodes);
1760 ChangeAudioNodes(audio_nodes);
1761
1762 // Verify the active output device is set to headphone.
1763 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
jennyzb47947f2014-09-25 00:42:041764 EXPECT_LE(1, test_observer_->active_input_node_changed_count());
1765 EXPECT_EQ(usb_mic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]4642d7792013-09-18 20:27:171766 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1767
1768 // Verfiy the audio devices data is consistent, i.e., the active input device
1769 // should be usb mic.
1770 cras_audio_handler_->GetAudioDevices(&audio_devices);
1771 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1772 for (size_t i = 0; i < audio_devices.size(); ++i) {
tbarzic3ab01a22016-12-17 03:12:531773 if (audio_devices[i].id == kInternalMic->id)
[email protected]4642d7792013-09-18 20:27:171774 EXPECT_FALSE(audio_devices[i].active);
1775 else if (audio_devices[i].id == usb_mic.id)
1776 EXPECT_TRUE(audio_devices[i].active);
1777 else
1778 NOTREACHED();
1779 }
1780}
1781
1782// This is the case of crbug.com/291303.
tbarzic3ab01a22016-12-17 03:12:531783TEST_P(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnSystemBoot) {
[email protected]4642d7792013-09-18 20:27:171784 // Set up audio handler with empty audio_nodes.
1785 AudioNodeList audio_nodes;
1786 SetUpCrasAudioHandler(audio_nodes);
1787
tbarzic3ab01a22016-12-17 03:12:531788 AudioNode internal_speaker = GenerateAudioNode(kInternalSpeaker);
[email protected]4642d7792013-09-18 20:27:171789 internal_speaker.active = false;
tbarzic3ab01a22016-12-17 03:12:531790 AudioNode headphone = GenerateAudioNode(kHeadphone);
[email protected]4642d7792013-09-18 20:27:171791 headphone.active = false;
tbarzic3ab01a22016-12-17 03:12:531792 AudioNode internal_mic = GenerateAudioNode(kInternalMic);
[email protected]4642d7792013-09-18 20:27:171793 internal_mic.active = false;
1794 audio_nodes.push_back(internal_speaker);
1795 audio_nodes.push_back(headphone);
1796 audio_nodes.push_back(internal_mic);
1797 const size_t init_nodes_size = audio_nodes.size();
1798
1799 // Simulate AudioNodesChanged signal being fired twice during system boot.
1800 ChangeAudioNodes(audio_nodes);
1801 ChangeAudioNodes(audio_nodes);
1802
1803 // Verify the active output device is set to headphone.
1804 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
jennyzb47947f2014-09-25 00:42:041805 EXPECT_LE(1, test_observer_->active_output_node_changed_count());
1806 EXPECT_EQ(headphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]4642d7792013-09-18 20:27:171807 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041808 EXPECT_TRUE(
1809 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]4642d7792013-09-18 20:27:171810 EXPECT_EQ(headphone.id, active_output.id);
1811
1812 // Verify the active input device id is set to internal mic.
jennyzb47947f2014-09-25 00:42:041813 EXPECT_EQ(internal_mic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]4642d7792013-09-18 20:27:171814
1815 // Verfiy the audio devices data is consistent, i.e., the active output device
1816 // should be headphone, and the active input device should internal mic.
1817 AudioDeviceList audio_devices;
1818 cras_audio_handler_->GetAudioDevices(&audio_devices);
1819 EXPECT_EQ(init_nodes_size, audio_devices.size());
1820 for (size_t i = 0; i < audio_devices.size(); ++i) {
1821 if (audio_devices[i].id == internal_speaker.id)
1822 EXPECT_FALSE(audio_devices[i].active);
1823 else if (audio_devices[i].id == headphone.id)
1824 EXPECT_TRUE(audio_devices[i].active);
1825 else if (audio_devices[i].id == internal_mic.id)
1826 EXPECT_TRUE(audio_devices[i].active);
1827 else
1828 NOTREACHED();
1829 }
1830}
1831
jennyz2d8986d2015-01-15 18:34:071832// This is the case of crbug.com/448924.
tbarzic3ab01a22016-12-17 03:12:531833TEST_P(CrasAudioHandlerTest,
jennyz2d8986d2015-01-15 18:34:071834 TwoNodesChangedSignalsForLosingTowNodesOnOneUnplug) {
1835 // Set up audio handler with 4 audio_nodes.
1836 AudioNodeList audio_nodes;
tbarzic3ab01a22016-12-17 03:12:531837 AudioNode internal_speaker = GenerateAudioNode(kInternalSpeaker);
jennyz2d8986d2015-01-15 18:34:071838 internal_speaker.active = false;
tbarzic3ab01a22016-12-17 03:12:531839 AudioNode headphone = GenerateAudioNode(kHeadphone);
jennyz2d8986d2015-01-15 18:34:071840 headphone.active = false;
tbarzic3ab01a22016-12-17 03:12:531841 AudioNode internal_mic = GenerateAudioNode(kInternalMic);
jennyz2d8986d2015-01-15 18:34:071842 internal_mic.active = false;
tbarzic3ab01a22016-12-17 03:12:531843 AudioNode micJack = GenerateAudioNode(kMicJack);
jennyz2d8986d2015-01-15 18:34:071844 micJack.active = false;
1845 audio_nodes.push_back(internal_speaker);
1846 audio_nodes.push_back(headphone);
1847 audio_nodes.push_back(internal_mic);
1848 audio_nodes.push_back(micJack);
1849 SetUpCrasAudioHandler(audio_nodes);
1850
1851 // Verify the audio devices size.
1852 AudioDeviceList audio_devices;
1853 cras_audio_handler_->GetAudioDevices(&audio_devices);
1854 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
1855
1856 // Verify the headphone has been selected as the active output.
1857 AudioDevice active_output;
1858 EXPECT_TRUE(
1859 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531860 EXPECT_EQ(kHeadphone->id, active_output.id);
1861 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
jennyz2d8986d2015-01-15 18:34:071862 EXPECT_TRUE(active_output.active);
1863 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1864
1865 // Verify the mic Jack has been selected as the active input.
1866 EXPECT_EQ(micJack.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1867 const AudioDevice* active_input = GetDeviceFromId(micJack.id);
1868 EXPECT_TRUE(active_input->active);
1869 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1870
1871 // Simulate the nodes list in first NodesChanged signal, only headphone is
1872 // removed, other nodes remains the same.
1873 AudioNodeList changed_nodes_1;
1874 internal_speaker.active = false;
1875 changed_nodes_1.push_back(internal_speaker);
1876 internal_mic.active = false;
1877 changed_nodes_1.push_back(internal_mic);
1878 micJack.active = true;
1879 changed_nodes_1.push_back(micJack);
1880
1881 // Simulate the nodes list in second NodesChanged signal, the micJac is
1882 // removed, but the internal_mic is inactive, which does not reflect the
1883 // active status set from the first NodesChanged signal since this was sent
1884 // before cras receives the SetActiveOutputNode from the first NodesChanged
1885 // handling.
1886 AudioNodeList changed_nodes_2;
1887 changed_nodes_2.push_back(internal_speaker);
1888 changed_nodes_2.push_back(internal_mic);
1889
1890 // Simulate AudioNodesChanged signal being fired twice for unplug an audio
1891 // device with both input and output nodes on it.
1892 ChangeAudioNodes(changed_nodes_1);
1893 ChangeAudioNodes(changed_nodes_2);
1894
1895 AudioDeviceList changed_devices;
1896 cras_audio_handler_->GetAudioDevices(&changed_devices);
1897 EXPECT_EQ(2u, changed_devices.size());
1898
1899 // Verify the active output device is set to internal speaker.
1900 EXPECT_EQ(internal_speaker.id,
1901 cras_audio_handler_->GetPrimaryActiveOutputNode());
1902 EXPECT_TRUE(
1903 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1904 EXPECT_EQ(internal_speaker.id, active_output.id);
1905 EXPECT_TRUE(active_output.active);
1906
1907 // Verify the active input device id is set to internal mic.
1908 EXPECT_EQ(internal_mic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1909 const AudioDevice* changed_active_input = GetDeviceFromId(internal_mic.id);
1910 EXPECT_TRUE(changed_active_input->active);
1911}
1912
tbarzic3ab01a22016-12-17 03:12:531913TEST_P(CrasAudioHandlerTest, SetOutputMono) {
1914 AudioNodeList audio_nodes = GenerateAudioNodeList({kHeadphone});
warx74524622016-03-31 01:07:211915 SetUpCrasAudioHandler(audio_nodes);
1916 EXPECT_EQ(0, test_observer_->output_channel_remixing_changed_count());
1917
1918 // Set output mono
1919 cras_audio_handler_->SetOutputMono(true);
1920
1921 // Verify the output is in mono mode, OnOuputChannelRemixingChanged event
1922 // is fired.
1923 EXPECT_TRUE(cras_audio_handler_->IsOutputMonoEnabled());
1924 EXPECT_EQ(1, test_observer_->output_channel_remixing_changed_count());
1925
1926 // Set output stereo
1927 cras_audio_handler_->SetOutputMono(false);
1928 EXPECT_FALSE(cras_audio_handler_->IsOutputMonoEnabled());
1929 EXPECT_EQ(2, test_observer_->output_channel_remixing_changed_count());
1930}
1931
tbarzic3ab01a22016-12-17 03:12:531932TEST_P(CrasAudioHandlerTest, SetOutputMute) {
1933 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker});
[email protected]ee38bc42013-07-29 21:41:321934 SetUpCrasAudioHandler(audio_nodes);
1935 EXPECT_EQ(0, test_observer_->output_mute_changed_count());
1936
1937 // Mute the device.
1938 cras_audio_handler_->SetOutputMute(true);
1939
1940 // Verify the output is muted, OnOutputMuteChanged event is fired,
1941 // and mute value is saved in the preferences.
1942 EXPECT_TRUE(cras_audio_handler_->IsOutputMuted());
1943 EXPECT_EQ(1, test_observer_->output_mute_changed_count());
tbarzic3ab01a22016-12-17 03:12:531944 AudioDevice speaker(GenerateAudioNode(kInternalSpeaker));
[email protected]ee38bc42013-07-29 21:41:321945 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(speaker));
1946
1947 // Unmute the device.
1948 cras_audio_handler_->SetOutputMute(false);
1949
1950 // Verify the output is unmuted, OnOutputMuteChanged event is fired,
1951 // and mute value is saved in the preferences.
1952 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
1953 EXPECT_EQ(2, test_observer_->output_mute_changed_count());
1954 EXPECT_FALSE(audio_pref_handler_->GetMuteValue(speaker));
1955}
1956
tbarzic3ab01a22016-12-17 03:12:531957TEST_P(CrasAudioHandlerTest, SetInputMute) {
1958 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalMic});
[email protected]ee38bc42013-07-29 21:41:321959 SetUpCrasAudioHandler(audio_nodes);
1960 EXPECT_EQ(0, test_observer_->input_mute_changed_count());
1961
1962 // Mute the device.
1963 cras_audio_handler_->SetInputMute(true);
1964
[email protected]f3dd2962014-04-22 04:48:381965 // Verify the input is muted, OnInputMuteChanged event is fired.
[email protected]ee38bc42013-07-29 21:41:321966 EXPECT_TRUE(cras_audio_handler_->IsInputMuted());
1967 EXPECT_EQ(1, test_observer_->input_mute_changed_count());
[email protected]ee38bc42013-07-29 21:41:321968
1969 // Unmute the device.
1970 cras_audio_handler_->SetInputMute(false);
1971
[email protected]f3dd2962014-04-22 04:48:381972 // Verify the input is unmuted, OnInputMuteChanged event is fired.
[email protected]ee38bc42013-07-29 21:41:321973 EXPECT_FALSE(cras_audio_handler_->IsInputMuted());
1974 EXPECT_EQ(2, test_observer_->input_mute_changed_count());
[email protected]ee38bc42013-07-29 21:41:321975}
1976
tbarzic3ab01a22016-12-17 03:12:531977TEST_P(CrasAudioHandlerTest, SetOutputVolumePercent) {
1978 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker});
[email protected]ee38bc42013-07-29 21:41:321979 SetUpCrasAudioHandler(audio_nodes);
1980 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
1981
warxfd7c37b2016-08-05 19:38:071982 const int kVolume = 60;
1983 cras_audio_handler_->SetOutputVolumePercent(kVolume);
[email protected]ee38bc42013-07-29 21:41:321984
1985 // Verify the output volume is changed to the designated value,
jennyz481292c2015-03-21 01:49:291986 // OnOutputNodeVolumeChanged event is fired, and the device volume value
warxfd7c37b2016-08-05 19:38:071987 // is saved in the preferences.
[email protected]ee38bc42013-07-29 21:41:321988 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent());
1989 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
1990 AudioDevice device;
jennyzb47947f2014-09-25 00:42:041991 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device));
tbarzic3ab01a22016-12-17 03:12:531992 EXPECT_EQ(device.id, kInternalSpeaker->id);
[email protected]140a31452013-09-09 18:54:371993 EXPECT_EQ(kVolume, audio_pref_handler_->GetOutputVolumeValue(&device));
[email protected]ee38bc42013-07-29 21:41:321994}
1995
tbarzic3ab01a22016-12-17 03:12:531996TEST_P(CrasAudioHandlerTest, SetOutputVolumePercentWithoutNotifyingObservers) {
1997 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker});
warxfd7c37b2016-08-05 19:38:071998 SetUpCrasAudioHandler(audio_nodes);
1999 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2000
2001 const int kVolume1 = 60;
2002 const int kVolume2 = 80;
2003 cras_audio_handler_->SetOutputVolumePercentWithoutNotifyingObservers(
2004 kVolume1, CrasAudioHandler::VOLUME_CHANGE_MAXIMIZE_MODE_SCREENSHOT);
2005 // Verify the output volume is changed to the designated value,
2006 // OnOutputNodeVolumeChanged event is not fired, and the device volume value
2007 // is saved in the preferences.
2008 EXPECT_EQ(kVolume1, cras_audio_handler_->GetOutputVolumePercent());
2009 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2010 AudioDevice device;
2011 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device));
tbarzic3ab01a22016-12-17 03:12:532012 EXPECT_EQ(device.id, kInternalSpeaker->id);
warxfd7c37b2016-08-05 19:38:072013 EXPECT_EQ(kVolume1, audio_pref_handler_->GetOutputVolumeValue(&device));
2014
2015 // Make another SetOutputVolumePercentWithoutNotifyingObservers call to make
2016 // sure everything is right.
2017 cras_audio_handler_->SetOutputVolumePercentWithoutNotifyingObservers(
2018 kVolume2, CrasAudioHandler::VOLUME_CHANGE_MAXIMIZE_MODE_SCREENSHOT);
2019 EXPECT_EQ(kVolume2, cras_audio_handler_->GetOutputVolumePercent());
2020 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2021 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device));
tbarzic3ab01a22016-12-17 03:12:532022 EXPECT_EQ(device.id, kInternalSpeaker->id);
warxfd7c37b2016-08-05 19:38:072023 EXPECT_EQ(kVolume2, audio_pref_handler_->GetOutputVolumeValue(&device));
2024
2025 // Make a final SetOutputVolumePercent call to check if
2026 // SetOutputVolumePercentWithoutNotifyingObservers may block subsequent
2027 // notifying observers.
2028 cras_audio_handler_->SetOutputVolumePercent(kVolume1);
2029 EXPECT_EQ(kVolume1, cras_audio_handler_->GetOutputVolumePercent());
2030 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
2031 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device));
tbarzic3ab01a22016-12-17 03:12:532032 EXPECT_EQ(device.id, kInternalSpeaker->id);
warxfd7c37b2016-08-05 19:38:072033 EXPECT_EQ(kVolume1, audio_pref_handler_->GetOutputVolumeValue(&device));
2034}
2035
tbarzic3ab01a22016-12-17 03:12:532036TEST_P(CrasAudioHandlerTest, RestartAudioClientWithCrasReady) {
2037 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker});
warxfd7c37b2016-08-05 19:38:072038 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(
tbarzic3ab01a22016-12-17 03:12:532053 kInternalSpeaker->id, kDefaultVolume);
warxfd7c37b2016-08-05 19:38:072054 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(
tbarzic3ab01a22016-12-17 03:12:532061 kInternalSpeaker->id, kVolume);
warxfd7c37b2016-08-05 19:38:072062 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
2063 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent());
2064}
2065
tbarzic3ab01a22016-12-17 03:12:532066TEST_P(CrasAudioHandlerTest, RestartAudioClientWithCrasDropRequest) {
2067 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker});
warxfd7c37b2016-08-05 19:38:072068 SetUpCrasAudioHandler(audio_nodes);
2069 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2070
2071 const int kDefaultVolume = cras_audio_handler_->GetOutputVolumePercent();
2072 // Disable the auto OutputNodeVolumeChanged signal.
2073 fake_cras_audio_client_->set_notify_volume_change_with_delay(true);
2074
2075 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes);
2076 RestartAudioClient();
2077 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2078 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2079
2080 // A wrong initialization OutputNodeVolumeChanged event is fired. This may
2081 // happen when Cras is not ready and drops request. The approach we use is
2082 // to log warning message, clear the pending automated volume change reasons,
2083 // and notify observers about this change.
2084 const int kVolume1 = 30;
2085 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
tbarzic3ab01a22016-12-17 03:12:532086 kInternalSpeaker->id, kVolume1);
warxfd7c37b2016-08-05 19:38:072087 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
2088 EXPECT_EQ(kVolume1, cras_audio_handler_->GetOutputVolumePercent());
2089
2090 // The later OutputNodeVolumeChanged event should notify observers.
2091 const int kVolume2 = 60;
2092 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
tbarzic3ab01a22016-12-17 03:12:532093 kInternalSpeaker->id, kVolume2);
warxfd7c37b2016-08-05 19:38:072094 EXPECT_EQ(2, test_observer_->output_volume_changed_count());
2095 EXPECT_EQ(kVolume2, cras_audio_handler_->GetOutputVolumePercent());
2096}
2097
tbarzic3ab01a22016-12-17 03:12:532098TEST_P(CrasAudioHandlerTest, SetOutputVolumeWithDelayedSignal) {
2099 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker});
jennyz96526d02016-06-22 17:03:432100 SetUpCrasAudioHandler(audio_nodes);
2101 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2102
2103 const int kDefaultVolume = 75;
2104 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2105
2106 // Disable the auto OutputNodeVolumeChanged signal.
2107 fake_cras_audio_client_->set_notify_volume_change_with_delay(true);
2108
2109 // Verify the volume state is not changed before OutputNodeVolumeChanged
2110 // signal fires.
2111 const int kVolume = 60;
2112 cras_audio_handler_->SetOutputVolumePercent(kVolume);
2113 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2114 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2115
2116 // Verify the output volume is changed to the designated value after
2117 // OnOutputNodeVolumeChanged cras signal fires, and the volume change event
2118 // has been fired to notify the observers.
2119 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
tbarzic3ab01a22016-12-17 03:12:532120 kInternalSpeaker->id, kVolume);
jennyz96526d02016-06-22 17:03:432121 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
2122 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent());
2123 AudioDevice device;
2124 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device));
tbarzic3ab01a22016-12-17 03:12:532125 EXPECT_EQ(device.id, kInternalSpeaker->id);
jennyz96526d02016-06-22 17:03:432126 EXPECT_EQ(kVolume, audio_pref_handler_->GetOutputVolumeValue(&device));
2127}
2128
tbarzic3ab01a22016-12-17 03:12:532129TEST_P(CrasAudioHandlerTest,
jennyz96526d02016-06-22 17:03:432130 ChangeOutputVolumesWithDelayedSignalForSingleActiveDevice) {
tbarzic3ab01a22016-12-17 03:12:532131 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker});
jennyz96526d02016-06-22 17:03:432132 SetUpCrasAudioHandler(audio_nodes);
2133 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2134
2135 const int kDefaultVolume = 75;
2136 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2137
2138 // Disable the auto OutputNodeVolumeChanged signal.
2139 fake_cras_audio_client_->set_notify_volume_change_with_delay(true);
2140
2141 // Verify the volume state is not changed before OutputNodeVolumeChanged
2142 // signal fires.
2143 const int kVolume1 = 50;
2144 const int kVolume2 = 60;
2145 cras_audio_handler_->SetOutputVolumePercent(kVolume1);
2146 cras_audio_handler_->SetOutputVolumePercent(kVolume2);
2147 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2148 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2149
2150 // Simulate OutputNodeVolumeChanged signal fired with big latency that
2151 // it lags behind the SetOutputNodeVolume requests. Chrome sets the volume
2152 // to 50 then 60, but the volume changed signal for 50 comes back after
2153 // chrome sets the volume to 60. Verify chrome will sync to the designated
2154 // volume level after all signals arrive.
2155 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
tbarzic3ab01a22016-12-17 03:12:532156 kInternalSpeaker->id, kVolume1);
jennyz96526d02016-06-22 17:03:432157 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
2158 EXPECT_EQ(kVolume1, cras_audio_handler_->GetOutputVolumePercent());
2159
2160 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
tbarzic3ab01a22016-12-17 03:12:532161 kInternalSpeaker->id, kVolume2);
jennyz96526d02016-06-22 17:03:432162 EXPECT_EQ(2, test_observer_->output_volume_changed_count());
2163 EXPECT_EQ(kVolume2, cras_audio_handler_->GetOutputVolumePercent());
2164}
2165
tbarzic3ab01a22016-12-17 03:12:532166TEST_P(CrasAudioHandlerTest,
jennyz96526d02016-06-22 17:03:432167 ChangeOutputVolumeFromNonChromeSourceSingleActiveDevice) {
tbarzic3ab01a22016-12-17 03:12:532168 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker});
jennyz96526d02016-06-22 17:03:432169 SetUpCrasAudioHandler(audio_nodes);
2170 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2171
2172 const int kDefaultVolume = 75;
2173 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2174 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2175
2176 // Simulate OutputNodeVolumeChanged signal fired by a non-chrome source.
2177 // Verify chrome will sync its volume state to the volume from the signal,
2178 // and notify its observers for the volume change event.
2179 const int kVolume = 20;
2180 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
tbarzic3ab01a22016-12-17 03:12:532181 kInternalSpeaker->id, kVolume);
jennyz96526d02016-06-22 17:03:432182 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
2183 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent());
2184 AudioDevice device;
2185 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device));
tbarzic3ab01a22016-12-17 03:12:532186 EXPECT_EQ(device.id, kInternalSpeaker->id);
jennyz96526d02016-06-22 17:03:432187 EXPECT_EQ(kVolume, audio_pref_handler_->GetOutputVolumeValue(&device));
2188}
2189
tbarzic3ab01a22016-12-17 03:12:532190TEST_P(CrasAudioHandlerTest, SetInputGainPercent) {
2191 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalMic});
[email protected]ee38bc42013-07-29 21:41:322192 SetUpCrasAudioHandler(audio_nodes);
2193 EXPECT_EQ(0, test_observer_->input_gain_changed_count());
2194
2195 cras_audio_handler_->SetInputGainPercent(60);
2196
2197 // Verify the input gain changed to the designated value,
jennyz481292c2015-03-21 01:49:292198 // OnInputNodeGainChanged event is fired, and the device gain value
[email protected]ee38bc42013-07-29 21:41:322199 // is saved in the preferences.
2200 const int kGain = 60;
2201 EXPECT_EQ(kGain, cras_audio_handler_->GetInputGainPercent());
2202 EXPECT_EQ(1, test_observer_->input_gain_changed_count());
tbarzic3ab01a22016-12-17 03:12:532203 AudioDevice internal_mic(GenerateAudioNode(kInternalMic));
[email protected]140a31452013-09-09 18:54:372204 EXPECT_EQ(kGain, audio_pref_handler_->GetInputGainValue(&internal_mic));
[email protected]ee38bc42013-07-29 21:41:322205}
2206
tbarzic3ab01a22016-12-17 03:12:532207TEST_P(CrasAudioHandlerTest, SetMuteForDevice) {
2208 AudioNodeList audio_nodes = GenerateAudioNodeList(
2209 {kInternalSpeaker, kHeadphone, kInternalMic, kUSBMic});
[email protected]ee38bc42013-07-29 21:41:322210 SetUpCrasAudioHandler(audio_nodes);
2211
2212 // Mute the active output device.
tbarzic3ab01a22016-12-17 03:12:532213 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2214 cras_audio_handler_->SetMuteForDevice(kHeadphone->id, true);
[email protected]ee38bc42013-07-29 21:41:322215
2216 // Verify the headphone is muted and mute value is saved in the preferences.
tbarzic3ab01a22016-12-17 03:12:532217 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kHeadphone->id));
2218 AudioDevice headphone(GenerateAudioNode(kHeadphone));
[email protected]ee38bc42013-07-29 21:41:322219 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(headphone));
2220
2221 // Mute the non-active output device.
tbarzic3ab01a22016-12-17 03:12:532222 cras_audio_handler_->SetMuteForDevice(kInternalSpeaker->id, true);
[email protected]ee38bc42013-07-29 21:41:322223
2224 // Verify the internal speaker is muted and mute value is saved in the
2225 // preferences.
tbarzic3ab01a22016-12-17 03:12:532226 EXPECT_TRUE(
2227 cras_audio_handler_->IsOutputMutedForDevice(kInternalSpeaker->id));
2228 AudioDevice internal_speaker(GenerateAudioNode(kInternalSpeaker));
[email protected]ee38bc42013-07-29 21:41:322229 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(internal_speaker));
2230
2231 // Mute the active input device.
tbarzic3ab01a22016-12-17 03:12:532232 EXPECT_EQ(kUSBMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
2233 cras_audio_handler_->SetMuteForDevice(kUSBMic->id, true);
[email protected]ee38bc42013-07-29 21:41:322234
[email protected]f3dd2962014-04-22 04:48:382235 // Verify the USB Mic is muted.
tbarzic3ab01a22016-12-17 03:12:532236 EXPECT_TRUE(cras_audio_handler_->IsInputMutedForDevice(kUSBMic->id));
[email protected]ee38bc42013-07-29 21:41:322237
[email protected]f3dd2962014-04-22 04:48:382238 // Mute the non-active input device should be a no-op, see crbug.com/365050.
tbarzic3ab01a22016-12-17 03:12:532239 cras_audio_handler_->SetMuteForDevice(kInternalMic->id, true);
[email protected]ee38bc42013-07-29 21:41:322240
[email protected]f3dd2962014-04-22 04:48:382241 // Verify IsInputMutedForDevice returns false for non-active input device.
tbarzic3ab01a22016-12-17 03:12:532242 EXPECT_FALSE(cras_audio_handler_->IsInputMutedForDevice(kInternalMic->id));
[email protected]ee38bc42013-07-29 21:41:322243}
2244
tbarzic3ab01a22016-12-17 03:12:532245TEST_P(CrasAudioHandlerTest, SetVolumeGainPercentForDevice) {
2246 AudioNodeList audio_nodes = GenerateAudioNodeList(
2247 {kInternalSpeaker, kHeadphone, kInternalMic, kUSBMic});
[email protected]ee38bc42013-07-29 21:41:322248 SetUpCrasAudioHandler(audio_nodes);
2249
2250 // Set volume percent for active output device.
2251 const int kHeadphoneVolume = 30;
tbarzic3ab01a22016-12-17 03:12:532252 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2253 cras_audio_handler_->SetVolumeGainPercentForDevice(kHeadphone->id,
[email protected]ee38bc42013-07-29 21:41:322254 kHeadphoneVolume);
2255
2256 // Verify the volume percent of headphone is set, and saved in preferences.
tbarzic3ab01a22016-12-17 03:12:532257 EXPECT_EQ(
2258 kHeadphoneVolume,
2259 cras_audio_handler_->GetOutputVolumePercentForDevice(kHeadphone->id));
2260 AudioDevice headphone(GenerateAudioNode(kHeadphone));
[email protected]ee38bc42013-07-29 21:41:322261 EXPECT_EQ(kHeadphoneVolume,
[email protected]140a31452013-09-09 18:54:372262 audio_pref_handler_->GetOutputVolumeValue(&headphone));
[email protected]ee38bc42013-07-29 21:41:322263
2264 // Set volume percent for non-active output device.
2265 const int kSpeakerVolume = 60;
tbarzic3ab01a22016-12-17 03:12:532266 cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalSpeaker->id,
[email protected]ee38bc42013-07-29 21:41:322267 kSpeakerVolume);
2268
2269 // Verify the volume percent of speaker is set, and saved in preferences.
2270 EXPECT_EQ(kSpeakerVolume,
2271 cras_audio_handler_->GetOutputVolumePercentForDevice(
tbarzic3ab01a22016-12-17 03:12:532272 kInternalSpeaker->id));
2273 AudioDevice speaker(GenerateAudioNode(kInternalSpeaker));
[email protected]ee38bc42013-07-29 21:41:322274 EXPECT_EQ(kSpeakerVolume,
[email protected]140a31452013-09-09 18:54:372275 audio_pref_handler_->GetOutputVolumeValue(&speaker));
[email protected]ee38bc42013-07-29 21:41:322276
2277 // Set gain percent for active input device.
2278 const int kUSBMicGain = 30;
tbarzic3ab01a22016-12-17 03:12:532279 EXPECT_EQ(kUSBMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
2280 cras_audio_handler_->SetVolumeGainPercentForDevice(kUSBMic->id, kUSBMicGain);
[email protected]ee38bc42013-07-29 21:41:322281
2282 // Verify the gain percent of USB mic is set, and saved in preferences.
2283 EXPECT_EQ(kUSBMicGain,
tbarzic3ab01a22016-12-17 03:12:532284 cras_audio_handler_->GetOutputVolumePercentForDevice(kUSBMic->id));
2285 AudioDevice usb_mic(GenerateAudioNode(kHeadphone));
[email protected]ee38bc42013-07-29 21:41:322286 EXPECT_EQ(kUSBMicGain,
[email protected]140a31452013-09-09 18:54:372287 audio_pref_handler_->GetInputGainValue(&usb_mic));
[email protected]ee38bc42013-07-29 21:41:322288
2289 // Set gain percent for non-active input device.
2290 const int kInternalMicGain = 60;
tbarzic3ab01a22016-12-17 03:12:532291 cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalMic->id,
[email protected]ee38bc42013-07-29 21:41:322292 kInternalMicGain);
2293
2294 // Verify the gain percent of internal mic is set, and saved in preferences.
tbarzic3ab01a22016-12-17 03:12:532295 EXPECT_EQ(
2296 kInternalMicGain,
2297 cras_audio_handler_->GetOutputVolumePercentForDevice(kInternalMic->id));
2298 AudioDevice internal_mic(GenerateAudioNode(kInternalMic));
[email protected]ee38bc42013-07-29 21:41:322299 EXPECT_EQ(kInternalMicGain,
[email protected]140a31452013-09-09 18:54:372300 audio_pref_handler_->GetInputGainValue(&internal_mic));
[email protected]ee38bc42013-07-29 21:41:322301}
2302
tbarzic3ab01a22016-12-17 03:12:532303TEST_P(CrasAudioHandlerTest, HandleOtherDeviceType) {
[email protected]ee38bc42013-07-29 21:41:322304 const size_t kNumValidAudioDevices = 4;
tbarzic3ab01a22016-12-17 03:12:532305 AudioNodeList audio_nodes = GenerateAudioNodeList(
2306 {kInternalSpeaker, kOtherTypeOutput, kInternalMic, kOtherTypeInput});
[email protected]ee38bc42013-07-29 21:41:322307 SetUpCrasAudioHandler(audio_nodes);
2308
2309 // Verify the audio devices size.
2310 AudioDeviceList audio_devices;
2311 cras_audio_handler_->GetAudioDevices(&audio_devices);
2312 EXPECT_EQ(kNumValidAudioDevices, audio_devices.size());
2313
2314 // Verify the internal speaker has been selected as the active output,
2315 // and the output device with some randown unknown type is handled gracefully.
2316 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:042317 EXPECT_TRUE(
2318 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:532319 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
2320 EXPECT_EQ(kInternalSpeaker->id,
jennyzb47947f2014-09-25 00:42:042321 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:322322 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
2323
2324 // Ensure the internal microphone has been selected as the active input,
2325 // and the input device with some random unknown type is handled gracefully.
2326 AudioDevice active_input;
tbarzic3ab01a22016-12-17 03:12:532327 EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:322328 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
2329}
2330
tbarzic3ab01a22016-12-17 03:12:532331TEST_P(CrasAudioHandlerTest, ActiveDeviceSelectionWithStableDeviceId) {
hychaofe100d542016-01-12 10:17:522332 AudioNodeList audio_nodes;
tbarzic3ab01a22016-12-17 03:12:532333 AudioNode internal_speaker = GenerateAudioNode(kInternalSpeaker);
hychaofe100d542016-01-12 10:17:522334 audio_nodes.push_back(internal_speaker);
tbarzic3ab01a22016-12-17 03:12:532335 AudioNode usb_headset = GenerateAudioNode(kUSBHeadphone1);
hychaofe100d542016-01-12 10:17:522336 usb_headset.plugged_time = 80000000;
2337 audio_nodes.push_back(usb_headset);
2338 SetUpCrasAudioHandler(audio_nodes);
2339
2340 // Verify the audio devices size.
2341 AudioDeviceList audio_devices;
2342 cras_audio_handler_->GetAudioDevices(&audio_devices);
2343 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2344
2345 // Initially active node is selected base on priority, so USB headphone
2346 // is selected.
tbarzic3ab01a22016-12-17 03:12:532347 EXPECT_EQ(kUSBHeadphone1->id,
hychaofe100d542016-01-12 10:17:522348 cras_audio_handler_->GetPrimaryActiveOutputNode());
2349
2350 // Change the active device to internal speaker, now USB headphone becomes
2351 // inactive.
tbarzic3ab01a22016-12-17 03:12:532352 AudioDevice speaker(GenerateAudioNode(kInternalSpeaker));
jennyzbd236742016-03-02 20:15:522353 cras_audio_handler_->SwitchToDevice(speaker, true,
2354 CrasAudioHandler::ACTIVATE_BY_USER);
tbarzic3ab01a22016-12-17 03:12:532355 EXPECT_NE(kUSBHeadphone1->id,
hychaofe100d542016-01-12 10:17:522356 cras_audio_handler_->GetPrimaryActiveOutputNode());
2357
2358 // Unplug USB headset.
2359 audio_nodes.clear();
2360 internal_speaker.active = true;
2361 audio_nodes.push_back(internal_speaker);
2362 ChangeAudioNodes(audio_nodes);
tbarzic3ab01a22016-12-17 03:12:532363 EXPECT_EQ(kInternalSpeaker->id,
hychaofe100d542016-01-12 10:17:522364 cras_audio_handler_->GetPrimaryActiveOutputNode());
2365
2366 // Plug the same USB headset back, id is different, but stable_device_id
2367 // remains the same.
2368 usb_headset.active = false;
2369 usb_headset.id = 98765;
2370 audio_nodes.push_back(usb_headset);
2371 ChangeAudioNodes(audio_nodes);
2372
2373 // Since USB headset was inactive before it was unplugged, it won't be
2374 // selected as active after it's plugged in again.
tbarzic3ab01a22016-12-17 03:12:532375 EXPECT_EQ(kInternalSpeaker->id,
hychaofe100d542016-01-12 10:17:522376 cras_audio_handler_->GetPrimaryActiveOutputNode());
2377
2378 // Plug the second USB headset.
tbarzic3ab01a22016-12-17 03:12:532379 AudioNode usb_headset2 = GenerateAudioNode(kUSBHeadphone2);
hychaofe100d542016-01-12 10:17:522380 usb_headset2.plugged_time = 80000001;
2381 audio_nodes.push_back(usb_headset2);
2382 ChangeAudioNodes(audio_nodes);
2383
2384 // Since the second USB device is new, it's selected as the active device
2385 // by its priority.
tbarzic3ab01a22016-12-17 03:12:532386 EXPECT_EQ(kUSBHeadphone2->id,
hychaofe100d542016-01-12 10:17:522387 cras_audio_handler_->GetPrimaryActiveOutputNode());
2388
2389 // Unplug the second USB headset.
2390 audio_nodes.clear();
2391 internal_speaker.active = false;
2392 audio_nodes.push_back(internal_speaker);
2393 audio_nodes.push_back(usb_headset);
2394 ChangeAudioNodes(audio_nodes);
2395
2396 // There is no active node after USB2 unplugged, the 1st USB got selected
2397 // by its priority.
2398 EXPECT_EQ(usb_headset.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2399
jennyzbd236742016-03-02 20:15:522400 audio_nodes.clear();
2401 internal_speaker.active = false;
2402 audio_nodes.push_back(internal_speaker);
2403 usb_headset.active = true;
2404 audio_nodes.push_back(usb_headset);
hychaofe100d542016-01-12 10:17:522405 usb_headset2.active = false;
2406 usb_headset2.plugged_time = 80000002;
2407 audio_nodes.push_back(usb_headset2);
2408 ChangeAudioNodes(audio_nodes);
2409
2410 // Plug the second USB again. Since it was the active node before it got
2411 // unplugged, it is now selected as the active node.
2412 EXPECT_EQ(usb_headset2.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2413}
2414
jennyzbd236742016-03-02 20:15:522415// Test the device new session case, either via reboot or logout, if there
2416// is an active device in the previous session, that device should still
2417// be set as active after the new session starts.
tbarzic3ab01a22016-12-17 03:12:532418TEST_P(CrasAudioHandlerTest, PersistActiveDeviceAcrossSession) {
jennyzbd236742016-03-02 20:15:522419 // Set the active device to internal speaker before the session starts.
tbarzic3ab01a22016-12-17 03:12:532420 AudioNodeList audio_nodes =
2421 GenerateAudioNodeList({kInternalSpeaker, kHeadphone});
jennyzbd236742016-03-02 20:15:522422 SetupCrasAudioHandlerWithActiveNodeInPref(
tbarzic3ab01a22016-12-17 03:12:532423 audio_nodes, audio_nodes,
2424 AudioDevice(GenerateAudioNode(kInternalSpeaker)), true);
jennyzbd236742016-03-02 20:15:522425
2426 // Verify the audio devices size.
2427 AudioDeviceList audio_devices;
2428 cras_audio_handler_->GetAudioDevices(&audio_devices);
2429 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2430
2431 // Verify the active device is the internal speaker, which is of a lower
2432 // priority, but selected as active since it was the active device previously.
tbarzic3ab01a22016-12-17 03:12:532433 EXPECT_EQ(kInternalSpeaker->id,
jennyzbd236742016-03-02 20:15:522434 cras_audio_handler_->GetPrimaryActiveOutputNode());
2435}
2436
tbarzic3ab01a22016-12-17 03:12:532437TEST_P(CrasAudioHandlerTest, PersistActiveSpeakerAcrossReboot) {
jennyzbd236742016-03-02 20:15:522438 // Simulates the device was shut down with three audio devices, and
2439 // internal speaker being the active one selected by user.
tbarzic3ab01a22016-12-17 03:12:532440 AudioNodeList audio_nodes_in_pref =
2441 GenerateAudioNodeList({kInternalSpeaker, kHeadphone, kUSBHeadphone1});
jennyzbd236742016-03-02 20:15:522442
2443 // Simulate the first NodesChanged signal coming with only one node.
tbarzic3ab01a22016-12-17 03:12:532444 AudioNodeList audio_nodes = GenerateAudioNodeList({kUSBHeadphone1});
jennyzbd236742016-03-02 20:15:522445
2446 SetupCrasAudioHandlerWithActiveNodeInPref(
tbarzic3ab01a22016-12-17 03:12:532447 audio_nodes, audio_nodes_in_pref,
2448 AudioDevice(GenerateAudioNode(kInternalSpeaker)), true);
jennyzbd236742016-03-02 20:15:522449
2450 // Verify the usb headphone has been made active.
2451 AudioDeviceList audio_devices;
2452 cras_audio_handler_->GetAudioDevices(&audio_devices);
2453 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
tbarzic3ab01a22016-12-17 03:12:532454 EXPECT_EQ(kUSBHeadphone1->id,
jennyzbd236742016-03-02 20:15:522455 cras_audio_handler_->GetPrimaryActiveOutputNode());
2456
2457 // Simulate another NodesChanged signal coming later with all ndoes.
tbarzic3ab01a22016-12-17 03:12:532458 audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
2459 audio_nodes.push_back(GenerateAudioNode(kHeadphone));
jennyzbd236742016-03-02 20:15:522460 ChangeAudioNodes(audio_nodes);
2461
2462 // Verify the active output has been restored to internal speaker.
2463 cras_audio_handler_->GetAudioDevices(&audio_devices);
2464 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
tbarzic3ab01a22016-12-17 03:12:532465 EXPECT_EQ(kInternalSpeaker->id,
jennyzbd236742016-03-02 20:15:522466 cras_audio_handler_->GetPrimaryActiveOutputNode());
2467}
2468
jennyz16e103e2017-03-31 18:15:252469// crbug.com/698809. User plug in USB speaker, then unplug it, leave
2470// internal speaker as active device. Power down, plug in USB speaker again.
2471// When the device powers up again, the first NodesChanged signal comes with
2472// only USB speaker; followed by another NodesChanged signal with internal
2473// speaker added.
2474TEST_P(CrasAudioHandlerTest, USBShouldBeActiveAfterReboot) {
2475 // Start with both interanl speaker and USB speaker.
2476 AudioNodeList audio_nodes =
2477 GenerateAudioNodeList({kInternalSpeaker, kUSBHeadphone1});
2478
2479 SetUpCrasAudioHandler(audio_nodes);
2480
2481 // Verify the usb headphone has been made active by priority.
2482 AudioDeviceList audio_devices;
2483 cras_audio_handler_->GetAudioDevices(&audio_devices);
2484 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2485 EXPECT_EQ(kUSBHeadphone1->id,
2486 cras_audio_handler_->GetPrimaryActiveOutputNode());
2487
2488 // Remove USB headphone.
2489 audio_nodes.clear();
2490 audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
2491 ChangeAudioNodes(audio_nodes);
2492 // Verify the internal speaker becomes the active device by priority.
2493 cras_audio_handler_->GetAudioDevices(&audio_devices);
2494 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2495 EXPECT_EQ(kInternalSpeaker->id,
2496 cras_audio_handler_->GetPrimaryActiveOutputNode());
2497
2498 // Simulate after power off, plug in usb header phone, then power on.
2499 // The first NodesChanged signal sends usb headphone only.
2500 audio_nodes.clear();
2501 audio_nodes.push_back(GenerateAudioNode(kUSBHeadphone1));
2502 ChangeAudioNodes(audio_nodes);
2503 // Verify the usb headerphone becomes the active device by priority.
2504 cras_audio_handler_->GetAudioDevices(&audio_devices);
2505 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2506 EXPECT_EQ(kUSBHeadphone1->id,
2507 cras_audio_handler_->GetPrimaryActiveOutputNode());
2508
2509 // Simulate the second NodesChanged signal comes with internal speaker added.
2510 audio_nodes.clear();
2511 AudioNode usb_headphone = GenerateAudioNode(kUSBHeadphone1);
2512 usb_headphone.active = true;
2513 audio_nodes.push_back(usb_headphone);
2514 audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
2515 ChangeAudioNodes(audio_nodes);
2516 // Verify the usb headerphone is still the active device.
2517 cras_audio_handler_->GetAudioDevices(&audio_devices);
2518 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2519 EXPECT_EQ(kUSBHeadphone1->id,
2520 cras_audio_handler_->GetPrimaryActiveOutputNode());
2521}
2522
jennyz6d5579b2016-07-28 17:49:112523// Test the corner case that headphone is plugged in for the first time on
2524// a cros device after the device is shutdown.
2525// crbug.com/622045.
tbarzic3ab01a22016-12-17 03:12:532526TEST_P(CrasAudioHandlerTest, PlugInHeadphoneFirstTimeAfterPowerDown) {
jennyz6d5579b2016-07-28 17:49:112527 // Simulate plugging headphone for the first on a cros device after it is
2528 // powered down. Internal speaker is set up in audio prefs as active
2529 // before the new cros session starts.
tbarzic3ab01a22016-12-17 03:12:532530 AudioNodeList audio_nodes_in_pref = GenerateAudioNodeList({kInternalSpeaker});
jennyz6d5579b2016-07-28 17:49:112531
tbarzic3ab01a22016-12-17 03:12:532532 AudioNodeList audio_nodes =
2533 GenerateAudioNodeList({kInternalSpeaker, kHeadphone});
jennyz6d5579b2016-07-28 17:49:112534
2535 SetupCrasAudioHandlerWithActiveNodeInPref(
tbarzic3ab01a22016-12-17 03:12:532536 audio_nodes, audio_nodes_in_pref,
2537 AudioDevice(GenerateAudioNode(kInternalSpeaker)), false);
jennyz6d5579b2016-07-28 17:49:112538
2539 // Verify the audio devices size.
2540 AudioDeviceList audio_devices;
2541 cras_audio_handler_->GetAudioDevices(&audio_devices);
2542 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2543
2544 // Verify headphone becomes the active output.
tbarzic3ab01a22016-12-17 03:12:532545 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
jennyz6d5579b2016-07-28 17:49:112546}
2547
tbarzic3ab01a22016-12-17 03:12:532548TEST_P(CrasAudioHandlerTest,
jennyzbd236742016-03-02 20:15:522549 PersistActiveUsbHeadphoneAcrossRebootUsbComeLater) {
2550 // Simulates the device was shut down with three audio devices, and
2551 // usb headphone being the active one selected by priority.
tbarzic3ab01a22016-12-17 03:12:532552 AudioNodeList audio_nodes_in_pref =
2553 GenerateAudioNodeList({kInternalSpeaker, kHeadphone, kUSBHeadphone1});
jennyzbd236742016-03-02 20:15:522554
2555 // Simulate the first NodesChanged signal coming with only internal speaker
2556 // and the headphone.
tbarzic3ab01a22016-12-17 03:12:532557 AudioNodeList audio_nodes =
2558 GenerateAudioNodeList({kInternalSpeaker, kHeadphone});
jennyzbd236742016-03-02 20:15:522559
tbarzic3ab01a22016-12-17 03:12:532560 SetupCrasAudioHandlerWithActiveNodeInPref(
2561 audio_nodes, audio_nodes_in_pref,
2562 AudioDevice(GenerateAudioNode(kUSBHeadphone1)), false);
jennyzbd236742016-03-02 20:15:522563
2564 // Verify the headphone has been made active.
2565 AudioDeviceList audio_devices;
2566 cras_audio_handler_->GetAudioDevices(&audio_devices);
2567 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
tbarzic3ab01a22016-12-17 03:12:532568 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
jennyzbd236742016-03-02 20:15:522569
2570 // Simulate USB node comes later with all ndoes.
tbarzic3ab01a22016-12-17 03:12:532571 AudioNode usb_node = GenerateAudioNode(kUSBHeadphone1);
jennyzbd236742016-03-02 20:15:522572 usb_node.plugged_time = 80000000;
2573 audio_nodes.push_back(usb_node);
2574 ChangeAudioNodes(audio_nodes);
2575
2576 // Verify the active output has been restored to usb headphone.
2577 cras_audio_handler_->GetAudioDevices(&audio_devices);
2578 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
tbarzic3ab01a22016-12-17 03:12:532579 EXPECT_EQ(kUSBHeadphone1->id,
jennyzbd236742016-03-02 20:15:522580 cras_audio_handler_->GetPrimaryActiveOutputNode());
2581}
2582
tbarzic3ab01a22016-12-17 03:12:532583TEST_P(CrasAudioHandlerTest,
jennyzbd236742016-03-02 20:15:522584 PersistActiveUsbHeadphoneAcrossRebootUsbComeFirst) {
2585 // Simulates the device was shut down with three audio devices, and
2586 // usb headphone being the active one selected by priority.
tbarzic3ab01a22016-12-17 03:12:532587 AudioNodeList audio_nodes_in_pref =
2588 GenerateAudioNodeList({kInternalSpeaker, kHeadphone, kUSBHeadphone1});
jennyzbd236742016-03-02 20:15:522589
2590 // Simulate the first NodesChanged signal coming with only internal speaker
2591 // and the USB headphone.
tbarzic3ab01a22016-12-17 03:12:532592 AudioNodeList audio_nodes =
2593 GenerateAudioNodeList({kInternalSpeaker, kUSBHeadphone1});
jennyzbd236742016-03-02 20:15:522594
tbarzic3ab01a22016-12-17 03:12:532595 SetupCrasAudioHandlerWithActiveNodeInPref(
2596 audio_nodes, audio_nodes_in_pref,
2597 AudioDevice(GenerateAudioNode(kUSBHeadphone1)), false);
jennyzbd236742016-03-02 20:15:522598
2599 // Verify the USB headphone has been made active.
2600 AudioDeviceList audio_devices;
2601 cras_audio_handler_->GetAudioDevices(&audio_devices);
2602 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
tbarzic3ab01a22016-12-17 03:12:532603 EXPECT_EQ(kUSBHeadphone1->id,
jennyzbd236742016-03-02 20:15:522604 cras_audio_handler_->GetPrimaryActiveOutputNode());
2605
2606 // Simulate another NodesChanged signal coming later with all ndoes.
tbarzic3ab01a22016-12-17 03:12:532607 AudioNode headphone_node = GenerateAudioNode(kHeadphone);
jennyzbd236742016-03-02 20:15:522608 headphone_node.plugged_time = 80000000;
2609 audio_nodes.push_back(headphone_node);
2610 ChangeAudioNodes(audio_nodes);
2611
2612 // Verify the active output has been restored to USB headphone.
2613 cras_audio_handler_->GetAudioDevices(&audio_devices);
2614 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
tbarzic3ab01a22016-12-17 03:12:532615 EXPECT_EQ(kUSBHeadphone1->id,
jennyzbd236742016-03-02 20:15:522616 cras_audio_handler_->GetPrimaryActiveOutputNode());
2617}
2618
2619// This covers the crbug.com/586026. Cras lost the active state of the internal
2620// speaker when user unplugs the headphone, which is a bug in cras. However,
2621// chrome code is still resilient and set the internal speaker back to active.
tbarzic3ab01a22016-12-17 03:12:532622TEST_P(CrasAudioHandlerTest, UnplugHeadphoneLostActiveInternalSpeakerByCras) {
jennyzbd236742016-03-02 20:15:522623 // Set up with three nodes.
tbarzic3ab01a22016-12-17 03:12:532624 AudioNodeList audio_nodes =
2625 GenerateAudioNodeList({kInternalSpeaker, kHeadphone, kUSBHeadphone1});
jennyzbd236742016-03-02 20:15:522626 SetUpCrasAudioHandler(audio_nodes);
2627
2628 // Switch the active output to internal speaker.
tbarzic3ab01a22016-12-17 03:12:532629 cras_audio_handler_->SwitchToDevice(
2630 AudioDevice(GenerateAudioNode(kInternalSpeaker)), true,
2631 CrasAudioHandler::ACTIVATE_BY_USER);
jennyzbd236742016-03-02 20:15:522632
2633 // Verify internal speaker has been made active.
2634 AudioDeviceList audio_devices;
2635 cras_audio_handler_->GetAudioDevices(&audio_devices);
2636 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
tbarzic3ab01a22016-12-17 03:12:532637 EXPECT_EQ(kInternalSpeaker->id,
jennyzbd236742016-03-02 20:15:522638 cras_audio_handler_->GetPrimaryActiveOutputNode());
2639
2640 // Simulate unplug the headphone. Cras sends NodesChanged signal with
2641 // both internal speaker and usb headphone being inactive.
2642 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:532643 audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
2644 audio_nodes.push_back(GenerateAudioNode(kUSBHeadphone1));
2645 for (const auto& node : audio_nodes)
2646 ASSERT_FALSE(node.active) << node.id << " expexted to be inactive";
jennyzbd236742016-03-02 20:15:522647 ChangeAudioNodes(audio_nodes);
2648
2649 // Verify the active output is set back to internal speaker.
2650 cras_audio_handler_->GetAudioDevices(&audio_devices);
2651 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
tbarzic3ab01a22016-12-17 03:12:532652 EXPECT_EQ(kInternalSpeaker->id,
jennyzbd236742016-03-02 20:15:522653 cras_audio_handler_->GetPrimaryActiveOutputNode());
2654}
2655
tbarzic3ab01a22016-12-17 03:12:532656TEST_P(CrasAudioHandlerTest, RemoveNonActiveDevice) {
jennyzbd236742016-03-02 20:15:522657 // Set up with three nodes.
tbarzic3ab01a22016-12-17 03:12:532658 AudioNodeList audio_nodes =
2659 GenerateAudioNodeList({kInternalSpeaker, kHeadphone, kUSBHeadphone1});
jennyzbd236742016-03-02 20:15:522660 SetUpCrasAudioHandler(audio_nodes);
2661
2662 // Switch the active output to internal speaker.
tbarzic3ab01a22016-12-17 03:12:532663 cras_audio_handler_->SwitchToDevice(
2664 AudioDevice(GenerateAudioNode(kInternalSpeaker)), true,
2665 CrasAudioHandler::ACTIVATE_BY_USER);
jennyzbd236742016-03-02 20:15:522666
2667 // Verify internal speaker has been made active.
2668 AudioDeviceList audio_devices;
2669 cras_audio_handler_->GetAudioDevices(&audio_devices);
2670 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
tbarzic3ab01a22016-12-17 03:12:532671 EXPECT_EQ(kInternalSpeaker->id,
jennyzbd236742016-03-02 20:15:522672 cras_audio_handler_->GetPrimaryActiveOutputNode());
2673
2674 // Remove headphone, which is an non-active device.
2675 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:532676 AudioNode speaker = GenerateAudioNode(kInternalSpeaker);
jennyzbd236742016-03-02 20:15:522677 speaker.active = true;
2678 audio_nodes.push_back(speaker);
tbarzic3ab01a22016-12-17 03:12:532679 AudioNode usb_headphone = GenerateAudioNode(kUSBHeadphone1);
jennyzbd236742016-03-02 20:15:522680 usb_headphone.active = false;
2681 audio_nodes.push_back(usb_headphone);
2682
2683 ChangeAudioNodes(audio_nodes);
2684
2685 // Verify the active output remains as internal speaker.
2686 cras_audio_handler_->GetAudioDevices(&audio_devices);
2687 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
tbarzic3ab01a22016-12-17 03:12:532688 EXPECT_EQ(kInternalSpeaker->id,
jennyzbd236742016-03-02 20:15:522689 cras_audio_handler_->GetPrimaryActiveOutputNode());
2690}
2691
tbarzic3ab01a22016-12-17 03:12:532692TEST_P(CrasAudioHandlerTest, ChangeActiveNodesHotrodInit) {
tbarziccae06e2a2017-01-17 23:14:472693 // This simulates a typical hotrod audio device configuration.
tbarzic3ab01a22016-12-17 03:12:532694 AudioNodeList audio_nodes = GenerateAudioNodeList(
2695 {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerOutput2,
2696 kUSBJabraSpeakerInput1, kUSBJabraSpeakerInput2, kUSBCameraInput});
jennyzb47947f2014-09-25 00:42:042697 SetUpCrasAudioHandler(audio_nodes);
2698
2699 // Verify the audio devices size.
2700 AudioDeviceList audio_devices;
2701 cras_audio_handler_->GetAudioDevices(&audio_devices);
2702 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2703
jennyz2cb9eb12014-11-05 19:21:052704 // Verify only the 1st jabra speaker's output and input are selected as active
2705 // nodes by CrasAudioHandler.
jennyzb47947f2014-09-25 00:42:042706 AudioDevice active_output;
2707 EXPECT_TRUE(
2708 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
jennyz2cb9eb12014-11-05 19:21:052709 EXPECT_EQ(2, GetActiveDeviceCount());
jennyzb47947f2014-09-25 00:42:042710 AudioDevice primary_active_device;
2711 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(
2712 &primary_active_device));
tbarzic3ab01a22016-12-17 03:12:532713 EXPECT_EQ(kUSBJabraSpeakerOutput1->id, primary_active_device.id);
2714 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
jennyz2cb9eb12014-11-05 19:21:052715 cras_audio_handler_->GetPrimaryActiveInputNode());
2716
2717 // Set both jabra speakers's input and output nodes to active, this simulate
2718 // the call sent by hotrod initialization process.
2719 test_observer_->reset_active_output_node_changed_count();
2720 test_observer_->reset_active_input_node_changed_count();
tbarziccae06e2a2017-01-17 23:14:472721 cras_audio_handler_->ChangeActiveNodes(
2722 {kUSBJabraSpeakerOutput1->id, kUSBJabraSpeakerOutput2->id,
2723 kUSBJabraSpeakerInput1->id, kUSBJabraSpeakerInput2->id});
jennyz2cb9eb12014-11-05 19:21:052724
2725 // Verify both jabra speakers' input/output nodes are made active.
2726 // num_active_nodes = GetActiveDeviceCount();
2727 EXPECT_EQ(4, GetActiveDeviceCount());
2728 const AudioDevice* active_output_1 =
tbarzic3ab01a22016-12-17 03:12:532729 GetDeviceFromId(kUSBJabraSpeakerOutput1->id);
jennyz2cb9eb12014-11-05 19:21:052730 EXPECT_TRUE(active_output_1->active);
2731 const AudioDevice* active_output_2 =
tbarzic3ab01a22016-12-17 03:12:532732 GetDeviceFromId(kUSBJabraSpeakerOutput2->id);
jennyz2cb9eb12014-11-05 19:21:052733 EXPECT_TRUE(active_output_2->active);
2734 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(
2735 &primary_active_device));
tbarzic3ab01a22016-12-17 03:12:532736 EXPECT_EQ(kUSBJabraSpeakerOutput1->id, primary_active_device.id);
jennyz2cb9eb12014-11-05 19:21:052737 const AudioDevice* active_input_1 =
tbarzic3ab01a22016-12-17 03:12:532738 GetDeviceFromId(kUSBJabraSpeakerInput1->id);
jennyz2cb9eb12014-11-05 19:21:052739 EXPECT_TRUE(active_input_1->active);
2740 const AudioDevice* active_input_2 =
tbarzic3ab01a22016-12-17 03:12:532741 GetDeviceFromId(kUSBJabraSpeakerInput2->id);
jennyz2cb9eb12014-11-05 19:21:052742 EXPECT_TRUE(active_input_2->active);
tbarzic3ab01a22016-12-17 03:12:532743 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
jennyz2cb9eb12014-11-05 19:21:052744 cras_audio_handler_->GetPrimaryActiveInputNode());
2745
2746 // Verify only 1 ActiveOutputNodeChanged notification has been sent out
2747 // by calling ChangeActiveNodes.
2748 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
2749 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:042750
2751 // Verify all active devices are the not muted and their volume values are
2752 // the same.
2753 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
jennyz2cb9eb12014-11-05 19:21:052754 EXPECT_FALSE(
tbarzic3ab01a22016-12-17 03:12:532755 cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput1->id));
jennyz2cb9eb12014-11-05 19:21:052756 EXPECT_FALSE(
tbarzic3ab01a22016-12-17 03:12:532757 cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput2->id));
jennyz2cb9eb12014-11-05 19:21:052758 EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(),
2759 cras_audio_handler_->GetOutputVolumePercentForDevice(
tbarzic3ab01a22016-12-17 03:12:532760 kUSBJabraSpeakerOutput1->id));
jennyz2cb9eb12014-11-05 19:21:052761 EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(),
2762 cras_audio_handler_->GetOutputVolumePercentForDevice(
tbarzic3ab01a22016-12-17 03:12:532763 kUSBJabraSpeakerOutput2->id));
jennyzb47947f2014-09-25 00:42:042764
2765 // Adjust the volume of output devices, verify all active nodes are set to
2766 // the same volume.
2767 cras_audio_handler_->SetOutputVolumePercent(25);
2768 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercent());
jennyz2cb9eb12014-11-05 19:21:052769 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercentForDevice(
tbarzic3ab01a22016-12-17 03:12:532770 kUSBJabraSpeakerOutput1->id));
jennyz2cb9eb12014-11-05 19:21:052771 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercentForDevice(
tbarzic3ab01a22016-12-17 03:12:532772 kUSBJabraSpeakerOutput2->id));
jennyz2cb9eb12014-11-05 19:21:052773}
jennyzb47947f2014-09-25 00:42:042774
tbarziccae06e2a2017-01-17 23:14:472775TEST_P(CrasAudioHandlerTest, SetActiveNodesHotrodInit) {
2776 // This simulates a typical hotrod audio device configuration.
2777 AudioNodeList audio_nodes = GenerateAudioNodeList(
2778 {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerOutput2,
2779 kUSBJabraSpeakerInput1, kUSBJabraSpeakerInput2, kUSBCameraInput});
2780 SetUpCrasAudioHandler(audio_nodes);
2781
2782 // Verify the audio devices size.
2783 AudioDeviceList audio_devices;
2784 cras_audio_handler_->GetAudioDevices(&audio_devices);
2785 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2786
2787 // Verify only the 1st jabra speaker's output and input are selected as active
2788 // nodes by CrasAudioHandler.
2789 AudioDevice active_output;
2790 EXPECT_TRUE(
2791 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
2792 EXPECT_EQ(2, GetActiveDeviceCount());
2793 AudioDevice primary_active_device;
2794 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(
2795 &primary_active_device));
2796 EXPECT_EQ(kUSBJabraSpeakerOutput1->id, primary_active_device.id);
2797 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
2798 cras_audio_handler_->GetPrimaryActiveInputNode());
2799
2800 // Set both jabra speakers's input and output nodes to active, this simulate
2801 // the call sent by hotrod initialization process.
2802 test_observer_->reset_active_output_node_changed_count();
2803 test_observer_->reset_active_input_node_changed_count();
2804
2805 cras_audio_handler_->SetActiveInputNodes(
2806 {kUSBJabraSpeakerInput1->id, kUSBJabraSpeakerInput2->id});
2807
2808 cras_audio_handler_->SetActiveOutputNodes(
2809 {kUSBJabraSpeakerOutput1->id, kUSBJabraSpeakerOutput2->id});
2810
2811 // Verify both jabra speakers' input/output nodes are made active.
2812 // num_active_nodes = GetActiveDeviceCount();
2813 EXPECT_EQ(4, GetActiveDeviceCount());
2814 const AudioDevice* active_output_1 =
2815 GetDeviceFromId(kUSBJabraSpeakerOutput1->id);
2816 EXPECT_TRUE(active_output_1->active);
2817 const AudioDevice* active_output_2 =
2818 GetDeviceFromId(kUSBJabraSpeakerOutput2->id);
2819 EXPECT_TRUE(active_output_2->active);
2820 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(
2821 &primary_active_device));
2822 EXPECT_EQ(kUSBJabraSpeakerOutput1->id, primary_active_device.id);
2823 const AudioDevice* active_input_1 =
2824 GetDeviceFromId(kUSBJabraSpeakerInput1->id);
2825 EXPECT_TRUE(active_input_1->active);
2826 const AudioDevice* active_input_2 =
2827 GetDeviceFromId(kUSBJabraSpeakerInput2->id);
2828 EXPECT_TRUE(active_input_2->active);
2829 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
2830 cras_audio_handler_->GetPrimaryActiveInputNode());
2831
2832 // Verify only 1 ActiveOutputNodeChanged notification has been sent out
2833 // by calling SetActiveNodes.
2834 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
2835 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
2836
2837 // Verify all active devices are the not muted and their volume values are
2838 // the same.
2839 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
2840 EXPECT_FALSE(
2841 cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput1->id));
2842 EXPECT_FALSE(
2843 cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput2->id));
2844 EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(),
2845 cras_audio_handler_->GetOutputVolumePercentForDevice(
2846 kUSBJabraSpeakerOutput1->id));
2847 EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(),
2848 cras_audio_handler_->GetOutputVolumePercentForDevice(
2849 kUSBJabraSpeakerOutput2->id));
2850
2851 // Adjust the volume of output devices, verify all active nodes are set to
2852 // the same volume.
2853 cras_audio_handler_->SetOutputVolumePercent(25);
2854 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercent());
2855 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercentForDevice(
2856 kUSBJabraSpeakerOutput1->id));
2857 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercentForDevice(
2858 kUSBJabraSpeakerOutput2->id));
2859}
2860
tbarzic3ab01a22016-12-17 03:12:532861TEST_P(CrasAudioHandlerTest, ChangeVolumeHotrodDualSpeakersWithDelayedSignals) {
2862 AudioNodeList audio_nodes = GenerateAudioNodeList(
2863 {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerOutput2});
jennyz96526d02016-06-22 17:03:432864 SetUpCrasAudioHandler(audio_nodes);
2865
2866 // Verify the audio devices size.
2867 AudioDeviceList audio_devices;
2868 cras_audio_handler_->GetAudioDevices(&audio_devices);
2869 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2870
2871 // Set both jabra speakers nodes to active, this simulate
2872 // the call sent by hotrod initialization process.
2873 test_observer_->reset_active_output_node_changed_count();
tbarziccae06e2a2017-01-17 23:14:472874 cras_audio_handler_->ChangeActiveNodes(
2875 {kUSBJabraSpeakerOutput1->id, kUSBJabraSpeakerOutput2->id});
jennyz96526d02016-06-22 17:03:432876
2877 // Verify both jabra speakers are made active.
2878 EXPECT_EQ(2, GetActiveDeviceCount());
2879 const AudioDevice* active_output_1 =
tbarzic3ab01a22016-12-17 03:12:532880 GetDeviceFromId(kUSBJabraSpeakerOutput1->id);
jennyz96526d02016-06-22 17:03:432881 EXPECT_TRUE(active_output_1->active);
2882 const AudioDevice* active_output_2 =
tbarzic3ab01a22016-12-17 03:12:532883 GetDeviceFromId(kUSBJabraSpeakerOutput2->id);
jennyz96526d02016-06-22 17:03:432884 EXPECT_TRUE(active_output_2->active);
2885
2886 // Verify all active devices are the not muted and their volume values are
2887 // the same.
2888 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
2889 EXPECT_FALSE(
tbarzic3ab01a22016-12-17 03:12:532890 cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput1->id));
jennyz96526d02016-06-22 17:03:432891 EXPECT_FALSE(
tbarzic3ab01a22016-12-17 03:12:532892 cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput2->id));
jennyz96526d02016-06-22 17:03:432893 EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(),
2894 cras_audio_handler_->GetOutputVolumePercentForDevice(
tbarzic3ab01a22016-12-17 03:12:532895 kUSBJabraSpeakerOutput1->id));
jennyz96526d02016-06-22 17:03:432896 EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(),
2897 cras_audio_handler_->GetOutputVolumePercentForDevice(
tbarzic3ab01a22016-12-17 03:12:532898 kUSBJabraSpeakerOutput2->id));
jennyz96526d02016-06-22 17:03:432899 const int kDefaultVolume = 75;
2900 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2901
2902 // Disable the auto OutputNodeVolumeChanged signal.
2903 fake_cras_audio_client_->set_notify_volume_change_with_delay(true);
2904 test_observer_->reset_output_volume_changed_count();
2905
2906 // Adjust the volume of output devices continuously.
2907 cras_audio_handler_->SetOutputVolumePercent(20);
2908 cras_audio_handler_->SetOutputVolumePercent(30);
2909
2910 // Sends delayed OutputNodeVolumeChanged signals.
2911 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
tbarzic3ab01a22016-12-17 03:12:532912 kUSBJabraSpeakerOutput2->id, 20);
jennyz96526d02016-06-22 17:03:432913 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
tbarzic3ab01a22016-12-17 03:12:532914 kUSBJabraSpeakerOutput1->id, 20);
jennyz96526d02016-06-22 17:03:432915 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
tbarzic3ab01a22016-12-17 03:12:532916 kUSBJabraSpeakerOutput2->id, 30);
jennyz96526d02016-06-22 17:03:432917 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
tbarzic3ab01a22016-12-17 03:12:532918 kUSBJabraSpeakerOutput1->id, 30);
jennyz96526d02016-06-22 17:03:432919
2920 // Verify that both speakers are set to the designated volume level after
2921 // receiving all delayed signals.
2922 EXPECT_EQ(4, test_observer_->output_volume_changed_count());
2923 EXPECT_EQ(30, cras_audio_handler_->GetOutputVolumePercent());
2924 EXPECT_EQ(30, cras_audio_handler_->GetOutputVolumePercentForDevice(
tbarzic3ab01a22016-12-17 03:12:532925 kUSBJabraSpeakerOutput1->id));
jennyz96526d02016-06-22 17:03:432926 EXPECT_EQ(30, cras_audio_handler_->GetOutputVolumePercentForDevice(
tbarzic3ab01a22016-12-17 03:12:532927 kUSBJabraSpeakerOutput2->id));
jennyz96526d02016-06-22 17:03:432928}
2929
tbarzic3ab01a22016-12-17 03:12:532930TEST_P(CrasAudioHandlerTest, ChangeActiveNodesHotrodInitWithCameraInputActive) {
2931 AudioNodeList audio_nodes = GenerateAudioNodeList(
2932 {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerOutput2,
2933 kUSBJabraSpeakerInput1, kUSBJabraSpeakerInput2});
jennyz2cb9eb12014-11-05 19:21:052934 // Make the camera input to be plugged in later than jabra's input.
tbarzic3ab01a22016-12-17 03:12:532935 AudioNode usb_camera = GenerateAudioNode(kUSBCameraInput);
jennyz2cb9eb12014-11-05 19:21:052936 usb_camera.plugged_time = 10000000;
2937 audio_nodes.push_back(usb_camera);
2938 SetUpCrasAudioHandler(audio_nodes);
2939
2940 // Verify the audio devices size.
2941 AudioDeviceList audio_devices;
2942 cras_audio_handler_->GetAudioDevices(&audio_devices);
2943 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2944
2945 // Verify the 1st jabra speaker's output is selected as active output
2946 // node and camera's input is selected active input by CrasAudioHandler.
2947 EXPECT_EQ(2, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:532948 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
jennyz2cb9eb12014-11-05 19:21:052949 cras_audio_handler_->GetPrimaryActiveOutputNode());
tbarzic3ab01a22016-12-17 03:12:532950 EXPECT_EQ(kUSBCameraInput->id,
jennyz2cb9eb12014-11-05 19:21:052951 cras_audio_handler_->GetPrimaryActiveInputNode());
2952
2953 // Set both jabra speakers's input and output nodes to active, this simulates
2954 // the call sent by hotrod initialization process.
2955 test_observer_->reset_active_output_node_changed_count();
2956 test_observer_->reset_active_input_node_changed_count();
tbarziccae06e2a2017-01-17 23:14:472957 cras_audio_handler_->ChangeActiveNodes(
2958 {kUSBJabraSpeakerOutput1->id, kUSBJabraSpeakerOutput2->id,
2959 kUSBJabraSpeakerInput1->id, kUSBJabraSpeakerInput2->id});
2960
2961 // Verify both jabra speakers' input/output nodes are made active.
2962 // num_active_nodes = GetActiveDeviceCount();
2963 EXPECT_EQ(4, GetActiveDeviceCount());
2964 const AudioDevice* active_output_1 =
2965 GetDeviceFromId(kUSBJabraSpeakerOutput1->id);
2966 EXPECT_TRUE(active_output_1->active);
2967 const AudioDevice* active_output_2 =
2968 GetDeviceFromId(kUSBJabraSpeakerOutput2->id);
2969 EXPECT_TRUE(active_output_2->active);
2970 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
2971 cras_audio_handler_->GetPrimaryActiveOutputNode());
2972 const AudioDevice* active_input_1 =
2973 GetDeviceFromId(kUSBJabraSpeakerInput1->id);
2974 EXPECT_TRUE(active_input_1->active);
2975 const AudioDevice* active_input_2 =
2976 GetDeviceFromId(kUSBJabraSpeakerInput2->id);
2977 EXPECT_TRUE(active_input_2->active);
2978 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
2979 cras_audio_handler_->GetPrimaryActiveInputNode());
2980
2981 // Verify only 1 ActiveOutputNodeChanged notification has been sent out
2982 // by calling ChangeActiveNodes.
2983 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
2984 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
2985}
2986
2987TEST_P(CrasAudioHandlerTest, SetActiveNodesHotrodInitWithCameraInputActive) {
2988 AudioNodeList audio_nodes = GenerateAudioNodeList(
2989 {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerOutput2,
2990 kUSBJabraSpeakerInput1, kUSBJabraSpeakerInput2});
2991 // Make the camera input to be plugged in later than jabra's input.
2992 AudioNode usb_camera = GenerateAudioNode(kUSBCameraInput);
2993 usb_camera.plugged_time = 10000000;
2994 audio_nodes.push_back(usb_camera);
2995 SetUpCrasAudioHandler(audio_nodes);
2996
2997 // Verify the audio devices size.
2998 AudioDeviceList audio_devices;
2999 cras_audio_handler_->GetAudioDevices(&audio_devices);
3000 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3001
3002 // Verify the 1st jabra speaker's output is selected as active output
3003 // node and camera's input is selected active input by CrasAudioHandler.
3004 EXPECT_EQ(2, GetActiveDeviceCount());
3005 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
3006 cras_audio_handler_->GetPrimaryActiveOutputNode());
3007 EXPECT_EQ(kUSBCameraInput->id,
3008 cras_audio_handler_->GetPrimaryActiveInputNode());
3009
3010 // Set both jabra speakers's input and output nodes to active, this simulates
3011 // the call sent by hotrod initialization process.
3012 test_observer_->reset_active_output_node_changed_count();
3013 test_observer_->reset_active_input_node_changed_count();
3014
3015 cras_audio_handler_->SetActiveOutputNodes(
3016 {kUSBJabraSpeakerOutput1->id, kUSBJabraSpeakerOutput2->id});
3017
3018 cras_audio_handler_->SetActiveInputNodes(
3019 {kUSBJabraSpeakerInput1->id, kUSBJabraSpeakerInput2->id});
jennyz2cb9eb12014-11-05 19:21:053020
3021 // Verify both jabra speakers' input/output nodes are made active.
3022 // num_active_nodes = GetActiveDeviceCount();
3023 EXPECT_EQ(4, GetActiveDeviceCount());
3024 const AudioDevice* active_output_1 =
tbarzic3ab01a22016-12-17 03:12:533025 GetDeviceFromId(kUSBJabraSpeakerOutput1->id);
jennyz2cb9eb12014-11-05 19:21:053026 EXPECT_TRUE(active_output_1->active);
3027 const AudioDevice* active_output_2 =
tbarzic3ab01a22016-12-17 03:12:533028 GetDeviceFromId(kUSBJabraSpeakerOutput2->id);
jennyz2cb9eb12014-11-05 19:21:053029 EXPECT_TRUE(active_output_2->active);
tbarzic3ab01a22016-12-17 03:12:533030 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
jennyz2cb9eb12014-11-05 19:21:053031 cras_audio_handler_->GetPrimaryActiveOutputNode());
3032 const AudioDevice* active_input_1 =
tbarzic3ab01a22016-12-17 03:12:533033 GetDeviceFromId(kUSBJabraSpeakerInput1->id);
jennyz2cb9eb12014-11-05 19:21:053034 EXPECT_TRUE(active_input_1->active);
3035 const AudioDevice* active_input_2 =
tbarzic3ab01a22016-12-17 03:12:533036 GetDeviceFromId(kUSBJabraSpeakerInput2->id);
jennyz2cb9eb12014-11-05 19:21:053037 EXPECT_TRUE(active_input_2->active);
tbarzic3ab01a22016-12-17 03:12:533038 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
jennyz2cb9eb12014-11-05 19:21:053039 cras_audio_handler_->GetPrimaryActiveInputNode());
3040
3041 // Verify only 1 ActiveOutputNodeChanged notification has been sent out
3042 // by calling ChangeActiveNodes.
3043 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
3044 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
3045}
3046
tbarzic3ab01a22016-12-17 03:12:533047TEST_P(CrasAudioHandlerTest, ChangeActiveNodesWithFewerActives) {
3048 AudioNodeList audio_nodes = GenerateAudioNodeList(
3049 {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerOutput2});
jennyz2cb9eb12014-11-05 19:21:053050 SetUpCrasAudioHandler(audio_nodes);
3051
3052 // Verify the audio devices size.
3053 AudioDeviceList audio_devices;
3054 cras_audio_handler_->GetAudioDevices(&audio_devices);
3055 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3056
3057 // Set all three nodes to be active.
tbarziccae06e2a2017-01-17 23:14:473058 cras_audio_handler_->ChangeActiveNodes({kHDMIOutput->id,
3059 kUSBJabraSpeakerOutput1->id,
3060 kUSBJabraSpeakerOutput2->id});
jennyz2cb9eb12014-11-05 19:21:053061
3062 // Verify all three nodes are active.
3063 EXPECT_EQ(3, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:533064 const AudioDevice* active_output_1 = GetDeviceFromId(kHDMIOutput->id);
jennyz2cb9eb12014-11-05 19:21:053065 EXPECT_TRUE(active_output_1->active);
3066 const AudioDevice* active_output_2 =
tbarzic3ab01a22016-12-17 03:12:533067 GetDeviceFromId(kUSBJabraSpeakerOutput1->id);
jennyz2cb9eb12014-11-05 19:21:053068 EXPECT_TRUE(active_output_2->active);
3069 const AudioDevice* active_output_3 =
tbarzic3ab01a22016-12-17 03:12:533070 GetDeviceFromId(kUSBJabraSpeakerOutput2->id);
jennyz2cb9eb12014-11-05 19:21:053071 EXPECT_TRUE(active_output_3->active);
3072
3073 // Now call ChangeActiveDevices with only 2 nodes.
tbarziccae06e2a2017-01-17 23:14:473074 cras_audio_handler_->ChangeActiveNodes(
3075 {kUSBJabraSpeakerOutput1->id, kUSBJabraSpeakerOutput2->id});
3076
3077 // Verify only 2 nodes are active.
3078 EXPECT_EQ(2, GetActiveDeviceCount());
3079 const AudioDevice* output_1 = GetDeviceFromId(kHDMIOutput->id);
3080 EXPECT_FALSE(output_1->active);
3081 const AudioDevice* output_2 = GetDeviceFromId(kUSBJabraSpeakerOutput1->id);
3082 EXPECT_TRUE(output_2->active);
3083 const AudioDevice* output_3 = GetDeviceFromId(kUSBJabraSpeakerOutput2->id);
3084 EXPECT_TRUE(output_3->active);
3085}
3086
3087TEST_P(CrasAudioHandlerTest, SetActiveNodesWithFewerActives) {
3088 AudioNodeList audio_nodes = GenerateAudioNodeList(
3089 {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerOutput2});
3090 SetUpCrasAudioHandler(audio_nodes);
3091
3092 // Verify the audio devices size.
3093 AudioDeviceList audio_devices;
3094 cras_audio_handler_->GetAudioDevices(&audio_devices);
3095 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3096
3097 // Set all three nodes to be active.
3098 cras_audio_handler_->SetActiveOutputNodes({kHDMIOutput->id,
3099 kUSBJabraSpeakerOutput1->id,
3100 kUSBJabraSpeakerOutput2->id});
3101
3102 // Verify all three nodes are active.
3103 EXPECT_EQ(3, GetActiveDeviceCount());
3104 const AudioDevice* active_output_1 = GetDeviceFromId(kHDMIOutput->id);
3105 EXPECT_TRUE(active_output_1->active);
3106 const AudioDevice* active_output_2 =
3107 GetDeviceFromId(kUSBJabraSpeakerOutput1->id);
3108 EXPECT_TRUE(active_output_2->active);
3109 const AudioDevice* active_output_3 =
3110 GetDeviceFromId(kUSBJabraSpeakerOutput2->id);
3111 EXPECT_TRUE(active_output_3->active);
3112
3113 // Now call SetActiveOutputNodes with only 2 nodes.
3114 cras_audio_handler_->SetActiveOutputNodes(
3115 {kUSBJabraSpeakerOutput1->id, kUSBJabraSpeakerOutput2->id});
jennyz2cb9eb12014-11-05 19:21:053116
3117 // Verify only 2 nodes are active.
3118 EXPECT_EQ(2, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:533119 const AudioDevice* output_1 = GetDeviceFromId(kHDMIOutput->id);
jennyz2cb9eb12014-11-05 19:21:053120 EXPECT_FALSE(output_1->active);
tbarzic3ab01a22016-12-17 03:12:533121 const AudioDevice* output_2 = GetDeviceFromId(kUSBJabraSpeakerOutput1->id);
jennyz2cb9eb12014-11-05 19:21:053122 EXPECT_TRUE(output_2->active);
tbarzic3ab01a22016-12-17 03:12:533123 const AudioDevice* output_3 = GetDeviceFromId(kUSBJabraSpeakerOutput2->id);
jennyz2cb9eb12014-11-05 19:21:053124 EXPECT_TRUE(output_3->active);
3125}
3126
tbarzic3ab01a22016-12-17 03:12:533127TEST_P(CrasAudioHandlerTest, HotrodInitWithSingleJabra) {
jennyz2cb9eb12014-11-05 19:21:053128 // Simulates the hotrod initializated with a single jabra device and
3129 // CrasAudioHandler selected jabra input/output as active devices.
tbarzic3ab01a22016-12-17 03:12:533130 AudioNodeList audio_nodes =
3131 GenerateAudioNodeList({kHDMIOutput, kUSBJabraSpeakerOutput1,
3132 kUSBJabraSpeakerInput1, kUSBCameraInput});
jennyz2cb9eb12014-11-05 19:21:053133 SetUpCrasAudioHandler(audio_nodes);
3134
3135 // Verify the audio devices size.
3136 AudioDeviceList audio_devices;
3137 cras_audio_handler_->GetAudioDevices(&audio_devices);
3138 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3139
3140 // Verify the jabra speaker's output and input are selected as active nodes
3141 // by CrasAudioHandler.
3142 EXPECT_EQ(2, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:533143 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
jennyz2cb9eb12014-11-05 19:21:053144 cras_audio_handler_->GetPrimaryActiveOutputNode());
tbarzic3ab01a22016-12-17 03:12:533145 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
jennyz2cb9eb12014-11-05 19:21:053146 cras_audio_handler_->GetPrimaryActiveInputNode());
3147}
3148
tbarzic3ab01a22016-12-17 03:12:533149TEST_P(CrasAudioHandlerTest,
jennyz2cb9eb12014-11-05 19:21:053150 ChangeActiveNodesHotrodInitWithSingleJabraCameraPlugInLater) {
tbarzic3ab01a22016-12-17 03:12:533151 AudioNodeList audio_nodes = GenerateAudioNodeList(
3152 {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerInput1});
3153 AudioNode usb_camera = GenerateAudioNode(kUSBCameraInput);
jennyz2cb9eb12014-11-05 19:21:053154 usb_camera.plugged_time = 10000000;
3155 audio_nodes.push_back(usb_camera);
3156 SetUpCrasAudioHandler(audio_nodes);
3157
3158 // Verify the audio devices size.
3159 AudioDeviceList audio_devices;
3160 cras_audio_handler_->GetAudioDevices(&audio_devices);
3161 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3162
3163 // Verify the jabra speaker's output is selected as active output, and
3164 // camera's input is selected as active input by CrasAudioHandler
3165 EXPECT_EQ(2, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:533166 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
jennyz2cb9eb12014-11-05 19:21:053167 cras_audio_handler_->GetPrimaryActiveOutputNode());
tbarzic3ab01a22016-12-17 03:12:533168 EXPECT_EQ(kUSBCameraInput->id,
jennyz2cb9eb12014-11-05 19:21:053169 cras_audio_handler_->GetPrimaryActiveInputNode());
3170
3171 // Simulate hotrod app call to set jabra input as active device with only
3172 // jabra input node in the active node list, which does not conform to the
3173 // new SetActiveDevices protocol, but just show we can still handle it if
3174 // this happens.
tbarziccae06e2a2017-01-17 23:14:473175 cras_audio_handler_->ChangeActiveNodes(
3176 {kUSBJabraSpeakerOutput1->id, kUSBJabraSpeakerInput1->id});
3177
3178 // Verify the jabra speaker's output is selected as active output, and
3179 // jabra's input is selected as active input.
3180 EXPECT_EQ(2, GetActiveDeviceCount());
3181 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
3182 cras_audio_handler_->GetPrimaryActiveOutputNode());
3183 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
3184 cras_audio_handler_->GetPrimaryActiveInputNode());
3185}
3186
3187TEST_P(CrasAudioHandlerTest,
3188 SetActiveNodesHotrodInitWithSingleJabraCameraPlugInLater) {
3189 AudioNodeList audio_nodes = GenerateAudioNodeList(
3190 {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerInput1});
3191 AudioNode usb_camera = GenerateAudioNode(kUSBCameraInput);
3192 usb_camera.plugged_time = 10000000;
3193 audio_nodes.push_back(usb_camera);
3194 SetUpCrasAudioHandler(audio_nodes);
3195
3196 // Verify the audio devices size.
3197 AudioDeviceList audio_devices;
3198 cras_audio_handler_->GetAudioDevices(&audio_devices);
3199 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3200
3201 // Verify the jabra speaker's output is selected as active output, and
3202 // camera's input is selected as active input by CrasAudioHandler
3203 EXPECT_EQ(2, GetActiveDeviceCount());
3204 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
3205 cras_audio_handler_->GetPrimaryActiveOutputNode());
3206 EXPECT_EQ(kUSBCameraInput->id,
3207 cras_audio_handler_->GetPrimaryActiveInputNode());
3208
3209 // Simulate hotrod app call to set jabra input as active device with only
3210 // jabra input node in the active node list, which does not conform to the
3211 // new SetActiveDevices protocol, but just show we can still handle it if
3212 // this happens.
3213 cras_audio_handler_->SetActiveOutputNodes({kUSBJabraSpeakerOutput1->id});
3214
3215 cras_audio_handler_->SetActiveInputNodes({kUSBJabraSpeakerInput1->id});
jennyz2cb9eb12014-11-05 19:21:053216
3217 // Verify the jabra speaker's output is selected as active output, and
3218 // jabra's input is selected as active input.
3219 EXPECT_EQ(2, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:533220 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
jennyz2cb9eb12014-11-05 19:21:053221 cras_audio_handler_->GetPrimaryActiveOutputNode());
tbarzic3ab01a22016-12-17 03:12:533222 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
jennyz2cb9eb12014-11-05 19:21:053223 cras_audio_handler_->GetPrimaryActiveInputNode());
3224}
3225
tbarzic3ab01a22016-12-17 03:12:533226TEST_P(CrasAudioHandlerTest, ChangeActiveNodesDeactivatePrimaryActiveNode) {
3227 AudioNodeList audio_nodes =
3228 GenerateAudioNodeList({kUSBJabraSpeakerInput1, kUSBJabraSpeakerInput2});
3229 AudioNode usb_camera = GenerateAudioNode(kUSBCameraInput);
tbarzic96ea32052016-11-03 00:46:163230 usb_camera.plugged_time = 10000000;
3231 audio_nodes.push_back(usb_camera);
3232 SetUpCrasAudioHandler(audio_nodes);
3233
3234 // Verify the audio devices size.
3235 AudioDeviceList audio_devices;
3236 cras_audio_handler_->GetAudioDevices(&audio_devices);
3237 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3238
3239 // Verify the camera's input is selected as active input by CrasAudioHandler
3240 EXPECT_EQ(1, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:533241 EXPECT_EQ(kUSBCameraInput->id,
tbarzic96ea32052016-11-03 00:46:163242 cras_audio_handler_->GetPrimaryActiveInputNode());
3243
3244 // Simulate hotrod app call to set jabra input as active device with only
3245 // jabra input node in the active node list, which does not conform to the
3246 // new SetActiveDevices protocol, but just show we can still handle it if
3247 // this happens.
tbarziccae06e2a2017-01-17 23:14:473248 cras_audio_handler_->ChangeActiveNodes(
3249 {kUSBJabraSpeakerInput1->id, kUSBCameraInput->id});
tbarzic96ea32052016-11-03 00:46:163250
tbarziccae06e2a2017-01-17 23:14:473251 // Verify active input devices are set as expected, with primary active input
3252 // staying the same.
tbarzic96ea32052016-11-03 00:46:163253 EXPECT_EQ(2, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:533254 EXPECT_EQ(kUSBCameraInput->id,
tbarzic96ea32052016-11-03 00:46:163255 cras_audio_handler_->GetPrimaryActiveInputNode());
3256
3257 const AudioDevice* additional_speaker =
tbarzic3ab01a22016-12-17 03:12:533258 cras_audio_handler_->GetDeviceFromId(kUSBJabraSpeakerInput1->id);
tbarzic96ea32052016-11-03 00:46:163259 ASSERT_TRUE(additional_speaker);
3260 EXPECT_TRUE(additional_speaker->active);
3261
tbarziccae06e2a2017-01-17 23:14:473262 // Update active device list so previously primary active device is not
3263 // active anymore.
3264 cras_audio_handler_->ChangeActiveNodes(
3265 {kUSBJabraSpeakerInput1->id, kUSBJabraSpeakerInput2->id});
tbarzic96ea32052016-11-03 00:46:163266
tbarziccae06e2a2017-01-17 23:14:473267 // Verify that list of active devices is correctly set, and that a new primary
3268 // active input is selected.
3269 EXPECT_EQ(2, GetActiveDeviceCount());
3270 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
3271 cras_audio_handler_->GetPrimaryActiveInputNode());
3272
3273 additional_speaker =
3274 cras_audio_handler_->GetDeviceFromId(kUSBJabraSpeakerInput2->id);
3275 ASSERT_TRUE(additional_speaker);
3276 EXPECT_TRUE(additional_speaker->active);
3277}
3278
3279TEST_P(CrasAudioHandlerTest, SetActiveNodesDeactivatePrimaryActiveNode) {
3280 AudioNodeList audio_nodes =
3281 GenerateAudioNodeList({kUSBJabraSpeakerInput1, kUSBJabraSpeakerInput2});
3282 AudioNode usb_camera = GenerateAudioNode(kUSBCameraInput);
3283 usb_camera.plugged_time = 10000000;
3284 audio_nodes.push_back(usb_camera);
3285 SetUpCrasAudioHandler(audio_nodes);
3286
3287 // Verify the audio devices size.
3288 AudioDeviceList audio_devices;
3289 cras_audio_handler_->GetAudioDevices(&audio_devices);
3290 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3291
3292 // Verify the camera's input is selected as active input by CrasAudioHandler.
3293 EXPECT_EQ(1, GetActiveDeviceCount());
3294 EXPECT_EQ(kUSBCameraInput->id,
3295 cras_audio_handler_->GetPrimaryActiveInputNode());
3296
3297 // Add another device to active input device list.
3298 cras_audio_handler_->SetActiveInputNodes(
3299 {kUSBJabraSpeakerInput1->id, kUSBCameraInput->id});
3300
3301 // Verify active input devices are set as expected, with primary active input
3302 // staying the same.
3303 EXPECT_EQ(2, GetActiveDeviceCount());
3304 EXPECT_EQ(kUSBCameraInput->id,
3305 cras_audio_handler_->GetPrimaryActiveInputNode());
3306
3307 const AudioDevice* additional_speaker =
3308 cras_audio_handler_->GetDeviceFromId(kUSBJabraSpeakerInput1->id);
3309 ASSERT_TRUE(additional_speaker);
3310 EXPECT_TRUE(additional_speaker->active);
3311
3312 // Update active device list so previously primary active device is not
3313 // active anymore.
3314 cras_audio_handler_->SetActiveInputNodes(
3315 {kUSBJabraSpeakerInput1->id, kUSBJabraSpeakerInput2->id});
3316
3317 // Verify that list of active devices is correctly set, and that a new primary
3318 // active input is selected.
tbarzic96ea32052016-11-03 00:46:163319 EXPECT_EQ(2, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:533320 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
tbarzic96ea32052016-11-03 00:46:163321 cras_audio_handler_->GetPrimaryActiveInputNode());
3322
3323 additional_speaker =
tbarzic3ab01a22016-12-17 03:12:533324 cras_audio_handler_->GetDeviceFromId(kUSBJabraSpeakerInput2->id);
tbarzic96ea32052016-11-03 00:46:163325 ASSERT_TRUE(additional_speaker);
3326 EXPECT_TRUE(additional_speaker->active);
3327}
3328
tbarzic3ab01a22016-12-17 03:12:533329TEST_P(CrasAudioHandlerTest,
jennyz2cb9eb12014-11-05 19:21:053330 ChangeActiveNodesHotrodInitWithSingleJabraCameraPlugInLaterOldCall) {
tbarzic3ab01a22016-12-17 03:12:533331 AudioNodeList audio_nodes = GenerateAudioNodeList(
3332 {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerInput1});
3333 AudioNode usb_camera = GenerateAudioNode(kUSBCameraInput);
jennyz2cb9eb12014-11-05 19:21:053334 usb_camera.plugged_time = 10000000;
3335 audio_nodes.push_back(usb_camera);
3336 SetUpCrasAudioHandler(audio_nodes);
3337
3338 // Verify the audio devices size.
3339 AudioDeviceList audio_devices;
3340 cras_audio_handler_->GetAudioDevices(&audio_devices);
3341 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3342
3343 // Verify the jabra speaker's output is selected as active output, and
3344 // camera's input is selected as active input by CrasAudioHandler
3345 EXPECT_EQ(2, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:533346 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
jennyz2cb9eb12014-11-05 19:21:053347 cras_audio_handler_->GetPrimaryActiveOutputNode());
tbarzic3ab01a22016-12-17 03:12:533348 EXPECT_EQ(kUSBCameraInput->id,
jennyz2cb9eb12014-11-05 19:21:053349 cras_audio_handler_->GetPrimaryActiveInputNode());
3350
3351 // Simulate hotrod app call to set jabra input as active device with only
3352 // jabra input node in the active node list, which does not conform to the
3353 // new SetActiveDevices protocol, but just show we can still handle it if
3354 // this happens.
tbarziccae06e2a2017-01-17 23:14:473355 cras_audio_handler_->ChangeActiveNodes({kUSBJabraSpeakerInput1->id});
3356
3357 // Verify the jabra speaker's output is selected as active output, and
3358 // jabra's input is selected as active input.
3359 EXPECT_EQ(2, GetActiveDeviceCount());
3360 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
3361 cras_audio_handler_->GetPrimaryActiveOutputNode());
3362 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
3363 cras_audio_handler_->GetPrimaryActiveInputNode());
3364}
3365
3366TEST_P(CrasAudioHandlerTest,
3367 SetActiveNodesHotrodInitWithSingleJabraCameraPlugInLaterOldCall) {
3368 AudioNodeList audio_nodes = GenerateAudioNodeList(
3369 {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerInput1});
3370 AudioNode usb_camera = GenerateAudioNode(kUSBCameraInput);
3371 usb_camera.plugged_time = 10000000;
3372 audio_nodes.push_back(usb_camera);
3373 SetUpCrasAudioHandler(audio_nodes);
3374
3375 // Verify the audio devices size.
3376 AudioDeviceList audio_devices;
3377 cras_audio_handler_->GetAudioDevices(&audio_devices);
3378 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3379
3380 // Verify the jabra speaker's output is selected as active output, and
3381 // camera's input is selected as active input by CrasAudioHandler
3382 EXPECT_EQ(2, GetActiveDeviceCount());
3383 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
3384 cras_audio_handler_->GetPrimaryActiveOutputNode());
3385 EXPECT_EQ(kUSBCameraInput->id,
3386 cras_audio_handler_->GetPrimaryActiveInputNode());
3387
3388 // Simulate hotrod app call to set jabra input as active device with only
3389 // jabra input node in the active node list, which does not conform to the
3390 // new SetActiveDevices protocol, but just show we can still handle it if
3391 // this happens.
3392 cras_audio_handler_->SetActiveInputNodes({kUSBJabraSpeakerInput1->id});
jennyz2cb9eb12014-11-05 19:21:053393
3394 // Verify the jabra speaker's output is selected as active output, and
3395 // jabra's input is selected as active input.
3396 EXPECT_EQ(2, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:533397 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
jennyz2cb9eb12014-11-05 19:21:053398 cras_audio_handler_->GetPrimaryActiveOutputNode());
tbarzic3ab01a22016-12-17 03:12:533399 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
jennyz2cb9eb12014-11-05 19:21:053400 cras_audio_handler_->GetPrimaryActiveInputNode());
3401}
3402
tbarzic3ab01a22016-12-17 03:12:533403TEST_P(CrasAudioHandlerTest,
jennyz2cb9eb12014-11-05 19:21:053404 ChangeActiveNodesHotrodInitWithSingleJabraChangeOutput) {
tbarzic3ab01a22016-12-17 03:12:533405 AudioNodeList audio_nodes =
3406 GenerateAudioNodeList({kHDMIOutput, kUSBJabraSpeakerOutput1,
3407 kUSBJabraSpeakerInput1, kUSBCameraInput});
jennyz2cb9eb12014-11-05 19:21:053408 SetUpCrasAudioHandler(audio_nodes);
3409
3410 // Verify the audio devices size.
3411 AudioDeviceList audio_devices;
3412 cras_audio_handler_->GetAudioDevices(&audio_devices);
3413 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3414
3415 // Verify the jabra speaker's output and input are selected as active output
3416 // by CrasAudioHandler.
3417 EXPECT_EQ(2, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:533418 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
jennyz2cb9eb12014-11-05 19:21:053419 cras_audio_handler_->GetPrimaryActiveOutputNode());
tbarzic3ab01a22016-12-17 03:12:533420 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
jennyz2cb9eb12014-11-05 19:21:053421 cras_audio_handler_->GetPrimaryActiveInputNode());
3422
3423 // Simulate hotrod app call SetActiveDevices to change active output
3424 // with only complete list of active nodes passed in, which is the new
3425 // way of hotrod app.
tbarziccae06e2a2017-01-17 23:14:473426 cras_audio_handler_->ChangeActiveNodes(
3427 {kHDMIOutput->id, kUSBJabraSpeakerInput1->id});
3428
3429 // Verify the jabra speaker's output is selected as active output, and
3430 // jabra's input is selected as active input.
3431 EXPECT_EQ(2, GetActiveDeviceCount());
3432 EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3433 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
3434 cras_audio_handler_->GetPrimaryActiveInputNode());
3435}
3436
3437TEST_P(CrasAudioHandlerTest,
3438 SetActiveNodesHotrodInitWithSingleJabraChangeOutput) {
3439 AudioNodeList audio_nodes =
3440 GenerateAudioNodeList({kHDMIOutput, kUSBJabraSpeakerOutput1,
3441 kUSBJabraSpeakerInput1, kUSBCameraInput});
3442 SetUpCrasAudioHandler(audio_nodes);
3443
3444 // Verify the audio devices size.
3445 AudioDeviceList audio_devices;
3446 cras_audio_handler_->GetAudioDevices(&audio_devices);
3447 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3448
3449 // Verify the jabra speaker's output and input are selected as active output
3450 // by CrasAudioHandler.
3451 EXPECT_EQ(2, GetActiveDeviceCount());
3452 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
3453 cras_audio_handler_->GetPrimaryActiveOutputNode());
3454 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
3455 cras_audio_handler_->GetPrimaryActiveInputNode());
3456
3457 // Simulate hotrod app calling SetActiveDeviceLists to change active input
3458 // and output with complete list of active nodes passed in.
3459 cras_audio_handler_->SetActiveOutputNodes({kHDMIOutput->id});
3460 cras_audio_handler_->SetActiveInputNodes({kUSBJabraSpeakerInput1->id});
jennyz2cb9eb12014-11-05 19:21:053461
3462 // Verify the jabra speaker's output is selected as active output, and
3463 // jabra's input is selected as active input.
3464 EXPECT_EQ(2, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:533465 EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3466 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
jennyz2cb9eb12014-11-05 19:21:053467 cras_audio_handler_->GetPrimaryActiveInputNode());
3468}
3469
tbarzic3ab01a22016-12-17 03:12:533470TEST_P(CrasAudioHandlerTest,
jennyz2cb9eb12014-11-05 19:21:053471 ChangeActiveNodesHotrodInitWithSingleJabraChangeOutputOldCall) {
tbarzic3ab01a22016-12-17 03:12:533472 AudioNodeList audio_nodes =
3473 GenerateAudioNodeList({kHDMIOutput, kUSBJabraSpeakerOutput1,
3474 kUSBJabraSpeakerInput1, kUSBCameraInput});
jennyz2cb9eb12014-11-05 19:21:053475 SetUpCrasAudioHandler(audio_nodes);
3476
3477 // Verify the audio devices size.
3478 AudioDeviceList audio_devices;
3479 cras_audio_handler_->GetAudioDevices(&audio_devices);
3480 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3481
3482 // Verify the jabra speaker's output and input are selected as active output
3483 // by CrasAudioHandler.
3484 EXPECT_EQ(2, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:533485 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
jennyz2cb9eb12014-11-05 19:21:053486 cras_audio_handler_->GetPrimaryActiveOutputNode());
tbarzic3ab01a22016-12-17 03:12:533487 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
jennyz2cb9eb12014-11-05 19:21:053488 cras_audio_handler_->GetPrimaryActiveInputNode());
3489
3490 // Simulate hotrod app call SetActiveDevices to change active output
3491 // with only a single active output nodes passed in, which is the old
3492 // way of hotrod app.
tbarziccae06e2a2017-01-17 23:14:473493 cras_audio_handler_->ChangeActiveNodes({kHDMIOutput->id});
jennyz2cb9eb12014-11-05 19:21:053494
3495 // Verify the jabra speaker's output is selected as active output, and
3496 // jabra's input is selected as active input.
3497 EXPECT_EQ(2, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:533498 EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3499 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
jennyz2cb9eb12014-11-05 19:21:053500 cras_audio_handler_->GetPrimaryActiveInputNode());
jennyzb47947f2014-09-25 00:42:043501}
3502
tbarziccae06e2a2017-01-17 23:14:473503TEST_P(CrasAudioHandlerTest, SetEmptyActiveOutputNodes) {
3504 AudioNodeList audio_nodes =
3505 GenerateAudioNodeList({kHDMIOutput, kUSBJabraSpeakerOutput1,
3506 kUSBJabraSpeakerInput1, kUSBCameraInput});
3507 SetUpCrasAudioHandler(audio_nodes);
3508
3509 // Verify the audio devices size.
3510 AudioDeviceList audio_devices;
3511 cras_audio_handler_->GetAudioDevices(&audio_devices);
3512 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3513
3514 // Verify the jabra speaker's output and input are selected as active output
3515 // by CrasAudioHandler.
3516 EXPECT_EQ(2, GetActiveDeviceCount());
3517 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
3518 cras_audio_handler_->GetPrimaryActiveOutputNode());
3519 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
3520 cras_audio_handler_->GetPrimaryActiveInputNode());
3521
3522 cras_audio_handler_->SetActiveOutputNodes(CrasAudioHandler::NodeIdList());
3523
3524 // Verify the jabra's input is selected as active input, and that there are
3525 // no active outputs.
3526 EXPECT_EQ(1, GetActiveDeviceCount());
3527 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
3528 cras_audio_handler_->GetPrimaryActiveInputNode());
3529 EXPECT_EQ(0u, cras_audio_handler_->GetPrimaryActiveOutputNode());
3530}
3531
3532TEST_P(CrasAudioHandlerTest, SetEmptyActiveInputNodes) {
3533 AudioNodeList audio_nodes =
3534 GenerateAudioNodeList({kHDMIOutput, kUSBJabraSpeakerOutput1,
3535 kUSBJabraSpeakerInput1, kUSBCameraInput});
3536 SetUpCrasAudioHandler(audio_nodes);
3537
3538 // Verify the audio devices size.
3539 AudioDeviceList audio_devices;
3540 cras_audio_handler_->GetAudioDevices(&audio_devices);
3541 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3542
3543 // Verify the jabra speaker's output and input are selected as active output
3544 // by CrasAudioHandler.
3545 EXPECT_EQ(2, GetActiveDeviceCount());
3546 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
3547 cras_audio_handler_->GetPrimaryActiveOutputNode());
3548 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
3549 cras_audio_handler_->GetPrimaryActiveInputNode());
3550
3551 cras_audio_handler_->SetActiveInputNodes(CrasAudioHandler::NodeIdList());
3552
3553 // Verify the jabra speaker's output is selected as active output, and
3554 // there are no active inputs..
3555 EXPECT_EQ(1, GetActiveDeviceCount());
3556 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
3557 cras_audio_handler_->GetPrimaryActiveOutputNode());
3558 EXPECT_EQ(0u, cras_audio_handler_->GetPrimaryActiveInputNode());
3559}
3560
tbarzic3ab01a22016-12-17 03:12:533561TEST_P(CrasAudioHandlerTest, NoMoreAudioInputDevices) {
mukai307596b62014-11-24 19:56:593562 // Some device like chromebox does not have the internal input device. The
3563 // active devices should be reset when the user plugs a device and then
3564 // unplugs it to such device.
tbarzic3ab01a22016-12-17 03:12:533565 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker});
mukai307596b62014-11-24 19:56:593566 SetUpCrasAudioHandler(audio_nodes);
3567
3568 EXPECT_EQ(0ULL, cras_audio_handler_->GetPrimaryActiveInputNode());
3569
tbarzic3ab01a22016-12-17 03:12:533570 audio_nodes.push_back(GenerateAudioNode(kMicJack));
mukai307596b62014-11-24 19:56:593571 ChangeAudioNodes(audio_nodes);
3572
tbarzic3ab01a22016-12-17 03:12:533573 EXPECT_EQ(kMicJack->id, cras_audio_handler_->GetPrimaryActiveInputNode());
mukai307596b62014-11-24 19:56:593574 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
3575 test_observer_->reset_active_input_node_changed_count();
3576
3577 audio_nodes.pop_back();
3578 ChangeAudioNodes(audio_nodes);
3579 EXPECT_EQ(0ULL, cras_audio_handler_->GetPrimaryActiveInputNode());
3580 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
3581}
3582
jennyz731ba262015-02-05 19:00:033583// Test the case in which an HDMI output is plugged in with other higher
3584// priority
3585// output devices already plugged and user has manually selected an active
3586// output.
3587// The hotplug of hdmi output should not change user's selection of active
3588// device.
3589// crbug.com/447826.
tbarzic3ab01a22016-12-17 03:12:533590TEST_P(CrasAudioHandlerTest, HotPlugHDMINotChangeActiveOutput) {
jennyz731ba262015-02-05 19:00:033591 AudioNodeList audio_nodes;
tbarzic3ab01a22016-12-17 03:12:533592 AudioNode internal_speaker = GenerateAudioNode(kInternalSpeaker);
jennyz731ba262015-02-05 19:00:033593 audio_nodes.push_back(internal_speaker);
tbarzic3ab01a22016-12-17 03:12:533594 AudioNode usb_headset = GenerateAudioNode(kUSBHeadphone1);
jennyz731ba262015-02-05 19:00:033595 usb_headset.plugged_time = 80000000;
3596 audio_nodes.push_back(usb_headset);
3597 SetUpCrasAudioHandler(audio_nodes);
3598
3599 // Verify the audio devices size.
3600 AudioDeviceList audio_devices;
3601 cras_audio_handler_->GetAudioDevices(&audio_devices);
3602 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3603
3604 // Verify the USB headset is selected as active output by default.
3605 EXPECT_EQ(usb_headset.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3606
3607 // Manually set the active output to internal speaker.
tbarzic3ab01a22016-12-17 03:12:533608 AudioDevice internal_output(GenerateAudioNode(kInternalSpeaker));
jennyzbd236742016-03-02 20:15:523609 cras_audio_handler_->SwitchToDevice(internal_output, true,
3610 CrasAudioHandler::ACTIVATE_BY_USER);
jennyz731ba262015-02-05 19:00:033611
3612 // Verify the active output is switched to internal speaker.
3613 EXPECT_EQ(internal_speaker.id,
3614 cras_audio_handler_->GetPrimaryActiveOutputNode());
tbarzic3ab01a22016-12-17 03:12:533615 EXPECT_LT(internal_speaker.plugged_time, usb_headset.plugged_time);
jennyz731ba262015-02-05 19:00:033616 const AudioDevice* usb_device = GetDeviceFromId(usb_headset.id);
3617 EXPECT_FALSE(usb_device->active);
3618
3619 // Plug in HDMI output.
3620 audio_nodes.clear();
3621 internal_speaker.active = true;
3622 audio_nodes.push_back(internal_speaker);
3623 usb_headset.active = false;
3624 audio_nodes.push_back(usb_headset);
tbarzic3ab01a22016-12-17 03:12:533625 AudioNode hdmi = GenerateAudioNode(kHDMIOutput);
jennyz731ba262015-02-05 19:00:033626 hdmi.plugged_time = 90000000;
3627 audio_nodes.push_back(hdmi);
3628 ChangeAudioNodes(audio_nodes);
3629
3630 // The active output should not change.
tbarzic3ab01a22016-12-17 03:12:533631 EXPECT_EQ(kInternalSpeaker->id,
jennyz731ba262015-02-05 19:00:033632 cras_audio_handler_->GetPrimaryActiveOutputNode());
3633}
3634
jennyz3147abc72015-04-29 00:13:033635// Test the case in which the active device was set to inactive from cras after
3636// resuming from suspension state. See crbug.com/478968.
tbarzic3ab01a22016-12-17 03:12:533637TEST_P(CrasAudioHandlerTest, ActiveNodeLostAfterResume) {
3638 AudioNodeList audio_nodes = GenerateAudioNodeList({kHeadphone, kHDMIOutput});
3639 for (const auto& node : audio_nodes)
3640 ASSERT_FALSE(node.active) << node.id << " expected to be inactive";
jennyz3147abc72015-04-29 00:13:033641 SetUpCrasAudioHandler(audio_nodes);
3642
3643 // Verify the headphone is selected as the active output.
3644 AudioDeviceList audio_devices;
3645 cras_audio_handler_->GetAudioDevices(&audio_devices);
3646 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
tbarzic3ab01a22016-12-17 03:12:533647 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3648 const AudioDevice* active_headphone = GetDeviceFromId(kHeadphone->id);
3649 EXPECT_EQ(kHeadphone->id, active_headphone->id);
jennyz3147abc72015-04-29 00:13:033650 EXPECT_TRUE(active_headphone->active);
3651
3652 // Simulate NodesChanged signal with headphone turning into inactive state,
3653 // and HDMI node removed.
3654 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:533655 audio_nodes.push_back(GenerateAudioNode(kHeadphone));
jennyz3147abc72015-04-29 00:13:033656 ChangeAudioNodes(audio_nodes);
3657
3658 // Verify the headphone is set to active again.
tbarzic3ab01a22016-12-17 03:12:533659 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3660 const AudioDevice* headphone_resumed = GetDeviceFromId(kHeadphone->id);
3661 EXPECT_EQ(kHeadphone->id, headphone_resumed->id);
jennyz3147abc72015-04-29 00:13:033662 EXPECT_TRUE(headphone_resumed->active);
3663}
3664
jennyz639ec6f2016-01-19 23:27:303665// In the mirror mode, when the device resumes after being suspended, the hmdi
3666// node will be lost first, then re-appear with a different node id, but with
3667// the same stable id. If it is set as the non-active node by user before
3668// suspend/resume, it should remain inactive after the device resumes even
3669// if it has a higher priority than the current active node.
3670// crbug.com/443014.
tbarzic3ab01a22016-12-17 03:12:533671TEST_P(CrasAudioHandlerTest, HDMIRemainInactiveAfterSuspendResume) {
jennyz639ec6f2016-01-19 23:27:303672 AudioNodeList audio_nodes;
tbarzic3ab01a22016-12-17 03:12:533673 AudioNode internal_speaker = GenerateAudioNode(kInternalSpeaker);
jennyz639ec6f2016-01-19 23:27:303674 audio_nodes.push_back(internal_speaker);
tbarzic3ab01a22016-12-17 03:12:533675 AudioNode hdmi_output = GenerateAudioNode(kHDMIOutput);
jennyz639ec6f2016-01-19 23:27:303676 audio_nodes.push_back(hdmi_output);
3677 SetUpCrasAudioHandler(audio_nodes);
3678
3679 // Verify the hdmi is selected as the active output since it has a higher
3680 // priority.
3681 AudioDeviceList audio_devices;
3682 cras_audio_handler_->GetAudioDevices(&audio_devices);
3683 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3684 EXPECT_EQ(hdmi_output.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3685
3686 // Manually set the active output to internal speaker.
jennyzbd236742016-03-02 20:15:523687 cras_audio_handler_->SwitchToDevice(AudioDevice(internal_speaker), true,
3688 CrasAudioHandler::ACTIVATE_BY_USER);
jennyz639ec6f2016-01-19 23:27:303689 EXPECT_EQ(internal_speaker.id,
3690 cras_audio_handler_->GetPrimaryActiveOutputNode());
3691
3692 // Simulate the suspend and resume of the device during mirror mode. The HDMI
3693 // node will be lost first.
3694 audio_nodes.clear();
3695 internal_speaker.active = true;
3696 audio_nodes.push_back(internal_speaker);
3697 ChangeAudioNodes(audio_nodes);
3698
3699 // Verify the HDMI node is lost, and internal speaker is still the active
3700 // node.
3701 cras_audio_handler_->GetAudioDevices(&audio_devices);
3702 EXPECT_EQ(1u, audio_devices.size());
3703 EXPECT_EQ(internal_speaker.id,
3704 cras_audio_handler_->GetPrimaryActiveOutputNode());
3705
3706 // Simulate the re-appearing of the hdmi node, which comes with a new id,
3707 // but the same stable device id.
3708 AudioNode hdmi_output_2(hdmi_output);
3709 hdmi_output_2.id = 20006;
3710 hdmi_output_2.plugged_time = internal_speaker.plugged_time + 100;
3711 audio_nodes.push_back(hdmi_output_2);
tbarzic3ab01a22016-12-17 03:12:533712 ASSERT_NE(hdmi_output.id, hdmi_output_2.id);
3713 ASSERT_EQ(hdmi_output.StableDeviceId(), hdmi_output_2.StableDeviceId());
jennyz639ec6f2016-01-19 23:27:303714 ChangeAudioNodes(audio_nodes);
3715
3716 // Verify the hdmi node is not set the active, and the current active node
3717 // , the internal speaker, remains active.
3718 cras_audio_handler_->GetAudioDevices(&audio_devices);
3719 EXPECT_EQ(2u, audio_devices.size());
3720 EXPECT_EQ(internal_speaker.id,
3721 cras_audio_handler_->GetPrimaryActiveOutputNode());
3722}
3723
jennyz3147abc72015-04-29 00:13:033724// Test the case in which there are two NodesChanged signal for discovering
3725// output devices, and there is race between NodesChange and SetActiveOutput
3726// during this process. See crbug.com/478968.
tbarzic3ab01a22016-12-17 03:12:533727TEST_P(CrasAudioHandlerTest, ActiveNodeLostDuringLoginSession) {
3728 AudioNodeList audio_nodes = GenerateAudioNodeList({kHeadphone});
3729 for (const auto& node : audio_nodes)
3730 ASSERT_FALSE(node.active) << node.id << " expected to be inactive";
jennyz3147abc72015-04-29 00:13:033731 SetUpCrasAudioHandler(audio_nodes);
3732
3733 // Verify the headphone is selected as the active output.
3734 AudioDeviceList audio_devices;
3735 cras_audio_handler_->GetAudioDevices(&audio_devices);
3736 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
tbarzic3ab01a22016-12-17 03:12:533737 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3738 const AudioDevice* active_headphone = GetDeviceFromId(kHeadphone->id);
3739 EXPECT_EQ(kHeadphone->id, active_headphone->id);
jennyz3147abc72015-04-29 00:13:033740 EXPECT_TRUE(active_headphone->active);
3741
3742 // Simulate NodesChanged signal with headphone turning into inactive state,
3743 // and add a new HDMI output node.
3744 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:533745 audio_nodes.push_back(GenerateAudioNode(kHeadphone));
3746 audio_nodes.push_back(GenerateAudioNode(kHDMIOutput));
jennyz3147abc72015-04-29 00:13:033747 ChangeAudioNodes(audio_nodes);
3748
3749 // Verify the headphone is set to active again.
tbarzic3ab01a22016-12-17 03:12:533750 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3751 const AudioDevice* headphone_resumed = GetDeviceFromId(kHeadphone->id);
3752 EXPECT_EQ(kHeadphone->id, headphone_resumed->id);
jennyz3147abc72015-04-29 00:13:033753 EXPECT_TRUE(headphone_resumed->active);
3754}
3755
jennyzc770dc772015-06-29 23:46:063756// This test HDMI output rediscovering case in crbug.com/503667.
tbarzic3ab01a22016-12-17 03:12:533757TEST_P(CrasAudioHandlerTest, HDMIOutputRediscover) {
3758 AudioNodeList audio_nodes =
3759 GenerateAudioNodeList({kInternalSpeaker, kHDMIOutput});
jennyzc770dc772015-06-29 23:46:063760 SetUpCrasAudioHandler(audio_nodes);
3761
3762 // Verify the HDMI device has been selected as the active output, and audio
3763 // output is not muted.
3764 AudioDevice active_output;
3765 EXPECT_TRUE(
3766 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:533767 EXPECT_EQ(kHDMIOutput->id, active_output.id);
3768 EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
jennyzc770dc772015-06-29 23:46:063769 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
3770 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
3771
3772 // Trigger HDMI rediscovering grace period, and remove the HDMI node.
3773 const int grace_period_in_ms = 200;
3774 SetHDMIRediscoverGracePeriodDuration(grace_period_in_ms);
3775 SetActiveHDMIRediscover();
tbarzic3ab01a22016-12-17 03:12:533776 AudioNodeList audio_nodes_lost_hdmi =
3777 GenerateAudioNodeList({kInternalSpeaker});
jennyzc770dc772015-06-29 23:46:063778 ChangeAudioNodes(audio_nodes_lost_hdmi);
3779
3780 // Verify the active output is switched to internal speaker, it is not muted
3781 // by preference, but the system output is muted during the grace period.
3782 EXPECT_TRUE(
3783 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:533784 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
jennyzc770dc772015-06-29 23:46:063785 EXPECT_FALSE(
tbarzic3ab01a22016-12-17 03:12:533786 cras_audio_handler_->IsOutputMutedForDevice(kInternalSpeaker->id));
jennyzc770dc772015-06-29 23:46:063787 EXPECT_TRUE(cras_audio_handler_->IsOutputMuted());
3788
3789 // Re-attach the HDMI device after a little delay.
3790 HDMIRediscoverWaiter waiter(this, grace_period_in_ms);
3791 waiter.WaitUntilTimeOut(grace_period_in_ms / 4);
3792 ChangeAudioNodes(audio_nodes);
3793
3794 // After HDMI re-discover grace period, verify HDMI output is selected as the
3795 // active device and not muted.
3796 waiter.WaitUntilHDMIRediscoverGracePeriodEnd();
3797 EXPECT_TRUE(
3798 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:533799 EXPECT_EQ(kHDMIOutput->id, active_output.id);
3800 EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
jennyzc770dc772015-06-29 23:46:063801 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
3802}
3803
jennyzc79ccda2015-07-28 23:04:353804// This tests the case of output unmuting event is notified after the hdmi
3805// output re-discover grace period ends, see crbug.com/512601.
tbarzic3ab01a22016-12-17 03:12:533806TEST_P(CrasAudioHandlerTest, HDMIOutputUnplugDuringSuspension) {
3807 AudioNodeList audio_nodes =
3808 GenerateAudioNodeList({kInternalSpeaker, kHDMIOutput});
jennyzc79ccda2015-07-28 23:04:353809 SetUpCrasAudioHandler(audio_nodes);
3810
3811 // Verify the HDMI device has been selected as the active output, and audio
3812 // output is not muted.
3813 AudioDevice active_output;
3814 EXPECT_TRUE(
3815 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:533816 EXPECT_EQ(kHDMIOutput->id, active_output.id);
3817 EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
jennyzc79ccda2015-07-28 23:04:353818 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
3819 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
3820
3821 // Trigger HDMI rediscovering grace period, and remove the HDMI node.
3822 const int grace_period_in_ms = 200;
3823 SetHDMIRediscoverGracePeriodDuration(grace_period_in_ms);
3824 SetActiveHDMIRediscover();
tbarzic3ab01a22016-12-17 03:12:533825 AudioNodeList audio_nodes_lost_hdmi =
3826 GenerateAudioNodeList({kInternalSpeaker});
jennyzc79ccda2015-07-28 23:04:353827 ChangeAudioNodes(audio_nodes_lost_hdmi);
3828
3829 // Verify the active output is switched to internal speaker, it is not muted
3830 // by preference, but the system output is muted during the grace period.
3831 EXPECT_TRUE(
3832 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:533833 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
jennyzc79ccda2015-07-28 23:04:353834 EXPECT_FALSE(
tbarzic3ab01a22016-12-17 03:12:533835 cras_audio_handler_->IsOutputMutedForDevice(kInternalSpeaker->id));
jennyzc79ccda2015-07-28 23:04:353836 EXPECT_TRUE(cras_audio_handler_->IsOutputMuted());
3837
3838 // After HDMI re-discover grace period, verify internal speaker is still the
3839 // active output and not muted, and unmute event by system is notified.
3840 test_observer_->reset_output_mute_changed_count();
3841 HDMIRediscoverWaiter waiter(this, grace_period_in_ms);
3842 waiter.WaitUntilHDMIRediscoverGracePeriodEnd();
3843 EXPECT_TRUE(
3844 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:533845 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
3846 EXPECT_EQ(kInternalSpeaker->id,
jennyzc79ccda2015-07-28 23:04:353847 cras_audio_handler_->GetPrimaryActiveOutputNode());
3848 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
3849 EXPECT_EQ(1, test_observer_->output_mute_changed_count());
3850 EXPECT_TRUE(test_observer_->output_mute_by_system());
3851}
3852
jennyzfbaa3e62017-03-06 20:14:023853TEST_P(CrasAudioHandlerTest, FrontCameraStartStop) {
3854 AudioNodeList audio_nodes = GenerateAudioNodeList({kFrontMic, kRearMic});
3855 SetUpCrasAudioHandler(audio_nodes);
3856
3857 // Verify the audio devices size.
3858 AudioDeviceList audio_devices;
3859 cras_audio_handler_->GetAudioDevices(&audio_devices);
3860 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3861
3862 // Verify the front mic has been selected as the active input.
3863 EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3864 EXPECT_TRUE(cras_audio_handler_->HasDualInternalMic());
3865
3866 // Start the front facing camera.
3867 StartFrontFacingCamera();
3868 // Verify the front mic has been selected as the active input.
3869 EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3870
3871 // Stop the front facing camera.
3872 StopFrontFacingCamera();
3873 // Verify the front mic has been selected as the active input.
3874 EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3875}
3876
3877TEST_P(CrasAudioHandlerTest, RearCameraStartStop) {
3878 AudioNodeList audio_nodes = GenerateAudioNodeList({kFrontMic, kRearMic});
3879 SetUpCrasAudioHandler(audio_nodes);
3880
3881 // Verify the audio devices size.
3882 AudioDeviceList audio_devices;
3883 cras_audio_handler_->GetAudioDevices(&audio_devices);
3884 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3885
3886 // Verify the front mic has been selected as the active input.
3887 EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3888 EXPECT_TRUE(cras_audio_handler_->HasDualInternalMic());
3889
3890 // Start the rear facing camera.
3891 StartRearFacingCamera();
3892 // Verify the active input is switched to the rear mic.
3893 EXPECT_EQ(kRearMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3894
3895 // Stop the rear facing camera.
3896 StopRearFacingCamera();
3897 // Verify the active input is switched back to front mic.
3898 EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3899}
3900
3901TEST_P(CrasAudioHandlerTest, SwitchFrontRearCamera) {
3902 AudioNodeList audio_nodes = GenerateAudioNodeList({kFrontMic, kRearMic});
3903 SetUpCrasAudioHandler(audio_nodes);
3904
3905 // Verify the audio devices size.
3906 AudioDeviceList audio_devices;
3907 cras_audio_handler_->GetAudioDevices(&audio_devices);
3908 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3909
3910 // Verify the front mic has been selected as the active input.
3911 EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3912 EXPECT_TRUE(cras_audio_handler_->HasDualInternalMic());
3913
3914 // Start the front facing camera.
3915 StartFrontFacingCamera();
3916 // Verify the front mic has been selected as the active input.
3917 EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3918
3919 // Simulates the camera app switching from front camera to rear camera.
3920 StopFrontFacingCamera();
3921 StartRearFacingCamera();
3922
3923 // Verify the rear mic has been selected as the active input.
3924 EXPECT_EQ(kRearMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3925}
3926
3927TEST_P(CrasAudioHandlerTest, StartFrontCameraWithActiveExternalInput) {
3928 AudioNodeList audio_nodes =
3929 GenerateAudioNodeList({kFrontMic, kRearMic, kMicJack});
3930 SetUpCrasAudioHandler(audio_nodes);
3931
3932 // Verify the audio devices size.
3933 AudioDeviceList audio_devices;
3934 cras_audio_handler_->GetAudioDevices(&audio_devices);
3935 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3936
3937 // Verify the mic Jack has been selected as the active input.
3938 EXPECT_EQ(kMicJackId, cras_audio_handler_->GetPrimaryActiveInputNode());
3939 EXPECT_TRUE(cras_audio_handler_->HasDualInternalMic());
3940
3941 // Start the front facing camera.
3942 StartFrontFacingCamera();
3943 // Verify the mic Jack has been selected as the active input.
3944 EXPECT_EQ(kMicJackId, cras_audio_handler_->GetPrimaryActiveInputNode());
3945
3946 // Stop the front facing camera.
3947 StopFrontFacingCamera();
3948 // Verify the mic Jack remains as the active input.
3949 EXPECT_EQ(kMicJackId, cras_audio_handler_->GetPrimaryActiveInputNode());
3950}
3951
3952TEST_P(CrasAudioHandlerTest, StartFrontCameraWithInactiveExternalInput) {
3953 AudioNodeList audio_nodes =
3954 GenerateAudioNodeList({kFrontMic, kRearMic, kMicJack});
3955 SetUpCrasAudioHandler(audio_nodes);
3956
3957 // Verify the audio devices size.
3958 AudioDeviceList audio_devices;
3959 cras_audio_handler_->GetAudioDevices(&audio_devices);
3960 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3961
3962 // Verify the mic Jack has been selected as the active input.
3963 EXPECT_EQ(kMicJackId, cras_audio_handler_->GetPrimaryActiveInputNode());
3964 EXPECT_TRUE(cras_audio_handler_->HasDualInternalMic());
3965
3966 // Change the active input to internal mic.
3967 cras_audio_handler_->SwitchToFrontOrRearMic();
3968 // Verify the active input has been switched to front mic.
3969 EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3970
3971 // Start the front facing camera.
3972 StartFrontFacingCamera();
3973 // Verify the front mic has been selected as the active input.
3974 EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3975
3976 // Stop the front facing camera.
3977 StopFrontFacingCamera();
3978 // Verify the active input remains as front mic.
3979 EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3980}
3981
3982TEST_P(CrasAudioHandlerTest, StartFrontCameraWithoutDualMic) {
3983 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalMic});
3984 SetUpCrasAudioHandler(audio_nodes);
3985
3986 // Verify the audio devices size.
3987 AudioDeviceList audio_devices;
3988 cras_audio_handler_->GetAudioDevices(&audio_devices);
3989 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3990
3991 // Verify the mic Jack has been selected as the active input.
3992 EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3993 EXPECT_FALSE(cras_audio_handler_->HasDualInternalMic());
3994
3995 // Start the front facing camera.
3996 StartFrontFacingCamera();
3997 // Verify the internal mic has been selected as the active input.
3998 EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3999
4000 // Stop the front facing camera.
4001 StopFrontFacingCamera();
4002 // Verify the active input remains as interanl mic.
4003 EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
4004}
4005
4006TEST_P(CrasAudioHandlerTest, FrontRearCameraBothOn) {
4007 AudioNodeList audio_nodes = GenerateAudioNodeList({kFrontMic, kRearMic});
4008 SetUpCrasAudioHandler(audio_nodes);
4009
4010 // Verify the audio devices size.
4011 AudioDeviceList audio_devices;
4012 cras_audio_handler_->GetAudioDevices(&audio_devices);
4013 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
4014
4015 // Verify the front mic has been selected as the active input.
4016 EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
4017 EXPECT_TRUE(cras_audio_handler_->HasDualInternalMic());
4018
4019 // Start the rear facing camera.
4020 StartRearFacingCamera();
4021 // Verify the rear mic has been selected as the active input.
4022 EXPECT_EQ(kRearMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
4023
4024 // Start front camera without stopping the front camera.
4025 StartFrontFacingCamera();
4026 // Verify the active microphone does not change.
4027 EXPECT_EQ(kRearMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
4028
4029 // Stop the front mic.
4030 StopFrontFacingCamera();
4031 // Verity the active mic does not change when there is still camera on.
4032 EXPECT_EQ(kRearMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
4033
4034 // Stop the rear mic.
4035 StopRearFacingCamera();
4036 // Verify the actice mic changes to front mic after both cameras stop.
4037 EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
4038}
4039
[email protected]ee38bc42013-07-29 21:41:324040} // namespace chromeos