Commit 86a4c8ec authored by Mihai Sardarescu's avatar Mihai Sardarescu Committed by Commit Bot

[unity] Add unit test for the unified consent service

This CL adds a unit tests for the unified consent service. The current
tests only covers the non-personalized services. It should be enhanced
to also include the sync-related part once teh unified consent starts
managing the sync state.

This CL also makes the following changes:
* replaces the dependencies of the service on profile_sync_service
with a dependency on the more generic sync_service
* enabled Url-keyed anonymized data collection when unified consent
is enabled.

Bug: 823809
Change-Id: I2ab27429272ecccfbd254ffc4100234ce95623df
Reviewed-on: https://chromium-review.googlesource.com/1122858
Commit-Queue: Mihai Sardarescu <msarda@chromium.org>
Reviewed-by: default avatarThomas Tangl <tangltom@chromium.org>
Cr-Commit-Position: refs/heads/master@{#572155}
parent 84109456
......@@ -53,7 +53,7 @@ KeyedService* UnifiedConsentServiceFactory::BuildServiceInstanceFor(
return new UnifiedConsentService(
new ChromeUnifiedConsentServiceClient(profile->GetPrefs()),
profile->GetPrefs(), IdentityManagerFactory::GetForProfile(profile),
ProfileSyncServiceFactory::GetForProfile(profile));
ProfileSyncServiceFactory::GetSyncServiceForBrowserContext(profile));
}
bool UnifiedConsentServiceFactory::ServiceIsNULLWhileTesting() const {
......
......@@ -26,14 +26,17 @@ static_library("unified_consent") {
source_set("unit_tests") {
testonly = true
sources = [
"unified_consent_service_unittest.cc",
"url_keyed_anonymized_data_collection_consent_helper_unittest.cc",
]
deps = [
":unified_consent",
"//base/test:test_support",
"//components/autofill/core/common",
"//components/sync",
"//components/sync:test_support_driver",
"//components/sync_preferences:test_support",
"//services/identity/public/cpp:test_support",
"//testing/gtest",
]
}
include_rules = [
"+components/autofill/core/common",
"+components/browser_sync",
"+components/keyed_service/core",
"+components/pref_registry",
"+components/prefs",
......
......@@ -5,11 +5,11 @@
#include "components/unified_consent/unified_consent_service.h"
#include "components/autofill/core/common/autofill_pref_names.h"
#include "components/browser_sync/profile_sync_service.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.h"
#include "components/sync/base/model_type.h"
#include "components/sync/driver/sync_service.h"
#include "components/unified_consent/pref_names.h"
#include "components/unified_consent/unified_consent_service_client.h"
......@@ -17,14 +17,14 @@ UnifiedConsentService::UnifiedConsentService(
UnifiedConsentServiceClient* service_client,
PrefService* pref_service,
identity::IdentityManager* identity_manager,
browser_sync::ProfileSyncService* profile_sync_service)
syncer::SyncService* sync_service)
: service_client_(service_client),
pref_service_(pref_service),
identity_manager_(identity_manager),
profile_sync_service_(profile_sync_service) {
sync_service_(sync_service) {
DCHECK(pref_service_);
DCHECK(identity_manager_);
DCHECK(profile_sync_service_);
DCHECK(sync_service_);
identity_manager_->AddObserver(this);
pref_change_registrar_ = std::make_unique<PrefChangeRegistrar>();
......@@ -66,22 +66,24 @@ void UnifiedConsentService::OnUnifiedConsentGivenPrefChanged() {
if (identity_manager_->HasPrimaryAccount()) {
// KeepEverythingSynced is set to false, so the user can select individual
// sync data types.
profile_sync_service_->OnUserChoseDatatypes(
false, syncer::UserSelectableTypes());
sync_service_->OnUserChoseDatatypes(false, syncer::UserSelectableTypes());
}
return;
}
DCHECK(profile_sync_service_->IsSyncAllowed());
DCHECK(sync_service_->IsSyncAllowed());
DCHECK(identity_manager_->HasPrimaryAccount());
// Enable all sync data types.
pref_service_->SetBoolean(autofill::prefs::kAutofillWalletImportEnabled,
true);
profile_sync_service_->OnUserChoseDatatypes(true,
syncer::UserSelectableTypes());
sync_service_->OnUserChoseDatatypes(true, syncer::UserSelectableTypes());
// Enable all non-personalized services.
pref_service_->SetBoolean(prefs::kUrlKeyedAnonymizedDataCollectionEnabled,
true);
// Inform client to enable non-personalized services.
service_client_->SetAlternateErrorPagesEnabled(true);
service_client_->SetMetricsReportingEnabled(true);
service_client_->SetSafeBrowsingExtendedReportingEnabled(true);
......
......@@ -20,8 +20,8 @@ class PrefRegistrySyncable;
class PrefChangeRegistrar;
class PrefService;
namespace browser_sync {
class ProfileSyncService;
namespace syncer {
class SyncService;
}
class UnifiedConsentServiceClient;
......@@ -35,7 +35,7 @@ class UnifiedConsentService : public KeyedService,
UnifiedConsentService(UnifiedConsentServiceClient* service_client,
PrefService* pref_service,
identity::IdentityManager* identity_manager,
browser_sync::ProfileSyncService* profile_sync_service);
syncer::SyncService* sync_service);
~UnifiedConsentService() override;
// Register the prefs used by this UnifiedConsentService.
......@@ -57,7 +57,7 @@ class UnifiedConsentService : public KeyedService,
UnifiedConsentServiceClient* service_client_;
PrefService* pref_service_;
identity::IdentityManager* identity_manager_;
browser_sync::ProfileSyncService* profile_sync_service_;
syncer::SyncService* sync_service_;
std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_;
......
// 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 <memory>
#include "base/message_loop/message_loop.h"
#include "components/autofill/core/common/autofill_pref_names.h"
#include "components/sync/driver/fake_sync_service.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "components/unified_consent/pref_names.h"
#include "components/unified_consent/unified_consent_service_client.h"
#include "services/identity/public/cpp/identity_test_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class TestSyncService : public syncer::FakeSyncService {
public:
bool IsSyncAllowed() const override { return true; }
};
class UnifiedConsentServiceTest : public testing::Test,
public UnifiedConsentServiceClient {
public:
// testing::Test:
void SetUp() override {
pref_service_.registry()->RegisterBooleanPref(
autofill::prefs::kAutofillWalletImportEnabled, false);
UnifiedConsentService::RegisterPrefs(pref_service_.registry());
consent_service_ = std::make_unique<UnifiedConsentService>(
this, &pref_service_, identity_test_environment_.identity_manager(),
&sync_service_);
}
void TearDown() override { consent_service_->Shutdown(); }
// UnifiedConsentServiceClient:
void SetAlternateErrorPagesEnabled(bool enabled) override {
alternate_error_pages_enabled_ = enabled;
}
void SetMetricsReportingEnabled(bool enabled) override {
metrics_reporting_enabled_ = enabled;
}
void SetNetworkPredictionEnabled(bool enabled) override {
network_predictions_enabled_ = enabled;
}
void SetSafeBrowsingEnabled(bool enabled) override {
safe_browsing_enabled_ = enabled;
}
void SetSafeBrowsingExtendedReportingEnabled(bool enabled) override {
safe_browsing_extended_reporting_enabled_ = enabled;
}
void SetSearchSuggestEnabled(bool enabled) override {
search_suggest_enabled_ = enabled;
}
protected:
base::MessageLoop message_loop_;
sync_preferences::TestingPrefServiceSyncable pref_service_;
identity::IdentityTestEnvironment identity_test_environment_;
TestSyncService sync_service_;
std::unique_ptr<UnifiedConsentService> consent_service_;
bool alternate_error_pages_enabled_ = false;
bool metrics_reporting_enabled_ = false;
bool network_predictions_enabled_ = false;
bool safe_browsing_enabled_ = false;
bool safe_browsing_extended_reporting_enabled_ = false;
bool search_suggest_enabled_ = false;
};
TEST_F(UnifiedConsentServiceTest, DefaultValuesWhenSignedOut) {
EXPECT_FALSE(pref_service_.GetBoolean(prefs::kUnifiedConsentGiven));
EXPECT_FALSE(pref_service_.GetBoolean(
prefs::kUrlKeyedAnonymizedDataCollectionEnabled));
}
TEST_F(UnifiedConsentServiceTest, EnableUnfiedConsent) {
identity_test_environment_.SetPrimaryAccount("testaccount");
EXPECT_FALSE(pref_service_.GetBoolean(prefs::kUnifiedConsentGiven));
EXPECT_FALSE(pref_service_.GetBoolean(
prefs::kUrlKeyedAnonymizedDataCollectionEnabled));
// Enable Unified Consent enables all non-personaized features
pref_service_.SetBoolean(prefs::kUnifiedConsentGiven, true);
EXPECT_TRUE(pref_service_.GetBoolean(prefs::kUnifiedConsentGiven));
EXPECT_TRUE(pref_service_.GetBoolean(
prefs::kUrlKeyedAnonymizedDataCollectionEnabled));
EXPECT_TRUE(alternate_error_pages_enabled_);
EXPECT_TRUE(metrics_reporting_enabled_);
EXPECT_TRUE(network_predictions_enabled_);
EXPECT_TRUE(safe_browsing_enabled_);
EXPECT_TRUE(safe_browsing_extended_reporting_enabled_);
EXPECT_TRUE(search_suggest_enabled_);
// Disable unified consent does not disable any of the non-personalized
// features.
pref_service_.SetBoolean(prefs::kUnifiedConsentGiven, false);
EXPECT_FALSE(pref_service_.GetBoolean(prefs::kUnifiedConsentGiven));
EXPECT_TRUE(pref_service_.GetBoolean(
prefs::kUrlKeyedAnonymizedDataCollectionEnabled));
EXPECT_TRUE(alternate_error_pages_enabled_);
EXPECT_TRUE(metrics_reporting_enabled_);
EXPECT_TRUE(network_predictions_enabled_);
EXPECT_TRUE(safe_browsing_enabled_);
EXPECT_TRUE(safe_browsing_extended_reporting_enabled_);
EXPECT_TRUE(search_suggest_enabled_);
}
#if !defined(OS_CHROMEOS)
TEST_F(UnifiedConsentServiceTest, ClearPrimaryAccountDisablesSomeServices) {
identity_test_environment_.SetPrimaryAccount("testaccount");
// Precondition: Enable unified consent.
pref_service_.SetBoolean(prefs::kUnifiedConsentGiven, true);
EXPECT_TRUE(pref_service_.GetBoolean(
prefs::kUrlKeyedAnonymizedDataCollectionEnabled));
EXPECT_TRUE(alternate_error_pages_enabled_);
EXPECT_TRUE(metrics_reporting_enabled_);
EXPECT_TRUE(network_predictions_enabled_);
EXPECT_TRUE(safe_browsing_enabled_);
EXPECT_TRUE(safe_browsing_extended_reporting_enabled_);
EXPECT_TRUE(search_suggest_enabled_);
// Clearing primary account revokes unfied consent and a couple of other
// non-personalized services.
identity_test_environment_.ClearPrimaryAccount();
EXPECT_FALSE(pref_service_.GetBoolean(prefs::kUnifiedConsentGiven));
EXPECT_FALSE(pref_service_.GetBoolean(
prefs::kUrlKeyedAnonymizedDataCollectionEnabled));
// Consent is not revoked for the following services.
EXPECT_TRUE(alternate_error_pages_enabled_);
EXPECT_TRUE(metrics_reporting_enabled_);
EXPECT_TRUE(network_predictions_enabled_);
EXPECT_TRUE(safe_browsing_enabled_);
EXPECT_TRUE(safe_browsing_extended_reporting_enabled_);
EXPECT_TRUE(search_suggest_enabled_);
}
#endif // !defined(OS_CHROMEOS)
} // namespace
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment