1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/autofill/address_bubbles_controller.h"
#include "base/test/mock_callback.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/hats/hats_service_factory.h"
#include "chrome/browser/ui/hats/mock_hats_service.h"
#include "chrome/browser/ui/hats/survey_config.h"
#include "chrome/browser/ui/views/side_panel/side_panel_coordinator.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/autofill/core/browser/foundations/autofill_client.h"
#include "components/autofill/core/browser/test_utils/autofill_test_utils.h"
#include "components/autofill/core/common/autofill_features.h"
#include "content/public/test/browser_test.h"
namespace autofill {
using ::testing::_;
using ::testing::Property;
using profile_ref = base::optional_ref<const AutofillProfile>;
class AddressBubblesControllerBrowserTest : public InProcessBrowserTest {
public:
AddressBubblesControllerBrowserTest() {
scoped_features_.InitAndEnableFeature(
autofill::features::kAutofillAddressUserDeclinedSaveSurvey);
}
AddressBubblesControllerBrowserTest(
const AddressBubblesControllerBrowserTest&) = delete;
AddressBubblesControllerBrowserTest& operator=(
const AddressBubblesControllerBrowserTest&) = delete;
~AddressBubblesControllerBrowserTest() override = default;
// InProcessBrowserTest:
void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
side_panel_coordinator()->SetNoDelaysForTesting(true);
side_panel_coordinator()->DisableAnimationsForTesting();
}
protected:
base::test::ScopedFeatureList scoped_features_;
raw_ptr<content::WebContents> tab_web_contents() const {
return browser()->tab_strip_model()->GetActiveWebContents();
}
AddressBubblesController* tab_controller() {
return AddressBubblesController::FromWebContents(tab_web_contents());
}
SidePanelCoordinator* side_panel_coordinator() {
return browser()->GetFeatures().side_panel_coordinator();
}
};
IN_PROC_BROWSER_TEST_F(AddressBubblesControllerBrowserTest,
DialogAcceptedInvokesCallback) {
AutofillProfile profile = test::GetFullProfile();
base::MockCallback<AutofillClient::AddressProfileSavePromptCallback> callback;
AddressBubblesController::SetUpAndShowSaveOrUpdateAddressBubble(
tab_web_contents(), profile, /*original_profile=*/nullptr,
/*is_migration_to_account=*/{}, /*user_has_any_profile_saved=*/{},
callback.Get());
EXPECT_CALL(callback,
Run(AutofillClient::AddressPromptUserDecision::kAccepted,
Property(&profile_ref::has_value, false)));
tab_controller()->OnUserDecision(
AutofillClient::AddressPromptUserDecision::kAccepted, std::nullopt);
}
// This is testing that the callback is invoked when the dialog is triggered in
// the side panel. It covers the regression found in crbug.com/401068467.
IN_PROC_BROWSER_TEST_F(AddressBubblesControllerBrowserTest,
DialogAcceptedInvokesCallbackForSidePanel) {
content::WebContents* side_panel_web_contents =
side_panel_coordinator()->GetWebContentsForTest(
SidePanelEntry::Id::kReadingList);
side_panel_coordinator()->Show(SidePanelEntry::Id::kReadingList);
AutofillProfile profile = test::GetFullProfile();
base::MockCallback<AutofillClient::AddressProfileSavePromptCallback> callback;
AddressBubblesController::SetUpAndShowSaveOrUpdateAddressBubble(
side_panel_web_contents, profile, /*original_profile=*/nullptr,
/*is_migration_to_account=*/{}, /*user_has_any_profile_saved=*/{},
callback.Get());
EXPECT_CALL(callback,
Run(AutofillClient::AddressPromptUserDecision::kAccepted,
Property(&profile_ref::has_value, false)));
AddressBubblesController::FromWebContents(side_panel_web_contents)
->OnUserDecision(AutofillClient::AddressPromptUserDecision::kAccepted,
std::nullopt);
}
IN_PROC_BROWSER_TEST_F(AddressBubblesControllerBrowserTest,
DialogCancelledInvokesCallback) {
AutofillProfile profile = test::GetFullProfile();
base::MockCallback<AutofillClient::AddressProfileSavePromptCallback> callback;
AddressBubblesController::SetUpAndShowSaveOrUpdateAddressBubble(
tab_web_contents(), profile, /*original_profile=*/nullptr,
/*is_migration_to_account=*/{}, /*user_has_any_profile_saved=*/{},
callback.Get());
EXPECT_CALL(callback,
Run(AutofillClient::AddressPromptUserDecision::kDeclined,
Property(&profile_ref::has_value, false)));
tab_controller()->OnUserDecision(
AutofillClient::AddressPromptUserDecision::kDeclined, std::nullopt);
}
#if !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(AddressBubblesControllerBrowserTest,
DeclinedSaveTriggersSurvey) {
MockHatsService* mock_hats_service = static_cast<MockHatsService*>(
HatsServiceFactory::GetInstance()->SetTestingFactoryAndUse(
browser()->profile(), base::BindRepeating(&BuildMockHatsService)));
auto empty_profile = AutofillProfile(AddressCountryCode("US"));
base::MockCallback<AutofillClient::AddressProfileSavePromptCallback> callback;
AddressBubblesController::SetUpAndShowSaveOrUpdateAddressBubble(
tab_web_contents(), empty_profile, /*original_profile=*/nullptr,
/*is_migration_to_account=*/false, /*user_has_any_profile_saved=*/false,
callback.Get());
EXPECT_CALL(
*mock_hats_service,
LaunchDelayedSurveyForWebContents(
kHatsSurveyTriggerAutofillAddressUserDeclinedSave,
_, _, _, _, _, _, _, _, _));
EXPECT_CALL(
callback,
Run(AutofillClient::AddressPromptUserDecision::kDeclined, _));
tab_controller()->OnUserDecision(
AutofillClient::AddressPromptUserDecision::kDeclined, std::nullopt);
}
IN_PROC_BROWSER_TEST_F(AddressBubblesControllerBrowserTest,
DeclinedSaveWithProfileDoesNotTriggerSurvey) {
MockHatsService* mock_hats_service = static_cast<MockHatsService*>(
HatsServiceFactory::GetInstance()->SetTestingFactoryAndUse(
browser()->profile(), base::BindRepeating(&BuildMockHatsService)));
base::MockCallback<AutofillClient::AddressProfileSavePromptCallback> callback;
AddressBubblesController::SetUpAndShowSaveOrUpdateAddressBubble(
tab_web_contents(), test::GetFullProfile(), /*original_profile=*/nullptr,
/*is_migration_to_account=*/false, /*user_has_any_profile_saved=*/true,
callback.Get());
EXPECT_CALL(
*mock_hats_service,
LaunchDelayedSurveyForWebContents(
kHatsSurveyTriggerAutofillAddressUserDeclinedSave,
_, _, _, _, _, _, _, _, _)).Times(0);
EXPECT_CALL(callback,
Run(AutofillClient::AddressPromptUserDecision::kDeclined, _));
tab_controller()->OnUserDecision(
AutofillClient::AddressPromptUserDecision::kDeclined, std::nullopt);
}
IN_PROC_BROWSER_TEST_F(AddressBubblesControllerBrowserTest,
AcceptedSaveDoesNotTriggerSurvey) {
MockHatsService* mock_hats_service = static_cast<MockHatsService*>(
HatsServiceFactory::GetInstance()->SetTestingFactoryAndUse(
browser()->profile(), base::BindRepeating(&BuildMockHatsService)));
auto empty_profile = AutofillProfile(AddressCountryCode("US"));
base::MockCallback<AutofillClient::AddressProfileSavePromptCallback> callback;
AddressBubblesController::SetUpAndShowSaveOrUpdateAddressBubble(
tab_web_contents(), empty_profile, /*original_profile=*/nullptr,
/*is_migration_to_account=*/false, /*user_has_any_profile_saved=*/false,
callback.Get());
EXPECT_CALL(
*mock_hats_service,
LaunchDelayedSurveyForWebContents(
kHatsSurveyTriggerAutofillAddressUserDeclinedSave,
_, _, _, _, _, _, _, _, _)).Times(0);
EXPECT_CALL(
callback,
Run(AutofillClient::AddressPromptUserDecision::kAccepted, _));
tab_controller()->OnUserDecision(
AutofillClient::AddressPromptUserDecision::kAccepted, std::nullopt);
}
#endif
// This is testing that closing all tabs (which effectively destroys the web
// contents) will trigger the save callback with kIgnored decions if the users
// hasn't interacted with the prompt already.
IN_PROC_BROWSER_TEST_F(AddressBubblesControllerBrowserTest,
WebContentsDestroyedInvokesCallback) {
AutofillProfile profile = test::GetFullProfile();
base::MockCallback<AutofillClient::AddressProfileSavePromptCallback> callback;
AddressBubblesController::SetUpAndShowSaveOrUpdateAddressBubble(
tab_web_contents(), profile, /*original_profile=*/nullptr,
/*is_migration_to_account=*/{}, /*user_has_any_profile_saved=*/{},
callback.Get());
TabStripModel* tab_strip_model = browser()->tab_strip_model();
CHECK_EQ(1, tab_strip_model->count());
// There is only now tab open, so the active web contents, are the
// controller's web contents.
content::WebContents* controller_web_contents =
tab_strip_model->GetActiveWebContents();
// Now add another tab, and close the controller tab to make sure the window
// remains open. This should destroy the web contents of the controller and
// invoke the callback with a decision kIgnored.
GURL url(url::kAboutBlankURL);
ASSERT_TRUE(AddTabAtIndex(0, url, ui::PAGE_TRANSITION_TYPED));
EXPECT_EQ(2, tab_strip_model->count());
EXPECT_CALL(callback, Run(AutofillClient::AddressPromptUserDecision::kIgnored,
Property(&profile_ref::has_value, false)));
// Close controller tab.
int previous_tab_count = browser()->tab_strip_model()->count();
browser()->tab_strip_model()->CloseWebContentsAt(
tab_strip_model->GetIndexOfWebContents(controller_web_contents),
TabCloseTypes::CLOSE_USER_GESTURE);
EXPECT_EQ(previous_tab_count - 1, browser()->tab_strip_model()->count());
}
// This is testing that the bubble is visible and active when shown.
IN_PROC_BROWSER_TEST_F(AddressBubblesControllerBrowserTest,
BubbleShouldBeVisibleByDefault) {
AutofillProfile profile = test::GetFullProfile();
AddressBubblesController::SetUpAndShowSaveOrUpdateAddressBubble(
tab_web_contents(), profile, /*original_profile=*/nullptr,
/*is_migration_to_account=*/{}, /*user_has_any_profile_saved=*/{},
/*callback=*/base::DoNothing());
// Bubble is visible and active
EXPECT_TRUE(tab_controller()->GetBubbleView());
EXPECT_TRUE(tab_controller()->IsBubbleActive());
}
// This is testing that when a second prompt comes while another prompt is
// shown, the controller will ignore it, and inform the backend that the second
// prompt has been auto declined.
IN_PROC_BROWSER_TEST_F(AddressBubblesControllerBrowserTest,
SecondPromptWillBeAutoDeclinedWhileFirstIsVisible) {
AutofillProfile profile = test::GetFullProfile();
AddressBubblesController::SetUpAndShowSaveOrUpdateAddressBubble(
tab_web_contents(), profile, /*original_profile=*/nullptr,
/*is_migration_to_account=*/{}, /*user_has_any_profile_saved=*/{},
/*callback=*/base::DoNothing());
// Second prompt should be auto declined.
base::MockCallback<AutofillClient::AddressProfileSavePromptCallback> callback;
EXPECT_CALL(callback,
Run(AutofillClient::AddressPromptUserDecision::kAutoDeclined,
Property(&profile_ref::has_value, false)));
AddressBubblesController::SetUpAndShowSaveOrUpdateAddressBubble(
tab_web_contents(), profile, /*original_profile=*/nullptr,
/*is_migration_to_account=*/{}, /*user_has_any_profile_saved=*/{},
callback.Get());
}
// This is testing that when a second prompt comes while another prompt is in
// progress but not shown, the controller will inform the backend that the first
// process is ignored.
IN_PROC_BROWSER_TEST_F(AddressBubblesControllerBrowserTest,
FirstHiddenPromptWillBeIgnoredWhenSecondPromptArrives) {
AutofillProfile profile = test::GetFullProfile();
base::MockCallback<AutofillClient::AddressProfileSavePromptCallback> callback;
AddressBubblesController::SetUpAndShowSaveOrUpdateAddressBubble(
tab_web_contents(), profile, /*original_profile=*/nullptr,
/*is_migration_to_account=*/{}, /*user_has_any_profile_saved=*/{},
callback.Get());
tab_controller()->OnBubbleClosed();
// When second prompt comes, the first one will be ignored.
EXPECT_CALL(callback, Run(AutofillClient::AddressPromptUserDecision::kIgnored,
Property(&profile_ref::has_value, false)));
AddressBubblesController::SetUpAndShowSaveOrUpdateAddressBubble(
tab_web_contents(), profile, /*original_profile=*/nullptr,
/*is_migration_to_account=*/{}, /*user_has_any_profile_saved=*/{},
/*callback=*/base::DoNothing());
}
} // namespace autofill
|