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
|
// 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/utils/time_utils.h"
#include "base/test/simple_test_clock.h"
#include "base/time/time.h"
#include "chromeos/ash/components/system/fake_statistics_provider.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/icu/source/i18n/unicode/timezone.h"
namespace ash::report::utils {
TEST(TimeUtilsTest, ConvertGmtToPt) {
// Test case for converting to PT.
base::Time ts;
EXPECT_TRUE(base::Time::FromUTCExploded({2023, 1, 0, 28, 0, 0, 0}, &ts));
// Set test clock to fake ts.
base::SimpleTestClock test_clock;
test_clock.SetNow(ts);
base::Time pt_ts = ConvertGmtToPt(&test_clock);
// Validate that the converted time is 8 hours behind GMT time.
base::TimeDelta expected_diff = base::Hours(-8);
EXPECT_EQ(pt_ts - ts, expected_diff);
}
TEST(TimeUtilsTest, ConvertGmtToPtDaylightSavings) {
// Test case for converting to PT during daylight savings.
base::Time ts;
EXPECT_TRUE(base::Time::FromUTCExploded({2023, 3, 0, 13, 0, 0, 0}, &ts));
// Set test clock to fake ts.
base::SimpleTestClock test_clock;
test_clock.SetNow(ts);
base::Time pt_ts = ConvertGmtToPt(&test_clock);
// Validate that the converted time is 7 hours behind GMT time.
base::TimeDelta expected_diff = base::Hours(-7);
EXPECT_EQ(pt_ts - ts, expected_diff);
}
TEST(TimeUtilsTest, ConvertGmtToPtUnknownTimeZone) {
// Test case for converting to PT when the time zone is unknown.
base::Time ts;
EXPECT_TRUE(base::Time::FromUTCExploded({2023, 1, 0, 28, 0, 0, 0}, &ts));
// Set test clock to fake ts.
base::SimpleTestClock test_clock;
test_clock.SetNow(ts);
// Mock TimeZone::createTimeZone() to return unknown time zone
icu::TimeZone::adoptDefault(
icu::TimeZone::createTimeZone(icu::UnicodeString("UnknownTimeZone")));
base::Time pt_ts = ConvertGmtToPt(&test_clock);
// Validate that the converted time is 8 hours behind GMT time.
base::TimeDelta expected_diff = base::Hours(-8);
EXPECT_EQ(pt_ts - ts, expected_diff);
}
TEST(TimeUtilsTest, GetPreviousMonth) {
// Test case for getting the previous month.
base::Time ts;
EXPECT_TRUE(base::Time::FromUTCExploded({2023, 5, 0, 28, 0, 0, 0}, &ts));
base::Time previous_month_ts = GetPreviousMonth(ts).value();
// Validate that the previous month is April 2023
base::Time expected_previous_month_ts;
EXPECT_TRUE(base::Time::FromUTCExploded({2023, 4, 0, 1, 0, 0, 0},
&expected_previous_month_ts));
EXPECT_EQ(previous_month_ts, expected_previous_month_ts);
}
TEST(TimeUtilsTest, GetNextMonth) {
// Test case for getting the next month.
base::Time ts;
EXPECT_TRUE(base::Time::FromUTCExploded({2023, 5, 0, 28, 0, 0, 0}, &ts));
base::Time next_month_ts = GetNextMonth(ts).value();
// Validate that the next month is June 2023
base::Time expected_next_month_ts;
EXPECT_TRUE(base::Time::FromUTCExploded({2023, 6, 0, 1, 0, 0, 0},
&expected_next_month_ts));
EXPECT_EQ(next_month_ts, expected_next_month_ts);
}
TEST(TimeUtilsTest, GetPreviousYear) {
// Test case for getting the previous year.
base::Time ts;
EXPECT_TRUE(base::Time::FromUTCExploded({2023, 5, 0, 28, 0, 0, 0}, &ts));
base::Time previous_year_ts = GetPreviousYear(ts).value();
// Validate that the previous year is 2022
base::Time expected_previous_year_ts;
EXPECT_TRUE(base::Time::FromUTCExploded({2022, 5, 0, 1, 0, 0, 0},
&expected_previous_year_ts));
EXPECT_EQ(previous_year_ts, expected_previous_year_ts);
}
TEST(TimeUtilsTest, GetPreviousMonthJanuary) {
// Test case for getting the previous month when the current month is January.
base::Time ts;
EXPECT_TRUE(base::Time::FromUTCExploded({2023, 1, 0, 28, 0, 0, 0}, &ts));
base::Time previous_month_ts = GetPreviousMonth(ts).value();
// Validate that the previous month is December 2022
base::Time expected_previous_month_ts;
EXPECT_TRUE(base::Time::FromUTCExploded({2022, 12, 0, 1, 0, 0, 0},
&expected_previous_month_ts));
EXPECT_EQ(previous_month_ts, expected_previous_month_ts);
}
TEST(TimeUtilsTest, GetNextMonthDecember) {
// Test case for getting the next month when the current month is December.
base::Time ts;
EXPECT_TRUE(base::Time::FromUTCExploded({2023, 12, 0, 28, 0, 0, 0}, &ts));
base::Time next_month_ts = GetNextMonth(ts).value();
// Validate that the next month is January 2024
base::Time expected_next_month_ts;
EXPECT_TRUE(base::Time::FromUTCExploded({2024, 1, 0, 1, 0, 0, 0},
&expected_next_month_ts));
EXPECT_EQ(next_month_ts, expected_next_month_ts);
}
TEST(TimeUtilsTest, GetPreviousYearJanuary) {
// Test case for getting the previous year when the current year is January.
base::Time ts;
EXPECT_TRUE(base::Time::FromUTCExploded({2023, 1, 0, 28, 0, 0, 0}, &ts));
base::Time previous_year_ts = GetPreviousYear(ts).value();
// Validate that the previous year is 2022
base::Time expected_previous_year_ts;
EXPECT_TRUE(base::Time::FromUTCExploded({2022, 1, 0, 1, 0, 0, 0},
&expected_previous_year_ts));
EXPECT_EQ(previous_year_ts, expected_previous_year_ts);
}
TEST(TimeUtilsTest, IsSameYearAndMonth) {
// Test case for checking if two timestamps have the same year and month.
base::Time ts1;
base::Time ts2;
EXPECT_TRUE(base::Time::FromUTCExploded({2023, 5, 0, 25, 0, 0, 0}, &ts1));
EXPECT_TRUE(base::Time::FromUTCExploded({2023, 5, 0, 10, 0, 0, 0}, &ts2));
// Validate that ts1 and ts2 have the same year and month
EXPECT_TRUE(IsSameYearAndMonth(ts1, ts2));
// Modify ts2 to have a different month
EXPECT_TRUE(base::Time::FromUTCExploded({2023, 6, 0, 10, 0, 0, 0}, &ts2));
EXPECT_FALSE(IsSameYearAndMonth(ts1, ts2));
// Modify ts2 to have a different year
EXPECT_TRUE(base::Time::FromUTCExploded({2024, 5, 0, 25, 0, 0, 0}, &ts2));
EXPECT_FALSE(IsSameYearAndMonth(ts1, ts2));
}
TEST(TimeUtilsTest, FormatTimestampToMidnightGMTString) {
// Test case for formatting a timestamp to midnight GMT string.
base::Time ts;
EXPECT_TRUE(
base::Time::FromUTCExploded({2023, 5, 0, 25, 23, 59, 59, 999}, &ts));
std::string formatted_ts = FormatTimestampToMidnightGMTString(ts);
// Validate the formatted string
std::string expected_formatted_ts = "2023-05-25 00:00:00.000 GMT";
EXPECT_EQ(formatted_ts, expected_formatted_ts);
}
TEST(TimeUtilsTest, TimeToYYYYMMDDString) {
// Test case for converting a time to YYYYMMDD string.
base::Time ts;
EXPECT_TRUE(
base::Time::FromUTCExploded({2023, 5, 0, 25, 23, 59, 59, 999}, &ts));
std::string formatted_ts = TimeToYYYYMMDDString(ts);
// Validate the formatted string
std::string expected_formatted_ts = "20230525";
EXPECT_EQ(formatted_ts, expected_formatted_ts);
}
TEST(TimeUtilsTest, TimeToYYYYMMString) {
// Test case for converting a time to YYYYMM string.
base::Time ts;
EXPECT_TRUE(
base::Time::FromUTCExploded({2023, 5, 0, 25, 23, 59, 59, 999}, &ts));
std::string formatted_ts = TimeToYYYYMMString(ts);
// Validate the formatted string
std::string expected_formatted_ts = "202305";
EXPECT_EQ(formatted_ts, expected_formatted_ts);
}
TEST(TimeUtilsTest, GetFirstActiveWeek) {
system::FakeStatisticsProvider statistics_provider;
system::StatisticsProvider::SetTestProvider(&statistics_provider);
// Mocking the StatisticsProvider for testing.
statistics_provider.SetMachineStatistic(system::kActivateDateKey, "2023-18");
// Call the method under test
auto result = GetFirstActiveWeek();
EXPECT_TRUE(result.has_value());
// Calculate the expected result for comparison.
base::Time expected_ts;
EXPECT_TRUE(
base::Time::FromUTCExploded({2023, 5, 0, 1, 0, 0, 0, 0}, &expected_ts));
EXPECT_EQ(result.value(), expected_ts);
}
TEST(TimeUtilsTest, FirstMondayOfISONewYear2000) {
// Call the method under test.
std::optional<base::Time> result = FirstMondayOfISONewYear(2000);
// Calculate the expected result for comparison.
base::Time expected_ts;
EXPECT_TRUE(
base::Time::FromUTCExploded({2000, 1, 0, 3, 0, 0, 0, 0}, &expected_ts));
EXPECT_EQ(result.value(), expected_ts);
}
TEST(TimeUtilsTest, FirstMondayOfISONewYear2001) {
// Call the method under test.
std::optional<base::Time> result = FirstMondayOfISONewYear(2001);
// Calculate the expected result for comparison.
base::Time expected_ts;
EXPECT_TRUE(
base::Time::FromUTCExploded({2001, 1, 0, 1, 0, 0, 0, 0}, &expected_ts));
EXPECT_EQ(result.value(), expected_ts);
}
TEST(TimeUtilsTest, FirstMondayOfISONewYear2020) {
// Call the method under test.
std::optional<base::Time> result = FirstMondayOfISONewYear(2020);
// Calculate the expected result for comparison.
base::Time expected_ts;
EXPECT_TRUE(
base::Time::FromUTCExploded({2019, 12, 1, 30, 0, 0, 0, 0}, &expected_ts));
EXPECT_EQ(result.value(), expected_ts);
}
TEST(TimeUtilsTest, FirstMondayOfISONewYear2025) {
// Call the method under test.
std::optional<base::Time> result = FirstMondayOfISONewYear(2025);
// Calculate the expected result for comparison.
base::Time expected_ts;
EXPECT_TRUE(
base::Time::FromUTCExploded({2024, 12, 1, 30, 0, 0, 0, 0}, &expected_ts));
EXPECT_EQ(result.value(), expected_ts);
}
TEST(TimeUtilsTest, Iso8601DateWeekAsTime_2023_W18) {
// Call method under test and calculate the expected result for comparison.
auto result = Iso8601DateWeekAsTime(2023, 18);
EXPECT_TRUE(result.has_value());
base::Time expected_ts;
EXPECT_TRUE(
base::Time::FromUTCExploded({2023, 5, 0, 1, 0, 0, 0, 0}, &expected_ts));
EXPECT_EQ(result.value(), expected_ts);
}
TEST(TimeUtilsTest, Iso8601DateWeekAsTime_2026_W01) {
// Call method under test and calculate the expected result for comparison.
auto result = Iso8601DateWeekAsTime(2026, 1);
EXPECT_TRUE(result.has_value());
base::Time expected_ts;
EXPECT_TRUE(
base::Time::FromUTCExploded({2025, 12, 1, 29, 0, 0, 0, 0}, &expected_ts));
EXPECT_EQ(result.value(), expected_ts);
}
TEST(TimeUtilsTest, Iso8601DateWeekAsTime_2026_W53) {
// Call method under test and calculate the expected result for comparison.
auto result = Iso8601DateWeekAsTime(2026, 53);
EXPECT_TRUE(result.has_value());
base::Time expected_ts;
EXPECT_TRUE(
base::Time::FromUTCExploded({2026, 12, 1, 28, 0, 0, 0, 0}, &expected_ts));
EXPECT_EQ(result.value(), expected_ts);
}
TEST(TimeUtilsTest, InvalidIso8601DateWeekAsTime) {
// Call the method under test
auto result = Iso8601DateWeekAsTime(0, 0);
EXPECT_FALSE(result.has_value());
result = Iso8601DateWeekAsTime(-1, 0);
EXPECT_FALSE(result.has_value());
// Week of year is greater than 53.
result = Iso8601DateWeekAsTime(2023, 54);
EXPECT_FALSE(result.has_value());
}
TEST(TimeUtilsTest, Iso8601DateWeekAsTimeWeek53) {
// Call the method under test
// I.e Year 2020 has 53 weeks instead of 52.
auto result = Iso8601DateWeekAsTime(2020, 53);
EXPECT_TRUE(result.has_value());
// Calculate the expected result for comparison.
base::Time expected_ts;
EXPECT_TRUE(
base::Time::FromUTCExploded({2020, 12, 0, 28, 0, 0, 0, 0}, &expected_ts));
EXPECT_EQ(result.value(), expected_ts);
}
TEST(TimeUtilsTest, ConvertTimeToISO8601String) {
// Call the method under test.
// Cross validated against calculator: https://planetcalc.com/8540/
struct {
base::Time::Exploded exploded;
std::string_view expected_yyyy_mm;
} kTestCases[] = {
{{2025, 1, 0, 1, 12, 30, 0, 0}, "2025-01"},
{{2020, 1, 0, 1, 12, 30, 0, 0}, "2020-01"},
{{2023, 1, 0, 1, 12, 30, 0, 0}, "2022-52"},
{{2023, 12, 0, 30, 12, 30, 0, 0}, "2023-52"},
{{2019, 12, 0, 30, 12, 30, 0, 0}, "2020-01"},
{{2019, 8, 0, 25, 12, 30, 0, 0}, "2019-34"},
{{2020, 8, 0, 25, 12, 30, 0, 0}, "2020-35"},
{{2021, 8, 0, 25, 12, 30, 0, 0}, "2021-34"},
{{2023, 11, 0, 25, 12, 30, 0, 0}, "2023-47"},
{{2020, 12, 0, 27, 12, 30, 0, 0}, "2020-52"},
{{2020, 12, 0, 28, 12, 30, 0, 0}, "2020-53"},
{{2021, 1, 0, 4, 12, 30, 0, 0}, "2021-01"},
// Edge Case: Returns 52 weeks, even though previous year has 53 weeks.
// ISO calendar date to Gregorian calendar calculator outputs 2020-53.
{{2021, 1, 0, 3, 12, 30, 0, 0}, "2020-52"},
};
for (const auto& test_case : kTestCases) {
SCOPED_TRACE(testing::Message() << "Case: " << test_case.expected_yyyy_mm);
base::Time test_ts;
EXPECT_TRUE(base::Time::FromUTCExploded(test_case.exploded, &test_ts));
EXPECT_EQ(ConvertTimeToISO8601String(test_ts), test_case.expected_yyyy_mm);
}
}
TEST(TimeUtilsTest, IsFirstActiveUnderFourMonthsAgo) {
struct {
base::Time::Exploded active_exploded;
base::Time::Exploded first_active_exploded;
bool expected_result;
} kTestCases[] = {
// Check when active and first active represent the same time.
{{2021, 1, 0, 3, 12, 30, 0, 0}, {2021, 1, 0, 3, 12, 30, 0, 0}, true},
// Check boundary case if first active under four months ago.
{{2021, 1, 0, 31, 12, 30, 0, 0}, {2020, 10, 0, 1, 12, 30, 0, 0}, true},
// Check outside four month boundary is false.
{{2021, 1, 0, 31, 12, 30, 0, 0}, {2020, 9, 0, 1, 12, 30, 0, 0}, false},
};
for (const auto& test_case : kTestCases) {
base::Time active_ts;
base::Time first_active_ts;
EXPECT_TRUE(
base::Time::FromUTCExploded(test_case.active_exploded, &active_ts));
EXPECT_TRUE(base::Time::FromUTCExploded(test_case.first_active_exploded,
&first_active_ts));
// 4 Months ago assuming max 31 days per month.
int max_days_in_4_months = 31 * 4;
EXPECT_EQ(IsFirstActiveUnderNDaysAgo(active_ts, first_active_ts,
max_days_in_4_months),
test_case.expected_result);
}
}
TEST(TimeUtilsTest, IsFirstActiveUnderFiveWeeksAgo) {
struct {
base::Time::Exploded active_exploded;
base::Time::Exploded first_active_exploded;
bool expected_result;
} kTestCases[] = {
// Check when active and first active represent the same time.
{{2021, 1, 0, 3, 12, 30, 0, 0}, {2021, 1, 0, 3, 12, 30, 0, 0}, true},
// Check boundary case if first active under 5 weeks ago.
{{2021, 2, 0, 4, 12, 30, 0, 0}, {2020, 12, 0, 31, 12, 30, 0, 0}, true},
// Check outside four month boundary is false.
{{2021, 1, 0, 31, 12, 30, 0, 0}, {2020, 9, 0, 1, 12, 30, 0, 0}, false},
};
for (const auto& test_case : kTestCases) {
base::Time active_ts;
base::Time first_active_ts;
EXPECT_TRUE(
base::Time::FromUTCExploded(test_case.active_exploded, &active_ts));
EXPECT_TRUE(base::Time::FromUTCExploded(test_case.first_active_exploded,
&first_active_ts));
int max_days_in_5_weeks = 7 * 5;
EXPECT_EQ(IsFirstActiveUnderNDaysAgo(active_ts, first_active_ts,
max_days_in_5_weeks),
test_case.expected_result);
}
}
} // namespace ash::report::utils
|