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.
5 #include "base/strings/utf_string_conversions.h"
6 #include "chrome/test/base/chrome_render_view_test.h"
7 #include "components/autofill/content/common/autofill_messages.h"
8 #include "components/autofill/content/renderer/autofill_agent.h"
9 #include "components/autofill/core/common/form_data.h"
10 #include "components/autofill/core/common/form_field_data.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/WebKit/public/platform/WebString.h"
13 #include "third_party/WebKit/public/web/WebDocument.h"
14 #include "third_party/WebKit/public/web/WebInputElement.h"
16 using base::ASCIIToUTF16
;
17 using blink::WebDocument
;
18 using blink::WebFrame
;
19 using blink::WebInputElement
;
20 using blink::WebString
;
26 autofill::FormFieldData
,
28 bool> AutofillQueryParam
;
30 TEST_F(ChromeRenderViewTest
, SendForms
) {
31 // Don't want any delay for form state sync changes. This will still post a
32 // message so updates will get coalesced, but as soon as we spin the message
33 // loop, it will generate an update.
34 SendContentStateImmediately();
36 LoadHTML("<form method=\"POST\">"
37 " <input type=\"text\" id=\"firstname\"/>"
38 " <input type=\"text\" id=\"middlename\"/>"
39 " <input type=\"text\" id=\"lastname\" autoComplete=\"off\"/>"
40 " <input type=\"hidden\" id=\"email\"/>"
41 " <select id=\"state\"/>"
43 " <option>California</option>"
44 " <option>Texas</option>"
48 // Verify that "FormsSeen" sends the expected number of fields.
49 const IPC::Message
* message
= render_thread_
->sink().GetFirstMessageMatching(
50 AutofillHostMsg_FormsSeen::ID
);
51 ASSERT_NE(static_cast<IPC::Message
*>(NULL
), message
);
52 AutofillHostMsg_FormsSeen::Param params
;
53 AutofillHostMsg_FormsSeen::Read(message
, ¶ms
);
54 const std::vector
<FormData
>& forms
= params
.a
;
55 ASSERT_EQ(1UL, forms
.size());
56 ASSERT_EQ(4UL, forms
[0].fields
.size());
58 FormFieldData expected
;
60 expected
.name
= ASCIIToUTF16("firstname");
61 expected
.value
= base::string16();
62 expected
.form_control_type
= "text";
63 expected
.max_length
= WebInputElement::defaultMaxLength();
64 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, forms
[0].fields
[0]);
66 expected
.name
= ASCIIToUTF16("middlename");
67 expected
.value
= base::string16();
68 expected
.form_control_type
= "text";
69 expected
.max_length
= WebInputElement::defaultMaxLength();
70 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, forms
[0].fields
[1]);
72 expected
.name
= ASCIIToUTF16("lastname");
73 expected
.value
= base::string16();
74 expected
.form_control_type
= "text";
75 expected
.autocomplete_attribute
= "off";
76 expected
.max_length
= WebInputElement::defaultMaxLength();
77 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, forms
[0].fields
[2]);
78 expected
.autocomplete_attribute
= std::string(); // reset
80 expected
.name
= ASCIIToUTF16("state");
81 expected
.value
= ASCIIToUTF16("?");
82 expected
.form_control_type
= "select-one";
83 expected
.max_length
= 0;
84 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, forms
[0].fields
[3]);
86 // Verify that |didAcceptAutofillSuggestion()| sends the expected number of
88 WebFrame
* web_frame
= GetMainFrame();
89 WebDocument document
= web_frame
->document();
90 WebInputElement firstname
=
91 document
.getElementById("firstname").to
<WebInputElement
>();
93 // Make sure to query for Autofill suggestions before selecting one.
94 autofill_agent_
->element_
= firstname
;
95 autofill_agent_
->QueryAutofillSuggestions(firstname
, false, false);
97 // Fill the form with a suggestion that contained a label. Labeled items
98 // indicate Autofill as opposed to Autocomplete. We're testing this
99 // distinction below with the |AutofillHostMsg_FillAutofillFormData::ID|
101 autofill_agent_
->FillAutofillFormData(
104 AutofillAgent::AUTOFILL_PREVIEW
);
106 ProcessPendingMessages();
107 const IPC::Message
* message2
=
108 render_thread_
->sink().GetUniqueMessageMatching(
109 AutofillHostMsg_FillAutofillFormData::ID
);
110 ASSERT_NE(static_cast<IPC::Message
*>(NULL
), message2
);
111 AutofillHostMsg_FillAutofillFormData::Param params2
;
112 AutofillHostMsg_FillAutofillFormData::Read(message2
, ¶ms2
);
113 const FormData
& form2
= params2
.b
;
114 ASSERT_EQ(3UL, form2
.fields
.size());
116 expected
.name
= ASCIIToUTF16("firstname");
117 expected
.value
= base::string16();
118 expected
.form_control_type
= "text";
119 expected
.max_length
= WebInputElement::defaultMaxLength();
120 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, form2
.fields
[0]);
122 expected
.name
= ASCIIToUTF16("middlename");
123 expected
.value
= base::string16();
124 expected
.form_control_type
= "text";
125 expected
.max_length
= WebInputElement::defaultMaxLength();
126 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, form2
.fields
[1]);
128 expected
.name
= ASCIIToUTF16("state");
129 expected
.value
= ASCIIToUTF16("?");
130 expected
.form_control_type
= "select-one";
131 expected
.max_length
= 0;
132 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, form2
.fields
[2]);
135 TEST_F(ChromeRenderViewTest
, EnsureNoFormSeenIfTooFewFields
) {
136 // Don't want any delay for form state sync changes. This will still post a
137 // message so updates will get coalesced, but as soon as we spin the message
138 // loop, it will generate an update.
139 SendContentStateImmediately();
141 LoadHTML("<form method=\"POST\">"
142 " <input type=\"text\" id=\"firstname\"/>"
143 " <input type=\"text\" id=\"middlename\"/>"
146 // Verify that "FormsSeen" isn't sent, as there are too few fields.
147 const IPC::Message
* message
= render_thread_
->sink().GetFirstMessageMatching(
148 AutofillHostMsg_FormsSeen::ID
);
149 ASSERT_NE(static_cast<IPC::Message
*>(NULL
), message
);
150 AutofillHostMsg_FormsSeen::Param params
;
151 AutofillHostMsg_FormsSeen::Read(message
, ¶ms
);
152 const std::vector
<FormData
>& forms
= params
.a
;
153 ASSERT_EQ(0UL, forms
.size());
156 TEST_F(ChromeRenderViewTest
, ShowAutofillWarning
) {
157 // Don't want any delay for form state sync changes. This will still post a
158 // message so updates will get coalesced, but as soon as we spin the message
159 // loop, it will generate an update.
160 SendContentStateImmediately();
162 LoadHTML("<form method=\"POST\" autocomplete=\"Off\">"
163 " <input id=\"firstname\" autocomplete=\"OFF\"/>"
164 " <input id=\"middlename\"/>"
165 " <input id=\"lastname\"/>"
168 // Verify that "QueryFormFieldAutofill" isn't sent prior to a user
170 const IPC::Message
* message0
= render_thread_
->sink().GetFirstMessageMatching(
171 AutofillHostMsg_QueryFormFieldAutofill::ID
);
172 EXPECT_EQ(static_cast<IPC::Message
*>(NULL
), message0
);
174 WebFrame
* web_frame
= GetMainFrame();
175 WebDocument document
= web_frame
->document();
176 WebInputElement firstname
=
177 document
.getElementById("firstname").to
<WebInputElement
>();
178 WebInputElement middlename
=
179 document
.getElementById("middlename").to
<WebInputElement
>();
181 // Simulate attempting to Autofill the form from the first element, which
182 // specifies autocomplete="off". This should still trigger an IPC which
183 // shouldn't display warnings.
184 autofill_agent_
->InputElementClicked(firstname
, true, true);
185 const IPC::Message
* message1
= render_thread_
->sink().GetFirstMessageMatching(
186 AutofillHostMsg_QueryFormFieldAutofill::ID
);
187 EXPECT_NE(static_cast<IPC::Message
*>(NULL
), message1
);
189 AutofillQueryParam query_param
;
190 AutofillHostMsg_QueryFormFieldAutofill::Read(message1
, &query_param
);
191 EXPECT_FALSE(query_param
.e
);
192 render_thread_
->sink().ClearMessages();
194 // Simulate attempting to Autofill the form from the second element, which
195 // does not specify autocomplete="off". This should trigger an IPC that will
196 // show warnings, as we *do* show warnings for elements that don't themselves
197 // set autocomplete="off", but for which the form does.
198 autofill_agent_
->InputElementClicked(middlename
, true, true);
199 const IPC::Message
* message2
= render_thread_
->sink().GetFirstMessageMatching(
200 AutofillHostMsg_QueryFormFieldAutofill::ID
);
201 ASSERT_NE(static_cast<IPC::Message
*>(NULL
), message2
);
203 AutofillHostMsg_QueryFormFieldAutofill::Read(message2
, &query_param
);
204 EXPECT_TRUE(query_param
.e
);
207 } // namespace autofill