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.
5 #include "ash/session/session_state_delegate.h"
7 #include "base/prefs/pref_service.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/chromeos/login/lock/screen_locker.h"
10 #include "chrome/browser/chromeos/login/lock/screen_locker_tester.h"
11 #include "chrome/browser/chromeos/login/login_manager_test.h"
12 #include "chrome/browser/chromeos/login/startup_utils.h"
13 #include "chrome/browser/chromeos/login/ui/login_display_host_impl.h"
14 #include "chrome/browser/chromeos/login/ui/user_adding_screen.h"
15 #include "chrome/browser/chromeos/login/ui/webui_login_view.h"
16 #include "chrome/browser/chromeos/login/users/multi_profile_user_controller.h"
17 #include "chrome/browser/chromeos/profiles/profile_helper.h"
18 #include "chrome/common/pref_names.h"
19 #include "chrome/test/base/in_process_browser_test.h"
20 #include "components/user_manager/user_manager.h"
21 #include "content/public/test/test_utils.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 using namespace testing
;
28 const char* const kTestUsers
[] = {"test-user1@gmail.com",
29 "test-user2@gmail.com",
30 "test-user3@gmail.com"};
32 } // anonymous namespace
36 class UserAddingScreenTest
: public LoginManagerTest
,
37 public UserAddingScreen::Observer
{
39 UserAddingScreenTest() : LoginManagerTest(false),
40 user_adding_started_(0),
41 user_adding_finished_(0) {
44 void SetUpInProcessBrowserTestFixture() override
{
45 LoginManagerTest::SetUpInProcessBrowserTestFixture();
46 UserAddingScreen::Get()->AddObserver(this);
49 void OnUserAddingFinished() override
{ ++user_adding_finished_
; }
51 void OnUserAddingStarted() override
{ ++user_adding_started_
; }
53 void SetUserCanLock(user_manager::User
* user
, bool can_lock
) {
54 user
->set_can_lock(can_lock
);
57 void CheckScreenIsVisible() {
58 views::View
* web_view
=
59 LoginDisplayHostImpl::default_host()->GetWebUILoginView()->child_at(0);
60 for (views::View
* current_view
= web_view
;
62 current_view
= current_view
->parent()) {
63 EXPECT_TRUE(current_view
->visible());
64 if (current_view
->layer())
65 EXPECT_EQ(current_view
->layer()->GetCombinedOpacity(), 1.f
);
67 for (aura::Window
* window
= web_view
->GetWidget()->GetNativeWindow();
69 window
= window
->parent()) {
70 EXPECT_TRUE(window
->IsVisible());
71 EXPECT_EQ(window
->layer()->GetCombinedOpacity(), 1.f
);
75 int user_adding_started() { return user_adding_started_
; }
77 int user_adding_finished() { return user_adding_finished_
; }
80 int user_adding_started_
;
81 int user_adding_finished_
;
83 DISALLOW_COPY_AND_ASSIGN(UserAddingScreenTest
);
86 IN_PROC_BROWSER_TEST_F(UserAddingScreenTest
, PRE_CancelAdding
) {
87 RegisterUser(kTestUsers
[0]);
88 RegisterUser(kTestUsers
[1]);
89 RegisterUser(kTestUsers
[2]);
90 StartupUtils::MarkOobeCompleted();
93 IN_PROC_BROWSER_TEST_F(UserAddingScreenTest
, CancelAdding
) {
94 EXPECT_EQ(3u, user_manager::UserManager::Get()->GetUsers().size());
95 EXPECT_EQ(0u, user_manager::UserManager::Get()->GetLoggedInUsers().size());
97 EXPECT_EQ(ash::SessionStateDelegate::SESSION_STATE_LOGIN_PRIMARY
,
98 ash::Shell::GetInstance()->session_state_delegate()->
101 LoginUser(kTestUsers
[0]);
102 EXPECT_EQ(1u, user_manager::UserManager::Get()->GetLoggedInUsers().size());
103 EXPECT_EQ(ash::SessionStateDelegate::SESSION_STATE_ACTIVE
,
104 ash::Shell::GetInstance()->session_state_delegate()->
107 UserAddingScreen::Get()->Start();
108 content::RunAllPendingInMessageLoop();
109 EXPECT_EQ(1, user_adding_started());
110 EXPECT_EQ(ash::SessionStateDelegate::SESSION_STATE_LOGIN_SECONDARY
,
111 ash::Shell::GetInstance()->session_state_delegate()->
114 UserAddingScreen::Get()->Cancel();
115 content::RunAllPendingInMessageLoop();
116 EXPECT_EQ(1, user_adding_finished());
117 EXPECT_EQ(ash::SessionStateDelegate::SESSION_STATE_ACTIVE
,
118 ash::Shell::GetInstance()->session_state_delegate()->
121 EXPECT_TRUE(LoginDisplayHostImpl::default_host() == NULL
);
122 EXPECT_EQ(1u, user_manager::UserManager::Get()->GetLoggedInUsers().size());
123 EXPECT_EQ(kTestUsers
[0],
124 user_manager::UserManager::Get()->GetActiveUser()->email());
127 IN_PROC_BROWSER_TEST_F(UserAddingScreenTest
, PRE_AddingSeveralUsers
) {
128 RegisterUser(kTestUsers
[0]);
129 RegisterUser(kTestUsers
[1]);
130 RegisterUser(kTestUsers
[2]);
131 StartupUtils::MarkOobeCompleted();
134 IN_PROC_BROWSER_TEST_F(UserAddingScreenTest
, AddingSeveralUsers
) {
135 EXPECT_EQ(ash::SessionStateDelegate::SESSION_STATE_LOGIN_PRIMARY
,
136 ash::Shell::GetInstance()->session_state_delegate()->
138 LoginUser(kTestUsers
[0]);
139 EXPECT_EQ(ash::SessionStateDelegate::SESSION_STATE_ACTIVE
,
140 ash::Shell::GetInstance()->session_state_delegate()->
143 user_manager::UserManager
* user_manager
= user_manager::UserManager::Get();
145 for (int i
= 1; i
< 3; ++i
) {
146 UserAddingScreen::Get()->Start();
147 content::RunAllPendingInMessageLoop();
148 EXPECT_EQ(i
, user_adding_started());
149 EXPECT_EQ(ash::SessionStateDelegate::SESSION_STATE_LOGIN_SECONDARY
,
150 ash::Shell::GetInstance()->session_state_delegate()->
152 AddUser(kTestUsers
[i
]);
153 EXPECT_EQ(i
, user_adding_finished());
154 EXPECT_EQ(ash::SessionStateDelegate::SESSION_STATE_ACTIVE
,
155 ash::Shell::GetInstance()->session_state_delegate()->
157 EXPECT_TRUE(LoginDisplayHostImpl::default_host() == NULL
);
158 ASSERT_EQ(unsigned(i
+ 1), user_manager
->GetLoggedInUsers().size());
161 EXPECT_EQ(ash::SessionStateDelegate::SESSION_STATE_ACTIVE
,
162 ash::Shell::GetInstance()->session_state_delegate()->
165 // Now check how unlock policy works for these users.
166 PrefService
* prefs1
=
168 ->GetProfileByUserUnsafe(user_manager
->GetLoggedInUsers()[0])
170 PrefService
* prefs2
=
172 ->GetProfileByUserUnsafe(user_manager
->GetLoggedInUsers()[1])
174 PrefService
* prefs3
=
176 ->GetProfileByUserUnsafe(user_manager
->GetLoggedInUsers()[2])
178 ASSERT_TRUE(prefs1
!= NULL
);
179 ASSERT_TRUE(prefs2
!= NULL
);
180 ASSERT_TRUE(prefs3
!= NULL
);
181 prefs1
->SetBoolean(prefs::kEnableAutoScreenLock
, false);
182 prefs2
->SetBoolean(prefs::kEnableAutoScreenLock
, false);
183 prefs3
->SetBoolean(prefs::kEnableAutoScreenLock
, false);
185 // One of the users has the primary-only policy.
186 // List of unlock users doesn't depend on kEnableLockScreen preference.
187 prefs1
->SetBoolean(prefs::kEnableAutoScreenLock
, true);
188 prefs1
->SetString(prefs::kMultiProfileUserBehavior
,
189 MultiProfileUserController::kBehaviorPrimaryOnly
);
190 prefs2
->SetString(prefs::kMultiProfileUserBehavior
,
191 MultiProfileUserController::kBehaviorUnrestricted
);
192 prefs3
->SetString(prefs::kMultiProfileUserBehavior
,
193 MultiProfileUserController::kBehaviorUnrestricted
);
194 user_manager::UserList unlock_users
= user_manager
->GetUnlockUsers();
195 ASSERT_EQ(1UL, unlock_users
.size());
196 EXPECT_EQ(kTestUsers
[0], unlock_users
[0]->email());
198 prefs1
->SetBoolean(prefs::kEnableAutoScreenLock
, false);
199 unlock_users
= user_manager
->GetUnlockUsers();
200 ASSERT_EQ(1UL, unlock_users
.size());
201 EXPECT_EQ(kTestUsers
[0], unlock_users
[0]->email());
203 // If all users have unrestricted policy then anyone can perform unlock.
204 prefs1
->SetString(prefs::kMultiProfileUserBehavior
,
205 MultiProfileUserController::kBehaviorUnrestricted
);
206 unlock_users
= user_manager
->GetUnlockUsers();
207 ASSERT_EQ(3UL, unlock_users
.size());
208 for (int i
= 0; i
< 3; ++i
)
209 EXPECT_EQ(kTestUsers
[i
], unlock_users
[i
]->email());
211 // This preference doesn't affect list of unlock users.
212 prefs2
->SetBoolean(prefs::kEnableAutoScreenLock
, true);
213 unlock_users
= user_manager
->GetUnlockUsers();
214 ASSERT_EQ(3UL, unlock_users
.size());
215 for (int i
= 0; i
< 3; ++i
)
216 EXPECT_EQ(kTestUsers
[i
], unlock_users
[i
]->email());
218 // Now one of the users is unable to unlock.
219 SetUserCanLock(user_manager
->GetLoggedInUsers()[2], false);
220 unlock_users
= user_manager
->GetUnlockUsers();
221 ASSERT_EQ(2UL, unlock_users
.size());
222 for (int i
= 0; i
< 2; ++i
)
223 EXPECT_EQ(kTestUsers
[i
], unlock_users
[i
]->email());
224 SetUserCanLock(user_manager
->GetLoggedInUsers()[2], true);
226 // Now one of the users has not-allowed policy.
227 // In this scenario this user is not allowed in multi-profile session but
228 // if that user happened to still be part of multi-profile session it should
229 // not be listed on screen lock.
230 prefs3
->SetString(prefs::kMultiProfileUserBehavior
,
231 MultiProfileUserController::kBehaviorNotAllowed
);
232 unlock_users
= user_manager
->GetUnlockUsers();
233 ASSERT_EQ(2UL, unlock_users
.size());
234 for (int i
= 0; i
< 2; ++i
)
235 EXPECT_EQ(kTestUsers
[i
], unlock_users
[i
]->email());
238 IN_PROC_BROWSER_TEST_F(UserAddingScreenTest
, PRE_ScreenVisibility
) {
239 RegisterUser(kTestUsers
[0]);
240 RegisterUser(kTestUsers
[1]);
241 StartupUtils::MarkOobeCompleted();
244 // Trying to catch http://crbug.com/362153.
245 IN_PROC_BROWSER_TEST_F(UserAddingScreenTest
, ScreenVisibility
) {
246 LoginUser(kTestUsers
[0]);
248 UserAddingScreen::Get()->Start();
249 content::RunAllPendingInMessageLoop();
250 CheckScreenIsVisible();
251 UserAddingScreen::Get()->Cancel();
252 content::RunAllPendingInMessageLoop();
254 ScreenLocker::Show();
255 content::WindowedNotificationObserver(
256 chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED
,
257 content::NotificationService::AllSources()).Wait();
259 ScreenLocker::Hide();
260 content::WindowedNotificationObserver(
261 chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED
,
262 content::NotificationService::AllSources()).Wait();
264 UserAddingScreen::Get()->Start();
265 content::RunAllPendingInMessageLoop();
266 CheckScreenIsVisible();
267 UserAddingScreen::Get()->Cancel();
268 content::RunAllPendingInMessageLoop();
271 } // namespace chromeos