OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/signin/signin_promo.h" |
| 6 #include "chrome/browser/signin/signin_tracker_factory.h" |
| 7 #include "chrome/browser/ui/browser.h" |
| 8 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 9 #include "chrome/browser/ui/webui/signin/inline_login_ui.h" |
| 10 #include "chrome/test/base/ui_test_utils.h" |
| 11 #include "content/public/browser/notification_service.h" |
| 12 #include "content/public/browser/notification_types.h" |
| 13 #include "content/public/browser/web_contents.h" |
| 14 #include "content/public/test/browser_test_utils.h" |
| 15 |
| 16 using content::MessageLoopRunner; |
| 17 |
| 18 // anonymous namespace for signin with UI helper functions. |
| 19 namespace { |
| 20 |
| 21 // The SignInObserver observes the signin manager and blocks until a |
| 22 // GoogleSigninSucceeded or a GoogleSigninFailed notification is fired. |
| 23 class SignInObserver : public SigninTracker::Observer { |
| 24 public: |
| 25 SignInObserver() |
| 26 : seen_(false), |
| 27 running_(false), |
| 28 signed_in_(false) {} |
| 29 |
| 30 virtual ~SignInObserver() {} |
| 31 |
| 32 // Returns whether a GoogleSigninSucceeded event has happened. |
| 33 bool DidSignIn() { |
| 34 return signed_in_; |
| 35 } |
| 36 |
| 37 // Blocks and waits until the user signs in. Wait() does not block if a |
| 38 // GoogleSigninSucceeded or a GoogleSigninFailed has already occurred. |
| 39 void Wait() { |
| 40 if (seen_) |
| 41 return; |
| 42 |
| 43 running_ = true; |
| 44 message_loop_runner_ = new MessageLoopRunner; |
| 45 message_loop_runner_->Run(); |
| 46 EXPECT_TRUE(seen_); |
| 47 } |
| 48 |
| 49 virtual void SigninFailed(const GoogleServiceAuthError& error) override { |
| 50 DVLOG(1) << "Google signin failed."; |
| 51 seen_ = true; |
| 52 if (!running_) |
| 53 return; |
| 54 message_loop_runner_->Quit(); |
| 55 running_ = false; |
| 56 } |
| 57 |
| 58 virtual void MergeSessionComplete( |
| 59 const GoogleServiceAuthError& error) override {} |
| 60 |
| 61 virtual void SigninSuccess() override { |
| 62 DVLOG(1) << "Google signin succeeded."; |
| 63 seen_ = true; |
| 64 signed_in_ = true; |
| 65 if (!running_) |
| 66 return; |
| 67 message_loop_runner_->Quit(); |
| 68 running_ = false; |
| 69 } |
| 70 |
| 71 private: |
| 72 // Bool to mark an observed event as seen prior to calling Wait(), used to |
| 73 // prevent the observer from blocking. |
| 74 bool seen_; |
| 75 // True is the message loop runner is running. |
| 76 bool running_; |
| 77 // True if a GoogleSigninSucceeded event has been observed. |
| 78 bool signed_in_; |
| 79 scoped_refptr<MessageLoopRunner> message_loop_runner_; |
| 80 }; |
| 81 |
| 82 } // anonymous namespace |
| 83 |
| 84 |
| 85 namespace login_ui_test_utils { |
| 86 |
| 87 void WaitUntilUIReady(Browser* browser) { |
| 88 content::DOMMessageQueue message_queue; |
| 89 ASSERT_TRUE(content::ExecuteScript( |
| 90 browser->tab_strip_model()->GetActiveWebContents(), |
| 91 "if (!inline.login.getAuthExtHost())" |
| 92 " inline.login.initialize();" |
| 93 "var handler = function() {" |
| 94 " window.domAutomationController.setAutomationId(0);" |
| 95 " window.domAutomationController.send('ready');" |
| 96 "};" |
| 97 "if (inline.login.isAuthReady())" |
| 98 " handler();" |
| 99 "else" |
| 100 " inline.login.getAuthExtHost().addEventListener('ready', handler);")); |
| 101 |
| 102 std::string message; |
| 103 do { |
| 104 ASSERT_TRUE(message_queue.WaitForMessage(&message)); |
| 105 } while (message != "\"ready\""); |
| 106 } |
| 107 |
| 108 void ExecuteJsToSigninInSigninFrame(Browser* browser, |
| 109 const std::string& email, |
| 110 const std::string& password) { |
| 111 std::string js = |
| 112 "document.getElementById('Email').value = '" + email + "';" |
| 113 "document.getElementById('Passwd').value = '" + password + "';" |
| 114 "document.getElementById('signIn').click();"; |
| 115 |
| 116 content::WebContents* web_contents = |
| 117 browser->tab_strip_model()->GetActiveWebContents(); |
| 118 ASSERT_TRUE(content::ExecuteScript(InlineLoginUI::GetAuthIframe( |
| 119 web_contents, GURL(), "signin-frame"), js)); |
| 120 } |
| 121 |
| 122 bool SignInWithUI(Browser* browser, |
| 123 const std::string& username, |
| 124 const std::string& password) { |
| 125 |
| 126 SignInObserver signin_observer; |
| 127 scoped_ptr<SigninTracker> tracker = |
| 128 SigninTrackerFactory::CreateForProfile(browser->profile(), |
| 129 &signin_observer); |
| 130 |
| 131 GURL signin_url = signin::GetPromoURL(signin::SOURCE_START_PAGE, false); |
| 132 DVLOG(1) << "Navigating to " << signin_url; |
| 133 content::WindowedNotificationObserver observer( |
| 134 content::NOTIFICATION_LOAD_STOP, |
| 135 content::NotificationService::AllSources()); |
| 136 ui_test_utils::NavigateToURL(browser, signin_url); |
| 137 observer.Wait(); |
| 138 // Wait until the signin page is ready. |
| 139 WaitUntilUIReady(browser); |
| 140 DVLOG(1) << "Sign in user: " << username; |
| 141 ExecuteJsToSigninInSigninFrame(browser, username, password); |
| 142 signin_observer.Wait(); |
| 143 return signin_observer.DidSignIn(); |
| 144 } |
| 145 |
| 146 } // namespace login_ui_test_utils |
OLD | NEW |