Commit 794427c3 authored by Mohamad Ahmadi's avatar Mohamad Ahmadi Committed by Commit Bot

[PR] Don't save Payment Request data in PersonalDataManager in Incognito

Fixes the bug where changes to existing autofill profiles and credit cards
or adding new ones get reflected in PersonalDataManager when user is in
incognito mode.
Also refactors the details of adding/updating autofill profiles and credit
cards into the paymentrequest.mm instead of the coordinators.
Updates the unit tests accordingly.

Bug: 814130
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: I2cc72032648d162304211fe3de6612bc23848350
Reviewed-on: https://chromium-review.googlesource.com/931625
Commit-Queue: Moe Ahmadi <mahmadi@chromium.org>
Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Reviewed-by: default avatarSebastien Seguin-Gagnon <sebsg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#539048}
parent 618b6f57
...@@ -149,7 +149,7 @@ class PersonalDataManager : public KeyedService, ...@@ -149,7 +149,7 @@ class PersonalDataManager : public KeyedService,
// Updates the use stats and billing address id for the server |credit_card|. // Updates the use stats and billing address id for the server |credit_card|.
// Looks up the card by server_id. // Looks up the card by server_id.
void UpdateServerCardMetadata(const CreditCard& credit_card); virtual void UpdateServerCardMetadata(const CreditCard& credit_card);
// Resets the card for |guid| to the masked state. // Resets the card for |guid| to the masked state.
void ResetFullServerCard(const std::string& guid); void ResetFullServerCard(const std::string& guid);
......
...@@ -32,6 +32,7 @@ source_set("payments") { ...@@ -32,6 +32,7 @@ source_set("payments") {
":constants", ":constants",
"//base", "//base",
"//components/autofill/core/browser", "//components/autofill/core/browser",
"//components/autofill/ios/browser",
"//components/image_fetcher/ios", "//components/image_fetcher/ios",
"//components/keyed_service/ios", "//components/keyed_service/ios",
"//components/payments/core", "//components/payments/core",
......
...@@ -184,6 +184,10 @@ class PaymentRequest : public PaymentOptionsProvider, ...@@ -184,6 +184,10 @@ class PaymentRequest : public PaymentOptionsProvider,
virtual autofill::AutofillProfile* AddAutofillProfile( virtual autofill::AutofillProfile* AddAutofillProfile(
const autofill::AutofillProfile& profile); const autofill::AutofillProfile& profile);
// Updates the given |profile| in the PersonalDataManager if the user is
// not in incognito mode.
virtual void UpdateAutofillProfile(const autofill::AutofillProfile& profile);
// Returns the available autofill profiles for this user to be used as // Returns the available autofill profiles for this user to be used as
// shipping profiles. // shipping profiles.
const std::vector<autofill::AutofillProfile*>& shipping_profiles() const { const std::vector<autofill::AutofillProfile*>& shipping_profiles() const {
...@@ -246,9 +250,14 @@ class PaymentRequest : public PaymentOptionsProvider, ...@@ -246,9 +250,14 @@ class PaymentRequest : public PaymentOptionsProvider,
return supported_card_types_set_; return supported_card_types_set_;
} }
// Creates and adds an AutofillPaymentInstrument, which makes a copy of // Creates and adds an AutofillPaymentInstrument to the list of payment
// |credit_card|. // instruments by making a copy of |credit_card|.
virtual AutofillPaymentInstrument* AddAutofillPaymentInstrument( virtual AutofillPaymentInstrument* CreateAndAddAutofillPaymentInstrument(
const autofill::CreditCard& credit_card);
// Updates the given |credit_card| in the PersonalDataManager if the user is
// not in incognito mode.
virtual void UpdateAutofillPaymentInstrument(
const autofill::CreditCard& credit_card); const autofill::CreditCard& credit_card);
// Returns the available payment methods for this user that match a supported // Returns the available payment methods for this user that match a supported
...@@ -344,6 +353,13 @@ class PaymentRequest : public PaymentOptionsProvider, ...@@ -344,6 +353,13 @@ class PaymentRequest : public PaymentOptionsProvider,
// methods. // methods.
void PopulateAvailablePaymentMethods(); void PopulateAvailablePaymentMethods();
// Creates and adds an AutofillPaymentInstrument to the list of payment
// instruments by making a copy of |credit_card|. Updates PersonalDataManager
// if not in incognito mode and |may_update_personal_data_manager| is true.
AutofillPaymentInstrument* CreateAndAddAutofillPaymentInstrument(
const autofill::CreditCard& credit_card,
bool may_update_personal_data_manager);
// Sets the available shipping options as references to the shipping options // Sets the available shipping options as references to the shipping options
// in |web_payment_request_|. // in |web_payment_request_|.
void PopulateAvailableShippingOptions(); void PopulateAvailableShippingOptions();
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "components/autofill/core/browser/personal_data_manager.h" #include "components/autofill/core/browser/personal_data_manager.h"
#include "components/autofill/core/browser/region_data_loader_impl.h" #include "components/autofill/core/browser/region_data_loader_impl.h"
#include "components/autofill/core/browser/validation.h" #include "components/autofill/core/browser/validation.h"
#import "components/autofill/ios/browser/credit_card_util.h"
#include "components/payments/core/autofill_payment_instrument.h" #include "components/payments/core/autofill_payment_instrument.h"
#include "components/payments/core/currency_formatter.h" #include "components/payments/core/currency_formatter.h"
#include "components/payments/core/features.h" #include "components/payments/core/features.h"
...@@ -289,9 +290,21 @@ autofill::AutofillProfile* PaymentRequest::AddAutofillProfile( ...@@ -289,9 +290,21 @@ autofill::AutofillProfile* PaymentRequest::AddAutofillProfile(
contact_profiles_.push_back(profile_cache_.back().get()); contact_profiles_.push_back(profile_cache_.back().get());
shipping_profiles_.push_back(profile_cache_.back().get()); shipping_profiles_.push_back(profile_cache_.back().get());
if (!IsIncognito())
personal_data_manager_->AddProfile(profile);
return profile_cache_.back().get(); return profile_cache_.back().get();
} }
void PaymentRequest::UpdateAutofillProfile(
const autofill::AutofillProfile& profile) {
// Cached profile must be invalidated once the profile is modified.
profile_comparator()->Invalidate(profile);
if (!IsIncognito())
personal_data_manager_->UpdateProfile(profile);
}
const PaymentDetailsModifier* PaymentRequest::GetApplicableModifier( const PaymentDetailsModifier* PaymentRequest::GetApplicableModifier(
PaymentInstrument* selected_instrument) const { PaymentInstrument* selected_instrument) const {
if (!selected_instrument || if (!selected_instrument ||
...@@ -361,8 +374,17 @@ void PaymentRequest::PopulateAvailableProfiles() { ...@@ -361,8 +374,17 @@ void PaymentRequest::PopulateAvailableProfiles() {
profile_comparator_.FilterProfilesForShipping(raw_profiles_for_filtering); profile_comparator_.FilterProfilesForShipping(raw_profiles_for_filtering);
} }
AutofillPaymentInstrument* PaymentRequest::AddAutofillPaymentInstrument( AutofillPaymentInstrument*
PaymentRequest::CreateAndAddAutofillPaymentInstrument(
const autofill::CreditCard& credit_card) { const autofill::CreditCard& credit_card) {
return CreateAndAddAutofillPaymentInstrument(
credit_card, /*may_update_personal_data_manager=*/true);
}
AutofillPaymentInstrument*
PaymentRequest::CreateAndAddAutofillPaymentInstrument(
const autofill::CreditCard& credit_card,
bool may_update_personal_data_manager) {
std::string basic_card_issuer_network = std::string basic_card_issuer_network =
autofill::data_util::GetPaymentRequestData(credit_card.network()) autofill::data_util::GetPaymentRequestData(credit_card.network())
.basic_card_issuer_network; .basic_card_issuer_network;
...@@ -398,10 +420,24 @@ AutofillPaymentInstrument* PaymentRequest::AddAutofillPaymentInstrument( ...@@ -398,10 +420,24 @@ AutofillPaymentInstrument* PaymentRequest::AddAutofillPaymentInstrument(
payment_methods_.push_back(payment_method_cache_.back().get()); payment_methods_.push_back(payment_method_cache_.back().get());
if (may_update_personal_data_manager && !IsIncognito())
personal_data_manager_->AddCreditCard(credit_card);
return static_cast<AutofillPaymentInstrument*>( return static_cast<AutofillPaymentInstrument*>(
payment_method_cache_.back().get()); payment_method_cache_.back().get());
} }
void PaymentRequest::UpdateAutofillPaymentInstrument(
const autofill::CreditCard& credit_card) {
if (IsIncognito())
return;
if (autofill::IsCreditCardLocal(credit_card))
personal_data_manager_->UpdateCreditCard(credit_card);
else
personal_data_manager_->UpdateServerCardMetadata(credit_card);
}
PaymentsProfileComparator* PaymentRequest::profile_comparator() { PaymentsProfileComparator* PaymentRequest::profile_comparator() {
return &profile_comparator_; return &profile_comparator_;
} }
...@@ -513,8 +549,12 @@ void PaymentRequest::PopulatePaymentMethodCache( ...@@ -513,8 +549,12 @@ void PaymentRequest::PopulatePaymentMethodCache(
for (auto& instrument : native_app_instruments) for (auto& instrument : native_app_instruments)
payment_method_cache_.push_back(std::move(instrument)); payment_method_cache_.push_back(std::move(instrument));
for (const auto* credit_card : credit_cards_to_suggest) for (const auto* credit_card : credit_cards_to_suggest) {
AddAutofillPaymentInstrument(*credit_card); // We only want to add the credit cards read from the PersonalDataManager to
// the list of payment instrument. Don't re-add them to PersonalDataManager.
CreateAndAddAutofillPaymentInstrument(
*credit_card, /*may_update_personal_data_manager=*/false);
}
PopulateAvailablePaymentMethods(); PopulateAvailablePaymentMethods();
......
...@@ -41,26 +41,12 @@ class TestPaymentRequest : public PaymentRequest { ...@@ -41,26 +41,12 @@ class TestPaymentRequest : public PaymentRequest {
ios::ChromeBrowserState* browser_state, ios::ChromeBrowserState* browser_state,
web::WebState* web_state, web::WebState* web_state,
autofill::PersonalDataManager* personal_data_manager, autofill::PersonalDataManager* personal_data_manager,
id<PaymentRequestUIDelegate> payment_request_ui_delegate) id<PaymentRequestUIDelegate> payment_request_ui_delegate);
: PaymentRequest(web_payment_request,
browser_state,
web_state,
personal_data_manager,
payment_request_ui_delegate),
address_normalization_manager_(&address_normalizer_, "en-US"),
region_data_loader_(nullptr),
pref_service_(nullptr),
profile_comparator_(nullptr) {}
TestPaymentRequest(const payments::WebPaymentRequest& web_payment_request, TestPaymentRequest(const payments::WebPaymentRequest& web_payment_request,
ios::ChromeBrowserState* browser_state, ios::ChromeBrowserState* browser_state,
web::WebState* web_state, web::WebState* web_state,
autofill::PersonalDataManager* personal_data_manager) autofill::PersonalDataManager* personal_data_manager);
: TestPaymentRequest(web_payment_request,
browser_state,
web_state,
personal_data_manager,
nil) {}
~TestPaymentRequest() override {} ~TestPaymentRequest() override {}
...@@ -101,6 +87,8 @@ class TestPaymentRequest : public PaymentRequest { ...@@ -101,6 +87,8 @@ class TestPaymentRequest : public PaymentRequest {
selected_shipping_option_ = option; selected_shipping_option_ = option;
} }
void set_is_incognito(bool is_incognito) { is_incognito_ = is_incognito; }
// PaymentRequest // PaymentRequest
autofill::AddressNormalizer* GetAddressNormalizer() override; autofill::AddressNormalizer* GetAddressNormalizer() override;
autofill::AddressNormalizationManager* GetAddressNormalizationManager() autofill::AddressNormalizationManager* GetAddressNormalizationManager()
...@@ -108,6 +96,7 @@ class TestPaymentRequest : public PaymentRequest { ...@@ -108,6 +96,7 @@ class TestPaymentRequest : public PaymentRequest {
autofill::RegionDataLoader* GetRegionDataLoader() override; autofill::RegionDataLoader* GetRegionDataLoader() override;
PrefService* GetPrefService() override; PrefService* GetPrefService() override;
PaymentsProfileComparator* profile_comparator() override; PaymentsProfileComparator* profile_comparator() override;
bool IsIncognito() const override;
private: private:
autofill::TestAddressNormalizer address_normalizer_; autofill::TestAddressNormalizer address_normalizer_;
...@@ -122,6 +111,9 @@ class TestPaymentRequest : public PaymentRequest { ...@@ -122,6 +111,9 @@ class TestPaymentRequest : public PaymentRequest {
// Not owned and must outlive this object. // Not owned and must outlive this object.
PaymentsProfileComparator* profile_comparator_; PaymentsProfileComparator* profile_comparator_;
// Whether the user is in incognito mode.
bool is_incognito_;
DISALLOW_COPY_AND_ASSIGN(TestPaymentRequest); DISALLOW_COPY_AND_ASSIGN(TestPaymentRequest);
}; };
......
...@@ -17,6 +17,34 @@ ...@@ -17,6 +17,34 @@
namespace payments { namespace payments {
TestPaymentRequest::TestPaymentRequest(
const payments::WebPaymentRequest& web_payment_request,
ios::ChromeBrowserState* browser_state,
web::WebState* web_state,
autofill::PersonalDataManager* personal_data_manager,
id<PaymentRequestUIDelegate> payment_request_ui_delegate)
: PaymentRequest(web_payment_request,
browser_state,
web_state,
personal_data_manager,
payment_request_ui_delegate),
address_normalization_manager_(&address_normalizer_, "en-US"),
region_data_loader_(nullptr),
pref_service_(nullptr),
profile_comparator_(nullptr),
is_incognito_(false) {}
TestPaymentRequest::TestPaymentRequest(
const payments::WebPaymentRequest& web_payment_request,
ios::ChromeBrowserState* browser_state,
web::WebState* web_state,
autofill::PersonalDataManager* personal_data_manager)
: TestPaymentRequest(web_payment_request,
browser_state,
web_state,
personal_data_manager,
/*payment_request_ui_delegate=*/nil) {}
void TestPaymentRequest::ClearShippingProfiles() { void TestPaymentRequest::ClearShippingProfiles() {
shipping_profiles_.clear(); shipping_profiles_.clear();
} }
...@@ -64,4 +92,8 @@ PaymentsProfileComparator* TestPaymentRequest::profile_comparator() { ...@@ -64,4 +92,8 @@ PaymentsProfileComparator* TestPaymentRequest::profile_comparator() {
return PaymentRequest::profile_comparator(); return PaymentRequest::profile_comparator();
} }
bool TestPaymentRequest::IsIncognito() const {
return is_incognito_;
}
} // namespace payments } // namespace payments
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
#include "components/autofill/core/browser/autofill_type.h" #include "components/autofill/core/browser/autofill_type.h"
#include "components/autofill/core/browser/personal_data_manager.h" #include "components/autofill/core/browser/personal_data_manager.h"
#include "components/autofill/core/common/autofill_constants.h" #include "components/autofill/core/common/autofill_constants.h"
#include "components/payments/core/payments_profile_comparator.h"
#include "ios/chrome/browser/payments/payment_request.h" #include "ios/chrome/browser/payments/payment_request.h"
#import "ios/chrome/browser/ui/autofill/autofill_ui_type_util.h" #import "ios/chrome/browser/ui/autofill/autofill_ui_type_util.h"
#import "ios/chrome/browser/ui/payments/address_edit_mediator.h" #import "ios/chrome/browser/ui/payments/address_edit_mediator.h"
...@@ -108,9 +107,10 @@ using ::AutofillTypeFromAutofillUIType; ...@@ -108,9 +107,10 @@ using ::AutofillTypeFromAutofillUIType;
// Create an empty autofill profile. If an address is being edited, copy over // Create an empty autofill profile. If an address is being edited, copy over
// the information. // the information.
autofill::AutofillProfile address = autofill::AutofillProfile address =
self.address ? *self.address self.address ? *self.address : autofill::AutofillProfile();
: autofill::AutofillProfile(base::GenerateGUID(),
autofill::kSettingsOrigin); // Set the origin, or override it if the autofill profile is being edited.
address.set_origin(autofill::kSettingsOrigin);
for (EditorField* field in fields) { for (EditorField* field in fields) {
address.SetInfo(autofill::AutofillType( address.SetInfo(autofill::AutofillType(
...@@ -120,20 +120,12 @@ using ::AutofillTypeFromAutofillUIType; ...@@ -120,20 +120,12 @@ using ::AutofillTypeFromAutofillUIType;
} }
if (!self.address) { if (!self.address) {
self.paymentRequest->GetPersonalDataManager()->AddProfile(address);
// Add the profile to the list of profiles in |self.paymentRequest|. // Add the profile to the list of profiles in |self.paymentRequest|.
self.address = self.paymentRequest->AddAutofillProfile(address); self.address = self.paymentRequest->AddAutofillProfile(address);
} else { } else {
// Override the origin.
address.set_origin(autofill::kSettingsOrigin);
self.paymentRequest->GetPersonalDataManager()->UpdateProfile(address);
// Cached profile must be invalidated once the profile is modified.
_paymentRequest->profile_comparator()->Invalidate(address);
// Update the original profile instance that is being edited. // Update the original profile instance that is being edited.
*self.address = address; *self.address = address;
self.paymentRequest->UpdateAutofillProfile(address);
} }
[self.delegate addressEditCoordinator:self [self.delegate addressEditCoordinator:self
......
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
#include "components/autofill/core/browser/country_names.h" #include "components/autofill/core/browser/country_names.h"
#include "components/autofill/core/browser/test_personal_data_manager.h" #include "components/autofill/core/browser/test_personal_data_manager.h"
#include "components/autofill/core/browser/test_region_data_loader.h" #include "components/autofill/core/browser/test_region_data_loader.h"
#include "components/payments/core/payments_profile_comparator.h"
#include "ios/chrome/browser/payments/payment_request_test_util.h" #include "ios/chrome/browser/payments/payment_request_test_util.h"
#include "ios/chrome/browser/payments/payment_request_unittest_base.h" #include "ios/chrome/browser/payments/payment_request_unittest_base.h"
#include "ios/chrome/browser/payments/test_payment_request.h" #include "ios/chrome/browser/payments/test_payment_request.h"
...@@ -34,21 +33,8 @@ ...@@ -34,21 +33,8 @@
#endif #endif
namespace { namespace {
class MockTestPersonalDataManager : public autofill::TestPersonalDataManager {
public:
MockTestPersonalDataManager() : TestPersonalDataManager() {}
MOCK_METHOD1(AddProfile, void(const autofill::AutofillProfile&));
MOCK_METHOD1(UpdateProfile, void(const autofill::AutofillProfile&));
};
class MockPaymentsProfileComparator using ::testing::_;
: public payments::PaymentsProfileComparator {
public:
MockPaymentsProfileComparator(const std::string& app_locale,
const payments::PaymentOptionsProvider& options)
: PaymentsProfileComparator(app_locale, options) {}
MOCK_METHOD1(Invalidate, void(const autofill::AutofillProfile&));
};
class MockTestPaymentRequest : public payments::TestPaymentRequest { class MockTestPaymentRequest : public payments::TestPaymentRequest {
public: public:
...@@ -62,6 +48,7 @@ class MockTestPaymentRequest : public payments::TestPaymentRequest { ...@@ -62,6 +48,7 @@ class MockTestPaymentRequest : public payments::TestPaymentRequest {
personal_data_manager) {} personal_data_manager) {}
MOCK_METHOD1(AddAutofillProfile, MOCK_METHOD1(AddAutofillProfile,
autofill::AutofillProfile*(const autofill::AutofillProfile&)); autofill::AutofillProfile*(const autofill::AutofillProfile&));
MOCK_METHOD1(UpdateAutofillProfile, void(const autofill::AutofillProfile&));
}; };
MATCHER_P4(ProfileMatches, name, country, state, phone_number, "") { MATCHER_P4(ProfileMatches, name, country, state, phone_number, "") {
...@@ -101,8 +88,6 @@ NSArray<EditorField*>* GetEditorFields() { ...@@ -101,8 +88,6 @@ NSArray<EditorField*>* GetEditorFields() {
required:YES], required:YES],
]; ];
} }
using ::testing::_;
} // namespace } // namespace
class PaymentRequestAddressEditCoordinatorTest class PaymentRequestAddressEditCoordinatorTest
...@@ -121,10 +106,6 @@ class PaymentRequestAddressEditCoordinatorTest ...@@ -121,10 +106,6 @@ class PaymentRequestAddressEditCoordinatorTest
payment_request_test_util::CreateTestWebPaymentRequest(), payment_request_test_util::CreateTestWebPaymentRequest(),
browser_state(), web_state(), &personal_data_manager_); browser_state(), web_state(), &personal_data_manager_);
profile_comparator_ = std::make_unique<MockPaymentsProfileComparator>(
payment_request_->GetApplicationLocale(), *payment_request_.get());
payment_request_->SetProfileComparator(profile_comparator_.get());
test_region_data_loader_.set_synchronous_callback(true); test_region_data_loader_.set_synchronous_callback(true);
payment_request_->SetRegionDataLoader(&test_region_data_loader_); payment_request_->SetRegionDataLoader(&test_region_data_loader_);
} }
...@@ -135,9 +116,8 @@ class PaymentRequestAddressEditCoordinatorTest ...@@ -135,9 +116,8 @@ class PaymentRequestAddressEditCoordinatorTest
PaymentRequestUnitTestBase::TearDown(); PaymentRequestUnitTestBase::TearDown();
} }
MockTestPersonalDataManager personal_data_manager_; autofill::TestPersonalDataManager personal_data_manager_;
autofill::TestRegionDataLoader test_region_data_loader_; autofill::TestRegionDataLoader test_region_data_loader_;
std::unique_ptr<MockPaymentsProfileComparator> profile_comparator_;
std::unique_ptr<MockTestPaymentRequest> payment_request_; std::unique_ptr<MockTestPaymentRequest> payment_request_;
}; };
...@@ -209,15 +189,8 @@ TEST_F(PaymentRequestAddressEditCoordinatorTest, DidFinishCreating) { ...@@ -209,15 +189,8 @@ TEST_F(PaymentRequestAddressEditCoordinatorTest, DidFinishCreating) {
AddAutofillProfile(ProfileMatches("John Doe", "CA" /* Canada */, AddAutofillProfile(ProfileMatches("John Doe", "CA" /* Canada */,
"Quebec", "1 650-211-1111"))) "Quebec", "1 650-211-1111")))
.Times(1); .Times(1);
// Expect an autofill profile to be added to the PersonalDataManager. // No autofill profile should get updated in the PaymentRequest.
EXPECT_CALL(personal_data_manager_, EXPECT_CALL(*payment_request_, UpdateAutofillProfile(_)).Times(0);
AddProfile(ProfileMatches("John Doe", "CA" /* Canada */, "Quebec",
"1 650-211-1111")))
.Times(1);
// No autofill profile should get updated in the PersonalDataManager.
EXPECT_CALL(personal_data_manager_, UpdateProfile(_)).Times(0);
// No autofill profile should get invalidated in PaymentsProfileComparator.
EXPECT_CALL(*profile_comparator_, Invalidate(_)).Times(0);
// Call the controller delegate method. // Call the controller delegate method.
EXPECT_TRUE([base_view_controller.presentedViewController EXPECT_TRUE([base_view_controller.presentedViewController
...@@ -270,17 +243,10 @@ TEST_F(PaymentRequestAddressEditCoordinatorTest, DidFinishEditing) { ...@@ -270,17 +243,10 @@ TEST_F(PaymentRequestAddressEditCoordinatorTest, DidFinishEditing) {
// No autofill profile should get added to the PaymentRequest. // No autofill profile should get added to the PaymentRequest.
EXPECT_CALL(*payment_request_, AddAutofillProfile(_)).Times(0); EXPECT_CALL(*payment_request_, AddAutofillProfile(_)).Times(0);
// No autofill profile should get added to the PersonalDataManager. // Expect an autofill profile to be updated in the PaymentRequest.
EXPECT_CALL(personal_data_manager_, AddProfile(_)).Times(0); EXPECT_CALL(*payment_request_,
// Expect an autofill profile to be updated in the PersonalDataManager. UpdateAutofillProfile(ProfileMatches(
EXPECT_CALL(personal_data_manager_, "John Doe", "CA" /* Canada */, "Quebec", "1 650-211-1111")))
UpdateProfile(ProfileMatches("John Doe", "CA" /* Canada */,
"Quebec", "1 650-211-1111")))
.Times(1);
// Expect an autofill profile to be invalidated in PaymentsProfileComparator.
EXPECT_CALL(*profile_comparator_,
Invalidate(ProfileMatches("John Doe", "CA" /* Canada */, "Quebec",
"1 650-211-1111")))
.Times(1); .Times(1);
// Call the controller delegate method. // Call the controller delegate method.
......
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
#include "components/autofill/core/browser/personal_data_manager.h" #include "components/autofill/core/browser/personal_data_manager.h"
#include "components/autofill/core/browser/validation.h" #include "components/autofill/core/browser/validation.h"
#include "components/autofill/core/common/autofill_constants.h" #include "components/autofill/core/common/autofill_constants.h"
#include "components/payments/core/payments_profile_comparator.h"
#include "ios/chrome/browser/payments/payment_request.h" #include "ios/chrome/browser/payments/payment_request.h"
#import "ios/chrome/browser/ui/autofill/autofill_ui_type_util.h" #import "ios/chrome/browser/ui/autofill/autofill_ui_type_util.h"
#import "ios/chrome/browser/ui/payments/contact_info_edit_mediator.h" #import "ios/chrome/browser/ui/payments/contact_info_edit_mediator.h"
...@@ -88,9 +87,9 @@ using ::AutofillTypeFromAutofillUIType; ...@@ -88,9 +87,9 @@ using ::AutofillTypeFromAutofillUIType;
// Create an empty autofill profile. If a profile is being edited, copy over // Create an empty autofill profile. If a profile is being edited, copy over
// the information. // the information.
autofill::AutofillProfile profile = autofill::AutofillProfile profile =
self.profile ? *self.profile self.profile ? *self.profile : autofill::AutofillProfile();
: autofill::AutofillProfile(base::GenerateGUID(), // Set the origin, or override it if the autofill profile is being edited.
autofill::kSettingsOrigin); profile.set_origin(autofill::kSettingsOrigin);
for (EditorField* field in fields) { for (EditorField* field in fields) {
profile.SetInfo(autofill::AutofillType( profile.SetInfo(autofill::AutofillType(
...@@ -100,20 +99,12 @@ using ::AutofillTypeFromAutofillUIType; ...@@ -100,20 +99,12 @@ using ::AutofillTypeFromAutofillUIType;
} }
if (!self.profile) { if (!self.profile) {
self.paymentRequest->GetPersonalDataManager()->AddProfile(profile);
// Add the profile to the list of profiles in |self.paymentRequest|. // Add the profile to the list of profiles in |self.paymentRequest|.
self.profile = self.paymentRequest->AddAutofillProfile(profile); self.profile = self.paymentRequest->AddAutofillProfile(profile);
} else { } else {
// Override the origin.
profile.set_origin(autofill::kSettingsOrigin);
self.paymentRequest->GetPersonalDataManager()->UpdateProfile(profile);
// Cached profile must be invalidated once the profile is modified.
_paymentRequest->profile_comparator()->Invalidate(profile);
// Update the original profile instance that is being edited. // Update the original profile instance that is being edited.
*self.profile = profile; *self.profile = profile;
self.paymentRequest->UpdateAutofillProfile(profile);
} }
[self.delegate contactInfoEditCoordinator:self [self.delegate contactInfoEditCoordinator:self
......
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
#include "components/autofill/core/browser/autofill_test_utils.h" #include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/test_personal_data_manager.h" #include "components/autofill/core/browser/test_personal_data_manager.h"
#include "components/autofill/core/browser/test_region_data_loader.h" #include "components/autofill/core/browser/test_region_data_loader.h"
#include "components/payments/core/payments_profile_comparator.h"
#include "ios/chrome/browser/payments/payment_request_test_util.h" #include "ios/chrome/browser/payments/payment_request_test_util.h"
#include "ios/chrome/browser/payments/payment_request_unittest_base.h" #include "ios/chrome/browser/payments/payment_request_unittest_base.h"
#include "ios/chrome/browser/payments/test_payment_request.h" #include "ios/chrome/browser/payments/test_payment_request.h"
...@@ -33,21 +32,8 @@ ...@@ -33,21 +32,8 @@
#endif #endif
namespace { namespace {
class MockTestPersonalDataManager : public autofill::TestPersonalDataManager {
public:
MockTestPersonalDataManager() : TestPersonalDataManager() {}
MOCK_METHOD1(AddProfile, void(const autofill::AutofillProfile&));
MOCK_METHOD1(UpdateProfile, void(const autofill::AutofillProfile&));
};
class MockPaymentsProfileComparator using ::testing::_;
: public payments::PaymentsProfileComparator {
public:
MockPaymentsProfileComparator(const std::string& app_locale,
const payments::PaymentOptionsProvider& options)
: PaymentsProfileComparator(app_locale, options) {}
MOCK_METHOD1(Invalidate, void(const autofill::AutofillProfile&));
};
class MockTestPaymentRequest : public payments::TestPaymentRequest { class MockTestPaymentRequest : public payments::TestPaymentRequest {
public: public:
...@@ -61,6 +47,7 @@ class MockTestPaymentRequest : public payments::TestPaymentRequest { ...@@ -61,6 +47,7 @@ class MockTestPaymentRequest : public payments::TestPaymentRequest {
personal_data_manager) {} personal_data_manager) {}
MOCK_METHOD1(AddAutofillProfile, MOCK_METHOD1(AddAutofillProfile,
autofill::AutofillProfile*(const autofill::AutofillProfile&)); autofill::AutofillProfile*(const autofill::AutofillProfile&));
MOCK_METHOD1(UpdateAutofillProfile, void(const autofill::AutofillProfile&));
}; };
MATCHER_P3(ProfileMatches, name, email, phone_number, "") { MATCHER_P3(ProfileMatches, name, email, phone_number, "") {
...@@ -91,8 +78,6 @@ NSArray<EditorField*>* GetEditorFields() { ...@@ -91,8 +78,6 @@ NSArray<EditorField*>* GetEditorFields() {
required:YES], required:YES],
]; ];
} }
using ::testing::_;
} // namespace } // namespace
class PaymentRequestContactInfoEditCoordinatorTest class PaymentRequestContactInfoEditCoordinatorTest
...@@ -110,10 +95,6 @@ class PaymentRequestContactInfoEditCoordinatorTest ...@@ -110,10 +95,6 @@ class PaymentRequestContactInfoEditCoordinatorTest
payment_request_test_util::CreateTestWebPaymentRequest(), payment_request_test_util::CreateTestWebPaymentRequest(),
browser_state(), web_state(), &personal_data_manager_); browser_state(), web_state(), &personal_data_manager_);
profile_comparator_ = std::make_unique<MockPaymentsProfileComparator>(
payment_request_->GetApplicationLocale(), *payment_request_.get());
payment_request_->SetProfileComparator(profile_comparator_.get());
test_region_data_loader_.set_synchronous_callback(true); test_region_data_loader_.set_synchronous_callback(true);
payment_request_->SetRegionDataLoader(&test_region_data_loader_); payment_request_->SetRegionDataLoader(&test_region_data_loader_);
} }
...@@ -124,9 +105,8 @@ class PaymentRequestContactInfoEditCoordinatorTest ...@@ -124,9 +105,8 @@ class PaymentRequestContactInfoEditCoordinatorTest
PaymentRequestUnitTestBase::TearDown(); PaymentRequestUnitTestBase::TearDown();
} }
MockTestPersonalDataManager personal_data_manager_; autofill::TestPersonalDataManager personal_data_manager_;
autofill::TestRegionDataLoader test_region_data_loader_; autofill::TestRegionDataLoader test_region_data_loader_;
std::unique_ptr<MockPaymentsProfileComparator> profile_comparator_;
std::unique_ptr<MockTestPaymentRequest> payment_request_; std::unique_ptr<MockTestPaymentRequest> payment_request_;
}; };
...@@ -198,15 +178,8 @@ TEST_F(PaymentRequestContactInfoEditCoordinatorTest, DidFinishCreating) { ...@@ -198,15 +178,8 @@ TEST_F(PaymentRequestContactInfoEditCoordinatorTest, DidFinishCreating) {
AddAutofillProfile( AddAutofillProfile(
ProfileMatches("John Doe", "john@doe.com", "1 650-211-1111"))) ProfileMatches("John Doe", "john@doe.com", "1 650-211-1111")))
.Times(1); .Times(1);
// Expect an autofill profile to be added to the PersonalDataManager. // No autofill profile should get updated in the PaymentRequest.
EXPECT_CALL( EXPECT_CALL(*payment_request_, UpdateAutofillProfile(_)).Times(0);
personal_data_manager_,
AddProfile(ProfileMatches("John Doe", "john@doe.com", "1 650-211-1111")))
.Times(1);
// No autofill profile should get updated in the PersonalDataManager.
EXPECT_CALL(personal_data_manager_, UpdateProfile(_)).Times(0);
// No autofill profile should get invalidated in PaymentsProfileComparator.
EXPECT_CALL(*profile_comparator_, Invalidate(_)).Times(0);
// Call the controller delegate method. // Call the controller delegate method.
EXPECT_TRUE([base_view_controller.presentedViewController EXPECT_TRUE([base_view_controller.presentedViewController
...@@ -259,18 +232,11 @@ TEST_F(PaymentRequestContactInfoEditCoordinatorTest, DidFinishEditing) { ...@@ -259,18 +232,11 @@ TEST_F(PaymentRequestContactInfoEditCoordinatorTest, DidFinishEditing) {
// No autofill profile should get added to the PaymentRequest. // No autofill profile should get added to the PaymentRequest.
EXPECT_CALL(*payment_request_, AddAutofillProfile(_)).Times(0); EXPECT_CALL(*payment_request_, AddAutofillProfile(_)).Times(0);
// No autofill profile should get added to the PersonalDataManager. // Expect an autofill profile to be updated in the PaymentRequest.
EXPECT_CALL(personal_data_manager_, AddProfile(_)).Times(0); EXPECT_CALL(*payment_request_,
// Expect an autofill profile to be updated in the PersonalDataManager. UpdateAutofillProfile(
EXPECT_CALL(personal_data_manager_,
UpdateProfile(
ProfileMatches("John Doe", "john@doe.com", "1 650-211-1111"))) ProfileMatches("John Doe", "john@doe.com", "1 650-211-1111")))
.Times(1); .Times(1);
// Expect an autofill profile to be invalidated in PaymentsProfileComparator.
EXPECT_CALL(
*profile_comparator_,
Invalidate(ProfileMatches("John Doe", "john@doe.com", "1 650-211-1111")))
.Times(1);
// Call the controller delegate method. // Call the controller delegate method.
EXPECT_TRUE([base_view_controller.presentedViewController EXPECT_TRUE([base_view_controller.presentedViewController
......
...@@ -128,9 +128,10 @@ ...@@ -128,9 +128,10 @@
// Create an empty credit card. If a credit card is being edited, copy over // Create an empty credit card. If a credit card is being edited, copy over
// the information. // the information.
autofill::CreditCard creditCard = autofill::CreditCard creditCard =
_creditCard ? *_creditCard _creditCard ? *_creditCard : autofill::CreditCard();
: autofill::CreditCard(base::GenerateGUID(),
autofill::kSettingsOrigin); // Set the origin, or override it if the card is being edited.
creditCard.set_origin(autofill::kSettingsOrigin);
for (EditorField* field in fields) { for (EditorField* field in fields) {
if (field.autofillUIType == AutofillUITypeCreditCardExpDate) { if (field.autofillUIType == AutofillUITypeCreditCardExpDate) {
...@@ -159,25 +160,15 @@ ...@@ -159,25 +160,15 @@
} }
if (!_creditCard) { if (!_creditCard) {
if (saveCreditCard)
_paymentRequest->GetPersonalDataManager()->AddCreditCard(creditCard);
// Add the credit card to the list of payment methods in |_paymentRequest|. // Add the credit card to the list of payment methods in |_paymentRequest|.
_paymentMethod = _paymentRequest->AddAutofillPaymentInstrument(creditCard); if (saveCreditCard) {
_paymentMethod =
_paymentRequest->CreateAndAddAutofillPaymentInstrument(creditCard);
}
} else { } else {
// Override the origin.
creditCard.set_origin(autofill::kSettingsOrigin);
// Update the original credit card instance that is being edited. // Update the original credit card instance that is being edited.
*_creditCard = creditCard; *_creditCard = creditCard;
_paymentRequest->UpdateAutofillPaymentInstrument(creditCard);
if (autofill::IsCreditCardLocal(creditCard)) {
_paymentRequest->GetPersonalDataManager()->UpdateCreditCard(creditCard);
} else {
// Update server credit card's billing address.
_paymentRequest->GetPersonalDataManager()->UpdateServerCardMetadata(
creditCard);
}
} }
[_delegate creditCardEditCoordinator:self [_delegate creditCardEditCoordinator:self
......
...@@ -32,12 +32,8 @@ ...@@ -32,12 +32,8 @@
#endif #endif
namespace { namespace {
class MockTestPersonalDataManager : public autofill::TestPersonalDataManager {
public: using ::testing::_;
MockTestPersonalDataManager() : TestPersonalDataManager() {}
MOCK_METHOD1(AddCreditCard, void(const autofill::CreditCard&));
MOCK_METHOD1(UpdateCreditCard, void(const autofill::CreditCard&));
};
class MockPaymentRequest : public payments::TestPaymentRequest { class MockPaymentRequest : public payments::TestPaymentRequest {
public: public:
...@@ -50,8 +46,10 @@ class MockPaymentRequest : public payments::TestPaymentRequest { ...@@ -50,8 +46,10 @@ class MockPaymentRequest : public payments::TestPaymentRequest {
web_state, web_state,
personal_data_manager) {} personal_data_manager) {}
MOCK_METHOD1( MOCK_METHOD1(
AddAutofillPaymentInstrument, CreateAndAddAutofillPaymentInstrument,
payments::AutofillPaymentInstrument*(const autofill::CreditCard&)); payments::AutofillPaymentInstrument*(const autofill::CreditCard&));
MOCK_METHOD1(UpdateAutofillPaymentInstrument,
void(const autofill::CreditCard&));
}; };
MATCHER_P5(CreditCardMatches, MATCHER_P5(CreditCardMatches,
...@@ -109,8 +107,6 @@ NSArray<EditorField*>* GetEditorFields(bool save_card) { ...@@ -109,8 +107,6 @@ NSArray<EditorField*>* GetEditorFields(bool save_card) {
required:YES], required:YES],
]; ];
} }
using ::testing::_;
} // namespace } // namespace
class PaymentRequestCreditCardEditCoordinatorTest class PaymentRequestCreditCardEditCoordinatorTest
...@@ -129,7 +125,7 @@ class PaymentRequestCreditCardEditCoordinatorTest ...@@ -129,7 +125,7 @@ class PaymentRequestCreditCardEditCoordinatorTest
void TearDown() override { PaymentRequestUnitTestBase::TearDown(); } void TearDown() override { PaymentRequestUnitTestBase::TearDown(); }
MockTestPersonalDataManager personal_data_manager_; autofill::TestPersonalDataManager personal_data_manager_;
std::unique_ptr<MockPaymentRequest> payment_request_; std::unique_ptr<MockPaymentRequest> payment_request_;
}; };
...@@ -199,16 +195,12 @@ TEST_F(PaymentRequestCreditCardEditCoordinatorTest, DidFinishCreatingWithSave) { ...@@ -199,16 +195,12 @@ TEST_F(PaymentRequestCreditCardEditCoordinatorTest, DidFinishCreatingWithSave) {
// Expect a payment method to be added to the PaymentRequest. // Expect a payment method to be added to the PaymentRequest.
EXPECT_CALL(*payment_request_, EXPECT_CALL(*payment_request_,
AddAutofillPaymentInstrument(CreditCardMatches( CreateAndAddAutofillPaymentInstrument(CreditCardMatches(
"4111111111111111", "John Doe", "12", "2090", "12345"))) "4111111111111111", "John Doe", "12", "2090", "12345")))
.Times(1); .Times(1);
// Expect a payment method to be added to the PersonalDataManager.
EXPECT_CALL(personal_data_manager_, // No payment method should get updated in the PaymentRequest.
AddCreditCard(CreditCardMatches("4111111111111111", "John Doe", EXPECT_CALL(*payment_request_, UpdateAutofillPaymentInstrument(_)).Times(0);
"12", "2090", "12345")))
.Times(1);
// No payment method should get updated in the PersonalDataManager.
EXPECT_CALL(personal_data_manager_, UpdateCreditCard(_)).Times(0);
// Call the controller delegate method. // Call the controller delegate method.
EXPECT_TRUE([base_view_controller.presentedViewController EXPECT_TRUE([base_view_controller.presentedViewController
...@@ -256,15 +248,11 @@ TEST_F(PaymentRequestCreditCardEditCoordinatorTest, DidFinishCreatingNoSave) { ...@@ -256,15 +248,11 @@ TEST_F(PaymentRequestCreditCardEditCoordinatorTest, DidFinishCreatingNoSave) {
base::test::ios::SpinRunLoopWithMaxDelay(base::TimeDelta::FromSecondsD(1.0)); base::test::ios::SpinRunLoopWithMaxDelay(base::TimeDelta::FromSecondsD(1.0));
EXPECT_NE(nil, base_view_controller.presentedViewController); EXPECT_NE(nil, base_view_controller.presentedViewController);
// Expect a payment method to be added to the PaymentRequest. // No payment method should get added to the PaymentRequest.
EXPECT_CALL(*payment_request_, EXPECT_CALL(*payment_request_, CreateAndAddAutofillPaymentInstrument(_))
AddAutofillPaymentInstrument(CreditCardMatches( .Times(0);
"4111111111111111", "John Doe", "12", "2090", "12345"))) // No payment method should get updated in the PaymentRequest.
.Times(1); EXPECT_CALL(*payment_request_, UpdateAutofillPaymentInstrument(_)).Times(0);
// No payment method should get added to the PersonalDataManager.
EXPECT_CALL(personal_data_manager_, AddCreditCard(_)).Times(0);
// No payment method should get updated in the PersonalDataManager.
EXPECT_CALL(personal_data_manager_, UpdateCreditCard(_)).Times(0);
// Call the controller delegate method. // Call the controller delegate method.
EXPECT_TRUE([base_view_controller.presentedViewController EXPECT_TRUE([base_view_controller.presentedViewController
...@@ -318,13 +306,12 @@ TEST_F(PaymentRequestCreditCardEditCoordinatorTest, DidFinishEditing) { ...@@ -318,13 +306,12 @@ TEST_F(PaymentRequestCreditCardEditCoordinatorTest, DidFinishEditing) {
EXPECT_NE(nil, base_view_controller.presentedViewController); EXPECT_NE(nil, base_view_controller.presentedViewController);
// No payment method should get added to the PaymentRequest. // No payment method should get added to the PaymentRequest.
EXPECT_CALL(*payment_request_, AddAutofillPaymentInstrument(_)).Times(0); EXPECT_CALL(*payment_request_, CreateAndAddAutofillPaymentInstrument(_))
// No payment method should get added to the PersonalDataManager. .Times(0);
EXPECT_CALL(personal_data_manager_, AddCreditCard(_)).Times(0); // No payment method should get updated in the PaymentRequest.
// Expect a payment method to be updated in the PersonalDataManager. EXPECT_CALL(*payment_request_,
EXPECT_CALL(personal_data_manager_, UpdateAutofillPaymentInstrument(CreditCardMatches(
UpdateCreditCard(CreditCardMatches("4111111111111111", "John Doe", "4111111111111111", "John Doe", "12", "2090", "12345")))
"12", "2090", "12345")))
.Times(1); .Times(1);
// Call the controller delegate method. // Call the controller delegate method.
......
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