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