Commit 7e80adb6 authored by Antonio Gomes's avatar Antonio Gomes Committed by Commit Bot

[s13n] Extend IdentityTestEnvironmentProfileAdaptor with APIs needed by identity_apitest.cc

This CL extends IdentityTestEnvironment{Profile,ChromeBrowserState}Adaptor
classes with a handful set of APIs needed for identity_apitest.cc, and possibly
some other conversions.

In summary:
- It adds a method that allows setting the testing factories that
 identity::IdentityTestEnvironment requires explicitly on a Profile
 that is passed to it.
- It extends all methods that (in)directly call
  GetIdentityTestEnvironmentFactories with the possibility to use
  or not the "fake URL fetcher" for Gaia's Cookie Manager factory.

BUG=906618

Change-Id: I54985c0743ed0ae2528140fac655fcf1baeb9531
Reviewed-on: https://chromium-review.googlesource.com/c/1346609
Commit-Queue: Antonio Gomes <tonikitoo@igalia.com>
Reviewed-by: default avatarMihai Sardarescu <msarda@chromium.org>
Reviewed-by: default avatarColin Blundell <blundell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#610836}
parent 9c452157
...@@ -442,8 +442,10 @@ class IdentityTestWithSignin : public AsyncExtensionBrowserTest { ...@@ -442,8 +442,10 @@ class IdentityTestWithSignin : public AsyncExtensionBrowserTest {
ProfileOAuth2TokenServiceFactory::GetInstance()->SetTestingFactory( ProfileOAuth2TokenServiceFactory::GetInstance()->SetTestingFactory(
context, base::BindRepeating(&BuildFakeProfileOAuth2TokenService)); context, base::BindRepeating(&BuildFakeProfileOAuth2TokenService));
GaiaCookieManagerServiceFactory::GetInstance()->SetTestingFactory( GaiaCookieManagerServiceFactory::GetInstance()->SetTestingFactory(
context, base::BindRepeating( context,
&BuildFakeGaiaCookieManagerServiceNoFakeUrlFetcher)); base::BindRepeating(
&BuildFakeGaiaCookieManagerServiceWithOptions,
/*create_fake_url_loader_factory_for_cookie_requests=*/false));
// Ensure that AccountFetcherService is (1) created at all and (2) created // Ensure that AccountFetcherService is (1) created at all and (2) created
// early enough for it to observe the Profile initialization process and // early enough for it to observe the Profile initialization process and
......
...@@ -11,27 +11,18 @@ ...@@ -11,27 +11,18 @@
#include "components/signin/core/browser/fake_gaia_cookie_manager_service.h" #include "components/signin/core/browser/fake_gaia_cookie_manager_service.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h" #include "components/signin/core/browser/profile_oauth2_token_service.h"
namespace {
std::unique_ptr<KeyedService> BuildFakeGaiaCookieManagerServiceImpl(
content::BrowserContext* context,
bool use_fake_url_fetcher) {
Profile* profile = Profile::FromBrowserContext(context);
return std::make_unique<FakeGaiaCookieManagerService>(
ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
ChromeSigninClientFactory::GetForProfile(profile), use_fake_url_fetcher);
}
} // namespace
std::unique_ptr<KeyedService> BuildFakeGaiaCookieManagerService( std::unique_ptr<KeyedService> BuildFakeGaiaCookieManagerService(
content::BrowserContext* context) { content::BrowserContext* context) {
return BuildFakeGaiaCookieManagerServiceImpl(context, return BuildFakeGaiaCookieManagerServiceWithOptions(
/*use_fake_url_fetcher=*/true); /*create_fake_url_loader_factory_for_cookie_requests=*/true, context);
} }
std::unique_ptr<KeyedService> BuildFakeGaiaCookieManagerServiceNoFakeUrlFetcher( std::unique_ptr<KeyedService> BuildFakeGaiaCookieManagerServiceWithOptions(
bool create_fake_url_loader_factory_for_cookie_requests,
content::BrowserContext* context) { content::BrowserContext* context) {
return BuildFakeGaiaCookieManagerServiceImpl(context, Profile* profile = Profile::FromBrowserContext(context);
/*use_fake_url_fetcher=*/false); return std::make_unique<FakeGaiaCookieManagerService>(
ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
ChromeSigninClientFactory::GetForProfile(profile),
create_fake_url_loader_factory_for_cookie_requests);
} }
...@@ -17,7 +17,8 @@ class BrowserContext; ...@@ -17,7 +17,8 @@ class BrowserContext;
std::unique_ptr<KeyedService> BuildFakeGaiaCookieManagerService( std::unique_ptr<KeyedService> BuildFakeGaiaCookieManagerService(
content::BrowserContext* context); content::BrowserContext* context);
std::unique_ptr<KeyedService> BuildFakeGaiaCookieManagerServiceNoFakeUrlFetcher( std::unique_ptr<KeyedService> BuildFakeGaiaCookieManagerServiceWithOptions(
bool create_fake_url_loader_factory_for_cookie_requests,
content::BrowserContext* context); content::BrowserContext* context);
#endif // CHROME_BROWSER_SIGNIN_FAKE_GAIA_COOKIE_MANAGER_SERVICE_BUILDER_H_ #endif // CHROME_BROWSER_SIGNIN_FAKE_GAIA_COOKIE_MANAGER_SERVICE_BUILDER_H_
...@@ -15,13 +15,16 @@ ...@@ -15,13 +15,16 @@
namespace { namespace {
TestingProfile::TestingFactories GetIdentityTestEnvironmentFactories() { TestingProfile::TestingFactories GetIdentityTestEnvironmentFactories(
return {{GaiaCookieManagerServiceFactory::GetInstance(), bool create_fake_url_loader_factory_for_cookie_requests = true) {
base::BindRepeating(&BuildFakeGaiaCookieManagerService)}, return {
{ProfileOAuth2TokenServiceFactory::GetInstance(), {GaiaCookieManagerServiceFactory::GetInstance(),
base::BindRepeating(&BuildFakeProfileOAuth2TokenService)}, base::BindRepeating(&BuildFakeGaiaCookieManagerServiceWithOptions,
{SigninManagerFactory::GetInstance(), create_fake_url_loader_factory_for_cookie_requests)},
base::BindRepeating(&BuildFakeSigninManagerForTesting)}}; {ProfileOAuth2TokenServiceFactory::GetInstance(),
base::BindRepeating(&BuildFakeProfileOAuth2TokenService)},
{SigninManagerFactory::GetInstance(),
base::BindRepeating(&BuildFakeSigninManagerForTesting)}};
} }
} // namespace } // namespace
...@@ -36,31 +39,47 @@ std::unique_ptr<TestingProfile> IdentityTestEnvironmentProfileAdaptor:: ...@@ -36,31 +39,47 @@ std::unique_ptr<TestingProfile> IdentityTestEnvironmentProfileAdaptor::
// static // static
std::unique_ptr<TestingProfile> std::unique_ptr<TestingProfile>
IdentityTestEnvironmentProfileAdaptor::CreateProfileForIdentityTestEnvironment( IdentityTestEnvironmentProfileAdaptor::CreateProfileForIdentityTestEnvironment(
const TestingProfile::TestingFactories& input_factories) { const TestingProfile::TestingFactories& input_factories,
bool create_fake_url_loader_factory_for_cookie_requests) {
TestingProfile::Builder builder; TestingProfile::Builder builder;
for (auto& input_factory : input_factories) { for (auto& input_factory : input_factories) {
builder.AddTestingFactory(input_factory.first, input_factory.second); builder.AddTestingFactory(input_factory.first, input_factory.second);
} }
return CreateProfileForIdentityTestEnvironment(builder); return CreateProfileForIdentityTestEnvironment(
builder, create_fake_url_loader_factory_for_cookie_requests);
} }
// static // static
std::unique_ptr<TestingProfile> std::unique_ptr<TestingProfile>
IdentityTestEnvironmentProfileAdaptor::CreateProfileForIdentityTestEnvironment( IdentityTestEnvironmentProfileAdaptor::CreateProfileForIdentityTestEnvironment(
TestingProfile::Builder& builder) { TestingProfile::Builder& builder,
for (auto& identity_factory : GetIdentityTestEnvironmentFactories()) { bool create_fake_url_loader_factory_for_cookie_requests) {
for (auto& identity_factory : GetIdentityTestEnvironmentFactories(
create_fake_url_loader_factory_for_cookie_requests)) {
builder.AddTestingFactory(identity_factory.first, identity_factory.second); builder.AddTestingFactory(identity_factory.first, identity_factory.second);
} }
return builder.Build(); return builder.Build();
} }
// static
void IdentityTestEnvironmentProfileAdaptor::
SetIdentityTestEnvironmentFactoriesOnBrowserContext(
content::BrowserContext* context,
bool create_fake_url_loader_factory_for_cookie_requests) {
for (const auto& factory_pair : GetIdentityTestEnvironmentFactories(
create_fake_url_loader_factory_for_cookie_requests)) {
factory_pair.first->SetTestingFactory(context, factory_pair.second);
}
}
// static // static
void IdentityTestEnvironmentProfileAdaptor:: void IdentityTestEnvironmentProfileAdaptor::
AppendIdentityTestEnvironmentFactories( AppendIdentityTestEnvironmentFactories(
TestingProfile::TestingFactories* factories_to_append_to) { TestingProfile::TestingFactories* factories_to_append_to,
bool create_fake_url_loader_factory_for_cookie_requests) {
TestingProfile::TestingFactories identity_factories = TestingProfile::TestingFactories identity_factories =
GetIdentityTestEnvironmentFactories(); GetIdentityTestEnvironmentFactories();
factories_to_append_to->insert(factories_to_append_to->end(), factories_to_append_to->insert(factories_to_append_to->end(),
......
...@@ -25,16 +25,31 @@ class IdentityTestEnvironmentProfileAdaptor { ...@@ -25,16 +25,31 @@ class IdentityTestEnvironmentProfileAdaptor {
CreateProfileForIdentityTestEnvironment(); CreateProfileForIdentityTestEnvironment();
// Like the above, but additionally configures the returned Profile with // Like the above, but additionally configures the returned Profile with
// |input_factories|. // |input_factories|. By default, internally constructs a
// TestURLLoaderFactory to use for cookie-related network requests. If this
// isn't desired (e.g., because the test is already using a
// TestURLLoaderFactory), set
// |create_fake_url_loader_factory_for_cookie_requests| to false.
static std::unique_ptr<TestingProfile> static std::unique_ptr<TestingProfile>
CreateProfileForIdentityTestEnvironment( CreateProfileForIdentityTestEnvironment(
const TestingProfile::TestingFactories& input_factories); const TestingProfile::TestingFactories& input_factories,
bool create_fake_url_loader_factory_for_cookie_requests = true);
// Creates and returns a TestingProfile that has been configured with the // Creates and returns a TestingProfile that has been configured with the
// given |builder| and the set of testing factories that // given |builder| and the set of testing factories that
// IdentityTestEnvironment requires. // IdentityTestEnvironment requires.
// See the above variant for comments on common parameters.
static std::unique_ptr<TestingProfile> static std::unique_ptr<TestingProfile>
CreateProfileForIdentityTestEnvironment(TestingProfile::Builder& builder); CreateProfileForIdentityTestEnvironment(
TestingProfile::Builder& builder,
bool create_fake_url_loader_factory_for_cookie_requests = true);
// Sets the testing factories that identity::IdentityTestEnvironment
// requires explicitly on a Profile that is passed to it.
// See the above variant for comments on common parameters.
static void SetIdentityTestEnvironmentFactoriesOnBrowserContext(
content::BrowserContext* browser_context,
bool create_fake_url_loader_factory_for_cookie_requests = true);
// Appends the set of testing factories that identity::IdentityTestEnvironment // Appends the set of testing factories that identity::IdentityTestEnvironment
// requires to |factories_to_append_to|, which should be the set of testing // requires to |factories_to_append_to|, which should be the set of testing
...@@ -43,8 +58,11 @@ class IdentityTestEnvironmentProfileAdaptor { ...@@ -43,8 +58,11 @@ class IdentityTestEnvironmentProfileAdaptor {
// API is primarily for use in tests that do not create the TestingProfile // API is primarily for use in tests that do not create the TestingProfile
// internally but rather simply supply the set of TestingFactories to some // internally but rather simply supply the set of TestingFactories to some
// external facility (e.g., a superclass). // external facility (e.g., a superclass).
// See CreateProfileForIdentityTestEnvironment() for comments on common
// parameters.
static void AppendIdentityTestEnvironmentFactories( static void AppendIdentityTestEnvironmentFactories(
TestingProfile::TestingFactories* factories_to_append_to); TestingProfile::TestingFactories* factories_to_append_to,
bool create_fake_url_loader_factory_for_cookie_requests = true);
// Constructs an adaptor that associates an IdentityTestEnvironment instance // Constructs an adaptor that associates an IdentityTestEnvironment instance
// with |profile| via the relevant backing objects. Note that // with |profile| via the relevant backing objects. Note that
......
...@@ -132,13 +132,15 @@ class DiceSigninUiUtilTest : public BrowserWithTestWindowTest { ...@@ -132,13 +132,15 @@ class DiceSigninUiUtilTest : public BrowserWithTestWindowTest {
// BrowserWithTestWindowTest: // BrowserWithTestWindowTest:
TestingProfile::TestingFactories GetTestingFactories() override { TestingProfile::TestingFactories GetTestingFactories() override {
return {{SigninManagerFactory::GetInstance(), return {
base::BindRepeating(&BuildFakeSigninManagerForTesting)}, {SigninManagerFactory::GetInstance(),
{ProfileOAuth2TokenServiceFactory::GetInstance(), base::BindRepeating(&BuildFakeSigninManagerForTesting)},
base::BindRepeating(&BuildFakeProfileOAuth2TokenService)}, {ProfileOAuth2TokenServiceFactory::GetInstance(),
{GaiaCookieManagerServiceFactory::GetInstance(), base::BindRepeating(&BuildFakeProfileOAuth2TokenService)},
base::BindRepeating( {GaiaCookieManagerServiceFactory::GetInstance(),
&BuildFakeGaiaCookieManagerServiceNoFakeUrlFetcher)}}; base::BindRepeating(
&BuildFakeGaiaCookieManagerServiceWithOptions,
/*create_fake_url_loader_factory_for_cookie_requests=*/false)}};
} }
// BrowserWithTestWindowTest: // BrowserWithTestWindowTest:
......
...@@ -13,10 +13,19 @@ ...@@ -13,10 +13,19 @@
std::unique_ptr<KeyedService> BuildFakeGaiaCookieManagerService( std::unique_ptr<KeyedService> BuildFakeGaiaCookieManagerService(
web::BrowserState* browser_state) { web::BrowserState* browser_state) {
return BuildFakeGaiaCookieManagerServiceWithOptions(
/*create_fake_url_loader_factory_for_cookie_requests=*/true,
browser_state);
}
std::unique_ptr<KeyedService> BuildFakeGaiaCookieManagerServiceWithOptions(
bool create_fake_url_loader_factory_for_cookie_requests,
web::BrowserState* browser_state) {
ios::ChromeBrowserState* chrome_browser_state = ios::ChromeBrowserState* chrome_browser_state =
ios::ChromeBrowserState::FromBrowserState(browser_state); ios::ChromeBrowserState::FromBrowserState(browser_state);
return std::make_unique<FakeGaiaCookieManagerService>( return std::make_unique<FakeGaiaCookieManagerService>(
ProfileOAuth2TokenServiceFactory::GetForBrowserState( ProfileOAuth2TokenServiceFactory::GetForBrowserState(
chrome_browser_state), chrome_browser_state),
SigninClientFactory::GetForBrowserState(chrome_browser_state)); SigninClientFactory::GetForBrowserState(chrome_browser_state),
create_fake_url_loader_factory_for_cookie_requests);
} }
...@@ -17,4 +17,8 @@ class BrowserState; ...@@ -17,4 +17,8 @@ class BrowserState;
std::unique_ptr<KeyedService> BuildFakeGaiaCookieManagerService( std::unique_ptr<KeyedService> BuildFakeGaiaCookieManagerService(
web::BrowserState* browser_state); web::BrowserState* browser_state);
std::unique_ptr<KeyedService> BuildFakeGaiaCookieManagerServiceWithOptions(
bool create_fake_url_loader_factory_for_cookie_requests,
web::BrowserState* browser_state);
#endif // IOS_CHROME_BROWSER_SIGNIN_FAKE_GAIA_COOKIE_MANAGER_SERVICE_BUILDER_H_ #endif // IOS_CHROME_BROWSER_SIGNIN_FAKE_GAIA_COOKIE_MANAGER_SERVICE_BUILDER_H_
...@@ -15,13 +15,16 @@ ...@@ -15,13 +15,16 @@
namespace { namespace {
TestChromeBrowserState::TestingFactories GetIdentityTestEnvironmentFactories() { TestChromeBrowserState::TestingFactories GetIdentityTestEnvironmentFactories(
return {{ios::GaiaCookieManagerServiceFactory::GetInstance(), bool create_fake_url_loader_factory_for_cookie_requests = true) {
base::BindRepeating(&BuildFakeGaiaCookieManagerService)}, return {
{ProfileOAuth2TokenServiceFactory::GetInstance(), {ios::GaiaCookieManagerServiceFactory::GetInstance(),
base::BindRepeating(&BuildFakeOAuth2TokenService)}, base::BindRepeating(&BuildFakeGaiaCookieManagerServiceWithOptions,
{ios::SigninManagerFactory::GetInstance(), create_fake_url_loader_factory_for_cookie_requests)},
base::BindRepeating(&ios::BuildFakeSigninManager)}}; {ProfileOAuth2TokenServiceFactory::GetInstance(),
base::BindRepeating(&BuildFakeOAuth2TokenService)},
{ios::SigninManagerFactory::GetInstance(),
base::BindRepeating(&ios::BuildFakeSigninManager)}};
} }
} // namespace } // namespace
...@@ -38,28 +41,43 @@ IdentityTestEnvironmentChromeBrowserStateAdaptor:: ...@@ -38,28 +41,43 @@ IdentityTestEnvironmentChromeBrowserStateAdaptor::
std::unique_ptr<TestChromeBrowserState> std::unique_ptr<TestChromeBrowserState>
IdentityTestEnvironmentChromeBrowserStateAdaptor:: IdentityTestEnvironmentChromeBrowserStateAdaptor::
CreateChromeBrowserStateForIdentityTestEnvironment( CreateChromeBrowserStateForIdentityTestEnvironment(
const TestChromeBrowserState::TestingFactories& input_factories) { const TestChromeBrowserState::TestingFactories& input_factories,
bool create_fake_url_loader_factory_for_cookie_requests) {
TestChromeBrowserState::Builder builder; TestChromeBrowserState::Builder builder;
for (auto& input_factory : input_factories) { for (auto& input_factory : input_factories) {
builder.AddTestingFactory(input_factory.first, input_factory.second); builder.AddTestingFactory(input_factory.first, input_factory.second);
} }
return CreateChromeBrowserStateForIdentityTestEnvironment(builder); return CreateChromeBrowserStateForIdentityTestEnvironment(
builder, create_fake_url_loader_factory_for_cookie_requests);
} }
// static // static
std::unique_ptr<TestChromeBrowserState> std::unique_ptr<TestChromeBrowserState>
IdentityTestEnvironmentChromeBrowserStateAdaptor:: IdentityTestEnvironmentChromeBrowserStateAdaptor::
CreateChromeBrowserStateForIdentityTestEnvironment( CreateChromeBrowserStateForIdentityTestEnvironment(
TestChromeBrowserState::Builder& builder) { TestChromeBrowserState::Builder& builder,
for (auto& identity_factory : GetIdentityTestEnvironmentFactories()) { bool create_fake_url_loader_factory_for_cookie_requests) {
for (auto& identity_factory : GetIdentityTestEnvironmentFactories(
create_fake_url_loader_factory_for_cookie_requests)) {
builder.AddTestingFactory(identity_factory.first, identity_factory.second); builder.AddTestingFactory(identity_factory.first, identity_factory.second);
} }
return builder.Build(); return builder.Build();
} }
// static
void IdentityTestEnvironmentChromeBrowserStateAdaptor::
SetIdentityTestEnvironmentFactoriesOnBrowserContext(
TestChromeBrowserState* browser_state,
bool create_fake_url_loader_factory_for_cookie_requests) {
for (const auto& factory_pair : GetIdentityTestEnvironmentFactories(
create_fake_url_loader_factory_for_cookie_requests)) {
factory_pair.first->SetTestingFactory(browser_state, factory_pair.second);
}
}
// static // static
void IdentityTestEnvironmentChromeBrowserStateAdaptor:: void IdentityTestEnvironmentChromeBrowserStateAdaptor::
AppendIdentityTestEnvironmentFactories( AppendIdentityTestEnvironmentFactories(
......
...@@ -24,16 +24,30 @@ class IdentityTestEnvironmentChromeBrowserStateAdaptor { ...@@ -24,16 +24,30 @@ class IdentityTestEnvironmentChromeBrowserStateAdaptor {
CreateChromeBrowserStateForIdentityTestEnvironment(); CreateChromeBrowserStateForIdentityTestEnvironment();
// Like the above, but additionally configures the returned ChromeBrowserState // Like the above, but additionally configures the returned ChromeBrowserState
// with |input_factories|. // with |input_factories|. By default, internally constructs a
// TestURLLoaderFactory to use for cookie-related network requests. If this
// isn't desired (e.g., because the test is already using a
// TestURLLoaderFactory), set
// |create_fake_url_loader_factory_for_cookie_requests| to false.
static std::unique_ptr<TestChromeBrowserState> static std::unique_ptr<TestChromeBrowserState>
CreateChromeBrowserStateForIdentityTestEnvironment( CreateChromeBrowserStateForIdentityTestEnvironment(
const TestChromeBrowserState::TestingFactories& input_factories); const TestChromeBrowserState::TestingFactories& input_factories,
bool create_fake_url_loader_factory_for_cookie_requests = true);
// Creates and returns a TestChromeBrowserState that has been configured with // Creates and returns a TestChromeBrowserState that has been configured with
// the given |builder|. // the given |builder|.
// See the above variant for comments on common parameters.
static std::unique_ptr<TestChromeBrowserState> static std::unique_ptr<TestChromeBrowserState>
CreateChromeBrowserStateForIdentityTestEnvironment( CreateChromeBrowserStateForIdentityTestEnvironment(
TestChromeBrowserState::Builder& builder); TestChromeBrowserState::Builder& builder,
bool create_fake_url_loader_factory_for_cookie_requests = true);
// Sets the testing factories that identity::IdentityTestEnvironment
// requires explicitly on a Profile that is passed to it.
// See the above variant for comments on common parameters.
static void SetIdentityTestEnvironmentFactoriesOnBrowserContext(
TestChromeBrowserState* browser_state,
bool create_fake_url_loader_factory_for_cookie_requests = true);
// Appends the set of testing factories that identity::IdentityTestEnvironment // Appends the set of testing factories that identity::IdentityTestEnvironment
// requires to |factories_to_append_to|, which should be the set of testing // requires to |factories_to_append_to|, which should be the set of testing
...@@ -42,6 +56,8 @@ class IdentityTestEnvironmentChromeBrowserStateAdaptor { ...@@ -42,6 +56,8 @@ class IdentityTestEnvironmentChromeBrowserStateAdaptor {
// fragile. This API is primarily for use in tests that do not create the // fragile. This API is primarily for use in tests that do not create the
// TestChromeBrowserState internally but rather simply supply the set of // TestChromeBrowserState internally but rather simply supply the set of
// TestingFactories to some external facility (e.g., a superclass). // TestingFactories to some external facility (e.g., a superclass).
// See CreateProfileForIdentityTestEnvironment() for comments on common
// parameters.
static void AppendIdentityTestEnvironmentFactories( static void AppendIdentityTestEnvironmentFactories(
TestChromeBrowserState::TestingFactories* factories_to_append_to); TestChromeBrowserState::TestingFactories* factories_to_append_to);
......
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