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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
// 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.
#include "components/plus_addresses/plus_address_suggestion_generator.h"
#include <string>
#include <utility>
#include "base/containers/flat_map.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "components/autofill/core/browser/integrators/password_form_classification.h"
#include "components/autofill/core/browser/suggestions/suggestion.h"
#include "components/autofill/core/browser/suggestions/suggestion_test_helpers.h"
#include "components/autofill/core/browser/suggestions/suggestion_type.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/autofill_test_utils.h"
#include "components/autofill/core/common/form_field_data.h"
#include "components/autofill/core/common/unique_ids.h"
#include "components/plus_addresses/fake_plus_address_allocator.h"
#include "components/plus_addresses/features.h"
#include "components/plus_addresses/grit/plus_addresses_strings.h"
#include "components/plus_addresses/plus_address_allocator.h"
#include "components/plus_addresses/plus_address_test_utils.h"
#include "components/plus_addresses/plus_address_types.h"
#include "components/plus_addresses/settings/fake_plus_address_setting_service.h"
#include "net/http/http_status_code.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/origin.h"
namespace plus_addresses {
namespace {
using autofill::AutofillSuggestionTriggerSource;
using autofill::EqualsSuggestion;
using autofill::FieldGlobalId;
using autofill::FormData;
using autofill::PasswordFormClassification;
using autofill::Suggestion;
using autofill::SuggestionType;
using autofill::test::CreateTestSignupFormData;
using ::testing::AllOf;
using ::testing::ElementsAre;
using ::testing::Field;
using ::testing::IsEmpty;
using ::testing::Property;
using ::testing::SizeIs;
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
auto IsCreateInlineSuggestion(
std::optional<std::u16string> suggested_plus_address) {
const bool is_loading = !suggested_plus_address.has_value();
const std::optional<std::u16string> voice_over =
suggested_plus_address
? l10n_util::GetStringFUTF16(
IDS_PLUS_ADDRESS_CREATE_INLINE_SUGGESTION_A11Y_VOICE_OVER,
*suggested_plus_address)
: std::optional<std::u16string>();
Suggestion::PlusAddressPayload payload(std::move(suggested_plus_address));
payload.offer_refresh = !is_loading;
return AllOf(
EqualsSuggestion(SuggestionType::kCreateNewPlusAddressInline),
Property(&Suggestion::GetPayload<Suggestion::PlusAddressPayload>,
std::move(payload)),
Field(&Suggestion::is_loading, Suggestion::IsLoading(is_loading)),
Field(&Suggestion::voice_over, voice_over));
}
#endif // !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
// Returns `form` with a non-null host form id and frame token.
FormData SetGeneratedFrameTokenAndHostFormId(FormData form) {
// Ensure that the form is not unowned.
form.set_renderer_id(autofill::test::MakeFormRendererId());
std::vector<autofill::FormFieldData> fields = form.ExtractFields();
for (autofill::FormFieldData& field : fields) {
field.set_host_form_id(form.renderer_id());
}
form.set_fields(std::move(fields));
// Set the same non-zero host frame for all fields.
return autofill::test::CreateFormDataForFrame(
std::move(form), autofill::test::MakeLocalFrameToken());
}
class PlusAddressSuggestionGeneratorTest : public ::testing::Test {
public:
PlusAddressSuggestionGeneratorTest() = default;
protected:
FakePlusAddressAllocator& allocator() { return allocator_; }
FakePlusAddressSettingService& setting_service() { return setting_service_; }
private:
autofill::test::AutofillUnitTestEnvironment autofill_env_;
FakePlusAddressAllocator allocator_;
FakePlusAddressSettingService setting_service_;
};
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
// Tests that an empty PlusAddressPayload is set if there are no cached plus
// addresses.
TEST_F(PlusAddressSuggestionGeneratorTest,
InlineGenerationWithoutPreallocatedAddresses) {
allocator().set_is_next_allocation_synchronous(false);
PlusAddressSuggestionGenerator generator(
&setting_service(), &allocator(),
url::Origin::Create(GURL("https://foo.bar")));
FormData form = CreateTestSignupFormData();
EXPECT_THAT(generator.GetSuggestions(
/*affiliated_plus_addresses=*/{},
/*is_creation_enabled=*/true, form, form.fields()[0],
/*form_field_type_groups=*/{}, PasswordFormClassification(),
AutofillSuggestionTriggerSource::kFormControlElementClicked),
ElementsAre(IsCreateInlineSuggestion(
/*suggested_plus_address=*/std::nullopt)));
}
// Tests that if there are cached plus addresses available, then one is set is
// the PlusAddressPayload.
TEST_F(PlusAddressSuggestionGeneratorTest,
InlineGenerationWithPreallocatedAddresses) {
allocator().set_is_next_allocation_synchronous(true);
PlusAddressSuggestionGenerator generator(
&setting_service(), &allocator(),
url::Origin::Create(GURL("https://foo.bar")));
FormData form = CreateTestSignupFormData();
EXPECT_THAT(generator.GetSuggestions(
/*affiliated_plus_addresses=*/{},
/*is_creation_enabled=*/true, form, form.fields()[0],
/*form_field_type_groups=*/{}, PasswordFormClassification(),
AutofillSuggestionTriggerSource::kFormControlElementClicked),
ElementsAre(IsCreateInlineSuggestion(
/*suggested_plus_address=*/base::UTF8ToUTF16(
*test::CreatePlusProfile().plus_address))));
}
TEST_F(PlusAddressSuggestionGeneratorTest,
SetSuggestedPlusAddressForSuggestion) {
const PlusAddress plus_address("plus@foo.com");
Suggestion suggestion(SuggestionType::kCreateNewPlusAddressInline);
suggestion.payload = Suggestion::PlusAddressPayload();
suggestion.is_loading = Suggestion::IsLoading(true);
PlusAddressSuggestionGenerator::SetSuggestedPlusAddressForSuggestion(
plus_address, suggestion);
EXPECT_FALSE(suggestion.is_loading);
EXPECT_EQ(suggestion.GetPayload<Suggestion::PlusAddressPayload>().address,
base::UTF8ToUTF16(*plus_address));
}
TEST_F(PlusAddressSuggestionGeneratorTest, GetPlusAddressErrorSuggestion) {
const Suggestion suggestion(
PlusAddressSuggestionGenerator::GetPlusAddressErrorSuggestion(
PlusAddressRequestError::AsNetworkError(net::HTTP_BAD_REQUEST)));
EXPECT_EQ(suggestion.type, SuggestionType::kPlusAddressError);
EXPECT_EQ(
suggestion.main_text.value,
l10n_util::GetStringUTF16(IDS_PLUS_ADDRESS_CREATE_SUGGESTION_MAIN_TEXT));
EXPECT_EQ(suggestion.icon, Suggestion::Icon::kError);
EXPECT_TRUE(
suggestion.GetPayload<Suggestion::PlusAddressPayload>().offer_refresh);
EXPECT_THAT(
suggestion.labels,
ElementsAre(ElementsAre(Suggestion::Text(l10n_util::GetStringUTF16(
IDS_PLUS_ADDRESS_RESERVE_GENERIC_ERROR_TEXT)))));
}
TEST_F(PlusAddressSuggestionGeneratorTest,
GetPlusAddressErrorSuggestionForQuotaError) {
const auto error =
PlusAddressRequestError::AsNetworkError(net::HTTP_TOO_MANY_REQUESTS);
ASSERT_TRUE(error.IsQuotaError());
const Suggestion suggestion(
PlusAddressSuggestionGenerator::GetPlusAddressErrorSuggestion(error));
EXPECT_EQ(suggestion.type, SuggestionType::kPlusAddressError);
EXPECT_EQ(
suggestion.main_text.value,
l10n_util::GetStringUTF16(IDS_PLUS_ADDRESS_CREATE_SUGGESTION_MAIN_TEXT));
EXPECT_EQ(suggestion.icon, Suggestion::Icon::kError);
EXPECT_FALSE(
suggestion.GetPayload<Suggestion::PlusAddressPayload>().offer_refresh);
EXPECT_THAT(
suggestion.labels,
ElementsAre(ElementsAre(Suggestion::Text(l10n_util::GetStringUTF16(
IDS_PLUS_ADDRESS_RESERVE_QUOTA_ERROR_TEXT)))));
}
// Tests that suggestions in the `is_loading` state do not have a refresh
// button and is not acceptable.
TEST_F(PlusAddressSuggestionGeneratorTest, LoadingStateProperties) {
Suggestion inline_suggestion(SuggestionType::kCreateNewPlusAddressInline);
inline_suggestion.payload = Suggestion::PlusAddressPayload();
PlusAddressSuggestionGenerator::SetLoadingStateForSuggestion(
/*is_loading=*/true, inline_suggestion);
EXPECT_TRUE(inline_suggestion.is_loading);
EXPECT_FALSE(inline_suggestion.IsAcceptable());
EXPECT_FALSE(inline_suggestion.GetPayload<Suggestion::PlusAddressPayload>()
.offer_refresh);
PlusAddressSuggestionGenerator::SetSuggestedPlusAddressForSuggestion(
PlusAddress("foo@moo.com"), inline_suggestion);
EXPECT_FALSE(inline_suggestion.is_loading);
EXPECT_TRUE(inline_suggestion.GetPayload<Suggestion::PlusAddressPayload>()
.offer_refresh);
EXPECT_TRUE(inline_suggestion.IsAcceptable());
}
// Tests that creation is offered on fields where autofill was previously
// triggered.
TEST_F(PlusAddressSuggestionGeneratorTest,
CreationSuggestionOnPreviouslyAutofilledFields) {
PlusAddressSuggestionGenerator generator(
&setting_service(), &allocator(),
url::Origin::Create(GURL("https://foo.bar")));
autofill::FormData form;
autofill::FormFieldData focused_field;
form.set_fields({focused_field});
EXPECT_THAT(generator.GetSuggestions(
/*affiliated_plus_addresses=*/{},
/*is_creation_enabled=*/true, form, focused_field,
/*form_field_type_groups=*/{}, PasswordFormClassification(),
AutofillSuggestionTriggerSource::kFormControlElementClicked),
ElementsAre(IsCreateInlineSuggestion(
/*suggested_plus_address=*/std::nullopt)));
// Field got autofilled. The values does not prefix-match any plus address.
focused_field.set_is_autofilled(true);
focused_field.set_value(u"pp");
form.set_fields({focused_field});
EXPECT_THAT(generator.GetSuggestions(
/*affiliated_plus_addresses=*/{},
/*is_creation_enabled=*/true, form, focused_field,
/*form_field_type_groups=*/{}, PasswordFormClassification(),
AutofillSuggestionTriggerSource::kFormControlElementClicked),
ElementsAre(IsCreateInlineSuggestion(
/*suggested_plus_address=*/std::nullopt)));
}
#endif // !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
// Tests that the creation suggestion contains no labels if the notice has not
// been accepted.
TEST_F(PlusAddressSuggestionGeneratorTest, FirstTimeCreateSuggestion) {
setting_service().set_has_accepted_notice(false);
PlusAddressSuggestionGenerator generator(
&setting_service(), &allocator(),
url::Origin::Create(GURL("https://foo.bar")));
FormData form = CreateTestSignupFormData();
EXPECT_THAT(
generator.GetSuggestions(
/*affiliated_plus_addresses=*/{},
/*is_creation_enabled=*/true, form, form.fields()[0],
/*form_field_type_groups=*/{}, PasswordFormClassification(),
AutofillSuggestionTriggerSource::kFormControlElementClicked),
ElementsAre(AllOf(EqualsSuggestion(SuggestionType::kCreateNewPlusAddress),
Field(&Suggestion::labels, IsEmpty()))));
}
// Tests that no creation suggestion is shown on a login form.
TEST_F(PlusAddressSuggestionGeneratorTest, NoSuggestionsOnLoginForm) {
PlusAddressSuggestionGenerator generator(
&setting_service(), &allocator(),
url::Origin::Create(GURL("https://foo.bar")));
const FormData login_form = SetGeneratedFrameTokenAndHostFormId(
autofill::test::CreateTestPasswordFormData());
ASSERT_THAT(login_form.fields(), SizeIs(2));
const autofill::FormFieldData focused_field = login_form.fields()[0];
const base::flat_map<FieldGlobalId, autofill::FieldTypeGroup>
form_field_type_groups = {
{focused_field.global_id(), autofill::FieldTypeGroup::kUsernameField},
{login_form.fields()[1].global_id(),
autofill::FieldTypeGroup::kPasswordField}};
PasswordFormClassification classification;
classification.type = PasswordFormClassification::Type::kLoginForm;
classification.username_field = focused_field.global_id();
classification.password_field = login_form.fields()[1].global_id();
EXPECT_THAT(generator.GetSuggestions(
/*affiliated_plus_addresses=*/{},
/*is_creation_enabled=*/true, login_form, focused_field,
form_field_type_groups, classification,
AutofillSuggestionTriggerSource::kFormControlElementClicked),
IsEmpty());
}
// Tests that creation is offered on forms classified by PWM as login forms if
// they have name or address fields included.
TEST_F(PlusAddressSuggestionGeneratorTest,
SuggestionsOnLoginFormWithNameFields) {
PlusAddressSuggestionGenerator generator(
&setting_service(), &allocator(),
url::Origin::Create(GURL("https://foo.bar")));
FormData form = autofill::test::CreateTestPasswordFormData();
{
std::vector<autofill::FormFieldData> fields = form.ExtractFields();
fields.push_back(autofill::test::CreateTestFormField(
/*label=*/"First name", /*name=*/"first_name",
/*value=*/"", autofill::FormControlType::kInputText));
form.set_fields(std::move(fields));
}
form = SetGeneratedFrameTokenAndHostFormId(std::move(form));
ASSERT_THAT(form.fields(), SizeIs(3));
const autofill::FormFieldData focused_field = form.fields()[0];
const base::flat_map<FieldGlobalId, autofill::FieldTypeGroup>
form_field_type_groups = {
{focused_field.global_id(), autofill::FieldTypeGroup::kUsernameField},
{form.fields()[1].global_id(),
autofill::FieldTypeGroup::kPasswordField},
{form.fields()[2].global_id(), autofill::FieldTypeGroup::kName}};
PasswordFormClassification classification;
classification.type = PasswordFormClassification::Type::kLoginForm;
classification.username_field = focused_field.global_id();
EXPECT_THAT(generator.GetSuggestions(
/*affiliated_plus_addresses=*/{},
/*is_creation_enabled=*/true, form, focused_field,
form_field_type_groups, classification,
AutofillSuggestionTriggerSource::kFormControlElementClicked),
test::IsSingleCreatePlusAddressSuggestion());
}
// Tests that creation is offered on forms classified by PWM as login forms if
// the password field is hidden.
TEST_F(PlusAddressSuggestionGeneratorTest,
SuggestionsOnLoginFormWithHiddenPasswordField) {
PlusAddressSuggestionGenerator generator(
&setting_service(), &allocator(),
url::Origin::Create(GURL("https://foo.bar")));
FormData form = autofill::test::CreateTestPasswordFormData();
{
std::vector<autofill::FormFieldData> fields = form.ExtractFields();
fields[1].set_is_visible(false);
form.set_fields(std::move(fields));
}
form = SetGeneratedFrameTokenAndHostFormId(std::move(form));
const autofill::FormFieldData focused_field = form.fields()[0];
PasswordFormClassification classification;
classification.type = PasswordFormClassification::Type::kLoginForm;
classification.username_field = focused_field.global_id();
classification.password_field = form.fields()[1].global_id();
EXPECT_THAT(generator.GetSuggestions(
/*affiliated_plus_addresses=*/{},
/*is_creation_enabled=*/true, form, focused_field,
/*form_field_type_groups=*/{}, classification,
AutofillSuggestionTriggerSource::kFormControlElementClicked),
test::IsSingleCreatePlusAddressSuggestion());
}
// Tests that filling is offered on fields where autofill was previously
// triggered and prefix-matching is not applied.
TEST_F(PlusAddressSuggestionGeneratorTest,
FillingSuggestionOnPreviouslyAutofilledFields) {
PlusAddressSuggestionGenerator generator(
&setting_service(), &allocator(),
url::Origin::Create(GURL("https://foo.bar")));
const std::string plus_address = "test+plus@test.com";
autofill::FormData form;
autofill::FormFieldData focused_field;
form.set_fields({focused_field});
EXPECT_THAT(generator.GetSuggestions(
/*affiliated_plus_addresses=*/{plus_address},
/*is_creation_enabled=*/true, form, focused_field,
/*form_field_type_groups=*/{}, PasswordFormClassification(),
AutofillSuggestionTriggerSource::kFormControlElementClicked),
test::IsSingleFillPlusAddressSuggestion(plus_address));
// Field got autofilled. The values does not prefix-match any plus address.
focused_field.set_is_autofilled(true);
focused_field.set_value(u"pp");
form.set_fields({focused_field});
EXPECT_THAT(generator.GetSuggestions(
/*affiliated_plus_addresses=*/{plus_address},
/*is_creation_enabled=*/true, form, focused_field,
/*form_field_type_groups=*/{}, PasswordFormClassification(),
AutofillSuggestionTriggerSource::kFormControlElementClicked),
test::IsSingleFillPlusAddressSuggestion(plus_address));
}
} // namespace
} // namespace plus_addresses
|