Chromium Code Reviews| Index: chrome/browser/ui/webui/signin/login_ui_test_utils.cc |
| diff --git a/chrome/browser/ui/webui/signin/login_ui_test_utils.cc b/chrome/browser/ui/webui/signin/login_ui_test_utils.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a663271fcacbc59430ef4bfb7480ef1bd1027933 |
| --- /dev/null |
| +++ b/chrome/browser/ui/webui/signin/login_ui_test_utils.cc |
| @@ -0,0 +1,146 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/signin/signin_promo.h" |
| +#include "chrome/browser/signin/signin_tracker_factory.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/tabs/tab_strip_model.h" |
| +#include "chrome/browser/ui/webui/signin/inline_login_ui.h" |
| +#include "chrome/test/base/ui_test_utils.h" |
| +#include "content/public/browser/notification_service.h" |
| +#include "content/public/browser/notification_types.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "content/public/test/browser_test_utils.h" |
| + |
| +using content::MessageLoopRunner; |
| + |
| +// anonymous namespace for signin with UI helper functions. |
| +namespace { |
| + |
| +// The SignInObserver observes the signin manager and blocks until a |
| +// GoogleSigninSucceeded or a GoogleSigninFailed notification is fired. |
| +class SignInObserver : public SigninTracker::Observer { |
| + public: |
| + SignInObserver() |
| + : seen_(false), |
| + running_(false), |
| + signed_in_(false) {} |
| + |
| + virtual ~SignInObserver() {} |
| + |
| + // Returns whether a GoogleSigninSucceeded event has happened. |
| + bool DidSignIn() { |
| + return signed_in_; |
| + } |
| + |
| + // Blocks and waits until the user signs in. Wait() does not block if a |
| + // GoogleSigninSucceeded or a GoogleSigninFailed has already occurred. |
| + void Wait() { |
| + if (seen_) |
| + return; |
| + |
| + running_ = true; |
| + message_loop_runner_ = new MessageLoopRunner; |
| + message_loop_runner_->Run(); |
| + EXPECT_TRUE(seen_); |
| + } |
| + |
| + virtual void SigninFailed(const GoogleServiceAuthError& error) override { |
| + DVLOG(1) << "Google signin failed."; |
| + seen_ = true; |
| + if (!running_) |
| + return; |
| + message_loop_runner_->Quit(); |
| + running_ = false; |
| + } |
| + |
| + virtual void MergeSessionComplete( |
| + const GoogleServiceAuthError& error) override {} |
| + |
| + virtual void SigninSuccess() override { |
| + DVLOG(1) << "Google signin succeeded."; |
| + seen_ = true; |
| + signed_in_ = true; |
| + if (!running_) |
| + return; |
| + message_loop_runner_->Quit(); |
| + running_ = false; |
| + } |
| + |
| + private: |
| + // Bool to mark an observed event as seen prior to calling Wait(), used to |
| + // prevent the observer from blocking. |
| + bool seen_; |
| + // True is the message loop runner is running. |
| + bool running_; |
| + // True if a GoogleSigninSucceeded event has been observed. |
| + bool signed_in_; |
| + scoped_refptr<MessageLoopRunner> message_loop_runner_; |
| +}; |
| + |
| +} // anonymous namespace |
| + |
| + |
| +namespace login_ui_test_utils { |
| + |
| +void WaitUntilUIReady(Browser* browser) { |
| + content::DOMMessageQueue message_queue; |
| + ASSERT_TRUE(content::ExecuteScript( |
| + browser->tab_strip_model()->GetActiveWebContents(), |
| + "if (!inline.login.getAuthExtHost())" |
| + " inline.login.initialize();" |
| + "var handler = function() {" |
| + " window.domAutomationController.setAutomationId(0);" |
| + " window.domAutomationController.send('ready');" |
| + "};" |
| + "if (inline.login.isAuthReady())" |
| + " handler();" |
| + "else" |
| + " inline.login.getAuthExtHost().addEventListener('ready', handler);")); |
| + |
| + std::string message; |
| + do { |
| + ASSERT_TRUE(message_queue.WaitForMessage(&message)); |
| + } while (message != "\"ready\""); |
| +} |
| + |
| +void ExecuteJsToSinginInSigninFrame(Browser* browser, |
| + const std::string& email, |
| + const std::string& password) { |
|
Roger Tawa OOO till Jul 10th
2014/11/13 15:12:59
typo: Singin --> Signin
shadi
2014/11/14 00:17:37
Done.
|
| + std::string js = |
| + "document.getElementById('Email').value = '" + email + "';" |
| + "document.getElementById('Passwd').value = '" + password + "';" |
| + "document.getElementById('signIn').click();"; |
|
Roger Tawa OOO till Jul 10th
2014/11/13 15:12:59
Indent 3 lines above 3 more spaces.
shadi
2014/11/14 00:17:37
Done.
|
| + |
| + content::WebContents* web_contents = |
| + browser->tab_strip_model()->GetActiveWebContents(); |
| + ASSERT_TRUE(content::ExecuteScript(InlineLoginUI::GetAuthIframe( |
| + web_contents, GURL(), "signin-frame"), js)); |
| +} |
| + |
| +bool SignInWithUI(Browser* browser, |
| + const std::string& username, |
| + const std::string& password) { |
| + |
| + SignInObserver signin_observer; |
| + scoped_ptr<SigninTracker> tracker = |
| + SigninTrackerFactory::CreateForProfile(browser->profile(), |
| + &signin_observer); |
| + |
| + GURL signin_URL = signin::GetPromoURL(signin::SOURCE_START_PAGE, false); |
|
Roger Tawa OOO till Jul 10th
2014/11/13 15:12:59
use lowercase signin_url
shadi
2014/11/14 00:17:37
Done.
|
| + DVLOG(1) << "Navigating to " << signin_URL; |
| + content::WindowedNotificationObserver observer( |
| + content::NOTIFICATION_LOAD_STOP, |
| + content::NotificationService::AllSources()); |
| + ui_test_utils::NavigateToURL(browser, signin_URL); |
| + observer.Wait(); |
| + // Wait until the signin page is ready. |
| + WaitUntilUIReady(browser); |
| + DVLOG(1) << "Sign in user: " << username; |
| + ExecuteJsToSinginInSigninFrame(browser, username, password); |
| + signin_observer.Wait(); |
| + return signin_observer.DidSignIn(); |
| +} |
| + |
| +} // namespace login_ui_test_utils |