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/ui/login_display_host_impl.h"
12 #include "chrome/browser/chromeos/login/ui/webui_login_view.h"
13 #include "chromeos/chromeos_switches.h"
14 #include "chromeos/login/auth/key.h"
15 #include "chromeos/login/auth/user_context.h"
16 #include "components/user_manager/user.h"
17 #include "components/user_manager/user_manager.h"
18 #include "content/public/browser/notification_service.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/test/browser_test_utils.h"
21 #include "content/public/test/test_utils.h"
22 #include "testing/gtest/include/gtest/gtest.h"
26 LoginManagerTest::LoginManagerTest(bool should_launch_browser)
27 : should_launch_browser_(should_launch_browser),
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(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();
47 mock_login_utils_ = new testing::NiceMock<MockLoginUtils>();
48 mock_login_utils_->DelegateToFake();
49 mock_login_utils_->GetFakeLoginUtils()->set_should_launch_browser(
50 should_launch_browser_);
51 LoginUtils::Set(mock_login_utils_);
54 void LoginManagerTest::SetUpOnMainThread() {
55 MixinBasedBrowserTest::SetUpOnMainThread();
56 content::WindowedNotificationObserver(
57 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
58 content::NotificationService::AllSources()).Wait();
59 InitializeWebContents();
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 login_utils().GetFakeLoginUtils()->SetExpectedCredentials(user_context);
71 bool LoginManagerTest::TryToLogin(const UserContext& user_context) {
72 if (!AddUserToSession(user_context))
74 if (const user_manager::User* active_user =
75 user_manager::UserManager::Get()->GetActiveUser())
76 return active_user->email() == user_context.GetUserID();
80 bool LoginManagerTest::AddUserToSession(const UserContext& user_context) {
81 ExistingUserController* controller =
82 ExistingUserController::current_controller();
87 content::WindowedNotificationObserver observer(
88 chrome::NOTIFICATION_SESSION_STARTED,
89 content::NotificationService::AllSources());
90 controller->Login(user_context, SigninSpecifics());
92 const user_manager::UserList& logged_users =
93 user_manager::UserManager::Get()->GetLoggedInUsers();
94 for (user_manager::UserList::const_iterator it = logged_users.begin();
95 it != logged_users.end();
97 if ((*it)->email() == user_context.GetUserID())
103 void LoginManagerTest::LoginUser(const std::string& user_id) {
104 UserContext user_context(user_id);
105 user_context.SetKey(Key("password"));
106 SetExpectedCredentials(user_context);
107 EXPECT_TRUE(TryToLogin(user_context));
110 void LoginManagerTest::AddUser(const std::string& user_id) {
111 UserContext user_context(user_id);
112 user_context.SetKey(Key("password"));
113 SetExpectedCredentials(user_context);
114 EXPECT_TRUE(AddUserToSession(user_context));
117 void LoginManagerTest::JSExpect(const std::string& expression) {
118 js_checker_.ExpectTrue(expression);
121 void LoginManagerTest::InitializeWebContents() {
122 LoginDisplayHost* host = LoginDisplayHostImpl::default_host();
123 EXPECT_TRUE(host != NULL);
125 content::WebContents* web_contents =
126 host->GetWebUILoginView()->GetWebContents();
127 EXPECT_TRUE(web_contents != NULL);
128 set_web_contents(web_contents);
129 js_checker_.set_web_contents(web_contents);
132 } // namespace chromeos