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/command_line.h"
8 #include "base/prefs/scoped_user_pref_update.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/chromeos/login/existing_user_controller.h"
12 #include "chrome/browser/chromeos/login/session/user_session_manager.h"
13 #include "chrome/browser/chromeos/login/session/user_session_manager_test_api.h"
14 #include "chrome/browser/chromeos/login/ui/login_display_host_impl.h"
15 #include "chrome/browser/chromeos/login/ui/webui_login_view.h"
16 #include "chromeos/chromeos_switches.h"
17 #include "chromeos/login/auth/key.h"
18 #include "chromeos/login/auth/user_context.h"
19 #include "components/user_manager/user.h"
20 #include "components/user_manager/user_manager.h"
21 #include "content/public/browser/notification_service.h"
22 #include "content/public/browser/web_contents.h"
23 #include "content/public/test/browser_test_utils.h"
24 #include "content/public/test/test_utils.h"
25 #include "testing/gtest/include/gtest/gtest.h"
29 LoginManagerTest::LoginManagerTest(bool should_launch_browser
)
30 : should_launch_browser_(should_launch_browser
),
32 set_exit_when_last_browser_closes(false);
35 void LoginManagerTest::TearDownOnMainThread() {
36 MixinBasedBrowserTest::TearDownOnMainThread();
37 if (LoginDisplayHostImpl::default_host())
38 LoginDisplayHostImpl::default_host()->Finalize();
39 base::MessageLoop::current()->RunUntilIdle();
42 void LoginManagerTest::SetUpCommandLine(base::CommandLine
* command_line
) {
43 command_line
->AppendSwitch(chromeos::switches::kLoginManager
);
44 command_line
->AppendSwitch(chromeos::switches::kForceLoginManagerInTests
);
45 MixinBasedBrowserTest::SetUpCommandLine(command_line
);
48 void LoginManagerTest::SetUpInProcessBrowserTestFixture() {
49 MixinBasedBrowserTest::SetUpInProcessBrowserTestFixture();
52 void LoginManagerTest::SetUpOnMainThread() {
53 MixinBasedBrowserTest::SetUpOnMainThread();
54 content::WindowedNotificationObserver(
55 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE
,
56 content::NotificationService::AllSources()).Wait();
57 InitializeWebContents();
58 test::UserSessionManagerTestApi
session_manager_test_api(
59 UserSessionManager::GetInstance());
60 session_manager_test_api
.SetShouldLaunchBrowserInTests(
61 should_launch_browser_
);
64 void LoginManagerTest::RegisterUser(const std::string
& user_id
) {
65 ListPrefUpdate
users_pref(g_browser_process
->local_state(), "LoggedInUsers");
66 users_pref
->AppendIfNotPresent(new base::StringValue(user_id
));
69 void LoginManagerTest::SetExpectedCredentials(const UserContext
& user_context
) {
70 test::UserSessionManagerTestApi
session_manager_test_api(
71 UserSessionManager::GetInstance());
72 session_manager_test_api
.InjectStubUserContext(user_context
);
75 bool LoginManagerTest::TryToLogin(const UserContext
& user_context
) {
76 if (!AddUserToSession(user_context
))
78 if (const user_manager::User
* active_user
=
79 user_manager::UserManager::Get()->GetActiveUser())
80 return active_user
->email() == user_context
.GetUserID();
84 bool LoginManagerTest::AddUserToSession(const UserContext
& user_context
) {
85 ExistingUserController
* controller
=
86 ExistingUserController::current_controller();
91 content::WindowedNotificationObserver
observer(
92 chrome::NOTIFICATION_SESSION_STARTED
,
93 content::NotificationService::AllSources());
94 controller
->Login(user_context
, SigninSpecifics());
96 const user_manager::UserList
& logged_users
=
97 user_manager::UserManager::Get()->GetLoggedInUsers();
98 for (user_manager::UserList::const_iterator it
= logged_users
.begin();
99 it
!= logged_users
.end();
101 if ((*it
)->email() == user_context
.GetUserID())
107 void LoginManagerTest::LoginUser(const std::string
& user_id
) {
108 UserContext
user_context(user_id
);
109 user_context
.SetKey(Key("password"));
110 SetExpectedCredentials(user_context
);
111 EXPECT_TRUE(TryToLogin(user_context
));
114 void LoginManagerTest::AddUser(const std::string
& user_id
) {
115 UserContext
user_context(user_id
);
116 user_context
.SetKey(Key("password"));
117 SetExpectedCredentials(user_context
);
118 EXPECT_TRUE(AddUserToSession(user_context
));
121 void LoginManagerTest::JSExpect(const std::string
& expression
) {
122 js_checker_
.ExpectTrue(expression
);
125 void LoginManagerTest::InitializeWebContents() {
126 LoginDisplayHost
* host
= LoginDisplayHostImpl::default_host();
127 EXPECT_TRUE(host
!= NULL
);
129 content::WebContents
* web_contents
=
130 host
->GetWebUILoginView()->GetWebContents();
131 EXPECT_TRUE(web_contents
!= NULL
);
132 set_web_contents(web_contents
);
133 js_checker_
.set_web_contents(web_contents
);
136 } // namespace chromeos