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