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
3 // found in the LICENSE file.
5 #include "base/time/time.h"
6 #include "chrome/test/base/chrome_render_view_test.h"
7 #include "components/autofill/content/common/autofill_messages.h"
8 #include "components/autofill/core/common/form_data.h"
9 #include "content/public/test/mock_render_thread.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/WebKit/public/platform/WebString.h"
12 #include "third_party/WebKit/public/platform/WebURLError.h"
13 #include "third_party/WebKit/public/web/WebDocument.h"
14 #include "third_party/WebKit/public/web/WebFormElement.h"
15 #include "third_party/WebKit/public/web/WebLocalFrame.h"
17 using blink::WebString
;
18 using blink::WebURLError
;
20 typedef ChromeRenderViewTest FormAutocompleteTest
;
26 // Helper function to verify the form-related messages received from the
27 // renderer. The same data is expected in both messages. Depending on
28 // |expect_submitted_message|, will verify presence of FormSubmitted message.
29 void VerifyReceivedRendererMessages(content::MockRenderThread
* render_thread
,
30 bool expect_submitted_message
) {
31 const IPC::Message
* will_submit_message
=
32 render_thread
->sink().GetFirstMessageMatching(
33 AutofillHostMsg_WillSubmitForm::ID
);
34 const IPC::Message
* submitted_message
=
35 render_thread
->sink().GetFirstMessageMatching(
36 AutofillHostMsg_FormSubmitted::ID
);
37 ASSERT_TRUE(will_submit_message
!= NULL
);
38 ASSERT_EQ(expect_submitted_message
, submitted_message
!= NULL
);
40 // The tuple also includes a timestamp, which is ignored.
41 Tuple
<FormData
, base::TimeTicks
> will_submit_forms
;
42 AutofillHostMsg_WillSubmitForm::Read(will_submit_message
, &will_submit_forms
);
43 ASSERT_EQ(2U, get
<0>(will_submit_forms
).fields
.size());
45 FormFieldData
& will_submit_form_field
= get
<0>(will_submit_forms
).fields
[0];
46 EXPECT_EQ(WebString("fname"), will_submit_form_field
.name
);
47 EXPECT_EQ(WebString("Rick"), will_submit_form_field
.value
);
48 will_submit_form_field
= get
<0>(will_submit_forms
).fields
[1];
49 EXPECT_EQ(WebString("lname"), will_submit_form_field
.name
);
50 EXPECT_EQ(WebString("Deckard"), will_submit_form_field
.value
);
52 if (expect_submitted_message
) {
53 Tuple
<FormData
> submitted_forms
;
54 AutofillHostMsg_FormSubmitted::Read(submitted_message
, &submitted_forms
);
55 ASSERT_EQ(2U, get
<0>(submitted_forms
).fields
.size());
57 FormFieldData
& submitted_field
= get
<0>(submitted_forms
).fields
[0];
58 EXPECT_EQ(WebString("fname"), submitted_field
.name
);
59 EXPECT_EQ(WebString("Rick"), submitted_field
.value
);
60 submitted_field
= get
<0>(submitted_forms
).fields
[1];
61 EXPECT_EQ(WebString("lname"), submitted_field
.name
);
62 EXPECT_EQ(WebString("Deckard"), submitted_field
.value
);
68 // Tests that submitting a form generates WillSubmitForm and FormSubmitted
69 // messages with the form fields.
70 TEST_F(FormAutocompleteTest
, NormalFormSubmit
) {
72 LoadHTML("<html><form id='myForm'><input name='fname' value='Rick'/>"
73 "<input name='lname' value='Deckard'/></form></html>");
76 ExecuteJavaScript("document.getElementById('myForm').submit();");
77 ProcessPendingMessages();
79 VerifyReceivedRendererMessages(render_thread_
.get(),
80 true /* expect_submitted_message */);
83 // Tests that submitting a form that prevents the submit event from propagating
84 // will only send the WillSubmitForm message.
85 TEST_F(FormAutocompleteTest
, SubmitEventPrevented
) {
88 "<html><form id='myForm'><input name='fname' value='Rick'/>"
89 "<input name='lname' value='Deckard'/><input type=submit></form>"
94 "var form = document.forms[0];"
95 "form.onsubmit = function(event) { event.preventDefault(); };"
96 "document.querySelector('input[type=submit]').click();");
97 ProcessPendingMessages();
99 VerifyReceivedRendererMessages(render_thread_
.get(),
100 false /* expect_submitted_message */);
103 // Tests that submitting a form that has autocomplete="off" generates
104 // WillSubmitForm and FormSubmitted messages.
105 TEST_F(FormAutocompleteTest
, AutoCompleteOffFormSubmit
) {
107 LoadHTML("<html><form id='myForm' autocomplete='off'>"
108 "<input name='fname' value='Rick'/>"
109 "<input name='lname' value='Deckard'/>"
113 ExecuteJavaScript("document.getElementById('myForm').submit();");
114 ProcessPendingMessages();
116 VerifyReceivedRendererMessages(render_thread_
.get(),
117 true /* expect_submitted_message */);
120 // Tests that fields with autocomplete off are submitted.
121 TEST_F(FormAutocompleteTest
, AutoCompleteOffInputSubmit
) {
123 LoadHTML("<html><form id='myForm'>"
124 "<input name='fname' value='Rick'/>"
125 "<input name='lname' value='Deckard' autocomplete='off'/>"
129 ExecuteJavaScript("document.getElementById('myForm').submit();");
130 ProcessPendingMessages();
132 VerifyReceivedRendererMessages(render_thread_
.get(),
133 true /* expect_submitted_message */);
136 // Tests that submitting a form that has been dynamically set as autocomplete
137 // off generates WillSubmitForm and FormSubmitted messages.
138 // Note: We previously did the opposite, for bug http://crbug.com/36520
139 TEST_F(FormAutocompleteTest
, DynamicAutoCompleteOffFormSubmit
) {
140 LoadHTML("<html><form id='myForm'><input name='fname' value='Rick'/>"
141 "<input name='lname' value='Deckard'/></form></html>");
143 blink::WebElement element
=
144 GetMainFrame()->document().getElementById(blink::WebString("myForm"));
145 ASSERT_FALSE(element
.isNull());
146 blink::WebFormElement form
= element
.to
<blink::WebFormElement
>();
147 EXPECT_TRUE(form
.autoComplete());
149 // Dynamically mark the form as autocomplete off.
150 ExecuteJavaScript("document.getElementById('myForm')."
151 "setAttribute('autocomplete', 'off');");
152 ProcessPendingMessages();
153 EXPECT_FALSE(form
.autoComplete());
156 ExecuteJavaScript("document.getElementById('myForm').submit();");
157 ProcessPendingMessages();
159 VerifyReceivedRendererMessages(render_thread_
.get(),
160 true /* expect_submitted_message */);
163 } // namespace autofill