Commit ae423e53 authored by Colin Blundell's avatar Colin Blundell Committed by Commit Bot

[ios] Provide ChromeBrowserState IdentityTestEnvironment adaptor

A straightforward port of //chrome's Profile adaptor that will be used
by //ios/chrome unittests that tied to the
BrowserStateKeyedServiceFactory infrastructure.

Bug: 895772
Change-Id: I61cd90a0fdcb494e661711ccfb8c2bdeb411193d
Reviewed-on: https://chromium-review.googlesource.com/c/1290941
Commit-Queue: Colin Blundell <blundell@chromium.org>
Reviewed-by: default avatarDavid Roger <droger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#601198}
parent bdb0829d
......@@ -111,6 +111,8 @@ source_set("test_support") {
"fake_oauth2_token_service_builder.mm",
"fake_signin_manager_builder.cc",
"fake_signin_manager_builder.h",
"identity_test_environment_chrome_browser_state_adaptor.cc",
"identity_test_environment_chrome_browser_state_adaptor.h",
]
deps = [
":signin",
......@@ -118,9 +120,11 @@ source_set("test_support") {
"//components/signin/core/browser:test_support",
"//components/signin/ios/browser",
"//ios/chrome/browser/browser_state",
"//ios/chrome/browser/browser_state:test_support",
"//ios/chrome/browser/sync",
"//ios/public/provider/chrome/browser",
"//ios/public/provider/chrome/browser/signin",
"//services/identity/public/cpp:test_support",
]
}
......
// 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 "ios/chrome/browser/signin/identity_test_environment_chrome_browser_state_adaptor.h"
#include "ios/chrome/browser/signin/account_tracker_service_factory.h"
#include "ios/chrome/browser/signin/fake_gaia_cookie_manager_service_builder.h"
#include "ios/chrome/browser/signin/fake_oauth2_token_service_builder.h"
#include "ios/chrome/browser/signin/fake_signin_manager_builder.h"
#include "ios/chrome/browser/signin/gaia_cookie_manager_service_factory.h"
#include "ios/chrome/browser/signin/identity_manager_factory.h"
#include "ios/chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "ios/chrome/browser/signin/signin_manager_factory.h"
namespace {
TestChromeBrowserState::TestingFactories GetIdentityTestEnvironmentFactories() {
return {{ios::GaiaCookieManagerServiceFactory::GetInstance(),
base::BindRepeating(&BuildFakeGaiaCookieManagerService)},
{ProfileOAuth2TokenServiceFactory::GetInstance(),
base::BindRepeating(&BuildFakeOAuth2TokenService)},
{ios::SigninManagerFactory::GetInstance(),
base::BindRepeating(&ios::BuildFakeSigninManager)}};
}
} // namespace
// static
std::unique_ptr<TestChromeBrowserState>
IdentityTestEnvironmentChromeBrowserStateAdaptor::
CreateChromeBrowserStateForIdentityTestEnvironment() {
return CreateChromeBrowserStateForIdentityTestEnvironment(
TestChromeBrowserState::TestingFactories());
}
// static
std::unique_ptr<TestChromeBrowserState>
IdentityTestEnvironmentChromeBrowserStateAdaptor::
CreateChromeBrowserStateForIdentityTestEnvironment(
const TestChromeBrowserState::TestingFactories& input_factories) {
TestChromeBrowserState::Builder builder;
for (auto& input_factory : input_factories) {
builder.AddTestingFactory(input_factory.first, input_factory.second);
}
for (auto& identity_factory : GetIdentityTestEnvironmentFactories()) {
builder.AddTestingFactory(identity_factory.first, identity_factory.second);
}
return builder.Build();
}
// static
void IdentityTestEnvironmentChromeBrowserStateAdaptor::
AppendIdentityTestEnvironmentFactories(
TestChromeBrowserState::TestingFactories* factories_to_append_to) {
TestChromeBrowserState::TestingFactories identity_factories =
GetIdentityTestEnvironmentFactories();
factories_to_append_to->insert(factories_to_append_to->end(),
identity_factories.begin(),
identity_factories.end());
}
IdentityTestEnvironmentChromeBrowserStateAdaptor::
IdentityTestEnvironmentChromeBrowserStateAdaptor(
ios::ChromeBrowserState* browser_state)
: identity_test_env_(
ios::AccountTrackerServiceFactory::GetForBrowserState(browser_state),
static_cast<FakeProfileOAuth2TokenService*>(
ProfileOAuth2TokenServiceFactory::GetForBrowserState(
browser_state)),
static_cast<FakeSigninManager*>(
ios::SigninManagerFactory::GetForBrowserState(browser_state)),
static_cast<FakeGaiaCookieManagerService*>(
ios::GaiaCookieManagerServiceFactory::GetForBrowserState(
browser_state)),
IdentityManagerFactory::GetForBrowserState(browser_state)) {}
// 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 IOS_CHROME_BROWSER_SIGNIN_IDENTITY_TEST_ENVIRONMENT_CHROME_BROWSER_STATE_ADAPTOR_H_
#define IOS_CHROME_BROWSER_SIGNIN_IDENTITY_TEST_ENVIRONMENT_CHROME_BROWSER_STATE_ADAPTOR_H_
#include "ios/chrome/browser/browser_state/test_chrome_browser_state.h"
#include "services/identity/public/cpp/identity_test_environment.h"
// Adaptor that supports identity::IdentityTestEnvironment's usage in testing
// contexts where the relevant fake objects must be injected via the
// BrowserStateKeyedServiceFactory infrastructure as the production code
// accesses IdentityManager via that infrastructure. Before using this
// class, please consider whether you can change the production code in question
// to take in the relevant dependencies directly rather than obtaining them from
// the ChromeBrowserState; this is both cleaner in general and allows for direct
// usage of identity::IdentityTestEnvironment in the test.
class IdentityTestEnvironmentChromeBrowserStateAdaptor {
public:
// Creates and returns a TestChromeBrowserState that has been configured with
// the set of testing factories that IdentityTestEnvironment requires.
static std::unique_ptr<TestChromeBrowserState>
CreateChromeBrowserStateForIdentityTestEnvironment();
// Like the above, but additionally configures the returned ChromeBrowserState
// with |input_factories|.
static std::unique_ptr<TestChromeBrowserState>
CreateChromeBrowserStateForIdentityTestEnvironment(
const TestChromeBrowserState::TestingFactories& input_factories);
// Appends the set of testing factories that identity::IdentityTestEnvironment
// requires to |factories_to_append_to|, which should be the set of testing
// factories supplied to TestChromeBrowserState (via one of the various
// mechanisms for doing so). Prefer the above API if possible, as it is less
// 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).
static void AppendIdentityTestEnvironmentFactories(
TestChromeBrowserState::TestingFactories* factories_to_append_to);
// Constructs an adaptor that associates an IdentityTestEnvironment instance
// with |browser_state| via the relevant backing objects. Note that
// |browser_state| must have been configured with the IdentityTestEnvironment
// testing factories, either because it was created via
// CreateChromeBrowserStateForIdentityTestEnvironment() or because
// AppendIdentityTestEnvironmentFactories() was invoked on the set of
// factories supplied to it.
// |browser_state| must outlive this object.
explicit IdentityTestEnvironmentChromeBrowserStateAdaptor(
ios::ChromeBrowserState* browser_state);
~IdentityTestEnvironmentChromeBrowserStateAdaptor() {}
// Returns the IdentityTestEnvironment associated with this object (and
// implicitly with the ChromeBrowserState passed to this object's
// constructor).
identity::IdentityTestEnvironment* identity_test_env() {
return &identity_test_env_;
}
private:
identity::IdentityTestEnvironment identity_test_env_;
DISALLOW_COPY_AND_ASSIGN(IdentityTestEnvironmentChromeBrowserStateAdaptor);
};
#endif // IOS_CHROME_BROWSER_SIGNIN_IDENTITY_TEST_ENVIRONMENT_CHROME_BROWSER_STATE_ADAPTOR_H_
......@@ -13,6 +13,7 @@
#include "services/identity/public/cpp/identity_manager.h"
#include "services/identity/public/cpp/identity_test_utils.h"
class IdentityTestEnvironmentChromeBrowserStateAdaptor;
class IdentityTestEnvironmentProfileAdaptor;
namespace identity {
......@@ -200,6 +201,7 @@ class IdentityTestEnvironment : public IdentityManager::DiagnosticsObserver {
void SetCallbackForNextAccessTokenRequest(base::OnceClosure callback);
private:
friend class ::IdentityTestEnvironmentChromeBrowserStateAdaptor;
friend class ::IdentityTestEnvironmentProfileAdaptor;
struct AccessTokenRequestState {
......
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