OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "ash/system/chromeos/session/tray_session_length_limit.h" | |
6 | |
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" | |
16 | |
17 namespace ash { | |
18 namespace test { | |
19 | |
20 class TraySessionLengthLimitTest : public AshTestBase { | |
21 public: | |
22 TraySessionLengthLimitTest() {} | |
23 ~TraySessionLengthLimitTest() override {} | |
24 | |
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_); | |
31 } | |
32 | |
33 void TearDown() override { | |
34 ClearSessionLengthLimit(); | |
35 AshTestBase::TearDown(); | |
36 } | |
37 | |
38 protected: | |
39 void UpdateSessionLengthLimitInMin(int mins) { | |
40 GetSystemTrayDelegate()->SetSessionLengthLimitForTest( | |
41 base::TimeDelta::FromMinutes(mins)); | |
42 tray_session_length_limit_->OnSessionLengthLimitChanged(); | |
43 } | |
44 | |
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(); | |
50 iter != notifications.end(); ++iter) { | |
51 if ((*iter)->id() == TraySessionLengthLimit::kNotificationId) | |
52 return *iter; | |
53 } | |
54 return nullptr; | |
55 } | |
56 | |
57 void ClearSessionLengthLimit() { | |
58 GetSystemTrayDelegate()->ClearSessionLengthLimit(); | |
59 tray_session_length_limit_->OnSessionLengthLimitChanged(); | |
60 } | |
61 | |
62 void RemoveNotification() { | |
63 message_center::MessageCenter::Get()->RemoveNotification( | |
64 TraySessionLengthLimit::kNotificationId, false /* by_user */); | |
65 } | |
66 | |
67 TraySessionLengthLimit* tray_session_length_limit() { | |
68 return tray_session_length_limit_; | |
69 } | |
70 | |
71 private: | |
72 // Weak reference, owned by the SystemTray. | |
73 TraySessionLengthLimit* tray_session_length_limit_; | |
74 | |
75 DISALLOW_COPY_AND_ASSIGN(TraySessionLengthLimitTest); | |
76 }; | |
77 | |
78 TEST_F(TraySessionLengthLimitTest, Notification) { | |
79 // No notifications when no session limit. | |
80 EXPECT_FALSE(GetNotification()); | |
81 | |
82 // Limit is 15 min. | |
83 UpdateSessionLengthLimitInMin(15); | |
84 message_center::Notification* notification = GetNotification(); | |
85 EXPECT_TRUE(notification); | |
86 EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority()); | |
87 base::string16 first_content = notification->message(); | |
88 // Should read the content. | |
89 EXPECT_TRUE(notification->rich_notification_data() | |
90 .should_make_spoken_feedback_for_popup_updates); | |
91 | |
92 // Limit is 10 min. | |
93 UpdateSessionLengthLimitInMin(10); | |
94 notification = GetNotification(); | |
95 EXPECT_TRUE(notification); | |
96 EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority()); | |
97 // The content should be updated. | |
98 EXPECT_NE(first_content, notification->message()); | |
99 // Should NOT read, because just update the remaining time. | |
100 EXPECT_FALSE(notification->rich_notification_data() | |
101 .should_make_spoken_feedback_for_popup_updates); | |
102 | |
103 // Limit is 3 min. | |
104 UpdateSessionLengthLimitInMin(3); | |
105 notification = GetNotification(); | |
106 EXPECT_TRUE(notification); | |
107 EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority()); | |
108 // Should read the content again because the state has changed. | |
109 EXPECT_TRUE(notification->rich_notification_data() | |
110 .should_make_spoken_feedback_for_popup_updates); | |
111 | |
112 // Session length limit is updated to longer: 15 min. | |
113 UpdateSessionLengthLimitInMin(15); | |
114 notification = GetNotification(); | |
115 EXPECT_TRUE(notification); | |
116 EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority()); | |
117 // Should read again because an increase of the remaining time is noteworthy. | |
118 EXPECT_TRUE(notification->rich_notification_data() | |
119 .should_make_spoken_feedback_for_popup_updates); | |
120 | |
121 // Clears the limit: the notification should be gone. | |
122 ClearSessionLengthLimit(); | |
123 EXPECT_FALSE(GetNotification()); | |
124 } | |
125 | |
126 TEST_F(TraySessionLengthLimitTest, RemoveNotification) { | |
127 // Limit is 15 min. | |
128 UpdateSessionLengthLimitInMin(15); | |
129 EXPECT_TRUE(GetNotification()); | |
130 | |
131 // Removes the notification. | |
132 RemoveNotification(); | |
133 EXPECT_FALSE(GetNotification()); | |
134 | |
135 // Limit is 10 min. The notification should not re-appear. | |
136 UpdateSessionLengthLimitInMin(10); | |
137 EXPECT_FALSE(GetNotification()); | |
138 | |
139 // Limit is 3 min. The notification should re-appear and should be re-read | |
140 // because of state change. | |
141 UpdateSessionLengthLimitInMin(3); | |
142 message_center::Notification* notification = GetNotification(); | |
143 EXPECT_TRUE(notification); | |
144 EXPECT_TRUE(notification->rich_notification_data() | |
145 .should_make_spoken_feedback_for_popup_updates); | |
146 | |
147 RemoveNotification(); | |
148 | |
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); | |
156 } | |
157 | |
158 } // namespace test | |
159 } // namespace ash | |
OLD | NEW |