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 command_line
->AppendSwitchASCII(::switches::kForceFieldTrials
,
41 "ChromeOSUseMultiProfiles/Enable/");
44 void LoginManagerTest::SetUpInProcessBrowserTestFixture() {
45 mock_login_utils_
= new testing::NiceMock
<MockLoginUtils
>();
46 mock_login_utils_
->DelegateToFake();
47 mock_login_utils_
->GetFakeLoginUtils()->set_should_launch_browser(
48 should_launch_browser_
);
49 LoginUtils::Set(mock_login_utils_
);
52 void LoginManagerTest::SetUpOnMainThread() {
53 content::WindowedNotificationObserver(
54 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE
,
55 content::NotificationService::AllSources()).Wait();
56 InitializeWebContents();
59 void LoginManagerTest::RegisterUser(const std::string
& username
) {
60 ListPrefUpdate
users_pref(g_browser_process
->local_state(), "LoggedInUsers");
61 users_pref
->AppendIfNotPresent(new base::StringValue(username
));
64 void LoginManagerTest::SetExpectedCredentials(const std::string
& username
,
65 const std::string
& password
) {
66 login_utils().GetFakeLoginUtils()->SetExpectedCredentials(username
, password
);
69 bool LoginManagerTest::TryToLogin(const std::string
& username
,
70 const std::string
& password
) {
71 if (!AddUserTosession(username
, password
))
73 if (const User
* active_user
= UserManager::Get()->GetActiveUser())
74 return active_user
->email() == username
;
78 bool LoginManagerTest::AddUserTosession(const std::string
& username
,
79 const std::string
& password
) {
80 ExistingUserController
* controller
=
81 ExistingUserController::current_controller();
86 controller
->Login(UserContext(username
, password
, std::string()));
87 content::WindowedNotificationObserver(
88 chrome::NOTIFICATION_SESSION_STARTED
,
89 content::NotificationService::AllSources()).Wait();
90 const UserList
& logged_users
= UserManager::Get()->GetLoggedInUsers();
91 for (UserList::const_iterator it
= logged_users
.begin();
92 it
!= logged_users
.end(); ++it
) {
93 if ((*it
)->email() == username
)
99 void LoginManagerTest::LoginUser(const std::string
& username
) {
100 SetExpectedCredentials(username
, "password");
101 EXPECT_TRUE(TryToLogin(username
, "password"));
104 void LoginManagerTest::AddUser(const std::string
& username
) {
105 SetExpectedCredentials(username
, "password");
106 EXPECT_TRUE(AddUserTosession(username
, "password"));
109 void LoginManagerTest::JSExpect(const std::string
& expression
) {
110 js_checker_
.ExpectTrue(expression
);
113 void LoginManagerTest::InitializeWebContents() {
114 LoginDisplayHost
* host
= LoginDisplayHostImpl::default_host();
115 EXPECT_TRUE(host
!= NULL
);
117 content::WebContents
* web_contents
=
118 host
->GetWebUILoginView()->GetWebContents();
119 EXPECT_TRUE(web_contents
!= NULL
);
120 set_web_contents(web_contents
);
121 js_checker_
.set_web_contents(web_contents
);
124 } // namespace chromeos