1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/profiles/profile_keyed_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_testing_helper.h"
#include "chrome/browser/profiles/refcounted_profile_keyed_service_factory.h"
#include "testing/gtest/include/gtest/gtest.h"
// This unittest file contains both tests for `ProfileKeyedServiceFactory` and
// `RefcountedProfileKeyedServiceFactory` for simplicity.
// Testing implementation for interface `ProfileKeyedServiceFactory`.
// The method `GetProfileToUseForTesting()` is used to test the protected method
// `GetBrowserContextToUse()`.
// Default nullptr implementation of `BuildServiceInstanceFor()` since
// `ProfileKeyedServiceFactory` doesn't add any logic to this method.
class ProfileKeyedServiceFactoryTest : public ProfileKeyedServiceFactory {
public:
explicit ProfileKeyedServiceFactoryTest(const char* name)
: ProfileKeyedServiceFactory(name) {}
ProfileKeyedServiceFactoryTest(const char* name,
const ProfileSelections& selections)
: ProfileKeyedServiceFactory(name, selections) {}
// Method used for testing only, calls `GetBrowserContextToUse()` for testing.
Profile* GetProfileToUseForTesting(Profile* profile) const {
return Profile::FromBrowserContext(GetBrowserContextToUse(profile));
}
protected:
// Implementation is not for testing.
std::unique_ptr<KeyedService> BuildServiceInstanceForBrowserContext(
content::BrowserContext* context) const override {
NOTREACHED();
}
};
// Similar testing implementation of `ProfileKeyedServiceFactory` but for Ref
// counted Services `RefcountedProfileKeyedServiceFactory`.
class RefcountedProfileKeyedServiceFactoryTest
: public RefcountedProfileKeyedServiceFactory {
public:
explicit RefcountedProfileKeyedServiceFactoryTest(const char* name)
: RefcountedProfileKeyedServiceFactory(name) {}
RefcountedProfileKeyedServiceFactoryTest(const char* name,
const ProfileSelections& selections)
: RefcountedProfileKeyedServiceFactory(name, selections) {}
// Method used for testing only, calls `GetBrowserContextToUse()` for testing.
Profile* GetProfileToUseForTesting(Profile* profile) const {
return Profile::FromBrowserContext(GetBrowserContextToUse(profile));
}
protected:
// Implementation is not for testing.
scoped_refptr<RefcountedKeyedService> BuildServiceInstanceFor(
content::BrowserContext* context) const override {
NOTREACHED();
}
};
class ProfileKeyedServiceFactoryUnittest : public testing::Test {
public:
void SetUp() override {
testing::Test::SetUp();
profile_testing_helper_.SetUp();
}
protected:
template <typename ProfileKeyedServiceFactoryTesting>
void TestProfileToUse(const ProfileKeyedServiceFactoryTesting& factory,
Profile* given_profile,
Profile* expected_profile) {
EXPECT_EQ(factory.GetProfileToUseForTesting(given_profile),
expected_profile);
}
TestingProfile* regular_profile() {
return profile_testing_helper_.regular_profile();
}
Profile* incognito_profile() {
return profile_testing_helper_.incognito_profile();
}
TestingProfile* guest_profile() {
return profile_testing_helper_.guest_profile();
}
Profile* guest_profile_otr() {
return profile_testing_helper_.guest_profile_otr();
}
#if !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_ANDROID)
TestingProfile* system_profile() {
return profile_testing_helper_.system_profile();
}
Profile* system_profile_otr() {
return profile_testing_helper_.system_profile_otr();
}
#endif // !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_ANDROID)
#if BUILDFLAG(IS_CHROMEOS)
TestingProfile* signin_profile() {
return profile_testing_helper_.signin_profile();
}
Profile* signin_profile_otr() {
return profile_testing_helper_.signin_profile_otr();
}
TestingProfile* lockscreen_profile() {
return profile_testing_helper_.lockscreen_profile();
}
Profile* lockscreen_profile_otr() {
return profile_testing_helper_.lockscreen_profile_otr();
}
#endif // BUILDFLAG(IS_CHROMEOS)
private:
ProfileTestingHelper profile_testing_helper_;
base::test::ScopedFeatureList feature_list_;
};
// Factory using default `ProfileKeyedServiceFactory` constructor
class DefaultFactoryTest : public ProfileKeyedServiceFactoryTest {
public:
DefaultFactoryTest() : ProfileKeyedServiceFactoryTest("DefaultFactory") {}
};
TEST_F(ProfileKeyedServiceFactoryUnittest, DefaultFactoryTest) {
DefaultFactoryTest factory;
TestProfileToUse(factory, regular_profile(), regular_profile());
TestProfileToUse(factory, incognito_profile(), nullptr);
TestProfileToUse(factory, guest_profile(), nullptr);
TestProfileToUse(factory, guest_profile_otr(), nullptr);
#if !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_ANDROID)
TestProfileToUse(factory, system_profile(), nullptr);
TestProfileToUse(factory, system_profile_otr(), nullptr);
#endif // !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_ANDROID)
#if BUILDFLAG(IS_CHROMEOS)
TestProfileToUse(factory, signin_profile(), nullptr);
TestProfileToUse(factory, signin_profile_otr(), nullptr);
TestProfileToUse(factory, lockscreen_profile(), nullptr);
TestProfileToUse(factory, lockscreen_profile_otr(), nullptr);
#endif // BUILDFLAG(IS_CHROMEOS)
}
// Factory using predefined `ProfileSelections` built
class PredefinedProfileSelectionsFactoryTest
: public ProfileKeyedServiceFactoryTest {
public:
PredefinedProfileSelectionsFactoryTest()
: ProfileKeyedServiceFactoryTest(
"PredefinedProfileSelectionsFactoryTest",
ProfileSelections::BuildRedirectedInIncognito()) {}
};
TEST_F(ProfileKeyedServiceFactoryUnittest,
PredefinedProfileSelectionsFactoryTest) {
PredefinedProfileSelectionsFactoryTest factory;
TestProfileToUse(factory, regular_profile(), regular_profile());
TestProfileToUse(factory, incognito_profile(), regular_profile());
TestProfileToUse(factory, guest_profile(), nullptr);
TestProfileToUse(factory, guest_profile_otr(), nullptr);
#if !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_ANDROID)
TestProfileToUse(factory, system_profile(), nullptr);
TestProfileToUse(factory, system_profile_otr(), nullptr);
#endif // !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_ANDROID)
#if BUILDFLAG(IS_CHROMEOS)
TestProfileToUse(factory, signin_profile(), nullptr);
TestProfileToUse(factory, signin_profile_otr(), nullptr);
TestProfileToUse(factory, lockscreen_profile(), nullptr);
TestProfileToUse(factory, lockscreen_profile_otr(), nullptr);
#endif // BUILDFLAG(IS_CHROMEOS)
}
// Factory using customized `ProfileSelections` using
// `ProfileSelections::Builder()`
class CustomizedProfileSelectionsFactoryTest
: public ProfileKeyedServiceFactoryTest {
public:
CustomizedProfileSelectionsFactoryTest()
: ProfileKeyedServiceFactoryTest(
"CustomizedProfileSelectionsFactoryTest",
ProfileSelections::Builder()
.WithRegular(ProfileSelection::kOriginalOnly)
.WithGuest(ProfileSelection::kOffTheRecordOnly)
.WithSystem(ProfileSelection::kNone)
.WithAshInternals(ProfileSelection::kOwnInstance)
.Build()) {}
};
TEST_F(ProfileKeyedServiceFactoryUnittest,
CustomizedProfileSelectionsFactoryTest) {
CustomizedProfileSelectionsFactoryTest factory;
TestProfileToUse(factory, regular_profile(), regular_profile());
TestProfileToUse(factory, incognito_profile(), nullptr);
TestProfileToUse(factory, guest_profile(), nullptr);
TestProfileToUse(factory, guest_profile_otr(), guest_profile_otr());
#if !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_ANDROID)
TestProfileToUse(factory, system_profile(), nullptr);
TestProfileToUse(factory, system_profile_otr(), nullptr);
#endif // !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_ANDROID)
#if BUILDFLAG(IS_CHROMEOS)
TestProfileToUse(factory, signin_profile(), signin_profile());
TestProfileToUse(factory, signin_profile_otr(), signin_profile_otr());
TestProfileToUse(factory, lockscreen_profile(), lockscreen_profile());
TestProfileToUse(factory, lockscreen_profile_otr(), lockscreen_profile_otr());
#endif // BUILDFLAG(IS_CHROMEOS)
}
// Factory using default `ProfileKeyedServiceFactory` constructor
class DefaultRefcountedFactoryTest
: public RefcountedProfileKeyedServiceFactoryTest {
public:
DefaultRefcountedFactoryTest()
: RefcountedProfileKeyedServiceFactoryTest(
"DefaultRefcountedFactoryTest") {}
};
TEST_F(ProfileKeyedServiceFactoryUnittest, DefaultRefcountedFactoryTest) {
DefaultRefcountedFactoryTest factory;
TestProfileToUse(factory, regular_profile(), regular_profile());
TestProfileToUse(factory, incognito_profile(), nullptr);
TestProfileToUse(factory, guest_profile(), nullptr);
TestProfileToUse(factory, guest_profile_otr(), nullptr);
#if !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_ANDROID)
TestProfileToUse(factory, system_profile(), nullptr);
TestProfileToUse(factory, system_profile_otr(), nullptr);
#endif // !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_ANDROID)
#if BUILDFLAG(IS_CHROMEOS)
TestProfileToUse(factory, signin_profile(), nullptr);
TestProfileToUse(factory, signin_profile_otr(), nullptr);
TestProfileToUse(factory, lockscreen_profile(), nullptr);
TestProfileToUse(factory, lockscreen_profile_otr(), nullptr);
#endif // BUILDFLAG(IS_CHROMEOS)
}
// Factory using customized `ProfileSelections` built
class CustomizedRefcountedProfileSelectionsFactoryTest
: public RefcountedProfileKeyedServiceFactoryTest {
public:
CustomizedRefcountedProfileSelectionsFactoryTest()
: RefcountedProfileKeyedServiceFactoryTest(
"PredefinedRefcountedProfileSelectionsFactoryTest",
ProfileSelections::Builder()
.WithRegular(ProfileSelection::kOwnInstance)
.WithGuest(ProfileSelection::kNone)
.WithSystem(ProfileSelection::kNone)
.WithAshInternals(ProfileSelection::kOffTheRecordOnly)
.Build()) {}
};
TEST_F(ProfileKeyedServiceFactoryUnittest,
CustomizedRefcountedProfileSelectionsFactoryTest) {
CustomizedRefcountedProfileSelectionsFactoryTest factory;
TestProfileToUse(factory, regular_profile(), regular_profile());
TestProfileToUse(factory, incognito_profile(), incognito_profile());
TestProfileToUse(factory, guest_profile(), nullptr);
TestProfileToUse(factory, guest_profile_otr(), nullptr);
#if !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_ANDROID)
TestProfileToUse(factory, system_profile(), nullptr);
TestProfileToUse(factory, system_profile_otr(), nullptr);
#endif // !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_ANDROID)
#if BUILDFLAG(IS_CHROMEOS)
TestProfileToUse(factory, signin_profile(), nullptr);
TestProfileToUse(factory, signin_profile_otr(), signin_profile_otr());
TestProfileToUse(factory, lockscreen_profile(), nullptr);
TestProfileToUse(factory, lockscreen_profile_otr(), lockscreen_profile_otr());
#endif // BUILDFLAG(IS_CHROMEOS)
}
|