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/existing_user_controller.h"
16#include "chrome/browser/chromeos/login/helper.h"
17#include "chrome/browser/chromeos/login/mock_login_utils.h"
18#include "chrome/browser/chromeos/login/ui/mock_login_display.h"
19#include "chrome/browser/chromeos/login/ui/mock_login_display_host.h"
20#include "chrome/browser/chromeos/login/users/mock_user_manager.h"
21#include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
22#include "chrome/browser/chromeos/login/wizard_controller.h"
23#include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
24#include "chrome/browser/chromeos/policy/device_local_account.h"
25#include "chrome/browser/chromeos/policy/device_local_account_policy_service.h"
26#include "chrome/browser/chromeos/policy/device_policy_cros_browser_test.h"
27#include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h"
28#include "chrome/browser/chromeos/settings/cros_settings.h"
29#include "chrome/test/base/testing_browser_process.h"
30#include "chrome/test/base/testing_profile.h"
31#include "chrome/test/base/ui_test_utils.h"
32#include "chromeos/chromeos_switches.h"
33#include "chromeos/dbus/fake_session_manager_client.h"
34#include "chromeos/login/auth/authenticator.h"
35#include "chromeos/login/auth/key.h"
36#include "chromeos/login/auth/mock_authenticator.h"
37#include "chromeos/login/auth/mock_url_fetchers.h"
38#include "chromeos/login/auth/user_context.h"
39#include "chromeos/settings/cros_settings_names.h"
40#include "components/policy/core/common/cloud/cloud_policy_constants.h"
41#include "components/policy/core/common/cloud/cloud_policy_core.h"
42#include "components/policy/core/common/cloud/cloud_policy_store.h"
43#include "components/policy/core/common/cloud/mock_cloud_policy_store.h"
44#include "components/policy/core/common/cloud/policy_builder.h"
45#include "components/user_manager/user.h"
46#include "components/user_manager/user_manager.h"
47#include "components/user_manager/user_type.h"
48#include "content/public/test/mock_notification_observer.h"
49#include "content/public/test/test_utils.h"
50#include "google_apis/gaia/mock_url_fetcher_factory.h"
51#include "testing/gmock/include/gmock/gmock.h"
52#include "testing/gtest/include/gtest/gtest.h"
53
54using ::testing::AnyNumber;
55using ::testing::Invoke;
56using ::testing::InvokeWithoutArgs;
57using ::testing::Return;
58using ::testing::ReturnNull;
59using ::testing::Sequence;
60using ::testing::WithArg;
61using ::testing::_;
62
63namespace em = enterprise_management;
64
65namespace chromeos {
66
67namespace {
68
69const char kUsername[] = "test_user@gmail.com";
70const char kNewUsername[] = "test_new_user@gmail.com";
71const char kPassword[] = "test_password";
72
73const char kPublicSessionAccountId[] = "public_session_user@localhost";
74const int kAutoLoginNoDelay = 0;
75const int kAutoLoginShortDelay = 1;
76const int kAutoLoginLongDelay = 10000;
77
78ACTION_P(CreateAuthenticator, user_context) {
79  return new MockAuthenticator(arg0, user_context);
80}
81
82}  // namespace
83
84class ExistingUserControllerTest : public policy::DevicePolicyCrosBrowserTest {
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  }
131
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  }
164
165  virtual void SetUpOnMainThread() OVERRIDE {
166    testing_profile_.reset(new TestingProfile());
167    SetUpUserManager();
168    existing_user_controller_.reset(
169        new ExistingUserController(mock_login_display_host_.get()));
170    ASSERT_EQ(existing_user_controller(), existing_user_controller_.get());
171    existing_user_controller_->Init(user_manager::UserList());
172    profile_prepared_cb_ =
173        base::Bind(&ExistingUserController::OnProfilePrepared,
174                   base::Unretained(existing_user_controller()),
175                   testing_profile_.get());
176  }
177
178  virtual void TearDownOnMainThread() OVERRIDE {
179    // ExistingUserController must be deleted before the thread is cleaned up:
180    // If there is an outstanding login attempt when ExistingUserController is
181    // deleted, its LoginPerformer instance will be deleted, which in turn
182    // deletes its OnlineAttemptHost instance.  However, OnlineAttemptHost must
183    // be deleted on the UI thread.
184    existing_user_controller_.reset();
185    DevicePolicyCrosBrowserTest::InProcessBrowserTest::TearDownOnMainThread();
186    testing_profile_.reset(NULL);
187    user_manager_enabler_.reset();
188  }
189
190  // ExistingUserController private member accessors.
191  base::OneShotTimer<ExistingUserController>* auto_login_timer() {
192    return existing_user_controller()->auto_login_timer_.get();
193  }
194
195  const std::string& auto_login_username() const {
196    return existing_user_controller()->public_session_auto_login_username_;
197  }
198
199  int auto_login_delay() const {
200    return existing_user_controller()->public_session_auto_login_delay_;
201  }
202
203  bool is_login_in_progress() const {
204    return existing_user_controller()->is_login_in_progress_;
205  }
206
207  scoped_ptr<ExistingUserController> existing_user_controller_;
208
209  // |mock_login_display_| is owned by the ExistingUserController, which calls
210  // CreateLoginDisplay() on the |mock_login_display_host_| to get it.
211  MockLoginDisplay* mock_login_display_;
212  scoped_ptr<MockLoginDisplayHost> mock_login_display_host_;
213
214  // Owned by LoginUtilsWrapper.
215  MockLoginUtils* mock_login_utils_;
216
217  MockUserManager* mock_user_manager_;  // Not owned.
218  scoped_ptr<ScopedUserManagerEnabler> user_manager_enabler_;
219
220  scoped_ptr<TestingProfile> testing_profile_;
221
222  // Mock URLFetcher.
223  MockURLFetcherFactory<SuccessFetcher> factory_;
224
225  base::Callback<void(void)> profile_prepared_cb_;
226
227 private:
228  DISALLOW_COPY_AND_ASSIGN(ExistingUserControllerTest);
229};
230
231IN_PROC_BROWSER_TEST_F(ExistingUserControllerTest, ExistingUserLogin) {
232  // This is disabled twice: once right after signin but before checking for
233  // auto-enrollment, and again after doing an ownership status check.
234  EXPECT_CALL(*mock_login_display_, SetUIEnabled(false))
235      .Times(2);
236  UserContext user_context(kUsername);
237  user_context.SetKey(Key(kPassword));
238  user_context.SetUserIDHash(kUsername);
239  EXPECT_CALL(*mock_login_utils_, CreateAuthenticator(_))
240      .Times(1)
241      .WillOnce(WithArg<0>(CreateAuthenticator(user_context)));
242  EXPECT_CALL(*mock_login_utils_, PrepareProfile(user_context, _, _, _))
243      .Times(1)
244      .WillOnce(InvokeWithoutArgs(&profile_prepared_cb_,
245                                  &base::Callback<void(void)>::Run));
246  EXPECT_CALL(*mock_login_utils_,
247              DoBrowserLaunch(testing_profile_.get(),
248                              mock_login_display_host_.get()))
249      .Times(1);
250  EXPECT_CALL(*mock_login_display_, SetUIEnabled(true))
251      .Times(1);
252  EXPECT_CALL(*mock_login_display_host_,
253              StartWizardPtr(WizardController::kTermsOfServiceScreenName, NULL))
254      .Times(0);
255  EXPECT_CALL(*mock_user_manager_, IsCurrentUserNew())
256      .Times(AnyNumber())
257      .WillRepeatedly(Return(false));
258  existing_user_controller()->Login(user_context, SigninSpecifics());
259  content::RunAllPendingInMessageLoop();
260}
261
262IN_PROC_BROWSER_TEST_F(ExistingUserControllerTest, AutoEnrollAfterSignIn) {
263  EXPECT_CALL(*mock_login_display_host_,
264              StartWizardPtr(WizardController::kEnrollmentScreenName,
265                             _))
266      .Times(1);
267  EXPECT_CALL(*mock_login_display_host_.get(), OnCompleteLogin())
268      .Times(1);
269  EXPECT_CALL(*mock_user_manager_, IsCurrentUserNew())
270      .Times(AnyNumber())
271      .WillRepeatedly(Return(false));
272  // The order of these expected calls matters: the UI if first disabled
273  // during the login sequence, and is enabled again for the enrollment screen.
274  Sequence uiEnabledSequence;
275  EXPECT_CALL(*mock_login_display_, SetUIEnabled(false))
276      .Times(1)
277      .InSequence(uiEnabledSequence);
278  EXPECT_CALL(*mock_login_display_, SetUIEnabled(true))
279      .Times(1)
280      .InSequence(uiEnabledSequence);
281  existing_user_controller()->DoAutoEnrollment();
282  UserContext user_context(kUsername);
283  user_context.SetKey(Key(kPassword));
284  existing_user_controller()->CompleteLogin(user_context);
285  content::RunAllPendingInMessageLoop();
286}
287
288IN_PROC_BROWSER_TEST_F(ExistingUserControllerTest,
289                       NewUserDontAutoEnrollAfterSignIn) {
290  EXPECT_CALL(*mock_login_display_host_,
291              StartWizardPtr(WizardController::kEnrollmentScreenName,
292                             _))
293      .Times(0);
294  EXPECT_CALL(*mock_login_display_host_,
295              StartWizardPtr(WizardController::kTermsOfServiceScreenName,
296                             NULL))
297      .Times(1);
298  UserContext user_context(kNewUsername);
299  user_context.SetKey(Key(kPassword));
300  user_context.SetUserIDHash(kNewUsername);
301  EXPECT_CALL(*mock_login_utils_, CreateAuthenticator(_))
302      .Times(1)
303      .WillOnce(WithArg<0>(CreateAuthenticator(user_context)));
304  base::Callback<void(void)> add_user_cb =
305      base::Bind(&MockUserManager::AddUser,
306                 base::Unretained(mock_user_manager_),
307                 kNewUsername);
308  EXPECT_CALL(*mock_login_utils_, PrepareProfile(user_context, _, _, _))
309      .Times(1)
310      .WillOnce(DoAll(
311          InvokeWithoutArgs(&add_user_cb,
312                            &base::Callback<void(void)>::Run),
313          InvokeWithoutArgs(&profile_prepared_cb_,
314                            &base::Callback<void(void)>::Run)));
315  EXPECT_CALL(*mock_login_display_host_.get(), OnCompleteLogin())
316      .Times(1);
317  EXPECT_CALL(*mock_user_manager_, IsCurrentUserNew())
318      .Times(AnyNumber())
319      .WillRepeatedly(Return(true));
320
321  // The order of these expected calls matters: the UI if first disabled
322  // during the login sequence, and is enabled again after login completion.
323  Sequence uiEnabledSequence;
324  // This is disabled twice: once right after signin but before checking for
325  // auto-enrollment, and again after doing an ownership status check.
326  EXPECT_CALL(*mock_login_display_, SetUIEnabled(false))
327      .Times(2)
328      .InSequence(uiEnabledSequence);
329  EXPECT_CALL(*mock_login_display_, SetUIEnabled(true))
330      .Times(1)
331      .InSequence(uiEnabledSequence);
332
333  existing_user_controller()->CompleteLogin(user_context);
334  content::RunAllPendingInMessageLoop();
335}
336
337MATCHER_P(HasDetails, expected, "") {
338  return expected == *content::Details<const std::string>(arg).ptr();
339}
340
341class ExistingUserControllerPublicSessionTest
342    : public ExistingUserControllerTest {
343 protected:
344  ExistingUserControllerPublicSessionTest()
345      : public_session_user_id_(policy::GenerateDeviceLocalAccountUserId(
346            kPublicSessionAccountId,
347            policy::DeviceLocalAccount::TYPE_PUBLIC_SESSION)) {
348  }
349
350  virtual void SetUpOnMainThread() OVERRIDE {
351    ExistingUserControllerTest::SetUpOnMainThread();
352
353    // Wait for the public session user to be created.
354    if (!user_manager::UserManager::Get()->IsKnownUser(
355            public_session_user_id_)) {
356      content::WindowedNotificationObserver(
357          chrome::NOTIFICATION_USER_LIST_CHANGED,
358          base::Bind(&user_manager::UserManager::IsKnownUser,
359                     base::Unretained(user_manager::UserManager::Get()),
360                     public_session_user_id_)).Wait();
361    }
362
363    // Wait for the device local account policy to be installed.
364    policy::CloudPolicyStore* store =
365        TestingBrowserProcess::GetGlobal()
366            ->platform_part()
367            ->browser_policy_connector_chromeos()
368            ->GetDeviceLocalAccountPolicyService()
369            ->GetBrokerForUser(public_session_user_id_)
370            ->core()
371            ->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 UserContext& user_context) {
428    EXPECT_CALL(*mock_login_display_, SetUIEnabled(false))
429        .Times(AnyNumber());
430    EXPECT_CALL(*mock_login_utils_, CreateAuthenticator(_))
431        .Times(1)
432        .WillOnce(WithArg<0>(CreateAuthenticator(user_context)));
433    EXPECT_CALL(*mock_login_utils_, PrepareProfile(user_context, _, _, _))
434        .Times(1)
435        .WillOnce(InvokeWithoutArgs(&profile_prepared_cb_,
436                                    &base::Callback<void(void)>::Run));
437    EXPECT_CALL(*mock_login_utils_,
438                DoBrowserLaunch(testing_profile_.get(),
439                                mock_login_display_host_.get()))
440        .Times(1);
441    EXPECT_CALL(*mock_login_display_, SetUIEnabled(true))
442        .Times(1);
443    EXPECT_CALL(*mock_login_display_host_,
444                StartWizardPtr(WizardController::kTermsOfServiceScreenName,
445                               NULL))
446        .Times(0);
447  }
448
449  void SetAutoLoginPolicy(const std::string& username, int delay) {
450    // Wait until ExistingUserController has finished auto-login
451    // configuration by observing the same settings that trigger
452    // ConfigurePublicSessionAutoLogin.
453
454    em::ChromeDeviceSettingsProto& proto(device_policy()->payload());
455
456    // If both settings have changed we need to wait for both to
457    // propagate, so check the new values against the old ones.
458    scoped_refptr<content::MessageLoopRunner> runner1;
459    scoped_ptr<CrosSettings::ObserverSubscription> subscription1;
460    if (!proto.has_device_local_accounts() ||
461        !proto.device_local_accounts().has_auto_login_id() ||
462        proto.device_local_accounts().auto_login_id() != username) {
463      runner1 = new content::MessageLoopRunner;
464      subscription1 = chromeos::CrosSettings::Get()->AddSettingsObserver(
465          chromeos::kAccountsPrefDeviceLocalAccountAutoLoginId,
466          runner1->QuitClosure());
467    }
468    scoped_refptr<content::MessageLoopRunner> runner2;
469    scoped_ptr<CrosSettings::ObserverSubscription> subscription2;
470    if (!proto.has_device_local_accounts() ||
471        !proto.device_local_accounts().has_auto_login_delay() ||
472        proto.device_local_accounts().auto_login_delay() != delay) {
473      runner1 = new content::MessageLoopRunner;
474      subscription1 = chromeos::CrosSettings::Get()->AddSettingsObserver(
475          chromeos::kAccountsPrefDeviceLocalAccountAutoLoginDelay,
476          runner1->QuitClosure());
477    }
478
479    // Update the policy.
480    proto.mutable_device_local_accounts()->set_auto_login_id(username);
481    proto.mutable_device_local_accounts()->set_auto_login_delay(delay);
482    RefreshDevicePolicy();
483
484    // Wait for ExistingUserController to read the updated settings.
485    if (runner1.get())
486      runner1->Run();
487    if (runner2.get())
488      runner2->Run();
489  }
490
491  void ConfigureAutoLogin() {
492    existing_user_controller()->ConfigurePublicSessionAutoLogin();
493  }
494
495  void FireAutoLogin() {
496    existing_user_controller()->OnPublicSessionAutoLoginTimerFire();
497  }
498
499  const std::string public_session_user_id_;
500
501 private:
502  DISALLOW_COPY_AND_ASSIGN(ExistingUserControllerPublicSessionTest);
503};
504
505IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,
506                       ConfigureAutoLoginUsingPolicy) {
507  existing_user_controller()->OnSigninScreenReady();
508  EXPECT_EQ("", auto_login_username());
509  EXPECT_EQ(0, auto_login_delay());
510  EXPECT_FALSE(auto_login_timer());
511
512  // Set the policy.
513  SetAutoLoginPolicy(kPublicSessionAccountId, kAutoLoginLongDelay);
514  EXPECT_EQ(public_session_user_id_, auto_login_username());
515  EXPECT_EQ(kAutoLoginLongDelay, auto_login_delay());
516  ASSERT_TRUE(auto_login_timer());
517  EXPECT_TRUE(auto_login_timer()->IsRunning());
518
519  // Unset the policy.
520  SetAutoLoginPolicy("", 0);
521  EXPECT_EQ("", auto_login_username());
522  EXPECT_EQ(0, auto_login_delay());
523  ASSERT_TRUE(auto_login_timer());
524  EXPECT_FALSE(auto_login_timer()->IsRunning());
525}
526
527IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,
528                       AutoLoginNoDelay) {
529  // Set up mocks to check login success.
530  UserContext user_context(user_manager::USER_TYPE_PUBLIC_ACCOUNT,
531                           public_session_user_id_);
532  user_context.SetUserIDHash(user_context.GetUserID());
533  ExpectSuccessfulLogin(user_context);
534  existing_user_controller()->OnSigninScreenReady();
535
536  // Start auto-login and wait for login tasks to complete.
537  SetAutoLoginPolicy(kPublicSessionAccountId, kAutoLoginNoDelay);
538  content::RunAllPendingInMessageLoop();
539}
540
541IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,
542                       AutoLoginShortDelay) {
543  // Set up mocks to check login success.
544  UserContext user_context(user_manager::USER_TYPE_PUBLIC_ACCOUNT,
545                           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
567IN_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, SigninSpecifics());
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
593IN_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
623IN_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
650IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,
651                       PublicSessionLoginStopsAutoLogin) {
652  // Set up mocks to check login success.
653  UserContext user_context(user_manager::USER_TYPE_PUBLIC_ACCOUNT,
654                           public_session_user_id_);
655  user_context.SetUserIDHash(user_context.GetUserID());
656  ExpectSuccessfulLogin(user_context);
657  existing_user_controller()->OnSigninScreenReady();
658  SetAutoLoginPolicy(kPublicSessionAccountId, kAutoLoginLongDelay);
659  ASSERT_TRUE(auto_login_timer());
660
661  // Login and check that it stopped the timer.
662  existing_user_controller()->LoginAsPublicSession(UserContext(
663      user_manager::USER_TYPE_PUBLIC_ACCOUNT,
664      public_session_user_id_));
665
666  EXPECT_TRUE(is_login_in_progress());
667  ASSERT_TRUE(auto_login_timer());
668  EXPECT_FALSE(auto_login_timer()->IsRunning());
669
670  // Wait for login tasks to complete.
671  content::RunAllPendingInMessageLoop();
672
673  // Timer should still be stopped after login completes.
674  ASSERT_TRUE(auto_login_timer());
675  EXPECT_FALSE(auto_login_timer()->IsRunning());
676}
677
678IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,
679                       PRE_TestLoadingPublicUsersFromLocalState) {
680  // First run propagates public accounts and stores them in Local State.
681}
682
683// See http://crbug.com/393704; flaky.
684IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,
685                       DISABLED_TestLoadingPublicUsersFromLocalState) {
686  // Second run loads list of public accounts from Local State.
687}
688
689}  // namespace chromeos
690