detachable_base_notification_controller_unittest.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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/detachable_base/detachable_base_notification_controller.h"
  5. #include <string>
  6. #include "ash/detachable_base/detachable_base_handler.h"
  7. #include "ash/public/cpp/session/user_info.h"
  8. #include "ash/session/session_controller_impl.h"
  9. #include "ash/session/test_session_controller_client.h"
  10. #include "ash/shell.h"
  11. #include "ash/test/ash_test_base.h"
  12. #include "base/time/time.h"
  13. #include "chromeos/dbus/power/fake_power_manager_client.h"
  14. #include "components/account_id/account_id.h"
  15. #include "components/user_manager/user_type.h"
  16. #include "ui/message_center/message_center.h"
  17. namespace ash {
  18. UserInfo CreateTestUserInfo(const std::string& user_email) {
  19. UserInfo user_info;
  20. user_info.type = user_manager::USER_TYPE_REGULAR;
  21. user_info.account_id = AccountId::FromUserEmail(user_email);
  22. user_info.display_name = "Test user";
  23. user_info.display_email = user_email;
  24. user_info.is_ephemeral = false;
  25. user_info.is_new_profile = false;
  26. return user_info;
  27. }
  28. class DetachableBaseNotificationControllerTest : public NoSessionAshTestBase {
  29. public:
  30. DetachableBaseNotificationControllerTest() = default;
  31. DetachableBaseNotificationControllerTest(
  32. const DetachableBaseNotificationControllerTest&) = delete;
  33. DetachableBaseNotificationControllerTest& operator=(
  34. const DetachableBaseNotificationControllerTest&) = delete;
  35. ~DetachableBaseNotificationControllerTest() override = default;
  36. void SetUp() override {
  37. AshTestBase::SetUp();
  38. chromeos::FakePowerManagerClient::Get()->SetTabletMode(
  39. chromeos::PowerManagerClient::TabletMode::OFF, base::TimeTicks());
  40. }
  41. bool IsBaseChangedNotificationVisible() {
  42. return message_center::MessageCenter::Get()->FindVisibleNotificationById(
  43. DetachableBaseNotificationController::kBaseChangedNotificationId);
  44. }
  45. bool IsBaseRequiresUpdateNotificationVisible() {
  46. return message_center::MessageCenter::Get()->FindVisibleNotificationById(
  47. DetachableBaseNotificationController::
  48. kBaseRequiresUpdateNotificationId);
  49. }
  50. void CloseBaseChangedNotification() {
  51. message_center::MessageCenter::Get()->RemoveNotification(
  52. DetachableBaseNotificationController::kBaseChangedNotificationId,
  53. true /*by_user*/);
  54. }
  55. DetachableBaseHandler* detachable_base_handler() {
  56. return Shell::Get()->detachable_base_handler();
  57. }
  58. SessionControllerImpl* session_controller() {
  59. return Shell::Get()->session_controller();
  60. }
  61. };
  62. TEST_F(DetachableBaseNotificationControllerTest,
  63. ShowPairingNotificationIfSessionNotBlocked) {
  64. CreateUserSessions(1);
  65. // The first detachable base used by the user - no notification expected.
  66. detachable_base_handler()->PairChallengeSucceeded({0x01, 0x01});
  67. EXPECT_FALSE(IsBaseChangedNotificationVisible());
  68. // If the user changes the paired base in session, the detachable base change
  69. // notification should be shown.
  70. detachable_base_handler()->PairChallengeSucceeded({0x02, 0x02});
  71. EXPECT_TRUE(IsBaseChangedNotificationVisible());
  72. CloseBaseChangedNotification();
  73. EXPECT_FALSE(IsBaseChangedNotificationVisible());
  74. // Verify that the notification is reshown if the base changes again.
  75. detachable_base_handler()->PairChallengeSucceeded({0x03, 0x03});
  76. EXPECT_TRUE(IsBaseChangedNotificationVisible());
  77. }
  78. TEST_F(DetachableBaseNotificationControllerTest,
  79. ShowNotificationOnNonAuthenticatedBases) {
  80. CreateUserSessions(1);
  81. detachable_base_handler()->PairChallengeFailed();
  82. EXPECT_TRUE(IsBaseChangedNotificationVisible());
  83. }
  84. TEST_F(DetachableBaseNotificationControllerTest,
  85. UpdateNotificationOnUserSwitch) {
  86. CreateUserSessions(1);
  87. // The first detachable base used by the user - no notification expected.
  88. detachable_base_handler()->PairChallengeSucceeded({0x01, 0x01});
  89. EXPECT_FALSE(IsBaseChangedNotificationVisible());
  90. SimulateUserLogin("secondary_user@test.com");
  91. EXPECT_FALSE(IsBaseChangedNotificationVisible());
  92. detachable_base_handler()->PairChallengeSucceeded({0x02, 0x02});
  93. EXPECT_TRUE(IsBaseChangedNotificationVisible());
  94. CloseBaseChangedNotification();
  95. GetSessionControllerClient()->SwitchActiveUser(
  96. session_controller()->GetUserSession(1)->user_info.account_id);
  97. EXPECT_TRUE(IsBaseChangedNotificationVisible());
  98. }
  99. TEST_F(DetachableBaseNotificationControllerTest,
  100. NonAuthenticatedBaseNotificationOnUserSwitch) {
  101. CreateUserSessions(1);
  102. detachable_base_handler()->PairChallengeFailed();
  103. EXPECT_TRUE(IsBaseChangedNotificationVisible());
  104. CloseBaseChangedNotification();
  105. SimulateUserLogin("secondary_user@test.com");
  106. EXPECT_TRUE(IsBaseChangedNotificationVisible());
  107. }
  108. TEST_F(DetachableBaseNotificationControllerTest,
  109. NoNotificationIfSessionNotStarted) {
  110. const char kTestUser[] = "user_1@test.com";
  111. UserInfo test_user_info = CreateTestUserInfo(kTestUser);
  112. // Set a detachable base as previously used by the user before log in.
  113. detachable_base_handler()->PairChallengeSucceeded({0x01, 0x01});
  114. EXPECT_TRUE(
  115. detachable_base_handler()->SetPairedBaseAsLastUsedByUser(test_user_info));
  116. // Set up another detachable base as attached when the user logs in.
  117. detachable_base_handler()->PairChallengeSucceeded({0x02, 0x02});
  118. // No active user, so the notification should not be shown, yet.
  119. EXPECT_FALSE(IsBaseChangedNotificationVisible());
  120. SimulateUserLogin(kTestUser);
  121. EXPECT_TRUE(IsBaseChangedNotificationVisible());
  122. }
  123. TEST_F(DetachableBaseNotificationControllerTest,
  124. NoNotificationOnSessionStartIfBaseMarkedAsLastUsed) {
  125. const char kTestUser[] = "user_1@test.com";
  126. UserInfo test_user_info = CreateTestUserInfo(kTestUser);
  127. // Set a detachable base as previously used by the user before log in.
  128. detachable_base_handler()->PairChallengeSucceeded({0x01, 0x01});
  129. EXPECT_TRUE(
  130. detachable_base_handler()->SetPairedBaseAsLastUsedByUser(test_user_info));
  131. // Set up another detachable base as attached when the user logs in.
  132. detachable_base_handler()->PairChallengeSucceeded({0x02, 0x02});
  133. // No active user, so the notification should not be shown, yet.
  134. EXPECT_FALSE(IsBaseChangedNotificationVisible());
  135. // Mark the current device as last used by the user, and verify there is no
  136. // notification when the user logs in.
  137. EXPECT_TRUE(
  138. detachable_base_handler()->SetPairedBaseAsLastUsedByUser(test_user_info));
  139. SimulateUserLogin(kTestUser);
  140. EXPECT_FALSE(IsBaseChangedNotificationVisible());
  141. }
  142. // Tests that a notification for non authenticated base is not shown before the
  143. // session is started - the login UI will show a custom UI to inform the user
  144. // about the base.
  145. TEST_F(DetachableBaseNotificationControllerTest,
  146. NonAuthenticatedBaseNotificationNotShownBeforeLogin) {
  147. detachable_base_handler()->PairChallengeFailed();
  148. EXPECT_FALSE(IsBaseChangedNotificationVisible());
  149. CreateUserSessions(1);
  150. EXPECT_TRUE(IsBaseChangedNotificationVisible());
  151. }
  152. TEST_F(DetachableBaseNotificationControllerTest, NoNotificationOnLockScreen) {
  153. CreateUserSessions(1);
  154. // The first detachable base used by the user - no notification expected.
  155. detachable_base_handler()->PairChallengeSucceeded({0x01, 0x01});
  156. BlockUserSession(UserSessionBlockReason::BLOCKED_BY_LOCK_SCREEN);
  157. detachable_base_handler()->PairChallengeSucceeded({0x02, 0x02});
  158. EXPECT_FALSE(IsBaseChangedNotificationVisible());
  159. UnblockUserSession();
  160. EXPECT_TRUE(IsBaseChangedNotificationVisible());
  161. }
  162. TEST_F(DetachableBaseNotificationControllerTest,
  163. NoNotificationAfterLockScreenIfSetAsUsed) {
  164. CreateUserSessions(1);
  165. // The first detachable base used by the user - no notification expected.
  166. detachable_base_handler()->PairChallengeSucceeded({0x01, 0x01});
  167. BlockUserSession(UserSessionBlockReason::BLOCKED_BY_LOCK_SCREEN);
  168. detachable_base_handler()->PairChallengeSucceeded({0x02, 0x02});
  169. EXPECT_FALSE(IsBaseChangedNotificationVisible());
  170. EXPECT_TRUE(detachable_base_handler()->SetPairedBaseAsLastUsedByUser(
  171. session_controller()->GetUserSession(0)->user_info));
  172. UnblockUserSession();
  173. EXPECT_FALSE(IsBaseChangedNotificationVisible());
  174. }
  175. // Tests that a notification for non authenticated base is not shown before the
  176. // session is started - the lock UI will show a custom UI to inform the user
  177. // about the base.
  178. TEST_F(DetachableBaseNotificationControllerTest,
  179. NonAuthenticatedBaseNotificationNotShownOnLock) {
  180. BlockUserSession(UserSessionBlockReason::BLOCKED_BY_LOCK_SCREEN);
  181. detachable_base_handler()->PairChallengeFailed();
  182. EXPECT_FALSE(IsBaseChangedNotificationVisible());
  183. UnblockUserSession();
  184. EXPECT_TRUE(IsBaseChangedNotificationVisible());
  185. }
  186. TEST_F(DetachableBaseNotificationControllerTest, NotificationOnUpdateRequired) {
  187. CreateUserSessions(1);
  188. detachable_base_handler()->BaseFirmwareUpdateNeeded();
  189. EXPECT_TRUE(IsBaseRequiresUpdateNotificationVisible());
  190. // The notification should be removed when the base gets detached.
  191. chromeos::FakePowerManagerClient::Get()->SetTabletMode(
  192. chromeos::PowerManagerClient::TabletMode::ON, base::TimeTicks());
  193. EXPECT_FALSE(IsBaseRequiresUpdateNotificationVisible());
  194. }
  195. TEST_F(DetachableBaseNotificationControllerTest,
  196. NotificationOnUpdateRequiredBeforeLogin) {
  197. // Update requirement detected before login - expect the update required
  198. // notification to be hidden.
  199. detachable_base_handler()->BaseFirmwareUpdateNeeded();
  200. EXPECT_FALSE(IsBaseRequiresUpdateNotificationVisible());
  201. // Login, expect the notification to be shown.
  202. CreateUserSessions(1);
  203. EXPECT_TRUE(IsBaseRequiresUpdateNotificationVisible());
  204. // The notification should be removed when the base gets detached.
  205. chromeos::FakePowerManagerClient::Get()->SetTabletMode(
  206. chromeos::PowerManagerClient::TabletMode::ON, base::TimeTicks());
  207. EXPECT_FALSE(IsBaseRequiresUpdateNotificationVisible());
  208. }
  209. TEST_F(DetachableBaseNotificationControllerTest,
  210. NotificationOnUpdateRequiredOnLockScreen) {
  211. // Update requirement detected while the session is blocked by the lock
  212. // screen - expect the update required notification to be shown.
  213. BlockUserSession(UserSessionBlockReason::BLOCKED_BY_LOCK_SCREEN);
  214. detachable_base_handler()->BaseFirmwareUpdateNeeded();
  215. EXPECT_TRUE(IsBaseRequiresUpdateNotificationVisible());
  216. // The notification should be removed when the base gets detached.
  217. chromeos::FakePowerManagerClient::Get()->SetTabletMode(
  218. chromeos::PowerManagerClient::TabletMode::ON, base::TimeTicks());
  219. EXPECT_FALSE(IsBaseRequiresUpdateNotificationVisible());
  220. }
  221. } // namespace ash