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