Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(148)

Side by Side Diff: chrome/renderer/autofill/password_autofill_agent_browsertest.cc

Issue 1028163002: Processing USERNAME reply from Autofill server in Password Manager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added test and changed sending message Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "base/strings/string_util.h" 5 #include "base/strings/string_util.h"
6 #include "base/strings/utf_string_conversions.h" 6 #include "base/strings/utf_string_conversions.h"
7 #include "chrome/test/base/chrome_render_view_test.h" 7 #include "chrome/test/base/chrome_render_view_test.h"
8 #include "components/autofill/content/common/autofill_messages.h" 8 #include "components/autofill/content/common/autofill_messages.h"
9 #include "components/autofill/content/renderer/autofill_agent.h" 9 #include "components/autofill/content/renderer/autofill_agent.h"
10 #include "components/autofill/content/renderer/form_autofill_util.h" 10 #include "components/autofill/content/renderer/form_autofill_util.h"
11 #include "components/autofill/content/renderer/password_autofill_agent.h" 11 #include "components/autofill/content/renderer/password_autofill_agent.h"
12 #include "components/autofill/content/renderer/test_password_autofill_agent.h" 12 #include "components/autofill/content/renderer/test_password_autofill_agent.h"
13 #include "components/autofill/core/common/autofill_constants.h" 13 #include "components/autofill/core/common/autofill_constants.h"
14 #include "components/autofill/core/common/autofill_switches.h" 14 #include "components/autofill/core/common/autofill_switches.h"
15 #include "components/autofill/core/common/form_data.h" 15 #include "components/autofill/core/common/form_data.h"
16 #include "components/autofill/core/common/form_field_data.h" 16 #include "components/autofill/core/common/form_field_data.h"
17 #include "content/public/renderer/render_frame.h" 17 #include "content/public/renderer/render_frame.h"
18 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "third_party/WebKit/public/platform/WebString.h" 19 #include "third_party/WebKit/public/platform/WebString.h"
20 #include "third_party/WebKit/public/platform/WebVector.h" 20 #include "third_party/WebKit/public/platform/WebVector.h"
21 #include "third_party/WebKit/public/web/WebDocument.h" 21 #include "third_party/WebKit/public/web/WebDocument.h"
22 #include "third_party/WebKit/public/web/WebElement.h" 22 #include "third_party/WebKit/public/web/WebElement.h"
23 #include "third_party/WebKit/public/web/WebFormElement.h" 23 #include "third_party/WebKit/public/web/WebFormElement.h"
24 #include "third_party/WebKit/public/web/WebFormControlElement.h"
24 #include "third_party/WebKit/public/web/WebInputElement.h" 25 #include "third_party/WebKit/public/web/WebInputElement.h"
25 #include "third_party/WebKit/public/web/WebLocalFrame.h" 26 #include "third_party/WebKit/public/web/WebLocalFrame.h"
26 #include "third_party/WebKit/public/web/WebNode.h" 27 #include "third_party/WebKit/public/web/WebNode.h"
27 #include "third_party/WebKit/public/web/WebView.h" 28 #include "third_party/WebKit/public/web/WebView.h"
28 #include "ui/events/keycodes/keyboard_codes.h" 29 #include "ui/events/keycodes/keyboard_codes.h"
29 30
30 using autofill::PasswordForm; 31 using autofill::PasswordForm;
31 using base::ASCIIToUTF16; 32 using base::ASCIIToUTF16;
32 using base::UTF16ToUTF8; 33 using base::UTF16ToUTF8;
33 using blink::WebDocument; 34 using blink::WebDocument;
34 using blink::WebElement; 35 using blink::WebElement;
35 using blink::WebFrame; 36 using blink::WebFrame;
36 using blink::WebInputElement; 37 using blink::WebInputElement;
37 using blink::WebString; 38 using blink::WebString;
38 using blink::WebView; 39 using blink::WebView;
39 40
40 namespace { 41 namespace {
41 42
42 const int kPasswordFillFormDataId = 1234; 43 const int kPasswordFillFormDataId = 1234;
43 44
44 // The name of the username/password element in the form. 45 // The name of the username/password element in the form.
45 const char kUsernameName[] = "username"; 46 const char kUsernameName[] = "username";
46 const char kPasswordName[] = "password"; 47 const char kPasswordName[] = "password";
48 const char kEmailName[] = "email";
47 49
48 const char kAliceUsername[] = "alice"; 50 const char kAliceUsername[] = "alice";
49 const char kAlicePassword[] = "password"; 51 const char kAlicePassword[] = "password";
50 const char kBobUsername[] = "bob"; 52 const char kBobUsername[] = "bob";
51 const char kBobPassword[] = "secret"; 53 const char kBobPassword[] = "secret";
52 const char kCarolUsername[] = "Carol"; 54 const char kCarolUsername[] = "Carol";
53 const char kCarolPassword[] = "test"; 55 const char kCarolPassword[] = "test";
54 const char kCarolAlternateUsername[] = "RealCarolUsername"; 56 const char kCarolAlternateUsername[] = "RealCarolUsername";
55 57
56 const char kFormHTML[] = 58 const char kFormHTML[] =
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 " usernameOnchangeCalled = false;" 155 " usernameOnchangeCalled = false;"
154 " passwordOnchangeCalled = false;" 156 " passwordOnchangeCalled = false;"
155 " document.getElementById('username').onchange = function() {" 157 " document.getElementById('username').onchange = function() {"
156 " usernameOnchangeCalled = true;" 158 " usernameOnchangeCalled = true;"
157 " };" 159 " };"
158 " document.getElementById('password').onchange = function() {" 160 " document.getElementById('password').onchange = function() {"
159 " passwordOnchangeCalled = true;" 161 " passwordOnchangeCalled = true;"
160 " };" 162 " };"
161 "</script>"; 163 "</script>";
162 164
165 const char kFormHTMLWithTwoTextFields[] =
166 "<FORM name='LoginTestForm' id='LoginTestForm' "
167 "action='http://www.bidule.com'>"
168 " <INPUT type='text' id='username'/>"
169 " <INPUT type='text' id='email'/>"
170 " <INPUT type='password' id='password'/>"
171 " <INPUT type='submit' value='Login'/>"
172 "</FORM>";
173
163 // Sets the "readonly" attribute of |element| to the value given by |read_only|. 174 // Sets the "readonly" attribute of |element| to the value given by |read_only|.
164 void SetElementReadOnly(WebInputElement& element, bool read_only) { 175 void SetElementReadOnly(WebInputElement& element, bool read_only) {
165 element.setAttribute(WebString::fromUTF8("readonly"), 176 element.setAttribute(WebString::fromUTF8("readonly"),
166 read_only ? WebString::fromUTF8("true") : WebString()); 177 read_only ? WebString::fromUTF8("true") : WebString());
167 } 178 }
168 179
169 } // namespace 180 } // namespace
170 181
171 namespace autofill { 182 namespace autofill {
172 183
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 WebDocument document = GetMainFrame()->document(); 271 WebDocument document = GetMainFrame()->document();
261 WebElement element = 272 WebElement element =
262 document.getElementById(WebString::fromUTF8(kUsernameName)); 273 document.getElementById(WebString::fromUTF8(kUsernameName));
263 ASSERT_FALSE(element.isNull()); 274 ASSERT_FALSE(element.isNull());
264 username_element_ = element.to<blink::WebInputElement>(); 275 username_element_ = element.to<blink::WebInputElement>();
265 element = document.getElementById(WebString::fromUTF8(kPasswordName)); 276 element = document.getElementById(WebString::fromUTF8(kPasswordName));
266 ASSERT_FALSE(element.isNull()); 277 ASSERT_FALSE(element.isNull());
267 password_element_ = element.to<blink::WebInputElement>(); 278 password_element_ = element.to<blink::WebInputElement>();
268 } 279 }
269 280
281 blink::WebInputElement GetInputElementByID(const char* id) {
282 WebDocument document = GetMainFrame()->document();
283 WebElement element = document.getElementById(WebString::fromUTF8(id));
284 return element.to<blink::WebInputElement>();
285 }
286
270 void ClearUsernameAndPasswordFields() { 287 void ClearUsernameAndPasswordFields() {
271 username_element_.setValue(""); 288 username_element_.setValue("");
272 username_element_.setAutofilled(false); 289 username_element_.setAutofilled(false);
273 password_element_.setValue(""); 290 password_element_.setValue("");
274 password_element_.setAutofilled(false); 291 password_element_.setAutofilled(false);
275 } 292 }
276 293
277 void SimulateDidEndEditing(WebFrame* input_frame, WebInputElement& input) { 294 void SimulateDidEndEditing(WebFrame* input_frame, WebInputElement& input) {
278 static_cast<blink::WebAutofillClient*>(autofill_agent_) 295 static_cast<blink::WebAutofillClient*>(autofill_agent_)
279 ->textFieldDidEndEditing(input); 296 ->textFieldDidEndEditing(input);
(...skipping 1567 matching lines...) Expand 10 before | Expand all | Expand 10 after
1847 autofill::switches::kEnableFillOnAccountSelect); 1864 autofill::switches::kEnableFillOnAccountSelect);
1848 1865
1849 ClearUsernameAndPasswordFields(); 1866 ClearUsernameAndPasswordFields();
1850 1867
1851 username_element_.setValue("foobar"); 1868 username_element_.setValue("foobar");
1852 SetElementReadOnly(username_element_, true); 1869 SetElementReadOnly(username_element_, true);
1853 1870
1854 CheckTextFieldsState(std::string("foobar"), false, std::string(), false); 1871 CheckTextFieldsState(std::string("foobar"), false, std::string(), false);
1855 } 1872 }
1856 1873
1874 // Tests that in a password form with 2 text input elements the last before
vabr (Chromium) 2015/03/25 10:12:33 nit: I found that sentence a bit difficult to pars
dvadym 2015/03/25 16:34:07 Done.
1875 // a password field text input element will be chosen as username.
1876 TEST_F(PasswordAutofillAgentTest, FindingUsernameWithoutAutofillPredictions) {
1877 LoadHTML(kFormHTMLWithTwoTextFields);
1878 UpdateUsernameAndPasswordElements();
1879 blink::WebInputElement email_element = GetInputElementByID(kEmailName);
1880 SimulateInputChangeForElement("temp", true, GetMainFrame(), username_element_,
1881 true);
1882 SimulateInputChangeForElement("temp@google.com", true, GetMainFrame(),
1883 email_element, true);
1884 SimulateInputChangeForElement("random", true, GetMainFrame(),
1885 password_element_, true);
1886 static_cast<content::RenderFrameObserver*>(password_autofill_agent_)
1887 ->WillSubmitForm(username_element_.form());
1888
1889 // Observe that the PasswordAutofillAgent parse email as username.
vabr (Chromium) 2015/03/25 10:12:33 grammar: parse -> parses But, actually, I suggest
dvadym 2015/03/25 16:34:07 Done.
1890 ExpectFormSubmittedWithUsernameAndPasswords("temp@google.com", "random", "");
1891 }
1892
1893 // Tests that in a password form with 2 text input elements it's chosen as
vabr (Chromium) 2015/03/25 10:12:33 The sentence has broken grammar and is hard to und
dvadym 2015/03/25 16:34:07 Done.
1894 // usernamethe that element that are in Autofill server predictions.
1895 TEST_F(PasswordAutofillAgentTest, FindingUsernameWithAutofillPredictions) {
1896 LoadHTML(kFormHTMLWithTwoTextFields);
1897 UpdateUsernameAndPasswordElements();
1898 blink::WebInputElement email_element = GetInputElementByID(kEmailName);
1899 SimulateInputChangeForElement("temp", true, GetMainFrame(), username_element_,
1900 true);
1901 SimulateInputChangeForElement("temp@google.com", true, GetMainFrame(),
1902 email_element, true);
1903 SimulateInputChangeForElement("random", true, GetMainFrame(),
1904 password_element_, true);
1905
1906 // Find FormData for visible password form.
1907 blink::WebFormElement form_element = username_element_.form();
1908 FormData form_data;
1909 ASSERT_TRUE(WebFormElementToFormData(
1910 form_element, blink::WebFormControlElement(), REQUIRE_NONE, EXTRACT_NONE,
1911 &form_data, nullptr));
1912 // Simulate Autofill predictions: the first field is username.
1913 std::map<autofill::FormData, autofill::FormFieldData> predictions;
1914 predictions[form_data] = form_data.fields[0];
1915 password_autofill_agent_->OnAutofillDataReceived(predictions);
1916 static_cast<content::RenderFrameObserver*>(password_autofill_agent_)
1917 ->WillSubmitForm(username_element_.form());
1918
1919 // Observe that the PasswordAutofillAgent parse email as username.
vabr (Chromium) 2015/03/25 10:12:33 Similarly to line 1893, I suggest: // Observe that
dvadym 2015/03/25 16:34:07 Done.
1920 ExpectFormSubmittedWithUsernameAndPasswords("temp", "random", "");
1921 }
1922
1857 } // namespace autofill 1923 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698