| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/test/base/render_view_test.h" | 7 #include "chrome/test/base/chrome_render_view_test.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h" | 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h" |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" | 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" |
| 12 #include "webkit/glue/form_data.h" | 12 #include "webkit/glue/form_data.h" |
| 13 #include "webkit/glue/form_field.h" | 13 #include "webkit/glue/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_glue::FormData; | 19 using webkit_glue::FormData; |
| 20 using webkit_glue::FormField; | 20 using webkit_glue::FormField; |
| 21 | 21 |
| 22 namespace autofill { | 22 namespace autofill { |
| 23 | 23 |
| 24 TEST_F(RenderViewTest, SendForms) { | 24 TEST_F(ChromeRenderViewTest, SendForms) { |
| 25 // Don't want any delay for form state sync changes. This will still post a | 25 // 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 | 26 // message so updates will get coalesced, but as soon as we spin the message |
| 27 // loop, it will generate an update. | 27 // loop, it will generate an update. |
| 28 SendContentStateImmediately(); | 28 SendContentStateImmediately(); |
| 29 | 29 |
| 30 LoadHTML("<form method=\"POST\">" | 30 LoadHTML("<form method=\"POST\">" |
| 31 " <input type=\"text\" id=\"firstname\"/>" | 31 " <input type=\"text\" id=\"firstname\"/>" |
| 32 " <input type=\"text\" id=\"middlename\"/>" | 32 " <input type=\"text\" id=\"middlename\"/>" |
| 33 " <input type=\"text\" id=\"lastname\" autoComplete=\"off\"/>" | 33 " <input type=\"text\" id=\"lastname\" autoComplete=\"off\"/>" |
| 34 " <input type=\"hidden\" id=\"email\"/>" | 34 " <input type=\"hidden\" id=\"email\"/>" |
| 35 " <select id=\"state\"/>" | 35 " <select id=\"state\"/>" |
| 36 " <option>?</option>" | 36 " <option>?</option>" |
| 37 " <option>California</option>" | 37 " <option>California</option>" |
| 38 " <option>Texas</option>" | 38 " <option>Texas</option>" |
| 39 " </select>" | 39 " </select>" |
| 40 "</form>"); | 40 "</form>"); |
| 41 | 41 |
| 42 // Verify that "FormsSeen" sends the expected number of fields. | 42 // Verify that "FormsSeen" sends the expected number of fields. |
| 43 ProcessPendingMessages(); | 43 ProcessPendingMessages(); |
| 44 const IPC::Message* message = render_thread_.sink().GetFirstMessageMatching( | 44 const IPC::Message* message = render_thread_->sink().GetFirstMessageMatching( |
| 45 AutofillHostMsg_FormsSeen::ID); | 45 AutofillHostMsg_FormsSeen::ID); |
| 46 ASSERT_NE(static_cast<IPC::Message*>(NULL), message); | 46 ASSERT_NE(static_cast<IPC::Message*>(NULL), message); |
| 47 AutofillHostMsg_FormsSeen::Param params; | 47 AutofillHostMsg_FormsSeen::Param params; |
| 48 AutofillHostMsg_FormsSeen::Read(message, ¶ms); | 48 AutofillHostMsg_FormsSeen::Read(message, ¶ms); |
| 49 const std::vector<FormData>& forms = params.a; | 49 const std::vector<FormData>& forms = params.a; |
| 50 ASSERT_EQ(1UL, forms.size()); | 50 ASSERT_EQ(1UL, forms.size()); |
| 51 ASSERT_EQ(4UL, forms[0].fields.size()); | 51 ASSERT_EQ(4UL, forms[0].fields.size()); |
| 52 | 52 |
| 53 FormField expected; | 53 FormField expected; |
| 54 | 54 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 // as opposed to Autocomplete. We're testing this distinction below with | 87 // as opposed to Autocomplete. We're testing this distinction below with |
| 88 // the |AutofillHostMsg_FillAutofillFormData::ID| message. | 88 // the |AutofillHostMsg_FillAutofillFormData::ID| message. |
| 89 autofill_agent_->didAcceptAutofillSuggestion( | 89 autofill_agent_->didAcceptAutofillSuggestion( |
| 90 firstname, | 90 firstname, |
| 91 WebKit::WebString::fromUTF8("Johnny"), | 91 WebKit::WebString::fromUTF8("Johnny"), |
| 92 WebKit::WebString::fromUTF8("Home"), | 92 WebKit::WebString::fromUTF8("Home"), |
| 93 1, | 93 1, |
| 94 -1); | 94 -1); |
| 95 | 95 |
| 96 ProcessPendingMessages(); | 96 ProcessPendingMessages(); |
| 97 const IPC::Message* message2 = render_thread_.sink().GetUniqueMessageMatching( | 97 const IPC::Message* message2 = |
| 98 AutofillHostMsg_FillAutofillFormData::ID); | 98 render_thread_->sink().GetUniqueMessageMatching( |
| 99 AutofillHostMsg_FillAutofillFormData::ID); |
| 99 ASSERT_NE(static_cast<IPC::Message*>(NULL), message2); | 100 ASSERT_NE(static_cast<IPC::Message*>(NULL), message2); |
| 100 AutofillHostMsg_FillAutofillFormData::Param params2; | 101 AutofillHostMsg_FillAutofillFormData::Param params2; |
| 101 AutofillHostMsg_FillAutofillFormData::Read(message2, ¶ms2); | 102 AutofillHostMsg_FillAutofillFormData::Read(message2, ¶ms2); |
| 102 const FormData& form2 = params2.b; | 103 const FormData& form2 = params2.b; |
| 103 ASSERT_EQ(3UL, form2.fields.size()); | 104 ASSERT_EQ(3UL, form2.fields.size()); |
| 104 | 105 |
| 105 expected.name = ASCIIToUTF16("firstname"); | 106 expected.name = ASCIIToUTF16("firstname"); |
| 106 expected.value = string16(); | 107 expected.value = string16(); |
| 107 expected.form_control_type = ASCIIToUTF16("text"); | 108 expected.form_control_type = ASCIIToUTF16("text"); |
| 108 expected.max_length = WebInputElement::defaultMaxLength(); | 109 expected.max_length = WebInputElement::defaultMaxLength(); |
| 109 EXPECT_FORM_FIELD_EQUALS(expected, form2.fields[0]); | 110 EXPECT_FORM_FIELD_EQUALS(expected, form2.fields[0]); |
| 110 | 111 |
| 111 expected.name = ASCIIToUTF16("middlename"); | 112 expected.name = ASCIIToUTF16("middlename"); |
| 112 expected.value = string16(); | 113 expected.value = string16(); |
| 113 expected.form_control_type = ASCIIToUTF16("text"); | 114 expected.form_control_type = ASCIIToUTF16("text"); |
| 114 expected.max_length = WebInputElement::defaultMaxLength(); | 115 expected.max_length = WebInputElement::defaultMaxLength(); |
| 115 EXPECT_FORM_FIELD_EQUALS(expected, form2.fields[1]); | 116 EXPECT_FORM_FIELD_EQUALS(expected, form2.fields[1]); |
| 116 | 117 |
| 117 expected.name = ASCIIToUTF16("state"); | 118 expected.name = ASCIIToUTF16("state"); |
| 118 expected.value = ASCIIToUTF16("?"); | 119 expected.value = ASCIIToUTF16("?"); |
| 119 expected.form_control_type = ASCIIToUTF16("select-one"); | 120 expected.form_control_type = ASCIIToUTF16("select-one"); |
| 120 expected.max_length = 0; | 121 expected.max_length = 0; |
| 121 EXPECT_FORM_FIELD_EQUALS(expected, form2.fields[2]); | 122 EXPECT_FORM_FIELD_EQUALS(expected, form2.fields[2]); |
| 122 } | 123 } |
| 123 | 124 |
| 124 TEST_F(RenderViewTest, FillFormElement) { | 125 TEST_F(ChromeRenderViewTest, FillFormElement) { |
| 125 // Don't want any delay for form state sync changes. This will still post a | 126 // Don't want any delay for form state sync changes. This will still post a |
| 126 // message so updates will get coalesced, but as soon as we spin the message | 127 // message so updates will get coalesced, but as soon as we spin the message |
| 127 // loop, it will generate an update. | 128 // loop, it will generate an update. |
| 128 SendContentStateImmediately(); | 129 SendContentStateImmediately(); |
| 129 | 130 |
| 130 LoadHTML("<form method=\"POST\">" | 131 LoadHTML("<form method=\"POST\">" |
| 131 " <input type=\"text\" id=\"firstname\"/>" | 132 " <input type=\"text\" id=\"firstname\"/>" |
| 132 " <input type=\"text\" id=\"middlename\"/>" | 133 " <input type=\"text\" id=\"middlename\"/>" |
| 133 "</form>"); | 134 "</form>"); |
| 134 | 135 |
| 135 // Verify that "FormsSeen" isn't sent, as there are too few fields. | 136 // Verify that "FormsSeen" isn't sent, as there are too few fields. |
| 136 ProcessPendingMessages(); | 137 ProcessPendingMessages(); |
| 137 const IPC::Message* message = render_thread_.sink().GetFirstMessageMatching( | 138 const IPC::Message* message = render_thread_->sink().GetFirstMessageMatching( |
| 138 AutofillHostMsg_FormsSeen::ID); | 139 AutofillHostMsg_FormsSeen::ID); |
| 139 ASSERT_EQ(static_cast<IPC::Message*>(NULL), message); | 140 ASSERT_EQ(static_cast<IPC::Message*>(NULL), message); |
| 140 | 141 |
| 141 // Verify that |didAcceptAutofillSuggestion()| sets the value of the expected | 142 // Verify that |didAcceptAutofillSuggestion()| sets the value of the expected |
| 142 // field. | 143 // field. |
| 143 WebFrame* web_frame = GetMainFrame(); | 144 WebFrame* web_frame = GetMainFrame(); |
| 144 WebDocument document = web_frame->document(); | 145 WebDocument document = web_frame->document(); |
| 145 WebInputElement firstname = | 146 WebInputElement firstname = |
| 146 document.getElementById("firstname").to<WebInputElement>(); | 147 document.getElementById("firstname").to<WebInputElement>(); |
| 147 WebInputElement middlename = | 148 WebInputElement middlename = |
| 148 document.getElementById("middlename").to<WebInputElement>(); | 149 document.getElementById("middlename").to<WebInputElement>(); |
| 149 middlename.setAutofilled(true); | 150 middlename.setAutofilled(true); |
| 150 | 151 |
| 151 // Accept a suggestion in a form that has been auto-filled. This triggers | 152 // Accept a suggestion in a form that has been auto-filled. This triggers |
| 152 // the direct filling of the firstname element with value parameter. | 153 // the direct filling of the firstname element with value parameter. |
| 153 autofill_agent_->didAcceptAutofillSuggestion(firstname, | 154 autofill_agent_->didAcceptAutofillSuggestion(firstname, |
| 154 WebString::fromUTF8("David"), | 155 WebString::fromUTF8("David"), |
| 155 WebString(), | 156 WebString(), |
| 156 0, | 157 0, |
| 157 0); | 158 0); |
| 158 | 159 |
| 159 ProcessPendingMessages(); | 160 ProcessPendingMessages(); |
| 160 const IPC::Message* message2 = render_thread_.sink().GetUniqueMessageMatching( | 161 const IPC::Message* message2 = |
| 161 AutofillHostMsg_FillAutofillFormData::ID); | 162 render_thread_->sink().GetUniqueMessageMatching( |
| 163 AutofillHostMsg_FillAutofillFormData::ID); |
| 162 | 164 |
| 163 // No message should be sent in this case. |firstname| is filled directly. | 165 // No message should be sent in this case. |firstname| is filled directly. |
| 164 ASSERT_EQ(static_cast<IPC::Message*>(NULL), message2); | 166 ASSERT_EQ(static_cast<IPC::Message*>(NULL), message2); |
| 165 EXPECT_EQ(firstname.value(), WebKit::WebString::fromUTF8("David")); | 167 EXPECT_EQ(firstname.value(), WebKit::WebString::fromUTF8("David")); |
| 166 } | 168 } |
| 167 | 169 |
| 168 } // namespace autofill | 170 } // namespace autofill |
| OLD | NEW |