| 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/string16.h" | 8 #include "base/string16.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| (...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 EXPECT_EQ(ASCIIToUTF16("CA"), result3.option_values[0]); | 295 EXPECT_EQ(ASCIIToUTF16("CA"), result3.option_values[0]); |
| 296 EXPECT_EQ(ASCIIToUTF16("California"), result3.option_contents[0]); | 296 EXPECT_EQ(ASCIIToUTF16("California"), result3.option_contents[0]); |
| 297 EXPECT_EQ(ASCIIToUTF16("TX"), result3.option_values[1]); | 297 EXPECT_EQ(ASCIIToUTF16("TX"), result3.option_values[1]); |
| 298 EXPECT_EQ(ASCIIToUTF16("Texas"), result3.option_contents[1]); | 298 EXPECT_EQ(ASCIIToUTF16("Texas"), result3.option_contents[1]); |
| 299 } | 299 } |
| 300 | 300 |
| 301 // We should not extract the value for non-text and non-select fields. | 301 // We should not extract the value for non-text and non-select fields. |
| 302 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldInvalidType) { | 302 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldInvalidType) { |
| 303 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">" | 303 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">" |
| 304 " <INPUT type=\"hidden\" id=\"hidden\" value=\"apple\"/>" | 304 " <INPUT type=\"hidden\" id=\"hidden\" value=\"apple\"/>" |
| 305 " <INPUT type=\"password\" id=\"password\" value=\"secret\"/>" | |
| 306 " <INPUT type=\"submit\" id=\"submit\" value=\"Send\"/>" | 305 " <INPUT type=\"submit\" id=\"submit\" value=\"Send\"/>" |
| 307 "</FORM>"); | 306 "</FORM>"); |
| 308 | 307 |
| 309 WebFrame* frame = GetMainFrame(); | 308 WebFrame* frame = GetMainFrame(); |
| 310 ASSERT_NE(static_cast<WebFrame*>(NULL), frame); | 309 ASSERT_NE(static_cast<WebFrame*>(NULL), frame); |
| 311 | 310 |
| 312 WebElement web_element = frame->document().getElementById("hidden"); | 311 WebElement web_element = frame->document().getElementById("hidden"); |
| 313 WebFormControlElement element = web_element.to<WebFormControlElement>(); | 312 WebFormControlElement element = web_element.to<WebFormControlElement>(); |
| 314 FormFieldData result; | 313 FormFieldData result; |
| 315 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result); | 314 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result); |
| 316 | 315 |
| 317 FormFieldData expected; | 316 FormFieldData expected; |
| 318 expected.max_length = 0; | 317 expected.max_length = 0; |
| 319 | 318 |
| 320 expected.name = ASCIIToUTF16("hidden"); | 319 expected.name = ASCIIToUTF16("hidden"); |
| 321 expected.form_control_type = "hidden"; | 320 expected.form_control_type = "hidden"; |
| 322 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result); | 321 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result); |
| 323 | 322 |
| 324 web_element = frame->document().getElementById("password"); | |
| 325 element = web_element.to<WebFormControlElement>(); | |
| 326 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result); | |
| 327 expected.name = ASCIIToUTF16("password"); | |
| 328 expected.form_control_type = "password"; | |
| 329 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result); | |
| 330 | |
| 331 web_element = frame->document().getElementById("submit"); | 323 web_element = frame->document().getElementById("submit"); |
| 332 element = web_element.to<WebFormControlElement>(); | 324 element = web_element.to<WebFormControlElement>(); |
| 333 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result); | 325 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result); |
| 334 expected.name = ASCIIToUTF16("submit"); | 326 expected.name = ASCIIToUTF16("submit"); |
| 335 expected.form_control_type = "submit"; | 327 expected.form_control_type = "submit"; |
| 336 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result); | 328 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result); |
| 337 } | 329 } |
| 338 | 330 |
| 331 // We should be able to extract password fields. |
| 332 TEST_F(FormAutofillTest, WebFormControlElementToPasswordFormField) { |
| 333 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">" |
| 334 " <INPUT type=\"password\" id=\"password\" value=\"secret\"/>" |
| 335 "</FORM>"); |
| 336 |
| 337 WebFrame* frame = GetMainFrame(); |
| 338 ASSERT_NE(static_cast<WebFrame*>(NULL), frame); |
| 339 |
| 340 WebElement web_element = frame->document().getElementById("password"); |
| 341 WebFormControlElement element = web_element.to<WebFormControlElement>(); |
| 342 FormFieldData result; |
| 343 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result); |
| 344 |
| 345 FormFieldData expected; |
| 346 expected.max_length = WebInputElement::defaultMaxLength(); |
| 347 expected.name = ASCIIToUTF16("password"); |
| 348 expected.form_control_type = "password"; |
| 349 expected.value = ASCIIToUTF16("secret"); |
| 350 expected.is_password_field = true; |
| 351 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result); |
| 352 } |
| 353 |
| 339 // We should be able to extract the autocompletetype attribute. | 354 // We should be able to extract the autocompletetype attribute. |
| 340 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldAutocompletetype) { | 355 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldAutocompletetype) { |
| 341 std::string html = | 356 std::string html = |
| 342 "<INPUT type=\"text\" id=\"absent\"/>" | 357 "<INPUT type=\"text\" id=\"absent\"/>" |
| 343 "<INPUT type=\"text\" id=\"empty\" autocomplete=\"\"/>" | 358 "<INPUT type=\"text\" id=\"empty\" autocomplete=\"\"/>" |
| 344 "<INPUT type=\"text\" id=\"off\" autocomplete=\"off\"/>" | 359 "<INPUT type=\"text\" id=\"off\" autocomplete=\"off\"/>" |
| 345 "<INPUT type=\"text\" id=\"regular\" autocomplete=\"email\"/>" | 360 "<INPUT type=\"text\" id=\"regular\" autocomplete=\"email\"/>" |
| 346 "<INPUT type=\"text\" id=\"multi-valued\" " | 361 "<INPUT type=\"text\" id=\"multi-valued\" " |
| 347 " autocomplete=\"billing email\"/>" | 362 " autocomplete=\"billing email\"/>" |
| 348 "<INPUT type=\"text\" id=\"experimental\" x-autocompletetype=\"email\"/>" | 363 "<INPUT type=\"text\" id=\"experimental\" x-autocompletetype=\"email\"/>" |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 412 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">" | 427 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">" |
| 413 " <LABEL for=\"firstname\">First name:</LABEL>" | 428 " <LABEL for=\"firstname\">First name:</LABEL>" |
| 414 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>" | 429 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>" |
| 415 " <LABEL for=\"lastname\">Last name:</LABEL>" | 430 " <LABEL for=\"lastname\">Last name:</LABEL>" |
| 416 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>" | 431 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>" |
| 417 " <LABEL for=\"state\">State:</LABEL>" | 432 " <LABEL for=\"state\">State:</LABEL>" |
| 418 " <SELECT id=\"state\"/>" | 433 " <SELECT id=\"state\"/>" |
| 419 " <OPTION value=\"CA\">California</OPTION>" | 434 " <OPTION value=\"CA\">California</OPTION>" |
| 420 " <OPTION value=\"TX\">Texas</OPTION>" | 435 " <OPTION value=\"TX\">Texas</OPTION>" |
| 421 " </SELECT>" | 436 " </SELECT>" |
| 437 " <LABEL for=\"password\">Password:</LABEL>" |
| 438 " <INPUT type=\"password\" id=\"password\" value=\"secret\"/>" |
| 439 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>" |
| 422 // The below inputs should be ignored | 440 // The below inputs should be ignored |
| 423 " <LABEL for=\"notvisible\">Hidden:</LABEL>" | 441 " <LABEL for=\"notvisible\">Hidden:</LABEL>" |
| 424 " <INPUT type=\"hidden\" id=\"notvisible\" value=\"apple\"/>" | 442 " <INPUT type=\"hidden\" id=\"notvisible\" value=\"apple\"/>" |
| 425 " <LABEL for=\"password\">Password:</LABEL>" | |
| 426 " <INPUT type=\"password\" id=\"password\" value=\"secret\"/>" | |
| 427 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>" | |
| 428 "</FORM>"); | 443 "</FORM>"); |
| 429 | 444 |
| 430 WebFrame* frame = GetMainFrame(); | 445 WebFrame* frame = GetMainFrame(); |
| 431 ASSERT_NE(static_cast<WebFrame*>(NULL), frame); | 446 ASSERT_NE(static_cast<WebFrame*>(NULL), frame); |
| 432 | 447 |
| 433 WebVector<WebFormElement> forms; | 448 WebVector<WebFormElement> forms; |
| 434 frame->document().forms(forms); | 449 frame->document().forms(forms); |
| 435 ASSERT_EQ(1U, forms.size()); | 450 ASSERT_EQ(1U, forms.size()); |
| 436 | 451 |
| 437 WebElement element = frame->document().getElementById("firstname"); | 452 WebElement element = frame->document().getElementById("firstname"); |
| 438 WebInputElement input_element = element.to<WebInputElement>(); | 453 WebInputElement input_element = element.to<WebInputElement>(); |
| 439 | 454 |
| 440 FormData form; | 455 FormData form; |
| 441 FormFieldData field; | 456 FormFieldData field; |
| 442 EXPECT_TRUE(WebFormElementToFormData(forms[0], | 457 EXPECT_TRUE(WebFormElementToFormData(forms[0], |
| 443 input_element, | 458 input_element, |
| 444 autofill::REQUIRE_NONE, | 459 autofill::REQUIRE_NONE, |
| 445 autofill::EXTRACT_VALUE, | 460 autofill::EXTRACT_VALUE, |
| 446 &form, | 461 &form, |
| 447 &field)); | 462 &field)); |
| 448 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name); | 463 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name); |
| 449 EXPECT_EQ(GURL(frame->document().url()), form.origin); | 464 EXPECT_EQ(GURL(frame->document().url()), form.origin); |
| 450 EXPECT_EQ(GURL("http://cnn.com"), form.action); | 465 EXPECT_EQ(GURL("http://cnn.com"), form.action); |
| 451 | 466 |
| 452 const std::vector<FormFieldData>& fields = form.fields; | 467 const std::vector<FormFieldData>& fields = form.fields; |
| 453 ASSERT_EQ(3U, fields.size()); | 468 ASSERT_EQ(4U, fields.size()); |
| 454 | 469 |
| 455 FormFieldData expected; | 470 FormFieldData expected; |
| 456 expected.name = ASCIIToUTF16("firstname"); | 471 expected.name = ASCIIToUTF16("firstname"); |
| 457 expected.value = ASCIIToUTF16("John"); | 472 expected.value = ASCIIToUTF16("John"); |
| 458 expected.label = ASCIIToUTF16("First name:"); | 473 expected.label = ASCIIToUTF16("First name:"); |
| 459 expected.form_control_type = "text"; | 474 expected.form_control_type = "text"; |
| 460 expected.max_length = WebInputElement::defaultMaxLength(); | 475 expected.max_length = WebInputElement::defaultMaxLength(); |
| 461 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]); | 476 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]); |
| 462 | 477 |
| 463 expected.name = ASCIIToUTF16("lastname"); | 478 expected.name = ASCIIToUTF16("lastname"); |
| 464 expected.value = ASCIIToUTF16("Smith"); | 479 expected.value = ASCIIToUTF16("Smith"); |
| 465 expected.label = ASCIIToUTF16("Last name:"); | 480 expected.label = ASCIIToUTF16("Last name:"); |
| 466 expected.form_control_type = "text"; | 481 expected.form_control_type = "text"; |
| 467 expected.max_length = WebInputElement::defaultMaxLength(); | 482 expected.max_length = WebInputElement::defaultMaxLength(); |
| 468 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]); | 483 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]); |
| 469 | 484 |
| 470 expected.name = ASCIIToUTF16("state"); | 485 expected.name = ASCIIToUTF16("state"); |
| 471 expected.value = ASCIIToUTF16("CA"); | 486 expected.value = ASCIIToUTF16("CA"); |
| 472 expected.label = ASCIIToUTF16("State:"); | 487 expected.label = ASCIIToUTF16("State:"); |
| 473 expected.form_control_type = "select-one"; | 488 expected.form_control_type = "select-one"; |
| 474 expected.max_length = 0; | 489 expected.max_length = 0; |
| 475 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]); | 490 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]); |
| 491 |
| 492 expected.name = ASCIIToUTF16("password"); |
| 493 expected.value = ASCIIToUTF16("secret"); |
| 494 expected.label = ASCIIToUTF16("Password:"); |
| 495 expected.form_control_type = "password"; |
| 496 expected.is_password_field = true; |
| 497 expected.max_length = WebInputElement::defaultMaxLength(); |
| 498 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]); |
| 476 } | 499 } |
| 477 | 500 |
| 478 // We should not be able to serialize a form with too many fillable fields. | 501 // We should not be able to serialize a form with too many fillable fields. |
| 479 TEST_F(FormAutofillTest, WebFormElementToFormDataTooManyFields) { | 502 TEST_F(FormAutofillTest, WebFormElementToFormDataTooManyFields) { |
| 480 std::string html = | 503 std::string html = |
| 481 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"; | 504 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"; |
| 482 for (size_t i = 0; i < (autofill::kMaxParseableFields + 1); ++i) { | 505 for (size_t i = 0; i < (autofill::kMaxParseableFields + 1); ++i) { |
| 483 html += "<INPUT type=\"text\"/>"; | 506 html += "<INPUT type=\"text\"/>"; |
| 484 } | 507 } |
| 485 html += "</FORM>"; | 508 html += "</FORM>"; |
| (...skipping 2558 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3044 expected.form_control_type = "text"; | 3067 expected.form_control_type = "text"; |
| 3045 expected.max_length = WebInputElement::defaultMaxLength(); | 3068 expected.max_length = WebInputElement::defaultMaxLength(); |
| 3046 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]); | 3069 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]); |
| 3047 | 3070 |
| 3048 expected.name = ASCIIToUTF16("country"); | 3071 expected.name = ASCIIToUTF16("country"); |
| 3049 expected.value = ASCIIToUTF16("AL"); | 3072 expected.value = ASCIIToUTF16("AL"); |
| 3050 expected.form_control_type = "select-one"; | 3073 expected.form_control_type = "select-one"; |
| 3051 expected.max_length = 0; | 3074 expected.max_length = 0; |
| 3052 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]); | 3075 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]); |
| 3053 } | 3076 } |
| OLD | NEW |