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.
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"
16 using content::MessageLoopRunner
;
18 // anonymous namespace for signin with UI helper functions.
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
{
30 virtual ~SignInObserver() {}
32 // Returns whether a GoogleSigninSucceeded event has happened.
37 // Blocks and waits until the user signs in. Wait() does not block if a
38 // GoogleSigninSucceeded or a GoogleSigninFailed has already occurred.
44 message_loop_runner_
= new MessageLoopRunner
;
45 message_loop_runner_
->Run();
49 void SigninFailed(const GoogleServiceAuthError
& error
) override
{
50 DVLOG(1) << "Google signin failed.";
54 message_loop_runner_
->Quit();
58 void MergeSessionComplete(const GoogleServiceAuthError
& error
) override
{}
60 void SigninSuccess() override
{
61 DVLOG(1) << "Google signin succeeded.";
66 message_loop_runner_
->Quit();
71 // Bool to mark an observed event as seen prior to calling Wait(), used to
72 // prevent the observer from blocking.
74 // True is the message loop runner is running.
76 // True if a GoogleSigninSucceeded event has been observed.
78 scoped_refptr
<MessageLoopRunner
> message_loop_runner_
;
81 } // anonymous namespace
84 namespace login_ui_test_utils
{
86 void WaitUntilUIReady(Browser
* browser
) {
87 content::DOMMessageQueue message_queue
;
88 ASSERT_TRUE(content::ExecuteScript(
89 browser
->tab_strip_model()->GetActiveWebContents(),
90 "if (!inline.login.getAuthExtHost())"
91 " inline.login.initialize();"
92 "var handler = function() {"
93 " window.domAutomationController.setAutomationId(0);"
94 " window.domAutomationController.send('ready');"
96 "if (inline.login.isAuthReady())"
99 " inline.login.getAuthExtHost().addEventListener('ready', handler);"));
103 ASSERT_TRUE(message_queue
.WaitForMessage(&message
));
104 } while (message
!= "\"ready\"");
107 void ExecuteJsToSigninInSigninFrame(Browser
* browser
,
108 const std::string
& email
,
109 const std::string
& password
) {
111 "document.getElementById('Email').value = '" + email
+ "';"
112 "document.getElementById('Passwd').value = '" + password
+ "';"
113 "document.getElementById('signIn').click();";
115 content::WebContents
* web_contents
=
116 browser
->tab_strip_model()->GetActiveWebContents();
117 ASSERT_TRUE(content::ExecuteScript(InlineLoginUI::GetAuthIframe(
118 web_contents
, GURL(), "signin-frame"), js
));
121 bool SignInWithUI(Browser
* browser
,
122 const std::string
& username
,
123 const std::string
& password
) {
125 SignInObserver signin_observer
;
126 scoped_ptr
<SigninTracker
> tracker
=
127 SigninTrackerFactory::CreateForProfile(browser
->profile(),
130 GURL signin_url
= signin::GetPromoURL(
131 signin_metrics::SOURCE_START_PAGE
, false);
132 DVLOG(1) << "Navigating to " << signin_url
;
133 // For some tests, the window is not shown yet and this might be the first tab
134 // navigation, so GetActiveWebContents() for CURRENT_TAB is NULL. That's why
135 // we use NEW_FOREGROUND_TAB rather than the CURRENT_TAB used by default in
136 // ui_test_utils::NavigateToURL().
137 ui_test_utils::NavigateToURLWithDisposition(
141 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
143 DVLOG(1) << "Wait for login UI to be ready.";
144 WaitUntilUIReady(browser
);
145 DVLOG(1) << "Sign in user: " << username
;
146 ExecuteJsToSigninInSigninFrame(browser
, username
, password
);
147 signin_observer
.Wait();
148 return signin_observer
.DidSignIn();
151 } // namespace login_ui_test_utils