Commit bc4e57ff authored by sebsg's avatar sebsg Committed by Commit Bot

[AF] Add function to decide when to offer to show cards from account.

It should be shown only if:

- The feature is enabled
- The user is not syncing
- The user has server cards
- The user has not already opted-in
- The user is in sync transport mode for Wallet

The last point is checked implicitly by the fact the the user is not
syncing and has some cards.

Bug: 909026
Change-Id: Id0976ea98a2be295968f825f1269888b3d820f0d
Reviewed-on: https://chromium-review.googlesource.com/c/1351944
Commit-Queue: Sebastien Seguin-Gagnon <sebsg@chromium.org>
Reviewed-by: default avatarTommy Martino <tmartino@chromium.org>
Reviewed-by: default avatarFabio Tirelo <ftirelo@chromium.org>
Reviewed-by: default avatarMarc Treib <treib@chromium.org>
Reviewed-by: default avatarFlorian Uunk <feuunk@chromium.org>
Cr-Commit-Position: refs/heads/master@{#611755}
parent 31435595
......@@ -2035,6 +2035,25 @@ bool PersonalDataManager::IsServerCard(const CreditCard* credit_card) const {
return false;
}
bool PersonalDataManager::ShouldShowCardsFromAccountOption() const {
// This option should only be shown for users that have not enabled the Sync
// Feature and that have server credit cards available.
if (!sync_service_ || sync_service_->IsSyncFeatureEnabled() ||
GetServerCreditCards().empty()) {
return false;
}
// If we have not returned yet, it should mean that the user is in Sync
// Transport mode for Wallet data (Sync Feature disabled but has server
// cards). This should only happen if that feature is enabled.
DCHECK(base::FeatureList::IsEnabled(
features::kAutofillEnableAccountWalletStorage));
// The option should only be shown if the user has not already accepted it.
return !::autofill::prefs::IsUserOptedInWalletSyncTransport(
pref_service_, sync_service_->GetAuthenticatedAccountInfo().account_id);
}
std::vector<Suggestion> PersonalDataManager::GetSuggestionsForCards(
const AutofillType& type,
const base::string16& field_contents,
......
......@@ -375,6 +375,10 @@ class PersonalDataManager : public KeyedService,
is_syncing_for_test_ = is_syncing_for_test;
}
// Returns whether a row to give the option of showing cards from the user's
// account should be shown in the dropdown.
bool ShouldShowCardsFromAccountOption() const;
protected:
// Only PersonalDataManagerFactory and certain tests can create instances of
// PersonalDataManager.
......
......@@ -7032,4 +7032,78 @@ TEST_F(PersonalDataManagerTest, OnGaiaCookieDeletedByUserAction) {
prefs_->GetDictionary(prefs::kAutofillSyncTransportOptIn)->DictEmpty());
}
TEST_F(PersonalDataManagerTest, ShouldShowCardsFromAccountOption) {
// The method should return false if one of these is not respected:
// * The sync_service is not null
// * The sync feature is not enabled
// * The user has server cards
// * The user has not opted-in to seeing their account cards
// Start by setting everything up, then making each of these conditions false
// independently, one by one.
// Set everything up so that the proposition should be shown.
// Set an an active secondary account.
AccountInfo active_info;
active_info.email = "signed_in_account@email.com";
active_info.account_id = "account_id";
sync_service_.SetAuthenticatedAccountInfo(active_info);
sync_service_.SetIsAuthenticatedAccountPrimary(false);
// Set a server credit card.
std::vector<CreditCard> server_cards;
server_cards.push_back(CreditCard(CreditCard::FULL_SERVER_CARD, "c789"));
test::SetCreditCardInfo(&server_cards.back(), "Clyde Barrow",
"378282246310005" /* American Express */, "04",
"2999", "1");
SetServerCards(server_cards);
personal_data_->Refresh();
WaitForOnPersonalDataChanged();
// Set the feature to enabled.
base::test::ScopedFeatureList scoped_features;
scoped_features.InitWithFeatures(
/*enabled_features=*/{features::kAutofillEnableAccountWalletStorage},
/*disabled_features=*/{});
// Make sure the function returns true.
EXPECT_TRUE(personal_data_->ShouldShowCardsFromAccountOption());
// Set that the user already opted-in. Check that the function now returns
// false.
::autofill::prefs::SetUserOptedInWalletSyncTransport(
prefs_.get(), active_info.account_id, true);
EXPECT_FALSE(personal_data_->ShouldShowCardsFromAccountOption());
// Re-opt the user out. Check that the function now returns true.
::autofill::prefs::SetUserOptedInWalletSyncTransport(
prefs_.get(), active_info.account_id, false);
EXPECT_TRUE(personal_data_->ShouldShowCardsFromAccountOption());
// Set that the user has no server cards. Check that the function now returns
// false.
SetServerCards({});
personal_data_->Refresh();
WaitForOnPersonalDataChanged();
EXPECT_FALSE(personal_data_->ShouldShowCardsFromAccountOption());
// Re-set some server cards. Check that the function now returns true.
SetServerCards(server_cards);
personal_data_->Refresh();
WaitForOnPersonalDataChanged();
EXPECT_TRUE(personal_data_->ShouldShowCardsFromAccountOption());
// Set that the user enabled the sync feature. Check that the function now
// returns false.
sync_service_.SetIsAuthenticatedAccountPrimary(true);
EXPECT_FALSE(personal_data_->ShouldShowCardsFromAccountOption());
// Re-disable the sync feature. Check that the function now returns true.
sync_service_.SetIsAuthenticatedAccountPrimary(false);
EXPECT_TRUE(personal_data_->ShouldShowCardsFromAccountOption());
// Set a null sync service. Check that the function now returns false.
personal_data_->SetSyncServiceForTest(nullptr);
EXPECT_FALSE(personal_data_->ShouldShowCardsFromAccountOption());
}
} // namespace autofill
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