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
|
// 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/session/session_limit_notification_controller.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/public/cpp/notification_types.h"
namespace ash {
class SessionLimitNotificationControllerTest : public AshTestBase {
public:
SessionLimitNotificationControllerTest() = default;
SessionLimitNotificationControllerTest(
const SessionLimitNotificationControllerTest&) = delete;
SessionLimitNotificationControllerTest& operator=(
const SessionLimitNotificationControllerTest&) = delete;
~SessionLimitNotificationControllerTest() override = default;
protected:
static const int kNotificationThresholdInMinutes = 60;
void UpdateSessionLengthLimitInMin(int mins) {
Shell::Get()->session_controller()->SetSessionLengthLimit(
base::Minutes(mins), base::Time::Now());
}
message_center::Notification* GetNotification() {
const message_center::NotificationList::Notifications& notifications =
message_center::MessageCenter::Get()->GetVisibleNotifications();
for (message_center::NotificationList::Notifications::const_iterator iter =
notifications.begin();
iter != notifications.end(); ++iter) {
if ((*iter)->id() == SessionLimitNotificationController::kNotificationId)
return *iter;
}
return nullptr;
}
void ClearSessionLengthLimit() {
Shell::Get()->session_controller()->SetSessionLengthLimit(base::TimeDelta(),
base::Time());
}
void RemoveNotification() {
message_center::MessageCenter::Get()->RemoveNotification(
SessionLimitNotificationController::kNotificationId,
false /* by_user */);
}
};
TEST_F(SessionLimitNotificationControllerTest, Notification) {
// No notifications when no session limit.
EXPECT_FALSE(GetNotification());
// Limit is 15 min.
UpdateSessionLengthLimitInMin(15);
message_center::Notification* notification = GetNotification();
EXPECT_TRUE(notification);
std::u16string first_title = notification->title();
// Should read the content.
EXPECT_TRUE(notification->rich_notification_data()
.should_make_spoken_feedback_for_popup_updates);
// Limit is 10 min.
UpdateSessionLengthLimitInMin(10);
notification = GetNotification();
EXPECT_TRUE(notification);
// The title should be updated.
EXPECT_NE(first_title, notification->title());
// Should NOT read, because just update the remaining time.
EXPECT_FALSE(notification->rich_notification_data()
.should_make_spoken_feedback_for_popup_updates);
// Limit is 3 min.
UpdateSessionLengthLimitInMin(3);
notification = GetNotification();
EXPECT_TRUE(notification);
// Should read the content again because the state has changed.
EXPECT_TRUE(notification->rich_notification_data()
.should_make_spoken_feedback_for_popup_updates);
// Session length limit is updated to longer: 15 min.
UpdateSessionLengthLimitInMin(15);
notification = GetNotification();
EXPECT_TRUE(notification);
// Should read again because an increase of the remaining time is noteworthy.
EXPECT_TRUE(notification->rich_notification_data()
.should_make_spoken_feedback_for_popup_updates);
// Clears the limit: the notification should be gone.
ClearSessionLengthLimit();
EXPECT_FALSE(GetNotification());
}
TEST_F(SessionLimitNotificationControllerTest, FarOffNotificationHidden) {
// Test that notification is not shown if the session end time is far off into
// the future, but an item should be present in system tray bubble.
// Notification should be absent.
EXPECT_FALSE(GetNotification());
UpdateSessionLengthLimitInMin(kNotificationThresholdInMinutes + 10);
EXPECT_FALSE(GetNotification());
RemoveNotification();
}
TEST_F(SessionLimitNotificationControllerTest,
NotificationShownAfterThreshold) {
// Test that a notification is shown when time runs under the notification
// display threshold.
// Start with a generous session length. We should not get a notification.
UpdateSessionLengthLimitInMin(kNotificationThresholdInMinutes + 10);
EXPECT_FALSE(GetNotification());
// Update the session length now, without changing limit_state_.
UpdateSessionLengthLimitInMin(kNotificationThresholdInMinutes - 1);
// A notification should be displayed now.
EXPECT_TRUE(GetNotification());
RemoveNotification();
}
TEST_F(SessionLimitNotificationControllerTest, RemoveNotification) {
// Limit is 15 min.
UpdateSessionLengthLimitInMin(15);
EXPECT_TRUE(GetNotification());
// Removes the notification.
RemoveNotification();
EXPECT_FALSE(GetNotification());
// Limit is 10 min. The notification should not re-appear.
UpdateSessionLengthLimitInMin(10);
EXPECT_FALSE(GetNotification());
// Limit is 3 min. The notification should re-appear and should be re-read
// because of state change.
UpdateSessionLengthLimitInMin(3);
message_center::Notification* notification = GetNotification();
EXPECT_TRUE(notification);
EXPECT_TRUE(notification->rich_notification_data()
.should_make_spoken_feedback_for_popup_updates);
RemoveNotification();
// Session length limit is updated to longer state. Notification should
// re-appear and be re-read.
UpdateSessionLengthLimitInMin(15);
notification = GetNotification();
EXPECT_TRUE(notification);
EXPECT_TRUE(notification->rich_notification_data()
.should_make_spoken_feedback_for_popup_updates);
RemoveNotification();
}
class SessionLimitNotificationControllerLoginTest
: public SessionLimitNotificationControllerTest {
public:
SessionLimitNotificationControllerLoginTest() { set_start_session(false); }
SessionLimitNotificationControllerLoginTest(
const SessionLimitNotificationControllerLoginTest&) = delete;
SessionLimitNotificationControllerLoginTest& operator=(
const SessionLimitNotificationControllerLoginTest&) = delete;
};
TEST_F(SessionLimitNotificationControllerLoginTest,
NotificationShownAfterLogin) {
UpdateSessionLengthLimitInMin(15);
// No notifications before login.
EXPECT_FALSE(GetNotification());
// Notification is shown after login.
SimulateUserLogin(kRegularUserLoginInfo);
EXPECT_TRUE(GetNotification());
RemoveNotification();
}
TEST_F(SessionLimitNotificationControllerLoginTest,
FarOffNotificationHiddenAfterLogin) {
// Test that notification is not shown if the session end time is far off into
// the future, but an item should be present in system tray bubble.
// Notification should be absent.
UpdateSessionLengthLimitInMin(kNotificationThresholdInMinutes + 10);
SimulateUserLogin(kRegularUserLoginInfo);
EXPECT_FALSE(GetNotification());
RemoveNotification();
}
} // namespace ash
|