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/json/json_file_value_serializer.h"
9 #include "base/path_service.h"
10 #include "base/prefs/scoped_user_pref_update.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/chromeos/login/existing_user_controller.h"
14 #include "chrome/browser/chromeos/login/session/user_session_manager.h"
15 #include "chrome/browser/chromeos/login/session/user_session_manager_test_api.h"
16 #include "chrome/browser/chromeos/login/startup_utils.h"
17 #include "chrome/browser/chromeos/login/ui/login_display_host_impl.h"
18 #include "chrome/browser/chromeos/login/ui/webui_login_view.h"
19 #include "chrome/common/chrome_constants.h"
20 #include "chrome/common/chrome_paths.h"
21 #include "chrome/common/pref_names.h"
22 #include "chromeos/chromeos_switches.h"
23 #include "chromeos/login/auth/key.h"
24 #include "chromeos/login/auth/user_context.h"
25 #include "components/user_manager/user.h"
26 #include "components/user_manager/user_manager.h"
27 #include "content/public/browser/notification_service.h"
28 #include "content/public/browser/web_contents.h"
29 #include "content/public/test/browser_test_utils.h"
30 #include "content/public/test/test_utils.h"
31 #include "testing/gtest/include/gtest/gtest.h"
35 LoginManagerTest::LoginManagerTest(bool should_launch_browser
)
37 should_launch_browser_(should_launch_browser
),
39 set_exit_when_last_browser_closes(false);
42 void LoginManagerTest::TearDownOnMainThread() {
43 MixinBasedBrowserTest::TearDownOnMainThread();
44 if (LoginDisplayHostImpl::default_host())
45 LoginDisplayHostImpl::default_host()->Finalize();
46 base::MessageLoop::current()->RunUntilIdle();
49 void LoginManagerTest::SetUpCommandLine(base::CommandLine
* command_line
) {
50 command_line
->AppendSwitch(chromeos::switches::kLoginManager
);
51 command_line
->AppendSwitch(chromeos::switches::kForceLoginManagerInTests
);
52 MixinBasedBrowserTest::SetUpCommandLine(command_line
);
55 void LoginManagerTest::SetUpInProcessBrowserTestFixture() {
56 MixinBasedBrowserTest::SetUpInProcessBrowserTestFixture();
59 void LoginManagerTest::SetUpOnMainThread() {
60 MixinBasedBrowserTest::SetUpOnMainThread();
61 content::WindowedNotificationObserver(
62 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE
,
63 content::NotificationService::AllSources()).Wait();
64 InitializeWebContents();
65 test::UserSessionManagerTestApi
session_manager_test_api(
66 UserSessionManager::GetInstance());
67 session_manager_test_api
.SetShouldLaunchBrowserInTests(
68 should_launch_browser_
);
69 session_manager_test_api
.SetShouldObtainTokenHandleInTests(false);
72 bool LoginManagerTest::SetUpUserDataDirectory() {
73 base::FilePath user_data_dir
;
74 CHECK(PathService::Get(chrome::DIR_USER_DATA
, &user_data_dir
));
75 base::FilePath local_state_path
=
76 user_data_dir
.Append(chrome::kLocalStateFilename
);
79 // Set webview disabled flag only when local state file does not exist.
80 // Otherwise, we break PRE tests that leave state in it.
81 if (!base::PathExists(local_state_path
)) {
82 base::DictionaryValue local_state_dict
;
84 // TODO(nkostylev): Fix tests that fail with webview signin.
85 local_state_dict
.SetBoolean(prefs::kWebviewSigninDisabled
, true);
87 CHECK(JSONFileValueSerializer(local_state_path
)
88 .Serialize(local_state_dict
));
92 return MixinBasedBrowserTest::SetUpUserDataDirectory();
95 void LoginManagerTest::RegisterUser(const std::string
& user_id
) {
96 ListPrefUpdate
users_pref(g_browser_process
->local_state(), "LoggedInUsers");
97 users_pref
->AppendIfNotPresent(new base::StringValue(user_id
));
100 void LoginManagerTest::SetExpectedCredentials(const UserContext
& user_context
) {
101 test::UserSessionManagerTestApi
session_manager_test_api(
102 UserSessionManager::GetInstance());
103 session_manager_test_api
.InjectStubUserContext(user_context
);
106 bool LoginManagerTest::TryToLogin(const UserContext
& user_context
) {
107 if (!AddUserToSession(user_context
))
109 if (const user_manager::User
* active_user
=
110 user_manager::UserManager::Get()->GetActiveUser())
111 return active_user
->email() == user_context
.GetUserID();
115 bool LoginManagerTest::AddUserToSession(const UserContext
& user_context
) {
116 ExistingUserController
* controller
=
117 ExistingUserController::current_controller();
122 content::WindowedNotificationObserver
observer(
123 chrome::NOTIFICATION_SESSION_STARTED
,
124 content::NotificationService::AllSources());
125 controller
->Login(user_context
, SigninSpecifics());
127 const user_manager::UserList
& logged_users
=
128 user_manager::UserManager::Get()->GetLoggedInUsers();
129 for (user_manager::UserList::const_iterator it
= logged_users
.begin();
130 it
!= logged_users
.end();
132 if ((*it
)->email() == user_context
.GetUserID())
138 void LoginManagerTest::LoginUser(const std::string
& user_id
) {
139 UserContext
user_context(user_id
);
140 user_context
.SetGaiaID(GetGaiaIDForUserID(user_id
));
141 user_context
.SetKey(Key("password"));
142 SetExpectedCredentials(user_context
);
143 EXPECT_TRUE(TryToLogin(user_context
));
146 void LoginManagerTest::AddUser(const std::string
& user_id
) {
147 UserContext
user_context(user_id
);
148 user_context
.SetGaiaID(GetGaiaIDForUserID(user_id
));
149 user_context
.SetKey(Key("password"));
150 SetExpectedCredentials(user_context
);
151 EXPECT_TRUE(AddUserToSession(user_context
));
155 std::string
LoginManagerTest::GetGaiaIDForUserID(const std::string
& user_id
) {
156 return "gaia-id-" + user_id
;
159 void LoginManagerTest::JSExpect(const std::string
& expression
) {
160 js_checker_
.ExpectTrue(expression
);
163 void LoginManagerTest::InitializeWebContents() {
164 LoginDisplayHost
* host
= LoginDisplayHostImpl::default_host();
165 EXPECT_TRUE(host
!= NULL
);
167 content::WebContents
* web_contents
=
168 host
->GetWebUILoginView()->GetWebContents();
169 EXPECT_TRUE(web_contents
!= NULL
);
170 set_web_contents(web_contents
);
171 js_checker_
.set_web_contents(web_contents
);
174 } // namespace chromeos