Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / renderer / autofill / form_autofill_browsertest.cc
index e12acc4..e0a1be9 100644 (file)
@@ -13,6 +13,7 @@
 #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"
@@ -24,6 +25,7 @@
 #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"
@@ -159,7 +161,7 @@ class FormAutofillTest : public ChromeRenderViewTest {
   }
 
   typedef void (*FillFormFunction)(const FormData& form,
-                                   const WebInputElement& element);
+                                   const WebFormControlElement& element);
 
   typedef WebString (*GetValueFunction)(WebFormControlElement element);
 
@@ -187,10 +189,10 @@ class FormAutofillTest : public ChromeRenderViewTest {
     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);
@@ -232,9 +234,8 @@ class FormAutofillTest : public ChromeRenderViewTest {
     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" ||
@@ -258,7 +259,7 @@ class FormAutofillTest : public ChromeRenderViewTest {
 
   static void FillFormIncludingNonFocusableElementsWrapper(
       const FormData& form,
-      const WebInputElement& element) {
+      const WebFormControlElement& element) {
     FillFormIncludingNonFocusableElements(form, element.form());
   }
 
@@ -266,6 +267,9 @@ class FormAutofillTest : public ChromeRenderViewTest {
     if (element.formControlType() == "textarea")
       return element.to<WebTextAreaElement>().value();
 
+    if (element.formControlType() == "select-one")
+      return element.to<WebSelectElement>().value();
+
     return element.to<WebInputElement>().value();
   }
 
@@ -273,6 +277,9 @@ class FormAutofillTest : public ChromeRenderViewTest {
     if (element.formControlType() == "textarea")
       return element.to<WebTextAreaElement>().suggestedValue();
 
+    if (element.formControlType() == "select-one")
+      return element.to<WebSelectElement>().suggestedValue();
+
     return element.to<WebInputElement>().suggestedValue();
   }
 
@@ -456,6 +463,29 @@ TEST_F(FormAutofillTest, WebFormControlElementToFormFieldSelect) {
   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\">"
@@ -1002,7 +1032,7 @@ TEST_F(FormAutofillTest, WebFormElementToFormDataAutocomplete) {
   }
 }
 
-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\"/>"
@@ -1027,8 +1057,10 @@ TEST_F(FormAutofillTest, FindForm) {
   // 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);
@@ -1062,8 +1094,11 @@ TEST_F(FormAutofillTest, FindForm) {
   // 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);
@@ -1088,6 +1123,109 @@ TEST_F(FormAutofillTest, FindForm) {
   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.&#10;"
+               "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[] = {
@@ -1201,14 +1339,15 @@ TEST_F(FormAutofillTest, PreviewForm) {
       // 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"},
@@ -2109,8 +2248,10 @@ TEST_F(FormAutofillTest, FillFormMaxLength) {
   // 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);
@@ -2145,8 +2286,10 @@ TEST_F(FormAutofillTest, FillFormMaxLength) {
   // 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);
@@ -2202,8 +2345,10 @@ TEST_F(FormAutofillTest, FillFormNegativeMaxLength) {
   // 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);
@@ -2233,8 +2378,10 @@ TEST_F(FormAutofillTest, FillFormNegativeMaxLength) {
   // 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);
@@ -2279,8 +2426,10 @@ TEST_F(FormAutofillTest, FillFormEmptyName) {
   // 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);
@@ -2310,8 +2459,10 @@ TEST_F(FormAutofillTest, FillFormEmptyName) {
   // 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);
@@ -2365,8 +2516,10 @@ TEST_F(FormAutofillTest, FillFormEmptyFormNames) {
   // 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);
@@ -2399,8 +2552,10 @@ TEST_F(FormAutofillTest, FillFormEmptyFormNames) {
   // 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);
@@ -2584,8 +2739,10 @@ TEST_F(FormAutofillTest, FillFormNonEmptyField) {
   // 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);
@@ -2626,8 +2783,10 @@ TEST_F(FormAutofillTest, FillFormNonEmptyField) {
   // 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);
@@ -2701,8 +2860,10 @@ TEST_F(FormAutofillTest, ClearFormWithNode) {
   // 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);
@@ -2803,8 +2964,10 @@ TEST_F(FormAutofillTest, ClearFormWithNodeContainingSelectOne) {
   // 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);