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 "base/scoped_observer.h" | |
6 #include "chrome/browser/profiles/profile.h" | |
7 #include "chrome/browser/signin/signin_manager_factory.h" | |
8 #include "chrome/browser/signin/signin_promo.h" | |
9 #include "chrome/browser/ui/browser.h" | |
10 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
11 #include "chrome/browser/ui/webui/signin/inline_login_ui.h" | |
12 #include "chrome/test/base/ui_test_utils.h" | |
13 #include "components/signin/core/browser/signin_manager.h" | |
14 #include "content/public/browser/notification_service.h" | |
15 #include "content/public/browser/notification_types.h" | |
16 #include "content/public/test/browser_test_utils.h" | |
17 | |
18 using content::MessageLoopRunner; | |
19 | |
20 // anonymous namespace for signin with UI helper functions. | |
21 namespace { | |
22 | |
23 // The SignInWaiter observes the signin manager and blocks until a | |
24 // GoogleSigninSucceeded or a GoogleSigninFailed notification is fired. | |
25 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.
| |
26 public: | |
27 explicit SignInWaiter(SigninManagerBase* signin_manager) | |
28 : seen_(false), | |
29 running_(false), | |
30 signed_in_(false), | |
31 scoped_observer_(this) { | |
32 scoped_observer_.Add(signin_manager); | |
33 } | |
34 virtual ~SignInWaiter() {} | |
35 | |
36 // Returns whether a GoogleSigninSucceeded event has happened. | |
37 bool DidSignIn() { | |
38 return signed_in_; | |
39 } | |
40 | |
41 // Blocks and waits until the user signs in. Wait() does not block if a | |
42 // GoogleSigninSucceeded or a GoogleSigninFailed has already occurred. | |
43 void Wait() { | |
44 if (seen_) | |
45 return; | |
46 | |
47 running_ = true; | |
48 message_loop_runner_ = new MessageLoopRunner; | |
49 message_loop_runner_->Run(); | |
50 EXPECT_TRUE(seen_); | |
51 } | |
52 | |
53 virtual void GoogleSigninFailed( | |
54 const GoogleServiceAuthError& error) override { | |
55 VLOG(1) << "Google signin failed."; | |
56 seen_ = true; | |
57 if (!running_) | |
58 return; | |
59 message_loop_runner_->Quit(); | |
60 running_ = false; | |
61 } | |
62 | |
63 virtual void GoogleSigninSucceeded(const std::string& account_id, | |
64 const std::string& username, | |
65 const std::string& password) override { | |
66 VLOG(1) << "Google signin succeeded for " << username; | |
67 seen_ = true; | |
68 signed_in_ = true; | |
69 if (!running_) | |
70 return; | |
71 message_loop_runner_->Quit(); | |
72 running_ = false; | |
73 } | |
74 | |
75 private: | |
76 // Bool to mark an observed event as seen prior to calling Wait(), used to | |
77 // prevent the observer from blocking. | |
78 bool seen_; | |
79 // True is the message loop runner is running. | |
80 bool running_; | |
81 // True if a GoogleSigninSucceeded event has been observed. | |
82 bool signed_in_; | |
83 ScopedObserver<SigninManagerBase, SignInWaiter> scoped_observer_; | |
84 scoped_refptr<MessageLoopRunner> message_loop_runner_; | |
85 }; | |
86 | |
87 bool HtmlElementExistsInSigninFrame(content::WebContents* web_contents, | |
88 const std::string& name) { | |
89 bool result; | |
90 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( | |
91 InlineLoginUI::GetAuthIframe(web_contents, GURL(), "signin-frame"), | |
92 "window.domAutomationController.send(" | |
93 "document.getElementById(\"" + name + "\") != null);", | |
94 &result)); | |
95 | |
96 return result; | |
97 } | |
98 | |
99 // Executes JavaScript code in the auth iframe hosted by gaia_auth extension. | |
100 void ExecuteScriptAndWaitForAnyPageLoad(content::WebContents* web_contents, | |
101 const std::string& script) { | |
102 content::WindowedNotificationObserver observer( | |
103 content::NOTIFICATION_LOAD_STOP, | |
104 content::Source<content::NavigationController>( | |
105 &web_contents->GetController())); | |
106 ASSERT_TRUE(content::ExecuteScript(InlineLoginUI::GetAuthIframe( | |
107 web_contents, GURL(), "signin-frame"), script)); | |
108 observer.Wait(); | |
109 } | |
110 | |
111 void WaitUntilUIReady(content::WebContents* web_contents) { | |
112 content::DOMMessageQueue message_queue; | |
113 ASSERT_TRUE(content::ExecuteScript( | |
114 web_contents, | |
115 "if (!inline.login.getAuthExtHost())" | |
116 " inline.login.initialize();" | |
117 "var handler = function() {" | |
118 " window.domAutomationController.setAutomationId(0);" | |
119 " window.domAutomationController.send('ready');" | |
120 "};" | |
121 "if (inline.login.isAuthReady())" | |
122 " handler();" | |
123 "else" | |
124 " inline.login.getAuthExtHost().addEventListener('ready', handler);")); | |
125 | |
126 std::string message; | |
127 do { | |
128 ASSERT_TRUE(message_queue.WaitForMessage(&message)); | |
129 } while (message != "\"ready\""); | |
130 } | |
131 | |
132 } // anonymous namespace | |
133 | |
134 bool SignInToGAIA(Browser* browser, | |
135 const std::string& username, | |
136 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.
| |
137 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.
| |
138 SigninManagerFactory::GetForProfile(browser->profile()); | |
139 SignInWaiter sign_in_waiter(signinManager); | |
140 | |
141 GURL signinURL = signin::GetPromoURL(signin::SOURCE_START_PAGE, false); | |
142 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.
| |
143 content::WindowedNotificationObserver observer( | |
144 content::NOTIFICATION_LOAD_STOP, | |
145 content::NotificationService::AllSources()); | |
146 ui_test_utils::NavigateToURL(browser, signinURL); | |
147 observer.Wait(); | |
148 content::WebContents* web_contents = | |
149 browser->tab_strip_model()->GetActiveWebContents(); | |
150 WaitUntilUIReady(web_contents); | |
151 VLOG(1) << "LOGIN UI READY"; | |
152 // The active tab should have the "Google Accounts" login page loaded. | |
153 if (!HtmlElementExistsInSigninFrame(web_contents, "Email") || | |
154 !HtmlElementExistsInSigninFrame(web_contents, "Passwd")) { | |
155 VLOG(1) << "Signin URL loaded but there are no email/password fields."; | |
156 return false; | |
157 } | |
158 | |
159 std::string js = | |
160 "document.getElementById('Email').value = '" + username + "';" | |
161 "document.getElementById('Passwd').value = '" + password + "';" | |
162 "document.getElementById('signIn').click();"; | |
163 ExecuteScriptAndWaitForAnyPageLoad(web_contents, js); | |
164 sign_in_waiter.Wait(); | |
165 return sign_in_waiter.DidSignIn(); | |
166 } | |
OLD | NEW |