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" does not generate 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 // No FormSubmitted message should have been sent.
66 EXPECT_FALSE(render_thread_
->sink().GetFirstMessageMatching(
67 AutofillHostMsg_FormSubmitted::ID
));
70 // Tests that fields with autocomplete off are not submitted.
71 TEST_F(FormAutocompleteTest
, AutoCompleteOffInputSubmit
) {
73 LoadHTML("<html><form id='myForm'>"
74 "<input name='fname' value='Rick'/>"
75 "<input name='lname' value='Deckard' autocomplete='off'/>"
79 ExecuteJavaScript("document.getElementById('myForm').submit();");
80 ProcessPendingMessages();
82 // No FormSubmitted message should have been sent.
83 const IPC::Message
* message
= render_thread_
->sink().GetFirstMessageMatching(
84 AutofillHostMsg_FormSubmitted::ID
);
85 ASSERT_TRUE(message
!= NULL
);
87 // The tuple also includes a timestamp, which is ignored.
88 Tuple2
<FormData
, base::TimeTicks
> forms
;
89 AutofillHostMsg_FormSubmitted::Read(message
, &forms
);
90 ASSERT_EQ(1U, forms
.a
.fields
.size());
92 FormFieldData
& form_field
= forms
.a
.fields
[0];
93 EXPECT_EQ(WebString("fname"), form_field
.name
);
94 EXPECT_EQ(WebString("Rick"), form_field
.value
);
97 // Tests that submitting a form that has been dynamically set as autocomplete
98 // off does not generate a FormSubmitted message.
99 // http://crbug.com/36520
100 TEST_F(FormAutocompleteTest
, DynamicAutoCompleteOffFormSubmit
) {
101 LoadHTML("<html><form id='myForm'><input name='fname' value='Rick'/>"
102 "<input name='lname' value='Deckard'/></form></html>");
104 blink::WebElement element
=
105 GetMainFrame()->document().getElementById(blink::WebString("myForm"));
106 ASSERT_FALSE(element
.isNull());
107 blink::WebFormElement form
= element
.to
<blink::WebFormElement
>();
108 EXPECT_TRUE(form
.autoComplete());
110 // Dynamically mark the form as autocomplete off.
111 ExecuteJavaScript("document.getElementById('myForm')."
112 "setAttribute('autocomplete', 'off');");
113 ProcessPendingMessages();
114 EXPECT_FALSE(form
.autoComplete());
117 ExecuteJavaScript("document.getElementById('myForm').submit();");
118 ProcessPendingMessages();
120 // No FormSubmitted message should have been sent.
121 EXPECT_FALSE(render_thread_
->sink().GetFirstMessageMatching(
122 AutofillHostMsg_FormSubmitted::ID
));
125 } // namespace autofill