Commit 6baa6c81 authored by Antonio Gomes's avatar Antonio Gomes Committed by Commit Bot

[s13n][ios] Convert payment_request.mm to IdentityManager

BUG=889871

Change-Id: I52a65e016b3e243af3356991eebe8621d9636f03
Reviewed-on: https://chromium-review.googlesource.com/c/1316930
Commit-Queue: Antonio Gomes <tonikitoo@igalia.com>
Reviewed-by: default avatarColin Blundell <blundell@chromium.org>
Reviewed-by: default avatarMihai Sardarescu <msarda@chromium.org>
Reviewed-by: default avatarMoe Ahmadi <mahmadi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#605367}
parent 78e6d870
......@@ -48,6 +48,7 @@ source_set("payments") {
"//ios/chrome/browser/signin",
"//ios/web",
"//net",
"//services/identity/public/cpp:cpp",
"//ui/base",
"//url",
]
......@@ -121,5 +122,6 @@ source_set("test_support") {
"//ios/web",
"//ios/web/public/test",
"//ios/web/public/test/fakes",
"//services/identity/public/cpp:cpp",
]
}
......@@ -26,7 +26,6 @@
#include "components/payments/core/payment_shipping_option.h"
#include "components/payments/core/web_payment_request.h"
#include "components/prefs/pref_service.h"
#include "components/signin/core/browser/signin_manager.h"
#include "ios/chrome/browser/application_context.h"
#include "ios/chrome/browser/autofill/address_normalizer_factory.h"
#include "ios/chrome/browser/autofill/validation_rules_storage_factory.h"
......@@ -34,8 +33,9 @@
#import "ios/chrome/browser/metrics/ukm_url_recorder.h"
#import "ios/chrome/browser/payments/ios_payment_instrument.h"
#import "ios/chrome/browser/payments/payment_request_util.h"
#include "ios/chrome/browser/signin/signin_manager_factory.h"
#include "ios/chrome/browser/signin/identity_manager_factory.h"
#include "ios/web/public/web_state/web_state.h"
#include "services/identity/public/cpp/identity_manager.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "third_party/libaddressinput/chromium/chrome_metadata_source.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h"
......@@ -179,10 +179,10 @@ ukm::UkmRecorder* PaymentRequest::GetUkmRecorder() {
}
std::string PaymentRequest::GetAuthenticatedEmail() const {
const SigninManager* signin_manager =
ios::SigninManagerFactory::GetForBrowserStateIfExists(browser_state_);
if (signin_manager && signin_manager->IsAuthenticated())
return signin_manager->GetAuthenticatedAccountInfo().email;
const identity::IdentityManager* identity_manager =
IdentityManagerFactory::GetForBrowserStateIfExists(browser_state_);
if (identity_manager && identity_manager->HasPrimaryAccount())
return identity_manager->GetPrimaryAccountInfo().email;
else
return std::string();
}
......
......@@ -25,7 +25,11 @@ class AutofillProfile;
class CreditCard;
} // namespace autofill
class SigninManager;
namespace identity {
class IdentityTestEnvironment;
} // namespace identity
class IdentityTestEnvironmentChromeBrowserStateAdaptor;
// Base class for various payment request related unit tests. This purposely
// does not inherit from PlatformTest (testing::Test) so that it can be used
......@@ -45,7 +49,7 @@ class PaymentRequestUnitTestBase {
void AddAutofillProfile(const autofill::AutofillProfile& profile);
void AddCreditCard(const autofill::CreditCard& card);
SigninManager* GetSigninManager();
identity::IdentityTestEnvironment* identity_test_env();
payments::TestPaymentRequest* payment_request() {
return payment_request_.get();
......@@ -72,6 +76,8 @@ class PaymentRequestUnitTestBase {
autofill::TestPersonalDataManager personal_data_manager_;
std::unique_ptr<TestChromeBrowserState> chrome_browser_state_;
std::unique_ptr<payments::TestPaymentRequest> payment_request_;
std::unique_ptr<IdentityTestEnvironmentChromeBrowserStateAdaptor>
identity_test_env_adaptor_;
};
#endif // IOS_CHROME_BROWSER_PAYMENTS_PAYMENT_REQUEST_UNITTEST_BASE_H_
......@@ -6,10 +6,9 @@
#include "components/payments/core/payment_prefs.h"
#include "components/payments/core/payments_test_util.h"
#include "components/signin/core/browser/signin_manager.h"
#include "ios/chrome/browser/payments/payment_request_test_util.h"
#include "ios/chrome/browser/signin/fake_signin_manager_builder.h"
#include "ios/chrome/browser/signin/signin_manager_factory.h"
#include "ios/chrome/browser/signin/identity_test_environment_chrome_browser_state_adaptor.h"
#include "services/identity/public/cpp/identity_manager.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
......@@ -21,13 +20,14 @@ PaymentRequestUnitTestBase::PaymentRequestUnitTestBase()
PaymentRequestUnitTestBase::~PaymentRequestUnitTestBase() {}
void PaymentRequestUnitTestBase::DoSetUp() {
TestChromeBrowserState::Builder test_cbs_builder;
test_cbs_builder.AddTestingFactory(
ios::SigninManagerFactory::GetInstance(),
base::BindRepeating(&ios::BuildFakeSigninManager));
chrome_browser_state_ = test_cbs_builder.Build();
chrome_browser_state_ = IdentityTestEnvironmentChromeBrowserStateAdaptor::
CreateChromeBrowserStateForIdentityTestEnvironment();
web_state_.SetBrowserState(chrome_browser_state_.get());
personal_data_manager_.SetPrefService(pref_service_.get());
identity_test_env_adaptor_ =
std::make_unique<IdentityTestEnvironmentChromeBrowserStateAdaptor>(
chrome_browser_state_.get());
}
void PaymentRequestUnitTestBase::DoTearDown() {
......@@ -51,7 +51,7 @@ void PaymentRequestUnitTestBase::AddCreditCard(
personal_data_manager_.AddCreditCard(card);
}
SigninManager* PaymentRequestUnitTestBase::GetSigninManager() {
return ios::SigninManagerFactory::GetForBrowserState(
chrome_browser_state_.get());
identity::IdentityTestEnvironment*
PaymentRequestUnitTestBase::identity_test_env() {
return identity_test_env_adaptor_->identity_test_env();
}
......@@ -61,6 +61,13 @@ identity::IdentityManager* IdentityManagerFactory::GetForBrowserState(
GetInstance()->GetServiceForBrowserState(browser_state, true));
}
// static
identity::IdentityManager* IdentityManagerFactory::GetForBrowserStateIfExists(
ios::ChromeBrowserState* browser_state) {
return static_cast<IdentityManagerWrapper*>(
GetInstance()->GetServiceForBrowserState(browser_state, false));
}
// static
IdentityManagerFactory* IdentityManagerFactory::GetInstance() {
return base::Singleton<IdentityManagerFactory>::get();
......
......@@ -22,6 +22,8 @@ class IdentityManagerFactory : public BrowserStateKeyedServiceFactory {
public:
static identity::IdentityManager* GetForBrowserState(
ios::ChromeBrowserState* browser_state);
static identity::IdentityManager* GetForBrowserStateIfExists(
ios::ChromeBrowserState* browser_state);
// Returns an instance of the IdentityManagerFactory singleton.
static IdentityManagerFactory* GetInstance();
......
......@@ -242,6 +242,8 @@ source_set("unit_tests") {
"//ios/web",
"//ios/web/public/test",
"//ios/web/public/test/fakes",
"//services/identity/public/cpp:cpp",
"//services/identity/public/cpp:test_support",
"//testing/gmock",
"//testing/gtest",
"//third_party/libaddressinput:strings_grit",
......
......@@ -16,7 +16,6 @@
#include "components/payments/core/payment_shipping_option.h"
#include "components/payments/core/strings_util.h"
#include "components/prefs/pref_service.h"
#include "components/signin/core/browser/signin_manager.h"
#include "components/strings/grit/components_strings.h"
#import "ios/chrome/browser/payments/payment_request_unittest_base.h"
#include "ios/chrome/browser/payments/payment_request_util.h"
......@@ -27,6 +26,8 @@
#import "ios/chrome/browser/ui/payments/cells/payment_method_item.h"
#import "ios/chrome/browser/ui/payments/cells/payments_text_item.h"
#import "ios/chrome/browser/ui/payments/cells/price_item.h"
#include "services/identity/public/cpp/identity_manager.h"
#include "services/identity/public/cpp/identity_test_environment.h"
#include "testing/platform_test.h"
#include "ui/base/l10n/l10n_util.h"
......@@ -415,9 +416,9 @@ TEST_F(PaymentRequestMediatorTest, TestFooterItem) {
false);
// Make sure the user is signed out.
if (GetSigninManager()->IsAuthenticated()) {
GetSigninManager()->SignOut(signin_metrics::SIGNOUT_TEST,
signin_metrics::SignoutDelete::IGNORE_METRIC);
auto* identity_manager = identity_test_env()->identity_manager();
if (identity_manager->HasPrimaryAccount()) {
identity_test_env()->ClearPrimaryAccount();
}
// Footer item should be of type CollectionViewFooterItem.
......@@ -430,8 +431,7 @@ TEST_F(PaymentRequestMediatorTest, TestFooterItem) {
IDS_PAYMENTS_CARD_AND_ADDRESS_SETTINGS_SIGNED_OUT)]);
// Fake a signed in user.
GetSigninManager()->SetAuthenticatedAccountInfo("12345",
"username@example.com");
identity_test_env()->SetPrimaryAccount("username@example.com");
item = [mediator() footerItem];
footer_item = base::mac::ObjCCastStrict<CollectionViewFooterItem>(item);
......@@ -451,8 +451,7 @@ TEST_F(PaymentRequestMediatorTest, TestFooterItem) {
IDS_PAYMENTS_CARD_AND_ADDRESS_SETTINGS)]);
// Sign the user out.
GetSigninManager()->SignOut(signin_metrics::SIGNOUT_TEST,
signin_metrics::SignoutDelete::IGNORE_METRIC);
identity_test_env()->ClearPrimaryAccount();
// The signed in state has no effect on the footer text if the first
// transaction has completed.
......
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