#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 );
}
};
TEST_F(SessionLimitNotificationControllerTest, Notification) {
EXPECT_FALSE(GetNotification());
UpdateSessionLengthLimitInMin(15);
message_center::Notification* notification = GetNotification();
EXPECT_TRUE(notification);
std::u16string first_title = notification->title();
EXPECT_TRUE(notification->rich_notification_data()
.should_make_spoken_feedback_for_popup_updates);
UpdateSessionLengthLimitInMin(10);
notification = GetNotification();
EXPECT_TRUE(notification);
EXPECT_NE(first_title, notification->title());
EXPECT_FALSE(notification->rich_notification_data()
.should_make_spoken_feedback_for_popup_updates);
UpdateSessionLengthLimitInMin(3);
notification = GetNotification();
EXPECT_TRUE(notification);
EXPECT_TRUE(notification->rich_notification_data()
.should_make_spoken_feedback_for_popup_updates);
UpdateSessionLengthLimitInMin(15);
notification = GetNotification();
EXPECT_TRUE(notification);
EXPECT_TRUE(notification->rich_notification_data()
.should_make_spoken_feedback_for_popup_updates);
ClearSessionLengthLimit();
EXPECT_FALSE(GetNotification());
}
TEST_F(SessionLimitNotificationControllerTest, FarOffNotificationHidden) {
EXPECT_FALSE(GetNotification());
UpdateSessionLengthLimitInMin(kNotificationThresholdInMinutes + 10);
EXPECT_FALSE(GetNotification());
RemoveNotification();
}
TEST_F(SessionLimitNotificationControllerTest,
NotificationShownAfterThreshold) {
UpdateSessionLengthLimitInMin(kNotificationThresholdInMinutes + 10);
EXPECT_FALSE(GetNotification());
UpdateSessionLengthLimitInMin(kNotificationThresholdInMinutes - 1);
EXPECT_TRUE(GetNotification());
RemoveNotification();
}
TEST_F(SessionLimitNotificationControllerTest, RemoveNotification) {
UpdateSessionLengthLimitInMin(15);
EXPECT_TRUE(GetNotification());
RemoveNotification();
EXPECT_FALSE(GetNotification());
UpdateSessionLengthLimitInMin(10);
EXPECT_FALSE(GetNotification());
UpdateSessionLengthLimitInMin(3);
message_center::Notification* notification = GetNotification();
EXPECT_TRUE(notification);
EXPECT_TRUE(notification->rich_notification_data()
.should_make_spoken_feedback_for_popup_updates);
RemoveNotification();
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);
EXPECT_FALSE(GetNotification());
CreateUserSessions(1);
EXPECT_TRUE(GetNotification());
RemoveNotification();
}
TEST_F(SessionLimitNotificationControllerLoginTest,
FarOffNotificationHiddenAfterLogin) {
UpdateSessionLengthLimitInMin(kNotificationThresholdInMinutes + 10);
CreateUserSessions(1);
EXPECT_FALSE(GetNotification());
RemoveNotification();
}
}