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 "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/WebKit/public/platform/WebString.h"
11 #include "third_party/WebKit/public/platform/WebURLError.h"
12 #include "third_party/WebKit/public/web/WebDocument.h"
13 #include "third_party/WebKit/public/web/WebFormElement.h"
14 #include "third_party/WebKit/public/web/WebLocalFrame.h"
16 using blink::WebString
;
17 using blink::WebURLError
;
19 typedef ChromeRenderViewTest FormAutocompleteTest
;
23 // Tests that submitting a form generates a FormSubmitted message
24 // with the form fields.
25 TEST_F(FormAutocompleteTest
, NormalFormSubmit
) {
27 LoadHTML("<html><form id='myForm'><input name='fname' value='Rick'/>"
28 "<input name='lname' value='Deckard'/></form></html>");
31 ExecuteJavaScript("document.getElementById('myForm').submit();");
32 ProcessPendingMessages();
34 const IPC::Message
* message
= render_thread_
->sink().GetFirstMessageMatching(
35 AutofillHostMsg_FormSubmitted::ID
);
36 ASSERT_TRUE(message
!= NULL
);
38 // The tuple also includes a timestamp, which is ignored.
39 Tuple2
<FormData
, base::TimeTicks
> forms
;
40 AutofillHostMsg_FormSubmitted::Read(message
, &forms
);
41 ASSERT_EQ(2U, forms
.a
.fields
.size());
43 FormFieldData
& form_field
= forms
.a
.fields
[0];
44 EXPECT_EQ(WebString("fname"), form_field
.name
);
45 EXPECT_EQ(WebString("Rick"), form_field
.value
);
47 form_field
= forms
.a
.fields
[1];
48 EXPECT_EQ(WebString("lname"), form_field
.name
);
49 EXPECT_EQ(WebString("Deckard"), form_field
.value
);
52 // Tests that submitting a form that has autocomplete="off" generates a
53 // FormSubmitted message.
54 TEST_F(FormAutocompleteTest
, AutoCompleteOffFormSubmit
) {
56 LoadHTML("<html><form id='myForm' autocomplete='off'>"
57 "<input name='fname' value='Rick'/>"
58 "<input name='lname' value='Deckard'/>"
62 ExecuteJavaScript("document.getElementById('myForm').submit();");
63 ProcessPendingMessages();
65 const IPC::Message
* message
= render_thread_
->sink().GetFirstMessageMatching(
66 AutofillHostMsg_FormSubmitted::ID
);
67 ASSERT_TRUE(message
!= NULL
);
69 // The tuple also includes a timestamp, which is ignored.
70 Tuple2
<FormData
, base::TimeTicks
> forms
;
71 AutofillHostMsg_FormSubmitted::Read(message
, &forms
);
72 ASSERT_EQ(2U, forms
.a
.fields
.size());
74 FormFieldData
& form_field
= forms
.a
.fields
[0];
75 EXPECT_EQ(WebString("fname"), form_field
.name
);
76 EXPECT_EQ(WebString("Rick"), form_field
.value
);
78 form_field
= forms
.a
.fields
[1];
79 EXPECT_EQ(WebString("lname"), form_field
.name
);
80 EXPECT_EQ(WebString("Deckard"), form_field
.value
);
83 // Tests that fields with autocomplete off are submitted.
84 TEST_F(FormAutocompleteTest
, AutoCompleteOffInputSubmit
) {
86 LoadHTML("<html><form id='myForm'>"
87 "<input name='fname' value='Rick'/>"
88 "<input name='lname' value='Deckard' autocomplete='off'/>"
92 ExecuteJavaScript("document.getElementById('myForm').submit();");
93 ProcessPendingMessages();
95 const IPC::Message
* message
= render_thread_
->sink().GetFirstMessageMatching(
96 AutofillHostMsg_FormSubmitted::ID
);
97 ASSERT_TRUE(message
!= NULL
);
99 // The tuple also includes a timestamp, which is ignored.
100 Tuple2
<FormData
, base::TimeTicks
> forms
;
101 AutofillHostMsg_FormSubmitted::Read(message
, &forms
);
102 ASSERT_EQ(2U, forms
.a
.fields
.size());
104 FormFieldData
& form_field
= forms
.a
.fields
[0];
105 EXPECT_EQ(WebString("fname"), form_field
.name
);
106 EXPECT_EQ(WebString("Rick"), form_field
.value
);
108 form_field
= forms
.a
.fields
[1];
109 EXPECT_EQ(WebString("lname"), form_field
.name
);
110 EXPECT_EQ(WebString("Deckard"), form_field
.value
);
113 // Tests that submitting a form that has been dynamically set as autocomplete
114 // off generates a FormSubmitted message.
115 // Note: We previously did the opposite, for bug http://crbug.com/36520
116 TEST_F(FormAutocompleteTest
, DynamicAutoCompleteOffFormSubmit
) {
117 LoadHTML("<html><form id='myForm'><input name='fname' value='Rick'/>"
118 "<input name='lname' value='Deckard'/></form></html>");
120 blink::WebElement element
=
121 GetMainFrame()->document().getElementById(blink::WebString("myForm"));
122 ASSERT_FALSE(element
.isNull());
123 blink::WebFormElement form
= element
.to
<blink::WebFormElement
>();
124 EXPECT_TRUE(form
.autoComplete());
126 // Dynamically mark the form as autocomplete off.
127 ExecuteJavaScript("document.getElementById('myForm')."
128 "setAttribute('autocomplete', 'off');");
129 ProcessPendingMessages();
130 EXPECT_FALSE(form
.autoComplete());
133 ExecuteJavaScript("document.getElementById('myForm').submit();");
134 ProcessPendingMessages();
136 const IPC::Message
* message
= render_thread_
->sink().GetFirstMessageMatching(
137 AutofillHostMsg_FormSubmitted::ID
);
138 ASSERT_TRUE(message
!= NULL
);
140 // The tuple also includes a timestamp, which is ignored.
141 Tuple2
<FormData
, base::TimeTicks
> forms
;
142 AutofillHostMsg_FormSubmitted::Read(message
, &forms
);
143 ASSERT_EQ(2U, forms
.a
.fields
.size());
145 FormFieldData
& form_field
= forms
.a
.fields
[0];
146 EXPECT_EQ(WebString("fname"), form_field
.name
);
147 EXPECT_EQ(WebString("Rick"), form_field
.value
);
149 form_field
= forms
.a
.fields
[1];
150 EXPECT_EQ(WebString("lname"), form_field
.name
);
151 EXPECT_EQ(WebString("Deckard"), form_field
.value
);
154 } // namespace autofill