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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 bool signed_in_; | 77 bool signed_in_; |
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 std::string message; |
88 ASSERT_TRUE(content::ExecuteScript( | 88 ASSERT_TRUE(content::ExecuteScriptAndExtractString( |
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);" | |
94 " window.domAutomationController.send('ready');" | 93 " window.domAutomationController.send('ready');" |
95 "};" | 94 "};" |
96 "if (inline.login.isAuthReady())" | 95 "if (inline.login.isAuthReady())" |
97 " handler();" | 96 " handler();" |
98 "else" | 97 "else" |
99 " inline.login.getAuthExtHost().addEventListener('ready', handler);")); | 98 " inline.login.getAuthExtHost().addEventListener('ready', handler);", |
100 | 99 &message)); |
101 std::string message; | 100 ASSERT_EQ("ready", message); |
102 do { | |
103 ASSERT_TRUE(message_queue.WaitForMessage(&message)); | |
104 } while (message != "\"ready\""); | |
105 } | 101 } |
106 | 102 |
107 void ExecuteJsToSigninInSigninFrame(Browser* browser, | 103 void WaitUntilElementExistsInSigninFrame(Browser* browser, |
108 const std::string& email, | 104 const std::string& element_id) { |
109 const std::string& password) { | 105 std::string message; |
110 std::string js = | 106 std::string js = |
| 107 "function WaitForElementById(elementId) {" |
| 108 " var retries = 10; /* 10 seconds. */" |
| 109 " function CheckelementExists() {" |
| 110 " if (document.getElementById(elementId) != null) {" |
| 111 " window.domAutomationController.send('found');" |
| 112 " } else if (retries > 0) { " |
| 113 " retries--;" |
| 114 " window.setTimeout(CheckelementExists, 1000);" |
| 115 " } else {" |
| 116 " window.domAutomationController.send('failed');" |
| 117 " }" |
| 118 " }" |
| 119 " CheckelementExists();" |
| 120 "}" |
| 121 "WaitForElementById('" + element_id + "');"; |
| 122 content::WebContents* web_contents = |
| 123 browser->tab_strip_model()->GetActiveWebContents(); |
| 124 ASSERT_TRUE(content::ExecuteScriptAndExtractString( |
| 125 InlineLoginUI::GetAuthFrame(web_contents, GURL(), "signin-frame"), |
| 126 js, &message)); |
| 127 |
| 128 ASSERT_EQ("found", message) << |
| 129 "Failed to find element with id " << element_id; |
| 130 } |
| 131 |
| 132 bool ElementExistsInSigninFrame(Browser* browser, |
| 133 const std::string& element_id) { |
| 134 content::WebContents* web_contents = |
| 135 browser->tab_strip_model()->GetActiveWebContents(); |
| 136 bool result = false; |
| 137 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
| 138 InlineLoginUI::GetAuthFrame(web_contents, GURL(), "signin-frame"), |
| 139 "window.domAutomationController.send(" |
| 140 " document.getElementById('" + element_id + "') != null);", |
| 141 &result)); |
| 142 return result; |
| 143 } |
| 144 |
| 145 void SigninInNewGaiaFlow(Browser* browser, |
| 146 const std::string& email, |
| 147 const std::string& password) { |
| 148 std::string js = "document.getElementById('Email').value = '" + email + "';" |
| 149 "document.getElementById('next').click();"; |
| 150 |
| 151 content::WebContents* web_contents = |
| 152 browser->tab_strip_model()->GetActiveWebContents(); |
| 153 ASSERT_TRUE(content::ExecuteScript(InlineLoginUI::GetAuthFrame( |
| 154 web_contents, GURL(), "signin-frame"), js)); |
| 155 |
| 156 WaitUntilElementExistsInSigninFrame(browser, "Passwd"); |
| 157 js = "document.getElementById('Passwd').value = '" + password + "';" |
| 158 "document.getElementById('signIn').click();"; |
| 159 |
| 160 ASSERT_TRUE(content::ExecuteScript(InlineLoginUI::GetAuthFrame( |
| 161 web_contents, GURL(), "signin-frame"), js)); |
| 162 } |
| 163 |
| 164 void SigninInOldGaiaFlow(Browser* browser, |
| 165 const std::string& email, |
| 166 const std::string& password) { |
| 167 std::string js = |
111 "document.getElementById('Email').value = '" + email + "';" | 168 "document.getElementById('Email').value = '" + email + "';" |
112 "document.getElementById('Passwd').value = '" + password + "';" | 169 "document.getElementById('Passwd').value = '" + password + "';" |
113 "document.getElementById('signIn').click();"; | 170 "document.getElementById('signIn').click();"; |
114 | 171 |
115 content::WebContents* web_contents = | 172 content::WebContents* web_contents = |
116 browser->tab_strip_model()->GetActiveWebContents(); | 173 browser->tab_strip_model()->GetActiveWebContents(); |
117 ASSERT_TRUE(content::ExecuteScript(InlineLoginUI::GetAuthFrame( | 174 ASSERT_TRUE(content::ExecuteScript(InlineLoginUI::GetAuthFrame( |
118 web_contents, GURL(), "signin-frame"), js)); | 175 web_contents, GURL(), "signin-frame"), js)); |
119 } | 176 } |
120 | 177 |
| 178 void ExecuteJsToSigninInSigninFrame(Browser* browser, |
| 179 const std::string& email, |
| 180 const std::string& password) { |
| 181 WaitUntilElementExistsInSigninFrame(browser, "Email"); |
| 182 if (ElementExistsInSigninFrame(browser, "next")) |
| 183 SigninInNewGaiaFlow(browser, email, password); |
| 184 else |
| 185 SigninInOldGaiaFlow(browser, email, password); |
| 186 } |
| 187 |
121 bool SignInWithUI(Browser* browser, | 188 bool SignInWithUI(Browser* browser, |
122 const std::string& username, | 189 const std::string& username, |
123 const std::string& password) { | 190 const std::string& password) { |
124 | 191 |
125 SignInObserver signin_observer; | 192 SignInObserver signin_observer; |
126 scoped_ptr<SigninTracker> tracker = | 193 scoped_ptr<SigninTracker> tracker = |
127 SigninTrackerFactory::CreateForProfile(browser->profile(), | 194 SigninTrackerFactory::CreateForProfile(browser->profile(), |
128 &signin_observer); | 195 &signin_observer); |
129 | 196 |
130 GURL signin_url = signin::GetPromoURL( | 197 GURL signin_url = signin::GetPromoURL( |
(...skipping 11 matching lines...) Expand all Loading... |
142 | 209 |
143 DVLOG(1) << "Wait for login UI to be ready."; | 210 DVLOG(1) << "Wait for login UI to be ready."; |
144 WaitUntilUIReady(browser); | 211 WaitUntilUIReady(browser); |
145 DVLOG(1) << "Sign in user: " << username; | 212 DVLOG(1) << "Sign in user: " << username; |
146 ExecuteJsToSigninInSigninFrame(browser, username, password); | 213 ExecuteJsToSigninInSigninFrame(browser, username, password); |
147 signin_observer.Wait(); | 214 signin_observer.Wait(); |
148 return signin_observer.DidSignIn(); | 215 return signin_observer.DidSignIn(); |
149 } | 216 } |
150 | 217 |
151 } // namespace login_ui_test_utils | 218 } // namespace login_ui_test_utils |
OLD | NEW |