Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ash / system / chromeos / session / tray_session_length_limit_unittest.cc
blobda203a5455fd0093aa635249e405349a15834438
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"
8 #include "ash/shell.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"
17 namespace ash {
18 namespace test {
20 class TraySessionLengthLimitTest : public AshTestBase {
21 public:
22 TraySessionLengthLimitTest() {}
23 ~TraySessionLengthLimitTest() override {}
25 void SetUp() override {
26 AshTestBase::SetUp();
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 void TearDown() override {
34 ClearSessionLengthLimit();
35 AshTestBase::TearDown();
38 protected:
39 void UpdateSessionLengthLimitInMin(int mins) {
40 GetSystemTrayDelegate()->SetSessionLengthLimitForTest(
41 base::TimeDelta::FromMinutes(mins));
42 tray_session_length_limit_->OnSessionLengthLimitChanged();
45 message_center::Notification* GetNotification() {
46 const message_center::NotificationList::Notifications& notifications =
47 message_center::MessageCenter::Get()->GetVisibleNotifications();
48 for (message_center::NotificationList::Notifications::const_iterator iter =
49 notifications.begin(); iter != notifications.end(); ++iter) {
50 if ((*iter)->id() == TraySessionLengthLimit::kNotificationId)
51 return *iter;
53 return nullptr;
56 void ClearSessionLengthLimit() {
57 GetSystemTrayDelegate()->ClearSessionLengthLimit();
58 tray_session_length_limit_->OnSessionLengthLimitChanged();
61 void RemoveNotification() {
62 message_center::MessageCenter::Get()->RemoveNotification(
63 TraySessionLengthLimit::kNotificationId, false /* by_user */);
66 TraySessionLengthLimit* tray_session_length_limit() {
67 return tray_session_length_limit_;
70 private:
71 // Weak reference, owned by the SystemTray.
72 TraySessionLengthLimit* tray_session_length_limit_;
74 DISALLOW_COPY_AND_ASSIGN(TraySessionLengthLimitTest);
77 TEST_F(TraySessionLengthLimitTest, Notification) {
78 // No notifications when no session limit.
79 EXPECT_FALSE(GetNotification());
81 // Limit is 15 min.
82 UpdateSessionLengthLimitInMin(15);
83 message_center::Notification* notification = GetNotification();
84 EXPECT_TRUE(notification);
85 EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority());
86 base::string16 first_content = notification->message();
87 // Should read the content.
88 EXPECT_TRUE(notification->rich_notification_data().
89 should_make_spoken_feedback_for_popup_updates);
91 // Limit is 10 min.
92 UpdateSessionLengthLimitInMin(10);
93 notification = GetNotification();
94 EXPECT_TRUE(notification);
95 EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority());
96 // The content should be updated.
97 EXPECT_NE(first_content, notification->message());
98 // Should NOT read, because just update the remaining time.
99 EXPECT_FALSE(notification->rich_notification_data().
100 should_make_spoken_feedback_for_popup_updates);
102 // Limit is 3 min.
103 UpdateSessionLengthLimitInMin(3);
104 notification = GetNotification();
105 EXPECT_TRUE(notification);
106 EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority());
107 // Should read the content again because the state has changed.
108 EXPECT_TRUE(notification->rich_notification_data().
109 should_make_spoken_feedback_for_popup_updates);
111 // Session length limit is updated to longer: 15 min.
112 UpdateSessionLengthLimitInMin(15);
113 notification = GetNotification();
114 EXPECT_TRUE(notification);
115 EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority());
116 // Should read again because an increase of the remaining time is noteworthy.
117 EXPECT_TRUE(notification->rich_notification_data().
118 should_make_spoken_feedback_for_popup_updates);
120 // Clears the limit: the notification should be gone.
121 ClearSessionLengthLimit();
122 EXPECT_FALSE(GetNotification());
125 TEST_F(TraySessionLengthLimitTest, RemoveNotification) {
126 // Limit is 15 min.
127 UpdateSessionLengthLimitInMin(15);
128 EXPECT_TRUE(GetNotification());
130 // Removes the notification.
131 RemoveNotification();
132 EXPECT_FALSE(GetNotification());
134 // Limit is 10 min. The notification should not re-appear.
135 UpdateSessionLengthLimitInMin(10);
136 EXPECT_FALSE(GetNotification());
138 // Limit is 3 min. The notification should re-appear and should be re-read
139 // because of state change.
140 UpdateSessionLengthLimitInMin(3);
141 message_center::Notification* notification = GetNotification();
142 EXPECT_TRUE(notification);
143 EXPECT_TRUE(notification->rich_notification_data().
144 should_make_spoken_feedback_for_popup_updates);
146 RemoveNotification();
148 // Session length limit is updated to longer state. Notification should
149 // re-appear and be re-read.
150 UpdateSessionLengthLimitInMin(15);
151 notification = GetNotification();
152 EXPECT_TRUE(notification);
153 EXPECT_TRUE(notification->rich_notification_data().
154 should_make_spoken_feedback_for_popup_updates);
157 } // namespace test
158 } // namespace ash