session_state_notification_blocker_unittest.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. #include "ash/system/message_center/session_state_notification_blocker.h"
  5. #include <memory>
  6. #include "ash/constants/ash_features.h"
  7. #include "ash/constants/notifier_catalogs.h"
  8. #include "ash/session/test_session_controller_client.h"
  9. #include "ash/system/power/battery_notification.h"
  10. #include "ash/test/ash_test_base.h"
  11. #include "base/command_line.h"
  12. #include "base/strings/utf_string_conversions.h"
  13. #include "base/test/scoped_feature_list.h"
  14. #include "ui/message_center/message_center.h"
  15. #include "ui/message_center/public/cpp/notification.h"
  16. using base::UTF8ToUTF16;
  17. using session_manager::SessionState;
  18. namespace ash {
  19. namespace {
  20. const char kNotifierSystemPriority[] = "ash.some-high-priority-component";
  21. class SessionStateNotificationBlockerTest
  22. : public NoSessionAshTestBase,
  23. public message_center::NotificationBlocker::Observer,
  24. public testing::WithParamInterface<bool> {
  25. public:
  26. SessionStateNotificationBlockerTest() = default;
  27. SessionStateNotificationBlockerTest(
  28. const SessionStateNotificationBlockerTest&) = delete;
  29. SessionStateNotificationBlockerTest& operator=(
  30. const SessionStateNotificationBlockerTest&) = delete;
  31. ~SessionStateNotificationBlockerTest() override = default;
  32. // tests::AshTestBase overrides:
  33. void SetUp() override {
  34. scoped_feature_list_ = std::make_unique<base::test::ScopedFeatureList>();
  35. scoped_feature_list_->InitWithFeatureState(features::kNotificationsRefresh,
  36. IsNotificationsRefreshEnabled());
  37. NoSessionAshTestBase::SetUp();
  38. blocker_ = std::make_unique<SessionStateNotificationBlocker>(
  39. message_center::MessageCenter::Get());
  40. blocker_->AddObserver(this);
  41. }
  42. bool IsNotificationsRefreshEnabled() const { return GetParam(); }
  43. void TearDown() override {
  44. blocker_->RemoveObserver(this);
  45. blocker_.reset();
  46. NoSessionAshTestBase::TearDown();
  47. }
  48. // message_center::NotificationBlocker::Observer overrides:
  49. void OnBlockingStateChanged(
  50. message_center::NotificationBlocker* blocker) override {
  51. state_changed_count_++;
  52. }
  53. int GetStateChangedCountAndReset() {
  54. int result = state_changed_count_;
  55. state_changed_count_ = 0;
  56. return result;
  57. }
  58. bool ShouldShowNotification(const message_center::NotifierId& notifier_id) {
  59. message_center::Notification notification(
  60. message_center::NOTIFICATION_TYPE_SIMPLE,
  61. GetNotificationId(notifier_id), u"chromeos-title", u"chromeos-message",
  62. ui::ImageModel(), u"chromeos-source", GURL(), notifier_id,
  63. message_center::RichNotificationData(), nullptr);
  64. if (notifier_id.id == kNotifierSystemPriority)
  65. notification.set_priority(message_center::SYSTEM_PRIORITY);
  66. return blocker_->ShouldShowNotification(notification);
  67. }
  68. bool ShouldShowNotificationAsPopup(
  69. const message_center::NotifierId& notifier_id) {
  70. message_center::Notification notification(
  71. message_center::NOTIFICATION_TYPE_SIMPLE,
  72. GetNotificationId(notifier_id), u"chromeos-title", u"chromeos-message",
  73. ui::ImageModel(), u"chromeos-source", GURL(), notifier_id,
  74. message_center::RichNotificationData(), nullptr);
  75. if (notifier_id.id == kNotifierSystemPriority)
  76. notification.set_priority(message_center::SYSTEM_PRIORITY);
  77. return blocker_->ShouldShowNotificationAsPopup(notification);
  78. }
  79. void SetLockedState(bool locked) {
  80. GetSessionControllerClient()->SetSessionState(
  81. locked ? SessionState::LOCKED : SessionState::ACTIVE);
  82. }
  83. private:
  84. std::string GetNotificationId(const message_center::NotifierId& notifier_id) {
  85. return notifier_id.id == kNotifierSystemPriority
  86. ? BatteryNotification::kNotificationId
  87. : "chromeos-id";
  88. }
  89. int state_changed_count_ = 0;
  90. std::unique_ptr<message_center::NotificationBlocker> blocker_;
  91. std::unique_ptr<base::test::ScopedFeatureList> scoped_feature_list_;
  92. };
  93. INSTANTIATE_TEST_SUITE_P(All,
  94. SessionStateNotificationBlockerTest,
  95. testing::Bool() /* IsNotificationsRefreshEnabled() */);
  96. TEST_P(SessionStateNotificationBlockerTest, BaseTest) {
  97. // OOBE.
  98. GetSessionControllerClient()->SetSessionState(SessionState::OOBE);
  99. EXPECT_EQ(0, GetStateChangedCountAndReset());
  100. message_center::NotifierId notifier_id(
  101. message_center::NotifierType::APPLICATION, "test-notifier");
  102. EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id));
  103. EXPECT_FALSE(ShouldShowNotification(notifier_id));
  104. // Login screen.
  105. GetSessionControllerClient()->SetSessionState(SessionState::LOGIN_PRIMARY);
  106. EXPECT_EQ(0, GetStateChangedCountAndReset());
  107. EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id));
  108. EXPECT_FALSE(ShouldShowNotification(notifier_id));
  109. // Logged in as a normal user.
  110. SimulateUserLogin("user@test.com");
  111. EXPECT_EQ(1, GetStateChangedCountAndReset());
  112. EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id));
  113. EXPECT_TRUE(ShouldShowNotification(notifier_id));
  114. // Lock.
  115. SetLockedState(true);
  116. EXPECT_EQ(1, GetStateChangedCountAndReset());
  117. EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id));
  118. EXPECT_TRUE(ShouldShowNotification(notifier_id));
  119. // Unlock.
  120. SetLockedState(false);
  121. EXPECT_EQ(1, GetStateChangedCountAndReset());
  122. EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id));
  123. EXPECT_TRUE(ShouldShowNotification(notifier_id));
  124. }
  125. TEST_P(SessionStateNotificationBlockerTest, AlwaysAllowedNotifier) {
  126. // NOTIFIER_DISPLAY is allowed to shown in the login screen.
  127. message_center::NotifierId notifier_id(
  128. message_center::NotifierType::SYSTEM_COMPONENT, kNotifierSystemPriority,
  129. NotificationCatalogName::kTestCatalogName);
  130. // OOBE.
  131. GetSessionControllerClient()->SetSessionState(SessionState::OOBE);
  132. EXPECT_EQ(0, GetStateChangedCountAndReset());
  133. EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id));
  134. EXPECT_TRUE(ShouldShowNotification(notifier_id));
  135. // Login screen.
  136. GetSessionControllerClient()->SetSessionState(SessionState::LOGIN_PRIMARY);
  137. EXPECT_EQ(0, GetStateChangedCountAndReset());
  138. EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id));
  139. EXPECT_TRUE(ShouldShowNotification(notifier_id));
  140. // Logged in as a normal user.
  141. SimulateUserLogin("user@test.com");
  142. EXPECT_EQ(1, GetStateChangedCountAndReset());
  143. EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id));
  144. EXPECT_TRUE(ShouldShowNotification(notifier_id));
  145. // Lock.
  146. SetLockedState(true);
  147. EXPECT_EQ(1, GetStateChangedCountAndReset());
  148. EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id));
  149. EXPECT_TRUE(ShouldShowNotification(notifier_id));
  150. // Unlock.
  151. SetLockedState(false);
  152. EXPECT_EQ(1, GetStateChangedCountAndReset());
  153. EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id));
  154. EXPECT_TRUE(ShouldShowNotification(notifier_id));
  155. }
  156. TEST_P(SessionStateNotificationBlockerTest, BlockOnPrefService) {
  157. // OOBE.
  158. GetSessionControllerClient()->SetSessionState(SessionState::OOBE);
  159. EXPECT_EQ(0, GetStateChangedCountAndReset());
  160. message_center::NotifierId notifier_id(
  161. message_center::NotifierType::APPLICATION, "test-notifier");
  162. EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id));
  163. // Login screen.
  164. GetSessionControllerClient()->SetSessionState(SessionState::LOGIN_PRIMARY);
  165. EXPECT_EQ(0, GetStateChangedCountAndReset());
  166. EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id));
  167. // Simulates login event sequence in production code:
  168. // - Add a user session;
  169. // - User session is set as active session;
  170. // - Session state changes to active;
  171. // - User PrefService is initialized sometime later.
  172. const AccountId kUserAccountId = AccountId::FromUserEmail("user@test.com");
  173. TestSessionControllerClient* const session_controller_client =
  174. GetSessionControllerClient();
  175. session_controller_client->AddUserSession(kUserAccountId.GetUserEmail(),
  176. user_manager::USER_TYPE_REGULAR,
  177. false /* provide_pref_service */);
  178. EXPECT_EQ(0, GetStateChangedCountAndReset());
  179. EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id));
  180. session_controller_client->SwitchActiveUser(kUserAccountId);
  181. EXPECT_EQ(0, GetStateChangedCountAndReset());
  182. EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id));
  183. session_controller_client->SetSessionState(SessionState::ACTIVE);
  184. EXPECT_EQ(1, GetStateChangedCountAndReset());
  185. EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id));
  186. session_controller_client->ProvidePrefServiceForUser(kUserAccountId);
  187. EXPECT_EQ(1, GetStateChangedCountAndReset());
  188. EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id));
  189. }
  190. TEST_P(SessionStateNotificationBlockerTest, BlockInKioskMode) {
  191. message_center::NotifierId notifier_id(
  192. message_center::NotifierType::SYSTEM_COMPONENT, kNotifierSystemPriority,
  193. NotificationCatalogName::kTestCatalogName);
  194. EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id));
  195. EXPECT_TRUE(ShouldShowNotification(notifier_id));
  196. SimulateKioskMode(user_manager::USER_TYPE_KIOSK_APP);
  197. EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id));
  198. EXPECT_FALSE(ShouldShowNotification(notifier_id));
  199. }
  200. TEST_P(SessionStateNotificationBlockerTest, DelayAfterLogin) {
  201. SessionStateNotificationBlocker::SetUseLoginNotificationDelayForTest(true);
  202. GetSessionControllerClient()->SetSessionState(SessionState::LOGIN_PRIMARY);
  203. // Logged in as a normal user.
  204. SimulateUserLogin("user@test.com");
  205. // Non system notification should not be shown immediately after login.
  206. message_center::NotifierId notifier_id(
  207. message_center::NotifierType::APPLICATION, "test-notifier");
  208. EXPECT_FALSE(ShouldShowNotification(notifier_id));
  209. // System notification should still be shown.
  210. message_center::NotifierId system_notifier_id(
  211. message_center::NotifierType::SYSTEM_COMPONENT, "system-notifier",
  212. NotificationCatalogName::kTestCatalogName);
  213. EXPECT_TRUE(ShouldShowNotification(system_notifier_id));
  214. // The notification delay should not be enabled for all other tests.
  215. SessionStateNotificationBlocker::SetUseLoginNotificationDelayForTest(false);
  216. }
  217. } // namespace
  218. } // namespace ash