// Copyright 2023 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_CONTENT_BROWSER_TEST_AUTOFILL_DRIVER_INJECTOR_H_ #define COMPONENTS_AUTOFILL_CONTENT_BROWSER_TEST_AUTOFILL_DRIVER_INJECTOR_H_ #include <concepts> #include <map> #include <memory> #include "components/autofill/content/browser/content_autofill_client.h" #include "components/autofill/content/browser/content_autofill_driver.h" #include "components/autofill/content/browser/content_autofill_driver_factory.h" #include "components/autofill/content/browser/content_autofill_driver_factory_test_api.h" #include "components/autofill/content/browser/content_autofill_driver_test_api.h" #include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents_observer.h" #include "content/public/test/browser_test_utils.h" namespace autofill { // Asserts that at construction time, no other TestAutofillDriverInjector and no // TestAutofillManagerInjector are alive. class TestAutofillDriverInjectorBase { … }; // RAII type that installs new AutofillDrivers of type `T` in all newly // navigated frames in all newly created WebContents. // // The injector only injects an AutofillDriver if a driver would also be created // Especially in unit tests it may be necessary to do a navigation to create the // driver, for example with // NavigateAndCommit(GURL("about:blank")) // or force-create the driver manually with // client->GetAutofillDriverFactory().DriverForFrame(rfh). // // The driver's AutofillManager is a fresh BrowserAutofillManager. // // To prevent hard-to-find bugs, only one TestAutofillDriverInjector may be // alive at a time. It must not be created before a TestAutofillClientInjector // and not after a TestAutofillManagerInjector. These conditions are CHECKed. // // Usage: // // class AutofillFooTest : public ... { // public: // TestAutofillDriver* autofill_driver(content::RenderFrameHost* rfh) { // return autofill_driver_injector_[rfh]; // } // // private: // TestAutofillDriverInjector<TestAutofillDriver> autofill_driver_injector_; // }; template <std::derived_from<ContentAutofillDriver> T> class TestAutofillDriverInjector : public TestAutofillDriverInjectorBase { … }; } // namespace autofill #endif // COMPONENTS_AUTOFILL_CONTENT_BROWSER_TEST_AUTOFILL_DRIVER_INJECTOR_H_