inactive_user_notification_blocker_unittest.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #include "ash/system/message_center/inactive_user_notification_blocker.h"
  5. #include "ash/constants/notifier_catalogs.h"
  6. #include "ash/public/cpp/test/shell_test_api.h"
  7. #include "ash/session/test_session_controller_client.h"
  8. #include "ash/shell.h"
  9. #include "ash/system/message_center/message_center_controller.h"
  10. #include "ash/test/ash_test_base.h"
  11. #include "base/strings/utf_string_conversions.h"
  12. #include "base/test/scoped_feature_list.h"
  13. #include "components/account_id/account_id.h"
  14. #include "ui/message_center/message_center.h"
  15. #include "ui/message_center/public/cpp/notification.h"
  16. namespace ash {
  17. namespace {
  18. using base::UTF8ToUTF16;
  19. const char kNotifierSystemPriority[] = "ash.some-high-priority-component";
  20. class InactiveUserNotificationBlockerTest
  21. : public NoSessionAshTestBase,
  22. public message_center::NotificationBlocker::Observer,
  23. public testing::WithParamInterface<bool> {
  24. public:
  25. InactiveUserNotificationBlockerTest() = default;
  26. InactiveUserNotificationBlockerTest(
  27. const InactiveUserNotificationBlockerTest&) = delete;
  28. InactiveUserNotificationBlockerTest& operator=(
  29. const InactiveUserNotificationBlockerTest&) = delete;
  30. ~InactiveUserNotificationBlockerTest() override = default;
  31. // AshTestBase overrides:
  32. void SetUp() override {
  33. scoped_feature_list_ = std::make_unique<base::test::ScopedFeatureList>();
  34. scoped_feature_list_->InitWithFeatureState(features::kNotificationsRefresh,
  35. IsNotificationsRefreshEnabled());
  36. AshTestBase::SetUp();
  37. blocker_ = ShellTestApi()
  38. .message_center_controller()
  39. ->inactive_user_notification_blocker_for_testing();
  40. blocker_->AddObserver(this);
  41. }
  42. bool IsNotificationsRefreshEnabled() const { return GetParam(); }
  43. void TearDown() override {
  44. blocker_->RemoveObserver(this);
  45. AshTestBase::TearDown();
  46. }
  47. // message_center::NotificationBlocker::Observer ovverrides:
  48. void OnBlockingStateChanged(
  49. message_center::NotificationBlocker* blocker) override {
  50. state_changed_count_++;
  51. }
  52. protected:
  53. const std::string GetDefaultUserId() { return "user0@tray"; }
  54. void AddUserSession(const std::string& email) {
  55. GetSessionControllerClient()->AddUserSession(email);
  56. }
  57. void SwitchActiveUser(const std::string& email) {
  58. const AccountId account_id(AccountId::FromUserEmail(email));
  59. GetSessionControllerClient()->SwitchActiveUser(account_id);
  60. }
  61. int GetStateChangedCountAndReset() {
  62. int result = state_changed_count_;
  63. state_changed_count_ = 0;
  64. return result;
  65. }
  66. bool ShouldShowAsPopup(const message_center::NotifierId& notifier_id,
  67. const std::string& profile_id) {
  68. message_center::NotifierId id_with_profile = notifier_id;
  69. id_with_profile.profile_id = profile_id;
  70. message_center::Notification notification(
  71. message_center::NOTIFICATION_TYPE_SIMPLE, "popup-id", u"popup-title",
  72. u"popup-message", ui::ImageModel(), u"popup-source", GURL(),
  73. id_with_profile, message_center::RichNotificationData(), nullptr);
  74. if (notifier_id.id == kNotifierSystemPriority)
  75. notification.set_priority(message_center::SYSTEM_PRIORITY);
  76. return blocker_->ShouldShowNotificationAsPopup(notification);
  77. }
  78. bool ShouldShow(const message_center::NotifierId& notifier_id,
  79. const std::string& profile_id) {
  80. message_center::NotifierId id_with_profile = notifier_id;
  81. id_with_profile.profile_id = profile_id;
  82. message_center::Notification notification(
  83. message_center::NOTIFICATION_TYPE_SIMPLE, "notification-id",
  84. u"notification-title", u"notification-message", ui::ImageModel(),
  85. u"notification-source", GURL(), id_with_profile,
  86. message_center::RichNotificationData(), nullptr);
  87. if (notifier_id.id == kNotifierSystemPriority)
  88. notification.set_priority(message_center::SYSTEM_PRIORITY);
  89. return blocker_->ShouldShowNotification(notification);
  90. }
  91. private:
  92. int state_changed_count_ = 0;
  93. InactiveUserNotificationBlocker* blocker_ = nullptr;
  94. std::unique_ptr<base::test::ScopedFeatureList> scoped_feature_list_;
  95. };
  96. INSTANTIATE_TEST_SUITE_P(All,
  97. InactiveUserNotificationBlockerTest,
  98. testing::Bool() /* IsNotificationsRefreshEnabled() */);
  99. TEST_P(InactiveUserNotificationBlockerTest, Basic) {
  100. message_center::NotifierId notifier_id(
  101. message_center::NotifierType::APPLICATION, "test-app");
  102. // System priority notifiers should always show regardless of fullscreen
  103. // or lock state.
  104. message_center::NotifierId ash_system_notifier(
  105. message_center::NotifierType::SYSTEM_COMPONENT, kNotifierSystemPriority,
  106. NotificationCatalogName::kTestCatalogName);
  107. // Other system notifiers should be treated as same as a normal notifier.
  108. message_center::NotifierId random_system_notifier(
  109. message_center::NotifierType::SYSTEM_COMPONENT,
  110. "ash.some-other-component", NotificationCatalogName::kTestCatalogName);
  111. // Notifications are not blocked before login.
  112. const std::string kEmptyUserId;
  113. EXPECT_TRUE(ShouldShow(notifier_id, kEmptyUserId));
  114. EXPECT_TRUE(ShouldShow(ash_system_notifier, kEmptyUserId));
  115. EXPECT_TRUE(ShouldShow(random_system_notifier, kEmptyUserId));
  116. // Login triggers blocking state change.
  117. SimulateUserLogin(GetDefaultUserId());
  118. EXPECT_EQ(1, GetStateChangedCountAndReset());
  119. // Notifications for a single user session are not blocked.
  120. EXPECT_TRUE(ShouldShow(ash_system_notifier, GetDefaultUserId()));
  121. EXPECT_TRUE(ShouldShow(notifier_id, GetDefaultUserId()));
  122. EXPECT_TRUE(ShouldShow(random_system_notifier, GetDefaultUserId()));
  123. // Notifications from a user other than the active one (in this case, default)
  124. // are generally blocked unless they're ash system notifications.
  125. AddUserSession("user1@tray");
  126. EXPECT_EQ(0, GetStateChangedCountAndReset());
  127. const std::string kInvalidUserId("invalid");
  128. EXPECT_FALSE(ShouldShowAsPopup(notifier_id, kInvalidUserId));
  129. EXPECT_TRUE(ShouldShowAsPopup(ash_system_notifier, kEmptyUserId));
  130. EXPECT_FALSE(ShouldShowAsPopup(random_system_notifier, kInvalidUserId));
  131. EXPECT_TRUE(ShouldShowAsPopup(notifier_id, GetDefaultUserId()));
  132. EXPECT_FALSE(ShouldShowAsPopup(notifier_id, "user1@tray"));
  133. EXPECT_TRUE(ShouldShowAsPopup(random_system_notifier, GetDefaultUserId()));
  134. EXPECT_FALSE(ShouldShowAsPopup(random_system_notifier, "user1@tray"));
  135. EXPECT_FALSE(ShouldShow(notifier_id, kInvalidUserId));
  136. EXPECT_TRUE(ShouldShow(ash_system_notifier, kEmptyUserId));
  137. EXPECT_FALSE(ShouldShow(random_system_notifier, kInvalidUserId));
  138. EXPECT_TRUE(ShouldShow(notifier_id, GetDefaultUserId()));
  139. EXPECT_FALSE(ShouldShow(notifier_id, "user1@tray"));
  140. EXPECT_TRUE(ShouldShow(random_system_notifier, GetDefaultUserId()));
  141. EXPECT_FALSE(ShouldShow(random_system_notifier, "user1@tray"));
  142. // Activate the second user and make sure the original/default user's
  143. // notifications are now hidden.
  144. SwitchActiveUser("user1@tray");
  145. EXPECT_FALSE(ShouldShowAsPopup(notifier_id, kInvalidUserId));
  146. EXPECT_TRUE(ShouldShowAsPopup(ash_system_notifier, kEmptyUserId));
  147. EXPECT_FALSE(ShouldShowAsPopup(random_system_notifier, kInvalidUserId));
  148. EXPECT_FALSE(ShouldShowAsPopup(notifier_id, GetDefaultUserId()));
  149. EXPECT_TRUE(ShouldShowAsPopup(notifier_id, "user1@tray"));
  150. EXPECT_FALSE(ShouldShowAsPopup(random_system_notifier, GetDefaultUserId()));
  151. EXPECT_TRUE(ShouldShowAsPopup(random_system_notifier, "user1@tray"));
  152. EXPECT_FALSE(ShouldShow(notifier_id, kInvalidUserId));
  153. EXPECT_TRUE(ShouldShow(ash_system_notifier, kEmptyUserId));
  154. EXPECT_FALSE(ShouldShow(random_system_notifier, kInvalidUserId));
  155. EXPECT_FALSE(ShouldShow(notifier_id, GetDefaultUserId()));
  156. EXPECT_TRUE(ShouldShow(notifier_id, "user1@tray"));
  157. EXPECT_FALSE(ShouldShow(random_system_notifier, GetDefaultUserId()));
  158. EXPECT_TRUE(ShouldShow(random_system_notifier, "user1@tray"));
  159. // Switch back and verify the active user's notifications are once again
  160. // shown.
  161. SwitchActiveUser(GetDefaultUserId());
  162. EXPECT_FALSE(ShouldShowAsPopup(notifier_id, kInvalidUserId));
  163. EXPECT_TRUE(ShouldShowAsPopup(ash_system_notifier, kEmptyUserId));
  164. EXPECT_FALSE(ShouldShowAsPopup(random_system_notifier, kInvalidUserId));
  165. EXPECT_TRUE(ShouldShowAsPopup(notifier_id, GetDefaultUserId()));
  166. EXPECT_FALSE(ShouldShowAsPopup(notifier_id, "user1@tray"));
  167. EXPECT_TRUE(ShouldShowAsPopup(random_system_notifier, GetDefaultUserId()));
  168. EXPECT_FALSE(ShouldShowAsPopup(random_system_notifier, "user1@tray"));
  169. EXPECT_FALSE(ShouldShow(notifier_id, kInvalidUserId));
  170. EXPECT_TRUE(ShouldShow(ash_system_notifier, kEmptyUserId));
  171. EXPECT_FALSE(ShouldShow(random_system_notifier, kInvalidUserId));
  172. EXPECT_TRUE(ShouldShow(notifier_id, GetDefaultUserId()));
  173. EXPECT_FALSE(ShouldShow(notifier_id, "user1@tray"));
  174. EXPECT_TRUE(ShouldShow(random_system_notifier, GetDefaultUserId()));
  175. EXPECT_FALSE(ShouldShow(random_system_notifier, "user1@tray"));
  176. }
  177. } // namespace
  178. } // namespace ash