Index: chrome/browser/chromeos/login/login_manager_test.h |
diff --git a/chrome/browser/chromeos/login/login_manager_test.h b/chrome/browser/chromeos/login/login_manager_test.h |
index b05db4860117f540bd8352dd53724d9c9200b554..b44852569334324b1ce3d0d0b456cca44e104fb7 100644 |
--- a/chrome/browser/chromeos/login/login_manager_test.h |
+++ b/chrome/browser/chromeos/login/login_manager_test.h |
@@ -8,6 +8,20 @@ |
#include "chrome/browser/chromeos/login/mock_login_utils.h" |
#include "chrome/browser/chromeos/login/test/js_checker.h" |
#include "chrome/test/base/in_process_browser_test.h" |
+#include "chrome/browser/chromeos/login/login_display_host_impl.h" |
+#include "base/prefs/scoped_user_pref_update.h" |
+#include "chrome/browser/browser_process.h" |
+#include "chrome/browser/chrome_notification_types.h" |
+#include "chrome/browser/chromeos/login/existing_user_controller.h" |
+#include "chrome/browser/chromeos/login/user_manager.h" |
+#include "chrome/browser/chromeos/login/webui_login_view.h" |
+#include "chrome/common/chrome_switches.h" |
+#include "chromeos/chromeos_switches.h" |
+#include "content/public/browser/notification_service.h" |
+#include "content/public/browser/web_contents.h" |
+#include "content/public/test/browser_test_utils.h" |
+#include "content/public/test/test_utils.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
namespace content { |
class WebContents; |
@@ -21,51 +35,128 @@ namespace chromeos { |
// out-of-box as completed. |
// Guarantees that WebUI has been initialized by waiting for |
// NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE notification. |
-class LoginManagerTest : public InProcessBrowserTest { |
+template <typename Base> |
+class LoginManagerTestProxy : public Base { |
public: |
- explicit LoginManagerTest(bool should_launch_browser); |
+ explicit LoginManagerTestProxy(bool should_launch_browser) |
+ : should_launch_browser_(should_launch_browser), |
+ web_contents_(NULL) { |
+ Base::set_exit_when_last_browser_closes(false); |
+ } |
// Overriden from InProcessBrowserTest. |
- virtual void CleanUpOnMainThread() OVERRIDE; |
- virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE; |
- virtual void SetUpInProcessBrowserTestFixture() OVERRIDE; |
- virtual void SetUpOnMainThread() OVERRIDE; |
+ virtual void CleanUpOnMainThread() OVERRIDE { |
+ if (LoginDisplayHostImpl::default_host()) |
+ LoginDisplayHostImpl::default_host()->Finalize(); |
+ base::MessageLoop::current()->RunUntilIdle(); |
+ Base::CleanUpOnMainThread(); |
+ } |
+ |
+ virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE { |
+ Base::SetUpCommandLine(command_line); |
+ command_line->AppendSwitch(chromeos::switches::kLoginManager); |
+ command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests); |
+ command_line->AppendSwitch(::switches::kMultiProfiles); |
+ } |
+ |
+ virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { |
+ Base::SetUpInProcessBrowserTestFixture(); |
+ mock_login_utils_ = new testing::NiceMock<MockLoginUtils>(); |
+ mock_login_utils_->DelegateToFake(); |
+ mock_login_utils_->GetFakeLoginUtils()->set_should_launch_browser( |
+ should_launch_browser_); |
+ LoginUtils::Set(mock_login_utils_); |
+ } |
+ |
+ virtual void SetUpOnMainThread() OVERRIDE { |
+ Base::SetUpOnMainThread(); |
+ content::WindowedNotificationObserver( |
+ chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, |
+ content::NotificationService::AllSources()).Wait(); |
+ InitializeWebContents(); |
+ } |
// Registers user with given |username| on device. |
// Should be called in PRE_* test. |
// TODO(dzhioev): add ability to register users without PRE_* test. |
- void RegisterUser(const std::string& username); |
+ void RegisterUser(const std::string& username) { |
+ ListPrefUpdate users_pref(g_browser_process->local_state(), "LoggedInUsers"); |
+ users_pref->AppendIfNotPresent(new base::StringValue(username)); |
+ } |
// Set expected credentials for next login attempt. |
void SetExpectedCredentials(const std::string& username, |
- const std::string& password); |
+ const std::string& password) { |
+ login_utils().GetFakeLoginUtils()->SetExpectedCredentials(username, password); |
+ } |
// Tries to login with |username| and |password|. Returns false if attempt |
// has failed. |
- bool TryToLogin(const std::string& username, const std::string& password); |
+ bool TryToLogin(const std::string& username, const std::string& password) { |
+ if (!AddUserTosession(username, password)) |
+ return false; |
+ if (const User* active_user = UserManager::Get()->GetActiveUser()) |
+ return active_user->email() == username; |
+ return false; |
+ } |
// Tries to add user to session with |username| and |password|. Returns false |
// if attempt has failed. this function does the same as TryToLogin but |
// doesn't check that new user become active user. |
bool AddUserTosession(const std::string& username, |
- const std::string& password); |
+ const std::string& password) { |
+ ExistingUserController* controller = |
+ ExistingUserController::current_controller(); |
+ if (!controller) { |
+ ADD_FAILURE(); |
+ return false; |
+ } |
+ controller->Login(UserContext(username, password, std::string())); |
+ content::WindowedNotificationObserver( |
+ chrome::NOTIFICATION_SESSION_STARTED, |
+ content::NotificationService::AllSources()).Wait(); |
+ const UserList& logged_users = UserManager::Get()->GetLoggedInUsers(); |
+ for (UserList::const_iterator it = logged_users.begin(); |
+ it != logged_users.end(); ++it) { |
+ if ((*it)->email() == username) |
+ return true; |
+ } |
+ return false; |
+ } |
// Login user with |username|. User should be registered using RegisterUser(). |
- void LoginUser(const std::string& username); |
+ void LoginUser(const std::string& username) { |
+ SetExpectedCredentials(username, "password"); |
+ EXPECT_TRUE(TryToLogin(username, "password")); |
+ } |
// Add user with |username| to session. |
- void AddUser(const std::string& username); |
+ void AddUser(const std::string& username) { |
+ SetExpectedCredentials(username, "password"); |
+ EXPECT_TRUE(AddUserTosession(username, "password")); |
+ } |
// Executes given JS |expression| in |web_contents_| and checks |
// that it is true. |
- void JSExpect(const std::string& expression); |
+ void JSExpect(const std::string& expression) { |
+ js_checker_.ExpectTrue(expression); |
+ } |
MockLoginUtils& login_utils() { return *mock_login_utils_; } |
content::WebContents* web_contents() { return web_contents_; } |
private: |
- void InitializeWebContents(); |
+ void InitializeWebContents() { |
+ LoginDisplayHost* host = LoginDisplayHostImpl::default_host(); |
+ EXPECT_TRUE(host != NULL); |
+ |
+ content::WebContents* web_contents = |
+ host->GetWebUILoginView()->GetWebContents(); |
+ EXPECT_TRUE(web_contents != NULL); |
+ set_web_contents(web_contents); |
+ js_checker_.set_web_contents(web_contents); |
+ } |
void set_web_contents(content::WebContents* web_contents) { |
web_contents_ = web_contents; |
@@ -76,9 +167,12 @@ class LoginManagerTest : public InProcessBrowserTest { |
content::WebContents* web_contents_; |
test::JSChecker js_checker_; |
- DISALLOW_COPY_AND_ASSIGN(LoginManagerTest); |
+ DISALLOW_COPY_AND_ASSIGN(LoginManagerTestProxy); |
}; |
+typedef LoginManagerTestProxy<InProcessBrowserTest> LoginManagerTest; |
+typedef LoginManagerTestProxy<WebUIBrowserTest> LoginManagerWebUITest; |
+ |
} // namespace chromeos |
#endif // CHROME_BROWSER_CHROMEOS_LOGIN_LOGIN_MANAGER_TEST_H_ |