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