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
ash / system / notification_center / inactive_user_notification_blocker_unittest.cc [blame]
// Copyright 2014 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/notification_center/inactive_user_notification_blocker.h"
#include "ash/constants/notifier_catalogs.h"
#include "ash/public/cpp/test/shell_test_api.h"
#include "ash/session/test_session_controller_client.h"
#include "ash/shell.h"
#include "ash/system/notification_center/message_center_controller.h"
#include "ash/test/ash_test_base.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "components/account_id/account_id.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/public/cpp/notification.h"
namespace ash {
namespace {
using base::UTF8ToUTF16;
const char kNotifierSystemPriority[] = "ash.some-high-priority-component";
class InactiveUserNotificationBlockerTest
: public NoSessionAshTestBase,
public message_center::NotificationBlocker::Observer {
public:
InactiveUserNotificationBlockerTest() = default;
InactiveUserNotificationBlockerTest(
const InactiveUserNotificationBlockerTest&) = delete;
InactiveUserNotificationBlockerTest& operator=(
const InactiveUserNotificationBlockerTest&) = delete;
~InactiveUserNotificationBlockerTest() override = default;
// AshTestBase overrides:
void SetUp() override {
AshTestBase::SetUp();
blocker_ = ShellTestApi()
.message_center_controller()
->inactive_user_notification_blocker_for_testing();
blocker_->AddObserver(this);
}
void TearDown() override {
blocker_->RemoveObserver(this);
AshTestBase::TearDown();
}
// message_center::NotificationBlocker::Observer ovverrides:
void OnBlockingStateChanged(
message_center::NotificationBlocker* blocker) override {
state_changed_count_++;
}
protected:
const std::string GetDefaultUserId() { return "user0@tray"; }
void AddUserSession(const std::string& email) {
GetSessionControllerClient()->AddUserSession(email);
}
void SwitchActiveUser(const std::string& email) {
const AccountId account_id(AccountId::FromUserEmail(email));
GetSessionControllerClient()->SwitchActiveUser(account_id);
}
int GetStateChangedCountAndReset() {
int result = state_changed_count_;
state_changed_count_ = 0;
return result;
}
bool ShouldShowAsPopup(const message_center::NotifierId& notifier_id,
const std::string& profile_id) {
message_center::NotifierId id_with_profile = notifier_id;
id_with_profile.profile_id = profile_id;
message_center::Notification notification(
message_center::NOTIFICATION_TYPE_SIMPLE, "popup-id", u"popup-title",
u"popup-message", ui::ImageModel(), u"popup-source", GURL(),
id_with_profile, message_center::RichNotificationData(), nullptr);
if (notifier_id.id == kNotifierSystemPriority)
notification.set_priority(message_center::SYSTEM_PRIORITY);
return blocker_->ShouldShowNotificationAsPopup(notification);
}
bool ShouldShow(const message_center::NotifierId& notifier_id,
const std::string& profile_id) {
message_center::NotifierId id_with_profile = notifier_id;
id_with_profile.profile_id = profile_id;
message_center::Notification notification(
message_center::NOTIFICATION_TYPE_SIMPLE, "notification-id",
u"notification-title", u"notification-message", ui::ImageModel(),
u"notification-source", GURL(), id_with_profile,
message_center::RichNotificationData(), nullptr);
if (notifier_id.id == kNotifierSystemPriority)
notification.set_priority(message_center::SYSTEM_PRIORITY);
return blocker_->ShouldShowNotification(notification);
}
private:
int state_changed_count_ = 0;
raw_ptr<InactiveUserNotificationBlocker, DanglingUntriaged> blocker_ =
nullptr;
};
TEST_F(InactiveUserNotificationBlockerTest, Basic) {
message_center::NotifierId notifier_id(
message_center::NotifierType::APPLICATION, "test-app");
// System priority notifiers should always show regardless of fullscreen
// or lock state.
message_center::NotifierId ash_system_notifier(
message_center::NotifierType::SYSTEM_COMPONENT, kNotifierSystemPriority,
NotificationCatalogName::kTestCatalogName);
// Other system notifiers should be treated as same as a normal notifier.
message_center::NotifierId random_system_notifier(
message_center::NotifierType::SYSTEM_COMPONENT,
"ash.some-other-component", NotificationCatalogName::kTestCatalogName);
// Notifications are not blocked before login.
const std::string kEmptyUserId;
EXPECT_TRUE(ShouldShow(notifier_id, kEmptyUserId));
EXPECT_TRUE(ShouldShow(ash_system_notifier, kEmptyUserId));
EXPECT_TRUE(ShouldShow(random_system_notifier, kEmptyUserId));
// Login triggers blocking state change.
SimulateUserLogin(GetDefaultUserId());
EXPECT_EQ(1, GetStateChangedCountAndReset());
// Notifications for a single user session are not blocked.
EXPECT_TRUE(ShouldShow(ash_system_notifier, GetDefaultUserId()));
EXPECT_TRUE(ShouldShow(notifier_id, GetDefaultUserId()));
EXPECT_TRUE(ShouldShow(random_system_notifier, GetDefaultUserId()));
// Notifications from a user other than the active one (in this case, default)
// are generally blocked unless they're ash system notifications.
AddUserSession("user1@tray");
EXPECT_EQ(0, GetStateChangedCountAndReset());
const std::string kInvalidUserId("invalid");
EXPECT_FALSE(ShouldShowAsPopup(notifier_id, kInvalidUserId));
EXPECT_TRUE(ShouldShowAsPopup(ash_system_notifier, kEmptyUserId));
EXPECT_FALSE(ShouldShowAsPopup(random_system_notifier, kInvalidUserId));
EXPECT_TRUE(ShouldShowAsPopup(notifier_id, GetDefaultUserId()));
EXPECT_FALSE(ShouldShowAsPopup(notifier_id, "user1@tray"));
EXPECT_TRUE(ShouldShowAsPopup(random_system_notifier, GetDefaultUserId()));
EXPECT_FALSE(ShouldShowAsPopup(random_system_notifier, "user1@tray"));
EXPECT_FALSE(ShouldShow(notifier_id, kInvalidUserId));
EXPECT_TRUE(ShouldShow(ash_system_notifier, kEmptyUserId));
EXPECT_FALSE(ShouldShow(random_system_notifier, kInvalidUserId));
EXPECT_TRUE(ShouldShow(notifier_id, GetDefaultUserId()));
EXPECT_FALSE(ShouldShow(notifier_id, "user1@tray"));
EXPECT_TRUE(ShouldShow(random_system_notifier, GetDefaultUserId()));
EXPECT_FALSE(ShouldShow(random_system_notifier, "user1@tray"));
// Activate the second user and make sure the original/default user's
// notifications are now hidden.
SwitchActiveUser("user1@tray");
EXPECT_FALSE(ShouldShowAsPopup(notifier_id, kInvalidUserId));
EXPECT_TRUE(ShouldShowAsPopup(ash_system_notifier, kEmptyUserId));
EXPECT_FALSE(ShouldShowAsPopup(random_system_notifier, kInvalidUserId));
EXPECT_FALSE(ShouldShowAsPopup(notifier_id, GetDefaultUserId()));
EXPECT_TRUE(ShouldShowAsPopup(notifier_id, "user1@tray"));
EXPECT_FALSE(ShouldShowAsPopup(random_system_notifier, GetDefaultUserId()));
EXPECT_TRUE(ShouldShowAsPopup(random_system_notifier, "user1@tray"));
EXPECT_FALSE(ShouldShow(notifier_id, kInvalidUserId));
EXPECT_TRUE(ShouldShow(ash_system_notifier, kEmptyUserId));
EXPECT_FALSE(ShouldShow(random_system_notifier, kInvalidUserId));
EXPECT_FALSE(ShouldShow(notifier_id, GetDefaultUserId()));
EXPECT_TRUE(ShouldShow(notifier_id, "user1@tray"));
EXPECT_FALSE(ShouldShow(random_system_notifier, GetDefaultUserId()));
EXPECT_TRUE(ShouldShow(random_system_notifier, "user1@tray"));
// Switch back and verify the active user's notifications are once again
// shown.
SwitchActiveUser(GetDefaultUserId());
EXPECT_FALSE(ShouldShowAsPopup(notifier_id, kInvalidUserId));
EXPECT_TRUE(ShouldShowAsPopup(ash_system_notifier, kEmptyUserId));
EXPECT_FALSE(ShouldShowAsPopup(random_system_notifier, kInvalidUserId));
EXPECT_TRUE(ShouldShowAsPopup(notifier_id, GetDefaultUserId()));
EXPECT_FALSE(ShouldShowAsPopup(notifier_id, "user1@tray"));
EXPECT_TRUE(ShouldShowAsPopup(random_system_notifier, GetDefaultUserId()));
EXPECT_FALSE(ShouldShowAsPopup(random_system_notifier, "user1@tray"));
EXPECT_FALSE(ShouldShow(notifier_id, kInvalidUserId));
EXPECT_TRUE(ShouldShow(ash_system_notifier, kEmptyUserId));
EXPECT_FALSE(ShouldShow(random_system_notifier, kInvalidUserId));
EXPECT_TRUE(ShouldShow(notifier_id, GetDefaultUserId()));
EXPECT_FALSE(ShouldShow(notifier_id, "user1@tray"));
EXPECT_TRUE(ShouldShow(random_system_notifier, GetDefaultUserId()));
EXPECT_FALSE(ShouldShow(random_system_notifier, "user1@tray"));
}
} // namespace
} // namespace ash