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
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ash/components/report/device_metrics/churn/active_status.h"
#include "base/check.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "chromeos/ash/components/report/proto/fresnel_service.pb.h"
#include "chromeos/ash/components/report/report_controller.h"
#include "chromeos/ash/components/report/utils/test_utils.h"
#include "chromeos/ash/components/report/utils/time_utils.h"
#include "chromeos/ash/components/system/fake_statistics_provider.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/testing_pref_service.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ash::report::device_metrics {
class ActiveStatusTest : public testing::Test {
protected:
void SetUp() override {
// Set the mock time to |kFakeTimeNow|.
base::Time ts;
ASSERT_TRUE(base::Time::FromUTCString(utils::kFakeTimeNowString, &ts));
task_environment_.AdvanceClock(ts - base::Time::Now());
// Register all related local state prefs.
ReportController::RegisterPrefs(local_state_.registry());
system::StatisticsProvider::SetTestProvider(&statistics_provider_);
active_status_ = std::make_unique<ActiveStatus>(&local_state_);
}
void TearDown() override { active_status_.reset(); }
ActiveStatus* GetActiveStatus() { return active_status_.get(); }
base::Time GetFakeTimeNow() { return base::Time::Now(); }
protected:
void SetActivateDate(const std::string& activate_date) {
statistics_provider_.SetMachineStatistic(system::kActivateDateKey,
activate_date);
}
std::optional<base::Time> GetFirstActiveWeekForTest() {
return utils::GetFirstActiveWeek();
}
base::test::TaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
private:
TestingPrefServiceSimple local_state_;
system::FakeStatisticsProvider statistics_provider_;
std::unique_ptr<ActiveStatus> active_status_;
};
TEST_F(ActiveStatusTest, SetAndGetValue) {
// Set a value using SetValue method.
GetActiveStatus()->SetValue(42);
// Verify that GetValue returns the same value.
EXPECT_EQ(GetActiveStatus()->GetValue(), 42);
}
TEST_F(ActiveStatusTest, CalculateValue) {
// Verify initial active status value updates to current month timestamp.
base::Time ts = GetFakeTimeNow();
std::optional<int> value = GetActiveStatus()->CalculateNewValue(ts);
EXPECT_TRUE(value.has_value());
EXPECT_EQ(value.value(), 72351745);
// Verify uninitialized time object returns nullopt.
base::Time uninitialized_ts;
value = GetActiveStatus()->CalculateNewValue(uninitialized_ts);
EXPECT_FALSE(value.has_value());
// Verify unix epoch timestamp returns nullopt.
base::Time unix_epoch_ts = base::Time::UnixEpoch();
value = GetActiveStatus()->CalculateNewValue(unix_epoch_ts);
EXPECT_FALSE(value.has_value());
// Verify timestamp before |kActiveStatusInceptionDate| returns nullopt.
base::Time inception_ts;
EXPECT_TRUE(base::Time::FromUTCString(
ActiveStatus::kActiveStatusInceptionDate, &inception_ts));
value = GetActiveStatus()->CalculateNewValue(inception_ts - base::Days(1));
EXPECT_FALSE(value.has_value());
}
TEST_F(ActiveStatusTest, CalculateNewValueSameMonthAsPreviousReturnsNullopt) {
base::Time current_month_ts = GetFakeTimeNow();
// Set up the initial active status value and the current month timestamp.
std::optional<int> value =
GetActiveStatus()->CalculateNewValue(current_month_ts);
GetActiveStatus()->SetValue(value.value());
// Calculate the new value for the same month as the previous one.
value = GetActiveStatus()->CalculateNewValue(current_month_ts);
// Ensure that the new value is not calculated and nullopt is returned.
EXPECT_FALSE(value.has_value());
}
TEST_F(ActiveStatusTest, CalculateNewValueNewMonthReturnsUpdatedValue) {
base::Time current_month_ts = GetFakeTimeNow();
// Set up the initial active status value and the current month timestamp.
std::optional<int> value =
GetActiveStatus()->CalculateNewValue(current_month_ts);
GetActiveStatus()->SetValue(value.value());
// Set the previous month's timestamp to simulate a new month.
base::Time next_month_ts = utils::GetNextMonth(current_month_ts).value();
// Calculate the new value for the new month.
std::optional<int> new_value =
GetActiveStatus()->CalculateNewValue(next_month_ts);
// Ensure the updated new value is generated based on the newer timestamp.
EXPECT_TRUE(new_value.has_value());
EXPECT_GT(new_value.value(), value.value());
}
TEST_F(ActiveStatusTest, GetCurrentActiveMonthTimestamp) {
ASSERT_EQ(GetActiveStatus()->GetValue(), 0);
std::optional<base::Time> current_active_month_ts =
GetActiveStatus()->GetCurrentActiveMonthTimestamp();
// Return inception ts since active status value has never been updated (= 0).
base::Time inception_ts;
EXPECT_TRUE(base::Time::FromUTCString(
ActiveStatus::kActiveStatusInceptionDate, &inception_ts));
EXPECT_TRUE(current_active_month_ts.has_value());
EXPECT_EQ(current_active_month_ts.value(), inception_ts);
// Set value to a fake time.
int val = GetActiveStatus()->CalculateNewValue(GetFakeTimeNow()).value();
GetActiveStatus()->SetValue(val);
// Get the current active month timestamp. The value was set to
// |GetFakeTimeNow()|.
current_active_month_ts = GetActiveStatus()->GetCurrentActiveMonthTimestamp();
EXPECT_TRUE(current_active_month_ts.has_value());
EXPECT_EQ(current_active_month_ts.value(), GetFakeTimeNow());
}
TEST_F(ActiveStatusTest, CalculateCohortMetadataReturnExpectedMetadata) {
// Set up the initial active status value and the current month timestamp.
base::Time current_month_ts = GetFakeTimeNow();
int val = GetActiveStatus()->CalculateNewValue(current_month_ts).value();
GetActiveStatus()->SetValue(val);
// Simulate a new month when calculating churn cohort metadata.
base::Time next_month_ts = utils::GetNextMonth(current_month_ts).value();
// Calculate the cohort metadata.
ChurnCohortMetadata metadata =
GetActiveStatus()->CalculateCohortMetadata(next_month_ts).value();
// Ensure that the metadata is calculated correctly.
EXPECT_EQ(metadata.active_status_value(), 72613891);
EXPECT_FALSE(metadata.is_first_active_in_cohort());
// Ensure local state doesn't get updated on calculate cohort metadata call.
EXPECT_EQ(GetActiveStatus()->GetCurrentActiveMonthTimestamp(),
current_month_ts);
}
TEST_F(ActiveStatusTest, CalculateCohortMetadataReturnFirstActive) {
base::Time current_month_ts = GetFakeTimeNow();
// Initialize ActivateDate VPD field to be first active in current month ts.
SetActivateDate("2023-02");
// Calculate the cohort metadata.
ChurnCohortMetadata metadata =
GetActiveStatus()->CalculateCohortMetadata(current_month_ts).value();
// Ensure that the metadata is calculated correctly.
EXPECT_EQ(metadata.active_status_value(), 72351745);
EXPECT_TRUE(metadata.is_first_active_in_cohort());
}
TEST_F(ActiveStatusTest,
CalculateCohortMetadataSameMonthAsPreviousReturnsNullopt) {
// Set up the initial active status value and the current month timestamp.
base::Time current_month_ts = GetFakeTimeNow();
int val = GetActiveStatus()->CalculateNewValue(current_month_ts).value();
GetActiveStatus()->SetValue(val);
// Calculate the cohort metadata.
std::optional<ChurnCohortMetadata> metadata =
GetActiveStatus()->CalculateCohortMetadata(current_month_ts);
// Ensure the metadata is not regenerated for same |current_month_ts|.
EXPECT_FALSE(metadata.has_value());
}
TEST_F(ActiveStatusTest, CalculateObservationMetadataReturnsExpectedMetadata) {
// Setup initial value to be last active in Jan-2023.
// Value represents device was active each of the 18 months prior.
// Represents binary: 0100010100 111111111111111111
int cur_value = 72613887;
base::Time cur_ts = GetFakeTimeNow();
GetActiveStatus()->SetValue(cur_value);
// Calculate the observation metadata for 3 periods.
std::optional<ChurnObservationMetadata> metadata_0 =
GetActiveStatus()->CalculateObservationMetadata(cur_ts, 0);
std::optional<ChurnObservationMetadata> metadata_1 =
GetActiveStatus()->CalculateObservationMetadata(cur_ts, 1);
std::optional<ChurnObservationMetadata> metadata_2 =
GetActiveStatus()->CalculateObservationMetadata(cur_ts, 2);
// Ensure that the metadata is calculated correctly.
EXPECT_TRUE(metadata_0->monthly_active_status());
EXPECT_TRUE(metadata_1->monthly_active_status());
EXPECT_TRUE(metadata_2->monthly_active_status());
EXPECT_TRUE(metadata_0->yearly_active_status());
EXPECT_TRUE(metadata_1->yearly_active_status());
EXPECT_TRUE(metadata_2->yearly_active_status());
EXPECT_EQ(
metadata_0->first_active_during_cohort(),
ChurnObservationMetadata_FirstActiveDuringCohort_EXISTED_OR_NOT_ACTIVE_YET);
EXPECT_EQ(
metadata_1->first_active_during_cohort(),
ChurnObservationMetadata_FirstActiveDuringCohort_EXISTED_OR_NOT_ACTIVE_YET);
EXPECT_EQ(
metadata_2->first_active_during_cohort(),
ChurnObservationMetadata_FirstActiveDuringCohort_EXISTED_OR_NOT_ACTIVE_YET);
}
TEST_F(ActiveStatusTest,
CalculateObservationMetadataReturnsExpectedMetadataActiveMonths) {
// Setup initial value to be last active in Jan-2023.
// Value represents device was active each of the 18 months prior.
// Represents binary: 0100010100 111101111111111101
int cur_value = 72605693;
base::Time cur_ts = GetFakeTimeNow();
GetActiveStatus()->SetValue(cur_value);
// Calculate the observation metadata for 3 periods.
std::optional<ChurnObservationMetadata> metadata_0 =
GetActiveStatus()->CalculateObservationMetadata(cur_ts, 0);
std::optional<ChurnObservationMetadata> metadata_1 =
GetActiveStatus()->CalculateObservationMetadata(cur_ts, 1);
std::optional<ChurnObservationMetadata> metadata_2 =
GetActiveStatus()->CalculateObservationMetadata(cur_ts, 2);
// Ensure that the metadata is calculated correctly.
EXPECT_FALSE(metadata_0->monthly_active_status());
EXPECT_TRUE(metadata_1->monthly_active_status());
EXPECT_TRUE(metadata_2->monthly_active_status());
EXPECT_FALSE(metadata_0->yearly_active_status());
EXPECT_TRUE(metadata_1->yearly_active_status());
EXPECT_TRUE(metadata_2->yearly_active_status());
EXPECT_EQ(
metadata_0->first_active_during_cohort(),
ChurnObservationMetadata_FirstActiveDuringCohort_EXISTED_OR_NOT_ACTIVE_YET);
EXPECT_EQ(
metadata_1->first_active_during_cohort(),
ChurnObservationMetadata_FirstActiveDuringCohort_EXISTED_OR_NOT_ACTIVE_YET);
EXPECT_EQ(
metadata_2->first_active_during_cohort(),
ChurnObservationMetadata_FirstActiveDuringCohort_EXISTED_OR_NOT_ACTIVE_YET);
}
TEST_F(ActiveStatusTest,
CalculateObservationMetadataReturnsExpectedMetadataFirstActiveYearly) {
// Setup initial value to be last active in Jan-2023.
// Value represents device was active each of the 18 months prior.
// Represents binary: 0100010100 000010000000000001
int cur_value = 72359937;
base::Time cur_ts = GetFakeTimeNow();
GetActiveStatus()->SetValue(cur_value);
SetActivateDate("2021-52");
// Calculate the observation metadata for 3 periods.
std::optional<ChurnObservationMetadata> metadata_0 =
GetActiveStatus()->CalculateObservationMetadata(cur_ts, 0);
std::optional<ChurnObservationMetadata> metadata_1 =
GetActiveStatus()->CalculateObservationMetadata(cur_ts, 1);
std::optional<ChurnObservationMetadata> metadata_2 =
GetActiveStatus()->CalculateObservationMetadata(cur_ts, 2);
// Ensure that the metadata is calculated correctly.
EXPECT_FALSE(metadata_0->monthly_active_status());
EXPECT_FALSE(metadata_1->monthly_active_status());
EXPECT_FALSE(metadata_2->monthly_active_status());
EXPECT_TRUE(metadata_0->yearly_active_status());
EXPECT_FALSE(metadata_1->yearly_active_status());
EXPECT_FALSE(metadata_2->yearly_active_status());
EXPECT_EQ(
metadata_0->first_active_during_cohort(),
ChurnObservationMetadata_FirstActiveDuringCohort_FIRST_ACTIVE_IN_YEARLY_COHORT);
EXPECT_EQ(
metadata_1->first_active_during_cohort(),
ChurnObservationMetadata_FirstActiveDuringCohort_EXISTED_OR_NOT_ACTIVE_YET);
EXPECT_EQ(
metadata_2->first_active_during_cohort(),
ChurnObservationMetadata_FirstActiveDuringCohort_EXISTED_OR_NOT_ACTIVE_YET);
}
TEST_F(ActiveStatusTest,
CalculateObservationMetadataReturnsExpectedMetadataFirstActiveMonthly) {
// Setup initial value to be last active in Jan-2023.
// Value represents device was active each of the 18 months prior.
// Represents binary: 0100010100 000000000000000011
int cur_value = 72351747;
base::Time cur_ts = GetFakeTimeNow();
GetActiveStatus()->SetValue(cur_value);
SetActivateDate("2022-52");
// Calculate the observation metadata for 3 periods.
std::optional<ChurnObservationMetadata> metadata_0 =
GetActiveStatus()->CalculateObservationMetadata(cur_ts, 0);
std::optional<ChurnObservationMetadata> metadata_1 =
GetActiveStatus()->CalculateObservationMetadata(cur_ts, 1);
std::optional<ChurnObservationMetadata> metadata_2 =
GetActiveStatus()->CalculateObservationMetadata(cur_ts, 2);
// Ensure that the metadata is calculated correctly.
EXPECT_TRUE(metadata_0->monthly_active_status());
EXPECT_FALSE(metadata_1->monthly_active_status());
EXPECT_FALSE(metadata_2->monthly_active_status());
EXPECT_FALSE(metadata_0->yearly_active_status());
EXPECT_FALSE(metadata_1->yearly_active_status());
EXPECT_FALSE(metadata_2->yearly_active_status());
EXPECT_EQ(
metadata_0->first_active_during_cohort(),
ChurnObservationMetadata_FirstActiveDuringCohort_FIRST_ACTIVE_IN_MONTHLY_COHORT);
EXPECT_EQ(
metadata_1->first_active_during_cohort(),
ChurnObservationMetadata_FirstActiveDuringCohort_EXISTED_OR_NOT_ACTIVE_YET);
EXPECT_EQ(
metadata_2->first_active_during_cohort(),
ChurnObservationMetadata_FirstActiveDuringCohort_EXISTED_OR_NOT_ACTIVE_YET);
}
TEST_F(
ActiveStatusTest,
CalculateObservationMetadataReturnsNulloptMisalignedCohortAndObservation) {
// Setup initial value to be last active in Dec-2022.
// Value represents device was active each of the 18 months prior.
// Represents binary: 0100010011 000000000000000001
int cur_value = 72089601;
GetActiveStatus()->SetValue(cur_value);
// Simulate unexpected scenario of observation being 1 month ahead of cohort.
base::Time next_month_ts = GetFakeTimeNow();
// Calculate the observation metadata for 3 periods.
std::optional<ChurnObservationMetadata> metadata_0 =
GetActiveStatus()->CalculateObservationMetadata(next_month_ts, 0);
std::optional<ChurnObservationMetadata> metadata_1 =
GetActiveStatus()->CalculateObservationMetadata(next_month_ts, 1);
std::optional<ChurnObservationMetadata> metadata_2 =
GetActiveStatus()->CalculateObservationMetadata(next_month_ts, 2);
// Ensure that the metadata is calculated correctly.
EXPECT_FALSE(metadata_0.has_value());
EXPECT_FALSE(metadata_1.has_value());
EXPECT_FALSE(metadata_2.has_value());
}
TEST_F(ActiveStatusTest, GetFirstActiveWeekActivateDateNotSetReturnsNullopt) {
// Get the first active week.
std::optional<base::Time> first_active_week = GetFirstActiveWeekForTest();
// Ensure that nullopt is returned when the activate date is not set.
EXPECT_FALSE(first_active_week.has_value());
}
TEST_F(ActiveStatusTest, GetFirstActiveWeekActivateDateSetReturnsExpectedWeek) {
// Setup the activate date (ISO8601) and expected first active week for test.
std::string activate_date = "2021-23";
base::Time expected_activate_date_ts;
ASSERT_TRUE(base::Time::FromUTCString("2021-06-07 00:00:00 GMT",
&expected_activate_date_ts));
SetActivateDate(activate_date);
std::optional<base::Time> first_active_week = GetFirstActiveWeekForTest();
// Ensure that the returned week matches the expected value.
EXPECT_TRUE(first_active_week.has_value());
EXPECT_EQ(first_active_week.value(), expected_activate_date_ts);
// Setup the activate date (ISO8601) and expected first active week for test.
activate_date = "2023-52";
ASSERT_TRUE(base::Time::FromUTCString("2023-12-25 00:00:00 GMT",
&expected_activate_date_ts));
SetActivateDate(activate_date);
first_active_week = GetFirstActiveWeekForTest();
// Ensure that the returned week matches the expected value.
EXPECT_TRUE(first_active_week.has_value());
EXPECT_EQ(first_active_week.value(), expected_activate_date_ts);
}
TEST_F(ActiveStatusTest, GetFirstActiveWeekInvalidActivateDateSet) {
// Setup the activate date (ISO8601) and expected first active week for test.
std::string activate_date = "2021-xx";
SetActivateDate(activate_date);
std::optional<base::Time> first_active_week = GetFirstActiveWeekForTest();
EXPECT_FALSE(first_active_week.has_value());
// Setup the activate date (ISO8601) and expected first active week for test.
activate_date = "2023-99";
SetActivateDate(activate_date);
first_active_week = GetFirstActiveWeekForTest();
EXPECT_FALSE(first_active_week.has_value());
// Setup the activate date (ISO8601) and expected first active week for test.
activate_date = "-------";
SetActivateDate(activate_date);
first_active_week = GetFirstActiveWeekForTest();
EXPECT_FALSE(first_active_week.has_value());
}
} // namespace ash::report::device_metrics
|