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.
5 #include "chrome/browser/chromeos/login/login_manager_test.h"
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/login_display_host_impl.h"
12 #include "chrome/browser/chromeos/login/user_manager.h"
13 #include "chrome/browser/chromeos/login/webui_login_view.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "chromeos/chromeos_switches.h"
16 #include "content/public/browser/notification_service.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/test/browser_test_utils.h"
19 #include "content/public/test/test_utils.h"
20 #include "testing/gtest/include/gtest/gtest.h"
24 LoginManagerTest::LoginManagerTest(bool should_launch_browser)
25 : should_launch_browser_(should_launch_browser),
27 set_exit_when_last_browser_closes(false);
30 void LoginManagerTest::CleanUpOnMainThread() {
31 if (LoginDisplayHostImpl::default_host())
32 LoginDisplayHostImpl::default_host()->Finalize();
33 base::MessageLoop::current()->RunUntilIdle();
36 void LoginManagerTest::SetUpCommandLine(CommandLine* command_line) {
37 command_line->AppendSwitch(chromeos::switches::kLoginManager);
38 command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests);
39 command_line->AppendSwitch(::switches::kMultiProfiles);
40 InProcessBrowserTest::SetUpCommandLine(command_line);
43 void LoginManagerTest::SetUpInProcessBrowserTestFixture() {
44 mock_login_utils_ = new testing::NiceMock<MockLoginUtils>();
45 mock_login_utils_->DelegateToFake();
46 mock_login_utils_->GetFakeLoginUtils()->set_should_launch_browser(
47 should_launch_browser_);
48 LoginUtils::Set(mock_login_utils_);
51 void LoginManagerTest::SetUpOnMainThread() {
52 content::WindowedNotificationObserver(
53 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
54 content::NotificationService::AllSources()).Wait();
55 InitializeWebContents();
58 void LoginManagerTest::RegisterUser(const std::string& username) {
59 ListPrefUpdate users_pref(g_browser_process->local_state(), "LoggedInUsers");
60 users_pref->AppendIfNotPresent(new base::StringValue(username));
63 void LoginManagerTest::SetExpectedCredentials(const std::string& username,
64 const std::string& password) {
65 login_utils().GetFakeLoginUtils()->SetExpectedCredentials(username, password);
68 bool LoginManagerTest::TryToLogin(const std::string& username,
69 const std::string& password) {
70 if (!AddUserTosession(username, password))
72 if (const User* active_user = UserManager::Get()->GetActiveUser())
73 return active_user->email() == username;
77 bool LoginManagerTest::AddUserTosession(const std::string& username,
78 const std::string& password) {
79 ExistingUserController* controller =
80 ExistingUserController::current_controller();
81 EXPECT_TRUE(controller != NULL);
82 controller->Login(UserContext(username, password, std::string()));
83 content::WindowedNotificationObserver(
84 chrome::NOTIFICATION_SESSION_STARTED,
85 content::NotificationService::AllSources()).Wait();
86 const UserList& logged_users = UserManager::Get()->GetLoggedInUsers();
87 for (UserList::const_iterator it = logged_users.begin();
88 it != logged_users.end(); ++it) {
89 if ((*it)->email() == username)
95 void LoginManagerTest::LoginUser(const std::string& username) {
96 SetExpectedCredentials(username, "password");
97 EXPECT_TRUE(TryToLogin(username, "password"));
100 void LoginManagerTest::AddUser(const std::string& username) {
101 SetExpectedCredentials(username, "password");
102 EXPECT_TRUE(AddUserTosession(username, "password"));
105 void LoginManagerTest::JSExpect(const std::string& expression) {
107 EXPECT_TRUE(web_contents_ != NULL);
108 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
110 "window.domAutomationController.send(!!(" + expression + "));",
112 ASSERT_TRUE(result) << expression;
115 void LoginManagerTest::InitializeWebContents() {
116 LoginDisplayHost* host = LoginDisplayHostImpl::default_host();
117 EXPECT_TRUE(host != NULL);
119 content::WebContents* web_contents =
120 host->GetWebUILoginView()->GetWebContents();
121 EXPECT_TRUE(web_contents != NULL);
122 set_web_contents(web_contents);
125 } // namespace chromeos