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
|
// 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/user_demographics.h"
#include <utility>
#include "base/time/time.h"
#include "base/values.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/metrics_proto/user_demographics.pb.h"
namespace metrics {
namespace {
// Gets the now time used for testing demographics.
base::Time GetNowTime() {
constexpr char kNowTimeInStringFormat[] = "22 Jul 2019 00:00:00 UDT";
base::Time now;
bool result = base::Time::FromString(kNowTimeInStringFormat, &now);
DCHECK(result);
return now;
}
} // namespace
TEST(UserDemographicsTest, UserDemographicsResult_ForValue) {
int user_birth_year = 1982;
UserDemographicsProto_Gender user_gender = UserDemographicsProto::GENDER_MALE;
UserDemographics user_demographics;
user_demographics.birth_year = user_birth_year;
user_demographics.gender = user_gender;
UserDemographicsResult user_demographics_result =
UserDemographicsResult::ForValue(std::move(user_demographics));
EXPECT_TRUE(user_demographics_result.IsSuccess());
EXPECT_EQ(UserDemographicsStatus::kSuccess,
user_demographics_result.status());
EXPECT_EQ(user_birth_year, user_demographics_result.value().birth_year);
EXPECT_EQ(user_gender, user_demographics_result.value().gender);
}
TEST(UserDemographicsTest, UserDemographicsResult_ForStatus) {
UserDemographicsStatus error_status =
UserDemographicsStatus::kIneligibleDemographicsData;
UserDemographicsResult user_demographics_result =
UserDemographicsResult::ForStatus(error_status);
EXPECT_FALSE(user_demographics_result.IsSuccess());
EXPECT_EQ(error_status, user_demographics_result.status());
}
class UserDemographicsPrefsTest : public testing::Test {
protected:
UserDemographicsPrefsTest() {
RegisterDemographicsLocalStatePrefs(pref_service_.registry());
RegisterDemographicsProfilePrefs(pref_service_.registry());
}
void SetDemographics(int birth_year, UserDemographicsProto::Gender gender) {
SetDemographicsImpl(kSyncDemographicsPrefName, birth_year, gender);
}
#if BUILDFLAG(IS_CHROMEOS)
void SetOsDemographics(int birth_year, UserDemographicsProto::Gender gender) {
SetDemographicsImpl(kSyncOsDemographicsPrefName, birth_year, gender);
}
#endif
PrefService* GetLocalState() { return &pref_service_; }
PrefService* GetProfilePrefs() { return &pref_service_; }
private:
void SetDemographicsImpl(const std::string& pref_name,
int birth_year,
UserDemographicsProto::Gender gender) {
base::Value::Dict dict;
dict.Set(kSyncDemographicsBirthYearPath, birth_year);
dict.Set(kSyncDemographicsGenderPath, static_cast<int>(gender));
GetProfilePrefs()->SetDict(pref_name, std::move(dict));
}
sync_preferences::TestingPrefServiceSyncable pref_service_;
};
TEST_F(UserDemographicsPrefsTest, ReadDemographicsWithRandomOffset) {
int user_demographics_birth_year = 1983;
UserDemographicsProto_Gender user_demographics_gender =
UserDemographicsProto::GENDER_MALE;
// Set user demographic prefs.
SetDemographics(user_demographics_birth_year, user_demographics_gender);
int provided_birth_year;
{
UserDemographicsResult demographics_result =
GetUserNoisedBirthYearAndGenderFromPrefs(GetNowTime(), GetLocalState(),
GetProfilePrefs());
ASSERT_TRUE(demographics_result.IsSuccess());
EXPECT_EQ(user_demographics_gender, demographics_result.value().gender);
// Verify that the provided birth year is within the range.
provided_birth_year = demographics_result.value().birth_year;
int delta = provided_birth_year - user_demographics_birth_year;
EXPECT_LE(delta, kUserDemographicsBirthYearNoiseOffsetRange);
EXPECT_GE(delta, -kUserDemographicsBirthYearNoiseOffsetRange);
}
// Verify that the offset is cached and that the randomized birth year is the
// same when doing more that one read of the birth year.
{
ASSERT_TRUE(
GetLocalState()->HasPrefPath(kUserDemographicsBirthYearOffsetPrefName));
UserDemographicsResult demographics_result =
GetUserNoisedBirthYearAndGenderFromPrefs(GetNowTime(), GetLocalState(),
GetProfilePrefs());
ASSERT_TRUE(demographics_result.IsSuccess());
EXPECT_EQ(provided_birth_year, demographics_result.value().birth_year);
}
}
#if BUILDFLAG(IS_CHROMEOS)
TEST_F(UserDemographicsPrefsTest, ReadOsDemographicsWithRandomOffset) {
int user_demographics_birth_year = 1983;
UserDemographicsProto_Gender user_demographics_gender =
UserDemographicsProto::GENDER_MALE;
// Set user demographic prefs.
SetOsDemographics(user_demographics_birth_year, user_demographics_gender);
int provided_birth_year;
{
UserDemographicsResult demographics_result =
GetUserNoisedBirthYearAndGenderFromPrefs(GetNowTime(), GetLocalState(),
GetProfilePrefs());
ASSERT_TRUE(demographics_result.IsSuccess());
EXPECT_EQ(user_demographics_gender, demographics_result.value().gender);
// Verify that the provided birth year is within the range.
provided_birth_year = demographics_result.value().birth_year;
int delta = provided_birth_year - user_demographics_birth_year;
EXPECT_LE(delta, kUserDemographicsBirthYearNoiseOffsetRange);
EXPECT_GE(delta, -kUserDemographicsBirthYearNoiseOffsetRange);
}
// Verify that the offset is cached and that the randomized birth year is the
// same when doing more that one read of the birth year.
{
ASSERT_TRUE(
GetLocalState()->HasPrefPath(kUserDemographicsBirthYearOffsetPrefName));
UserDemographicsResult demographics_result =
GetUserNoisedBirthYearAndGenderFromPrefs(GetNowTime(), GetLocalState(),
GetProfilePrefs());
ASSERT_TRUE(demographics_result.IsSuccess());
EXPECT_EQ(provided_birth_year, demographics_result.value().birth_year);
}
}
#endif // BUILDFLAG(IS_CHROMEOS)
TEST_F(UserDemographicsPrefsTest, ReadAndClearUserDemographicPreferences) {
// Verify demographic prefs are not available when there is nothing set.
ASSERT_FALSE(GetUserNoisedBirthYearAndGenderFromPrefs(
GetNowTime(), GetLocalState(), GetProfilePrefs())
.IsSuccess());
// Set demographic prefs directly from the pref service interface because
// demographic prefs will only be set on the server-side. The SyncPrefs
// interface cannot set demographic prefs.
SetDemographics(1983, UserDemographicsProto::GENDER_FEMALE);
// Set birth year noise offset to not have it randomized.
GetProfilePrefs()->SetInteger(kUserDemographicsBirthYearOffsetPrefName, 2);
// Verify that demographics are provided.
{
UserDemographicsResult demographics_result =
GetUserNoisedBirthYearAndGenderFromPrefs(GetNowTime(), GetLocalState(),
GetProfilePrefs());
ASSERT_TRUE(demographics_result.IsSuccess());
}
ClearDemographicsPrefs(GetProfilePrefs());
// Verify that demographics are not provided and kSyncDemographics is cleared.
// Note that we retain k*DemographicsBirthYearOffset. If the user resumes
// syncing, causing these prefs to be recreated, we don't want them to start
// reporting a different randomized birth year as this could narrow down or
// even reveal their true birth year.
EXPECT_FALSE(GetUserNoisedBirthYearAndGenderFromPrefs(
GetNowTime(), GetLocalState(), GetProfilePrefs())
.IsSuccess());
EXPECT_FALSE(GetProfilePrefs()->HasPrefPath(kSyncDemographicsPrefName));
#if BUILDFLAG(IS_CHROMEOS)
EXPECT_FALSE(GetProfilePrefs()->HasPrefPath(kSyncOsDemographicsPrefName));
#endif
EXPECT_TRUE(
GetLocalState()->HasPrefPath(kUserDemographicsBirthYearOffsetPrefName));
// Deprecated offset is not created if it does not already exist.
EXPECT_FALSE(GetProfilePrefs()->HasPrefPath(
kDeprecatedDemographicsBirthYearOffsetPrefName));
}
TEST_F(UserDemographicsPrefsTest, ReadAndClearDeprecatedOffsetPref) {
// Verify demographic prefs are not available when there is nothing set.
ASSERT_FALSE(GetUserNoisedBirthYearAndGenderFromPrefs(
GetNowTime(), GetLocalState(), GetProfilePrefs())
.IsSuccess());
// Set demographic prefs directly from the pref service interface because
// demographic prefs will only be set on the server-side. The SyncPrefs
// interface cannot set demographic prefs.
SetDemographics(1983, UserDemographicsProto::GENDER_FEMALE);
// Set deprecated birth year noise offset in the UserPrefs
GetProfilePrefs()->SetInteger(kDeprecatedDemographicsBirthYearOffsetPrefName,
2);
// Verify that demographics are provided.
{
UserDemographicsResult demographics_result =
GetUserNoisedBirthYearAndGenderFromPrefs(GetNowTime(), GetLocalState(),
GetProfilePrefs());
ASSERT_TRUE(demographics_result.IsSuccess());
}
// Offset if migrated to new pref.
EXPECT_EQ(
2, GetLocalState()->GetInteger(kUserDemographicsBirthYearOffsetPrefName));
// TODO(crbug.com/40240008): clear/remove deprecated pref after 2023/09
EXPECT_TRUE(GetProfilePrefs()->HasPrefPath(
kDeprecatedDemographicsBirthYearOffsetPrefName));
}
#if BUILDFLAG(IS_CHROMEOS)
TEST_F(UserDemographicsPrefsTest, ChromeOsAsh) {
// Verify demographic prefs are not available when there is nothing set.
ASSERT_FALSE(GetUserNoisedBirthYearAndGenderFromPrefs(
GetNowTime(), GetLocalState(), GetProfilePrefs())
.IsSuccess());
// Set OS demographic prefs directly within the pref service interface.
SetOsDemographics(1983, UserDemographicsProto::GENDER_FEMALE);
// Set birth year noise offset in the UserPrefs
GetLocalState()->SetInteger(kUserDemographicsBirthYearOffsetPrefName, 2);
// Verify that demographics are provided.
{
UserDemographicsResult demographics_result =
GetUserNoisedBirthYearAndGenderFromPrefs(GetNowTime(), GetLocalState(),
GetProfilePrefs());
ASSERT_TRUE(demographics_result.IsSuccess());
}
EXPECT_FALSE(GetProfilePrefs()->HasPrefPath(kSyncDemographicsPrefName));
EXPECT_TRUE(GetProfilePrefs()->HasPrefPath(kSyncOsDemographicsPrefName));
EXPECT_TRUE(
GetLocalState()->HasPrefPath(kUserDemographicsBirthYearOffsetPrefName));
}
#endif // BUILDFLAG(IS_CHROMEOS)
struct DemographicsTestParam {
// Birth year of the user.
int birth_year = kUserDemographicsBirthYearDefaultValue;
// Non-random offset to apply to |birth_year| as noise.
int birth_year_offset = kUserDemographicsBirthYearNoiseOffsetDefaultValue;
// Gender of the user.
UserDemographicsProto_Gender gender = kUserDemographicGenderDefaultEnumValue;
// Status of the retrieval of demographics.
UserDemographicsStatus status = UserDemographicsStatus::kMaxValue;
};
// Extend UserDemographicsPrefsTest fixture for parameterized tests on
// demographics.
class UserDemographicsPrefsTestWithParam
: public UserDemographicsPrefsTest,
public testing::WithParamInterface<DemographicsTestParam> {};
TEST_P(UserDemographicsPrefsTestWithParam, ReadDemographics_OffsetIsNotRandom) {
DemographicsTestParam param = GetParam();
// Set user demographic prefs.
SetDemographics(param.birth_year, param.gender);
// Set birth year noise offset to not have it randomized.
GetLocalState()->SetInteger(kUserDemographicsBirthYearOffsetPrefName,
param.birth_year_offset);
// Verify provided demographics for the different parameterized test cases.
UserDemographicsResult demographics_result =
GetUserNoisedBirthYearAndGenderFromPrefs(GetNowTime(), GetLocalState(),
GetProfilePrefs());
if (param.status == UserDemographicsStatus::kSuccess) {
ASSERT_TRUE(demographics_result.IsSuccess());
EXPECT_EQ(param.birth_year + param.birth_year_offset,
demographics_result.value().birth_year);
EXPECT_EQ(param.gender, demographics_result.value().gender);
} else {
ASSERT_FALSE(demographics_result.IsSuccess());
EXPECT_EQ(param.status, demographics_result.status());
}
}
// Test suite composed of different test cases of getting user demographics.
// The now time in each test case is "22 Jul 2019 00:00:00 UDT" which falls into
// the year bucket of 2018. Users need at most a |birth_year| +
// |birth_year_offset| of 1998 to be able to provide demographics.
INSTANTIATE_TEST_SUITE_P(
All,
UserDemographicsPrefsTestWithParam,
::testing::Values(
// Test where birth year should not be provided because |birth_year| + 2
// > 1998.
DemographicsTestParam{
/*birth_year=*/1997,
/*birth_year_offset=*/2,
/*gender=*/UserDemographicsProto::GENDER_FEMALE,
/*status=*/UserDemographicsStatus::kIneligibleDemographicsData},
// Test where birth year should not be provided because |birth_year| - 2
// > 1998.
DemographicsTestParam{
/*birth_year=*/2001,
/*birth_year_offset=*/-2,
/*gender=*/UserDemographicsProto::GENDER_FEMALE,
/*status=*/UserDemographicsStatus::kIneligibleDemographicsData},
// Test where birth year should not be provided because age of user is
// |kUserDemographicsMaxAge| + 1, which is over the max age.
DemographicsTestParam{
/*birth_year=*/1933,
/*birth_year_offset=*/0,
/*gender=*/UserDemographicsProto::GENDER_FEMALE,
/*status=*/UserDemographicsStatus::kIneligibleDemographicsData},
// Test where gender should not be provided because it has a low
// population that can have their privacy compromised because of high
// entropy.
DemographicsTestParam{
/*birth_year=*/1986,
/*birth_year_offset=*/0,
/*gender=*/UserDemographicsProto::GENDER_CUSTOM_OR_OTHER,
/*status=*/UserDemographicsStatus::kIneligibleDemographicsData},
// Test where birth year can be provided because |birth_year| + 2 ==
// 1998.
DemographicsTestParam{/*birth_year=*/1996,
/*birth_year_offset=*/2,
/*gender=*/UserDemographicsProto::GENDER_FEMALE,
/*status=*/UserDemographicsStatus::kSuccess},
// Test where birth year can be provided because |birth_year| - 2 ==
// 1998.
DemographicsTestParam{/*birth_year=*/2000,
/*birth_year_offset=*/-2,
/*gender=*/UserDemographicsProto::GENDER_MALE,
/*status=*/UserDemographicsStatus::kSuccess},
// Test where birth year can be provided because |birth_year| + 2 <
// 1998.
DemographicsTestParam{/*birth_year=*/1995,
/*birth_year_offset=*/2,
/*gender=*/UserDemographicsProto::GENDER_FEMALE,
/*status=*/UserDemographicsStatus::kSuccess},
// Test where birth year can be provided because |birth_year| - 2 <
// 1998.
DemographicsTestParam{/*birth_year=*/1999,
/*birth_year_offset=*/-2,
/*gender=*/UserDemographicsProto::GENDER_MALE,
/*status=*/UserDemographicsStatus::kSuccess},
// Test where gender can be provided because it is part of a large
// population with a low entropy.
DemographicsTestParam{/*birth_year=*/1986,
/*birth_year_offset=*/0,
/*gender=*/UserDemographicsProto::GENDER_FEMALE,
/*status=*/UserDemographicsStatus::kSuccess},
// Test where gender can be provided because it is part of a large
// population with a low entropy.
DemographicsTestParam{/*birth_year=*/1986,
/*birth_year_offset=*/0,
/*gender=*/UserDemographicsProto::GENDER_MALE,
/*status=*/UserDemographicsStatus::kSuccess}));
} // namespace metrics
|