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

[Autofill] Adds a function to see if profiles have the same data.

When constructing labels for a collection of profiles, it is useful to
know whether certain fields, such as email address and first name, can
disambiguate profiles.

Also (1) changes the name of GetLabelName to GetLabelFullName, (2) adds
GetLabelFirstName, and (3) removes HaveSameEmailAddresses.

GetLabelFirstName will be used by mobile-friendly LabelFormatters since
first names take up less space than full names.

This is a separate CL to reduce the size and complexity of the upcoming
mobile-friendly-related LabelFormatter changes.

Bug: 958333
Change-Id: I971cb78342ed89987765bb16656c302e5bc4d4e2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1594074
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@{#658716}
parent 7f6503d9
......@@ -37,7 +37,8 @@ base::string16 AddressContactFormLabelFormatter::GetLabelForProfile(
!IsStreetAddressPart(focused_field_type());
if (focused_group != NAME && !non_street_address_is_focused) {
AddLabelPartIfNotEmpty(GetLabelName(profile, app_locale()), &label_parts);
AddLabelPartIfNotEmpty(GetLabelFullName(profile, app_locale()),
&label_parts);
}
if (!street_address_is_focused) {
......
......@@ -41,7 +41,8 @@ base::string16 AddressEmailFormLabelFormatter::
std::vector<base::string16> label_parts;
if (focused_group != NAME) {
AddLabelPartIfNotEmpty(GetLabelName(profile, app_locale()), &label_parts);
AddLabelPartIfNotEmpty(GetLabelFullName(profile, app_locale()),
&label_parts);
}
if (focused_group != ADDRESS_HOME) {
......
......@@ -26,7 +26,8 @@ base::string16 AddressFormLabelFormatter::GetLabelForProfile(
field_types_for_labels());
} else {
std::vector<base::string16> label_parts;
AddLabelPartIfNotEmpty(GetLabelName(profile, app_locale()), &label_parts);
AddLabelPartIfNotEmpty(GetLabelFullName(profile, app_locale()),
&label_parts);
AddLabelPartIfNotEmpty(GetLabelForFocusedAddress(
focused_field_type(), form_has_street_address_,
profile, app_locale(), field_types_for_labels()),
......
......@@ -40,7 +40,8 @@ base::string16 AddressPhoneFormLabelFormatter::
FieldTypeGroup focused_group) const {
std::vector<base::string16> label_parts;
if (focused_group != NAME) {
AddLabelPartIfNotEmpty(GetLabelName(profile, app_locale()), &label_parts);
AddLabelPartIfNotEmpty(GetLabelFullName(profile, app_locale()),
&label_parts);
}
if (focused_group != PHONE_HOME) {
......
......@@ -26,7 +26,8 @@ base::string16 ContactFormLabelFormatter::GetLabelForProfile(
FieldTypeGroup focused_group) const {
std::vector<base::string16> label_parts;
if (focused_group != NAME) {
AddLabelPartIfNotEmpty(GetLabelName(profile, app_locale()), &label_parts);
AddLabelPartIfNotEmpty(GetLabelFullName(profile, app_locale()),
&label_parts);
}
if (focused_group != PHONE_HOME) {
......
......@@ -8,6 +8,8 @@
#include <iterator>
#include <memory>
#include "base/bind.h"
#include "base/callback.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/address_i18n.h"
......@@ -21,6 +23,38 @@
#include "ui/base/l10n/l10n_util.h"
namespace autofill {
namespace {
// Returns true if all |profiles| have the same value for the data retrieved by
// |get_data|.
bool HaveSameData(
const std::vector<AutofillProfile*>& profiles,
const std::string& app_locale,
base::RepeatingCallback<base::string16(const AutofillProfile&,
const std::string&)> get_data,
base::RepeatingCallback<bool(const base::string16& str1,
const base::string16& str2)> matches) {
if (profiles.size() <= 1) {
return true;
}
const base::string16 first_profile_data =
get_data.Run(*profiles[0], app_locale);
for (size_t i = 1; i < profiles.size(); ++i) {
const base::string16 current_profile_data =
get_data.Run(*profiles[i], app_locale);
if (!matches.Run(first_profile_data, current_profile_data)) {
return false;
}
}
return true;
}
bool Equals(const base::string16& str1, const base::string16& str2) {
return str1 == str2;
}
} // namespace
const int kStreetAddressFieldTypes[] = {
ADDRESS_HOME_LINE1, ADDRESS_HOME_LINE2,
......@@ -126,11 +160,6 @@ AutofillProfile MakeTrimmedProfile(const AutofillProfile& profile,
return trimmed_profile;
}
base::string16 GetLabelName(const AutofillProfile& profile,
const std::string& app_locale) {
return profile.GetInfo(AutofillType(NAME_FULL), app_locale);
}
base::string16 GetLabelForFocusedAddress(
ServerFieldType focused_field_type,
bool form_has_street_address,
......@@ -197,6 +226,16 @@ base::string16 GetLabelForProfileOnFocusedNonStreetAddress(
return ConstructLabelLine(label_parts);
}
base::string16 GetLabelFullName(const AutofillProfile& profile,
const std::string& app_locale) {
return profile.GetInfo(AutofillType(NAME_FULL), app_locale);
}
base::string16 GetLabelFirstName(const AutofillProfile& profile,
const std::string& app_locale) {
return profile.GetInfo(AutofillType(NAME_FIRST), app_locale);
}
base::string16 GetLabelEmail(const AutofillProfile& profile,
const std::string& app_locale) {
const base::string16 email =
......@@ -217,46 +256,35 @@ base::string16 GetLabelPhone(const AutofillProfile& profile,
bool HaveSameEmailAddresses(const std::vector<AutofillProfile*>& profiles,
const std::string& app_locale) {
bool first_email_found = false;
base::string16 first_email;
for (const AutofillProfile* profile : profiles) {
base::string16 email_from_profile = GetLabelEmail(*profile, app_locale);
if (!first_email_found) {
// Store the first email address whether it's empty or not because we
// consider "" and "hao.le@aol.com" to be different email addresses.
first_email_found = true;
first_email = email_from_profile;
} else if (email_from_profile != first_email) {
return false;
}
}
return true;
return HaveSameData(profiles, app_locale, base::BindRepeating(&GetLabelEmail),
base::BindRepeating(&Equals));
}
bool HaveSameFirstNames(const std::vector<AutofillProfile*>& profiles,
const std::string& app_locale) {
return HaveSameData(profiles, app_locale,
base::BindRepeating(&GetLabelFirstName),
base::BindRepeating(&Equals));
}
bool HaveSamePhoneNumbers(const std::vector<AutofillProfile*>& profiles,
const std::string& app_locale) {
bool first_phone_found = false;
base::string16 first_phone;
for (const AutofillProfile* profile : profiles) {
base::string16 phone_from_profile = GetLabelPhone(*profile, app_locale);
if (!first_phone_found) {
// Store the first phone number whether it's empty or not because we
// consider "" and "(514) 873-1100" to be different phone numbers.
first_phone_found = true;
first_phone = phone_from_profile;
} else if (!(first_phone.empty() && phone_from_profile.empty()) &&
!i18n::PhoneNumbersMatch(first_phone, phone_from_profile,
base::UTF16ToASCII(profile->GetInfo(
ADDRESS_HOME_COUNTRY, app_locale)),
app_locale)) {
return false;
}
}
return true;
// Note that the same country code is used in all comparisons.
auto equals = [](const std::string& country_code,
const std::string& app_locale, const base::string16& phone1,
const base::string16& phone2) -> bool {
return (phone1.empty() && phone2.empty()) ||
i18n::PhoneNumbersMatch(phone1, phone2, country_code, app_locale);
};
return profiles.size() <= 1
? true
: HaveSameData(
profiles, app_locale, base::BindRepeating(&GetLabelPhone),
base::BindRepeating(equals,
base::UTF16ToASCII(profiles[0]->GetInfo(
ADDRESS_HOME_COUNTRY, app_locale)),
app_locale));
}
} // namespace autofill
......@@ -64,10 +64,6 @@ AutofillProfile MakeTrimmedProfile(const AutofillProfile& profile,
const std::string& app_locale,
const std::vector<ServerFieldType>& types);
// Returns the full name associated with |profile|.
base::string16 GetLabelName(const AutofillProfile& profile,
const std::string& app_locale);
// Returns either street-address data or non-street-address data found in
// |profile|. If |focused_field_type| is a street address field, then returns
// non-street-address data, e.g. Lowell, MA 01852.
......@@ -122,6 +118,16 @@ base::string16 GetLabelForProfileOnFocusedNonStreetAddress(
const std::vector<ServerFieldType>& types,
const base::string16& contact_info);
// Returns the full name associated with |profile|, if any; otherwise, returns
// an empty string.
base::string16 GetLabelFullName(const AutofillProfile& profile,
const std::string& app_locale);
// Returns the first name associated with |profile|, if any; otherwise, returns
// an empty string.
base::string16 GetLabelFirstName(const AutofillProfile& profile,
const std::string& app_locale);
// Returns the email address associated with |profile|, if any; otherwise,
// returns an empty string.
base::string16 GetLabelEmail(const AutofillProfile& profile,
......@@ -139,12 +145,17 @@ base::string16 GetLabelPhone(const AutofillProfile& profile,
bool HaveSameEmailAddresses(const std::vector<AutofillProfile*>& profiles,
const std::string& app_locale);
// Returns true if all |profiles| have the same first name. Note that names are
// compared without normalization, so José and Jose are considered different
// names.
bool HaveSameFirstNames(const std::vector<AutofillProfile*>& profiles,
const std::string& app_locale);
// Returns true if all |profiles| have the same phone number after
// normalization. Note that the absence of a phone number and an actual phone
// number, e.g. (401) 847-8720, are considered different phone numbers.
bool HaveSamePhoneNumbers(const std::vector<AutofillProfile*>& profiles,
const std::string& app_locale);
} // namespace autofill
#endif // COMPONENTS_AUTOFILL_CORE_BROWSER_LABEL_FORMATTER_UTILS_H_
......@@ -4,6 +4,7 @@
#include "components/autofill/core/browser/label_formatter_utils.h"
#include "base/bind.h"
#include "base/guid.h"
#include "base/strings/string16.h"
#include "base/strings/string_util.h"
......@@ -19,24 +20,73 @@
namespace autofill {
namespace {
TEST(LabelFormatterUtilsTest, ConstructLabelLine) {
EXPECT_EQ(base::string16(), ConstructLabelLine({}));
TEST(LabelFormatterUtilsTest, HaveSameFirstNames_OneProfileAndNoFirstName) {
AutofillProfile profile =
AutofillProfile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile, "", "", "", "", "", "", "", "", "", "", "DE",
"");
EXPECT_TRUE(HaveSameFirstNames({&profile}, "de"));
}
base::string16 name = base::ASCIIToUTF16("Blaise Pascal");
base::string16 phone = base::ASCIIToUTF16("01 53 01 82 00");
base::string16 email = base::ASCIIToUTF16("b.pascal@orange.fr");
TEST(LabelFormatterUtilsTest, HaveSameFirstNames_OneProfileAndFirstName) {
AutofillProfile profile =
AutofillProfile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile, "Maria", "", "", "", "", "", "", "", "", "",
"DE", "");
EXPECT_TRUE(HaveSameFirstNames({&profile}, "de"));
}
base::string16 separator =
l10n_util::GetStringUTF16(IDS_AUTOFILL_SUGGESTION_LABEL_SEPARATOR);
TEST(LabelFormatterUtilsTest, HaveSameFirstNames_NoFirstNames) {
AutofillProfile profile1 =
AutofillProfile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile1, "", "", "Kirch", "", "", "", "", "", "", "",
"DE", "");
AutofillProfile profile2 =
AutofillProfile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile2, "", "", "Winckelmann", "", "", "", "", "", "",
"", "DE", "");
EXPECT_TRUE(HaveSameFirstNames({&profile1, &profile2}, "de"));
}
EXPECT_EQ(name, ConstructLabelLine({name}));
EXPECT_EQ(base::JoinString({name, separator, phone, separator, email},
base::string16()),
ConstructLabelLine({name, phone, email}));
TEST(LabelFormatterUtilsTest, HaveSameFirstNames_SameFirstNames) {
AutofillProfile profile1 =
AutofillProfile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile1, "Maria", "", "Kirch", "", "", "", "", "", "",
"", "DE", "");
AutofillProfile profile2 =
AutofillProfile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile2, "Maria", "", "Winckelmann", "", "", "", "",
"", "", "", "DE", "");
EXPECT_TRUE(HaveSameFirstNames({&profile1, &profile2}, "de"));
}
TEST(LabelFormatterUtilsTest, HaveSameFirstNames_DifferentNonEmptyFirstNames) {
AutofillProfile profile1 =
AutofillProfile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile1, "Maria", "", "Kirch", "", "", "", "", "", "",
"", "DE", "");
AutofillProfile profile2 =
AutofillProfile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile2, "Mary", "", "Kirch", "", "", "", "", "", "",
"", "DE", "");
EXPECT_FALSE(HaveSameFirstNames({&profile1, &profile2}, "de"));
}
TEST(LabelFormatterUtilsTest, HaveSameFirstNames_NonEmptyAndEmptyFirstNames) {
AutofillProfile profile1 =
AutofillProfile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile1, "Maria", "Margaretha", "Kirch", "", "", "",
"", "", "", "", "DE", "");
AutofillProfile profile2 =
AutofillProfile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile2, "", "Margaretha", "Winckelmann", "", "", "",
"", "", "", "", "DE", "");
EXPECT_FALSE(HaveSameFirstNames({&profile1, &profile2}, "de"));
EXPECT_FALSE(HaveSameFirstNames({&profile2, &profile1}, "de"));
}
TEST(LabelFormatterUtilsTest,
HaveSameEmailAddressesWithOneProfileAndNoEmailAddress) {
HaveSameEmailAddresses_OneProfileAndNoEmailAddress) {
AutofillProfile profile =
AutofillProfile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile, "Maria", "Margaretha", "Kirch",
......@@ -45,7 +95,7 @@ TEST(LabelFormatterUtilsTest,
}
TEST(LabelFormatterUtilsTest,
HaveSameEmailAddressesWithOneProfileAndEmailAddress) {
HaveSameEmailAddresses_OneProfileAndEmailAddress) {
AutofillProfile profile =
AutofillProfile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile, "Maria", "Margaretha", "Kirch", "", "", "", "",
......@@ -53,8 +103,7 @@ TEST(LabelFormatterUtilsTest,
EXPECT_TRUE(HaveSameEmailAddresses({&profile}, "de"));
}
TEST(LabelFormatterUtilsTest,
HaveSameEmailAddressesWithProfilesAndNoEmailAddresses) {
TEST(LabelFormatterUtilsTest, HaveSameEmailAddresses_NoEmailAddresses) {
AutofillProfile profile1 =
AutofillProfile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile1, "Maria", "Margaretha", "Kirch", "", "", "",
......@@ -66,8 +115,7 @@ TEST(LabelFormatterUtilsTest,
EXPECT_TRUE(HaveSameEmailAddresses({&profile1, &profile2}, "de"));
}
TEST(LabelFormatterUtilsTest,
HaveSameEmailAddressesWithProfilesAndSameEmailAddresses) {
TEST(LabelFormatterUtilsTest, HaveSameEmailAddresses_SameEmailAddresses) {
AutofillProfile profile1 =
AutofillProfile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile1, "Maria", "Margaretha", "Kirch",
......@@ -80,7 +128,7 @@ TEST(LabelFormatterUtilsTest,
}
TEST(LabelFormatterUtilsTest,
HaveSameEmailAddressesWithProfilesAndDifferentNonEmptyEmailAddresses) {
HaveSameEmailAddresses_DifferentNonEmptyEmailAddresses) {
AutofillProfile profile1 =
AutofillProfile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile1, "Maria", "Margaretha", "Kirch",
......@@ -93,7 +141,7 @@ TEST(LabelFormatterUtilsTest,
}
TEST(LabelFormatterUtilsTest,
HaveSameEmailAddressesWithProfilesAndNonEmptyAndEmptyEmailAddresses) {
HaveSameEmailAddresses_NonEmptyAndEmptyEmailAddresses) {
AutofillProfile profile1 =
AutofillProfile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile1, "Maria", "Margaretha", "Kirch",
......@@ -106,8 +154,7 @@ TEST(LabelFormatterUtilsTest,
EXPECT_FALSE(HaveSameEmailAddresses({&profile2, &profile1}, "de"));
}
TEST(LabelFormatterUtilsTest,
HaveSamePhoneNumbersWithOneProfileAndNoPhoneNumber) {
TEST(LabelFormatterUtilsTest, HaveSamePhoneNumbers_OneProfileAndNoPhoneNumber) {
AutofillProfile profile =
AutofillProfile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile, "Maria", "Margaretha", "Kirch", "", "", "", "",
......@@ -115,8 +162,7 @@ TEST(LabelFormatterUtilsTest,
EXPECT_TRUE(HaveSamePhoneNumbers({&profile}, "de"));
}
TEST(LabelFormatterUtilsTest,
HaveSamePhoneNumbersWithOneProfileAndPhoneNumber) {
TEST(LabelFormatterUtilsTest, HaveSamePhoneNumbers_OneProfileAndPhoneNumber) {
AutofillProfile profile =
AutofillProfile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile, "Maria", "Margaretha", "Kirch", "", "", "", "",
......@@ -124,8 +170,7 @@ TEST(LabelFormatterUtilsTest,
EXPECT_TRUE(HaveSamePhoneNumbers({&profile}, "de"));
}
TEST(LabelFormatterUtilsTest,
HaveSamePhoneNumbersWithProfilesAndNoPhoneNumber) {
TEST(LabelFormatterUtilsTest, HaveSamePhoneNumbers_NoPhoneNumber) {
AutofillProfile profile1 =
AutofillProfile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile1, "Maria", "Margaretha", "Kirch", "", "", "",
......@@ -137,8 +182,7 @@ TEST(LabelFormatterUtilsTest,
EXPECT_TRUE(HaveSamePhoneNumbers({&profile1, &profile2}, "de"));
}
TEST(LabelFormatterUtilsTest,
HaveSamePhoneNumbersWithProfilesAndSamePhoneNumbers) {
TEST(LabelFormatterUtilsTest, HaveSamePhoneNumbers_SamePhoneNumbers) {
AutofillProfile profile1 =
AutofillProfile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile1, "Maria", "Margaretha", "Kirch", "", "", "",
......@@ -155,7 +199,7 @@ TEST(LabelFormatterUtilsTest,
}
TEST(LabelFormatterUtilsTest,
HaveSamePhoneNumbersWithProfilesAndDifferentNonEmptyPhoneNumbers) {
HaveSamePhoneNumbers_DifferentNonEmptyPhoneNumbers) {
AutofillProfile profile1 =
AutofillProfile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile1, "Maria", "Margaretha", "Kirch", "", "", "",
......@@ -168,7 +212,7 @@ TEST(LabelFormatterUtilsTest,
}
TEST(LabelFormatterUtilsTest,
HaveSamePhoneNumbersWithProfilesAndNonEmptyAndEmptyPhoneNumbers) {
HaveSamePhoneNumbers_NonEmptyAndEmptyPhoneNumbers) {
AutofillProfile profile1 =
AutofillProfile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile1, "Maria", "Margaretha", "Kirch", "", "", "",
......
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