Skip to content

Commit c08710a

Browse files
authored
Merge pull request #2112 from brave/stats-updater-fixes
Stats updater fixes
2 parents 382e967 + c957080 commit c08710a

File tree

3 files changed

+93
-18
lines changed

3 files changed

+93
-18
lines changed

browser/brave_stats_updater_params.cc

+18-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* This Source Code Form is subject to the terms of the Mozilla Public
1+
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
2+
* This Source Code Form is subject to the terms of the Mozilla Public
23
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
34
* You can obtain one at http://mozilla.org/MPL/2.0/. */
45

@@ -14,7 +15,7 @@
1415

1516
namespace brave {
1617

17-
base::Time BraveStatsUpdaterParams::g_current_time(base::Time::Now());
18+
base::Time BraveStatsUpdaterParams::g_current_time;
1819

1920
BraveStatsUpdaterParams::BraveStatsUpdaterParams(PrefService* pref_service)
2021
: BraveStatsUpdaterParams(pref_service,
@@ -84,31 +85,31 @@ std::string BraveStatsUpdaterParams::BooleanToString(bool bool_value) const {
8485
}
8586

8687
std::string BraveStatsUpdaterParams::GetCurrentDateAsYMD() const {
87-
return brave::GetDateAsYMD(g_current_time);
88+
return brave::GetDateAsYMD(GetCurrentTimeNow());
8889
}
8990

9091
std::string BraveStatsUpdaterParams::GetLastMondayAsYMD() const {
91-
base::Time now = g_current_time;
92+
base::Time now = GetCurrentTimeNow();
9293
base::Time::Exploded exploded;
9394
now.LocalExplode(&exploded);
9495

95-
base::Time last_monday;
96-
last_monday = base::Time::FromJsTime(
97-
now.ToJsTime() -
98-
((exploded.day_of_week - 1) * base::Time::kMillisecondsPerDay));
96+
int days_adjusted =
97+
(exploded.day_of_week == 0) ? 6 : exploded.day_of_week - 1;
98+
base::Time last_monday = base::Time::FromJsTime(
99+
now.ToJsTime() - (days_adjusted * base::Time::kMillisecondsPerDay));
99100

100101
return brave::GetDateAsYMD(last_monday);
101102
}
102103

103104
int BraveStatsUpdaterParams::GetCurrentMonth() const {
104-
base::Time now = g_current_time;
105+
base::Time now = GetCurrentTimeNow();
105106
base::Time::Exploded exploded;
106107
now.LocalExplode(&exploded);
107108
return exploded.month;
108109
}
109110

110111
int BraveStatsUpdaterParams::GetCurrentISOWeekNumber() const {
111-
base::Time now = g_current_time;
112+
base::Time now = GetCurrentTimeNow();
112113
base::Time::Exploded now_exploded;
113114
now.LocalExplode(&now_exploded);
114115
now_exploded.hour = 0;
@@ -138,12 +139,17 @@ int BraveStatsUpdaterParams::GetCurrentISOWeekNumber() const {
138139

139140
return 1 + std::round(
140141
((now_adjusted.ToJsTime() - jan4_time.ToJsTime()) / 86400000 -
141-
3 + (jan4_exploded.day_of_month + 6) % 7) /
142+
3 + (jan4_exploded.day_of_week + 6) % 7) /
142143
7);
143144
}
144145

146+
base::Time BraveStatsUpdaterParams::GetCurrentTimeNow() const {
147+
return g_current_time.is_null() ? base::Time::Now() : g_current_time;
148+
}
149+
145150
// static
146-
void BraveStatsUpdaterParams::SetCurrentTimeForTest(const base::Time& current_time) {
151+
void BraveStatsUpdaterParams::SetCurrentTimeForTest(
152+
const base::Time& current_time) {
147153
g_current_time = current_time;
148154
}
149155

browser/brave_stats_updater_params.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* This Source Code Form is subject to the terms of the Mozilla Public
1+
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
2+
* This Source Code Form is subject to the terms of the Mozilla Public
23
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
34
* You can obtain one at http://mozilla.org/MPL/2.0/. */
45

@@ -20,7 +21,7 @@ namespace brave {
2021

2122
class BraveStatsUpdaterParams {
2223
public:
23-
BraveStatsUpdaterParams(PrefService* pref_service);
24+
explicit BraveStatsUpdaterParams(PrefService* pref_service);
2425
BraveStatsUpdaterParams(PrefService* pref_service,
2526
const std::string& ymd,
2627
int woy,
@@ -36,7 +37,7 @@ class BraveStatsUpdaterParams {
3637

3738
void SavePrefs();
3839

39-
private:
40+
private:
4041
friend class ::BraveStatsUpdaterTest;
4142
PrefService* pref_service_;
4243
std::string ymd_;
@@ -59,6 +60,7 @@ class BraveStatsUpdaterParams {
5960
std::string GetLastMondayAsYMD() const;
6061
int GetCurrentMonth() const;
6162
int GetCurrentISOWeekNumber() const;
63+
base::Time GetCurrentTimeNow() const;
6264

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

browser/brave_stats_updater_unittest.cc

+70-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* This Source Code Form is subject to the terms of the Mozilla Public
1+
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
2+
* This Source Code Form is subject to the terms of the Mozilla Public
23
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
34
* You can obtain one at http://mozilla.org/MPL/2.0/. */
45

@@ -32,8 +33,8 @@ class BraveStatsUpdaterTest: public testing::Test {
3233

3334
void SetUp() override {
3435
brave::RegisterPrefsForBraveStatsUpdater(testing_local_state_.registry());
35-
brave::RegisterPrefsForBraveReferralsService(testing_local_state_.registry());
36-
brave::BraveStatsUpdaterParams::SetCurrentTimeForTest(base::Time::Now());
36+
brave::RegisterPrefsForBraveReferralsService(
37+
testing_local_state_.registry());
3738
}
3839

3940
PrefService* GetLocalState() { return &testing_local_state_; }
@@ -218,3 +219,69 @@ TEST_F(BraveStatsUpdaterTest, IsWeeklyUpdateNeededOnMondayLastCheckedOnSunday) {
218219
ASSERT_EQ(GetLocalState()->GetInteger(kLastCheckWOY), 45);
219220
}
220221
}
222+
223+
TEST_F(BraveStatsUpdaterTest, HasCorrectWeekOfInstallation) {
224+
base::Time::Exploded exploded;
225+
base::Time current_time;
226+
227+
{
228+
// Set date to 2019-03-24 (Sunday)
229+
exploded.hour = 0;
230+
exploded.minute = 0;
231+
exploded.second = 0;
232+
exploded.millisecond = 0;
233+
exploded.day_of_week = 0;
234+
exploded.year = 2019;
235+
exploded.month = 3;
236+
exploded.day_of_month = 24;
237+
238+
ASSERT_TRUE(base::Time::FromLocalExploded(exploded, &current_time));
239+
SetCurrentTimeForTest(current_time);
240+
241+
// Make sure that week of installation is previous Monday
242+
brave::BraveStatsUpdaterParams brave_stats_updater_params(GetLocalState());
243+
ASSERT_EQ(brave_stats_updater_params.GetWeekOfInstallationParam(),
244+
"2019-03-18");
245+
}
246+
247+
{
248+
// Set date to 2019-03-25 (Monday)
249+
exploded.hour = 0;
250+
exploded.minute = 0;
251+
exploded.second = 0;
252+
exploded.millisecond = 0;
253+
exploded.day_of_week = 0;
254+
exploded.year = 2019;
255+
exploded.month = 3;
256+
exploded.day_of_month = 25;
257+
258+
ASSERT_TRUE(base::Time::FromLocalExploded(exploded, &current_time));
259+
SetCurrentTimeForTest(current_time);
260+
261+
// Make sure that week of installation is today, since today is a
262+
// Monday
263+
brave::BraveStatsUpdaterParams brave_stats_updater_params(GetLocalState());
264+
ASSERT_EQ(brave_stats_updater_params.GetWeekOfInstallationParam(),
265+
"2019-03-25");
266+
}
267+
268+
{
269+
// Set date to 2019-03-30 (Saturday)
270+
exploded.hour = 0;
271+
exploded.minute = 0;
272+
exploded.second = 0;
273+
exploded.millisecond = 0;
274+
exploded.day_of_week = 0;
275+
exploded.year = 2019;
276+
exploded.month = 3;
277+
exploded.day_of_month = 30;
278+
279+
ASSERT_TRUE(base::Time::FromLocalExploded(exploded, &current_time));
280+
SetCurrentTimeForTest(current_time);
281+
282+
// Make sure that week of installation is previous Monday
283+
brave::BraveStatsUpdaterParams brave_stats_updater_params(GetLocalState());
284+
ASSERT_EQ(brave_stats_updater_params.GetWeekOfInstallationParam(),
285+
"2019-03-25");
286+
}
287+
}

0 commit comments

Comments
 (0)