Skip to content

Stats updater fixes #2112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions browser/brave_stats_updater_params.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

Expand All @@ -14,7 +15,7 @@

namespace brave {

base::Time BraveStatsUpdaterParams::g_current_time(base::Time::Now());
base::Time BraveStatsUpdaterParams::g_current_time;

BraveStatsUpdaterParams::BraveStatsUpdaterParams(PrefService* pref_service)
: BraveStatsUpdaterParams(pref_service,
Expand Down Expand Up @@ -84,31 +85,31 @@ std::string BraveStatsUpdaterParams::BooleanToString(bool bool_value) const {
}

std::string BraveStatsUpdaterParams::GetCurrentDateAsYMD() const {
return brave::GetDateAsYMD(g_current_time);
return brave::GetDateAsYMD(GetCurrentTimeNow());
}

std::string BraveStatsUpdaterParams::GetLastMondayAsYMD() const {
base::Time now = g_current_time;
base::Time now = GetCurrentTimeNow();
base::Time::Exploded exploded;
now.LocalExplode(&exploded);

base::Time last_monday;
last_monday = base::Time::FromJsTime(
now.ToJsTime() -
((exploded.day_of_week - 1) * base::Time::kMillisecondsPerDay));
int days_adjusted =
(exploded.day_of_week == 0) ? 6 : exploded.day_of_week - 1;
base::Time last_monday = base::Time::FromJsTime(
now.ToJsTime() - (days_adjusted * base::Time::kMillisecondsPerDay));

return brave::GetDateAsYMD(last_monday);
}

int BraveStatsUpdaterParams::GetCurrentMonth() const {
base::Time now = g_current_time;
base::Time now = GetCurrentTimeNow();
base::Time::Exploded exploded;
now.LocalExplode(&exploded);
return exploded.month;
}

int BraveStatsUpdaterParams::GetCurrentISOWeekNumber() const {
base::Time now = g_current_time;
base::Time now = GetCurrentTimeNow();
base::Time::Exploded now_exploded;
now.LocalExplode(&now_exploded);
now_exploded.hour = 0;
Expand Down Expand Up @@ -138,12 +139,17 @@ int BraveStatsUpdaterParams::GetCurrentISOWeekNumber() const {

return 1 + std::round(
((now_adjusted.ToJsTime() - jan4_time.ToJsTime()) / 86400000 -
3 + (jan4_exploded.day_of_month + 6) % 7) /
3 + (jan4_exploded.day_of_week + 6) % 7) /
7);
}

base::Time BraveStatsUpdaterParams::GetCurrentTimeNow() const {
return g_current_time.is_null() ? base::Time::Now() : g_current_time;
}

// static
void BraveStatsUpdaterParams::SetCurrentTimeForTest(const base::Time& current_time) {
void BraveStatsUpdaterParams::SetCurrentTimeForTest(
const base::Time& current_time) {
g_current_time = current_time;
}

Expand Down
8 changes: 5 additions & 3 deletions browser/brave_stats_updater_params.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

Expand All @@ -20,7 +21,7 @@ namespace brave {

class BraveStatsUpdaterParams {
public:
BraveStatsUpdaterParams(PrefService* pref_service);
explicit BraveStatsUpdaterParams(PrefService* pref_service);
BraveStatsUpdaterParams(PrefService* pref_service,
const std::string& ymd,
int woy,
Expand All @@ -36,7 +37,7 @@ class BraveStatsUpdaterParams {

void SavePrefs();

private:
private:
friend class ::BraveStatsUpdaterTest;
PrefService* pref_service_;
std::string ymd_;
Expand All @@ -59,6 +60,7 @@ class BraveStatsUpdaterParams {
std::string GetLastMondayAsYMD() const;
int GetCurrentMonth() const;
int GetCurrentISOWeekNumber() const;
base::Time GetCurrentTimeNow() const;

static void SetCurrentTimeForTest(const base::Time& current_time);

Expand Down
73 changes: 70 additions & 3 deletions browser/brave_stats_updater_unittest.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

Expand Down Expand Up @@ -32,8 +33,8 @@ class BraveStatsUpdaterTest: public testing::Test {

void SetUp() override {
brave::RegisterPrefsForBraveStatsUpdater(testing_local_state_.registry());
brave::RegisterPrefsForBraveReferralsService(testing_local_state_.registry());
brave::BraveStatsUpdaterParams::SetCurrentTimeForTest(base::Time::Now());
brave::RegisterPrefsForBraveReferralsService(
testing_local_state_.registry());
}

PrefService* GetLocalState() { return &testing_local_state_; }
Expand Down Expand Up @@ -218,3 +219,69 @@ TEST_F(BraveStatsUpdaterTest, IsWeeklyUpdateNeededOnMondayLastCheckedOnSunday) {
ASSERT_EQ(GetLocalState()->GetInteger(kLastCheckWOY), 45);
}
}

TEST_F(BraveStatsUpdaterTest, HasCorrectWeekOfInstallation) {
base::Time::Exploded exploded;
base::Time current_time;

{
// Set date to 2019-03-24 (Sunday)
exploded.hour = 0;
exploded.minute = 0;
exploded.second = 0;
exploded.millisecond = 0;
exploded.day_of_week = 0;
exploded.year = 2019;
exploded.month = 3;
exploded.day_of_month = 24;

ASSERT_TRUE(base::Time::FromLocalExploded(exploded, &current_time));
SetCurrentTimeForTest(current_time);

// Make sure that week of installation is previous Monday
brave::BraveStatsUpdaterParams brave_stats_updater_params(GetLocalState());
ASSERT_EQ(brave_stats_updater_params.GetWeekOfInstallationParam(),
"2019-03-18");
}

{
// Set date to 2019-03-25 (Monday)
exploded.hour = 0;
exploded.minute = 0;
exploded.second = 0;
exploded.millisecond = 0;
exploded.day_of_week = 0;
exploded.year = 2019;
exploded.month = 3;
exploded.day_of_month = 25;

ASSERT_TRUE(base::Time::FromLocalExploded(exploded, &current_time));
SetCurrentTimeForTest(current_time);

// Make sure that week of installation is today, since today is a
// Monday
brave::BraveStatsUpdaterParams brave_stats_updater_params(GetLocalState());
ASSERT_EQ(brave_stats_updater_params.GetWeekOfInstallationParam(),
"2019-03-25");
}

{
// Set date to 2019-03-30 (Saturday)
exploded.hour = 0;
exploded.minute = 0;
exploded.second = 0;
exploded.millisecond = 0;
exploded.day_of_week = 0;
exploded.year = 2019;
exploded.month = 3;
exploded.day_of_month = 30;

ASSERT_TRUE(base::Time::FromLocalExploded(exploded, &current_time));
SetCurrentTimeForTest(current_time);

// Make sure that week of installation is previous Monday
brave::BraveStatsUpdaterParams brave_stats_updater_params(GetLocalState());
ASSERT_EQ(brave_stats_updater_params.GetWeekOfInstallationParam(),
"2019-03-25");
}
}