unified_consent_service_unittest.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/unified_consent/unified_consent_service.h"
  5. #include <map>
  6. #include <memory>
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/run_loop.h"
  9. #include "base/test/metrics/histogram_tester.h"
  10. #include "base/test/task_environment.h"
  11. #include "build/build_config.h"
  12. #include "build/chromeos_buildflags.h"
  13. #include "components/signin/public/identity_manager/identity_test_environment.h"
  14. #include "components/sync/base/sync_prefs.h"
  15. #include "components/sync/driver/sync_user_settings.h"
  16. #include "components/sync/driver/test_sync_service.h"
  17. #include "components/sync_preferences/testing_pref_service_syncable.h"
  18. #include "components/unified_consent/pref_names.h"
  19. #include "components/unified_consent/unified_consent_metrics.h"
  20. #include "testing/gtest/include/gtest/gtest.h"
  21. namespace unified_consent {
  22. namespace {
  23. class TestSyncService : public syncer::TestSyncService {
  24. public:
  25. TestSyncService() = default;
  26. TestSyncService(const TestSyncService&) = delete;
  27. TestSyncService& operator=(const TestSyncService&) = delete;
  28. void AddObserver(syncer::SyncServiceObserver* observer) override {
  29. observer_ = observer;
  30. }
  31. void FireStateChanged() {
  32. if (observer_)
  33. observer_->OnStateChanged(this);
  34. }
  35. private:
  36. raw_ptr<syncer::SyncServiceObserver> observer_ = nullptr;
  37. };
  38. } // namespace
  39. class UnifiedConsentServiceTest : public testing::Test {
  40. public:
  41. UnifiedConsentServiceTest() {
  42. UnifiedConsentService::RegisterPrefs(pref_service_.registry());
  43. syncer::SyncPrefs::RegisterProfilePrefs(pref_service_.registry());
  44. }
  45. UnifiedConsentServiceTest(const UnifiedConsentServiceTest&) = delete;
  46. UnifiedConsentServiceTest& operator=(const UnifiedConsentServiceTest&) =
  47. delete;
  48. ~UnifiedConsentServiceTest() override {
  49. if (consent_service_)
  50. consent_service_->Shutdown();
  51. }
  52. void CreateConsentService() {
  53. consent_service_ = std::make_unique<UnifiedConsentService>(
  54. &pref_service_, identity_test_environment_.identity_manager(),
  55. &sync_service_, std::vector<std::string>());
  56. sync_service_.FireStateChanged();
  57. // Run until idle so the migration can finish.
  58. base::RunLoop().RunUntilIdle();
  59. }
  60. unified_consent::MigrationState GetMigrationState() {
  61. int migration_state_int =
  62. pref_service_.GetInteger(prefs::kUnifiedConsentMigrationState);
  63. return static_cast<unified_consent::MigrationState>(migration_state_int);
  64. }
  65. protected:
  66. base::test::SingleThreadTaskEnvironment task_environment_;
  67. sync_preferences::TestingPrefServiceSyncable pref_service_;
  68. signin::IdentityTestEnvironment identity_test_environment_;
  69. TestSyncService sync_service_;
  70. std::unique_ptr<UnifiedConsentService> consent_service_;
  71. };
  72. TEST_F(UnifiedConsentServiceTest, DefaultValuesWhenSignedOut) {
  73. CreateConsentService();
  74. EXPECT_FALSE(pref_service_.GetBoolean(
  75. prefs::kUrlKeyedAnonymizedDataCollectionEnabled));
  76. }
  77. TEST_F(UnifiedConsentServiceTest, EnableUrlKeyedAnonymizedDataCollection) {
  78. CreateConsentService();
  79. identity_test_environment_.SetPrimaryAccount("testaccount@gmail.com",
  80. signin::ConsentLevel::kSync);
  81. EXPECT_FALSE(pref_service_.GetBoolean(
  82. prefs::kUrlKeyedAnonymizedDataCollectionEnabled));
  83. // Enable services and check expectations.
  84. consent_service_->SetUrlKeyedAnonymizedDataCollectionEnabled(true);
  85. EXPECT_TRUE(pref_service_.GetBoolean(
  86. prefs::kUrlKeyedAnonymizedDataCollectionEnabled));
  87. }
  88. TEST_F(UnifiedConsentServiceTest, Migration_UpdateSettings) {
  89. // Create user that syncs history and has no custom passphrase.
  90. identity_test_environment_.SetPrimaryAccount("testaccount@gmail.com",
  91. signin::ConsentLevel::kSync);
  92. sync_service_.GetUserSettings()->SetSelectedTypes(
  93. false, {syncer::UserSelectableType::kHistory});
  94. EXPECT_TRUE(sync_service_.IsSyncFeatureActive());
  95. // Url keyed data collection is off before the migration.
  96. EXPECT_FALSE(pref_service_.GetBoolean(
  97. prefs::kUrlKeyedAnonymizedDataCollectionEnabled));
  98. CreateConsentService();
  99. EXPECT_EQ(GetMigrationState(), unified_consent::MigrationState::kCompleted);
  100. // During the migration Url keyed data collection is enabled.
  101. EXPECT_TRUE(pref_service_.GetBoolean(
  102. prefs::kUrlKeyedAnonymizedDataCollectionEnabled));
  103. }
  104. #if !BUILDFLAG(IS_CHROMEOS_ASH)
  105. TEST_F(UnifiedConsentServiceTest, ClearPrimaryAccountDisablesSomeServices) {
  106. base::HistogramTester histogram_tester;
  107. CreateConsentService();
  108. identity_test_environment_.SetPrimaryAccount("testaccount@gmail.com",
  109. signin::ConsentLevel::kSync);
  110. // Precondition: Enable unified consent.
  111. consent_service_->SetUrlKeyedAnonymizedDataCollectionEnabled(true);
  112. EXPECT_TRUE(pref_service_.GetBoolean(
  113. prefs::kUrlKeyedAnonymizedDataCollectionEnabled));
  114. // Clearing primary account revokes unfied consent and a couple of other
  115. // non-personalized services.
  116. identity_test_environment_.ClearPrimaryAccount();
  117. EXPECT_FALSE(pref_service_.GetBoolean(
  118. prefs::kUrlKeyedAnonymizedDataCollectionEnabled));
  119. }
  120. TEST_F(UnifiedConsentServiceTest, Migration_NotSignedIn) {
  121. base::HistogramTester histogram_tester;
  122. CreateConsentService();
  123. // The user is signed out, so the migration is completed after the
  124. // creation of the consent service.
  125. EXPECT_EQ(GetMigrationState(), unified_consent::MigrationState::kCompleted);
  126. }
  127. #endif // !BUILDFLAG(IS_CHROMEOS_ASH)
  128. } // namespace unified_consent