login_screen_controller_unittest.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Copyright 2017 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/login/login_screen_controller.h"
  5. #include "ash/constants/ash_pref_names.h"
  6. #include "ash/login/mock_login_screen_client.h"
  7. #include "ash/login/ui/lock_screen.h"
  8. #include "ash/root_window_controller.h"
  9. #include "ash/session/session_controller_impl.h"
  10. #include "ash/session/test_session_controller_client.h"
  11. #include "ash/shell.h"
  12. #include "ash/system/status_area_widget.h"
  13. #include "ash/system/tray/system_tray_notifier.h"
  14. #include "ash/system/unified/unified_system_tray.h"
  15. #include "ash/test/ash_test_base.h"
  16. #include "ash/test/ash_test_util.h"
  17. #include "ash/wallpaper/wallpaper_controller_impl.h"
  18. #include "base/bind.h"
  19. #include "base/run_loop.h"
  20. #include "base/test/bind.h"
  21. #include "components/prefs/pref_service.h"
  22. #include "components/session_manager/session_manager_types.h"
  23. #include "components/user_manager/known_user.h"
  24. using session_manager::SessionState;
  25. using ::testing::_;
  26. namespace ash {
  27. namespace {
  28. class LoginScreenControllerTest : public AshTestBase {
  29. public:
  30. LoginScreenControllerTest() {
  31. user_manager::KnownUser::RegisterPrefs(local_state()->registry());
  32. }
  33. };
  34. using LoginScreenControllerNoSessionTest = NoSessionAshTestBase;
  35. // Enum instead of enum class, because it is used for indexing.
  36. enum WindowType { kPrimary = 0, kSecondary = 1 };
  37. TEST_F(LoginScreenControllerTest, RequestAuthentication) {
  38. LoginScreenController* controller = Shell::Get()->login_screen_controller();
  39. auto client = std::make_unique<MockLoginScreenClient>();
  40. AccountId id = AccountId::FromUserEmail("user1@test.com");
  41. std::string password = "password";
  42. // Verify AuthenticateUser mojo call is run with the same account id, a
  43. // (hashed) password, and the correct PIN state.
  44. EXPECT_CALL(*client,
  45. AuthenticateUserWithPasswordOrPin_(id, password, false, _));
  46. absl::optional<bool> callback_result;
  47. base::RunLoop run_loop1;
  48. controller->AuthenticateUserWithPasswordOrPin(
  49. id, password, false,
  50. base::BindLambdaForTesting([&](absl::optional<bool> did_auth) {
  51. callback_result = did_auth;
  52. run_loop1.Quit();
  53. }));
  54. run_loop1.Run();
  55. EXPECT_TRUE(callback_result.has_value());
  56. EXPECT_TRUE(*callback_result);
  57. // Verify that pin is hashed correctly.
  58. PrefService* prefs =
  59. Shell::Get()->session_controller()->GetLastActiveUserPrefService();
  60. EXPECT_TRUE(prefs->FindPreference(prefs::kQuickUnlockPinSalt));
  61. // Use a long PIN (N > 2^64) for the test to ensure that there is no overflow.
  62. std::string pin = "12345678901234567890";
  63. EXPECT_CALL(*client, AuthenticateUserWithPasswordOrPin_(id, pin, true, _));
  64. base::RunLoop run_loop2;
  65. controller->AuthenticateUserWithPasswordOrPin(
  66. id, pin, true,
  67. base::BindLambdaForTesting([&](absl::optional<bool> did_auth) {
  68. callback_result = did_auth;
  69. run_loop2.Quit();
  70. }));
  71. run_loop2.Run();
  72. EXPECT_TRUE(callback_result.has_value());
  73. EXPECT_TRUE(*callback_result);
  74. }
  75. TEST_F(LoginScreenControllerTest, RequestEasyUnlock) {
  76. LoginScreenController* controller = Shell::Get()->login_screen_controller();
  77. auto client = std::make_unique<MockLoginScreenClient>();
  78. AccountId id = AccountId::FromUserEmail("user1@test.com");
  79. // Verify AttemptUnlock mojo call is run with the same account id.
  80. EXPECT_CALL(*client, AuthenticateUserWithEasyUnlock(id));
  81. controller->AuthenticateUserWithEasyUnlock(id);
  82. base::RunLoop().RunUntilIdle();
  83. // Verify HardlockPod mojo call is run with the same account id.
  84. EXPECT_CALL(*client, HardlockPod(id));
  85. controller->HardlockPod(id);
  86. base::RunLoop().RunUntilIdle();
  87. }
  88. TEST_F(LoginScreenControllerTest, RequestUserPodFocus) {
  89. LoginScreenController* controller = Shell::Get()->login_screen_controller();
  90. auto client = std::make_unique<MockLoginScreenClient>();
  91. AccountId id = AccountId::FromUserEmail("user1@test.com");
  92. // Verify FocusPod mojo call is run with the same account id.
  93. EXPECT_CALL(*client, OnFocusPod(id));
  94. controller->OnFocusPod(id);
  95. base::RunLoop().RunUntilIdle();
  96. // Verify NoPodFocused mojo call is run.
  97. EXPECT_CALL(*client, OnNoPodFocused());
  98. controller->OnNoPodFocused();
  99. base::RunLoop().RunUntilIdle();
  100. }
  101. TEST_F(LoginScreenControllerNoSessionTest, ShowSystemTrayOnPrimaryLoginScreen) {
  102. // Create setup with 2 displays primary and secondary.
  103. UpdateDisplay("800x600,800x600");
  104. aura::Window::Windows root_windows = Shell::GetAllRootWindows();
  105. ASSERT_EQ(2u, root_windows.size());
  106. EXPECT_FALSE(ash::LockScreen::HasInstance());
  107. EXPECT_FALSE(IsSystemTrayForRootWindowVisible(WindowType::kPrimary));
  108. EXPECT_FALSE(IsSystemTrayForRootWindowVisible(WindowType::kSecondary));
  109. // Show login screen.
  110. GetSessionControllerClient()->SetSessionState(SessionState::LOGIN_PRIMARY);
  111. Shell::Get()->login_screen_controller()->ShowLoginScreen();
  112. EXPECT_TRUE(ash::LockScreen::HasInstance());
  113. EXPECT_TRUE(IsSystemTrayForRootWindowVisible(WindowType::kPrimary));
  114. EXPECT_FALSE(IsSystemTrayForRootWindowVisible(WindowType::kSecondary));
  115. ash::LockScreen::Get()->Destroy();
  116. }
  117. TEST_F(LoginScreenControllerNoSessionTest,
  118. SystemTrayVisibilityOnSecondaryScreenRestored) {
  119. // Create setup with 2 displays primary and secondary.
  120. UpdateDisplay("800x600,800x600");
  121. aura::Window::Windows root_windows = Shell::GetAllRootWindows();
  122. // Show login screen, then hide it.
  123. GetSessionControllerClient()->SetSessionState(SessionState::LOGIN_PRIMARY);
  124. Shell::Get()->login_screen_controller()->ShowLoginScreen();
  125. ash::LockScreen::Get()->Destroy();
  126. // The system tray should be visible on the secondary screen.
  127. EXPECT_TRUE(IsSystemTrayForRootWindowVisible(WindowType::kSecondary));
  128. }
  129. TEST_F(LoginScreenControllerTest, ShowSystemTrayOnPrimaryLockScreen) {
  130. // Create setup with 2 displays primary and secondary.
  131. UpdateDisplay("800x600,800x600");
  132. aura::Window::Windows root_windows = Shell::GetAllRootWindows();
  133. ASSERT_EQ(2u, root_windows.size());
  134. GetSessionControllerClient()->SetSessionState(SessionState::ACTIVE);
  135. EXPECT_FALSE(ash::LockScreen::HasInstance());
  136. EXPECT_TRUE(IsSystemTrayForRootWindowVisible(WindowType::kPrimary));
  137. EXPECT_TRUE(IsSystemTrayForRootWindowVisible(WindowType::kSecondary));
  138. // Show lock screen.
  139. GetSessionControllerClient()->SetSessionState(SessionState::LOCKED);
  140. Shell::Get()->login_screen_controller()->ShowLockScreen();
  141. EXPECT_TRUE(ash::LockScreen::HasInstance());
  142. EXPECT_TRUE(IsSystemTrayForRootWindowVisible(WindowType::kPrimary));
  143. EXPECT_FALSE(IsSystemTrayForRootWindowVisible(WindowType::kSecondary));
  144. ash::LockScreen::Get()->Destroy();
  145. }
  146. TEST_F(LoginScreenControllerTest, ShowLoginScreenRequiresWallpaper) {
  147. // Show login screen.
  148. EXPECT_FALSE(ash::LockScreen::HasInstance());
  149. GetSessionControllerClient()->SetSessionState(SessionState::LOGIN_PRIMARY);
  150. Shell::Get()->login_screen_controller()->ShowLoginScreen();
  151. // Verify the instance has been created, but the login screen is not actually
  152. // shown yet because there's no wallpaper.
  153. EXPECT_TRUE(ash::LockScreen::HasInstance());
  154. EXPECT_FALSE(ash::LockScreen::Get()->is_shown());
  155. // Set the wallpaper. Verify the login screen is shown.
  156. Shell::Get()->wallpaper_controller()->ShowDefaultWallpaperForTesting();
  157. base::RunLoop().RunUntilIdle();
  158. EXPECT_TRUE(ash::LockScreen::Get()->is_shown());
  159. ash::LockScreen::Get()->Destroy();
  160. }
  161. TEST_F(LoginScreenControllerTest, SystemTrayFocus) {
  162. auto client = std::make_unique<MockLoginScreenClient>();
  163. EXPECT_CALL(*client, OnFocusLeavingSystemTray(true)).Times(1);
  164. Shell::Get()->system_tray_notifier()->NotifyFocusOut(true);
  165. EXPECT_CALL(*client, OnFocusLeavingSystemTray(false)).Times(1);
  166. Shell::Get()->system_tray_notifier()->NotifyFocusOut(false);
  167. }
  168. } // namespace
  169. } // namespace ash