Skip to content

Commit

Permalink
Remove set_autofill_client_for_test in PasswordAutofillManager.
Browse files Browse the repository at this point in the history
There is no need for this test-only method - instead, use Autofill's
injector mechanism to inject an ObservingAutofillClient directly
into the WebContents.

Bug: 1473235
Change-Id: I265faec3a560a3b01058e42e8240f33afceb2ec6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4777761
Reviewed-by: Christoph Schwering <schwering@google.com>
Reviewed-by: Vasilii Sukhanov <vasilii@chromium.org>
Commit-Queue: Jan Keitel <jkeitel@google.com>
Cr-Commit-Position: refs/heads/main@{#1184699}
  • Loading branch information
Jan Keitel authored and Chromium LUCI CQ committed Aug 17, 2023
1 parent 102b122 commit 27b9c79
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/autofill/content/browser/content_autofill_client.h"
#include "components/autofill/content/browser/test_autofill_client_injector.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/test_autofill_client.h"
#include "components/autofill/core/common/autofill_features.h"
Expand Down Expand Up @@ -155,6 +156,20 @@ class PasswordGenerationInteractiveTest
TestGenerationPopupObserver observer_;
};

// A test fixture that injects an `ObservingAutofillClient` into newly created
// tabs to allow waiting for an Autofill popup to open.
class PasswordGenerationAutofillPopupInteractiveTest
: public PasswordGenerationInteractiveTest {
protected:
ObservingAutofillClient& autofill_client() {
return *autofill_client_injector_[WebContents()];
}

private:
autofill::TestAutofillClientInjector<ObservingAutofillClient>
autofill_client_injector_;
};

IN_PROC_BROWSER_TEST_F(PasswordGenerationInteractiveTest,
PopupShownAndPasswordSelected) {
FocusPasswordField();
Expand Down Expand Up @@ -243,7 +258,7 @@ IN_PROC_BROWSER_TEST_F(PasswordGenerationInteractiveTest,
// Verify that password generation popup is hidden when popup
// with generation and password suggestions is visible.
IN_PROC_BROWSER_TEST_F(
PasswordGenerationInteractiveTest,
PasswordGenerationAutofillPopupInteractiveTest,
HidesGenerationPopupWhenShowingPasswordSuggestionsWithGeneration) {
// Save the credentials since the autofill popup with generation and
// password suggestion would not appear without stored passwords.
Expand All @@ -267,22 +282,11 @@ IN_PROC_BROWSER_TEST_F(
WaitForStatus(TestGenerationPopupObserver::GenerationPopup::kShown);
EXPECT_TRUE(GenerationPopupShowing());

password_manager::ContentPasswordManagerDriverFactory* driver_factory =
password_manager::ContentPasswordManagerDriverFactory::FromWebContents(
WebContents());
ObservingAutofillClient::CreateForWebContents(WebContents());
ObservingAutofillClient* observing_autofill_client =
ObservingAutofillClient::FromWebContents(WebContents());
password_manager::ContentPasswordManagerDriver* driver =
driver_factory->GetDriverForFrame(WebContents()->GetPrimaryMainFrame());
driver->GetPasswordAutofillManager()->set_autofill_client_for_test(
observing_autofill_client);

// Click on the password field to display the autofill popup.
content::SimulateMouseClickOrTapElementWithId(WebContents(),
"password_field");
// Make sure the autofill popup would be shown.
observing_autofill_client->WaitForAutofillPopup();
// Make sure that the autofill popup is showing.
autofill_client().WaitForAutofillPopup();
// Make sure the generation popup is dismissed.
WaitForStatus(TestGenerationPopupObserver::GenerationPopup::kHidden);
}
Expand Down
36 changes: 18 additions & 18 deletions chrome/browser/password_manager/password_manager_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#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/test_autofill_client_injector.h"
#include "components/autofill/content/common/mojom/autofill_driver.mojom-test-utils.h"
#include "components/autofill/content/common/mojom/autofill_driver.mojom.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
Expand Down Expand Up @@ -141,6 +142,20 @@ class PasswordManagerBrowserTest : public PasswordManagerBrowserTestBase {
~PasswordManagerBrowserTest() override = default;
};

// A test fixture that injects an `ObservingAutofillClient` into newly created
// tabs to allow waiting for an Autofill popup to open.
class PasswordManagerAutofillPopupBrowserTest
: public PasswordManagerBrowserTest {
protected:
ObservingAutofillClient& autofill_client() {
return *autofill_client_injector_[WebContents()];
}

private:
autofill::TestAutofillClientInjector<ObservingAutofillClient>
autofill_client_injector_;
};

// This fixture enables communication to the Autofill crowdsourcing server, but
// denies any such requests.
class PasswordManagerVotingBrowserTest : public PasswordManagerBrowserTest {
Expand Down Expand Up @@ -2017,7 +2032,7 @@ IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
EXPECT_FALSE(prompt_observer.IsSavePromptShownAutomatically());
}

IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
IN_PROC_BROWSER_TEST_F(PasswordManagerAutofillPopupBrowserTest,
InFrameNavigationDoesNotClearPopupState) {
password_manager::PasswordStoreInterface* password_store =
PasswordStoreFactory::GetForProfile(browser()->profile(),
Expand All @@ -2030,21 +2045,6 @@ IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
password_store->AddLogin(signin_form);

NavigateToFile("/password/password_form.html");

// Mock out the AutofillClient so we know how long to wait. Unfortunately
// there isn't otherwise a good event to wait on to verify that the popup
// would have been shown.
password_manager::ContentPasswordManagerDriverFactory* driver_factory =
password_manager::ContentPasswordManagerDriverFactory::FromWebContents(
WebContents());
ObservingAutofillClient::CreateForWebContents(WebContents());
ObservingAutofillClient* observing_autofill_client =
ObservingAutofillClient::FromWebContents(WebContents());
password_manager::ContentPasswordManagerDriver* driver =
driver_factory->GetDriverForFrame(WebContents()->GetPrimaryMainFrame());
driver->GetPasswordAutofillManager()->set_autofill_client_for_test(
observing_autofill_client);

// Trigger in page navigation.
std::string in_page_navigate = "location.hash = '#blah';";
ASSERT_TRUE(content::ExecJs(RenderFrameHost(), in_page_navigate,
Expand All @@ -2053,8 +2053,8 @@ IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
// Click on the username field to display the popup.
content::SimulateMouseClickOrTapElementWithId(WebContents(),
"username_field");
// Make sure the popup would be shown.
observing_autofill_client->WaitForAutofillPopup();
// Make sure that the popup is showing.
autofill_client().WaitForAutofillPopup();
}

IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, ChangePwdFormBubbleShown) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "chrome/browser/password_manager/password_manager_uitest_util.h"

#include "chrome/browser/ui/autofill/chrome_autofill_client.h"
#include "testing/gtest/include/gtest/gtest.h"

void TestGenerationPopupObserver::OnPopupShown(
Expand Down Expand Up @@ -52,6 +53,10 @@ void TestGenerationPopupObserver::MaybeQuitRunLoop() {
}
}

ObservingAutofillClient::ObservingAutofillClient(
content::WebContents* web_contents)
: autofill::ChromeAutofillClient(web_contents) {}

void ObservingAutofillClient::WaitForAutofillPopup() {
base::RunLoop run_loop;
run_loop_ = &run_loop;
Expand All @@ -66,5 +71,3 @@ void ObservingAutofillClient::ShowAutofillPopup(
run_loop_->Quit();
run_loop_ = nullptr;
}

WEB_CONTENTS_USER_DATA_KEY_IMPL(ObservingAutofillClient);
17 changes: 3 additions & 14 deletions chrome/browser/password_manager/password_manager_uitest_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "chrome/browser/ui/autofill/chrome_autofill_client.h"
#include "chrome/browser/ui/passwords/password_generation_popup_controller.h"
#include "chrome/browser/ui/passwords/password_generation_popup_observer.h"
#include "components/autofill/core/browser/test_autofill_client.h"

using GenerationUIState = PasswordGenerationPopupController::GenerationUIState;

Expand Down Expand Up @@ -51,14 +50,10 @@ class TestGenerationPopupObserver : public PasswordGenerationPopupObserver {
PasswordGenerationPopupController::kOfferGeneration;
};

// A test AutofillClient client that can block until
// the ShowAutofillPopup() is called.
class ObservingAutofillClient
: public autofill::TestAutofillClient,
public content::WebContentsUserData<ObservingAutofillClient> {
// An Autofill client that can block until ShowAutofillPopup() is called.
class ObservingAutofillClient : public autofill::ChromeAutofillClient {
public:
ObservingAutofillClient(const ObservingAutofillClient&) = delete;
ObservingAutofillClient& operator=(const ObservingAutofillClient&) = delete;
explicit ObservingAutofillClient(content::WebContents* web_contents);

// Blocks the current thread until ShowAutofillPopup() is called.
void WaitForAutofillPopup();
Expand All @@ -68,13 +63,7 @@ class ObservingAutofillClient
base::WeakPtr<autofill::AutofillPopupDelegate> delegate) override;

private:
explicit ObservingAutofillClient(content::WebContents* web_contents)
: content::WebContentsUserData<ObservingAutofillClient>(*web_contents) {}
friend class content::WebContentsUserData<ObservingAutofillClient>;

raw_ptr<base::RunLoop> run_loop_ = nullptr;

WEB_CONTENTS_USER_DATA_KEY_DECL();
};

#endif // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_MANAGER_UITEST_UTIL_H_
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,4 @@ ContentPasswordManagerDriverFactoryTestApi::
ContentPasswordManagerDriverFactory* factory)
: factory_(factory) {}

void ContentPasswordManagerDriverFactoryTestApi::SetAutofillClient(
autofill::AutofillClient* autofill_client) {
factory_->autofill_client_ = autofill_client;
for (auto& [rfh, driver] : factory_->frame_driver_map_) {
driver.GetPasswordAutofillManager()->set_autofill_client_for_test(
autofill_client);
}
}

} // namespace password_manager
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ class ContentPasswordManagerDriverFactoryTestApi {
explicit ContentPasswordManagerDriverFactoryTestApi(
ContentPasswordManagerDriverFactory* factory);

void SetAutofillClient(autofill::AutofillClient* autofill_client);

private:
raw_ptr<ContentPasswordManagerDriverFactory> factory_;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,6 @@ class PasswordAutofillManager : public autofill::AutofillPopupDelegate {
// A public version of PreviewSuggestion(), only for use in tests.
bool PreviewSuggestionForTest(const std::u16string& username);

void set_autofill_client_for_test(autofill::AutofillClient* autofill_client) {
autofill_client_ = autofill_client;
}

private:
using ForPasswordField = base::StrongAlias<class ForPasswordFieldTag, bool>;
using OffersGeneration = base::StrongAlias<class OffersGenerationTag, bool>;
Expand Down Expand Up @@ -224,7 +220,7 @@ class PasswordAutofillManager : public autofill::AutofillPopupDelegate {
// The driver that owns |this|.
const raw_ptr<PasswordManagerDriver> password_manager_driver_;

raw_ptr<autofill::AutofillClient> autofill_client_;
const raw_ptr<autofill::AutofillClient> autofill_client_;

const raw_ptr<PasswordManagerClient> password_client_;

Expand Down

0 comments on commit 27b9c79

Please sign in to comment.