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
ash / system / privacy / screen_security_controller_unittest.cc [blame]
// Copyright 2018 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/screen_security_controller.h"
#include "ash/constants/ash_constants.h"
#include "ash/constants/ash_features.h"
#include "ash/root_window_controller.h"
#include "ash/shell.h"
#include "ash/system/notification_center/notification_center_tray.h"
#include "ash/system/privacy/privacy_indicators_tray_item_view.h"
#include "ash/system/privacy/screen_security_controller.h"
#include "ash/system/status_area_widget.h"
#include "ash/system/system_notification_controller.h"
#include "ash/system/tray/system_tray_notifier.h"
#include "ash/system/unified/unified_system_tray.h"
#include "ash/test/ash_test_base.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/test/scoped_feature_list.h"
#include "ui/color/color_id.h"
#include "ui/message_center/message_center.h"
namespace ash {
namespace {
message_center::Notification* FindNotification(const std::string& id) {
return message_center::MessageCenter::Get()->FindVisibleNotificationById(id);
}
// Check the visibility of privacy indicators in all displays.
void ExpectPrivacyIndicatorsVisible(bool visible) {
for (ash::RootWindowController* root_window_controller :
ash::Shell::Get()->GetAllRootWindowControllers()) {
auto* privacy_indicators_view =
root_window_controller->GetStatusAreaWidget()
->notification_center_tray()
->privacy_indicators_view();
EXPECT_EQ(privacy_indicators_view->GetVisible(), visible);
}
}
} // namespace
class ScreenSecurityControllerTest : public AshTestBase {
public:
ScreenSecurityControllerTest() = default;
ScreenSecurityControllerTest(const ScreenSecurityControllerTest&) = delete;
ScreenSecurityControllerTest& operator=(const ScreenSecurityControllerTest&) =
delete;
~ScreenSecurityControllerTest() override = default;
// AppAccessNotifierBaseTest:
void SetUp() override {
// This class is used only when video conference feature is not available.
scoped_feature_list_.InitAndDisableFeature(
features::kFeatureManagementVideoConference);
AshTestBase::SetUp();
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
// Tests that `StopAllSessions()` is working properly with both params.
TEST_F(ScreenSecurityControllerTest, StopAllSessions) {
bool stop_callback_called = false;
auto stop_callback = base::BindRepeating(
[](bool* stop_callback_called) { *stop_callback_called = true; },
base::Unretained(&stop_callback_called));
Shell::Get()->system_tray_notifier()->NotifyScreenAccessStart(
stop_callback, base::RepeatingClosure(), std::u16string());
Shell::Get()
->system_notification_controller()
->screen_security_controller()
->StopAllSessions(/*is_screen_access=*/true);
EXPECT_TRUE(stop_callback_called);
stop_callback_called = false;
Shell::Get()->system_tray_notifier()->NotifyRemotingScreenShareStart(
stop_callback);
Shell::Get()
->system_notification_controller()
->screen_security_controller()
->StopAllSessions(/*is_screen_access=*/false);
EXPECT_TRUE(stop_callback_called);
}
TEST_F(ScreenSecurityControllerTest, ShowScreenCaptureNotification) {
Shell::Get()->system_tray_notifier()->NotifyScreenAccessStart(
base::DoNothing(), base::RepeatingClosure(), std::u16string());
EXPECT_TRUE(FindNotification(kScreenAccessNotificationId));
Shell::Get()->system_tray_notifier()->NotifyScreenAccessStop();
EXPECT_FALSE(FindNotification(kScreenAccessNotificationId));
}
TEST_F(ScreenSecurityControllerTest, ShowScreenShareNotification) {
Shell::Get()->system_tray_notifier()->NotifyRemotingScreenShareStart(
base::DoNothing());
EXPECT_TRUE(FindNotification(kRemotingScreenShareNotificationId));
Shell::Get()->system_tray_notifier()->NotifyRemotingScreenShareStop();
EXPECT_FALSE(FindNotification(kRemotingScreenShareNotificationId));
}
// Tests that `NotifyRemotingScreenShareStop()` does not crash if called with no
// notification with VideoConference enabled and disabled.
TEST_F(ScreenSecurityControllerTest, NotifyScreenShareStopNoNotification) {
Shell::Get()->system_tray_notifier()->NotifyRemotingScreenShareStop();
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeature(
features::kFeatureManagementVideoConference);
Shell::Get()->system_tray_notifier()->NotifyRemotingScreenShareStop();
}
// Tests that screen share notifications do not show when VideoConference is
// enabled.
TEST_F(ScreenSecurityControllerTest,
NoScreenShareNotificationWithVideoConference) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeature(
features::kFeatureManagementVideoConference);
Shell::Get()->system_tray_notifier()->NotifyRemotingScreenShareStart(
base::DoNothing());
EXPECT_FALSE(FindNotification(kRemotingScreenShareNotificationId));
}
// Tests that calling `NotifyScreenAccessStop()` does not crash if called with
// no notification with VideoConference enabled and disabled.
TEST_F(ScreenSecurityControllerTest, NotifyScreenCaptureStopNoNotification) {
Shell::Get()->system_tray_notifier()->NotifyScreenAccessStop();
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeature(
features::kFeatureManagementVideoConference);
Shell::Get()->system_tray_notifier()->NotifyScreenAccessStop();
}
// Tests that screen capture notifications do not show with video conference
// enabled.
TEST_F(ScreenSecurityControllerTest,
ScreenCaptureShowsNotificationWithVideoConference) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeature(
features::kFeatureManagementVideoConference);
Shell::Get()->system_tray_notifier()->NotifyScreenAccessStart(
base::DoNothing(), base::RepeatingClosure(), std::u16string());
EXPECT_FALSE(FindNotification(kScreenAccessNotificationId));
}
TEST_F(ScreenSecurityControllerTest,
DoNotShowScreenCaptureNotificationWhenCasting) {
Shell::Get()->OnCastingSessionStartedOrStopped(true /* started */);
Shell::Get()->system_tray_notifier()->NotifyScreenAccessStart(
base::DoNothing(), base::RepeatingClosure(), std::u16string());
EXPECT_FALSE(FindNotification(kScreenAccessNotificationId));
Shell::Get()->system_tray_notifier()->NotifyScreenAccessStop();
Shell::Get()->OnCastingSessionStartedOrStopped(false /* started */);
EXPECT_FALSE(FindNotification(kScreenAccessNotificationId));
}
// Tests that the screen share notification is created with proper metadata when
// the `SystemTrayNotifier` notifies observers of screen share start.
TEST_F(ScreenSecurityControllerTest, ScreenShareNotification) {
Shell::Get()->system_tray_notifier()->NotifyRemotingScreenShareStart(
base::DoNothing());
auto* notification = FindNotification(kRemotingScreenShareNotificationId);
EXPECT_TRUE(notification);
// Notification should have the correct notifier id so that it will be grouped
// with other privacy indicators notification.
EXPECT_EQ(kPrivacyIndicatorsNotifierId, notification->notifier_id().id);
EXPECT_EQ(ui::kColorAshPrivacyIndicatorsBackground,
notification->accent_color_id());
}
TEST_F(ScreenSecurityControllerTest, ScreenShareTrayItemIndicator) {
// Make sure the indicator shows up on multiple displays.
UpdateDisplay("400x300,400x300,400x300,400x300");
ExpectPrivacyIndicatorsVisible(/*visible=*/false);
Shell::Get()->system_tray_notifier()->NotifyScreenAccessStart(
base::DoNothing(), base::DoNothing(), std::u16string());
ExpectPrivacyIndicatorsVisible(/*visible=*/true);
Shell::Get()->system_tray_notifier()->NotifyScreenAccessStop();
ExpectPrivacyIndicatorsVisible(/*visible=*/false);
}
// Tests that the privacy indicator shows up on multiple displays, if they
// displays exist before screen share starts.
TEST_F(ScreenSecurityControllerTest, RemotingScreenShareTrayItemIndicator) {
// Make sure the indicator shows up on multiple displays.
UpdateDisplay("400x300,400x300,400x300,400x300");
ExpectPrivacyIndicatorsVisible(/*visible=*/false);
Shell::Get()->system_tray_notifier()->NotifyRemotingScreenShareStart(
base::DoNothing());
ExpectPrivacyIndicatorsVisible(/*visible=*/true);
Shell::Get()->system_tray_notifier()->NotifyRemotingScreenShareStop();
ExpectPrivacyIndicatorsVisible(/*visible=*/false);
}
} // namespace ash