| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/login/login_manager_test.h" | 5 #include "chrome/browser/chromeos/login/login_manager_test.h" |
| 6 | 6 |
| 7 #include "base/prefs/scoped_user_pref_update.h" | 7 #include "base/prefs/scoped_user_pref_update.h" |
| 8 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/chrome_notification_types.h" | 9 #include "chrome/browser/chrome_notification_types.h" |
| 10 #include "chrome/browser/chromeos/login/auth/key.h" |
| 10 #include "chrome/browser/chromeos/login/auth/user_context.h" | 11 #include "chrome/browser/chromeos/login/auth/user_context.h" |
| 11 #include "chrome/browser/chromeos/login/existing_user_controller.h" | 12 #include "chrome/browser/chromeos/login/existing_user_controller.h" |
| 12 #include "chrome/browser/chromeos/login/ui/login_display_host_impl.h" | 13 #include "chrome/browser/chromeos/login/ui/login_display_host_impl.h" |
| 13 #include "chrome/browser/chromeos/login/ui/webui_login_view.h" | 14 #include "chrome/browser/chromeos/login/ui/webui_login_view.h" |
| 14 #include "chrome/browser/chromeos/login/users/user.h" | 15 #include "chrome/browser/chromeos/login/users/user.h" |
| 15 #include "chrome/browser/chromeos/login/users/user_manager.h" | 16 #include "chrome/browser/chromeos/login/users/user_manager.h" |
| 16 #include "chromeos/chromeos_switches.h" | 17 #include "chromeos/chromeos_switches.h" |
| 17 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
| 18 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
| 19 #include "content/public/test/browser_test_utils.h" | 20 #include "content/public/test/browser_test_utils.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 47 LoginUtils::Set(mock_login_utils_); | 48 LoginUtils::Set(mock_login_utils_); |
| 48 } | 49 } |
| 49 | 50 |
| 50 void LoginManagerTest::SetUpOnMainThread() { | 51 void LoginManagerTest::SetUpOnMainThread() { |
| 51 content::WindowedNotificationObserver( | 52 content::WindowedNotificationObserver( |
| 52 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, | 53 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, |
| 53 content::NotificationService::AllSources()).Wait(); | 54 content::NotificationService::AllSources()).Wait(); |
| 54 InitializeWebContents(); | 55 InitializeWebContents(); |
| 55 } | 56 } |
| 56 | 57 |
| 57 void LoginManagerTest::RegisterUser(const std::string& username) { | 58 void LoginManagerTest::RegisterUser(const std::string& user_id) { |
| 58 ListPrefUpdate users_pref(g_browser_process->local_state(), "LoggedInUsers"); | 59 ListPrefUpdate users_pref(g_browser_process->local_state(), "LoggedInUsers"); |
| 59 users_pref->AppendIfNotPresent(new base::StringValue(username)); | 60 users_pref->AppendIfNotPresent(new base::StringValue(user_id)); |
| 60 } | 61 } |
| 61 | 62 |
| 62 void LoginManagerTest::SetExpectedCredentials(const std::string& username, | 63 void LoginManagerTest::SetExpectedCredentials(const UserContext& user_context) { |
| 63 const std::string& password) { | 64 login_utils().GetFakeLoginUtils()->SetExpectedCredentials(user_context); |
| 64 login_utils().GetFakeLoginUtils()->SetExpectedCredentials(username, password); | |
| 65 } | 65 } |
| 66 | 66 |
| 67 bool LoginManagerTest::TryToLogin(const std::string& username, | 67 bool LoginManagerTest::TryToLogin(const UserContext& user_context) { |
| 68 const std::string& password) { | 68 if (!AddUserToSession(user_context)) |
| 69 if (!AddUserToSession(username, password)) | |
| 70 return false; | 69 return false; |
| 71 if (const User* active_user = UserManager::Get()->GetActiveUser()) | 70 if (const User* active_user = UserManager::Get()->GetActiveUser()) |
| 72 return active_user->email() == username; | 71 return active_user->email() == user_context.GetUserID(); |
| 73 return false; | 72 return false; |
| 74 } | 73 } |
| 75 | 74 |
| 76 bool LoginManagerTest::AddUserToSession(const std::string& username, | 75 bool LoginManagerTest::AddUserToSession(const UserContext& user_context) { |
| 77 const std::string& password) { | |
| 78 ExistingUserController* controller = | 76 ExistingUserController* controller = |
| 79 ExistingUserController::current_controller(); | 77 ExistingUserController::current_controller(); |
| 80 if (!controller) { | 78 if (!controller) { |
| 81 ADD_FAILURE(); | 79 ADD_FAILURE(); |
| 82 return false; | 80 return false; |
| 83 } | 81 } |
| 84 UserContext user_context(username); | |
| 85 user_context.SetPassword(password); | |
| 86 controller->Login(user_context); | 82 controller->Login(user_context); |
| 87 content::WindowedNotificationObserver( | 83 content::WindowedNotificationObserver( |
| 88 chrome::NOTIFICATION_SESSION_STARTED, | 84 chrome::NOTIFICATION_SESSION_STARTED, |
| 89 content::NotificationService::AllSources()).Wait(); | 85 content::NotificationService::AllSources()).Wait(); |
| 90 const UserList& logged_users = UserManager::Get()->GetLoggedInUsers(); | 86 const UserList& logged_users = UserManager::Get()->GetLoggedInUsers(); |
| 91 for (UserList::const_iterator it = logged_users.begin(); | 87 for (UserList::const_iterator it = logged_users.begin(); |
| 92 it != logged_users.end(); ++it) { | 88 it != logged_users.end(); ++it) { |
| 93 if ((*it)->email() == username) | 89 if ((*it)->email() == user_context.GetUserID()) |
| 94 return true; | 90 return true; |
| 95 } | 91 } |
| 96 return false; | 92 return false; |
| 97 } | 93 } |
| 98 | 94 |
| 99 void LoginManagerTest::LoginUser(const std::string& username) { | 95 void LoginManagerTest::LoginUser(const std::string& user_id) { |
| 100 SetExpectedCredentials(username, "password"); | 96 UserContext user_context(user_id); |
| 101 EXPECT_TRUE(TryToLogin(username, "password")); | 97 user_context.SetKey(Key("password")); |
| 98 SetExpectedCredentials(user_context); |
| 99 EXPECT_TRUE(TryToLogin(user_context)); |
| 102 } | 100 } |
| 103 | 101 |
| 104 void LoginManagerTest::AddUser(const std::string& username) { | 102 void LoginManagerTest::AddUser(const std::string& user_id) { |
| 105 SetExpectedCredentials(username, "password"); | 103 UserContext user_context(user_id); |
| 106 EXPECT_TRUE(AddUserToSession(username, "password")); | 104 user_context.SetKey(Key("password")); |
| 105 SetExpectedCredentials(user_context); |
| 106 EXPECT_TRUE(AddUserToSession(user_context)); |
| 107 } | 107 } |
| 108 | 108 |
| 109 void LoginManagerTest::JSExpect(const std::string& expression) { | 109 void LoginManagerTest::JSExpect(const std::string& expression) { |
| 110 js_checker_.ExpectTrue(expression); | 110 js_checker_.ExpectTrue(expression); |
| 111 } | 111 } |
| 112 | 112 |
| 113 void LoginManagerTest::InitializeWebContents() { | 113 void LoginManagerTest::InitializeWebContents() { |
| 114 LoginDisplayHost* host = LoginDisplayHostImpl::default_host(); | 114 LoginDisplayHost* host = LoginDisplayHostImpl::default_host(); |
| 115 EXPECT_TRUE(host != NULL); | 115 EXPECT_TRUE(host != NULL); |
| 116 | 116 |
| 117 content::WebContents* web_contents = | 117 content::WebContents* web_contents = |
| 118 host->GetWebUILoginView()->GetWebContents(); | 118 host->GetWebUILoginView()->GetWebContents(); |
| 119 EXPECT_TRUE(web_contents != NULL); | 119 EXPECT_TRUE(web_contents != NULL); |
| 120 set_web_contents(web_contents); | 120 set_web_contents(web_contents); |
| 121 js_checker_.set_web_contents(web_contents); | 121 js_checker_.set_web_contents(web_contents); |
| 122 } | 122 } |
| 123 | 123 |
| 124 } // namespace chromeos | 124 } // namespace chromeos |
| OLD | NEW |