1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/metrics/demographics/demographic_metrics_provider.h"
#include <memory>
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_clock.h"
#include "base/time/time.h"
#include "base/values.h"
#include "components/metrics/demographics/user_demographics.h"
#include "components/metrics/metrics_log_uploader.h"
#include "components/sync/base/features.h"
#include "components/sync/service/sync_prefs.h"
#include "components/sync/test/test_sync_service.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/metrics_proto/chrome_user_metrics_extension.pb.h"
#include "third_party/metrics_proto/ukm/report.pb.h"
namespace metrics {
namespace {
constexpr int kTestBirthYear = 1983;
constexpr UserDemographicsProto::Gender kTestGender =
UserDemographicsProto::GENDER_FEMALE;
enum TestSyncServiceState {
NULL_SYNC_SERVICE,
SYNC_FEATURE_NOT_ENABLED,
SYNC_FEATURE_ENABLED,
SYNC_FEATURE_ENABLED_BUT_PAUSED,
SYNC_FEATURE_DISABLED_BUT_PREFERENCES_ENABLED,
SYNC_FEATURE_ENABLED_BUT_PREFERENCES_NOT_SELECTED,
#if BUILDFLAG(IS_CHROMEOS)
// Represents the user clearing sync data via dashboard. On all platforms
// except ChromeOS, this clears the primary account (which is basically
// SYNC_FEATURE_NOT_ENABLED). On ChromeOS, Sync enters a special state.
SYNC_FEATURE_DISABLED_ON_CHROMEOS_VIA_DASHBOARD,
#endif // BUILDFLAG(IS_CHROMEOS)
};
// Profile client for testing that gets fake Profile information and services.
class TestProfileClient : public DemographicMetricsProvider::ProfileClient {
public:
TestProfileClient(const TestProfileClient&) = delete;
TestProfileClient& operator=(const TestProfileClient&) = delete;
~TestProfileClient() override = default;
TestProfileClient(int number_of_profiles,
TestSyncServiceState sync_service_state)
: number_of_profiles_(number_of_profiles) {
RegisterDemographicsLocalStatePrefs(pref_service_.registry());
RegisterDemographicsProfilePrefs(pref_service_.registry());
switch (sync_service_state) {
case NULL_SYNC_SERVICE:
break;
case SYNC_FEATURE_NOT_ENABLED:
sync_service_ = std::make_unique<syncer::TestSyncService>();
// Set an arbitrary disable reason to mimic sync feature being unable to
// start.
sync_service_->SetHasUnrecoverableError(true);
break;
case SYNC_FEATURE_ENABLED:
// TestSyncService by default behaves as everything enabled/active.
sync_service_ = std::make_unique<syncer::TestSyncService>();
CHECK(sync_service_->GetDisableReasons().empty());
CHECK_EQ(syncer::SyncService::TransportState::ACTIVE,
sync_service_->GetTransportState());
break;
case SYNC_FEATURE_ENABLED_BUT_PAUSED:
sync_service_ = std::make_unique<syncer::TestSyncService>();
// Mimic the user signing out from content are (sync paused).
sync_service_->SetPersistentAuthError();
CHECK(sync_service_->GetDisableReasons().empty());
CHECK_EQ(syncer::SyncService::TransportState::PAUSED,
sync_service_->GetTransportState());
break;
case SYNC_FEATURE_DISABLED_BUT_PREFERENCES_ENABLED:
sync_service_ = std::make_unique<syncer::TestSyncService>();
sync_service_->SetSignedIn(signin::ConsentLevel::kSignin);
CHECK(sync_service_->GetUserSettings()->GetSelectedTypes().Has(
syncer::UserSelectableType::kPreferences));
CHECK(!sync_service_->IsSyncFeatureEnabled());
CHECK(sync_service_->GetDisableReasons().empty());
CHECK_EQ(syncer::SyncService::TransportState::ACTIVE,
sync_service_->GetTransportState());
break;
case SYNC_FEATURE_ENABLED_BUT_PREFERENCES_NOT_SELECTED:
sync_service_ = std::make_unique<syncer::TestSyncService>();
sync_service_->GetUserSettings()->SetSelectedTypes(
/*sync_everything=*/false,
/*types=*/{});
CHECK(sync_service_->IsSyncFeatureEnabled());
CHECK(sync_service_->GetDisableReasons().empty());
CHECK_EQ(syncer::SyncService::TransportState::ACTIVE,
sync_service_->GetTransportState());
break;
#if BUILDFLAG(IS_CHROMEOS)
case SYNC_FEATURE_DISABLED_ON_CHROMEOS_VIA_DASHBOARD:
sync_service_ = std::make_unique<syncer::TestSyncService>();
sync_service_->GetUserSettings()->SetSyncFeatureDisabledViaDashboard();
// On ChromeOS Ash, IsInitialSyncFeatureSetupComplete always returns
// true but IsSyncFeatureEnabled() stays false because the user needs to
// manually resume sync the feature.
CHECK(sync_service_->GetUserSettings()
->IsInitialSyncFeatureSetupComplete());
CHECK(!sync_service_->IsSyncFeatureEnabled());
break;
#endif // BUILDFLAG(IS_CHROMEOS)
}
}
int GetNumberOfProfilesOnDisk() override { return number_of_profiles_; }
syncer::SyncService* GetSyncService() override { return sync_service_.get(); }
PrefService* GetLocalState() override { return &pref_service_; }
PrefService* GetProfilePrefs() override { return &pref_service_; }
base::Time GetNetworkTime() const override {
base::Time time;
auto result = base::Time::FromString("17 Jun 2019 00:00:00 UDT", &time);
DCHECK(result);
return time;
}
void SetDemographicsInPrefs(int birth_year,
metrics::UserDemographicsProto_Gender gender) {
base::Value::Dict dict;
dict.Set(kSyncDemographicsBirthYearPath, birth_year);
dict.Set(kSyncDemographicsGenderPath, static_cast<int>(gender));
pref_service_.SetDict(kSyncDemographicsPrefName, std::move(dict));
}
private:
sync_preferences::TestingPrefServiceSyncable pref_service_;
std::unique_ptr<syncer::TestSyncService> sync_service_;
const int number_of_profiles_;
base::SimpleTestClock clock_;
};
TEST(DemographicMetricsProviderTest,
ProvideSyncedUserNoisedBirthYearAndGender_FeatureEnabled) {
base::HistogramTester histogram;
auto client = std::make_unique<TestProfileClient>(/*number_of_profiles=*/1,
SYNC_FEATURE_ENABLED);
client->SetDemographicsInPrefs(kTestBirthYear, kTestGender);
// Set birth year noise offset to not have it randomized.
const int kBirthYearOffset = 3;
client->GetLocalState()->SetInteger(kUserDemographicsBirthYearOffsetPrefName,
kBirthYearOffset);
// Run demographics provider.
DemographicMetricsProvider provider(
std::move(client), MetricsLogUploader::MetricServiceType::UMA);
ChromeUserMetricsExtension uma_proto;
provider.ProvideSyncedUserNoisedBirthYearAndGender(&uma_proto);
// Verify provided demographics.
EXPECT_EQ(kTestBirthYear + kBirthYearOffset,
uma_proto.user_demographics().birth_year());
EXPECT_EQ(kTestGender, uma_proto.user_demographics().gender());
// Verify histograms.
histogram.ExpectUniqueSample("UMA.UserDemographics.Status",
UserDemographicsStatus::kSuccess, 1);
}
TEST(DemographicMetricsProviderTest,
ProvideSyncedUserNoisedBirthYearAndGender_NoSyncService) {
base::HistogramTester histogram;
auto client = std::make_unique<TestProfileClient>(/*number_of_profiles=*/1,
NULL_SYNC_SERVICE);
// Run demographics provider.
DemographicMetricsProvider provider(
std::move(client), MetricsLogUploader::MetricServiceType::UMA);
ChromeUserMetricsExtension uma_proto;
provider.ProvideSyncedUserNoisedBirthYearAndGender(&uma_proto);
// Expect the proto fields to be not set and left to default.
EXPECT_FALSE(uma_proto.user_demographics().has_birth_year());
EXPECT_FALSE(uma_proto.user_demographics().has_gender());
// Verify histograms.
histogram.ExpectUniqueSample("UMA.UserDemographics.Status",
UserDemographicsStatus::kNoSyncService, 1);
}
TEST(DemographicMetricsProviderTest,
ProvideSyncedUserNoisedBirthYearAndGender_SyncEnabledButPaused) {
base::HistogramTester histogram;
auto client = std::make_unique<TestProfileClient>(
/*number_of_profiles=*/1, SYNC_FEATURE_ENABLED_BUT_PAUSED);
// Run demographics provider.
DemographicMetricsProvider provider(
std::move(client), MetricsLogUploader::MetricServiceType::UMA);
ChromeUserMetricsExtension uma_proto;
provider.ProvideSyncedUserNoisedBirthYearAndGender(&uma_proto);
// Expect the proto fields to be not set and left to default.
EXPECT_FALSE(uma_proto.user_demographics().has_birth_year());
EXPECT_FALSE(uma_proto.user_demographics().has_gender());
// Verify histograms.
histogram.ExpectUniqueSample("UMA.UserDemographics.Status",
UserDemographicsStatus::kSyncNotEnabled, 1);
}
TEST(
DemographicMetricsProviderTest,
ProvideSyncedUserNoisedBirthYearAndGender_SyncFeatureDisabledButPreferencesEnabled) {
// Disable `kSyncSupportAlwaysSyncingPriorityPreferences` since it decouples
// the PRIORITY_PREFERENCES type from the sync toggle, and makes the test
// redundant.
base::test::ScopedFeatureList feature_list;
feature_list.InitAndDisableFeature(
syncer::kSyncSupportAlwaysSyncingPriorityPreferences);
base::HistogramTester histogram;
auto client = std::make_unique<TestProfileClient>(
/*number_of_profiles=*/1, SYNC_FEATURE_DISABLED_BUT_PREFERENCES_ENABLED);
client->SetDemographicsInPrefs(kTestBirthYear, kTestGender);
// Set birth year noise offset to not have it randomized.
const int kBirthYearOffset = 3;
client->GetLocalState()->SetInteger(kUserDemographicsBirthYearOffsetPrefName,
kBirthYearOffset);
// Run demographics provider.
DemographicMetricsProvider provider(
std::move(client), MetricsLogUploader::MetricServiceType::UMA);
ChromeUserMetricsExtension uma_proto;
provider.ProvideSyncedUserNoisedBirthYearAndGender(&uma_proto);
// Verify provided demographics.
EXPECT_EQ(kTestBirthYear + kBirthYearOffset,
uma_proto.user_demographics().birth_year());
EXPECT_EQ(kTestGender, uma_proto.user_demographics().gender());
// Verify histograms: Demographics should be provided.
histogram.ExpectUniqueSample("UMA.UserDemographics.Status",
UserDemographicsStatus::kSuccess, 1);
}
#if BUILDFLAG(IS_CHROMEOS)
TEST(
DemographicMetricsProviderTest,
ProvideSyncedUserNoisedBirthYearAndGender_SyncFeatureDisabledOnChromeOsAshViaSyncDashboard) {
base::HistogramTester histogram;
auto client = std::make_unique<TestProfileClient>(
/*number_of_profiles=*/1,
SYNC_FEATURE_DISABLED_ON_CHROMEOS_VIA_DASHBOARD);
// Run demographics provider.
DemographicMetricsProvider provider(
std::move(client), MetricsLogUploader::MetricServiceType::UMA);
ChromeUserMetricsExtension uma_proto;
provider.ProvideSyncedUserNoisedBirthYearAndGender(&uma_proto);
// Expect the proto fields to be not set and left to default.
EXPECT_FALSE(uma_proto.user_demographics().has_birth_year());
EXPECT_FALSE(uma_proto.user_demographics().has_gender());
// Verify histograms.
histogram.ExpectUniqueSample("UMA.UserDemographics.Status",
UserDemographicsStatus::kSyncNotEnabled, 1);
}
#endif // BUILDFLAG(IS_CHROMEOS)
TEST(DemographicMetricsProviderTest,
ProvideSyncedUserNoisedBirthYearAndGender_SyncNotEnabled) {
base::HistogramTester histogram;
auto client = std::make_unique<TestProfileClient>(/*number_of_profiles=*/1,
SYNC_FEATURE_NOT_ENABLED);
// Run demographics provider.
DemographicMetricsProvider provider(
std::move(client), MetricsLogUploader::MetricServiceType::UMA);
ChromeUserMetricsExtension uma_proto;
provider.ProvideSyncedUserNoisedBirthYearAndGender(&uma_proto);
// Expect the proto fields to be not set and left to default.
EXPECT_FALSE(uma_proto.user_demographics().has_birth_year());
EXPECT_FALSE(uma_proto.user_demographics().has_gender());
// Verify histograms.
histogram.ExpectUniqueSample("UMA.UserDemographics.Status",
UserDemographicsStatus::kSyncNotEnabled, 1);
}
TEST(DemographicMetricsProviderTest,
ProvideSyncedUserNoisedBirthYearAndGender_FeatureDisabled) {
// Disable demographics reporting feature.
base::test::ScopedFeatureList local_feature;
local_feature.InitAndDisableFeature(kDemographicMetricsReporting);
base::HistogramTester histogram;
auto client = std::make_unique<TestProfileClient>(/*number_of_profiles=*/1,
SYNC_FEATURE_ENABLED);
client->SetDemographicsInPrefs(kTestBirthYear, kTestGender);
// Run demographics provider.
DemographicMetricsProvider provider(
std::move(client), MetricsLogUploader::MetricServiceType::UMA);
ChromeUserMetricsExtension uma_proto;
provider.ProvideSyncedUserNoisedBirthYearAndGender(&uma_proto);
// Expect that the UMA proto is untouched.
EXPECT_FALSE(uma_proto.user_demographics().has_birth_year());
EXPECT_FALSE(uma_proto.user_demographics().has_gender());
// Verify that there are no histograms for user demographics.
histogram.ExpectTotalCount("UMA.UserDemographics.Status", 0);
}
TEST(DemographicMetricsProviderTest,
ProvideSyncedUserNoisedBirthYearAndGender_NotExactlyOneProfile) {
base::HistogramTester histogram;
auto client = std::make_unique<TestProfileClient>(/*number_of_profiles=*/2,
SYNC_FEATURE_ENABLED);
client->SetDemographicsInPrefs(kTestBirthYear, kTestGender);
// Run demographics provider with not exactly one Profile on disk.
DemographicMetricsProvider provider(
std::move(client), MetricsLogUploader::MetricServiceType::UMA);
ChromeUserMetricsExtension uma_proto;
provider.ProvideSyncedUserNoisedBirthYearAndGender(&uma_proto);
#if !BUILDFLAG(IS_CHROMEOS)
// Expect that the UMA proto is untouched.
EXPECT_FALSE(uma_proto.user_demographics().has_birth_year());
EXPECT_FALSE(uma_proto.user_demographics().has_gender());
// Verify histograms.
histogram.ExpectUniqueSample("UMA.UserDemographics.Status",
UserDemographicsStatus::kMoreThanOneProfile, 1);
#else
// On ChromeOS, we have a profile selection strategy, so expect UMA reporting
// to work.
EXPECT_TRUE(uma_proto.user_demographics().has_birth_year());
EXPECT_TRUE(uma_proto.user_demographics().has_gender());
// Verify histograms.
histogram.ExpectUniqueSample("UMA.UserDemographics.Status",
UserDemographicsStatus::kSuccess, 1);
#endif // !BUILDFLAG(IS_CHROMEOS)
}
TEST(DemographicMetricsProviderTest,
ProvideSyncedUserNoisedBirthYearAndGender_NoUserDemographics) {
base::HistogramTester histogram;
auto client = std::make_unique<TestProfileClient>(/*number_of_profiles=*/1,
SYNC_FEATURE_ENABLED);
// Set some ineligible values to prefs.
client->SetDemographicsInPrefs(/*birth_year=*/-17,
UserDemographicsProto::GENDER_UNKNOWN);
// Run demographics provider with a ProfileClient that does not provide
// demographics because of some error.
DemographicMetricsProvider provider(
std::move(client), MetricsLogUploader::MetricServiceType::UMA);
ChromeUserMetricsExtension uma_proto;
provider.ProvideSyncedUserNoisedBirthYearAndGender(&uma_proto);
// Expect that the UMA proto is untouched.
EXPECT_FALSE(uma_proto.user_demographics().has_birth_year());
EXPECT_FALSE(uma_proto.user_demographics().has_gender());
// Verify that there are no histograms for user demographics.
histogram.ExpectUniqueSample(
"UMA.UserDemographics.Status",
UserDemographicsStatus::kIneligibleDemographicsData, 1);
}
TEST(DemographicMetricsProviderTest,
ProvideSyncedUserNoisedBirthYearAndGenderToUkmReport) {
base::HistogramTester histogram;
auto client = std::make_unique<TestProfileClient>(/*number_of_profiles=*/1,
SYNC_FEATURE_ENABLED);
client->SetDemographicsInPrefs(kTestBirthYear, kTestGender);
// Set birth year noise offset to not have it randomized.
const int kBirthYearOffset = 3;
client->GetLocalState()->SetInteger(kUserDemographicsBirthYearOffsetPrefName,
kBirthYearOffset);
// Run demographics provider.
DemographicMetricsProvider provider(
std::move(client), MetricsLogUploader::MetricServiceType::UKM);
ukm::Report report;
provider.ProvideSyncedUserNoisedBirthYearAndGenderToReport(&report);
// Verify provided demographics.
EXPECT_EQ(kTestBirthYear + kBirthYearOffset,
report.user_demographics().birth_year());
EXPECT_EQ(kTestGender, report.user_demographics().gender());
}
TEST(
DemographicMetricsProviderTest,
ProvideSyncedUserNoisedBirthYearAndGender_PreferencesSyncNotSelected_WithoutAlwaysSyncingFeature) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndDisableFeature(
syncer::kSyncSupportAlwaysSyncingPriorityPreferences);
base::HistogramTester histogram;
auto client = std::make_unique<TestProfileClient>(
/*number_of_profiles=*/1,
SYNC_FEATURE_ENABLED_BUT_PREFERENCES_NOT_SELECTED);
client->SetDemographicsInPrefs(kTestBirthYear, kTestGender);
// Run demographics provider.
DemographicMetricsProvider provider(
std::move(client), MetricsLogUploader::MetricServiceType::UMA);
ChromeUserMetricsExtension uma_proto;
provider.ProvideSyncedUserNoisedBirthYearAndGender(&uma_proto);
// Expect the proto fields to be not set and left to default.
EXPECT_FALSE(uma_proto.user_demographics().has_birth_year());
EXPECT_FALSE(uma_proto.user_demographics().has_gender());
// Verify histograms.
histogram.ExpectUniqueSample("UMA.UserDemographics.Status",
UserDemographicsStatus::kSyncNotEnabled, 1);
}
TEST(
DemographicMetricsProviderTest,
ProvideSyncedUserNoisedBirthYearAndGender_PreferencesSyncNotSelected_WithAlwaysSyncingFeature) {
base::test::ScopedFeatureList feature_list(
syncer::kSyncSupportAlwaysSyncingPriorityPreferences);
base::HistogramTester histogram;
auto client = std::make_unique<TestProfileClient>(
/*number_of_profiles=*/1,
SYNC_FEATURE_ENABLED_BUT_PREFERENCES_NOT_SELECTED);
client->SetDemographicsInPrefs(kTestBirthYear, kTestGender);
// Set birth year noise offset to not have it randomized.
const int kBirthYearOffset = 3;
client->GetLocalState()->SetInteger(kUserDemographicsBirthYearOffsetPrefName,
kBirthYearOffset);
// Run demographics provider.
DemographicMetricsProvider provider(
std::move(client), MetricsLogUploader::MetricServiceType::UMA);
ChromeUserMetricsExtension uma_proto;
provider.ProvideSyncedUserNoisedBirthYearAndGender(&uma_proto);
// Expect the proto fields to be set, since allowlisted priority preferences
// are always synced.
EXPECT_EQ(kTestBirthYear + kBirthYearOffset,
uma_proto.user_demographics().birth_year());
EXPECT_EQ(kTestGender, uma_proto.user_demographics().gender());
// Verify histograms.
histogram.ExpectUniqueSample("UMA.UserDemographics.Status",
UserDemographicsStatus::kSuccess, 1);
}
} // namespace
} // namespace metrics
|