chromium/chrome/renderer/autofill/form_autocomplete_browsertest.cc

// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>
#include <tuple>

#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/test/base/chrome_render_view_test.h"
#include "components/autofill/content/renderer/autofill_agent.h"
#include "components/autofill/content/renderer/autofill_agent_test_api.h"
#include "components/autofill/content/renderer/focus_test_utils.h"
#include "components/autofill/content/renderer/form_autofill_util.h"
#include "components/autofill/content/renderer/form_tracker_test_api.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/autofill_test_utils.h"
#include "components/autofill/core/common/form_data.h"
#include "components/autofill/core/common/form_data_test_api.h"
#include "components/autofill/core/common/mojom/autofill_types.mojom-shared.h"
#include "content/public/renderer/render_frame.h"
#include "mojo/public/cpp/bindings/associated_receiver_set.h"
#include "mojo/public/cpp/bindings/pending_associated_receiver.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/metrics/document_update_reason.h"
#include "third_party/blink/public/web/web_document.h"
#include "third_party/blink/public/web/web_element.h"
#include "third_party/blink/public/web/web_form_element.h"
#include "third_party/blink/public/web/web_frame_widget.h"
#include "third_party/blink/public/web/web_input_element.h"
#include "third_party/blink/public/web/web_local_frame.h"

WebDocument;
WebElement;
WebFormControlElement;
WebInputElement;
WebString;

namespace autofill {

SubmissionSource;

namespace {

class FakeContentAutofillDriver : public mojom::AutofillDriver {};

// Helper function to verify the form-related messages received from the
// renderer. The same data is expected in both messages. Depending on
// `expect_submitted_message`, will verify presence of FormSubmitted message.
void VerifyReceivedRendererMessages(
    const FakeContentAutofillDriver& fake_driver,
    const std::string& fname,
    const std::string& lname,
    bool expect_known_success,
    SubmissionSource expect_submission_source) {}

void VerifyReceivedAddressRendererMessages(
    const FakeContentAutofillDriver& fake_driver,
    const std::string& address,
    bool expect_known_success,
    SubmissionSource expect_submission_source) {}

// Helper function to verify that NO form-related messages are received from the
// renderer.
void VerifyNoSubmitMessagesReceived(
    const FakeContentAutofillDriver& fake_driver) {}

// TODO(crbug.com/41495779): Update.
FormData CreateAutofillFormData(blink::WebLocalFrame* main_frame) {}

std::vector<FormFieldData::FillData> GetFieldsForFilling(
    const std::vector<FormData>& forms) {}

class FormAutocompleteTest : public ChromeRenderViewTest {};

// Tests that correct focus, change and blur events are emitted during the
// autofilling process when there is an initial focused element in a form
// having non-fillable fields.
TEST_F(FormAutocompleteTest, VerifyFocusAndBlurEventsAfterAutofill) {}

// Tests that correct focus, change and blur events are emitted during the
// autofilling process when there is an initial focused element.
TEST_F(FormAutocompleteTest,
       VerifyFocusAndBlurEventsAfterAutofillWithFocusedElement) {}

// Tests that correct focus, change and blur events are emitted during the
// autofilling process when there is an initial focused element in a form having
// single field.
TEST_F(FormAutocompleteTest,
       VerifyFocusAndBlurEventAfterAutofillWithFocusedElementForSingleElement) {}

// Tests that a field is added to the form between the times of triggering
// and executing the filling.
TEST_F(FormAutocompleteTest, VerifyFocusAndBlurEventAfterElementAdded) {}

// Tests that a field is removed from the form between the times of
// triggering and executing the filling.
TEST_F(FormAutocompleteTest, VerifyFocusAndBlurEventAfterElementRemoved) {}

// Unit test for AutofillAgent::AcceptDataListSuggestion.
TEST_F(FormAutocompleteTest, AcceptDataListSuggestion) {}

TEST_F(FormAutocompleteTest, SelectControlChanged) {}

// Parameterized test for submission detection. The parameter dictates whether
// the tests run with `kAutofillReplaceFormElementObserver` enabled or not.
class FormAutocompleteSubmissionTest : public FormAutocompleteTest,
                                       public testing::WithParamInterface<int> {};

INSTANTIATE_TEST_SUITE_P();

// Tests that submitting a form generates FormSubmitted message with the form
// fields.
TEST_P(FormAutocompleteSubmissionTest, NormalFormSubmit) {}

// Tests that FormSubmitted message is generated even the submit event isn't
// propagated by Javascript.
TEST_P(FormAutocompleteSubmissionTest, SubmitEventPrevented) {}

// Tests that having the form disappear after autofilling triggers submission
// from Autofill's point of view.
TEST_P(FormAutocompleteSubmissionTest, DomMutationAfterAutofill) {}

// Tests that completing an Ajax request and having the form disappear will
// trigger submission from Autofill's point of view.
TEST_P(FormAutocompleteSubmissionTest, AjaxSucceeded_NoLongerVisible) {}

// Tests that completing an Ajax request and having the form with a specific
// action disappear will trigger submission from Autofill's point of view, even
// if there is another form with the same data but different action on the page.
TEST_P(FormAutocompleteSubmissionTest,
       AjaxSucceeded_NoLongerVisible_DifferentActionsSameData) {}

// Tests that completing an Ajax request and having the form with no action
// specified disappear will trigger submission from Autofill's point of view,
// even if there is still another form with no action in the page. It will
// compare field data within the forms.
// TODO(kolos) Re-enable when the implementation of IsFormVisible is on-par
// for these platforms.
#if BUILDFLAG(IS_MAC)
#define MAYBE_NoLongerVisibleBothNoActions
#else
#define MAYBE_NoLongerVisibleBothNoActions
#endif
TEST_P(FormAutocompleteSubmissionTest, MAYBE_NoLongerVisibleBothNoActions) {}

// Tests that completing an Ajax request and having the form with no action
// specified disappear will trigger submission from Autofill's point of view.
TEST_P(FormAutocompleteSubmissionTest, AjaxSucceeded_NoLongerVisible_NoAction) {}

// Tests that completing an Ajax request but leaving a form visible will not
// trigger submission from Autofill's point of view.
TEST_P(FormAutocompleteSubmissionTest, AjaxSucceeded_StillVisible) {}

// Tests that completing an Ajax request without any prior form interaction
// does not trigger form submission from Autofill's point of view.
TEST_P(FormAutocompleteSubmissionTest,
       AjaxSucceeded_NoFormInteractionInvisible) {}

// Tests that completing an Ajax request after having autofilled a form,
// with the form disappearing, will trigger submission from Autofill's
// point of view.
TEST_P(FormAutocompleteSubmissionTest, AjaxSucceeded_FilledFormIsInvisible) {}

// Tests that completing an Ajax request after having autofilled a form,
// without the form disappearing, will not trigger submission from Autofill's
// point of view.
TEST_P(FormAutocompleteSubmissionTest, AjaxSucceeded_FilledFormStillVisible) {}

// Tests that completing an Ajax request without a form present will still
// trigger submission, if all the inputs the user has modified disappear.
TEST_P(FormAutocompleteSubmissionTest, AjaxSucceeded_FormlessElements) {}

// Tests that submitting a form that has autocomplete="off" generates
// WillSubmitForm and FormSubmitted messages.
TEST_P(FormAutocompleteSubmissionTest, AutoCompleteOffFormSubmit) {}

// Tests that fields with autocomplete off are submitted.
TEST_P(FormAutocompleteSubmissionTest, AutoCompleteOffInputSubmit) {}

// Tests that submitting a form that has been dynamically set as autocomplete
// off generates WillSubmitForm and FormSubmitted messages.
// Note: We previously did the opposite, for bug http://crbug.com/36520
TEST_P(FormAutocompleteSubmissionTest, DynamicAutoCompleteOffFormSubmit) {}

TEST_P(FormAutocompleteSubmissionTest, FormSubmittedByDOMMutationAfterXHR) {}

TEST_P(FormAutocompleteSubmissionTest, FormSubmittedBySameDocumentNavigation) {}

TEST_P(FormAutocompleteSubmissionTest, FormSubmittedByProbablyFormSubmitted) {}

}  // namespace

}  // namespace autofill