Commit 3c26fc7b authored by ziran.sun's avatar ziran.sun Committed by Commit bot

Adjust displayed phone number for prefix/suffix case.

R=isherman@chromium.org
BUG=84200

Review URL: https://codereview.chromium.org/442403002

Cr-Commit-Position: refs/heads/master@{#296162}
parent 99a2922a
......@@ -280,23 +280,8 @@ bool FillCreditCardTypeSelectControl(const base::string16& value,
void FillPhoneNumberField(const AutofillField& field,
const base::string16& number,
FormFieldData* field_data) {
// Check to see if the size field matches the "prefix" or "suffix" sizes and
// fill accordingly.
base::string16 value = number;
if (number.length() ==
PhoneNumber::kPrefixLength + PhoneNumber::kSuffixLength) {
if (field.phone_part() == AutofillField::PHONE_PREFIX ||
field_data->max_length == PhoneNumber::kPrefixLength) {
value = number.substr(PhoneNumber::kPrefixOffset,
PhoneNumber::kPrefixLength);
} else if (field.phone_part() == AutofillField::PHONE_SUFFIX ||
field_data->max_length == PhoneNumber::kSuffixLength) {
value = number.substr(PhoneNumber::kSuffixOffset,
PhoneNumber::kSuffixLength);
}
}
field_data->value = value;
field_data->value =
AutofillField::GetPhoneNumberValue(field, number, *field_data);
}
// Set |field_data|'s value to |number|, or possibly an appropriate substring
......@@ -515,4 +500,30 @@ bool AutofillField::FillFormField(const AutofillField& field,
return true;
}
base::string16 AutofillField::GetPhoneNumberValue(
const AutofillField& field,
const base::string16& number,
const FormFieldData& field_data) {
// Check to see if the size field matches the "prefix" or "suffix" size.
// If so, return the appropriate substring.
if (number.length() !=
PhoneNumber::kPrefixLength + PhoneNumber::kSuffixLength) {
return number;
}
if (field.phone_part() == AutofillField::PHONE_PREFIX ||
field_data.max_length == PhoneNumber::kPrefixLength) {
return
number.substr(PhoneNumber::kPrefixOffset, PhoneNumber::kPrefixLength);
}
if (field.phone_part() == AutofillField::PHONE_SUFFIX ||
field_data.max_length == PhoneNumber::kSuffixLength) {
return
number.substr(PhoneNumber::kSuffixOffset, PhoneNumber::kSuffixLength);
}
return number;
}
} // namespace autofill
......@@ -82,6 +82,13 @@ class AutofillField : public FormFieldData {
const std::string& app_locale,
FormFieldData* field_data);
// Returns the phone number value for the given |field|. The returned value
// might be |number|, or could possibly be a prefix or suffix of |number|
// if that's appropriate for the field.
static base::string16 GetPhoneNumberValue(const AutofillField& field,
const base::string16& number,
const FormFieldData& field_data);
private:
// The unique name of this field, generated by Autofill.
base::string16 unique_name_;
......
......@@ -475,8 +475,13 @@ void AutofillManager::OnQueryFormFieldAutofill(int query_id,
GetCreditCardSuggestions(
field, type, &values, &labels, &icons, &unique_ids);
} else {
GetProfileSuggestions(
*form_structure, field, type, &values, &labels, &icons, &unique_ids);
GetProfileSuggestions(*form_structure,
field,
*autofill_field,
&values,
&labels,
&icons,
&unique_ids);
}
DCHECK_EQ(values.size(), labels.size());
......@@ -1080,7 +1085,7 @@ bool AutofillManager::UpdateCachedForm(const FormData& live_form,
void AutofillManager::GetProfileSuggestions(
const FormStructure& form,
const FormFieldData& field,
const AutofillType& type,
const AutofillField& autofill_field,
std::vector<base::string16>* values,
std::vector<base::string16>* labels,
std::vector<base::string16>* icons,
......@@ -1092,10 +1097,18 @@ void AutofillManager::GetProfileSuggestions(
std::vector<GUIDPair> guid_pairs;
personal_data_->GetProfileSuggestions(
type, field.value, field.is_autofilled, field_types,
autofill_field.Type(), field.value, field.is_autofilled, field_types,
base::Callback<bool(const AutofillProfile&)>(),
values, labels, icons, &guid_pairs);
// Adjust phone number to display in prefix/suffix case.
if (autofill_field.Type().GetStorableType() == PHONE_HOME_NUMBER) {
for (size_t i = 0; i < values->size(); ++i) {
(*values)[i] = AutofillField::GetPhoneNumberValue(
autofill_field, (*values)[i], field);
}
}
for (size_t i = 0; i < guid_pairs.size(); ++i) {
unique_ids->push_back(PackGUIDs(GUIDPair(std::string(), 0),
guid_pairs[i]));
......
......@@ -265,7 +265,7 @@ class AutofillManager : public AutofillDownloadManager::Observer {
// is filled with the Profile label.
void GetProfileSuggestions(const FormStructure& form,
const FormFieldData& field,
const AutofillType& type,
const AutofillField& autofill_field,
std::vector<base::string16>* values,
std::vector<base::string16>* labels,
std::vector<base::string16>* icons,
......
......@@ -1473,6 +1473,81 @@ TEST_F(AutofillManagerTest, GetProfileSuggestionsFancyPhone) {
expected_labels, expected_icons, expected_unique_ids);
}
TEST_F(AutofillManagerTest, GetProfileSuggestionsForPhonePrefixOrSuffix) {
// Set up our form data.
FormData form;
form.name = ASCIIToUTF16("MyForm");
form.origin = GURL("http://myform.com/form.html");
form.action = GURL("http://myform.com/submit.html");
form.user_submitted = true;
struct {
const char* const label;
const char* const name;
size_t max_length;
const char* const autocomplete_attribute;
} test_fields[] = {{"country code", "country_code", 1, "tel-country-code"},
{"area code", "area_code", 3, "tel-area-code"},
{"phone", "phone_prefix", 3, "tel-local-prefix"},
{"-", "phone_suffix", 4, "tel-local-suffix"},
{"Phone Extension", "ext", 5, "tel-extension"}};
FormFieldData field;
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_fields); ++i) {
test::CreateTestFormField(
test_fields[i].label, test_fields[i].name, "", "text", &field);
field.max_length = test_fields[i].max_length;
field.autocomplete_attribute = std::string();
form.fields.push_back(field);
}
std::vector<FormData> forms(1, form);
FormsSeen(forms);
AutofillProfile* profile = new AutofillProfile;
profile->set_guid("00000000-0000-0000-0000-000000000104");
std::vector<base::string16> multi_values(2);
multi_values[0] = ASCIIToUTF16("1800FLOWERS");
multi_values[1] = ASCIIToUTF16("14158889999");
profile->SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, multi_values);
personal_data_.ClearAutofillProfiles();
autofill_manager_->AddProfile(profile);
const FormFieldData& phone_prefix = form.fields[2];
GetAutofillSuggestions(form, phone_prefix);
AutocompleteSuggestionsReturned(std::vector<base::string16>());
// Test that we sent the right prefix values to the external delegate.
base::string16 expected_prefix_values[] = {ASCIIToUTF16("356"),
ASCIIToUTF16("888")};
base::string16 expected_prefix_labels[] = {ASCIIToUTF16("1"),
ASCIIToUTF16("1")};
base::string16 expected_prefix_icons[] = {base::string16(), base::string16()};
int expected_unique_ids[] = {1, 2};
external_delegate_->CheckSuggestions(kDefaultPageID,
arraysize(expected_prefix_values),
expected_prefix_values,
expected_prefix_labels,
expected_prefix_icons,
expected_unique_ids);
const FormFieldData& phone_suffix = form.fields[3];
GetAutofillSuggestions(form, phone_suffix);
AutocompleteSuggestionsReturned(std::vector<base::string16>());
// Test that we sent the right suffix values to the external delegate.
base::string16 expected_suffix_values[] = {ASCIIToUTF16("9377"),
ASCIIToUTF16("9999")};
base::string16 expected_suffix_labels[] = {ASCIIToUTF16("1"),
ASCIIToUTF16("1")};
base::string16 expected_suffix_icons[] = {base::string16(), base::string16()};
external_delegate_->CheckSuggestions(kDefaultPageID,
arraysize(expected_suffix_values),
expected_suffix_values,
expected_suffix_labels,
expected_suffix_icons,
expected_unique_ids);
}
// Test that we correctly fill an address form.
TEST_F(AutofillManagerTest, FillAddressForm) {
// Set up our form data.
......
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