1 // Copyright 2013 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 "chrome/browser/chromeos/login/login_manager_test.h" 6 7 #include "base/prefs/scoped_user_pref_update.h" 8 #include "chrome/browser/browser_process.h" 9 #include "chrome/browser/chrome_notification_types.h" 10 #include "chrome/browser/chromeos/login/existing_user_controller.h" 11 #include "chrome/browser/chromeos/login/ui/login_display_host_impl.h" 12 #include "chrome/browser/chromeos/login/ui/webui_login_view.h" 13 #include "chromeos/chromeos_switches.h" 14 #include "chromeos/login/auth/key.h" 15 #include "chromeos/login/auth/user_context.h" 16 #include "components/user_manager/user.h" 17 #include "components/user_manager/user_manager.h" 18 #include "content/public/browser/notification_service.h" 19 #include "content/public/browser/web_contents.h" 20 #include "content/public/test/browser_test_utils.h" 21 #include "content/public/test/test_utils.h" 22 #include "testing/gtest/include/gtest/gtest.h" 23 24 namespace chromeos { 25 26 LoginManagerTest::LoginManagerTest(bool should_launch_browser) 27 : should_launch_browser_(should_launch_browser), 28 web_contents_(NULL) { 29 set_exit_when_last_browser_closes(false); 30 } 31 32 void LoginManagerTest::TearDownOnMainThread() { 33 MixinBasedBrowserTest::TearDownOnMainThread(); 34 if (LoginDisplayHostImpl::default_host()) 35 LoginDisplayHostImpl::default_host()->Finalize(); 36 base::MessageLoop::current()->RunUntilIdle(); 37 } 38 39 void LoginManagerTest::SetUpCommandLine(CommandLine* command_line) { 40 command_line->AppendSwitch(chromeos::switches::kLoginManager); 41 command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests); 42 MixinBasedBrowserTest::SetUpCommandLine(command_line); 43 } 44 45 void LoginManagerTest::SetUpInProcessBrowserTestFixture() { 46 mock_login_utils_ = new testing::NiceMock<MockLoginUtils>(); 47 mock_login_utils_->DelegateToFake(); 48 mock_login_utils_->GetFakeLoginUtils()->set_should_launch_browser( 49 should_launch_browser_); 50 LoginUtils::Set(mock_login_utils_); 51 MixinBasedBrowserTest::SetUpInProcessBrowserTestFixture(); 52 } 53 54 void LoginManagerTest::SetUpOnMainThread() { 55 content::WindowedNotificationObserver( 56 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, 57 content::NotificationService::AllSources()).Wait(); 58 InitializeWebContents(); 59 MixinBasedBrowserTest::SetUpOnMainThread(); 60 } 61 62 void LoginManagerTest::RegisterUser(const std::string& user_id) { 63 ListPrefUpdate users_pref(g_browser_process->local_state(), "LoggedInUsers"); 64 users_pref->AppendIfNotPresent(new base::StringValue(user_id)); 65 } 66 67 void LoginManagerTest::SetExpectedCredentials(const UserContext& user_context) { 68 login_utils().GetFakeLoginUtils()->SetExpectedCredentials(user_context); 69 } 70 71 bool LoginManagerTest::TryToLogin(const UserContext& user_context) { 72 if (!AddUserToSession(user_context)) 73 return false; 74 if (const user_manager::User* active_user = 75 user_manager::UserManager::Get()->GetActiveUser()) 76 return active_user->email() == user_context.GetUserID(); 77 return false; 78 } 79 80 bool LoginManagerTest::AddUserToSession(const UserContext& user_context) { 81 ExistingUserController* controller = 82 ExistingUserController::current_controller(); 83 if (!controller) { 84 ADD_FAILURE(); 85 return false; 86 } 87 controller->Login(user_context, SigninSpecifics()); 88 content::WindowedNotificationObserver( 89 chrome::NOTIFICATION_SESSION_STARTED, 90 content::NotificationService::AllSources()).Wait(); 91 const user_manager::UserList& logged_users = 92 user_manager::UserManager::Get()->GetLoggedInUsers(); 93 for (user_manager::UserList::const_iterator it = logged_users.begin(); 94 it != logged_users.end(); 95 ++it) { 96 if ((*it)->email() == user_context.GetUserID()) 97 return true; 98 } 99 return false; 100 } 101 102 void LoginManagerTest::LoginUser(const std::string& user_id) { 103 UserContext user_context(user_id); 104 user_context.SetKey(Key("password")); 105 SetExpectedCredentials(user_context); 106 EXPECT_TRUE(TryToLogin(user_context)); 107 } 108 109 void LoginManagerTest::AddUser(const std::string& user_id) { 110 UserContext user_context(user_id); 111 user_context.SetKey(Key("password")); 112 SetExpectedCredentials(user_context); 113 EXPECT_TRUE(AddUserToSession(user_context)); 114 } 115 116 void LoginManagerTest::JSExpect(const std::string& expression) { 117 js_checker_.ExpectTrue(expression); 118 } 119 120 void LoginManagerTest::InitializeWebContents() { 121 LoginDisplayHost* host = LoginDisplayHostImpl::default_host(); 122 EXPECT_TRUE(host != NULL); 123 124 content::WebContents* web_contents = 125 host->GetWebUILoginView()->GetWebContents(); 126 EXPECT_TRUE(web_contents != NULL); 127 set_web_contents(web_contents); 128 js_checker_.set_web_contents(web_contents); 129 } 130 131 } // namespace chromeos 132