Commit 2d7072f9 authored by Sylvain Defresne's avatar Sylvain Defresne Committed by Commit Bot

Convert UserCloudPolicyManagerFactory to TestingFactory

TestingFactory is a base::Callback<> while TestingFactoryFunction
is a function pointer. Convert UserCloudPolicyManagerFactory to
the callback based type.

Bug: 809610
Change-Id: If041d74cf623b29672c90351460f23be9a5fb24d
Reviewed-on: https://chromium-review.googlesource.com/1245713
Commit-Queue: Sylvain Defresne <sdefresne@chromium.org>
Reviewed-by: default avatarSergey Poromov <poromov@chromium.org>
Cr-Commit-Position: refs/heads/master@{#595841}
parent 9f23bd07
......@@ -74,7 +74,7 @@ UserCloudPolicyManagerFactory::CreateForOriginalBrowserContext(
const scoped_refptr<base::SequencedTaskRunner>& background_task_runner) {
UserCloudPolicyManagerFactory* factory = GetInstance();
// If there's a testing factory set, don't bother creating a new one.
if (factory->testing_factory_ != NULL)
if (factory->testing_factory_)
return std::unique_ptr<UserCloudPolicyManager>();
return factory->CreateManagerForOriginalBrowserContext(
context, force_immediate_load, background_task_runner);
......@@ -90,24 +90,23 @@ UserCloudPolicyManagerFactory::RegisterForOffTheRecordBrowserContext(
}
void UserCloudPolicyManagerFactory::RegisterTestingFactory(
TestingFactoryFunction factory) {
TestingFactory factory) {
// Can't set a testing factory when a testing factory has already been
// created, or after UCPMs have already been built.
DCHECK(!testing_factory_);
DCHECK(factory);
DCHECK(manager_wrappers_.empty());
testing_factory_ = factory;
testing_factory_ = std::move(factory);
}
void UserCloudPolicyManagerFactory::ClearTestingFactory() {
testing_factory_ = NULL;
testing_factory_ = TestingFactory();
}
UserCloudPolicyManagerFactory::UserCloudPolicyManagerFactory()
: BrowserContextKeyedBaseFactory(
"UserCloudPolicyManager",
BrowserContextDependencyManager::GetInstance()),
testing_factory_(NULL) {
"UserCloudPolicyManager",
BrowserContextDependencyManager::GetInstance()) {
DependsOn(SchemaRegistryServiceFactory::GetInstance());
}
......@@ -191,19 +190,20 @@ void UserCloudPolicyManagerFactory::SetEmptyTestingFactory(
bool UserCloudPolicyManagerFactory::HasTestingFactory(
content::BrowserContext* context) {
return testing_factory_ != NULL;
return !testing_factory_.is_null();
}
// If there's a TestingFactory set, then create a service during BrowserContext
// initialization.
bool UserCloudPolicyManagerFactory::ServiceIsCreatedWithBrowserContext() const {
return testing_factory_ != NULL;
return !testing_factory_.is_null();
}
void UserCloudPolicyManagerFactory::CreateServiceNow(
content::BrowserContext* context) {
DCHECK(testing_factory_);
manager_wrappers_[context] = new ManagerWrapper(testing_factory_(context));
manager_wrappers_[context] =
new ManagerWrapper(testing_factory_.Run(context));
}
} // namespace policy
......@@ -8,6 +8,7 @@
#include <map>
#include <memory>
#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/singleton.h"
......@@ -68,15 +69,15 @@ class UserCloudPolicyManagerFactory : public BrowserContextKeyedBaseFactory {
content::BrowserContext* original_context,
content::BrowserContext* off_the_record_context);
typedef UserCloudPolicyManager*
(*TestingFactoryFunction)(content::BrowserContext* context);
using TestingFactory = base::RepeatingCallback<UserCloudPolicyManager*(
content::BrowserContext* context)>;
// Allows testing code to inject UserCloudPolicyManager objects for tests.
// The factory function will be invoked for every Profile created. Because
// this class does not free the UserCloudPolicyManager objects it manages,
// it is up to the tests themselves to free the objects after the profile is
// shut down.
void RegisterTestingFactory(TestingFactoryFunction factory);
void RegisterTestingFactory(TestingFactory factory);
void ClearTestingFactory();
private:
......@@ -111,7 +112,7 @@ class UserCloudPolicyManagerFactory : public BrowserContextKeyedBaseFactory {
typedef std::map<content::BrowserContext*, ManagerWrapper*> ManagerWrapperMap;
ManagerWrapperMap manager_wrappers_;
TestingFactoryFunction testing_factory_;
TestingFactory testing_factory_;
DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerFactory);
};
......
......@@ -5,6 +5,7 @@
#include <memory>
#include <utility>
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/run_loop.h"
#include "base/test/bind_test_util.h"
......@@ -169,7 +170,7 @@ class UserPolicySigninServiceTest : public testing::Test {
// instances) so we have to inject our testing factory via a special
// API before creating the profile.
UserCloudPolicyManagerFactory::GetInstance()->RegisterTestingFactory(
BuildCloudPolicyManager);
base::BindRepeating(&BuildCloudPolicyManager));
TestingProfile::Builder builder;
builder.SetPrefService(
std::unique_ptr<sync_preferences::PrefServiceSyncable>(
......
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