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/test_utils.h"
27 LoginManagerTest::LoginManagerTest(bool should_launch_browser
)
28 : should_launch_browser_(should_launch_browser
), web_contents_(NULL
) {
29 set_exit_when_last_browser_closes(false);
32 void LoginManagerTest::TearDownOnMainThread() {
33 MixinBasedBrowserTest::TearDownOnMainThread();
34 if (LoginDisplayHostImpl::default_host())
35 LoginDisplayHostImpl::default_host()->Finalize();
36 base::MessageLoop::current()->RunUntilIdle();
39 void LoginManagerTest::SetUpCommandLine(base::CommandLine
* command_line
) {
40 command_line
->AppendSwitch(chromeos::switches::kLoginManager
);
41 command_line
->AppendSwitch(chromeos::switches::kForceLoginManagerInTests
);
42 MixinBasedBrowserTest::SetUpCommandLine(command_line
);
45 void LoginManagerTest::SetUpInProcessBrowserTestFixture() {
46 MixinBasedBrowserTest::SetUpInProcessBrowserTestFixture();
49 void LoginManagerTest::SetUpOnMainThread() {
50 MixinBasedBrowserTest::SetUpOnMainThread();
51 content::WindowedNotificationObserver(
52 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE
,
53 content::NotificationService::AllSources()).Wait();
54 InitializeWebContents();
55 test::UserSessionManagerTestApi
session_manager_test_api(
56 UserSessionManager::GetInstance());
57 session_manager_test_api
.SetShouldLaunchBrowserInTests(
58 should_launch_browser_
);
59 session_manager_test_api
.SetShouldObtainTokenHandleInTests(false);
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
));
67 void LoginManagerTest::SetExpectedCredentials(const UserContext
& user_context
) {
68 test::UserSessionManagerTestApi
session_manager_test_api(
69 UserSessionManager::GetInstance());
70 session_manager_test_api
.InjectStubUserContext(user_context
);
73 bool LoginManagerTest::TryToLogin(const UserContext
& user_context
) {
74 if (!AddUserToSession(user_context
))
76 if (const user_manager::User
* active_user
=
77 user_manager::UserManager::Get()->GetActiveUser())
78 return active_user
->email() == user_context
.GetUserID();
82 bool LoginManagerTest::AddUserToSession(const UserContext
& user_context
) {
83 ExistingUserController
* controller
=
84 ExistingUserController::current_controller();
89 content::WindowedNotificationObserver
observer(
90 chrome::NOTIFICATION_SESSION_STARTED
,
91 content::NotificationService::AllSources());
92 controller
->Login(user_context
, SigninSpecifics());
94 const user_manager::UserList
& logged_users
=
95 user_manager::UserManager::Get()->GetLoggedInUsers();
96 for (user_manager::UserList::const_iterator it
= logged_users
.begin();
97 it
!= logged_users
.end();
99 if ((*it
)->email() == user_context
.GetUserID())
105 void LoginManagerTest::LoginUser(const std::string
& user_id
) {
106 UserContext
user_context(user_id
);
107 user_context
.SetGaiaID(GetGaiaIDForUserID(user_id
));
108 user_context
.SetKey(Key("password"));
109 SetExpectedCredentials(user_context
);
110 EXPECT_TRUE(TryToLogin(user_context
));
113 void LoginManagerTest::AddUser(const std::string
& user_id
) {
114 UserContext
user_context(user_id
);
115 user_context
.SetGaiaID(GetGaiaIDForUserID(user_id
));
116 user_context
.SetKey(Key("password"));
117 SetExpectedCredentials(user_context
);
118 EXPECT_TRUE(AddUserToSession(user_context
));
122 std::string
LoginManagerTest::GetGaiaIDForUserID(const std::string
& user_id
) {
123 return "gaia-id-" + user_id
;
126 void LoginManagerTest::JSExpect(const std::string
& expression
) {
127 js_checker_
.ExpectTrue(expression
);
130 void LoginManagerTest::InitializeWebContents() {
131 LoginDisplayHost
* host
= LoginDisplayHostImpl::default_host();
132 EXPECT_TRUE(host
!= NULL
);
134 content::WebContents
* web_contents
=
135 host
->GetWebUILoginView()->GetWebContents();
136 EXPECT_TRUE(web_contents
!= NULL
);
137 set_web_contents(web_contents
);
138 js_checker_
.set_web_contents(web_contents
);
141 } // namespace chromeos