OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/utf_string_conversions.h" | |
6 #include "chrome/common/autofill_messages.h" | |
7 #include "chrome/test/base/chrome_render_view_test.h" | |
8 #include "testing/gtest/include/gtest/gtest.h" | |
9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | |
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h" | |
11 #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 | |
15 using WebKit::WebDocument; | |
16 using WebKit::WebFrame; | |
17 using WebKit::WebInputElement; | |
18 using WebKit::WebString; | |
19 using webkit::forms::FormData; | |
20 using webkit::forms::FormField; | |
21 | |
22 namespace autofill { | |
23 | |
24 TEST_F(ChromeRenderViewTest, SendForms) { | |
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 | |
27 // loop, it will generate an update. | |
28 SendContentStateImmediately(); | |
29 | |
30 LoadHTML("<form method=\"POST\">" | |
31 " <input type=\"text\" id=\"firstname\"/>" | |
32 " <input type=\"text\" id=\"middlename\"/>" | |
33 " <input type=\"text\" id=\"lastname\" autoComplete=\"off\"/>" | |
34 " <input type=\"hidden\" id=\"email\"/>" | |
35 " <select id=\"state\"/>" | |
36 " <option>?</option>" | |
37 " <option>California</option>" | |
38 " <option>Texas</option>" | |
39 " </select>" | |
40 "</form>"); | |
41 | |
42 // Verify that "FormsSeen" sends the expected number of fields. | |
43 const IPC::Message* message = render_thread_->sink().GetFirstMessageMatching( | |
44 AutofillHostMsg_FormsSeen::ID); | |
45 ASSERT_NE(static_cast<IPC::Message*>(NULL), message); | |
46 AutofillHostMsg_FormsSeen::Param params; | |
47 AutofillHostMsg_FormsSeen::Read(message, ¶ms); | |
48 const std::vector<FormData>& forms = params.a; | |
49 ASSERT_EQ(1UL, forms.size()); | |
50 ASSERT_EQ(4UL, forms[0].fields.size()); | |
51 | |
52 FormField expected; | |
53 | |
54 expected.name = ASCIIToUTF16("firstname"); | |
55 expected.value = string16(); | |
56 expected.form_control_type = ASCIIToUTF16("text"); | |
57 expected.max_length = WebInputElement::defaultMaxLength(); | |
58 EXPECT_FORM_FIELD_EQUALS(expected, forms[0].fields[0]); | |
59 | |
60 expected.name = ASCIIToUTF16("middlename"); | |
61 expected.value = string16(); | |
62 expected.form_control_type = ASCIIToUTF16("text"); | |
63 expected.max_length = WebInputElement::defaultMaxLength(); | |
64 EXPECT_FORM_FIELD_EQUALS(expected, forms[0].fields[1]); | |
65 | |
66 expected.name = ASCIIToUTF16("lastname"); | |
67 expected.value = string16(); | |
68 expected.form_control_type = ASCIIToUTF16("text"); | |
69 expected.max_length = WebInputElement::defaultMaxLength(); | |
70 EXPECT_FORM_FIELD_EQUALS(expected, forms[0].fields[2]); | |
71 | |
72 expected.name = ASCIIToUTF16("state"); | |
73 expected.value = ASCIIToUTF16("?"); | |
74 expected.form_control_type = ASCIIToUTF16("select-one"); | |
75 expected.max_length = 0; | |
76 EXPECT_FORM_FIELD_EQUALS(expected, forms[0].fields[3]); | |
77 | |
78 // Verify that |didAcceptAutofillSuggestion()| sends the expected number of | |
79 // fields. | |
80 WebFrame* web_frame = GetMainFrame(); | |
81 WebDocument document = web_frame->document(); | |
82 WebInputElement firstname = | |
83 document.getElementById("firstname").to<WebInputElement>(); | |
84 | |
85 // Make sure to query for Autofill suggestions before selecting one. | |
86 autofill_agent_->element_ = firstname; | |
87 autofill_agent_->QueryAutofillSuggestions(firstname, false); | |
88 | |
89 // Accept suggestion that contains a label. Labeled items indicate Autofill | |
90 // as opposed to Autocomplete. We're testing this distinction below with | |
91 // the |AutofillHostMsg_FillAutofillFormData::ID| message. | |
92 autofill_agent_->didAcceptAutofillSuggestion( | |
93 firstname, | |
94 WebKit::WebString::fromUTF8("Johnny"), | |
95 WebKit::WebString::fromUTF8("Home"), | |
96 1, | |
97 -1); | |
98 | |
99 ProcessPendingMessages(); | |
100 const IPC::Message* message2 = | |
101 render_thread_->sink().GetUniqueMessageMatching( | |
102 AutofillHostMsg_FillAutofillFormData::ID); | |
103 ASSERT_NE(static_cast<IPC::Message*>(NULL), message2); | |
104 AutofillHostMsg_FillAutofillFormData::Param params2; | |
105 AutofillHostMsg_FillAutofillFormData::Read(message2, ¶ms2); | |
106 const FormData& form2 = params2.b; | |
107 ASSERT_EQ(3UL, form2.fields.size()); | |
108 | |
109 expected.name = ASCIIToUTF16("firstname"); | |
110 expected.value = string16(); | |
111 expected.form_control_type = ASCIIToUTF16("text"); | |
112 expected.max_length = WebInputElement::defaultMaxLength(); | |
113 EXPECT_FORM_FIELD_EQUALS(expected, form2.fields[0]); | |
114 | |
115 expected.name = ASCIIToUTF16("middlename"); | |
116 expected.value = string16(); | |
117 expected.form_control_type = ASCIIToUTF16("text"); | |
118 expected.max_length = WebInputElement::defaultMaxLength(); | |
119 EXPECT_FORM_FIELD_EQUALS(expected, form2.fields[1]); | |
120 | |
121 expected.name = ASCIIToUTF16("state"); | |
122 expected.value = ASCIIToUTF16("?"); | |
123 expected.form_control_type = ASCIIToUTF16("select-one"); | |
124 expected.max_length = 0; | |
125 EXPECT_FORM_FIELD_EQUALS(expected, form2.fields[2]); | |
126 } | |
127 | |
128 TEST_F(ChromeRenderViewTest, FillFormElement) { | |
129 // 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 | |
131 // loop, it will generate an update. | |
132 SendContentStateImmediately(); | |
133 | |
134 LoadHTML("<form method=\"POST\">" | |
135 " <input type=\"text\" id=\"firstname\"/>" | |
136 " <input type=\"text\" id=\"middlename\"/>" | |
137 "</form>"); | |
138 | |
139 // Verify that "FormsSeen" isn't sent, as there are too few fields. | |
140 const IPC::Message* message = render_thread_->sink().GetFirstMessageMatching( | |
141 AutofillHostMsg_FormsSeen::ID); | |
142 ASSERT_EQ(static_cast<IPC::Message*>(NULL), message); | |
143 | |
144 // Verify that |didAcceptAutofillSuggestion()| sets the value of the expected | |
145 // field. | |
146 WebFrame* web_frame = GetMainFrame(); | |
147 WebDocument document = web_frame->document(); | |
148 WebInputElement firstname = | |
149 document.getElementById("firstname").to<WebInputElement>(); | |
150 WebInputElement middlename = | |
151 document.getElementById("middlename").to<WebInputElement>(); | |
152 middlename.setAutofilled(true); | |
153 | |
154 // Make sure to query for Autofill suggestions before selecting one. | |
155 autofill_agent_->element_ = firstname; | |
156 autofill_agent_->QueryAutofillSuggestions(firstname, false); | |
157 | |
158 // Accept a suggestion in a form that has been auto-filled. This triggers | |
159 // the direct filling of the firstname element with value parameter. | |
160 autofill_agent_->didAcceptAutofillSuggestion(firstname, | |
161 WebString::fromUTF8("David"), | |
162 WebString(), | |
163 0, | |
164 0); | |
165 | |
166 ProcessPendingMessages(); | |
167 const IPC::Message* message2 = | |
168 render_thread_->sink().GetUniqueMessageMatching( | |
169 AutofillHostMsg_FillAutofillFormData::ID); | |
170 | |
171 // No message should be sent in this case. |firstname| is filled directly. | |
172 ASSERT_EQ(static_cast<IPC::Message*>(NULL), message2); | |
173 EXPECT_EQ(firstname.value(), WebKit::WebString::fromUTF8("David")); | |
174 } | |
175 | |
176 } // namespace autofill | |
OLD | NEW |