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
ash / system / privacy / privacy_indicators_controller_unittest.cc [blame]
// Copyright 2022 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/privacy/privacy_indicators_controller.h"
#include <memory>
#include <string>
#include "ash/constants/ash_features.h"
#include "ash/public/cpp/ash_view_ids.h"
#include "ash/public/cpp/session/session_types.h"
#include "ash/root_window_controller.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/notification_center/ash_message_popup_collection.h"
#include "ash/system/notification_center/message_center_utils.h"
#include "ash/system/notification_center/notification_center_tray.h"
#include "ash/system/notification_center/views/message_view_container.h"
#include "ash/system/notification_center/views/notification_center_view.h"
#include "ash/system/notification_center/views/notification_list_view.h"
#include "ash/system/privacy/privacy_indicators_tray_item_view.h"
#include "ash/system/unified/unified_system_tray.h"
#include "ash/system/video_conference/fake_video_conference_tray_controller.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/ash_test_util.h"
#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/memory/weak_ptr.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "chromeos/ash/components/audio/cras_audio_handler.h"
#include "components/session_manager/session_manager_types.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/events/test/event_generator.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/public/cpp/message_center_constants.h"
#include "ui/message_center/views/message_view.h"
#include "ui/message_center/views/notification_view_base.h"
#include "ui/views/widget/widget_utils.h"
namespace ash {
namespace {
void SetSessionState(session_manager::SessionState state) {
ash::SessionInfo info;
info.state = state;
ash::Shell::Get()->session_controller()->SetSessionInfo(info);
}
class TestDelegate : public PrivacyIndicatorsNotificationDelegate {
public:
explicit TestDelegate(bool has_launch_settings_callback = true) {
if (has_launch_settings_callback) {
SetLaunchSettingsCallback(
base::BindRepeating(&TestDelegate::LaunchAppSettings,
weak_pointer_factory_.GetWeakPtr()));
}
}
TestDelegate(const TestDelegate&) = delete;
TestDelegate& operator=(const TestDelegate&) = delete;
void LaunchAppSettings() { launch_settings_called_ = true; }
bool launch_settings_called() { return launch_settings_called_; }
private:
~TestDelegate() override = default;
bool launch_settings_called_ = false;
base::WeakPtrFactory<TestDelegate> weak_pointer_factory_{this};
};
void ExpectPrivacyIndicatorsTrayItemVisible(bool visible,
bool camera_visible,
bool microphone_visible) {
for (auto* root_window_controller :
ash::Shell::Get()->GetAllRootWindowControllers()) {
StatusAreaWidget* status_area_widget =
root_window_controller->GetStatusAreaWidget();
PrivacyIndicatorsTrayItemView* privacy_indicators_view =
status_area_widget->notification_center_tray()
->privacy_indicators_view();
ASSERT_TRUE(privacy_indicators_view);
EXPECT_EQ(visible, privacy_indicators_view->GetVisible());
if (visible) {
EXPECT_EQ(camera_visible,
privacy_indicators_view->camera_icon()->GetVisible());
EXPECT_EQ(microphone_visible,
privacy_indicators_view->microphone_icon()->GetVisible());
}
}
}
} // namespace
class PrivacyIndicatorsControllerTest
: public AshTestBase,
public testing::WithParamInterface<bool> {
public:
PrivacyIndicatorsControllerTest()
: AshTestBase(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {
scoped_feature_list_.InitWithFeatureState(features::kOngoingProcesses,
AreOngoingProcessesEnabled());
}
// Get the notification view from message center associated with `id`.
const message_center::MessageView* GetMessageViewFromMessageCenter(
const std::string& id) {
NotificationCenterTray* notification_center_tray =
Shell::GetPrimaryRootWindowController()
->GetStatusAreaWidget()
->notification_center_tray();
notification_center_tray->ShowBubble();
if (features::AreOngoingProcessesEnabled()) {
return notification_center_tray->bubble()
->GetOngoingProcessMessageViewContainerById(id)
->message_view();
}
return notification_center_tray->GetNotificationListView()
->GetMessageViewForNotificationId(id);
}
// Get the popup notification view associated with `id`.
views::View* GetPopupNotificationView(const std::string& id) {
return GetPrimaryNotificationCenterTray()
->popup_collection()
->GetMessageViewForNotificationId(id);
}
void ClickPrimaryNotificationButton(const std::string& id) {
if (features::AreOngoingProcessesEnabled()) {
auto* notification_view = GetMessageViewFromMessageCenter(id);
ASSERT_TRUE(notification_view);
auto* button_view = notification_view->GetViewByID(
VIEW_ID_ONGOING_PROCESS_PRIMARY_ICON_BUTTON);
ASSERT_TRUE(button_view);
LeftClickOn(button_view);
return;
}
auto* notification_view = GetMessageViewFromMessageCenter(id);
ASSERT_TRUE(notification_view);
auto* action_buttons = notification_view->GetViewByID(
message_center::NotificationViewBase::kActionButtonsRow);
ASSERT_TRUE(action_buttons);
auto* button_view = action_buttons->children()[0].get();
ASSERT_TRUE(button_view);
LeftClickOn(button_view);
}
PrivacyIndicatorsTrayItemView* GetPrimaryDisplayPrivacyIndicatorsView()
const {
return Shell::GetPrimaryRootWindowController()
->GetStatusAreaWidget()
->notification_center_tray()
->privacy_indicators_view();
}
bool AreOngoingProcessesEnabled() { return GetParam(); }
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
INSTANTIATE_TEST_SUITE_P(All,
PrivacyIndicatorsControllerTest,
/*are_ongoing_processes_enabled=*/testing::Bool());
TEST_P(PrivacyIndicatorsControllerTest, NotificationMetadata) {
std::string app_id = "test_app_id";
std::u16string app_name = u"test_app_name";
std::string notification_id = GetPrivacyIndicatorsNotificationId(app_id);
scoped_refptr<TestDelegate> delegate = base::MakeRefCounted<TestDelegate>();
PrivacyIndicatorsController::Get()->UpdatePrivacyIndicators(
app_id, app_name, /*is_camera_used=*/true,
/*is_microphone_used=*/true, delegate, PrivacyIndicatorsSource::kApps);
auto* notification =
message_center::MessageCenter::Get()->FindNotificationById(
notification_id);
// Notification message should contains app name.
EXPECT_TRUE(base::Contains((notification->title()), app_name));
// Privacy indicators notification should not be a popup. It is silently added
// to the tray.
EXPECT_FALSE(GetPopupNotificationView(notification_id));
}
TEST_P(PrivacyIndicatorsControllerTest, NotificationWithNoButton) {
std::string app_id = "test_app_id";
std::string notification_id = GetPrivacyIndicatorsNotificationId(app_id);
scoped_refptr<TestDelegate> delegate = base::MakeRefCounted<TestDelegate>(
/*has_launch_settings_callback=*/false);
PrivacyIndicatorsController::Get()->UpdatePrivacyIndicators(
app_id, u"test_app_name",
/*is_camera_used=*/true,
/*is_microphone_used=*/true, delegate, PrivacyIndicatorsSource::kApps);
auto* notification =
message_center::MessageCenter::Get()->FindNotificationById(
notification_id);
// With the delegate don't provide any callbacks, the notification
// should have one button for launching the app.
EXPECT_EQ(0u, notification->buttons().size());
}
TEST_P(PrivacyIndicatorsControllerTest,
NotificationClickWithLaunchSettingsButton) {
std::string app_id = "test_app_id";
std::string notification_id = GetPrivacyIndicatorsNotificationId(app_id);
scoped_refptr<TestDelegate> delegate = base::MakeRefCounted<TestDelegate>(
/*has_launch_settings_callback=*/true);
PrivacyIndicatorsController::Get()->UpdatePrivacyIndicators(
app_id, u"test_app_name",
/*is_camera_used=*/true,
/*is_microphone_used=*/true, delegate, PrivacyIndicatorsSource::kApps);
auto* notification =
message_center::MessageCenter::Get()->FindNotificationById(
notification_id);
// With the delegate provides only launch settings callbacks, the notification
// should have one button for launching the app settings.
auto buttons = notification->buttons();
ASSERT_EQ(1u, buttons.size());
// Clicking that button will trigger launching the app settings.
EXPECT_FALSE(delegate->launch_settings_called());
ClickPrimaryNotificationButton(notification_id);
EXPECT_TRUE(delegate->launch_settings_called());
}
TEST_P(PrivacyIndicatorsControllerTest, NotificationClickBody) {
std::string app_id = "test_app_id";
std::string notification_id = GetPrivacyIndicatorsNotificationId(app_id);
// Create a notification without a launch settings callback.
scoped_refptr<TestDelegate> delegate = base::MakeRefCounted<TestDelegate>(
/*has_launch_settings_callback=*/false);
PrivacyIndicatorsController::Get()->UpdatePrivacyIndicators(
app_id, u"test_app_name",
/*is_camera_used=*/true,
/*is_microphone_used=*/true, delegate, PrivacyIndicatorsSource::kApps);
auto* notification_view = GetMessageViewFromMessageCenter(notification_id);
ASSERT_TRUE(notification_view);
// Clicking the notification body without a launch settings callback will not
// do anything.
EXPECT_FALSE(delegate->launch_settings_called());
LeftClickOn(notification_view);
EXPECT_FALSE(delegate->launch_settings_called());
// Update the notification so it has a launch settings callback.
scoped_refptr<TestDelegate> delegate_with_settings_callback =
base::MakeRefCounted<TestDelegate>(
/*has_launch_settings_callback=*/true);
PrivacyIndicatorsController::Get()->UpdatePrivacyIndicators(
app_id, u"test_app_name",
/*is_camera_used=*/true,
/*is_microphone_used=*/true, delegate_with_settings_callback,
PrivacyIndicatorsSource::kApps);
// Clicking the notification body with a launch settings callback should
// launch the app settings.
EXPECT_FALSE(delegate_with_settings_callback->launch_settings_called());
LeftClickOn(notification_view);
EXPECT_TRUE(delegate_with_settings_callback->launch_settings_called());
}
// Tests that privacy indicators notifications are working properly when there
// are two running apps.
TEST_P(PrivacyIndicatorsControllerTest, NotificationWithTwoApps) {
std::string app_id1 = "test_app_id1";
std::string app_id2 = "test_app_id2";
std::string notification_id1 = GetPrivacyIndicatorsNotificationId(app_id1);
std::string notification_id2 = GetPrivacyIndicatorsNotificationId(app_id2);
scoped_refptr<TestDelegate> delegate = base::MakeRefCounted<TestDelegate>();
PrivacyIndicatorsController::Get()->UpdatePrivacyIndicators(
app_id1, u"test_app_name",
/*is_camera_used=*/true,
/*is_microphone_used=*/true, delegate, PrivacyIndicatorsSource::kApps);
PrivacyIndicatorsController::Get()->UpdatePrivacyIndicators(
app_id2, u"test_app_name",
/*is_camera_used=*/true,
/*is_microphone_used=*/true, delegate, PrivacyIndicatorsSource::kApps);
auto* message_center = message_center::MessageCenter::Get();
EXPECT_TRUE(message_center->FindNotificationById(notification_id1));
EXPECT_TRUE(message_center->FindNotificationById(notification_id2));
// A group parent notification should also be created for these 2
// notifications.
std::string id_parent =
notification_id1 +
message_center_utils::GenerateGroupParentNotificationIdSuffix(
message_center->FindNotificationById(notification_id1)
->notifier_id());
EXPECT_TRUE(message_center->FindNotificationById(id_parent));
// Update the state. All notifications should be removed.
PrivacyIndicatorsController::Get()->UpdatePrivacyIndicators(
app_id1, u"test_app_name",
/*is_camera_used=*/false,
/*is_microphone_used=*/false, delegate, PrivacyIndicatorsSource::kApps);
// Fast forward by the minimum duration the privacy indicator should be held.
task_environment()->FastForwardBy(
PrivacyIndicatorsController::kPrivacyIndicatorsMinimumHoldDuration);
PrivacyIndicatorsController::Get()->UpdatePrivacyIndicators(
app_id2, u"test_app_name",
/*is_camera_used=*/false,
/*is_microphone_used=*/false, delegate, PrivacyIndicatorsSource::kApps);
// Fast forward by the minimum duration the privacy indicator should be held.
task_environment()->FastForwardBy(
PrivacyIndicatorsController::kPrivacyIndicatorsMinimumHoldDuration);
EXPECT_FALSE(message_center->FindNotificationById(notification_id1));
EXPECT_FALSE(message_center->FindNotificationById(notification_id2));
EXPECT_FALSE(message_center->FindNotificationById(id_parent));
}
// Tests privacy indicators tray item visibility across all status area widgets.
TEST_P(PrivacyIndicatorsControllerTest, PrivacyIndicatorsTrayItemView) {
// Uses normal animation duration so that the icons would not be immediately
// hidden after the animation.
ui::ScopedAnimationDurationScaleMode animation_scale(
ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
// Make sure privacy indicators work on multiple displays.
UpdateDisplay("300x200,500x400");
scoped_refptr<TestDelegate> delegate = base::MakeRefCounted<TestDelegate>();
PrivacyIndicatorsController::Get()->UpdatePrivacyIndicators(
"test_id", u"test_app_name",
/*is_camera_used=*/true,
/*is_microphone_used=*/false, delegate, PrivacyIndicatorsSource::kApps);
ExpectPrivacyIndicatorsTrayItemVisible(
/*visible=*/true, /*camera_visible=*/true, /*microphone_visible=*/false);
PrivacyIndicatorsController::Get()->UpdatePrivacyIndicators(
"test_id", u"test_app_name",
/*is_camera_used=*/true,
/*is_microphone_used=*/true, delegate, PrivacyIndicatorsSource::kApps);
ExpectPrivacyIndicatorsTrayItemVisible(
/*visible=*/true, /*camera_visible=*/true, /*microphone_visible=*/true);
PrivacyIndicatorsController::Get()->UpdatePrivacyIndicators(
"test_id", u"test_app_name",
/*is_camera_used=*/false,
/*is_microphone_used=*/false, delegate, PrivacyIndicatorsSource::kApps);
// Fast forward by the minimum duration the privacy indicator should be held.
task_environment()->FastForwardBy(
PrivacyIndicatorsController::kPrivacyIndicatorsMinimumHoldDuration);
ExpectPrivacyIndicatorsTrayItemVisible(
/*visible=*/false, /*camera_visible=*/false,
/*microphone_visible=*/false);
PrivacyIndicatorsController::Get()->UpdatePrivacyIndicators(
"test_id", u"test_app_name",
/*is_camera_used=*/false,
/*is_microphone_used=*/true, delegate, PrivacyIndicatorsSource::kApps);
ExpectPrivacyIndicatorsTrayItemVisible(
/*visible=*/true, /*camera_visible=*/false, /*microphone_visible=*/true);
}
TEST_P(PrivacyIndicatorsControllerTest, SourceMetricsCollection) {
base::HistogramTester histogram_tester;
scoped_refptr<TestDelegate> delegate = base::MakeRefCounted<TestDelegate>();
const std::string histogram_name = "Ash.PrivacyIndicators.Source";
PrivacyIndicatorsController::Get()->UpdatePrivacyIndicators(
"test_id", u"test_app_name",
/*is_camera_used=*/true,
/*is_microphone_used=*/false, delegate, PrivacyIndicatorsSource::kApps);
histogram_tester.ExpectBucketCount(histogram_name,
PrivacyIndicatorsSource::kApps, 1);
PrivacyIndicatorsController::Get()->UpdatePrivacyIndicators(
"test_id", u"test_app_name",
/*is_camera_used=*/true,
/*is_microphone_used=*/false, delegate,
PrivacyIndicatorsSource::kScreenCapture);
histogram_tester.ExpectBucketCount(
histogram_name, PrivacyIndicatorsSource::kScreenCapture, 1);
PrivacyIndicatorsController::Get()->UpdatePrivacyIndicators(
"test_id", u"test_app_name",
/*is_camera_used=*/true,
/*is_microphone_used=*/false, delegate,
PrivacyIndicatorsSource::kLinuxVm);
histogram_tester.ExpectBucketCount(histogram_name,
PrivacyIndicatorsSource::kLinuxVm, 1);
}
TEST_P(PrivacyIndicatorsControllerTest, CameraDisabledWithOneApp) {
auto* controller = PrivacyIndicatorsController::Get();
std::string app_id = "test_app_id";
std::u16string app_name = u"test_app_name";
scoped_refptr<TestDelegate> delegate = base::MakeRefCounted<TestDelegate>();
controller->UpdatePrivacyIndicators(app_id, app_name,
/*is_camera_used=*/true,
/*is_microphone_used=*/false, delegate,
PrivacyIndicatorsSource::kApps);
std::string notification_id = GetPrivacyIndicatorsNotificationId(app_id);
ASSERT_TRUE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id));
ASSERT_TRUE(GetPrimaryDisplayPrivacyIndicatorsView()->GetVisible());
// Camera is in use, but if the camera is being hardware muted, the
// notification and indicator view should not show.
controller->OnCameraHWPrivacySwitchStateChanged(
"test_device_id", cros::mojom::CameraPrivacySwitchState::ON);
EXPECT_FALSE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id));
EXPECT_FALSE(GetPrimaryDisplayPrivacyIndicatorsView()->GetVisible());
// Flip back the switch. Indicators should show again.
controller->OnCameraHWPrivacySwitchStateChanged(
"test_device_id", cros::mojom::CameraPrivacySwitchState::OFF);
EXPECT_TRUE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id));
EXPECT_TRUE(GetPrimaryDisplayPrivacyIndicatorsView()->GetVisible());
// Camera is in use, but if the camera is being software muted, the
// notification and indicator view should not show.
controller->OnCameraSWPrivacySwitchStateChanged(
cros::mojom::CameraPrivacySwitchState::ON);
EXPECT_FALSE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id));
EXPECT_FALSE(GetPrimaryDisplayPrivacyIndicatorsView()->GetVisible());
// Flip back the switch. Indicators should show again.
controller->OnCameraSWPrivacySwitchStateChanged(
cros::mojom::CameraPrivacySwitchState::OFF);
EXPECT_TRUE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id));
EXPECT_TRUE(GetPrimaryDisplayPrivacyIndicatorsView()->GetVisible());
// If both camera and microphone is in use, but the camera is muted. The
// notification content (i.e. the title) should reflect that only mic is being
// in used.
controller->UpdatePrivacyIndicators(app_id, app_name,
/*is_camera_used=*/true,
/*is_microphone_used=*/true, delegate,
PrivacyIndicatorsSource::kApps);
controller->OnCameraSWPrivacySwitchStateChanged(
cros::mojom::CameraPrivacySwitchState::ON);
auto* notification =
message_center::MessageCenter::Get()->FindNotificationById(
notification_id);
EXPECT_TRUE(notification);
EXPECT_EQ(
l10n_util::GetStringFUTF16(IDS_PRIVACY_NOTIFICATION_TITLE_MIC, app_name),
notification->title());
// Flip back.
controller->OnCameraSWPrivacySwitchStateChanged(
cros::mojom::CameraPrivacySwitchState::OFF);
notification = message_center::MessageCenter::Get()->FindNotificationById(
notification_id);
EXPECT_TRUE(notification);
EXPECT_EQ(l10n_util::GetStringFUTF16(
IDS_PRIVACY_NOTIFICATION_TITLE_CAMERA_AND_MIC, app_name),
notification->title());
}
TEST_P(PrivacyIndicatorsControllerTest, MicrophoneDisabledWithOneApp) {
auto* controller = PrivacyIndicatorsController::Get();
std::string app_id = "test_app_id";
std::u16string app_name = u"test_app_name";
scoped_refptr<TestDelegate> delegate = base::MakeRefCounted<TestDelegate>();
controller->UpdatePrivacyIndicators(app_id, u"test_app_name",
/*is_camera_used=*/false,
/*is_microphone_used=*/true, delegate,
PrivacyIndicatorsSource::kApps);
std::string notification_id = GetPrivacyIndicatorsNotificationId(app_id);
ASSERT_TRUE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id));
ASSERT_TRUE(GetPrimaryDisplayPrivacyIndicatorsView()->GetVisible());
// Microphone is in use, but if the microphone is being muted, the
// notification and indicator view should not show.
CrasAudioHandler::Get()->SetInputMute(
/*mute_on=*/true,
CrasAudioHandler::InputMuteChangeMethod::kKeyboardButton);
EXPECT_FALSE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id));
EXPECT_FALSE(GetPrimaryDisplayPrivacyIndicatorsView()->GetVisible());
// Flip back the switch. Indicators should show again.
CrasAudioHandler::Get()->SetInputMute(
/*mute_on=*/false,
CrasAudioHandler::InputMuteChangeMethod::kKeyboardButton);
EXPECT_TRUE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id));
EXPECT_TRUE(GetPrimaryDisplayPrivacyIndicatorsView()->GetVisible());
// If both camera and microphone is in use, but the camera is muted. The
// notification content (i.e. the title) should reflect that only mic is being
// in used.
controller->UpdatePrivacyIndicators(app_id, u"test_app_name",
/*is_camera_used=*/true,
/*is_microphone_used=*/true, delegate,
PrivacyIndicatorsSource::kApps);
CrasAudioHandler::Get()->SetInputMute(
/*mute_on=*/true,
CrasAudioHandler::InputMuteChangeMethod::kKeyboardButton);
auto* notification =
message_center::MessageCenter::Get()->FindNotificationById(
notification_id);
EXPECT_TRUE(notification);
EXPECT_EQ(l10n_util::GetStringFUTF16(IDS_PRIVACY_NOTIFICATION_TITLE_CAMERA,
app_name),
notification->title());
// Flip back.
CrasAudioHandler::Get()->SetInputMute(
/*mute_on=*/false,
CrasAudioHandler::InputMuteChangeMethod::kKeyboardButton);
notification = message_center::MessageCenter::Get()->FindNotificationById(
notification_id);
EXPECT_TRUE(notification);
EXPECT_EQ(l10n_util::GetStringFUTF16(
IDS_PRIVACY_NOTIFICATION_TITLE_CAMERA_AND_MIC, app_name),
notification->title());
}
// When both microphone and camera is disabled, no privacy indicators should
// show for camera/microphone usage.
TEST_P(PrivacyIndicatorsControllerTest, CameraAndMicrophoneDisabledWithOneApp) {
auto* controller = PrivacyIndicatorsController::Get();
std::string app_id = "test_app_id";
scoped_refptr<TestDelegate> delegate = base::MakeRefCounted<TestDelegate>();
controller->UpdatePrivacyIndicators(app_id, u"test_app_name",
/*is_camera_used=*/true,
/*is_microphone_used=*/true, delegate,
PrivacyIndicatorsSource::kApps);
std::string notification_id = GetPrivacyIndicatorsNotificationId(app_id);
ASSERT_TRUE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id));
ASSERT_TRUE(GetPrimaryDisplayPrivacyIndicatorsView()->GetVisible());
CrasAudioHandler::Get()->SetInputMute(
/*mute_on=*/true,
CrasAudioHandler::InputMuteChangeMethod::kKeyboardButton);
controller->OnCameraSWPrivacySwitchStateChanged(
cros::mojom::CameraPrivacySwitchState::ON);
EXPECT_FALSE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id));
EXPECT_FALSE(GetPrimaryDisplayPrivacyIndicatorsView()->GetVisible());
CrasAudioHandler::Get()->SetInputMute(
/*mute_on=*/true,
CrasAudioHandler::InputMuteChangeMethod::kKeyboardButton);
controller->OnCameraHWPrivacySwitchStateChanged(
"test_device_id", cros::mojom::CameraPrivacySwitchState::ON);
EXPECT_FALSE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id));
EXPECT_FALSE(GetPrimaryDisplayPrivacyIndicatorsView()->GetVisible());
}
TEST_P(PrivacyIndicatorsControllerTest, CameraDisabledWithMultipleApps) {
auto* controller = PrivacyIndicatorsController::Get();
std::string app_id1 = "test_app_id1";
std::string app_id2 = "test_app_id2";
std::string app_id3 = "test_app_id3";
scoped_refptr<TestDelegate> delegate = base::MakeRefCounted<TestDelegate>();
controller->UpdatePrivacyIndicators(app_id1, u"test_app_name",
/*is_camera_used=*/true,
/*is_microphone_used=*/false, delegate,
PrivacyIndicatorsSource::kApps);
controller->UpdatePrivacyIndicators(app_id2, u"test_app_name",
/*is_camera_used=*/true,
/*is_microphone_used=*/false, delegate,
PrivacyIndicatorsSource::kApps);
controller->UpdatePrivacyIndicators(app_id3, u"test_app_name",
/*is_camera_used=*/false,
/*is_microphone_used=*/true, delegate,
PrivacyIndicatorsSource::kApps);
std::string notification_id1 = GetPrivacyIndicatorsNotificationId(app_id1);
std::string notification_id2 = GetPrivacyIndicatorsNotificationId(app_id2);
std::string notification_id3 = GetPrivacyIndicatorsNotificationId(app_id3);
ASSERT_TRUE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id1));
ASSERT_TRUE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id2));
ASSERT_TRUE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id3));
controller->OnCameraSWPrivacySwitchStateChanged(
cros::mojom::CameraPrivacySwitchState::ON);
EXPECT_FALSE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id1));
EXPECT_FALSE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id2));
// The app that uses microphone should not be affected.
EXPECT_TRUE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id3));
// When flip back, all old notification should be re-created.
controller->OnCameraSWPrivacySwitchStateChanged(
cros::mojom::CameraPrivacySwitchState::OFF);
EXPECT_TRUE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id1));
EXPECT_TRUE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id2));
}
TEST_P(PrivacyIndicatorsControllerTest, MicrophoneDisabledWithMultipleApps) {
auto* controller = PrivacyIndicatorsController::Get();
std::string app_id1 = "test_app_id1";
std::string app_id2 = "test_app_id2";
std::string app_id3 = "test_app_id3";
scoped_refptr<TestDelegate> delegate = base::MakeRefCounted<TestDelegate>();
controller->UpdatePrivacyIndicators(app_id1, u"test_app_name",
/*is_camera_used=*/false,
/*is_microphone_used=*/true, delegate,
PrivacyIndicatorsSource::kApps);
controller->UpdatePrivacyIndicators(app_id2, u"test_app_name",
/*is_camera_used=*/false,
/*is_microphone_used=*/true, delegate,
PrivacyIndicatorsSource::kApps);
controller->UpdatePrivacyIndicators(app_id3, u"test_app_name",
/*is_camera_used=*/true,
/*is_microphone_used=*/false, delegate,
PrivacyIndicatorsSource::kApps);
std::string notification_id1 = GetPrivacyIndicatorsNotificationId(app_id1);
std::string notification_id2 = GetPrivacyIndicatorsNotificationId(app_id2);
std::string notification_id3 = GetPrivacyIndicatorsNotificationId(app_id3);
ASSERT_TRUE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id1));
ASSERT_TRUE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id2));
ASSERT_TRUE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id3));
CrasAudioHandler::Get()->SetInputMute(
/*mute_on=*/true,
CrasAudioHandler::InputMuteChangeMethod::kKeyboardButton);
EXPECT_FALSE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id1));
EXPECT_FALSE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id2));
// The app that uses camera should not be affected.
EXPECT_TRUE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id3));
// When flip back, all old notification should be re-created.
CrasAudioHandler::Get()->SetInputMute(
/*mute_on=*/false,
CrasAudioHandler::InputMuteChangeMethod::kKeyboardButton);
EXPECT_TRUE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id1));
EXPECT_TRUE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id2));
}
TEST_P(PrivacyIndicatorsControllerTest,
HidingDelayTimerMinimumEnabledWithOneApp) {
auto* controller = PrivacyIndicatorsController::Get();
std::string app_id = "test_app_id";
scoped_refptr<TestDelegate> delegate = base::MakeRefCounted<TestDelegate>();
controller->UpdatePrivacyIndicators(app_id, u"test_app_name",
/*is_camera_used=*/true,
/*is_microphone_used=*/false, delegate,
PrivacyIndicatorsSource::kApps);
std::string notification_id1 = GetPrivacyIndicatorsNotificationId(app_id);
ASSERT_TRUE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id1));
controller->UpdatePrivacyIndicators(app_id, u"test_app_name",
/*is_camera_used=*/false,
/*is_microphone_used=*/false, delegate,
PrivacyIndicatorsSource::kApps);
EXPECT_TRUE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id1));
EXPECT_TRUE(GetPrimaryDisplayPrivacyIndicatorsView()->GetVisible());
// Fast forward by the minimum duration the privacy indicator should be held.
task_environment()->FastForwardBy(
PrivacyIndicatorsController::kPrivacyIndicatorsMinimumHoldDuration);
EXPECT_FALSE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id1));
EXPECT_FALSE(GetPrimaryDisplayPrivacyIndicatorsView()->GetVisible());
}
TEST_P(PrivacyIndicatorsControllerTest, HidingDelayTimerHoldEnabledWithOneApp) {
auto* controller = PrivacyIndicatorsController::Get();
std::string app_id = "test_app_id";
scoped_refptr<TestDelegate> delegate = base::MakeRefCounted<TestDelegate>();
controller->UpdatePrivacyIndicators(app_id, u"test_app_name",
/*is_camera_used=*/true,
/*is_microphone_used=*/false, delegate,
PrivacyIndicatorsSource::kApps);
// Fast forward by the minimum duration the privacy indicator should be held.
task_environment()->FastForwardBy(
PrivacyIndicatorsController::kPrivacyIndicatorsMinimumHoldDuration);
std::string notification_id1 = GetPrivacyIndicatorsNotificationId(app_id);
ASSERT_TRUE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id1));
controller->UpdatePrivacyIndicators(app_id, u"test_app_name",
/*is_camera_used=*/false,
/*is_microphone_used=*/false, delegate,
PrivacyIndicatorsSource::kApps);
EXPECT_TRUE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id1));
EXPECT_TRUE(GetPrimaryDisplayPrivacyIndicatorsView()->GetVisible());
// Fast forward by the after use duration the privacy indicator should be
// held.
task_environment()->FastForwardBy(
PrivacyIndicatorsController::kPrivacyIndicatorsHoldAfterUseDuration);
EXPECT_FALSE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id1));
EXPECT_FALSE(GetPrimaryDisplayPrivacyIndicatorsView()->GetVisible());
}
// Tests to make sure that privacy indicators are updated accordingly in locked
// screen.
TEST_P(PrivacyIndicatorsControllerTest, UpdateUsageStageInLockScreen) {
auto* controller = PrivacyIndicatorsController::Get();
std::string app_id = "test_app_id";
scoped_refptr<TestDelegate> delegate = base::MakeRefCounted<TestDelegate>();
SetSessionState(session_manager::SessionState::ACTIVE);
controller->UpdatePrivacyIndicators(app_id, u"test_app_name",
/*is_camera_used=*/true,
/*is_microphone_used=*/true, delegate,
PrivacyIndicatorsSource::kApps);
// Privacy indicators should show up as expected in an active session.
std::string notification_id = GetPrivacyIndicatorsNotificationId(app_id);
ASSERT_TRUE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id));
ASSERT_TRUE(GetPrimaryDisplayPrivacyIndicatorsView()->GetVisible());
// Privacy indicators should show up as expected in a locked session.
SetSessionState(session_manager::SessionState::LOCKED);
EXPECT_TRUE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id));
EXPECT_TRUE(GetPrimaryDisplayPrivacyIndicatorsView()->GetVisible());
// Update privacy indicators in a locked session. Should update accordingly.
controller->UpdatePrivacyIndicators(app_id, u"test_app_name",
/*is_camera_used=*/false,
/*is_microphone_used=*/false, delegate,
PrivacyIndicatorsSource::kApps);
// Fast forward by the minimum duration the privacy indicator should be held.
task_environment()->FastForwardBy(
PrivacyIndicatorsController::kPrivacyIndicatorsMinimumHoldDuration);
EXPECT_FALSE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id));
EXPECT_FALSE(GetPrimaryDisplayPrivacyIndicatorsView()->GetVisible());
// Indicators should still show up correctly when log back to an active
// session.
SetSessionState(session_manager::SessionState::ACTIVE);
EXPECT_FALSE(message_center::MessageCenter::Get()->FindNotificationById(
notification_id));
EXPECT_FALSE(GetPrimaryDisplayPrivacyIndicatorsView()->GetVisible());
}
// Tests enabling `kVideoConference`.
class PrivacyIndicatorsControllerVideoConferenceTest
: public AshTestBase,
public testing::WithParamInterface<bool> {
public:
PrivacyIndicatorsControllerVideoConferenceTest()
: scoped_feature_list_(features::kFeatureManagementVideoConference) {}
PrivacyIndicatorsControllerVideoConferenceTest(
const PrivacyIndicatorsControllerVideoConferenceTest&) = delete;
PrivacyIndicatorsControllerVideoConferenceTest& operator=(
const PrivacyIndicatorsControllerVideoConferenceTest&) = delete;
~PrivacyIndicatorsControllerVideoConferenceTest() override = default;
// AshTestBase:
void SetUp() override {
// Instantiates a fake controller (the real one is created in
// ChromeBrowserMainExtraPartsAsh::PreProfileInit() which is not called in
// ash unit tests).
controller_ = std::make_unique<FakeVideoConferenceTrayController>();
AshTestBase::SetUp();
}
void TearDown() override {
AshTestBase::TearDown();
controller_.reset();
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
std::unique_ptr<FakeVideoConferenceTrayController> controller_;
};
// Make sure that when `kVideoConference` is enabled, the privacy indicators
// view and the controller is not created.
TEST_F(PrivacyIndicatorsControllerVideoConferenceTest, ObjectsCreation) {
EXPECT_FALSE(PrivacyIndicatorsController::Get());
for (auto* root_window_controller :
Shell::Get()->GetAllRootWindowControllers()) {
DCHECK(root_window_controller);
auto* status_area_widget = root_window_controller->GetStatusAreaWidget();
DCHECK(status_area_widget);
auto* privacy_indicators_view =
status_area_widget->notification_center_tray()
->privacy_indicators_view();
EXPECT_FALSE(privacy_indicators_view);
}
}
} // namespace ash