1 // Copyright 2013 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/session_length_limit/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 virtual ~TraySessionLengthLimitTest() {} 24 25 virtual void SetUp() OVERRIDE { 26 AshTestBase::SetUp(); 27 SystemTray* system_tray = 28 Shell::GetPrimaryRootWindowController()->GetSystemTray(); 29 tray_session_length_limit_ = new internal::TraySessionLengthLimit( 30 system_tray); 31 system_tray->AddTrayItem(tray_session_length_limit_); 32 } 33 34 virtual void TearDown() OVERRIDE { 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(); iter != notifications.end(); ++iter) { 50 if ((*iter)->id() == internal::TraySessionLengthLimit::kNotificationId) 51 return *iter; 52 } 53 return NULL; 54 } 55 56 void ClearSessionLengthLimit() { 57 GetSystemTrayDelegate()->ClearSessionLengthLimit(); 58 tray_session_length_limit_->OnSessionLengthLimitChanged(); 59 } 60 61 void RemoveNotification() { 62 message_center::MessageCenter::Get()->RemoveNotification( 63 internal::TraySessionLengthLimit::kNotificationId, true /* by_user */); 64 } 65 66 internal::TraySessionLengthLimit* tray_session_length_limit() { 67 return tray_session_length_limit_; 68 } 69 70 bool IsTrayViewVisible() { 71 return tray_session_length_limit_->IsTrayViewVisibleForTest(); 72 } 73 74 private: 75 // Weak reference, owned by the SystemTray. 76 internal::TraySessionLengthLimit* tray_session_length_limit_; 77 78 DISALLOW_COPY_AND_ASSIGN(TraySessionLengthLimitTest); 79 }; 80 81 TEST_F(TraySessionLengthLimitTest, TrayView) { 82 // No session limit. 83 EXPECT_FALSE(IsTrayViewVisible()); 84 85 // Limit is 15 min. 86 UpdateSessionLengthLimitInMin(15); 87 EXPECT_EQ(internal::TraySessionLengthLimit::LIMIT_SET, 88 tray_session_length_limit()->GetLimitState()); 89 EXPECT_TRUE(IsTrayViewVisible()); 90 91 // Limit is 3 min. 92 UpdateSessionLengthLimitInMin(3); 93 EXPECT_EQ(internal::TraySessionLengthLimit::LIMIT_EXPIRING_SOON, 94 tray_session_length_limit()->GetLimitState()); 95 EXPECT_TRUE(IsTrayViewVisible()); 96 97 // Nothing left. 98 UpdateSessionLengthLimitInMin(0); 99 EXPECT_EQ(internal::TraySessionLengthLimit::LIMIT_EXPIRING_SOON, 100 tray_session_length_limit()->GetLimitState()); 101 EXPECT_TRUE(IsTrayViewVisible()); 102 103 // Checks the behavior in case the limit goes negative. 104 UpdateSessionLengthLimitInMin(-5); 105 EXPECT_EQ(internal::TraySessionLengthLimit::LIMIT_EXPIRING_SOON, 106 tray_session_length_limit()->GetLimitState()); 107 EXPECT_TRUE(IsTrayViewVisible()); 108 109 // Clears the session length limit, the TrayView should get invisible. 110 ClearSessionLengthLimit(); 111 ASSERT_EQ(internal::TraySessionLengthLimit::LIMIT_NONE, 112 tray_session_length_limit()->GetLimitState()); 113 EXPECT_FALSE(IsTrayViewVisible()); 114 } 115 116 TEST_F(TraySessionLengthLimitTest, Notification) { 117 // No notifications when no session limit. 118 EXPECT_FALSE(GetNotification()); 119 120 // Limit is 15 min. 121 UpdateSessionLengthLimitInMin(15); 122 message_center::Notification* notification = GetNotification(); 123 EXPECT_TRUE(notification); 124 EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority()); 125 base::string16 first_content = notification->title(); 126 // Should read the content. 127 EXPECT_TRUE(notification->rich_notification_data(). 128 should_make_spoken_feedback_for_popup_updates); 129 130 // Limit is 10 min. 131 UpdateSessionLengthLimitInMin(10); 132 notification = GetNotification(); 133 EXPECT_TRUE(notification); 134 EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority()); 135 // The content should be updated. 136 EXPECT_NE(first_content, notification->title()); 137 // Should NOT read, because just update the remaining time. 138 EXPECT_FALSE(notification->rich_notification_data(). 139 should_make_spoken_feedback_for_popup_updates); 140 141 // Limit is 3 min. 142 UpdateSessionLengthLimitInMin(3); 143 notification = GetNotification(); 144 EXPECT_TRUE(notification); 145 EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority()); 146 // Should read the content again because the state has changed. 147 EXPECT_TRUE(notification->rich_notification_data(). 148 should_make_spoken_feedback_for_popup_updates); 149 150 // Session length limit is updated to longer. This should not read the 151 // notification content again. 152 UpdateSessionLengthLimitInMin(15); 153 notification = GetNotification(); 154 EXPECT_TRUE(notification); 155 EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority()); 156 // Should not read again because the state has changed to longer. 157 EXPECT_FALSE(notification->rich_notification_data(). 158 should_make_spoken_feedback_for_popup_updates); 159 160 // Clears the limit: the notification should be gone. 161 ClearSessionLengthLimit(); 162 EXPECT_FALSE(GetNotification()); 163 } 164 165 TEST_F(TraySessionLengthLimitTest, RemoveNotification) { 166 // Limit is 15 min. 167 UpdateSessionLengthLimitInMin(15); 168 EXPECT_TRUE(GetNotification()); 169 170 // Limit is 14 min. 171 UpdateSessionLengthLimitInMin(14); 172 EXPECT_TRUE(GetNotification()); 173 174 // Removes the notification. 175 RemoveNotification(); 176 EXPECT_FALSE(GetNotification()); 177 178 // Limit is 13 min. The notification should not re-appear. 179 UpdateSessionLengthLimitInMin(13); 180 EXPECT_FALSE(GetNotification()); 181 182 // Limit is 3 min. The notification should re-appear because of state change. 183 UpdateSessionLengthLimitInMin(3); 184 EXPECT_TRUE(GetNotification()); 185 186 RemoveNotification(); 187 188 // Session length limit is updated to longer state. This should not re-appear 189 // the notification. 190 UpdateSessionLengthLimitInMin(15); 191 EXPECT_FALSE(GetNotification()); 192 } 193 194 } // namespace test 195 } // namespace ash 196