Commit 1406fedf authored by Caitlin Fischer's avatar Caitlin Fischer Committed by Commit Bot

[Autofill] Allows forms without name fields to be formatted.

Bug: 969137
Change-Id: I05d0646f11e0546197b1502f35b3d79bfa8cb324
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1637055Reviewed-by: default avatarFabio Tirelo <ftirelo@chromium.org>
Commit-Queue: Caitlin Fischer <caitlinfischer@google.com>
Cr-Commit-Position: refs/heads/master@{#665711}
parent 5ae85268
...@@ -2784,28 +2784,6 @@ TEST_F(PersonalDataManagerTest, ...@@ -2784,28 +2784,6 @@ TEST_F(PersonalDataManagerTest,
} }
#endif // #if !defined(OS_ANDROID) && !defined(OS_IOS) #endif // #if !defined(OS_ANDROID) && !defined(OS_IOS)
#if !defined(OS_ANDROID) && !defined(OS_IOS)
TEST_F(PersonalDataManagerTest, GetProfileSuggestions_FormWithoutNameField) {
AutofillProfile profile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile, "Hoa", "", "Pham", "hoa.pham@comcast.net", "",
"", "", "", "", "01852", "US", "");
AddProfileToPersonalDataManager(profile);
base::test::ScopedFeatureList scoped_features;
scoped_features.InitAndEnableFeature(
features::kAutofillUseImprovedLabelDisambiguation);
EXPECT_THAT(personal_data_->GetProfileSuggestions(
AutofillType(ADDRESS_HOME_ZIP), base::string16(), false,
std::vector<ServerFieldType>{ADDRESS_HOME_ZIP, EMAIL_ADDRESS,
PHONE_HOME_WHOLE_NUMBER}),
ElementsAre(AllOf(
testing::Field(&Suggestion::label,
base::ASCIIToUTF16("hoa.pham@comcast.net")),
testing::Field(&Suggestion::icon, ""))));
}
#endif // #if !defined(OS_ANDROID) && !defined(OS_IOS)
#if defined(OS_ANDROID) || defined(OS_IOS) #if defined(OS_ANDROID) || defined(OS_IOS)
TEST_F(PersonalDataManagerTest, GetProfileSuggestions_MobileShowOne) { TEST_F(PersonalDataManagerTest, GetProfileSuggestions_MobileShowOne) {
std::map<std::string, std::string> parameters; std::map<std::string, std::string> parameters;
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "components/autofill/core/browser/ui/address_contact_form_label_formatter.h" #include "components/autofill/core/browser/ui/address_contact_form_label_formatter.h"
#include "components/autofill/core/browser/autofill_data_util.h"
#include "components/autofill/core/browser/ui/label_formatter_utils.h" #include "components/autofill/core/browser/ui/label_formatter_utils.h"
namespace autofill { namespace autofill {
...@@ -39,7 +40,8 @@ base::string16 AddressContactFormLabelFormatter::GetLabelForProfile( ...@@ -39,7 +40,8 @@ base::string16 AddressContactFormLabelFormatter::GetLabelForProfile(
focused_group == ADDRESS_HOME && focused_group == ADDRESS_HOME &&
!IsStreetAddressPart(focused_field_type()); !IsStreetAddressPart(focused_field_type());
if (focused_group != NAME && !non_street_address_is_focused) { if (focused_group != NAME && !non_street_address_is_focused &&
data_util::ContainsName(groups())) {
AddLabelPartIfNotEmpty(GetLabelFullName(profile, app_locale()), AddLabelPartIfNotEmpty(GetLabelFullName(profile, app_locale()),
&label_parts); &label_parts);
} }
...@@ -59,6 +61,15 @@ base::string16 AddressContactFormLabelFormatter::GetLabelForProfile( ...@@ -59,6 +61,15 @@ base::string16 AddressContactFormLabelFormatter::GetLabelForProfile(
AddLabelPartIfNotEmpty(GetLabelEmail(profile, app_locale()), &label_parts); AddLabelPartIfNotEmpty(GetLabelEmail(profile, app_locale()), &label_parts);
} }
// |label_parts| is empty if all of the following are true:
// (A) The form doesn't have a name field.
// (B) The user is focused on the street address.
// (C) The user either (i) has one profile or (ii) has multiple profiles and
// has the same phone number and email address in the profiles.
if (street_address_is_focused && label_parts.empty()) {
AddLabelPartIfNotEmpty(GetLabelPhone(profile, app_locale()), &label_parts);
}
return ConstructLabelLine(label_parts); return ConstructLabelLine(label_parts);
} }
......
...@@ -505,5 +505,53 @@ TEST(AddressContactFormLabelFormatterTest, ...@@ -505,5 +505,53 @@ TEST(AddressContactFormLabelFormatterTest,
{base::ASCIIToUTF16("Sarah Revere"), base::ASCIIToUTF16("02113")}))); {base::ASCIIToUTF16("Sarah Revere"), base::ASCIIToUTF16("02113")})));
} }
TEST(AddressContactFormLabelFormatterTest,
GetLabelsForFormWithoutName_FocusedStreetAddress) {
AutofillProfile profile1 =
AutofillProfile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile1, "Sarah", "", "Revere", "sarah.revere@aol.com",
"", "19 North Sq", "", "Boston", "MA", "02113", "US",
"16175232338");
std::vector<AutofillProfile*> profiles{&profile1};
std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
profiles, "en-US", ADDRESS_BILLING_LINE1,
{ADDRESS_BILLING_ZIP, EMAIL_ADDRESS, PHONE_BILLING_WHOLE_NUMBER});
// Checks that the name is not in the label and that the phone number is for
// a unique profile.
EXPECT_THAT(formatter->GetLabels(),
ElementsAre(base::ASCIIToUTF16("(617) 523-2338")));
profiles = {&profile1, &profile1};
formatter =
LabelFormatter::Create(profiles, "en-US", ADDRESS_BILLING_LINE1,
{ADDRESS_BILLING_LINE1, ADDRESS_BILLING_ZIP,
EMAIL_ADDRESS, PHONE_BILLING_WHOLE_NUMBER});
// Checks that the name is not in the label and that the phone number is for
// multiple profiles with the same phone number and email address.
EXPECT_THAT(formatter->GetLabels(),
ElementsAre(base::ASCIIToUTF16("(617) 523-2338"),
base::ASCIIToUTF16("(617) 523-2338")));
AutofillProfile profile2 =
AutofillProfile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile2, "Sarah", "", "Revere", "sarah@gmail.com", "",
"19 North Sq", "", "Boston", "MA", "02113", "US",
"16175232338");
profiles = {&profile1, &profile2};
formatter =
LabelFormatter::Create(profiles, "en-US", ADDRESS_BILLING_LINE1,
{ADDRESS_BILLING_LINE1, ADDRESS_BILLING_ZIP,
EMAIL_ADDRESS, PHONE_BILLING_WHOLE_NUMBER});
// Checks that the name is not in the label and that the email address is
// shown because the profiles' email addresses are different.
EXPECT_THAT(formatter->GetLabels(),
ElementsAre(base::ASCIIToUTF16("sarah.revere@aol.com"),
base::ASCIIToUTF16("sarah@gmail.com")));
}
} // namespace } // namespace
} // namespace autofill } // namespace autofill
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "components/autofill/core/browser/ui/address_email_form_label_formatter.h" #include "components/autofill/core/browser/ui/address_email_form_label_formatter.h"
#include "components/autofill/core/browser/autofill_data_util.h"
#include "components/autofill/core/browser/ui/label_formatter_utils.h" #include "components/autofill/core/browser/ui/label_formatter_utils.h"
namespace autofill { namespace autofill {
...@@ -45,7 +46,7 @@ base::string16 AddressEmailFormLabelFormatter:: ...@@ -45,7 +46,7 @@ base::string16 AddressEmailFormLabelFormatter::
FieldTypeGroup focused_group) const { FieldTypeGroup focused_group) const {
std::vector<base::string16> label_parts; std::vector<base::string16> label_parts;
if (focused_group != NAME) { if (focused_group != NAME && data_util::ContainsName(groups())) {
AddLabelPartIfNotEmpty(GetLabelFullName(profile, app_locale()), AddLabelPartIfNotEmpty(GetLabelFullName(profile, app_locale()),
&label_parts); &label_parts);
} }
......
...@@ -327,5 +327,22 @@ TEST(AddressEmailFormLabelFormatterTest, ...@@ -327,5 +327,22 @@ TEST(AddressEmailFormLabelFormatterTest,
base::ASCIIToUTF16("Brookline, MA")}))); base::ASCIIToUTF16("Brookline, MA")})));
} }
TEST(AddressEmailFormLabelFormatterTest, GetLabelsForFormWithoutName) {
AutofillProfile profile =
AutofillProfile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile, "Sarah", "", "Revere", "sarah.revere@aol.com",
"", "19 North Sq", "", "Boston", "MA", "02113", "US",
"16175232338");
const std::vector<AutofillProfile*> profiles{&profile};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
profiles, "en-US", ADDRESS_BILLING_LINE1,
{ADDRESS_BILLING_LINE1, ADDRESS_BILLING_ZIP, EMAIL_ADDRESS});
// Checks that the name does not appear in the labels.
EXPECT_THAT(formatter->GetLabels(),
ElementsAre(base::ASCIIToUTF16("sarah.revere@aol.com")));
}
} // namespace } // namespace
} // namespace autofill } // namespace autofill
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "components/autofill/core/browser/ui/address_form_label_formatter.h" #include "components/autofill/core/browser/ui/address_form_label_formatter.h"
#include "components/autofill/core/browser/autofill_data_util.h"
#include "components/autofill/core/browser/ui/label_formatter_utils.h" #include "components/autofill/core/browser/ui/label_formatter_utils.h"
namespace autofill { namespace autofill {
...@@ -31,8 +32,12 @@ base::string16 AddressFormLabelFormatter::GetLabelForProfile( ...@@ -31,8 +32,12 @@ base::string16 AddressFormLabelFormatter::GetLabelForProfile(
app_locale()); app_locale());
} else { } else {
std::vector<base::string16> label_parts; std::vector<base::string16> label_parts;
AddLabelPartIfNotEmpty(GetLabelFullName(profile, app_locale()),
&label_parts); if (data_util::ContainsName(groups())) {
AddLabelPartIfNotEmpty(GetLabelFullName(profile, app_locale()),
&label_parts);
}
AddLabelPartIfNotEmpty(GetLabelForFocusedAddress( AddLabelPartIfNotEmpty(GetLabelForFocusedAddress(
focused_field_type(), form_has_street_address_, focused_field_type(), form_has_street_address_,
profile, app_locale(), field_types_for_labels()), profile, app_locale(), field_types_for_labels()),
......
...@@ -187,5 +187,23 @@ TEST(AddressFormLabelFormatterTest, GetLabelsForBRProfilesAndFocusedName) { ...@@ -187,5 +187,23 @@ TEST(AddressFormLabelFormatterTest, GetLabelsForBRProfilesAndFocusedName) {
"Paulo-SP, 04094-050"))); "Paulo-SP, 04094-050")));
} }
TEST(AddressFormLabelFormatterTest, GetLabelsForFormWithoutName) {
AutofillProfile profile =
AutofillProfile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile, "Sarah", "", "Revere", "sarah.revere@aol.com",
"", "19 North Sq", "", "Boston", "MA", "02113", "US",
"16175232338");
const std::vector<AutofillProfile*> profiles{&profile};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
profiles, "en-US", ADDRESS_HOME_LINE1,
{ADDRESS_HOME_CITY, ADDRESS_HOME_STATE, ADDRESS_HOME_DEPENDENT_LOCALITY,
ADDRESS_HOME_ZIP});
// Checks that the name does not appear in the labels.
EXPECT_THAT(formatter->GetLabels(),
ElementsAre(base::ASCIIToUTF16("Boston, MA 02113")));
}
} // namespace } // namespace
} // namespace autofill } // namespace autofill
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "components/autofill/core/browser/ui/address_phone_form_label_formatter.h" #include "components/autofill/core/browser/ui/address_phone_form_label_formatter.h"
#include "components/autofill/core/browser/autofill_data_util.h"
#include "components/autofill/core/browser/ui/label_formatter_utils.h" #include "components/autofill/core/browser/ui/label_formatter_utils.h"
namespace autofill { namespace autofill {
...@@ -44,7 +45,7 @@ base::string16 AddressPhoneFormLabelFormatter:: ...@@ -44,7 +45,7 @@ base::string16 AddressPhoneFormLabelFormatter::
const AutofillProfile& profile, const AutofillProfile& profile,
FieldTypeGroup focused_group) const { FieldTypeGroup focused_group) const {
std::vector<base::string16> label_parts; std::vector<base::string16> label_parts;
if (focused_group != NAME) { if (focused_group != NAME && data_util::ContainsName(groups())) {
AddLabelPartIfNotEmpty(GetLabelFullName(profile, app_locale()), AddLabelPartIfNotEmpty(GetLabelFullName(profile, app_locale()),
&label_parts); &label_parts);
} }
......
...@@ -321,5 +321,22 @@ TEST(AddressPhoneFormLabelFormatterTest, ...@@ -321,5 +321,22 @@ TEST(AddressPhoneFormLabelFormatterTest,
base::ASCIIToUTF16("02445")}))); base::ASCIIToUTF16("02445")})));
} }
TEST(AddressPhoneFormLabelFormatterTest, GetLabelsForFormWithoutName) {
AutofillProfile profile =
AutofillProfile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile, "Sarah", "", "Revere", "sarah.revere@aol.com",
"", "19 North Sq", "", "Boston", "MA", "02113", "US",
"16175232338");
const std::vector<AutofillProfile*> profiles{&profile};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
profiles, "en-US", ADDRESS_BILLING_LINE1,
{ADDRESS_BILLING_LINE1, ADDRESS_BILLING_ZIP, PHONE_HOME_WHOLE_NUMBER});
// Checks that the name does not appear in the labels.
EXPECT_THAT(formatter->GetLabels(),
ElementsAre(base::ASCIIToUTF16("(617) 523-2338")));
}
} // namespace } // namespace
} // namespace autofill } // namespace autofill
\ No newline at end of file
...@@ -30,7 +30,7 @@ base::string16 ContactFormLabelFormatter::GetLabelForProfile( ...@@ -30,7 +30,7 @@ base::string16 ContactFormLabelFormatter::GetLabelForProfile(
const AutofillProfile& profile, const AutofillProfile& profile,
FieldTypeGroup focused_group) const { FieldTypeGroup focused_group) const {
std::vector<base::string16> label_parts; std::vector<base::string16> label_parts;
if (focused_group != NAME) { if (focused_group != NAME && data_util::ContainsName(groups())) {
AddLabelPartIfNotEmpty(GetLabelFullName(profile, app_locale()), AddLabelPartIfNotEmpty(GetLabelFullName(profile, app_locale()),
&label_parts); &label_parts);
} }
......
...@@ -302,5 +302,22 @@ TEST(ContactFormLabelFormatterTest, GetLabelsForNameAndEmailWithFocusedEmail) { ...@@ -302,5 +302,22 @@ TEST(ContactFormLabelFormatterTest, GetLabelsForNameAndEmailWithFocusedEmail) {
ElementsAre(base::ASCIIToUTF16("John F Kennedy"))); ElementsAre(base::ASCIIToUTF16("John F Kennedy")));
} }
TEST(ContactFormLabelFormatterTest, GetLabelsForFormWithoutName) {
AutofillProfile profile =
AutofillProfile(base::GenerateGUID(), test::kEmptyOrigin);
test::SetProfileInfo(&profile, "Sarah", "", "Revere", "sarah.revere@aol.com",
"", "19 North Sq", "", "Boston", "MA", "02113", "US",
"16175232338");
const std::vector<AutofillProfile*> profiles{&profile};
const std::unique_ptr<LabelFormatter> formatter = LabelFormatter::Create(
profiles, "en-US", PHONE_HOME_COUNTRY_CODE,
{EMAIL_ADDRESS, PHONE_HOME_COUNTRY_CODE, PHONE_HOME_CITY_AND_NUMBER});
// Checks that the name does not appear in the labels.
EXPECT_THAT(formatter->GetLabels(),
ElementsAre(base::ASCIIToUTF16("sarah.revere@aol.com")));
}
} // namespace } // namespace
} // namespace autofill } // namespace autofill
\ No newline at end of file
...@@ -30,19 +30,17 @@ using data_util::bit_field_type_groups::kEmail; ...@@ -30,19 +30,17 @@ 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;
#if defined(OS_ANDROID) || defined(OS_IOS)
namespace { namespace {
// Returns true if a form has address fields or has least two supported // Returns true if a form has address fields or has least two supported
// non-address fields. // non-address fields.
bool IsSupportedFormTypeOnMobile(uint32_t groups) { bool IsSupportedFormType(uint32_t groups) {
return ContainsAddress(groups) || return ContainsAddress(groups) ||
ContainsName(groups) + ContainsEmail(groups) + ContainsPhone(groups) >= ContainsName(groups) + ContainsEmail(groups) + ContainsPhone(groups) >=
2; 2;
} }
} // namespace } // namespace
#endif // defined(OS_ANDROID) || defined(OS_IOS)
LabelFormatter::LabelFormatter(const std::vector<AutofillProfile*>& profiles, LabelFormatter::LabelFormatter(const std::vector<AutofillProfile*>& profiles,
const std::string& app_locale, const std::string& app_locale,
...@@ -102,27 +100,32 @@ std::unique_ptr<LabelFormatter> LabelFormatter::Create( ...@@ -102,27 +100,32 @@ std::unique_ptr<LabelFormatter> LabelFormatter::Create(
ServerFieldType focused_field_type, ServerFieldType focused_field_type,
const std::vector<ServerFieldType>& field_types) { const std::vector<ServerFieldType>& field_types) {
const uint32_t groups = data_util::DetermineGroups(field_types); const uint32_t groups = data_util::DetermineGroups(field_types);
if (!IsSupportedFormType(groups)) {
return nullptr;
}
#if defined(OS_ANDROID) || defined(OS_IOS) #if defined(OS_ANDROID) || defined(OS_IOS)
return IsSupportedFormTypeOnMobile(groups) return std::make_unique<MobileLabelFormatter>(
? std::make_unique<MobileLabelFormatter>(profiles, app_locale, profiles, app_locale, focused_field_type, groups, field_types);
focused_field_type,
groups, field_types)
: nullptr;
#else #else
switch (groups) { switch (groups) {
case kAddress | kEmail | kPhone:
case kName | kAddress | kEmail | kPhone: case kName | kAddress | kEmail | kPhone:
return std::make_unique<AddressContactFormLabelFormatter>( return std::make_unique<AddressContactFormLabelFormatter>(
profiles, app_locale, focused_field_type, groups, field_types); profiles, app_locale, focused_field_type, groups, field_types);
case kAddress | kPhone:
case kName | kAddress | kPhone: case kName | kAddress | kPhone:
return std::make_unique<AddressPhoneFormLabelFormatter>( return std::make_unique<AddressPhoneFormLabelFormatter>(
profiles, app_locale, focused_field_type, groups, field_types); profiles, app_locale, focused_field_type, groups, field_types);
case kAddress | kEmail:
case kName | kAddress | kEmail: case kName | kAddress | kEmail:
return std::make_unique<AddressEmailFormLabelFormatter>( return std::make_unique<AddressEmailFormLabelFormatter>(
profiles, app_locale, focused_field_type, groups, field_types); profiles, app_locale, focused_field_type, groups, field_types);
case kAddress:
case kName | kAddress: case kName | kAddress:
return std::make_unique<AddressFormLabelFormatter>( return std::make_unique<AddressFormLabelFormatter>(
profiles, app_locale, focused_field_type, groups, field_types); profiles, app_locale, focused_field_type, groups, field_types);
case kEmail | kPhone:
case kName | kEmail | kPhone: case kName | kEmail | kPhone:
case kName | kEmail: case kName | kEmail:
case kName | kPhone: case kName | kPhone:
......
...@@ -13,14 +13,6 @@ ...@@ -13,14 +13,6 @@
namespace autofill { namespace autofill {
namespace { namespace {
TEST(LabelFormatterTest, CreateWithoutNameFieldType) {
const std::vector<AutofillProfile*> profiles{};
EXPECT_EQ(LabelFormatter::Create(
profiles, "en-US", EMAIL_ADDRESS,
{EMAIL_ADDRESS, PHONE_HOME_WHOLE_NUMBER, ADDRESS_HOME_LINE1}),
nullptr);
}
TEST(LabelFormatterTest, CreateWithMissingFieldTypes) { TEST(LabelFormatterTest, CreateWithMissingFieldTypes) {
const std::vector<AutofillProfile*> profiles{}; const std::vector<AutofillProfile*> profiles{};
EXPECT_EQ(LabelFormatter::Create(profiles, "en-US", NAME_FIRST, EXPECT_EQ(LabelFormatter::Create(profiles, "en-US", NAME_FIRST,
......
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