url_keyed_data_collection_consent_helper_unittest.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/url_keyed_data_collection_consent_helper.h"
  5. #include <vector>
  6. #include "components/sync/driver/test_sync_service.h"
  7. #include "components/sync/engine/cycle/sync_cycle_snapshot.h"
  8. #include "components/sync_preferences/testing_pref_service_syncable.h"
  9. #include "components/unified_consent/pref_names.h"
  10. #include "components/unified_consent/unified_consent_service.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. namespace unified_consent {
  13. namespace {
  14. class TestSyncService : public syncer::TestSyncService {
  15. public:
  16. TestSyncService() { SetActiveDataTypes({}); }
  17. void AddActiveDataType(syncer::ModelType type) {
  18. syncer::ModelTypeSet active_types = GetActiveDataTypes();
  19. active_types.Put(type);
  20. SetActiveDataTypes(active_types);
  21. }
  22. void FireOnStateChangeOnAllObservers() {
  23. for (auto& observer : observers_)
  24. observer.OnStateChanged(this);
  25. }
  26. // syncer::TestSyncService:
  27. void AddObserver(syncer::SyncServiceObserver* observer) override {
  28. observers_.AddObserver(observer);
  29. }
  30. void RemoveObserver(syncer::SyncServiceObserver* observer) override {
  31. observers_.RemoveObserver(observer);
  32. }
  33. private:
  34. base::ObserverList<syncer::SyncServiceObserver>::Unchecked observers_;
  35. };
  36. class UrlKeyedDataCollectionConsentHelperTest
  37. : public testing::Test,
  38. public UrlKeyedDataCollectionConsentHelper::Observer {
  39. public:
  40. // testing::Test:
  41. void SetUp() override {
  42. UnifiedConsentService::RegisterPrefs(pref_service_.registry());
  43. }
  44. void OnUrlKeyedDataCollectionConsentStateChanged(
  45. UrlKeyedDataCollectionConsentHelper* consent_helper) override {
  46. state_changed_notifications_.push_back(consent_helper->IsEnabled());
  47. }
  48. protected:
  49. sync_preferences::TestingPrefServiceSyncable pref_service_;
  50. std::vector<bool> state_changed_notifications_;
  51. TestSyncService sync_service_;
  52. };
  53. TEST_F(UrlKeyedDataCollectionConsentHelperTest, AnonymizedDataCollection) {
  54. std::unique_ptr<UrlKeyedDataCollectionConsentHelper> helper =
  55. UrlKeyedDataCollectionConsentHelper::
  56. NewAnonymizedDataCollectionConsentHelper(&pref_service_);
  57. helper->AddObserver(this);
  58. EXPECT_FALSE(helper->IsEnabled());
  59. EXPECT_TRUE(state_changed_notifications_.empty());
  60. pref_service_.SetBoolean(prefs::kUrlKeyedAnonymizedDataCollectionEnabled,
  61. true);
  62. EXPECT_TRUE(helper->IsEnabled());
  63. ASSERT_EQ(1U, state_changed_notifications_.size());
  64. EXPECT_TRUE(state_changed_notifications_[0]);
  65. state_changed_notifications_.clear();
  66. pref_service_.SetBoolean(prefs::kUrlKeyedAnonymizedDataCollectionEnabled,
  67. false);
  68. EXPECT_FALSE(helper->IsEnabled());
  69. ASSERT_EQ(1U, state_changed_notifications_.size());
  70. EXPECT_FALSE(state_changed_notifications_[0]);
  71. helper->RemoveObserver(this);
  72. }
  73. TEST_F(UrlKeyedDataCollectionConsentHelperTest, PersonalizedDataCollection) {
  74. std::unique_ptr<UrlKeyedDataCollectionConsentHelper> helper =
  75. UrlKeyedDataCollectionConsentHelper::
  76. NewPersonalizedDataCollectionConsentHelper(&sync_service_);
  77. helper->AddObserver(this);
  78. EXPECT_FALSE(helper->IsEnabled());
  79. EXPECT_TRUE(state_changed_notifications_.empty());
  80. sync_service_.AddActiveDataType(syncer::ModelType::HISTORY_DELETE_DIRECTIVES);
  81. sync_service_.FireOnStateChangeOnAllObservers();
  82. EXPECT_TRUE(helper->IsEnabled());
  83. EXPECT_EQ(1U, state_changed_notifications_.size());
  84. helper->RemoveObserver(this);
  85. }
  86. TEST_F(UrlKeyedDataCollectionConsentHelperTest,
  87. PersonalizedDataCollection_NullSyncService) {
  88. std::unique_ptr<UrlKeyedDataCollectionConsentHelper> helper =
  89. UrlKeyedDataCollectionConsentHelper::
  90. NewPersonalizedDataCollectionConsentHelper(
  91. nullptr /* sync_service */);
  92. EXPECT_FALSE(helper->IsEnabled());
  93. }
  94. } // namespace
  95. } // namespace unified_consent