-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Test] Added unittests for ProfileKeyedServiceFactory and ProfileSele…
…ctions profile_keyed_service_factory_unittest.cc: unit tests for ProfileKeyedServiceFactory with some implementations of the interface and example of usage. profile_selections_unittest.cc: ProfileSelections testing with different combinations of Prebuilt and customised ProfileSelections. profile_testing_helper.h/cc: Helper testing class to provide easy access to different types of profiles for testing. Bug: 1346533 Change-Id: Ie322233880d6b91879d371c48911b7bca3c347b1 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3781352 Reviewed-by: David Roger <droger@chromium.org> Commit-Queue: Ryan Sultanem <rsult@google.com> Cr-Commit-Position: refs/heads/main@{#1028326}
- Loading branch information
Ryan Sultanem
authored and
Chromium LUCI CQ
committed
Jul 26, 2022
1 parent
18bccbb
commit 687a16f
Showing
6 changed files
with
423 additions
and
1 deletion.
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
chrome/browser/profiles/profile_keyed_service_factory_unittest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
// Copyright 2022 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 "chrome/browser/profiles/profile_keyed_service_factory.h" | ||
|
||
#include "chrome/browser/profiles/profile.h" | ||
#include "chrome/browser/profiles/profile_testing_helper.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
// 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. | ||
KeyedService* BuildServiceInstanceFor( | ||
content::BrowserContext* context) const override { | ||
NOTREACHED(); | ||
return nullptr; | ||
} | ||
}; | ||
|
||
class ProfileKeyedServiceFactoryUnittest : public testing::Test { | ||
public: | ||
void SetUp() override { | ||
testing::Test::SetUp(); | ||
profile_testing_helper_.SetUp(); | ||
} | ||
|
||
protected: | ||
void TestProfileToUse(const ProfileKeyedServiceFactoryTest& 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_ASH) && !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_ASH) && !BUILDFLAG(IS_ANDROID) | ||
|
||
private: | ||
ProfileTestingHelper profile_testing_helper_; | ||
}; | ||
|
||
// 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(), guest_profile()); | ||
TestProfileToUse(factory, guest_profile_otr(), nullptr); | ||
|
||
#if !BUILDFLAG(IS_CHROMEOS_ASH) && !BUILDFLAG(IS_ANDROID) | ||
TestProfileToUse(factory, system_profile(), system_profile()); | ||
TestProfileToUse(factory, system_profile_otr(), nullptr); | ||
#endif // !BUILDFLAG(IS_CHROMEOS_ASH) && !BUILDFLAG(IS_ANDROID) | ||
} | ||
|
||
// Factory using predefined `ProfileSelections` built | ||
class PredefinedProfileSelectionsFactoryTest | ||
: public ProfileKeyedServiceFactoryTest { | ||
public: | ||
PredefinedProfileSelectionsFactoryTest() | ||
: ProfileKeyedServiceFactoryTest( | ||
"PredefinedProfileSelectionsFactoryTest", | ||
ProfileSelections::BuildServicesRedirectedToOriginal()) {} | ||
}; | ||
|
||
TEST_F(ProfileKeyedServiceFactoryUnittest, | ||
PredefinedProfileSelectionsFactoryTest) { | ||
PredefinedProfileSelectionsFactoryTest factory; | ||
TestProfileToUse(factory, regular_profile(), regular_profile()); | ||
TestProfileToUse(factory, incognito_profile(), regular_profile()); | ||
|
||
TestProfileToUse(factory, guest_profile(), guest_profile()); | ||
TestProfileToUse(factory, guest_profile_otr(), guest_profile()); | ||
|
||
#if !BUILDFLAG(IS_CHROMEOS_ASH) && !BUILDFLAG(IS_ANDROID) | ||
TestProfileToUse(factory, system_profile(), system_profile()); | ||
TestProfileToUse(factory, system_profile_otr(), system_profile()); | ||
#endif // !BUILDFLAG(IS_CHROMEOS_ASH) && !BUILDFLAG(IS_ANDROID) | ||
} | ||
|
||
// 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) | ||
.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_ASH) && !BUILDFLAG(IS_ANDROID) | ||
TestProfileToUse(factory, system_profile(), nullptr); | ||
TestProfileToUse(factory, system_profile_otr(), nullptr); | ||
#endif // !BUILDFLAG(IS_CHROMEOS_ASH) && !BUILDFLAG(IS_ANDROID) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
// Copyright 2022 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 "chrome/browser/profiles/profile_selections.h" | ||
|
||
#include "chrome/browser/profiles/profile_testing_helper.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
class ProfileSelectionsTest : public testing::Test { | ||
public: | ||
void SetUp() override { | ||
testing::Test::SetUp(); | ||
profile_testing_helper_.SetUp(); | ||
} | ||
|
||
protected: | ||
void TestProfileSelection(const ProfileSelections& selections, | ||
Profile* given_profile, | ||
Profile* expected_profile) { | ||
EXPECT_EQ(selections.ApplyProfileSelection(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_ASH) && !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_ASH) && !BUILDFLAG(IS_ANDROID) | ||
|
||
private: | ||
ProfileTestingHelper profile_testing_helper_; | ||
}; | ||
|
||
TEST_F(ProfileSelectionsTest, DefaultImplementation) { | ||
ProfileSelections selections = ProfileSelections::BuildDefault(); | ||
|
||
TestProfileSelection(selections, regular_profile(), regular_profile()); | ||
TestProfileSelection(selections, incognito_profile(), nullptr); | ||
|
||
TestProfileSelection(selections, guest_profile(), guest_profile()); | ||
TestProfileSelection(selections, guest_profile_otr(), nullptr); | ||
|
||
#if !BUILDFLAG(IS_CHROMEOS_ASH) && !BUILDFLAG(IS_ANDROID) | ||
TestProfileSelection(selections, system_profile(), system_profile()); | ||
TestProfileSelection(selections, system_profile_otr(), nullptr); | ||
#endif // !BUILDFLAG(IS_CHROMEOS_ASH) && !BUILDFLAG(IS_ANDROID) | ||
} | ||
|
||
TEST_F(ProfileSelectionsTest, CustomImplementation) { | ||
ProfileSelections selections = | ||
ProfileSelections::Builder() | ||
.WithRegular(ProfileSelection::kOwnInstance) | ||
.WithGuest(ProfileSelection::kOffTheRecordOnly) | ||
.WithSystem(ProfileSelection::kNone) | ||
.Build(); | ||
|
||
TestProfileSelection(selections, regular_profile(), regular_profile()); | ||
TestProfileSelection(selections, incognito_profile(), incognito_profile()); | ||
|
||
TestProfileSelection(selections, guest_profile(), nullptr); | ||
TestProfileSelection(selections, guest_profile_otr(), guest_profile_otr()); | ||
|
||
#if !BUILDFLAG(IS_CHROMEOS_ASH) && !BUILDFLAG(IS_ANDROID) | ||
TestProfileSelection(selections, system_profile(), nullptr); | ||
TestProfileSelection(selections, system_profile_otr(), nullptr); | ||
#endif // !BUILDFLAG(IS_CHROMEOS_ASH) && !BUILDFLAG(IS_ANDROID) | ||
} | ||
|
||
TEST_F(ProfileSelectionsTest, OnlyRegularProfile) { | ||
ProfileSelections selections = | ||
ProfileSelections::BuildServicesForRegularProfile(); | ||
|
||
TestProfileSelection(selections, regular_profile(), regular_profile()); | ||
TestProfileSelection(selections, incognito_profile(), nullptr); | ||
|
||
TestProfileSelection(selections, guest_profile(), nullptr); | ||
TestProfileSelection(selections, guest_profile_otr(), nullptr); | ||
|
||
#if !BUILDFLAG(IS_CHROMEOS_ASH) && !BUILDFLAG(IS_ANDROID) | ||
TestProfileSelection(selections, system_profile(), nullptr); | ||
TestProfileSelection(selections, system_profile_otr(), nullptr); | ||
#endif // !BUILDFLAG(IS_CHROMEOS_ASH) && !BUILDFLAG(IS_ANDROID) | ||
} | ||
|
||
TEST_F(ProfileSelectionsTest, RedirectedInIncognito) { | ||
ProfileSelections selections = | ||
ProfileSelections::BuildServicesRedirectedInIncognito(); | ||
|
||
TestProfileSelection(selections, regular_profile(), regular_profile()); | ||
TestProfileSelection(selections, incognito_profile(), regular_profile()); | ||
|
||
TestProfileSelection(selections, guest_profile(), nullptr); | ||
TestProfileSelection(selections, guest_profile_otr(), nullptr); | ||
|
||
#if !BUILDFLAG(IS_CHROMEOS_ASH) && !BUILDFLAG(IS_ANDROID) | ||
TestProfileSelection(selections, system_profile(), nullptr); | ||
TestProfileSelection(selections, system_profile_otr(), nullptr); | ||
#endif // !BUILDFLAG(IS_CHROMEOS_ASH) && !BUILDFLAG(IS_ANDROID) | ||
} | ||
|
||
TEST_F(ProfileSelectionsTest, RedirectedToOriginal) { | ||
ProfileSelections selections = | ||
ProfileSelections::BuildServicesRedirectedToOriginal(); | ||
|
||
TestProfileSelection(selections, regular_profile(), regular_profile()); | ||
TestProfileSelection(selections, incognito_profile(), regular_profile()); | ||
|
||
TestProfileSelection(selections, guest_profile(), guest_profile()); | ||
TestProfileSelection(selections, guest_profile_otr(), guest_profile()); | ||
|
||
#if !BUILDFLAG(IS_CHROMEOS_ASH) && !BUILDFLAG(IS_ANDROID) | ||
TestProfileSelection(selections, system_profile(), system_profile()); | ||
TestProfileSelection(selections, system_profile_otr(), system_profile()); | ||
#endif // !BUILDFLAG(IS_CHROMEOS_ASH) && !BUILDFLAG(IS_ANDROID) | ||
} | ||
|
||
TEST_F(ProfileSelectionsTest, ForAllProfiles) { | ||
ProfileSelections selections = | ||
ProfileSelections::BuildServicesForAllProfiles(); | ||
|
||
TestProfileSelection(selections, regular_profile(), regular_profile()); | ||
TestProfileSelection(selections, incognito_profile(), incognito_profile()); | ||
|
||
TestProfileSelection(selections, guest_profile(), guest_profile()); | ||
TestProfileSelection(selections, guest_profile_otr(), guest_profile_otr()); | ||
|
||
#if !BUILDFLAG(IS_CHROMEOS_ASH) && !BUILDFLAG(IS_ANDROID) | ||
TestProfileSelection(selections, system_profile(), system_profile()); | ||
TestProfileSelection(selections, system_profile_otr(), system_profile_otr()); | ||
#endif // !BUILDFLAG(IS_CHROMEOS_ASH) && !BUILDFLAG(IS_ANDROID) | ||
} | ||
|
||
TEST_F(ProfileSelectionsTest, NoProfiles) { | ||
ProfileSelections selections = | ||
ProfileSelections::BuildNoServicesForAllProfiles(); | ||
|
||
TestProfileSelection(selections, regular_profile(), nullptr); | ||
TestProfileSelection(selections, incognito_profile(), nullptr); | ||
|
||
TestProfileSelection(selections, guest_profile(), nullptr); | ||
TestProfileSelection(selections, guest_profile_otr(), nullptr); | ||
|
||
#if !BUILDFLAG(IS_CHROMEOS_ASH) && !BUILDFLAG(IS_ANDROID) | ||
TestProfileSelection(selections, system_profile(), nullptr); | ||
TestProfileSelection(selections, system_profile_otr(), nullptr); | ||
#endif // !BUILDFLAG(IS_CHROMEOS_ASH) && !BUILDFLAG(IS_ANDROID) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2022 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 "chrome/browser/profiles/profile_testing_helper.h" | ||
|
||
#include "chrome/test/base/testing_browser_process.h" | ||
#include "components/profile_metrics/browser_profile_type.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
ProfileTestingHelper::ProfileTestingHelper() | ||
: manager_(TestingBrowserProcess::GetGlobal()) {} | ||
|
||
ProfileTestingHelper::~ProfileTestingHelper() { | ||
manager_.DeleteAllTestingProfiles(); | ||
} | ||
|
||
void ProfileTestingHelper::SetUp() { | ||
ASSERT_TRUE(manager_.SetUp()); | ||
|
||
regular_profile_ = manager_.CreateTestingProfile("testing"); | ||
ASSERT_TRUE(regular_profile_); | ||
ASSERT_FALSE(regular_profile_->IsOffTheRecord()); | ||
ASSERT_TRUE(regular_profile_->IsRegularProfile()); | ||
incognito_profile_ = regular_profile_->GetPrimaryOTRProfile(true); | ||
ASSERT_TRUE(incognito_profile_); | ||
ASSERT_TRUE(incognito_profile_->IsOffTheRecord()); | ||
ASSERT_TRUE(incognito_profile_->IsIncognitoProfile()); | ||
|
||
guest_profile_ = manager_.CreateGuestProfile(); | ||
ASSERT_TRUE(guest_profile_); | ||
ASSERT_FALSE(guest_profile_->IsOffTheRecord()); | ||
ASSERT_TRUE(guest_profile_->IsGuestSession()); | ||
guest_profile_otr_ = guest_profile_->GetPrimaryOTRProfile(true); | ||
ASSERT_TRUE(guest_profile_otr_); | ||
ASSERT_TRUE(guest_profile_otr_->IsOffTheRecord()); | ||
ASSERT_TRUE(guest_profile_otr_->IsGuestSession()); | ||
|
||
#if !BUILDFLAG(IS_CHROMEOS_ASH) && !BUILDFLAG(IS_ANDROID) | ||
system_profile_ = manager_.CreateSystemProfile(); | ||
ASSERT_TRUE(system_profile_); | ||
ASSERT_FALSE(system_profile_->IsOffTheRecord()); | ||
ASSERT_TRUE(system_profile_->IsSystemProfile()); | ||
system_profile_otr_ = system_profile_->GetPrimaryOTRProfile(true); | ||
ASSERT_TRUE(system_profile_otr_); | ||
ASSERT_TRUE(system_profile_otr_->IsOffTheRecord()); | ||
ASSERT_TRUE(system_profile_otr_->IsSystemProfile()); | ||
#endif // !BUILDFLAG(IS_CHROMEOS_ASH) && !BUILDFLAG(IS_ANDROID) | ||
} |
Oops, something went wrong.