Index: chrome/browser/sync/test/integration/signin_helper.cc |
diff --git a/chrome/browser/sync/test/integration/signin_helper.cc b/chrome/browser/sync/test/integration/signin_helper.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a3080f86b8e95772d82baac0f5f55faea549a199 |
--- /dev/null |
+++ b/chrome/browser/sync/test/integration/signin_helper.cc |
@@ -0,0 +1,166 @@ |
+// 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 "base/scoped_observer.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/browser/signin/signin_manager_factory.h" |
+#include "chrome/browser/signin/signin_promo.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 "components/signin/core/browser/signin_manager.h" |
+#include "content/public/browser/notification_service.h" |
+#include "content/public/browser/notification_types.h" |
+#include "content/public/test/browser_test_utils.h" |
+ |
+using content::MessageLoopRunner; |
+ |
+// anonymous namespace for signin with UI helper functions. |
+namespace { |
+ |
+// The SignInWaiter observes the signin manager and blocks until a |
+// GoogleSigninSucceeded or a GoogleSigninFailed notification is fired. |
+class SignInWaiter : public SigninManagerBase::Observer { |
Roger Tawa OOO till Jul 10th
2014/11/10 16:33:49
Please use the SigninTracker class to wait for sig
shadi
2014/11/11 22:08:18
Done.
|
+ public: |
+ explicit SignInWaiter(SigninManagerBase* signin_manager) |
+ : seen_(false), |
+ running_(false), |
+ signed_in_(false), |
+ scoped_observer_(this) { |
+ scoped_observer_.Add(signin_manager); |
+ } |
+ virtual ~SignInWaiter() {} |
+ |
+ // 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 GoogleSigninFailed( |
+ const GoogleServiceAuthError& error) override { |
+ VLOG(1) << "Google signin failed."; |
+ seen_ = true; |
+ if (!running_) |
+ return; |
+ message_loop_runner_->Quit(); |
+ running_ = false; |
+ } |
+ |
+ virtual void GoogleSigninSucceeded(const std::string& account_id, |
+ const std::string& username, |
+ const std::string& password) override { |
+ VLOG(1) << "Google signin succeeded for " << username; |
+ 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_; |
+ ScopedObserver<SigninManagerBase, SignInWaiter> scoped_observer_; |
+ scoped_refptr<MessageLoopRunner> message_loop_runner_; |
+}; |
+ |
+bool HtmlElementExistsInSigninFrame(content::WebContents* web_contents, |
+ const std::string& name) { |
+ bool result; |
+ EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
+ InlineLoginUI::GetAuthIframe(web_contents, GURL(), "signin-frame"), |
+ "window.domAutomationController.send(" |
+ "document.getElementById(\"" + name + "\") != null);", |
+ &result)); |
+ |
+ return result; |
+} |
+ |
+// Executes JavaScript code in the auth iframe hosted by gaia_auth extension. |
+void ExecuteScriptAndWaitForAnyPageLoad(content::WebContents* web_contents, |
+ const std::string& script) { |
+ content::WindowedNotificationObserver observer( |
+ content::NOTIFICATION_LOAD_STOP, |
+ content::Source<content::NavigationController>( |
+ &web_contents->GetController())); |
+ ASSERT_TRUE(content::ExecuteScript(InlineLoginUI::GetAuthIframe( |
+ web_contents, GURL(), "signin-frame"), script)); |
+ observer.Wait(); |
+} |
+ |
+void WaitUntilUIReady(content::WebContents* web_contents) { |
+ content::DOMMessageQueue message_queue; |
+ ASSERT_TRUE(content::ExecuteScript( |
+ web_contents, |
+ "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\""); |
+} |
+ |
+} // anonymous namespace |
+ |
+bool SignInToGAIA(Browser* browser, |
+ const std::string& username, |
+ const std::string& password) { |
Roger Tawa OOO till Jul 10th
2014/11/10 16:33:49
Should this function be called SignInWithUI?
shadi
2014/11/11 22:08:18
Done.
|
+ SigninManager* signinManager = |
Roger Tawa OOO till Jul 10th
2014/11/10 16:33:49
In c++, variables don't use camelcase. Use signin
shadi
2014/11/11 22:08:18
Done.
|
+ SigninManagerFactory::GetForProfile(browser->profile()); |
+ SignInWaiter sign_in_waiter(signinManager); |
+ |
+ GURL signinURL = signin::GetPromoURL(signin::SOURCE_START_PAGE, false); |
+ VLOG(1) << "Navigating to " << signinURL; |
Roger Tawa OOO till Jul 10th
2014/11/10 16:33:49
I think all VLOGs should be DVLOGs now.
shadi
2014/11/11 22:08:18
Done.
|
+ content::WindowedNotificationObserver observer( |
+ content::NOTIFICATION_LOAD_STOP, |
+ content::NotificationService::AllSources()); |
+ ui_test_utils::NavigateToURL(browser, signinURL); |
+ observer.Wait(); |
+ content::WebContents* web_contents = |
+ browser->tab_strip_model()->GetActiveWebContents(); |
+ WaitUntilUIReady(web_contents); |
+ VLOG(1) << "LOGIN UI READY"; |
+ // The active tab should have the "Google Accounts" login page loaded. |
+ if (!HtmlElementExistsInSigninFrame(web_contents, "Email") || |
+ !HtmlElementExistsInSigninFrame(web_contents, "Passwd")) { |
+ VLOG(1) << "Signin URL loaded but there are no email/password fields."; |
+ return false; |
+ } |
+ |
+ std::string js = |
+ "document.getElementById('Email').value = '" + username + "';" |
+ "document.getElementById('Passwd').value = '" + password + "';" |
+ "document.getElementById('signIn').click();"; |
+ ExecuteScriptAndWaitForAnyPageLoad(web_contents, js); |
+ sign_in_waiter.Wait(); |
+ return sign_in_waiter.DidSignIn(); |
+} |