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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
|
// 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/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/test/base/chrome_render_view_test.h"
#include "components/autofill/content/common/autofill_messages.h"
#include "components/autofill/content/renderer/autofill_agent.h"
#include "components/autofill/core/common/form_data.h"
#include "content/public/test/mock_render_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebElement.h"
#include "third_party/WebKit/public/web/WebFormElement.h"
#include "third_party/WebKit/public/web/WebInputElement.h"
#include "third_party/WebKit/public/web/WebLocalFrame.h"
using blink::WebDocument;
using blink::WebElement;
using blink::WebInputElement;
using blink::WebString;
typedef ChromeRenderViewTest FormAutocompleteTest;
namespace autofill {
namespace {
// Helper function to verify the form-related messages received from the
// renderer. The same data is expected in both messages. Depending on
// |expect_submitted_message|, will verify presence of FormSubmitted message.
void VerifyReceivedRendererMessages(content::MockRenderThread* render_thread,
const std::string& fname,
const std::string& lname,
bool expect_submitted_message) {
const IPC::Message* will_submit_message =
render_thread->sink().GetFirstMessageMatching(
AutofillHostMsg_WillSubmitForm::ID);
const IPC::Message* submitted_message =
render_thread->sink().GetFirstMessageMatching(
AutofillHostMsg_FormSubmitted::ID);
ASSERT_TRUE(will_submit_message != NULL);
ASSERT_EQ(expect_submitted_message, submitted_message != NULL);
// The tuple also includes a timestamp, which is ignored.
base::Tuple<FormData, base::TimeTicks> will_submit_forms;
AutofillHostMsg_WillSubmitForm::Read(will_submit_message, &will_submit_forms);
ASSERT_EQ(2U, base::get<0>(will_submit_forms).fields.size());
FormFieldData& will_submit_form_field =
base::get<0>(will_submit_forms).fields[0];
EXPECT_EQ(WebString("fname"), will_submit_form_field.name);
EXPECT_EQ(WebString(base::UTF8ToUTF16(fname)), will_submit_form_field.value);
will_submit_form_field = base::get<0>(will_submit_forms).fields[1];
EXPECT_EQ(WebString("lname"), will_submit_form_field.name);
EXPECT_EQ(WebString(base::UTF8ToUTF16(lname)), will_submit_form_field.value);
if (expect_submitted_message) {
base::Tuple<FormData> submitted_forms;
AutofillHostMsg_FormSubmitted::Read(submitted_message, &submitted_forms);
ASSERT_EQ(2U, base::get<0>(submitted_forms).fields.size());
FormFieldData& submitted_field = base::get<0>(submitted_forms).fields[0];
EXPECT_EQ(WebString("fname"), submitted_field.name);
EXPECT_EQ(WebString(base::UTF8ToUTF16(fname)), submitted_field.value);
submitted_field = base::get<0>(submitted_forms).fields[1];
EXPECT_EQ(WebString("lname"), submitted_field.name);
EXPECT_EQ(WebString(base::UTF8ToUTF16(lname)), submitted_field.value);
}
}
// Helper function to verify that NO form-related messages are received from the
// renderer.
void VerifyNoSubmitMessagesReceived(content::MockRenderThread* render_thread) {
// No submission messages sent.
const IPC::Message* will_submit_message =
render_thread->sink().GetFirstMessageMatching(
AutofillHostMsg_WillSubmitForm::ID);
const IPC::Message* submitted_message =
render_thread->sink().GetFirstMessageMatching(
AutofillHostMsg_FormSubmitted::ID);
EXPECT_EQ(NULL, will_submit_message);
EXPECT_EQ(NULL, submitted_message);
}
// Simulates receiving a message from the browser to fill a form.
void SimulateOnFillForm(content::MockRenderThread* render_thread,
autofill::AutofillAgent* autofill_agent,
blink::WebFrame* main_frame) {
WebDocument document = main_frame->document();
WebElement element =
document.getElementById(WebString::fromUTF8("fname"));
ASSERT_FALSE(element.isNull());
// This call is necessary to setup the autofill agent appropriate for the
// user selection; simulates the menu actually popping up.
render_thread->sink().ClearMessages();
static_cast<autofill::PageClickListener*>(autofill_agent)
->FormControlElementClicked(element.to<WebInputElement>(), false);
FormData data;
data.name = base::ASCIIToUTF16("name");
data.origin = GURL("http://example.com/");
data.action = GURL("http://example.com/blade.php");
data.is_form_tag = true; // Default value.
FormFieldData field_data;
field_data.name = base::ASCIIToUTF16("fname");
field_data.value = base::ASCIIToUTF16("John");
field_data.is_autofilled = true;
data.fields.push_back(field_data);
field_data.name = base::ASCIIToUTF16("lname");
field_data.value = base::ASCIIToUTF16("Smith");
field_data.is_autofilled = true;
data.fields.push_back(field_data);
AutofillMsg_FillForm msg(0, 0, data);
static_cast<content::RenderFrameObserver*>(autofill_agent)
->OnMessageReceived(msg);
}
} // end namespace
// Tests that submitting a form generates WillSubmitForm and FormSubmitted
// messages with the form fields.
TEST_F(FormAutocompleteTest, NormalFormSubmit) {
// Load a form.
LoadHTML("<html><form id='myForm'><input name='fname' value='Rick'/>"
"<input name='lname' value='Deckard'/></form></html>");
// Submit the form.
ExecuteJavaScriptForTests("document.getElementById('myForm').submit();");
ProcessPendingMessages();
VerifyReceivedRendererMessages(render_thread_.get(), "Rick", "Deckard",
true /* expect_submitted_message */);
}
// Tests that submitting a form that prevents the submit event from propagating
// will only send the WillSubmitForm message.
TEST_F(FormAutocompleteTest, SubmitEventPrevented) {
// Load a form.
LoadHTML(
"<html><form id='myForm'><input name='fname' value='Rick'/>"
"<input name='lname' value='Deckard'/><input type=submit></form>"
"</html>");
// Submit the form.
ExecuteJavaScriptForTests(
"var form = document.forms[0];"
"form.onsubmit = function(event) { event.preventDefault(); };"
"document.querySelector('input[type=submit]').click();");
ProcessPendingMessages();
VerifyReceivedRendererMessages(render_thread_.get(), "Rick", "Deckard",
false /* expect_submitted_message */);
}
// Tests that completing an Ajax request and having the form disappear will
// trigger submission from Autofill's point of view.
TEST_F(FormAutocompleteTest, AjaxSucceeded_NoLongerVisible) {
// Load a form.
LoadHTML(
"<html><form id='myForm' action='http://example.com/blade.php'>"
"<input name='fname' id='fname' value='Bob'/>"
"<input name='lname' value='Deckard'/><input type=submit></form></html>");
// Simulate user input so that the form is "remembered".
WebDocument document = GetMainFrame()->document();
WebElement element = document.getElementById(WebString::fromUTF8("fname"));
ASSERT_FALSE(element.isNull());
WebInputElement fname_element = element.to<WebInputElement>();
SimulateUserInputChangeForElement(&fname_element, std::string("Rick"));
// Simulate removing the form just before the ajax request completes.
ExecuteJavaScriptForTests(
"var element = document.getElementById('myForm');"
"element.parentNode.removeChild(element);");
// Simulate an Ajax request completing.
static_cast<blink::WebAutofillClient*>(autofill_agent_)->ajaxSucceeded();
ProcessPendingMessages();
VerifyReceivedRendererMessages(render_thread_.get(), "Rick", "Deckard",
true /* expect_submitted_message */);
}
// Tests that completing an Ajax request and having the form with a specific
// action disappear will trigger submission from Autofill's point of view, even
// if there is another form with the same data but different action on the page.
TEST_F(FormAutocompleteTest,
AjaxSucceeded_NoLongerVisible_DifferentActionsSameData) {
// Load a form.
LoadHTML(
"<html><form id='myForm' action='http://example.com/blade.php'>"
"<input name='fname' id='fname' value='Bob'/>"
"<input name='lname' value='Deckard'/><input type=submit></form>"
"<form id='myForm2' action='http://example.com/runner.php'>"
"<input name='fname' id='fname2' value='Bob'/>"
"<input name='lname' value='Deckard'/><input type=submit></form></html>");
// Simulate user input so that the form is "remembered".
WebDocument document = GetMainFrame()->document();
WebElement element = document.getElementById(WebString::fromUTF8("fname"));
ASSERT_FALSE(element.isNull());
WebInputElement fname_element = element.to<WebInputElement>();
SimulateUserInputChangeForElement(&fname_element, std::string("Rick"));
// Simulate removing the form just before the ajax request completes.
ExecuteJavaScriptForTests(
"var element = document.getElementById('myForm');"
"element.parentNode.removeChild(element);");
// Simulate an Ajax request completing.
static_cast<blink::WebAutofillClient*>(autofill_agent_)->ajaxSucceeded();
ProcessPendingMessages();
VerifyReceivedRendererMessages(render_thread_.get(), "Rick", "Deckard",
true /* expect_submitted_message */);
}
// Tests that completing an Ajax request and having the form with no action
// specified disappear will trigger submission from Autofill's point of view,
// even if there is still another form with no action in the page. It will
// compare field data within the forms.
// TODO(kolos) Re-enable when the implementation of IsFormVisible is on-par
// for these platforms.
#if defined(OS_MACOSX) || defined(OS_ANDROID)
#define MAYBE_NoLongerVisibleBothNoActions DISABLED_NoLongerVisibleBothNoActions
#else
#define MAYBE_NoLongerVisibleBothNoActions NoLongerVisibleBothNoActions
#endif
TEST_F(FormAutocompleteTest, MAYBE_NoLongerVisibleBothNoActions) {
// Load a form.
LoadHTML(
"<html><form id='myForm'>"
"<input name='fname' id='fname' value='Bob'/>"
"<input name='lname' value='Deckard'/><input type=submit></form>"
"<form id='myForm2'>"
"<input name='fname' id='fname2' value='John'/>"
"<input name='lname' value='Doe'/><input type=submit></form></html>");
// Simulate user input so that the form is "remembered".
WebDocument document = GetMainFrame()->document();
WebElement element = document.getElementById(WebString::fromUTF8("fname"));
ASSERT_FALSE(element.isNull());
WebInputElement fname_element = element.to<WebInputElement>();
SimulateUserInputChangeForElement(&fname_element, std::string("Rick"));
// Simulate removing the form just before the ajax request completes.
ExecuteJavaScriptForTests(
"var element = document.getElementById('myForm');"
"element.parentNode.removeChild(element);");
// Simulate an Ajax request completing.
static_cast<blink::WebAutofillClient*>(autofill_agent_)->ajaxSucceeded();
ProcessPendingMessages();
VerifyReceivedRendererMessages(render_thread_.get(), "Rick", "Deckard",
true /* expect_submitted_message */);
}
// Tests that completing an Ajax request and having the form with no action
// specified disappear will trigger submission from Autofill's point of view.
TEST_F(FormAutocompleteTest, AjaxSucceeded_NoLongerVisible_NoAction) {
// Load a form.
LoadHTML(
"<html><form id='myForm'>"
"<input name='fname' id='fname' value='Bob'/>"
"<input name='lname' value='Deckard'/><input type=submit></form></html>");
// Simulate user input so that the form is "remembered".
WebDocument document = GetMainFrame()->document();
WebElement element =
document.getElementById(WebString::fromUTF8("fname"));
ASSERT_FALSE(element.isNull());
WebInputElement fname_element = element.to<WebInputElement>();
SimulateUserInputChangeForElement(&fname_element, std::string("Rick"));
// Simulate removing the form just before the ajax request completes.
ExecuteJavaScriptForTests("var element = document.getElementById('myForm');"
"element.parentNode.removeChild(element);");
// Simulate an Ajax request completing.
static_cast<blink::WebAutofillClient*>(autofill_agent_)->ajaxSucceeded();
ProcessPendingMessages();
VerifyReceivedRendererMessages(render_thread_.get(), "Rick", "Deckard",
true /* expect_submitted_message */);
}
// Tests that completing an Ajax request but leaving a form visible will not
// trigger submission from Autofill's point of view.
TEST_F(FormAutocompleteTest, AjaxSucceeded_StillVisible) {
// Load a form.
LoadHTML(
"<html><form id='myForm' action='http://example.com/blade.php'>"
"<input name='fname' id='fname' value='Bob'/>"
"<input name='lname' value='Deckard'/><input type=submit></form></html>");
// Simulate user input so that the form is "remembered".
WebDocument document = GetMainFrame()->document();
WebElement element =
document.getElementById(WebString::fromUTF8("fname"));
ASSERT_FALSE(element.isNull());
WebInputElement fname_element = element.to<WebInputElement>();
SimulateUserInputChangeForElement(&fname_element, std::string("Rick"));
// Simulate an Ajax request completing.
static_cast<blink::WebAutofillClient*>(autofill_agent_)->ajaxSucceeded();
ProcessPendingMessages();
// No submission messages sent.
VerifyNoSubmitMessagesReceived(render_thread_.get());
}
// Tests that completing an Ajax request without any prior form interaction
// does not trigger form submission from Autofill's point of view.
TEST_F(FormAutocompleteTest, AjaxSucceeded_NoFormInteractionInvisible) {
// Load a form.
LoadHTML(
"<html><form id='myForm' action='http://example.com/blade.php'>"
"<input name='fname' id='fname' value='Bob'/>"
"<input name='lname' value='Deckard'/><input type=submit></form></html>");
// No form interaction.
// Simulate removing the form just before the ajax request completes.
ExecuteJavaScriptForTests("var element = document.getElementById('myForm');"
"element.parentNode.removeChild(element);");
// Simulate an Ajax request completing without prior user interaction.
static_cast<blink::WebAutofillClient*>(autofill_agent_)->ajaxSucceeded();
ProcessPendingMessages();
// No submission messages sent.
VerifyNoSubmitMessagesReceived(render_thread_.get());
}
// Tests that completing an Ajax request after having autofilled a form,
// with the form disappearing, will trigger submission from Autofill's
// point of view.
TEST_F(FormAutocompleteTest, AjaxSucceeded_FilledFormIsInvisible) {
// Load a form.
LoadHTML(
"<html><form id='myForm' action='http://example.com/blade.php'>"
"<input name='fname' id='fname'/>"
"<input name='lname'/></form></html>");
// Simulate filling a form using Autofill.
SimulateOnFillForm(render_thread_.get(), autofill_agent_, GetMainFrame());
// Simulate removing the form just before the ajax request completes.
ExecuteJavaScriptForTests("var element = document.getElementById('myForm');"
"element.parentNode.removeChild(element);");
// Simulate an Ajax request completing.
static_cast<blink::WebAutofillClient*>(autofill_agent_)->ajaxSucceeded();
ProcessPendingMessages();
VerifyReceivedRendererMessages(render_thread_.get(), "John", "Smith",
true /* expect_submitted_message */);
}
// Tests that completing an Ajax request after having autofilled a form,
// without the form disappearing, will not trigger submission from Autofill's
// point of view.
TEST_F(FormAutocompleteTest, AjaxSucceeded_FilledFormStillVisible) {
// Load a form.
LoadHTML(
"<html><form id='myForm' action='http://example.com/blade.php'>"
"<input name='fname' id='fname' value='Rick'/>"
"<input name='lname' value='Deckard'/></form></html>");
// Simulate filling a form using Autofill.
SimulateOnFillForm(render_thread_.get(), autofill_agent_, GetMainFrame());
// Form still visible.
// Simulate an Ajax request completing.
static_cast<blink::WebAutofillClient*>(autofill_agent_)->ajaxSucceeded();
ProcessPendingMessages();
// No submission messages sent.
VerifyNoSubmitMessagesReceived(render_thread_.get());
}
// Test that a FocusNoLongerOnForm message is sent if focus goes from an
// interacted form to an element outside the form.
TEST_F(FormAutocompleteTest,
InteractedFormNoLongerFocused_FocusNoLongerOnForm) {
// Load a form.
LoadHTML(
"<html><input type='text' id='different'/>"
"<form id='myForm' action='http://example.com/blade.php'>"
"<input name='fname' id='fname' value='Bob'/>"
"<input name='lname' value='Deckard'/><input type=submit></form></html>");
// Simulate user input so that the form is "remembered".
WebDocument document = GetMainFrame()->document();
WebElement element = document.getElementById(WebString::fromUTF8("fname"));
ASSERT_FALSE(element.isNull());
WebInputElement fname_element = element.to<WebInputElement>();
SimulateUserInputChangeForElement(&fname_element, std::string("Rick"));
// Change focus to a different node outside the form.
WebElement different =
document.getElementById(WebString::fromUTF8("different"));
SetFocused(different);
ProcessPendingMessages();
EXPECT_TRUE(render_thread_->sink().GetFirstMessageMatching(
AutofillHostMsg_FocusNoLongerOnForm::ID) != nullptr);
}
// Test that a FocusNoLongerOnForm message is sent if focus goes from one
// interacted form to another.
TEST_F(FormAutocompleteTest, InteractingInDifferentForms_FocusNoLongerOnForm) {
// Load a form.
LoadHTML(
"<html><form id='myForm' action='http://example.com/blade.php'>"
"<input name='fname' id='fname' value='Bob'/>"
"<input name='lname' value='Deckard'/><input type=submit></form>"
"<form id='myForm2' action='http://example.com/runner.php'>"
"<input name='fname' id='fname2' value='Bob'/>"
"<input name='lname' value='Deckard'/><input type=submit></form></html>");
// Simulate user input in the first form so that the form is "remembered".
WebDocument document = GetMainFrame()->document();
WebElement element = document.getElementById(WebString::fromUTF8("fname"));
ASSERT_FALSE(element.isNull());
WebInputElement fname_element = element.to<WebInputElement>();
SimulateUserInputChangeForElement(&fname_element, std::string("Rick"));
// Simulate user input in the second form so that a "no longer focused"
// message is sent for the first form.
document = GetMainFrame()->document();
element = document.getElementById(WebString::fromUTF8("fname2"));
ASSERT_FALSE(element.isNull());
fname_element = element.to<WebInputElement>();
SimulateUserInputChangeForElement(&fname_element, std::string("John"));
ProcessPendingMessages();
EXPECT_TRUE(render_thread_->sink().GetFirstMessageMatching(
AutofillHostMsg_FocusNoLongerOnForm::ID) != nullptr);
}
// Tests that submitting a form that has autocomplete="off" generates
// WillSubmitForm and FormSubmitted messages.
TEST_F(FormAutocompleteTest, AutoCompleteOffFormSubmit) {
// Load a form.
LoadHTML("<html><form id='myForm' autocomplete='off'>"
"<input name='fname' value='Rick'/>"
"<input name='lname' value='Deckard'/>"
"</form></html>");
// Submit the form.
ExecuteJavaScriptForTests("document.getElementById('myForm').submit();");
ProcessPendingMessages();
VerifyReceivedRendererMessages(render_thread_.get(), "Rick", "Deckard",
true /* expect_submitted_message */);
}
// Tests that fields with autocomplete off are submitted.
TEST_F(FormAutocompleteTest, AutoCompleteOffInputSubmit) {
// Load a form.
LoadHTML("<html><form id='myForm'>"
"<input name='fname' value='Rick'/>"
"<input name='lname' value='Deckard' autocomplete='off'/>"
"</form></html>");
// Submit the form.
ExecuteJavaScriptForTests("document.getElementById('myForm').submit();");
ProcessPendingMessages();
VerifyReceivedRendererMessages(render_thread_.get(), "Rick", "Deckard",
true /* expect_submitted_message */);
}
// Tests that submitting a form that has been dynamically set as autocomplete
// off generates WillSubmitForm and FormSubmitted messages.
// Note: We previously did the opposite, for bug http://crbug.com/36520
TEST_F(FormAutocompleteTest, DynamicAutoCompleteOffFormSubmit) {
LoadHTML("<html><form id='myForm'><input name='fname' value='Rick'/>"
"<input name='lname' value='Deckard'/></form></html>");
WebElement element =
GetMainFrame()->document().getElementById(blink::WebString("myForm"));
ASSERT_FALSE(element.isNull());
blink::WebFormElement form = element.to<blink::WebFormElement>();
EXPECT_TRUE(form.autoComplete());
// Dynamically mark the form as autocomplete off.
ExecuteJavaScriptForTests(
"document.getElementById('myForm')."
"setAttribute('autocomplete', 'off');");
ProcessPendingMessages();
EXPECT_FALSE(form.autoComplete());
// Submit the form.
ExecuteJavaScriptForTests("document.getElementById('myForm').submit();");
ProcessPendingMessages();
VerifyReceivedRendererMessages(render_thread_.get(), "Rick", "Deckard",
true /* expect_submitted_message */);
}
} // namespace autofill
|