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.
7 #include "base/message_loop/message_loop.h"
8 #include "base/values.h"
9 #include "chrome/browser/chromeos/login/existing_user_controller.h"
10 #include "chrome/browser/chromeos/login/ui/mock_login_display.h"
11 #include "chrome/browser/chromeos/login/ui/mock_login_display_host.h"
12 #include "chrome/browser/chromeos/login/users/mock_user_manager.h"
13 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
14 #include "chrome/browser/chromeos/policy/device_local_account.h"
15 #include "chrome/browser/chromeos/settings/cros_settings.h"
16 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h"
17 #include "chrome/test/base/scoped_testing_local_state.h"
18 #include "chrome/test/base/testing_browser_process.h"
19 #include "chromeos/settings/cros_settings_names.h"
20 #include "content/public/test/test_browser_thread.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 using testing::AnyNumber
;
25 using testing::Return
;
26 using testing::ReturnNull
;
33 const char kAutoLoginAccountId
[] = "public_session_user@localhost";
34 // These values are only used to test the configuration. They don't
36 const int kAutoLoginDelay1
= 60000;
37 const int kAutoLoginDelay2
= 180000;
41 class ExistingUserControllerAutoLoginTest
: public ::testing::Test
{
43 ExistingUserControllerAutoLoginTest()
44 : auto_login_user_id_(policy::GenerateDeviceLocalAccountUserId(
46 policy::DeviceLocalAccount::TYPE_PUBLIC_SESSION
)),
47 ui_thread_(content::BrowserThread::UI
, &message_loop_
),
48 local_state_(TestingBrowserProcess::GetGlobal()),
49 mock_user_manager_(new MockUserManager()),
50 scoped_user_manager_(mock_user_manager_
) {
53 void SetUp() override
{
54 mock_login_display_host_
.reset(new MockLoginDisplayHost
);
55 mock_login_display_
= new MockLoginDisplay();
57 EXPECT_CALL(*mock_login_display_host_
.get(), CreateLoginDisplay(_
))
59 .WillOnce(Return(mock_login_display_
));
61 EXPECT_CALL(*mock_user_manager_
, Shutdown()).Times(AnyNumber());
62 EXPECT_CALL(*mock_user_manager_
, FindUser(_
))
63 .WillRepeatedly(ReturnNull());
64 EXPECT_CALL(*mock_user_manager_
, FindUser(auto_login_user_id_
))
65 .WillRepeatedly(Return(
66 mock_user_manager_
->CreatePublicAccountUser(auto_login_user_id_
)));
68 existing_user_controller_
.reset(
69 new ExistingUserController(mock_login_display_host_
.get()));
71 scoped_ptr
<base::DictionaryValue
> account(new base::DictionaryValue
);
72 account
->SetStringWithoutPathExpansion(
73 kAccountsPrefDeviceLocalAccountsKeyId
,
75 account
->SetIntegerWithoutPathExpansion(
76 kAccountsPrefDeviceLocalAccountsKeyType
,
77 policy::DeviceLocalAccount::TYPE_PUBLIC_SESSION
);
78 base::ListValue accounts
;
79 accounts
.Append(account
.release());
80 CrosSettings::Get()->Set(kAccountsPrefDeviceLocalAccounts
, accounts
);
82 // Prevent settings changes from auto-starting the timer.
83 existing_user_controller_
->
84 local_account_auto_login_id_subscription_
.reset();
85 existing_user_controller_
->
86 local_account_auto_login_delay_subscription_
.reset();
89 const ExistingUserController
* existing_user_controller() const {
90 return ExistingUserController::current_controller();
93 ExistingUserController
* existing_user_controller() {
94 return ExistingUserController::current_controller();
97 void SetAutoLoginSettings(const std::string
& account_id
, int delay
) {
98 CrosSettings::Get()->SetString(
99 kAccountsPrefDeviceLocalAccountAutoLoginId
,
101 CrosSettings::Get()->SetInteger(
102 kAccountsPrefDeviceLocalAccountAutoLoginDelay
,
106 // ExistingUserController private member accessors.
107 base::OneShotTimer
<ExistingUserController
>* auto_login_timer() {
108 return existing_user_controller()->auto_login_timer_
.get();
111 const std::string
& auto_login_username() const {
112 return existing_user_controller()->public_session_auto_login_username_
;
114 void set_auto_login_username(const std::string
& username
) {
115 existing_user_controller()->public_session_auto_login_username_
= username
;
118 int auto_login_delay() const {
119 return existing_user_controller()->public_session_auto_login_delay_
;
121 void set_auto_login_delay(int delay
) {
122 existing_user_controller()->public_session_auto_login_delay_
= delay
;
125 bool is_login_in_progress() const {
126 return existing_user_controller()->is_login_in_progress_
;
128 void set_is_login_in_progress(bool is_login_in_progress
) {
129 existing_user_controller()->is_login_in_progress_
= is_login_in_progress
;
132 void ConfigureAutoLogin() {
133 existing_user_controller()->ConfigurePublicSessionAutoLogin();
136 const std::string auto_login_user_id_
;
139 // |mock_login_display_| is owned by the ExistingUserController, which calls
140 // CreateLoginDisplay() on the |mock_login_display_host_| to get it.
141 MockLoginDisplay
* mock_login_display_
;
143 scoped_ptr
<MockLoginDisplayHost
> mock_login_display_host_
;
144 base::MessageLoopForUI message_loop_
;
145 content::TestBrowserThread ui_thread_
;
146 ScopedTestingLocalState local_state_
;
148 // Required by ExistingUserController:
149 ScopedDeviceSettingsTestHelper device_settings_test_helper_
;
150 ScopedTestCrosSettings test_cros_settings_
;
151 MockUserManager
* mock_user_manager_
;
152 ScopedUserManagerEnabler scoped_user_manager_
;
154 // |existing_user_controller_| must be destroyed before
155 // |device_settings_test_helper_|.
156 scoped_ptr
<ExistingUserController
> existing_user_controller_
;
159 TEST_F(ExistingUserControllerAutoLoginTest
, StartAutoLoginTimer
) {
160 // Timer shouldn't start until signin screen is ready.
161 set_auto_login_username(auto_login_user_id_
);
162 set_auto_login_delay(kAutoLoginDelay2
);
163 existing_user_controller()->StartPublicSessionAutoLoginTimer();
164 EXPECT_FALSE(auto_login_timer());
166 // Timer shouldn't start if the policy isn't set.
167 set_auto_login_username("");
168 existing_user_controller()->OnSigninScreenReady();
169 existing_user_controller()->StartPublicSessionAutoLoginTimer();
170 EXPECT_FALSE(auto_login_timer());
172 // Timer shouldn't fire in the middle of a login attempt.
173 set_auto_login_username(auto_login_user_id_
);
174 set_is_login_in_progress(true);
175 existing_user_controller()->StartPublicSessionAutoLoginTimer();
176 EXPECT_FALSE(auto_login_timer());
179 set_is_login_in_progress(false);
180 existing_user_controller()->StartPublicSessionAutoLoginTimer();
181 ASSERT_TRUE(auto_login_timer());
182 EXPECT_TRUE(auto_login_timer()->IsRunning());
183 EXPECT_EQ(auto_login_timer()->GetCurrentDelay().InMilliseconds(),
187 TEST_F(ExistingUserControllerAutoLoginTest
, StopAutoLoginTimer
) {
188 existing_user_controller()->OnSigninScreenReady();
189 set_auto_login_username(auto_login_user_id_
);
190 set_auto_login_delay(kAutoLoginDelay2
);
192 existing_user_controller()->StartPublicSessionAutoLoginTimer();
193 ASSERT_TRUE(auto_login_timer());
194 EXPECT_TRUE(auto_login_timer()->IsRunning());
196 existing_user_controller()->StopPublicSessionAutoLoginTimer();
197 ASSERT_TRUE(auto_login_timer());
198 EXPECT_FALSE(auto_login_timer()->IsRunning());
201 TEST_F(ExistingUserControllerAutoLoginTest
, ResetAutoLoginTimer
) {
202 existing_user_controller()->OnSigninScreenReady();
203 set_auto_login_username(auto_login_user_id_
);
205 // Timer starts off not running.
206 EXPECT_FALSE(auto_login_timer());
208 // When the timer isn't running, nothing should happen.
209 existing_user_controller()->ResetPublicSessionAutoLoginTimer();
210 EXPECT_FALSE(auto_login_timer());
213 set_auto_login_delay(kAutoLoginDelay2
);
214 existing_user_controller()->StartPublicSessionAutoLoginTimer();
215 ASSERT_TRUE(auto_login_timer());
216 EXPECT_TRUE(auto_login_timer()->IsRunning());
217 EXPECT_EQ(auto_login_timer()->GetCurrentDelay().InMilliseconds(),
220 // User activity should restart the timer, so check to see that the
221 // timer delay was modified.
222 set_auto_login_delay(kAutoLoginDelay1
);
223 existing_user_controller()->ResetPublicSessionAutoLoginTimer();
224 ASSERT_TRUE(auto_login_timer());
225 EXPECT_TRUE(auto_login_timer()->IsRunning());
226 EXPECT_EQ(auto_login_timer()->GetCurrentDelay().InMilliseconds(),
230 TEST_F(ExistingUserControllerAutoLoginTest
, ConfigureAutoLogin
) {
231 existing_user_controller()->OnSigninScreenReady();
233 // Timer shouldn't start when the policy is disabled.
234 ConfigureAutoLogin();
235 EXPECT_FALSE(auto_login_timer());
236 EXPECT_EQ(auto_login_delay(), 0);
237 EXPECT_EQ(auto_login_username(), "");
239 // Timer shouldn't start when the delay alone is set.
240 SetAutoLoginSettings("", kAutoLoginDelay1
);
241 ConfigureAutoLogin();
242 EXPECT_FALSE(auto_login_timer());
243 EXPECT_EQ(auto_login_delay(), kAutoLoginDelay1
);
244 EXPECT_EQ(auto_login_username(), "");
246 // Timer should start when the account ID is set.
247 SetAutoLoginSettings(kAutoLoginAccountId
, kAutoLoginDelay1
);
248 ConfigureAutoLogin();
249 ASSERT_TRUE(auto_login_timer());
250 EXPECT_TRUE(auto_login_timer()->IsRunning());
251 EXPECT_EQ(auto_login_timer()->GetCurrentDelay().InMilliseconds(),
253 EXPECT_EQ(auto_login_delay(), kAutoLoginDelay1
);
254 EXPECT_EQ(auto_login_username(), auto_login_user_id_
);
256 // Timer should restart when the delay is changed.
257 SetAutoLoginSettings(kAutoLoginAccountId
, kAutoLoginDelay2
);
258 ConfigureAutoLogin();
259 ASSERT_TRUE(auto_login_timer());
260 EXPECT_TRUE(auto_login_timer()->IsRunning());
261 EXPECT_EQ(auto_login_timer()->GetCurrentDelay().InMilliseconds(),
263 EXPECT_EQ(auto_login_delay(), kAutoLoginDelay2
);
264 EXPECT_EQ(auto_login_username(), auto_login_user_id_
);
266 // Timer should stop when the account ID is unset.
267 SetAutoLoginSettings("", kAutoLoginDelay2
);
268 ConfigureAutoLogin();
269 ASSERT_TRUE(auto_login_timer());
270 EXPECT_FALSE(auto_login_timer()->IsRunning());
271 EXPECT_EQ(auto_login_timer()->GetCurrentDelay().InMilliseconds(),
273 EXPECT_EQ(auto_login_username(), "");
274 EXPECT_EQ(auto_login_delay(), kAutoLoginDelay2
);
277 } // namespace chromeos