session_limit_notification_controller_unittest.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Copyright 2018 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. #include "ash/system/session/session_limit_notification_controller.h"
  5. #include "ash/session/session_controller_impl.h"
  6. #include "ash/shell.h"
  7. #include "ash/test/ash_test_base.h"
  8. #include "ui/message_center/message_center.h"
  9. #include "ui/message_center/public/cpp/notification.h"
  10. #include "ui/message_center/public/cpp/notification_types.h"
  11. namespace ash {
  12. class SessionLimitNotificationControllerTest : public AshTestBase {
  13. public:
  14. SessionLimitNotificationControllerTest() = default;
  15. SessionLimitNotificationControllerTest(
  16. const SessionLimitNotificationControllerTest&) = delete;
  17. SessionLimitNotificationControllerTest& operator=(
  18. const SessionLimitNotificationControllerTest&) = delete;
  19. ~SessionLimitNotificationControllerTest() override = default;
  20. protected:
  21. static const int kNotificationThresholdInMinutes = 60;
  22. void UpdateSessionLengthLimitInMin(int mins) {
  23. Shell::Get()->session_controller()->SetSessionLengthLimit(
  24. base::Minutes(mins), base::Time::Now());
  25. }
  26. message_center::Notification* GetNotification() {
  27. const message_center::NotificationList::Notifications& notifications =
  28. message_center::MessageCenter::Get()->GetVisibleNotifications();
  29. for (message_center::NotificationList::Notifications::const_iterator iter =
  30. notifications.begin();
  31. iter != notifications.end(); ++iter) {
  32. if ((*iter)->id() == SessionLimitNotificationController::kNotificationId)
  33. return *iter;
  34. }
  35. return nullptr;
  36. }
  37. void ClearSessionLengthLimit() {
  38. Shell::Get()->session_controller()->SetSessionLengthLimit(base::TimeDelta(),
  39. base::Time());
  40. }
  41. void RemoveNotification() {
  42. message_center::MessageCenter::Get()->RemoveNotification(
  43. SessionLimitNotificationController::kNotificationId,
  44. false /* by_user */);
  45. }
  46. };
  47. TEST_F(SessionLimitNotificationControllerTest, Notification) {
  48. // No notifications when no session limit.
  49. EXPECT_FALSE(GetNotification());
  50. // Limit is 15 min.
  51. UpdateSessionLengthLimitInMin(15);
  52. message_center::Notification* notification = GetNotification();
  53. EXPECT_TRUE(notification);
  54. std::u16string first_title = notification->title();
  55. // Should read the content.
  56. EXPECT_TRUE(notification->rich_notification_data()
  57. .should_make_spoken_feedback_for_popup_updates);
  58. // Limit is 10 min.
  59. UpdateSessionLengthLimitInMin(10);
  60. notification = GetNotification();
  61. EXPECT_TRUE(notification);
  62. // The title should be updated.
  63. EXPECT_NE(first_title, notification->title());
  64. // Should NOT read, because just update the remaining time.
  65. EXPECT_FALSE(notification->rich_notification_data()
  66. .should_make_spoken_feedback_for_popup_updates);
  67. // Limit is 3 min.
  68. UpdateSessionLengthLimitInMin(3);
  69. notification = GetNotification();
  70. EXPECT_TRUE(notification);
  71. // Should read the content again because the state has changed.
  72. EXPECT_TRUE(notification->rich_notification_data()
  73. .should_make_spoken_feedback_for_popup_updates);
  74. // Session length limit is updated to longer: 15 min.
  75. UpdateSessionLengthLimitInMin(15);
  76. notification = GetNotification();
  77. EXPECT_TRUE(notification);
  78. // Should read again because an increase of the remaining time is noteworthy.
  79. EXPECT_TRUE(notification->rich_notification_data()
  80. .should_make_spoken_feedback_for_popup_updates);
  81. // Clears the limit: the notification should be gone.
  82. ClearSessionLengthLimit();
  83. EXPECT_FALSE(GetNotification());
  84. }
  85. TEST_F(SessionLimitNotificationControllerTest, FarOffNotificationHidden) {
  86. // Test that notification is not shown if the session end time is far off into
  87. // the future, but an item should be present in system tray bubble.
  88. // Notification should be absent.
  89. EXPECT_FALSE(GetNotification());
  90. UpdateSessionLengthLimitInMin(kNotificationThresholdInMinutes + 10);
  91. EXPECT_FALSE(GetNotification());
  92. RemoveNotification();
  93. }
  94. TEST_F(SessionLimitNotificationControllerTest,
  95. NotificationShownAfterThreshold) {
  96. // Test that a notification is shown when time runs under the notification
  97. // display threshold.
  98. // Start with a generous session length. We should not get a notification.
  99. UpdateSessionLengthLimitInMin(kNotificationThresholdInMinutes + 10);
  100. EXPECT_FALSE(GetNotification());
  101. // Update the session length now, without changing limit_state_.
  102. UpdateSessionLengthLimitInMin(kNotificationThresholdInMinutes - 1);
  103. // A notification should be displayed now.
  104. EXPECT_TRUE(GetNotification());
  105. RemoveNotification();
  106. }
  107. TEST_F(SessionLimitNotificationControllerTest, RemoveNotification) {
  108. // Limit is 15 min.
  109. UpdateSessionLengthLimitInMin(15);
  110. EXPECT_TRUE(GetNotification());
  111. // Removes the notification.
  112. RemoveNotification();
  113. EXPECT_FALSE(GetNotification());
  114. // Limit is 10 min. The notification should not re-appear.
  115. UpdateSessionLengthLimitInMin(10);
  116. EXPECT_FALSE(GetNotification());
  117. // Limit is 3 min. The notification should re-appear and should be re-read
  118. // because of state change.
  119. UpdateSessionLengthLimitInMin(3);
  120. message_center::Notification* notification = GetNotification();
  121. EXPECT_TRUE(notification);
  122. EXPECT_TRUE(notification->rich_notification_data()
  123. .should_make_spoken_feedback_for_popup_updates);
  124. RemoveNotification();
  125. // Session length limit is updated to longer state. Notification should
  126. // re-appear and be re-read.
  127. UpdateSessionLengthLimitInMin(15);
  128. notification = GetNotification();
  129. EXPECT_TRUE(notification);
  130. EXPECT_TRUE(notification->rich_notification_data()
  131. .should_make_spoken_feedback_for_popup_updates);
  132. RemoveNotification();
  133. }
  134. class SessionLimitNotificationControllerLoginTest
  135. : public SessionLimitNotificationControllerTest {
  136. public:
  137. SessionLimitNotificationControllerLoginTest() { set_start_session(false); }
  138. SessionLimitNotificationControllerLoginTest(
  139. const SessionLimitNotificationControllerLoginTest&) = delete;
  140. SessionLimitNotificationControllerLoginTest& operator=(
  141. const SessionLimitNotificationControllerLoginTest&) = delete;
  142. };
  143. TEST_F(SessionLimitNotificationControllerLoginTest,
  144. NotificationShownAfterLogin) {
  145. UpdateSessionLengthLimitInMin(15);
  146. // No notifications before login.
  147. EXPECT_FALSE(GetNotification());
  148. // Notification is shown after login.
  149. CreateUserSessions(1);
  150. EXPECT_TRUE(GetNotification());
  151. RemoveNotification();
  152. }
  153. TEST_F(SessionLimitNotificationControllerLoginTest,
  154. FarOffNotificationHiddenAfterLogin) {
  155. // Test that notification is not shown if the session end time is far off into
  156. // the future, but an item should be present in system tray bubble.
  157. // Notification should be absent.
  158. UpdateSessionLengthLimitInMin(kNotificationThresholdInMinutes + 10);
  159. CreateUserSessions(1);
  160. EXPECT_FALSE(GetNotification());
  161. RemoveNotification();
  162. }
  163. } // namespace ash