0

Avoid dangling pointer in AutofillPopupController test.

The current lifetime behavior of AutofillPopupControllerImpl is such
that it deletes itself on hiding. Therefore one should only keep
weak pointers to it.

Bug: 1419786
Change-Id: I43e10965effacf746c06fa827f1f8a22f2e081f1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4807484
Commit-Queue: Christoph Schwering <schwering@google.com>
Auto-Submit: Jan Keitel <jkeitel@google.com>
Reviewed-by: Christoph Schwering <schwering@google.com>
Cr-Commit-Position: refs/heads/main@{#1187336}
This commit is contained in:
Jan Keitel
2023-08-23 17:06:34 +00:00
committed by Chromium LUCI CQ
parent df84a6cc4f
commit 7555bb62a5

@ -8,7 +8,6 @@
#include <string> #include <string>
#include <utility> #include <utility>
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h" #include "base/test/metrics/histogram_tester.h"
@ -222,17 +221,21 @@ class AutofillPopupControllerUnitTest : public ChromeRenderViewHostTestHarness {
autofill_popup_view_ = std::make_unique<NiceMock<MockAutofillPopupView>>(); autofill_popup_view_ = std::make_unique<NiceMock<MockAutofillPopupView>>();
#if BUILDFLAG(IS_ANDROID) #if BUILDFLAG(IS_ANDROID)
autofill_popup_controller_ = new NiceMock<TestAutofillPopupController>( autofill_popup_controller_ =
external_delegate_->GetWeakPtrForTest(), web_contents(), gfx::RectF(), (new NiceMock<TestAutofillPopupController>(
show_pwd_migration_warning_callback_.Get()); external_delegate_->GetWeakPtrForTest(), web_contents(),
gfx::RectF(), show_pwd_migration_warning_callback_.Get()))
->GetWeakPtr();
ManualFillingControllerImpl::CreateForWebContentsForTesting( ManualFillingControllerImpl::CreateForWebContentsForTesting(
web_contents(), mock_pwd_controller_.AsWeakPtr(), web_contents(), mock_pwd_controller_.AsWeakPtr(),
mock_address_controller_.AsWeakPtr(), mock_cc_controller_.AsWeakPtr(), mock_address_controller_.AsWeakPtr(), mock_cc_controller_.AsWeakPtr(),
std::make_unique<NiceMock<MockManualFillingView>>()); std::make_unique<NiceMock<MockManualFillingView>>());
#else #else
autofill_popup_controller_ = new NiceMock<TestAutofillPopupController>( autofill_popup_controller_ =
external_delegate_->GetWeakPtrForTest(), web_contents(), gfx::RectF(), (new NiceMock<TestAutofillPopupController>(
base::DoNothing()); external_delegate_->GetWeakPtrForTest(), web_contents(),
gfx::RectF(), base::DoNothing()))
->GetWeakPtr();
#endif #endif
autofill_popup_controller_->SetViewForTesting( autofill_popup_controller_->SetViewForTesting(
autofill_popup_view()->GetWeakPtr()); autofill_popup_view()->GetWeakPtr());
@ -242,7 +245,7 @@ class AutofillPopupControllerUnitTest : public ChromeRenderViewHostTestHarness {
// This will make sure the controller and the view (if any) are both // This will make sure the controller and the view (if any) are both
// cleaned up. // cleaned up.
if (autofill_popup_controller_) { if (autofill_popup_controller_) {
autofill_popup_controller_->DoHide(); popup_controller().DoHide();
} }
external_delegate_.reset(); external_delegate_.reset();
@ -272,7 +275,8 @@ class AutofillPopupControllerUnitTest : public ChromeRenderViewHostTestHarness {
} }
TestAutofillPopupController& popup_controller() { TestAutofillPopupController& popup_controller() {
return *autofill_popup_controller_; return static_cast<TestAutofillPopupController&>(
*autofill_popup_controller_);
} }
NiceMock<MockAutofillExternalDelegate>* delegate() { NiceMock<MockAutofillExternalDelegate>* delegate() {
@ -332,8 +336,7 @@ class AutofillPopupControllerUnitTest : public ChromeRenderViewHostTestHarness {
password_manager::metrics_util::PasswordMigrationWarningTriggers)>> password_manager::metrics_util::PasswordMigrationWarningTriggers)>>
show_pwd_migration_warning_callback_; show_pwd_migration_warning_callback_;
#endif #endif
raw_ptr<NiceMock<TestAutofillPopupController>, AcrossTasksDanglingUntriaged> base::WeakPtr<AutofillPopupControllerImpl> autofill_popup_controller_;
autofill_popup_controller_ = nullptr;
}; };
TEST_F(AutofillPopupControllerUnitTest, RemoveSuggestion) { TEST_F(AutofillPopupControllerUnitTest, RemoveSuggestion) {