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
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/variations/service/google_groups_manager.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/test/scoped_feature_list.h"
#include "base/values.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/prefs/testing_pref_service.h"
#include "components/sync/test/test_sync_service.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "components/variations/pref_names.h"
#include "components/variations/service/google_groups_manager_prefs.h"
#include "components/variations/variations_seed_processor.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
class GoogleGroupsManagerTest : public ::testing::Test {
public:
GoogleGroupsManagerTest() {
key_ = "Default";
target_prefs_.registry()->RegisterDictionaryPref(
variations::prefs::kVariationsGoogleGroups);
GoogleGroupsManager::RegisterProfilePrefs(source_prefs_.registry());
}
~GoogleGroupsManagerTest() override = default;
void SetSourcePref(std::vector<std::string> groups) {
base::Value::List pref_groups_list;
for (const std::string& group : groups) {
base::Value::Dict group_dict;
group_dict.Set(variations::kDogfoodGroupsSyncPrefGaiaIdKey, group);
pref_groups_list.Append(std::move(group_dict));
}
source_prefs_.SetList(
#if BUILDFLAG(IS_CHROMEOS)
variations::kOsDogfoodGroupsSyncPrefName,
#else
variations::kDogfoodGroupsSyncPrefName,
#endif
std::move(pref_groups_list));
}
void SetTargetPref(std::vector<std::string> groups) {
base::Value::Dict groups_dict;
base::Value::List pref_groups_list;
for (const std::string& group : groups) {
pref_groups_list.Append(group);
}
groups_dict.Set(key_, std::move(pref_groups_list));
target_prefs_.SetDict(variations::prefs::kVariationsGoogleGroups,
std::move(groups_dict));
}
void CheckSourcePrefCleared() {
EXPECT_TRUE(source_prefs_
.GetList(
#if BUILDFLAG(IS_CHROMEOS)
variations::kOsDogfoodGroupsSyncPrefName
#else
variations::kDogfoodGroupsSyncPrefName
#endif
)
.empty());
}
void CheckTargetPref(std::vector<std::string> expected_groups) {
base::Value::List expected_list;
for (const std::string& group : expected_groups) {
expected_list.Append(group);
}
const base::Value::List* actual_list =
target_prefs_.GetDict(variations::prefs::kVariationsGoogleGroups)
.FindList(key_);
EXPECT_EQ(*actual_list, expected_list);
}
protected:
TestingPrefServiceSimple target_prefs_;
std::string key_;
sync_preferences::TestingPrefServiceSyncable source_prefs_;
};
TEST_F(GoogleGroupsManagerTest, NoSyncGroupsEmptyListWritten) {
GoogleGroupsManager google_groups_updater(target_prefs_, key_,
source_prefs_);
// Check there are no groups in the target pref.
CheckTargetPref({});
}
TEST_F(GoogleGroupsManagerTest, EmptySyncGroupsEmptyListWritten) {
GoogleGroupsManager google_groups_updater(target_prefs_, key_,
source_prefs_);
SetSourcePref({});
// Check there are no groups in the target pref.
CheckTargetPref({});
}
TEST_F(GoogleGroupsManagerTest, SourceSyncGroupsWrittenToEmptyTarget) {
GoogleGroupsManager google_groups_updater(target_prefs_, key_,
source_prefs_);
SetSourcePref({"123", "456"});
// Check the groups have been copied to the target pref.
CheckTargetPref({"123", "456"});
}
TEST_F(GoogleGroupsManagerTest,
SourceSyncGroupsWrittenToNonEmptyTarget) {
GoogleGroupsManager google_groups_updater(target_prefs_, key_,
source_prefs_);
SetTargetPref({"123", "456"});
// Now update the source prefs (keeping one group, deleting one group, and
// adding one group).
SetSourcePref({"123", "789"});
CheckTargetPref({"123", "789"});
}
TEST_F(GoogleGroupsManagerTest, ClearProfilePrefsNotPreviouslySet) {
syncer::TestSyncService sync_service;
GoogleGroupsManager google_groups_updater(target_prefs_, key_,
source_prefs_);
google_groups_updater.OnSyncServiceInitialized(&sync_service);
// This just checks that ClearSigninScopedState() deals with the case where
// the source pref was unset (i.e. is a no-op and doesn't crash).
sync_service.SetSignedOut();
google_groups_updater.OnStateChanged(&sync_service);
CheckTargetPref({});
}
TEST_F(GoogleGroupsManagerTest, ClearProfilePrefsClearsTargetPref) {
syncer::TestSyncService sync_service;
GoogleGroupsManager google_groups_updater(target_prefs_, key_,
source_prefs_);
google_groups_updater.OnSyncServiceInitialized(&sync_service);
SetSourcePref({"123", "456"});
CheckTargetPref({"123", "456"});
sync_service.SetSignedOut();
google_groups_updater.OnStateChanged(&sync_service);
// Check the source and target prefs have been cleared.
CheckSourcePrefCleared();
CheckTargetPref({});
}
// Tests that `IsFeatureGroupControlled` checks whether the internal feature
// parameter that contains the ids in the google_groups filter is non-empty.
TEST_F(GoogleGroupsManagerTest, IsFeatureGroupControlled) {
static BASE_FEATURE(kGroupControlledFeature, "GroupControlledFeature",
base::FEATURE_DISABLED_BY_DEFAULT);
static BASE_FEATURE(kOtherFeature, "OtherFeature",
base::FEATURE_DISABLED_BY_DEFAULT);
auto feature_list = std::make_unique<base::FeatureList>();
GoogleGroupsManager google_groups_updater(target_prefs_, key_, source_prefs_);
base::test::FeatureRefAndParams enabled_feature{
kGroupControlledFeature,
{{variations::internal::kGoogleGroupFeatureParamName, "1234"}}};
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitWithFeaturesAndParameters({enabled_feature},
/*disabled_features=*/{});
EXPECT_TRUE(
GoogleGroupsManager::IsFeatureGroupControlled(kGroupControlledFeature));
EXPECT_FALSE(GoogleGroupsManager::IsFeatureGroupControlled(kOtherFeature));
}
// Tests that `IsFeatureEnabledForProfile` returns true if the feature is
// enabled and the source prefs of the `GoogleGroupsManager` contain at
// least one of the google_groups specified for the feature.
TEST_F(GoogleGroupsManagerTest, IsFeatureEnabledForProfile) {
static BASE_FEATURE(kSampleFeature, "SampleFeature",
base::FEATURE_DISABLED_BY_DEFAULT);
auto feature_list = std::make_unique<base::FeatureList>();
GoogleGroupsManager google_groups_updater(target_prefs_, key_,
source_prefs_);
constexpr char kRelevantGroupId[] = "1234";
base::test::FeatureRefAndParams enabled_feature{
kSampleFeature,
{{variations::internal::kGoogleGroupFeatureParamName, kRelevantGroupId}}};
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitWithFeaturesAndParameters({enabled_feature},
/*disabled_features=*/{});
SetSourcePref({"123", "789"});
EXPECT_TRUE(base::FeatureList::IsEnabled(kSampleFeature));
EXPECT_FALSE(
google_groups_updater.IsFeatureEnabledForProfile(kSampleFeature));
SetSourcePref({"123", kRelevantGroupId});
EXPECT_TRUE(base::FeatureList::IsEnabled(kSampleFeature));
EXPECT_TRUE(google_groups_updater.IsFeatureEnabledForProfile(kSampleFeature));
}
// Tests that `IsFeatureEnabledForProfile` can deal properly with google_groups
// parameters of size longer than 1.
TEST_F(GoogleGroupsManagerTest,
IsFeatureEnabledForProfileMultipleGroups) {
static BASE_FEATURE(kSampleFeature, "SampleFeature",
base::FEATURE_DISABLED_BY_DEFAULT);
auto feature_list = std::make_unique<base::FeatureList>();
GoogleGroupsManager google_groups_updater(target_prefs_, key_,
source_prefs_);
constexpr char kRelevantGroupId[] = "1234";
base::test::FeatureRefAndParams enabled_feature{
kSampleFeature,
{{variations::internal::kGoogleGroupFeatureParamName,
base::StrCat({"645",
variations::internal::kGoogleGroupFeatureParamSeparator,
kRelevantGroupId})}}};
SetSourcePref({kRelevantGroupId, "789"});
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitWithFeaturesAndParameters({enabled_feature},
/*disabled_features=*/{});
EXPECT_TRUE(base::FeatureList::IsEnabled(kSampleFeature));
EXPECT_TRUE(google_groups_updater.IsFeatureEnabledForProfile(kSampleFeature));
}
// Tests that `IsFeatureEnabledForProfile` is always false if the feature itself
// is disabled.
TEST_F(GoogleGroupsManagerTest,
IsFeatureEnabledForProfileForDisabledFeature) {
static BASE_FEATURE(kSampleFeature, "SampleFeature",
base::FEATURE_DISABLED_BY_DEFAULT);
auto feature_list = std::make_unique<base::FeatureList>();
GoogleGroupsManager google_groups_updater(target_prefs_, key_,
source_prefs_);
constexpr char kTrialName[] = "SampleTrial";
constexpr char kGroupName[] = "SampleGroup";
constexpr char kRelevantGroupId[] = "1234";
base::FieldTrialList::CreateFieldTrial(kTrialName, kGroupName);
ASSERT_TRUE(base::AssociateFieldTrialParams(
kTrialName, kGroupName,
{{variations::internal::kGoogleGroupFeatureParamName,
kRelevantGroupId}}));
SetSourcePref({"123", kRelevantGroupId});
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitWithFeatureList(std::move(feature_list));
EXPECT_FALSE(base::FeatureList::IsEnabled(kSampleFeature));
EXPECT_FALSE(
google_groups_updater.IsFeatureEnabledForProfile(kSampleFeature));
}
|