#include "chrome/browser/ui/autofill/autofill_client_provider.h"
#include <memory>
#include "base/test/metrics/histogram_tester.h"
#include "base/test/mock_callback.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/autofill/android/android_autofill_availability_status.h"
#include "chrome/browser/ui/autofill/autofill_client_provider_factory.h"
#include "chrome/test/base/testing_profile.h"
#include "components/autofill/content/browser/test_content_autofill_client.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/autofill_prefs.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace autofill {
namespace {
#if BUILDFLAG(IS_ANDROID)
constexpr const char* kAvailabilityMetric =
"Autofill.AndroidAutofillAvailabilityStatus";
constexpr const char* kResetPrefMetric = "Autofill.ResetAutofillPrefToChrome";
#endif
class AutofillClientProviderBaseTest : public testing::Test { … };
TEST_F(AutofillClientProviderBaseTest, ProvidesServiceInNonIncognito) { … }
TEST_F(AutofillClientProviderBaseTest, ProvidesServiceInIncognito) { … }
TEST_F(AutofillClientProviderBaseTest, ProvidesNoServiceWithoutProfile) { … }
TEST_F(AutofillClientProviderBaseTest, UsesBuiltInAutofillForDisabledPref) { … }
#if BUILDFLAG(IS_ANDROID)
class AutofillClientProviderLegacyTest : public AutofillClientProviderBaseTest {
public:
void SetUp() override {
AutofillClientProviderBaseTest::SetUp();
scoped_feature_list_.InitAndDisableFeature(
features::kAutofillVirtualViewStructureAndroid);
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
TEST_F(AutofillClientProviderLegacyTest, AlwaysCreatesChromeClient) {
base::HistogramTester histogram_tester;
prefs()->SetBoolean(prefs::kAutofillUsingVirtualViewStructure, true);
EXPECT_FALSE(provider().uses_platform_autofill());
histogram_tester.ExpectUniqueSample(
kAvailabilityMetric, AndroidAutofillAvailabilityStatus::kSettingTurnedOff,
1);
}
class AutofillClientProviderTest : public AutofillClientProviderBaseTest {
public:
AutofillClientProviderTest() {
scoped_feature_list_.InitAndEnableFeatureWithParameters(
features::kAutofillVirtualViewStructureAndroid,
{{features::kAutofillVirtualViewStructureAndroidSkipsCompatibilityCheck
.name,
"skip_all_checks"}});
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
TEST_F(AutofillClientProviderTest, CreateAndroidClientForEnabledPref) {
base::HistogramTester histogram_tester;
prefs()->SetBoolean(prefs::kAutofillUsingVirtualViewStructure, true);
EXPECT_TRUE(provider().uses_platform_autofill());
prefs()->SetBoolean(prefs::kAutofillUsingVirtualViewStructure, false);
EXPECT_TRUE(provider().uses_platform_autofill());
histogram_tester.ExpectUniqueSample(kResetPrefMetric, false, 1);
}
TEST_F(AutofillClientProviderTest, CreateChromeClientIfPolicyDisabled) {
base::HistogramTester histogram_tester;
prefs()->SetBoolean(prefs::kAutofillThirdPartyPasswordManagersAllowed, false);
prefs()->SetBoolean(prefs::kAutofillUsingVirtualViewStructure, true);
EXPECT_FALSE(provider().uses_platform_autofill());
histogram_tester.ExpectUniqueSample(kResetPrefMetric, true, 1);
EXPECT_FALSE(prefs()->GetBoolean(prefs::kAutofillUsingVirtualViewStructure));
histogram_tester.ExpectUniqueSample(
kAvailabilityMetric,
AndroidAutofillAvailabilityStatus::kNotAllowedByPolicy, 1);
}
#endif
}
}