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 295
|
// Copyright 2024 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/commerce/core/feature_utils.h"
#include <memory>
#include <vector>
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "components/commerce/core/commerce_feature_list.h"
#include "components/commerce/core/mock_account_checker.h"
#include "components/commerce/core/product_specifications/mock_product_specifications_service.h"
#include "components/commerce/core/product_specifications/product_specifications_set.h"
#include "components/commerce/core/test_utils.h"
#include "components/optimization_guide/core/feature_registry/feature_registration.h"
#include "components/optimization_guide/core/optimization_guide_features.h"
#include "components/prefs/testing_pref_service.h"
#include "components/sync/base/data_type.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace commerce {
namespace {
class FeatureUtilsTest : public testing::Test {
public:
FeatureUtilsTest()
: prefs_(std::make_unique<TestingPrefServiceSimple>()),
account_checker_(std::make_unique<MockAccountChecker>()),
specifications_service_(
std::make_unique<MockProductSpecificationsService>()) {
MockAccountChecker::RegisterCommercePrefs(prefs_->registry());
account_checker_->SetPrefs(prefs_.get());
}
~FeatureUtilsTest() override = default;
protected:
// Set up so that all product specs checks pass by default.
void SetupProductSpecificationsEnabled() {
commerce::EnableProductSpecificationsDataFetch(account_checker_.get(),
prefs_.get());
// Default to having no sets.
ON_CALL(*specifications_service_, GetAllProductSpecifications())
.WillByDefault(
testing::Return(std::vector<ProductSpecificationsSet>()));
// TODO(356845106): Integrate country and locale checks.
}
base::test::ScopedFeatureList test_features_;
base::test::TaskEnvironment task_environment_;
std::unique_ptr<TestingPrefServiceSimple> prefs_;
std::unique_ptr<MockAccountChecker> account_checker_;
std::unique_ptr<MockProductSpecificationsService> specifications_service_;
};
TEST_F(FeatureUtilsTest, CanLoadProductSpecificationsFullPageUi_HasFlag) {
test_features_.InitAndEnableFeature(kProductSpecifications);
ASSERT_TRUE(CanLoadProductSpecificationsFullPageUi(account_checker_.get()));
}
TEST_F(FeatureUtilsTest, CanLoadProductSpecificationsFullPageUi_NoFlag) {
ASSERT_FALSE(CanLoadProductSpecificationsFullPageUi(account_checker_.get()));
}
TEST_F(FeatureUtilsTest, CanManageProductSpecificationsSets_NullParams) {
ASSERT_FALSE(CanManageProductSpecificationsSets(nullptr, nullptr));
}
TEST_F(FeatureUtilsTest, CanManageProductSpecificationsSets_HasSets_NoFlag) {
SetupProductSpecificationsEnabled();
std::vector<ProductSpecificationsSet> sets;
sets.push_back(ProductSpecificationsSet(
"10000000-0000-0000-0000-000000000000", 0, 0, std::vector<GURL>{}, ""));
ON_CALL(*specifications_service_, GetAllProductSpecifications())
.WillByDefault(testing::Return(sets));
ASSERT_FALSE(CanManageProductSpecificationsSets(
account_checker_.get(), specifications_service_.get()));
}
TEST_F(FeatureUtilsTest,
CanManageProductSpecificationsSets_HasSets_HasFlag_NoFetch) {
test_features_.InitAndEnableFeature(kProductSpecifications);
SetupProductSpecificationsEnabled();
std::vector<ProductSpecificationsSet> sets;
sets.push_back(ProductSpecificationsSet(
"10000000-0000-0000-0000-000000000000", 0, 0, std::vector<GURL>{}, ""));
ON_CALL(*specifications_service_, GetAllProductSpecifications())
.WillByDefault(testing::Return(sets));
// Before turning off MSBB, we should be allowed to fetch.
ASSERT_TRUE(CanFetchProductSpecificationsData(account_checker_.get()));
// Turning off MSBB should block fetches for new data from happening.
account_checker_->SetAnonymizedUrlDataCollectionEnabled(false);
ASSERT_FALSE(CanFetchProductSpecificationsData(account_checker_.get()));
ASSERT_TRUE(CanManageProductSpecificationsSets(
account_checker_.get(), specifications_service_.get()));
}
TEST_F(FeatureUtilsTest, CanManageProductSpecificationsSets_NoSets_HasFlag) {
test_features_.InitAndEnableFeature(kProductSpecifications);
SetupProductSpecificationsEnabled();
ASSERT_EQ(0u, specifications_service_->GetAllProductSpecifications().size());
ASSERT_TRUE(CanManageProductSpecificationsSets(
account_checker_.get(), specifications_service_.get()));
}
TEST_F(FeatureUtilsTest, CanManageProductSpecificationsSets_NoSets_NoFlag) {
SetupProductSpecificationsEnabled();
ASSERT_EQ(0u, specifications_service_->GetAllProductSpecifications().size());
ASSERT_FALSE(CanManageProductSpecificationsSets(
account_checker_.get(), specifications_service_.get()));
}
TEST_F(FeatureUtilsTest, CanFetchProductSpecificationsData_AllRequirements) {
test_features_.InitAndEnableFeature(kProductSpecifications);
SetupProductSpecificationsEnabled();
ASSERT_TRUE(CanFetchProductSpecificationsData(account_checker_.get()));
}
TEST_F(FeatureUtilsTest, CanFetchProductSpecificationsData_NoMSBB) {
test_features_.InitAndEnableFeature(kProductSpecifications);
SetupProductSpecificationsEnabled();
// We should be able to fetch data before turning off msbb.
ASSERT_TRUE(CanFetchProductSpecificationsData(account_checker_.get()));
account_checker_->SetAnonymizedUrlDataCollectionEnabled(false);
ASSERT_FALSE(CanFetchProductSpecificationsData(account_checker_.get()));
}
TEST_F(FeatureUtilsTest, CanFetchProductSpecificationsData_NoSync) {
test_features_.InitWithFeatures(
{kProductSpecifications,
optimization_guide::features::kAiSettingsPageEnterpriseDisabledUi},
{});
SetupProductSpecificationsEnabled();
// We should be able to fetch data before turning off sync.
ASSERT_TRUE(CanFetchProductSpecificationsData(account_checker_.get()));
ASSERT_TRUE(IsProductSpecificationsSettingVisible(account_checker_.get()));
ON_CALL(*account_checker_, IsSyncTypeEnabled)
.WillByDefault(testing::Return(false));
ASSERT_FALSE(CanFetchProductSpecificationsData(account_checker_.get()));
ASSERT_FALSE(IsProductSpecificationsSettingVisible(account_checker_.get()));
}
TEST_F(FeatureUtilsTest, CanFetchProductSpecificationsData_SyncInactive) {
test_features_.InitWithFeatures(
{kProductSpecifications,
optimization_guide::features::kAiSettingsPageEnterpriseDisabledUi},
{});
SetupProductSpecificationsEnabled();
// We should be able to fetch data before turning off sync.
ASSERT_TRUE(CanFetchProductSpecificationsData(account_checker_.get()));
ASSERT_TRUE(IsProductSpecificationsSettingVisible(account_checker_.get()));
account_checker_->SetSyncAvailable(false);
ASSERT_FALSE(CanFetchProductSpecificationsData(account_checker_.get()));
ASSERT_FALSE(IsProductSpecificationsSettingVisible(account_checker_.get()));
}
TEST_F(FeatureUtilsTest,
CanFetchProductSpecificationsData_NoEnterpriseNoSettings) {
test_features_.InitWithFeatures(
{kProductSpecifications},
{optimization_guide::features::kAiSettingsPageEnterpriseDisabledUi});
SetupProductSpecificationsEnabled();
// We should be able to fetch data before turning off enterprise.
ASSERT_TRUE(CanFetchProductSpecificationsData(account_checker_.get()));
// 1 is enabled but without logging.
SetTabCompareEnterprisePolicyPref(prefs_.get(), 1);
ASSERT_TRUE(CanFetchProductSpecificationsData(account_checker_.get()));
// 2 is the disabled enterprise state for the feature.
SetTabCompareEnterprisePolicyPref(prefs_.get(), 2);
ASSERT_FALSE(CanFetchProductSpecificationsData(account_checker_.get()));
}
TEST_F(FeatureUtilsTest,
CanFetchProductSpecificationsData_NoEnterpriseWithSettings) {
test_features_.InitWithFeatures(
{kProductSpecifications,
optimization_guide::features::kAiSettingsPageEnterpriseDisabledUi},
{});
SetupProductSpecificationsEnabled();
// We should be able to fetch data before turning off enterprise.
ASSERT_TRUE(CanFetchProductSpecificationsData(account_checker_.get()));
ASSERT_TRUE(IsProductSpecificationsSettingVisible(account_checker_.get()));
// 1 is enabled but without logging.
SetTabCompareEnterprisePolicyPref(prefs_.get(), 1);
ASSERT_TRUE(CanFetchProductSpecificationsData(account_checker_.get()));
ASSERT_TRUE(IsProductSpecificationsSettingVisible(account_checker_.get()));
// 2 is the disabled enterprise state for the feature.
SetTabCompareEnterprisePolicyPref(prefs_.get(), 2);
ASSERT_FALSE(CanFetchProductSpecificationsData(account_checker_.get()));
ASSERT_TRUE(IsProductSpecificationsSettingVisible(account_checker_.get()));
}
TEST_F(FeatureUtilsTest,
CanFetchProductSpecificationsData_EnterpriseQualityLogging) {
test_features_.InitAndEnableFeature(kProductSpecifications);
SetupProductSpecificationsEnabled();
// We should be able to fetch data before turning off enterprise.
ASSERT_TRUE(IsProductSpecificationsQualityLoggingAllowed(prefs_.get()));
// 1 is enabled but without logging.
SetTabCompareEnterprisePolicyPref(prefs_.get(), 1);
ASSERT_FALSE(IsProductSpecificationsQualityLoggingAllowed(prefs_.get()));
// 2 is the disabled enterprise state for the feature.
SetTabCompareEnterprisePolicyPref(prefs_.get(), 2);
ASSERT_FALSE(IsProductSpecificationsQualityLoggingAllowed(prefs_.get()));
}
TEST_F(FeatureUtilsTest,
IsProductSpecificationsAllowedForEnterprise_EnterpriseNotSet) {
// Without any other setup, enterprise should be allowed.
ASSERT_TRUE(IsProductSpecificationsAllowedForEnterprise(prefs_.get()));
// 2 is the disabled enterprise state for the feature.
SetTabCompareEnterprisePolicyPref(prefs_.get(), 2);
ASSERT_FALSE(IsProductSpecificationsAllowedForEnterprise(prefs_.get()));
}
TEST_F(FeatureUtilsTest,
IsProductSpecificationsAllowedForEnterprise_EnterpriseNotManaged) {
TestingPrefServiceSimple prefs;
// If the preference is local, we shouldn't be respecting it.
MockAccountChecker::RegisterCommercePrefs(prefs.registry());
prefs.SetInteger(
optimization_guide::prefs::kProductSpecificationsEnterprisePolicyAllowed,
2);
ASSERT_TRUE(IsProductSpecificationsAllowedForEnterprise(&prefs));
}
TEST_F(FeatureUtilsTest,
CanFetchProductSpecificationsData_BlockedByParentalControls) {
test_features_.InitAndEnableFeature(kProductSpecifications);
SetupProductSpecificationsEnabled();
// We should be able to fetch data before turning off enterprise.
ASSERT_TRUE(CanFetchProductSpecificationsData(account_checker_.get()));
account_checker_->SetIsSubjectToParentalControls(true);
ASSERT_FALSE(CanFetchProductSpecificationsData(account_checker_.get()));
}
TEST_F(FeatureUtilsTest,
CanFetchProductSpecificationsData_ModelExecutionNotAllowed) {
test_features_.InitAndEnableFeature(kProductSpecifications);
SetupProductSpecificationsEnabled();
// We should be able to fetch data before turning off enterprise.
ASSERT_TRUE(CanFetchProductSpecificationsData(account_checker_.get()));
account_checker_->SetCanUseModelExecutionFeatures(false);
ASSERT_FALSE(CanFetchProductSpecificationsData(account_checker_.get()));
}
} // namespace
} // namespace commerce
|