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