| 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 "base/utf_string_conversions.h" | 5 #include "base/utf_string_conversions.h" |
| 6 #include "chrome/common/autofill_messages.h" | 6 #include "chrome/common/autofill_messages.h" |
| 7 #include "chrome/common/form_data.h" |
| 8 #include "chrome/common/form_field_data.h" |
| 7 #include "chrome/test/base/chrome_render_view_test.h" | 9 #include "chrome/test/base/chrome_render_view_test.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h" | 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h" |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
| 12 #include "webkit/forms/form_data.h" | |
| 13 #include "webkit/forms/form_field.h" | |
| 14 | 14 |
| 15 using WebKit::WebDocument; | 15 using WebKit::WebDocument; |
| 16 using WebKit::WebFrame; | 16 using WebKit::WebFrame; |
| 17 using WebKit::WebInputElement; | 17 using WebKit::WebInputElement; |
| 18 using WebKit::WebString; | 18 using WebKit::WebString; |
| 19 using webkit::forms::FormData; | |
| 20 using webkit::forms::FormField; | |
| 21 | 19 |
| 22 namespace autofill { | 20 namespace autofill { |
| 23 | 21 |
| 24 TEST_F(ChromeRenderViewTest, SendForms) { | 22 TEST_F(ChromeRenderViewTest, SendForms) { |
| 25 // Don't want any delay for form state sync changes. This will still post a | 23 // Don't want any delay for form state sync changes. This will still post a |
| 26 // message so updates will get coalesced, but as soon as we spin the message | 24 // message so updates will get coalesced, but as soon as we spin the message |
| 27 // loop, it will generate an update. | 25 // loop, it will generate an update. |
| 28 SendContentStateImmediately(); | 26 SendContentStateImmediately(); |
| 29 | 27 |
| 30 LoadHTML("<form method=\"POST\">" | 28 LoadHTML("<form method=\"POST\">" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 42 // Verify that "FormsSeen" sends the expected number of fields. | 40 // Verify that "FormsSeen" sends the expected number of fields. |
| 43 const IPC::Message* message = render_thread_->sink().GetFirstMessageMatching( | 41 const IPC::Message* message = render_thread_->sink().GetFirstMessageMatching( |
| 44 AutofillHostMsg_FormsSeen::ID); | 42 AutofillHostMsg_FormsSeen::ID); |
| 45 ASSERT_NE(static_cast<IPC::Message*>(NULL), message); | 43 ASSERT_NE(static_cast<IPC::Message*>(NULL), message); |
| 46 AutofillHostMsg_FormsSeen::Param params; | 44 AutofillHostMsg_FormsSeen::Param params; |
| 47 AutofillHostMsg_FormsSeen::Read(message, ¶ms); | 45 AutofillHostMsg_FormsSeen::Read(message, ¶ms); |
| 48 const std::vector<FormData>& forms = params.a; | 46 const std::vector<FormData>& forms = params.a; |
| 49 ASSERT_EQ(1UL, forms.size()); | 47 ASSERT_EQ(1UL, forms.size()); |
| 50 ASSERT_EQ(4UL, forms[0].fields.size()); | 48 ASSERT_EQ(4UL, forms[0].fields.size()); |
| 51 | 49 |
| 52 FormField expected; | 50 FormFieldData expected; |
| 53 | 51 |
| 54 expected.name = ASCIIToUTF16("firstname"); | 52 expected.name = ASCIIToUTF16("firstname"); |
| 55 expected.value = string16(); | 53 expected.value = string16(); |
| 56 expected.form_control_type = ASCIIToUTF16("text"); | 54 expected.form_control_type = ASCIIToUTF16("text"); |
| 57 expected.max_length = WebInputElement::defaultMaxLength(); | 55 expected.max_length = WebInputElement::defaultMaxLength(); |
| 58 EXPECT_FORM_FIELD_EQUALS(expected, forms[0].fields[0]); | 56 EXPECT_FORM_FIELD_DATA_EQUALS(expected, forms[0].fields[0]); |
| 59 | 57 |
| 60 expected.name = ASCIIToUTF16("middlename"); | 58 expected.name = ASCIIToUTF16("middlename"); |
| 61 expected.value = string16(); | 59 expected.value = string16(); |
| 62 expected.form_control_type = ASCIIToUTF16("text"); | 60 expected.form_control_type = ASCIIToUTF16("text"); |
| 63 expected.max_length = WebInputElement::defaultMaxLength(); | 61 expected.max_length = WebInputElement::defaultMaxLength(); |
| 64 EXPECT_FORM_FIELD_EQUALS(expected, forms[0].fields[1]); | 62 EXPECT_FORM_FIELD_DATA_EQUALS(expected, forms[0].fields[1]); |
| 65 | 63 |
| 66 expected.name = ASCIIToUTF16("lastname"); | 64 expected.name = ASCIIToUTF16("lastname"); |
| 67 expected.value = string16(); | 65 expected.value = string16(); |
| 68 expected.form_control_type = ASCIIToUTF16("text"); | 66 expected.form_control_type = ASCIIToUTF16("text"); |
| 69 expected.max_length = WebInputElement::defaultMaxLength(); | 67 expected.max_length = WebInputElement::defaultMaxLength(); |
| 70 EXPECT_FORM_FIELD_EQUALS(expected, forms[0].fields[2]); | 68 EXPECT_FORM_FIELD_DATA_EQUALS(expected, forms[0].fields[2]); |
| 71 | 69 |
| 72 expected.name = ASCIIToUTF16("state"); | 70 expected.name = ASCIIToUTF16("state"); |
| 73 expected.value = ASCIIToUTF16("?"); | 71 expected.value = ASCIIToUTF16("?"); |
| 74 expected.form_control_type = ASCIIToUTF16("select-one"); | 72 expected.form_control_type = ASCIIToUTF16("select-one"); |
| 75 expected.max_length = 0; | 73 expected.max_length = 0; |
| 76 EXPECT_FORM_FIELD_EQUALS(expected, forms[0].fields[3]); | 74 EXPECT_FORM_FIELD_DATA_EQUALS(expected, forms[0].fields[3]); |
| 77 | 75 |
| 78 // Verify that |didAcceptAutofillSuggestion()| sends the expected number of | 76 // Verify that |didAcceptAutofillSuggestion()| sends the expected number of |
| 79 // fields. | 77 // fields. |
| 80 WebFrame* web_frame = GetMainFrame(); | 78 WebFrame* web_frame = GetMainFrame(); |
| 81 WebDocument document = web_frame->document(); | 79 WebDocument document = web_frame->document(); |
| 82 WebInputElement firstname = | 80 WebInputElement firstname = |
| 83 document.getElementById("firstname").to<WebInputElement>(); | 81 document.getElementById("firstname").to<WebInputElement>(); |
| 84 | 82 |
| 85 // Make sure to query for Autofill suggestions before selecting one. | 83 // Make sure to query for Autofill suggestions before selecting one. |
| 86 autofill_agent_->element_ = firstname; | 84 autofill_agent_->element_ = firstname; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 103 ASSERT_NE(static_cast<IPC::Message*>(NULL), message2); | 101 ASSERT_NE(static_cast<IPC::Message*>(NULL), message2); |
| 104 AutofillHostMsg_FillAutofillFormData::Param params2; | 102 AutofillHostMsg_FillAutofillFormData::Param params2; |
| 105 AutofillHostMsg_FillAutofillFormData::Read(message2, ¶ms2); | 103 AutofillHostMsg_FillAutofillFormData::Read(message2, ¶ms2); |
| 106 const FormData& form2 = params2.b; | 104 const FormData& form2 = params2.b; |
| 107 ASSERT_EQ(3UL, form2.fields.size()); | 105 ASSERT_EQ(3UL, form2.fields.size()); |
| 108 | 106 |
| 109 expected.name = ASCIIToUTF16("firstname"); | 107 expected.name = ASCIIToUTF16("firstname"); |
| 110 expected.value = string16(); | 108 expected.value = string16(); |
| 111 expected.form_control_type = ASCIIToUTF16("text"); | 109 expected.form_control_type = ASCIIToUTF16("text"); |
| 112 expected.max_length = WebInputElement::defaultMaxLength(); | 110 expected.max_length = WebInputElement::defaultMaxLength(); |
| 113 EXPECT_FORM_FIELD_EQUALS(expected, form2.fields[0]); | 111 EXPECT_FORM_FIELD_DATA_EQUALS(expected, form2.fields[0]); |
| 114 | 112 |
| 115 expected.name = ASCIIToUTF16("middlename"); | 113 expected.name = ASCIIToUTF16("middlename"); |
| 116 expected.value = string16(); | 114 expected.value = string16(); |
| 117 expected.form_control_type = ASCIIToUTF16("text"); | 115 expected.form_control_type = ASCIIToUTF16("text"); |
| 118 expected.max_length = WebInputElement::defaultMaxLength(); | 116 expected.max_length = WebInputElement::defaultMaxLength(); |
| 119 EXPECT_FORM_FIELD_EQUALS(expected, form2.fields[1]); | 117 EXPECT_FORM_FIELD_DATA_EQUALS(expected, form2.fields[1]); |
| 120 | 118 |
| 121 expected.name = ASCIIToUTF16("state"); | 119 expected.name = ASCIIToUTF16("state"); |
| 122 expected.value = ASCIIToUTF16("?"); | 120 expected.value = ASCIIToUTF16("?"); |
| 123 expected.form_control_type = ASCIIToUTF16("select-one"); | 121 expected.form_control_type = ASCIIToUTF16("select-one"); |
| 124 expected.max_length = 0; | 122 expected.max_length = 0; |
| 125 EXPECT_FORM_FIELD_EQUALS(expected, form2.fields[2]); | 123 EXPECT_FORM_FIELD_DATA_EQUALS(expected, form2.fields[2]); |
| 126 } | 124 } |
| 127 | 125 |
| 128 TEST_F(ChromeRenderViewTest, FillFormElement) { | 126 TEST_F(ChromeRenderViewTest, FillFormElement) { |
| 129 // Don't want any delay for form state sync changes. This will still post a | 127 // Don't want any delay for form state sync changes. This will still post a |
| 130 // message so updates will get coalesced, but as soon as we spin the message | 128 // message so updates will get coalesced, but as soon as we spin the message |
| 131 // loop, it will generate an update. | 129 // loop, it will generate an update. |
| 132 SendContentStateImmediately(); | 130 SendContentStateImmediately(); |
| 133 | 131 |
| 134 LoadHTML("<form method=\"POST\">" | 132 LoadHTML("<form method=\"POST\">" |
| 135 " <input type=\"text\" id=\"firstname\"/>" | 133 " <input type=\"text\" id=\"firstname\"/>" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 const IPC::Message* message2 = | 165 const IPC::Message* message2 = |
| 168 render_thread_->sink().GetUniqueMessageMatching( | 166 render_thread_->sink().GetUniqueMessageMatching( |
| 169 AutofillHostMsg_FillAutofillFormData::ID); | 167 AutofillHostMsg_FillAutofillFormData::ID); |
| 170 | 168 |
| 171 // No message should be sent in this case. |firstname| is filled directly. | 169 // No message should be sent in this case. |firstname| is filled directly. |
| 172 ASSERT_EQ(static_cast<IPC::Message*>(NULL), message2); | 170 ASSERT_EQ(static_cast<IPC::Message*>(NULL), message2); |
| 173 EXPECT_EQ(firstname.value(), WebKit::WebString::fromUTF8("David")); | 171 EXPECT_EQ(firstname.value(), WebKit::WebString::fromUTF8("David")); |
| 174 } | 172 } |
| 175 | 173 |
| 176 } // namespace autofill | 174 } // namespace autofill |
| OLD | NEW |