#include "chrome/test/base/chrome_render_view_test.h"
#include "components/autofill/content/renderer/form_autofill_util.h"
#include "components/autofill/content/renderer/form_cache.h"
+#include "components/autofill/core/common/autofill_data_validation.h"
#include "components/autofill/core/common/form_data.h"
#include "components/autofill/core/common/web_element_descriptor.h"
#include "components/variations/entropy_provider.h"
#include "third_party/WebKit/public/web/WebFormControlElement.h"
#include "third_party/WebKit/public/web/WebFormElement.h"
#include "third_party/WebKit/public/web/WebInputElement.h"
+#include "third_party/WebKit/public/web/WebLocalFrame.h"
#include "third_party/WebKit/public/web/WebNode.h"
#include "third_party/WebKit/public/web/WebSelectElement.h"
#include "third_party/WebKit/public/web/WebTextAreaElement.h"
}
typedef void (*FillFormFunction)(const FormData& form,
- const WebInputElement& element);
+ const WebFormControlElement& element);
typedef WebString (*GetValueFunction)(WebFormControlElement element);
FormData form_data;
FormFieldData field;
EXPECT_TRUE(
- FindFormAndFieldForInputElement(input_element,
- &form_data,
- &field,
- autofill::REQUIRE_AUTOCOMPLETE));
+ FindFormAndFieldForFormControlElement(input_element,
+ &form_data,
+ &field,
+ autofill::REQUIRE_AUTOCOMPLETE));
EXPECT_EQ(ASCIIToUTF16("TestForm"), form_data.name);
EXPECT_EQ(GURL(web_frame->document().url()), form_data.origin);
EXPECT_EQ(GURL("http://buh.com"), form_data.action);
WebString value;
WebFormControlElement element = GetMainFrame()->document().getElementById(
ASCIIToUTF16(field_case.name)).to<WebFormControlElement>();
- if (element.formControlType() == "select-one") {
- value = element.to<WebSelectElement>().value();
- } else if (element.formControlType() == "textarea") {
+ if ((element.formControlType() == "select-one") ||
+ (element.formControlType() == "textarea")) {
value = get_value_function(element);
} else {
ASSERT_TRUE(element.formControlType() == "text" ||
static void FillFormIncludingNonFocusableElementsWrapper(
const FormData& form,
- const WebInputElement& element) {
+ const WebFormControlElement& element) {
FillFormIncludingNonFocusableElements(form, element.form());
}
if (element.formControlType() == "textarea")
return element.to<WebTextAreaElement>().value();
+ if (element.formControlType() == "select-one")
+ return element.to<WebSelectElement>().value();
+
return element.to<WebInputElement>().value();
}
if (element.formControlType() == "textarea")
return element.to<WebTextAreaElement>().suggestedValue();
+ if (element.formControlType() == "select-one")
+ return element.to<WebSelectElement>().suggestedValue();
+
return element.to<WebInputElement>().suggestedValue();
}
EXPECT_EQ(ASCIIToUTF16("Texas"), result3.option_contents[1]);
}
+// When faced with <select> field with *many* options, we should trim them to a
+// reasonable number.
+TEST_F(FormAutofillTest, WebFormControlElementToFormFieldLongSelect) {
+ std::string html = "<SELECT id=\"element\"/>";
+ for (size_t i = 0; i < 2 * kMaxListSize; ++i) {
+ html += base::StringPrintf("<OPTION value=\"%" PRIuS "\">"
+ "%" PRIuS "</OPTION>", i, i);
+ }
+ html += "</SELECT>";
+ LoadHTML(html.c_str());
+
+ WebFrame* frame = GetMainFrame();
+ ASSERT_TRUE(frame);
+
+ WebElement web_element = frame->document().getElementById("element");
+ WebFormControlElement element = web_element.to<WebFormControlElement>();
+ FormFieldData result;
+ WebFormControlElementToFormField(element, autofill::EXTRACT_OPTIONS, &result);
+
+ EXPECT_EQ(0U, result.option_values.size());
+ EXPECT_EQ(0U, result.option_contents.size());
+}
+
// We should be able to extract a <textarea> field.
TEST_F(FormAutofillTest, WebFormControlElementToFormFieldTextArea) {
LoadHTML("<TEXTAREA id=\"element\">"
}
}
-TEST_F(FormAutofillTest, FindForm) {
+TEST_F(FormAutofillTest, FindFormForInputElement) {
LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
" <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
" <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
// Find the form and verify it's the correct form.
FormData form;
FormFieldData field;
- EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form, &field,
- autofill::REQUIRE_NONE));
+ EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
+ &form,
+ &field,
+ autofill::REQUIRE_NONE));
EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
EXPECT_EQ(GURL("http://buh.com"), form.action);
// Try again, but require autocomplete.
FormData form2;
FormFieldData field2;
- EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form2, &field2,
- autofill::REQUIRE_AUTOCOMPLETE));
+ EXPECT_TRUE(FindFormAndFieldForFormControlElement(
+ input_element,
+ &form2,
+ &field2,
+ autofill::REQUIRE_AUTOCOMPLETE));
EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
EXPECT_EQ(GURL("http://buh.com"), form2.action);
EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
}
+TEST_F(FormAutofillTest, FindFormForTextAreaElement) {
+ LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
+ " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
+ " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
+ " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\""
+ "autocomplete=\"off\" />"
+ " <TEXTAREA id=\"street-address\">"
+ "123 Fantasy Ln. "
+ "Apt. 42"
+ "</TEXTAREA>"
+ " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
+ "</FORM>");
+
+ WebFrame* web_frame = GetMainFrame();
+ ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
+
+ FormCache form_cache;
+ std::vector<FormData> forms;
+ form_cache.ExtractForms(*web_frame, &forms);
+ ASSERT_EQ(1U, forms.size());
+
+ // Get the textarea element we want to find.
+ WebElement element = web_frame->document().getElementById("street-address");
+ WebTextAreaElement textarea_element = element.to<WebTextAreaElement>();
+
+ // Find the form and verify it's the correct form.
+ FormData form;
+ FormFieldData field;
+ EXPECT_TRUE(FindFormAndFieldForFormControlElement(textarea_element,
+ &form,
+ &field,
+ autofill::REQUIRE_NONE));
+ EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
+ EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
+ EXPECT_EQ(GURL("http://buh.com"), form.action);
+
+ const std::vector<FormFieldData>& fields = form.fields;
+ ASSERT_EQ(4U, fields.size());
+
+ FormFieldData expected;
+
+ expected.name = ASCIIToUTF16("firstname");
+ expected.value = ASCIIToUTF16("John");
+ expected.form_control_type = "text";
+ expected.max_length = WebInputElement::defaultMaxLength();
+ EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
+
+ expected.name = ASCIIToUTF16("lastname");
+ expected.value = ASCIIToUTF16("Smith");
+ expected.form_control_type = "text";
+ expected.max_length = WebInputElement::defaultMaxLength();
+ EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
+
+ expected.name = ASCIIToUTF16("email");
+ expected.value = ASCIIToUTF16("john@example.com");
+ expected.autocomplete_attribute = "off";
+ expected.form_control_type = "text";
+ expected.max_length = WebInputElement::defaultMaxLength();
+ EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
+ expected.autocomplete_attribute = std::string(); // reset
+
+ expected.name = ASCIIToUTF16("street-address");
+ expected.value = ASCIIToUTF16("123 Fantasy Ln.\nApt. 42");
+ expected.form_control_type = "textarea";
+ expected.max_length = 0;
+ EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
+ EXPECT_FORM_FIELD_DATA_EQUALS(expected, field);
+
+ // Try again, but require autocomplete.
+ FormData form2;
+ FormFieldData field2;
+ EXPECT_TRUE(FindFormAndFieldForFormControlElement(
+ textarea_element,
+ &form2,
+ &field2,
+ autofill::REQUIRE_AUTOCOMPLETE));
+ EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
+ EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
+ EXPECT_EQ(GURL("http://buh.com"), form2.action);
+
+ const std::vector<FormFieldData>& fields2 = form2.fields;
+ ASSERT_EQ(3U, fields2.size());
+
+ expected.name = ASCIIToUTF16("firstname");
+ expected.value = ASCIIToUTF16("John");
+ expected.form_control_type = "text";
+ expected.max_length = WebInputElement::defaultMaxLength();
+ EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
+
+ expected.name = ASCIIToUTF16("lastname");
+ expected.value = ASCIIToUTF16("Smith");
+ expected.form_control_type = "text";
+ expected.max_length = WebInputElement::defaultMaxLength();
+ EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
+
+ expected.name = ASCIIToUTF16("street-address");
+ expected.value = ASCIIToUTF16("123 Fantasy Ln.\nApt. 42");
+ expected.form_control_type = "textarea";
+ expected.max_length = 0;
+ EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
+ EXPECT_FORM_FIELD_DATA_EQUALS(expected, field);
+}
+
// Test regular FillForm function.
TEST_F(FormAutofillTest, FillForm) {
static const AutofillFieldCase field_cases[] = {
// Fields with "display:none" should not previewed.
{"text", "displaynone", "", "", false, "suggested displaynone",
""},
- // Regular <input type="month"> should not be previewed.
- {"month", "month", "", "", false, "2017-11", ""},
+ // Regular <input type="month"> should be previewed.
+ {"month", "month", "", "", true, "2017-11", "2017-11"},
// Non-empty <input type="month"> should not be previewed.
{"month", "month-nonempty", "2011-12", "", false, "2017-11", ""},
- // Regular select fields preview is not yet supported
- {"select-one", "select", "", "", false, "TX", ""},
- // Select fields preview is not yet supported
- {"select-one", "select-nonempty", "CA", "", false, "TX", "CA"},
+ // Regular select fields should be previewed.
+ {"select-one", "select", "", "", true, "TX", "TX"},
+ // Select fields should be previewed even if they already have a
+ // non-empty value.
+ {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"},
// Normal textarea elements should be previewed.
{"textarea", "textarea", "", "", true, "suggested multi-\nline value",
"suggested multi-\nline value"},
// Find the form that contains the input element.
FormData form;
FormFieldData field;
- EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form, &field,
- autofill::REQUIRE_NONE));
+ EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
+ &form,
+ &field,
+ autofill::REQUIRE_NONE));
EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
EXPECT_EQ(GURL("http://buh.com"), form.action);
// Find the newly-filled form that contains the input element.
FormData form2;
FormFieldData field2;
- EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form2, &field2,
- autofill::REQUIRE_NONE));
+ EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
+ &form2,
+ &field2,
+ autofill::REQUIRE_NONE));
EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
// Find the form that contains the input element.
FormData form;
FormFieldData field;
- EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form, &field,
- autofill::REQUIRE_NONE));
+ EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
+ &form,
+ &field,
+ autofill::REQUIRE_NONE));
EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
EXPECT_EQ(GURL("http://buh.com"), form.action);
// Find the newly-filled form that contains the input element.
FormData form2;
FormFieldData field2;
- EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form2, &field2,
- autofill::REQUIRE_NONE));
+ EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
+ &form2,
+ &field2,
+ autofill::REQUIRE_NONE));
EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
// Find the form that contains the input element.
FormData form;
FormFieldData field;
- EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form, &field,
- autofill::REQUIRE_NONE));
+ EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
+ &form,
+ &field,
+ autofill::REQUIRE_NONE));
EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
EXPECT_EQ(GURL("http://buh.com"), form.action);
// Find the newly-filled form that contains the input element.
FormData form2;
FormFieldData field2;
- EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form2, &field2,
- autofill::REQUIRE_NONE));
+ EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
+ &form2,
+ &field2,
+ autofill::REQUIRE_NONE));
EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
// Find the form that contains the input element.
FormData form;
FormFieldData field;
- EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form, &field,
- autofill::REQUIRE_NONE));
+ EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
+ &form,
+ &field,
+ autofill::REQUIRE_NONE));
EXPECT_EQ(base::string16(), form.name);
EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
EXPECT_EQ(GURL("http://abc.com"), form.action);
// Find the newly-filled form that contains the input element.
FormData form2;
FormFieldData field2;
- EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form2, &field2,
- autofill::REQUIRE_NONE));
+ EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
+ &form2,
+ &field2,
+ autofill::REQUIRE_NONE));
EXPECT_EQ(base::string16(), form2.name);
EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
// Find the form that contains the input element.
FormData form;
FormFieldData field;
- EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form, &field,
- autofill::REQUIRE_NONE));
+ EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
+ &form,
+ &field,
+ autofill::REQUIRE_NONE));
EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
EXPECT_EQ(GURL("http://buh.com"), form.action);
// Find the newly-filled form that contains the input element.
FormData form2;
FormFieldData field2;
- EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form2, &field2,
- autofill::REQUIRE_NONE));
+ EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
+ &form2,
+ &field2,
+ autofill::REQUIRE_NONE));
EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
// Verify the form is cleared.
FormData form2;
FormFieldData field2;
- EXPECT_TRUE(FindFormAndFieldForInputElement(firstname, &form2, &field2,
- autofill::REQUIRE_NONE));
+ EXPECT_TRUE(FindFormAndFieldForFormControlElement(firstname,
+ &form2,
+ &field2,
+ autofill::REQUIRE_NONE));
EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
EXPECT_EQ(GURL("http://buh.com"), form2.action);
// Verify the form is cleared.
FormData form2;
FormFieldData field2;
- EXPECT_TRUE(FindFormAndFieldForInputElement(firstname, &form2, &field2,
- autofill::REQUIRE_NONE));
+ EXPECT_TRUE(FindFormAndFieldForFormControlElement(firstname,
+ &form2,
+ &field2,
+ autofill::REQUIRE_NONE));
EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
EXPECT_EQ(GURL("http://buh.com"), form2.action);