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

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

Issue 2650623002: Use explicit WebString conversions in autofill (Closed)
Patch Set: . Created 3 years, 11 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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/format_macros.h" 9 #include "base/format_macros.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 } 412 }
413 } 413 }
414 414
415 // Validate an Autofilled field. 415 // Validate an Autofilled field.
416 void ValidateFilledField(const AutofillFieldCase& field_case, 416 void ValidateFilledField(const AutofillFieldCase& field_case,
417 GetValueFunction get_value_function) { 417 GetValueFunction get_value_function) {
418 SCOPED_TRACE(base::StringPrintf("Verify autofilled value for field %s", 418 SCOPED_TRACE(base::StringPrintf("Verify autofilled value for field %s",
419 field_case.name)); 419 field_case.name));
420 WebString value; 420 WebString value;
421 WebFormControlElement element = 421 WebFormControlElement element =
422 GetFormControlElementById(ASCIIToUTF16(field_case.name)); 422 GetFormControlElementById(WebString::fromASCII(field_case.name));
423 if ((element.formControlType() == "select-one") || 423 if ((element.formControlType() == "select-one") ||
424 (element.formControlType() == "textarea")) { 424 (element.formControlType() == "textarea")) {
425 value = get_value_function(element); 425 value = get_value_function(element);
426 } else { 426 } else {
427 ASSERT_TRUE(element.formControlType() == "text" || 427 ASSERT_TRUE(element.formControlType() == "text" ||
428 element.formControlType() == "month"); 428 element.formControlType() == "month");
429 value = get_value_function(element); 429 value = get_value_function(element);
430 } 430 }
431 431
432 const WebString expected_value = ASCIIToUTF16(field_case.expected_value); 432 const WebString expected_value =
433 WebString::fromASCII(field_case.expected_value);
433 if (expected_value.isEmpty()) 434 if (expected_value.isEmpty())
434 EXPECT_TRUE(value.isEmpty()); 435 EXPECT_TRUE(value.isEmpty());
435 else 436 else
436 EXPECT_EQ(expected_value.utf8(), value.utf8()); 437 EXPECT_EQ(expected_value.utf8(), value.utf8());
437 438
438 EXPECT_EQ(field_case.should_be_autofilled, element.isAutofilled()); 439 EXPECT_EQ(field_case.should_be_autofilled, element.isAutofilled());
439 } 440 }
440 441
441 WebFormControlElement GetFormControlElementById(const WebString& id) { 442 WebFormControlElement GetFormControlElementById(const WebString& id) {
442 return GetMainFrame()->document().getElementById( 443 return GetMainFrame()->document().getElementById(
(...skipping 624 matching lines...) Expand 10 before | Expand all | Expand 10 after
1067 ASSERT_NE(nullptr, web_frame); 1068 ASSERT_NE(nullptr, web_frame);
1068 1069
1069 FormCache form_cache(*web_frame); 1070 FormCache form_cache(*web_frame);
1070 std::vector<FormData> forms = form_cache.ExtractNewForms(); 1071 std::vector<FormData> forms = form_cache.ExtractNewForms();
1071 ASSERT_EQ(1U, forms.size()); 1072 ASSERT_EQ(1U, forms.size());
1072 1073
1073 // Get the input element we want to find. 1074 // Get the input element we want to find.
1074 WebInputElement input_element = GetInputElementById("firstname"); 1075 WebInputElement input_element = GetInputElementById("firstname");
1075 1076
1076 // Simulate typing by modifying the field value. 1077 // Simulate typing by modifying the field value.
1077 input_element.setValue(ASCIIToUTF16("Wy")); 1078 input_element.setValue(WebString::fromASCII("Wy"));
1078 1079
1079 // Find the form that contains the input element. 1080 // Find the form that contains the input element.
1080 FormData form; 1081 FormData form;
1081 FormFieldData field; 1082 FormFieldData field;
1082 EXPECT_TRUE( 1083 EXPECT_TRUE(
1083 FindFormAndFieldForFormControlElement(input_element, &form, &field)); 1084 FindFormAndFieldForFormControlElement(input_element, &form, &field));
1084 EXPECT_EQ(GetCanonicalOriginForDocument(web_frame->document()), 1085 EXPECT_EQ(GetCanonicalOriginForDocument(web_frame->document()),
1085 form.origin); 1086 form.origin);
1086 if (!unowned) { 1087 if (!unowned) {
1087 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name); 1088 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after
1390 WebInputElement lastname = GetInputElementById("lastname"); 1391 WebInputElement lastname = GetInputElementById("lastname");
1391 lastname.setAutofilled(true); 1392 lastname.setAutofilled(true);
1392 WebInputElement email = GetInputElementById("email"); 1393 WebInputElement email = GetInputElementById("email");
1393 email.setAutofilled(true); 1394 email.setAutofilled(true);
1394 WebInputElement email2 = GetInputElementById("email2"); 1395 WebInputElement email2 = GetInputElementById("email2");
1395 email2.setAutofilled(true); 1396 email2.setAutofilled(true);
1396 WebInputElement phone = GetInputElementById("phone"); 1397 WebInputElement phone = GetInputElementById("phone");
1397 phone.setAutofilled(true); 1398 phone.setAutofilled(true);
1398 1399
1399 // Set the suggested values on two of the elements. 1400 // Set the suggested values on two of the elements.
1400 lastname.setSuggestedValue(ASCIIToUTF16("Earp")); 1401 lastname.setSuggestedValue(WebString::fromASCII("Earp"));
1401 email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com")); 1402 email.setSuggestedValue(WebString::fromASCII("wyatt@earp.com"));
1402 email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com")); 1403 email2.setSuggestedValue(WebString::fromASCII("wyatt@earp.com"));
1403 phone.setSuggestedValue(ASCIIToUTF16("650-777-9999")); 1404 phone.setSuggestedValue(WebString::fromASCII("650-777-9999"));
1404 1405
1405 // Clear the previewed fields. 1406 // Clear the previewed fields.
1406 EXPECT_TRUE(ClearPreviewedFormWithElement(lastname, false)); 1407 EXPECT_TRUE(ClearPreviewedFormWithElement(lastname, false));
1407 1408
1408 // Fields with empty suggestions suggestions are not modified. 1409 // Fields with empty suggestions suggestions are not modified.
1409 EXPECT_EQ(ASCIIToUTF16("Wyatt"), firstname.value()); 1410 EXPECT_EQ(ASCIIToUTF16("Wyatt"), firstname.value().utf16());
1410 EXPECT_TRUE(firstname.suggestedValue().isEmpty()); 1411 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
1411 EXPECT_TRUE(firstname.isAutofilled()); 1412 EXPECT_TRUE(firstname.isAutofilled());
1412 1413
1413 // Verify the previewed fields are cleared. 1414 // Verify the previewed fields are cleared.
1414 EXPECT_TRUE(lastname.value().isEmpty()); 1415 EXPECT_TRUE(lastname.value().isEmpty());
1415 EXPECT_TRUE(lastname.suggestedValue().isEmpty()); 1416 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
1416 EXPECT_FALSE(lastname.isAutofilled()); 1417 EXPECT_FALSE(lastname.isAutofilled());
1417 EXPECT_TRUE(email.value().isEmpty()); 1418 EXPECT_TRUE(email.value().isEmpty());
1418 EXPECT_TRUE(email.suggestedValue().isEmpty()); 1419 EXPECT_TRUE(email.suggestedValue().isEmpty());
1419 EXPECT_FALSE(email.isAutofilled()); 1420 EXPECT_FALSE(email.isAutofilled());
(...skipping 25 matching lines...) Expand all
1445 lastname.setAutofilled(true); 1446 lastname.setAutofilled(true);
1446 WebInputElement email = GetInputElementById("email"); 1447 WebInputElement email = GetInputElementById("email");
1447 email.setAutofilled(true); 1448 email.setAutofilled(true);
1448 WebInputElement email2 = GetInputElementById("email2"); 1449 WebInputElement email2 = GetInputElementById("email2");
1449 email2.setAutofilled(true); 1450 email2.setAutofilled(true);
1450 WebInputElement phone = GetInputElementById("phone"); 1451 WebInputElement phone = GetInputElementById("phone");
1451 phone.setAutofilled(true); 1452 phone.setAutofilled(true);
1452 1453
1453 1454
1454 // Set the suggested values on all of the elements. 1455 // Set the suggested values on all of the elements.
1455 firstname.setSuggestedValue(ASCIIToUTF16("Wyatt")); 1456 firstname.setSuggestedValue(WebString::fromASCII("Wyatt"));
1456 lastname.setSuggestedValue(ASCIIToUTF16("Earp")); 1457 lastname.setSuggestedValue(WebString::fromASCII("Earp"));
1457 email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com")); 1458 email.setSuggestedValue(WebString::fromASCII("wyatt@earp.com"));
1458 email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com")); 1459 email2.setSuggestedValue(WebString::fromASCII("wyatt@earp.com"));
1459 phone.setSuggestedValue(ASCIIToUTF16("650-777-9999")); 1460 phone.setSuggestedValue(WebString::fromASCII("650-777-9999"));
1460 1461
1461 // Clear the previewed fields. 1462 // Clear the previewed fields.
1462 EXPECT_TRUE(ClearPreviewedFormWithElement(firstname, false)); 1463 EXPECT_TRUE(ClearPreviewedFormWithElement(firstname, false));
1463 1464
1464 // Fields with non-empty values are restored. 1465 // Fields with non-empty values are restored.
1465 EXPECT_EQ(ASCIIToUTF16("W"), firstname.value()); 1466 EXPECT_EQ(ASCIIToUTF16("W"), firstname.value().utf16());
1466 EXPECT_TRUE(firstname.suggestedValue().isEmpty()); 1467 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
1467 EXPECT_FALSE(firstname.isAutofilled()); 1468 EXPECT_FALSE(firstname.isAutofilled());
1468 EXPECT_EQ(1, firstname.selectionStart()); 1469 EXPECT_EQ(1, firstname.selectionStart());
1469 EXPECT_EQ(1, firstname.selectionEnd()); 1470 EXPECT_EQ(1, firstname.selectionEnd());
1470 1471
1471 // Verify the previewed fields are cleared. 1472 // Verify the previewed fields are cleared.
1472 EXPECT_TRUE(lastname.value().isEmpty()); 1473 EXPECT_TRUE(lastname.value().isEmpty());
1473 EXPECT_TRUE(lastname.suggestedValue().isEmpty()); 1474 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
1474 EXPECT_FALSE(lastname.isAutofilled()); 1475 EXPECT_FALSE(lastname.isAutofilled());
1475 EXPECT_TRUE(email.value().isEmpty()); 1476 EXPECT_TRUE(email.value().isEmpty());
(...skipping 22 matching lines...) Expand all
1498 WebInputElement lastname = GetInputElementById("lastname"); 1499 WebInputElement lastname = GetInputElementById("lastname");
1499 lastname.setAutofilled(true); 1500 lastname.setAutofilled(true);
1500 WebInputElement email = GetInputElementById("email"); 1501 WebInputElement email = GetInputElementById("email");
1501 email.setAutofilled(true); 1502 email.setAutofilled(true);
1502 WebInputElement email2 = GetInputElementById("email2"); 1503 WebInputElement email2 = GetInputElementById("email2");
1503 email2.setAutofilled(true); 1504 email2.setAutofilled(true);
1504 WebInputElement phone = GetInputElementById("phone"); 1505 WebInputElement phone = GetInputElementById("phone");
1505 phone.setAutofilled(true); 1506 phone.setAutofilled(true);
1506 1507
1507 // Set the suggested values on all of the elements. 1508 // Set the suggested values on all of the elements.
1508 firstname.setSuggestedValue(ASCIIToUTF16("Wyatt")); 1509 firstname.setSuggestedValue(WebString::fromASCII("Wyatt"));
1509 lastname.setSuggestedValue(ASCIIToUTF16("Earp")); 1510 lastname.setSuggestedValue(WebString::fromASCII("Earp"));
1510 email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com")); 1511 email.setSuggestedValue(WebString::fromASCII("wyatt@earp.com"));
1511 email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com")); 1512 email2.setSuggestedValue(WebString::fromASCII("wyatt@earp.com"));
1512 phone.setSuggestedValue(ASCIIToUTF16("650-777-9999")); 1513 phone.setSuggestedValue(WebString::fromASCII("650-777-9999"));
1513 1514
1514 // Clear the previewed fields. 1515 // Clear the previewed fields.
1515 EXPECT_TRUE(ClearPreviewedFormWithElement(firstname, true)); 1516 EXPECT_TRUE(ClearPreviewedFormWithElement(firstname, true));
1516 1517
1517 // Fields with non-empty values are restored. 1518 // Fields with non-empty values are restored.
1518 EXPECT_EQ(ASCIIToUTF16("W"), firstname.value()); 1519 EXPECT_EQ(ASCIIToUTF16("W"), firstname.value().utf16());
1519 EXPECT_TRUE(firstname.suggestedValue().isEmpty()); 1520 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
1520 EXPECT_TRUE(firstname.isAutofilled()); 1521 EXPECT_TRUE(firstname.isAutofilled());
1521 EXPECT_EQ(1, firstname.selectionStart()); 1522 EXPECT_EQ(1, firstname.selectionStart());
1522 EXPECT_EQ(1, firstname.selectionEnd()); 1523 EXPECT_EQ(1, firstname.selectionEnd());
1523 1524
1524 // Verify the previewed fields are cleared. 1525 // Verify the previewed fields are cleared.
1525 EXPECT_TRUE(lastname.value().isEmpty()); 1526 EXPECT_TRUE(lastname.value().isEmpty());
1526 EXPECT_TRUE(lastname.suggestedValue().isEmpty()); 1527 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
1527 EXPECT_FALSE(lastname.isAutofilled()); 1528 EXPECT_FALSE(lastname.isAutofilled());
1528 EXPECT_TRUE(email.value().isEmpty()); 1529 EXPECT_TRUE(email.value().isEmpty());
(...skipping 24 matching lines...) Expand all
1553 lastname.setAutofilled(true); 1554 lastname.setAutofilled(true);
1554 WebInputElement email = GetInputElementById("email"); 1555 WebInputElement email = GetInputElementById("email");
1555 email.setAutofilled(true); 1556 email.setAutofilled(true);
1556 WebInputElement phone = GetInputElementById("phone"); 1557 WebInputElement phone = GetInputElementById("phone");
1557 phone.setAutofilled(true); 1558 phone.setAutofilled(true);
1558 1559
1559 // Clear the fields. 1560 // Clear the fields.
1560 EXPECT_TRUE(form_cache.ClearFormWithElement(firstname)); 1561 EXPECT_TRUE(form_cache.ClearFormWithElement(firstname));
1561 1562
1562 // Verify only autofilled fields are cleared. 1563 // Verify only autofilled fields are cleared.
1563 EXPECT_EQ(ASCIIToUTF16("Wyatt"), firstname.value()); 1564 EXPECT_EQ(ASCIIToUTF16("Wyatt"), firstname.value().utf16());
1564 EXPECT_TRUE(firstname.suggestedValue().isEmpty()); 1565 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
1565 EXPECT_FALSE(firstname.isAutofilled()); 1566 EXPECT_FALSE(firstname.isAutofilled());
1566 EXPECT_TRUE(lastname.value().isEmpty()); 1567 EXPECT_TRUE(lastname.value().isEmpty());
1567 EXPECT_TRUE(lastname.suggestedValue().isEmpty()); 1568 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
1568 EXPECT_FALSE(lastname.isAutofilled()); 1569 EXPECT_FALSE(lastname.isAutofilled());
1569 EXPECT_TRUE(email.value().isEmpty()); 1570 EXPECT_TRUE(email.value().isEmpty());
1570 EXPECT_TRUE(email.suggestedValue().isEmpty()); 1571 EXPECT_TRUE(email.suggestedValue().isEmpty());
1571 EXPECT_FALSE(email.isAutofilled()); 1572 EXPECT_FALSE(email.isAutofilled());
1572 EXPECT_TRUE(phone.value().isEmpty()); 1573 EXPECT_TRUE(phone.value().isEmpty());
1573 EXPECT_TRUE(phone.suggestedValue().isEmpty()); 1574 EXPECT_TRUE(phone.suggestedValue().isEmpty());
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after
1980 { "select", "select-one", "state" }, 1981 { "select", "select-one", "state" },
1981 // <textarea> elements should also behave no differently from text fields. 1982 // <textarea> elements should also behave no differently from text fields.
1982 { "textarea", "textarea", "street-address" }, 1983 { "textarea", "textarea", "street-address" },
1983 // Very long attribute values should be replaced by a default string, to 1984 // Very long attribute values should be replaced by a default string, to
1984 // prevent malicious websites from DOSing the browser process. 1985 // prevent malicious websites from DOSing the browser process.
1985 { "malicious", "text", "x-max-data-length-exceeded" }, 1986 { "malicious", "text", "x-max-data-length-exceeded" },
1986 }; 1987 };
1987 1988
1988 WebDocument document = frame->document(); 1989 WebDocument document = frame->document();
1989 for (size_t i = 0; i < arraysize(test_cases); ++i) { 1990 for (size_t i = 0; i < arraysize(test_cases); ++i) {
1990 WebFormControlElement element = 1991 WebFormControlElement element = GetFormControlElementById(
1991 GetFormControlElementById(ASCIIToUTF16(test_cases[i].element_id)); 1992 WebString::fromASCII(test_cases[i].element_id));
1992 FormFieldData result; 1993 FormFieldData result;
1993 WebFormControlElementToFormField(element, nullptr, EXTRACT_NONE, &result); 1994 WebFormControlElementToFormField(element, nullptr, EXTRACT_NONE, &result);
1994 1995
1995 FormFieldData expected; 1996 FormFieldData expected;
1996 expected.name = ASCIIToUTF16(test_cases[i].element_id); 1997 expected.name = ASCIIToUTF16(test_cases[i].element_id);
1997 expected.form_control_type = test_cases[i].form_control_type; 1998 expected.form_control_type = test_cases[i].form_control_type;
1998 expected.autocomplete_attribute = test_cases[i].autocomplete_attribute; 1999 expected.autocomplete_attribute = test_cases[i].autocomplete_attribute;
1999 if (test_cases[i].form_control_type == "text") 2000 if (test_cases[i].form_control_type == "text")
2000 expected.max_length = WebInputElement::defaultMaxLength(); 2001 expected.max_length = WebInputElement::defaultMaxLength();
2001 else 2002 else
(...skipping 2658 matching lines...) Expand 10 before | Expand all | Expand 10 after
4660 4661
4661 if (test_case.has_extracted_form) { 4662 if (test_case.has_extracted_form) {
4662 EXPECT_EQ(test_case.is_form_tag, forms[0].is_form_tag); 4663 EXPECT_EQ(test_case.is_form_tag, forms[0].is_form_tag);
4663 EXPECT_EQ(test_case.is_formless_checkout, forms[0].is_formless_checkout); 4664 EXPECT_EQ(test_case.is_formless_checkout, forms[0].is_formless_checkout);
4664 } 4665 }
4665 } 4666 }
4666 } 4667 }
4667 4668
4668 } // namespace form_util 4669 } // namespace form_util
4669 } // namespace autofill 4670 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698