blob: 9c84174548ade5b8f288abbadcc43e2d979067b4 [file] [log] [blame]
[email protected]ee38bc42013-07-29 21:41:321// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chromeos/audio/cras_audio_handler.h"
6
avi6e1a22d2015-12-21 03:43:207#include <stddef.h>
8#include <stdint.h>
9
dcheng0a6e80c2016-04-08 18:37:3810#include <memory>
11
jennyzc770dc772015-06-29 23:46:0612#include "base/bind.h"
avi6e1a22d2015-12-21 03:43:2013#include "base/macros.h"
[email protected]ee38bc42013-07-29 21:41:3214#include "base/memory/ref_counted.h"
[email protected]ee38bc42013-07-29 21:41:3215#include "base/message_loop/message_loop.h"
jennyzc770dc772015-06-29 23:46:0616#include "base/run_loop.h"
gabd85cc46b2016-05-11 18:23:2017#include "base/threading/thread_task_runner_handle.h"
[email protected]ee38bc42013-07-29 21:41:3218#include "base/values.h"
jennyzbd236742016-03-02 20:15:5219#include "chromeos/audio/audio_devices_pref_handler.h"
[email protected]ee38bc42013-07-29 21:41:3220#include "chromeos/audio/audio_devices_pref_handler_stub.h"
21#include "chromeos/dbus/audio_node.h"
[email protected]ee38bc42013-07-29 21:41:3222#include "chromeos/dbus/dbus_thread_manager.h"
satoruxcca5cc12014-10-27 16:33:5923#include "chromeos/dbus/fake_cras_audio_client.h"
jennyzfbaa3e62017-03-06 20:14:0224#include "media/base/video_facing.h"
[email protected]ee38bc42013-07-29 21:41:3225#include "testing/gtest/include/gtest/gtest.h"
26
27namespace chromeos {
oshima2aaee552015-07-09 16:50:2628namespace {
[email protected]ee38bc42013-07-29 21:41:3229
avi6e1a22d2015-12-21 03:43:2030const uint64_t kInternalSpeakerId = 10001;
31const uint64_t kHeadphoneId = 10002;
32const uint64_t kInternalMicId = 10003;
33const uint64_t kUSBMicId = 10004;
34const uint64_t kBluetoothHeadsetId = 10005;
35const uint64_t kHDMIOutputId = 10006;
36const uint64_t kUSBHeadphoneId1 = 10007;
37const uint64_t kUSBHeadphoneId2 = 10008;
38const uint64_t kMicJackId = 10009;
39const uint64_t kKeyboardMicId = 10010;
jennyzfbaa3e62017-03-06 20:14:0240const uint64_t kFrontMicId = 10011;
41const uint64_t kRearMicId = 10012;
avi6e1a22d2015-12-21 03:43:2042const uint64_t kOtherTypeOutputId = 90001;
43const uint64_t kOtherTypeInputId = 90002;
44const uint64_t kUSBJabraSpeakerOutputId1 = 90003;
45const uint64_t kUSBJabraSpeakerOutputId2 = 90004;
46const uint64_t kUSBJabraSpeakerInputId1 = 90005;
47const uint64_t kUSBJabraSpeakerInputId2 = 90006;
48const uint64_t kUSBCameraInputId = 90007;
[email protected]ee38bc42013-07-29 21:41:3249
tbarzic3ab01a22016-12-17 03:12:5350struct AudioNodeInfo {
51 bool is_input;
52 uint64_t id;
53 const char* const device_name;
54 const char* const type;
55 const char* const name;
56};
[email protected]ee38bc42013-07-29 21:41:3257
tbarzic3ab01a22016-12-17 03:12:5358const AudioNodeInfo kInternalSpeaker[] = {
59 {false, kInternalSpeakerId, "Fake Speaker", "INTERNAL_SPEAKER", "Speaker"}};
[email protected]ee38bc42013-07-29 21:41:3260
tbarzic3ab01a22016-12-17 03:12:5361const AudioNodeInfo kHeadphone[] = {
62 {false, kHeadphoneId, "Fake Headphone", "HEADPHONE", "Headphone"}};
[email protected]ee38bc42013-07-29 21:41:3263
tbarzic3ab01a22016-12-17 03:12:5364const AudioNodeInfo kInternalMic[] = {
65 {true, kInternalMicId, "Fake Mic", "INTERNAL_MIC", "Internal Mic"}};
[email protected]ce631ee2014-01-23 03:37:0666
tbarzic3ab01a22016-12-17 03:12:5367const AudioNodeInfo kMicJack[] = {
68 {true, kMicJackId, "Fake Mic Jack", "MIC", "Mic Jack"}};
[email protected]ee38bc42013-07-29 21:41:3269
tbarzic3ab01a22016-12-17 03:12:5370const AudioNodeInfo kUSBMic[] = {
71 {true, kUSBMicId, "Fake USB Mic", "USB", "USB Microphone"}};
[email protected]f9a49d342014-07-29 23:47:2072
tbarzic3ab01a22016-12-17 03:12:5373const AudioNodeInfo kKeyboardMic[] = {{true, kKeyboardMicId,
74 "Fake Keyboard Mic", "KEYBOARD_MIC",
75 "Keyboard Mic"}};
[email protected]ee38bc42013-07-29 21:41:3276
jennyzfbaa3e62017-03-06 20:14:0277const AudioNodeInfo kFrontMic[] = {
78 {true, kFrontMicId, "Fake Front Mic", "FRONT_MIC", "Front Mic"}};
79
80const AudioNodeInfo kRearMic[] = {
81 {true, kRearMicId, "Fake Rear Mic", "REAR_MIC", "Rear Mic"}};
82
tbarzic3ab01a22016-12-17 03:12:5383const AudioNodeInfo kOtherTypeOutput[] = {{false, kOtherTypeOutputId,
84 "Output Device", "SOME_OTHER_TYPE",
85 "Other Type Output Device"}};
[email protected]ee38bc42013-07-29 21:41:3286
tbarzic3ab01a22016-12-17 03:12:5387const AudioNodeInfo kOtherTypeInput[] = {{true, kOtherTypeInputId,
88 "Input Device", "SOME_OTHER_TYPE",
89 "Other Type Input Device"}};
[email protected]ee38bc42013-07-29 21:41:3290
tbarzic3ab01a22016-12-17 03:12:5391const AudioNodeInfo kBluetoothHeadset[] = {{false, kBluetoothHeadsetId,
92 "Bluetooth Headset", "BLUETOOTH",
93 "Bluetooth Headset 1"}};
[email protected]ee38bc42013-07-29 21:41:3294
tbarzic3ab01a22016-12-17 03:12:5395const AudioNodeInfo kHDMIOutput[] = {
96 {false, kHDMIOutputId, "HDMI output", "HDMI", "HDMI output"}};
[email protected]ee38bc42013-07-29 21:41:3297
tbarzic3ab01a22016-12-17 03:12:5398const AudioNodeInfo kUSBHeadphone1[] = {
99 {false, kUSBHeadphoneId1, "USB Headphone", "USB", "USB Headphone 1"}};
[email protected]ee38bc42013-07-29 21:41:32100
tbarzic3ab01a22016-12-17 03:12:53101const AudioNodeInfo kUSBHeadphone2[] = {
102 {false, kUSBHeadphoneId2, "USB Headphone", "USB", "USB Headphone 1"}};
jennyz2cb9eb12014-11-05 19:21:05103
tbarzic3ab01a22016-12-17 03:12:53104const AudioNodeInfo kUSBJabraSpeakerOutput1[] = {
105 {false, kUSBJabraSpeakerOutputId1, "Jabra Speaker 1", "USB",
106 "Jabra Speaker 1"}};
jennyz2cb9eb12014-11-05 19:21:05107
tbarzic3ab01a22016-12-17 03:12:53108const AudioNodeInfo kUSBJabraSpeakerOutput2[] = {
109 {false, kUSBJabraSpeakerOutputId2, "Jabra Speaker 2", "USB",
110 "Jabra Speaker 2"}};
jennyz2cb9eb12014-11-05 19:21:05111
tbarzic3ab01a22016-12-17 03:12:53112const AudioNodeInfo kUSBJabraSpeakerInput1[] = {{true, kUSBJabraSpeakerInputId1,
113 "Jabra Speaker 1", "USB",
114 "Jabra Speaker"}};
jennyz2cb9eb12014-11-05 19:21:05115
tbarzic3ab01a22016-12-17 03:12:53116const AudioNodeInfo kUSBJabraSpeakerInput2[] = {{true, kUSBJabraSpeakerInputId2,
117 "Jabra Speaker 2", "USB",
118 "Jabra Speaker 2"}};
119
120const AudioNodeInfo kUSBCameraInput[] = {
121 {true, kUSBCameraInputId, "USB Camera", "USB", "USB Camera"}};
[email protected]ee38bc42013-07-29 21:41:32122
123class TestObserver : public chromeos::CrasAudioHandler::AudioObserver {
124 public:
tbarzic3ab01a22016-12-17 03:12:53125 TestObserver() {}
[email protected]ee38bc42013-07-29 21:41:32126
127 int active_output_node_changed_count() const {
128 return active_output_node_changed_count_;
129 }
130
jennyz2cb9eb12014-11-05 19:21:05131 void reset_active_output_node_changed_count() {
132 active_output_node_changed_count_ = 0;
133 }
134
[email protected]ee38bc42013-07-29 21:41:32135 int active_input_node_changed_count() const {
136 return active_input_node_changed_count_;
137 }
138
jennyz2cb9eb12014-11-05 19:21:05139 void reset_active_input_node_changed_count() {
mukai307596b62014-11-24 19:56:59140 active_input_node_changed_count_ = 0;
jennyz2cb9eb12014-11-05 19:21:05141 }
142
[email protected]ee38bc42013-07-29 21:41:32143 int audio_nodes_changed_count() const {
144 return audio_nodes_changed_count_;
145 }
146
147 int output_mute_changed_count() const {
148 return output_mute_changed_count_;
149 }
150
jennyzc79ccda2015-07-28 23:04:35151 void reset_output_mute_changed_count() { input_mute_changed_count_ = 0; }
152
[email protected]ee38bc42013-07-29 21:41:32153 int input_mute_changed_count() const {
154 return input_mute_changed_count_;
155 }
156
157 int output_volume_changed_count() const {
158 return output_volume_changed_count_;
159 }
160
jennyz96526d02016-06-22 17:03:43161 void reset_output_volume_changed_count() { output_volume_changed_count_ = 0; }
162
[email protected]ee38bc42013-07-29 21:41:32163 int input_gain_changed_count() const {
164 return input_gain_changed_count_;
165 }
166
jennyzc79ccda2015-07-28 23:04:35167 bool output_mute_by_system() const { return output_mute_by_system_; }
168
warx74524622016-03-31 01:07:21169 int output_channel_remixing_changed_count() const {
170 return output_channel_remixing_changed_count_;
171 }
172
dchengae98daa2015-01-21 20:30:49173 ~TestObserver() override {}
[email protected]ee38bc42013-07-29 21:41:32174
175 protected:
176 // chromeos::CrasAudioHandler::AudioObserver overrides.
dchengae98daa2015-01-21 20:30:49177 void OnActiveOutputNodeChanged() override {
[email protected]ee38bc42013-07-29 21:41:32178 ++active_output_node_changed_count_;
179 }
180
dchengae98daa2015-01-21 20:30:49181 void OnActiveInputNodeChanged() override {
[email protected]ee38bc42013-07-29 21:41:32182 ++active_input_node_changed_count_;
183 }
184
dchengae98daa2015-01-21 20:30:49185 void OnAudioNodesChanged() override { ++audio_nodes_changed_count_; }
[email protected]ee38bc42013-07-29 21:41:32186
jennyzc79ccda2015-07-28 23:04:35187 void OnOutputMuteChanged(bool /* mute_on */, bool system_adjust) override {
jennyz170e7fb2015-03-30 23:37:39188 ++output_mute_changed_count_;
jennyzc79ccda2015-07-28 23:04:35189 output_mute_by_system_ = system_adjust;
jennyz170e7fb2015-03-30 23:37:39190 }
[email protected]ee38bc42013-07-29 21:41:32191
jennyz170e7fb2015-03-30 23:37:39192 void OnInputMuteChanged(bool /* mute_on */) override {
193 ++input_mute_changed_count_;
194 }
[email protected]ee38bc42013-07-29 21:41:32195
avi6e1a22d2015-12-21 03:43:20196 void OnOutputNodeVolumeChanged(uint64_t /* node_id */,
jennyz481292c2015-03-21 01:49:29197 int /* volume */) override {
198 ++output_volume_changed_count_;
199 }
[email protected]ee38bc42013-07-29 21:41:32200
avi6e1a22d2015-12-21 03:43:20201 void OnInputNodeGainChanged(uint64_t /* node_id */, int /* gain */) override {
jennyz481292c2015-03-21 01:49:29202 ++input_gain_changed_count_;
203 }
[email protected]ee38bc42013-07-29 21:41:32204
warx74524622016-03-31 01:07:21205 void OnOuputChannelRemixingChanged(bool /* mono_on */) override {
206 ++output_channel_remixing_changed_count_;
207 }
208
[email protected]ee38bc42013-07-29 21:41:32209 private:
tbarzic3ab01a22016-12-17 03:12:53210 int active_output_node_changed_count_ = 0;
211 int active_input_node_changed_count_ = 0;
212 int audio_nodes_changed_count_ = 0;
213 int output_mute_changed_count_ = 0;
214 int input_mute_changed_count_ = 0;
215 int output_volume_changed_count_ = 0;
216 int input_gain_changed_count_ = 0;
217 bool output_mute_by_system_ = false; // output mute state adjusted by system.
218 int output_channel_remixing_changed_count_ = 0;
[email protected]ee38bc42013-07-29 21:41:32219
220 DISALLOW_COPY_AND_ASSIGN(TestObserver);
221};
222
jennyzfbaa3e62017-03-06 20:14:02223class FakeVideoCaptureManager {
224 public:
225 FakeVideoCaptureManager() {}
226 virtual ~FakeVideoCaptureManager() {}
227
228 void AddObserver(media::VideoCaptureObserver* observer) {
229 observers_.AddObserver(observer);
230 }
231
232 void RemoveAllObservers() { observers_.Clear(); }
233
234 void NotifyVideoCaptureStarted(media::VideoFacingMode facing) {
235 for (auto& observer : observers_)
236 observer.OnVideoCaptureStarted(facing);
237 }
238
239 void NotifyVideoCaptureStopped(media::VideoFacingMode facing) {
240 for (auto& observer : observers_)
241 observer.OnVideoCaptureStopped(facing);
242 }
243
244 private:
245 base::ObserverList<media::VideoCaptureObserver> observers_;
246 DISALLOW_COPY_AND_ASSIGN(FakeVideoCaptureManager);
247};
248
oshima2aaee552015-07-09 16:50:26249} // namespace
250
tbarzic3ab01a22016-12-17 03:12:53251// Test param is the version of stabel device id used by audio node.
252class CrasAudioHandlerTest : public testing::TestWithParam<int> {
[email protected]ee38bc42013-07-29 21:41:32253 public:
tbarzic3ab01a22016-12-17 03:12:53254 CrasAudioHandlerTest() {}
dchengae98daa2015-01-21 20:30:49255 ~CrasAudioHandlerTest() override {}
[email protected]ee38bc42013-07-29 21:41:32256
jennyzfbaa3e62017-03-06 20:14:02257 void SetUp() override {
258 video_capture_manager_.reset(new FakeVideoCaptureManager);
259 }
[email protected]ee38bc42013-07-29 21:41:32260
dchengae98daa2015-01-21 20:30:49261 void TearDown() override {
[email protected]ee38bc42013-07-29 21:41:32262 cras_audio_handler_->RemoveAudioObserver(test_observer_.get());
263 test_observer_.reset();
jennyzfbaa3e62017-03-06 20:14:02264 video_capture_manager_->RemoveAllObservers();
265 video_capture_manager_.reset();
[email protected]ee38bc42013-07-29 21:41:32266 CrasAudioHandler::Shutdown();
tbarzic3ab01a22016-12-17 03:12:53267 audio_pref_handler_ = nullptr;
[email protected]ee38bc42013-07-29 21:41:32268 DBusThreadManager::Shutdown();
269 }
270
tbarzic3ab01a22016-12-17 03:12:53271 AudioNode GenerateAudioNode(const AudioNodeInfo* node_info) {
272 uint64_t stable_device_id_v2 = GetParam() == 1 ? 0 : (node_info->id ^ 0xFF);
273 uint64_t stable_device_id_v1 = node_info->id;
274 return AudioNode(node_info->is_input, node_info->id, GetParam() == 2,
275 stable_device_id_v1, stable_device_id_v2,
276 node_info->device_name, node_info->type, node_info->name,
277 false /* is_active*/, 0 /* pluged_time */);
278 }
279
280 AudioNodeList GenerateAudioNodeList(
281 const std::vector<const AudioNodeInfo*> nodes) {
282 AudioNodeList node_list;
vmpstr843b41a2017-03-01 21:15:03283 for (auto* node_info : nodes) {
tbarzic3ab01a22016-12-17 03:12:53284 node_list.push_back(GenerateAudioNode(node_info));
285 }
286 return node_list;
287 }
288
[email protected]ee38bc42013-07-29 21:41:32289 void SetUpCrasAudioHandler(const AudioNodeList& audio_nodes) {
zelidrag29fe3382014-08-27 01:44:48290 DBusThreadManager::Initialize();
satoruxcca5cc12014-10-27 16:33:59291 fake_cras_audio_client_ = static_cast<FakeCrasAudioClient*>(
[email protected]ee38bc42013-07-29 21:41:32292 DBusThreadManager::Get()->GetCrasAudioClient());
satoruxcca5cc12014-10-27 16:33:59293 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes);
[email protected]ee38bc42013-07-29 21:41:32294 audio_pref_handler_ = new AudioDevicesPrefHandlerStub();
295 CrasAudioHandler::Initialize(audio_pref_handler_);
296 cras_audio_handler_ = CrasAudioHandler::Get();
297 test_observer_.reset(new TestObserver);
298 cras_audio_handler_->AddAudioObserver(test_observer_.get());
jennyzfbaa3e62017-03-06 20:14:02299 video_capture_manager_->AddObserver(cras_audio_handler_);
fdorayf5b47fd12016-09-13 14:12:36300 base::RunLoop().RunUntilIdle();
[email protected]ee38bc42013-07-29 21:41:32301 }
302
jennyzbd236742016-03-02 20:15:52303 // Set up cras audio handlers with |audio_nodes| and set the active state of
304 // |active_device_in_pref| as active and |activate_by_user| in the pref,
305 // and rest of nodes in |audio_nodes_in_pref| as inactive.
306 void SetupCrasAudioHandlerWithActiveNodeInPref(
307 const AudioNodeList& audio_nodes,
308 const AudioNodeList& audio_nodes_in_pref,
309 const AudioDevice& active_device_in_pref,
310 bool activate_by_user) {
311 DBusThreadManager::Initialize();
312 fake_cras_audio_client_ = static_cast<FakeCrasAudioClient*>(
313 DBusThreadManager::Get()->GetCrasAudioClient());
314 audio_pref_handler_ = new AudioDevicesPrefHandlerStub();
315 bool active;
316 for (const AudioNode& node : audio_nodes_in_pref) {
317 active = node.id == active_device_in_pref.id;
318 audio_pref_handler_->SetDeviceActive(AudioDevice(node), active,
319 activate_by_user);
320 }
321
322 bool activate_by;
323 EXPECT_TRUE(audio_pref_handler_->GetDeviceActive(active_device_in_pref,
324 &active, &activate_by));
325 EXPECT_TRUE(active);
326 EXPECT_EQ(activate_by, activate_by_user);
327
328 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes);
329 CrasAudioHandler::Initialize(audio_pref_handler_);
330
331 cras_audio_handler_ = CrasAudioHandler::Get();
332 test_observer_.reset(new TestObserver);
333 cras_audio_handler_->AddAudioObserver(test_observer_.get());
fdorayf5b47fd12016-09-13 14:12:36334 base::RunLoop().RunUntilIdle();
jennyzbd236742016-03-02 20:15:52335 }
336
jennyzb47947f2014-09-25 00:42:04337 void SetUpCrasAudioHandlerWithPrimaryActiveNode(
338 const AudioNodeList& audio_nodes,
339 const AudioNode& primary_active_node) {
340 DBusThreadManager::Initialize();
satoruxcca5cc12014-10-27 16:33:59341 fake_cras_audio_client_ = static_cast<FakeCrasAudioClient*>(
jennyzb47947f2014-09-25 00:42:04342 DBusThreadManager::Get()->GetCrasAudioClient());
satoruxcca5cc12014-10-27 16:33:59343 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes);
344 fake_cras_audio_client_->SetActiveOutputNode(primary_active_node.id),
jennyzb47947f2014-09-25 00:42:04345 audio_pref_handler_ = new AudioDevicesPrefHandlerStub();
346 CrasAudioHandler::Initialize(audio_pref_handler_);
347 cras_audio_handler_ = CrasAudioHandler::Get();
348 test_observer_.reset(new TestObserver);
349 cras_audio_handler_->AddAudioObserver(test_observer_.get());
fdorayf5b47fd12016-09-13 14:12:36350 base::RunLoop().RunUntilIdle();
jennyzb47947f2014-09-25 00:42:04351 }
352
[email protected]ee38bc42013-07-29 21:41:32353 void ChangeAudioNodes(const AudioNodeList& audio_nodes) {
satoruxcca5cc12014-10-27 16:33:59354 fake_cras_audio_client_->SetAudioNodesAndNotifyObserversForTesting(
derat3698adff2014-08-29 16:10:43355 audio_nodes);
fdorayf5b47fd12016-09-13 14:12:36356 base::RunLoop().RunUntilIdle();
[email protected]ee38bc42013-07-29 21:41:32357 }
358
avi6e1a22d2015-12-21 03:43:20359 const AudioDevice* GetDeviceFromId(uint64_t id) {
jennyzb47947f2014-09-25 00:42:04360 return cras_audio_handler_->GetDeviceFromId(id);
361 }
362
jennyz2cb9eb12014-11-05 19:21:05363 int GetActiveDeviceCount() const {
364 int num_active_nodes = 0;
365 AudioDeviceList audio_devices;
366 cras_audio_handler_->GetAudioDevices(&audio_devices);
367 for (size_t i = 0; i < audio_devices.size(); ++i) {
368 if (audio_devices[i].active)
369 ++num_active_nodes;
370 }
371 return num_active_nodes;
372 }
373
jennyzc770dc772015-06-29 23:46:06374 void SetActiveHDMIRediscover() {
375 cras_audio_handler_->SetActiveHDMIOutoutRediscoveringIfNecessary(true);
376 }
377
378 void SetHDMIRediscoverGracePeriodDuration(int duration_in_ms) {
379 cras_audio_handler_->SetHDMIRediscoverGracePeriodForTesting(duration_in_ms);
380 }
381
382 bool IsDuringHDMIRediscoverGracePeriod() {
383 return cras_audio_handler_->hdmi_rediscovering();
384 }
385
warxfd7c37b2016-08-05 19:38:07386 void RestartAudioClient() {
387 cras_audio_handler_->AudioClientRestarted();
fdorayf5b47fd12016-09-13 14:12:36388 base::RunLoop().RunUntilIdle();
warxfd7c37b2016-08-05 19:38:07389 }
390
jennyzfbaa3e62017-03-06 20:14:02391 void StartFrontFacingCamera() {
392 video_capture_manager_->NotifyVideoCaptureStarted(
393 media::MEDIA_VIDEO_FACING_USER);
394 }
395
396 void StopFrontFacingCamera() {
397 video_capture_manager_->NotifyVideoCaptureStopped(
398 media::MEDIA_VIDEO_FACING_USER);
399 }
400
401 void StartRearFacingCamera() {
402 video_capture_manager_->NotifyVideoCaptureStarted(
403 media::MEDIA_VIDEO_FACING_ENVIRONMENT);
404 }
405
406 void StopRearFacingCamera() {
407 video_capture_manager_->NotifyVideoCaptureStopped(
408 media::MEDIA_VIDEO_FACING_ENVIRONMENT);
409 }
410
[email protected]ee38bc42013-07-29 21:41:32411 protected:
412 base::MessageLoopForUI message_loop_;
tbarzic3ab01a22016-12-17 03:12:53413 CrasAudioHandler* cras_audio_handler_ = nullptr; // Not owned.
414 FakeCrasAudioClient* fake_cras_audio_client_ = nullptr; // Not owned.
dcheng0a6e80c2016-04-08 18:37:38415 std::unique_ptr<TestObserver> test_observer_;
[email protected]ee38bc42013-07-29 21:41:32416 scoped_refptr<AudioDevicesPrefHandlerStub> audio_pref_handler_;
jennyzfbaa3e62017-03-06 20:14:02417 std::unique_ptr<FakeVideoCaptureManager> video_capture_manager_;
[email protected]ee38bc42013-07-29 21:41:32418
419 private:
420 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandlerTest);
421};
422
jennyzc770dc772015-06-29 23:46:06423class HDMIRediscoverWaiter {
424 public:
425 HDMIRediscoverWaiter(CrasAudioHandlerTest* cras_audio_handler_test,
426 int grace_period_duration_in_ms)
427 : cras_audio_handler_test_(cras_audio_handler_test),
428 grace_period_duration_in_ms_(grace_period_duration_in_ms) {}
429
430 void WaitUntilTimeOut(int wait_duration_in_ms) {
431 base::RunLoop run_loop;
432 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
433 FROM_HERE, run_loop.QuitClosure(),
434 base::TimeDelta::FromMilliseconds(wait_duration_in_ms));
435 run_loop.Run();
436 }
437
438 void CheckHDMIRediscoverGracePeriodEnd(const base::Closure& quit_loop_func) {
439 if (!cras_audio_handler_test_->IsDuringHDMIRediscoverGracePeriod()) {
440 quit_loop_func.Run();
441 return;
442 }
443 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
444 FROM_HERE,
445 base::Bind(&HDMIRediscoverWaiter::CheckHDMIRediscoverGracePeriodEnd,
446 base::Unretained(this), quit_loop_func),
447 base::TimeDelta::FromMilliseconds(grace_period_duration_in_ms_ / 4));
448 }
449
450 void WaitUntilHDMIRediscoverGracePeriodEnd() {
451 base::RunLoop run_loop;
452 CheckHDMIRediscoverGracePeriodEnd(run_loop.QuitClosure());
453 run_loop.Run();
454 }
455
456 private:
457 CrasAudioHandlerTest* cras_audio_handler_test_; // not owned
458 int grace_period_duration_in_ms_;
459
460 DISALLOW_COPY_AND_ASSIGN(HDMIRediscoverWaiter);
461};
462
tbarzic3ab01a22016-12-17 03:12:53463INSTANTIATE_TEST_CASE_P(StableIdV1, CrasAudioHandlerTest, testing::Values(1));
464INSTANTIATE_TEST_CASE_P(StabelIdV2, CrasAudioHandlerTest, testing::Values(2));
465
466TEST_P(CrasAudioHandlerTest, InitializeWithOnlyDefaultAudioDevices) {
467 AudioNodeList audio_nodes =
468 GenerateAudioNodeList({kInternalSpeaker, kInternalMic});
[email protected]ee38bc42013-07-29 21:41:32469 SetUpCrasAudioHandler(audio_nodes);
470
471 // Verify the audio devices size.
472 AudioDeviceList audio_devices;
473 cras_audio_handler_->GetAudioDevices(&audio_devices);
474 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
475
476 // Verify the internal speaker has been selected as the active output.
477 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04478 EXPECT_TRUE(
479 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53480 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
481 EXPECT_EQ(kInternalSpeaker->id,
jennyzb47947f2014-09-25 00:42:04482 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32483 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
484
485 // Ensure the internal microphone has been selected as the active input.
486 AudioDevice active_input;
tbarzic3ab01a22016-12-17 03:12:53487 EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:32488 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
489}
490
tbarzic3ab01a22016-12-17 03:12:53491TEST_P(CrasAudioHandlerTest, InitializeWithAlternativeAudioDevices) {
492 AudioNodeList audio_nodes = GenerateAudioNodeList(
493 {kInternalSpeaker, kHeadphone, kInternalMic, kUSBMic});
[email protected]ee38bc42013-07-29 21:41:32494 SetUpCrasAudioHandler(audio_nodes);
495
496 // Verify the audio devices size.
497 AudioDeviceList audio_devices;
498 cras_audio_handler_->GetAudioDevices(&audio_devices);
499 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
500
501 // Verify the headphone has been selected as the active output.
502 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04503 EXPECT_TRUE(
504 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53505 EXPECT_EQ(kHeadphone->id, active_output.id);
506 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32507 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
508
509 // Ensure the USB microphone has been selected as the active input.
jennyzb47947f2014-09-25 00:42:04510 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:32511 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
512}
513
tbarzic3ab01a22016-12-17 03:12:53514TEST_P(CrasAudioHandlerTest, InitializeWithKeyboardMic) {
515 AudioNodeList audio_nodes =
516 GenerateAudioNodeList({kInternalSpeaker, kInternalMic, kKeyboardMic});
[email protected]f9a49d342014-07-29 23:47:20517 SetUpCrasAudioHandler(audio_nodes);
518
519 // Verify the audio devices size.
520 AudioDeviceList audio_devices;
521 cras_audio_handler_->GetAudioDevices(&audio_devices);
522 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
jennyzb47947f2014-09-25 00:42:04523 EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic());
[email protected]f9a49d342014-07-29 23:47:20524
525 // Verify the internal speaker has been selected as the active output.
526 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04527 EXPECT_TRUE(
528 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53529 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
530 EXPECT_EQ(kInternalSpeaker->id,
jennyzb47947f2014-09-25 00:42:04531 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]f9a49d342014-07-29 23:47:20532 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
533
534 // Ensure the internal microphone has been selected as the active input,
535 // not affected by keyboard mic.
536 AudioDevice active_input;
tbarzic3ab01a22016-12-17 03:12:53537 EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]f9a49d342014-07-29 23:47:20538 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
tbarzic3ab01a22016-12-17 03:12:53539 const AudioDevice* keyboard_mic = GetDeviceFromId(kKeyboardMic->id);
jennyzb47947f2014-09-25 00:42:04540 EXPECT_FALSE(keyboard_mic->active);
541}
542
tbarzic3ab01a22016-12-17 03:12:53543TEST_P(CrasAudioHandlerTest, SetKeyboardMicActive) {
544 AudioNodeList audio_nodes =
545 GenerateAudioNodeList({kInternalMic, kKeyboardMic});
jennyzb47947f2014-09-25 00:42:04546 SetUpCrasAudioHandler(audio_nodes);
547
548 // Verify the audio devices size.
549 AudioDeviceList audio_devices;
550 cras_audio_handler_->GetAudioDevices(&audio_devices);
551 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
552 EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic());
553
554 // Ensure the internal microphone has been selected as the active input,
555 // not affected by keyboard mic.
556 AudioDevice active_input;
tbarzic3ab01a22016-12-17 03:12:53557 EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
jennyzb47947f2014-09-25 00:42:04558 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
tbarzic3ab01a22016-12-17 03:12:53559 const AudioDevice* keyboard_mic = GetDeviceFromId(kKeyboardMic->id);
jennyzb47947f2014-09-25 00:42:04560 EXPECT_FALSE(keyboard_mic->active);
561
562 // Make keyboard mic active.
563 cras_audio_handler_->SetKeyboardMicActive(true);
tbarzic3ab01a22016-12-17 03:12:53564 EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
565 const AudioDevice* active_keyboard_mic = GetDeviceFromId(kKeyboardMic->id);
jennyzb47947f2014-09-25 00:42:04566 EXPECT_TRUE(active_keyboard_mic->active);
567
568 // Make keyboard mic inactive.
569 cras_audio_handler_->SetKeyboardMicActive(false);
tbarzic3ab01a22016-12-17 03:12:53570 EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
571 const AudioDevice* inactive_keyboard_mic = GetDeviceFromId(kKeyboardMic->id);
jennyzb47947f2014-09-25 00:42:04572 EXPECT_FALSE(inactive_keyboard_mic->active);
[email protected]f9a49d342014-07-29 23:47:20573}
574
tbarzic3ab01a22016-12-17 03:12:53575TEST_P(CrasAudioHandlerTest, KeyboardMicNotSetAsPrimaryActive) {
576 AudioNodeList audio_nodes = GenerateAudioNodeList({kKeyboardMic});
jennyzb46664f2016-03-08 18:18:10577 SetUpCrasAudioHandler(audio_nodes);
578
579 // Verify keyboard mic is not set as primary active input.
580 AudioDeviceList audio_devices;
581 cras_audio_handler_->GetAudioDevices(&audio_devices);
582 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
583 EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic());
584 EXPECT_EQ(0u, cras_audio_handler_->GetPrimaryActiveInputNode());
585
586 // Verify the internal mic is set as primary input.
tbarzic3ab01a22016-12-17 03:12:53587 audio_nodes.push_back(GenerateAudioNode(kInternalMic));
jennyzb46664f2016-03-08 18:18:10588 ChangeAudioNodes(audio_nodes);
589 cras_audio_handler_->GetAudioDevices(&audio_devices);
590 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
591 EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic());
tbarzic3ab01a22016-12-17 03:12:53592 EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
jennyzb46664f2016-03-08 18:18:10593}
594
tbarzic3ab01a22016-12-17 03:12:53595TEST_P(CrasAudioHandlerTest, SwitchActiveOutputDevice) {
596 AudioNodeList audio_nodes =
597 GenerateAudioNodeList({kInternalSpeaker, kHeadphone});
[email protected]ee38bc42013-07-29 21:41:32598 SetUpCrasAudioHandler(audio_nodes);
tbarzic3ab01a22016-12-17 03:12:53599
[email protected]ee38bc42013-07-29 21:41:32600 AudioDeviceList audio_devices;
601 cras_audio_handler_->GetAudioDevices(&audio_devices);
602 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
603
604 // Verify the initial active output device is headphone.
605 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
606 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04607 EXPECT_TRUE(
608 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53609 EXPECT_EQ(kHeadphone->id, active_output.id);
610 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32611
612 // Switch the active output to internal speaker.
tbarzic3ab01a22016-12-17 03:12:53613 AudioDevice internal_speaker(GenerateAudioNode(kInternalSpeaker));
jennyzbd236742016-03-02 20:15:52614 cras_audio_handler_->SwitchToDevice(internal_speaker, true,
615 CrasAudioHandler::ACTIVATE_BY_USER);
[email protected]ee38bc42013-07-29 21:41:32616
617 // Verify the active output is switched to internal speaker, and the
618 // ActiveOutputNodeChanged event is fired.
619 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04620 EXPECT_TRUE(
621 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53622 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
623 EXPECT_EQ(kInternalSpeaker->id,
jennyzb47947f2014-09-25 00:42:04624 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32625}
626
tbarzic3ab01a22016-12-17 03:12:53627TEST_P(CrasAudioHandlerTest, SwitchActiveInputDevice) {
628 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalMic, kUSBMic});
[email protected]ee38bc42013-07-29 21:41:32629 SetUpCrasAudioHandler(audio_nodes);
tbarzic3ab01a22016-12-17 03:12:53630
[email protected]ee38bc42013-07-29 21:41:32631 AudioDeviceList audio_devices;
632 cras_audio_handler_->GetAudioDevices(&audio_devices);
633 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
634
635 // Verify the initial active input device is USB mic.
636 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:04637 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:32638
639 // Switch the active input to internal mic.
tbarzic3ab01a22016-12-17 03:12:53640 AudioDevice internal_mic(GenerateAudioNode(kInternalMic));
jennyzbd236742016-03-02 20:15:52641 cras_audio_handler_->SwitchToDevice(internal_mic, true,
642 CrasAudioHandler::ACTIVATE_BY_USER);
[email protected]ee38bc42013-07-29 21:41:32643
644 // Verify the active output is switched to internal speaker, and the active
645 // ActiveInputNodeChanged event is fired.
646 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
tbarzic3ab01a22016-12-17 03:12:53647 EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:32648}
649
tbarzic3ab01a22016-12-17 03:12:53650TEST_P(CrasAudioHandlerTest, PlugHeadphone) {
[email protected]ee38bc42013-07-29 21:41:32651 // Set up initial audio devices, only with internal speaker.
tbarzic3ab01a22016-12-17 03:12:53652 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker});
[email protected]ee38bc42013-07-29 21:41:32653 SetUpCrasAudioHandler(audio_nodes);
654 const size_t init_nodes_size = audio_nodes.size();
655
656 // Verify the audio devices size.
657 AudioDeviceList audio_devices;
658 cras_audio_handler_->GetAudioDevices(&audio_devices);
659 EXPECT_EQ(init_nodes_size, audio_devices.size());
660 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
661
662 // Verify the internal speaker has been selected as the active output.
663 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
664 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04665 EXPECT_TRUE(
666 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53667 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
668 EXPECT_EQ(kInternalSpeaker->id,
jennyzb47947f2014-09-25 00:42:04669 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32670 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
671
672 // Plug the headphone.
[email protected]4642d7792013-09-18 20:27:17673 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:53674 AudioNode internal_speaker = GenerateAudioNode(kInternalSpeaker);
[email protected]4642d7792013-09-18 20:27:17675 internal_speaker.active = true;
676 audio_nodes.push_back(internal_speaker);
tbarzic3ab01a22016-12-17 03:12:53677 audio_nodes.push_back(GenerateAudioNode(kHeadphone));
[email protected]ee38bc42013-07-29 21:41:32678 ChangeAudioNodes(audio_nodes);
679
680 // Verify the AudioNodesChanged event is fired and new audio device is added.
681 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
682 cras_audio_handler_->GetAudioDevices(&audio_devices);
683 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
684
685 // Verify the active output device is switched to headphone and
686 // ActiveOutputChanged event is fired.
687 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04688 EXPECT_TRUE(
689 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53690 EXPECT_EQ(kHeadphone->id, active_output.id);
691 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32692 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
693}
694
tbarzic3ab01a22016-12-17 03:12:53695TEST_P(CrasAudioHandlerTest, UnplugHeadphone) {
[email protected]ee38bc42013-07-29 21:41:32696 // Set up initial audio devices, with internal speaker and headphone.
tbarzic3ab01a22016-12-17 03:12:53697 AudioNodeList audio_nodes =
698 GenerateAudioNodeList({kInternalSpeaker, kHeadphone});
[email protected]ee38bc42013-07-29 21:41:32699 SetUpCrasAudioHandler(audio_nodes);
700 const size_t init_nodes_size = audio_nodes.size();
701
702 // Verify the audio devices size.
703 AudioDeviceList audio_devices;
704 cras_audio_handler_->GetAudioDevices(&audio_devices);
705 EXPECT_EQ(init_nodes_size, audio_devices.size());
706 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
707
708 // Verify the headphone has been selected as the active output.
709 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
710 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04711 EXPECT_TRUE(
712 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53713 EXPECT_EQ(kHeadphone->id, active_output.id);
714 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32715 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
716
717 // Unplug the headphone.
718 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:53719 audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
[email protected]ee38bc42013-07-29 21:41:32720 ChangeAudioNodes(audio_nodes);
721
722 // Verify the AudioNodesChanged event is fired and one audio device is
723 // removed.
724 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
725 cras_audio_handler_->GetAudioDevices(&audio_devices);
726 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
727
728 // Verify the active output device is switched to internal speaker and
729 // ActiveOutputChanged event is fired.
730 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04731 EXPECT_TRUE(
732 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53733 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
734 EXPECT_EQ(kInternalSpeaker->id,
jennyzb47947f2014-09-25 00:42:04735 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32736 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
737}
738
tbarzic3ab01a22016-12-17 03:12:53739TEST_P(CrasAudioHandlerTest, InitializeWithBluetoothHeadset) {
740 AudioNodeList audio_nodes =
741 GenerateAudioNodeList({kInternalSpeaker, kBluetoothHeadset});
[email protected]ee38bc42013-07-29 21:41:32742 SetUpCrasAudioHandler(audio_nodes);
743
744 // Verify the audio devices size.
745 AudioDeviceList audio_devices;
746 cras_audio_handler_->GetAudioDevices(&audio_devices);
747 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
748 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
749
750 // Verify the bluetooth headset has been selected as the active output.
751 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
752 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04753 EXPECT_TRUE(
754 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53755 EXPECT_EQ(kBluetoothHeadset->id, active_output.id);
756 EXPECT_EQ(kBluetoothHeadset->id,
jennyzb47947f2014-09-25 00:42:04757 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32758 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
759}
760
tbarzic3ab01a22016-12-17 03:12:53761TEST_P(CrasAudioHandlerTest, ConnectAndDisconnectBluetoothHeadset) {
[email protected]ee38bc42013-07-29 21:41:32762 // Initialize with internal speaker and headphone.
tbarzic3ab01a22016-12-17 03:12:53763 AudioNodeList audio_nodes =
764 GenerateAudioNodeList({kInternalSpeaker, kHeadphone});
[email protected]ee38bc42013-07-29 21:41:32765 SetUpCrasAudioHandler(audio_nodes);
766 const size_t init_nodes_size = audio_nodes.size();
767
768 // Verify the audio devices size.
769 AudioDeviceList audio_devices;
770 cras_audio_handler_->GetAudioDevices(&audio_devices);
771 EXPECT_EQ(init_nodes_size, audio_devices.size());
772 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
773
774 // Verify the headphone is selected as the active output initially.
775 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
776 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04777 EXPECT_TRUE(
778 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53779 EXPECT_EQ(kHeadphone->id, active_output.id);
780 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32781 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
782
783 // Connect to bluetooth headset. Since it is plugged in later than
784 // headphone, active output should be switched to it.
785 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:53786 audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
787 AudioNode headphone = GenerateAudioNode(kHeadphone);
[email protected]ee38bc42013-07-29 21:41:32788 headphone.plugged_time = 80000000;
789 headphone.active = true;
790 audio_nodes.push_back(headphone);
tbarzic3ab01a22016-12-17 03:12:53791 AudioNode bluetooth_headset = GenerateAudioNode(kBluetoothHeadset);
[email protected]ee38bc42013-07-29 21:41:32792 bluetooth_headset.plugged_time = 90000000;
793 audio_nodes.push_back(bluetooth_headset);
794 ChangeAudioNodes(audio_nodes);
795
796 // Verify the AudioNodesChanged event is fired and new audio device is added.
797 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
798 cras_audio_handler_->GetAudioDevices(&audio_devices);
799 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
800
801 // Verify the active output device is switched to bluetooth headset, and
802 // ActiveOutputChanged event is fired.
803 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04804 EXPECT_TRUE(
805 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53806 EXPECT_EQ(kBluetoothHeadset->id, active_output.id);
807 EXPECT_EQ(kBluetoothHeadset->id,
jennyzb47947f2014-09-25 00:42:04808 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32809 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
810
811 // Disconnect bluetooth headset.
812 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:53813 audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
[email protected]4642d7792013-09-18 20:27:17814 headphone.active = false;
[email protected]ee38bc42013-07-29 21:41:32815 audio_nodes.push_back(headphone);
816 ChangeAudioNodes(audio_nodes);
817
818 // Verify the AudioNodesChanged event is fired and one audio device is
819 // removed.
820 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
821 cras_audio_handler_->GetAudioDevices(&audio_devices);
822 EXPECT_EQ(init_nodes_size, audio_devices.size());
823
824 // Verify the active output device is switched to headphone, and
825 // ActiveOutputChanged event is fired.
826 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04827 EXPECT_TRUE(
828 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53829 EXPECT_EQ(kHeadphone->id, active_output.id);
830 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32831 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
832}
833
tbarzic3ab01a22016-12-17 03:12:53834TEST_P(CrasAudioHandlerTest, InitializeWithHDMIOutput) {
835 AudioNodeList audio_nodes =
836 GenerateAudioNodeList({kInternalSpeaker, kHDMIOutput});
[email protected]ee38bc42013-07-29 21:41:32837 SetUpCrasAudioHandler(audio_nodes);
838
839 // Verify the audio devices size.
840 AudioDeviceList audio_devices;
841 cras_audio_handler_->GetAudioDevices(&audio_devices);
842 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
843 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
844
845 // Verify the HDMI device has been selected as the active output.
846 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
847 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04848 EXPECT_TRUE(
849 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53850 EXPECT_EQ(kHDMIOutput->id, active_output.id);
851 EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32852 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
853}
854
tbarzic3ab01a22016-12-17 03:12:53855TEST_P(CrasAudioHandlerTest, ConnectAndDisconnectHDMIOutput) {
[email protected]ee38bc42013-07-29 21:41:32856 // Initialize with internal speaker.
tbarzic3ab01a22016-12-17 03:12:53857 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker});
[email protected]ee38bc42013-07-29 21:41:32858 SetUpCrasAudioHandler(audio_nodes);
859 const size_t init_nodes_size = audio_nodes.size();
860
861 // Verify the audio devices size.
862 AudioDeviceList audio_devices;
863 cras_audio_handler_->GetAudioDevices(&audio_devices);
864 EXPECT_EQ(init_nodes_size, audio_devices.size());
865 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
866
867 // Verify the internal speaker is selected as the active output initially.
868 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
869 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04870 EXPECT_TRUE(
871 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53872 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
873 EXPECT_EQ(kInternalSpeaker->id,
jennyzb47947f2014-09-25 00:42:04874 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32875 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
876
877 // Connect to HDMI output.
878 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:53879 AudioNode internal_speaker = GenerateAudioNode(kInternalSpeaker);
[email protected]ee38bc42013-07-29 21:41:32880 internal_speaker.active = true;
881 internal_speaker.plugged_time = 80000000;
882 audio_nodes.push_back(internal_speaker);
tbarzic3ab01a22016-12-17 03:12:53883 AudioNode hdmi = GenerateAudioNode(kHDMIOutput);
[email protected]ee38bc42013-07-29 21:41:32884 hdmi.plugged_time = 90000000;
885 audio_nodes.push_back(hdmi);
886 ChangeAudioNodes(audio_nodes);
887
888 // Verify the AudioNodesChanged event is fired and new audio device is added.
889 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
890 cras_audio_handler_->GetAudioDevices(&audio_devices);
891 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
892
893 // Verify the active output device is switched to hdmi output, and
894 // ActiveOutputChanged event is fired.
895 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04896 EXPECT_TRUE(
897 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53898 EXPECT_EQ(kHDMIOutput->id, active_output.id);
899 EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32900 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
901
902 // Disconnect hdmi headset.
903 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:53904 audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
[email protected]ee38bc42013-07-29 21:41:32905 ChangeAudioNodes(audio_nodes);
906
907 // Verify the AudioNodesChanged event is fired and one audio device is
908 // removed.
909 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
910 cras_audio_handler_->GetAudioDevices(&audio_devices);
911 EXPECT_EQ(init_nodes_size, audio_devices.size());
912
913 // Verify the active output device is switched to internal speaker, and
914 // ActiveOutputChanged event is fired.
915 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04916 EXPECT_TRUE(
917 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53918 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
919 EXPECT_EQ(kInternalSpeaker->id,
jennyzb47947f2014-09-25 00:42:04920 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32921 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
922}
923
tbarzic3ab01a22016-12-17 03:12:53924TEST_P(CrasAudioHandlerTest, HandleHeadphoneAndHDMIOutput) {
[email protected]ee38bc42013-07-29 21:41:32925 // Initialize with internal speaker, headphone and HDMI output.
tbarzic3ab01a22016-12-17 03:12:53926 AudioNodeList audio_nodes =
927 GenerateAudioNodeList({kInternalSpeaker, kHeadphone, kHDMIOutput});
[email protected]ee38bc42013-07-29 21:41:32928 SetUpCrasAudioHandler(audio_nodes);
929 const size_t init_nodes_size = audio_nodes.size();
930
931 // Verify the audio devices size.
932 AudioDeviceList audio_devices;
933 cras_audio_handler_->GetAudioDevices(&audio_devices);
934 EXPECT_EQ(init_nodes_size, audio_devices.size());
935 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
936
937 // Verify the headphone is selected as the active output initially.
938 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
939 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04940 EXPECT_TRUE(
941 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53942 EXPECT_EQ(kHeadphone->id, active_output.id);
943 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32944 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
945
946 // Disconnect HDMI output.
947 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:53948 audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
949 audio_nodes.push_back(GenerateAudioNode(kHDMIOutput));
[email protected]ee38bc42013-07-29 21:41:32950 ChangeAudioNodes(audio_nodes);
951
952 // Verify the AudioNodesChanged event is fired and one audio device is
953 // removed.
954 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
955 cras_audio_handler_->GetAudioDevices(&audio_devices);
956 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
957
958 // Verify the active output device is switched to HDMI output, and
959 // ActiveOutputChanged event is fired.
960 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:04961 EXPECT_TRUE(
962 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53963 EXPECT_EQ(kHDMIOutput->id, active_output.id);
964 EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32965 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
966}
967
tbarzic3ab01a22016-12-17 03:12:53968TEST_P(CrasAudioHandlerTest, InitializeWithUSBHeadphone) {
969 AudioNodeList audio_nodes =
970 GenerateAudioNodeList({kInternalSpeaker, kUSBHeadphone1});
[email protected]ee38bc42013-07-29 21:41:32971 SetUpCrasAudioHandler(audio_nodes);
972
973 // Verify the audio devices size.
974 AudioDeviceList audio_devices;
975 cras_audio_handler_->GetAudioDevices(&audio_devices);
976 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
977 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
978
979 // Verify the usb headphone has been selected as the active output.
980 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
981 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:04982 EXPECT_TRUE(
983 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:53984 EXPECT_EQ(kUSBHeadphone1->id, active_output.id);
985 EXPECT_EQ(kUSBHeadphone1->id,
jennyzb47947f2014-09-25 00:42:04986 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:32987 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
988}
989
tbarzic3ab01a22016-12-17 03:12:53990TEST_P(CrasAudioHandlerTest, PlugAndUnplugUSBHeadphone) {
[email protected]ee38bc42013-07-29 21:41:32991 // Initialize with internal speaker.
tbarzic3ab01a22016-12-17 03:12:53992 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker});
[email protected]ee38bc42013-07-29 21:41:32993 SetUpCrasAudioHandler(audio_nodes);
994 const size_t init_nodes_size = audio_nodes.size();
995
996 // Verify the audio devices size.
997 AudioDeviceList audio_devices;
998 cras_audio_handler_->GetAudioDevices(&audio_devices);
999 EXPECT_EQ(init_nodes_size, audio_devices.size());
1000 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1001
1002 // Verify the internal speaker is selected as the active output initially.
1003 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1004 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041005 EXPECT_TRUE(
1006 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531007 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
1008 EXPECT_EQ(kInternalSpeaker->id,
jennyzb47947f2014-09-25 00:42:041009 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:321010 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
1011
1012 // Plug in usb headphone
1013 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531014 AudioNode internal_speaker = GenerateAudioNode(kInternalSpeaker);
[email protected]ee38bc42013-07-29 21:41:321015 internal_speaker.active = true;
1016 internal_speaker.plugged_time = 80000000;
1017 audio_nodes.push_back(internal_speaker);
tbarzic3ab01a22016-12-17 03:12:531018 AudioNode usb_headphone = GenerateAudioNode(kUSBHeadphone1);
[email protected]ee38bc42013-07-29 21:41:321019 usb_headphone.plugged_time = 90000000;
1020 audio_nodes.push_back(usb_headphone);
1021 ChangeAudioNodes(audio_nodes);
1022
1023 // Verify the AudioNodesChanged event is fired and new audio device is added.
1024 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1025 cras_audio_handler_->GetAudioDevices(&audio_devices);
1026 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1027
1028 // Verify the active output device is switched to usb headphone, and
1029 // ActiveOutputChanged event is fired.
1030 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041031 EXPECT_TRUE(
1032 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531033 EXPECT_EQ(kUSBHeadphone1->id, active_output.id);
1034 EXPECT_EQ(kUSBHeadphone1->id,
jennyzb47947f2014-09-25 00:42:041035 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:321036 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1037
1038 // Unplug usb headphone.
1039 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531040 audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
[email protected]ee38bc42013-07-29 21:41:321041 ChangeAudioNodes(audio_nodes);
1042
1043 // Verify the AudioNodesChanged event is fired and one audio device is
1044 // removed.
1045 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1046 cras_audio_handler_->GetAudioDevices(&audio_devices);
1047 EXPECT_EQ(init_nodes_size, audio_devices.size());
1048
1049 // Verify the active output device is switched to internal speaker, and
1050 // ActiveOutputChanged event is fired.
1051 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
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());
1058}
1059
tbarzic3ab01a22016-12-17 03:12:531060TEST_P(CrasAudioHandlerTest, HandleMultipleUSBHeadphones) {
[email protected]ee38bc42013-07-29 21:41:321061 // Initialize with internal speaker and one usb headphone.
tbarzic3ab01a22016-12-17 03:12:531062 AudioNodeList audio_nodes =
1063 GenerateAudioNodeList({kInternalSpeaker, kUSBHeadphone1});
[email protected]ee38bc42013-07-29 21:41:321064 SetUpCrasAudioHandler(audio_nodes);
1065 const size_t init_nodes_size = audio_nodes.size();
1066
1067 // Verify the audio devices size.
1068 AudioDeviceList audio_devices;
1069 cras_audio_handler_->GetAudioDevices(&audio_devices);
1070 EXPECT_EQ(init_nodes_size, audio_devices.size());
1071 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1072
1073 // Verify the usb headphone is selected as the active output initially.
1074 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1075 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041076 EXPECT_TRUE(
1077 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531078 EXPECT_EQ(kUSBHeadphone1->id, active_output.id);
1079 EXPECT_EQ(kUSBHeadphone1->id,
jennyzb47947f2014-09-25 00:42:041080 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:321081 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1082
1083 // Plug in another usb headphone.
1084 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531085 audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
1086 AudioNode usb_headphone_1 = GenerateAudioNode(kUSBHeadphone1);
[email protected]ee38bc42013-07-29 21:41:321087 usb_headphone_1.active = true;
1088 usb_headphone_1.plugged_time = 80000000;
1089 audio_nodes.push_back(usb_headphone_1);
tbarzic3ab01a22016-12-17 03:12:531090 AudioNode usb_headphone_2 = GenerateAudioNode(kUSBHeadphone2);
[email protected]ee38bc42013-07-29 21:41:321091 usb_headphone_2.plugged_time = 90000000;
1092 audio_nodes.push_back(usb_headphone_2);
1093 ChangeAudioNodes(audio_nodes);
1094
1095 // Verify the AudioNodesChanged event is fired and new audio device is added.
1096 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1097 cras_audio_handler_->GetAudioDevices(&audio_devices);
1098 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1099
1100 // Verify the active output device is switched to the 2nd usb headphone, which
1101 // is plugged later, and ActiveOutputChanged event is fired.
1102 EXPECT_EQ(1, 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(kUSBHeadphone2->id, active_output.id);
1106 EXPECT_EQ(kUSBHeadphone2->id,
jennyzb47947f2014-09-25 00:42:041107 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:321108 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1109
1110 // Unplug the 2nd usb headphone.
1111 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531112 audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
1113 audio_nodes.push_back(GenerateAudioNode(kUSBHeadphone1));
[email protected]ee38bc42013-07-29 21:41:321114 ChangeAudioNodes(audio_nodes);
1115
1116 // Verify the AudioNodesChanged event is fired and one audio device is
1117 // removed.
1118 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1119 cras_audio_handler_->GetAudioDevices(&audio_devices);
1120 EXPECT_EQ(init_nodes_size, audio_devices.size());
1121
1122 // Verify the active output device is switched to the first usb headphone, and
1123 // ActiveOutputChanged event is fired.
1124 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041125 EXPECT_TRUE(
1126 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531127 EXPECT_EQ(kUSBHeadphone1->id, active_output.id);
1128 EXPECT_EQ(kUSBHeadphone1->id,
jennyzb47947f2014-09-25 00:42:041129 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:321130 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1131}
1132
tbarzic3ab01a22016-12-17 03:12:531133TEST_P(CrasAudioHandlerTest, UnplugUSBHeadphonesWithActiveSpeaker) {
[email protected]ca473252013-08-15 11:48:391134 // Initialize with internal speaker and one usb headphone.
tbarzic3ab01a22016-12-17 03:12:531135 AudioNodeList audio_nodes =
1136 GenerateAudioNodeList({kInternalSpeaker, kUSBHeadphone1});
[email protected]ca473252013-08-15 11:48:391137 SetUpCrasAudioHandler(audio_nodes);
1138 const size_t init_nodes_size = audio_nodes.size();
1139
1140 // Verify the audio devices size.
1141 AudioDeviceList audio_devices;
1142 cras_audio_handler_->GetAudioDevices(&audio_devices);
1143 EXPECT_EQ(init_nodes_size, audio_devices.size());
1144 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1145
1146 // Verify the usb headphone is selected as the active output initially.
1147 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1148 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041149 EXPECT_TRUE(
1150 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531151 EXPECT_EQ(kUSBHeadphone1->id, active_output.id);
1152 EXPECT_EQ(kUSBHeadphone1->id,
jennyzb47947f2014-09-25 00:42:041153 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ca473252013-08-15 11:48:391154 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1155
1156 // Plug in the headphone jack.
1157 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531158 audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
1159 AudioNode usb_headphone_1 = GenerateAudioNode(kUSBHeadphone1);
[email protected]ca473252013-08-15 11:48:391160 usb_headphone_1.active = true;
1161 usb_headphone_1.plugged_time = 80000000;
1162 audio_nodes.push_back(usb_headphone_1);
tbarzic3ab01a22016-12-17 03:12:531163 AudioNode headphone_jack = GenerateAudioNode(kHeadphone);
[email protected]ca473252013-08-15 11:48:391164 headphone_jack.plugged_time = 90000000;
1165 audio_nodes.push_back(headphone_jack);
1166 ChangeAudioNodes(audio_nodes);
1167
1168 // Verify the AudioNodesChanged event is fired and new audio device is added.
1169 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1170 cras_audio_handler_->GetAudioDevices(&audio_devices);
1171 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1172
1173 // Verify the active output device is switched to the headphone jack, which
1174 // is plugged later, and ActiveOutputChanged event is fired.
1175 EXPECT_EQ(1, 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(kHeadphone->id, active_output.id);
1179 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ca473252013-08-15 11:48:391180 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1181
1182 // Select the speaker to be the active output device.
tbarzic3ab01a22016-12-17 03:12:531183 AudioDevice internal_speaker(GenerateAudioNode(kInternalSpeaker));
jennyzbd236742016-03-02 20:15:521184 cras_audio_handler_->SwitchToDevice(internal_speaker, true,
1185 CrasAudioHandler::ACTIVATE_BY_USER);
[email protected]ca473252013-08-15 11:48:391186
1187 // Verify the active output is switched to internal speaker, and the
1188 // ActiveOutputNodeChanged event is fired.
1189 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041190 EXPECT_TRUE(
1191 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531192 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
1193 EXPECT_EQ(kInternalSpeaker->id,
jennyzb47947f2014-09-25 00:42:041194 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ca473252013-08-15 11:48:391195
1196 // Unplug the usb headphone.
1197 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531198 AudioNode internal_speaker_node(GenerateAudioNode(kInternalSpeaker));
[email protected]ca473252013-08-15 11:48:391199 internal_speaker_node.active = true;
1200 internal_speaker_node.plugged_time = 70000000;
1201 audio_nodes.push_back(internal_speaker_node);
1202 headphone_jack.active = false;
1203 audio_nodes.push_back(headphone_jack);
1204 ChangeAudioNodes(audio_nodes);
1205
1206 // Verify the AudioNodesChanged event is fired and one audio device is
1207 // removed.
1208 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1209 cras_audio_handler_->GetAudioDevices(&audio_devices);
1210 EXPECT_EQ(init_nodes_size, audio_devices.size());
1211
1212 // Verify the active output device remains to be speaker.
1213 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041214 EXPECT_TRUE(
1215 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531216 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
1217 EXPECT_EQ(kInternalSpeaker->id,
jennyzb47947f2014-09-25 00:42:041218 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ca473252013-08-15 11:48:391219 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1220}
1221
tbarzic3ab01a22016-12-17 03:12:531222TEST_P(CrasAudioHandlerTest, OneActiveAudioOutputAfterLoginNewUserSession) {
[email protected]d413fcc2013-08-16 23:20:031223 // This tests the case found with crbug.com/273271.
1224 // Initialize with internal speaker, bluetooth headphone and headphone jack
1225 // for a new chrome session after user signs out from the previous session.
1226 // Headphone jack is plugged in later than bluetooth headphone, but bluetooth
1227 // headphone is selected as the active output by user from previous user
1228 // session.
tbarzic3ab01a22016-12-17 03:12:531229 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker});
1230 AudioNode bluetooth_headphone = GenerateAudioNode(kBluetoothHeadset);
[email protected]d413fcc2013-08-16 23:20:031231 bluetooth_headphone.active = true;
1232 bluetooth_headphone.plugged_time = 70000000;
1233 audio_nodes.push_back(bluetooth_headphone);
tbarzic3ab01a22016-12-17 03:12:531234 AudioNode headphone_jack = GenerateAudioNode(kHeadphone);
[email protected]d413fcc2013-08-16 23:20:031235 headphone_jack.plugged_time = 80000000;
1236 audio_nodes.push_back(headphone_jack);
jennyzb47947f2014-09-25 00:42:041237 SetUpCrasAudioHandlerWithPrimaryActiveNode(audio_nodes, bluetooth_headphone);
[email protected]d413fcc2013-08-16 23:20:031238 const size_t init_nodes_size = audio_nodes.size();
1239
1240 // Verify the audio devices size.
1241 AudioDeviceList audio_devices;
1242 cras_audio_handler_->GetAudioDevices(&audio_devices);
1243 EXPECT_EQ(init_nodes_size, audio_devices.size());
1244 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1245
1246 // Verify the headphone jack is selected as the active output and all other
1247 // audio devices are not active.
1248 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1249 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041250 EXPECT_TRUE(
1251 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531252 EXPECT_EQ(kHeadphone->id, active_output.id);
1253 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]d413fcc2013-08-16 23:20:031254 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1255 for (size_t i = 0; i < audio_devices.size(); ++i) {
tbarzic3ab01a22016-12-17 03:12:531256 if (audio_devices[i].id != kHeadphone->id)
[email protected]d413fcc2013-08-16 23:20:031257 EXPECT_FALSE(audio_devices[i].active);
1258 }
1259}
1260
tbarzic3ab01a22016-12-17 03:12:531261TEST_P(CrasAudioHandlerTest, BluetoothSpeakerIdChangedOnFly) {
[email protected]110babd2013-09-17 01:46:021262 // Initialize with internal speaker and bluetooth headset.
tbarzic3ab01a22016-12-17 03:12:531263 AudioNodeList audio_nodes =
1264 GenerateAudioNodeList({kInternalSpeaker, kBluetoothHeadset});
[email protected]110babd2013-09-17 01:46:021265 SetUpCrasAudioHandler(audio_nodes);
1266 const size_t init_nodes_size = audio_nodes.size();
1267
1268 // Verify the audio devices size.
1269 AudioDeviceList audio_devices;
1270 cras_audio_handler_->GetAudioDevices(&audio_devices);
1271 EXPECT_EQ(init_nodes_size, audio_devices.size());
1272 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1273
1274 // Verify the bluetooth headset is selected as the active output and all other
1275 // audio devices are not active.
1276 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
1277 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041278 EXPECT_TRUE(
1279 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531280 EXPECT_EQ(kBluetoothHeadset->id, active_output.id);
1281 EXPECT_EQ(kBluetoothHeadset->id,
jennyzb47947f2014-09-25 00:42:041282 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]110babd2013-09-17 01:46:021283 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1284
1285 // Cras changes the bluetooth headset's id on the fly.
1286 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531287 AudioNode internal_speaker(GenerateAudioNode(kInternalSpeaker));
[email protected]110babd2013-09-17 01:46:021288 internal_speaker.active = false;
1289 audio_nodes.push_back(internal_speaker);
tbarzic3ab01a22016-12-17 03:12:531290 AudioNode bluetooth_headphone = GenerateAudioNode(kBluetoothHeadset);
[email protected]110babd2013-09-17 01:46:021291 // Change bluetooth headphone id.
1292 bluetooth_headphone.id = kBluetoothHeadsetId + 20000;
1293 bluetooth_headphone.active = false;
1294 audio_nodes.push_back(bluetooth_headphone);
1295 ChangeAudioNodes(audio_nodes);
1296
1297 // Verify NodesChanged event is fired, and the audio devices size is not
1298 // changed.
1299 audio_devices.clear();
1300 cras_audio_handler_->GetAudioDevices(&audio_devices);
1301 EXPECT_EQ(init_nodes_size, audio_devices.size());
1302 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1303
1304 // Verify ActiveOutputNodeChanged event is fired, and active device should be
1305 // bluetooth headphone.
1306 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041307 EXPECT_TRUE(
1308 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]110babd2013-09-17 01:46:021309 EXPECT_EQ(bluetooth_headphone.id, active_output.id);
1310}
1311
tbarzic3ab01a22016-12-17 03:12:531312TEST_P(CrasAudioHandlerTest, PlugUSBMic) {
[email protected]ee38bc42013-07-29 21:41:321313 // Set up initial audio devices, only with internal mic.
tbarzic3ab01a22016-12-17 03:12:531314 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalMic});
[email protected]ee38bc42013-07-29 21:41:321315 SetUpCrasAudioHandler(audio_nodes);
1316 const size_t init_nodes_size = audio_nodes.size();
1317
1318 // Verify the audio devices size.
1319 AudioDeviceList audio_devices;
1320 cras_audio_handler_->GetAudioDevices(&audio_devices);
1321 EXPECT_EQ(init_nodes_size, audio_devices.size());
1322 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1323
[email protected]babc24b2013-09-09 23:50:231324 // Verify the internal mic is selected as the active input.
[email protected]ee38bc42013-07-29 21:41:321325 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
tbarzic3ab01a22016-12-17 03:12:531326 EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:321327 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1328
1329 // Plug the USB Mic.
[email protected]4642d7792013-09-18 20:27:171330 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531331 AudioNode internal_mic(GenerateAudioNode(kInternalMic));
[email protected]4642d7792013-09-18 20:27:171332 internal_mic.active = true;
1333 audio_nodes.push_back(internal_mic);
tbarzic3ab01a22016-12-17 03:12:531334 audio_nodes.push_back(GenerateAudioNode(kUSBMic));
[email protected]ee38bc42013-07-29 21:41:321335 ChangeAudioNodes(audio_nodes);
1336
1337 // Verify the AudioNodesChanged event is fired and new audio device is added.
1338 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1339 cras_audio_handler_->GetAudioDevices(&audio_devices);
1340 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1341
1342 // Verify the active input device is switched to USB mic and
1343 // and ActiveInputChanged event is fired.
1344 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041345 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:321346 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1347}
1348
tbarzic3ab01a22016-12-17 03:12:531349TEST_P(CrasAudioHandlerTest, UnplugUSBMic) {
[email protected]ee38bc42013-07-29 21:41:321350 // Set up initial audio devices, with internal mic and USB Mic.
tbarzic3ab01a22016-12-17 03:12:531351 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalMic, kUSBMic});
[email protected]ee38bc42013-07-29 21:41:321352 SetUpCrasAudioHandler(audio_nodes);
1353 const size_t init_nodes_size = audio_nodes.size();
1354
1355 // Verify the audio devices size.
1356 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1357 AudioDeviceList audio_devices;
1358 cras_audio_handler_->GetAudioDevices(&audio_devices);
1359 EXPECT_EQ(init_nodes_size, audio_devices.size());
1360
1361 // Verify the USB mic is selected as the active output.
1362 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041363 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:321364 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1365
1366 // Unplug the USB Mic.
1367 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531368 audio_nodes.push_back(GenerateAudioNode(kInternalMic));
[email protected]ee38bc42013-07-29 21:41:321369 ChangeAudioNodes(audio_nodes);
1370
1371 // Verify the AudioNodesChanged event is fired, and one audio device is
1372 // removed.
1373 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1374 cras_audio_handler_->GetAudioDevices(&audio_devices);
1375 EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
1376
1377 // Verify the active input device is switched to internal mic, and
1378 // and ActiveInputChanged event is fired.
1379 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
tbarzic3ab01a22016-12-17 03:12:531380 EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:321381 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1382}
1383
tbarzic3ab01a22016-12-17 03:12:531384TEST_P(CrasAudioHandlerTest, PlugUSBMicNotAffectActiveOutput) {
[email protected]babc24b2013-09-09 23:50:231385 // Set up initial audio devices.
tbarzic3ab01a22016-12-17 03:12:531386 AudioNodeList audio_nodes =
1387 GenerateAudioNodeList({kInternalSpeaker, kHeadphone, kInternalMic});
[email protected]babc24b2013-09-09 23:50:231388 SetUpCrasAudioHandler(audio_nodes);
1389 const size_t init_nodes_size = audio_nodes.size();
1390
1391 // Verify the audio devices size.
1392 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1393 AudioDeviceList audio_devices;
1394 cras_audio_handler_->GetAudioDevices(&audio_devices);
1395 EXPECT_EQ(init_nodes_size, audio_devices.size());
1396
1397 // Verify the internal mic is selected as the active input.
1398 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041399 EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]babc24b2013-09-09 23:50:231400 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1401
1402 // Verify the headphone is selected as the active output.
1403 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041404 EXPECT_EQ(kHeadphoneId, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]babc24b2013-09-09 23:50:231405 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1406
1407 // Switch the active output to internal speaker.
tbarzic3ab01a22016-12-17 03:12:531408 AudioDevice internal_speaker(GenerateAudioNode(kInternalSpeaker));
jennyzbd236742016-03-02 20:15:521409 cras_audio_handler_->SwitchToDevice(internal_speaker, true,
1410 CrasAudioHandler::ACTIVATE_BY_USER);
[email protected]babc24b2013-09-09 23:50:231411
1412 // Verify the active output is switched to internal speaker, and the
1413 // ActiveOutputNodeChanged event is fired.
1414 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
1415 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041416 EXPECT_TRUE(
1417 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531418 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
1419 EXPECT_EQ(kInternalSpeaker->id,
jennyzb47947f2014-09-25 00:42:041420 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]babc24b2013-09-09 23:50:231421
1422 // Plug the USB Mic.
[email protected]4642d7792013-09-18 20:27:171423 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531424 AudioNode internal_speaker_node = GenerateAudioNode(kInternalSpeaker);
[email protected]4642d7792013-09-18 20:27:171425 internal_speaker_node.active = true;
1426 audio_nodes.push_back(internal_speaker_node);
tbarzic3ab01a22016-12-17 03:12:531427 audio_nodes.push_back(GenerateAudioNode(kHeadphone));
1428 AudioNode internal_mic = GenerateAudioNode(kInternalMic);
[email protected]4642d7792013-09-18 20:27:171429 internal_mic.active = true;
1430 audio_nodes.push_back(internal_mic);
tbarzic3ab01a22016-12-17 03:12:531431 audio_nodes.push_back(GenerateAudioNode(kUSBMic));
[email protected]babc24b2013-09-09 23:50:231432 ChangeAudioNodes(audio_nodes);
1433
1434 // Verify the AudioNodesChanged event is fired, one new device is added.
1435 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1436 cras_audio_handler_->GetAudioDevices(&audio_devices);
1437 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1438
1439 // Verify the active input device is switched to USB mic, and
1440 // and ActiveInputChanged event is fired.
1441 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
tbarzic3ab01a22016-12-17 03:12:531442 EXPECT_EQ(kUSBMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]babc24b2013-09-09 23:50:231443 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1444
1445 // Verify the active output device is not changed.
1446 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041447 EXPECT_TRUE(
1448 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531449 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
1450 EXPECT_EQ(kInternalSpeaker->id,
jennyzb47947f2014-09-25 00:42:041451 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]babc24b2013-09-09 23:50:231452}
1453
tbarzic3ab01a22016-12-17 03:12:531454TEST_P(CrasAudioHandlerTest, PlugHeadphoneAutoUnplugSpeakerWithActiveUSB) {
[email protected]ce631ee2014-01-23 03:37:061455 // Set up initial audio devices.
tbarzic3ab01a22016-12-17 03:12:531456 AudioNodeList audio_nodes =
1457 GenerateAudioNodeList({kUSBHeadphone1, kInternalSpeaker, kInternalMic});
[email protected]ce631ee2014-01-23 03:37:061458 SetUpCrasAudioHandler(audio_nodes);
1459 const size_t init_nodes_size = audio_nodes.size();
1460
1461 // Verify the audio devices size.
1462 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1463 AudioDeviceList audio_devices;
1464 cras_audio_handler_->GetAudioDevices(&audio_devices);
1465 EXPECT_EQ(init_nodes_size, audio_devices.size());
1466
1467 // Verify the internal mic is selected as the active input.
1468 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041469 EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ce631ee2014-01-23 03:37:061470 EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
1471
1472 // Verify the USB headphone is selected as the active output.
1473 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041474 EXPECT_EQ(kUSBHeadphoneId1,
1475 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ce631ee2014-01-23 03:37:061476 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1477
1478 // Plug the headphone and auto-unplug internal speaker.
1479 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531480 AudioNode usb_headphone_node = GenerateAudioNode(kUSBHeadphone1);
[email protected]ce631ee2014-01-23 03:37:061481 usb_headphone_node.active = true;
1482 audio_nodes.push_back(usb_headphone_node);
tbarzic3ab01a22016-12-17 03:12:531483 AudioNode headphone_node = GenerateAudioNode(kHeadphone);
[email protected]ce631ee2014-01-23 03:37:061484 headphone_node.plugged_time = 1000;
1485 audio_nodes.push_back(headphone_node);
tbarzic3ab01a22016-12-17 03:12:531486 AudioNode internal_mic = GenerateAudioNode(kInternalMic);
[email protected]ce631ee2014-01-23 03:37:061487 internal_mic.active = true;
1488 audio_nodes.push_back(internal_mic);
1489 ChangeAudioNodes(audio_nodes);
1490
1491 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1492 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1493 cras_audio_handler_->GetAudioDevices(&audio_devices);
1494 EXPECT_EQ(init_nodes_size, audio_devices.size());
1495
1496 // Verify the active output device is switched to headphone, and
1497 // an ActiveOutputChanged event is fired.
1498 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
tbarzic3ab01a22016-12-17 03:12:531499 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ce631ee2014-01-23 03:37:061500 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1501
1502 // Unplug the headphone and internal speaker auto-plugs back.
1503 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531504 audio_nodes.push_back(GenerateAudioNode(kUSBHeadphone1));
1505 AudioNode internal_speaker_node = GenerateAudioNode(kInternalSpeaker);
[email protected]ce631ee2014-01-23 03:37:061506 internal_speaker_node.plugged_time = 2000;
1507 audio_nodes.push_back(internal_speaker_node);
1508 audio_nodes.push_back(internal_mic);
1509 ChangeAudioNodes(audio_nodes);
1510
1511 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1512 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1513 cras_audio_handler_->GetAudioDevices(&audio_devices);
1514 EXPECT_EQ(init_nodes_size, audio_devices.size());
1515
1516 // Verify the active output device is switched back to USB, and
1517 // an ActiveOutputChanged event is fired.
1518 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
tbarzic3ab01a22016-12-17 03:12:531519 EXPECT_EQ(kUSBHeadphone1->id,
jennyzb47947f2014-09-25 00:42:041520 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ce631ee2014-01-23 03:37:061521 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1522
1523 // Verify the active input device is not changed.
1524 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
tbarzic3ab01a22016-12-17 03:12:531525 EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ce631ee2014-01-23 03:37:061526}
1527
tbarzic3ab01a22016-12-17 03:12:531528TEST_P(CrasAudioHandlerTest, PlugMicAutoUnplugInternalMicWithActiveUSB) {
[email protected]ce631ee2014-01-23 03:37:061529 // Set up initial audio devices.
tbarzic3ab01a22016-12-17 03:12:531530 AudioNodeList audio_nodes = GenerateAudioNodeList(
1531 {kUSBHeadphone1, kInternalSpeaker, kUSBMic, kInternalMic});
[email protected]ce631ee2014-01-23 03:37:061532 SetUpCrasAudioHandler(audio_nodes);
1533 const size_t init_nodes_size = audio_nodes.size();
1534
1535 // Verify the audio devices size.
1536 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1537 AudioDeviceList audio_devices;
1538 cras_audio_handler_->GetAudioDevices(&audio_devices);
1539 EXPECT_EQ(init_nodes_size, audio_devices.size());
1540
1541 // Verify the internal mic is selected as the active input.
1542 EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:041543 EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ce631ee2014-01-23 03:37:061544 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1545
1546 // Verify the internal speaker is selected as the active output.
1547 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041548 EXPECT_EQ(kUSBHeadphoneId1,
1549 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ce631ee2014-01-23 03:37:061550 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1551
1552 // Plug the headphone and mic, auto-unplug internal mic and speaker.
1553 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531554 AudioNode usb_headphone_node = GenerateAudioNode(kUSBHeadphone1);
[email protected]ce631ee2014-01-23 03:37:061555 usb_headphone_node.active = true;
1556 audio_nodes.push_back(usb_headphone_node);
tbarzic3ab01a22016-12-17 03:12:531557 AudioNode headphone_node = GenerateAudioNode(kHeadphone);
[email protected]ce631ee2014-01-23 03:37:061558 headphone_node.plugged_time = 1000;
1559 audio_nodes.push_back(headphone_node);
tbarzic3ab01a22016-12-17 03:12:531560 AudioNode usb_mic = GenerateAudioNode(kUSBMic);
[email protected]ce631ee2014-01-23 03:37:061561 usb_mic.active = true;
1562 audio_nodes.push_back(usb_mic);
tbarzic3ab01a22016-12-17 03:12:531563 AudioNode mic_jack = GenerateAudioNode(kMicJack);
[email protected]ce631ee2014-01-23 03:37:061564 mic_jack.plugged_time = 1000;
1565 audio_nodes.push_back(mic_jack);
1566 ChangeAudioNodes(audio_nodes);
1567
1568 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1569 EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
1570 cras_audio_handler_->GetAudioDevices(&audio_devices);
1571 EXPECT_EQ(init_nodes_size, audio_devices.size());
1572
1573 // Verify the active output device is switched to headphone, and
1574 // an ActiveOutputChanged event is fired.
1575 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
tbarzic3ab01a22016-12-17 03:12:531576 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ce631ee2014-01-23 03:37:061577 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1578
1579 // Verify the active input device is switched to mic jack, and
1580 // an ActiveInputChanged event is fired.
1581 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
tbarzic3ab01a22016-12-17 03:12:531582 EXPECT_EQ(kMicJack->id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ce631ee2014-01-23 03:37:061583 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1584
1585 // Unplug the headphone and internal speaker auto-plugs back.
1586 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531587 audio_nodes.push_back(GenerateAudioNode(kUSBHeadphone1));
1588 AudioNode internal_speaker_node = GenerateAudioNode(kInternalSpeaker);
[email protected]ce631ee2014-01-23 03:37:061589 internal_speaker_node.plugged_time = 2000;
1590 audio_nodes.push_back(internal_speaker_node);
tbarzic3ab01a22016-12-17 03:12:531591 audio_nodes.push_back(GenerateAudioNode(kUSBMic));
1592 AudioNode internal_mic = GenerateAudioNode(kInternalMic);
[email protected]ce631ee2014-01-23 03:37:061593 internal_mic.plugged_time = 2000;
1594 audio_nodes.push_back(internal_mic);
1595 ChangeAudioNodes(audio_nodes);
1596
1597 // Verify the AudioNodesChanged event is fired, with nodes count unchanged.
1598 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
1599 cras_audio_handler_->GetAudioDevices(&audio_devices);
1600 EXPECT_EQ(init_nodes_size, audio_devices.size());
1601
1602 // Verify the active output device is switched back to USB, and
1603 // an ActiveOutputChanged event is fired.
1604 EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
tbarzic3ab01a22016-12-17 03:12:531605 EXPECT_EQ(kUSBHeadphone1->id,
jennyzb47947f2014-09-25 00:42:041606 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ce631ee2014-01-23 03:37:061607 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1608
1609 // Verify the active input device is switched back to USB mic, and
1610 // an ActiveInputChanged event is fired.
1611 EXPECT_EQ(2, test_observer_->active_input_node_changed_count());
tbarzic3ab01a22016-12-17 03:12:531612 EXPECT_EQ(kUSBMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ce631ee2014-01-23 03:37:061613 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1614}
1615
tbarzic3ab01a22016-12-17 03:12:531616TEST_P(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnPlugInHeadphone) {
[email protected]4642d7792013-09-18 20:27:171617 // Set up initial audio devices.
tbarzic3ab01a22016-12-17 03:12:531618 AudioNodeList audio_nodes =
1619 GenerateAudioNodeList({kInternalSpeaker, kBluetoothHeadset});
[email protected]4642d7792013-09-18 20:27:171620 SetUpCrasAudioHandler(audio_nodes);
1621 const size_t init_nodes_size = audio_nodes.size();
1622
1623 // Verify the audio devices size.
1624 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1625 AudioDeviceList audio_devices;
1626 cras_audio_handler_->GetAudioDevices(&audio_devices);
1627 EXPECT_EQ(init_nodes_size, audio_devices.size());
1628
1629 // Verify the bluetooth headset is selected as the active output.
1630 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
jennyzb47947f2014-09-25 00:42:041631 EXPECT_EQ(kBluetoothHeadsetId,
1632 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]4642d7792013-09-18 20:27:171633 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041634 EXPECT_TRUE(
1635 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]4642d7792013-09-18 20:27:171636 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1637
1638 // Plug in headphone, but fire NodesChanged signal twice.
1639 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531640 audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
1641 AudioNode bluetooth_headset = GenerateAudioNode(kBluetoothHeadset);
[email protected]4642d7792013-09-18 20:27:171642 bluetooth_headset.plugged_time = 1000;
1643 bluetooth_headset.active = true;
1644 audio_nodes.push_back(bluetooth_headset);
tbarzic3ab01a22016-12-17 03:12:531645 AudioNode headphone = GenerateAudioNode(kHeadphone);
[email protected]4642d7792013-09-18 20:27:171646 headphone.active = false;
1647 headphone.plugged_time = 2000;
1648 audio_nodes.push_back(headphone);
1649 ChangeAudioNodes(audio_nodes);
1650 ChangeAudioNodes(audio_nodes);
1651
1652 // Verify the active output device is set to headphone.
1653 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
jennyzb47947f2014-09-25 00:42:041654 EXPECT_LE(1, test_observer_->active_output_node_changed_count());
1655 EXPECT_EQ(headphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
1656 EXPECT_TRUE(
1657 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]4642d7792013-09-18 20:27:171658 EXPECT_EQ(headphone.id, active_output.id);
1659
1660 // Verfiy the audio devices data is consistent, i.e., the active output device
1661 // should be headphone.
1662 cras_audio_handler_->GetAudioDevices(&audio_devices);
1663 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1664 for (size_t i = 0; i < audio_devices.size(); ++i) {
tbarzic3ab01a22016-12-17 03:12:531665 if (audio_devices[i].id == kInternalSpeaker->id)
[email protected]4642d7792013-09-18 20:27:171666 EXPECT_FALSE(audio_devices[i].active);
1667 else if (audio_devices[i].id == bluetooth_headset.id)
1668 EXPECT_FALSE(audio_devices[i].active);
1669 else if (audio_devices[i].id == headphone.id)
1670 EXPECT_TRUE(audio_devices[i].active);
1671 else
1672 NOTREACHED();
1673 }
1674}
1675
tbarzic3ab01a22016-12-17 03:12:531676TEST_P(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnPlugInUSBMic) {
[email protected]4642d7792013-09-18 20:27:171677 // Set up initial audio devices.
tbarzic3ab01a22016-12-17 03:12:531678 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalMic});
[email protected]4642d7792013-09-18 20:27:171679 SetUpCrasAudioHandler(audio_nodes);
1680 const size_t init_nodes_size = audio_nodes.size();
1681
1682 // Verify the audio devices size.
1683 EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
1684 AudioDeviceList audio_devices;
1685 cras_audio_handler_->GetAudioDevices(&audio_devices);
1686 EXPECT_EQ(init_nodes_size, audio_devices.size());
1687
1688 // Verify the internal mic is selected as the active output.
1689 EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
tbarzic3ab01a22016-12-17 03:12:531690 EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]4642d7792013-09-18 20:27:171691 EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
1692 EXPECT_TRUE(audio_devices[0].active);
1693
1694 // Plug in usb mic, but fire NodesChanged signal twice.
1695 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:531696 AudioNode internal_mic = GenerateAudioNode(kInternalMic);
[email protected]4642d7792013-09-18 20:27:171697 internal_mic.active = true;
1698 internal_mic.plugged_time = 1000;
1699 audio_nodes.push_back(internal_mic);
tbarzic3ab01a22016-12-17 03:12:531700 AudioNode usb_mic = GenerateAudioNode(kUSBMic);
[email protected]4642d7792013-09-18 20:27:171701 usb_mic.active = false;
1702 usb_mic.plugged_time = 2000;
1703 audio_nodes.push_back(usb_mic);
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_input_node_changed_count());
1710 EXPECT_EQ(usb_mic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]4642d7792013-09-18 20:27:171711 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1712
1713 // Verfiy the audio devices data is consistent, i.e., the active input device
1714 // should be usb mic.
1715 cras_audio_handler_->GetAudioDevices(&audio_devices);
1716 EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
1717 for (size_t i = 0; i < audio_devices.size(); ++i) {
tbarzic3ab01a22016-12-17 03:12:531718 if (audio_devices[i].id == kInternalMic->id)
[email protected]4642d7792013-09-18 20:27:171719 EXPECT_FALSE(audio_devices[i].active);
1720 else if (audio_devices[i].id == usb_mic.id)
1721 EXPECT_TRUE(audio_devices[i].active);
1722 else
1723 NOTREACHED();
1724 }
1725}
1726
1727// This is the case of crbug.com/291303.
tbarzic3ab01a22016-12-17 03:12:531728TEST_P(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnSystemBoot) {
[email protected]4642d7792013-09-18 20:27:171729 // Set up audio handler with empty audio_nodes.
1730 AudioNodeList audio_nodes;
1731 SetUpCrasAudioHandler(audio_nodes);
1732
tbarzic3ab01a22016-12-17 03:12:531733 AudioNode internal_speaker = GenerateAudioNode(kInternalSpeaker);
[email protected]4642d7792013-09-18 20:27:171734 internal_speaker.active = false;
tbarzic3ab01a22016-12-17 03:12:531735 AudioNode headphone = GenerateAudioNode(kHeadphone);
[email protected]4642d7792013-09-18 20:27:171736 headphone.active = false;
tbarzic3ab01a22016-12-17 03:12:531737 AudioNode internal_mic = GenerateAudioNode(kInternalMic);
[email protected]4642d7792013-09-18 20:27:171738 internal_mic.active = false;
1739 audio_nodes.push_back(internal_speaker);
1740 audio_nodes.push_back(headphone);
1741 audio_nodes.push_back(internal_mic);
1742 const size_t init_nodes_size = audio_nodes.size();
1743
1744 // Simulate AudioNodesChanged signal being fired twice during system boot.
1745 ChangeAudioNodes(audio_nodes);
1746 ChangeAudioNodes(audio_nodes);
1747
1748 // Verify the active output device is set to headphone.
1749 EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
jennyzb47947f2014-09-25 00:42:041750 EXPECT_LE(1, test_observer_->active_output_node_changed_count());
1751 EXPECT_EQ(headphone.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]4642d7792013-09-18 20:27:171752 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:041753 EXPECT_TRUE(
1754 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
[email protected]4642d7792013-09-18 20:27:171755 EXPECT_EQ(headphone.id, active_output.id);
1756
1757 // Verify the active input device id is set to internal mic.
jennyzb47947f2014-09-25 00:42:041758 EXPECT_EQ(internal_mic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]4642d7792013-09-18 20:27:171759
1760 // Verfiy the audio devices data is consistent, i.e., the active output device
1761 // should be headphone, and the active input device should internal mic.
1762 AudioDeviceList audio_devices;
1763 cras_audio_handler_->GetAudioDevices(&audio_devices);
1764 EXPECT_EQ(init_nodes_size, audio_devices.size());
1765 for (size_t i = 0; i < audio_devices.size(); ++i) {
1766 if (audio_devices[i].id == internal_speaker.id)
1767 EXPECT_FALSE(audio_devices[i].active);
1768 else if (audio_devices[i].id == headphone.id)
1769 EXPECT_TRUE(audio_devices[i].active);
1770 else if (audio_devices[i].id == internal_mic.id)
1771 EXPECT_TRUE(audio_devices[i].active);
1772 else
1773 NOTREACHED();
1774 }
1775}
1776
jennyz2d8986d2015-01-15 18:34:071777// This is the case of crbug.com/448924.
tbarzic3ab01a22016-12-17 03:12:531778TEST_P(CrasAudioHandlerTest,
jennyz2d8986d2015-01-15 18:34:071779 TwoNodesChangedSignalsForLosingTowNodesOnOneUnplug) {
1780 // Set up audio handler with 4 audio_nodes.
1781 AudioNodeList audio_nodes;
tbarzic3ab01a22016-12-17 03:12:531782 AudioNode internal_speaker = GenerateAudioNode(kInternalSpeaker);
jennyz2d8986d2015-01-15 18:34:071783 internal_speaker.active = false;
tbarzic3ab01a22016-12-17 03:12:531784 AudioNode headphone = GenerateAudioNode(kHeadphone);
jennyz2d8986d2015-01-15 18:34:071785 headphone.active = false;
tbarzic3ab01a22016-12-17 03:12:531786 AudioNode internal_mic = GenerateAudioNode(kInternalMic);
jennyz2d8986d2015-01-15 18:34:071787 internal_mic.active = false;
tbarzic3ab01a22016-12-17 03:12:531788 AudioNode micJack = GenerateAudioNode(kMicJack);
jennyz2d8986d2015-01-15 18:34:071789 micJack.active = false;
1790 audio_nodes.push_back(internal_speaker);
1791 audio_nodes.push_back(headphone);
1792 audio_nodes.push_back(internal_mic);
1793 audio_nodes.push_back(micJack);
1794 SetUpCrasAudioHandler(audio_nodes);
1795
1796 // Verify the audio devices size.
1797 AudioDeviceList audio_devices;
1798 cras_audio_handler_->GetAudioDevices(&audio_devices);
1799 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
1800
1801 // Verify the headphone has been selected as the active output.
1802 AudioDevice active_output;
1803 EXPECT_TRUE(
1804 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:531805 EXPECT_EQ(kHeadphone->id, active_output.id);
1806 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
jennyz2d8986d2015-01-15 18:34:071807 EXPECT_TRUE(active_output.active);
1808 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
1809
1810 // Verify the mic Jack has been selected as the active input.
1811 EXPECT_EQ(micJack.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1812 const AudioDevice* active_input = GetDeviceFromId(micJack.id);
1813 EXPECT_TRUE(active_input->active);
1814 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
1815
1816 // Simulate the nodes list in first NodesChanged signal, only headphone is
1817 // removed, other nodes remains the same.
1818 AudioNodeList changed_nodes_1;
1819 internal_speaker.active = false;
1820 changed_nodes_1.push_back(internal_speaker);
1821 internal_mic.active = false;
1822 changed_nodes_1.push_back(internal_mic);
1823 micJack.active = true;
1824 changed_nodes_1.push_back(micJack);
1825
1826 // Simulate the nodes list in second NodesChanged signal, the micJac is
1827 // removed, but the internal_mic is inactive, which does not reflect the
1828 // active status set from the first NodesChanged signal since this was sent
1829 // before cras receives the SetActiveOutputNode from the first NodesChanged
1830 // handling.
1831 AudioNodeList changed_nodes_2;
1832 changed_nodes_2.push_back(internal_speaker);
1833 changed_nodes_2.push_back(internal_mic);
1834
1835 // Simulate AudioNodesChanged signal being fired twice for unplug an audio
1836 // device with both input and output nodes on it.
1837 ChangeAudioNodes(changed_nodes_1);
1838 ChangeAudioNodes(changed_nodes_2);
1839
1840 AudioDeviceList changed_devices;
1841 cras_audio_handler_->GetAudioDevices(&changed_devices);
1842 EXPECT_EQ(2u, changed_devices.size());
1843
1844 // Verify the active output device is set to internal speaker.
1845 EXPECT_EQ(internal_speaker.id,
1846 cras_audio_handler_->GetPrimaryActiveOutputNode());
1847 EXPECT_TRUE(
1848 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
1849 EXPECT_EQ(internal_speaker.id, active_output.id);
1850 EXPECT_TRUE(active_output.active);
1851
1852 // Verify the active input device id is set to internal mic.
1853 EXPECT_EQ(internal_mic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
1854 const AudioDevice* changed_active_input = GetDeviceFromId(internal_mic.id);
1855 EXPECT_TRUE(changed_active_input->active);
1856}
1857
tbarzic3ab01a22016-12-17 03:12:531858TEST_P(CrasAudioHandlerTest, SetOutputMono) {
1859 AudioNodeList audio_nodes = GenerateAudioNodeList({kHeadphone});
warx74524622016-03-31 01:07:211860 SetUpCrasAudioHandler(audio_nodes);
1861 EXPECT_EQ(0, test_observer_->output_channel_remixing_changed_count());
1862
1863 // Set output mono
1864 cras_audio_handler_->SetOutputMono(true);
1865
1866 // Verify the output is in mono mode, OnOuputChannelRemixingChanged event
1867 // is fired.
1868 EXPECT_TRUE(cras_audio_handler_->IsOutputMonoEnabled());
1869 EXPECT_EQ(1, test_observer_->output_channel_remixing_changed_count());
1870
1871 // Set output stereo
1872 cras_audio_handler_->SetOutputMono(false);
1873 EXPECT_FALSE(cras_audio_handler_->IsOutputMonoEnabled());
1874 EXPECT_EQ(2, test_observer_->output_channel_remixing_changed_count());
1875}
1876
tbarzic3ab01a22016-12-17 03:12:531877TEST_P(CrasAudioHandlerTest, SetOutputMute) {
1878 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker});
[email protected]ee38bc42013-07-29 21:41:321879 SetUpCrasAudioHandler(audio_nodes);
1880 EXPECT_EQ(0, test_observer_->output_mute_changed_count());
1881
1882 // Mute the device.
1883 cras_audio_handler_->SetOutputMute(true);
1884
1885 // Verify the output is muted, OnOutputMuteChanged event is fired,
1886 // and mute value is saved in the preferences.
1887 EXPECT_TRUE(cras_audio_handler_->IsOutputMuted());
1888 EXPECT_EQ(1, test_observer_->output_mute_changed_count());
tbarzic3ab01a22016-12-17 03:12:531889 AudioDevice speaker(GenerateAudioNode(kInternalSpeaker));
[email protected]ee38bc42013-07-29 21:41:321890 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(speaker));
1891
1892 // Unmute the device.
1893 cras_audio_handler_->SetOutputMute(false);
1894
1895 // Verify the output is unmuted, OnOutputMuteChanged event is fired,
1896 // and mute value is saved in the preferences.
1897 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
1898 EXPECT_EQ(2, test_observer_->output_mute_changed_count());
1899 EXPECT_FALSE(audio_pref_handler_->GetMuteValue(speaker));
1900}
1901
tbarzic3ab01a22016-12-17 03:12:531902TEST_P(CrasAudioHandlerTest, SetInputMute) {
1903 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalMic});
[email protected]ee38bc42013-07-29 21:41:321904 SetUpCrasAudioHandler(audio_nodes);
1905 EXPECT_EQ(0, test_observer_->input_mute_changed_count());
1906
1907 // Mute the device.
1908 cras_audio_handler_->SetInputMute(true);
1909
[email protected]f3dd2962014-04-22 04:48:381910 // Verify the input is muted, OnInputMuteChanged event is fired.
[email protected]ee38bc42013-07-29 21:41:321911 EXPECT_TRUE(cras_audio_handler_->IsInputMuted());
1912 EXPECT_EQ(1, test_observer_->input_mute_changed_count());
[email protected]ee38bc42013-07-29 21:41:321913
1914 // Unmute the device.
1915 cras_audio_handler_->SetInputMute(false);
1916
[email protected]f3dd2962014-04-22 04:48:381917 // Verify the input is unmuted, OnInputMuteChanged event is fired.
[email protected]ee38bc42013-07-29 21:41:321918 EXPECT_FALSE(cras_audio_handler_->IsInputMuted());
1919 EXPECT_EQ(2, test_observer_->input_mute_changed_count());
[email protected]ee38bc42013-07-29 21:41:321920}
1921
tbarzic3ab01a22016-12-17 03:12:531922TEST_P(CrasAudioHandlerTest, SetOutputVolumePercent) {
1923 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker});
[email protected]ee38bc42013-07-29 21:41:321924 SetUpCrasAudioHandler(audio_nodes);
1925 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
1926
warxfd7c37b2016-08-05 19:38:071927 const int kVolume = 60;
1928 cras_audio_handler_->SetOutputVolumePercent(kVolume);
[email protected]ee38bc42013-07-29 21:41:321929
1930 // Verify the output volume is changed to the designated value,
jennyz481292c2015-03-21 01:49:291931 // OnOutputNodeVolumeChanged event is fired, and the device volume value
warxfd7c37b2016-08-05 19:38:071932 // is saved in the preferences.
[email protected]ee38bc42013-07-29 21:41:321933 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent());
1934 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
1935 AudioDevice device;
jennyzb47947f2014-09-25 00:42:041936 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device));
tbarzic3ab01a22016-12-17 03:12:531937 EXPECT_EQ(device.id, kInternalSpeaker->id);
[email protected]140a31452013-09-09 18:54:371938 EXPECT_EQ(kVolume, audio_pref_handler_->GetOutputVolumeValue(&device));
[email protected]ee38bc42013-07-29 21:41:321939}
1940
tbarzic3ab01a22016-12-17 03:12:531941TEST_P(CrasAudioHandlerTest, SetOutputVolumePercentWithoutNotifyingObservers) {
1942 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker});
warxfd7c37b2016-08-05 19:38:071943 SetUpCrasAudioHandler(audio_nodes);
1944 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
1945
1946 const int kVolume1 = 60;
1947 const int kVolume2 = 80;
1948 cras_audio_handler_->SetOutputVolumePercentWithoutNotifyingObservers(
1949 kVolume1, CrasAudioHandler::VOLUME_CHANGE_MAXIMIZE_MODE_SCREENSHOT);
1950 // Verify the output volume is changed to the designated value,
1951 // OnOutputNodeVolumeChanged event is not fired, and the device volume value
1952 // is saved in the preferences.
1953 EXPECT_EQ(kVolume1, cras_audio_handler_->GetOutputVolumePercent());
1954 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
1955 AudioDevice device;
1956 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device));
tbarzic3ab01a22016-12-17 03:12:531957 EXPECT_EQ(device.id, kInternalSpeaker->id);
warxfd7c37b2016-08-05 19:38:071958 EXPECT_EQ(kVolume1, audio_pref_handler_->GetOutputVolumeValue(&device));
1959
1960 // Make another SetOutputVolumePercentWithoutNotifyingObservers call to make
1961 // sure everything is right.
1962 cras_audio_handler_->SetOutputVolumePercentWithoutNotifyingObservers(
1963 kVolume2, CrasAudioHandler::VOLUME_CHANGE_MAXIMIZE_MODE_SCREENSHOT);
1964 EXPECT_EQ(kVolume2, cras_audio_handler_->GetOutputVolumePercent());
1965 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
1966 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device));
tbarzic3ab01a22016-12-17 03:12:531967 EXPECT_EQ(device.id, kInternalSpeaker->id);
warxfd7c37b2016-08-05 19:38:071968 EXPECT_EQ(kVolume2, audio_pref_handler_->GetOutputVolumeValue(&device));
1969
1970 // Make a final SetOutputVolumePercent call to check if
1971 // SetOutputVolumePercentWithoutNotifyingObservers may block subsequent
1972 // notifying observers.
1973 cras_audio_handler_->SetOutputVolumePercent(kVolume1);
1974 EXPECT_EQ(kVolume1, cras_audio_handler_->GetOutputVolumePercent());
1975 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
1976 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device));
tbarzic3ab01a22016-12-17 03:12:531977 EXPECT_EQ(device.id, kInternalSpeaker->id);
warxfd7c37b2016-08-05 19:38:071978 EXPECT_EQ(kVolume1, audio_pref_handler_->GetOutputVolumeValue(&device));
1979}
1980
tbarzic3ab01a22016-12-17 03:12:531981TEST_P(CrasAudioHandlerTest, RestartAudioClientWithCrasReady) {
1982 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker});
warxfd7c37b2016-08-05 19:38:071983 SetUpCrasAudioHandler(audio_nodes);
1984 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
1985
1986 const int kDefaultVolume = cras_audio_handler_->GetOutputVolumePercent();
1987 // Disable the auto OutputNodeVolumeChanged signal.
1988 fake_cras_audio_client_->set_notify_volume_change_with_delay(true);
1989
1990 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes);
1991 RestartAudioClient();
1992 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
1993 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
1994
1995 // The correct initialization OutputNodeVolumeChanged event is fired. We
1996 // should avoid notifying observers.
1997 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
tbarzic3ab01a22016-12-17 03:12:531998 kInternalSpeaker->id, kDefaultVolume);
warxfd7c37b2016-08-05 19:38:071999 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2000 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2001
2002 // The later OutputNodeVolumeChanged event after initialization should notify
2003 // observers.
2004 const int kVolume = 60;
2005 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
tbarzic3ab01a22016-12-17 03:12:532006 kInternalSpeaker->id, kVolume);
warxfd7c37b2016-08-05 19:38:072007 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
2008 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent());
2009}
2010
tbarzic3ab01a22016-12-17 03:12:532011TEST_P(CrasAudioHandlerTest, RestartAudioClientWithCrasDropRequest) {
2012 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker});
warxfd7c37b2016-08-05 19:38:072013 SetUpCrasAudioHandler(audio_nodes);
2014 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2015
2016 const int kDefaultVolume = cras_audio_handler_->GetOutputVolumePercent();
2017 // Disable the auto OutputNodeVolumeChanged signal.
2018 fake_cras_audio_client_->set_notify_volume_change_with_delay(true);
2019
2020 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes);
2021 RestartAudioClient();
2022 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2023 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2024
2025 // A wrong initialization OutputNodeVolumeChanged event is fired. This may
2026 // happen when Cras is not ready and drops request. The approach we use is
2027 // to log warning message, clear the pending automated volume change reasons,
2028 // and notify observers about this change.
2029 const int kVolume1 = 30;
2030 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
tbarzic3ab01a22016-12-17 03:12:532031 kInternalSpeaker->id, kVolume1);
warxfd7c37b2016-08-05 19:38:072032 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
2033 EXPECT_EQ(kVolume1, cras_audio_handler_->GetOutputVolumePercent());
2034
2035 // The later OutputNodeVolumeChanged event should notify observers.
2036 const int kVolume2 = 60;
2037 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
tbarzic3ab01a22016-12-17 03:12:532038 kInternalSpeaker->id, kVolume2);
warxfd7c37b2016-08-05 19:38:072039 EXPECT_EQ(2, test_observer_->output_volume_changed_count());
2040 EXPECT_EQ(kVolume2, cras_audio_handler_->GetOutputVolumePercent());
2041}
2042
tbarzic3ab01a22016-12-17 03:12:532043TEST_P(CrasAudioHandlerTest, SetOutputVolumeWithDelayedSignal) {
2044 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker});
jennyz96526d02016-06-22 17:03:432045 SetUpCrasAudioHandler(audio_nodes);
2046 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2047
2048 const int kDefaultVolume = 75;
2049 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2050
2051 // Disable the auto OutputNodeVolumeChanged signal.
2052 fake_cras_audio_client_->set_notify_volume_change_with_delay(true);
2053
2054 // Verify the volume state is not changed before OutputNodeVolumeChanged
2055 // signal fires.
2056 const int kVolume = 60;
2057 cras_audio_handler_->SetOutputVolumePercent(kVolume);
2058 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2059 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2060
2061 // Verify the output volume is changed to the designated value after
2062 // OnOutputNodeVolumeChanged cras signal fires, and the volume change event
2063 // has been fired to notify the observers.
2064 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
tbarzic3ab01a22016-12-17 03:12:532065 kInternalSpeaker->id, kVolume);
jennyz96526d02016-06-22 17:03:432066 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
2067 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent());
2068 AudioDevice device;
2069 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device));
tbarzic3ab01a22016-12-17 03:12:532070 EXPECT_EQ(device.id, kInternalSpeaker->id);
jennyz96526d02016-06-22 17:03:432071 EXPECT_EQ(kVolume, audio_pref_handler_->GetOutputVolumeValue(&device));
2072}
2073
tbarzic3ab01a22016-12-17 03:12:532074TEST_P(CrasAudioHandlerTest,
jennyz96526d02016-06-22 17:03:432075 ChangeOutputVolumesWithDelayedSignalForSingleActiveDevice) {
tbarzic3ab01a22016-12-17 03:12:532076 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker});
jennyz96526d02016-06-22 17:03:432077 SetUpCrasAudioHandler(audio_nodes);
2078 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2079
2080 const int kDefaultVolume = 75;
2081 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2082
2083 // Disable the auto OutputNodeVolumeChanged signal.
2084 fake_cras_audio_client_->set_notify_volume_change_with_delay(true);
2085
2086 // Verify the volume state is not changed before OutputNodeVolumeChanged
2087 // signal fires.
2088 const int kVolume1 = 50;
2089 const int kVolume2 = 60;
2090 cras_audio_handler_->SetOutputVolumePercent(kVolume1);
2091 cras_audio_handler_->SetOutputVolumePercent(kVolume2);
2092 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2093 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2094
2095 // Simulate OutputNodeVolumeChanged signal fired with big latency that
2096 // it lags behind the SetOutputNodeVolume requests. Chrome sets the volume
2097 // to 50 then 60, but the volume changed signal for 50 comes back after
2098 // chrome sets the volume to 60. Verify chrome will sync to the designated
2099 // volume level after all signals arrive.
2100 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
tbarzic3ab01a22016-12-17 03:12:532101 kInternalSpeaker->id, kVolume1);
jennyz96526d02016-06-22 17:03:432102 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
2103 EXPECT_EQ(kVolume1, cras_audio_handler_->GetOutputVolumePercent());
2104
2105 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
tbarzic3ab01a22016-12-17 03:12:532106 kInternalSpeaker->id, kVolume2);
jennyz96526d02016-06-22 17:03:432107 EXPECT_EQ(2, test_observer_->output_volume_changed_count());
2108 EXPECT_EQ(kVolume2, cras_audio_handler_->GetOutputVolumePercent());
2109}
2110
tbarzic3ab01a22016-12-17 03:12:532111TEST_P(CrasAudioHandlerTest,
jennyz96526d02016-06-22 17:03:432112 ChangeOutputVolumeFromNonChromeSourceSingleActiveDevice) {
tbarzic3ab01a22016-12-17 03:12:532113 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker});
jennyz96526d02016-06-22 17:03:432114 SetUpCrasAudioHandler(audio_nodes);
2115 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2116
2117 const int kDefaultVolume = 75;
2118 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2119 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2120
2121 // Simulate OutputNodeVolumeChanged signal fired by a non-chrome source.
2122 // Verify chrome will sync its volume state to the volume from the signal,
2123 // and notify its observers for the volume change event.
2124 const int kVolume = 20;
2125 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
tbarzic3ab01a22016-12-17 03:12:532126 kInternalSpeaker->id, kVolume);
jennyz96526d02016-06-22 17:03:432127 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
2128 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent());
2129 AudioDevice device;
2130 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device));
tbarzic3ab01a22016-12-17 03:12:532131 EXPECT_EQ(device.id, kInternalSpeaker->id);
jennyz96526d02016-06-22 17:03:432132 EXPECT_EQ(kVolume, audio_pref_handler_->GetOutputVolumeValue(&device));
2133}
2134
tbarzic3ab01a22016-12-17 03:12:532135TEST_P(CrasAudioHandlerTest, SetInputGainPercent) {
2136 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalMic});
[email protected]ee38bc42013-07-29 21:41:322137 SetUpCrasAudioHandler(audio_nodes);
2138 EXPECT_EQ(0, test_observer_->input_gain_changed_count());
2139
2140 cras_audio_handler_->SetInputGainPercent(60);
2141
2142 // Verify the input gain changed to the designated value,
jennyz481292c2015-03-21 01:49:292143 // OnInputNodeGainChanged event is fired, and the device gain value
[email protected]ee38bc42013-07-29 21:41:322144 // is saved in the preferences.
2145 const int kGain = 60;
2146 EXPECT_EQ(kGain, cras_audio_handler_->GetInputGainPercent());
2147 EXPECT_EQ(1, test_observer_->input_gain_changed_count());
tbarzic3ab01a22016-12-17 03:12:532148 AudioDevice internal_mic(GenerateAudioNode(kInternalMic));
[email protected]140a31452013-09-09 18:54:372149 EXPECT_EQ(kGain, audio_pref_handler_->GetInputGainValue(&internal_mic));
[email protected]ee38bc42013-07-29 21:41:322150}
2151
tbarzic3ab01a22016-12-17 03:12:532152TEST_P(CrasAudioHandlerTest, SetMuteForDevice) {
2153 AudioNodeList audio_nodes = GenerateAudioNodeList(
2154 {kInternalSpeaker, kHeadphone, kInternalMic, kUSBMic});
[email protected]ee38bc42013-07-29 21:41:322155 SetUpCrasAudioHandler(audio_nodes);
2156
2157 // Mute the active output device.
tbarzic3ab01a22016-12-17 03:12:532158 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2159 cras_audio_handler_->SetMuteForDevice(kHeadphone->id, true);
[email protected]ee38bc42013-07-29 21:41:322160
2161 // Verify the headphone is muted and mute value is saved in the preferences.
tbarzic3ab01a22016-12-17 03:12:532162 EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kHeadphone->id));
2163 AudioDevice headphone(GenerateAudioNode(kHeadphone));
[email protected]ee38bc42013-07-29 21:41:322164 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(headphone));
2165
2166 // Mute the non-active output device.
tbarzic3ab01a22016-12-17 03:12:532167 cras_audio_handler_->SetMuteForDevice(kInternalSpeaker->id, true);
[email protected]ee38bc42013-07-29 21:41:322168
2169 // Verify the internal speaker is muted and mute value is saved in the
2170 // preferences.
tbarzic3ab01a22016-12-17 03:12:532171 EXPECT_TRUE(
2172 cras_audio_handler_->IsOutputMutedForDevice(kInternalSpeaker->id));
2173 AudioDevice internal_speaker(GenerateAudioNode(kInternalSpeaker));
[email protected]ee38bc42013-07-29 21:41:322174 EXPECT_TRUE(audio_pref_handler_->GetMuteValue(internal_speaker));
2175
2176 // Mute the active input device.
tbarzic3ab01a22016-12-17 03:12:532177 EXPECT_EQ(kUSBMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
2178 cras_audio_handler_->SetMuteForDevice(kUSBMic->id, true);
[email protected]ee38bc42013-07-29 21:41:322179
[email protected]f3dd2962014-04-22 04:48:382180 // Verify the USB Mic is muted.
tbarzic3ab01a22016-12-17 03:12:532181 EXPECT_TRUE(cras_audio_handler_->IsInputMutedForDevice(kUSBMic->id));
[email protected]ee38bc42013-07-29 21:41:322182
[email protected]f3dd2962014-04-22 04:48:382183 // Mute the non-active input device should be a no-op, see crbug.com/365050.
tbarzic3ab01a22016-12-17 03:12:532184 cras_audio_handler_->SetMuteForDevice(kInternalMic->id, true);
[email protected]ee38bc42013-07-29 21:41:322185
[email protected]f3dd2962014-04-22 04:48:382186 // Verify IsInputMutedForDevice returns false for non-active input device.
tbarzic3ab01a22016-12-17 03:12:532187 EXPECT_FALSE(cras_audio_handler_->IsInputMutedForDevice(kInternalMic->id));
[email protected]ee38bc42013-07-29 21:41:322188}
2189
tbarzic3ab01a22016-12-17 03:12:532190TEST_P(CrasAudioHandlerTest, SetVolumeGainPercentForDevice) {
2191 AudioNodeList audio_nodes = GenerateAudioNodeList(
2192 {kInternalSpeaker, kHeadphone, kInternalMic, kUSBMic});
[email protected]ee38bc42013-07-29 21:41:322193 SetUpCrasAudioHandler(audio_nodes);
2194
2195 // Set volume percent for active output device.
2196 const int kHeadphoneVolume = 30;
tbarzic3ab01a22016-12-17 03:12:532197 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2198 cras_audio_handler_->SetVolumeGainPercentForDevice(kHeadphone->id,
[email protected]ee38bc42013-07-29 21:41:322199 kHeadphoneVolume);
2200
2201 // Verify the volume percent of headphone is set, and saved in preferences.
tbarzic3ab01a22016-12-17 03:12:532202 EXPECT_EQ(
2203 kHeadphoneVolume,
2204 cras_audio_handler_->GetOutputVolumePercentForDevice(kHeadphone->id));
2205 AudioDevice headphone(GenerateAudioNode(kHeadphone));
[email protected]ee38bc42013-07-29 21:41:322206 EXPECT_EQ(kHeadphoneVolume,
[email protected]140a31452013-09-09 18:54:372207 audio_pref_handler_->GetOutputVolumeValue(&headphone));
[email protected]ee38bc42013-07-29 21:41:322208
2209 // Set volume percent for non-active output device.
2210 const int kSpeakerVolume = 60;
tbarzic3ab01a22016-12-17 03:12:532211 cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalSpeaker->id,
[email protected]ee38bc42013-07-29 21:41:322212 kSpeakerVolume);
2213
2214 // Verify the volume percent of speaker is set, and saved in preferences.
2215 EXPECT_EQ(kSpeakerVolume,
2216 cras_audio_handler_->GetOutputVolumePercentForDevice(
tbarzic3ab01a22016-12-17 03:12:532217 kInternalSpeaker->id));
2218 AudioDevice speaker(GenerateAudioNode(kInternalSpeaker));
[email protected]ee38bc42013-07-29 21:41:322219 EXPECT_EQ(kSpeakerVolume,
[email protected]140a31452013-09-09 18:54:372220 audio_pref_handler_->GetOutputVolumeValue(&speaker));
[email protected]ee38bc42013-07-29 21:41:322221
2222 // Set gain percent for active input device.
2223 const int kUSBMicGain = 30;
tbarzic3ab01a22016-12-17 03:12:532224 EXPECT_EQ(kUSBMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
2225 cras_audio_handler_->SetVolumeGainPercentForDevice(kUSBMic->id, kUSBMicGain);
[email protected]ee38bc42013-07-29 21:41:322226
2227 // Verify the gain percent of USB mic is set, and saved in preferences.
2228 EXPECT_EQ(kUSBMicGain,
tbarzic3ab01a22016-12-17 03:12:532229 cras_audio_handler_->GetOutputVolumePercentForDevice(kUSBMic->id));
2230 AudioDevice usb_mic(GenerateAudioNode(kHeadphone));
[email protected]ee38bc42013-07-29 21:41:322231 EXPECT_EQ(kUSBMicGain,
[email protected]140a31452013-09-09 18:54:372232 audio_pref_handler_->GetInputGainValue(&usb_mic));
[email protected]ee38bc42013-07-29 21:41:322233
2234 // Set gain percent for non-active input device.
2235 const int kInternalMicGain = 60;
tbarzic3ab01a22016-12-17 03:12:532236 cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalMic->id,
[email protected]ee38bc42013-07-29 21:41:322237 kInternalMicGain);
2238
2239 // Verify the gain percent of internal mic is set, and saved in preferences.
tbarzic3ab01a22016-12-17 03:12:532240 EXPECT_EQ(
2241 kInternalMicGain,
2242 cras_audio_handler_->GetOutputVolumePercentForDevice(kInternalMic->id));
2243 AudioDevice internal_mic(GenerateAudioNode(kInternalMic));
[email protected]ee38bc42013-07-29 21:41:322244 EXPECT_EQ(kInternalMicGain,
[email protected]140a31452013-09-09 18:54:372245 audio_pref_handler_->GetInputGainValue(&internal_mic));
[email protected]ee38bc42013-07-29 21:41:322246}
2247
tbarzic3ab01a22016-12-17 03:12:532248TEST_P(CrasAudioHandlerTest, HandleOtherDeviceType) {
[email protected]ee38bc42013-07-29 21:41:322249 const size_t kNumValidAudioDevices = 4;
tbarzic3ab01a22016-12-17 03:12:532250 AudioNodeList audio_nodes = GenerateAudioNodeList(
2251 {kInternalSpeaker, kOtherTypeOutput, kInternalMic, kOtherTypeInput});
[email protected]ee38bc42013-07-29 21:41:322252 SetUpCrasAudioHandler(audio_nodes);
2253
2254 // Verify the audio devices size.
2255 AudioDeviceList audio_devices;
2256 cras_audio_handler_->GetAudioDevices(&audio_devices);
2257 EXPECT_EQ(kNumValidAudioDevices, audio_devices.size());
2258
2259 // Verify the internal speaker has been selected as the active output,
2260 // and the output device with some randown unknown type is handled gracefully.
2261 AudioDevice active_output;
jennyzb47947f2014-09-25 00:42:042262 EXPECT_TRUE(
2263 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:532264 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
2265 EXPECT_EQ(kInternalSpeaker->id,
jennyzb47947f2014-09-25 00:42:042266 cras_audio_handler_->GetPrimaryActiveOutputNode());
[email protected]ee38bc42013-07-29 21:41:322267 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
2268
2269 // Ensure the internal microphone has been selected as the active input,
2270 // and the input device with some random unknown type is handled gracefully.
2271 AudioDevice active_input;
tbarzic3ab01a22016-12-17 03:12:532272 EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
[email protected]ee38bc42013-07-29 21:41:322273 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
2274}
2275
tbarzic3ab01a22016-12-17 03:12:532276TEST_P(CrasAudioHandlerTest, ActiveDeviceSelectionWithStableDeviceId) {
hychaofe100d542016-01-12 10:17:522277 AudioNodeList audio_nodes;
tbarzic3ab01a22016-12-17 03:12:532278 AudioNode internal_speaker = GenerateAudioNode(kInternalSpeaker);
hychaofe100d542016-01-12 10:17:522279 audio_nodes.push_back(internal_speaker);
tbarzic3ab01a22016-12-17 03:12:532280 AudioNode usb_headset = GenerateAudioNode(kUSBHeadphone1);
hychaofe100d542016-01-12 10:17:522281 usb_headset.plugged_time = 80000000;
2282 audio_nodes.push_back(usb_headset);
2283 SetUpCrasAudioHandler(audio_nodes);
2284
2285 // Verify the audio devices size.
2286 AudioDeviceList audio_devices;
2287 cras_audio_handler_->GetAudioDevices(&audio_devices);
2288 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2289
2290 // Initially active node is selected base on priority, so USB headphone
2291 // is selected.
tbarzic3ab01a22016-12-17 03:12:532292 EXPECT_EQ(kUSBHeadphone1->id,
hychaofe100d542016-01-12 10:17:522293 cras_audio_handler_->GetPrimaryActiveOutputNode());
2294
2295 // Change the active device to internal speaker, now USB headphone becomes
2296 // inactive.
tbarzic3ab01a22016-12-17 03:12:532297 AudioDevice speaker(GenerateAudioNode(kInternalSpeaker));
jennyzbd236742016-03-02 20:15:522298 cras_audio_handler_->SwitchToDevice(speaker, true,
2299 CrasAudioHandler::ACTIVATE_BY_USER);
tbarzic3ab01a22016-12-17 03:12:532300 EXPECT_NE(kUSBHeadphone1->id,
hychaofe100d542016-01-12 10:17:522301 cras_audio_handler_->GetPrimaryActiveOutputNode());
2302
2303 // Unplug USB headset.
2304 audio_nodes.clear();
2305 internal_speaker.active = true;
2306 audio_nodes.push_back(internal_speaker);
2307 ChangeAudioNodes(audio_nodes);
tbarzic3ab01a22016-12-17 03:12:532308 EXPECT_EQ(kInternalSpeaker->id,
hychaofe100d542016-01-12 10:17:522309 cras_audio_handler_->GetPrimaryActiveOutputNode());
2310
2311 // Plug the same USB headset back, id is different, but stable_device_id
2312 // remains the same.
2313 usb_headset.active = false;
2314 usb_headset.id = 98765;
2315 audio_nodes.push_back(usb_headset);
2316 ChangeAudioNodes(audio_nodes);
2317
2318 // Since USB headset was inactive before it was unplugged, it won't be
2319 // selected as active after it's plugged in again.
tbarzic3ab01a22016-12-17 03:12:532320 EXPECT_EQ(kInternalSpeaker->id,
hychaofe100d542016-01-12 10:17:522321 cras_audio_handler_->GetPrimaryActiveOutputNode());
2322
2323 // Plug the second USB headset.
tbarzic3ab01a22016-12-17 03:12:532324 AudioNode usb_headset2 = GenerateAudioNode(kUSBHeadphone2);
hychaofe100d542016-01-12 10:17:522325 usb_headset2.plugged_time = 80000001;
2326 audio_nodes.push_back(usb_headset2);
2327 ChangeAudioNodes(audio_nodes);
2328
2329 // Since the second USB device is new, it's selected as the active device
2330 // by its priority.
tbarzic3ab01a22016-12-17 03:12:532331 EXPECT_EQ(kUSBHeadphone2->id,
hychaofe100d542016-01-12 10:17:522332 cras_audio_handler_->GetPrimaryActiveOutputNode());
2333
2334 // Unplug the second USB headset.
2335 audio_nodes.clear();
2336 internal_speaker.active = false;
2337 audio_nodes.push_back(internal_speaker);
2338 audio_nodes.push_back(usb_headset);
2339 ChangeAudioNodes(audio_nodes);
2340
2341 // There is no active node after USB2 unplugged, the 1st USB got selected
2342 // by its priority.
2343 EXPECT_EQ(usb_headset.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2344
jennyzbd236742016-03-02 20:15:522345 audio_nodes.clear();
2346 internal_speaker.active = false;
2347 audio_nodes.push_back(internal_speaker);
2348 usb_headset.active = true;
2349 audio_nodes.push_back(usb_headset);
hychaofe100d542016-01-12 10:17:522350 usb_headset2.active = false;
2351 usb_headset2.plugged_time = 80000002;
2352 audio_nodes.push_back(usb_headset2);
2353 ChangeAudioNodes(audio_nodes);
2354
2355 // Plug the second USB again. Since it was the active node before it got
2356 // unplugged, it is now selected as the active node.
2357 EXPECT_EQ(usb_headset2.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
2358}
2359
jennyzbd236742016-03-02 20:15:522360// Test the device new session case, either via reboot or logout, if there
2361// is an active device in the previous session, that device should still
2362// be set as active after the new session starts.
tbarzic3ab01a22016-12-17 03:12:532363TEST_P(CrasAudioHandlerTest, PersistActiveDeviceAcrossSession) {
jennyzbd236742016-03-02 20:15:522364 // Set the active device to internal speaker before the session starts.
tbarzic3ab01a22016-12-17 03:12:532365 AudioNodeList audio_nodes =
2366 GenerateAudioNodeList({kInternalSpeaker, kHeadphone});
jennyzbd236742016-03-02 20:15:522367 SetupCrasAudioHandlerWithActiveNodeInPref(
tbarzic3ab01a22016-12-17 03:12:532368 audio_nodes, audio_nodes,
2369 AudioDevice(GenerateAudioNode(kInternalSpeaker)), true);
jennyzbd236742016-03-02 20:15:522370
2371 // Verify the audio devices size.
2372 AudioDeviceList audio_devices;
2373 cras_audio_handler_->GetAudioDevices(&audio_devices);
2374 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2375
2376 // Verify the active device is the internal speaker, which is of a lower
2377 // priority, but selected as active since it was the active device previously.
tbarzic3ab01a22016-12-17 03:12:532378 EXPECT_EQ(kInternalSpeaker->id,
jennyzbd236742016-03-02 20:15:522379 cras_audio_handler_->GetPrimaryActiveOutputNode());
2380}
2381
tbarzic3ab01a22016-12-17 03:12:532382TEST_P(CrasAudioHandlerTest, PersistActiveSpeakerAcrossReboot) {
jennyzbd236742016-03-02 20:15:522383 // Simulates the device was shut down with three audio devices, and
2384 // internal speaker being the active one selected by user.
tbarzic3ab01a22016-12-17 03:12:532385 AudioNodeList audio_nodes_in_pref =
2386 GenerateAudioNodeList({kInternalSpeaker, kHeadphone, kUSBHeadphone1});
jennyzbd236742016-03-02 20:15:522387
2388 // Simulate the first NodesChanged signal coming with only one node.
tbarzic3ab01a22016-12-17 03:12:532389 AudioNodeList audio_nodes = GenerateAudioNodeList({kUSBHeadphone1});
jennyzbd236742016-03-02 20:15:522390
2391 SetupCrasAudioHandlerWithActiveNodeInPref(
tbarzic3ab01a22016-12-17 03:12:532392 audio_nodes, audio_nodes_in_pref,
2393 AudioDevice(GenerateAudioNode(kInternalSpeaker)), true);
jennyzbd236742016-03-02 20:15:522394
2395 // Verify the usb headphone has been made active.
2396 AudioDeviceList audio_devices;
2397 cras_audio_handler_->GetAudioDevices(&audio_devices);
2398 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
tbarzic3ab01a22016-12-17 03:12:532399 EXPECT_EQ(kUSBHeadphone1->id,
jennyzbd236742016-03-02 20:15:522400 cras_audio_handler_->GetPrimaryActiveOutputNode());
2401
2402 // Simulate another NodesChanged signal coming later with all ndoes.
tbarzic3ab01a22016-12-17 03:12:532403 audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
2404 audio_nodes.push_back(GenerateAudioNode(kHeadphone));
jennyzbd236742016-03-02 20:15:522405 ChangeAudioNodes(audio_nodes);
2406
2407 // Verify the active output has been restored to internal speaker.
2408 cras_audio_handler_->GetAudioDevices(&audio_devices);
2409 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
tbarzic3ab01a22016-12-17 03:12:532410 EXPECT_EQ(kInternalSpeaker->id,
jennyzbd236742016-03-02 20:15:522411 cras_audio_handler_->GetPrimaryActiveOutputNode());
2412}
2413
jennyz16e103e2017-03-31 18:15:252414// crbug.com/698809. User plug in USB speaker, then unplug it, leave
2415// internal speaker as active device. Power down, plug in USB speaker again.
2416// When the device powers up again, the first NodesChanged signal comes with
2417// only USB speaker; followed by another NodesChanged signal with internal
2418// speaker added.
2419TEST_P(CrasAudioHandlerTest, USBShouldBeActiveAfterReboot) {
2420 // Start with both interanl speaker and USB speaker.
2421 AudioNodeList audio_nodes =
2422 GenerateAudioNodeList({kInternalSpeaker, kUSBHeadphone1});
2423
2424 SetUpCrasAudioHandler(audio_nodes);
2425
2426 // Verify the usb headphone has been made active by priority.
2427 AudioDeviceList audio_devices;
2428 cras_audio_handler_->GetAudioDevices(&audio_devices);
2429 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2430 EXPECT_EQ(kUSBHeadphone1->id,
2431 cras_audio_handler_->GetPrimaryActiveOutputNode());
2432
2433 // Remove USB headphone.
2434 audio_nodes.clear();
2435 audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
2436 ChangeAudioNodes(audio_nodes);
2437 // Verify the internal speaker becomes the active device by priority.
2438 cras_audio_handler_->GetAudioDevices(&audio_devices);
2439 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2440 EXPECT_EQ(kInternalSpeaker->id,
2441 cras_audio_handler_->GetPrimaryActiveOutputNode());
2442
2443 // Simulate after power off, plug in usb header phone, then power on.
2444 // The first NodesChanged signal sends usb headphone only.
2445 audio_nodes.clear();
2446 audio_nodes.push_back(GenerateAudioNode(kUSBHeadphone1));
2447 ChangeAudioNodes(audio_nodes);
2448 // Verify the usb headerphone becomes the active device by priority.
2449 cras_audio_handler_->GetAudioDevices(&audio_devices);
2450 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2451 EXPECT_EQ(kUSBHeadphone1->id,
2452 cras_audio_handler_->GetPrimaryActiveOutputNode());
2453
2454 // Simulate the second NodesChanged signal comes with internal speaker added.
2455 audio_nodes.clear();
2456 AudioNode usb_headphone = GenerateAudioNode(kUSBHeadphone1);
2457 usb_headphone.active = true;
2458 audio_nodes.push_back(usb_headphone);
2459 audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
2460 ChangeAudioNodes(audio_nodes);
2461 // Verify the usb headerphone is still the active device.
2462 cras_audio_handler_->GetAudioDevices(&audio_devices);
2463 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2464 EXPECT_EQ(kUSBHeadphone1->id,
2465 cras_audio_handler_->GetPrimaryActiveOutputNode());
2466}
2467
jennyz6d5579b2016-07-28 17:49:112468// Test the corner case that headphone is plugged in for the first time on
2469// a cros device after the device is shutdown.
2470// crbug.com/622045.
tbarzic3ab01a22016-12-17 03:12:532471TEST_P(CrasAudioHandlerTest, PlugInHeadphoneFirstTimeAfterPowerDown) {
jennyz6d5579b2016-07-28 17:49:112472 // Simulate plugging headphone for the first on a cros device after it is
2473 // powered down. Internal speaker is set up in audio prefs as active
2474 // before the new cros session starts.
tbarzic3ab01a22016-12-17 03:12:532475 AudioNodeList audio_nodes_in_pref = GenerateAudioNodeList({kInternalSpeaker});
jennyz6d5579b2016-07-28 17:49:112476
tbarzic3ab01a22016-12-17 03:12:532477 AudioNodeList audio_nodes =
2478 GenerateAudioNodeList({kInternalSpeaker, kHeadphone});
jennyz6d5579b2016-07-28 17:49:112479
2480 SetupCrasAudioHandlerWithActiveNodeInPref(
tbarzic3ab01a22016-12-17 03:12:532481 audio_nodes, audio_nodes_in_pref,
2482 AudioDevice(GenerateAudioNode(kInternalSpeaker)), false);
jennyz6d5579b2016-07-28 17:49:112483
2484 // Verify the audio devices size.
2485 AudioDeviceList audio_devices;
2486 cras_audio_handler_->GetAudioDevices(&audio_devices);
2487 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2488
2489 // Verify headphone becomes the active output.
tbarzic3ab01a22016-12-17 03:12:532490 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
jennyz6d5579b2016-07-28 17:49:112491}
2492
tbarzic3ab01a22016-12-17 03:12:532493TEST_P(CrasAudioHandlerTest,
jennyzbd236742016-03-02 20:15:522494 PersistActiveUsbHeadphoneAcrossRebootUsbComeLater) {
2495 // Simulates the device was shut down with three audio devices, and
2496 // usb headphone being the active one selected by priority.
tbarzic3ab01a22016-12-17 03:12:532497 AudioNodeList audio_nodes_in_pref =
2498 GenerateAudioNodeList({kInternalSpeaker, kHeadphone, kUSBHeadphone1});
jennyzbd236742016-03-02 20:15:522499
2500 // Simulate the first NodesChanged signal coming with only internal speaker
2501 // and the headphone.
tbarzic3ab01a22016-12-17 03:12:532502 AudioNodeList audio_nodes =
2503 GenerateAudioNodeList({kInternalSpeaker, kHeadphone});
jennyzbd236742016-03-02 20:15:522504
tbarzic3ab01a22016-12-17 03:12:532505 SetupCrasAudioHandlerWithActiveNodeInPref(
2506 audio_nodes, audio_nodes_in_pref,
2507 AudioDevice(GenerateAudioNode(kUSBHeadphone1)), false);
jennyzbd236742016-03-02 20:15:522508
2509 // Verify the headphone has been made active.
2510 AudioDeviceList audio_devices;
2511 cras_audio_handler_->GetAudioDevices(&audio_devices);
2512 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
tbarzic3ab01a22016-12-17 03:12:532513 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
jennyzbd236742016-03-02 20:15:522514
2515 // Simulate USB node comes later with all ndoes.
tbarzic3ab01a22016-12-17 03:12:532516 AudioNode usb_node = GenerateAudioNode(kUSBHeadphone1);
jennyzbd236742016-03-02 20:15:522517 usb_node.plugged_time = 80000000;
2518 audio_nodes.push_back(usb_node);
2519 ChangeAudioNodes(audio_nodes);
2520
2521 // Verify the active output has been restored to usb headphone.
2522 cras_audio_handler_->GetAudioDevices(&audio_devices);
2523 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
tbarzic3ab01a22016-12-17 03:12:532524 EXPECT_EQ(kUSBHeadphone1->id,
jennyzbd236742016-03-02 20:15:522525 cras_audio_handler_->GetPrimaryActiveOutputNode());
2526}
2527
tbarzic3ab01a22016-12-17 03:12:532528TEST_P(CrasAudioHandlerTest,
jennyzbd236742016-03-02 20:15:522529 PersistActiveUsbHeadphoneAcrossRebootUsbComeFirst) {
2530 // Simulates the device was shut down with three audio devices, and
2531 // usb headphone being the active one selected by priority.
tbarzic3ab01a22016-12-17 03:12:532532 AudioNodeList audio_nodes_in_pref =
2533 GenerateAudioNodeList({kInternalSpeaker, kHeadphone, kUSBHeadphone1});
jennyzbd236742016-03-02 20:15:522534
2535 // Simulate the first NodesChanged signal coming with only internal speaker
2536 // and the USB headphone.
tbarzic3ab01a22016-12-17 03:12:532537 AudioNodeList audio_nodes =
2538 GenerateAudioNodeList({kInternalSpeaker, kUSBHeadphone1});
jennyzbd236742016-03-02 20:15:522539
tbarzic3ab01a22016-12-17 03:12:532540 SetupCrasAudioHandlerWithActiveNodeInPref(
2541 audio_nodes, audio_nodes_in_pref,
2542 AudioDevice(GenerateAudioNode(kUSBHeadphone1)), false);
jennyzbd236742016-03-02 20:15:522543
2544 // Verify the USB headphone has been made active.
2545 AudioDeviceList audio_devices;
2546 cras_audio_handler_->GetAudioDevices(&audio_devices);
2547 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
tbarzic3ab01a22016-12-17 03:12:532548 EXPECT_EQ(kUSBHeadphone1->id,
jennyzbd236742016-03-02 20:15:522549 cras_audio_handler_->GetPrimaryActiveOutputNode());
2550
2551 // Simulate another NodesChanged signal coming later with all ndoes.
tbarzic3ab01a22016-12-17 03:12:532552 AudioNode headphone_node = GenerateAudioNode(kHeadphone);
jennyzbd236742016-03-02 20:15:522553 headphone_node.plugged_time = 80000000;
2554 audio_nodes.push_back(headphone_node);
2555 ChangeAudioNodes(audio_nodes);
2556
2557 // Verify the active output has been restored to USB headphone.
2558 cras_audio_handler_->GetAudioDevices(&audio_devices);
2559 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
tbarzic3ab01a22016-12-17 03:12:532560 EXPECT_EQ(kUSBHeadphone1->id,
jennyzbd236742016-03-02 20:15:522561 cras_audio_handler_->GetPrimaryActiveOutputNode());
2562}
2563
2564// This covers the crbug.com/586026. Cras lost the active state of the internal
2565// speaker when user unplugs the headphone, which is a bug in cras. However,
2566// chrome code is still resilient and set the internal speaker back to active.
tbarzic3ab01a22016-12-17 03:12:532567TEST_P(CrasAudioHandlerTest, UnplugHeadphoneLostActiveInternalSpeakerByCras) {
jennyzbd236742016-03-02 20:15:522568 // Set up with three nodes.
tbarzic3ab01a22016-12-17 03:12:532569 AudioNodeList audio_nodes =
2570 GenerateAudioNodeList({kInternalSpeaker, kHeadphone, kUSBHeadphone1});
jennyzbd236742016-03-02 20:15:522571 SetUpCrasAudioHandler(audio_nodes);
2572
2573 // Switch the active output to internal speaker.
tbarzic3ab01a22016-12-17 03:12:532574 cras_audio_handler_->SwitchToDevice(
2575 AudioDevice(GenerateAudioNode(kInternalSpeaker)), true,
2576 CrasAudioHandler::ACTIVATE_BY_USER);
jennyzbd236742016-03-02 20:15:522577
2578 // Verify internal speaker has been made active.
2579 AudioDeviceList audio_devices;
2580 cras_audio_handler_->GetAudioDevices(&audio_devices);
2581 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
tbarzic3ab01a22016-12-17 03:12:532582 EXPECT_EQ(kInternalSpeaker->id,
jennyzbd236742016-03-02 20:15:522583 cras_audio_handler_->GetPrimaryActiveOutputNode());
2584
2585 // Simulate unplug the headphone. Cras sends NodesChanged signal with
2586 // both internal speaker and usb headphone being inactive.
2587 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:532588 audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
2589 audio_nodes.push_back(GenerateAudioNode(kUSBHeadphone1));
2590 for (const auto& node : audio_nodes)
2591 ASSERT_FALSE(node.active) << node.id << " expexted to be inactive";
jennyzbd236742016-03-02 20:15:522592 ChangeAudioNodes(audio_nodes);
2593
2594 // Verify the active output is set back to internal speaker.
2595 cras_audio_handler_->GetAudioDevices(&audio_devices);
2596 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
tbarzic3ab01a22016-12-17 03:12:532597 EXPECT_EQ(kInternalSpeaker->id,
jennyzbd236742016-03-02 20:15:522598 cras_audio_handler_->GetPrimaryActiveOutputNode());
2599}
2600
tbarzic3ab01a22016-12-17 03:12:532601TEST_P(CrasAudioHandlerTest, RemoveNonActiveDevice) {
jennyzbd236742016-03-02 20:15:522602 // Set up with three nodes.
tbarzic3ab01a22016-12-17 03:12:532603 AudioNodeList audio_nodes =
2604 GenerateAudioNodeList({kInternalSpeaker, kHeadphone, kUSBHeadphone1});
jennyzbd236742016-03-02 20:15:522605 SetUpCrasAudioHandler(audio_nodes);
2606
2607 // Switch the active output to internal speaker.
tbarzic3ab01a22016-12-17 03:12:532608 cras_audio_handler_->SwitchToDevice(
2609 AudioDevice(GenerateAudioNode(kInternalSpeaker)), true,
2610 CrasAudioHandler::ACTIVATE_BY_USER);
jennyzbd236742016-03-02 20:15:522611
2612 // Verify internal speaker has been made active.
2613 AudioDeviceList audio_devices;
2614 cras_audio_handler_->GetAudioDevices(&audio_devices);
2615 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
tbarzic3ab01a22016-12-17 03:12:532616 EXPECT_EQ(kInternalSpeaker->id,
jennyzbd236742016-03-02 20:15:522617 cras_audio_handler_->GetPrimaryActiveOutputNode());
2618
2619 // Remove headphone, which is an non-active device.
2620 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:532621 AudioNode speaker = GenerateAudioNode(kInternalSpeaker);
jennyzbd236742016-03-02 20:15:522622 speaker.active = true;
2623 audio_nodes.push_back(speaker);
tbarzic3ab01a22016-12-17 03:12:532624 AudioNode usb_headphone = GenerateAudioNode(kUSBHeadphone1);
jennyzbd236742016-03-02 20:15:522625 usb_headphone.active = false;
2626 audio_nodes.push_back(usb_headphone);
2627
2628 ChangeAudioNodes(audio_nodes);
2629
2630 // Verify the active output remains as internal speaker.
2631 cras_audio_handler_->GetAudioDevices(&audio_devices);
2632 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
tbarzic3ab01a22016-12-17 03:12:532633 EXPECT_EQ(kInternalSpeaker->id,
jennyzbd236742016-03-02 20:15:522634 cras_audio_handler_->GetPrimaryActiveOutputNode());
2635}
2636
tbarzic3ab01a22016-12-17 03:12:532637TEST_P(CrasAudioHandlerTest, ChangeActiveNodesHotrodInit) {
tbarziccae06e2a2017-01-17 23:14:472638 // This simulates a typical hotrod audio device configuration.
tbarzic3ab01a22016-12-17 03:12:532639 AudioNodeList audio_nodes = GenerateAudioNodeList(
2640 {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerOutput2,
2641 kUSBJabraSpeakerInput1, kUSBJabraSpeakerInput2, kUSBCameraInput});
jennyzb47947f2014-09-25 00:42:042642 SetUpCrasAudioHandler(audio_nodes);
2643
2644 // Verify the audio devices size.
2645 AudioDeviceList audio_devices;
2646 cras_audio_handler_->GetAudioDevices(&audio_devices);
2647 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2648
jennyz2cb9eb12014-11-05 19:21:052649 // Verify only the 1st jabra speaker's output and input are selected as active
2650 // nodes by CrasAudioHandler.
jennyzb47947f2014-09-25 00:42:042651 AudioDevice active_output;
2652 EXPECT_TRUE(
2653 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
jennyz2cb9eb12014-11-05 19:21:052654 EXPECT_EQ(2, GetActiveDeviceCount());
jennyzb47947f2014-09-25 00:42:042655 AudioDevice primary_active_device;
2656 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(
2657 &primary_active_device));
tbarzic3ab01a22016-12-17 03:12:532658 EXPECT_EQ(kUSBJabraSpeakerOutput1->id, primary_active_device.id);
2659 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
jennyz2cb9eb12014-11-05 19:21:052660 cras_audio_handler_->GetPrimaryActiveInputNode());
2661
2662 // Set both jabra speakers's input and output nodes to active, this simulate
2663 // the call sent by hotrod initialization process.
2664 test_observer_->reset_active_output_node_changed_count();
2665 test_observer_->reset_active_input_node_changed_count();
tbarziccae06e2a2017-01-17 23:14:472666 cras_audio_handler_->ChangeActiveNodes(
2667 {kUSBJabraSpeakerOutput1->id, kUSBJabraSpeakerOutput2->id,
2668 kUSBJabraSpeakerInput1->id, kUSBJabraSpeakerInput2->id});
jennyz2cb9eb12014-11-05 19:21:052669
2670 // Verify both jabra speakers' input/output nodes are made active.
2671 // num_active_nodes = GetActiveDeviceCount();
2672 EXPECT_EQ(4, GetActiveDeviceCount());
2673 const AudioDevice* active_output_1 =
tbarzic3ab01a22016-12-17 03:12:532674 GetDeviceFromId(kUSBJabraSpeakerOutput1->id);
jennyz2cb9eb12014-11-05 19:21:052675 EXPECT_TRUE(active_output_1->active);
2676 const AudioDevice* active_output_2 =
tbarzic3ab01a22016-12-17 03:12:532677 GetDeviceFromId(kUSBJabraSpeakerOutput2->id);
jennyz2cb9eb12014-11-05 19:21:052678 EXPECT_TRUE(active_output_2->active);
2679 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(
2680 &primary_active_device));
tbarzic3ab01a22016-12-17 03:12:532681 EXPECT_EQ(kUSBJabraSpeakerOutput1->id, primary_active_device.id);
jennyz2cb9eb12014-11-05 19:21:052682 const AudioDevice* active_input_1 =
tbarzic3ab01a22016-12-17 03:12:532683 GetDeviceFromId(kUSBJabraSpeakerInput1->id);
jennyz2cb9eb12014-11-05 19:21:052684 EXPECT_TRUE(active_input_1->active);
2685 const AudioDevice* active_input_2 =
tbarzic3ab01a22016-12-17 03:12:532686 GetDeviceFromId(kUSBJabraSpeakerInput2->id);
jennyz2cb9eb12014-11-05 19:21:052687 EXPECT_TRUE(active_input_2->active);
tbarzic3ab01a22016-12-17 03:12:532688 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
jennyz2cb9eb12014-11-05 19:21:052689 cras_audio_handler_->GetPrimaryActiveInputNode());
2690
2691 // Verify only 1 ActiveOutputNodeChanged notification has been sent out
2692 // by calling ChangeActiveNodes.
2693 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
2694 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
jennyzb47947f2014-09-25 00:42:042695
2696 // Verify all active devices are the not muted and their volume values are
2697 // the same.
2698 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
jennyz2cb9eb12014-11-05 19:21:052699 EXPECT_FALSE(
tbarzic3ab01a22016-12-17 03:12:532700 cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput1->id));
jennyz2cb9eb12014-11-05 19:21:052701 EXPECT_FALSE(
tbarzic3ab01a22016-12-17 03:12:532702 cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput2->id));
jennyz2cb9eb12014-11-05 19:21:052703 EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(),
2704 cras_audio_handler_->GetOutputVolumePercentForDevice(
tbarzic3ab01a22016-12-17 03:12:532705 kUSBJabraSpeakerOutput1->id));
jennyz2cb9eb12014-11-05 19:21:052706 EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(),
2707 cras_audio_handler_->GetOutputVolumePercentForDevice(
tbarzic3ab01a22016-12-17 03:12:532708 kUSBJabraSpeakerOutput2->id));
jennyzb47947f2014-09-25 00:42:042709
2710 // Adjust the volume of output devices, verify all active nodes are set to
2711 // the same volume.
2712 cras_audio_handler_->SetOutputVolumePercent(25);
2713 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercent());
jennyz2cb9eb12014-11-05 19:21:052714 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercentForDevice(
tbarzic3ab01a22016-12-17 03:12:532715 kUSBJabraSpeakerOutput1->id));
jennyz2cb9eb12014-11-05 19:21:052716 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercentForDevice(
tbarzic3ab01a22016-12-17 03:12:532717 kUSBJabraSpeakerOutput2->id));
jennyz2cb9eb12014-11-05 19:21:052718}
jennyzb47947f2014-09-25 00:42:042719
tbarziccae06e2a2017-01-17 23:14:472720TEST_P(CrasAudioHandlerTest, SetActiveNodesHotrodInit) {
2721 // This simulates a typical hotrod audio device configuration.
2722 AudioNodeList audio_nodes = GenerateAudioNodeList(
2723 {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerOutput2,
2724 kUSBJabraSpeakerInput1, kUSBJabraSpeakerInput2, kUSBCameraInput});
2725 SetUpCrasAudioHandler(audio_nodes);
2726
2727 // Verify the audio devices size.
2728 AudioDeviceList audio_devices;
2729 cras_audio_handler_->GetAudioDevices(&audio_devices);
2730 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2731
2732 // Verify only the 1st jabra speaker's output and input are selected as active
2733 // nodes by CrasAudioHandler.
2734 AudioDevice active_output;
2735 EXPECT_TRUE(
2736 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
2737 EXPECT_EQ(2, GetActiveDeviceCount());
2738 AudioDevice primary_active_device;
2739 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(
2740 &primary_active_device));
2741 EXPECT_EQ(kUSBJabraSpeakerOutput1->id, primary_active_device.id);
2742 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
2743 cras_audio_handler_->GetPrimaryActiveInputNode());
2744
2745 // Set both jabra speakers's input and output nodes to active, this simulate
2746 // the call sent by hotrod initialization process.
2747 test_observer_->reset_active_output_node_changed_count();
2748 test_observer_->reset_active_input_node_changed_count();
2749
2750 cras_audio_handler_->SetActiveInputNodes(
2751 {kUSBJabraSpeakerInput1->id, kUSBJabraSpeakerInput2->id});
2752
2753 cras_audio_handler_->SetActiveOutputNodes(
2754 {kUSBJabraSpeakerOutput1->id, kUSBJabraSpeakerOutput2->id});
2755
2756 // Verify both jabra speakers' input/output nodes are made active.
2757 // num_active_nodes = GetActiveDeviceCount();
2758 EXPECT_EQ(4, GetActiveDeviceCount());
2759 const AudioDevice* active_output_1 =
2760 GetDeviceFromId(kUSBJabraSpeakerOutput1->id);
2761 EXPECT_TRUE(active_output_1->active);
2762 const AudioDevice* active_output_2 =
2763 GetDeviceFromId(kUSBJabraSpeakerOutput2->id);
2764 EXPECT_TRUE(active_output_2->active);
2765 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(
2766 &primary_active_device));
2767 EXPECT_EQ(kUSBJabraSpeakerOutput1->id, primary_active_device.id);
2768 const AudioDevice* active_input_1 =
2769 GetDeviceFromId(kUSBJabraSpeakerInput1->id);
2770 EXPECT_TRUE(active_input_1->active);
2771 const AudioDevice* active_input_2 =
2772 GetDeviceFromId(kUSBJabraSpeakerInput2->id);
2773 EXPECT_TRUE(active_input_2->active);
2774 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
2775 cras_audio_handler_->GetPrimaryActiveInputNode());
2776
2777 // Verify only 1 ActiveOutputNodeChanged notification has been sent out
2778 // by calling SetActiveNodes.
2779 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
2780 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
2781
2782 // Verify all active devices are the not muted and their volume values are
2783 // the same.
2784 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
2785 EXPECT_FALSE(
2786 cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput1->id));
2787 EXPECT_FALSE(
2788 cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput2->id));
2789 EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(),
2790 cras_audio_handler_->GetOutputVolumePercentForDevice(
2791 kUSBJabraSpeakerOutput1->id));
2792 EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(),
2793 cras_audio_handler_->GetOutputVolumePercentForDevice(
2794 kUSBJabraSpeakerOutput2->id));
2795
2796 // Adjust the volume of output devices, verify all active nodes are set to
2797 // the same volume.
2798 cras_audio_handler_->SetOutputVolumePercent(25);
2799 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercent());
2800 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercentForDevice(
2801 kUSBJabraSpeakerOutput1->id));
2802 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercentForDevice(
2803 kUSBJabraSpeakerOutput2->id));
2804}
2805
tbarzic3ab01a22016-12-17 03:12:532806TEST_P(CrasAudioHandlerTest, ChangeVolumeHotrodDualSpeakersWithDelayedSignals) {
2807 AudioNodeList audio_nodes = GenerateAudioNodeList(
2808 {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerOutput2});
jennyz96526d02016-06-22 17:03:432809 SetUpCrasAudioHandler(audio_nodes);
2810
2811 // Verify the audio devices size.
2812 AudioDeviceList audio_devices;
2813 cras_audio_handler_->GetAudioDevices(&audio_devices);
2814 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2815
2816 // Set both jabra speakers nodes to active, this simulate
2817 // the call sent by hotrod initialization process.
2818 test_observer_->reset_active_output_node_changed_count();
tbarziccae06e2a2017-01-17 23:14:472819 cras_audio_handler_->ChangeActiveNodes(
2820 {kUSBJabraSpeakerOutput1->id, kUSBJabraSpeakerOutput2->id});
jennyz96526d02016-06-22 17:03:432821
2822 // Verify both jabra speakers are made active.
2823 EXPECT_EQ(2, GetActiveDeviceCount());
2824 const AudioDevice* active_output_1 =
tbarzic3ab01a22016-12-17 03:12:532825 GetDeviceFromId(kUSBJabraSpeakerOutput1->id);
jennyz96526d02016-06-22 17:03:432826 EXPECT_TRUE(active_output_1->active);
2827 const AudioDevice* active_output_2 =
tbarzic3ab01a22016-12-17 03:12:532828 GetDeviceFromId(kUSBJabraSpeakerOutput2->id);
jennyz96526d02016-06-22 17:03:432829 EXPECT_TRUE(active_output_2->active);
2830
2831 // Verify all active devices are the not muted and their volume values are
2832 // the same.
2833 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
2834 EXPECT_FALSE(
tbarzic3ab01a22016-12-17 03:12:532835 cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput1->id));
jennyz96526d02016-06-22 17:03:432836 EXPECT_FALSE(
tbarzic3ab01a22016-12-17 03:12:532837 cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput2->id));
jennyz96526d02016-06-22 17:03:432838 EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(),
2839 cras_audio_handler_->GetOutputVolumePercentForDevice(
tbarzic3ab01a22016-12-17 03:12:532840 kUSBJabraSpeakerOutput1->id));
jennyz96526d02016-06-22 17:03:432841 EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(),
2842 cras_audio_handler_->GetOutputVolumePercentForDevice(
tbarzic3ab01a22016-12-17 03:12:532843 kUSBJabraSpeakerOutput2->id));
jennyz96526d02016-06-22 17:03:432844 const int kDefaultVolume = 75;
2845 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2846
2847 // Disable the auto OutputNodeVolumeChanged signal.
2848 fake_cras_audio_client_->set_notify_volume_change_with_delay(true);
2849 test_observer_->reset_output_volume_changed_count();
2850
2851 // Adjust the volume of output devices continuously.
2852 cras_audio_handler_->SetOutputVolumePercent(20);
2853 cras_audio_handler_->SetOutputVolumePercent(30);
2854
2855 // Sends delayed OutputNodeVolumeChanged signals.
2856 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
tbarzic3ab01a22016-12-17 03:12:532857 kUSBJabraSpeakerOutput2->id, 20);
jennyz96526d02016-06-22 17:03:432858 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
tbarzic3ab01a22016-12-17 03:12:532859 kUSBJabraSpeakerOutput1->id, 20);
jennyz96526d02016-06-22 17:03:432860 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
tbarzic3ab01a22016-12-17 03:12:532861 kUSBJabraSpeakerOutput2->id, 30);
jennyz96526d02016-06-22 17:03:432862 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
tbarzic3ab01a22016-12-17 03:12:532863 kUSBJabraSpeakerOutput1->id, 30);
jennyz96526d02016-06-22 17:03:432864
2865 // Verify that both speakers are set to the designated volume level after
2866 // receiving all delayed signals.
2867 EXPECT_EQ(4, test_observer_->output_volume_changed_count());
2868 EXPECT_EQ(30, cras_audio_handler_->GetOutputVolumePercent());
2869 EXPECT_EQ(30, cras_audio_handler_->GetOutputVolumePercentForDevice(
tbarzic3ab01a22016-12-17 03:12:532870 kUSBJabraSpeakerOutput1->id));
jennyz96526d02016-06-22 17:03:432871 EXPECT_EQ(30, cras_audio_handler_->GetOutputVolumePercentForDevice(
tbarzic3ab01a22016-12-17 03:12:532872 kUSBJabraSpeakerOutput2->id));
jennyz96526d02016-06-22 17:03:432873}
2874
tbarzic3ab01a22016-12-17 03:12:532875TEST_P(CrasAudioHandlerTest, ChangeActiveNodesHotrodInitWithCameraInputActive) {
2876 AudioNodeList audio_nodes = GenerateAudioNodeList(
2877 {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerOutput2,
2878 kUSBJabraSpeakerInput1, kUSBJabraSpeakerInput2});
jennyz2cb9eb12014-11-05 19:21:052879 // Make the camera input to be plugged in later than jabra's input.
tbarzic3ab01a22016-12-17 03:12:532880 AudioNode usb_camera = GenerateAudioNode(kUSBCameraInput);
jennyz2cb9eb12014-11-05 19:21:052881 usb_camera.plugged_time = 10000000;
2882 audio_nodes.push_back(usb_camera);
2883 SetUpCrasAudioHandler(audio_nodes);
2884
2885 // Verify the audio devices size.
2886 AudioDeviceList audio_devices;
2887 cras_audio_handler_->GetAudioDevices(&audio_devices);
2888 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2889
2890 // Verify the 1st jabra speaker's output is selected as active output
2891 // node and camera's input is selected active input by CrasAudioHandler.
2892 EXPECT_EQ(2, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:532893 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
jennyz2cb9eb12014-11-05 19:21:052894 cras_audio_handler_->GetPrimaryActiveOutputNode());
tbarzic3ab01a22016-12-17 03:12:532895 EXPECT_EQ(kUSBCameraInput->id,
jennyz2cb9eb12014-11-05 19:21:052896 cras_audio_handler_->GetPrimaryActiveInputNode());
2897
2898 // Set both jabra speakers's input and output nodes to active, this simulates
2899 // the call sent by hotrod initialization process.
2900 test_observer_->reset_active_output_node_changed_count();
2901 test_observer_->reset_active_input_node_changed_count();
tbarziccae06e2a2017-01-17 23:14:472902 cras_audio_handler_->ChangeActiveNodes(
2903 {kUSBJabraSpeakerOutput1->id, kUSBJabraSpeakerOutput2->id,
2904 kUSBJabraSpeakerInput1->id, kUSBJabraSpeakerInput2->id});
2905
2906 // Verify both jabra speakers' input/output nodes are made active.
2907 // num_active_nodes = GetActiveDeviceCount();
2908 EXPECT_EQ(4, GetActiveDeviceCount());
2909 const AudioDevice* active_output_1 =
2910 GetDeviceFromId(kUSBJabraSpeakerOutput1->id);
2911 EXPECT_TRUE(active_output_1->active);
2912 const AudioDevice* active_output_2 =
2913 GetDeviceFromId(kUSBJabraSpeakerOutput2->id);
2914 EXPECT_TRUE(active_output_2->active);
2915 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
2916 cras_audio_handler_->GetPrimaryActiveOutputNode());
2917 const AudioDevice* active_input_1 =
2918 GetDeviceFromId(kUSBJabraSpeakerInput1->id);
2919 EXPECT_TRUE(active_input_1->active);
2920 const AudioDevice* active_input_2 =
2921 GetDeviceFromId(kUSBJabraSpeakerInput2->id);
2922 EXPECT_TRUE(active_input_2->active);
2923 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
2924 cras_audio_handler_->GetPrimaryActiveInputNode());
2925
2926 // Verify only 1 ActiveOutputNodeChanged notification has been sent out
2927 // by calling ChangeActiveNodes.
2928 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
2929 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
2930}
2931
2932TEST_P(CrasAudioHandlerTest, SetActiveNodesHotrodInitWithCameraInputActive) {
2933 AudioNodeList audio_nodes = GenerateAudioNodeList(
2934 {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerOutput2,
2935 kUSBJabraSpeakerInput1, kUSBJabraSpeakerInput2});
2936 // Make the camera input to be plugged in later than jabra's input.
2937 AudioNode usb_camera = GenerateAudioNode(kUSBCameraInput);
2938 usb_camera.plugged_time = 10000000;
2939 audio_nodes.push_back(usb_camera);
2940 SetUpCrasAudioHandler(audio_nodes);
2941
2942 // Verify the audio devices size.
2943 AudioDeviceList audio_devices;
2944 cras_audio_handler_->GetAudioDevices(&audio_devices);
2945 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2946
2947 // Verify the 1st jabra speaker's output is selected as active output
2948 // node and camera's input is selected active input by CrasAudioHandler.
2949 EXPECT_EQ(2, GetActiveDeviceCount());
2950 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
2951 cras_audio_handler_->GetPrimaryActiveOutputNode());
2952 EXPECT_EQ(kUSBCameraInput->id,
2953 cras_audio_handler_->GetPrimaryActiveInputNode());
2954
2955 // Set both jabra speakers's input and output nodes to active, this simulates
2956 // the call sent by hotrod initialization process.
2957 test_observer_->reset_active_output_node_changed_count();
2958 test_observer_->reset_active_input_node_changed_count();
2959
2960 cras_audio_handler_->SetActiveOutputNodes(
2961 {kUSBJabraSpeakerOutput1->id, kUSBJabraSpeakerOutput2->id});
2962
2963 cras_audio_handler_->SetActiveInputNodes(
2964 {kUSBJabraSpeakerInput1->id, kUSBJabraSpeakerInput2->id});
jennyz2cb9eb12014-11-05 19:21:052965
2966 // Verify both jabra speakers' input/output nodes are made active.
2967 // num_active_nodes = GetActiveDeviceCount();
2968 EXPECT_EQ(4, GetActiveDeviceCount());
2969 const AudioDevice* active_output_1 =
tbarzic3ab01a22016-12-17 03:12:532970 GetDeviceFromId(kUSBJabraSpeakerOutput1->id);
jennyz2cb9eb12014-11-05 19:21:052971 EXPECT_TRUE(active_output_1->active);
2972 const AudioDevice* active_output_2 =
tbarzic3ab01a22016-12-17 03:12:532973 GetDeviceFromId(kUSBJabraSpeakerOutput2->id);
jennyz2cb9eb12014-11-05 19:21:052974 EXPECT_TRUE(active_output_2->active);
tbarzic3ab01a22016-12-17 03:12:532975 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
jennyz2cb9eb12014-11-05 19:21:052976 cras_audio_handler_->GetPrimaryActiveOutputNode());
2977 const AudioDevice* active_input_1 =
tbarzic3ab01a22016-12-17 03:12:532978 GetDeviceFromId(kUSBJabraSpeakerInput1->id);
jennyz2cb9eb12014-11-05 19:21:052979 EXPECT_TRUE(active_input_1->active);
2980 const AudioDevice* active_input_2 =
tbarzic3ab01a22016-12-17 03:12:532981 GetDeviceFromId(kUSBJabraSpeakerInput2->id);
jennyz2cb9eb12014-11-05 19:21:052982 EXPECT_TRUE(active_input_2->active);
tbarzic3ab01a22016-12-17 03:12:532983 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
jennyz2cb9eb12014-11-05 19:21:052984 cras_audio_handler_->GetPrimaryActiveInputNode());
2985
2986 // Verify only 1 ActiveOutputNodeChanged notification has been sent out
2987 // by calling ChangeActiveNodes.
2988 EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
2989 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
2990}
2991
tbarzic3ab01a22016-12-17 03:12:532992TEST_P(CrasAudioHandlerTest, ChangeActiveNodesWithFewerActives) {
2993 AudioNodeList audio_nodes = GenerateAudioNodeList(
2994 {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerOutput2});
jennyz2cb9eb12014-11-05 19:21:052995 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 // Set all three nodes to be active.
tbarziccae06e2a2017-01-17 23:14:473003 cras_audio_handler_->ChangeActiveNodes({kHDMIOutput->id,
3004 kUSBJabraSpeakerOutput1->id,
3005 kUSBJabraSpeakerOutput2->id});
jennyz2cb9eb12014-11-05 19:21:053006
3007 // Verify all three nodes are active.
3008 EXPECT_EQ(3, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:533009 const AudioDevice* active_output_1 = GetDeviceFromId(kHDMIOutput->id);
jennyz2cb9eb12014-11-05 19:21:053010 EXPECT_TRUE(active_output_1->active);
3011 const AudioDevice* active_output_2 =
tbarzic3ab01a22016-12-17 03:12:533012 GetDeviceFromId(kUSBJabraSpeakerOutput1->id);
jennyz2cb9eb12014-11-05 19:21:053013 EXPECT_TRUE(active_output_2->active);
3014 const AudioDevice* active_output_3 =
tbarzic3ab01a22016-12-17 03:12:533015 GetDeviceFromId(kUSBJabraSpeakerOutput2->id);
jennyz2cb9eb12014-11-05 19:21:053016 EXPECT_TRUE(active_output_3->active);
3017
3018 // Now call ChangeActiveDevices with only 2 nodes.
tbarziccae06e2a2017-01-17 23:14:473019 cras_audio_handler_->ChangeActiveNodes(
3020 {kUSBJabraSpeakerOutput1->id, kUSBJabraSpeakerOutput2->id});
3021
3022 // Verify only 2 nodes are active.
3023 EXPECT_EQ(2, GetActiveDeviceCount());
3024 const AudioDevice* output_1 = GetDeviceFromId(kHDMIOutput->id);
3025 EXPECT_FALSE(output_1->active);
3026 const AudioDevice* output_2 = GetDeviceFromId(kUSBJabraSpeakerOutput1->id);
3027 EXPECT_TRUE(output_2->active);
3028 const AudioDevice* output_3 = GetDeviceFromId(kUSBJabraSpeakerOutput2->id);
3029 EXPECT_TRUE(output_3->active);
3030}
3031
3032TEST_P(CrasAudioHandlerTest, SetActiveNodesWithFewerActives) {
3033 AudioNodeList audio_nodes = GenerateAudioNodeList(
3034 {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerOutput2});
3035 SetUpCrasAudioHandler(audio_nodes);
3036
3037 // Verify the audio devices size.
3038 AudioDeviceList audio_devices;
3039 cras_audio_handler_->GetAudioDevices(&audio_devices);
3040 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3041
3042 // Set all three nodes to be active.
3043 cras_audio_handler_->SetActiveOutputNodes({kHDMIOutput->id,
3044 kUSBJabraSpeakerOutput1->id,
3045 kUSBJabraSpeakerOutput2->id});
3046
3047 // Verify all three nodes are active.
3048 EXPECT_EQ(3, GetActiveDeviceCount());
3049 const AudioDevice* active_output_1 = GetDeviceFromId(kHDMIOutput->id);
3050 EXPECT_TRUE(active_output_1->active);
3051 const AudioDevice* active_output_2 =
3052 GetDeviceFromId(kUSBJabraSpeakerOutput1->id);
3053 EXPECT_TRUE(active_output_2->active);
3054 const AudioDevice* active_output_3 =
3055 GetDeviceFromId(kUSBJabraSpeakerOutput2->id);
3056 EXPECT_TRUE(active_output_3->active);
3057
3058 // Now call SetActiveOutputNodes with only 2 nodes.
3059 cras_audio_handler_->SetActiveOutputNodes(
3060 {kUSBJabraSpeakerOutput1->id, kUSBJabraSpeakerOutput2->id});
jennyz2cb9eb12014-11-05 19:21:053061
3062 // Verify only 2 nodes are active.
3063 EXPECT_EQ(2, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:533064 const AudioDevice* output_1 = GetDeviceFromId(kHDMIOutput->id);
jennyz2cb9eb12014-11-05 19:21:053065 EXPECT_FALSE(output_1->active);
tbarzic3ab01a22016-12-17 03:12:533066 const AudioDevice* output_2 = GetDeviceFromId(kUSBJabraSpeakerOutput1->id);
jennyz2cb9eb12014-11-05 19:21:053067 EXPECT_TRUE(output_2->active);
tbarzic3ab01a22016-12-17 03:12:533068 const AudioDevice* output_3 = GetDeviceFromId(kUSBJabraSpeakerOutput2->id);
jennyz2cb9eb12014-11-05 19:21:053069 EXPECT_TRUE(output_3->active);
3070}
3071
tbarzic3ab01a22016-12-17 03:12:533072TEST_P(CrasAudioHandlerTest, HotrodInitWithSingleJabra) {
jennyz2cb9eb12014-11-05 19:21:053073 // Simulates the hotrod initializated with a single jabra device and
3074 // CrasAudioHandler selected jabra input/output as active devices.
tbarzic3ab01a22016-12-17 03:12:533075 AudioNodeList audio_nodes =
3076 GenerateAudioNodeList({kHDMIOutput, kUSBJabraSpeakerOutput1,
3077 kUSBJabraSpeakerInput1, kUSBCameraInput});
jennyz2cb9eb12014-11-05 19:21:053078 SetUpCrasAudioHandler(audio_nodes);
3079
3080 // Verify the audio devices size.
3081 AudioDeviceList audio_devices;
3082 cras_audio_handler_->GetAudioDevices(&audio_devices);
3083 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3084
3085 // Verify the jabra speaker's output and input are selected as active nodes
3086 // by CrasAudioHandler.
3087 EXPECT_EQ(2, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:533088 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
jennyz2cb9eb12014-11-05 19:21:053089 cras_audio_handler_->GetPrimaryActiveOutputNode());
tbarzic3ab01a22016-12-17 03:12:533090 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
jennyz2cb9eb12014-11-05 19:21:053091 cras_audio_handler_->GetPrimaryActiveInputNode());
3092}
3093
tbarzic3ab01a22016-12-17 03:12:533094TEST_P(CrasAudioHandlerTest,
jennyz2cb9eb12014-11-05 19:21:053095 ChangeActiveNodesHotrodInitWithSingleJabraCameraPlugInLater) {
tbarzic3ab01a22016-12-17 03:12:533096 AudioNodeList audio_nodes = GenerateAudioNodeList(
3097 {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerInput1});
3098 AudioNode usb_camera = GenerateAudioNode(kUSBCameraInput);
jennyz2cb9eb12014-11-05 19:21:053099 usb_camera.plugged_time = 10000000;
3100 audio_nodes.push_back(usb_camera);
3101 SetUpCrasAudioHandler(audio_nodes);
3102
3103 // Verify the audio devices size.
3104 AudioDeviceList audio_devices;
3105 cras_audio_handler_->GetAudioDevices(&audio_devices);
3106 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3107
3108 // Verify the jabra speaker's output is selected as active output, and
3109 // camera's input is selected as active input by CrasAudioHandler
3110 EXPECT_EQ(2, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:533111 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
jennyz2cb9eb12014-11-05 19:21:053112 cras_audio_handler_->GetPrimaryActiveOutputNode());
tbarzic3ab01a22016-12-17 03:12:533113 EXPECT_EQ(kUSBCameraInput->id,
jennyz2cb9eb12014-11-05 19:21:053114 cras_audio_handler_->GetPrimaryActiveInputNode());
3115
3116 // Simulate hotrod app call to set jabra input as active device with only
3117 // jabra input node in the active node list, which does not conform to the
3118 // new SetActiveDevices protocol, but just show we can still handle it if
3119 // this happens.
tbarziccae06e2a2017-01-17 23:14:473120 cras_audio_handler_->ChangeActiveNodes(
3121 {kUSBJabraSpeakerOutput1->id, kUSBJabraSpeakerInput1->id});
3122
3123 // Verify the jabra speaker's output is selected as active output, and
3124 // jabra's input is selected as active input.
3125 EXPECT_EQ(2, GetActiveDeviceCount());
3126 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
3127 cras_audio_handler_->GetPrimaryActiveOutputNode());
3128 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
3129 cras_audio_handler_->GetPrimaryActiveInputNode());
3130}
3131
3132TEST_P(CrasAudioHandlerTest,
3133 SetActiveNodesHotrodInitWithSingleJabraCameraPlugInLater) {
3134 AudioNodeList audio_nodes = GenerateAudioNodeList(
3135 {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerInput1});
3136 AudioNode usb_camera = GenerateAudioNode(kUSBCameraInput);
3137 usb_camera.plugged_time = 10000000;
3138 audio_nodes.push_back(usb_camera);
3139 SetUpCrasAudioHandler(audio_nodes);
3140
3141 // Verify the audio devices size.
3142 AudioDeviceList audio_devices;
3143 cras_audio_handler_->GetAudioDevices(&audio_devices);
3144 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3145
3146 // Verify the jabra speaker's output is selected as active output, and
3147 // camera's input is selected as active input by CrasAudioHandler
3148 EXPECT_EQ(2, GetActiveDeviceCount());
3149 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
3150 cras_audio_handler_->GetPrimaryActiveOutputNode());
3151 EXPECT_EQ(kUSBCameraInput->id,
3152 cras_audio_handler_->GetPrimaryActiveInputNode());
3153
3154 // Simulate hotrod app call to set jabra input as active device with only
3155 // jabra input node in the active node list, which does not conform to the
3156 // new SetActiveDevices protocol, but just show we can still handle it if
3157 // this happens.
3158 cras_audio_handler_->SetActiveOutputNodes({kUSBJabraSpeakerOutput1->id});
3159
3160 cras_audio_handler_->SetActiveInputNodes({kUSBJabraSpeakerInput1->id});
jennyz2cb9eb12014-11-05 19:21:053161
3162 // Verify the jabra speaker's output is selected as active output, and
3163 // jabra's input is selected as active input.
3164 EXPECT_EQ(2, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:533165 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
jennyz2cb9eb12014-11-05 19:21:053166 cras_audio_handler_->GetPrimaryActiveOutputNode());
tbarzic3ab01a22016-12-17 03:12:533167 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
jennyz2cb9eb12014-11-05 19:21:053168 cras_audio_handler_->GetPrimaryActiveInputNode());
3169}
3170
tbarzic3ab01a22016-12-17 03:12:533171TEST_P(CrasAudioHandlerTest, ChangeActiveNodesDeactivatePrimaryActiveNode) {
3172 AudioNodeList audio_nodes =
3173 GenerateAudioNodeList({kUSBJabraSpeakerInput1, kUSBJabraSpeakerInput2});
3174 AudioNode usb_camera = GenerateAudioNode(kUSBCameraInput);
tbarzic96ea32052016-11-03 00:46:163175 usb_camera.plugged_time = 10000000;
3176 audio_nodes.push_back(usb_camera);
3177 SetUpCrasAudioHandler(audio_nodes);
3178
3179 // Verify the audio devices size.
3180 AudioDeviceList audio_devices;
3181 cras_audio_handler_->GetAudioDevices(&audio_devices);
3182 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3183
3184 // Verify the camera's input is selected as active input by CrasAudioHandler
3185 EXPECT_EQ(1, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:533186 EXPECT_EQ(kUSBCameraInput->id,
tbarzic96ea32052016-11-03 00:46:163187 cras_audio_handler_->GetPrimaryActiveInputNode());
3188
3189 // Simulate hotrod app call to set jabra input as active device with only
3190 // jabra input node in the active node list, which does not conform to the
3191 // new SetActiveDevices protocol, but just show we can still handle it if
3192 // this happens.
tbarziccae06e2a2017-01-17 23:14:473193 cras_audio_handler_->ChangeActiveNodes(
3194 {kUSBJabraSpeakerInput1->id, kUSBCameraInput->id});
tbarzic96ea32052016-11-03 00:46:163195
tbarziccae06e2a2017-01-17 23:14:473196 // Verify active input devices are set as expected, with primary active input
3197 // staying the same.
tbarzic96ea32052016-11-03 00:46:163198 EXPECT_EQ(2, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:533199 EXPECT_EQ(kUSBCameraInput->id,
tbarzic96ea32052016-11-03 00:46:163200 cras_audio_handler_->GetPrimaryActiveInputNode());
3201
3202 const AudioDevice* additional_speaker =
tbarzic3ab01a22016-12-17 03:12:533203 cras_audio_handler_->GetDeviceFromId(kUSBJabraSpeakerInput1->id);
tbarzic96ea32052016-11-03 00:46:163204 ASSERT_TRUE(additional_speaker);
3205 EXPECT_TRUE(additional_speaker->active);
3206
tbarziccae06e2a2017-01-17 23:14:473207 // Update active device list so previously primary active device is not
3208 // active anymore.
3209 cras_audio_handler_->ChangeActiveNodes(
3210 {kUSBJabraSpeakerInput1->id, kUSBJabraSpeakerInput2->id});
tbarzic96ea32052016-11-03 00:46:163211
tbarziccae06e2a2017-01-17 23:14:473212 // Verify that list of active devices is correctly set, and that a new primary
3213 // active input is selected.
3214 EXPECT_EQ(2, GetActiveDeviceCount());
3215 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
3216 cras_audio_handler_->GetPrimaryActiveInputNode());
3217
3218 additional_speaker =
3219 cras_audio_handler_->GetDeviceFromId(kUSBJabraSpeakerInput2->id);
3220 ASSERT_TRUE(additional_speaker);
3221 EXPECT_TRUE(additional_speaker->active);
3222}
3223
3224TEST_P(CrasAudioHandlerTest, SetActiveNodesDeactivatePrimaryActiveNode) {
3225 AudioNodeList audio_nodes =
3226 GenerateAudioNodeList({kUSBJabraSpeakerInput1, kUSBJabraSpeakerInput2});
3227 AudioNode usb_camera = GenerateAudioNode(kUSBCameraInput);
3228 usb_camera.plugged_time = 10000000;
3229 audio_nodes.push_back(usb_camera);
3230 SetUpCrasAudioHandler(audio_nodes);
3231
3232 // Verify the audio devices size.
3233 AudioDeviceList audio_devices;
3234 cras_audio_handler_->GetAudioDevices(&audio_devices);
3235 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3236
3237 // Verify the camera's input is selected as active input by CrasAudioHandler.
3238 EXPECT_EQ(1, GetActiveDeviceCount());
3239 EXPECT_EQ(kUSBCameraInput->id,
3240 cras_audio_handler_->GetPrimaryActiveInputNode());
3241
3242 // Add another device to active input device list.
3243 cras_audio_handler_->SetActiveInputNodes(
3244 {kUSBJabraSpeakerInput1->id, kUSBCameraInput->id});
3245
3246 // Verify active input devices are set as expected, with primary active input
3247 // staying the same.
3248 EXPECT_EQ(2, GetActiveDeviceCount());
3249 EXPECT_EQ(kUSBCameraInput->id,
3250 cras_audio_handler_->GetPrimaryActiveInputNode());
3251
3252 const AudioDevice* additional_speaker =
3253 cras_audio_handler_->GetDeviceFromId(kUSBJabraSpeakerInput1->id);
3254 ASSERT_TRUE(additional_speaker);
3255 EXPECT_TRUE(additional_speaker->active);
3256
3257 // Update active device list so previously primary active device is not
3258 // active anymore.
3259 cras_audio_handler_->SetActiveInputNodes(
3260 {kUSBJabraSpeakerInput1->id, kUSBJabraSpeakerInput2->id});
3261
3262 // Verify that list of active devices is correctly set, and that a new primary
3263 // active input is selected.
tbarzic96ea32052016-11-03 00:46:163264 EXPECT_EQ(2, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:533265 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
tbarzic96ea32052016-11-03 00:46:163266 cras_audio_handler_->GetPrimaryActiveInputNode());
3267
3268 additional_speaker =
tbarzic3ab01a22016-12-17 03:12:533269 cras_audio_handler_->GetDeviceFromId(kUSBJabraSpeakerInput2->id);
tbarzic96ea32052016-11-03 00:46:163270 ASSERT_TRUE(additional_speaker);
3271 EXPECT_TRUE(additional_speaker->active);
3272}
3273
tbarzic3ab01a22016-12-17 03:12:533274TEST_P(CrasAudioHandlerTest,
jennyz2cb9eb12014-11-05 19:21:053275 ChangeActiveNodesHotrodInitWithSingleJabraCameraPlugInLaterOldCall) {
tbarzic3ab01a22016-12-17 03:12:533276 AudioNodeList audio_nodes = GenerateAudioNodeList(
3277 {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerInput1});
3278 AudioNode usb_camera = GenerateAudioNode(kUSBCameraInput);
jennyz2cb9eb12014-11-05 19:21:053279 usb_camera.plugged_time = 10000000;
3280 audio_nodes.push_back(usb_camera);
3281 SetUpCrasAudioHandler(audio_nodes);
3282
3283 // Verify the audio devices size.
3284 AudioDeviceList audio_devices;
3285 cras_audio_handler_->GetAudioDevices(&audio_devices);
3286 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3287
3288 // Verify the jabra speaker's output is selected as active output, and
3289 // camera's input is selected as active input by CrasAudioHandler
3290 EXPECT_EQ(2, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:533291 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
jennyz2cb9eb12014-11-05 19:21:053292 cras_audio_handler_->GetPrimaryActiveOutputNode());
tbarzic3ab01a22016-12-17 03:12:533293 EXPECT_EQ(kUSBCameraInput->id,
jennyz2cb9eb12014-11-05 19:21:053294 cras_audio_handler_->GetPrimaryActiveInputNode());
3295
3296 // Simulate hotrod app call to set jabra input as active device with only
3297 // jabra input node in the active node list, which does not conform to the
3298 // new SetActiveDevices protocol, but just show we can still handle it if
3299 // this happens.
tbarziccae06e2a2017-01-17 23:14:473300 cras_audio_handler_->ChangeActiveNodes({kUSBJabraSpeakerInput1->id});
3301
3302 // Verify the jabra speaker's output is selected as active output, and
3303 // jabra's input is selected as active input.
3304 EXPECT_EQ(2, GetActiveDeviceCount());
3305 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
3306 cras_audio_handler_->GetPrimaryActiveOutputNode());
3307 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
3308 cras_audio_handler_->GetPrimaryActiveInputNode());
3309}
3310
3311TEST_P(CrasAudioHandlerTest,
3312 SetActiveNodesHotrodInitWithSingleJabraCameraPlugInLaterOldCall) {
3313 AudioNodeList audio_nodes = GenerateAudioNodeList(
3314 {kHDMIOutput, kUSBJabraSpeakerOutput1, kUSBJabraSpeakerInput1});
3315 AudioNode usb_camera = GenerateAudioNode(kUSBCameraInput);
3316 usb_camera.plugged_time = 10000000;
3317 audio_nodes.push_back(usb_camera);
3318 SetUpCrasAudioHandler(audio_nodes);
3319
3320 // Verify the audio devices size.
3321 AudioDeviceList audio_devices;
3322 cras_audio_handler_->GetAudioDevices(&audio_devices);
3323 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3324
3325 // Verify the jabra speaker's output is selected as active output, and
3326 // camera's input is selected as active input by CrasAudioHandler
3327 EXPECT_EQ(2, GetActiveDeviceCount());
3328 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
3329 cras_audio_handler_->GetPrimaryActiveOutputNode());
3330 EXPECT_EQ(kUSBCameraInput->id,
3331 cras_audio_handler_->GetPrimaryActiveInputNode());
3332
3333 // Simulate hotrod app call to set jabra input as active device with only
3334 // jabra input node in the active node list, which does not conform to the
3335 // new SetActiveDevices protocol, but just show we can still handle it if
3336 // this happens.
3337 cras_audio_handler_->SetActiveInputNodes({kUSBJabraSpeakerInput1->id});
jennyz2cb9eb12014-11-05 19:21:053338
3339 // Verify the jabra speaker's output is selected as active output, and
3340 // jabra's input is selected as active input.
3341 EXPECT_EQ(2, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:533342 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
jennyz2cb9eb12014-11-05 19:21:053343 cras_audio_handler_->GetPrimaryActiveOutputNode());
tbarzic3ab01a22016-12-17 03:12:533344 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
jennyz2cb9eb12014-11-05 19:21:053345 cras_audio_handler_->GetPrimaryActiveInputNode());
3346}
3347
tbarzic3ab01a22016-12-17 03:12:533348TEST_P(CrasAudioHandlerTest,
jennyz2cb9eb12014-11-05 19:21:053349 ChangeActiveNodesHotrodInitWithSingleJabraChangeOutput) {
tbarzic3ab01a22016-12-17 03:12:533350 AudioNodeList audio_nodes =
3351 GenerateAudioNodeList({kHDMIOutput, kUSBJabraSpeakerOutput1,
3352 kUSBJabraSpeakerInput1, kUSBCameraInput});
jennyz2cb9eb12014-11-05 19:21:053353 SetUpCrasAudioHandler(audio_nodes);
3354
3355 // Verify the audio devices size.
3356 AudioDeviceList audio_devices;
3357 cras_audio_handler_->GetAudioDevices(&audio_devices);
3358 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3359
3360 // Verify the jabra speaker's output and input are selected as active output
3361 // by CrasAudioHandler.
3362 EXPECT_EQ(2, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:533363 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
jennyz2cb9eb12014-11-05 19:21:053364 cras_audio_handler_->GetPrimaryActiveOutputNode());
tbarzic3ab01a22016-12-17 03:12:533365 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
jennyz2cb9eb12014-11-05 19:21:053366 cras_audio_handler_->GetPrimaryActiveInputNode());
3367
3368 // Simulate hotrod app call SetActiveDevices to change active output
3369 // with only complete list of active nodes passed in, which is the new
3370 // way of hotrod app.
tbarziccae06e2a2017-01-17 23:14:473371 cras_audio_handler_->ChangeActiveNodes(
3372 {kHDMIOutput->id, kUSBJabraSpeakerInput1->id});
3373
3374 // Verify the jabra speaker's output is selected as active output, and
3375 // jabra's input is selected as active input.
3376 EXPECT_EQ(2, GetActiveDeviceCount());
3377 EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3378 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
3379 cras_audio_handler_->GetPrimaryActiveInputNode());
3380}
3381
3382TEST_P(CrasAudioHandlerTest,
3383 SetActiveNodesHotrodInitWithSingleJabraChangeOutput) {
3384 AudioNodeList audio_nodes =
3385 GenerateAudioNodeList({kHDMIOutput, kUSBJabraSpeakerOutput1,
3386 kUSBJabraSpeakerInput1, kUSBCameraInput});
3387 SetUpCrasAudioHandler(audio_nodes);
3388
3389 // Verify the audio devices size.
3390 AudioDeviceList audio_devices;
3391 cras_audio_handler_->GetAudioDevices(&audio_devices);
3392 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3393
3394 // Verify the jabra speaker's output and input are selected as active output
3395 // by CrasAudioHandler.
3396 EXPECT_EQ(2, GetActiveDeviceCount());
3397 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
3398 cras_audio_handler_->GetPrimaryActiveOutputNode());
3399 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
3400 cras_audio_handler_->GetPrimaryActiveInputNode());
3401
3402 // Simulate hotrod app calling SetActiveDeviceLists to change active input
3403 // and output with complete list of active nodes passed in.
3404 cras_audio_handler_->SetActiveOutputNodes({kHDMIOutput->id});
3405 cras_audio_handler_->SetActiveInputNodes({kUSBJabraSpeakerInput1->id});
jennyz2cb9eb12014-11-05 19:21:053406
3407 // Verify the jabra speaker's output is selected as active output, and
3408 // jabra's input is selected as active input.
3409 EXPECT_EQ(2, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:533410 EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3411 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
jennyz2cb9eb12014-11-05 19:21:053412 cras_audio_handler_->GetPrimaryActiveInputNode());
3413}
3414
tbarzic3ab01a22016-12-17 03:12:533415TEST_P(CrasAudioHandlerTest,
jennyz2cb9eb12014-11-05 19:21:053416 ChangeActiveNodesHotrodInitWithSingleJabraChangeOutputOldCall) {
tbarzic3ab01a22016-12-17 03:12:533417 AudioNodeList audio_nodes =
3418 GenerateAudioNodeList({kHDMIOutput, kUSBJabraSpeakerOutput1,
3419 kUSBJabraSpeakerInput1, kUSBCameraInput});
jennyz2cb9eb12014-11-05 19:21:053420 SetUpCrasAudioHandler(audio_nodes);
3421
3422 // Verify the audio devices size.
3423 AudioDeviceList audio_devices;
3424 cras_audio_handler_->GetAudioDevices(&audio_devices);
3425 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3426
3427 // Verify the jabra speaker's output and input are selected as active output
3428 // by CrasAudioHandler.
3429 EXPECT_EQ(2, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:533430 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
jennyz2cb9eb12014-11-05 19:21:053431 cras_audio_handler_->GetPrimaryActiveOutputNode());
tbarzic3ab01a22016-12-17 03:12:533432 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
jennyz2cb9eb12014-11-05 19:21:053433 cras_audio_handler_->GetPrimaryActiveInputNode());
3434
3435 // Simulate hotrod app call SetActiveDevices to change active output
3436 // with only a single active output nodes passed in, which is the old
3437 // way of hotrod app.
tbarziccae06e2a2017-01-17 23:14:473438 cras_audio_handler_->ChangeActiveNodes({kHDMIOutput->id});
jennyz2cb9eb12014-11-05 19:21:053439
3440 // Verify the jabra speaker's output is selected as active output, and
3441 // jabra's input is selected as active input.
3442 EXPECT_EQ(2, GetActiveDeviceCount());
tbarzic3ab01a22016-12-17 03:12:533443 EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3444 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
jennyz2cb9eb12014-11-05 19:21:053445 cras_audio_handler_->GetPrimaryActiveInputNode());
jennyzb47947f2014-09-25 00:42:043446}
3447
tbarziccae06e2a2017-01-17 23:14:473448TEST_P(CrasAudioHandlerTest, SetEmptyActiveOutputNodes) {
3449 AudioNodeList audio_nodes =
3450 GenerateAudioNodeList({kHDMIOutput, kUSBJabraSpeakerOutput1,
3451 kUSBJabraSpeakerInput1, kUSBCameraInput});
3452 SetUpCrasAudioHandler(audio_nodes);
3453
3454 // Verify the audio devices size.
3455 AudioDeviceList audio_devices;
3456 cras_audio_handler_->GetAudioDevices(&audio_devices);
3457 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3458
3459 // Verify the jabra speaker's output and input are selected as active output
3460 // by CrasAudioHandler.
3461 EXPECT_EQ(2, GetActiveDeviceCount());
3462 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
3463 cras_audio_handler_->GetPrimaryActiveOutputNode());
3464 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
3465 cras_audio_handler_->GetPrimaryActiveInputNode());
3466
3467 cras_audio_handler_->SetActiveOutputNodes(CrasAudioHandler::NodeIdList());
3468
3469 // Verify the jabra's input is selected as active input, and that there are
3470 // no active outputs.
3471 EXPECT_EQ(1, GetActiveDeviceCount());
3472 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
3473 cras_audio_handler_->GetPrimaryActiveInputNode());
3474 EXPECT_EQ(0u, cras_audio_handler_->GetPrimaryActiveOutputNode());
3475}
3476
3477TEST_P(CrasAudioHandlerTest, SetEmptyActiveInputNodes) {
3478 AudioNodeList audio_nodes =
3479 GenerateAudioNodeList({kHDMIOutput, kUSBJabraSpeakerOutput1,
3480 kUSBJabraSpeakerInput1, kUSBCameraInput});
3481 SetUpCrasAudioHandler(audio_nodes);
3482
3483 // Verify the audio devices size.
3484 AudioDeviceList audio_devices;
3485 cras_audio_handler_->GetAudioDevices(&audio_devices);
3486 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3487
3488 // Verify the jabra speaker's output and input are selected as active output
3489 // by CrasAudioHandler.
3490 EXPECT_EQ(2, GetActiveDeviceCount());
3491 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
3492 cras_audio_handler_->GetPrimaryActiveOutputNode());
3493 EXPECT_EQ(kUSBJabraSpeakerInput1->id,
3494 cras_audio_handler_->GetPrimaryActiveInputNode());
3495
3496 cras_audio_handler_->SetActiveInputNodes(CrasAudioHandler::NodeIdList());
3497
3498 // Verify the jabra speaker's output is selected as active output, and
3499 // there are no active inputs..
3500 EXPECT_EQ(1, GetActiveDeviceCount());
3501 EXPECT_EQ(kUSBJabraSpeakerOutput1->id,
3502 cras_audio_handler_->GetPrimaryActiveOutputNode());
3503 EXPECT_EQ(0u, cras_audio_handler_->GetPrimaryActiveInputNode());
3504}
3505
tbarzic3ab01a22016-12-17 03:12:533506TEST_P(CrasAudioHandlerTest, NoMoreAudioInputDevices) {
mukai307596b62014-11-24 19:56:593507 // Some device like chromebox does not have the internal input device. The
3508 // active devices should be reset when the user plugs a device and then
3509 // unplugs it to such device.
tbarzic3ab01a22016-12-17 03:12:533510 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker});
mukai307596b62014-11-24 19:56:593511 SetUpCrasAudioHandler(audio_nodes);
3512
3513 EXPECT_EQ(0ULL, cras_audio_handler_->GetPrimaryActiveInputNode());
3514
tbarzic3ab01a22016-12-17 03:12:533515 audio_nodes.push_back(GenerateAudioNode(kMicJack));
mukai307596b62014-11-24 19:56:593516 ChangeAudioNodes(audio_nodes);
3517
tbarzic3ab01a22016-12-17 03:12:533518 EXPECT_EQ(kMicJack->id, cras_audio_handler_->GetPrimaryActiveInputNode());
mukai307596b62014-11-24 19:56:593519 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
3520 test_observer_->reset_active_input_node_changed_count();
3521
3522 audio_nodes.pop_back();
3523 ChangeAudioNodes(audio_nodes);
3524 EXPECT_EQ(0ULL, cras_audio_handler_->GetPrimaryActiveInputNode());
3525 EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
3526}
3527
jennyz731ba262015-02-05 19:00:033528// Test the case in which an HDMI output is plugged in with other higher
3529// priority
3530// output devices already plugged and user has manually selected an active
3531// output.
3532// The hotplug of hdmi output should not change user's selection of active
3533// device.
3534// crbug.com/447826.
tbarzic3ab01a22016-12-17 03:12:533535TEST_P(CrasAudioHandlerTest, HotPlugHDMINotChangeActiveOutput) {
jennyz731ba262015-02-05 19:00:033536 AudioNodeList audio_nodes;
tbarzic3ab01a22016-12-17 03:12:533537 AudioNode internal_speaker = GenerateAudioNode(kInternalSpeaker);
jennyz731ba262015-02-05 19:00:033538 audio_nodes.push_back(internal_speaker);
tbarzic3ab01a22016-12-17 03:12:533539 AudioNode usb_headset = GenerateAudioNode(kUSBHeadphone1);
jennyz731ba262015-02-05 19:00:033540 usb_headset.plugged_time = 80000000;
3541 audio_nodes.push_back(usb_headset);
3542 SetUpCrasAudioHandler(audio_nodes);
3543
3544 // Verify the audio devices size.
3545 AudioDeviceList audio_devices;
3546 cras_audio_handler_->GetAudioDevices(&audio_devices);
3547 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3548
3549 // Verify the USB headset is selected as active output by default.
3550 EXPECT_EQ(usb_headset.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3551
3552 // Manually set the active output to internal speaker.
tbarzic3ab01a22016-12-17 03:12:533553 AudioDevice internal_output(GenerateAudioNode(kInternalSpeaker));
jennyzbd236742016-03-02 20:15:523554 cras_audio_handler_->SwitchToDevice(internal_output, true,
3555 CrasAudioHandler::ACTIVATE_BY_USER);
jennyz731ba262015-02-05 19:00:033556
3557 // Verify the active output is switched to internal speaker.
3558 EXPECT_EQ(internal_speaker.id,
3559 cras_audio_handler_->GetPrimaryActiveOutputNode());
tbarzic3ab01a22016-12-17 03:12:533560 EXPECT_LT(internal_speaker.plugged_time, usb_headset.plugged_time);
jennyz731ba262015-02-05 19:00:033561 const AudioDevice* usb_device = GetDeviceFromId(usb_headset.id);
3562 EXPECT_FALSE(usb_device->active);
3563
3564 // Plug in HDMI output.
3565 audio_nodes.clear();
3566 internal_speaker.active = true;
3567 audio_nodes.push_back(internal_speaker);
3568 usb_headset.active = false;
3569 audio_nodes.push_back(usb_headset);
tbarzic3ab01a22016-12-17 03:12:533570 AudioNode hdmi = GenerateAudioNode(kHDMIOutput);
jennyz731ba262015-02-05 19:00:033571 hdmi.plugged_time = 90000000;
3572 audio_nodes.push_back(hdmi);
3573 ChangeAudioNodes(audio_nodes);
3574
3575 // The active output should not change.
tbarzic3ab01a22016-12-17 03:12:533576 EXPECT_EQ(kInternalSpeaker->id,
jennyz731ba262015-02-05 19:00:033577 cras_audio_handler_->GetPrimaryActiveOutputNode());
3578}
3579
jennyz3147abc72015-04-29 00:13:033580// Test the case in which the active device was set to inactive from cras after
3581// resuming from suspension state. See crbug.com/478968.
tbarzic3ab01a22016-12-17 03:12:533582TEST_P(CrasAudioHandlerTest, ActiveNodeLostAfterResume) {
3583 AudioNodeList audio_nodes = GenerateAudioNodeList({kHeadphone, kHDMIOutput});
3584 for (const auto& node : audio_nodes)
3585 ASSERT_FALSE(node.active) << node.id << " expected to be inactive";
jennyz3147abc72015-04-29 00:13:033586 SetUpCrasAudioHandler(audio_nodes);
3587
3588 // Verify the headphone is selected as the active output.
3589 AudioDeviceList audio_devices;
3590 cras_audio_handler_->GetAudioDevices(&audio_devices);
3591 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
tbarzic3ab01a22016-12-17 03:12:533592 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3593 const AudioDevice* active_headphone = GetDeviceFromId(kHeadphone->id);
3594 EXPECT_EQ(kHeadphone->id, active_headphone->id);
jennyz3147abc72015-04-29 00:13:033595 EXPECT_TRUE(active_headphone->active);
3596
3597 // Simulate NodesChanged signal with headphone turning into inactive state,
3598 // and HDMI node removed.
3599 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:533600 audio_nodes.push_back(GenerateAudioNode(kHeadphone));
jennyz3147abc72015-04-29 00:13:033601 ChangeAudioNodes(audio_nodes);
3602
3603 // Verify the headphone is set to active again.
tbarzic3ab01a22016-12-17 03:12:533604 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3605 const AudioDevice* headphone_resumed = GetDeviceFromId(kHeadphone->id);
3606 EXPECT_EQ(kHeadphone->id, headphone_resumed->id);
jennyz3147abc72015-04-29 00:13:033607 EXPECT_TRUE(headphone_resumed->active);
3608}
3609
jennyz639ec6f2016-01-19 23:27:303610// In the mirror mode, when the device resumes after being suspended, the hmdi
3611// node will be lost first, then re-appear with a different node id, but with
3612// the same stable id. If it is set as the non-active node by user before
3613// suspend/resume, it should remain inactive after the device resumes even
3614// if it has a higher priority than the current active node.
3615// crbug.com/443014.
tbarzic3ab01a22016-12-17 03:12:533616TEST_P(CrasAudioHandlerTest, HDMIRemainInactiveAfterSuspendResume) {
jennyz639ec6f2016-01-19 23:27:303617 AudioNodeList audio_nodes;
tbarzic3ab01a22016-12-17 03:12:533618 AudioNode internal_speaker = GenerateAudioNode(kInternalSpeaker);
jennyz639ec6f2016-01-19 23:27:303619 audio_nodes.push_back(internal_speaker);
tbarzic3ab01a22016-12-17 03:12:533620 AudioNode hdmi_output = GenerateAudioNode(kHDMIOutput);
jennyz639ec6f2016-01-19 23:27:303621 audio_nodes.push_back(hdmi_output);
3622 SetUpCrasAudioHandler(audio_nodes);
3623
3624 // Verify the hdmi is selected as the active output since it has a higher
3625 // priority.
3626 AudioDeviceList audio_devices;
3627 cras_audio_handler_->GetAudioDevices(&audio_devices);
3628 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3629 EXPECT_EQ(hdmi_output.id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3630
3631 // Manually set the active output to internal speaker.
jennyzbd236742016-03-02 20:15:523632 cras_audio_handler_->SwitchToDevice(AudioDevice(internal_speaker), true,
3633 CrasAudioHandler::ACTIVATE_BY_USER);
jennyz639ec6f2016-01-19 23:27:303634 EXPECT_EQ(internal_speaker.id,
3635 cras_audio_handler_->GetPrimaryActiveOutputNode());
3636
3637 // Simulate the suspend and resume of the device during mirror mode. The HDMI
3638 // node will be lost first.
3639 audio_nodes.clear();
3640 internal_speaker.active = true;
3641 audio_nodes.push_back(internal_speaker);
3642 ChangeAudioNodes(audio_nodes);
3643
3644 // Verify the HDMI node is lost, and internal speaker is still the active
3645 // node.
3646 cras_audio_handler_->GetAudioDevices(&audio_devices);
3647 EXPECT_EQ(1u, audio_devices.size());
3648 EXPECT_EQ(internal_speaker.id,
3649 cras_audio_handler_->GetPrimaryActiveOutputNode());
3650
3651 // Simulate the re-appearing of the hdmi node, which comes with a new id,
3652 // but the same stable device id.
3653 AudioNode hdmi_output_2(hdmi_output);
3654 hdmi_output_2.id = 20006;
3655 hdmi_output_2.plugged_time = internal_speaker.plugged_time + 100;
3656 audio_nodes.push_back(hdmi_output_2);
tbarzic3ab01a22016-12-17 03:12:533657 ASSERT_NE(hdmi_output.id, hdmi_output_2.id);
3658 ASSERT_EQ(hdmi_output.StableDeviceId(), hdmi_output_2.StableDeviceId());
jennyz639ec6f2016-01-19 23:27:303659 ChangeAudioNodes(audio_nodes);
3660
3661 // Verify the hdmi node is not set the active, and the current active node
3662 // , the internal speaker, remains active.
3663 cras_audio_handler_->GetAudioDevices(&audio_devices);
3664 EXPECT_EQ(2u, audio_devices.size());
3665 EXPECT_EQ(internal_speaker.id,
3666 cras_audio_handler_->GetPrimaryActiveOutputNode());
3667}
3668
jennyz3147abc72015-04-29 00:13:033669// Test the case in which there are two NodesChanged signal for discovering
3670// output devices, and there is race between NodesChange and SetActiveOutput
3671// during this process. See crbug.com/478968.
tbarzic3ab01a22016-12-17 03:12:533672TEST_P(CrasAudioHandlerTest, ActiveNodeLostDuringLoginSession) {
3673 AudioNodeList audio_nodes = GenerateAudioNodeList({kHeadphone});
3674 for (const auto& node : audio_nodes)
3675 ASSERT_FALSE(node.active) << node.id << " expected to be inactive";
jennyz3147abc72015-04-29 00:13:033676 SetUpCrasAudioHandler(audio_nodes);
3677
3678 // Verify the headphone is selected as the active output.
3679 AudioDeviceList audio_devices;
3680 cras_audio_handler_->GetAudioDevices(&audio_devices);
3681 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
tbarzic3ab01a22016-12-17 03:12:533682 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3683 const AudioDevice* active_headphone = GetDeviceFromId(kHeadphone->id);
3684 EXPECT_EQ(kHeadphone->id, active_headphone->id);
jennyz3147abc72015-04-29 00:13:033685 EXPECT_TRUE(active_headphone->active);
3686
3687 // Simulate NodesChanged signal with headphone turning into inactive state,
3688 // and add a new HDMI output node.
3689 audio_nodes.clear();
tbarzic3ab01a22016-12-17 03:12:533690 audio_nodes.push_back(GenerateAudioNode(kHeadphone));
3691 audio_nodes.push_back(GenerateAudioNode(kHDMIOutput));
jennyz3147abc72015-04-29 00:13:033692 ChangeAudioNodes(audio_nodes);
3693
3694 // Verify the headphone is set to active again.
tbarzic3ab01a22016-12-17 03:12:533695 EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
3696 const AudioDevice* headphone_resumed = GetDeviceFromId(kHeadphone->id);
3697 EXPECT_EQ(kHeadphone->id, headphone_resumed->id);
jennyz3147abc72015-04-29 00:13:033698 EXPECT_TRUE(headphone_resumed->active);
3699}
3700
jennyzc770dc772015-06-29 23:46:063701// This test HDMI output rediscovering case in crbug.com/503667.
tbarzic3ab01a22016-12-17 03:12:533702TEST_P(CrasAudioHandlerTest, HDMIOutputRediscover) {
3703 AudioNodeList audio_nodes =
3704 GenerateAudioNodeList({kInternalSpeaker, kHDMIOutput});
jennyzc770dc772015-06-29 23:46:063705 SetUpCrasAudioHandler(audio_nodes);
3706
3707 // Verify the HDMI device has been selected as the active output, and audio
3708 // output is not muted.
3709 AudioDevice active_output;
3710 EXPECT_TRUE(
3711 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:533712 EXPECT_EQ(kHDMIOutput->id, active_output.id);
3713 EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
jennyzc770dc772015-06-29 23:46:063714 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
3715 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
3716
3717 // Trigger HDMI rediscovering grace period, and remove the HDMI node.
3718 const int grace_period_in_ms = 200;
3719 SetHDMIRediscoverGracePeriodDuration(grace_period_in_ms);
3720 SetActiveHDMIRediscover();
tbarzic3ab01a22016-12-17 03:12:533721 AudioNodeList audio_nodes_lost_hdmi =
3722 GenerateAudioNodeList({kInternalSpeaker});
jennyzc770dc772015-06-29 23:46:063723 ChangeAudioNodes(audio_nodes_lost_hdmi);
3724
3725 // Verify the active output is switched to internal speaker, it is not muted
3726 // by preference, but the system output is muted during the grace period.
3727 EXPECT_TRUE(
3728 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:533729 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
jennyzc770dc772015-06-29 23:46:063730 EXPECT_FALSE(
tbarzic3ab01a22016-12-17 03:12:533731 cras_audio_handler_->IsOutputMutedForDevice(kInternalSpeaker->id));
jennyzc770dc772015-06-29 23:46:063732 EXPECT_TRUE(cras_audio_handler_->IsOutputMuted());
3733
3734 // Re-attach the HDMI device after a little delay.
3735 HDMIRediscoverWaiter waiter(this, grace_period_in_ms);
3736 waiter.WaitUntilTimeOut(grace_period_in_ms / 4);
3737 ChangeAudioNodes(audio_nodes);
3738
3739 // After HDMI re-discover grace period, verify HDMI output is selected as the
3740 // active device and not muted.
3741 waiter.WaitUntilHDMIRediscoverGracePeriodEnd();
3742 EXPECT_TRUE(
3743 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:533744 EXPECT_EQ(kHDMIOutput->id, active_output.id);
3745 EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
jennyzc770dc772015-06-29 23:46:063746 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
3747}
3748
jennyzc79ccda2015-07-28 23:04:353749// This tests the case of output unmuting event is notified after the hdmi
3750// output re-discover grace period ends, see crbug.com/512601.
tbarzic3ab01a22016-12-17 03:12:533751TEST_P(CrasAudioHandlerTest, HDMIOutputUnplugDuringSuspension) {
3752 AudioNodeList audio_nodes =
3753 GenerateAudioNodeList({kInternalSpeaker, kHDMIOutput});
jennyzc79ccda2015-07-28 23:04:353754 SetUpCrasAudioHandler(audio_nodes);
3755
3756 // Verify the HDMI device has been selected as the active output, and audio
3757 // output is not muted.
3758 AudioDevice active_output;
3759 EXPECT_TRUE(
3760 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:533761 EXPECT_EQ(kHDMIOutput->id, active_output.id);
3762 EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
jennyzc79ccda2015-07-28 23:04:353763 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
3764 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
3765
3766 // Trigger HDMI rediscovering grace period, and remove the HDMI node.
3767 const int grace_period_in_ms = 200;
3768 SetHDMIRediscoverGracePeriodDuration(grace_period_in_ms);
3769 SetActiveHDMIRediscover();
tbarzic3ab01a22016-12-17 03:12:533770 AudioNodeList audio_nodes_lost_hdmi =
3771 GenerateAudioNodeList({kInternalSpeaker});
jennyzc79ccda2015-07-28 23:04:353772 ChangeAudioNodes(audio_nodes_lost_hdmi);
3773
3774 // Verify the active output is switched to internal speaker, it is not muted
3775 // by preference, but the system output is muted during the grace period.
3776 EXPECT_TRUE(
3777 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:533778 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
jennyzc79ccda2015-07-28 23:04:353779 EXPECT_FALSE(
tbarzic3ab01a22016-12-17 03:12:533780 cras_audio_handler_->IsOutputMutedForDevice(kInternalSpeaker->id));
jennyzc79ccda2015-07-28 23:04:353781 EXPECT_TRUE(cras_audio_handler_->IsOutputMuted());
3782
3783 // After HDMI re-discover grace period, verify internal speaker is still the
3784 // active output and not muted, and unmute event by system is notified.
3785 test_observer_->reset_output_mute_changed_count();
3786 HDMIRediscoverWaiter waiter(this, grace_period_in_ms);
3787 waiter.WaitUntilHDMIRediscoverGracePeriodEnd();
3788 EXPECT_TRUE(
3789 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
tbarzic3ab01a22016-12-17 03:12:533790 EXPECT_EQ(kInternalSpeaker->id, active_output.id);
3791 EXPECT_EQ(kInternalSpeaker->id,
jennyzc79ccda2015-07-28 23:04:353792 cras_audio_handler_->GetPrimaryActiveOutputNode());
3793 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
3794 EXPECT_EQ(1, test_observer_->output_mute_changed_count());
3795 EXPECT_TRUE(test_observer_->output_mute_by_system());
3796}
3797
jennyzfbaa3e62017-03-06 20:14:023798TEST_P(CrasAudioHandlerTest, FrontCameraStartStop) {
3799 AudioNodeList audio_nodes = GenerateAudioNodeList({kFrontMic, kRearMic});
3800 SetUpCrasAudioHandler(audio_nodes);
3801
3802 // Verify the audio devices size.
3803 AudioDeviceList audio_devices;
3804 cras_audio_handler_->GetAudioDevices(&audio_devices);
3805 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3806
3807 // Verify the front mic has been selected as the active input.
3808 EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3809 EXPECT_TRUE(cras_audio_handler_->HasDualInternalMic());
3810
3811 // Start the front facing camera.
3812 StartFrontFacingCamera();
3813 // Verify the front mic has been selected as the active input.
3814 EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3815
3816 // Stop the front facing camera.
3817 StopFrontFacingCamera();
3818 // Verify the front mic has been selected as the active input.
3819 EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3820}
3821
3822TEST_P(CrasAudioHandlerTest, RearCameraStartStop) {
3823 AudioNodeList audio_nodes = GenerateAudioNodeList({kFrontMic, kRearMic});
3824 SetUpCrasAudioHandler(audio_nodes);
3825
3826 // Verify the audio devices size.
3827 AudioDeviceList audio_devices;
3828 cras_audio_handler_->GetAudioDevices(&audio_devices);
3829 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3830
3831 // Verify the front mic has been selected as the active input.
3832 EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3833 EXPECT_TRUE(cras_audio_handler_->HasDualInternalMic());
3834
3835 // Start the rear facing camera.
3836 StartRearFacingCamera();
3837 // Verify the active input is switched to the rear mic.
3838 EXPECT_EQ(kRearMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3839
3840 // Stop the rear facing camera.
3841 StopRearFacingCamera();
3842 // Verify the active input is switched back to front mic.
3843 EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3844}
3845
3846TEST_P(CrasAudioHandlerTest, SwitchFrontRearCamera) {
3847 AudioNodeList audio_nodes = GenerateAudioNodeList({kFrontMic, kRearMic});
3848 SetUpCrasAudioHandler(audio_nodes);
3849
3850 // Verify the audio devices size.
3851 AudioDeviceList audio_devices;
3852 cras_audio_handler_->GetAudioDevices(&audio_devices);
3853 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3854
3855 // Verify the front mic has been selected as the active input.
3856 EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3857 EXPECT_TRUE(cras_audio_handler_->HasDualInternalMic());
3858
3859 // Start the front facing camera.
3860 StartFrontFacingCamera();
3861 // Verify the front mic has been selected as the active input.
3862 EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3863
3864 // Simulates the camera app switching from front camera to rear camera.
3865 StopFrontFacingCamera();
3866 StartRearFacingCamera();
3867
3868 // Verify the rear mic has been selected as the active input.
3869 EXPECT_EQ(kRearMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3870}
3871
3872TEST_P(CrasAudioHandlerTest, StartFrontCameraWithActiveExternalInput) {
3873 AudioNodeList audio_nodes =
3874 GenerateAudioNodeList({kFrontMic, kRearMic, kMicJack});
3875 SetUpCrasAudioHandler(audio_nodes);
3876
3877 // Verify the audio devices size.
3878 AudioDeviceList audio_devices;
3879 cras_audio_handler_->GetAudioDevices(&audio_devices);
3880 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3881
3882 // Verify the mic Jack has been selected as the active input.
3883 EXPECT_EQ(kMicJackId, cras_audio_handler_->GetPrimaryActiveInputNode());
3884 EXPECT_TRUE(cras_audio_handler_->HasDualInternalMic());
3885
3886 // Start the front facing camera.
3887 StartFrontFacingCamera();
3888 // Verify the mic Jack has been selected as the active input.
3889 EXPECT_EQ(kMicJackId, cras_audio_handler_->GetPrimaryActiveInputNode());
3890
3891 // Stop the front facing camera.
3892 StopFrontFacingCamera();
3893 // Verify the mic Jack remains as the active input.
3894 EXPECT_EQ(kMicJackId, cras_audio_handler_->GetPrimaryActiveInputNode());
3895}
3896
3897TEST_P(CrasAudioHandlerTest, StartFrontCameraWithInactiveExternalInput) {
3898 AudioNodeList audio_nodes =
3899 GenerateAudioNodeList({kFrontMic, kRearMic, kMicJack});
3900 SetUpCrasAudioHandler(audio_nodes);
3901
3902 // Verify the audio devices size.
3903 AudioDeviceList audio_devices;
3904 cras_audio_handler_->GetAudioDevices(&audio_devices);
3905 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3906
3907 // Verify the mic Jack has been selected as the active input.
3908 EXPECT_EQ(kMicJackId, cras_audio_handler_->GetPrimaryActiveInputNode());
3909 EXPECT_TRUE(cras_audio_handler_->HasDualInternalMic());
3910
3911 // Change the active input to internal mic.
3912 cras_audio_handler_->SwitchToFrontOrRearMic();
3913 // Verify the active input has been switched to front mic.
3914 EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3915
3916 // Start the front facing camera.
3917 StartFrontFacingCamera();
3918 // Verify the front mic has been selected as the active input.
3919 EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3920
3921 // Stop the front facing camera.
3922 StopFrontFacingCamera();
3923 // Verify the active input remains as front mic.
3924 EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3925}
3926
3927TEST_P(CrasAudioHandlerTest, StartFrontCameraWithoutDualMic) {
3928 AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalMic});
3929 SetUpCrasAudioHandler(audio_nodes);
3930
3931 // Verify the audio devices size.
3932 AudioDeviceList audio_devices;
3933 cras_audio_handler_->GetAudioDevices(&audio_devices);
3934 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3935
3936 // Verify the mic Jack has been selected as the active input.
3937 EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3938 EXPECT_FALSE(cras_audio_handler_->HasDualInternalMic());
3939
3940 // Start the front facing camera.
3941 StartFrontFacingCamera();
3942 // Verify the internal mic has been selected as the active input.
3943 EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3944
3945 // Stop the front facing camera.
3946 StopFrontFacingCamera();
3947 // Verify the active input remains as interanl mic.
3948 EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3949}
3950
3951TEST_P(CrasAudioHandlerTest, FrontRearCameraBothOn) {
3952 AudioNodeList audio_nodes = GenerateAudioNodeList({kFrontMic, kRearMic});
3953 SetUpCrasAudioHandler(audio_nodes);
3954
3955 // Verify the audio devices size.
3956 AudioDeviceList audio_devices;
3957 cras_audio_handler_->GetAudioDevices(&audio_devices);
3958 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
3959
3960 // Verify the front mic has been selected as the active input.
3961 EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3962 EXPECT_TRUE(cras_audio_handler_->HasDualInternalMic());
3963
3964 // Start the rear facing camera.
3965 StartRearFacingCamera();
3966 // Verify the rear mic has been selected as the active input.
3967 EXPECT_EQ(kRearMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3968
3969 // Start front camera without stopping the front camera.
3970 StartFrontFacingCamera();
3971 // Verify the active microphone does not change.
3972 EXPECT_EQ(kRearMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3973
3974 // Stop the front mic.
3975 StopFrontFacingCamera();
3976 // Verity the active mic does not change when there is still camera on.
3977 EXPECT_EQ(kRearMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3978
3979 // Stop the rear mic.
3980 StopRearFacingCamera();
3981 // Verify the actice mic changes to front mic after both cameras stop.
3982 EXPECT_EQ(kFrontMicId, cras_audio_handler_->GetPrimaryActiveInputNode());
3983}
3984
[email protected]ee38bc42013-07-29 21:41:323985} // namespace chromeos