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