Commit e94b7cc9 authored by Caitlin Fischer's avatar Caitlin Fischer Committed by Commit Bot

[Autofill] Adds profiles as a private data member in LabelFormatter.

Currently, profiles are passed once to Create() and once to
GetLabels(). This is problematic because certain formatters, e.g.
AddressContactFormLabelFormatters, are created specifically for the
profiles passed to Create(), and the profiles could potentially change
between the calls to Create() and GetLabels().

Now, LabelFormatters store a constant reference to a collection of
profiles to ensure that the LabelFormatter and the labels it creates are
guaranteed to be made for the same profiles.

Bug: 958333
Change-Id: I242e3a24483afa614ea8256182d57b376abcdec9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1598492
Commit-Queue: Caitlin Fischer <caitlinfischer@google.com>
Reviewed-by: default avatarTommy Martino <tmartino@chromium.org>
Reviewed-by: default avatarFabio Tirelo <ftirelo@chromium.org>
Cr-Commit-Position: refs/heads/master@{#659226}
parent 455989b0
......@@ -9,16 +9,19 @@
namespace autofill {
AddressContactFormLabelFormatter::AddressContactFormLabelFormatter(
const std::vector<AutofillProfile*>& profiles,
const std::string& app_locale,
ServerFieldType focused_field_type,
uint32_t groups,
const std::vector<ServerFieldType>& field_types,
bool show_phone,
bool show_email)
: LabelFormatter(app_locale, focused_field_type, groups, field_types),
const std::vector<ServerFieldType>& field_types)
: LabelFormatter(profiles,
app_locale,
focused_field_type,
groups,
field_types),
form_has_street_address_(HasStreetAddress(field_types_for_labels())),
show_phone_(show_phone),
show_email_(show_email) {}
email_disambiguates_(!HaveSameEmailAddresses(profiles, app_locale)),
phone_disambiguates_(!HaveSamePhoneNumbers(profiles, app_locale)) {}
AddressContactFormLabelFormatter::~AddressContactFormLabelFormatter() {}
......@@ -48,11 +51,11 @@ base::string16 AddressContactFormLabelFormatter::GetLabelForProfile(
&label_parts);
}
if (focused_group != PHONE_HOME && show_phone_) {
if (focused_group != PHONE_HOME && phone_disambiguates_) {
AddLabelPartIfNotEmpty(GetLabelPhone(profile, app_locale()), &label_parts);
}
if (focused_group != EMAIL && show_email_) {
if (focused_group != EMAIL && email_disambiguates_) {
AddLabelPartIfNotEmpty(GetLabelEmail(profile, app_locale()), &label_parts);
}
......
......@@ -20,12 +20,11 @@ namespace autofill {
class AddressContactFormLabelFormatter : public LabelFormatter {
public:
AddressContactFormLabelFormatter(
const std::vector<AutofillProfile*>& profiles,
const std::string& app_locale,
ServerFieldType focused_field_type,
uint32_t groups,
const std::vector<ServerFieldType>& field_types,
bool show_phone,
bool show_email);
const std::vector<ServerFieldType>& field_types);
~AddressContactFormLabelFormatter() override;
......@@ -40,11 +39,9 @@ class AddressContactFormLabelFormatter : public LabelFormatter {
// street addresses should not appear in labels.
bool form_has_street_address_;
// True if phone numbers should be included in labels.
bool show_phone_;
// True if email addresses should be included in labels.
bool show_email_;
// True if the field disambiguates |profiles_|.
bool email_disambiguates_;
bool phone_disambiguates_;
};
} // namespace autofill
......
......@@ -40,8 +40,8 @@ std::vector<ServerFieldType> GetFieldTypes() {
TEST(AddressContactFormLabelFormatterTest, GetLabelsWithMissingProfiles) {
const std::vector<AutofillProfile*> profiles{};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"en-US", NAME_BILLING_FULL, GetFieldTypes(), profiles);
EXPECT_TRUE(formatter->GetLabels(profiles).empty());
profiles, "en-US", NAME_BILLING_FULL, GetFieldTypes());
EXPECT_TRUE(formatter->GetLabels().empty());
}
TEST(AddressContactFormLabelFormatterTest,
......@@ -82,10 +82,10 @@ TEST(AddressContactFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile1, &profile2, &profile3,
&profile4, &profile5, &profile6};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"en-US", NAME_BILLING_FULL, GetFieldTypes(), profiles);
profiles, "en-US", NAME_BILLING_FULL, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(
ConstructLabelLine({base::ASCIIToUTF16("19 North Sq"),
base::ASCIIToUTF16("(617) 523-2338"),
......@@ -138,10 +138,10 @@ TEST(AddressContactFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile1, &profile2, &profile3,
&profile4, &profile5, &profile6};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"en-US", ADDRESS_BILLING_LINE1, GetFieldTypes(), profiles);
profiles, "en-US", ADDRESS_BILLING_LINE1, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(
ConstructLabelLine({base::ASCIIToUTF16("Sarah Revere"),
base::ASCIIToUTF16("(617) 523-2338"),
......@@ -194,10 +194,10 @@ TEST(AddressContactFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile1, &profile2, &profile3,
&profile4, &profile5, &profile6};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"en-US", ADDRESS_BILLING_CITY, GetFieldTypes(), profiles);
profiles, "en-US", ADDRESS_BILLING_CITY, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(
ConstructLabelLine({base::ASCIIToUTF16("19 North Sq"),
base::ASCIIToUTF16("(617) 523-2338"),
......@@ -250,10 +250,10 @@ TEST(AddressContactFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile1, &profile2, &profile3,
&profile4, &profile5, &profile6};
const std::unique_ptr<LabelFormatter> formatter =
LabelFormatter::Create("en-US", EMAIL_ADDRESS, GetFieldTypes(), profiles);
LabelFormatter::Create(profiles, "en-US", EMAIL_ADDRESS, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(ConstructLabelLine({base::ASCIIToUTF16("Sarah Revere"),
base::ASCIIToUTF16("19 North Sq"),
base::ASCIIToUTF16("(617) 523-2338")}),
......@@ -306,10 +306,10 @@ TEST(AddressContactFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile1, &profile2, &profile3,
&profile4, &profile5, &profile6};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"en-US", PHONE_BILLING_WHOLE_NUMBER, GetFieldTypes(), profiles);
profiles, "en-US", PHONE_BILLING_WHOLE_NUMBER, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(
ConstructLabelLine({base::ASCIIToUTF16("Sarah Revere"),
base::ASCIIToUTF16("19 North Sq"),
......@@ -343,10 +343,10 @@ TEST(AddressContactFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile1, &profile2};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"pt-BR", NAME_BILLING_FULL, GetFieldTypes(), profiles);
profiles, "pt-BR", NAME_BILLING_FULL, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(
ConstructLabelLine(
{base::UTF8ToUTF16("Av. Pedro Álvares Cabral, 1301"),
......@@ -375,10 +375,10 @@ TEST(AddressContactFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile1, &profile2};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"pt-BR", ADDRESS_BILLING_LINE1, GetFieldTypes(), profiles);
profiles, "pt-BR", ADDRESS_BILLING_LINE1, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(
ConstructLabelLine({base::ASCIIToUTF16("Tarsila do Amaral"),
base::ASCIIToUTF16("(11) 2648-0254"),
......@@ -406,10 +406,10 @@ TEST(AddressContactFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile1, &profile2};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"pt-BR", ADDRESS_BILLING_ZIP, GetFieldTypes(), profiles);
profiles, "pt-BR", ADDRESS_BILLING_ZIP, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(
ConstructLabelLine(
{base::UTF8ToUTF16("Av. Pedro Álvares Cabral, 1301"),
......@@ -438,10 +438,10 @@ TEST(AddressContactFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile1, &profile2};
const std::unique_ptr<LabelFormatter> formatter =
LabelFormatter::Create("pt-BR", EMAIL_ADDRESS, GetFieldTypes(), profiles);
LabelFormatter::Create(profiles, "pt-BR", EMAIL_ADDRESS, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(
ConstructLabelLine(
{base::ASCIIToUTF16("Tarsila do Amaral"),
......@@ -470,10 +470,10 @@ TEST(AddressContactFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile1, &profile2};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"pt-BR", PHONE_BILLING_WHOLE_NUMBER, GetFieldTypes(), profiles);
profiles, "pt-BR", PHONE_BILLING_WHOLE_NUMBER, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(
ConstructLabelLine(
{base::ASCIIToUTF16("Tarsila do Amaral"),
......@@ -494,14 +494,13 @@ TEST(AddressContactFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile};
const std::unique_ptr<LabelFormatter> formatter =
LabelFormatter::Create("en-US", EMAIL_ADDRESS,
LabelFormatter::Create(profiles, "en-US", EMAIL_ADDRESS,
{NAME_BILLING_FULL, EMAIL_ADDRESS,
ADDRESS_BILLING_ZIP, PHONE_BILLING_WHOLE_NUMBER},
profiles);
ADDRESS_BILLING_ZIP, PHONE_BILLING_WHOLE_NUMBER});
// Checks that only address fields in the form are shown in the label.
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(ConstructLabelLine(
{base::ASCIIToUTF16("Sarah Revere"), base::ASCIIToUTF16("02113")})));
}
......
......@@ -9,11 +9,16 @@
namespace autofill {
AddressEmailFormLabelFormatter::AddressEmailFormLabelFormatter(
const std::vector<AutofillProfile*>& profiles,
const std::string& app_locale,
ServerFieldType focused_field_type,
uint32_t groups,
const std::vector<ServerFieldType>& field_types)
: LabelFormatter(app_locale, focused_field_type, groups, field_types),
: LabelFormatter(profiles,
app_locale,
focused_field_type,
groups,
field_types),
form_has_street_address_(HasStreetAddress(field_types_for_labels())) {}
AddressEmailFormLabelFormatter::~AddressEmailFormLabelFormatter() {}
......
......@@ -20,6 +20,7 @@ namespace autofill {
class AddressEmailFormLabelFormatter : public LabelFormatter {
public:
AddressEmailFormLabelFormatter(
const std::vector<AutofillProfile*>& profiles,
const std::string& app_locale,
ServerFieldType focused_field_type,
uint32_t groups,
......
......@@ -38,8 +38,8 @@ std::vector<ServerFieldType> GetFieldTypes() {
TEST(AddressEmailFormLabelFormatterTest, GetLabelsWithMissingProfiles) {
const std::vector<AutofillProfile*> profiles{};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"en-US", NAME_BILLING_FULL, GetFieldTypes(), profiles);
EXPECT_TRUE(formatter->GetLabels(profiles).empty());
profiles, "en-US", NAME_BILLING_FULL, GetFieldTypes());
EXPECT_TRUE(formatter->GetLabels().empty());
}
TEST(AddressEmailFormLabelFormatterTest, GetLabelsForUSProfilesAndFocusedName) {
......@@ -68,10 +68,10 @@ TEST(AddressEmailFormLabelFormatterTest, GetLabelsForUSProfilesAndFocusedName) {
const std::vector<AutofillProfile*> profiles{&profile1, &profile2, &profile3,
&profile4};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"en-US", NAME_BILLING_FULL, GetFieldTypes(), profiles);
profiles, "en-US", NAME_BILLING_FULL, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(ConstructLabelLine({base::ASCIIToUTF16("333 Washington St"),
base::ASCIIToUTF16("jfk@gmail.com")}),
base::ASCIIToUTF16("151 Irving Ave"),
......@@ -105,10 +105,10 @@ TEST(AddressEmailFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile1, &profile2, &profile3,
&profile4};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"en-US", ADDRESS_BILLING_LINE1, GetFieldTypes(), profiles);
profiles, "en-US", ADDRESS_BILLING_LINE1, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(ConstructLabelLine({base::ASCIIToUTF16("John F Kennedy"),
base::ASCIIToUTF16("jfk@gmail.com")}),
base::ASCIIToUTF16("Jackie Kennedy"),
......@@ -142,10 +142,10 @@ TEST(AddressEmailFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile1, &profile2, &profile3,
&profile4};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"en-US", ADDRESS_BILLING_ZIP, GetFieldTypes(), profiles);
profiles, "en-US", ADDRESS_BILLING_ZIP, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(ConstructLabelLine({base::ASCIIToUTF16("333 Washington St"),
base::ASCIIToUTF16("jfk@gmail.com")}),
base::ASCIIToUTF16("151 Irving Ave"),
......@@ -178,10 +178,10 @@ TEST(AddressEmailFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile1, &profile2, &profile3,
&profile4};
const std::unique_ptr<LabelFormatter> formatter =
LabelFormatter::Create("en-US", EMAIL_ADDRESS, GetFieldTypes(), profiles);
LabelFormatter::Create(profiles, "en-US", EMAIL_ADDRESS, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(ConstructLabelLine({base::ASCIIToUTF16("John F Kennedy"),
base::ASCIIToUTF16("333 Washington St")}),
base::ASCIIToUTF16("Jackie Kennedy"), base::string16(),
......@@ -205,10 +205,10 @@ TEST(AddressEmailFormLabelFormatterTest, GetLabelsForBRProfilesAndFocusedName) {
const std::vector<AutofillProfile*> profiles{&profile1, &profile2};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"pt-BR", NAME_BILLING_FULL, GetFieldTypes(), profiles);
profiles, "pt-BR", NAME_BILLING_FULL, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(
ConstructLabelLine(
{base::UTF8ToUTF16("Av. Pedro Álvares Cabral, 1301"),
......@@ -235,10 +235,10 @@ TEST(AddressEmailFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile1, &profile2};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"pt-BR", ADDRESS_BILLING_LINE1, GetFieldTypes(), profiles);
profiles, "pt-BR", ADDRESS_BILLING_LINE1, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(
ConstructLabelLine({base::ASCIIToUTF16("Tarsila do Amaral"),
base::ASCIIToUTF16("tarsila@aol.com")}),
......@@ -264,10 +264,10 @@ TEST(AddressEmailFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile1, &profile2};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"pt-BR", ADDRESS_BILLING_DEPENDENT_LOCALITY, GetFieldTypes(), profiles);
profiles, "pt-BR", ADDRESS_BILLING_DEPENDENT_LOCALITY, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(
ConstructLabelLine(
{base::UTF8ToUTF16("Av. Pedro Álvares Cabral, 1301"),
......@@ -294,10 +294,10 @@ TEST(AddressEmailFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile1, &profile2};
const std::unique_ptr<LabelFormatter> formatter =
LabelFormatter::Create("pt-BR", EMAIL_ADDRESS, GetFieldTypes(), profiles);
LabelFormatter::Create(profiles, "pt-BR", EMAIL_ADDRESS, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(ConstructLabelLine(
{base::ASCIIToUTF16("Tarsila do Amaral"),
base::UTF8ToUTF16("Av. Pedro Álvares Cabral, 1301")}),
......@@ -316,14 +316,13 @@ TEST(AddressEmailFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile};
const std::unique_ptr<LabelFormatter> formatter =
LabelFormatter::Create("en-US", EMAIL_ADDRESS,
LabelFormatter::Create(profiles, "en-US", EMAIL_ADDRESS,
{NAME_BILLING_FULL, EMAIL_ADDRESS,
ADDRESS_BILLING_CITY, ADDRESS_BILLING_STATE},
profiles);
ADDRESS_BILLING_CITY, ADDRESS_BILLING_STATE});
// Checks that only address fields in the form are shown in the label.
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(ConstructLabelLine({base::ASCIIToUTF16("John F Kennedy"),
base::ASCIIToUTF16("Brookline, MA")})));
}
......
......@@ -9,11 +9,16 @@
namespace autofill {
AddressFormLabelFormatter::AddressFormLabelFormatter(
const std::vector<AutofillProfile*>& profiles,
const std::string& app_locale,
ServerFieldType focused_field_type,
uint32_t groups,
const std::vector<ServerFieldType>& field_types)
: LabelFormatter(app_locale, focused_field_type, groups, field_types),
: LabelFormatter(profiles,
app_locale,
focused_field_type,
groups,
field_types),
form_has_street_address_(HasStreetAddress(field_types_for_labels())) {}
AddressFormLabelFormatter::~AddressFormLabelFormatter() {}
......
......@@ -19,7 +19,8 @@ namespace autofill {
// with name and address fields and without email or phone fields.
class AddressFormLabelFormatter : public LabelFormatter {
public:
AddressFormLabelFormatter(const std::string& app_locale,
AddressFormLabelFormatter(const std::vector<AutofillProfile*>& profiles,
const std::string& app_locale,
ServerFieldType focused_field_type,
uint32_t groups,
const std::vector<ServerFieldType>& field_types);
......
......@@ -32,8 +32,8 @@ std::vector<ServerFieldType> GetFieldTypes() {
TEST(AddressFormLabelFormatterTest, GetLabelsWithMissingProfiles) {
const std::vector<AutofillProfile*> profiles{};
const std::unique_ptr<LabelFormatter> formatter =
LabelFormatter::Create("en-US", NAME_FIRST, GetFieldTypes(), profiles);
EXPECT_TRUE(formatter->GetLabels(profiles).empty());
LabelFormatter::Create(profiles, "en-US", NAME_FIRST, GetFieldTypes());
EXPECT_TRUE(formatter->GetLabels().empty());
}
TEST(AddressFormLabelFormatterTest,
......@@ -63,9 +63,9 @@ TEST(AddressFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile1, &profile2, &profile3,
&profile4};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"en-US", ADDRESS_HOME_LINE1, GetFieldTypes(), profiles);
profiles, "en-US", ADDRESS_HOME_LINE1, GetFieldTypes());
EXPECT_THAT(formatter->GetLabels(profiles),
EXPECT_THAT(formatter->GetLabels(),
ElementsAre(ConstructLabelLine(
{base::ASCIIToUTF16("John F Kennedy"),
base::ASCIIToUTF16("Brookline, MA 02445")}),
......@@ -100,10 +100,10 @@ TEST(AddressFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile1, &profile2, &profile3,
&profile4};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"en-US", ADDRESS_HOME_CITY, GetFieldTypes(), profiles);
profiles, "en-US", ADDRESS_HOME_CITY, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(ConstructLabelLine({base::ASCIIToUTF16("John F Kennedy"),
base::ASCIIToUTF16("333 Washington St")}),
base::ASCIIToUTF16("151 Irving Ave"),
......@@ -125,10 +125,10 @@ TEST(AddressFormLabelFormatterTest, GetLabelsForUSProfilesAndFocusedName) {
const std::vector<AutofillProfile*> profiles{&profile1, &profile2};
const std::unique_ptr<LabelFormatter> formatter =
LabelFormatter::Create("en-US", NAME_FIRST, GetFieldTypes(), profiles);
LabelFormatter::Create(profiles, "en-US", NAME_FIRST, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(base::ASCIIToUTF16("333 Washington St, Brookline, MA 02445"),
base::ASCIIToUTF16("151 Irving Ave, Hyannis, MA 02601")));
}
......@@ -143,10 +143,10 @@ TEST(AddressFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"pt-BR", ADDRESS_HOME_LINE1, GetFieldTypes(), profiles);
profiles, "pt-BR", ADDRESS_HOME_LINE1, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(ConstructLabelLine(
{base::ASCIIToUTF16("Tarsila do Amaral"),
base::UTF8ToUTF16("Vila Mariana, São Paulo-SP, 04094-050")})));
......@@ -162,9 +162,9 @@ TEST(AddressFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"pt-BR", ADDRESS_HOME_ZIP, GetFieldTypes(), profiles);
profiles, "pt-BR", ADDRESS_HOME_ZIP, GetFieldTypes());
EXPECT_THAT(formatter->GetLabels(profiles),
EXPECT_THAT(formatter->GetLabels(),
ElementsAre(ConstructLabelLine(
{base::ASCIIToUTF16("Tarsila do Amaral"),
base::UTF8ToUTF16("Av. Pedro Álvares Cabral, 1301")})));
......@@ -179,9 +179,9 @@ TEST(AddressFormLabelFormatterTest, GetLabelsForBRProfilesAndFocusedName) {
const std::vector<AutofillProfile*> profiles{&profile};
const std::unique_ptr<LabelFormatter> formatter =
LabelFormatter::Create("pt-BR", NAME_FIRST, GetFieldTypes(), profiles);
LabelFormatter::Create(profiles, "pt-BR", NAME_FIRST, GetFieldTypes());
EXPECT_THAT(formatter->GetLabels(profiles),
EXPECT_THAT(formatter->GetLabels(),
ElementsAre(base::UTF8ToUTF16(
"Av. Pedro Álvares Cabral, 1301, Vila Mariana, São "
"Paulo-SP, 04094-050")));
......
......@@ -9,11 +9,16 @@
namespace autofill {
AddressPhoneFormLabelFormatter::AddressPhoneFormLabelFormatter(
const std::vector<AutofillProfile*>& profiles,
const std::string& app_locale,
ServerFieldType focused_field_type,
uint32_t groups,
const std::vector<ServerFieldType>& field_types)
: LabelFormatter(app_locale, focused_field_type, groups, field_types),
: LabelFormatter(profiles,
app_locale,
focused_field_type,
groups,
field_types),
form_has_street_address_(HasStreetAddress(field_types_for_labels())) {}
AddressPhoneFormLabelFormatter::~AddressPhoneFormLabelFormatter() {}
......
......@@ -20,6 +20,7 @@ namespace autofill {
class AddressPhoneFormLabelFormatter : public LabelFormatter {
public:
AddressPhoneFormLabelFormatter(
const std::vector<AutofillProfile*>& profiles,
const std::string& app_locale,
ServerFieldType focused_field_type,
uint32_t groups,
......
......@@ -32,8 +32,8 @@ std::vector<ServerFieldType> GetFieldTypes() {
TEST(AddressPhoneFormLabelFormatterTest, GetLabelsWithMissingProfiles) {
const std::vector<AutofillProfile*> profiles{};
const std::unique_ptr<LabelFormatter> formatter =
LabelFormatter::Create("en-US", NAME_FULL, GetFieldTypes(), profiles);
EXPECT_TRUE(formatter->GetLabels(profiles).empty());
LabelFormatter::Create(profiles, "en-US", NAME_FULL, GetFieldTypes());
EXPECT_TRUE(formatter->GetLabels().empty());
}
TEST(AddressPhoneFormLabelFormatterTest, GetLabelsForUSProfilesAndFocusedName) {
......@@ -62,10 +62,10 @@ TEST(AddressPhoneFormLabelFormatterTest, GetLabelsForUSProfilesAndFocusedName) {
const std::vector<AutofillProfile*> profiles{&profile1, &profile2, &profile3,
&profile4};
const std::unique_ptr<LabelFormatter> formatter =
LabelFormatter::Create("en-US", NAME_FULL, GetFieldTypes(), profiles);
LabelFormatter::Create(profiles, "en-US", NAME_FULL, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(ConstructLabelLine({base::ASCIIToUTF16("(617) 730-2000"),
base::ASCIIToUTF16("333 Washington St")}),
base::ASCIIToUTF16("151 Irving Ave"),
......@@ -99,10 +99,10 @@ TEST(AddressPhoneFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile1, &profile2, &profile3,
&profile4};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"en-US", ADDRESS_HOME_LINE1, GetFieldTypes(), profiles);
profiles, "en-US", ADDRESS_HOME_LINE1, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(ConstructLabelLine({base::ASCIIToUTF16("John F Kennedy"),
base::ASCIIToUTF16("(617) 730-2000")}),
base::ASCIIToUTF16("Jackie Kennedy"),
......@@ -136,10 +136,10 @@ TEST(AddressPhoneFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile1, &profile2, &profile3,
&profile4};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"en-US", ADDRESS_HOME_CITY, GetFieldTypes(), profiles);
profiles, "en-US", ADDRESS_HOME_CITY, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(ConstructLabelLine({base::ASCIIToUTF16("333 Washington St"),
base::ASCIIToUTF16("(617) 730-2000")}),
base::ASCIIToUTF16("151 Irving Ave"),
......@@ -173,10 +173,10 @@ TEST(AddressPhoneFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile1, &profile2, &profile3,
&profile4};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"en-US", PHONE_HOME_WHOLE_NUMBER, GetFieldTypes(), profiles);
profiles, "en-US", PHONE_HOME_WHOLE_NUMBER, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(ConstructLabelLine({base::ASCIIToUTF16("John F Kennedy"),
base::ASCIIToUTF16("333 Washington St")}),
base::ASCIIToUTF16("Jackie Kennedy"),
......@@ -201,10 +201,10 @@ TEST(AddressPhoneFormLabelFormatterTest, GetLabelsForBRProfilesAndFocusedName) {
const std::vector<AutofillProfile*> profiles{&profile1, &profile2};
const std::unique_ptr<LabelFormatter> formatter =
LabelFormatter::Create("pt-BR", NAME_FULL, GetFieldTypes(), profiles);
LabelFormatter::Create(profiles, "pt-BR", NAME_FULL, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(ConstructLabelLine(
{base::ASCIIToUTF16("(11) 2648-0254"),
base::UTF8ToUTF16("Av. Pedro Álvares Cabral, 1301")}),
......@@ -231,10 +231,10 @@ TEST(AddressPhoneFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile1, &profile2};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"pt-BR", ADDRESS_HOME_LINE1, GetFieldTypes(), profiles);
profiles, "pt-BR", ADDRESS_HOME_LINE1, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(ConstructLabelLine({base::ASCIIToUTF16("Tarsila do Amaral"),
base::ASCIIToUTF16("(11) 2648-0254")}),
ConstructLabelLine({base::ASCIIToUTF16("Artur Avila"),
......@@ -259,10 +259,10 @@ TEST(AddressPhoneFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile1, &profile2};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"pt-BR", ADDRESS_HOME_ZIP, GetFieldTypes(), profiles);
profiles, "pt-BR", ADDRESS_HOME_ZIP, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(
ConstructLabelLine(
{base::UTF8ToUTF16("Av. Pedro Álvares Cabral, 1301"),
......@@ -289,10 +289,10 @@ TEST(AddressPhoneFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile1, &profile2};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"pt-BR", PHONE_HOME_WHOLE_NUMBER, GetFieldTypes(), profiles);
profiles, "pt-BR", PHONE_HOME_WHOLE_NUMBER, GetFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(ConstructLabelLine(
{base::ASCIIToUTF16("Tarsila do Amaral"),
base::UTF8ToUTF16("Av. Pedro Álvares Cabral, 1301")}),
......@@ -311,12 +311,12 @@ TEST(AddressPhoneFormLabelFormatterTest,
const std::vector<AutofillProfile*> profiles{&profile};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"en-US", PHONE_HOME_WHOLE_NUMBER,
{NAME_FULL, PHONE_HOME_WHOLE_NUMBER, ADDRESS_HOME_ZIP}, profiles);
profiles, "en-US", PHONE_HOME_WHOLE_NUMBER,
{NAME_FULL, PHONE_HOME_WHOLE_NUMBER, ADDRESS_HOME_ZIP});
// Checks that only address fields in the form are shown in the label.
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(ConstructLabelLine({base::ASCIIToUTF16("John F Kennedy"),
base::ASCIIToUTF16("02445")})));
}
......
......@@ -10,11 +10,16 @@
namespace autofill {
ContactFormLabelFormatter::ContactFormLabelFormatter(
const std::vector<AutofillProfile*>& profiles,
const std::string& app_locale,
ServerFieldType focused_field_type,
uint32_t groups,
const std::vector<ServerFieldType>& field_types)
: LabelFormatter(app_locale, focused_field_type, groups, field_types) {}
: LabelFormatter(profiles,
app_locale,
focused_field_type,
groups,
field_types) {}
ContactFormLabelFormatter::~ContactFormLabelFormatter() {}
......
......@@ -19,7 +19,8 @@ namespace autofill {
// containing name and phone or email fields.
class ContactFormLabelFormatter : public LabelFormatter {
public:
ContactFormLabelFormatter(const std::string& app_locale,
ContactFormLabelFormatter(const std::vector<AutofillProfile*>& profiles,
const std::string& app_locale,
ServerFieldType focused_field_type,
uint32_t groups,
const std::vector<ServerFieldType>& field_types);
......
......@@ -30,8 +30,8 @@ std::vector<ServerFieldType> GetNamePhoneAndEmailFieldTypes() {
TEST(ContactFormLabelFormatterTest, GetLabelsWithMissingProfiles) {
const std::vector<AutofillProfile*> profiles{};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"en-US", NAME_FIRST, GetNamePhoneAndEmailFieldTypes(), profiles);
EXPECT_TRUE(formatter->GetLabels(profiles).empty());
profiles, "en-US", NAME_FIRST, GetNamePhoneAndEmailFieldTypes());
EXPECT_TRUE(formatter->GetLabels().empty());
}
TEST(ContactFormLabelFormatterTest, GetLabelsForUSProfilesAndFocusedName) {
......@@ -61,10 +61,10 @@ TEST(ContactFormLabelFormatterTest, GetLabelsForUSProfilesAndFocusedName) {
const std::vector<AutofillProfile*> profiles{&profile1, &profile2, &profile3,
&profile4};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"en-US", NAME_LAST, GetNamePhoneAndEmailFieldTypes(), profiles);
profiles, "en-US", NAME_LAST, GetNamePhoneAndEmailFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(ConstructLabelLine({base::ASCIIToUTF16("(617) 730-2000"),
base::ASCIIToUTF16("jfk@gmail.com")}),
base::ASCIIToUTF16("jackie@outlook.com"),
......@@ -97,10 +97,10 @@ TEST(ContactFormLabelFormatterTest, GetLabelsForUSProfilesAndFocusedEmail) {
const std::vector<AutofillProfile*> profiles{&profile1, &profile2, &profile3,
&profile4};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"en-US", EMAIL_ADDRESS, GetNamePhoneAndEmailFieldTypes(), profiles);
profiles, "en-US", EMAIL_ADDRESS, GetNamePhoneAndEmailFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(ConstructLabelLine({base::ASCIIToUTF16("John F Kennedy"),
base::ASCIIToUTF16("(617) 730-2000")}),
base::ASCIIToUTF16("Jackie Kennedy"),
......@@ -135,11 +135,11 @@ TEST(ContactFormLabelFormatterTest, GetLabelsForUSProfilesAndFocusedPhone) {
const std::vector<AutofillProfile*> profiles{&profile1, &profile2, &profile3,
&profile4};
const std::unique_ptr<LabelFormatter> formatter =
LabelFormatter::Create("en-US", PHONE_HOME_WHOLE_NUMBER,
GetNamePhoneAndEmailFieldTypes(), profiles);
LabelFormatter::Create(profiles, "en-US", PHONE_HOME_WHOLE_NUMBER,
GetNamePhoneAndEmailFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(
ConstructLabelLine({base::ASCIIToUTF16("John F Kennedy"),
base::ASCIIToUTF16("jfk@gmail.com")}),
......@@ -165,10 +165,10 @@ TEST(ContactFormLabelFormatterTest, GetLabelsForBRProfilesAndFocusedName) {
const std::vector<AutofillProfile*> profiles{&profile1, &profile2};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"pt-BR", NAME_LAST, GetNamePhoneAndEmailFieldTypes(), profiles);
profiles, "pt-BR", NAME_LAST, GetNamePhoneAndEmailFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(
ConstructLabelLine({base::ASCIIToUTF16("(11) 2648-0254"),
base::ASCIIToUTF16("tarsila@aol.com")}),
......@@ -193,10 +193,10 @@ TEST(ContactFormLabelFormatterTest, GetLabelsForBRProfilesAndFocusedEmail) {
const std::vector<AutofillProfile*> profiles{&profile1, &profile2};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"pt-BR", EMAIL_ADDRESS, GetNamePhoneAndEmailFieldTypes(), profiles);
profiles, "pt-BR", EMAIL_ADDRESS, GetNamePhoneAndEmailFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(ConstructLabelLine({base::ASCIIToUTF16("Tarsila do Amaral"),
base::ASCIIToUTF16("(11) 2648-0254")}),
ConstructLabelLine({base::ASCIIToUTF16("Artur Avila"),
......@@ -220,11 +220,11 @@ TEST(ContactFormLabelFormatterTest, GetLabelsForBRProfilesAndFocusedPhone) {
const std::vector<AutofillProfile*> profiles{&profile1, &profile2};
const std::unique_ptr<LabelFormatter> formatter =
LabelFormatter::Create("pt-BR", PHONE_HOME_WHOLE_NUMBER,
GetNamePhoneAndEmailFieldTypes(), profiles);
LabelFormatter::Create(profiles, "pt-BR", PHONE_HOME_WHOLE_NUMBER,
GetNamePhoneAndEmailFieldTypes());
EXPECT_THAT(
formatter->GetLabels(profiles),
formatter->GetLabels(),
ElementsAre(
ConstructLabelLine({base::ASCIIToUTF16("Tarsila do Amaral"),
base::ASCIIToUTF16("tarsila@aol.com")}),
......@@ -240,13 +240,13 @@ TEST(ContactFormLabelFormatterTest, GetLabelsForNameAndPhoneWithFocusedName) {
"US", "16177302000");
const std::vector<AutofillProfile*> profiles{&profile};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"en-US", NAME_LAST, {NAME_FIRST, NAME_LAST, PHONE_HOME_WHOLE_NUMBER},
profiles);
const std::unique_ptr<LabelFormatter> formatter =
LabelFormatter::Create(profiles, "en-US", NAME_LAST,
{NAME_FIRST, NAME_LAST, PHONE_HOME_WHOLE_NUMBER});
// Checks that the email address is excluded when the form does not contain an
// email field.
EXPECT_THAT(formatter->GetLabels(profiles),
EXPECT_THAT(formatter->GetLabels(),
ElementsAre(base::ASCIIToUTF16("(617) 730-2000")));
}
......@@ -258,13 +258,13 @@ TEST(ContactFormLabelFormatterTest, GetLabelsForNameAndPhoneWithFocusedPhone) {
"US", "16177302000");
const std::vector<AutofillProfile*> profiles{&profile};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"en-US", PHONE_HOME_WHOLE_NUMBER,
{NAME_FIRST, NAME_LAST, PHONE_HOME_WHOLE_NUMBER}, profiles);
const std::unique_ptr<LabelFormatter> formatter =
LabelFormatter::Create(profiles, "en-US", PHONE_HOME_WHOLE_NUMBER,
{NAME_FIRST, NAME_LAST, PHONE_HOME_WHOLE_NUMBER});
// Checks that the email address is excluded when the form does not contain an
// email field.
EXPECT_THAT(formatter->GetLabels(profiles),
EXPECT_THAT(formatter->GetLabels(),
ElementsAre(base::ASCIIToUTF16("John F Kennedy")));
}
......@@ -277,11 +277,11 @@ TEST(ContactFormLabelFormatterTest, GetLabelsForNameAndEmailWithFocusedName) {
const std::vector<AutofillProfile*> profiles{&profile};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"en-US", NAME_LAST, {NAME_FIRST, NAME_LAST, EMAIL_ADDRESS}, profiles);
profiles, "en-US", NAME_LAST, {NAME_FIRST, NAME_LAST, EMAIL_ADDRESS});
// Checks that the phone number is excluded when the form does not contain a
// phone field.
EXPECT_THAT(formatter->GetLabels(profiles),
EXPECT_THAT(formatter->GetLabels(),
ElementsAre(base::ASCIIToUTF16("jfk@gmail.com")));
}
......@@ -294,11 +294,11 @@ TEST(ContactFormLabelFormatterTest, GetLabelsForNameAndEmailWithFocusedEmail) {
const std::vector<AutofillProfile*> profiles{&profile};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
"en-US", EMAIL_ADDRESS, {NAME_FIRST, NAME_LAST, EMAIL_ADDRESS}, profiles);
profiles, "en-US", EMAIL_ADDRESS, {NAME_FIRST, NAME_LAST, EMAIL_ADDRESS});
// Checks that the phone number is excluded when the form does not contain a
// phone field.
EXPECT_THAT(formatter->GetLabels(profiles),
EXPECT_THAT(formatter->GetLabels(),
ElementsAre(base::ASCIIToUTF16("John F Kennedy")));
}
......
......@@ -24,11 +24,13 @@ using data_util::bit_field_type_groups::kEmail;
using data_util::bit_field_type_groups::kName;
using data_util::bit_field_type_groups::kPhone;
LabelFormatter::LabelFormatter(const std::string& app_locale,
LabelFormatter::LabelFormatter(const std::vector<AutofillProfile*>& profiles,
const std::string& app_locale,
ServerFieldType focused_field_type,
uint32_t groups,
const std::vector<ServerFieldType>& field_types)
: app_locale_(app_locale),
: profiles_(profiles),
app_locale_(app_locale),
focused_field_type_(focused_field_type),
groups_(groups) {
const FieldTypeGroup focused_group = GetFocusedNonBillingGroup();
......@@ -60,10 +62,9 @@ LabelFormatter::LabelFormatter(const std::string& app_locale,
LabelFormatter::~LabelFormatter() = default;
std::vector<base::string16> LabelFormatter::GetLabels(
const std::vector<AutofillProfile*>& profiles) const {
std::vector<base::string16> LabelFormatter::GetLabels() const {
std::vector<base::string16> labels;
for (const AutofillProfile* profile : profiles) {
for (const AutofillProfile* profile : profiles_) {
labels.push_back(GetLabelForProfile(*profile, GetFocusedNonBillingGroup()));
}
return labels;
......@@ -76,32 +77,30 @@ FieldTypeGroup LabelFormatter::GetFocusedNonBillingGroup() const {
// static
std::unique_ptr<LabelFormatter> LabelFormatter::Create(
const std::vector<AutofillProfile*>& profiles,
const std::string& app_locale,
ServerFieldType focused_field_type,
const std::vector<ServerFieldType>& field_types,
const std::vector<AutofillProfile*>& profiles) {
const std::vector<ServerFieldType>& field_types) {
const uint32_t groups = data_util::DetermineGroups(field_types);
switch (groups) {
case kName | kAddress | kEmail | kPhone:
return std::make_unique<AddressContactFormLabelFormatter>(
app_locale, focused_field_type, groups, field_types,
!HaveSamePhoneNumbers(profiles, app_locale),
!HaveSameEmailAddresses(profiles, app_locale));
profiles, app_locale, focused_field_type, groups, field_types);
case kName | kAddress | kPhone:
return std::make_unique<AddressPhoneFormLabelFormatter>(
app_locale, focused_field_type, groups, field_types);
profiles, app_locale, focused_field_type, groups, field_types);
case kName | kAddress | kEmail:
return std::make_unique<AddressEmailFormLabelFormatter>(
app_locale, focused_field_type, groups, field_types);
profiles, app_locale, focused_field_type, groups, field_types);
case kName | kAddress:
return std::make_unique<AddressFormLabelFormatter>(
app_locale, focused_field_type, groups, field_types);
profiles, app_locale, focused_field_type, groups, field_types);
case kName | kEmail | kPhone:
case kName | kEmail:
case kName | kPhone:
return std::make_unique<ContactFormLabelFormatter>(
app_locale, focused_field_type, groups, field_types);
profiles, app_locale, focused_field_type, groups, field_types);
default:
return nullptr;
}
......
......@@ -18,7 +18,8 @@ namespace autofill {
// Handles the creation of Suggestions' disambiguating labels.
class LabelFormatter {
public:
LabelFormatter(const std::string& app_locale,
LabelFormatter(const std::vector<AutofillProfile*>& profiles,
const std::string& app_locale,
ServerFieldType focused_field_type,
uint32_t groups,
const std::vector<ServerFieldType>& field_types);
......@@ -29,18 +30,17 @@ class LabelFormatter {
uint32_t groups() const { return groups_; }
// Returns a collection of labels formed by extracting useful disambiguating
// information from a collection of |profiles_|.
std::vector<base::string16> GetLabels(
const std::vector<AutofillProfile*>& profiles) const;
// information from |profiles_|.
std::vector<base::string16> GetLabels() const;
// Creates a form-specific LabelFormatter according to |field_types|. This
// formatter has the ability to build labels with disambiguating information
// from the given |profiles|.
static std::unique_ptr<LabelFormatter> Create(
const std::vector<AutofillProfile*>& profiles,
const std::string& app_locale,
ServerFieldType focused_field_type,
const std::vector<ServerFieldType>& field_types,
const std::vector<AutofillProfile*>& profiles);
const std::vector<ServerFieldType>& field_types);
protected:
// Returns a label to show the user. The elements of the label and their
......@@ -65,6 +65,15 @@ class LabelFormatter {
}
private:
// The collection of profiles for which to build labels. Storing this
// collection ensures that the profiles for which this formatter is created
// are the profiles for which the labels are constructed.
//
// It is safe to store a reference here because the LabelFormatter is
// destroyed when the suggestions for which the labels are constructed are
// returned.
const std::vector<AutofillProfile*>& profiles_;
// The locale for which to generate labels. This reflects the language and
// country for which the application is translated, e.g. en-AU for Australian
// English.
......
......@@ -14,9 +14,9 @@ namespace autofill {
namespace {
TEST(LabelFormatterTest, CreateWithMissingFieldTypes) {
EXPECT_EQ(LabelFormatter::Create("en-US", NAME_FIRST,
std::vector<ServerFieldType>(),
std::vector<AutofillProfile*>()),
const std::vector<AutofillProfile*> profiles{};
EXPECT_EQ(LabelFormatter::Create(profiles, "en-US", NAME_FIRST,
std::vector<ServerFieldType>()),
nullptr);
}
......
......@@ -1170,18 +1170,19 @@ std::vector<Suggestion> PersonalDataManager::GetProfileSuggestions(
std::unique_ptr<LabelFormatter> formatter;
#if !defined(OS_ANDROID) && !defined(OS_IOS)
bool use_improved_label_disambiguation = base::FeatureList::IsEnabled(
autofill::features::kAutofillUseImprovedLabelDisambiguation);
formatter = use_improved_label_disambiguation
? LabelFormatter::Create(app_locale_, type.GetStorableType(),
field_types, unique_matched_profiles)
// The formatter stores a constant reference to |unique_matched_profiles|.
// This is safe since the formatter is destroyed when this function returns.
formatter = base::FeatureList::IsEnabled(
autofill::features::kAutofillUseImprovedLabelDisambiguation)
? LabelFormatter::Create(unique_matched_profiles, app_locale_,
type.GetStorableType(), field_types)
: nullptr;
#endif // !defined(OS_ANDROID) && !defined(OS_IOS)
// Generate disambiguating labels based on the list of matches.
std::vector<base::string16> labels;
if (formatter) {
labels = formatter->GetLabels(unique_matched_profiles);
labels = formatter->GetLabels();
} else {
AutofillProfile::CreateInferredLabels(unique_matched_profiles, &field_types,
type.GetStorableType(), 1,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment