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. 4 5 #include "base/time/time.h" 6 #include "chrome/test/base/chrome_render_view_test.h" 7 #include "components/autofill/core/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 15 using WebKit::WebFrame; 16 using WebKit::WebString; 17 using WebKit::WebURLError; 18 19 typedef ChromeRenderViewTest FormAutocompleteTest; 20 21 namespace autofill { 22 23 // Tests that submitting a form generates a FormSubmitted message 24 // with the form fields. 25 TEST_F(FormAutocompleteTest, NormalFormSubmit) { 26 // Load a form. 27 LoadHTML("<html><form id='myForm'><input name='fname' value='Rick'/>" 28 "<input name='lname' value='Deckard'/></form></html>"); 29 30 // Submit the form. 31 ExecuteJavaScript("document.getElementById('myForm').submit();"); 32 ProcessPendingMessages(); 33 34 const IPC::Message* message = render_thread_->sink().GetFirstMessageMatching( 35 AutofillHostMsg_FormSubmitted::ID); 36 ASSERT_TRUE(message != NULL); 37 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()); 42 43 FormFieldData& form_field = forms.a.fields[0]; 44 EXPECT_EQ(WebString("fname"), form_field.name); 45 EXPECT_EQ(WebString("Rick"), form_field.value); 46 47 form_field = forms.a.fields[1]; 48 EXPECT_EQ(WebString("lname"), form_field.name); 49 EXPECT_EQ(WebString("Deckard"), form_field.value); 50 } 51 52 // Tests that submitting a form that has autocomplete="off" does not generate a 53 // FormSubmitted message. 54 TEST_F(FormAutocompleteTest, AutoCompleteOffFormSubmit) { 55 // Load a form. 56 LoadHTML("<html><form id='myForm' autocomplete='off'>" 57 "<input name='fname' value='Rick'/>" 58 "<input name='lname' value='Deckard'/>" 59 "</form></html>"); 60 61 // Submit the form. 62 ExecuteJavaScript("document.getElementById('myForm').submit();"); 63 ProcessPendingMessages(); 64 65 // No FormSubmitted message should have been sent. 66 EXPECT_FALSE(render_thread_->sink().GetFirstMessageMatching( 67 AutofillHostMsg_FormSubmitted::ID)); 68 } 69 70 // Tests that fields with autocomplete off are not submitted. 71 TEST_F(FormAutocompleteTest, AutoCompleteOffInputSubmit) { 72 // Load a form. 73 LoadHTML("<html><form id='myForm'>" 74 "<input name='fname' value='Rick'/>" 75 "<input name='lname' value='Deckard' autocomplete='off'/>" 76 "</form></html>"); 77 78 // Submit the form. 79 ExecuteJavaScript("document.getElementById('myForm').submit();"); 80 ProcessPendingMessages(); 81 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); 86 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()); 91 92 FormFieldData& form_field = forms.a.fields[0]; 93 EXPECT_EQ(WebString("fname"), form_field.name); 94 EXPECT_EQ(WebString("Rick"), form_field.value); 95 } 96 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>"); 103 104 WebKit::WebElement element = 105 GetMainFrame()->document().getElementById(WebKit::WebString("myForm")); 106 ASSERT_FALSE(element.isNull()); 107 WebKit::WebFormElement form = element.to<WebKit::WebFormElement>(); 108 EXPECT_TRUE(form.autoComplete()); 109 110 // Dynamically mark the form as autocomplete off. 111 ExecuteJavaScript("document.getElementById('myForm')." 112 "setAttribute('autocomplete', 'off');"); 113 ProcessPendingMessages(); 114 EXPECT_FALSE(form.autoComplete()); 115 116 // Submit the form. 117 ExecuteJavaScript("document.getElementById('myForm').submit();"); 118 ProcessPendingMessages(); 119 120 // No FormSubmitted message should have been sent. 121 EXPECT_FALSE(render_thread_->sink().GetFirstMessageMatching( 122 AutofillHostMsg_FormSubmitted::ID)); 123 } 124 125 } // namespace autofill 126