1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/utf_string_conversions.h"
#include "chrome/common/autofill_messages.h"
#include "chrome/test/base/render_view_test.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h"
#include "webkit/glue/form_data.h"
#include "webkit/glue/form_field.h"
using WebKit::WebDocument;
using WebKit::WebFrame;
using WebKit::WebInputElement;
using WebKit::WebString;
using webkit_glue::FormData;
using webkit_glue::FormField;
TEST_F(RenderViewTest, SendForms) {
// Don't want any delay for form state sync changes. This will still post a
// message so updates will get coalesced, but as soon as we spin the message
// loop, it will generate an update.
view_->set_send_content_state_immediately(true);
LoadHTML("<form method=\"POST\">"
" <input type=\"text\" id=\"firstname\"/>"
" <input type=\"text\" id=\"middlename\" autoComplete=\"off\"/>"
" <input type=\"hidden\" id=\"lastname\"/>"
" <select id=\"state\"/>"
" <option>?</option>"
" <option>California</option>"
" <option>Texas</option>"
" </select>"
"</form>");
// Verify that "FormsSeen" sends the expected number of fields.
ProcessPendingMessages();
const IPC::Message* message = render_thread_.sink().GetFirstMessageMatching(
AutofillHostMsg_FormsSeen::ID);
ASSERT_NE(static_cast<IPC::Message*>(NULL), message);
AutofillHostMsg_FormsSeen::Param params;
AutofillHostMsg_FormsSeen::Read(message, ¶ms);
const std::vector<FormData>& forms = params.a;
ASSERT_EQ(1UL, forms.size());
ASSERT_EQ(3UL, forms[0].fields.size());
FormField expected;
expected.name = ASCIIToUTF16("firstname");
expected.value = string16();
expected.form_control_type = ASCIIToUTF16("text");
expected.max_length = WebInputElement::defaultMaxLength();
EXPECT_FORM_FIELD_EQUALS(expected, forms[0].fields[0]);
expected.name = ASCIIToUTF16("middlename");
expected.value = string16();
expected.form_control_type = ASCIIToUTF16("text");
expected.max_length = WebInputElement::defaultMaxLength();
EXPECT_FORM_FIELD_EQUALS(expected, forms[0].fields[1]);
expected.name = ASCIIToUTF16("state");
expected.value = ASCIIToUTF16("?");
expected.form_control_type = ASCIIToUTF16("select-one");
expected.max_length = 0;
EXPECT_FORM_FIELD_EQUALS(expected, forms[0].fields[2]);
// Verify that |didAcceptAutofillSuggestion()| sends the expected number of
// fields.
WebFrame* web_frame = GetMainFrame();
WebDocument document = web_frame->document();
WebInputElement firstname =
document.getElementById("firstname").to<WebInputElement>();
// Accept suggestion that contains a label. Labeled items indicate Autofill
// as opposed to Autocomplete. We're testing this distinction below with
// the |AutofillHostMsg_FillAutofillFormData::ID| message.
autofill_agent_->didAcceptAutofillSuggestion(
firstname,
WebKit::WebString::fromUTF8("Johnny"),
WebKit::WebString::fromUTF8("Home"),
1,
-1);
ProcessPendingMessages();
const IPC::Message* message2 = render_thread_.sink().GetUniqueMessageMatching(
AutofillHostMsg_FillAutofillFormData::ID);
ASSERT_NE(static_cast<IPC::Message*>(NULL), message2);
AutofillHostMsg_FillAutofillFormData::Param params2;
AutofillHostMsg_FillAutofillFormData::Read(message2, ¶ms2);
const FormData& form2 = params2.b;
ASSERT_EQ(3UL, form2.fields.size());
expected.name = ASCIIToUTF16("firstname");
expected.value = string16();
expected.form_control_type = ASCIIToUTF16("text");
expected.max_length = WebInputElement::defaultMaxLength();
EXPECT_FORM_FIELD_EQUALS(expected, form2.fields[0]);
expected.name = ASCIIToUTF16("middlename");
expected.value = string16();
expected.form_control_type = ASCIIToUTF16("text");
expected.max_length = WebInputElement::defaultMaxLength();
EXPECT_FORM_FIELD_EQUALS(expected, form2.fields[1]);
expected.name = ASCIIToUTF16("state");
expected.value = ASCIIToUTF16("?");
expected.form_control_type = ASCIIToUTF16("select-one");
expected.max_length = 0;
EXPECT_FORM_FIELD_EQUALS(expected, form2.fields[2]);
}
TEST_F(RenderViewTest, FillFormElement) {
// Don't want any delay for form state sync changes. This will still post a
// message so updates will get coalesced, but as soon as we spin the message
// loop, it will generate an update.
view_->set_send_content_state_immediately(true);
LoadHTML("<form method=\"POST\">"
" <input type=\"text\" id=\"firstname\"/>"
" <input type=\"text\" id=\"middlename\"/>"
"</form>");
// Verify that "FormsSeen" isn't sent, as there are too few fields.
ProcessPendingMessages();
const IPC::Message* message = render_thread_.sink().GetFirstMessageMatching(
AutofillHostMsg_FormsSeen::ID);
ASSERT_EQ(static_cast<IPC::Message*>(NULL), message);
// Verify that |didAcceptAutofillSuggestion()| sets the value of the expected
// field.
WebFrame* web_frame = GetMainFrame();
WebDocument document = web_frame->document();
WebInputElement firstname =
document.getElementById("firstname").to<WebInputElement>();
WebInputElement middlename =
document.getElementById("middlename").to<WebInputElement>();
middlename.setAutofilled(true);
// Accept a suggestion in a form that has been auto-filled. This triggers
// the direct filling of the firstname element with value parameter.
autofill_agent_->didAcceptAutofillSuggestion(firstname,
WebString::fromUTF8("David"),
WebString(),
0,
0);
ProcessPendingMessages();
const IPC::Message* message2 = render_thread_.sink().GetUniqueMessageMatching(
AutofillHostMsg_FillAutofillFormData::ID);
// No message should be sent in this case. |firstname| is filled directly.
ASSERT_EQ(static_cast<IPC::Message*>(NULL), message2);
EXPECT_EQ(firstname.value(), WebKit::WebString::fromUTF8("David"));
}
|