| OLD | NEW |
| 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 <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
| 8 #include "base/metrics/field_trial.h" | 8 #include "base/metrics/field_trial.h" |
| 9 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 labels.push_back(ASCIIToUTF16("Email:")); | 154 labels.push_back(ASCIIToUTF16("Email:")); |
| 155 names.push_back(ASCIIToUTF16("email")); | 155 names.push_back(ASCIIToUTF16("email")); |
| 156 values.push_back(ASCIIToUTF16("john@example.com")); | 156 values.push_back(ASCIIToUTF16("john@example.com")); |
| 157 | 157 |
| 158 ExpectLabels(html, labels, names, values); | 158 ExpectLabels(html, labels, names, values); |
| 159 } | 159 } |
| 160 | 160 |
| 161 typedef void (*FillFormFunction)(const FormData& form, | 161 typedef void (*FillFormFunction)(const FormData& form, |
| 162 const WebInputElement& element); | 162 const WebInputElement& element); |
| 163 | 163 |
| 164 typedef WebString (WebInputElement::*GetValueFunction)(void) const; | 164 typedef WebString (*GetValueFunction)(WebFormControlElement element); |
| 165 | 165 |
| 166 // Test FormFillxxx functions. | 166 // Test FormFillxxx functions. |
| 167 void TestFormFillFunctions(const char* html, | 167 void TestFormFillFunctions(const char* html, |
| 168 const AutofillFieldCase* field_cases, | 168 const AutofillFieldCase* field_cases, |
| 169 size_t number_of_field_cases, | 169 size_t number_of_field_cases, |
| 170 FillFormFunction fill_form_function, | 170 FillFormFunction fill_form_function, |
| 171 GetValueFunction get_value_function) { | 171 GetValueFunction get_value_function) { |
| 172 LoadHTML(html); | 172 LoadHTML(html); |
| 173 | 173 |
| 174 WebFrame* web_frame = GetMainFrame(); | 174 WebFrame* web_frame = GetMainFrame(); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 void ValidteFilledField(const AutofillFieldCase& field_case, | 228 void ValidteFilledField(const AutofillFieldCase& field_case, |
| 229 GetValueFunction get_value_function) { | 229 GetValueFunction get_value_function) { |
| 230 SCOPED_TRACE(base::StringPrintf("Verify autofilled value for field %s", | 230 SCOPED_TRACE(base::StringPrintf("Verify autofilled value for field %s", |
| 231 field_case.name)); | 231 field_case.name)); |
| 232 WebString value; | 232 WebString value; |
| 233 WebFormControlElement element = GetMainFrame()->document().getElementById( | 233 WebFormControlElement element = GetMainFrame()->document().getElementById( |
| 234 ASCIIToUTF16(field_case.name)).to<WebFormControlElement>(); | 234 ASCIIToUTF16(field_case.name)).to<WebFormControlElement>(); |
| 235 if (element.formControlType() == "select-one") { | 235 if (element.formControlType() == "select-one") { |
| 236 value = element.to<WebSelectElement>().value(); | 236 value = element.to<WebSelectElement>().value(); |
| 237 } else if (element.formControlType() == "textarea") { | 237 } else if (element.formControlType() == "textarea") { |
| 238 value = element.to<WebTextAreaElement>().value(); | 238 value = get_value_function(element); |
| 239 } else { | 239 } else { |
| 240 ASSERT_TRUE(element.formControlType() == "text" || | 240 ASSERT_TRUE(element.formControlType() == "text" || |
| 241 element.formControlType() == "month"); | 241 element.formControlType() == "month"); |
| 242 WebInputElement input_element = GetMainFrame()->document().getElementById( | 242 value = get_value_function(element); |
| 243 ASCIIToUTF16(field_case.name)).to<WebInputElement>(); | |
| 244 value = (input_element.*get_value_function)(); | |
| 245 } | 243 } |
| 246 | 244 |
| 247 const WebString expected_value = ASCIIToUTF16(field_case.expected_value); | 245 const WebString expected_value = ASCIIToUTF16(field_case.expected_value); |
| 248 if (expected_value.isEmpty()) | 246 if (expected_value.isEmpty()) |
| 249 EXPECT_TRUE(value.isEmpty()); | 247 EXPECT_TRUE(value.isEmpty()); |
| 250 else | 248 else |
| 251 EXPECT_EQ(expected_value, value); | 249 EXPECT_EQ(expected_value, value); |
| 252 | 250 |
| 253 EXPECT_EQ(field_case.should_be_autofilled, element.isAutofilled()); | 251 EXPECT_EQ(field_case.should_be_autofilled, element.isAutofilled()); |
| 254 } | 252 } |
| 255 | 253 |
| 256 static void FillFormForAllFieldsWrapper(const FormData& form, | 254 static void FillFormForAllFieldsWrapper(const FormData& form, |
| 257 const WebInputElement& element) { | 255 const WebInputElement& element) { |
| 258 FillFormForAllElements(form, element.form()); | 256 FillFormForAllElements(form, element.form()); |
| 259 } | 257 } |
| 260 | 258 |
| 261 static void FillFormIncludingNonFocusableElementsWrapper( | 259 static void FillFormIncludingNonFocusableElementsWrapper( |
| 262 const FormData& form, | 260 const FormData& form, |
| 263 const WebInputElement& element) { | 261 const WebInputElement& element) { |
| 264 FillFormIncludingNonFocusableElements(form, element.form()); | 262 FillFormIncludingNonFocusableElements(form, element.form()); |
| 265 } | 263 } |
| 266 | 264 |
| 265 static WebString GetValueWrapper(WebFormControlElement element) { |
| 266 if (element.formControlType() == "textarea") |
| 267 return element.to<WebTextAreaElement>().value(); |
| 268 |
| 269 return element.to<WebInputElement>().value(); |
| 270 } |
| 271 |
| 272 static WebString GetSuggestedValueWrapper(WebFormControlElement element) { |
| 273 if (element.formControlType() == "textarea") |
| 274 return element.to<WebTextAreaElement>().suggestedValue(); |
| 275 |
| 276 return element.to<WebInputElement>().suggestedValue(); |
| 277 } |
| 278 |
| 267 private: | 279 private: |
| 268 DISALLOW_COPY_AND_ASSIGN(FormAutofillTest); | 280 DISALLOW_COPY_AND_ASSIGN(FormAutofillTest); |
| 269 }; | 281 }; |
| 270 | 282 |
| 271 // We should be able to extract a normal text field. | 283 // We should be able to extract a normal text field. |
| 272 TEST_F(FormAutofillTest, WebFormControlElementToFormField) { | 284 TEST_F(FormAutofillTest, WebFormControlElementToFormField) { |
| 273 LoadHTML("<INPUT type=\"text\" id=\"element\" value=\"value\"/>"); | 285 LoadHTML("<INPUT type=\"text\" id=\"element\" value=\"value\"/>"); |
| 274 | 286 |
| 275 WebFrame* frame = GetMainFrame(); | 287 WebFrame* frame = GetMainFrame(); |
| 276 ASSERT_NE(static_cast<WebFrame*>(NULL), frame); | 288 ASSERT_NE(static_cast<WebFrame*>(NULL), frame); |
| (...skipping 831 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1108 // non-empty value. | 1120 // non-empty value. |
| 1109 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"}, | 1121 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"}, |
| 1110 // Regular textarea elements should be autofilled. | 1122 // Regular textarea elements should be autofilled. |
| 1111 {"textarea", "textarea", "", "", true, "some multi-\nline value", | 1123 {"textarea", "textarea", "", "", true, "some multi-\nline value", |
| 1112 "some multi-\nline value"}, | 1124 "some multi-\nline value"}, |
| 1113 // Non-empty textarea elements should not be autofilled. | 1125 // Non-empty textarea elements should not be autofilled. |
| 1114 {"textarea", "textarea-nonempty", "Go\naway!", "", false, | 1126 {"textarea", "textarea-nonempty", "Go\naway!", "", false, |
| 1115 "some multi-\nline value", "Go\naway!"}, | 1127 "some multi-\nline value", "Go\naway!"}, |
| 1116 }; | 1128 }; |
| 1117 TestFormFillFunctions(kFormHtml, field_cases, arraysize(field_cases), | 1129 TestFormFillFunctions(kFormHtml, field_cases, arraysize(field_cases), |
| 1118 FillForm, &WebInputElement::value); | 1130 FillForm, &GetValueWrapper); |
| 1119 // Verify preview selection. | 1131 // Verify preview selection. |
| 1120 WebInputElement firstname = GetMainFrame()->document(). | 1132 WebInputElement firstname = GetMainFrame()->document(). |
| 1121 getElementById("firstname").to<WebInputElement>(); | 1133 getElementById("firstname").to<WebInputElement>(); |
| 1122 EXPECT_EQ(16, firstname.selectionStart()); | 1134 EXPECT_EQ(16, firstname.selectionStart()); |
| 1123 EXPECT_EQ(16, firstname.selectionEnd()); | 1135 EXPECT_EQ(16, firstname.selectionEnd()); |
| 1124 } | 1136 } |
| 1125 | 1137 |
| 1126 TEST_F(FormAutofillTest, FillFormIncludingNonFocusableElements) { | 1138 TEST_F(FormAutofillTest, FillFormIncludingNonFocusableElements) { |
| 1127 static const AutofillFieldCase field_cases[] = { | 1139 static const AutofillFieldCase field_cases[] = { |
| 1128 // fields: form_control_type, name, initial_value, autocomplete_attribute, | 1140 // fields: form_control_type, name, initial_value, autocomplete_attribute, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 1158 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"}, | 1170 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"}, |
| 1159 // Regular textarea elements should be autofilled. | 1171 // Regular textarea elements should be autofilled. |
| 1160 {"textarea", "textarea", "", "", true, "some multi-\nline value", | 1172 {"textarea", "textarea", "", "", true, "some multi-\nline value", |
| 1161 "some multi-\nline value"}, | 1173 "some multi-\nline value"}, |
| 1162 // Nonempty textarea elements should be overridden. | 1174 // Nonempty textarea elements should be overridden. |
| 1163 {"textarea", "textarea-nonempty", "Go\naway!", "", true, | 1175 {"textarea", "textarea-nonempty", "Go\naway!", "", true, |
| 1164 "some multi-\nline value", "some multi-\nline value"}, | 1176 "some multi-\nline value", "some multi-\nline value"}, |
| 1165 }; | 1177 }; |
| 1166 TestFormFillFunctions(kFormHtml, field_cases, arraysize(field_cases), | 1178 TestFormFillFunctions(kFormHtml, field_cases, arraysize(field_cases), |
| 1167 &FillFormIncludingNonFocusableElementsWrapper, | 1179 &FillFormIncludingNonFocusableElementsWrapper, |
| 1168 &WebInputElement::value); | 1180 &GetValueWrapper); |
| 1169 } | 1181 } |
| 1170 | 1182 |
| 1171 TEST_F(FormAutofillTest, PreviewForm) { | 1183 TEST_F(FormAutofillTest, PreviewForm) { |
| 1172 static const char* html = | |
| 1173 "<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">" | |
| 1174 " <INPUT type=\"text\" id=\"firstname\"/>" | |
| 1175 " <INPUT type=\"text\" id=\"lastname\"/>" | |
| 1176 " <INPUT type=\"text\" id=\"notempty\" value=\"Hi\"/>" | |
| 1177 " <INPUT type=\"text\" autocomplete=\"off\" id=\"noautocomplete\"/>" | |
| 1178 " <INPUT type=\"text\" disabled=\"disabled\" id=\"notenabled\"/>" | |
| 1179 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>" | |
| 1180 "</FORM>"; | |
| 1181 | |
| 1182 static const AutofillFieldCase field_cases[] = { | 1184 static const AutofillFieldCase field_cases[] = { |
| 1183 // Normal empty fields should be previewed. | 1185 // Normal empty fields should be previewed. |
| 1184 {"text", "firstname", "", "", true, "suggested firstname", | 1186 {"text", "firstname", "", "", true, "suggested firstname", |
| 1185 "suggested firstname"}, | 1187 "suggested firstname"}, |
| 1186 {"text", "lastname", "", "", true, "suggested lastname", | 1188 {"text", "lastname", "", "", true, "suggested lastname", |
| 1187 "suggested lastname"}, | 1189 "suggested lastname"}, |
| 1190 // Hidden fields should not be extracted to form_data. |
| 1188 // Non empty fields should not be previewed. | 1191 // Non empty fields should not be previewed. |
| 1189 {"text", "notempty", "Hi", "", false, "filled notempty", ""}, | 1192 {"text", "notempty", "Hi", "", false, "suggested notempty", ""}, |
| 1190 // "noautocomplete" should not be extracted to form_data. | 1193 // "noautocomplete" should not be extracted to form_data. |
| 1191 // Disabled fields should not be previewed. | 1194 // Disabled fields should not be previewed. |
| 1192 {"text", "notenabled", "", "", false, "filled notenabled", ""}, | 1195 {"text", "notenabled", "", "", false, "suggested notenabled", ""}, |
| 1196 // Readonly fields should not be previewed. |
| 1197 {"text", "readonly", "", "", false, "suggested readonly", ""}, |
| 1198 // Fields with "visibility: hidden" should not be previewed. |
| 1199 {"text", "invisible", "", "", false, "suggested invisible", |
| 1200 ""}, |
| 1201 // Fields with "display:none" should not previewed. |
| 1202 {"text", "displaynone", "", "", false, "suggested displaynone", |
| 1203 ""}, |
| 1204 // Regular <input type="month"> should not be previewed. |
| 1205 {"month", "month", "", "", false, "2017-11", ""}, |
| 1206 // Non-empty <input type="month"> should not be previewed. |
| 1207 {"month", "month-nonempty", "2011-12", "", false, "2017-11", ""}, |
| 1208 // Regular select fields preview is not yet supported |
| 1209 {"select-one", "select", "", "", false, "TX", ""}, |
| 1210 // Select fields preview is not yet supported |
| 1211 {"select-one", "select-nonempty", "CA", "", false, "TX", "CA"}, |
| 1212 // Normal textarea elements should be previewed. |
| 1213 {"textarea", "textarea", "", "", true, "suggested multi-\nline value", |
| 1214 "suggested multi-\nline value"}, |
| 1215 // Nonempty textarea elements should not be previewed. |
| 1216 {"textarea", "textarea-nonempty", "Go\naway!", "", false, |
| 1217 "suggested multi-\nline value", ""}, |
| 1193 }; | 1218 }; |
| 1194 TestFormFillFunctions(html, field_cases, arraysize(field_cases), &PreviewForm, | 1219 TestFormFillFunctions(kFormHtml, field_cases, arraysize(field_cases), |
| 1195 &WebInputElement::suggestedValue); | 1220 &PreviewForm, &GetSuggestedValueWrapper); |
| 1196 | 1221 |
| 1197 // Verify preview selection. | 1222 // Verify preview selection. |
| 1198 WebInputElement firstname = GetMainFrame()->document(). | 1223 WebInputElement firstname = GetMainFrame()->document(). |
| 1199 getElementById("firstname").to<WebInputElement>(); | 1224 getElementById("firstname").to<WebInputElement>(); |
| 1200 EXPECT_EQ(0, firstname.selectionStart()); | 1225 EXPECT_EQ(0, firstname.selectionStart()); |
| 1201 EXPECT_EQ(19, firstname.selectionEnd()); | 1226 EXPECT_EQ(19, firstname.selectionEnd()); |
| 1202 } | 1227 } |
| 1203 | 1228 |
| 1204 TEST_F(FormAutofillTest, Labels) { | 1229 TEST_F(FormAutofillTest, Labels) { |
| 1205 ExpectJohnSmithLabels( | 1230 ExpectJohnSmithLabels( |
| (...skipping 1983 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3189 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]); | 3214 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]); |
| 3190 | 3215 |
| 3191 expected.name = ASCIIToUTF16("country"); | 3216 expected.name = ASCIIToUTF16("country"); |
| 3192 expected.value = ASCIIToUTF16("AL"); | 3217 expected.value = ASCIIToUTF16("AL"); |
| 3193 expected.form_control_type = "select-one"; | 3218 expected.form_control_type = "select-one"; |
| 3194 expected.max_length = 0; | 3219 expected.max_length = 0; |
| 3195 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]); | 3220 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]); |
| 3196 } | 3221 } |
| 3197 | 3222 |
| 3198 } // namespace autofill | 3223 } // namespace autofill |
| OLD | NEW |