forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcras_audio_handler_unittest.cc
4195 lines (3569 loc) · 173 KB
/
cras_audio_handler_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/audio/cras_audio_handler.h"
#include <stddef.h>
#include <stdint.h>
#include <memory>
#include <vector>
#include "base/bind.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "base/system_monitor/system_monitor.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/values.h"
#include "chromeos/audio/audio_devices_pref_handler.h"
#include "chromeos/audio/audio_devices_pref_handler_stub.h"
#include "chromeos/dbus/audio_node.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/fake_cras_audio_client.h"
#include "media/base/video_facing.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace {
const uint64_t kInternalSpeakerId = 10001;
const uint64_t kHeadphoneId = 10002;
const uint64_t kInternalMicId = 10003;
const uint64_t kUSBMicId1 = 10004;
const uint64_t kUSBMicId2 = 10005;
const uint64_t kBluetoothHeadsetId = 10006;
const uint64_t kHDMIOutputId = 10007;
const uint64_t kUSBHeadphoneId1 = 10008;
const uint64_t kUSBHeadphoneId2 = 10009;
const uint64_t kMicJackId = 10010;
const uint64_t kKeyboardMicId = 10011;
const uint64_t kFrontMicId = 10012;
const uint64_t kRearMicId = 10013;
const uint64_t kOtherTypeOutputId = 90001;
const uint64_t kOtherTypeInputId = 90002;
const uint64_t kUSBJabraSpeakerOutputId1 = 90003;
const uint64_t kUSBJabraSpeakerOutputId2 = 90004;
const uint64_t kUSBJabraSpeakerInputId1 = 90005;
const uint64_t kUSBJabraSpeakerInputId2 = 90006;
const uint64_t kUSBCameraInputId = 90007;
struct AudioNodeInfo {
bool is_input;
uint64_t id;
const char* const device_name;
const char* const type;
const char* const name;
};
const AudioNodeInfo kInternalSpeaker[] = {
{false, kInternalSpeakerId, "Fake Speaker", "INTERNAL_SPEAKER", "Speaker"}};
const AudioNodeInfo kHeadphone[] = {
{false, kHeadphoneId, "Fake Headphone", "HEADPHONE", "Headphone"}};
const AudioNodeInfo kInternalMic[] = {
{true, kInternalMicId, "Fake Mic", "INTERNAL_MIC", "Internal Mic"}};
const AudioNodeInfo kMicJack[] = {
{true, kMicJackId, "Fake Mic Jack", "MIC", "Mic Jack"}};
const AudioNodeInfo kUSBMic1[] = {
{true, kUSBMicId1, "Fake USB Mic 1", "USB", "USB Microphone 1"}};
const AudioNodeInfo kUSBMic2[] = {
{true, kUSBMicId2, "Fake USB Mic 2", "USB", "USB Microphone 2"}};
const AudioNodeInfo kKeyboardMic[] = {{true, kKeyboardMicId,
"Fake Keyboard Mic", "KEYBOARD_MIC",
"Keyboard Mic"}};
const AudioNodeInfo kFrontMic[] = {
{true, kFrontMicId, "Fake Front Mic", "FRONT_MIC", "Front Mic"}};
const AudioNodeInfo kRearMic[] = {
{true, kRearMicId, "Fake Rear Mic", "REAR_MIC", "Rear Mic"}};
const AudioNodeInfo kOtherTypeOutput[] = {{false, kOtherTypeOutputId,
"Output Device", "SOME_OTHER_TYPE",
"Other Type Output Device"}};
const AudioNodeInfo kOtherTypeInput[] = {{true, kOtherTypeInputId,
"Input Device", "SOME_OTHER_TYPE",
"Other Type Input Device"}};
const AudioNodeInfo kBluetoothHeadset[] = {{false, kBluetoothHeadsetId,
"Bluetooth Headset", "BLUETOOTH",
"Bluetooth Headset 1"}};
const AudioNodeInfo kHDMIOutput[] = {
{false, kHDMIOutputId, "HDMI output", "HDMI", "HDMI output"}};
const AudioNodeInfo kUSBHeadphone1[] = {
{false, kUSBHeadphoneId1, "USB Headphone", "USB", "USB Headphone 1"}};
const AudioNodeInfo kUSBHeadphone2[] = {
{false, kUSBHeadphoneId2, "USB Headphone", "USB", "USB Headphone 1"}};
const AudioNodeInfo kUSBJabraSpeakerOutput1[] = {
{false, kUSBJabraSpeakerOutputId1, "Jabra Speaker 1", "USB",
"Jabra Speaker 1"}};
const AudioNodeInfo kUSBJabraSpeakerOutput2[] = {
{false, kUSBJabraSpeakerOutputId2, "Jabra Speaker 2", "USB",
"Jabra Speaker 2"}};
const AudioNodeInfo kUSBJabraSpeakerInput1[] = {{true, kUSBJabraSpeakerInputId1,
"Jabra Speaker 1", "USB",
"Jabra Speaker"}};
const AudioNodeInfo kUSBJabraSpeakerInput2[] = {{true, kUSBJabraSpeakerInputId2,
"Jabra Speaker 2", "USB",
"Jabra Speaker 2"}};
const AudioNodeInfo kUSBCameraInput[] = {
{true, kUSBCameraInputId, "USB Camera", "USB", "USB Camera"}};
class TestObserver : public chromeos::CrasAudioHandler::AudioObserver {
public:
TestObserver() = default;
int active_output_node_changed_count() const {
return active_output_node_changed_count_;
}
void reset_active_output_node_changed_count() {
active_output_node_changed_count_ = 0;
}
int active_input_node_changed_count() const {
return active_input_node_changed_count_;
}
void reset_active_input_node_changed_count() {
active_input_node_changed_count_ = 0;
}
int audio_nodes_changed_count() const {
return audio_nodes_changed_count_;
}
int output_mute_changed_count() const {
return output_mute_changed_count_;
}
void reset_output_mute_changed_count() { input_mute_changed_count_ = 0; }
int input_mute_changed_count() const {
return input_mute_changed_count_;
}
int output_volume_changed_count() const {
return output_volume_changed_count_;
}
void reset_output_volume_changed_count() { output_volume_changed_count_ = 0; }
int input_gain_changed_count() const {
return input_gain_changed_count_;
}
bool output_mute_by_system() const { return output_mute_by_system_; }
int output_channel_remixing_changed_count() const {
return output_channel_remixing_changed_count_;
}
~TestObserver() override = default;
protected:
// chromeos::CrasAudioHandler::AudioObserver overrides.
void OnActiveOutputNodeChanged() override {
++active_output_node_changed_count_;
}
void OnActiveInputNodeChanged() override {
++active_input_node_changed_count_;
}
void OnAudioNodesChanged() override { ++audio_nodes_changed_count_; }
void OnOutputMuteChanged(bool /* mute_on */, bool system_adjust) override {
++output_mute_changed_count_;
output_mute_by_system_ = system_adjust;
}
void OnInputMuteChanged(bool /* mute_on */) override {
++input_mute_changed_count_;
}
void OnOutputNodeVolumeChanged(uint64_t /* node_id */,
int /* volume */) override {
++output_volume_changed_count_;
}
void OnInputNodeGainChanged(uint64_t /* node_id */, int /* gain */) override {
++input_gain_changed_count_;
}
void OnOutputChannelRemixingChanged(bool /* mono_on */) override {
++output_channel_remixing_changed_count_;
}
private:
int active_output_node_changed_count_ = 0;
int active_input_node_changed_count_ = 0;
int audio_nodes_changed_count_ = 0;
int output_mute_changed_count_ = 0;
int input_mute_changed_count_ = 0;
int output_volume_changed_count_ = 0;
int input_gain_changed_count_ = 0;
bool output_mute_by_system_ = false; // output mute state adjusted by system.
int output_channel_remixing_changed_count_ = 0;
DISALLOW_COPY_AND_ASSIGN(TestObserver);
};
class SystemMonitorObserver
: public base::SystemMonitor::DevicesChangedObserver {
public:
SystemMonitorObserver() : device_changes_received_(0) {}
~SystemMonitorObserver() override = default;
void OnDevicesChanged(base::SystemMonitor::DeviceType device_type) override {
if (device_type == base::SystemMonitor::DeviceType::DEVTYPE_AUDIO)
device_changes_received_++;
}
int device_changes_received() const { return device_changes_received_; }
void reset_count() { device_changes_received_ = 0; }
private:
int device_changes_received_;
};
class FakeVideoCaptureManager {
public:
FakeVideoCaptureManager() = default;
virtual ~FakeVideoCaptureManager() = default;
void AddObserver(media::VideoCaptureObserver* observer) {
observers_.AddObserver(observer);
}
void RemoveAllObservers() { observers_.Clear(); }
void NotifyVideoCaptureStarted(media::VideoFacingMode facing) {
for (auto& observer : observers_)
observer.OnVideoCaptureStarted(facing);
}
void NotifyVideoCaptureStopped(media::VideoFacingMode facing) {
for (auto& observer : observers_)
observer.OnVideoCaptureStopped(facing);
}
private:
base::ObserverList<media::VideoCaptureObserver> observers_;
DISALLOW_COPY_AND_ASSIGN(FakeVideoCaptureManager);
};
} // namespace
// Test param is the version of stabel device id used by audio node.
class CrasAudioHandlerTest : public testing::TestWithParam<int> {
public:
CrasAudioHandlerTest()
: scoped_task_environment_(
base::test::ScopedTaskEnvironment::MainThreadType::UI) {}
~CrasAudioHandlerTest() override = default;
void SetUp() override {
system_monitor_.AddDevicesChangedObserver(&system_monitor_observer_);
video_capture_manager_.reset(new FakeVideoCaptureManager);
}
void TearDown() override {
system_monitor_.RemoveDevicesChangedObserver(&system_monitor_observer_);
cras_audio_handler_->RemoveAudioObserver(test_observer_.get());
test_observer_.reset();
video_capture_manager_->RemoveAllObservers();
video_capture_manager_.reset();
CrasAudioHandler::Shutdown();
audio_pref_handler_ = nullptr;
DBusThreadManager::Shutdown();
}
AudioNode GenerateAudioNode(const AudioNodeInfo* node_info) {
uint64_t stable_device_id_v2 = GetParam() == 1 ? 0 : (node_info->id ^ 0xFF);
uint64_t stable_device_id_v1 = node_info->id;
return AudioNode(node_info->is_input, node_info->id, GetParam() == 2,
stable_device_id_v1, stable_device_id_v2,
node_info->device_name, node_info->type, node_info->name,
false /* is_active*/, 0 /* pluged_time */);
}
AudioNodeList GenerateAudioNodeList(
const std::vector<const AudioNodeInfo*> nodes) {
AudioNodeList node_list;
for (auto* node_info : nodes) {
node_list.push_back(GenerateAudioNode(node_info));
}
return node_list;
}
void SetUpCrasAudioHandler(const AudioNodeList& audio_nodes) {
DBusThreadManager::Initialize();
fake_cras_audio_client_ = static_cast<FakeCrasAudioClient*>(
DBusThreadManager::Get()->GetCrasAudioClient());
fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes);
audio_pref_handler_ = new AudioDevicesPrefHandlerStub();
CrasAudioHandler::Initialize(audio_pref_handler_);
cras_audio_handler_ = CrasAudioHandler::Get();
test_observer_.reset(new TestObserver);
cras_audio_handler_->AddAudioObserver(test_observer_.get());
video_capture_manager_->AddObserver(cras_audio_handler_);
base::RunLoop().RunUntilIdle();
}
// Set up cras audio handlers with |audio_nodes| and set the active state of
// |active_device_in_pref| as active and |activate_by_user| in the pref,
// and rest of nodes in |audio_nodes_in_pref| as inactive.
void SetupCrasAudioHandlerWithActiveNodeInPref(
const AudioNodeList& audio_nodes,
const AudioNodeList& audio_nodes_in_pref,
const AudioDevice& active_device_in_pref,
bool activate_by_user) {
DBusThreadManager::Initialize();
fake_cras_audio_client_ = static_cast<FakeCrasAudioClient*>(
DBusThreadManager::Get()->GetCrasAudioClient());
audio_pref_handler_ = new AudioDevicesPrefHandlerStub();
bool active;
for (const AudioNode& node : audio_nodes_in_pref) {
active = node.id == active_device_in_pref.id;
audio_pref_handler_->SetDeviceActive(AudioDevice(node), active,
activate_by_user);
}
bool activate_by;
EXPECT_TRUE(audio_pref_handler_->GetDeviceActive(active_device_in_pref,
&active, &activate_by));
EXPECT_TRUE(active);
EXPECT_EQ(activate_by, activate_by_user);
fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes);
CrasAudioHandler::Initialize(audio_pref_handler_);
cras_audio_handler_ = CrasAudioHandler::Get();
test_observer_.reset(new TestObserver);
cras_audio_handler_->AddAudioObserver(test_observer_.get());
base::RunLoop().RunUntilIdle();
}
void SetUpCrasAudioHandlerWithPrimaryActiveNode(
const AudioNodeList& audio_nodes,
const AudioNode& primary_active_node) {
DBusThreadManager::Initialize();
fake_cras_audio_client_ = static_cast<FakeCrasAudioClient*>(
DBusThreadManager::Get()->GetCrasAudioClient());
fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes);
fake_cras_audio_client_->SetActiveOutputNode(primary_active_node.id);
audio_pref_handler_ = new AudioDevicesPrefHandlerStub();
CrasAudioHandler::Initialize(audio_pref_handler_);
cras_audio_handler_ = CrasAudioHandler::Get();
test_observer_.reset(new TestObserver);
cras_audio_handler_->AddAudioObserver(test_observer_.get());
base::RunLoop().RunUntilIdle();
}
void ChangeAudioNodes(const AudioNodeList& audio_nodes) {
fake_cras_audio_client_->SetAudioNodesAndNotifyObserversForTesting(
audio_nodes);
base::RunLoop().RunUntilIdle();
}
void VerifySystemMonitorWasCalled() {
// System monitor uses PostTask internally, so we have to process messages
// before verifying the expectation.
base::RunLoop().RunUntilIdle();
EXPECT_LE(1, system_monitor_observer_.device_changes_received());
}
const AudioDevice* GetDeviceFromId(uint64_t id) {
return cras_audio_handler_->GetDeviceFromId(id);
}
int GetActiveDeviceCount() const {
int num_active_nodes = 0;
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
for (size_t i = 0; i < audio_devices.size(); ++i) {
if (audio_devices[i].active)
++num_active_nodes;
}
return num_active_nodes;
}
void SetActiveHDMIRediscover() {
cras_audio_handler_->SetActiveHDMIOutoutRediscoveringIfNecessary(true);
}
void SetHDMIRediscoverGracePeriodDuration(int duration_in_ms) {
cras_audio_handler_->SetHDMIRediscoverGracePeriodForTesting(duration_in_ms);
}
bool IsDuringHDMIRediscoverGracePeriod() {
return cras_audio_handler_->hdmi_rediscovering();
}
void RestartAudioClient() {
cras_audio_handler_->AudioClientRestarted();
base::RunLoop().RunUntilIdle();
}
void StartFrontFacingCamera() {
video_capture_manager_->NotifyVideoCaptureStarted(
media::MEDIA_VIDEO_FACING_USER);
}
void StopFrontFacingCamera() {
video_capture_manager_->NotifyVideoCaptureStopped(
media::MEDIA_VIDEO_FACING_USER);
}
void StartRearFacingCamera() {
video_capture_manager_->NotifyVideoCaptureStarted(
media::MEDIA_VIDEO_FACING_ENVIRONMENT);
}
void StopRearFacingCamera() {
video_capture_manager_->NotifyVideoCaptureStopped(
media::MEDIA_VIDEO_FACING_ENVIRONMENT);
}
bool output_mono_enabled() const {
return cras_audio_handler_->output_mono_enabled_;
}
protected:
base::test::ScopedTaskEnvironment scoped_task_environment_;
base::SystemMonitor system_monitor_;
SystemMonitorObserver system_monitor_observer_;
CrasAudioHandler* cras_audio_handler_ = nullptr; // Not owned.
FakeCrasAudioClient* fake_cras_audio_client_ = nullptr; // Not owned.
std::unique_ptr<TestObserver> test_observer_;
scoped_refptr<AudioDevicesPrefHandlerStub> audio_pref_handler_;
std::unique_ptr<FakeVideoCaptureManager> video_capture_manager_;
private:
DISALLOW_COPY_AND_ASSIGN(CrasAudioHandlerTest);
};
class HDMIRediscoverWaiter {
public:
HDMIRediscoverWaiter(CrasAudioHandlerTest* cras_audio_handler_test,
int grace_period_duration_in_ms)
: cras_audio_handler_test_(cras_audio_handler_test),
grace_period_duration_in_ms_(grace_period_duration_in_ms) {}
void WaitUntilTimeOut(int wait_duration_in_ms) {
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, run_loop.QuitClosure(),
base::TimeDelta::FromMilliseconds(wait_duration_in_ms));
run_loop.Run();
}
void CheckHDMIRediscoverGracePeriodEnd(const base::Closure& quit_loop_func) {
if (!cras_audio_handler_test_->IsDuringHDMIRediscoverGracePeriod()) {
quit_loop_func.Run();
return;
}
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&HDMIRediscoverWaiter::CheckHDMIRediscoverGracePeriodEnd,
base::Unretained(this), quit_loop_func),
base::TimeDelta::FromMilliseconds(grace_period_duration_in_ms_ / 4));
}
void WaitUntilHDMIRediscoverGracePeriodEnd() {
base::RunLoop run_loop;
CheckHDMIRediscoverGracePeriodEnd(run_loop.QuitClosure());
run_loop.Run();
}
private:
CrasAudioHandlerTest* cras_audio_handler_test_; // not owned
int grace_period_duration_in_ms_;
DISALLOW_COPY_AND_ASSIGN(HDMIRediscoverWaiter);
};
INSTANTIATE_TEST_CASE_P(StableIdV1, CrasAudioHandlerTest, testing::Values(1));
INSTANTIATE_TEST_CASE_P(StableIdV2, CrasAudioHandlerTest, testing::Values(2));
TEST_P(CrasAudioHandlerTest, InitializeWithOnlyDefaultAudioDevices) {
AudioNodeList audio_nodes =
GenerateAudioNodeList({kInternalSpeaker, kInternalMic});
SetUpCrasAudioHandler(audio_nodes);
// Verify the audio devices size.
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(audio_nodes.size(), audio_devices.size());
// Verify the internal speaker has been selected as the active output.
AudioDevice active_output;
EXPECT_TRUE(
cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
EXPECT_EQ(kInternalSpeaker->id, active_output.id);
EXPECT_EQ(kInternalSpeaker->id,
cras_audio_handler_->GetPrimaryActiveOutputNode());
EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
// Ensure the internal microphone has been selected as the active input.
AudioDevice active_input;
EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
}
TEST_P(CrasAudioHandlerTest, InitializeWithAlternativeAudioDevices) {
AudioNodeList audio_nodes = GenerateAudioNodeList(
{kInternalSpeaker, kHeadphone, kInternalMic, kUSBMic1});
SetUpCrasAudioHandler(audio_nodes);
// Verify the audio devices size.
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(audio_nodes.size(), audio_devices.size());
// Verify the headphone has been selected as the active output.
AudioDevice active_output;
EXPECT_TRUE(
cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
EXPECT_EQ(kHeadphone->id, active_output.id);
EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
// Ensure the USB microphone has been selected as the active input.
EXPECT_EQ(kUSBMicId1, cras_audio_handler_->GetPrimaryActiveInputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
}
TEST_P(CrasAudioHandlerTest, InitializeWithKeyboardMic) {
AudioNodeList audio_nodes =
GenerateAudioNodeList({kInternalSpeaker, kInternalMic, kKeyboardMic});
SetUpCrasAudioHandler(audio_nodes);
// Verify the audio devices size.
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(audio_nodes.size(), audio_devices.size());
EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic());
// Verify the internal speaker has been selected as the active output.
AudioDevice active_output;
EXPECT_TRUE(
cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
EXPECT_EQ(kInternalSpeaker->id, active_output.id);
EXPECT_EQ(kInternalSpeaker->id,
cras_audio_handler_->GetPrimaryActiveOutputNode());
EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
// Ensure the internal microphone has been selected as the active input,
// not affected by keyboard mic.
AudioDevice active_input;
EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
const AudioDevice* keyboard_mic = GetDeviceFromId(kKeyboardMic->id);
EXPECT_FALSE(keyboard_mic->active);
}
TEST_P(CrasAudioHandlerTest, SetKeyboardMicActive) {
AudioNodeList audio_nodes =
GenerateAudioNodeList({kInternalMic, kKeyboardMic});
SetUpCrasAudioHandler(audio_nodes);
// Verify the audio devices size.
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(audio_nodes.size(), audio_devices.size());
EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic());
// Ensure the internal microphone has been selected as the active input,
// not affected by keyboard mic.
AudioDevice active_input;
EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
const AudioDevice* keyboard_mic = GetDeviceFromId(kKeyboardMic->id);
EXPECT_FALSE(keyboard_mic->active);
// Make keyboard mic active.
cras_audio_handler_->SetKeyboardMicActive(true);
EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
const AudioDevice* active_keyboard_mic = GetDeviceFromId(kKeyboardMic->id);
EXPECT_TRUE(active_keyboard_mic->active);
// Make keyboard mic inactive.
cras_audio_handler_->SetKeyboardMicActive(false);
EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
const AudioDevice* inactive_keyboard_mic = GetDeviceFromId(kKeyboardMic->id);
EXPECT_FALSE(inactive_keyboard_mic->active);
}
TEST_P(CrasAudioHandlerTest, KeyboardMicNotSetAsPrimaryActive) {
AudioNodeList audio_nodes = GenerateAudioNodeList({kKeyboardMic});
SetUpCrasAudioHandler(audio_nodes);
// Verify keyboard mic is not set as primary active input.
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(audio_nodes.size(), audio_devices.size());
EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic());
EXPECT_EQ(0u, cras_audio_handler_->GetPrimaryActiveInputNode());
// Verify the internal mic is set as primary input.
audio_nodes.push_back(GenerateAudioNode(kInternalMic));
ChangeAudioNodes(audio_nodes);
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(audio_nodes.size(), audio_devices.size());
EXPECT_TRUE(cras_audio_handler_->HasKeyboardMic());
EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
}
TEST_P(CrasAudioHandlerTest, SwitchActiveOutputDevice) {
AudioNodeList audio_nodes =
GenerateAudioNodeList({kInternalSpeaker, kHeadphone});
SetUpCrasAudioHandler(audio_nodes);
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(audio_nodes.size(), audio_devices.size());
system_monitor_observer_.reset_count();
// Verify the initial active output device is headphone.
EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
AudioDevice active_output;
EXPECT_TRUE(
cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
EXPECT_EQ(kHeadphone->id, active_output.id);
EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
// Switch the active output to internal speaker.
AudioDevice internal_speaker(GenerateAudioNode(kInternalSpeaker));
cras_audio_handler_->SwitchToDevice(internal_speaker, true,
CrasAudioHandler::ACTIVATE_BY_USER);
// Verify the active output is switched to internal speaker, and the
// ActiveOutputNodeChanged event is fired.
EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
VerifySystemMonitorWasCalled();
EXPECT_TRUE(
cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
EXPECT_EQ(kInternalSpeaker->id, active_output.id);
EXPECT_EQ(kInternalSpeaker->id,
cras_audio_handler_->GetPrimaryActiveOutputNode());
}
TEST_P(CrasAudioHandlerTest, SwitchActiveInputDevice) {
AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalMic, kUSBMic1});
SetUpCrasAudioHandler(audio_nodes);
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(audio_nodes.size(), audio_devices.size());
// Verify the initial active input device is USB mic.
EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
EXPECT_EQ(kUSBMicId1, cras_audio_handler_->GetPrimaryActiveInputNode());
// Switch the active input to internal mic.
AudioDevice internal_mic(GenerateAudioNode(kInternalMic));
cras_audio_handler_->SwitchToDevice(internal_mic, true,
CrasAudioHandler::ACTIVATE_BY_USER);
// Verify the active output is switched to internal speaker, and the active
// ActiveInputNodeChanged event is fired.
EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
EXPECT_EQ(kInternalMic->id, cras_audio_handler_->GetPrimaryActiveInputNode());
}
TEST_P(CrasAudioHandlerTest, PlugHeadphone) {
// Set up initial audio devices, only with internal speaker.
AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker});
SetUpCrasAudioHandler(audio_nodes);
const size_t init_nodes_size = audio_nodes.size();
// Verify the audio devices size.
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
// Verify the internal speaker has been selected as the active output.
EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
AudioDevice active_output;
EXPECT_TRUE(
cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
EXPECT_EQ(kInternalSpeaker->id, active_output.id);
EXPECT_EQ(kInternalSpeaker->id,
cras_audio_handler_->GetPrimaryActiveOutputNode());
EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
system_monitor_observer_.reset_count();
// Plug the headphone.
audio_nodes.clear();
AudioNode internal_speaker = GenerateAudioNode(kInternalSpeaker);
internal_speaker.active = true;
audio_nodes.push_back(internal_speaker);
audio_nodes.push_back(GenerateAudioNode(kHeadphone));
ChangeAudioNodes(audio_nodes);
// Verify the AudioNodesChanged event is fired and new audio device is added.
EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
VerifySystemMonitorWasCalled();
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
// Verify the active output device is switched to headphone and
// ActiveOutputChanged event is fired.
EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
EXPECT_TRUE(
cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
EXPECT_EQ(kHeadphone->id, active_output.id);
EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
}
TEST_P(CrasAudioHandlerTest, UnplugHeadphone) {
// Set up initial audio devices, with internal speaker and headphone.
AudioNodeList audio_nodes =
GenerateAudioNodeList({kInternalSpeaker, kHeadphone});
SetUpCrasAudioHandler(audio_nodes);
const size_t init_nodes_size = audio_nodes.size();
// Verify the audio devices size.
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
// Verify the headphone has been selected as the active output.
EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
AudioDevice active_output;
EXPECT_TRUE(
cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
EXPECT_EQ(kHeadphone->id, active_output.id);
EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
system_monitor_observer_.reset_count();
// Unplug the headphone.
audio_nodes.clear();
audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
ChangeAudioNodes(audio_nodes);
// Verify the AudioNodesChanged event is fired and one audio device is
// removed.
EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
VerifySystemMonitorWasCalled();
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
// Verify the active output device is switched to internal speaker and
// ActiveOutputChanged event is fired.
EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
EXPECT_TRUE(
cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
EXPECT_EQ(kInternalSpeaker->id, active_output.id);
EXPECT_EQ(kInternalSpeaker->id,
cras_audio_handler_->GetPrimaryActiveOutputNode());
EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
}
TEST_P(CrasAudioHandlerTest, InitializeWithBluetoothHeadset) {
AudioNodeList audio_nodes =
GenerateAudioNodeList({kInternalSpeaker, kBluetoothHeadset});
SetUpCrasAudioHandler(audio_nodes);
// Verify the audio devices size.
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(audio_nodes.size(), audio_devices.size());
EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
// Verify the bluetooth headset has been selected as the active output.
EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
AudioDevice active_output;
EXPECT_TRUE(
cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
EXPECT_EQ(kBluetoothHeadset->id, active_output.id);
EXPECT_EQ(kBluetoothHeadset->id,
cras_audio_handler_->GetPrimaryActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
}
TEST_P(CrasAudioHandlerTest, ConnectAndDisconnectBluetoothHeadset) {
// Initialize with internal speaker and headphone.
AudioNodeList audio_nodes =
GenerateAudioNodeList({kInternalSpeaker, kHeadphone});
SetUpCrasAudioHandler(audio_nodes);
const size_t init_nodes_size = audio_nodes.size();
// Verify the audio devices size.
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
// Verify the headphone is selected as the active output initially.
EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
AudioDevice active_output;
EXPECT_TRUE(
cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
EXPECT_EQ(kHeadphone->id, active_output.id);
EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
// Connect to bluetooth headset. Since it is plugged in later than
// headphone, active output should be switched to it.
system_monitor_observer_.reset_count();
audio_nodes.clear();
audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
AudioNode headphone = GenerateAudioNode(kHeadphone);
headphone.plugged_time = 80000000;
headphone.active = true;
audio_nodes.push_back(headphone);
AudioNode bluetooth_headset = GenerateAudioNode(kBluetoothHeadset);
bluetooth_headset.plugged_time = 90000000;
audio_nodes.push_back(bluetooth_headset);
ChangeAudioNodes(audio_nodes);
// Verify the AudioNodesChanged event is fired and new audio device is added.
EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
VerifySystemMonitorWasCalled();
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
// Verify the active output device is switched to bluetooth headset, and
// ActiveOutputChanged event is fired.
EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
EXPECT_TRUE(
cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
EXPECT_EQ(kBluetoothHeadset->id, active_output.id);
EXPECT_EQ(kBluetoothHeadset->id,
cras_audio_handler_->GetPrimaryActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
// Disconnect bluetooth headset.
system_monitor_observer_.reset_count();
audio_nodes.clear();
audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
headphone.active = false;
audio_nodes.push_back(headphone);
ChangeAudioNodes(audio_nodes);
// Verify the AudioNodesChanged event is fired and one audio device is
// removed.
EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
VerifySystemMonitorWasCalled();
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
// Verify the active output device is switched to headphone, and
// ActiveOutputChanged event is fired.
EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
EXPECT_TRUE(
cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
EXPECT_EQ(kHeadphone->id, active_output.id);
EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
}
TEST_P(CrasAudioHandlerTest, InitializeWithHDMIOutput) {
AudioNodeList audio_nodes =
GenerateAudioNodeList({kInternalSpeaker, kHDMIOutput});
SetUpCrasAudioHandler(audio_nodes);
// Verify the audio devices size.
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(audio_nodes.size(), audio_devices.size());
EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
// Verify the HDMI device has been selected as the active output.
EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
AudioDevice active_output;
EXPECT_TRUE(
cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
EXPECT_EQ(kHDMIOutput->id, active_output.id);
EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
}
TEST_P(CrasAudioHandlerTest, ConnectAndDisconnectHDMIOutput) {
// Initialize with internal speaker.
AudioNodeList audio_nodes = GenerateAudioNodeList({kInternalSpeaker});
SetUpCrasAudioHandler(audio_nodes);
const size_t init_nodes_size = audio_nodes.size();
// Verify the audio devices size.
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
// Verify the internal speaker is selected as the active output initially.
EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
AudioDevice active_output;
EXPECT_TRUE(
cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
EXPECT_EQ(kInternalSpeaker->id, active_output.id);
EXPECT_EQ(kInternalSpeaker->id,
cras_audio_handler_->GetPrimaryActiveOutputNode());
EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
system_monitor_observer_.reset_count();
// Connect to HDMI output.
audio_nodes.clear();
AudioNode internal_speaker = GenerateAudioNode(kInternalSpeaker);
internal_speaker.active = true;
internal_speaker.plugged_time = 80000000;
audio_nodes.push_back(internal_speaker);
AudioNode hdmi = GenerateAudioNode(kHDMIOutput);
hdmi.plugged_time = 90000000;
audio_nodes.push_back(hdmi);
ChangeAudioNodes(audio_nodes);
// Verify the AudioNodesChanged event is fired and new audio device is added.
EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
VerifySystemMonitorWasCalled();
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
// Verify the active output device is switched to hdmi output, and
// ActiveOutputChanged event is fired.
EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
EXPECT_TRUE(
cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
EXPECT_EQ(kHDMIOutput->id, active_output.id);
EXPECT_EQ(kHDMIOutput->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
system_monitor_observer_.reset_count();
// Disconnect hdmi headset.
audio_nodes.clear();
audio_nodes.push_back(GenerateAudioNode(kInternalSpeaker));
ChangeAudioNodes(audio_nodes);
// Verify the AudioNodesChanged event is fired and one audio device is
// removed.
EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
VerifySystemMonitorWasCalled();
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
// Verify the active output device is switched to internal speaker, and
// ActiveOutputChanged event is fired.
EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
EXPECT_TRUE(
cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
EXPECT_EQ(kInternalSpeaker->id, active_output.id);
EXPECT_EQ(kInternalSpeaker->id,
cras_audio_handler_->GetPrimaryActiveOutputNode());
EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
}
TEST_P(CrasAudioHandlerTest, HandleHeadphoneAndHDMIOutput) {
// Initialize with internal speaker, headphone and HDMI output.
AudioNodeList audio_nodes =
GenerateAudioNodeList({kInternalSpeaker, kHeadphone, kHDMIOutput});
SetUpCrasAudioHandler(audio_nodes);
const size_t init_nodes_size = audio_nodes.size();
// Verify the audio devices size.
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
// Verify the headphone is selected as the active output initially.
EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
AudioDevice active_output;
EXPECT_TRUE(
cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
EXPECT_EQ(kHeadphone->id, active_output.id);
EXPECT_EQ(kHeadphone->id, cras_audio_handler_->GetPrimaryActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());