• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 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 <vector>
6 
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "base/command_line.h"
11 #include "base/location.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/run_loop.h"
14 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/chromeos/login/auth/authenticator.h"
16 #include "chrome/browser/chromeos/login/auth/key.h"
17 #include "chrome/browser/chromeos/login/auth/mock_authenticator.h"
18 #include "chrome/browser/chromeos/login/auth/mock_url_fetchers.h"
19 #include "chrome/browser/chromeos/login/auth/user_context.h"
20 #include "chrome/browser/chromeos/login/existing_user_controller.h"
21 #include "chrome/browser/chromeos/login/helper.h"
22 #include "chrome/browser/chromeos/login/mock_login_utils.h"
23 #include "chrome/browser/chromeos/login/ui/mock_login_display.h"
24 #include "chrome/browser/chromeos/login/ui/mock_login_display_host.h"
25 #include "chrome/browser/chromeos/login/users/mock_user_manager.h"
26 #include "chrome/browser/chromeos/login/users/user.h"
27 #include "chrome/browser/chromeos/login/users/user_manager.h"
28 #include "chrome/browser/chromeos/login/wizard_controller.h"
29 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
30 #include "chrome/browser/chromeos/policy/device_local_account.h"
31 #include "chrome/browser/chromeos/policy/device_local_account_policy_service.h"
32 #include "chrome/browser/chromeos/policy/device_policy_cros_browser_test.h"
33 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h"
34 #include "chrome/browser/chromeos/settings/cros_settings.h"
35 #include "chrome/test/base/testing_browser_process.h"
36 #include "chrome/test/base/testing_profile.h"
37 #include "chrome/test/base/ui_test_utils.h"
38 #include "chromeos/chromeos_switches.h"
39 #include "chromeos/dbus/fake_session_manager_client.h"
40 #include "chromeos/settings/cros_settings_names.h"
41 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
42 #include "components/policy/core/common/cloud/cloud_policy_core.h"
43 #include "components/policy/core/common/cloud/cloud_policy_store.h"
44 #include "components/policy/core/common/cloud/mock_cloud_policy_store.h"
45 #include "components/policy/core/common/cloud/policy_builder.h"
46 #include "content/public/test/mock_notification_observer.h"
47 #include "content/public/test/test_utils.h"
48 #include "google_apis/gaia/mock_url_fetcher_factory.h"
49 #include "grit/generated_resources.h"
50 #include "testing/gmock/include/gmock/gmock.h"
51 #include "testing/gtest/include/gtest/gtest.h"
52 #include "ui/base/l10n/l10n_util.h"
53 
54 using ::testing::AnyNumber;
55 using ::testing::Invoke;
56 using ::testing::InvokeWithoutArgs;
57 using ::testing::Return;
58 using ::testing::ReturnNull;
59 using ::testing::Sequence;
60 using ::testing::WithArg;
61 using ::testing::_;
62 
63 namespace em = enterprise_management;
64 
65 namespace chromeos {
66 
67 namespace {
68 
69 const char kUsername[] = "test_user@gmail.com";
70 const char kNewUsername[] = "test_new_user@gmail.com";
71 const char kPassword[] = "test_password";
72 
73 const char kPublicSessionAccountId[] = "public_session_user@localhost";
74 const int kAutoLoginNoDelay = 0;
75 const int kAutoLoginShortDelay = 1;
76 const int kAutoLoginLongDelay = 10000;
77 
ACTION_P(CreateAuthenticator,user_context)78 ACTION_P(CreateAuthenticator, user_context) {
79   return new MockAuthenticator(arg0, user_context);
80 }
81 
82 }  // namespace
83 
84 class ExistingUserControllerTest : public policy::DevicePolicyCrosBrowserTest {
85  protected:
ExistingUserControllerTest()86   ExistingUserControllerTest()
87       : mock_login_display_(NULL), mock_user_manager_(NULL) {}
88 
existing_user_controller()89   ExistingUserController* existing_user_controller() {
90     return ExistingUserController::current_controller();
91   }
92 
existing_user_controller() const93   const ExistingUserController* existing_user_controller() const {
94     return ExistingUserController::current_controller();
95   }
96 
SetUpInProcessBrowserTestFixture()97   virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
98     SetUpSessionManager();
99 
100     DevicePolicyCrosBrowserTest::SetUpInProcessBrowserTestFixture();
101 
102     mock_login_utils_ = new MockLoginUtils();
103     LoginUtils::Set(mock_login_utils_);
104     EXPECT_CALL(*mock_login_utils_, DelegateDeleted(_))
105         .Times(1);
106 
107     mock_login_display_host_.reset(new MockLoginDisplayHost());
108     mock_login_display_ = new MockLoginDisplay();
109     SetUpLoginDisplay();
110   }
111 
SetUpSessionManager()112   virtual void SetUpSessionManager() {
113   }
114 
SetUpLoginDisplay()115   virtual void SetUpLoginDisplay() {
116     EXPECT_CALL(*mock_login_display_host_.get(), CreateLoginDisplay(_))
117         .Times(1)
118         .WillOnce(Return(mock_login_display_));
119     EXPECT_CALL(*mock_login_display_host_.get(), GetNativeWindow())
120         .Times(1)
121         .WillOnce(ReturnNull());
122     EXPECT_CALL(*mock_login_display_host_.get(), OnPreferencesChanged())
123         .Times(1);
124     EXPECT_CALL(*mock_login_display_, Init(_, false, true, true))
125         .Times(1);
126   }
127 
SetUpCommandLine(CommandLine * command_line)128   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
129     command_line->AppendSwitch(switches::kLoginManager);
130   }
131 
SetUpUserManager()132   virtual void SetUpUserManager() {
133     // Replace the UserManager singleton with a mock.
134     mock_user_manager_ = new MockUserManager;
135     user_manager_enabler_.reset(
136         new ScopedUserManagerEnabler(mock_user_manager_));
137     EXPECT_CALL(*mock_user_manager_, IsKnownUser(kUsername))
138         .Times(AnyNumber())
139         .WillRepeatedly(Return(true));
140     EXPECT_CALL(*mock_user_manager_, IsKnownUser(kNewUsername))
141         .Times(AnyNumber())
142         .WillRepeatedly(Return(false));
143     EXPECT_CALL(*mock_user_manager_, IsUserLoggedIn())
144         .Times(AnyNumber())
145         .WillRepeatedly(Return(false));
146     EXPECT_CALL(*mock_user_manager_, IsLoggedInAsGuest())
147         .Times(AnyNumber())
148         .WillRepeatedly(Return(false));
149     EXPECT_CALL(*mock_user_manager_, IsLoggedInAsDemoUser())
150         .Times(AnyNumber())
151         .WillRepeatedly(Return(false));
152     EXPECT_CALL(*mock_user_manager_, IsLoggedInAsPublicAccount())
153         .Times(AnyNumber())
154         .WillRepeatedly(Return(false));
155     EXPECT_CALL(*mock_user_manager_, IsSessionStarted())
156         .Times(AnyNumber())
157         .WillRepeatedly(Return(false));
158     EXPECT_CALL(*mock_user_manager_, IsCurrentUserNew())
159         .Times(AnyNumber())
160         .WillRepeatedly(Return(false));
161     EXPECT_CALL(*mock_user_manager_, Shutdown())
162         .Times(1);
163     EXPECT_CALL(*mock_user_manager_, GetProfileByUser(_))
164         .Times(AnyNumber())
165         .WillRepeatedly(Return(testing_profile_.get()));
166   }
167 
SetUpOnMainThread()168   virtual void SetUpOnMainThread() OVERRIDE {
169     testing_profile_.reset(new TestingProfile());
170     SetUpUserManager();
171     existing_user_controller_.reset(
172         new ExistingUserController(mock_login_display_host_.get()));
173     ASSERT_EQ(existing_user_controller(), existing_user_controller_.get());
174     existing_user_controller_->Init(UserList());
175     profile_prepared_cb_ =
176         base::Bind(&ExistingUserController::OnProfilePrepared,
177                    base::Unretained(existing_user_controller()),
178                    testing_profile_.get());
179   }
180 
CleanUpOnMainThread()181   virtual void CleanUpOnMainThread() OVERRIDE {
182     // ExistingUserController must be deleted before the thread is cleaned up:
183     // If there is an outstanding login attempt when ExistingUserController is
184     // deleted, its LoginPerformer instance will be deleted, which in turn
185     // deletes its OnlineAttemptHost instance.  However, OnlineAttemptHost must
186     // be deleted on the UI thread.
187     existing_user_controller_.reset();
188     DevicePolicyCrosBrowserTest::InProcessBrowserTest::CleanUpOnMainThread();
189     testing_profile_.reset(NULL);
190     user_manager_enabler_.reset();
191   }
192 
193   // ExistingUserController private member accessors.
auto_login_timer()194   base::OneShotTimer<ExistingUserController>* auto_login_timer() {
195     return existing_user_controller()->auto_login_timer_.get();
196   }
197 
auto_login_username() const198   const std::string& auto_login_username() const {
199     return existing_user_controller()->public_session_auto_login_username_;
200   }
201 
auto_login_delay() const202   int auto_login_delay() const {
203     return existing_user_controller()->public_session_auto_login_delay_;
204   }
205 
is_login_in_progress() const206   bool is_login_in_progress() const {
207     return existing_user_controller()->is_login_in_progress_;
208   }
209 
210   scoped_ptr<ExistingUserController> existing_user_controller_;
211 
212   // |mock_login_display_| is owned by the ExistingUserController, which calls
213   // CreateLoginDisplay() on the |mock_login_display_host_| to get it.
214   MockLoginDisplay* mock_login_display_;
215   scoped_ptr<MockLoginDisplayHost> mock_login_display_host_;
216 
217   // Owned by LoginUtilsWrapper.
218   MockLoginUtils* mock_login_utils_;
219 
220   MockUserManager* mock_user_manager_;  // Not owned.
221   scoped_ptr<ScopedUserManagerEnabler> user_manager_enabler_;
222 
223   scoped_ptr<TestingProfile> testing_profile_;
224 
225   // Mock URLFetcher.
226   MockURLFetcherFactory<SuccessFetcher> factory_;
227 
228   base::Callback<void(void)> profile_prepared_cb_;
229 
230  private:
231   DISALLOW_COPY_AND_ASSIGN(ExistingUserControllerTest);
232 };
233 
IN_PROC_BROWSER_TEST_F(ExistingUserControllerTest,ExistingUserLogin)234 IN_PROC_BROWSER_TEST_F(ExistingUserControllerTest, ExistingUserLogin) {
235   // This is disabled twice: once right after signin but before checking for
236   // auto-enrollment, and again after doing an ownership status check.
237   EXPECT_CALL(*mock_login_display_, SetUIEnabled(false))
238       .Times(2);
239   UserContext user_context(kUsername);
240   user_context.SetKey(Key(kPassword));
241   user_context.SetUserIDHash(kUsername);
242   EXPECT_CALL(*mock_login_utils_, CreateAuthenticator(_))
243       .Times(1)
244       .WillOnce(WithArg<0>(CreateAuthenticator(user_context)));
245   EXPECT_CALL(*mock_login_utils_, PrepareProfile(user_context, _, _, _))
246       .Times(1)
247       .WillOnce(InvokeWithoutArgs(&profile_prepared_cb_,
248                                   &base::Callback<void(void)>::Run));
249   EXPECT_CALL(*mock_login_utils_,
250               DoBrowserLaunch(testing_profile_.get(),
251                               mock_login_display_host_.get()))
252       .Times(1);
253   EXPECT_CALL(*mock_login_display_, SetUIEnabled(true))
254       .Times(1);
255   EXPECT_CALL(*mock_login_display_host_,
256               StartWizardPtr(WizardController::kTermsOfServiceScreenName, NULL))
257       .Times(0);
258   EXPECT_CALL(*mock_user_manager_, IsCurrentUserNew())
259       .Times(AnyNumber())
260       .WillRepeatedly(Return(false));
261   existing_user_controller()->Login(user_context);
262   content::RunAllPendingInMessageLoop();
263 }
264 
IN_PROC_BROWSER_TEST_F(ExistingUserControllerTest,AutoEnrollAfterSignIn)265 IN_PROC_BROWSER_TEST_F(ExistingUserControllerTest, AutoEnrollAfterSignIn) {
266   EXPECT_CALL(*mock_login_display_host_,
267               StartWizardPtr(WizardController::kEnrollmentScreenName,
268                              _))
269       .Times(1);
270   EXPECT_CALL(*mock_login_display_host_.get(), OnCompleteLogin())
271       .Times(1);
272   EXPECT_CALL(*mock_user_manager_, IsCurrentUserNew())
273       .Times(AnyNumber())
274       .WillRepeatedly(Return(false));
275   // The order of these expected calls matters: the UI if first disabled
276   // during the login sequence, and is enabled again for the enrollment screen.
277   Sequence uiEnabledSequence;
278   EXPECT_CALL(*mock_login_display_, SetUIEnabled(false))
279       .Times(1)
280       .InSequence(uiEnabledSequence);
281   EXPECT_CALL(*mock_login_display_, SetUIEnabled(true))
282       .Times(1)
283       .InSequence(uiEnabledSequence);
284   existing_user_controller()->DoAutoEnrollment();
285   UserContext user_context(kUsername);
286   user_context.SetKey(Key(kPassword));
287   existing_user_controller()->CompleteLogin(user_context);
288   content::RunAllPendingInMessageLoop();
289 }
290 
IN_PROC_BROWSER_TEST_F(ExistingUserControllerTest,NewUserDontAutoEnrollAfterSignIn)291 IN_PROC_BROWSER_TEST_F(ExistingUserControllerTest,
292                        NewUserDontAutoEnrollAfterSignIn) {
293   EXPECT_CALL(*mock_login_display_host_,
294               StartWizardPtr(WizardController::kEnrollmentScreenName,
295                              _))
296       .Times(0);
297   EXPECT_CALL(*mock_login_display_host_,
298               StartWizardPtr(WizardController::kTermsOfServiceScreenName,
299                              NULL))
300       .Times(1);
301   UserContext user_context(kNewUsername);
302   user_context.SetKey(Key(kPassword));
303   user_context.SetUserIDHash(kNewUsername);
304   EXPECT_CALL(*mock_login_utils_, CreateAuthenticator(_))
305       .Times(1)
306       .WillOnce(WithArg<0>(CreateAuthenticator(user_context)));
307   base::Callback<void(void)> add_user_cb =
308       base::Bind(&MockUserManager::AddUser,
309                  base::Unretained(mock_user_manager_),
310                  kNewUsername);
311   EXPECT_CALL(*mock_login_utils_, PrepareProfile(user_context, _, _, _))
312       .Times(1)
313       .WillOnce(DoAll(
314           InvokeWithoutArgs(&add_user_cb,
315                             &base::Callback<void(void)>::Run),
316           InvokeWithoutArgs(&profile_prepared_cb_,
317                             &base::Callback<void(void)>::Run)));
318   EXPECT_CALL(*mock_login_display_host_.get(), OnCompleteLogin())
319       .Times(1);
320   EXPECT_CALL(*mock_user_manager_, IsCurrentUserNew())
321       .Times(AnyNumber())
322       .WillRepeatedly(Return(true));
323 
324   // The order of these expected calls matters: the UI if first disabled
325   // during the login sequence, and is enabled again after login completion.
326   Sequence uiEnabledSequence;
327   // This is disabled twice: once right after signin but before checking for
328   // auto-enrollment, and again after doing an ownership status check.
329   EXPECT_CALL(*mock_login_display_, SetUIEnabled(false))
330       .Times(2)
331       .InSequence(uiEnabledSequence);
332   EXPECT_CALL(*mock_login_display_, SetUIEnabled(true))
333       .Times(1)
334       .InSequence(uiEnabledSequence);
335 
336   existing_user_controller()->CompleteLogin(user_context);
337   content::RunAllPendingInMessageLoop();
338 }
339 
340 MATCHER_P(HasDetails, expected, "") {
341   return expected == *content::Details<const std::string>(arg).ptr();
342 }
343 
344 class ExistingUserControllerPublicSessionTest
345     : public ExistingUserControllerTest {
346  protected:
ExistingUserControllerPublicSessionTest()347   ExistingUserControllerPublicSessionTest()
348       : public_session_user_id_(policy::GenerateDeviceLocalAccountUserId(
349             kPublicSessionAccountId,
350             policy::DeviceLocalAccount::TYPE_PUBLIC_SESSION)) {
351   }
352 
SetUpOnMainThread()353   virtual void SetUpOnMainThread() OVERRIDE {
354     ExistingUserControllerTest::SetUpOnMainThread();
355 
356     // Wait for the public session user to be created.
357     if (!chromeos::UserManager::Get()->IsKnownUser(public_session_user_id_)) {
358       content::WindowedNotificationObserver(
359           chrome::NOTIFICATION_USER_LIST_CHANGED,
360           base::Bind(&chromeos::UserManager::IsKnownUser,
361                      base::Unretained(chromeos::UserManager::Get()),
362                      public_session_user_id_)).Wait();
363     }
364 
365     // Wait for the device local account policy to be installed.
366     policy::CloudPolicyStore* store =
367         TestingBrowserProcess::GetGlobal()
368             ->platform_part()
369             ->browser_policy_connector_chromeos()
370             ->GetDeviceLocalAccountPolicyService()
371             ->GetBrokerForUser(public_session_user_id_)
372             ->core()
373             ->store();
374     if (!store->has_policy()) {
375       policy::MockCloudPolicyStoreObserver observer;
376 
377       base::RunLoop loop;
378       store->AddObserver(&observer);
379       EXPECT_CALL(observer, OnStoreLoaded(store))
380           .Times(1)
381           .WillOnce(InvokeWithoutArgs(&loop, &base::RunLoop::Quit));
382       loop.Run();
383       store->RemoveObserver(&observer);
384     }
385   }
386 
SetUpSessionManager()387   virtual void SetUpSessionManager() OVERRIDE {
388     InstallOwnerKey();
389 
390     // Setup the device policy.
391     em::ChromeDeviceSettingsProto& proto(device_policy()->payload());
392     em::DeviceLocalAccountInfoProto* account =
393         proto.mutable_device_local_accounts()->add_account();
394     account->set_account_id(kPublicSessionAccountId);
395     account->set_type(
396         em::DeviceLocalAccountInfoProto::ACCOUNT_TYPE_PUBLIC_SESSION);
397     RefreshDevicePolicy();
398 
399     // Setup the device local account policy.
400     policy::UserPolicyBuilder device_local_account_policy;
401     device_local_account_policy.policy_data().set_username(
402         kPublicSessionAccountId);
403     device_local_account_policy.policy_data().set_policy_type(
404         policy::dm_protocol::kChromePublicAccountPolicyType);
405     device_local_account_policy.policy_data().set_settings_entity_id(
406         kPublicSessionAccountId);
407     device_local_account_policy.Build();
408     session_manager_client()->set_device_local_account_policy(
409         kPublicSessionAccountId,
410         device_local_account_policy.GetBlob());
411   }
412 
SetUpLoginDisplay()413   virtual void SetUpLoginDisplay() OVERRIDE {
414     EXPECT_CALL(*mock_login_display_host_.get(), CreateLoginDisplay(_))
415         .Times(1)
416         .WillOnce(Return(mock_login_display_));
417     EXPECT_CALL(*mock_login_display_host_.get(), GetNativeWindow())
418       .Times(AnyNumber())
419       .WillRepeatedly(ReturnNull());
420     EXPECT_CALL(*mock_login_display_host_.get(), OnPreferencesChanged())
421       .Times(AnyNumber());
422     EXPECT_CALL(*mock_login_display_, Init(_, _, _, _))
423       .Times(AnyNumber());
424   }
425 
SetUpUserManager()426   virtual void SetUpUserManager() OVERRIDE {
427   }
428 
ExpectSuccessfulLogin(const UserContext & user_context)429   void ExpectSuccessfulLogin(const UserContext& user_context) {
430     EXPECT_CALL(*mock_login_display_, SetUIEnabled(false))
431         .Times(AnyNumber());
432     EXPECT_CALL(*mock_login_utils_, CreateAuthenticator(_))
433         .Times(1)
434         .WillOnce(WithArg<0>(CreateAuthenticator(user_context)));
435     EXPECT_CALL(*mock_login_utils_, PrepareProfile(user_context, _, _, _))
436         .Times(1)
437         .WillOnce(InvokeWithoutArgs(&profile_prepared_cb_,
438                                     &base::Callback<void(void)>::Run));
439     EXPECT_CALL(*mock_login_utils_,
440                 DoBrowserLaunch(testing_profile_.get(),
441                                 mock_login_display_host_.get()))
442         .Times(1);
443     EXPECT_CALL(*mock_login_display_, SetUIEnabled(true))
444         .Times(1);
445     EXPECT_CALL(*mock_login_display_host_,
446                 StartWizardPtr(WizardController::kTermsOfServiceScreenName,
447                                NULL))
448         .Times(0);
449   }
450 
SetAutoLoginPolicy(const std::string & username,int delay)451   void SetAutoLoginPolicy(const std::string& username, int delay) {
452     // Wait until ExistingUserController has finished auto-login
453     // configuration by observing the same settings that trigger
454     // ConfigurePublicSessionAutoLogin.
455 
456     em::ChromeDeviceSettingsProto& proto(device_policy()->payload());
457 
458     // If both settings have changed we need to wait for both to
459     // propagate, so check the new values against the old ones.
460     scoped_refptr<content::MessageLoopRunner> runner1;
461     scoped_ptr<CrosSettings::ObserverSubscription> subscription1;
462     if (!proto.has_device_local_accounts() ||
463         !proto.device_local_accounts().has_auto_login_id() ||
464         proto.device_local_accounts().auto_login_id() != username) {
465       runner1 = new content::MessageLoopRunner;
466       subscription1 = chromeos::CrosSettings::Get()->AddSettingsObserver(
467           chromeos::kAccountsPrefDeviceLocalAccountAutoLoginId,
468           runner1->QuitClosure());
469     }
470     scoped_refptr<content::MessageLoopRunner> runner2;
471     scoped_ptr<CrosSettings::ObserverSubscription> subscription2;
472     if (!proto.has_device_local_accounts() ||
473         !proto.device_local_accounts().has_auto_login_delay() ||
474         proto.device_local_accounts().auto_login_delay() != delay) {
475       runner1 = new content::MessageLoopRunner;
476       subscription1 = chromeos::CrosSettings::Get()->AddSettingsObserver(
477           chromeos::kAccountsPrefDeviceLocalAccountAutoLoginDelay,
478           runner1->QuitClosure());
479     }
480 
481     // Update the policy.
482     proto.mutable_device_local_accounts()->set_auto_login_id(username);
483     proto.mutable_device_local_accounts()->set_auto_login_delay(delay);
484     RefreshDevicePolicy();
485 
486     // Wait for ExistingUserController to read the updated settings.
487     if (runner1.get())
488       runner1->Run();
489     if (runner2.get())
490       runner2->Run();
491   }
492 
ConfigureAutoLogin()493   void ConfigureAutoLogin() {
494     existing_user_controller()->ConfigurePublicSessionAutoLogin();
495   }
496 
FireAutoLogin()497   void FireAutoLogin() {
498     existing_user_controller()->OnPublicSessionAutoLoginTimerFire();
499   }
500 
501   const std::string public_session_user_id_;
502 
503  private:
504   DISALLOW_COPY_AND_ASSIGN(ExistingUserControllerPublicSessionTest);
505 };
506 
IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,ConfigureAutoLoginUsingPolicy)507 IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,
508                        ConfigureAutoLoginUsingPolicy) {
509   existing_user_controller()->OnSigninScreenReady();
510   EXPECT_EQ("", auto_login_username());
511   EXPECT_EQ(0, auto_login_delay());
512   EXPECT_FALSE(auto_login_timer());
513 
514   // Set the policy.
515   SetAutoLoginPolicy(kPublicSessionAccountId, kAutoLoginLongDelay);
516   EXPECT_EQ(public_session_user_id_, auto_login_username());
517   EXPECT_EQ(kAutoLoginLongDelay, auto_login_delay());
518   ASSERT_TRUE(auto_login_timer());
519   EXPECT_TRUE(auto_login_timer()->IsRunning());
520 
521   // Unset the policy.
522   SetAutoLoginPolicy("", 0);
523   EXPECT_EQ("", auto_login_username());
524   EXPECT_EQ(0, auto_login_delay());
525   ASSERT_TRUE(auto_login_timer());
526   EXPECT_FALSE(auto_login_timer()->IsRunning());
527 }
528 
IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,AutoLoginNoDelay)529 IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,
530                        AutoLoginNoDelay) {
531   // Set up mocks to check login success.
532   UserContext user_context(public_session_user_id_);
533   user_context.SetUserIDHash(user_context.GetUserID());
534   ExpectSuccessfulLogin(user_context);
535   existing_user_controller()->OnSigninScreenReady();
536 
537   // Start auto-login and wait for login tasks to complete.
538   SetAutoLoginPolicy(kPublicSessionAccountId, kAutoLoginNoDelay);
539   content::RunAllPendingInMessageLoop();
540 }
541 
IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,AutoLoginShortDelay)542 IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,
543                        AutoLoginShortDelay) {
544   // Set up mocks to check login success.
545   UserContext user_context(public_session_user_id_);
546   user_context.SetUserIDHash(user_context.GetUserID());
547   ExpectSuccessfulLogin(user_context);
548   existing_user_controller()->OnSigninScreenReady();
549   SetAutoLoginPolicy(kPublicSessionAccountId, kAutoLoginShortDelay);
550   ASSERT_TRUE(auto_login_timer());
551   // Don't assert that timer is running: with the short delay sometimes
552   // the trigger happens before the assert.  We've already tested that
553   // the timer starts when it should.
554 
555   // Wait for the timer to fire.
556   base::RunLoop runner;
557   base::OneShotTimer<base::RunLoop> timer;
558   timer.Start(FROM_HERE,
559               base::TimeDelta::FromMilliseconds(kAutoLoginShortDelay + 1),
560               runner.QuitClosure());
561   runner.Run();
562 
563   // Wait for login tasks to complete.
564   content::RunAllPendingInMessageLoop();
565 }
566 
IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,LoginStopsAutoLogin)567 IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,
568                        LoginStopsAutoLogin) {
569   // Set up mocks to check login success.
570   UserContext user_context(kUsername);
571   user_context.SetKey(Key(kPassword));
572   user_context.SetUserIDHash(user_context.GetUserID());
573   ExpectSuccessfulLogin(user_context);
574 
575   existing_user_controller()->OnSigninScreenReady();
576   SetAutoLoginPolicy(kPublicSessionAccountId, kAutoLoginLongDelay);
577   ASSERT_TRUE(auto_login_timer());
578 
579   // Log in and check that it stopped the timer.
580   existing_user_controller()->Login(user_context);
581   EXPECT_TRUE(is_login_in_progress());
582   ASSERT_TRUE(auto_login_timer());
583   EXPECT_FALSE(auto_login_timer()->IsRunning());
584 
585   // Wait for login tasks to complete.
586   content::RunAllPendingInMessageLoop();
587 
588   // Timer should still be stopped after login completes.
589   ASSERT_TRUE(auto_login_timer());
590   EXPECT_FALSE(auto_login_timer()->IsRunning());
591 }
592 
IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,GuestModeLoginStopsAutoLogin)593 IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,
594                        GuestModeLoginStopsAutoLogin) {
595   EXPECT_CALL(*mock_login_display_, SetUIEnabled(false))
596       .Times(1);
597   UserContext user_context(kUsername);
598   user_context.SetKey(Key(kPassword));
599   EXPECT_CALL(*mock_login_utils_, CreateAuthenticator(_))
600       .Times(1)
601       .WillOnce(WithArg<0>(CreateAuthenticator(user_context)));
602   EXPECT_CALL(*mock_login_utils_, CompleteOffTheRecordLogin(_))
603       .Times(1);
604 
605   existing_user_controller()->OnSigninScreenReady();
606   SetAutoLoginPolicy(kPublicSessionAccountId, kAutoLoginLongDelay);
607   ASSERT_TRUE(auto_login_timer());
608 
609   // Login and check that it stopped the timer.
610   existing_user_controller()->LoginAsGuest();
611   EXPECT_TRUE(is_login_in_progress());
612   ASSERT_TRUE(auto_login_timer());
613   EXPECT_FALSE(auto_login_timer()->IsRunning());
614 
615   // Wait for login tasks to complete.
616   content::RunAllPendingInMessageLoop();
617 
618   // Timer should still be stopped after login completes.
619   ASSERT_TRUE(auto_login_timer());
620   EXPECT_FALSE(auto_login_timer()->IsRunning());
621 }
622 
IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,CompleteLoginStopsAutoLogin)623 IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,
624                        CompleteLoginStopsAutoLogin) {
625   // Set up mocks to check login success.
626   UserContext user_context(kUsername);
627   user_context.SetKey(Key(kPassword));
628   user_context.SetUserIDHash(user_context.GetUserID());
629   ExpectSuccessfulLogin(user_context);
630   EXPECT_CALL(*mock_login_display_host_, OnCompleteLogin())
631       .Times(1);
632 
633   existing_user_controller()->OnSigninScreenReady();
634   SetAutoLoginPolicy(kPublicSessionAccountId, kAutoLoginLongDelay);
635   ASSERT_TRUE(auto_login_timer());
636 
637   // Check that login completes and stops the timer.
638   existing_user_controller()->CompleteLogin(user_context);
639   ASSERT_TRUE(auto_login_timer());
640   EXPECT_FALSE(auto_login_timer()->IsRunning());
641 
642   // Wait for login tasks to complete.
643   content::RunAllPendingInMessageLoop();
644 
645   // Timer should still be stopped after login completes.
646   ASSERT_TRUE(auto_login_timer());
647   EXPECT_FALSE(auto_login_timer()->IsRunning());
648 }
649 
IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,PublicSessionLoginStopsAutoLogin)650 IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,
651                        PublicSessionLoginStopsAutoLogin) {
652   // Set up mocks to check login success.
653   UserContext user_context(public_session_user_id_);
654   user_context.SetUserIDHash(user_context.GetUserID());
655   ExpectSuccessfulLogin(user_context);
656   existing_user_controller()->OnSigninScreenReady();
657   SetAutoLoginPolicy(kPublicSessionAccountId, kAutoLoginLongDelay);
658   ASSERT_TRUE(auto_login_timer());
659 
660   // Login and check that it stopped the timer.
661   existing_user_controller()->LoginAsPublicAccount(public_session_user_id_);
662   EXPECT_TRUE(is_login_in_progress());
663   ASSERT_TRUE(auto_login_timer());
664   EXPECT_FALSE(auto_login_timer()->IsRunning());
665 
666   // Wait for login tasks to complete.
667   content::RunAllPendingInMessageLoop();
668 
669   // Timer should still be stopped after login completes.
670   ASSERT_TRUE(auto_login_timer());
671   EXPECT_FALSE(auto_login_timer()->IsRunning());
672 }
673 
IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,PRE_TestLoadingPublicUsersFromLocalState)674 IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,
675                        PRE_TestLoadingPublicUsersFromLocalState) {
676   // First run propagates public accounts and stores them in Local State.
677 }
678 
IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,TestLoadingPublicUsersFromLocalState)679 IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,
680                        TestLoadingPublicUsersFromLocalState) {
681   // Second run loads list of public accounts from Local State.
682 }
683 
684 }  // namespace chromeos
685