OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include <string> |
| 6 |
5 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/values.h" |
6 #include "chrome/browser/chromeos/login/existing_user_controller.h" | 9 #include "chrome/browser/chromeos/login/existing_user_controller.h" |
7 #include "chrome/browser/chromeos/login/mock_login_display.h" | 10 #include "chrome/browser/chromeos/login/mock_login_display.h" |
8 #include "chrome/browser/chromeos/login/mock_login_display_host.h" | 11 #include "chrome/browser/chromeos/login/mock_login_display_host.h" |
9 #include "chrome/browser/chromeos/login/mock_login_utils.h" | 12 #include "chrome/browser/chromeos/login/mock_login_utils.h" |
10 #include "chrome/browser/chromeos/login/mock_user_manager.h" | 13 #include "chrome/browser/chromeos/login/mock_user_manager.h" |
11 #include "chrome/browser/chromeos/login/user_manager.h" | 14 #include "chrome/browser/chromeos/login/user_manager.h" |
| 15 #include "chrome/browser/chromeos/policy/device_local_account.h" |
12 #include "chrome/browser/chromeos/settings/cros_settings.h" | 16 #include "chrome/browser/chromeos/settings/cros_settings.h" |
13 #include "chrome/browser/chromeos/settings/cros_settings_names.h" | 17 #include "chrome/browser/chromeos/settings/cros_settings_names.h" |
14 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h" | 18 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h" |
15 #include "chrome/test/base/scoped_testing_local_state.h" | 19 #include "chrome/test/base/scoped_testing_local_state.h" |
16 #include "chrome/test/base/testing_browser_process.h" | 20 #include "chrome/test/base/testing_browser_process.h" |
17 #include "content/public/test/test_browser_thread.h" | 21 #include "content/public/test/test_browser_thread.h" |
18 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
20 | 24 |
21 using testing::AnyNumber; | 25 using testing::AnyNumber; |
22 using testing::Return; | 26 using testing::Return; |
23 using testing::ReturnNull; | 27 using testing::ReturnNull; |
24 using testing::_; | 28 using testing::_; |
25 | 29 |
26 namespace chromeos { | 30 namespace chromeos { |
27 | 31 |
28 namespace { | 32 namespace { |
29 | 33 |
30 const char kAutoLoginUsername[] = "public_session_user@localhost"; | 34 const char kAutoLoginAccountId[] = "public_session_user@localhost"; |
31 // These values are only used to test the configuration. They don't | 35 // These values are only used to test the configuration. They don't |
32 // delay the test. | 36 // delay the test. |
33 const int kAutoLoginNoDelay = 0; | 37 const int kAutoLoginNoDelay = 0; |
34 const int kAutoLoginDelay1 = 60000; | 38 const int kAutoLoginDelay1 = 60000; |
35 const int kAutoLoginDelay2 = 180000; | 39 const int kAutoLoginDelay2 = 180000; |
36 | 40 |
37 } // namespace | 41 } // namespace |
38 | 42 |
39 class ExistingUserControllerAutoLoginTest : public ::testing::Test { | 43 class ExistingUserControllerAutoLoginTest : public ::testing::Test { |
40 protected: | 44 protected: |
41 ExistingUserControllerAutoLoginTest() | 45 ExistingUserControllerAutoLoginTest() |
42 : message_loop_(MessageLoop::TYPE_UI), | 46 : auto_login_user_id_(policy::GenerateDeviceLocalAccountUserId( |
| 47 kAutoLoginAccountId, |
| 48 policy::DeviceLocalAccount::TYPE_PUBLIC_SESSION)), |
| 49 message_loop_(MessageLoop::TYPE_UI), |
43 ui_thread_(content::BrowserThread::UI, &message_loop_), | 50 ui_thread_(content::BrowserThread::UI, &message_loop_), |
44 local_state_(TestingBrowserProcess::GetGlobal()), | 51 local_state_(TestingBrowserProcess::GetGlobal()), |
45 mock_user_manager_(new MockUserManager()), | 52 mock_user_manager_(new MockUserManager()), |
46 scoped_user_manager_(mock_user_manager_) { | 53 scoped_user_manager_(mock_user_manager_) { |
47 } | 54 } |
48 | 55 |
49 virtual void SetUp() { | 56 virtual void SetUp() { |
50 mock_login_display_host_.reset(new MockLoginDisplayHost); | 57 mock_login_display_host_.reset(new MockLoginDisplayHost); |
51 mock_login_display_ = new MockLoginDisplay(); | 58 mock_login_display_ = new MockLoginDisplay(); |
52 mock_login_utils_ = new MockLoginUtils(); | 59 mock_login_utils_ = new MockLoginUtils(); |
53 LoginUtils::Set(mock_login_utils_); | 60 LoginUtils::Set(mock_login_utils_); |
54 | 61 |
55 EXPECT_CALL(*mock_login_display_host_.get(), CreateLoginDisplay(_)) | 62 EXPECT_CALL(*mock_login_display_host_.get(), CreateLoginDisplay(_)) |
56 .Times(1) | 63 .Times(1) |
57 .WillOnce(Return(mock_login_display_)); | 64 .WillOnce(Return(mock_login_display_)); |
58 | 65 |
59 EXPECT_CALL(*mock_login_utils_, DelegateDeleted(_)).Times(AnyNumber()); | 66 EXPECT_CALL(*mock_login_utils_, DelegateDeleted(_)).Times(AnyNumber()); |
60 | 67 |
61 EXPECT_CALL(*mock_user_manager_, Shutdown()).Times(AnyNumber()); | 68 EXPECT_CALL(*mock_user_manager_, Shutdown()).Times(AnyNumber()); |
62 EXPECT_CALL(*mock_user_manager_, FindUser(_)) | 69 EXPECT_CALL(*mock_user_manager_, FindUser(_)) |
63 .WillRepeatedly(ReturnNull()); | 70 .WillRepeatedly(ReturnNull()); |
64 EXPECT_CALL(*mock_user_manager_, FindUser(kAutoLoginUsername)) | 71 EXPECT_CALL(*mock_user_manager_, FindUser(auto_login_user_id_)) |
65 .WillRepeatedly(Return( | 72 .WillRepeatedly(Return( |
66 mock_user_manager_->CreatePublicAccountUser(kAutoLoginUsername))); | 73 mock_user_manager_->CreatePublicAccountUser(auto_login_user_id_))); |
67 | 74 |
68 existing_user_controller_.reset( | 75 existing_user_controller_.reset( |
69 new ExistingUserController(mock_login_display_host_.get())); | 76 new ExistingUserController(mock_login_display_host_.get())); |
70 | 77 |
| 78 scoped_ptr<base::DictionaryValue> account(new base::DictionaryValue); |
| 79 account->SetStringWithoutPathExpansion( |
| 80 kAccountsPrefDeviceLocalAccountsKeyId, |
| 81 kAutoLoginAccountId); |
| 82 account->SetIntegerWithoutPathExpansion( |
| 83 kAccountsPrefDeviceLocalAccountsKeyType, |
| 84 policy::DeviceLocalAccount::TYPE_PUBLIC_SESSION); |
| 85 base::ListValue accounts; |
| 86 accounts.Append(account.release()); |
| 87 CrosSettings::Get()->Set(kAccountsPrefDeviceLocalAccounts, accounts); |
| 88 |
71 // Prevent settings changes from auto-starting the timer. | 89 // Prevent settings changes from auto-starting the timer. |
72 CrosSettings::Get()->RemoveSettingsObserver( | 90 CrosSettings::Get()->RemoveSettingsObserver( |
73 kAccountsPrefDeviceLocalAccountAutoLoginId, | 91 kAccountsPrefDeviceLocalAccountAutoLoginId, |
74 existing_user_controller()); | 92 existing_user_controller()); |
75 CrosSettings::Get()->RemoveSettingsObserver( | 93 CrosSettings::Get()->RemoveSettingsObserver( |
76 kAccountsPrefDeviceLocalAccountAutoLoginDelay, | 94 kAccountsPrefDeviceLocalAccountAutoLoginDelay, |
77 existing_user_controller()); | 95 existing_user_controller()); |
78 } | 96 } |
79 | 97 |
80 const ExistingUserController* existing_user_controller() const { | 98 const ExistingUserController* existing_user_controller() const { |
81 return ExistingUserController::current_controller(); | 99 return ExistingUserController::current_controller(); |
82 } | 100 } |
83 | 101 |
84 ExistingUserController* existing_user_controller() { | 102 ExistingUserController* existing_user_controller() { |
85 return ExistingUserController::current_controller(); | 103 return ExistingUserController::current_controller(); |
86 } | 104 } |
87 | 105 |
88 void SetAutoLoginSettings(const std::string& username, int delay) { | 106 void SetAutoLoginSettings(const std::string& account_id, int delay) { |
89 CrosSettings::Get()->SetString( | 107 CrosSettings::Get()->SetString( |
90 kAccountsPrefDeviceLocalAccountAutoLoginId, | 108 kAccountsPrefDeviceLocalAccountAutoLoginId, |
91 username); | 109 account_id); |
92 CrosSettings::Get()->SetInteger( | 110 CrosSettings::Get()->SetInteger( |
93 kAccountsPrefDeviceLocalAccountAutoLoginDelay, | 111 kAccountsPrefDeviceLocalAccountAutoLoginDelay, |
94 delay); | 112 delay); |
95 } | 113 } |
96 | 114 |
97 // ExistingUserController private member accessors. | 115 // ExistingUserController private member accessors. |
98 base::OneShotTimer<ExistingUserController>* auto_login_timer() { | 116 base::OneShotTimer<ExistingUserController>* auto_login_timer() { |
99 return existing_user_controller()->auto_login_timer_.get(); | 117 return existing_user_controller()->auto_login_timer_.get(); |
100 } | 118 } |
101 | 119 |
(...skipping 15 matching lines...) Expand all Loading... |
117 return existing_user_controller()->is_login_in_progress_; | 135 return existing_user_controller()->is_login_in_progress_; |
118 } | 136 } |
119 void set_is_login_in_progress(bool is_login_in_progress) { | 137 void set_is_login_in_progress(bool is_login_in_progress) { |
120 existing_user_controller()->is_login_in_progress_ = is_login_in_progress; | 138 existing_user_controller()->is_login_in_progress_ = is_login_in_progress; |
121 } | 139 } |
122 | 140 |
123 void ConfigureAutoLogin() { | 141 void ConfigureAutoLogin() { |
124 existing_user_controller()->ConfigurePublicSessionAutoLogin(); | 142 existing_user_controller()->ConfigurePublicSessionAutoLogin(); |
125 } | 143 } |
126 | 144 |
| 145 const std::string auto_login_user_id_; |
| 146 |
127 private: | 147 private: |
128 // Owned by LoginUtilsWrapper. | 148 // Owned by LoginUtilsWrapper. |
129 MockLoginUtils* mock_login_utils_; | 149 MockLoginUtils* mock_login_utils_; |
130 | 150 |
131 // |mock_login_display_| is owned by the ExistingUserController, which calls | 151 // |mock_login_display_| is owned by the ExistingUserController, which calls |
132 // CreateLoginDisplay() on the |mock_login_display_host_| to get it. | 152 // CreateLoginDisplay() on the |mock_login_display_host_| to get it. |
133 MockLoginDisplay* mock_login_display_; | 153 MockLoginDisplay* mock_login_display_; |
134 | 154 |
135 scoped_ptr<MockLoginDisplayHost> mock_login_display_host_; | 155 scoped_ptr<MockLoginDisplayHost> mock_login_display_host_; |
136 MessageLoop message_loop_; | 156 MessageLoop message_loop_; |
137 content::TestBrowserThread ui_thread_; | 157 content::TestBrowserThread ui_thread_; |
138 ScopedTestingLocalState local_state_; | 158 ScopedTestingLocalState local_state_; |
139 | 159 |
140 // Required by ExistingUserController: | 160 // Required by ExistingUserController: |
141 ScopedDeviceSettingsTestHelper device_settings_test_helper_; | 161 ScopedDeviceSettingsTestHelper device_settings_test_helper_; |
142 ScopedTestCrosSettings test_cros_settings_; | 162 ScopedTestCrosSettings test_cros_settings_; |
143 MockUserManager* mock_user_manager_; | 163 MockUserManager* mock_user_manager_; |
144 ScopedUserManagerEnabler scoped_user_manager_; | 164 ScopedUserManagerEnabler scoped_user_manager_; |
145 | 165 |
146 // |existing_user_controller_| must be destroyed before | 166 // |existing_user_controller_| must be destroyed before |
147 // |device_settings_test_helper_|. | 167 // |device_settings_test_helper_|. |
148 scoped_ptr<ExistingUserController> existing_user_controller_; | 168 scoped_ptr<ExistingUserController> existing_user_controller_; |
149 }; | 169 }; |
150 | 170 |
151 TEST_F(ExistingUserControllerAutoLoginTest, StartAutoLoginTimer) { | 171 TEST_F(ExistingUserControllerAutoLoginTest, StartAutoLoginTimer) { |
152 // Timer shouldn't start until signin screen is ready. | 172 // Timer shouldn't start until signin screen is ready. |
153 set_auto_login_username(kAutoLoginUsername); | 173 set_auto_login_username(auto_login_user_id_); |
154 set_auto_login_delay(kAutoLoginDelay2); | 174 set_auto_login_delay(kAutoLoginDelay2); |
155 existing_user_controller()->StartPublicSessionAutoLoginTimer(); | 175 existing_user_controller()->StartPublicSessionAutoLoginTimer(); |
156 EXPECT_FALSE(auto_login_timer()); | 176 EXPECT_FALSE(auto_login_timer()); |
157 | 177 |
158 // Timer shouldn't start if the policy isn't set. | 178 // Timer shouldn't start if the policy isn't set. |
159 set_auto_login_username(""); | 179 set_auto_login_username(""); |
160 existing_user_controller()->OnSigninScreenReady(); | 180 existing_user_controller()->OnSigninScreenReady(); |
161 existing_user_controller()->StartPublicSessionAutoLoginTimer(); | 181 existing_user_controller()->StartPublicSessionAutoLoginTimer(); |
162 EXPECT_FALSE(auto_login_timer()); | 182 EXPECT_FALSE(auto_login_timer()); |
163 | 183 |
164 // Timer shouldn't fire in the middle of a login attempt. | 184 // Timer shouldn't fire in the middle of a login attempt. |
165 set_auto_login_username(kAutoLoginUsername); | 185 set_auto_login_username(auto_login_user_id_); |
166 set_is_login_in_progress(true); | 186 set_is_login_in_progress(true); |
167 existing_user_controller()->StartPublicSessionAutoLoginTimer(); | 187 existing_user_controller()->StartPublicSessionAutoLoginTimer(); |
168 EXPECT_FALSE(auto_login_timer()); | 188 EXPECT_FALSE(auto_login_timer()); |
169 | 189 |
170 // Otherwise start. | 190 // Otherwise start. |
171 set_is_login_in_progress(false); | 191 set_is_login_in_progress(false); |
172 existing_user_controller()->StartPublicSessionAutoLoginTimer(); | 192 existing_user_controller()->StartPublicSessionAutoLoginTimer(); |
173 ASSERT_TRUE(auto_login_timer()); | 193 ASSERT_TRUE(auto_login_timer()); |
174 EXPECT_TRUE(auto_login_timer()->IsRunning()); | 194 EXPECT_TRUE(auto_login_timer()->IsRunning()); |
175 EXPECT_EQ(auto_login_timer()->GetCurrentDelay().InMilliseconds(), | 195 EXPECT_EQ(auto_login_timer()->GetCurrentDelay().InMilliseconds(), |
176 kAutoLoginDelay2); | 196 kAutoLoginDelay2); |
177 } | 197 } |
178 | 198 |
179 TEST_F(ExistingUserControllerAutoLoginTest, StopAutoLoginTimer) { | 199 TEST_F(ExistingUserControllerAutoLoginTest, StopAutoLoginTimer) { |
180 existing_user_controller()->OnSigninScreenReady(); | 200 existing_user_controller()->OnSigninScreenReady(); |
181 set_auto_login_username(kAutoLoginUsername); | 201 set_auto_login_username(auto_login_user_id_); |
182 set_auto_login_delay(kAutoLoginDelay2); | 202 set_auto_login_delay(kAutoLoginDelay2); |
183 | 203 |
184 existing_user_controller()->StartPublicSessionAutoLoginTimer(); | 204 existing_user_controller()->StartPublicSessionAutoLoginTimer(); |
185 ASSERT_TRUE(auto_login_timer()); | 205 ASSERT_TRUE(auto_login_timer()); |
186 EXPECT_TRUE(auto_login_timer()->IsRunning()); | 206 EXPECT_TRUE(auto_login_timer()->IsRunning()); |
187 | 207 |
188 existing_user_controller()->StopPublicSessionAutoLoginTimer(); | 208 existing_user_controller()->StopPublicSessionAutoLoginTimer(); |
189 ASSERT_TRUE(auto_login_timer()); | 209 ASSERT_TRUE(auto_login_timer()); |
190 EXPECT_FALSE(auto_login_timer()->IsRunning()); | 210 EXPECT_FALSE(auto_login_timer()->IsRunning()); |
191 } | 211 } |
192 | 212 |
193 TEST_F(ExistingUserControllerAutoLoginTest, ResetAutoLoginTimer) { | 213 TEST_F(ExistingUserControllerAutoLoginTest, ResetAutoLoginTimer) { |
194 existing_user_controller()->OnSigninScreenReady(); | 214 existing_user_controller()->OnSigninScreenReady(); |
195 set_auto_login_username(kAutoLoginUsername); | 215 set_auto_login_username(auto_login_user_id_); |
196 | 216 |
197 // Timer starts off not running. | 217 // Timer starts off not running. |
198 EXPECT_FALSE(auto_login_timer()); | 218 EXPECT_FALSE(auto_login_timer()); |
199 | 219 |
200 // When the timer isn't running, nothing should happen. | 220 // When the timer isn't running, nothing should happen. |
201 existing_user_controller()->ResetPublicSessionAutoLoginTimer(); | 221 existing_user_controller()->ResetPublicSessionAutoLoginTimer(); |
202 EXPECT_FALSE(auto_login_timer()); | 222 EXPECT_FALSE(auto_login_timer()); |
203 | 223 |
204 // Start the timer. | 224 // Start the timer. |
205 set_auto_login_delay(kAutoLoginDelay2); | 225 set_auto_login_delay(kAutoLoginDelay2); |
(...skipping 22 matching lines...) Expand all Loading... |
228 EXPECT_EQ(auto_login_delay(), 0); | 248 EXPECT_EQ(auto_login_delay(), 0); |
229 EXPECT_EQ(auto_login_username(), ""); | 249 EXPECT_EQ(auto_login_username(), ""); |
230 | 250 |
231 // Timer shouldn't start when the delay alone is set. | 251 // Timer shouldn't start when the delay alone is set. |
232 SetAutoLoginSettings("", kAutoLoginDelay1); | 252 SetAutoLoginSettings("", kAutoLoginDelay1); |
233 ConfigureAutoLogin(); | 253 ConfigureAutoLogin(); |
234 EXPECT_FALSE(auto_login_timer()); | 254 EXPECT_FALSE(auto_login_timer()); |
235 EXPECT_EQ(auto_login_delay(), kAutoLoginDelay1); | 255 EXPECT_EQ(auto_login_delay(), kAutoLoginDelay1); |
236 EXPECT_EQ(auto_login_username(), ""); | 256 EXPECT_EQ(auto_login_username(), ""); |
237 | 257 |
238 // Timer should start when the username is set. | 258 // Timer should start when the account ID is set. |
239 SetAutoLoginSettings(kAutoLoginUsername, kAutoLoginDelay1); | 259 SetAutoLoginSettings(kAutoLoginAccountId, kAutoLoginDelay1); |
240 ConfigureAutoLogin(); | 260 ConfigureAutoLogin(); |
241 ASSERT_TRUE(auto_login_timer()); | 261 ASSERT_TRUE(auto_login_timer()); |
242 EXPECT_TRUE(auto_login_timer()->IsRunning()); | 262 EXPECT_TRUE(auto_login_timer()->IsRunning()); |
243 EXPECT_EQ(auto_login_timer()->GetCurrentDelay().InMilliseconds(), | 263 EXPECT_EQ(auto_login_timer()->GetCurrentDelay().InMilliseconds(), |
244 kAutoLoginDelay1); | 264 kAutoLoginDelay1); |
245 EXPECT_EQ(auto_login_delay(), kAutoLoginDelay1); | 265 EXPECT_EQ(auto_login_delay(), kAutoLoginDelay1); |
246 EXPECT_EQ(auto_login_username(), kAutoLoginUsername); | 266 EXPECT_EQ(auto_login_username(), auto_login_user_id_); |
247 | 267 |
248 // Timer should restart when the delay is changed. | 268 // Timer should restart when the delay is changed. |
249 SetAutoLoginSettings(kAutoLoginUsername, kAutoLoginDelay2); | 269 SetAutoLoginSettings(kAutoLoginAccountId, kAutoLoginDelay2); |
250 ConfigureAutoLogin(); | 270 ConfigureAutoLogin(); |
251 ASSERT_TRUE(auto_login_timer()); | 271 ASSERT_TRUE(auto_login_timer()); |
252 EXPECT_TRUE(auto_login_timer()->IsRunning()); | 272 EXPECT_TRUE(auto_login_timer()->IsRunning()); |
253 EXPECT_EQ(auto_login_timer()->GetCurrentDelay().InMilliseconds(), | 273 EXPECT_EQ(auto_login_timer()->GetCurrentDelay().InMilliseconds(), |
254 kAutoLoginDelay2); | 274 kAutoLoginDelay2); |
255 EXPECT_EQ(auto_login_delay(), kAutoLoginDelay2); | 275 EXPECT_EQ(auto_login_delay(), kAutoLoginDelay2); |
256 EXPECT_EQ(auto_login_username(), kAutoLoginUsername); | 276 EXPECT_EQ(auto_login_username(), auto_login_user_id_); |
257 | 277 |
258 // Timer should stop when the username is unset. | 278 // Timer should stop when the account ID is unset. |
259 SetAutoLoginSettings("", kAutoLoginDelay2); | 279 SetAutoLoginSettings("", kAutoLoginDelay2); |
260 ConfigureAutoLogin(); | 280 ConfigureAutoLogin(); |
261 ASSERT_TRUE(auto_login_timer()); | 281 ASSERT_TRUE(auto_login_timer()); |
262 EXPECT_FALSE(auto_login_timer()->IsRunning()); | 282 EXPECT_FALSE(auto_login_timer()->IsRunning()); |
263 EXPECT_EQ(auto_login_timer()->GetCurrentDelay().InMilliseconds(), | 283 EXPECT_EQ(auto_login_timer()->GetCurrentDelay().InMilliseconds(), |
264 kAutoLoginDelay2); | 284 kAutoLoginDelay2); |
265 EXPECT_EQ(auto_login_username(), ""); | 285 EXPECT_EQ(auto_login_username(), ""); |
266 EXPECT_EQ(auto_login_delay(), kAutoLoginDelay2); | 286 EXPECT_EQ(auto_login_delay(), kAutoLoginDelay2); |
267 } | 287 } |
268 | 288 |
269 } // namespace chromeos | 289 } // namespace chromeos |
OLD | NEW |