Commit 71f8a9fc authored by sandromaggi's avatar sandromaggi Committed by Commit Bot

[Autofill Assistant] Split profiles into usage cases.

Previously the source for contacts and shipping addresses was the same
sorted list of profiles. There may be cases where we want to sort them
differently, which is enabled by splitting the list in two.

This CL also moves the transformation logic from CreditCard / Profile
to their destination objects (AutofillContact, AutofillAddress,
AutofillPaymentInstrument) from the DataBinder to native/model
directly.

Bug: b/144005336
Change-Id: I644d4b52f962582a87b8aabce5e55066e3356481
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1992082
Commit-Queue: Sandro Maggi <sandromaggi@google.com>
Auto-Submit: Sandro Maggi <sandromaggi@google.com>
Reviewed-by: default avatarClemens Arbesser <arbesser@google.com>
Cr-Commit-Position: refs/heads/master@{#730165}
parent 0e9ae106
......@@ -14,12 +14,9 @@ import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import org.chromium.chrome.autofill_assistant.R;
import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile;
import org.chromium.chrome.browser.payments.AutofillContact;
import org.chromium.chrome.browser.payments.ContactEditor;
import org.chromium.chrome.browser.payments.ui.ContactDetailsSection;
import java.util.ArrayList;
import java.util.List;
/**
......@@ -140,39 +137,21 @@ public class AssistantContactDetailsSection
/**
* The Chrome profiles have changed externally. This will rebuild the UI with the new/changed
* set of profiles, while keeping the selected item if possible.
* set of contacts derived from the profiles, while keeping the selected item if possible.
*/
void onProfilesChanged(List<AutofillProfile> profiles, boolean requestPayerEmail,
boolean requestPayerName, boolean requestPayerPhone) {
void onContactsChanged(List<AutofillContact> contacts) {
if (mIgnoreProfileChangeNotifications) {
return;
}
if (!requestPayerEmail && !requestPayerName && !requestPayerPhone) {
return;
}
// Note: we create a temporary editor (necessary for converting profiles to contacts)
// instead of using mEditor, which may be null.
ContactEditor tempEditor =
new ContactEditor(requestPayerName, requestPayerPhone, requestPayerEmail, false);
// Convert profiles into a list of |AutofillContact|.
int selectedContactIndex = -1;
ContactDetailsSection sectionInformation =
new ContactDetailsSection(mContext, profiles, tempEditor, null);
List<AutofillContact> contacts = new ArrayList<>();
for (int i = 0; i < sectionInformation.getSize(); i++) {
AutofillContact contact = (AutofillContact) sectionInformation.getItem(i);
if (contact == null) {
continue;
}
contacts.add(contact);
if (mSelectedOption != null && areEqual(contact, mSelectedOption)) {
selectedContactIndex = i;
if (mSelectedOption != null) {
for (int i = 0; i < contacts.size(); i++) {
if (areEqual(contacts.get(i), mSelectedOption)) {
selectedContactIndex = i;
break;
}
}
}
// Replace current set of items, keep selection if possible.
setItems(contacts, selectedContactIndex);
}
......
......@@ -57,7 +57,7 @@ public class AssistantPaymentMethodSection
String guid = method.getCard().getBillingAddressId();
PersonalDataManager.AutofillProfile profile = personalDataManager.getProfile(guid);
if (profile != null) {
addAutocompleteInformationToEditor(profile);
addAutocompleteInformationToEditor(new AutofillAddress(mContext, profile));
}
}
}
......@@ -167,10 +167,10 @@ public class AssistantPaymentMethodSection
return TextUtils.equals(profileA.getGUID(), profileB.getGUID());
}
void onProfilesChanged(List<PersonalDataManager.AutofillProfile> profiles) {
void onAddressesChanged(List<AutofillAddress> addresses) {
// TODO(crbug.com/806868): replace suggested billing addresses (remove if necessary).
for (PersonalDataManager.AutofillProfile profile : profiles) {
addAutocompleteInformationToEditor(profile);
for (AutofillAddress address : addresses) {
addAutocompleteInformationToEditor(address);
}
}
......@@ -209,12 +209,12 @@ public class AssistantPaymentMethodSection
mCreditCardExpiredText = text;
}
private void addAutocompleteInformationToEditor(PersonalDataManager.AutofillProfile profile) {
private void addAutocompleteInformationToEditor(AutofillAddress address) {
// The check for non-null label is necessary to prevent crash in editor when opening.
if (mEditor == null || profile.getLabel() == null) {
if (mEditor == null || address.getProfile().getLabel() == null) {
return;
}
mEditor.updateBillingAddressIfComplete(new AutofillAddress(mContext, profile));
mEditor.updateBillingAddressIfComplete(address);
}
private void setErrorMessage(TextView errorMessageView, AutofillPaymentInstrument method) {
......
......@@ -18,7 +18,6 @@ import org.chromium.chrome.browser.autofill.PersonalDataManager;
import org.chromium.chrome.browser.payments.AddressEditor;
import org.chromium.chrome.browser.payments.AutofillAddress;
import java.util.ArrayList;
import java.util.List;
/**
......@@ -125,21 +124,23 @@ public class AssistantShippingAddressSection
return TextUtils.equals(optionA.getProfile().getGUID(), optionB.getProfile().getGUID());
}
void onProfilesChanged(List<PersonalDataManager.AutofillProfile> profiles) {
/**
* The Chrome profiles have changed externally. This will rebuild the UI with the new/changed
* set of addresses derived from the profiles, while keeping the selected item if possible.
*/
void onAddressesChanged(List<AutofillAddress> addresses) {
if (mIgnoreProfileChangeNotifications) {
return;
}
int selectedAddressIndex = -1;
List<AutofillAddress> addresses = new ArrayList<>();
for (int i = 0; i < profiles.size(); i++) {
AutofillAddress autofillAddress = new AutofillAddress(mContext, profiles.get(i));
if (mSelectedOption != null && areEqual(mSelectedOption, autofillAddress)) {
selectedAddressIndex = i;
if (mSelectedOption != null) {
for (int i = 0; i < addresses.size(); i++) {
if (areEqual(addresses.get(i), mSelectedOption)) {
selectedAddressIndex = i;
break;
}
}
addresses.add(autofillAddress);
}
// Replace current set of items, keep selection if possible.
setItems(addresses, selectedAddressIndex);
}
......
......@@ -1031,6 +1031,9 @@ void UiControllerAndroid::OnUserDataChanged(
return;
}
auto jcontext =
Java_AutofillAssistantUiController_getContext(env, java_object_);
if (field_change == UserData::FieldChange::ALL ||
field_change == UserData::FieldChange::TERMS_AND_CONDITIONS) {
Java_AssistantCollectUserDataModel_setTermsStatus(
......@@ -1039,65 +1042,119 @@ void UiControllerAndroid::OnUserDataChanged(
if (field_change == UserData::FieldChange::ALL ||
field_change == UserData::FieldChange::AVAILABLE_PROFILES) {
auto sorted_profile_indices = SortByCompleteness(*collect_user_data_options,
state->available_profiles);
auto jlist =
Java_AssistantCollectUserDataModel_createAutofillProfileList(env);
for (int index : sorted_profile_indices) {
Java_AssistantCollectUserDataModel_addAutofillProfile(
env, jlist,
// Contact profiles.
auto jcontactlist =
Java_AssistantCollectUserDataModel_createAutofillContactList(env);
auto contact_indices = SortContactsByCompleteness(
*collect_user_data_options, state->available_profiles);
for (int index : contact_indices) {
auto jcontact = Java_AssistantCollectUserDataModel_createAutofillContact(
env, jcontext,
autofill::PersonalDataManagerAndroid::CreateJavaProfileFromNative(
env, *state->available_profiles[index]));
env, *state->available_profiles[index]),
collect_user_data_options->request_payer_name,
collect_user_data_options->request_payer_phone,
collect_user_data_options->request_payer_email);
if (jcontact) {
Java_AssistantCollectUserDataModel_addAutofillContact(env, jcontactlist,
jcontact);
}
}
Java_AssistantCollectUserDataModel_setAutofillProfiles(env, jmodel, jlist);
Java_AssistantCollectUserDataModel_setAvailableContacts(env, jmodel,
jcontactlist);
// Ignore changes to FieldChange::CONTACT_PROFILE, this is already coming
// from the view.
autofill::AutofillProfile* contact_profile = state->contact_profile.get();
Java_AssistantCollectUserDataModel_setContactDetails(
Java_AssistantCollectUserDataModel_setSelectedContactDetails(
env, jmodel,
contact_profile == nullptr
? nullptr
: autofill::PersonalDataManagerAndroid::CreateJavaProfileFromNative(
env, *contact_profile));
: Java_AssistantCollectUserDataModel_createAutofillContact(
env, jcontext,
autofill::PersonalDataManagerAndroid::
CreateJavaProfileFromNative(env, *contact_profile),
collect_user_data_options->request_payer_name,
collect_user_data_options->request_payer_phone,
collect_user_data_options->request_payer_email));
// Billing addresses profiles.
auto jbillinglist =
Java_AssistantCollectUserDataModel_createAutofillAddressList(env);
for (const auto& profile : state->available_profiles) {
auto jaddress = Java_AssistantCollectUserDataModel_createAutofillAddress(
env, jcontext,
autofill::PersonalDataManagerAndroid::CreateJavaProfileFromNative(
env, *profile));
if (jaddress) {
Java_AssistantCollectUserDataModel_addAutofillAddress(env, jbillinglist,
jaddress);
}
}
Java_AssistantCollectUserDataModel_setAvailableBillingAddresses(
env, jmodel, jbillinglist);
// Address profiles.
auto jshippinglist =
Java_AssistantCollectUserDataModel_createAutofillAddressList(env);
auto address_indices = SortAddressesByCompleteness(
*collect_user_data_options, state->available_profiles);
for (int index : address_indices) {
auto jaddress = Java_AssistantCollectUserDataModel_createAutofillAddress(
env, jcontext,
autofill::PersonalDataManagerAndroid::CreateJavaProfileFromNative(
env, *state->available_profiles[index]));
if (jaddress) {
Java_AssistantCollectUserDataModel_addAutofillAddress(
env, jshippinglist, jaddress);
}
}
Java_AssistantCollectUserDataModel_setAvailableShippingAddresses(
env, jmodel, jshippinglist);
// Ignore changes to FieldChange::SHIPPING_ADDRESS, this is already coming
// from the view.
autofill::AutofillProfile* shipping_address = state->shipping_address.get();
Java_AssistantCollectUserDataModel_setShippingAddress(
Java_AssistantCollectUserDataModel_setSelectedShippingAddress(
env, jmodel,
shipping_address == nullptr
? nullptr
: autofill::PersonalDataManagerAndroid::CreateJavaProfileFromNative(
env, *shipping_address));
: Java_AssistantCollectUserDataModel_createAutofillAddress(
env, jcontext,
autofill::PersonalDataManagerAndroid::
CreateJavaProfileFromNative(env, *shipping_address)));
}
if (field_change == UserData::FieldChange::ALL ||
field_change == UserData::FieldChange::AVAILABLE_PAYMENT_INSTRUMENTS) {
auto sorted_payment_instrument_indices = SortByCompleteness(
*collect_user_data_options, state->available_payment_instruments);
auto jlist =
Java_AssistantCollectUserDataModel_createAutofillPaymentMethodList(env);
Java_AssistantCollectUserDataModel_createAutofillPaymentInstrumentList(
env);
auto sorted_payment_instrument_indices =
SortPaymentInstrumentsByCompleteness(
*collect_user_data_options, state->available_payment_instruments);
for (int index : sorted_payment_instrument_indices) {
const auto& instrument = state->available_payment_instruments[index];
Java_AssistantCollectUserDataModel_addAutofillPaymentMethod(
env, jlist,
autofill::PersonalDataManagerAndroid::CreateJavaCreditCardFromNative(
env, *(instrument->card)),
Java_AssistantCollectUserDataModel_addAutofillPaymentInstrument(
env, jmodel, jlist,
instrument->card == nullptr
? nullptr
: autofill::PersonalDataManagerAndroid::
CreateJavaCreditCardFromNative(env, *(instrument->card)),
instrument->billing_address == nullptr
? nullptr
: autofill::PersonalDataManagerAndroid::
CreateJavaProfileFromNative(
env, *(instrument->billing_address)));
}
Java_AssistantCollectUserDataModel_setAutofillPaymentMethods(env, jmodel,
jlist);
Java_AssistantCollectUserDataModel_setAvailablePaymentInstruments(
env, jmodel, jlist);
// Ignore changes to FieldChange::CARD, this is already coming from the
// view.
autofill::CreditCard* card = state->card.get();
autofill::AutofillProfile* billing_address = state->billing_address.get();
Java_AssistantCollectUserDataModel_setPaymentMethod(
Java_AssistantCollectUserDataModel_setSelectedPaymentInstrument(
env, jmodel,
card == nullptr ? nullptr
: autofill::PersonalDataManagerAndroid::
......
......@@ -844,8 +844,8 @@ void CollectUserDataAction::UpdatePersonalDataManagerProfiles(
(collect_user_data_options_->request_payer_name ||
collect_user_data_options_->request_payer_phone ||
collect_user_data_options_->request_payer_email)) {
int default_selection = GetDefaultProfile(*collect_user_data_options_,
user_data->available_profiles);
int default_selection = GetDefaultContactProfile(
*collect_user_data_options_, user_data->available_profiles);
if (default_selection != -1) {
user_data->contact_profile = std::make_unique<autofill::AutofillProfile>(
*(user_data->available_profiles[default_selection]));
......@@ -857,8 +857,8 @@ void CollectUserDataAction::UpdatePersonalDataManagerProfiles(
}
if (user_data->shipping_address == nullptr &&
collect_user_data_options_->request_shipping) {
int default_selection = GetDefaultProfile(*collect_user_data_options_,
user_data->available_profiles);
int default_selection = GetDefaultAddressProfile(
*collect_user_data_options_, user_data->available_profiles);
if (default_selection != -1) {
user_data->shipping_address = std::make_unique<autofill::AutofillProfile>(
*(user_data->available_profiles[default_selection]));
......
......@@ -8,6 +8,9 @@
#include "base/i18n/case_conversion.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/autofill_data_util.h"
#include "components/autofill/core/browser/geo/address_i18n.h"
#include "third_party/libaddressinput/chromium/addressinput_util.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_data.h"
namespace autofill_assistant {
namespace {
......@@ -20,8 +23,8 @@ base::string16 GetProfileFullName(const autofill::AutofillProfile& profile) {
profile.GetRawInfo(autofill::NAME_LAST));
}
int CountCompleteFields(const CollectUserDataOptions& options,
const autofill::AutofillProfile& profile) {
int CountCompleteContactFields(const CollectUserDataOptions& options,
const autofill::AutofillProfile& profile) {
int completed_fields = 0;
if (options.request_payer_name && !GetProfileFullName(profile).empty()) {
++completed_fields;
......@@ -44,11 +47,11 @@ int CountCompleteFields(const CollectUserDataOptions& options,
// Helper function that compares instances of AutofillProfile by completeness
// in regards to the current options. Full profiles should be ordered before
// empty ones and fall back to compare the profile's name in case of equality.
bool CompletenessCompare(const CollectUserDataOptions& options,
const autofill::AutofillProfile& a,
const autofill::AutofillProfile& b) {
int complete_fields_a = CountCompleteFields(options, a);
int complete_fields_b = CountCompleteFields(options, b);
bool CompletenessCompareContacts(const CollectUserDataOptions& options,
const autofill::AutofillProfile& a,
const autofill::AutofillProfile& b) {
int complete_fields_a = CountCompleteContactFields(options, a);
int complete_fields_b = CountCompleteContactFields(options, b);
if (complete_fields_a == complete_fields_b) {
return base::i18n::ToLower(GetProfileFullName(a))
.compare(base::i18n::ToLower(GetProfileFullName(b))) < 0;
......@@ -56,8 +59,35 @@ bool CompletenessCompare(const CollectUserDataOptions& options,
return complete_fields_a > complete_fields_b;
}
int CountCompleteFields(const CollectUserDataOptions& options,
const PaymentInstrument& instrument) {
int GetAddressCompletenessRating(const CollectUserDataOptions& options,
const autofill::AutofillProfile& profile) {
auto address_data =
autofill::i18n::CreateAddressDataFromAutofillProfile(profile, "en-US");
std::multimap<i18n::addressinput::AddressField,
i18n::addressinput::AddressProblem>
problems;
autofill::addressinput::ValidateRequiredFields(
*address_data, /* filter= */ nullptr, &problems);
return -problems.size();
}
// Helper function that compares instances of AutofillProfile by completeness
// in regards to the current options. Full profiles should be ordered before
// empty ones and fall back to compare the profile's name in case of equality.
bool CompletenessCompareAddresses(const CollectUserDataOptions& options,
const autofill::AutofillProfile& a,
const autofill::AutofillProfile& b) {
int complete_fields_a = GetAddressCompletenessRating(options, a);
int complete_fields_b = GetAddressCompletenessRating(options, b);
if (complete_fields_a == complete_fields_b) {
return base::i18n::ToLower(GetProfileFullName(a))
.compare(base::i18n::ToLower(GetProfileFullName(b))) < 0;
}
return complete_fields_a > complete_fields_b;
}
int CountCompletePaymentInstrumentFields(const CollectUserDataOptions& options,
const PaymentInstrument& instrument) {
int complete_fields = 0;
if (!instrument.card->GetRawInfo(autofill::CREDIT_CARD_NAME_FULL).empty()) {
++complete_fields;
......@@ -88,11 +118,12 @@ int CountCompleteFields(const CollectUserDataOptions& options,
// in regards to the current options. Full payment instruments should be
// ordered before empty ones and fall back to compare the full name on the
// credit card in case of equality.
bool CompletenessCompare(const CollectUserDataOptions& options,
const PaymentInstrument& a,
const PaymentInstrument& b) {
int complete_fields_a = CountCompleteFields(options, a);
int complete_fields_b = CountCompleteFields(options, b);
bool CompletenessComparePaymentInstruments(
const CollectUserDataOptions& options,
const PaymentInstrument& a,
const PaymentInstrument& b) {
int complete_fields_a = CountCompletePaymentInstrumentFields(options, a);
int complete_fields_b = CountCompletePaymentInstrumentFields(options, b);
if (complete_fields_a == complete_fields_b) {
return base::i18n::ToLower(
a.card->GetRawInfo(autofill::CREDIT_CARD_NAME_FULL))
......@@ -104,26 +135,27 @@ bool CompletenessCompare(const CollectUserDataOptions& options,
} // namespace
std::vector<int> SortByCompleteness(
std::vector<int> SortContactsByCompleteness(
const CollectUserDataOptions& collect_user_data_options,
const std::vector<std::unique_ptr<autofill::AutofillProfile>>& profiles) {
std::vector<int> profile_indices(profiles.size());
std::iota(std::begin(profile_indices), std::end(profile_indices), 0);
std::sort(profile_indices.begin(), profile_indices.end(),
[&collect_user_data_options, &profiles](int i, int j) {
return CompletenessCompare(collect_user_data_options,
*profiles[i], *profiles[j]);
return CompletenessCompareContacts(collect_user_data_options,
*profiles[i], *profiles[j]);
});
return profile_indices;
}
int GetDefaultProfile(
int GetDefaultContactProfile(
const CollectUserDataOptions& collect_user_data_options,
const std::vector<std::unique_ptr<autofill::AutofillProfile>>& profiles) {
if (profiles.empty()) {
return -1;
}
auto sorted_indices = SortByCompleteness(collect_user_data_options, profiles);
auto sorted_indices =
SortContactsByCompleteness(collect_user_data_options, profiles);
if (!collect_user_data_options.default_email.empty()) {
for (int index : sorted_indices) {
if (base::UTF16ToUTF8(
......@@ -136,7 +168,31 @@ int GetDefaultProfile(
return sorted_indices[0];
}
std::vector<int> SortByCompleteness(
std::vector<int> SortAddressesByCompleteness(
const CollectUserDataOptions& collect_user_data_options,
const std::vector<std::unique_ptr<autofill::AutofillProfile>>& profiles) {
std::vector<int> profile_indices(profiles.size());
std::iota(std::begin(profile_indices), std::end(profile_indices), 0);
std::sort(profile_indices.begin(), profile_indices.end(),
[&collect_user_data_options, &profiles](int i, int j) {
return CompletenessCompareAddresses(collect_user_data_options,
*profiles[i], *profiles[j]);
});
return profile_indices;
}
int GetDefaultAddressProfile(
const CollectUserDataOptions& collect_user_data_options,
const std::vector<std::unique_ptr<autofill::AutofillProfile>>& profiles) {
if (profiles.empty()) {
return -1;
}
auto sorted_indices =
SortContactsByCompleteness(collect_user_data_options, profiles);
return sorted_indices[0];
}
std::vector<int> SortPaymentInstrumentsByCompleteness(
const CollectUserDataOptions& collect_user_data_options,
const std::vector<std::unique_ptr<PaymentInstrument>>&
payment_instruments) {
......@@ -146,9 +202,9 @@ std::vector<int> SortByCompleteness(
std::sort(payment_instrument_indices.begin(),
payment_instrument_indices.end(),
[&collect_user_data_options, &payment_instruments](int a, int b) {
return CompletenessCompare(collect_user_data_options,
*payment_instruments[a],
*payment_instruments[b]);
return CompletenessComparePaymentInstruments(
collect_user_data_options, *payment_instruments[a],
*payment_instruments[b]);
});
return payment_instrument_indices;
}
......@@ -160,8 +216,8 @@ int GetDefaultPaymentInstrument(
if (payment_instruments.empty()) {
return -1;
}
auto sorted_indices =
SortByCompleteness(collect_user_data_options, payment_instruments);
auto sorted_indices = SortPaymentInstrumentsByCompleteness(
collect_user_data_options, payment_instruments);
return sorted_indices[0];
}
......
......@@ -16,13 +16,27 @@ namespace autofill_assistant {
// vector of profile indices in sorted order. Full profiles will be ordered
// before empty ones, and for equally complete profiles, this falls back to
// sorting based on the profile names.
std::vector<int> SortByCompleteness(
std::vector<int> SortContactsByCompleteness(
const CollectUserDataOptions& collect_user_data_options,
const std::vector<std::unique_ptr<autofill::AutofillProfile>>& profiles);
// Get the default selection for the current list of profiles. Returns -1 if no
// default selection is possible.
int GetDefaultProfile(
int GetDefaultContactProfile(
const CollectUserDataOptions& collect_user_data_options,
const std::vector<std::unique_ptr<autofill::AutofillProfile>>& profiles);
// Sorts the given autofill profiles based on completeness, and returns a
// vector of profile indices in sorted order. Full profiles will be ordered
// before empty ones, and for equally complete profiles, this falls back to
// sorting based on the profile names.
std::vector<int> SortAddressesByCompleteness(
const CollectUserDataOptions& collect_user_data_options,
const std::vector<std::unique_ptr<autofill::AutofillProfile>>& profiles);
// Get the default selection for the current list of profiles. Returns -1 if no
// default selection is possible.
int GetDefaultAddressProfile(
const CollectUserDataOptions& collect_user_data_options,
const std::vector<std::unique_ptr<autofill::AutofillProfile>>& profiles);
......@@ -30,7 +44,7 @@ int GetDefaultProfile(
// of payment instrument indices in sorted order. Full payment instruments will
// be ordered before empty ones, and for equally complete payment instruments,
// this falls back to sorting based on the full name on the credit card.
std::vector<int> SortByCompleteness(
std::vector<int> SortPaymentInstrumentsByCompleteness(
const CollectUserDataOptions& collect_user_data_options,
const std::vector<std::unique_ptr<PaymentInstrument>>& payment_instruments);
......
......@@ -18,7 +18,7 @@ using ::testing::ElementsAre;
using ::testing::Eq;
using ::testing::SizeIs;
TEST(UserDataUtilTest, SortsCompleteProfilesAlphabetically) {
TEST(UserDataUtilTest, SortsCompleteContactsAlphabetically) {
auto profile_a = std::make_unique<autofill::AutofillProfile>();
autofill::test::SetProfileInfo(profile_a.get(), "Adam", "", "West",
"adam.west@gmail.com", "", "", "", "", "", "",
......@@ -47,12 +47,12 @@ TEST(UserDataUtilTest, SortsCompleteProfilesAlphabetically) {
options.request_payer_email = true;
std::vector<int> profile_indices =
autofill_assistant::SortByCompleteness(options, profiles);
autofill_assistant::SortContactsByCompleteness(options, profiles);
EXPECT_THAT(profile_indices, SizeIs(profiles.size()));
EXPECT_THAT(profile_indices, ElementsAre(2, 1, 0));
}
TEST(UserDataUtilTest, SortsProfilesByCompleteness) {
TEST(UserDataUtilTest, SortsContactsByCompleteness) {
auto profile_complete = std::make_unique<autofill::AutofillProfile>();
autofill::test::SetProfileInfo(
profile_complete.get(), "Charlie", "", "West", "charlie.west@gmail.com",
......@@ -81,19 +81,19 @@ TEST(UserDataUtilTest, SortsProfilesByCompleteness) {
options.request_shipping = true;
std::vector<int> profile_indices =
autofill_assistant::SortByCompleteness(options, profiles);
autofill_assistant::SortContactsByCompleteness(options, profiles);
EXPECT_THAT(profile_indices, SizeIs(profiles.size()));
EXPECT_THAT(profile_indices, ElementsAre(2, 1, 0));
}
TEST(UserDataUtilTest, GetDefaultSelectionForEmptyProfiles) {
TEST(UserDataUtilTest, GetDefaultContactSelectionForEmptyProfiles) {
std::vector<std::unique_ptr<autofill::AutofillProfile>> profiles;
CollectUserDataOptions options;
EXPECT_THAT(GetDefaultProfile(options, profiles), -1);
EXPECT_THAT(GetDefaultContactProfile(options, profiles), -1);
}
TEST(UserDataUtilTest, GetDefaultSelectionForCompleteProfiles) {
TEST(UserDataUtilTest, GetDefaultContactSelectionForCompleteProfiles) {
auto profile_b = std::make_unique<autofill::AutofillProfile>();
autofill::test::SetProfileInfo(profile_b.get(), "Berta", "", "West",
"berta.west@gmail.com", "", "", "", "", "", "",
......@@ -113,7 +113,7 @@ TEST(UserDataUtilTest, GetDefaultSelectionForCompleteProfiles) {
options.request_payer_name = true;
options.request_payer_email = true;
EXPECT_THAT(GetDefaultProfile(options, profiles), 1);
EXPECT_THAT(GetDefaultContactProfile(options, profiles), 1);
}
TEST(UserDataUtilTest, GetDefaultSelectionForDefaultEmail) {
......@@ -146,7 +146,89 @@ TEST(UserDataUtilTest, GetDefaultSelectionForDefaultEmail) {
options.request_payer_phone = true;
options.default_email = "adam.west@gmail.com";
EXPECT_THAT(GetDefaultProfile(options, profiles), 2);
EXPECT_THAT(GetDefaultContactProfile(options, profiles), 2);
}
TEST(UserDataUtilTest, SortsCompleteAddressesAlphabetically) {
auto profile_b = std::make_unique<autofill::AutofillProfile>();
autofill::test::SetProfileInfo(profile_b.get(), "Berta", "", "West", "", "",
"Brandschenkestrasse 110", "", "Zurich", "",
"8002", "CH", "");
auto profile_a = std::make_unique<autofill::AutofillProfile>();
autofill::test::SetProfileInfo(profile_a.get(), "Adam", "", "West", "", "",
"Brandschenkestrasse 110", "", "Zurich", "",
"8002", "CH", "");
// Specify profiles in reverse order to force sorting.
std::vector<std::unique_ptr<autofill::AutofillProfile>> profiles;
profiles.emplace_back(std::move(profile_b));
profiles.emplace_back(std::move(profile_a));
CollectUserDataOptions options;
std::vector<int> profile_indices =
autofill_assistant::SortAddressesByCompleteness(options, profiles);
EXPECT_THAT(profile_indices, SizeIs(profiles.size()));
EXPECT_THAT(profile_indices, ElementsAre(1, 0));
}
TEST(UserDataUtilTest, SortsAddressesByCompleteness) {
// Adding email address and phone number to demonstrate that they are not
// checked for completeness.
auto profile_no_street = std::make_unique<autofill::AutofillProfile>();
autofill::test::SetProfileInfo(profile_no_street.get(), "Adam", "", "West",
"adam.west@gmail.com", "", "", "", "Zurich",
"", "8002", "CH", "+41");
auto profile_complete = std::make_unique<autofill::AutofillProfile>();
autofill::test::SetProfileInfo(profile_complete.get(), "Berta", "", "West",
"", "", "Brandschenkestrasse 110", "",
"Zurich", "", "8002", "UK", "");
// Specify profiles in reverse order to force sorting.
std::vector<std::unique_ptr<autofill::AutofillProfile>> profiles;
profiles.emplace_back(std::move(profile_no_street));
profiles.emplace_back(std::move(profile_complete));
CollectUserDataOptions options;
std::vector<int> profile_indices =
autofill_assistant::SortAddressesByCompleteness(options, profiles);
EXPECT_THAT(profile_indices, SizeIs(profiles.size()));
EXPECT_THAT(profile_indices, ElementsAre(1, 0));
}
TEST(UserDataUtilTest, GetDefaultAddressSelectionForEmptyProfiles) {
std::vector<std::unique_ptr<autofill::AutofillProfile>> profiles;
CollectUserDataOptions options;
EXPECT_THAT(GetDefaultAddressProfile(options, profiles), -1);
}
TEST(UserDataUtilTest, GetDefaultAddressSelectionForCompleteProfiles) {
// Adding email address and phone number to demonstrate that they are not
// checked for completeness.
auto profile_with_irrelevant_details =
std::make_unique<autofill::AutofillProfile>();
autofill::test::SetProfileInfo(profile_with_irrelevant_details.get(), "Berta",
"berta.west@gmail.com", "West", "", "",
"Brandschenkestrasse 110", "", "Zurich", "",
"8002", "CH", "+41");
auto profile_complete = std::make_unique<autofill::AutofillProfile>();
autofill::test::SetProfileInfo(profile_complete.get(), "Adam", "", "West", "",
"", "Brandschenkestrasse 110", "", "Zurich",
"", "8002", "CH", "");
// Specify profiles in reverse order to force sorting.
std::vector<std::unique_ptr<autofill::AutofillProfile>> profiles;
profiles.emplace_back(std::move(profile_with_irrelevant_details));
profiles.emplace_back(std::move(profile_complete));
CollectUserDataOptions options;
EXPECT_THAT(GetDefaultAddressProfile(options, profiles), 1);
}
TEST(UserDataUtilTest, SortsCreditCardsByCompleteness) {
......@@ -172,7 +254,7 @@ TEST(UserDataUtilTest, SortsCreditCardsByCompleteness) {
CollectUserDataOptions options;
std::vector<int> sorted_indices =
SortByCompleteness(options, payment_instruments);
SortPaymentInstrumentsByCompleteness(options, payment_instruments);
EXPECT_THAT(sorted_indices, SizeIs(payment_instruments.size()));
EXPECT_THAT(sorted_indices, ElementsAre(1, 0));
}
......@@ -200,7 +282,7 @@ TEST(UserDataUtilTest, SortsCompleteCardsByName) {
CollectUserDataOptions options;
std::vector<int> sorted_indices =
SortByCompleteness(options, payment_instruments);
SortPaymentInstrumentsByCompleteness(options, payment_instruments);
EXPECT_THAT(sorted_indices, SizeIs(payment_instruments.size()));
EXPECT_THAT(sorted_indices, ElementsAre(1, 0));
}
......@@ -253,7 +335,7 @@ TEST(UserDataUtilTest, SortsCreditCardsByAddressCompleteness) {
options.require_billing_postal_code = true;
std::vector<int> sorted_indices =
SortByCompleteness(options, payment_instruments);
SortPaymentInstrumentsByCompleteness(options, payment_instruments);
EXPECT_THAT(sorted_indices, SizeIs(payment_instruments.size()));
EXPECT_THAT(sorted_indices, ElementsAre(2, 1, 0));
}
......
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