OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/signin/signin_promo.h" | 5 #include "chrome/browser/signin/signin_promo.h" |
6 #include "chrome/browser/signin/signin_tracker_factory.h" | 6 #include "chrome/browser/signin/signin_tracker_factory.h" |
7 #include "chrome/browser/ui/browser.h" | 7 #include "chrome/browser/ui/browser.h" |
8 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 8 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
9 #include "chrome/browser/ui/webui/signin/inline_login_ui.h" | 9 #include "chrome/browser/ui/webui/signin/inline_login_ui.h" |
10 #include "chrome/test/base/ui_test_utils.h" | 10 #include "chrome/test/base/ui_test_utils.h" |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
78 scoped_refptr<MessageLoopRunner> message_loop_runner_; | 78 scoped_refptr<MessageLoopRunner> message_loop_runner_; |
79 }; | 79 }; |
80 | 80 |
81 } // anonymous namespace | 81 } // anonymous namespace |
82 | 82 |
83 | 83 |
84 namespace login_ui_test_utils { | 84 namespace login_ui_test_utils { |
85 | 85 |
86 void WaitUntilUIReady(Browser* browser) { | 86 void WaitUntilUIReady(Browser* browser) { |
87 content::DOMMessageQueue message_queue; | 87 content::DOMMessageQueue message_queue; |
88 ASSERT_TRUE(content::ExecuteScript( | 88 ASSERT_TRUE(content::ExecuteScript( |
shadi
2015/04/23 18:17:11
ExecuteScript is a bad use here since it already a
| |
89 browser->tab_strip_model()->GetActiveWebContents(), | 89 browser->tab_strip_model()->GetActiveWebContents(), |
90 "if (!inline.login.getAuthExtHost())" | 90 "if (!inline.login.getAuthExtHost())" |
91 " inline.login.initialize();" | 91 " inline.login.initialize();" |
92 "var handler = function() {" | 92 "var handler = function() {" |
93 " window.domAutomationController.setAutomationId(0);" | 93 " window.domAutomationController.setAutomationId(0);" |
94 " window.domAutomationController.send('ready');" | 94 " window.domAutomationController.send('ready');" |
95 "};" | 95 "};" |
96 "if (inline.login.isAuthReady())" | 96 "if (inline.login.isAuthReady())" |
97 " handler();" | 97 " handler();" |
98 "else" | 98 "else" |
99 " inline.login.getAuthExtHost().addEventListener('ready', handler);")); | 99 " inline.login.getAuthExtHost().addEventListener('ready', handler);")); |
100 | 100 |
101 std::string message; | 101 std::string message; |
102 do { | 102 do { |
103 ASSERT_TRUE(message_queue.WaitForMessage(&message)); | 103 ASSERT_TRUE(message_queue.WaitForMessage(&message)); |
shadi
2015/04/23 18:17:11
Using ExecuteScriptAndExtractString should return
| |
104 } while (message != "\"ready\""); | 104 } while (message != "\"ready\""); |
105 } | 105 } |
106 | 106 |
107 void ExecuteJsToSigninInSigninFrame(Browser* browser, | 107 void WaitUntilElementExistsInSigninFrame(Browser* browser, |
108 const std::string& email, | 108 const std::string& element_id) { |
109 const std::string& password) { | 109 content::DOMMessageQueue message_queue; |
110 std::string js = | 110 std::string js = |
111 "function WaitForElementById(elementId) {" | |
112 " var retries = 10; /* 10 seconds. */" | |
113 " function CheckelementExists() {" | |
114 " if (document.getElementById(elementId) != null) {" | |
115 " window.domAutomationController.setAutomationId(0);" | |
116 " window.domAutomationController.send('found');" | |
117 " } else if (retries > 0) { " | |
118 " retries--;" | |
119 " window.setTimeout(CheckelementExists, 1000);" | |
120 " } else {" | |
121 " window.domAutomationController.setAutomationId(0);" | |
122 " window.domAutomationController.send('failed');" | |
123 " }" | |
124 " }" | |
125 " CheckelementExists();" | |
126 "}" | |
127 "WaitForElementById('" + element_id + "');"; | |
128 content::WebContents* web_contents = | |
129 browser->tab_strip_model()->GetActiveWebContents(); | |
130 ASSERT_TRUE(content::ExecuteScript(InlineLoginUI::GetAuthFrame( | |
131 web_contents, GURL(), "signin-frame"), js)); | |
132 | |
133 std::string message; | |
134 do { | |
135 ASSERT_TRUE(message_queue.WaitForMessage(&message)); | |
136 if (message == "\"failed\"") | |
137 LOG(ERROR) << "Failed to find element with id " << element_id; | |
138 } while (message != "\"found\""); | |
139 } | |
140 | |
141 bool ElementExistsInSigninFrame(Browser* browser, | |
142 const std::string& element_id) { | |
143 content::DOMMessageQueue message_queue; | |
144 std::string js = | |
145 "window.domAutomationController.setAutomationId(0);" | |
146 "if (document.getElementById('" + element_id + "') != null) {" | |
147 " window.domAutomationController.send('found');" | |
148 "} else { " | |
149 " window.domAutomationController.send('not found');" | |
150 "}"; | |
151 content::WebContents* web_contents = | |
152 browser->tab_strip_model()->GetActiveWebContents(); | |
153 EXPECT_TRUE(content::ExecuteScript(InlineLoginUI::GetAuthFrame( | |
154 web_contents, GURL(), "signin-frame"), js)); | |
155 | |
156 std::string message; | |
157 EXPECT_TRUE(message_queue.WaitForMessage(&message)); | |
158 return message == "\"found\""; | |
159 } | |
160 | |
161 void SigninInNewGaiaFlow(Browser* browser, | |
162 const std::string& email, | |
163 const std::string& password) { | |
164 std::string js = "document.getElementById('Email').value = '" + email + "';" | |
165 "document.getElementById('next').click();"; | |
166 | |
167 content::WebContents* web_contents = | |
168 browser->tab_strip_model()->GetActiveWebContents(); | |
169 ASSERT_TRUE(content::ExecuteScript(InlineLoginUI::GetAuthFrame( | |
170 web_contents, GURL(), "signin-frame"), js)); | |
171 | |
172 WaitUntilElementExistsInSigninFrame(browser, "Passwd"); | |
173 js = "document.getElementById('Passwd').value = '" + password + "';" | |
174 "document.getElementById('signIn').click();"; | |
175 | |
176 ASSERT_TRUE(content::ExecuteScript(InlineLoginUI::GetAuthFrame( | |
177 web_contents, GURL(), "signin-frame"), js)); | |
178 } | |
179 | |
180 void SigninInOldGaiaFlow(Browser* browser, | |
181 const std::string& email, | |
182 const std::string& password) { | |
183 std::string js = | |
111 "document.getElementById('Email').value = '" + email + "';" | 184 "document.getElementById('Email').value = '" + email + "';" |
112 "document.getElementById('Passwd').value = '" + password + "';" | 185 "document.getElementById('Passwd').value = '" + password + "';" |
113 "document.getElementById('signIn').click();"; | 186 "document.getElementById('signIn').click();"; |
114 | 187 |
115 content::WebContents* web_contents = | 188 content::WebContents* web_contents = |
116 browser->tab_strip_model()->GetActiveWebContents(); | 189 browser->tab_strip_model()->GetActiveWebContents(); |
117 ASSERT_TRUE(content::ExecuteScript(InlineLoginUI::GetAuthFrame( | 190 ASSERT_TRUE(content::ExecuteScript(InlineLoginUI::GetAuthFrame( |
118 web_contents, GURL(), "signin-frame"), js)); | 191 web_contents, GURL(), "signin-frame"), js)); |
119 } | 192 } |
120 | 193 |
194 void ExecuteJsToSigninInSigninFrame(Browser* browser, | |
195 const std::string& email, | |
196 const std::string& password) { | |
197 WaitUntilElementExistsInSigninFrame(browser, "Email"); | |
198 if (ElementExistsInSigninFrame(browser, "next")) | |
199 SigninInNewGaiaFlow(browser, email, password); | |
200 else | |
201 SigninInOldGaiaFlow(browser, email, password); | |
202 } | |
203 | |
121 bool SignInWithUI(Browser* browser, | 204 bool SignInWithUI(Browser* browser, |
122 const std::string& username, | 205 const std::string& username, |
123 const std::string& password) { | 206 const std::string& password) { |
124 | 207 |
125 SignInObserver signin_observer; | 208 SignInObserver signin_observer; |
126 scoped_ptr<SigninTracker> tracker = | 209 scoped_ptr<SigninTracker> tracker = |
127 SigninTrackerFactory::CreateForProfile(browser->profile(), | 210 SigninTrackerFactory::CreateForProfile(browser->profile(), |
128 &signin_observer); | 211 &signin_observer); |
129 | 212 |
130 GURL signin_url = signin::GetPromoURL( | 213 GURL signin_url = signin::GetPromoURL( |
(...skipping 11 matching lines...) Expand all Loading... | |
142 | 225 |
143 DVLOG(1) << "Wait for login UI to be ready."; | 226 DVLOG(1) << "Wait for login UI to be ready."; |
144 WaitUntilUIReady(browser); | 227 WaitUntilUIReady(browser); |
145 DVLOG(1) << "Sign in user: " << username; | 228 DVLOG(1) << "Sign in user: " << username; |
146 ExecuteJsToSigninInSigninFrame(browser, username, password); | 229 ExecuteJsToSigninInSigninFrame(browser, username, password); |
147 signin_observer.Wait(); | 230 signin_observer.Wait(); |
148 return signin_observer.DidSignIn(); | 231 return signin_observer.DidSignIn(); |
149 } | 232 } |
150 | 233 |
151 } // namespace login_ui_test_utils | 234 } // namespace login_ui_test_utils |
OLD | NEW |