| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/system/chromeos/session/tray_session_length_limit.h" | 5 #include "ash/system/chromeos/session/tray_session_length_limit.h" |
| 6 | 6 |
| 7 #include "ash/root_window_controller.h" | 7 #include "ash/root_window_controller.h" |
| 8 #include "ash/shell.h" | 8 #include "ash/shell.h" |
| 9 #include "ash/system/tray/system_tray.h" | 9 #include "ash/system/tray/system_tray.h" |
| 10 #include "ash/test/ash_test_base.h" | 10 #include "ash/test/ash_test_base.h" |
| 11 #include "ash/test/test_system_tray_delegate.h" | 11 #include "ash/test/test_system_tray_delegate.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "ui/message_center/message_center.h" | 13 #include "ui/message_center/message_center.h" |
| 14 #include "ui/message_center/notification.h" | 14 #include "ui/message_center/notification.h" |
| 15 #include "ui/message_center/notification_types.h" | 15 #include "ui/message_center/notification_types.h" |
| 16 | 16 |
| 17 namespace ash { | 17 namespace ash { |
| 18 namespace test { | 18 namespace test { |
| 19 | 19 |
| 20 class TraySessionLengthLimitTest : public AshTestBase { | 20 class TraySessionLengthLimitTest : public AshTestBase { |
| 21 public: | 21 public: |
| 22 TraySessionLengthLimitTest() {} | 22 TraySessionLengthLimitTest() {} |
| 23 virtual ~TraySessionLengthLimitTest() {} | 23 virtual ~TraySessionLengthLimitTest() {} |
| 24 | 24 |
| 25 virtual void SetUp() OVERRIDE { | 25 virtual void SetUp() OVERRIDE { |
| 26 AshTestBase::SetUp(); | 26 AshTestBase::SetUp(); |
| 27 SystemTray* system_tray = | 27 SystemTray* system_tray = |
| 28 Shell::GetPrimaryRootWindowController()->GetSystemTray(); | 28 Shell::GetPrimaryRootWindowController()->GetSystemTray(); |
| 29 tray_session_length_limit_ = new internal::TraySessionLengthLimit( | 29 tray_session_length_limit_ = new TraySessionLengthLimit(system_tray); |
| 30 system_tray); | |
| 31 system_tray->AddTrayItem(tray_session_length_limit_); | 30 system_tray->AddTrayItem(tray_session_length_limit_); |
| 32 } | 31 } |
| 33 | 32 |
| 34 virtual void TearDown() OVERRIDE { | 33 virtual void TearDown() OVERRIDE { |
| 35 AshTestBase::TearDown(); | 34 AshTestBase::TearDown(); |
| 36 } | 35 } |
| 37 | 36 |
| 38 protected: | 37 protected: |
| 39 void UpdateSessionLengthLimitInMin(int mins) { | 38 void UpdateSessionLengthLimitInMin(int mins) { |
| 40 GetSystemTrayDelegate()->SetSessionLengthLimitForTest( | 39 GetSystemTrayDelegate()->SetSessionLengthLimitForTest( |
| 41 base::TimeDelta::FromMinutes(mins)); | 40 base::TimeDelta::FromMinutes(mins)); |
| 42 tray_session_length_limit_->OnSessionLengthLimitChanged(); | 41 tray_session_length_limit_->OnSessionLengthLimitChanged(); |
| 43 } | 42 } |
| 44 | 43 |
| 45 message_center::Notification* GetNotification() { | 44 message_center::Notification* GetNotification() { |
| 46 const message_center::NotificationList::Notifications& notifications = | 45 const message_center::NotificationList::Notifications& notifications = |
| 47 message_center::MessageCenter::Get()->GetVisibleNotifications(); | 46 message_center::MessageCenter::Get()->GetVisibleNotifications(); |
| 48 for (message_center::NotificationList::Notifications::const_iterator iter = | 47 for (message_center::NotificationList::Notifications::const_iterator iter = |
| 49 notifications.begin(); iter != notifications.end(); ++iter) { | 48 notifications.begin(); iter != notifications.end(); ++iter) { |
| 50 if ((*iter)->id() == internal::TraySessionLengthLimit::kNotificationId) | 49 if ((*iter)->id() == TraySessionLengthLimit::kNotificationId) |
| 51 return *iter; | 50 return *iter; |
| 52 } | 51 } |
| 53 return NULL; | 52 return NULL; |
| 54 } | 53 } |
| 55 | 54 |
| 56 void ClearSessionLengthLimit() { | 55 void ClearSessionLengthLimit() { |
| 57 GetSystemTrayDelegate()->ClearSessionLengthLimit(); | 56 GetSystemTrayDelegate()->ClearSessionLengthLimit(); |
| 58 tray_session_length_limit_->OnSessionLengthLimitChanged(); | 57 tray_session_length_limit_->OnSessionLengthLimitChanged(); |
| 59 } | 58 } |
| 60 | 59 |
| 61 void RemoveNotification() { | 60 void RemoveNotification() { |
| 62 message_center::MessageCenter::Get()->RemoveNotification( | 61 message_center::MessageCenter::Get()->RemoveNotification( |
| 63 internal::TraySessionLengthLimit::kNotificationId, true /* by_user */); | 62 TraySessionLengthLimit::kNotificationId, true /* by_user */); |
| 64 } | 63 } |
| 65 | 64 |
| 66 internal::TraySessionLengthLimit* tray_session_length_limit() { | 65 TraySessionLengthLimit* tray_session_length_limit() { |
| 67 return tray_session_length_limit_; | 66 return tray_session_length_limit_; |
| 68 } | 67 } |
| 69 | 68 |
| 70 bool IsTrayViewVisible() { | 69 bool IsTrayViewVisible() { |
| 71 return tray_session_length_limit_->IsTrayViewVisibleForTest(); | 70 return tray_session_length_limit_->IsTrayViewVisibleForTest(); |
| 72 } | 71 } |
| 73 | 72 |
| 74 private: | 73 private: |
| 75 // Weak reference, owned by the SystemTray. | 74 // Weak reference, owned by the SystemTray. |
| 76 internal::TraySessionLengthLimit* tray_session_length_limit_; | 75 TraySessionLengthLimit* tray_session_length_limit_; |
| 77 | 76 |
| 78 DISALLOW_COPY_AND_ASSIGN(TraySessionLengthLimitTest); | 77 DISALLOW_COPY_AND_ASSIGN(TraySessionLengthLimitTest); |
| 79 }; | 78 }; |
| 80 | 79 |
| 81 TEST_F(TraySessionLengthLimitTest, TrayView) { | 80 TEST_F(TraySessionLengthLimitTest, TrayView) { |
| 82 // No session limit. | 81 // No session limit. |
| 83 EXPECT_FALSE(IsTrayViewVisible()); | 82 EXPECT_FALSE(IsTrayViewVisible()); |
| 84 | 83 |
| 85 // Limit is 15 min. | 84 // Limit is 15 min. |
| 86 UpdateSessionLengthLimitInMin(15); | 85 UpdateSessionLengthLimitInMin(15); |
| 87 EXPECT_EQ(internal::TraySessionLengthLimit::LIMIT_SET, | 86 EXPECT_EQ(TraySessionLengthLimit::LIMIT_SET, |
| 88 tray_session_length_limit()->GetLimitState()); | 87 tray_session_length_limit()->GetLimitState()); |
| 89 EXPECT_TRUE(IsTrayViewVisible()); | 88 EXPECT_TRUE(IsTrayViewVisible()); |
| 90 | 89 |
| 91 // Limit is 3 min. | 90 // Limit is 3 min. |
| 92 UpdateSessionLengthLimitInMin(3); | 91 UpdateSessionLengthLimitInMin(3); |
| 93 EXPECT_EQ(internal::TraySessionLengthLimit::LIMIT_EXPIRING_SOON, | 92 EXPECT_EQ(TraySessionLengthLimit::LIMIT_EXPIRING_SOON, |
| 94 tray_session_length_limit()->GetLimitState()); | 93 tray_session_length_limit()->GetLimitState()); |
| 95 EXPECT_TRUE(IsTrayViewVisible()); | 94 EXPECT_TRUE(IsTrayViewVisible()); |
| 96 | 95 |
| 97 // Nothing left. | 96 // Nothing left. |
| 98 UpdateSessionLengthLimitInMin(0); | 97 UpdateSessionLengthLimitInMin(0); |
| 99 EXPECT_EQ(internal::TraySessionLengthLimit::LIMIT_EXPIRING_SOON, | 98 EXPECT_EQ(TraySessionLengthLimit::LIMIT_EXPIRING_SOON, |
| 100 tray_session_length_limit()->GetLimitState()); | 99 tray_session_length_limit()->GetLimitState()); |
| 101 EXPECT_TRUE(IsTrayViewVisible()); | 100 EXPECT_TRUE(IsTrayViewVisible()); |
| 102 | 101 |
| 103 // Checks the behavior in case the limit goes negative. | 102 // Checks the behavior in case the limit goes negative. |
| 104 UpdateSessionLengthLimitInMin(-5); | 103 UpdateSessionLengthLimitInMin(-5); |
| 105 EXPECT_EQ(internal::TraySessionLengthLimit::LIMIT_EXPIRING_SOON, | 104 EXPECT_EQ(TraySessionLengthLimit::LIMIT_EXPIRING_SOON, |
| 106 tray_session_length_limit()->GetLimitState()); | 105 tray_session_length_limit()->GetLimitState()); |
| 107 EXPECT_TRUE(IsTrayViewVisible()); | 106 EXPECT_TRUE(IsTrayViewVisible()); |
| 108 | 107 |
| 109 // Clears the session length limit, the TrayView should get invisible. | 108 // Clears the session length limit, the TrayView should get invisible. |
| 110 ClearSessionLengthLimit(); | 109 ClearSessionLengthLimit(); |
| 111 ASSERT_EQ(internal::TraySessionLengthLimit::LIMIT_NONE, | 110 ASSERT_EQ(TraySessionLengthLimit::LIMIT_NONE, |
| 112 tray_session_length_limit()->GetLimitState()); | 111 tray_session_length_limit()->GetLimitState()); |
| 113 EXPECT_FALSE(IsTrayViewVisible()); | 112 EXPECT_FALSE(IsTrayViewVisible()); |
| 114 } | 113 } |
| 115 | 114 |
| 116 TEST_F(TraySessionLengthLimitTest, Notification) { | 115 TEST_F(TraySessionLengthLimitTest, Notification) { |
| 117 // No notifications when no session limit. | 116 // No notifications when no session limit. |
| 118 EXPECT_FALSE(GetNotification()); | 117 EXPECT_FALSE(GetNotification()); |
| 119 | 118 |
| 120 // Limit is 15 min. | 119 // Limit is 15 min. |
| 121 UpdateSessionLengthLimitInMin(15); | 120 UpdateSessionLengthLimitInMin(15); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 RemoveNotification(); | 185 RemoveNotification(); |
| 187 | 186 |
| 188 // Session length limit is updated to longer state. This should not re-appear | 187 // Session length limit is updated to longer state. This should not re-appear |
| 189 // the notification. | 188 // the notification. |
| 190 UpdateSessionLengthLimitInMin(15); | 189 UpdateSessionLengthLimitInMin(15); |
| 191 EXPECT_FALSE(GetNotification()); | 190 EXPECT_FALSE(GetNotification()); |
| 192 } | 191 } |
| 193 | 192 |
| 194 } // namespace test | 193 } // namespace test |
| 195 } // namespace ash | 194 } // namespace ash |
| OLD | NEW |