Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(309)

Side by Side Diff: chrome/browser/chromeos/login/existing_user_controller_unittest.cc

Issue 12218078: Implement a policy to autologin a public account. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: shorten timer delay in test; add two more stop/start timer cases Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "base/memory/scoped_ptr.h"
6 #include "base/message_loop.h"
7 #include "chrome/browser/chromeos/login/existing_user_controller.h"
8 #include "chrome/browser/chromeos/login/login_display.h"
9 #include "chrome/browser/chromeos/login/login_display_host.h"
10 #include "chrome/browser/chromeos/login/login_utils.h"
11 #include "chrome/browser/chromeos/login/mock_login_display.h"
12 #include "chrome/browser/chromeos/login/mock_login_display_host.h"
13 #include "chrome/browser/chromeos/login/mock_login_utils.h"
14 #include "chrome/browser/chromeos/settings/cros_settings.h"
15 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h"
16 #include "chrome/test/base/scoped_testing_local_state.h"
17 #include "chrome/test/base/testing_browser_process.h"
18 #include "content/public/test/test_browser_thread.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 using testing::AnyNumber;
23 using testing::AtLeast;
24 using testing::Mock;
25 using testing::Return;
26 using testing::ReturnNull;
27 using testing::SaveArg;
28 using testing::_;
29
30 namespace chromeos {
31
32 namespace {
33
34 const char kAutoLoginUsername[] = "public_session_user@localhost";
35 const int kAutoLoginNoDelay = 0;
36 const int kAutoLoginShortDelay = 100;
Nikita (slow) 2013/03/06 17:07:16 Could this be set to 0? Or at 1?
dconnelly 2013/03/07 08:38:00 Done.
37 const int kAutoLoginLongDelay = 10000;
38
39 } // namespace
40
41 class ExistingUserControllerAutoLoginTest : public ::testing::Test {
Nikita (slow) 2013/03/06 17:07:16 nit: Class name should be consistent with filename
dconnelly 2013/03/07 08:38:00 Done.
42 protected:
43 ExistingUserControllerAutoLoginTest()
44 : message_loop_(MessageLoop::TYPE_UI),
45 ui_thread_(content::BrowserThread::UI, &message_loop_),
46 local_state_(TestingBrowserProcess::GetGlobal()) {
47 }
48
49 virtual void SetUp() {
50 mock_login_display_host_.reset(new MockLoginDisplayHost);
51 mock_login_display_ = new MockLoginDisplay();
52 mock_login_utils_ = new MockLoginUtils();
53 LoginUtils::Set(mock_login_utils_);
54
55 EXPECT_CALL(*mock_login_display_host_.get(), CreateLoginDisplay(_))
56 .Times(1)
57 .WillOnce(Return(mock_login_display_));
58
59 existing_user_controller_.reset(
60 new ExistingUserController(mock_login_display_host_.get()));
61
62 CrosSettings::Get()->RemoveSettingsObserver(
63 kAccountsPrefDeviceLocalAccountAutoLoginId,
64 existing_user_controller());
65 CrosSettings::Get()->RemoveSettingsObserver(
66 kAccountsPrefDeviceLocalAccountAutoLoginDelay,
67 existing_user_controller());
68 }
69
70 ExistingUserController* existing_user_controller() {
71 return ExistingUserController::current_controller();
72 }
73
74 void SetAutoLoginSettings(const std::string& username, int delay) {
75 CrosSettings::Get()->SetString(
76 kAccountsPrefDeviceLocalAccountAutoLoginId,
77 username);
78 CrosSettings::Get()->SetInteger(
79 kAccountsPrefDeviceLocalAccountAutoLoginDelay,
80 delay);
81 }
82
83 // ExistingUserController private member accessors.
84 base::OneShotTimer<ExistingUserController>* auto_login_timer() {
85 return existing_user_controller()->auto_login_timer_.get();
86 }
87
88 std::string auto_login_username() {
89 return existing_user_controller()->public_session_auto_login_username_;
90 }
91 void set_auto_login_username(const std::string& username) {
92 existing_user_controller()->public_session_auto_login_username_ = username;
93 }
94
95 int auto_login_delay() {
96 return existing_user_controller()->public_session_auto_login_delay_;
97 }
98 void set_auto_login_delay(int delay) {
99 existing_user_controller()->public_session_auto_login_delay_ = delay;
100 }
101
102 bool is_login_in_progress() {
103 return existing_user_controller()->is_login_in_progress_;
104 }
105 void set_is_login_in_progress(bool is_login_in_progress) {
106 existing_user_controller()->is_login_in_progress_ = is_login_in_progress;
107 }
108
109 void ConfigureAutoLogin() {
110 existing_user_controller()->ConfigurePublicSessionAutoLogin();
111 }
112
113 private:
114 MockLoginUtils* mock_login_utils_;
Nikita (slow) 2013/03/06 17:07:16 nit: // Owned by LoginUtilsWrapper.
dconnelly 2013/03/07 08:38:00 Done.
115 MockLoginDisplay* mock_login_display_;
Nikita (slow) 2013/03/06 17:07:16 nit: Missing comment: // |mock_login_display_| is
dconnelly 2013/03/07 08:38:00 Done.
116 scoped_ptr<MockLoginDisplayHost> mock_login_display_host_;
117 scoped_ptr<ExistingUserController> existing_user_controller_;
118 MessageLoop message_loop_;
119 content::TestBrowserThread ui_thread_;
120 ScopedTestingLocalState local_state_;
121 ScopedDeviceSettingsTestHelper device_settings_test_helper_;
122 };
123
124 TEST_F(ExistingUserControllerAutoLoginTest, StartAutoLoginTimer) {
125 // Timer shouldn't start until signin screen is ready.
126 set_auto_login_username(kAutoLoginUsername);
127 set_auto_login_delay(kAutoLoginLongDelay);
128 existing_user_controller()->StartPublicSessionAutoLoginTimer();
129 EXPECT_FALSE(auto_login_timer());
130
131 // Timer shouldn't start if the policy isn't set.
132 set_auto_login_username("");
133 existing_user_controller()->OnSigninScreenReady();
134 existing_user_controller()->StartPublicSessionAutoLoginTimer();
135 EXPECT_FALSE(auto_login_timer());
136
137 // Timer shouldn't fire in the middle of a login attempt.
138 set_auto_login_username(kAutoLoginUsername);
139 set_is_login_in_progress(true);
140 existing_user_controller()->StartPublicSessionAutoLoginTimer();
141 EXPECT_FALSE(auto_login_timer());
142
143 // Otherwise start.
144 set_is_login_in_progress(false);
145 existing_user_controller()->StartPublicSessionAutoLoginTimer();
146 ASSERT_TRUE(auto_login_timer());
147 EXPECT_TRUE(auto_login_timer()->IsRunning());
148 EXPECT_EQ(auto_login_timer()->GetCurrentDelay().InMilliseconds(),
149 kAutoLoginLongDelay);
150 }
151
152 TEST_F(ExistingUserControllerAutoLoginTest, StopAutoLoginTimer) {
153 existing_user_controller()->OnSigninScreenReady();
154 set_auto_login_username(kAutoLoginUsername);
155 set_auto_login_delay(kAutoLoginLongDelay);
156
157 existing_user_controller()->StartPublicSessionAutoLoginTimer();
158 ASSERT_TRUE(auto_login_timer());
159 EXPECT_TRUE(auto_login_timer()->IsRunning());
160
161 existing_user_controller()->StopPublicSessionAutoLoginTimer();
162 ASSERT_TRUE(auto_login_timer());
163 EXPECT_FALSE(auto_login_timer()->IsRunning());
164 }
165
166 TEST_F(ExistingUserControllerAutoLoginTest, ResetAutoLoginTimer) {
167 existing_user_controller()->OnSigninScreenReady();
168 set_auto_login_username(kAutoLoginUsername);
169
170 // Timer starts off not running.
171 EXPECT_FALSE(auto_login_timer());
172
173 // When the timer isn't running, nothing should happen.
174 existing_user_controller()->ResetPublicSessionAutoLoginTimer();
175 EXPECT_FALSE(auto_login_timer());
176
177 // Start the timer.
178 set_auto_login_delay(kAutoLoginLongDelay);
179 existing_user_controller()->StartPublicSessionAutoLoginTimer();
180 ASSERT_TRUE(auto_login_timer());
181 EXPECT_TRUE(auto_login_timer()->IsRunning());
182 EXPECT_EQ(auto_login_timer()->GetCurrentDelay().InMilliseconds(),
183 kAutoLoginLongDelay);
184
185 // User activity should restart the timer, so check to see that the
186 // timer delay was modified.
187 set_auto_login_delay(kAutoLoginShortDelay);
188 existing_user_controller()->ResetPublicSessionAutoLoginTimer();
189 ASSERT_TRUE(auto_login_timer());
190 EXPECT_TRUE(auto_login_timer()->IsRunning());
191 EXPECT_EQ(auto_login_timer()->GetCurrentDelay().InMilliseconds(),
192 kAutoLoginShortDelay);
193 }
194
195 TEST_F(ExistingUserControllerAutoLoginTest, ConfigureAutoLogin) {
196 existing_user_controller()->OnSigninScreenReady();
197
198 // Timer shouldn't start when the policy is disabled.
199 SetAutoLoginSettings("", 0);
200 ConfigureAutoLogin();
201 EXPECT_FALSE(auto_login_timer());
202 EXPECT_EQ(auto_login_delay(), 0);
203 EXPECT_EQ(auto_login_username(), "");
204
205 // Timer shouldn't start when the delay alone is set.
206 SetAutoLoginSettings("", kAutoLoginShortDelay);
207 ConfigureAutoLogin();
208 EXPECT_FALSE(auto_login_timer());
209 EXPECT_EQ(auto_login_delay(), kAutoLoginShortDelay);
210 EXPECT_EQ(auto_login_username(), "");
211
212 // Timer should start when the username is set.
213 SetAutoLoginSettings(kAutoLoginUsername, kAutoLoginShortDelay);
214 ConfigureAutoLogin();
215 ASSERT_TRUE(auto_login_timer());
216 EXPECT_TRUE(auto_login_timer()->IsRunning());
217 EXPECT_EQ(auto_login_timer()->GetCurrentDelay().InMilliseconds(),
218 kAutoLoginShortDelay);
219 EXPECT_EQ(auto_login_delay(), kAutoLoginShortDelay);
220 EXPECT_EQ(auto_login_username(), kAutoLoginUsername);
221
222 // Timer should restart when the delay is changed.
223 SetAutoLoginSettings(kAutoLoginUsername, kAutoLoginLongDelay);
224 ConfigureAutoLogin();
225 ASSERT_TRUE(auto_login_timer());
226 EXPECT_TRUE(auto_login_timer()->IsRunning());
227 EXPECT_EQ(auto_login_timer()->GetCurrentDelay().InMilliseconds(),
228 kAutoLoginLongDelay);
229 EXPECT_EQ(auto_login_delay(), kAutoLoginLongDelay);
230 EXPECT_EQ(auto_login_username(), kAutoLoginUsername);
231
232 // Timer should stop when the username is unset.
233 SetAutoLoginSettings("", kAutoLoginLongDelay);
234 ConfigureAutoLogin();
235 ASSERT_TRUE(auto_login_timer());
236 EXPECT_FALSE(auto_login_timer()->IsRunning());
237 EXPECT_EQ(auto_login_timer()->GetCurrentDelay().InMilliseconds(),
238 kAutoLoginLongDelay);
239 EXPECT_EQ(auto_login_username(), "");
240 EXPECT_EQ(auto_login_delay(), kAutoLoginLongDelay);
241 }
242
243 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/login/existing_user_controller_browsertest.cc ('k') | chrome/browser/chromeos/login/login_display.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698