#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 {
base::Time GetNowTime() { … }
}
TEST(UserDemographicsTest, UserDemographicsResult_ForValue) { … }
TEST(UserDemographicsTest, UserDemographicsResult_ForStatus) { … }
class UserDemographicsPrefsTest : public testing::Test { … };
TEST_F(UserDemographicsPrefsTest, ReadDemographicsWithRandomOffset) { … }
#if BUILDFLAG(IS_CHROMEOS_ASH)
TEST_F(UserDemographicsPrefsTest, ReadOsDemographicsWithRandomOffset) {
int user_demographics_birth_year = 1983;
UserDemographicsProto_Gender user_demographics_gender =
UserDemographicsProto::GENDER_MALE;
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);
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);
}
{
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
TEST_F(UserDemographicsPrefsTest, ReadAndClearUserDemographicPreferences) { … }
TEST_F(UserDemographicsPrefsTest, ReadAndClearDeprecatedOffsetPref) { … }
#if BUILDFLAG(IS_CHROMEOS_ASH)
TEST_F(UserDemographicsPrefsTest, ChromeOsAsh) {
ASSERT_FALSE(GetUserNoisedBirthYearAndGenderFromPrefs(
GetNowTime(), GetLocalState(), GetProfilePrefs())
.IsSuccess());
SetOsDemographics(1983, UserDemographicsProto::GENDER_FEMALE);
GetLocalState()->SetInteger(kUserDemographicsBirthYearOffsetPrefName, 2);
{
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
struct DemographicsTestParam { … };
class UserDemographicsPrefsTestWithParam
: public UserDemographicsPrefsTest,
public testing::WithParamInterface<DemographicsTestParam> { … };
TEST_P(UserDemographicsPrefsTestWithParam, ReadDemographics_OffsetIsNotRandom) { … }
INSTANTIATE_TEST_SUITE_P(…);
}