Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(423)

Unified Diff: chrome/browser/sync/test/integration/signin_test_utils.cc

Issue 621153002: A Sign in to GAIA test utility. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sign in with chrome://chrome-signin Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/sync/test/integration/signin_test_utils.cc
diff --git a/chrome/browser/sync/test/integration/signin_test_utils.cc b/chrome/browser/sync/test/integration/signin_test_utils.cc
new file mode 100644
index 0000000000000000000000000000000000000000..3b57ace93446c97245abeef6d4ae4dad5258e6e6
--- /dev/null
+++ b/chrome/browser/sync/test/integration/signin_test_utils.cc
@@ -0,0 +1,155 @@
+// 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"
Nicolas Zea 2014/11/07 23:55:48 You should probably include <string> here, since y
shadi 2014/11/08 01:37:57 I added it in .h file.
+#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;
+
+class SignInWaiter : public SigninManagerBase::Observer {
pval...(no longer on Chromium) 2014/11/07 23:54:14 I believe this class (and all other util method fo
Nicolas Zea 2014/11/07 23:55:48 nit: put all this helper logic anonymous namespace
shadi 2014/11/08 01:37:57 Done.
shadi 2014/11/08 01:37:57 Done.
+ public:
+ explicit SignInWaiter(SigninManagerBase* signin_manager)
+ : seen_(false),
+ running_(false),
+ signed_in_(false),
+ scoped_observer_(this) {
+ scoped_observer_.Add(signin_manager);
+ }
+ virtual ~SignInWaiter() {}
+
+ bool DidSignIn() {
Nicolas Zea 2014/11/07 23:55:48 Comments for these methods?
shadi 2014/11/08 01:37:56 Done.
+ return signed_in_;
+ }
+
+ 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 seen_;
Nicolas Zea 2014/11/07 23:55:48 comments for these members?
shadi 2014/11/08 01:37:57 Done.
+ bool running_;
+ bool signed_in_;
+ ScopedObserver<SigninManagerBase, SignInWaiter> scoped_observer_;
+ scoped_refptr<MessageLoopRunner> message_loop_runner_;
+};
+
+// static
Nicolas Zea 2014/11/07 23:55:48 This isn't actually static (here and below)
shadi 2014/11/08 01:37:57 Done.
+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;
+}
+
+// static
+// 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\"");
+}
+
+// static
+bool SignInToGAIA(Browser* browser,
+ const std::string& username,
+ const std::string& password) {
+ SigninManager* signinManager =
+ SigninManagerFactory::GetForProfile(browser->profile());
+ SignInWaiter sign_in_waiter(signinManager);
+
+ GURL signinURL = signin::GetPromoURL(signin::SOURCE_START_PAGE, false);
+ VLOG(1) << "Navigating to " << signinURL;
+ 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();
+}

Powered by Google App Engine
This is Rietveld 408576698