OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_helper.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/user_context.h" | 10 #include "chrome/browser/chromeos/login/auth/user_context.h" |
11 #include "chrome/browser/chromeos/login/existing_user_controller.h" | 11 #include "chrome/browser/chromeos/login/existing_user_controller.h" |
12 #include "chrome/browser/chromeos/login/ui/login_display_host_impl.h" | 12 #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/users/user.h" | 13 #include "chrome/browser/chromeos/login/users/user.h" |
15 #include "chrome/browser/chromeos/login/users/user_manager.h" | 14 #include "chrome/browser/chromeos/login/users/user_manager.h" |
16 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
17 #include "chromeos/chromeos_switches.h" | 16 #include "chromeos/chromeos_switches.h" |
18 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
19 #include "content/public/browser/web_contents.h" | |
20 #include "content/public/test/browser_test_utils.h" | 18 #include "content/public/test/browser_test_utils.h" |
21 #include "content/public/test/test_utils.h" | 19 #include "content/public/test/test_utils.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
23 | 21 |
24 namespace chromeos { | 22 namespace chromeos { |
25 | 23 |
26 LoginManagerTest::LoginManagerTest(bool should_launch_browser) | 24 LoginManagerTestHelper::LoginManagerTestHelper() {} |
27 : should_launch_browser_(should_launch_browser), | 25 |
28 web_contents_(NULL) { | 26 LoginManagerTestHelper::~LoginManagerTestHelper() {} |
29 set_exit_when_last_browser_closes(false); | 27 |
| 28 void LoginManagerTestHelper::SetUpCommandLine(CommandLine* command_line) { |
| 29 command_line->AppendSwitch(chromeos::switches::kLoginManager); |
| 30 command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests); |
| 31 command_line->AppendSwitch(::switches::kMultiProfiles); |
30 } | 32 } |
31 | 33 |
32 void LoginManagerTest::CleanUpOnMainThread() { | 34 void LoginManagerTestHelper::SetUpLoginUtils(bool should_launch_browser) { |
| 35 mock_login_utils_ = new testing::NiceMock<MockLoginUtils>(); |
| 36 mock_login_utils_->DelegateToFake(); |
| 37 mock_login_utils_->GetFakeLoginUtils()->set_should_launch_browser( |
| 38 should_launch_browser); |
| 39 LoginUtils::Set(mock_login_utils_); |
| 40 } |
| 41 |
| 42 void LoginManagerTestHelper::WaitForLoginWebUI() { |
| 43 content::WindowedNotificationObserver( |
| 44 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, |
| 45 content::NotificationService::AllSources()).Wait(); |
| 46 } |
| 47 |
| 48 void LoginManagerTestHelper::CloseLoginWebUI() { |
33 if (LoginDisplayHostImpl::default_host()) | 49 if (LoginDisplayHostImpl::default_host()) |
34 LoginDisplayHostImpl::default_host()->Finalize(); | 50 LoginDisplayHostImpl::default_host()->Finalize(); |
35 base::MessageLoop::current()->RunUntilIdle(); | 51 base::MessageLoop::current()->RunUntilIdle(); |
36 } | 52 } |
37 | 53 |
38 void LoginManagerTest::SetUpCommandLine(CommandLine* command_line) { | 54 void LoginManagerTestHelper::RegisterUser(const std::string& username) { |
39 command_line->AppendSwitch(chromeos::switches::kLoginManager); | |
40 command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests); | |
41 command_line->AppendSwitch(::switches::kMultiProfiles); | |
42 } | |
43 | |
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_); | |
50 } | |
51 | |
52 void LoginManagerTest::SetUpOnMainThread() { | |
53 content::WindowedNotificationObserver( | |
54 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, | |
55 content::NotificationService::AllSources()).Wait(); | |
56 InitializeWebContents(); | |
57 } | |
58 | |
59 void LoginManagerTest::RegisterUser(const std::string& username) { | |
60 ListPrefUpdate users_pref(g_browser_process->local_state(), "LoggedInUsers"); | 55 ListPrefUpdate users_pref(g_browser_process->local_state(), "LoggedInUsers"); |
61 users_pref->AppendIfNotPresent(new base::StringValue(username)); | 56 users_pref->AppendIfNotPresent(new base::StringValue(username)); |
62 } | 57 } |
63 | 58 |
64 void LoginManagerTest::SetExpectedCredentials(const std::string& username, | 59 void LoginManagerTestHelper::SetExpectedCredentials( |
65 const std::string& password) { | 60 const std::string& username, const std::string& password) { |
66 login_utils().GetFakeLoginUtils()->SetExpectedCredentials(username, password); | 61 mock_login_utils_->GetFakeLoginUtils()->SetExpectedCredentials(username, |
| 62 password); |
67 } | 63 } |
68 | 64 |
69 bool LoginManagerTest::TryToLogin(const std::string& username, | 65 bool LoginManagerTestHelper::TryToLogIn(const std::string& username, |
70 const std::string& password) { | 66 const std::string& password) { |
71 if (!AddUserToSession(username, password)) | 67 if (!AddUserToSession(username, password)) |
72 return false; | 68 return false; |
73 if (const User* active_user = UserManager::Get()->GetActiveUser()) | 69 if (const User* active_user = UserManager::Get()->GetActiveUser()) |
74 return active_user->email() == username; | 70 return active_user->email() == username; |
75 return false; | 71 return false; |
76 } | 72 } |
77 | 73 |
78 bool LoginManagerTest::AddUserToSession(const std::string& username, | 74 bool LoginManagerTestHelper::AddUserToSession(const std::string& username, |
79 const std::string& password) { | 75 const std::string& password) { |
80 ExistingUserController* controller = | 76 ExistingUserController* controller = |
81 ExistingUserController::current_controller(); | 77 ExistingUserController::current_controller(); |
82 if (!controller) { | 78 if (!controller) { |
83 ADD_FAILURE(); | 79 ADD_FAILURE(); |
84 return false; | 80 return false; |
85 } | 81 } |
86 UserContext user_context(username); | 82 UserContext user_context(username); |
87 user_context.SetPassword(password); | 83 user_context.SetPassword(password); |
88 controller->Login(user_context); | 84 controller->Login(user_context); |
89 content::WindowedNotificationObserver( | 85 content::WindowedNotificationObserver( |
90 chrome::NOTIFICATION_SESSION_STARTED, | 86 chrome::NOTIFICATION_SESSION_STARTED, |
91 content::NotificationService::AllSources()).Wait(); | 87 content::NotificationService::AllSources()).Wait(); |
92 const UserList& logged_users = UserManager::Get()->GetLoggedInUsers(); | 88 const UserList& logged_users = UserManager::Get()->GetLoggedInUsers(); |
93 for (UserList::const_iterator it = logged_users.begin(); | 89 for (UserList::const_iterator it = logged_users.begin(); |
94 it != logged_users.end(); ++it) { | 90 it != logged_users.end(); ++it) { |
95 if ((*it)->email() == username) | 91 if ((*it)->email() == username) |
96 return true; | 92 return true; |
97 } | 93 } |
98 return false; | 94 return false; |
99 } | 95 } |
100 | 96 |
101 void LoginManagerTest::LoginUser(const std::string& username) { | 97 void LoginManagerTestHelper::LogInUser(const std::string& username) { |
102 SetExpectedCredentials(username, "password"); | 98 SetExpectedCredentials(username, "password"); |
103 EXPECT_TRUE(TryToLogin(username, "password")); | 99 EXPECT_TRUE(TryToLogIn(username, "password")); |
104 } | 100 } |
105 | 101 |
106 void LoginManagerTest::AddUser(const std::string& username) { | 102 void LoginManagerTestHelper::AddUser(const std::string& username) { |
107 SetExpectedCredentials(username, "password"); | 103 SetExpectedCredentials(username, "password"); |
108 EXPECT_TRUE(AddUserToSession(username, "password")); | 104 EXPECT_TRUE(AddUserToSession(username, "password")); |
109 } | 105 } |
110 | 106 |
111 void LoginManagerTest::JSExpect(const std::string& expression) { | |
112 js_checker_.ExpectTrue(expression); | |
113 } | |
114 | |
115 void LoginManagerTest::InitializeWebContents() { | |
116 LoginDisplayHost* host = LoginDisplayHostImpl::default_host(); | |
117 EXPECT_TRUE(host != NULL); | |
118 | |
119 content::WebContents* web_contents = | |
120 host->GetWebUILoginView()->GetWebContents(); | |
121 EXPECT_TRUE(web_contents != NULL); | |
122 set_web_contents(web_contents); | |
123 js_checker_.set_web_contents(web_contents); | |
124 } | |
125 | |
126 } // namespace chromeos | 107 } // namespace chromeos |
OLD | NEW |