Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / renderer / autofill / form_autocomplete_browsertest.cc
blob52a3db553eb74e3d681b8608fb1acfe027b536e0
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;
22 namespace autofill {
24 namespace {
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 base::Tuple<FormData, base::TimeTicks> will_submit_forms;
42 AutofillHostMsg_WillSubmitForm::Read(will_submit_message, &will_submit_forms);
43 ASSERT_EQ(2U, base::get<0>(will_submit_forms).fields.size());
45 FormFieldData& will_submit_form_field =
46 base::get<0>(will_submit_forms).fields[0];
47 EXPECT_EQ(WebString("fname"), will_submit_form_field.name);
48 EXPECT_EQ(WebString("Rick"), will_submit_form_field.value);
49 will_submit_form_field = base::get<0>(will_submit_forms).fields[1];
50 EXPECT_EQ(WebString("lname"), will_submit_form_field.name);
51 EXPECT_EQ(WebString("Deckard"), will_submit_form_field.value);
53 if (expect_submitted_message) {
54 base::Tuple<FormData> submitted_forms;
55 AutofillHostMsg_FormSubmitted::Read(submitted_message, &submitted_forms);
56 ASSERT_EQ(2U, base::get<0>(submitted_forms).fields.size());
58 FormFieldData& submitted_field = base::get<0>(submitted_forms).fields[0];
59 EXPECT_EQ(WebString("fname"), submitted_field.name);
60 EXPECT_EQ(WebString("Rick"), submitted_field.value);
61 submitted_field = base::get<0>(submitted_forms).fields[1];
62 EXPECT_EQ(WebString("lname"), submitted_field.name);
63 EXPECT_EQ(WebString("Deckard"), submitted_field.value);
67 } // end namespace
69 // Tests that submitting a form generates WillSubmitForm and FormSubmitted
70 // messages with the form fields.
71 TEST_F(FormAutocompleteTest, NormalFormSubmit) {
72 // Load a form.
73 LoadHTML("<html><form id='myForm'><input name='fname' value='Rick'/>"
74 "<input name='lname' value='Deckard'/></form></html>");
76 // Submit the form.
77 ExecuteJavaScriptForTests("document.getElementById('myForm').submit();");
78 ProcessPendingMessages();
80 VerifyReceivedRendererMessages(render_thread_.get(),
81 true /* expect_submitted_message */);
84 // Tests that submitting a form that prevents the submit event from propagating
85 // will only send the WillSubmitForm message.
86 TEST_F(FormAutocompleteTest, SubmitEventPrevented) {
87 // Load a form.
88 LoadHTML(
89 "<html><form id='myForm'><input name='fname' value='Rick'/>"
90 "<input name='lname' value='Deckard'/><input type=submit></form>"
91 "</html>");
93 // Submit the form.
94 ExecuteJavaScriptForTests(
95 "var form = document.forms[0];"
96 "form.onsubmit = function(event) { event.preventDefault(); };"
97 "document.querySelector('input[type=submit]').click();");
98 ProcessPendingMessages();
100 VerifyReceivedRendererMessages(render_thread_.get(),
101 false /* expect_submitted_message */);
104 // Tests that submitting a form that has autocomplete="off" generates
105 // WillSubmitForm and FormSubmitted messages.
106 TEST_F(FormAutocompleteTest, AutoCompleteOffFormSubmit) {
107 // Load a form.
108 LoadHTML("<html><form id='myForm' autocomplete='off'>"
109 "<input name='fname' value='Rick'/>"
110 "<input name='lname' value='Deckard'/>"
111 "</form></html>");
113 // Submit the form.
114 ExecuteJavaScriptForTests("document.getElementById('myForm').submit();");
115 ProcessPendingMessages();
117 VerifyReceivedRendererMessages(render_thread_.get(),
118 true /* expect_submitted_message */);
121 // Tests that fields with autocomplete off are submitted.
122 TEST_F(FormAutocompleteTest, AutoCompleteOffInputSubmit) {
123 // Load a form.
124 LoadHTML("<html><form id='myForm'>"
125 "<input name='fname' value='Rick'/>"
126 "<input name='lname' value='Deckard' autocomplete='off'/>"
127 "</form></html>");
129 // Submit the form.
130 ExecuteJavaScriptForTests("document.getElementById('myForm').submit();");
131 ProcessPendingMessages();
133 VerifyReceivedRendererMessages(render_thread_.get(),
134 true /* expect_submitted_message */);
137 // Tests that submitting a form that has been dynamically set as autocomplete
138 // off generates WillSubmitForm and FormSubmitted messages.
139 // Note: We previously did the opposite, for bug http://crbug.com/36520
140 TEST_F(FormAutocompleteTest, DynamicAutoCompleteOffFormSubmit) {
141 LoadHTML("<html><form id='myForm'><input name='fname' value='Rick'/>"
142 "<input name='lname' value='Deckard'/></form></html>");
144 blink::WebElement element =
145 GetMainFrame()->document().getElementById(blink::WebString("myForm"));
146 ASSERT_FALSE(element.isNull());
147 blink::WebFormElement form = element.to<blink::WebFormElement>();
148 EXPECT_TRUE(form.autoComplete());
150 // Dynamically mark the form as autocomplete off.
151 ExecuteJavaScriptForTests(
152 "document.getElementById('myForm')."
153 "setAttribute('autocomplete', 'off');");
154 ProcessPendingMessages();
155 EXPECT_FALSE(form.autoComplete());
157 // Submit the form.
158 ExecuteJavaScriptForTests("document.getElementById('myForm').submit();");
159 ProcessPendingMessages();
161 VerifyReceivedRendererMessages(render_thread_.get(),
162 true /* expect_submitted_message */);
165 } // namespace autofill