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 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result); |
| 351 } |
| 352 |
339 // We should be able to extract the autocompletetype attribute. | 353 // We should be able to extract the autocompletetype attribute. |
340 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldAutocompletetype) { | 354 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldAutocompletetype) { |
341 std::string html = | 355 std::string html = |
342 "<INPUT type=\"text\" id=\"absent\"/>" | 356 "<INPUT type=\"text\" id=\"absent\"/>" |
343 "<INPUT type=\"text\" id=\"empty\" autocomplete=\"\"/>" | 357 "<INPUT type=\"text\" id=\"empty\" autocomplete=\"\"/>" |
344 "<INPUT type=\"text\" id=\"off\" autocomplete=\"off\"/>" | 358 "<INPUT type=\"text\" id=\"off\" autocomplete=\"off\"/>" |
345 "<INPUT type=\"text\" id=\"regular\" autocomplete=\"email\"/>" | 359 "<INPUT type=\"text\" id=\"regular\" autocomplete=\"email\"/>" |
346 "<INPUT type=\"text\" id=\"multi-valued\" " | 360 "<INPUT type=\"text\" id=\"multi-valued\" " |
347 " autocomplete=\"billing email\"/>" | 361 " autocomplete=\"billing email\"/>" |
348 "<INPUT type=\"text\" id=\"experimental\" x-autocompletetype=\"email\"/>" | 362 "<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\">" | 426 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">" |
413 " <LABEL for=\"firstname\">First name:</LABEL>" | 427 " <LABEL for=\"firstname\">First name:</LABEL>" |
414 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>" | 428 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>" |
415 " <LABEL for=\"lastname\">Last name:</LABEL>" | 429 " <LABEL for=\"lastname\">Last name:</LABEL>" |
416 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>" | 430 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>" |
417 " <LABEL for=\"state\">State:</LABEL>" | 431 " <LABEL for=\"state\">State:</LABEL>" |
418 " <SELECT id=\"state\"/>" | 432 " <SELECT id=\"state\"/>" |
419 " <OPTION value=\"CA\">California</OPTION>" | 433 " <OPTION value=\"CA\">California</OPTION>" |
420 " <OPTION value=\"TX\">Texas</OPTION>" | 434 " <OPTION value=\"TX\">Texas</OPTION>" |
421 " </SELECT>" | 435 " </SELECT>" |
| 436 " <LABEL for=\"password\">Password:</LABEL>" |
| 437 " <INPUT type=\"password\" id=\"password\" value=\"secret\"/>" |
| 438 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>" |
422 // The below inputs should be ignored | 439 // The below inputs should be ignored |
423 " <LABEL for=\"notvisible\">Hidden:</LABEL>" | 440 " <LABEL for=\"notvisible\">Hidden:</LABEL>" |
424 " <INPUT type=\"hidden\" id=\"notvisible\" value=\"apple\"/>" | 441 " <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>"); | 442 "</FORM>"); |
429 | 443 |
430 WebFrame* frame = GetMainFrame(); | 444 WebFrame* frame = GetMainFrame(); |
431 ASSERT_NE(static_cast<WebFrame*>(NULL), frame); | 445 ASSERT_NE(static_cast<WebFrame*>(NULL), frame); |
432 | 446 |
433 WebVector<WebFormElement> forms; | 447 WebVector<WebFormElement> forms; |
434 frame->document().forms(forms); | 448 frame->document().forms(forms); |
435 ASSERT_EQ(1U, forms.size()); | 449 ASSERT_EQ(1U, forms.size()); |
436 | 450 |
437 WebElement element = frame->document().getElementById("firstname"); | 451 WebElement element = frame->document().getElementById("firstname"); |
438 WebInputElement input_element = element.to<WebInputElement>(); | 452 WebInputElement input_element = element.to<WebInputElement>(); |
439 | 453 |
440 FormData form; | 454 FormData form; |
441 FormFieldData field; | 455 FormFieldData field; |
442 EXPECT_TRUE(WebFormElementToFormData(forms[0], | 456 EXPECT_TRUE(WebFormElementToFormData(forms[0], |
443 input_element, | 457 input_element, |
444 autofill::REQUIRE_NONE, | 458 autofill::REQUIRE_NONE, |
445 autofill::EXTRACT_VALUE, | 459 autofill::EXTRACT_VALUE, |
446 &form, | 460 &form, |
447 &field)); | 461 &field)); |
448 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name); | 462 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name); |
449 EXPECT_EQ(GURL(frame->document().url()), form.origin); | 463 EXPECT_EQ(GURL(frame->document().url()), form.origin); |
450 EXPECT_EQ(GURL("http://cnn.com"), form.action); | 464 EXPECT_EQ(GURL("http://cnn.com"), form.action); |
451 | 465 |
452 const std::vector<FormFieldData>& fields = form.fields; | 466 const std::vector<FormFieldData>& fields = form.fields; |
453 ASSERT_EQ(3U, fields.size()); | 467 ASSERT_EQ(4U, fields.size()); |
454 | 468 |
455 FormFieldData expected; | 469 FormFieldData expected; |
456 expected.name = ASCIIToUTF16("firstname"); | 470 expected.name = ASCIIToUTF16("firstname"); |
457 expected.value = ASCIIToUTF16("John"); | 471 expected.value = ASCIIToUTF16("John"); |
458 expected.label = ASCIIToUTF16("First name:"); | 472 expected.label = ASCIIToUTF16("First name:"); |
459 expected.form_control_type = "text"; | 473 expected.form_control_type = "text"; |
460 expected.max_length = WebInputElement::defaultMaxLength(); | 474 expected.max_length = WebInputElement::defaultMaxLength(); |
461 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]); | 475 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]); |
462 | 476 |
463 expected.name = ASCIIToUTF16("lastname"); | 477 expected.name = ASCIIToUTF16("lastname"); |
464 expected.value = ASCIIToUTF16("Smith"); | 478 expected.value = ASCIIToUTF16("Smith"); |
465 expected.label = ASCIIToUTF16("Last name:"); | 479 expected.label = ASCIIToUTF16("Last name:"); |
466 expected.form_control_type = "text"; | 480 expected.form_control_type = "text"; |
467 expected.max_length = WebInputElement::defaultMaxLength(); | 481 expected.max_length = WebInputElement::defaultMaxLength(); |
468 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]); | 482 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]); |
469 | 483 |
470 expected.name = ASCIIToUTF16("state"); | 484 expected.name = ASCIIToUTF16("state"); |
471 expected.value = ASCIIToUTF16("CA"); | 485 expected.value = ASCIIToUTF16("CA"); |
472 expected.label = ASCIIToUTF16("State:"); | 486 expected.label = ASCIIToUTF16("State:"); |
473 expected.form_control_type = "select-one"; | 487 expected.form_control_type = "select-one"; |
474 expected.max_length = 0; | 488 expected.max_length = 0; |
475 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]); | 489 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]); |
| 490 |
| 491 expected.name = ASCIIToUTF16("password"); |
| 492 expected.value = ASCIIToUTF16("secret"); |
| 493 expected.label = ASCIIToUTF16("Password:"); |
| 494 expected.form_control_type = "password"; |
| 495 expected.max_length = WebInputElement::defaultMaxLength(); |
| 496 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]); |
476 } | 497 } |
477 | 498 |
478 // We should not be able to serialize a form with too many fillable fields. | 499 // We should not be able to serialize a form with too many fillable fields. |
479 TEST_F(FormAutofillTest, WebFormElementToFormDataTooManyFields) { | 500 TEST_F(FormAutofillTest, WebFormElementToFormDataTooManyFields) { |
480 std::string html = | 501 std::string html = |
481 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"; | 502 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"; |
482 for (size_t i = 0; i < (autofill::kMaxParseableFields + 1); ++i) { | 503 for (size_t i = 0; i < (autofill::kMaxParseableFields + 1); ++i) { |
483 html += "<INPUT type=\"text\"/>"; | 504 html += "<INPUT type=\"text\"/>"; |
484 } | 505 } |
485 html += "</FORM>"; | 506 html += "</FORM>"; |
(...skipping 2558 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3044 expected.form_control_type = "text"; | 3065 expected.form_control_type = "text"; |
3045 expected.max_length = WebInputElement::defaultMaxLength(); | 3066 expected.max_length = WebInputElement::defaultMaxLength(); |
3046 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]); | 3067 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]); |
3047 | 3068 |
3048 expected.name = ASCIIToUTF16("country"); | 3069 expected.name = ASCIIToUTF16("country"); |
3049 expected.value = ASCIIToUTF16("AL"); | 3070 expected.value = ASCIIToUTF16("AL"); |
3050 expected.form_control_type = "select-one"; | 3071 expected.form_control_type = "select-one"; |
3051 expected.max_length = 0; | 3072 expected.max_length = 0; |
3052 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]); | 3073 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]); |
3053 } | 3074 } |
OLD | NEW |