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
|
// Copyright 2025 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/ui/lens/lens_search_feature_flag_utils.h"
#include <memory>
#include <optional>
#include <tuple>
#include <vector>
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/autocomplete/aim_eligibility_service_factory.h"
#include "chrome/browser/autocomplete/chrome_aim_eligibility_service.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/scoped_browser_locale.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/lens/lens_features.h"
#include "components/prefs/pref_service.h"
#include "components/variations/service/variations_service.h"
#include "content/public/browser/browser_context.h"
#include "content/public/test/browser_test.h"
#include "testing/gtest/include/gtest/gtest.h"
// A test AimEligibilityService that returns a fixed eligibility value.
class TestingAimEligibilityService : public ChromeAimEligibilityService {
public:
explicit TestingAimEligibilityService(
bool is_locally_eligible,
bool is_server_eligible,
bool server_eligibility_enabled,
PrefService& pref_service,
TemplateURLService* template_url_service)
: ChromeAimEligibilityService(pref_service,
template_url_service,
/*url_loader_factory=*/nullptr,
/*identity_manager=*/nullptr),
is_locally_eligible_(is_locally_eligible),
is_server_eligible_(is_server_eligible),
server_eligibility_enabled_(server_eligibility_enabled) {}
~TestingAimEligibilityService() override = default;
bool IsAimLocallyEligible() const override { return is_locally_eligible_; }
bool IsServerEligibilityEnabled() const override {
return server_eligibility_enabled_;
}
bool IsAimEligible() const override {
if (!IsAimLocallyEligible()) {
return false;
}
if (IsServerEligibilityEnabled()) {
return is_server_eligible_;
}
return true;
}
private:
bool is_locally_eligible_;
bool is_server_eligible_;
bool server_eligibility_enabled_;
};
class LensSearchFeatureFlagsUtilsBrowserTestBase : public InProcessBrowserTest {
public:
LensSearchFeatureFlagsUtilsBrowserTestBase() = default;
~LensSearchFeatureFlagsUtilsBrowserTestBase() override = default;
protected:
void SetUpAimEligibilityService(bool is_locally_eligible,
bool is_server_eligible,
bool server_eligibility_enabled) {
AimEligibilityServiceFactory::GetInstance()->SetTestingFactory(
browser()->profile(),
base::BindLambdaForTesting(
[is_locally_eligible, is_server_eligible,
server_eligibility_enabled](content::BrowserContext* context) {
Profile* profile = Profile::FromBrowserContext(context);
return static_cast<std::unique_ptr<KeyedService>>(
std::make_unique<TestingAimEligibilityService>(
is_locally_eligible, is_server_eligible,
server_eligibility_enabled, *profile->GetPrefs(),
TemplateURLServiceFactory::GetForProfile(profile)));
}));
}
};
// Test fixture with kLensSearchAimM3 feature enabled.
class LensSearchFeatureFlagsUtilsAimM3EnabledTest
: public LensSearchFeatureFlagsUtilsBrowserTestBase {
public:
LensSearchFeatureFlagsUtilsAimM3EnabledTest() = default;
~LensSearchFeatureFlagsUtilsAimM3EnabledTest() override = default;
protected:
base::test::ScopedFeatureList feature_list_{lens::features::kLensSearchAimM3};
};
IN_PROC_BROWSER_TEST_F(LensSearchFeatureFlagsUtilsAimM3EnabledTest,
TestIsAimM3Enabled_IsTrue) {
SetUpAimEligibilityService(/*is_locally_eligible=*/true,
/*is_server_eligible=*/true,
/*server_eligibility_enabled=*/true);
EXPECT_TRUE(lens::IsAimM3Enabled(browser()->profile()));
// Returns true when server eligibility checking is disabled as long as the
// local eligibility check passes.
SetUpAimEligibilityService(/*is_locally_eligible=*/true,
/*is_server_eligible=*/false,
/*server_eligibility_enabled=*/false);
EXPECT_TRUE(lens::IsAimM3Enabled(browser()->profile()));
}
IN_PROC_BROWSER_TEST_F(LensSearchFeatureFlagsUtilsAimM3EnabledTest,
TestIsAimM3Enabled_WithIneligibleService_IsFalse) {
SetUpAimEligibilityService(/*is_locally_eligible=*/false,
/*is_server_eligible=*/true,
/*server_eligibility_enabled=*/true);
EXPECT_FALSE(lens::IsAimM3Enabled(browser()->profile()));
SetUpAimEligibilityService(/*is_locally_eligible=*/true,
/*is_server_eligible=*/false,
/*server_eligibility_enabled=*/true);
EXPECT_FALSE(lens::IsAimM3Enabled(browser()->profile()));
}
// Test fixture with kLensSearchAimM3 feature enabled and eligibility check
// disabled.
class LensSearchFeatureFlagsUtilsAimM3EnabledAndEligibilityCheckDisabledTest
: public LensSearchFeatureFlagsUtilsBrowserTestBase {
public:
LensSearchFeatureFlagsUtilsAimM3EnabledAndEligibilityCheckDisabledTest() {
// Initialize the feature list to enable kLensSearchAimM3
// and set the 'use-aim-eligibility-service' param to false.
feature_list_.InitAndEnableFeatureWithParameters(
lens::features::kLensSearchAimM3,
{{"use-aim-eligibility-service", "false"}});
}
~LensSearchFeatureFlagsUtilsAimM3EnabledAndEligibilityCheckDisabledTest()
override = default;
protected:
base::test::ScopedFeatureList feature_list_;
};
IN_PROC_BROWSER_TEST_F(
LensSearchFeatureFlagsUtilsAimM3EnabledAndEligibilityCheckDisabledTest,
TestIsAimM3EnabledButEligibilityCheckDisabled_IsTrue) {
// Returns true when the eligibility check is disabled.
SetUpAimEligibilityService(/*is_locally_eligible=*/false,
/*is_server_eligible=*/false,
/*server_eligibility_enabled=*/false);
EXPECT_TRUE(lens::IsAimM3Enabled(browser()->profile()));
}
// Test fixture with kLensSearchAimM3 feature disabled.
class LensSearchFeatureFlagsUtilsAimM3DisabledTest
: public LensSearchFeatureFlagsUtilsBrowserTestBase {
public:
LensSearchFeatureFlagsUtilsAimM3DisabledTest() = default;
~LensSearchFeatureFlagsUtilsAimM3DisabledTest() override = default;
};
IN_PROC_BROWSER_TEST_F(LensSearchFeatureFlagsUtilsAimM3DisabledTest,
TestIsAimM3Enabled_WithFlagDisabled_IsFalse) {
SetUpAimEligibilityService(/*is_locally_eligible=*/true,
/*is_server_eligible=*/true,
/*server_eligibility_enabled=*/true);
EXPECT_FALSE(lens::IsAimM3Enabled(browser()->profile()));
}
|