0

Add FormFillerTestApi

Previously, private members/methods of FormFiller were accessed through
BAMTestApi, which is not optimal.

This CL creates a FormFillerTestApi class for improved clarity.

Bug: 41490871
Change-Id: I6a45694312a75dd66d0c1ef8624d9984f7f9de88
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5509491
Auto-Submit: Jihad Hanna <jihadghanna@google.com>
Commit-Queue: Jihad Hanna <jihadghanna@google.com>
Reviewed-by: Florian Leimgruber <fleimgruber@google.com>
Commit-Queue: Florian Leimgruber <fleimgruber@google.com>
Cr-Commit-Position: refs/heads/main@{#1295381}
This commit is contained in:
Jihad Hanna
2024-05-02 11:46:15 +00:00
committed by Chromium LUCI CQ
parent f4fd06124f
commit 74e46efd79
9 changed files with 56 additions and 49 deletions

@ -80,7 +80,8 @@ class TestAutofillManager : public BrowserAutofillManager {
public:
explicit TestAutofillManager(ContentAutofillDriver* driver)
: BrowserAutofillManager(driver, "en-US") {
test_api(*this).set_limit_before_refill(base::Hours(1));
test_api(test_api(*this).form_filler())
.set_limit_before_refill(base::Hours(1));
}
static TestAutofillManager& GetForRenderFrameHost(

@ -2640,7 +2640,7 @@ class AutofillInteractiveTestDynamicForm : public AutofillInteractiveTest {
public:
void SetUpOnMainThread() override {
AutofillInteractiveTest::SetUpOnMainThread();
test_api(*GetBrowserAutofillManager())
test_api(test_api(*GetBrowserAutofillManager()).form_filler())
.set_limit_before_refill(base::Hours(1));
}
@ -2735,7 +2735,7 @@ IN_PROC_BROWSER_TEST_F(AutofillInteractiveTestDynamicForm,
DynamicChangingFormFill_AfterDelay) {
// Lower the refill limit, so the test doesn't have to wait forever.
constexpr base::TimeDelta kLimitBeforeRefillForTest = base::Milliseconds(100);
test_api(*GetBrowserAutofillManager())
test_api(test_api(*GetBrowserAutofillManager()).form_filler())
.set_limit_before_refill(kLimitBeforeRefillForTest);
CreateTestProfile();

@ -148,7 +148,7 @@ void AutofillUiTest::SetUpOnMainThread() {
// Refills normally only happen if the form changes within 1 second of the
// initial fill. On a slow bot, this may lead to flakiness. We hence set a
// very high limit.
test_api(*GetBrowserAutofillManager())
test_api(test_api(*GetBrowserAutofillManager()).form_filler())
.set_limit_before_refill(base::Hours(1));
autofill_driver_factory_observation_.Observe(
client->GetAutofillDriverFactory());
@ -324,7 +324,9 @@ void AutofillUiTest::OnContentAutofillDriverCreated(
// Refills normally only happen if the form changes within 1 second of the
// initial fill. On a slow bot, this may lead to flakiness. We hence set a
// very high limit.
test_api(static_cast<BrowserAutofillManager&>(driver.GetAutofillManager()))
test_api(test_api(static_cast<BrowserAutofillManager&>(
driver.GetAutofillManager()))
.form_filler())
.set_limit_before_refill(base::Hours(1));
}

@ -850,6 +850,7 @@ static_library("test_support") {
"data_model/test_autofill_data_model.cc",
"data_model/test_autofill_data_model.h",
"form_data_importer_test_api.h",
"form_filler_test_api.h",
"form_forest_test_api.cc",
"form_forest_test_api.h",
"form_structure_test_api.cc",

@ -12,6 +12,7 @@
#include "components/autofill/core/browser/autofill_trigger_details.h"
#include "components/autofill/core/browser/browser_autofill_manager.h"
#include "components/autofill/core/browser/filling_product.h"
#include "components/autofill/core/browser/form_filler_test_api.h"
#include "components/autofill/core/browser/payments/credit_card_access_manager.h"
#include "components/autofill/core/browser/single_field_form_fill_router.h"
#include "testing/gtest/include/gtest/gtest.h"
@ -39,23 +40,6 @@ class BrowserAutofillManagerTestApi : public AutofillManagerTestApi {
return manager_->external_delegate_.get();
}
void set_limit_before_refill(base::TimeDelta limit) {
manager_->form_filler_->limit_before_refill_ = limit;
}
// TODO(crbug.com/41490871): Remove.
bool ShouldTriggerRefill(const FormStructure& form_structure,
RefillTriggerReason refill_trigger_reason) {
return manager_->form_filler_->ShouldTriggerRefill(form_structure,
refill_trigger_reason);
}
// TODO(crbug.com/41490871): Remove.
void TriggerRefill(const FormData& form,
const AutofillTriggerDetails& trigger_details) {
manager_->form_filler_->TriggerRefill(form, trigger_details);
}
void PreProcessStateMatchingTypes(
const std::vector<AutofillProfile>& profiles,
FormStructure* form_structure) {
@ -94,22 +78,6 @@ class BrowserAutofillManagerTestApi : public AutofillManagerTestApi {
manager_->OnCreditCardFetched(result, credit_card);
}
// TODO(crbug.com/41490871): Remove.
void FillOrPreviewDataModelForm(
mojom::ActionPersistence action_persistence,
const FormData& form,
const FormFieldData& field,
absl::variant<const AutofillProfile*, const CreditCard*>
profile_or_credit_card,
base::optional_ref<const std::u16string> cvc,
FormStructure* form_structure,
AutofillField* autofill_field) {
return manager_->form_filler_->FillOrPreviewForm(
action_persistence, form, field, profile_or_credit_card, cvc,
form_structure, autofill_field,
{.trigger_source = AutofillTriggerSource::kPopup});
}
base::flat_map<std::string, VirtualCardUsageData::VirtualCardLastFour>
GetVirtualCreditCardsForStandaloneCvcField(const url::Origin& origin) {
return manager_->GetVirtualCreditCardsForStandaloneCvcField(origin);
@ -133,15 +101,7 @@ class BrowserAutofillManagerTestApi : public AutofillManagerTestApi {
consider_form_as_secure_for_testing;
}
// TODO(crbug.com/41490871): Remove.
void AddFormFillEntry(
base::span<const FormFieldData* const> filled_fields,
base::span<const AutofillField* const> filled_autofill_fields,
FillingProduct filling_product,
bool is_refill) {
manager_->form_filler_->form_autofill_history_.AddFormFillEntry(
filled_fields, filled_autofill_fields, filling_product, is_refill);
}
FormFiller& form_filler() { return *manager_->form_filler_; }
void set_form_filler(std::unique_ptr<FormFiller> form_filler) {
manager_->form_filler_ = std::move(form_filler);

@ -204,7 +204,7 @@ class FormFiller {
const AutofillTriggerDetails& trigger_details);
private:
friend class BrowserAutofillManagerTestApi;
friend class FormFillerTestApi;
// Keeps track of the filling context for a form, used to make refill
// attempts.

@ -0,0 +1,41 @@
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_FORM_FILLER_TEST_API_H_
#define COMPONENTS_AUTOFILL_CORE_BROWSER_FORM_FILLER_TEST_API_H_
#include "components/autofill/core/browser/form_filler.h"
namespace autofill {
// Exposes some testing operations for BrowserAutofillManager.
class FormFillerTestApi {
public:
explicit FormFillerTestApi(FormFiller* form_filler)
: form_filler_(*form_filler) {}
void set_limit_before_refill(base::TimeDelta limit) {
form_filler_->limit_before_refill_ = limit;
}
void AddFormFillEntry(
base::span<const FormFieldData* const> filled_fields,
base::span<const AutofillField* const> filled_autofill_fields,
FillingProduct filling_product,
bool is_refill) {
form_filler_->form_autofill_history_.AddFormFillEntry(
filled_fields, filled_autofill_fields, filling_product, is_refill);
}
private:
raw_ref<FormFiller> form_filler_;
};
inline FormFillerTestApi test_api(FormFiller& form_filler) {
return FormFillerTestApi(&form_filler);
}
} // namespace autofill
#endif // COMPONENTS_AUTOFILL_CORE_BROWSER_FORM_FILLER_TEST_API_H_

@ -501,7 +501,7 @@ TEST_F(FormFillerTest, UndoResetsCachedAutofillState) {
FormFieldData* field_ptr = &form.fields.front();
AutofillField* autofill_field_ptr = &filled_autofill_field;
form.fields.front().set_is_autofilled(false);
test_api(*browser_autofill_manager_)
test_api(test_api(*browser_autofill_manager_).form_filler())
.AddFormFillEntry(base::make_span(&field_ptr, 1u),
base::make_span(&autofill_field_ptr, 1u),
FillingProduct::kAddress, /*is_refill=*/false);

@ -5606,6 +5606,7 @@ TEST_F(AutofillMetricsTest, DynamicFormMetrics) {
// Simulate checking whether to fill a dynamic form before the form was filled
// initially.
test_api(autofill_manager())
.form_filler()
.ShouldTriggerRefill(FormStructure(form),
RefillTriggerReason::kFormChanged);
histogram_tester.ExpectTotalCount("Autofill.FormEvents.Address", 0);
@ -5619,6 +5620,7 @@ TEST_F(AutofillMetricsTest, DynamicFormMetrics) {
// Simulate checking whether to fill a dynamic form after the form was filled
// initially.
test_api(autofill_manager())
.form_filler()
.ShouldTriggerRefill(FormStructure(form),
RefillTriggerReason::kFormChanged);
EXPECT_THAT(histogram_tester.GetAllSamples("Autofill.FormEvents.Address"),