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 {
ProfileOAuth2TokenServiceFactory::GetInstance()->SetTestingFactory(
context, base::BindRepeating(&BuildFakeProfileOAuth2TokenService));
GaiaCookieManagerServiceFactory::GetInstance()->SetTestingFactory(
context, base::BindRepeating(
&BuildFakeGaiaCookieManagerServiceNoFakeUrlFetcher));
context,
base::BindRepeating(
&BuildFakeGaiaCookieManagerServiceWithOptions,
/*create_fake_url_loader_factory_for_cookie_requests=*/false));
// Ensure that AccountFetcherService is (1) created at all and (2) created
// early enough for it to observe the Profile initialization process and
......
......@@ -11,27 +11,18 @@
#include "components/signin/core/browser/fake_gaia_cookie_manager_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(
content::BrowserContext* context) {
return BuildFakeGaiaCookieManagerServiceImpl(context,
/*use_fake_url_fetcher=*/true);
return BuildFakeGaiaCookieManagerServiceWithOptions(
/*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) {
return BuildFakeGaiaCookieManagerServiceImpl(context,
/*use_fake_url_fetcher=*/false);
Profile* profile = Profile::FromBrowserContext(context);
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;
std::unique_ptr<KeyedService> BuildFakeGaiaCookieManagerService(
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);
#endif // CHROME_BROWSER_SIGNIN_FAKE_GAIA_COOKIE_MANAGER_SERVICE_BUILDER_H_
......@@ -15,13 +15,16 @@
namespace {
TestingProfile::TestingFactories GetIdentityTestEnvironmentFactories() {
return {{GaiaCookieManagerServiceFactory::GetInstance(),
base::BindRepeating(&BuildFakeGaiaCookieManagerService)},
{ProfileOAuth2TokenServiceFactory::GetInstance(),
base::BindRepeating(&BuildFakeProfileOAuth2TokenService)},
{SigninManagerFactory::GetInstance(),
base::BindRepeating(&BuildFakeSigninManagerForTesting)}};
TestingProfile::TestingFactories GetIdentityTestEnvironmentFactories(
bool create_fake_url_loader_factory_for_cookie_requests = true) {
return {
{GaiaCookieManagerServiceFactory::GetInstance(),
base::BindRepeating(&BuildFakeGaiaCookieManagerServiceWithOptions,
create_fake_url_loader_factory_for_cookie_requests)},
{ProfileOAuth2TokenServiceFactory::GetInstance(),
base::BindRepeating(&BuildFakeProfileOAuth2TokenService)},
{SigninManagerFactory::GetInstance(),
base::BindRepeating(&BuildFakeSigninManagerForTesting)}};
}
} // namespace
......@@ -36,31 +39,47 @@ std::unique_ptr<TestingProfile> IdentityTestEnvironmentProfileAdaptor::
// static
std::unique_ptr<TestingProfile>
IdentityTestEnvironmentProfileAdaptor::CreateProfileForIdentityTestEnvironment(
const TestingProfile::TestingFactories& input_factories) {
const TestingProfile::TestingFactories& input_factories,
bool create_fake_url_loader_factory_for_cookie_requests) {
TestingProfile::Builder builder;
for (auto& input_factory : input_factories) {
builder.AddTestingFactory(input_factory.first, input_factory.second);
}
return CreateProfileForIdentityTestEnvironment(builder);
return CreateProfileForIdentityTestEnvironment(
builder, create_fake_url_loader_factory_for_cookie_requests);
}
// static
std::unique_ptr<TestingProfile>
IdentityTestEnvironmentProfileAdaptor::CreateProfileForIdentityTestEnvironment(
TestingProfile::Builder& builder) {
for (auto& identity_factory : GetIdentityTestEnvironmentFactories()) {
TestingProfile::Builder& builder,
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);
}
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
void IdentityTestEnvironmentProfileAdaptor::
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 =
GetIdentityTestEnvironmentFactories();
factories_to_append_to->insert(factories_to_append_to->end(),
......
......@@ -25,16 +25,31 @@ class IdentityTestEnvironmentProfileAdaptor {
CreateProfileForIdentityTestEnvironment();
// 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>
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
// given |builder| and the set of testing factories that
// IdentityTestEnvironment requires.
// See the above variant for comments on common parameters.
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
// requires to |factories_to_append_to|, which should be the set of testing
......@@ -43,8 +58,11 @@ class IdentityTestEnvironmentProfileAdaptor {
// API is primarily for use in tests that do not create the TestingProfile
// internally but rather simply supply the set of TestingFactories to some
// external facility (e.g., a superclass).
// See CreateProfileForIdentityTestEnvironment() for comments on common
// parameters.
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
// with |profile| via the relevant backing objects. Note that
......
......@@ -132,13 +132,15 @@ class DiceSigninUiUtilTest : public BrowserWithTestWindowTest {
// BrowserWithTestWindowTest:
TestingProfile::TestingFactories GetTestingFactories() override {
return {{SigninManagerFactory::GetInstance(),
base::BindRepeating(&BuildFakeSigninManagerForTesting)},
{ProfileOAuth2TokenServiceFactory::GetInstance(),
base::BindRepeating(&BuildFakeProfileOAuth2TokenService)},
{GaiaCookieManagerServiceFactory::GetInstance(),
base::BindRepeating(
&BuildFakeGaiaCookieManagerServiceNoFakeUrlFetcher)}};
return {
{SigninManagerFactory::GetInstance(),
base::BindRepeating(&BuildFakeSigninManagerForTesting)},
{ProfileOAuth2TokenServiceFactory::GetInstance(),
base::BindRepeating(&BuildFakeProfileOAuth2TokenService)},
{GaiaCookieManagerServiceFactory::GetInstance(),
base::BindRepeating(
&BuildFakeGaiaCookieManagerServiceWithOptions,
/*create_fake_url_loader_factory_for_cookie_requests=*/false)}};
}
// BrowserWithTestWindowTest:
......
......@@ -13,10 +13,19 @@
std::unique_ptr<KeyedService> BuildFakeGaiaCookieManagerService(
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::FromBrowserState(browser_state);
return std::make_unique<FakeGaiaCookieManagerService>(
ProfileOAuth2TokenServiceFactory::GetForBrowserState(
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;
std::unique_ptr<KeyedService> BuildFakeGaiaCookieManagerService(
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_
......@@ -15,13 +15,16 @@
namespace {
TestChromeBrowserState::TestingFactories GetIdentityTestEnvironmentFactories() {
return {{ios::GaiaCookieManagerServiceFactory::GetInstance(),
base::BindRepeating(&BuildFakeGaiaCookieManagerService)},
{ProfileOAuth2TokenServiceFactory::GetInstance(),
base::BindRepeating(&BuildFakeOAuth2TokenService)},
{ios::SigninManagerFactory::GetInstance(),
base::BindRepeating(&ios::BuildFakeSigninManager)}};
TestChromeBrowserState::TestingFactories GetIdentityTestEnvironmentFactories(
bool create_fake_url_loader_factory_for_cookie_requests = true) {
return {
{ios::GaiaCookieManagerServiceFactory::GetInstance(),
base::BindRepeating(&BuildFakeGaiaCookieManagerServiceWithOptions,
create_fake_url_loader_factory_for_cookie_requests)},
{ProfileOAuth2TokenServiceFactory::GetInstance(),
base::BindRepeating(&BuildFakeOAuth2TokenService)},
{ios::SigninManagerFactory::GetInstance(),
base::BindRepeating(&ios::BuildFakeSigninManager)}};
}
} // namespace
......@@ -38,28 +41,43 @@ IdentityTestEnvironmentChromeBrowserStateAdaptor::
std::unique_ptr<TestChromeBrowserState>
IdentityTestEnvironmentChromeBrowserStateAdaptor::
CreateChromeBrowserStateForIdentityTestEnvironment(
const TestChromeBrowserState::TestingFactories& input_factories) {
const TestChromeBrowserState::TestingFactories& input_factories,
bool create_fake_url_loader_factory_for_cookie_requests) {
TestChromeBrowserState::Builder builder;
for (auto& input_factory : input_factories) {
builder.AddTestingFactory(input_factory.first, input_factory.second);
}
return CreateChromeBrowserStateForIdentityTestEnvironment(builder);
return CreateChromeBrowserStateForIdentityTestEnvironment(
builder, create_fake_url_loader_factory_for_cookie_requests);
}
// static
std::unique_ptr<TestChromeBrowserState>
IdentityTestEnvironmentChromeBrowserStateAdaptor::
CreateChromeBrowserStateForIdentityTestEnvironment(
TestChromeBrowserState::Builder& builder) {
for (auto& identity_factory : GetIdentityTestEnvironmentFactories()) {
TestChromeBrowserState::Builder& builder,
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);
}
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
void IdentityTestEnvironmentChromeBrowserStateAdaptor::
AppendIdentityTestEnvironmentFactories(
......
......@@ -24,16 +24,30 @@ class IdentityTestEnvironmentChromeBrowserStateAdaptor {
CreateChromeBrowserStateForIdentityTestEnvironment();
// 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>
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
// the given |builder|.
// See the above variant for comments on common parameters.
static std::unique_ptr<TestChromeBrowserState>
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
// requires to |factories_to_append_to|, which should be the set of testing
......@@ -42,6 +56,8 @@ class IdentityTestEnvironmentChromeBrowserStateAdaptor {
// fragile. This API is primarily for use in tests that do not create the
// TestChromeBrowserState internally but rather simply supply the set of
// TestingFactories to some external facility (e.g., a superclass).
// See CreateProfileForIdentityTestEnvironment() for comments on common
// parameters.
static void AppendIdentityTestEnvironmentFactories(
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