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 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_LOGIN_MANAGER_TEST_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_LOGIN_MANAGER_TEST_H_ |
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_LOGIN_MANAGER_TEST_H_ | 6 #define CHROME_BROWSER_CHROMEOS_LOGIN_LOGIN_MANAGER_TEST_H_ |
7 | 7 |
8 #include "chrome/browser/chromeos/login/mock_login_utils.h" | 8 #include "chrome/browser/chromeos/login/mock_login_utils.h" |
9 #include "chrome/browser/chromeos/login/test/js_checker.h" | 9 #include "chrome/browser/chromeos/login/test/js_checker.h" |
10 #include "chrome/test/base/in_process_browser_test.h" | 10 #include "chrome/test/base/in_process_browser_test.h" |
| 11 #include "chrome/browser/chromeos/login/login_display_host_impl.h" |
| 12 #include "base/prefs/scoped_user_pref_update.h" |
| 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/chrome_notification_types.h" |
| 15 #include "chrome/browser/chromeos/login/existing_user_controller.h" |
| 16 #include "chrome/browser/chromeos/login/user_manager.h" |
| 17 #include "chrome/browser/chromeos/login/webui_login_view.h" |
| 18 #include "chrome/common/chrome_switches.h" |
| 19 #include "chromeos/chromeos_switches.h" |
| 20 #include "content/public/browser/notification_service.h" |
| 21 #include "content/public/browser/web_contents.h" |
| 22 #include "content/public/test/browser_test_utils.h" |
| 23 #include "content/public/test/test_utils.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" |
11 | 25 |
12 namespace content { | 26 namespace content { |
13 class WebContents; | 27 class WebContents; |
14 } // namespace content | 28 } // namespace content |
15 | 29 |
16 namespace chromeos { | 30 namespace chromeos { |
17 | 31 |
18 // Base class for Chrome OS out-of-box/login WebUI tests. | 32 // Base class for Chrome OS out-of-box/login WebUI tests. |
19 // If no special configuration is done launches out-of-box WebUI. | 33 // If no special configuration is done launches out-of-box WebUI. |
20 // To launch login UI use PRE_* test that will register user(s) and mark | 34 // To launch login UI use PRE_* test that will register user(s) and mark |
21 // out-of-box as completed. | 35 // out-of-box as completed. |
22 // Guarantees that WebUI has been initialized by waiting for | 36 // Guarantees that WebUI has been initialized by waiting for |
23 // NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE notification. | 37 // NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE notification. |
24 class LoginManagerTest : public InProcessBrowserTest { | 38 template <typename Base> |
| 39 class LoginManagerTestProxy : public Base { |
25 public: | 40 public: |
26 explicit LoginManagerTest(bool should_launch_browser); | 41 explicit LoginManagerTestProxy(bool should_launch_browser) |
| 42 : should_launch_browser_(should_launch_browser), |
| 43 web_contents_(NULL) { |
| 44 Base::set_exit_when_last_browser_closes(false); |
| 45 } |
27 | 46 |
28 // Overriden from InProcessBrowserTest. | 47 // Overriden from InProcessBrowserTest. |
29 virtual void CleanUpOnMainThread() OVERRIDE; | 48 virtual void CleanUpOnMainThread() OVERRIDE { |
30 virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE; | 49 if (LoginDisplayHostImpl::default_host()) |
31 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE; | 50 LoginDisplayHostImpl::default_host()->Finalize(); |
32 virtual void SetUpOnMainThread() OVERRIDE; | 51 base::MessageLoop::current()->RunUntilIdle(); |
| 52 Base::CleanUpOnMainThread(); |
| 53 } |
| 54 |
| 55 virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE { |
| 56 Base::SetUpCommandLine(command_line); |
| 57 command_line->AppendSwitch(chromeos::switches::kLoginManager); |
| 58 command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests); |
| 59 command_line->AppendSwitch(::switches::kMultiProfiles); |
| 60 } |
| 61 |
| 62 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { |
| 63 Base::SetUpInProcessBrowserTestFixture(); |
| 64 mock_login_utils_ = new testing::NiceMock<MockLoginUtils>(); |
| 65 mock_login_utils_->DelegateToFake(); |
| 66 mock_login_utils_->GetFakeLoginUtils()->set_should_launch_browser( |
| 67 should_launch_browser_); |
| 68 LoginUtils::Set(mock_login_utils_); |
| 69 } |
| 70 |
| 71 virtual void SetUpOnMainThread() OVERRIDE { |
| 72 Base::SetUpOnMainThread(); |
| 73 content::WindowedNotificationObserver( |
| 74 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, |
| 75 content::NotificationService::AllSources()).Wait(); |
| 76 InitializeWebContents(); |
| 77 } |
33 | 78 |
34 // Registers user with given |username| on device. | 79 // Registers user with given |username| on device. |
35 // Should be called in PRE_* test. | 80 // Should be called in PRE_* test. |
36 // TODO(dzhioev): add ability to register users without PRE_* test. | 81 // TODO(dzhioev): add ability to register users without PRE_* test. |
37 void RegisterUser(const std::string& username); | 82 void RegisterUser(const std::string& username) { |
| 83 ListPrefUpdate users_pref(g_browser_process->local_state(), "LoggedInUsers")
; |
| 84 users_pref->AppendIfNotPresent(new base::StringValue(username)); |
| 85 } |
38 | 86 |
39 // Set expected credentials for next login attempt. | 87 // Set expected credentials for next login attempt. |
40 void SetExpectedCredentials(const std::string& username, | 88 void SetExpectedCredentials(const std::string& username, |
41 const std::string& password); | 89 const std::string& password) { |
| 90 login_utils().GetFakeLoginUtils()->SetExpectedCredentials(username, password
); |
| 91 } |
42 | 92 |
43 // Tries to login with |username| and |password|. Returns false if attempt | 93 // Tries to login with |username| and |password|. Returns false if attempt |
44 // has failed. | 94 // has failed. |
45 bool TryToLogin(const std::string& username, const std::string& password); | 95 bool TryToLogin(const std::string& username, const std::string& password) { |
| 96 if (!AddUserTosession(username, password)) |
| 97 return false; |
| 98 if (const User* active_user = UserManager::Get()->GetActiveUser()) |
| 99 return active_user->email() == username; |
| 100 return false; |
| 101 } |
46 | 102 |
47 // Tries to add user to session with |username| and |password|. Returns false | 103 // Tries to add user to session with |username| and |password|. Returns false |
48 // if attempt has failed. this function does the same as TryToLogin but | 104 // if attempt has failed. this function does the same as TryToLogin but |
49 // doesn't check that new user become active user. | 105 // doesn't check that new user become active user. |
50 bool AddUserTosession(const std::string& username, | 106 bool AddUserTosession(const std::string& username, |
51 const std::string& password); | 107 const std::string& password) { |
| 108 ExistingUserController* controller = |
| 109 ExistingUserController::current_controller(); |
| 110 if (!controller) { |
| 111 ADD_FAILURE(); |
| 112 return false; |
| 113 } |
| 114 controller->Login(UserContext(username, password, std::string())); |
| 115 content::WindowedNotificationObserver( |
| 116 chrome::NOTIFICATION_SESSION_STARTED, |
| 117 content::NotificationService::AllSources()).Wait(); |
| 118 const UserList& logged_users = UserManager::Get()->GetLoggedInUsers(); |
| 119 for (UserList::const_iterator it = logged_users.begin(); |
| 120 it != logged_users.end(); ++it) { |
| 121 if ((*it)->email() == username) |
| 122 return true; |
| 123 } |
| 124 return false; |
| 125 } |
52 | 126 |
53 // Login user with |username|. User should be registered using RegisterUser(). | 127 // Login user with |username|. User should be registered using RegisterUser(). |
54 void LoginUser(const std::string& username); | 128 void LoginUser(const std::string& username) { |
| 129 SetExpectedCredentials(username, "password"); |
| 130 EXPECT_TRUE(TryToLogin(username, "password")); |
| 131 } |
55 | 132 |
56 // Add user with |username| to session. | 133 // Add user with |username| to session. |
57 void AddUser(const std::string& username); | 134 void AddUser(const std::string& username) { |
| 135 SetExpectedCredentials(username, "password"); |
| 136 EXPECT_TRUE(AddUserTosession(username, "password")); |
| 137 } |
58 | 138 |
59 // Executes given JS |expression| in |web_contents_| and checks | 139 // Executes given JS |expression| in |web_contents_| and checks |
60 // that it is true. | 140 // that it is true. |
61 void JSExpect(const std::string& expression); | 141 void JSExpect(const std::string& expression) { |
| 142 js_checker_.ExpectTrue(expression); |
| 143 } |
62 | 144 |
63 MockLoginUtils& login_utils() { return *mock_login_utils_; } | 145 MockLoginUtils& login_utils() { return *mock_login_utils_; } |
64 | 146 |
65 content::WebContents* web_contents() { return web_contents_; } | 147 content::WebContents* web_contents() { return web_contents_; } |
66 | 148 |
67 private: | 149 private: |
68 void InitializeWebContents(); | 150 void InitializeWebContents() { |
| 151 LoginDisplayHost* host = LoginDisplayHostImpl::default_host(); |
| 152 EXPECT_TRUE(host != NULL); |
| 153 |
| 154 content::WebContents* web_contents = |
| 155 host->GetWebUILoginView()->GetWebContents(); |
| 156 EXPECT_TRUE(web_contents != NULL); |
| 157 set_web_contents(web_contents); |
| 158 js_checker_.set_web_contents(web_contents); |
| 159 } |
69 | 160 |
70 void set_web_contents(content::WebContents* web_contents) { | 161 void set_web_contents(content::WebContents* web_contents) { |
71 web_contents_ = web_contents; | 162 web_contents_ = web_contents; |
72 } | 163 } |
73 | 164 |
74 MockLoginUtils* mock_login_utils_; | 165 MockLoginUtils* mock_login_utils_; |
75 bool should_launch_browser_; | 166 bool should_launch_browser_; |
76 content::WebContents* web_contents_; | 167 content::WebContents* web_contents_; |
77 test::JSChecker js_checker_; | 168 test::JSChecker js_checker_; |
78 | 169 |
79 DISALLOW_COPY_AND_ASSIGN(LoginManagerTest); | 170 DISALLOW_COPY_AND_ASSIGN(LoginManagerTestProxy); |
80 }; | 171 }; |
81 | 172 |
| 173 typedef LoginManagerTestProxy<InProcessBrowserTest> LoginManagerTest; |
| 174 typedef LoginManagerTestProxy<WebUIBrowserTest> LoginManagerWebUITest; |
| 175 |
82 } // namespace chromeos | 176 } // namespace chromeos |
83 | 177 |
84 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_LOGIN_MANAGER_TEST_H_ | 178 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_LOGIN_MANAGER_TEST_H_ |
OLD | NEW |