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

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

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

Powered by Google App Engine
This is Rietveld 408576698