#include "components/ukm/observers/ukm_consent_state_observer.h"
#include "base/observer_list.h"
#include "base/test/scoped_feature_list.h"
#include "components/sync/engine/cycle/sync_cycle_snapshot.h"
#include "components/sync/protocol/sync_enums.pb.h"
#include "components/sync/service/sync_token_status.h"
#include "components/sync/test/test_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.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "chromeos/components/kiosk/kiosk_test_utils.h"
#include "chromeos/components/mgs/managed_guest_session_test_utils.h"
#endif
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "components/user_manager/fake_user_manager.h"
#include "components/user_manager/scoped_user_manager.h"
#endif
namespace ukm {
namespace {
class MockSyncService : public syncer::TestSyncService { … };
class TestUkmConsentStateObserver : public UkmConsentStateObserver { … };
class UkmConsentStateObserverTest : public testing::TestWithParam<bool> { … };
}
TEST_F(UkmConsentStateObserverTest, NoProfiles) { … }
TEST_F(UkmConsentStateObserverTest, NotActive) { … }
TEST_F(UkmConsentStateObserverTest, OneEnabled) { … }
TEST_F(UkmConsentStateObserverTest, MixedProfiles) { … }
TEST_F(UkmConsentStateObserverTest, TwoEnabled) { … }
TEST_F(UkmConsentStateObserverTest, OneAddRemove) { … }
TEST_F(UkmConsentStateObserverTest, PurgeOnDisable) { … }
TEST_F(UkmConsentStateObserverTest, NoInitialUkmConsentState) { … }
#if BUILDFLAG(IS_CHROMEOS_ASH)
TEST_F(UkmConsentStateObserverTest, VerifyConsentStates) {
sync_preferences::TestingPrefServiceSyncable prefs;
RegisterUrlKeyedAnonymizedDataCollectionPref(prefs);
TestUkmConsentStateObserver observer;
MockSyncService sync;
sync.SetAppSync(false);
SetUrlKeyedAnonymizedDataCollectionEnabled(&prefs, true);
observer.StartObserving(&sync, &prefs);
UkmConsentState state = observer.GetUkmConsentState();
EXPECT_TRUE(observer.IsUkmAllowedForAllProfiles());
EXPECT_TRUE(state.Has(MSBB));
EXPECT_TRUE(state.Has(EXTENSIONS));
EXPECT_FALSE(state.Has(APPS));
EXPECT_TRUE(observer.ResetNotified());
EXPECT_FALSE(observer.ResetPurged());
sync.SetAppSync(true);
state = observer.GetUkmConsentState();
EXPECT_TRUE(observer.IsUkmAllowedForAllProfiles());
EXPECT_TRUE(state.Has(MSBB));
EXPECT_TRUE(state.Has(EXTENSIONS));
EXPECT_TRUE(observer.ResetNotified());
EXPECT_FALSE(observer.ResetPurged());
EXPECT_TRUE(state.Has(APPS));
SetUrlKeyedAnonymizedDataCollectionEnabled(&prefs, false);
state = observer.GetUkmConsentState();
EXPECT_TRUE(observer.IsUkmAllowedForAllProfiles());
EXPECT_FALSE(state.Has(MSBB));
EXPECT_FALSE(state.Has(EXTENSIONS));
EXPECT_TRUE(state.Has(APPS));
EXPECT_TRUE(observer.ResetNotified());
EXPECT_FALSE(observer.ResetPurged());
sync.SetAppSync(false);
state = observer.GetUkmConsentState();
EXPECT_FALSE(state.Has(MSBB));
EXPECT_FALSE(state.Has(EXTENSIONS));
EXPECT_FALSE(state.Has(APPS));
EXPECT_TRUE(observer.ResetNotified());
EXPECT_TRUE(observer.ResetPurged());
}
TEST_F(UkmConsentStateObserverTest, VerifyConflictingProfilesRevokesConsent) {
sync_preferences::TestingPrefServiceSyncable prefs1;
RegisterUrlKeyedAnonymizedDataCollectionPref(prefs1);
sync_preferences::TestingPrefServiceSyncable prefs2;
RegisterUrlKeyedAnonymizedDataCollectionPref(prefs2);
TestUkmConsentStateObserver observer;
MockSyncService sync1;
sync1.SetAppSync(false);
SetUrlKeyedAnonymizedDataCollectionEnabled(&prefs1, true);
observer.StartObserving(&sync1, &prefs1);
EXPECT_TRUE(observer.ResetNotified());
EXPECT_FALSE(observer.ResetPurged());
const UkmConsentState consent_state = observer.GetUkmConsentState();
EXPECT_TRUE(consent_state.Has(MSBB));
EXPECT_FALSE(consent_state.Has(APPS));
MockSyncService sync2;
sync2.SetAppSync(true);
SetUrlKeyedAnonymizedDataCollectionEnabled(&prefs2, false);
observer.StartObserving(&sync2, &prefs2);
EXPECT_TRUE(observer.ResetNotified());
UkmConsentState state = observer.GetUkmConsentState();
EXPECT_FALSE(observer.IsUkmAllowedForAllProfiles());
EXPECT_FALSE(state.Has(MSBB));
EXPECT_FALSE(state.Has(EXTENSIONS));
EXPECT_FALSE(state.Has(APPS));
EXPECT_FALSE(observer.ResetPurged());
}
#endif
#if BUILDFLAG(IS_CHROMEOS)
class KioskUkmConsentStateObserverTest : public UkmConsentStateObserverTest {
public:
bool is_ukm_collection_enabled() const { return GetParam(); }
};
TEST_P(KioskUkmConsentStateObserverTest, VerifyDefaultConsent) {
#if BUILDFLAG(IS_CHROMEOS_ASH)
user_manager::ScopedUserManager user_manager(
std::make_unique<user_manager::FakeUserManager>());
#endif
chromeos::SetUpFakeKioskSession();
sync_preferences::TestingPrefServiceSyncable prefs;
RegisterUrlKeyedAnonymizedDataCollectionPref(prefs);
TestUkmConsentStateObserver observer;
MockSyncService sync;
sync.SetAppSync(false);
SetUrlKeyedAnonymizedDataCollectionEnabled(&prefs,
is_ukm_collection_enabled());
observer.StartObserving(&sync, &prefs);
UkmConsentState state = observer.GetUkmConsentState();
EXPECT_EQ(is_ukm_collection_enabled(), observer.IsUkmAllowedForAllProfiles());
EXPECT_EQ(is_ukm_collection_enabled(), state.Has(MSBB));
EXPECT_EQ(is_ukm_collection_enabled(), state.Has(APPS));
}
INSTANTIATE_TEST_SUITE_P(KioskUkmConsentStateObserverTest,
KioskUkmConsentStateObserverTest,
::testing::Bool());
class MgsUkmConsentStateObserverTest : public UkmConsentStateObserverTest {
public:
bool is_ukm_collection_enabled() const { return GetParam(); }
private:
chromeos::FakeManagedGuestSession managed_guest_session;
};
TEST_P(MgsUkmConsentStateObserverTest, VerifyAppsOnlyConsent) {
sync_preferences::TestingPrefServiceSyncable prefs;
RegisterUrlKeyedAnonymizedDataCollectionPref(prefs);
TestUkmConsentStateObserver observer;
MockSyncService sync;
sync.SetAppSync(false);
SetUrlKeyedAnonymizedDataCollectionEnabled(&prefs,
is_ukm_collection_enabled());
observer.StartObserving(&sync, &prefs);
UkmConsentState state = observer.GetUkmConsentState();
EXPECT_EQ(is_ukm_collection_enabled(), state.Has(APPS));
EXPECT_EQ(false, state.Has(MSBB));
}
INSTANTIATE_TEST_SUITE_P(MgsUkmConsentStateObserverTest,
MgsUkmConsentStateObserverTest,
::testing::Bool());
#endif
}