ProfilePolicyConnectorFactory: Refactoring from Profile to BrowserContext.
[chromium-blink-merge.git] / chrome / browser / ui / webui / signin / login_ui_test_utils.cc
blob376bd4b25bdab8b62e8ed92b0c39c7a19982c7ba
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.
19 namespace {
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) {}
30 virtual ~SignInObserver() {}
32 // Returns whether a GoogleSigninSucceeded event has happened.
33 bool DidSignIn() {
34 return signed_in_;
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;
43 running_ = true;
44 message_loop_runner_ = new MessageLoopRunner;
45 message_loop_runner_->Run();
46 EXPECT_TRUE(seen_);
49 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;
58 void MergeSessionComplete(const GoogleServiceAuthError& error) override {}
60 void SigninSuccess() override {
61 DVLOG(1) << "Google signin succeeded.";
62 seen_ = true;
63 signed_in_ = true;
64 if (!running_)
65 return;
66 message_loop_runner_->Quit();
67 running_ = false;
70 private:
71 // Bool to mark an observed event as seen prior to calling Wait(), used to
72 // prevent the observer from blocking.
73 bool seen_;
74 // True is the message loop runner is running.
75 bool running_;
76 // True if a GoogleSigninSucceeded event has been observed.
77 bool signed_in_;
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');"
95 "};"
96 "if (inline.login.isAuthReady())"
97 " handler();"
98 "else"
99 " inline.login.getAuthExtHost().addEventListener('ready', handler);"));
101 std::string message;
102 do {
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) {
110 std::string js =
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(),
128 &signin_observer);
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(
138 browser,
139 signin_url,
140 NEW_FOREGROUND_TAB,
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