1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/system/chromeos/session/tray_session_length_limit.h"
7 #include "ash/root_window_controller.h"
9 #include "ash/system/tray/system_tray.h"
10 #include "ash/test/ash_test_base.h"
11 #include "ash/test/test_system_tray_delegate.h"
12 #include "base/time/time.h"
13 #include "ui/message_center/message_center.h"
14 #include "ui/message_center/notification.h"
15 #include "ui/message_center/notification_types.h"
20 class TraySessionLengthLimitTest : public AshTestBase {
22 TraySessionLengthLimitTest() {}
23 virtual ~TraySessionLengthLimitTest() {}
25 virtual void SetUp() override {
27 SystemTray* system_tray =
28 Shell::GetPrimaryRootWindowController()->GetSystemTray();
29 tray_session_length_limit_ = new TraySessionLengthLimit(system_tray);
30 system_tray->AddTrayItem(tray_session_length_limit_);
33 virtual void TearDown() override {
34 AshTestBase::TearDown();
38 void UpdateSessionLengthLimitInMin(int mins) {
39 GetSystemTrayDelegate()->SetSessionLengthLimitForTest(
40 base::TimeDelta::FromMinutes(mins));
41 tray_session_length_limit_->OnSessionLengthLimitChanged();
44 message_center::Notification* GetNotification() {
45 const message_center::NotificationList::Notifications& notifications =
46 message_center::MessageCenter::Get()->GetVisibleNotifications();
47 for (message_center::NotificationList::Notifications::const_iterator iter =
48 notifications.begin(); iter != notifications.end(); ++iter) {
49 if ((*iter)->id() == TraySessionLengthLimit::kNotificationId)
55 void ClearSessionLengthLimit() {
56 GetSystemTrayDelegate()->ClearSessionLengthLimit();
57 tray_session_length_limit_->OnSessionLengthLimitChanged();
60 void RemoveNotification() {
61 message_center::MessageCenter::Get()->RemoveNotification(
62 TraySessionLengthLimit::kNotificationId, false /* by_user */);
65 TraySessionLengthLimit* tray_session_length_limit() {
66 return tray_session_length_limit_;
70 // Weak reference, owned by the SystemTray.
71 TraySessionLengthLimit* tray_session_length_limit_;
73 DISALLOW_COPY_AND_ASSIGN(TraySessionLengthLimitTest);
76 TEST_F(TraySessionLengthLimitTest, Notification) {
77 // No notifications when no session limit.
78 EXPECT_FALSE(GetNotification());
81 UpdateSessionLengthLimitInMin(15);
82 message_center::Notification* notification = GetNotification();
83 EXPECT_TRUE(notification);
84 EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority());
85 base::string16 first_content = notification->message();
86 // Should read the content.
87 EXPECT_TRUE(notification->rich_notification_data().
88 should_make_spoken_feedback_for_popup_updates);
91 UpdateSessionLengthLimitInMin(10);
92 notification = GetNotification();
93 EXPECT_TRUE(notification);
94 EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority());
95 // The content should be updated.
96 EXPECT_NE(first_content, notification->message());
97 // Should NOT read, because just update the remaining time.
98 EXPECT_FALSE(notification->rich_notification_data().
99 should_make_spoken_feedback_for_popup_updates);
102 UpdateSessionLengthLimitInMin(3);
103 notification = GetNotification();
104 EXPECT_TRUE(notification);
105 EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority());
106 // Should read the content again because the state has changed.
107 EXPECT_TRUE(notification->rich_notification_data().
108 should_make_spoken_feedback_for_popup_updates);
110 // Session length limit is updated to longer: 15 min.
111 UpdateSessionLengthLimitInMin(15);
112 notification = GetNotification();
113 EXPECT_TRUE(notification);
114 EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority());
115 // Should read again because an increase of the remaining time is noteworthy.
116 EXPECT_TRUE(notification->rich_notification_data().
117 should_make_spoken_feedback_for_popup_updates);
119 // Clears the limit: the notification should be gone.
120 ClearSessionLengthLimit();
121 EXPECT_FALSE(GetNotification());
124 TEST_F(TraySessionLengthLimitTest, RemoveNotification) {
125 message_center::Notification* notification;
128 UpdateSessionLengthLimitInMin(15);
129 EXPECT_TRUE(GetNotification());
131 // Removes the notification.
132 RemoveNotification();
133 EXPECT_FALSE(GetNotification());
135 // Limit is 10 min. The notification should not re-appear.
136 UpdateSessionLengthLimitInMin(10);
137 EXPECT_FALSE(GetNotification());
139 // Limit is 3 min. The notification should re-appear and should be re-read
140 // because of state change.
141 UpdateSessionLengthLimitInMin(3);
142 notification = GetNotification();
143 EXPECT_TRUE(notification);
144 EXPECT_TRUE(notification->rich_notification_data().
145 should_make_spoken_feedback_for_popup_updates);
147 RemoveNotification();
149 // Session length limit is updated to longer state. Notification should
150 // re-appear and be re-read.
151 UpdateSessionLengthLimitInMin(15);
152 notification = GetNotification();
153 EXPECT_TRUE(notification);
154 EXPECT_TRUE(notification->rich_notification_data().
155 should_make_spoken_feedback_for_popup_updates);