Chromium Code Reviews| 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 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 153 labels.push_back(ASCIIToUTF16("Email:")); | 153 labels.push_back(ASCIIToUTF16("Email:")); |
| 154 names.push_back(ASCIIToUTF16("email")); | 154 names.push_back(ASCIIToUTF16("email")); |
| 155 values.push_back(ASCIIToUTF16("john@example.com")); | 155 values.push_back(ASCIIToUTF16("john@example.com")); |
| 156 | 156 |
| 157 ExpectLabels(html, labels, names, values); | 157 ExpectLabels(html, labels, names, values); |
| 158 } | 158 } |
| 159 | 159 |
| 160 typedef void (*FillFormFunction)(const FormData& form, | 160 typedef void (*FillFormFunction)(const FormData& form, |
| 161 const WebInputElement& element); | 161 const WebInputElement& element); |
| 162 | 162 |
| 163 typedef WebString (WebInputElement::*GetValueFunction)(void) const; | 163 typedef WebString (*GetValueFunction)(WebFormControlElement element); |
| 164 | 164 |
| 165 // Test FormFillxxx functions. | 165 // Test FormFillxxx functions. |
| 166 void TestFormFillFunctions(const char* html, | 166 void TestFormFillFunctions(const char* html, |
| 167 const AutofillFieldCase* field_cases, | 167 const AutofillFieldCase* field_cases, |
| 168 size_t number_of_field_cases, | 168 size_t number_of_field_cases, |
| 169 FillFormFunction fill_form_function, | 169 FillFormFunction fill_form_function, |
| 170 GetValueFunction get_value_function) { | 170 GetValueFunction get_value_function) { |
| 171 LoadHTML(html); | 171 LoadHTML(html); |
| 172 | 172 |
| 173 WebFrame* web_frame = GetMainFrame(); | 173 WebFrame* web_frame = GetMainFrame(); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 227 void ValidteFilledField(const AutofillFieldCase& field_case, | 227 void ValidteFilledField(const AutofillFieldCase& field_case, |
| 228 GetValueFunction get_value_function) { | 228 GetValueFunction get_value_function) { |
| 229 SCOPED_TRACE(base::StringPrintf("Verify autofilled value for field %s", | 229 SCOPED_TRACE(base::StringPrintf("Verify autofilled value for field %s", |
| 230 field_case.name)); | 230 field_case.name)); |
| 231 WebString value; | 231 WebString value; |
| 232 WebFormControlElement element = GetMainFrame()->document().getElementById( | 232 WebFormControlElement element = GetMainFrame()->document().getElementById( |
| 233 ASCIIToUTF16(field_case.name)).to<WebFormControlElement>(); | 233 ASCIIToUTF16(field_case.name)).to<WebFormControlElement>(); |
| 234 if (element.formControlType() == "select-one") { | 234 if (element.formControlType() == "select-one") { |
| 235 value = element.to<WebSelectElement>().value(); | 235 value = element.to<WebSelectElement>().value(); |
| 236 } else if (element.formControlType() == "textarea") { | 236 } else if (element.formControlType() == "textarea") { |
| 237 value = element.to<WebTextAreaElement>().value(); | 237 value = get_value_function(element); |
| 238 } else { | 238 } else { |
| 239 ASSERT_TRUE(element.formControlType() == "text" || | 239 ASSERT_TRUE(element.formControlType() == "text" || |
| 240 element.formControlType() == "month"); | 240 element.formControlType() == "month"); |
| 241 WebInputElement input_element = GetMainFrame()->document().getElementById( | 241 value = get_value_function(element); |
| 242 ASCIIToUTF16(field_case.name)).to<WebInputElement>(); | |
| 243 value = (input_element.*get_value_function)(); | |
| 244 } | 242 } |
| 245 | 243 |
| 246 const WebString expected_value = ASCIIToUTF16(field_case.expected_value); | 244 const WebString expected_value = ASCIIToUTF16(field_case.expected_value); |
| 247 if (expected_value.isEmpty()) | 245 if (expected_value.isEmpty()) |
| 248 EXPECT_TRUE(value.isEmpty()); | 246 EXPECT_TRUE(value.isEmpty()); |
| 249 else | 247 else |
| 250 EXPECT_EQ(expected_value, value); | 248 EXPECT_EQ(expected_value, value); |
| 251 | 249 |
| 252 EXPECT_EQ(field_case.should_be_autofilled, element.isAutofilled()); | 250 EXPECT_EQ(field_case.should_be_autofilled, element.isAutofilled()); |
| 253 } | 251 } |
| 254 | 252 |
| 255 static void FillFormForAllFieldsWrapper(const FormData& form, | 253 static void FillFormForAllFieldsWrapper(const FormData& form, |
| 256 const WebInputElement& element) { | 254 const WebInputElement& element) { |
| 257 FillFormForAllElements(form, element.form()); | 255 FillFormForAllElements(form, element.form()); |
| 258 } | 256 } |
| 259 | 257 |
| 260 static void FillFormIncludingNonFocusableElementsWrapper( | 258 static void FillFormIncludingNonFocusableElementsWrapper( |
| 261 const FormData& form, | 259 const FormData& form, |
| 262 const WebInputElement& element) { | 260 const WebInputElement& element) { |
| 263 FillFormIncludingNonFocusableElements(form, element.form()); | 261 FillFormIncludingNonFocusableElements(form, element.form()); |
| 264 } | 262 } |
| 265 | 263 |
| 264 static WebString GetValueWrapper(WebFormControlElement element) { | |
| 265 WebString value; | |
| 266 if (element.formControlType() == "textarea") | |
| 267 value = element.to<WebTextAreaElement>().value(); | |
| 268 else | |
| 269 value = element.to<WebInputElement>().value(); | |
| 270 return value; | |
|
Ilya Sherman
2014/01/07 01:09:38
nit: No need for the local variable value, i.e. yo
ziran.sun
2014/01/07 16:40:41
Done.
| |
| 271 } | |
| 272 | |
| 273 static WebString GetSuggestedValueWrapper(WebFormControlElement element) { | |
| 274 WebString suggested_value; | |
| 275 if (element.formControlType() == "textarea") | |
| 276 suggested_value = element.to<WebTextAreaElement>().suggestedValue(); | |
| 277 else | |
| 278 suggested_value = element.to<WebInputElement>().suggestedValue(); | |
| 279 return suggested_value; | |
| 280 } | |
|
Ilya Sherman
2014/01/07 01:09:38
Likewise for this method.
ziran.sun
2014/01/07 16:40:41
Done.
| |
| 281 | |
| 266 private: | 282 private: |
| 267 DISALLOW_COPY_AND_ASSIGN(FormAutofillTest); | 283 DISALLOW_COPY_AND_ASSIGN(FormAutofillTest); |
| 268 }; | 284 }; |
| 269 | 285 |
| 270 // We should be able to extract a normal text field. | 286 // We should be able to extract a normal text field. |
| 271 TEST_F(FormAutofillTest, WebFormControlElementToFormField) { | 287 TEST_F(FormAutofillTest, WebFormControlElementToFormField) { |
| 272 LoadHTML("<INPUT type=\"text\" id=\"element\" value=\"value\"/>"); | 288 LoadHTML("<INPUT type=\"text\" id=\"element\" value=\"value\"/>"); |
| 273 | 289 |
| 274 WebFrame* frame = GetMainFrame(); | 290 WebFrame* frame = GetMainFrame(); |
| 275 ASSERT_NE(static_cast<WebFrame*>(NULL), frame); | 291 ASSERT_NE(static_cast<WebFrame*>(NULL), frame); |
| (...skipping 831 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1107 // non-empty value. | 1123 // non-empty value. |
| 1108 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"}, | 1124 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"}, |
| 1109 // Regular textarea elements should be autofilled. | 1125 // Regular textarea elements should be autofilled. |
| 1110 {"textarea", "textarea", "", "", true, "some multi-\nline value", | 1126 {"textarea", "textarea", "", "", true, "some multi-\nline value", |
| 1111 "some multi-\nline value"}, | 1127 "some multi-\nline value"}, |
| 1112 // Non-empty textarea elements should not be autofilled. | 1128 // Non-empty textarea elements should not be autofilled. |
| 1113 {"textarea", "textarea-nonempty", "Go\naway!", "", false, | 1129 {"textarea", "textarea-nonempty", "Go\naway!", "", false, |
| 1114 "some multi-\nline value", "Go\naway!"}, | 1130 "some multi-\nline value", "Go\naway!"}, |
| 1115 }; | 1131 }; |
| 1116 TestFormFillFunctions(kFormHtml, field_cases, arraysize(field_cases), | 1132 TestFormFillFunctions(kFormHtml, field_cases, arraysize(field_cases), |
| 1117 FillForm, &WebInputElement::value); | 1133 FillForm, &GetValueWrapper); |
| 1118 // Verify preview selection. | 1134 // Verify preview selection. |
| 1119 WebInputElement firstname = GetMainFrame()->document(). | 1135 WebInputElement firstname = GetMainFrame()->document(). |
| 1120 getElementById("firstname").to<WebInputElement>(); | 1136 getElementById("firstname").to<WebInputElement>(); |
| 1121 EXPECT_EQ(16, firstname.selectionStart()); | 1137 EXPECT_EQ(16, firstname.selectionStart()); |
| 1122 EXPECT_EQ(16, firstname.selectionEnd()); | 1138 EXPECT_EQ(16, firstname.selectionEnd()); |
| 1123 } | 1139 } |
| 1124 | 1140 |
| 1125 TEST_F(FormAutofillTest, FillFormIncludingNonFocusableElements) { | 1141 TEST_F(FormAutofillTest, FillFormIncludingNonFocusableElements) { |
| 1126 static const AutofillFieldCase field_cases[] = { | 1142 static const AutofillFieldCase field_cases[] = { |
| 1127 // fields: form_control_type, name, initial_value, autocomplete_attribute, | 1143 // fields: form_control_type, name, initial_value, autocomplete_attribute, |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 1157 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"}, | 1173 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"}, |
| 1158 // Regular textarea elements should be autofilled. | 1174 // Regular textarea elements should be autofilled. |
| 1159 {"textarea", "textarea", "", "", true, "some multi-\nline value", | 1175 {"textarea", "textarea", "", "", true, "some multi-\nline value", |
| 1160 "some multi-\nline value"}, | 1176 "some multi-\nline value"}, |
| 1161 // Nonempty textarea elements should be overridden. | 1177 // Nonempty textarea elements should be overridden. |
| 1162 {"textarea", "textarea-nonempty", "Go\naway!", "", true, | 1178 {"textarea", "textarea-nonempty", "Go\naway!", "", true, |
| 1163 "some multi-\nline value", "some multi-\nline value"}, | 1179 "some multi-\nline value", "some multi-\nline value"}, |
| 1164 }; | 1180 }; |
| 1165 TestFormFillFunctions(kFormHtml, field_cases, arraysize(field_cases), | 1181 TestFormFillFunctions(kFormHtml, field_cases, arraysize(field_cases), |
| 1166 &FillFormIncludingNonFocusableElementsWrapper, | 1182 &FillFormIncludingNonFocusableElementsWrapper, |
| 1167 &WebInputElement::value); | 1183 &GetValueWrapper); |
| 1168 } | 1184 } |
| 1169 | 1185 |
| 1170 TEST_F(FormAutofillTest, PreviewForm) { | 1186 TEST_F(FormAutofillTest, PreviewForm) { |
| 1171 static const char* html = | 1187 static const char* html = |
| 1172 "<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">" | 1188 "<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">" |
| 1173 " <INPUT type=\"text\" id=\"firstname\"/>" | 1189 " <INPUT type=\"text\" id=\"firstname\"/>" |
| 1174 " <INPUT type=\"text\" id=\"lastname\"/>" | 1190 " <INPUT type=\"text\" id=\"lastname\"/>" |
| 1175 " <INPUT type=\"text\" id=\"notempty\" value=\"Hi\"/>" | 1191 " <INPUT type=\"text\" id=\"notempty\" value=\"Hi\"/>" |
| 1176 " <INPUT type=\"text\" autocomplete=\"off\" id=\"noautocomplete\"/>" | 1192 " <INPUT type=\"text\" autocomplete=\"off\" id=\"noautocomplete\"/>" |
| 1177 " <INPUT type=\"text\" disabled=\"disabled\" id=\"notenabled\"/>" | 1193 " <INPUT type=\"text\" disabled=\"disabled\" id=\"notenabled\"/>" |
| 1194 " <TEXTAREA id=\"textarea\"></TEXTAREA>" | |
| 1178 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>" | 1195 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>" |
| 1179 "</FORM>"; | 1196 "</FORM>"; |
|
Ilya Sherman
2014/01/07 01:09:38
Please update this test to use kFormHtml rather th
ziran.sun
2014/01/07 16:40:41
I think selection preview is not supported from th
Ilya Sherman
2014/01/08 04:13:47
That's true, but I think we could still have test
ziran.sun
2014/01/08 17:02:39
I've made a note on <select> fields saying that pr
ziran.sun
2014/01/08 17:02:39
Done.
| |
| 1180 | 1197 |
| 1181 static const AutofillFieldCase field_cases[] = { | 1198 static const AutofillFieldCase field_cases[] = { |
| 1182 // Normal empty fields should be previewed. | 1199 // Normal empty fields should be previewed. |
| 1183 {"text", "firstname", "", "", true, "suggested firstname", | 1200 {"text", "firstname", "", "", true, "suggested firstname", |
| 1184 "suggested firstname"}, | 1201 "suggested firstname"}, |
| 1185 {"text", "lastname", "", "", true, "suggested lastname", | 1202 {"text", "lastname", "", "", true, "suggested lastname", |
| 1186 "suggested lastname"}, | 1203 "suggested lastname"}, |
| 1187 // Non empty fields should not be previewed. | 1204 // Non empty fields should not be previewed. |
| 1188 {"text", "notempty", "Hi", "", false, "filled notempty", ""}, | 1205 {"text", "notempty", "Hi", "", false, "filled notempty", ""}, |
| 1189 // "noautocomplete" should not be extracted to form_data. | 1206 // "noautocomplete" should not be extracted to form_data. |
| 1190 // Disabled fields should not be previewed. | 1207 // Disabled fields should not be previewed. |
| 1191 {"text", "notenabled", "", "", false, "filled notenabled", ""}, | 1208 {"text", "notenabled", "", "", false, "filled notenabled", ""}, |
| 1209 // Normal textarea elements should be previewed. | |
| 1210 {"textarea", "textarea", "", "", true, "suggested multi-\nline value", | |
| 1211 "suggested multi-\nline value"}, | |
| 1192 }; | 1212 }; |
| 1193 TestFormFillFunctions(html, field_cases, arraysize(field_cases), &PreviewForm, | 1213 TestFormFillFunctions(html, field_cases, arraysize(field_cases), &PreviewForm, |
| 1194 &WebInputElement::suggestedValue); | 1214 &GetSuggestedValueWrapper); |
| 1195 | 1215 |
| 1196 // Verify preview selection. | 1216 // Verify preview selection. |
| 1197 WebInputElement firstname = GetMainFrame()->document(). | 1217 WebInputElement firstname = GetMainFrame()->document(). |
| 1198 getElementById("firstname").to<WebInputElement>(); | 1218 getElementById("firstname").to<WebInputElement>(); |
| 1199 EXPECT_EQ(0, firstname.selectionStart()); | 1219 EXPECT_EQ(0, firstname.selectionStart()); |
| 1200 EXPECT_EQ(19, firstname.selectionEnd()); | 1220 EXPECT_EQ(19, firstname.selectionEnd()); |
| 1201 } | 1221 } |
| 1202 | 1222 |
| 1203 TEST_F(FormAutofillTest, Labels) { | 1223 TEST_F(FormAutofillTest, Labels) { |
| 1204 ExpectJohnSmithLabels( | 1224 ExpectJohnSmithLabels( |
| (...skipping 1983 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3188 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]); | 3208 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]); |
| 3189 | 3209 |
| 3190 expected.name = ASCIIToUTF16("country"); | 3210 expected.name = ASCIIToUTF16("country"); |
| 3191 expected.value = ASCIIToUTF16("AL"); | 3211 expected.value = ASCIIToUTF16("AL"); |
| 3192 expected.form_control_type = "select-one"; | 3212 expected.form_control_type = "select-one"; |
| 3193 expected.max_length = 0; | 3213 expected.max_length = 0; |
| 3194 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]); | 3214 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]); |
| 3195 } | 3215 } |
| 3196 | 3216 |
| 3197 } // namespace autofill | 3217 } // namespace autofill |
| OLD | NEW |