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
|
// 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 "chrome/browser/password_manager/chrome_password_change_service.h"
#include "base/command_line.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/mock_callback.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "chrome/browser/optimization_guide/mock_optimization_guide_keyed_service.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service_factory.h"
#include "chrome/common/chrome_switches.h"
#include "components/affiliations/core/browser/mock_affiliation_service.h"
#include "components/password_manager/core/browser/features/password_features.h"
#include "components/password_manager/core/browser/mock_password_feature_manager.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
struct TestCase {
using TupleT = std::tuple<bool, bool, bool>;
explicit TestCase(TupleT configuration)
: is_generation_available(std::get<0>(configuration)),
is_model_execution_allowed(std::get<1>(configuration)),
is_feature_enabled(std::get<2>(configuration)) {}
bool expected_outcome() const {
#if BUILDFLAG(IS_ANDROID)
return false;
#else
return is_generation_available & is_model_execution_allowed &
is_feature_enabled;
#endif // BUILDFLAG(IS_ANDROID)
}
const bool is_generation_available;
const bool is_model_execution_allowed;
const bool is_feature_enabled;
};
} // namespace
class ChromePasswordChangeServiceBase {
public:
ChromePasswordChangeServiceBase() {
auto feature_manager = std::make_unique<
testing::StrictMock<password_manager::MockPasswordFeatureManager>>();
feature_manager_ = feature_manager.get();
change_service_ = std::make_unique<ChromePasswordChangeService>(
&mock_affiliation_service_, &mock_optimization_service_,
std::move(feature_manager));
}
~ChromePasswordChangeServiceBase() = default;
affiliations::MockAffiliationService& affiliation_service() {
return mock_affiliation_service_;
}
MockOptimizationGuideKeyedService& mock_optimization_service() {
return mock_optimization_service_;
}
password_manager::PasswordChangeServiceInterface* change_service() {
return change_service_.get();
}
password_manager::MockPasswordFeatureManager* feature_manager() {
return feature_manager_;
}
private:
content::BrowserTaskEnvironment task_environment_;
base::test::ScopedFeatureList feature_list_{
password_manager::features::kImprovedPasswordChangeService};
testing::StrictMock<affiliations::MockAffiliationService>
mock_affiliation_service_;
testing::StrictMock<MockOptimizationGuideKeyedService>
mock_optimization_service_;
std::unique_ptr<ChromePasswordChangeService> change_service_;
raw_ptr<password_manager::MockPasswordFeatureManager> feature_manager_;
};
class ChromePasswordChangeServiceTest : public testing::Test,
public ChromePasswordChangeServiceBase {
};
#if !BUILDFLAG(IS_ANDROID)
TEST_F(ChromePasswordChangeServiceTest, PasswordChangeSupportedForURL) {
base::HistogramTester histogram_tester;
GURL url("https://test.com/");
EXPECT_CALL(affiliation_service(), GetChangePasswordURL(url))
.WillOnce(testing::Return(GURL("https://test.com/password/")));
EXPECT_CALL(mock_optimization_service(), ShouldModelExecutionBeAllowedForUser)
.WillOnce(testing::Return(true));
EXPECT_CALL(*feature_manager(), IsGenerationEnabled)
.WillOnce(testing::Return(true));
EXPECT_TRUE(change_service()->IsPasswordChangeSupported(url));
histogram_tester.ExpectUniqueSample(
ChromePasswordChangeService::kHasPasswordChangeUrlHistogram, true, 1);
}
TEST_F(ChromePasswordChangeServiceTest, PasswordChangeNotSupportedForUrl) {
base::HistogramTester histogram_tester;
GURL url("https://test.com/");
EXPECT_CALL(affiliation_service(), GetChangePasswordURL(url))
.WillOnce(testing::Return(GURL()));
EXPECT_CALL(mock_optimization_service(), ShouldModelExecutionBeAllowedForUser)
.WillOnce(testing::Return(true));
EXPECT_CALL(*feature_manager(), IsGenerationEnabled)
.WillOnce(testing::Return(true));
EXPECT_FALSE(change_service()->IsPasswordChangeSupported(url));
histogram_tester.ExpectUniqueSample(
ChromePasswordChangeService::kHasPasswordChangeUrlHistogram, false, 1);
}
TEST_F(ChromePasswordChangeServiceTest,
PasswordChangeNotSupportedSettingNotVisible) {
GURL url("https://test.com/");
EXPECT_CALL(affiliation_service(), GetChangePasswordURL).Times(0);
EXPECT_CALL(mock_optimization_service(), ShouldModelExecutionBeAllowedForUser)
.WillOnce(testing::Return(false));
EXPECT_CALL(*feature_manager(), IsGenerationEnabled)
.WillOnce(testing::Return(true));
EXPECT_FALSE(change_service()->IsPasswordChangeSupported(url));
}
TEST_F(ChromePasswordChangeServiceTest,
PasswordChangeSupportedIfCommandLineArgProvided) {
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
switches::kPasswordChangeUrl, "https://test.com/new_password/");
GURL url("https://test.com/");
EXPECT_CALL(affiliation_service(), GetChangePasswordURL).Times(0);
EXPECT_TRUE(change_service()->IsPasswordChangeSupported(url));
}
TEST_F(ChromePasswordChangeServiceTest,
PasswordChangeSupportedIfPSLMatchedInArg) {
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
switches::kPasswordChangeUrl, "https://test.com/new_password/");
GURL url("https://www.test.com/");
EXPECT_CALL(affiliation_service(), GetChangePasswordURL).Times(0);
EXPECT_TRUE(change_service()->IsPasswordChangeSupported(url));
}
#endif // !BUILDFLAG(IS_ANDROID)
class ChromePasswordChangeServiceAvailabilityTest
: public testing::TestWithParam<TestCase>,
public ChromePasswordChangeServiceBase {
public:
ChromePasswordChangeServiceAvailabilityTest() {
feature_list_.InitWithFeatureState(
password_manager::features::kImprovedPasswordChangeService,
GetParam().is_feature_enabled);
}
private:
base::test::ScopedFeatureList feature_list_;
};
TEST_P(ChromePasswordChangeServiceAvailabilityTest, TestWithNoArgs) {
#if !BUILDFLAG(IS_ANDROID)
EXPECT_CALL(*feature_manager(), IsGenerationEnabled)
.WillOnce(testing::Return(GetParam().is_generation_available));
if (GetParam().is_generation_available) {
EXPECT_CALL(mock_optimization_service(),
ShouldModelExecutionBeAllowedForUser)
.WillOnce(testing::Return(GetParam().is_model_execution_allowed));
}
#endif // !BUILDFLAG(IS_ANDROID)
EXPECT_EQ(change_service()->IsPasswordChangeAvailable(),
GetParam().expected_outcome());
}
TEST_P(ChromePasswordChangeServiceAvailabilityTest, TestWithChangePwdUrlArg) {
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
switches::kPasswordChangeUrl, "https://test.com/new_password/");
EXPECT_CALL(*feature_manager(), IsGenerationEnabled).Times(0);
EXPECT_CALL(mock_optimization_service(), ShouldModelExecutionBeAllowedForUser)
.Times(0);
#if !BUILDFLAG(IS_ANDROID)
EXPECT_TRUE(change_service()->IsPasswordChangeAvailable());
#else
EXPECT_FALSE(change_service()->IsPasswordChangeAvailable());
#endif // !BUILDFLAG(IS_ANDROID)
}
INSTANTIATE_TEST_SUITE_P(
Availability,
ChromePasswordChangeServiceAvailabilityTest,
testing::ConvertGenerator<TestCase::TupleT>(
testing::Combine(testing::Bool(), testing::Bool(), testing::Bool())),
[](const ::testing::TestParamInfo<TestCase>& info) {
std::string test_name;
test_name +=
info.param.is_generation_available ? "GenerationOn" : "GenerationOff";
test_name += info.param.is_model_execution_allowed ? "ExecutionOn"
: "ExecutionOff";
test_name += info.param.is_feature_enabled ? "FeatureOn" : "FeatureOff";
return test_name;
});
|