blob: 1c8e6864250894eb8d49bb086c113abe9e3178bc [file] [log] [blame]
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/unified_consent/unified_consent_service.h"
#include <map>
#include <memory>
#include "base/run_loop.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_task_environment.h"
#include "build/build_config.h"
#include "components/sync/base/sync_prefs.h"
#include "components/sync/driver/sync_user_settings.h"
#include "components/sync/driver/test_sync_service.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "components/unified_consent/pref_names.h"
#include "components/unified_consent/scoped_unified_consent.h"
#include "components/unified_consent/unified_consent_metrics.h"
#include "services/identity/public/cpp/identity_test_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace unified_consent {
namespace {
class TestSyncService : public syncer::TestSyncService {
public:
TestSyncService() = default;
void AddObserver(syncer::SyncServiceObserver* observer) override {
observer_ = observer;
}
void FireStateChanged() {
if (observer_)
observer_->OnStateChanged(this);
}
private:
syncer::SyncServiceObserver* observer_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(TestSyncService);
};
} // namespace
class UnifiedConsentServiceTest : public testing::Test {
public:
UnifiedConsentServiceTest() {
UnifiedConsentService::RegisterPrefs(pref_service_.registry());
syncer::SyncPrefs::RegisterProfilePrefs(pref_service_.registry());
}
~UnifiedConsentServiceTest() override {
if (consent_service_)
consent_service_->Shutdown();
}
void CreateConsentService() {
if (!scoped_unified_consent_) {
SetUnifiedConsentFeatureState(
unified_consent::UnifiedConsentFeatureState::kEnabled);
}
consent_service_ = std::make_unique<UnifiedConsentService>(
&pref_service_, identity_test_environment_.identity_manager(),
&sync_service_);
sync_service_.FireStateChanged();
// Run until idle so the migration can finish.
base::RunLoop().RunUntilIdle();
}
void SetUnifiedConsentFeatureState(
unified_consent::UnifiedConsentFeatureState feature_state) {
// First reset |scoped_unified_consent_| to nullptr in case it was set
// before and then initialize it with the new value. This makes sure that
// the old scoped object is deleted before the new one is created.
scoped_unified_consent_.reset();
scoped_unified_consent_.reset(
new unified_consent::ScopedUnifiedConsent(feature_state));
}
unified_consent::MigrationState GetMigrationState() {
int migration_state_int =
pref_service_.GetInteger(prefs::kUnifiedConsentMigrationState);
return static_cast<unified_consent::MigrationState>(migration_state_int);
}
protected:
base::test::ScopedTaskEnvironment task_environment_;
sync_preferences::TestingPrefServiceSyncable pref_service_;
identity::IdentityTestEnvironment identity_test_environment_;
TestSyncService sync_service_;
std::unique_ptr<UnifiedConsentService> consent_service_;
std::unique_ptr<ScopedUnifiedConsent> scoped_unified_consent_;
DISALLOW_COPY_AND_ASSIGN(UnifiedConsentServiceTest);
};
TEST_F(UnifiedConsentServiceTest, DefaultValuesWhenSignedOut) {
CreateConsentService();
EXPECT_FALSE(pref_service_.GetBoolean(
prefs::kUrlKeyedAnonymizedDataCollectionEnabled));
}
TEST_F(UnifiedConsentServiceTest, EnableServices) {
CreateConsentService();
identity_test_environment_.SetPrimaryAccount("testaccount");
EXPECT_FALSE(pref_service_.GetBoolean(
prefs::kUrlKeyedAnonymizedDataCollectionEnabled));
// Enable services and check expectations.
consent_service_->EnableGoogleServices();
EXPECT_TRUE(pref_service_.GetBoolean(
prefs::kUrlKeyedAnonymizedDataCollectionEnabled));
}
TEST_F(UnifiedConsentServiceTest, EnableServices_WithUnsupportedService) {
CreateConsentService();
identity_test_environment_.SetPrimaryAccount("testaccount");
EXPECT_FALSE(pref_service_.GetBoolean(
prefs::kUrlKeyedAnonymizedDataCollectionEnabled));
// Enable services and check expectations.
consent_service_->EnableGoogleServices();
EXPECT_TRUE(pref_service_.GetBoolean(
prefs::kUrlKeyedAnonymizedDataCollectionEnabled));
}
TEST_F(UnifiedConsentServiceTest, Migration_UpdateSettings) {
// Create user that syncs history and has no custom passphrase.
identity_test_environment_.SetPrimaryAccount("testaccount");
sync_service_.GetUserSettings()->SetChosenDataTypes(
false, syncer::ModelTypeSet(syncer::TYPED_URLS));
EXPECT_TRUE(sync_service_.IsSyncFeatureActive());
// Url keyed data collection is off before the migration.
EXPECT_FALSE(pref_service_.GetBoolean(
prefs::kUrlKeyedAnonymizedDataCollectionEnabled));
CreateConsentService();
EXPECT_EQ(GetMigrationState(), unified_consent::MigrationState::kCompleted);
// During the migration Url keyed data collection is enabled.
EXPECT_TRUE(pref_service_.GetBoolean(
prefs::kUrlKeyedAnonymizedDataCollectionEnabled));
}
#if !defined(OS_CHROMEOS)
TEST_F(UnifiedConsentServiceTest, ClearPrimaryAccountDisablesSomeServices) {
base::HistogramTester histogram_tester;
CreateConsentService();
identity_test_environment_.SetPrimaryAccount("testaccount");
// Precondition: Enable unified consent.
consent_service_->EnableGoogleServices();
EXPECT_TRUE(pref_service_.GetBoolean(
prefs::kUrlKeyedAnonymizedDataCollectionEnabled));
// Clearing primary account revokes unfied consent and a couple of other
// non-personalized services.
identity_test_environment_.ClearPrimaryAccount();
EXPECT_FALSE(pref_service_.GetBoolean(
prefs::kUrlKeyedAnonymizedDataCollectionEnabled));
}
TEST_F(UnifiedConsentServiceTest, Migration_NotSignedIn) {
base::HistogramTester histogram_tester;
CreateConsentService();
// The user is signed out, so the migration is completed after the
// creation of the consent service.
EXPECT_EQ(GetMigrationState(), unified_consent::MigrationState::kCompleted);
}
#endif // !defined(OS_CHROMEOS)
TEST_F(UnifiedConsentServiceTest, Rollback_UserOptedIntoUnifiedConsent) {
identity_test_environment_.SetPrimaryAccount("testaccount");
// Migrate and opt into unified consent.
CreateConsentService();
consent_service_->EnableGoogleServices();
// Check expectations after opt-in.
EXPECT_TRUE(pref_service_.GetBoolean(
prefs::kUrlKeyedAnonymizedDataCollectionEnabled));
EXPECT_EQ(unified_consent::MigrationState::kCompleted, GetMigrationState());
EXPECT_TRUE(pref_service_.GetBoolean(
prefs::kUrlKeyedAnonymizedDataCollectionEnabled));
consent_service_->Shutdown();
consent_service_.reset();
SetUnifiedConsentFeatureState(UnifiedConsentFeatureState::kDisabled);
// Rollback
UnifiedConsentService::RollbackIfNeeded(&pref_service_, &sync_service_);
// Unified consent prefs should be cleared.
EXPECT_EQ(unified_consent::MigrationState::kNotInitialized,
GetMigrationState());
}
} // namespace unified_consent