Commit f3ef59a4 authored by Collin Baker's avatar Collin Baker Committed by Commit Bot

Add SetTestingFactoryAndUse variant for KeyedService subclasses

SetTestingFactoryAndUse often requires users to do two casts
themselves: an upcast of their service to KeyedService in their
TestingFactory implementation, then a downcast from KeyedService to
their service subclass on the result.

This adds a method, SetTestingSubclassFactoryAndUse, which does these
casts internally.

Change-Id: I6d77d06dd06ebcb212498e2692f2ec9f1fb584b0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2244095
Commit-Queue: Collin Baker <collinbaker@chromium.org>
Reviewed-by: default avatarHenrique Nakashima <hnakashima@chromium.org>
Cr-Commit-Position: refs/heads/master@{#779614}
parent 5a7edaeb
...@@ -25,7 +25,13 @@ class TabGroupsIPHControllerTest : public BrowserWithTestWindowTest { ...@@ -25,7 +25,13 @@ class TabGroupsIPHControllerTest : public BrowserWithTestWindowTest {
public: public:
void SetUp() override { void SetUp() override {
BrowserWithTestWindowTest::SetUp(); BrowserWithTestWindowTest::SetUp();
mock_tracker_ = InjectTrackerForProfile(GetProfile()); mock_tracker_ =
feature_engagement::TrackerFactory::GetInstance()
->SetTestingSubclassFactoryAndUse(
GetProfile(), base::BindRepeating([](content::BrowserContext*) {
return std::make_unique<
feature_engagement::test::MockTracker>();
}));
// Other features call into the IPH backend. We don't want to fail // Other features call into the IPH backend. We don't want to fail
// on their calls, so allow them. Our test cases will set // on their calls, so allow them. Our test cases will set
...@@ -38,19 +44,6 @@ class TabGroupsIPHControllerTest : public BrowserWithTestWindowTest { ...@@ -38,19 +44,6 @@ class TabGroupsIPHControllerTest : public BrowserWithTestWindowTest {
protected: protected:
feature_engagement::test::MockTracker* mock_tracker_; feature_engagement::test::MockTracker* mock_tracker_;
private:
static feature_engagement::test::MockTracker* InjectTrackerForProfile(
Profile* profile) {
KeyedService* const tracker =
feature_engagement::TrackerFactory::GetInstance()
->SetTestingFactoryAndUse(profile, base::Bind(MakeTracker));
return static_cast<feature_engagement::test::MockTracker*>(tracker);
}
static std::unique_ptr<KeyedService> MakeTracker(content::BrowserContext*) {
return std::make_unique<feature_engagement::test::MockTracker>();
}
}; };
TEST_F(TabGroupsIPHControllerTest, NotifyEventAndTriggerOnSixthTabOpened) { TEST_F(TabGroupsIPHControllerTest, NotifyEventAndTriggerOnSixthTabOpened) {
......
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
#include <memory> #include <memory>
#include "base/bind.h"
#include "base/callback.h"
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "base/macros.h" #include "base/macros.h"
#include "components/keyed_service/core/keyed_service_export.h" #include "components/keyed_service/core/keyed_service_export.h"
...@@ -48,6 +50,34 @@ class KEYED_SERVICE_EXPORT BrowserContextKeyedServiceFactory ...@@ -48,6 +50,34 @@ class KEYED_SERVICE_EXPORT BrowserContextKeyedServiceFactory
KeyedService* SetTestingFactoryAndUse(content::BrowserContext* context, KeyedService* SetTestingFactoryAndUse(content::BrowserContext* context,
TestingFactory testing_factory); TestingFactory testing_factory);
// A variant of |TestingFactory| for supplying a subclass of
// KeyedService for a given BrowserContext.
template <typename Derived>
using TestingSubclassFactory =
base::RepeatingCallback<std::unique_ptr<Derived>(
content::BrowserContext* context)>;
// Like |SetTestingFactoryAndUse|, but instead takes a factory for a
// subclass of KeyedService and returns a pointer to this subclass.
// This allows callers to avoid using static_cast in both directions:
// casting up to KeyedService in their factory, and casting down to
// their subclass on the returned pointer.
template <typename Derived>
Derived* SetTestingSubclassFactoryAndUse(
content::BrowserContext* context,
TestingSubclassFactory<Derived> derived_factory) {
TestingFactory upcast_factory = base::BindRepeating(
[](TestingSubclassFactory<Derived> derived_factory,
content::BrowserContext* context) {
return std::unique_ptr<KeyedService>(
derived_factory.Run(context).release());
},
std::move(derived_factory));
return static_cast<Derived*>(
SetTestingFactoryAndUse(context, std::move(upcast_factory)));
}
protected: protected:
// BrowserContextKeyedServiceFactories must communicate with a // BrowserContextKeyedServiceFactories must communicate with a
// BrowserContextDependencyManager. For all non-test code, write your subclass // BrowserContextDependencyManager. For all non-test code, write your subclass
......
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