// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "chrome/common/render_messages.h" #include "chrome/test/render_view_test.h" #include "testing/gtest/include/gtest/gtest.h" #include "third_party/WebKit/WebKit/chromium/public/WebDocument.h" #include "third_party/WebKit/WebKit/chromium/public/WebString.h" #include "third_party/WebKit/WebKit/chromium/public/WebURLError.h" using WebKit::WebCompositionCommand; using WebKit::WebFrame; using WebKit::WebString; using WebKit::WebTextDirection; using WebKit::WebURLError; class FormAutocompleteTest : public RenderViewTest { }; // Tests that submitting a form generates a FormFieldValuesSubmitted message // with the form fields. TEST_F(FormAutocompleteTest, NormalFormSubmit) { // Load a form. LoadHTML("
" "
"); // Submit the form. ExecuteJavaScript("document.getElementById('myForm').submit();"); ProcessPendingMessages(); const IPC::Message* message = render_thread_.sink().GetFirstMessageMatching( ViewHostMsg_FormFieldValuesSubmitted::ID); ASSERT_TRUE(message != NULL); Tuple1 form_fields; ViewHostMsg_FormFieldValuesSubmitted::Read(message, &form_fields); ASSERT_EQ(2U, form_fields.a.elements.size()); webkit_glue::FormField& form_field = form_fields.a.elements[0]; EXPECT_EQ(WebString("fname"), form_field.name()); EXPECT_EQ(WebString("Rick"), form_field.value()); form_field = form_fields.a.elements[1]; EXPECT_EQ(WebString("lname"), form_field.name()); EXPECT_EQ(WebString("Deckard"), form_field.value()); } // Tests that submitting a form that has autocomplete="off" does not generate a // FormFieldValuesSubmitted message. TEST_F(FormAutocompleteTest, AutoCompleteOffFormSubmit) { // Load a form. LoadHTML("
" "" "" "
"); // Submit the form. ExecuteJavaScript("document.getElementById('myForm').submit();"); ProcessPendingMessages(); // No FormFieldValuesSubmitted message should have been sent. EXPECT_FALSE(render_thread_.sink().GetFirstMessageMatching( ViewHostMsg_FormFieldValuesSubmitted::ID)); } // Tests that fields with autocomplete off are not submitted. TEST_F(FormAutocompleteTest, AutoCompleteOffInputSubmit) { // Load a form. LoadHTML("
" "" "" "
"); // Submit the form. ExecuteJavaScript("document.getElementById('myForm').submit();"); ProcessPendingMessages(); // No FormFieldValuesSubmitted message should have been sent. const IPC::Message* message = render_thread_.sink().GetFirstMessageMatching( ViewHostMsg_FormFieldValuesSubmitted::ID); ASSERT_TRUE(message != NULL); Tuple1 form_fields; ViewHostMsg_FormFieldValuesSubmitted::Read(message, &form_fields); ASSERT_EQ(1U, form_fields.a.elements.size()); webkit_glue::FormField& form_field = form_fields.a.elements[0]; EXPECT_EQ(WebString("fname"), form_field.name()); EXPECT_EQ(WebString("Rick"), form_field.value()); } // Tests that submitting a form that has been dynamically set as autocomplete // off does not generate a FormFieldValuesSubmitted message. // http://crbug.com/36520 // TODO(jcampan): reenable when WebKit bug 35823 is fixed. TEST_F(FormAutocompleteTest, DISABLED_DynamicAutoCompleteOffFormSubmit) { LoadHTML("
" "
"); WebKit::WebElement element = GetMainFrame()->document().getElementById(WebKit::WebString("myForm")); ASSERT_FALSE(element.isNull()); WebKit::WebFormElement form = element.toElement(); EXPECT_TRUE(form.autoComplete()); // Dynamically mark the form as autocomplete off. ExecuteJavaScript("document.getElementById('myForm').autocomplete='off';"); ProcessPendingMessages(); EXPECT_FALSE(form.autoComplete()); // Submit the form. ExecuteJavaScript("document.getElementById('myForm').submit();"); ProcessPendingMessages(); // No FormFieldValuesSubmitted message should have been sent. EXPECT_FALSE(render_thread_.sink().GetFirstMessageMatching( ViewHostMsg_FormFieldValuesSubmitted::ID)); }