chromium/components/metrics/demographics/user_demographics_unittest.cc

// 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() {}

}  // namespace

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;

  // 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_ASH)

TEST_F(UserDemographicsPrefsTest, ReadAndClearUserDemographicPreferences) {}

TEST_F(UserDemographicsPrefsTest, ReadAndClearDeprecatedOffsetPref) {}

#if BUILDFLAG(IS_CHROMEOS_ASH)
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_ASH)

struct DemographicsTestParam {};

// Extend UserDemographicsPrefsTest fixture for parameterized tests on
// demographics.
class UserDemographicsPrefsTestWithParam
    : public UserDemographicsPrefsTest,
      public testing::WithParamInterface<DemographicsTestParam> {};

TEST_P(UserDemographicsPrefsTestWithParam, ReadDemographics_OffsetIsNotRandom) {}

// 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();

}  // namespace metrics