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.
5 #include "ash/session/session_state_delegate.h"
7 #include "base/prefs/pref_service.h"
8 #include "chrome/browser/chromeos/login/login_display_host_impl.h"
9 #include "chrome/browser/chromeos/login/login_manager_test.h"
10 #include "chrome/browser/chromeos/login/multi_profile_user_controller.h"
11 #include "chrome/browser/chromeos/login/startup_utils.h"
12 #include "chrome/browser/chromeos/login/user_adding_screen.h"
13 #include "chrome/browser/chromeos/login/user_manager.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "chrome/common/pref_names.h"
16 #include "chrome/test/base/in_process_browser_test.h"
17 #include "content/public/test/test_utils.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 using namespace testing;
24 const char* kTestUsers[] = {"test-user1@gmail.com",
25 "test-user2@gmail.com",
26 "test-user3@gmail.com"};
28 } // anonymous namespace
32 class UserAddingScreenTest : public LoginManagerTest,
33 public UserAddingScreen::Observer {
35 UserAddingScreenTest() : LoginManagerTest(false),
36 user_adding_started_(0),
37 user_adding_finished_(0) {
40 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
41 LoginManagerTest::SetUpCommandLine(command_line);
42 command_line->AppendSwitch(::switches::kMultiProfiles);
45 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
46 LoginManagerTest::SetUpInProcessBrowserTestFixture();
47 UserAddingScreen::Get()->AddObserver(this);
50 virtual void OnUserAddingFinished() OVERRIDE { ++user_adding_finished_; }
52 virtual void OnUserAddingStarted() OVERRIDE { ++user_adding_started_; }
54 void SetUserCanLock(User* user, bool can_lock) {
55 user->set_can_lock(can_lock);
58 int user_adding_started() { return user_adding_started_; }
60 int user_adding_finished() { return user_adding_finished_; }
63 int user_adding_started_;
64 int user_adding_finished_;
66 DISALLOW_COPY_AND_ASSIGN(UserAddingScreenTest);
69 IN_PROC_BROWSER_TEST_F(UserAddingScreenTest, PRE_CancelAdding) {
70 RegisterUser(kTestUsers[0]);
71 RegisterUser(kTestUsers[1]);
72 RegisterUser(kTestUsers[2]);
73 StartupUtils::MarkOobeCompleted();
76 IN_PROC_BROWSER_TEST_F(UserAddingScreenTest, CancelAdding) {
77 EXPECT_CALL(login_utils(), DoBrowserLaunch(_, _)).Times(1);
78 EXPECT_EQ(3u, UserManager::Get()->GetUsers().size());
79 EXPECT_EQ(0u, UserManager::Get()->GetLoggedInUsers().size());
81 EXPECT_EQ(ash::SessionStateDelegate::SESSION_STATE_LOGIN_PRIMARY,
82 ash::Shell::GetInstance()->session_state_delegate()->
85 LoginUser(kTestUsers[0]);
86 EXPECT_EQ(1u, UserManager::Get()->GetLoggedInUsers().size());
87 EXPECT_EQ(ash::SessionStateDelegate::SESSION_STATE_ACTIVE,
88 ash::Shell::GetInstance()->session_state_delegate()->
91 UserAddingScreen::Get()->Start();
92 content::RunAllPendingInMessageLoop();
93 EXPECT_EQ(1, user_adding_started());
94 EXPECT_EQ(ash::SessionStateDelegate::SESSION_STATE_LOGIN_SECONDARY,
95 ash::Shell::GetInstance()->session_state_delegate()->
98 UserAddingScreen::Get()->Cancel();
99 content::RunAllPendingInMessageLoop();
100 EXPECT_EQ(1, user_adding_finished());
101 EXPECT_EQ(ash::SessionStateDelegate::SESSION_STATE_ACTIVE,
102 ash::Shell::GetInstance()->session_state_delegate()->
105 EXPECT_TRUE(LoginDisplayHostImpl::default_host() == NULL);
106 EXPECT_EQ(1u, UserManager::Get()->GetLoggedInUsers().size());
107 EXPECT_EQ(kTestUsers[0], UserManager::Get()->GetActiveUser()->email());
110 IN_PROC_BROWSER_TEST_F(UserAddingScreenTest, PRE_AddingSeveralUsers) {
111 RegisterUser(kTestUsers[0]);
112 RegisterUser(kTestUsers[1]);
113 RegisterUser(kTestUsers[2]);
114 StartupUtils::MarkOobeCompleted();
117 IN_PROC_BROWSER_TEST_F(UserAddingScreenTest, AddingSeveralUsers) {
118 EXPECT_EQ(ash::SessionStateDelegate::SESSION_STATE_LOGIN_PRIMARY,
119 ash::Shell::GetInstance()->session_state_delegate()->
121 EXPECT_CALL(login_utils(), DoBrowserLaunch(_, _)).Times(3);
122 LoginUser(kTestUsers[0]);
123 EXPECT_EQ(ash::SessionStateDelegate::SESSION_STATE_ACTIVE,
124 ash::Shell::GetInstance()->session_state_delegate()->
127 UserManager* user_manager = UserManager::Get();
129 for (int i = 1; i < 3; ++i) {
130 UserAddingScreen::Get()->Start();
131 content::RunAllPendingInMessageLoop();
132 EXPECT_EQ(i, user_adding_started());
133 EXPECT_EQ(ash::SessionStateDelegate::SESSION_STATE_LOGIN_SECONDARY,
134 ash::Shell::GetInstance()->session_state_delegate()->
136 AddUser(kTestUsers[i]);
137 EXPECT_EQ(i, user_adding_finished());
138 EXPECT_EQ(ash::SessionStateDelegate::SESSION_STATE_ACTIVE,
139 ash::Shell::GetInstance()->session_state_delegate()->
141 EXPECT_TRUE(LoginDisplayHostImpl::default_host() == NULL);
142 ASSERT_EQ(unsigned(i + 1), user_manager->GetLoggedInUsers().size());
145 EXPECT_EQ(ash::SessionStateDelegate::SESSION_STATE_ACTIVE,
146 ash::Shell::GetInstance()->session_state_delegate()->
149 // Now check how unlock policy works for these users.
150 PrefService* prefs1 = user_manager->
151 GetProfileByUser(user_manager->GetLoggedInUsers()[0])->GetPrefs();
152 PrefService* prefs2 = user_manager->
153 GetProfileByUser(user_manager->GetLoggedInUsers()[1])->GetPrefs();
154 PrefService* prefs3 = user_manager->
155 GetProfileByUser(user_manager->GetLoggedInUsers()[2])->GetPrefs();
156 ASSERT_TRUE(prefs1 != NULL);
157 ASSERT_TRUE(prefs2 != NULL);
158 ASSERT_TRUE(prefs3 != NULL);
159 prefs1->SetBoolean(prefs::kEnableAutoScreenLock, false);
160 prefs2->SetBoolean(prefs::kEnableAutoScreenLock, false);
161 prefs3->SetBoolean(prefs::kEnableAutoScreenLock, false);
163 // One of the users has the primary-only policy.
164 // List of unlock users doesn't depend on kEnableLockScreen preference.
165 prefs1->SetBoolean(prefs::kEnableAutoScreenLock, true);
166 prefs1->SetString(prefs::kMultiProfileUserBehavior,
167 MultiProfileUserController::kBehaviorPrimaryOnly);
168 prefs2->SetString(prefs::kMultiProfileUserBehavior,
169 MultiProfileUserController::kBehaviorUnrestricted);
170 prefs3->SetString(prefs::kMultiProfileUserBehavior,
171 MultiProfileUserController::kBehaviorUnrestricted);
172 chromeos::UserList unlock_users = user_manager->GetUnlockUsers();
173 ASSERT_EQ(1UL, unlock_users.size());
174 EXPECT_EQ(kTestUsers[0], unlock_users[0]->email());
176 prefs1->SetBoolean(prefs::kEnableAutoScreenLock, false);
177 unlock_users = user_manager->GetUnlockUsers();
178 ASSERT_EQ(1UL, unlock_users.size());
179 EXPECT_EQ(kTestUsers[0], unlock_users[0]->email());
181 // If all users have unrestricted policy then anyone can perform unlock.
182 prefs1->SetString(prefs::kMultiProfileUserBehavior,
183 MultiProfileUserController::kBehaviorUnrestricted);
184 unlock_users = user_manager->GetUnlockUsers();
185 ASSERT_EQ(3UL, unlock_users.size());
186 for (int i = 0; i < 3; ++i)
187 EXPECT_EQ(kTestUsers[i], unlock_users[i]->email());
189 // This preference doesn't affect list of unlock users.
190 prefs2->SetBoolean(prefs::kEnableAutoScreenLock, true);
191 unlock_users = user_manager->GetUnlockUsers();
192 ASSERT_EQ(3UL, unlock_users.size());
193 for (int i = 0; i < 3; ++i)
194 EXPECT_EQ(kTestUsers[i], unlock_users[i]->email());
196 // Now one of the users is unable to unlock.
197 SetUserCanLock(user_manager->GetLoggedInUsers()[2], false);
198 unlock_users = user_manager->GetUnlockUsers();
199 ASSERT_EQ(2UL, unlock_users.size());
200 for (int i = 0; i < 2; ++i)
201 EXPECT_EQ(kTestUsers[i], unlock_users[i]->email());
202 SetUserCanLock(user_manager->GetLoggedInUsers()[2], true);
204 // Now one of the users has not-allowed policy.
205 // In this scenario this user is not allowed in multi-profile session but
206 // if that user happened to still be part of multi-profile session it should
207 // not be listed on screen lock.
208 prefs3->SetString(prefs::kMultiProfileUserBehavior,
209 MultiProfileUserController::kBehaviorNotAllowed);
210 unlock_users = user_manager->GetUnlockUsers();
211 ASSERT_EQ(2UL, unlock_users.size());
212 for (int i = 0; i < 2; ++i)
213 EXPECT_EQ(kTestUsers[i], unlock_users[i]->email());
216 } // namespace chromeos