Commit 46c534fb authored by Jan Krcal's avatar Jan Krcal Committed by Commit Bot

Reland: [AF] Add integration tests for wallet metadata

This CL creates the first integration tests for the autofill wallet
metadata model type.

Bug: 894001
Change-Id: I55ab6517d03d7ccb199d24ccbbc9ff13927017f0
Reviewed-on: https://chromium-review.googlesource.com/c/1332934Reviewed-by: default avatarSebastien Seguin-Gagnon <sebsg@chromium.org>
Commit-Queue: Sebastien Seguin-Gagnon <sebsg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#607638}
parent 8d37adb9
......@@ -22,7 +22,6 @@
#include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/autofill_type.h"
#include "components/autofill/core/browser/payments/payments_customer_data.h"
#include "components/autofill/core/browser/personal_data_manager.h"
#include "components/autofill/core/browser/webdata/autofill_entry.h"
#include "components/autofill/core/browser/webdata/autofill_table.h"
......@@ -60,6 +59,11 @@ class MockWebDataServiceObserver
void(const AutofillChangeList& changes));
};
scoped_refptr<AutofillWebDataService> GetWebDataService(int index) {
return WebDataServiceFactory::GetAutofillWebDataForProfile(
test()->GetProfile(index), ServiceAccessType::EXPLICIT_ACCESS);
}
void WaitForCurrentTasksToComplete(base::SequencedTaskRunner* task_runner) {
base::WaitableEvent event(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
......@@ -76,8 +80,7 @@ void RemoveKeyDontBlockForSync(int profile, const AutofillKey& key) {
EXPECT_CALL(mock_observer, AutofillEntriesChanged(_))
.WillOnce(SignalEvent(&done_event));
scoped_refptr<AutofillWebDataService> wds =
autofill_helper::GetProfileWebDataService(profile);
scoped_refptr<AutofillWebDataService> wds = GetWebDataService(profile);
void (AutofillWebDataService::*add_observer_func)(
AutofillWebDataServiceObserverOnDBSequence*) =
......@@ -111,30 +114,6 @@ std::vector<AutofillEntry> GetAllAutofillEntries(AutofillWebDataService* wds) {
return entries;
}
void SetServerCardsOnDBSequence(
AutofillWebDataService* wds,
const std::vector<autofill::CreditCard>& credit_cards) {
DCHECK(wds->GetDBTaskRunner()->RunsTasksInCurrentSequence());
AutofillTable::FromWebDatabase(wds->GetDatabase())
->SetServerCreditCards(credit_cards);
}
void SetServerProfilesOnDBSequence(
AutofillWebDataService* wds,
const std::vector<autofill::AutofillProfile>& profiles) {
DCHECK(wds->GetDBTaskRunner()->RunsTasksInCurrentSequence());
AutofillTable::FromWebDatabase(wds->GetDatabase())
->SetServerProfiles(profiles);
}
void SetPaymentsCustomerDataOnDBSequence(
AutofillWebDataService* wds,
const autofill::PaymentsCustomerData& customer_data) {
DCHECK(wds->GetDBTaskRunner()->RunsTasksInCurrentSequence());
AutofillTable::FromWebDatabase(wds->GetDatabase())
->SetPaymentsCustomerData(&customer_data);
}
bool ProfilesMatchImpl(
int profile_a,
const std::vector<AutofillProfile*>& autofill_profiles_a,
......@@ -227,16 +206,6 @@ AutofillProfile CreateUniqueAutofillProfile() {
return profile;
}
scoped_refptr<AutofillWebDataService> GetProfileWebDataService(int index) {
return WebDataServiceFactory::GetAutofillWebDataForProfile(
test()->GetProfile(index), ServiceAccessType::EXPLICIT_ACCESS);
}
scoped_refptr<AutofillWebDataService> GetAccountWebDataService(int index) {
return WebDataServiceFactory::GetAutofillWebDataForAccount(
test()->GetProfile(index), ServiceAccessType::EXPLICIT_ACCESS);
}
PersonalDataManager* GetPersonalDataManager(int index) {
return autofill::PersonalDataManagerFactory::GetForProfile(
test()->GetProfile(index));
......@@ -257,7 +226,7 @@ void AddKeys(int profile, const std::set<AutofillKey>& keys) {
EXPECT_CALL(mock_observer, AutofillEntriesChanged(_))
.WillOnce(SignalEvent(&done_event));
scoped_refptr<AutofillWebDataService> wds = GetProfileWebDataService(profile);
scoped_refptr<AutofillWebDataService> wds = GetWebDataService(profile);
void (AutofillWebDataService::*add_observer_func)(
AutofillWebDataServiceObserverOnDBSequence*) =
......@@ -278,8 +247,7 @@ void AddKeys(int profile, const std::set<AutofillKey>& keys) {
void RemoveKey(int profile, const AutofillKey& key) {
RemoveKeyDontBlockForSync(profile, key);
WaitForCurrentTasksToComplete(
autofill_helper::GetProfileWebDataService(profile)->GetDBTaskRunner());
WaitForCurrentTasksToComplete(GetWebDataService(profile)->GetDBTaskRunner());
}
void RemoveKeys(int profile) {
......@@ -287,12 +255,11 @@ void RemoveKeys(int profile) {
for (const AutofillEntry& entry : keys) {
RemoveKeyDontBlockForSync(profile, entry.key());
}
WaitForCurrentTasksToComplete(
autofill_helper::GetProfileWebDataService(profile)->GetDBTaskRunner());
WaitForCurrentTasksToComplete(GetWebDataService(profile)->GetDBTaskRunner());
}
std::set<AutofillEntry> GetAllKeys(int profile) {
scoped_refptr<AutofillWebDataService> wds = GetProfileWebDataService(profile);
scoped_refptr<AutofillWebDataService> wds = GetWebDataService(profile);
std::vector<AutofillEntry> all_entries = GetAllAutofillEntries(wds.get());
return std::set<AutofillEntry>(all_entries.begin(), all_entries.end());
}
......@@ -309,35 +276,6 @@ void SetCreditCards(int profile, std::vector<CreditCard>* credit_cards) {
GetPersonalDataManager(profile)->SetCreditCards(credit_cards);
}
void SetServerCreditCards(
int profile,
const std::vector<autofill::CreditCard>& credit_cards) {
scoped_refptr<AutofillWebDataService> wds = GetProfileWebDataService(profile);
wds->GetDBTaskRunner()->PostTask(
FROM_HERE, base::BindOnce(&SetServerCardsOnDBSequence,
base::Unretained(wds.get()), credit_cards));
WaitForCurrentTasksToComplete(wds->GetDBTaskRunner());
}
void SetServerProfiles(int profile,
const std::vector<autofill::AutofillProfile>& profiles) {
scoped_refptr<AutofillWebDataService> wds = GetProfileWebDataService(profile);
wds->GetDBTaskRunner()->PostTask(
FROM_HERE, base::BindOnce(&SetServerProfilesOnDBSequence,
base::Unretained(wds.get()), profiles));
WaitForCurrentTasksToComplete(wds->GetDBTaskRunner());
}
void SetPaymentsCustomerData(
int profile,
const autofill::PaymentsCustomerData& customer_data) {
scoped_refptr<AutofillWebDataService> wds = GetProfileWebDataService(profile);
wds->GetDBTaskRunner()->PostTask(
FROM_HERE, base::BindOnce(&SetPaymentsCustomerDataOnDBSequence,
base::Unretained(wds.get()), customer_data));
WaitForCurrentTasksToComplete(wds->GetDBTaskRunner());
}
void AddProfile(int profile, const AutofillProfile& autofill_profile) {
std::vector<AutofillProfile> autofill_profiles;
for (AutofillProfile* profile : GetAllAutoFillProfiles(profile)) {
......@@ -389,8 +327,7 @@ std::vector<AutofillProfile*> GetAllAutoFillProfiles(int profile) {
// data, but this shouldn't cause problems. While PersonalDataManager will
// cancel outstanding queries, this is only instigated on the UI sequence,
// which we are about to block, which means we are safe.
WaitForCurrentTasksToComplete(
autofill_helper::GetProfileWebDataService(profile)->GetDBTaskRunner());
WaitForCurrentTasksToComplete(GetWebDataService(profile)->GetDBTaskRunner());
return pdm->GetProfiles();
}
......@@ -446,9 +383,9 @@ bool AutofillProfileChecker::Wait() {
// before any locally instigated async writes. This is run exactly one time
// before the first IsExitConditionSatisfied() is called.
WaitForCurrentTasksToComplete(
autofill_helper::GetProfileWebDataService(profile_a_)->GetDBTaskRunner());
GetWebDataService(profile_a_)->GetDBTaskRunner());
WaitForCurrentTasksToComplete(
autofill_helper::GetProfileWebDataService(profile_b_)->GetDBTaskRunner());
GetWebDataService(profile_b_)->GetDBTaskRunner());
return StatusChangeChecker::Wait();
}
......
......@@ -20,9 +20,7 @@ class AutofillEntry;
class AutofillKey;
class AutofillProfile;
class AutofillType;
class AutofillWebDataService;
class CreditCard;
struct PaymentsCustomerData;
class PersonalDataManager;
} // namespace autofill
......@@ -35,15 +33,6 @@ enum ProfileType {
PROFILE_NULL
};
// Used to access the web data service within a particular sync profile.
scoped_refptr<autofill::AutofillWebDataService> GetProfileWebDataService(
int index) WARN_UNUSED_RESULT;
// Used to access the account-scoped web data service within a particular sync
// profile.
scoped_refptr<autofill::AutofillWebDataService> GetAccountWebDataService(
int index) WARN_UNUSED_RESULT;
// Used to access the personal data manager within a particular sync profile.
autofill::PersonalDataManager* GetPersonalDataManager(
int index) WARN_UNUSED_RESULT;
......@@ -76,17 +65,6 @@ void SetProfiles(int profile,
void SetCreditCards(int profile,
std::vector<autofill::CreditCard>* credit_cards);
void SetServerCreditCards(
int profile,
const std::vector<autofill::CreditCard>& credit_cards);
void SetServerProfiles(int profile,
const std::vector<autofill::AutofillProfile>& profiles);
void SetPaymentsCustomerData(
int profile,
const autofill::PaymentsCustomerData& customer_data);
// Adds the autofill profile |autofill_profile| to sync profile |profile|.
void AddProfile(int profile, const autofill::AutofillProfile& autofill_profile);
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/macros.h"
#include "chrome/browser/sync/test/integration/sync_test.h"
#include "chrome/browser/sync/test/integration/wallet_helper.h"
#include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/credit_card.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
using autofill::AutofillProfile;
using autofill::CreditCard;
using autofill::PersonalDataManager;
using wallet_helper::CreateDefaultSyncPaymentsCustomerData;
using wallet_helper::CreateSyncWalletAddress;
using wallet_helper::CreateSyncWalletCard;
using wallet_helper::GetCreditCard;
using wallet_helper::GetServerCreditCards;
using wallet_helper::GetServerProfiles;
using wallet_helper::UpdateServerAddressMetadata;
using wallet_helper::UpdateServerCardMetadata;
class TwoClientWalletSyncTest : public UssWalletSwitchToggler, public SyncTest {
public:
TwoClientWalletSyncTest() : SyncTest(TWO_CLIENT) {}
~TwoClientWalletSyncTest() override {}
bool TestUsesSelfNotifications() override { return false; }
bool SetupSync() override {
if (!SyncTest::SetupSync()) {
return false;
}
// As this test does not use self notifications, wait for the metadata to
// converge with the specialized wallet checker.
return AutofillWalletChecker(0, 1).Wait();
}
private:
DISALLOW_COPY_AND_ASSIGN(TwoClientWalletSyncTest);
};
IN_PROC_BROWSER_TEST_P(TwoClientWalletSyncTest, UpdateCreditCardMetadata) {
InitWithDefaultFeatures();
GetFakeServer()->SetWalletData(
{CreateSyncWalletCard(/*name=*/"card-1", /*last_four=*/"0001"),
CreateSyncWalletAddress(/*name=*/"address-1", /*company=*/"Company-1"),
CreateDefaultSyncPaymentsCustomerData()});
ASSERT_TRUE(SetupSync());
// Grab the current card on the first client.
std::vector<CreditCard*> credit_cards = GetServerCreditCards(0);
ASSERT_EQ(1u, credit_cards.size());
CreditCard card = *credit_cards[0];
// Simulate using it -- increase both its use count and use date.
ASSERT_EQ(1u, card.use_count());
card.set_use_count(2);
base::Time new_use_date = base::Time::Now();
ASSERT_NE(new_use_date, card.use_date());
card.set_use_date(new_use_date);
UpdateServerCardMetadata(0, card);
// Wait for the change to propagate.
EXPECT_TRUE(AutofillWalletChecker(0, 1).Wait());
credit_cards = GetServerCreditCards(1);
EXPECT_EQ(1U, credit_cards.size());
EXPECT_EQ(2u, credit_cards[0]->use_count());
EXPECT_EQ(new_use_date, credit_cards[0]->use_date());
credit_cards = GetServerCreditCards(0);
EXPECT_EQ(1U, credit_cards.size());
EXPECT_EQ(2u, credit_cards[0]->use_count());
EXPECT_EQ(new_use_date, credit_cards[0]->use_date());
}
INSTANTIATE_TEST_CASE_P(USS,
TwoClientWalletSyncTest,
::testing::Values(false, true));
} // namespace
This diff is collapsed.
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_SYNC_TEST_INTEGRATION_WALLET_HELPER_H_
#define CHROME_BROWSER_SYNC_TEST_INTEGRATION_WALLET_HELPER_H_
#include <vector>
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/sync/test/integration/multi_client_status_change_checker.h"
#include "components/autofill/core/browser/personal_data_manager_observer.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace autofill {
class AutofillProfile;
class AutofillWebDataService;
class CreditCard;
class PersonalDataManager;
struct PaymentsCustomerData;
} // namespace autofill
namespace sync_pb {
class SyncEntity;
}
namespace wallet_helper {
extern const char kDefaultCardID[];
extern const char kDefaultAddressID[];
extern const char kDefaultCustomerID[];
extern const char kDefaultBillingAddressID[];
// Used to access the personal data manager within a particular sync profile.
autofill::PersonalDataManager* GetPersonalDataManager(int index)
WARN_UNUSED_RESULT;
// Used to access the web data service within a particular sync profile.
scoped_refptr<autofill::AutofillWebDataService> GetProfileWebDataService(
int index) WARN_UNUSED_RESULT;
// Used to access the account-scoped web data service within a particular sync
// profile.
scoped_refptr<autofill::AutofillWebDataService> GetAccountWebDataService(
int index) WARN_UNUSED_RESULT;
void SetServerCreditCards(
int profile,
const std::vector<autofill::CreditCard>& credit_cards);
void SetServerProfiles(int profile,
const std::vector<autofill::AutofillProfile>& profiles);
void SetPaymentsCustomerData(
int profile,
const autofill::PaymentsCustomerData& customer_data);
void UpdateServerCardMetadata(int profile,
const autofill::CreditCard& credit_card);
void UpdateServerAddressMetadata(
int profile,
const autofill::AutofillProfile& server_address);
sync_pb::SyncEntity CreateDefaultSyncWalletCard();
sync_pb::SyncEntity CreateSyncWalletCard(const std::string& name,
const std::string& last_four);
sync_pb::SyncEntity CreateSyncPaymentsCustomerData(
const std::string& customer_id);
sync_pb::SyncEntity CreateDefaultSyncPaymentsCustomerData();
autofill::CreditCard GetDefaultCreditCard();
autofill::CreditCard GetCreditCard(const std::string& name,
const std::string& last_four);
sync_pb::SyncEntity CreateDefaultSyncWalletAddress();
sync_pb::SyncEntity CreateSyncWalletAddress(const std::string& name,
const std::string& company);
// TODO(sebsg): Instead add a function to create a card, and one to inject in
// the server. Then compare the cards directly.
void ExpectDefaultCreditCardValues(const autofill::CreditCard& card);
// TODO(sebsg): Instead add a function to create a profile, and one to inject in
// the server. Then compare the profiles directly.
void ExpectDefaultProfileValues(const autofill::AutofillProfile& profile);
// Load current data from the database of profile |profile|.
std::vector<autofill::AutofillProfile*> GetServerProfiles(int profile);
std::vector<autofill::CreditCard*> GetServerCreditCards(int profile);
} // namespace wallet_helper
// Checker to block until autofill wallet & server profiles match on both
// profiles.
class AutofillWalletChecker : public StatusChangeChecker,
public autofill::PersonalDataManagerObserver {
public:
AutofillWalletChecker(int profile_a, int profile_b);
~AutofillWalletChecker() override;
// StatusChangeChecker implementation.
bool Wait() override;
bool IsExitConditionSatisfied() override;
std::string GetDebugMessage() const override;
// autofill::PersonalDataManager implementation.
void OnPersonalDataChanged() override;
private:
const int profile_a_;
const int profile_b_;
};
// Class that enables or disables USS based on test parameter. Must be the first
// base class of the test fixture.
// TODO(jkrcal): When the new implementation fully launches, remove this class,
// convert all tests from *_P back to *_F and remove the instance at the end.
class UssWalletSwitchToggler : public testing::WithParamInterface<bool> {
public:
UssWalletSwitchToggler();
// Sets up feature overrides, based on the parameter of the test.
void InitWithDefaultFeatures();
// Sets up feature overrides, adds the toggled feature on top of specified
// |enabled_features| and |disabled_features|. Vectors are passed by value
// because we need to alter them anyway.
void InitWithFeatures(std::vector<base::Feature> enabled_features,
std::vector<base::Feature> disabled_features);
private:
base::test::ScopedFeatureList override_features_;
};
#endif // CHROME_BROWSER_SYNC_TEST_INTEGRATION_WALLET_HELPER_H_
......@@ -5292,6 +5292,8 @@ if (!is_android && !is_fuchsia) {
"../browser/sync/test/integration/typed_urls_helper.h",
"../browser/sync/test/integration/updated_progress_marker_checker.cc",
"../browser/sync/test/integration/updated_progress_marker_checker.h",
"../browser/sync/test/integration/wallet_helper.cc",
"../browser/sync/test/integration/wallet_helper.h",
"../browser/sync/test/integration/wifi_credentials_helper.cc",
"../browser/sync/test/integration/wifi_credentials_helper.h",
"../browser/sync/test/integration/wifi_credentials_helper_chromeos.cc",
......@@ -5398,6 +5400,7 @@ if (!is_android && !is_fuchsia) {
"../browser/sync/test/integration/two_client_themes_sync_test.cc",
"../browser/sync/test/integration/two_client_typed_urls_sync_test.cc",
"../browser/sync/test/integration/two_client_uss_sync_test.cc",
"../browser/sync/test/integration/two_client_wallet_sync_test.cc",
"data/resource.rc",
]
......
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