Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / chromeos / login / existing_user_controller_browsertest.cc
blob890fa92df173724a4e9ff76b53622fefe277d75a
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.
5 #include <string>
6 #include <vector>
8 #include "base/bind.h"
9 #include "base/bind_helpers.h"
10 #include "base/callback.h"
11 #include "base/command_line.h"
12 #include "base/location.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/prefs/pref_service.h"
15 #include "base/prefs/scoped_user_pref_update.h"
16 #include "base/run_loop.h"
17 #include "chrome/browser/chrome_notification_types.h"
18 #include "chrome/browser/chromeos/login/existing_user_controller.h"
19 #include "chrome/browser/chromeos/login/help_app_launcher.h"
20 #include "chrome/browser/chromeos/login/helper.h"
21 #include "chrome/browser/chromeos/login/screens/mock_base_screen_delegate.h"
22 #include "chrome/browser/chromeos/login/session/user_session_manager.h"
23 #include "chrome/browser/chromeos/login/session/user_session_manager_test_api.h"
24 #include "chrome/browser/chromeos/login/supervised/supervised_user_creation_screen.h"
25 #include "chrome/browser/chromeos/login/ui/mock_login_display.h"
26 #include "chrome/browser/chromeos/login/ui/mock_login_display_host.h"
27 #include "chrome/browser/chromeos/login/wizard_controller.h"
28 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
29 #include "chrome/browser/chromeos/policy/device_local_account.h"
30 #include "chrome/browser/chromeos/policy/device_local_account_policy_service.h"
31 #include "chrome/browser/chromeos/policy/device_policy_cros_browser_test.h"
32 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h"
33 #include "chrome/browser/chromeos/settings/cros_settings.h"
34 #include "chrome/browser/ui/webui/chromeos/login/supervised_user_creation_screen_handler.h"
35 #include "chrome/grit/generated_resources.h"
36 #include "chrome/test/base/testing_browser_process.h"
37 #include "chromeos/chromeos_switches.h"
38 #include "chromeos/dbus/fake_session_manager_client.h"
39 #include "chromeos/login/auth/key.h"
40 #include "chromeos/login/auth/mock_url_fetchers.h"
41 #include "chromeos/login/auth/user_context.h"
42 #include "chromeos/login/user_names.h"
43 #include "chromeos/settings/cros_settings_names.h"
44 #include "chromeos/settings/cros_settings_provider.h"
45 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
46 #include "components/policy/core/common/cloud/cloud_policy_core.h"
47 #include "components/policy/core/common/cloud/cloud_policy_store.h"
48 #include "components/policy/core/common/cloud/mock_cloud_policy_store.h"
49 #include "components/policy/core/common/cloud/policy_builder.h"
50 #include "components/user_manager/user.h"
51 #include "components/user_manager/user_manager.h"
52 #include "components/user_manager/user_type.h"
53 #include "content/public/test/mock_notification_observer.h"
54 #include "content/public/test/test_utils.h"
55 #include "google_apis/gaia/mock_url_fetcher_factory.h"
56 #include "policy/proto/device_management_backend.pb.h"
57 #include "testing/gmock/include/gmock/gmock.h"
58 #include "testing/gtest/include/gtest/gtest.h"
60 using ::testing::AnyNumber;
61 using ::testing::Invoke;
62 using ::testing::InvokeWithoutArgs;
63 using ::testing::Return;
64 using ::testing::ReturnNull;
65 using ::testing::WithArg;
66 using ::testing::_;
68 namespace em = enterprise_management;
70 namespace chromeos {
72 namespace {
74 const char kGaiaID[] = "12345";
75 const char kUsername[] = "test_user@gmail.com";
76 const char kSupervisedUserID[] = "supervised_user@locally-managed.localhost";
77 const char kPassword[] = "test_password";
79 const char kPublicSessionAccountId[] = "public_session_user@localhost";
80 const int kAutoLoginNoDelay = 0;
81 const int kAutoLoginShortDelay = 1;
82 const int kAutoLoginLongDelay = 10000;
84 // Wait for cros settings to become permanently untrusted and run |callback|.
85 void WaitForPermanentlyUntrustedStatusAndRun(const base::Closure& callback) {
86 while (true) {
87 const CrosSettingsProvider::TrustedStatus status =
88 CrosSettings::Get()->PrepareTrustedValues(base::Bind(
89 &WaitForPermanentlyUntrustedStatusAndRun,
90 callback));
91 switch (status) {
92 case CrosSettingsProvider::PERMANENTLY_UNTRUSTED:
93 callback.Run();
94 return;
95 case CrosSettingsProvider::TEMPORARILY_UNTRUSTED:
96 return;
97 case CrosSettingsProvider::TRUSTED:
98 content::RunAllPendingInMessageLoop();
99 break;
104 } // namespace
106 class ExistingUserControllerTest : public policy::DevicePolicyCrosBrowserTest {
107 protected:
108 ExistingUserControllerTest() : mock_login_display_(NULL) {}
110 ExistingUserController* existing_user_controller() {
111 return ExistingUserController::current_controller();
114 const ExistingUserController* existing_user_controller() const {
115 return ExistingUserController::current_controller();
118 void SetUpInProcessBrowserTestFixture() override {
119 SetUpSessionManager();
121 DevicePolicyCrosBrowserTest::SetUpInProcessBrowserTestFixture();
123 mock_login_display_host_.reset(new MockLoginDisplayHost());
124 mock_login_display_ = new MockLoginDisplay();
125 SetUpLoginDisplay();
128 virtual void SetUpSessionManager() {
131 virtual void SetUpLoginDisplay() {
132 EXPECT_CALL(*mock_login_display_host_.get(), CreateLoginDisplay(_))
133 .Times(1)
134 .WillOnce(Return(mock_login_display_));
135 EXPECT_CALL(*mock_login_display_host_.get(), GetNativeWindow())
136 .Times(1)
137 .WillOnce(ReturnNull());
138 EXPECT_CALL(*mock_login_display_host_.get(), OnPreferencesChanged())
139 .Times(1);
140 EXPECT_CALL(*mock_login_display_, Init(_, false, true, true))
141 .Times(1);
144 void SetUpCommandLine(base::CommandLine* command_line) override {
145 command_line->AppendSwitch(switches::kLoginManager);
146 command_line->AppendSwitch(switches::kForceLoginManagerInTests);
149 void SetUpOnMainThread() override {
150 existing_user_controller_.reset(
151 new ExistingUserController(mock_login_display_host_.get()));
152 ASSERT_EQ(existing_user_controller(), existing_user_controller_.get());
153 existing_user_controller_->Init(user_manager::UserList());
155 chromeos::test::UserSessionManagerTestApi session_manager_test_api(
156 chromeos::UserSessionManager::GetInstance());
157 session_manager_test_api.SetShouldObtainTokenHandleInTests(false);
160 void TearDownOnMainThread() override {
161 // ExistingUserController must be deleted before the thread is cleaned up:
162 // If there is an outstanding login attempt when ExistingUserController is
163 // deleted, its LoginPerformer instance will be deleted, which in turn
164 // deletes its OnlineAttemptHost instance. However, OnlineAttemptHost must
165 // be deleted on the UI thread.
166 existing_user_controller_.reset();
167 DevicePolicyCrosBrowserTest::InProcessBrowserTest::TearDownOnMainThread();
169 // Test case may be configured with the real user manager but empty user
170 // list initially. So network OOBE screen is initialized.
171 // Need to reset it manually so that we don't end up with CrosSettings
172 // observer that wasn't removed.
173 WizardController* controller = WizardController::default_controller();
174 if (controller && controller->current_screen())
175 controller->current_screen()->Hide();
178 void ExpectLoginFailure() {
179 EXPECT_CALL(*mock_login_display_, SetUIEnabled(false))
180 .Times(1);
181 EXPECT_CALL(*mock_login_display_,
182 ShowError(IDS_LOGIN_ERROR_OWNER_KEY_LOST,
184 HelpAppLauncher::HELP_CANT_ACCESS_ACCOUNT))
185 .Times(1);
186 EXPECT_CALL(*mock_login_display_, SetUIEnabled(true))
187 .Times(1);
190 void RegisterUser(const std::string& user_id) {
191 ListPrefUpdate users_pref(g_browser_process->local_state(),
192 "LoggedInUsers");
193 users_pref->AppendIfNotPresent(new base::StringValue(user_id));
196 // ExistingUserController private member accessors.
197 base::OneShotTimer<ExistingUserController>* auto_login_timer() {
198 return existing_user_controller()->auto_login_timer_.get();
201 const std::string& auto_login_username() const {
202 return existing_user_controller()->public_session_auto_login_username_;
205 int auto_login_delay() const {
206 return existing_user_controller()->public_session_auto_login_delay_;
209 bool is_login_in_progress() const {
210 return existing_user_controller()->is_login_in_progress_;
213 scoped_ptr<ExistingUserController> existing_user_controller_;
215 // |mock_login_display_| is owned by the ExistingUserController, which calls
216 // CreateLoginDisplay() on the |mock_login_display_host_| to get it.
217 MockLoginDisplay* mock_login_display_;
218 scoped_ptr<MockLoginDisplayHost> mock_login_display_host_;
220 // Mock URLFetcher.
221 MockURLFetcherFactory<SuccessFetcher> factory_;
223 private:
224 DISALLOW_COPY_AND_ASSIGN(ExistingUserControllerTest);
227 IN_PROC_BROWSER_TEST_F(ExistingUserControllerTest, PRE_ExistingUserLogin) {
228 RegisterUser(kUsername);
231 IN_PROC_BROWSER_TEST_F(ExistingUserControllerTest, ExistingUserLogin) {
232 EXPECT_CALL(*mock_login_display_, SetUIEnabled(false))
233 .Times(2);
234 UserContext user_context(kUsername);
235 user_context.SetGaiaID(kGaiaID);
236 user_context.SetKey(Key(kPassword));
237 user_context.SetUserIDHash(kUsername);
238 test::UserSessionManagerTestApi session_manager_test_api(
239 UserSessionManager::GetInstance());
240 session_manager_test_api.InjectStubUserContext(user_context);
241 EXPECT_CALL(*mock_login_display_, SetUIEnabled(true))
242 .Times(1);
243 EXPECT_CALL(*mock_login_display_host_,
244 StartWizard(WizardController::kTermsOfServiceScreenName))
245 .Times(0);
247 content::WindowedNotificationObserver profile_prepared_observer(
248 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
249 content::NotificationService::AllSources());
250 existing_user_controller()->Login(user_context, SigninSpecifics());
252 profile_prepared_observer.Wait();
253 content::RunAllPendingInMessageLoop();
256 // Verifies that when the cros settings are untrusted, no new session can be
257 // started.
258 class ExistingUserControllerUntrustedTest : public ExistingUserControllerTest {
259 public:
260 ExistingUserControllerUntrustedTest();
262 void SetUpInProcessBrowserTestFixture() override;
264 void SetUpSessionManager() override;
266 private:
267 DISALLOW_COPY_AND_ASSIGN(ExistingUserControllerUntrustedTest);
270 ExistingUserControllerUntrustedTest::ExistingUserControllerUntrustedTest() {
273 void ExistingUserControllerUntrustedTest::SetUpInProcessBrowserTestFixture() {
274 ExistingUserControllerTest::SetUpInProcessBrowserTestFixture();
276 ExpectLoginFailure();
279 void ExistingUserControllerUntrustedTest::SetUpSessionManager() {
280 InstallOwnerKey();
283 IN_PROC_BROWSER_TEST_F(ExistingUserControllerUntrustedTest,
284 ExistingUserLoginForbidden) {
285 UserContext user_context(kUsername);
286 user_context.SetGaiaID(kGaiaID);
287 user_context.SetKey(Key(kPassword));
288 user_context.SetUserIDHash(kUsername);
289 existing_user_controller()->Login(user_context, SigninSpecifics());
292 IN_PROC_BROWSER_TEST_F(ExistingUserControllerUntrustedTest,
293 NewUserLoginForbidden) {
294 UserContext user_context(kUsername);
295 user_context.SetGaiaID(kGaiaID);
296 user_context.SetKey(Key(kPassword));
297 user_context.SetUserIDHash(kUsername);
298 existing_user_controller()->CompleteLogin(user_context);
301 IN_PROC_BROWSER_TEST_F(ExistingUserControllerUntrustedTest,
302 CreateAccountForbidden) {
303 existing_user_controller()->CreateAccount();
306 IN_PROC_BROWSER_TEST_F(ExistingUserControllerUntrustedTest,
307 GuestLoginForbidden) {
308 existing_user_controller()->Login(
309 UserContext(user_manager::USER_TYPE_GUEST, std::string()),
310 SigninSpecifics());
313 IN_PROC_BROWSER_TEST_F(ExistingUserControllerUntrustedTest,
314 SupervisedUserLoginForbidden) {
315 UserContext user_context(kSupervisedUserID);
316 user_context.SetKey(Key(kPassword));
317 user_context.SetUserIDHash(kUsername);
318 existing_user_controller()->Login(user_context, SigninSpecifics());
321 IN_PROC_BROWSER_TEST_F(ExistingUserControllerUntrustedTest,
322 SupervisedUserCreationForbidden) {
323 MockBaseScreenDelegate mock_base_screen_delegate;
324 SupervisedUserCreationScreenHandler supervised_user_creation_screen_handler;
325 SupervisedUserCreationScreen supervised_user_creation_screen(
326 &mock_base_screen_delegate, &supervised_user_creation_screen_handler);
328 supervised_user_creation_screen.AuthenticateManager(kUsername, kPassword);
331 MATCHER_P(HasDetails, expected, "") {
332 return expected == *content::Details<const std::string>(arg).ptr();
335 class ExistingUserControllerPublicSessionTest
336 : public ExistingUserControllerTest {
337 protected:
338 ExistingUserControllerPublicSessionTest()
339 : public_session_user_id_(policy::GenerateDeviceLocalAccountUserId(
340 kPublicSessionAccountId,
341 policy::DeviceLocalAccount::TYPE_PUBLIC_SESSION)) {
344 void SetUpOnMainThread() override {
345 ExistingUserControllerTest::SetUpOnMainThread();
347 // Wait for the public session user to be created.
348 if (!user_manager::UserManager::Get()->IsKnownUser(
349 public_session_user_id_)) {
350 content::WindowedNotificationObserver(
351 chrome::NOTIFICATION_USER_LIST_CHANGED,
352 base::Bind(&user_manager::UserManager::IsKnownUser,
353 base::Unretained(user_manager::UserManager::Get()),
354 public_session_user_id_)).Wait();
357 // Wait for the device local account policy to be installed.
358 policy::CloudPolicyStore* store =
359 TestingBrowserProcess::GetGlobal()
360 ->platform_part()
361 ->browser_policy_connector_chromeos()
362 ->GetDeviceLocalAccountPolicyService()
363 ->GetBrokerForUser(public_session_user_id_)
364 ->core()
365 ->store();
366 if (!store->has_policy()) {
367 policy::MockCloudPolicyStoreObserver observer;
369 base::RunLoop loop;
370 store->AddObserver(&observer);
371 EXPECT_CALL(observer, OnStoreLoaded(store))
372 .Times(1)
373 .WillOnce(InvokeWithoutArgs(&loop, &base::RunLoop::Quit));
374 loop.Run();
375 store->RemoveObserver(&observer);
379 void SetUpSessionManager() override {
380 InstallOwnerKey();
382 // Setup the device policy.
383 em::ChromeDeviceSettingsProto& proto(device_policy()->payload());
384 em::DeviceLocalAccountInfoProto* account =
385 proto.mutable_device_local_accounts()->add_account();
386 account->set_account_id(kPublicSessionAccountId);
387 account->set_type(
388 em::DeviceLocalAccountInfoProto::ACCOUNT_TYPE_PUBLIC_SESSION);
389 RefreshDevicePolicy();
391 // Setup the device local account policy.
392 policy::UserPolicyBuilder device_local_account_policy;
393 device_local_account_policy.policy_data().set_username(
394 kPublicSessionAccountId);
395 device_local_account_policy.policy_data().set_policy_type(
396 policy::dm_protocol::kChromePublicAccountPolicyType);
397 device_local_account_policy.policy_data().set_settings_entity_id(
398 kPublicSessionAccountId);
399 device_local_account_policy.Build();
400 session_manager_client()->set_device_local_account_policy(
401 kPublicSessionAccountId,
402 device_local_account_policy.GetBlob());
405 void SetUpLoginDisplay() override {
406 EXPECT_CALL(*mock_login_display_host_.get(), CreateLoginDisplay(_))
407 .Times(1)
408 .WillOnce(Return(mock_login_display_));
409 EXPECT_CALL(*mock_login_display_host_.get(), GetNativeWindow())
410 .Times(AnyNumber())
411 .WillRepeatedly(ReturnNull());
412 EXPECT_CALL(*mock_login_display_host_.get(), OnPreferencesChanged())
413 .Times(AnyNumber());
414 EXPECT_CALL(*mock_login_display_, Init(_, _, _, _))
415 .Times(AnyNumber());
418 void TearDownOnMainThread() override {
419 ExistingUserControllerTest::TearDownOnMainThread();
421 // Test case may be configured with the real user manager but empty user
422 // list initially. So network OOBE screen is initialized.
423 // Need to reset it manually so that we don't end up with CrosSettings
424 // observer that wasn't removed.
425 WizardController* controller = WizardController::default_controller();
426 if (controller && controller->current_screen())
427 controller->current_screen()->Hide();
430 void ExpectSuccessfulLogin(const UserContext& user_context) {
431 test::UserSessionManagerTestApi session_manager_test_api(
432 UserSessionManager::GetInstance());
433 session_manager_test_api.InjectStubUserContext(user_context);
434 EXPECT_CALL(*mock_login_display_host_,
435 StartWizard(WizardController::kTermsOfServiceScreenName))
436 .Times(0);
437 EXPECT_CALL(*mock_login_display_, SetUIEnabled(false)).Times(AnyNumber());
438 EXPECT_CALL(*mock_login_display_, SetUIEnabled(true)).Times(AnyNumber());
441 void SetAutoLoginPolicy(const std::string& username, int delay) {
442 // Wait until ExistingUserController has finished auto-login
443 // configuration by observing the same settings that trigger
444 // ConfigurePublicSessionAutoLogin.
446 em::ChromeDeviceSettingsProto& proto(device_policy()->payload());
448 // If both settings have changed we need to wait for both to
449 // propagate, so check the new values against the old ones.
450 scoped_refptr<content::MessageLoopRunner> runner1;
451 scoped_ptr<CrosSettings::ObserverSubscription> subscription1;
452 if (!proto.has_device_local_accounts() ||
453 !proto.device_local_accounts().has_auto_login_id() ||
454 proto.device_local_accounts().auto_login_id() != username) {
455 runner1 = new content::MessageLoopRunner;
456 subscription1 = chromeos::CrosSettings::Get()->AddSettingsObserver(
457 chromeos::kAccountsPrefDeviceLocalAccountAutoLoginId,
458 runner1->QuitClosure());
460 scoped_refptr<content::MessageLoopRunner> runner2;
461 scoped_ptr<CrosSettings::ObserverSubscription> subscription2;
462 if (!proto.has_device_local_accounts() ||
463 !proto.device_local_accounts().has_auto_login_delay() ||
464 proto.device_local_accounts().auto_login_delay() != delay) {
465 runner1 = new content::MessageLoopRunner;
466 subscription1 = chromeos::CrosSettings::Get()->AddSettingsObserver(
467 chromeos::kAccountsPrefDeviceLocalAccountAutoLoginDelay,
468 runner1->QuitClosure());
471 // Update the policy.
472 proto.mutable_device_local_accounts()->set_auto_login_id(username);
473 proto.mutable_device_local_accounts()->set_auto_login_delay(delay);
474 RefreshDevicePolicy();
476 // Wait for ExistingUserController to read the updated settings.
477 if (runner1.get())
478 runner1->Run();
479 if (runner2.get())
480 runner2->Run();
483 void ConfigureAutoLogin() {
484 existing_user_controller()->ConfigurePublicSessionAutoLogin();
487 void FireAutoLogin() {
488 existing_user_controller()->OnPublicSessionAutoLoginTimerFire();
491 void MakeCrosSettingsPermanentlyUntrusted() {
492 device_policy()->policy().set_policy_data_signature("bad signature");
493 session_manager_client()->set_device_policy(device_policy()->GetBlob());
494 session_manager_client()->OnPropertyChangeComplete(true);
496 base::RunLoop run_loop;
497 WaitForPermanentlyUntrustedStatusAndRun(run_loop.QuitClosure());
498 run_loop.Run();
501 const std::string public_session_user_id_;
503 private:
504 DISALLOW_COPY_AND_ASSIGN(ExistingUserControllerPublicSessionTest);
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());
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());
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());
529 IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,
530 AutoLoginNoDelay) {
531 // Set up mocks to check login success.
532 UserContext user_context(user_manager::USER_TYPE_PUBLIC_ACCOUNT,
533 public_session_user_id_);
534 user_context.SetUserIDHash(user_context.GetUserID());
535 ExpectSuccessfulLogin(user_context);
536 existing_user_controller()->OnSigninScreenReady();
538 // Start auto-login and wait for login tasks to complete.
539 SetAutoLoginPolicy(kPublicSessionAccountId, kAutoLoginNoDelay);
540 content::RunAllPendingInMessageLoop();
543 IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,
544 AutoLoginShortDelay) {
545 // Set up mocks to check login success.
546 UserContext user_context(user_manager::USER_TYPE_PUBLIC_ACCOUNT,
547 public_session_user_id_);
548 user_context.SetUserIDHash(user_context.GetUserID());
549 ExpectSuccessfulLogin(user_context);
550 existing_user_controller()->OnSigninScreenReady();
552 content::WindowedNotificationObserver profile_prepared_observer(
553 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
554 content::NotificationService::AllSources());
556 SetAutoLoginPolicy(kPublicSessionAccountId, kAutoLoginShortDelay);
557 ASSERT_TRUE(auto_login_timer());
558 // Don't assert that timer is running: with the short delay sometimes
559 // the trigger happens before the assert. We've already tested that
560 // the timer starts when it should.
562 // Wait for the timer to fire.
563 base::RunLoop runner;
564 base::OneShotTimer<base::RunLoop> timer;
565 timer.Start(FROM_HERE,
566 base::TimeDelta::FromMilliseconds(kAutoLoginShortDelay + 1),
567 runner.QuitClosure());
568 runner.Run();
570 profile_prepared_observer.Wait();
572 // Wait for login tasks to complete.
573 content::RunAllPendingInMessageLoop();
576 IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,
577 LoginStopsAutoLogin) {
578 // Set up mocks to check login success.
579 UserContext user_context(kUsername);
580 user_context.SetGaiaID(kGaiaID);
581 user_context.SetKey(Key(kPassword));
582 user_context.SetUserIDHash(user_context.GetUserID());
583 ExpectSuccessfulLogin(user_context);
585 existing_user_controller()->OnSigninScreenReady();
586 SetAutoLoginPolicy(kPublicSessionAccountId, kAutoLoginLongDelay);
587 EXPECT_TRUE(auto_login_timer());
589 content::WindowedNotificationObserver profile_prepared_observer(
590 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
591 content::NotificationService::AllSources());
593 // Log in and check that it stopped the timer.
594 existing_user_controller()->Login(user_context, SigninSpecifics());
595 EXPECT_TRUE(is_login_in_progress());
596 ASSERT_TRUE(auto_login_timer());
597 EXPECT_FALSE(auto_login_timer()->IsRunning());
599 profile_prepared_observer.Wait();
601 // Wait for login tasks to complete.
602 content::RunAllPendingInMessageLoop();
604 // Timer should still be stopped after login completes.
605 ASSERT_TRUE(auto_login_timer());
606 EXPECT_FALSE(auto_login_timer()->IsRunning());
609 IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,
610 GuestModeLoginStopsAutoLogin) {
611 EXPECT_CALL(*mock_login_display_, SetUIEnabled(false))
612 .Times(2);
613 UserContext user_context(kUsername);
614 user_context.SetGaiaID(kGaiaID);
615 user_context.SetKey(Key(kPassword));
616 test::UserSessionManagerTestApi session_manager_test_api(
617 UserSessionManager::GetInstance());
618 session_manager_test_api.InjectStubUserContext(user_context);
620 existing_user_controller()->OnSigninScreenReady();
621 SetAutoLoginPolicy(kPublicSessionAccountId, kAutoLoginLongDelay);
622 EXPECT_TRUE(auto_login_timer());
624 // Login and check that it stopped the timer.
625 existing_user_controller()->Login(UserContext(user_manager::USER_TYPE_GUEST,
626 std::string()),
627 SigninSpecifics());
628 EXPECT_TRUE(is_login_in_progress());
629 ASSERT_TRUE(auto_login_timer());
630 EXPECT_FALSE(auto_login_timer()->IsRunning());
632 // Wait for login tasks to complete.
633 content::RunAllPendingInMessageLoop();
635 // Timer should still be stopped after login completes.
636 ASSERT_TRUE(auto_login_timer());
637 EXPECT_FALSE(auto_login_timer()->IsRunning());
640 IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,
641 CompleteLoginStopsAutoLogin) {
642 // Set up mocks to check login success.
643 UserContext user_context(kUsername);
644 user_context.SetGaiaID(kGaiaID);
645 user_context.SetKey(Key(kPassword));
646 user_context.SetUserIDHash(user_context.GetUserID());
647 ExpectSuccessfulLogin(user_context);
648 EXPECT_CALL(*mock_login_display_host_, OnCompleteLogin())
649 .Times(1);
651 existing_user_controller()->OnSigninScreenReady();
652 SetAutoLoginPolicy(kPublicSessionAccountId, kAutoLoginLongDelay);
653 EXPECT_TRUE(auto_login_timer());
655 content::WindowedNotificationObserver profile_prepared_observer(
656 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
657 content::NotificationService::AllSources());
659 // Check that login completes and stops the timer.
660 existing_user_controller()->CompleteLogin(user_context);
661 ASSERT_TRUE(auto_login_timer());
662 EXPECT_FALSE(auto_login_timer()->IsRunning());
664 profile_prepared_observer.Wait();
666 // Wait for login tasks to complete.
667 content::RunAllPendingInMessageLoop();
669 // Timer should still be stopped after login completes.
670 ASSERT_TRUE(auto_login_timer());
671 EXPECT_FALSE(auto_login_timer()->IsRunning());
674 IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,
675 PublicSessionLoginStopsAutoLogin) {
676 // Set up mocks to check login success.
677 UserContext user_context(user_manager::USER_TYPE_PUBLIC_ACCOUNT,
678 public_session_user_id_);
679 user_context.SetUserIDHash(user_context.GetUserID());
680 ExpectSuccessfulLogin(user_context);
681 existing_user_controller()->OnSigninScreenReady();
682 SetAutoLoginPolicy(kPublicSessionAccountId, kAutoLoginLongDelay);
683 EXPECT_TRUE(auto_login_timer());
685 content::WindowedNotificationObserver profile_prepared_observer(
686 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
687 content::NotificationService::AllSources());
689 // Login and check that it stopped the timer.
690 existing_user_controller()->Login(
691 UserContext(user_manager::USER_TYPE_PUBLIC_ACCOUNT,
692 public_session_user_id_),
693 SigninSpecifics());
695 EXPECT_TRUE(is_login_in_progress());
696 ASSERT_TRUE(auto_login_timer());
697 EXPECT_FALSE(auto_login_timer()->IsRunning());
699 profile_prepared_observer.Wait();
701 // Wait for login tasks to complete.
702 content::RunAllPendingInMessageLoop();
704 // Timer should still be stopped after login completes.
705 ASSERT_TRUE(auto_login_timer());
706 EXPECT_FALSE(auto_login_timer()->IsRunning());
709 IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,
710 LoginForbiddenWhenUntrusted) {
711 // Make cros settings untrusted.
712 MakeCrosSettingsPermanentlyUntrusted();
714 // Check that the attempt to start a public session fails with an error.
715 ExpectLoginFailure();
716 UserContext user_context(kUsername);
717 user_context.SetGaiaID(kGaiaID);
718 user_context.SetKey(Key(kPassword));
719 user_context.SetUserIDHash(user_context.GetUserID());
720 existing_user_controller()->Login(user_context, SigninSpecifics());
723 IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,
724 NoAutoLoginWhenUntrusted) {
725 // Start the public session timer.
726 SetAutoLoginPolicy(kPublicSessionAccountId, kAutoLoginLongDelay);
727 existing_user_controller()->OnSigninScreenReady();
728 EXPECT_TRUE(auto_login_timer());
730 // Make cros settings untrusted.
731 MakeCrosSettingsPermanentlyUntrusted();
733 // Check that when the timer fires, auto-login fails with an error.
734 ExpectLoginFailure();
735 FireAutoLogin();
738 IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,
739 PRE_TestLoadingPublicUsersFromLocalState) {
740 // First run propagates public accounts and stores them in Local State.
743 // See http://crbug.com/393704; flaky.
744 IN_PROC_BROWSER_TEST_F(ExistingUserControllerPublicSessionTest,
745 DISABLED_TestLoadingPublicUsersFromLocalState) {
746 // Second run loads list of public accounts from Local State.
749 } // namespace chromeos