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
ash / system / audio / audio_effects_controller_unittest.cc [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/audio/audio_effects_controller.h"
#include <vector>
#include "ash/accessibility/accessibility_controller.h"
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_switches.h"
#include "ash/shell.h"
#include "ash/system/status_area_widget.h"
#include "ash/system/status_area_widget_test_helper.h"
#include "ash/system/video_conference/effects/video_conference_tray_effects_manager_types.h"
#include "ash/system/video_conference/fake_video_conference_tray_controller.h"
#include "ash/system/video_conference/video_conference_common.h"
#include "ash/system/video_conference/video_conference_tray.h"
#include "ash/test/ash_test_base.h"
#include "base/command_line.h"
#include "base/memory/raw_ptr.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "chromeos/ash/components/audio/cras_audio_handler.h"
#include "chromeos/ash/components/dbus/audio/cras_audio_client.h"
#include "chromeos/ash/components/dbus/audio/fake_cras_audio_client.h"
namespace ash {
namespace {
struct AudioNodeInfo {
bool is_input;
uint64_t id;
const char* const device_name;
const char* const type;
const char* const name;
uint32_t audio_effect;
};
constexpr uint32_t kNoiseCancellationAudioEffect = 1;
constexpr uint32_t kStyleTransferAudioEffect = 4;
constexpr AudioNodeInfo kInternalMic_NC[] = {
{.is_input = true,
.id = 10001,
.device_name = "Internal Mic",
.type = "INTERNAL_MIC",
.name = "Internal Mic",
.audio_effect = kNoiseCancellationAudioEffect}};
constexpr AudioNodeInfo kInternalMic_NoEffects[] = {
{.is_input = true,
.id = 10002,
.device_name = "Internal Mic",
.type = "INTERNAL_MIC",
.name = "Internal Mic",
.audio_effect = 0u}};
constexpr AudioNodeInfo kInternalMic_NC_ST[] = {
{.is_input = true,
.id = 10003,
.device_name = "Internal Mic",
.type = "INTERNAL_MIC",
.name = "Internal Mic",
.audio_effect =
kNoiseCancellationAudioEffect | kStyleTransferAudioEffect}};
constexpr AudioNodeInfo kInternalMic_ST[] = {
{.is_input = true,
.id = 10004,
.device_name = "Internal Mic",
.type = "INTERNAL_MIC",
.name = "Internal Mic",
.audio_effect = kStyleTransferAudioEffect}};
constexpr AudioNodeInfo kInternalSpeakerWithNC[] = {
{.is_input = false,
.id = 20001,
.device_name = "Internal Speaker",
.type = "INTERNAL_SPEAKER",
.name = "Internal Speaker",
.audio_effect = kNoiseCancellationAudioEffect}};
constexpr AudioNodeInfo kInternalSpeakerWithoutNC[] = {
{.is_input = false,
.id = 20002,
.device_name = "Internal Speaker",
.type = "INTERNAL_SPEAKER",
.name = "Internal Speaker",
.audio_effect = 0u}};
AudioNode GenerateAudioNode(const AudioNodeInfo* node_info) {
return AudioNode(node_info->is_input, node_info->id,
/*has_v2_stable_device_id=*/false, node_info->id,
/*stable_device_id_v2=*/0, node_info->device_name,
node_info->type, node_info->name, /*active=*/false,
/* plugged_time=*/0,
/*max_supported_channels=*/1, node_info->audio_effect,
/*number_of_volume_steps=*/0);
}
AudioNodeList GenerateAudioNodeList(
const std::vector<const AudioNodeInfo*>& nodes) {
AudioNodeList node_list;
for (auto* node_info : nodes) {
node_list.emplace_back(GenerateAudioNode(node_info));
}
return node_list;
}
} // namespace
class AudioEffectsControllerTest : public NoSessionAshTestBase {
public:
AudioEffectsControllerTest() = default;
AudioEffectsControllerTest(const AudioEffectsControllerTest&) = delete;
AudioEffectsControllerTest& operator=(const AudioEffectsControllerTest&) =
delete;
~AudioEffectsControllerTest() override = default;
// NoSessionAshTestBase:
void SetUp() override {
scoped_feature_list_.InitAndEnableFeature(
features::kFeatureManagementVideoConference);
// Here we have to create the global instance of `CrasAudioHandler` before
// `FakeVideoConferenceTrayController`, so we do it here and not in
// `AshTestBase`.
CrasAudioClient::InitializeFake();
CrasAudioHandler::InitializeForTesting();
// Instantiates a fake controller (the real one is created in
// ChromeBrowserMainExtraPartsAsh::PreProfileInit() which is not called in
// ash unit tests). This controller is needed because it owns the effects
// manager.
tray_controller_ = std::make_unique<FakeVideoConferenceTrayController>();
set_create_global_cras_audio_handler(false);
NoSessionAshTestBase::SetUp();
audio_effects_controller_ = Shell::Get()->audio_effects_controller();
}
void TearDown() override {
AshTestBase::TearDown();
tray_controller_.reset();
CrasAudioHandler::Shutdown();
CrasAudioClient::Shutdown();
}
protected:
FakeCrasAudioClient* fake_cras_audio_client() {
return FakeCrasAudioClient::Get();
}
CrasAudioHandler* cras_audio_handler() { return CrasAudioHandler::Get(); }
AudioEffectsController* audio_effects_controller() {
return audio_effects_controller_;
}
void ChangeAudioInput(const AudioNodeInfo* node_info) {
const std::vector<const AudioNodeInfo*> audio_info_nodes = {node_info};
fake_cras_audio_client()->SetAudioNodesAndNotifyObserversForTesting(
GenerateAudioNodeList(audio_info_nodes));
cras_audio_handler()->RequestNoiseCancellationSupported(base::DoNothing());
}
void ChangeAudioOutput(bool noise_cancellation_supported) {
// Noise cancellation support state for output device is set in the platform
// level (not in chrome level), then it will propagate that state through
// `RequestNoiseCancellationSupported()`. Since we can't access platform
// level, we will mimic that process like below for testing purpose.
fake_cras_audio_client()->SetNoiseCancellationSupported(
noise_cancellation_supported);
cras_audio_handler()->RequestNoiseCancellationSupported(base::DoNothing());
cras_audio_handler()->SwitchToDevice(
AudioDevice(GenerateAudioNode(noise_cancellation_supported
? kInternalSpeakerWithNC
: kInternalSpeakerWithoutNC)),
/*notify=*/true, DeviceActivateType::kActivateByUser);
}
VideoConferenceTray* GetVideoConfereneTray() {
return StatusAreaWidgetTestHelper::GetStatusAreaWidget()
->video_conference_tray();
}
void OpenVideoConferenceBubble() {
// Update media status to make the video conference tray visible.
VideoConferenceMediaState state;
state.has_media_app = true;
state.has_camera_permission = true;
state.has_microphone_permission = true;
state.is_capturing_screen = true;
tray_controller_->UpdateWithMediaState(state);
// Open the bubble by clicking the toggle button.
LeftClickOn(GetVideoConfereneTray()->toggle_bubble_button());
}
base::HistogramTester histogram_tester_;
private:
raw_ptr<AudioEffectsController, DanglingUntriaged> audio_effects_controller_ =
nullptr;
std::unique_ptr<FakeVideoConferenceTrayController> tray_controller_;
base::test::ScopedFeatureList scoped_feature_list_;
};
TEST_F(AudioEffectsControllerTest, NoiseCancellationNotSupported) {
// Prepare `CrasAudioHandler` to report that noise cancellation is
// not-supported.
fake_cras_audio_client()->SetNoiseCancellationSupported(false);
cras_audio_handler()->RequestNoiseCancellationSupported(base::DoNothing());
SimulateUserLogin("testuser1@gmail.com");
// `AudioEffectsController` reports noise that cancellation is not-supported.
EXPECT_FALSE(audio_effects_controller()->IsEffectSupported(
VcEffectId::kNoiseCancellation));
EXPECT_FALSE(audio_effects_controller()->GetEffectById(
VcEffectId::kNoiseCancellation));
}
TEST_F(AudioEffectsControllerTest,
NoiseCancellationNotSupportedByVcIfStyleTransferSupported) {
// Change audio input to support both NC and style_transfer
ChangeAudioInput(kInternalMic_NC_ST);
// Prepare `CrasAudioHandler` to report that noise cancellation and style
// transfer are both supported by hardware.
fake_cras_audio_client()->SetNoiseCancellationSupported(true);
fake_cras_audio_client()->SetStyleTransferSupported(true);
cras_audio_handler()->RequestNoiseCancellationSupported(base::DoNothing());
cras_audio_handler()->RequestStyleTransferSupported(base::DoNothing());
SimulateUserLogin("testuser1@gmail.com");
EXPECT_FALSE(audio_effects_controller()->IsEffectSupported(
VcEffectId::kNoiseCancellation));
EXPECT_FALSE(audio_effects_controller()->GetEffectById(
VcEffectId::kNoiseCancellation));
EXPECT_TRUE(audio_effects_controller()->IsEffectSupported(
VcEffectId::kStyleTransfer));
EXPECT_TRUE(
audio_effects_controller()->GetEffectById(VcEffectId::kStyleTransfer));
}
TEST_F(AudioEffectsControllerTest, NoiseCancellationSupported) {
// Prepare `CrasAudioHandler` to report that noise cancellation is supported.
fake_cras_audio_client()->SetNoiseCancellationSupported(true);
cras_audio_handler()->RequestNoiseCancellationSupported(base::DoNothing());
SimulateUserLogin("testuser1@gmail.com");
// `AudioEffectsController` reports that noise cancellation is supported.
EXPECT_TRUE(audio_effects_controller()->IsEffectSupported(
VcEffectId::kNoiseCancellation));
EXPECT_TRUE(audio_effects_controller()->GetEffectById(
VcEffectId::kNoiseCancellation));
// Makes sure the dependency flag is set when the effect is supported.
auto* effect =
audio_effects_controller()->GetEffectById(VcEffectId::kNoiseCancellation);
EXPECT_EQ(VcHostedEffect::ResourceDependency::kMicrophone,
effect->dependency_flags());
// Delegate should be registered.
EXPECT_TRUE(VideoConferenceTrayController::Get()
->GetEffectsManager()
.IsDelegateRegistered(audio_effects_controller()));
}
TEST_F(AudioEffectsControllerTest, NoiseCancellationNotEnabled) {
// Prepare noise cancellation support.
fake_cras_audio_client()->SetNoiseCancellationSupported(true);
cras_audio_handler()->RequestNoiseCancellationSupported(base::DoNothing());
SimulateUserLogin("testuser1@gmail.com");
// Explicitly disable noise cancellation.
cras_audio_handler()->SetNoiseCancellationState(
false, CrasAudioHandler::AudioSettingsChangeSource::kVideoConferenceTray);
histogram_tester_.ExpectBucketCount(
CrasAudioHandler::kNoiseCancellationEnabledSourceHistogramName,
CrasAudioHandler::AudioSettingsChangeSource::kVideoConferenceTray, 1);
// Noise cancellation effect state is disabled.
std::optional<int> effect_state = audio_effects_controller()->GetEffectState(
VcEffectId::kNoiseCancellation);
EXPECT_TRUE(effect_state.has_value());
EXPECT_EQ(effect_state, 0);
cras_audio_handler()->SetNoiseCancellationState(
true, CrasAudioHandler::AudioSettingsChangeSource::kVideoConferenceTray);
effect_state = audio_effects_controller()->GetEffectState(
VcEffectId::kNoiseCancellation);
EXPECT_TRUE(effect_state.has_value());
EXPECT_EQ(effect_state, 1);
histogram_tester_.ExpectBucketCount(
CrasAudioHandler::kNoiseCancellationEnabledSourceHistogramName,
CrasAudioHandler::AudioSettingsChangeSource::kVideoConferenceTray, 2);
}
TEST_F(AudioEffectsControllerTest, NoiseCancellationEnabled) {
// Prepare noise cancellation support.
fake_cras_audio_client()->SetNoiseCancellationSupported(true);
cras_audio_handler()->RequestNoiseCancellationSupported(base::DoNothing());
SimulateUserLogin("testuser1@gmail.com");
// Explicitly enable noise cancellation.
cras_audio_handler()->SetNoiseCancellationState(
true, CrasAudioHandler::AudioSettingsChangeSource::kVideoConferenceTray);
histogram_tester_.ExpectBucketCount(
CrasAudioHandler::kNoiseCancellationEnabledSourceHistogramName,
CrasAudioHandler::AudioSettingsChangeSource::kVideoConferenceTray, 1);
// Noise cancellation effect state is disabled.
std::optional<int> effect_state = audio_effects_controller()->GetEffectState(
VcEffectId::kNoiseCancellation);
EXPECT_TRUE(effect_state.has_value());
EXPECT_EQ(effect_state, 1);
}
TEST_F(AudioEffectsControllerTest, NoiseCancellationSetNotEnabled) {
// Prepare noise cancellation support.
fake_cras_audio_client()->SetNoiseCancellationSupported(true);
cras_audio_handler()->RequestNoiseCancellationSupported(base::DoNothing());
SimulateUserLogin("testuser1@gmail.com");
// Explicitly enable noise cancellation.
cras_audio_handler()->SetNoiseCancellationState(
true, CrasAudioHandler::AudioSettingsChangeSource::kVideoConferenceTray);
// Check that noise cancellation is enabled.
EXPECT_TRUE(cras_audio_handler()->GetNoiseCancellationState());
// User pressed the noise cancellation toggle.
audio_effects_controller()->OnEffectControlActivated(
VcEffectId::kNoiseCancellation, std::nullopt);
// State should now be disabled.
EXPECT_FALSE(cras_audio_handler()->GetNoiseCancellationState());
}
TEST_F(AudioEffectsControllerTest, NoiseCancellationSetEnabled) {
// Prepare noise cancellation support.
fake_cras_audio_client()->SetNoiseCancellationSupported(true);
cras_audio_handler()->RequestNoiseCancellationSupported(base::DoNothing());
SimulateUserLogin("testuser1@gmail.com");
// Explicitly disable noise cancellation.
cras_audio_handler()->SetNoiseCancellationState(
false, CrasAudioHandler::AudioSettingsChangeSource::kVideoConferenceTray);
// Check that noise cancellation is disabled.
EXPECT_FALSE(cras_audio_handler()->GetNoiseCancellationState());
// User pressed the noise cancellation toggle.
audio_effects_controller()->OnEffectControlActivated(
VcEffectId::kNoiseCancellation, std::nullopt);
// State should now be enabled.
EXPECT_TRUE(cras_audio_handler()->GetNoiseCancellationState());
}
TEST_F(AudioEffectsControllerTest, NoiseCancellationAudioInputDevice) {
// Prepare `CrasAudioHandler` to report that noise cancellation is supported.
// However, the input audio does not support noise cancellation.
fake_cras_audio_client()->SetNoiseCancellationSupported(true);
cras_audio_handler()->RequestNoiseCancellationSupported(base::DoNothing());
ChangeAudioInput(kInternalMic_NoEffects);
SimulateUserLogin("testuser1@gmail.com");
// `AudioEffectsController` reports noise that cancellation is not-supported.
EXPECT_FALSE(audio_effects_controller()->IsEffectSupported(
VcEffectId::kNoiseCancellation));
EXPECT_FALSE(audio_effects_controller()->GetEffectById(
VcEffectId::kNoiseCancellation));
// Change to an input that does support. The state should reflect that.
ChangeAudioInput(kInternalMic_NC);
EXPECT_TRUE(audio_effects_controller()->IsEffectSupported(
VcEffectId::kNoiseCancellation));
EXPECT_TRUE(audio_effects_controller()->GetEffectById(
VcEffectId::kNoiseCancellation));
}
TEST_F(AudioEffectsControllerTest, NoiseCancellationSwitchInputDevice) {
fake_cras_audio_client()->SetAudioNodesAndNotifyObserversForTesting(
GenerateAudioNodeList({kInternalMic_NC, kInternalMic_NoEffects}));
// Prepare `CrasAudioHandler` to report that noise cancellation is supported.
fake_cras_audio_client()->SetNoiseCancellationSupported(true);
cras_audio_handler()->RequestNoiseCancellationSupported(base::DoNothing());
SimulateUserLogin("testuser1@gmail.com");
// Switch to use `kInternalMic_NoEffects`, `AudioEffectsController` reports
// noise that cancellation is not-supported.
cras_audio_handler()->SwitchToDevice(
AudioDevice(GenerateAudioNode(kInternalMic_NoEffects)), /*notify=*/true,
DeviceActivateType::kActivateByUser);
EXPECT_FALSE(audio_effects_controller()->IsEffectSupported(
VcEffectId::kNoiseCancellation));
EXPECT_FALSE(audio_effects_controller()->GetEffectById(
VcEffectId::kNoiseCancellation));
// Switch to use `kInternalMic_NC`, `AudioEffectsController` reports noise
// that cancellation is supported.
cras_audio_handler()->SwitchToDevice(
AudioDevice(GenerateAudioNode(kInternalMic_NC)), /*notify=*/true,
DeviceActivateType::kActivateByUser);
EXPECT_TRUE(audio_effects_controller()->IsEffectSupported(
VcEffectId::kNoiseCancellation));
EXPECT_TRUE(audio_effects_controller()->GetEffectById(
VcEffectId::kNoiseCancellation));
// Switch back to use `kInternalMic_NoEffects`, `AudioEffectsController`
// reports noise that cancellation is not-supported.
cras_audio_handler()->SwitchToDevice(
AudioDevice(GenerateAudioNode(kInternalMic_NoEffects)), /*notify=*/true,
DeviceActivateType::kActivateByUser);
EXPECT_FALSE(audio_effects_controller()->IsEffectSupported(
VcEffectId::kNoiseCancellation));
EXPECT_FALSE(audio_effects_controller()->GetEffectById(
VcEffectId::kNoiseCancellation));
}
TEST_F(AudioEffectsControllerTest, NoiseCancellationSwitchOutputDevice) {
fake_cras_audio_client()->SetAudioNodesAndNotifyObserversForTesting(
GenerateAudioNodeList({kInternalMic_NC, kInternalSpeakerWithNC,
kInternalSpeakerWithoutNC}));
// Prepare `CrasAudioHandler` to report that noise cancellation is supported.
fake_cras_audio_client()->SetNoiseCancellationSupported(true);
cras_audio_handler()->RequestNoiseCancellationSupported(base::DoNothing());
SimulateUserLogin("testuser1@gmail.com");
// Switch output device to not support NC, `AudioEffectsController` reports
// noise that cancellation is not-supported.
ChangeAudioOutput(/*noise_cancellation_supported=*/false);
EXPECT_FALSE(audio_effects_controller()->IsEffectSupported(
VcEffectId::kNoiseCancellation));
EXPECT_FALSE(audio_effects_controller()->GetEffectById(
VcEffectId::kNoiseCancellation));
// Switch output device to support NC, `AudioEffectsController` reports noise
// that cancellation is supported.
ChangeAudioOutput(/*noise_cancellation_supported=*/true);
EXPECT_TRUE(audio_effects_controller()->IsEffectSupported(
VcEffectId::kNoiseCancellation));
EXPECT_TRUE(audio_effects_controller()->GetEffectById(
VcEffectId::kNoiseCancellation));
// Switch back output device to not support NC, `AudioEffectsController`
// reports noise that cancellation is not-supported.
ChangeAudioOutput(/*noise_cancellation_supported=*/false);
EXPECT_FALSE(audio_effects_controller()->IsEffectSupported(
VcEffectId::kNoiseCancellation));
EXPECT_FALSE(audio_effects_controller()->GetEffectById(
VcEffectId::kNoiseCancellation));
}
TEST_F(AudioEffectsControllerTest, CloseBubble) {
fake_cras_audio_client()->SetNoiseCancellationSupported(true);
cras_audio_handler()->RequestNoiseCancellationSupported(base::DoNothing());
ChangeAudioInput(kInternalMic_NC);
SimulateUserLogin("testuser1@gmail.com");
OpenVideoConferenceBubble();
ASSERT_TRUE(GetVideoConfereneTray()->GetBubbleView());
// Change to an input device that does not support noise cancellation. The
// bubble should close automatically to update effect state.
ChangeAudioInput(kInternalMic_NoEffects);
EXPECT_FALSE(GetVideoConfereneTray()->GetBubbleView());
}
TEST_F(AudioEffectsControllerTest, LiveCaptionNotSupported) {
SimulateUserLogin("testuser1@gmail.com");
// No live caption feature flags enabled, so `AudioEffectsController` reports
// that live caption is not supported.
EXPECT_FALSE(
audio_effects_controller()->IsEffectSupported(VcEffectId::kLiveCaption));
}
TEST_F(AudioEffectsControllerTest, LiveCaptionSupported) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitWithFeatures(
{features::kOnDeviceSpeechRecognition,
features::kShowLiveCaptionInVideoConferenceTray},
{});
SimulateUserLogin("testuser1@gmail.com");
// Live caption feature flags are enabled, so `AudioEffectsController` reports
// that live caption is supported.
EXPECT_TRUE(
audio_effects_controller()->IsEffectSupported(VcEffectId::kLiveCaption));
EXPECT_TRUE(
audio_effects_controller()->GetEffectById(VcEffectId::kLiveCaption));
// Delegate should be registered.
EXPECT_TRUE(VideoConferenceTrayController::Get()
->GetEffectsManager()
.IsDelegateRegistered(audio_effects_controller()));
}
// Tests that with `features::kShowLiveCaptionInVideoConferenceTray`
// disabled, the live caption button does not show up in the vc tray.
TEST_F(AudioEffectsControllerTest, DoNotShowLiveCaptionInVcTray) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitWithFeatures(
{features::kOnDeviceSpeechRecognition},
{features::kShowLiveCaptionInVideoConferenceTray});
SimulateUserLogin("testuser1@gmail.com");
EXPECT_FALSE(
audio_effects_controller()->IsEffectSupported(VcEffectId::kLiveCaption));
EXPECT_FALSE(
audio_effects_controller()->GetEffectById(VcEffectId::kLiveCaption));
}
TEST_F(AudioEffectsControllerTest, LiveCaptionNotEnabled) {
// Ensure that live caption is supported.
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitWithFeatures({features::kOnDeviceSpeechRecognition},
{});
SimulateUserLogin("testuser1@gmail.com");
// Explicitly disable live caption, confirm that it is disabled.
AccessibilityController* controller =
Shell::Get()->accessibility_controller();
controller->live_caption().SetEnabled(false);
EXPECT_FALSE(controller->live_caption().enabled());
// Live caption effect state is disabled.
std::optional<int> state =
audio_effects_controller()->GetEffectState(VcEffectId::kLiveCaption);
EXPECT_TRUE(state.has_value());
EXPECT_FALSE(state.value());
}
TEST_F(AudioEffectsControllerTest, LiveCaptionEnabled) {
// Ensure that live caption is supported.
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitWithFeatures({features::kOnDeviceSpeechRecognition},
{});
SimulateUserLogin("testuser1@gmail.com");
// Explicitly enable live caption, confirm that it is enabled.
AccessibilityController* controller =
Shell::Get()->accessibility_controller();
controller->live_caption().SetEnabled(true);
EXPECT_TRUE(controller->live_caption().enabled());
// Live caption effect state is enabled.
std::optional<int> state =
audio_effects_controller()->GetEffectState(VcEffectId::kLiveCaption);
EXPECT_TRUE(state.has_value());
EXPECT_TRUE(state.value());
}
TEST_F(AudioEffectsControllerTest, LiveCaptionSetNotEnabled) {
// Ensure that live caption is supported.
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitWithFeatures({features::kOnDeviceSpeechRecognition},
{});
SimulateUserLogin("testuser1@gmail.com");
// Explicitly enable live caption, confirm that it is enabled.
AccessibilityController* controller =
Shell::Get()->accessibility_controller();
controller->live_caption().SetEnabled(true);
EXPECT_TRUE(controller->live_caption().enabled());
// User pressed the live caption toggle.
audio_effects_controller()->OnEffectControlActivated(VcEffectId::kLiveCaption,
std::nullopt);
// Live caption is now disabled.
EXPECT_FALSE(controller->live_caption().enabled());
}
TEST_F(AudioEffectsControllerTest, LiveCaptionSetEnabled) {
// Ensure that live caption is supported.
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitWithFeatures({features::kOnDeviceSpeechRecognition},
{});
SimulateUserLogin("testuser1@gmail.com");
// Explicitly disable live caption, confirm that it is disabled.
AccessibilityController* controller =
Shell::Get()->accessibility_controller();
controller->live_caption().SetEnabled(false);
EXPECT_FALSE(controller->live_caption().enabled());
// User pressed the live caption toggle.
audio_effects_controller()->OnEffectControlActivated(VcEffectId::kLiveCaption,
std::nullopt);
// Live caption is now enabled.
EXPECT_TRUE(controller->live_caption().enabled());
}
TEST_F(AudioEffectsControllerTest, LiveCaptionAndNoiseCancellationAdded) {
// Prepare noise cancellation support.
fake_cras_audio_client()->SetNoiseCancellationSupported(true);
cras_audio_handler()->RequestNoiseCancellationSupported(base::DoNothing());
// Ensure that live caption is supported.
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitWithFeatures(
{features::kOnDeviceSpeechRecognition,
features::kShowLiveCaptionInVideoConferenceTray},
{});
SimulateUserLogin("testuser1@gmail.com");
// Both effects should be supported and added.
EXPECT_TRUE(audio_effects_controller()->IsEffectSupported(
VcEffectId::kNoiseCancellation));
EXPECT_TRUE(
audio_effects_controller()->IsEffectSupported(VcEffectId::kLiveCaption));
EXPECT_TRUE(audio_effects_controller()->GetEffectById(
VcEffectId::kNoiseCancellation));
EXPECT_TRUE(
audio_effects_controller()->GetEffectById(VcEffectId::kLiveCaption));
// Delegate should be registered.
EXPECT_TRUE(VideoConferenceTrayController::Get()
->GetEffectsManager()
.IsDelegateRegistered(audio_effects_controller()));
}
TEST_F(AudioEffectsControllerTest, DelegateRegistered) {
VideoConferenceTrayEffectsManager& effects_manager =
VideoConferenceTrayController::Get()->GetEffectsManager();
// No effects supported. Delegate should not be registered.
SimulateUserLogin("testuser1@gmail.com");
EXPECT_FALSE(
effects_manager.IsDelegateRegistered(audio_effects_controller()));
// Change audio input to support noise cancellation. Delegate should be
// registered now.
fake_cras_audio_client()->SetNoiseCancellationSupported(true);
cras_audio_handler()->RequestNoiseCancellationSupported(base::DoNothing());
ChangeAudioInput(kInternalMic_NC);
EXPECT_TRUE(effects_manager.IsDelegateRegistered(audio_effects_controller()));
}
TEST_F(AudioEffectsControllerTest, StyleTransferNotSupported) {
// Change audio input to support both NC and style_transfer
ChangeAudioInput(kInternalMic_NC_ST);
// Prepare `CrasAudioHandler` to report that style transfer is
// not-supported.
fake_cras_audio_client()->SetStyleTransferSupported(false);
cras_audio_handler()->RequestStyleTransferSupported(base::DoNothing());
SimulateUserLogin("testuser1@gmail.com");
// `AudioEffectsController` reports that style transfer is not-supported.
EXPECT_FALSE(audio_effects_controller()->IsEffectSupported(
VcEffectId::kStyleTransfer));
EXPECT_FALSE(
audio_effects_controller()->GetEffectById(VcEffectId::kStyleTransfer));
}
TEST_F(AudioEffectsControllerTest, StyleTransferSupported) {
// Change audio input to support both NC and style_transfer
ChangeAudioInput(kInternalMic_NC_ST);
// Prepare `CrasAudioHandler` to report that style transfer is supported.
fake_cras_audio_client()->SetStyleTransferSupported(true);
cras_audio_handler()->RequestStyleTransferSupported(base::DoNothing());
SimulateUserLogin("testuser1@gmail.com");
// `AudioEffectsController` reports thatstyle transfer is supported.
EXPECT_TRUE(audio_effects_controller()->IsEffectSupported(
VcEffectId::kStyleTransfer));
EXPECT_TRUE(
audio_effects_controller()->GetEffectById(VcEffectId::kStyleTransfer));
// Makes sure the dependency flag is set when the effect is supported.
auto* effect =
audio_effects_controller()->GetEffectById(VcEffectId::kStyleTransfer);
EXPECT_EQ(VcHostedEffect::ResourceDependency::kMicrophone,
effect->dependency_flags());
// Delegate should be registered.
EXPECT_TRUE(VideoConferenceTrayController::Get()
->GetEffectsManager()
.IsDelegateRegistered(audio_effects_controller()));
}
TEST_F(AudioEffectsControllerTest, StyleTransferSupportedWithoutNC) {
// Change audio input to support both NC and style_transfer
ChangeAudioInput(kInternalMic_ST);
// Prepare `CrasAudioHandler` to report that style transfer is supported.
fake_cras_audio_client()->SetStyleTransferSupported(true);
cras_audio_handler()->RequestStyleTransferSupported(base::DoNothing());
SimulateUserLogin("testuser1@gmail.com");
// `AudioEffectsController` reports thatstyle transfer is supported.
EXPECT_TRUE(audio_effects_controller()->IsEffectSupported(
VcEffectId::kStyleTransfer));
EXPECT_TRUE(
audio_effects_controller()->GetEffectById(VcEffectId::kStyleTransfer));
// Makes sure the dependency flag is set when the effect is supported.
auto* effect =
audio_effects_controller()->GetEffectById(VcEffectId::kStyleTransfer);
EXPECT_EQ(VcHostedEffect::ResourceDependency::kMicrophone,
effect->dependency_flags());
// Delegate should be registered.
EXPECT_TRUE(VideoConferenceTrayController::Get()
->GetEffectsManager()
.IsDelegateRegistered(audio_effects_controller()));
}
TEST_F(AudioEffectsControllerTest,
StyleTransferEnableDisableFromCrasAudioHandler) {
// Change audio input to support both NC and style_transfer
ChangeAudioInput(kInternalMic_NC_ST);
// Prepare `CrasAudioHandler` to report that style transfer is supported.
fake_cras_audio_client()->SetStyleTransferSupported(true);
cras_audio_handler()->RequestStyleTransferSupported(base::DoNothing());
SimulateUserLogin("testuser1@gmail.com");
// Explicitly disable style transfer.
cras_audio_handler()->SetStyleTransferState(false);
std::optional<int> effect_state =
audio_effects_controller()->GetEffectState(VcEffectId::kStyleTransfer);
EXPECT_TRUE(effect_state.has_value());
EXPECT_EQ(effect_state, 0);
// Explicitly enable style transfer.
cras_audio_handler()->SetStyleTransferState(true);
effect_state =
audio_effects_controller()->GetEffectState(VcEffectId::kStyleTransfer);
EXPECT_TRUE(effect_state.has_value());
EXPECT_EQ(effect_state, 1);
// Explicitly disable style transfer.
cras_audio_handler()->SetStyleTransferState(false);
effect_state =
audio_effects_controller()->GetEffectState(VcEffectId::kStyleTransfer);
EXPECT_TRUE(effect_state.has_value());
EXPECT_EQ(effect_state, 0);
}
TEST_F(AudioEffectsControllerTest,
StyleTransferEnableDisableFromAudioEffectsController) {
// Change audio input to support both NC and style_transfer
ChangeAudioInput(kInternalMic_NC_ST);
// Prepare noise cancellation support.
fake_cras_audio_client()->SetNoiseCancellationSupported(true);
cras_audio_handler()->RequestNoiseCancellationSupported(base::DoNothing());
SimulateUserLogin("testuser1@gmail.com");
// Explicitly disable style transfer.
cras_audio_handler()->SetStyleTransferState(false);
std::optional<int> effect_state =
audio_effects_controller()->GetEffectState(VcEffectId::kStyleTransfer);
EXPECT_TRUE(effect_state.has_value());
EXPECT_EQ(effect_state, 0);
// User pressed the style transfer toggle.
audio_effects_controller()->OnEffectControlActivated(
VcEffectId::kStyleTransfer, std::nullopt);
// CrasAudioHandler should return true.
EXPECT_TRUE(cras_audio_handler()->GetStyleTransferState());
// AudioEffectsController should return true.
effect_state =
audio_effects_controller()->GetEffectState(VcEffectId::kStyleTransfer);
EXPECT_TRUE(effect_state.has_value());
EXPECT_EQ(effect_state, 1);
// User pressed the style transfer toggle.
audio_effects_controller()->OnEffectControlActivated(
VcEffectId::kStyleTransfer, std::nullopt);
// CrasAudioHandler should return false.
EXPECT_FALSE(cras_audio_handler()->GetStyleTransferState());
// AudioEffectsController should return false.
effect_state =
audio_effects_controller()->GetEffectState(VcEffectId::kStyleTransfer);
EXPECT_TRUE(effect_state.has_value());
EXPECT_EQ(effect_state, 0);
}
} // namespace ash