1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/signin/public/base/persistent_repeating_timer.h"
6
7 #include "base/test/task_environment.h"
8 #include "components/prefs/pref_registry_simple.h"
9 #include "components/prefs/testing_pref_service.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace signin {
13
14 namespace {
15
16 const char kLastUpdatedTimePref[] = "test.last_updated_time";
17 constexpr base::TimeDelta kTestDelay = base::TimeDelta::FromHours(2);
18
19 } // namespace
20
21 class PersistentRepeatingTimerTest : public ::testing::Test {
22 public:
PersistentRepeatingTimerTest()23 PersistentRepeatingTimerTest() {
24 pref_service_.registry()->RegisterTimePref(kLastUpdatedTimePref,
25 base::Time());
26 }
27
RunTask()28 void RunTask() { ++call_count_; }
29
CheckCallCount(int call_count)30 void CheckCallCount(int call_count) {
31 base::RunLoop().RunUntilIdle();
32 EXPECT_EQ(call_count, call_count_);
33 }
34
35 base::test::TaskEnvironment task_environment_{
36 base::test::TaskEnvironment::TimeSource::MOCK_TIME};
37 TestingPrefServiceSimple pref_service_;
38 int call_count_ = 0;
39 };
40
41 // Checks that the missing pref is treated like an old one.
TEST_F(PersistentRepeatingTimerTest,MissingPref)42 TEST_F(PersistentRepeatingTimerTest, MissingPref) {
43 PersistentRepeatingTimer timer(
44 &pref_service_, kLastUpdatedTimePref, kTestDelay,
45 base::BindRepeating(&PersistentRepeatingTimerTest::RunTask,
46 base::Unretained(this)));
47 CheckCallCount(0);
48
49 // The task is run immediately on start.
50 timer.Start();
51 CheckCallCount(1);
52
53 task_environment_.FastForwardBy(base::TimeDelta::FromMinutes(1));
54 CheckCallCount(1);
55
56 // And after the delay.
57 task_environment_.FastForwardBy(kTestDelay);
58 CheckCallCount(2);
59 }
60
61 // Checks that spurious calls to Start() have no effect.
TEST_F(PersistentRepeatingTimerTest,MultipleStarts)62 TEST_F(PersistentRepeatingTimerTest, MultipleStarts) {
63 PersistentRepeatingTimer timer(
64 &pref_service_, kLastUpdatedTimePref, kTestDelay,
65 base::BindRepeating(&PersistentRepeatingTimerTest::RunTask,
66 base::Unretained(this)));
67 CheckCallCount(0);
68
69 // The task is run immediately on start.
70 timer.Start();
71 CheckCallCount(1);
72 timer.Start();
73 CheckCallCount(1);
74
75 task_environment_.FastForwardBy(base::TimeDelta::FromMinutes(1));
76 CheckCallCount(1);
77 task_environment_.FastForwardBy(base::TimeDelta::FromMinutes(1));
78 timer.Start();
79 CheckCallCount(1);
80
81 // And after the delay.
82 task_environment_.FastForwardBy(kTestDelay);
83 CheckCallCount(2);
84 timer.Start();
85 CheckCallCount(2);
86 }
87
TEST_F(PersistentRepeatingTimerTest,RecentPref)88 TEST_F(PersistentRepeatingTimerTest, RecentPref) {
89 pref_service_.SetTime(kLastUpdatedTimePref,
90 base::Time::Now() - base::TimeDelta::FromHours(1));
91
92 PersistentRepeatingTimer timer(
93 &pref_service_, kLastUpdatedTimePref, kTestDelay,
94 base::BindRepeating(&PersistentRepeatingTimerTest::RunTask,
95 base::Unretained(this)));
96 CheckCallCount(0);
97
98 // The task is NOT run immediately on start.
99 timer.Start();
100 CheckCallCount(0);
101
102 task_environment_.FastForwardBy(base::TimeDelta::FromMinutes(1));
103 CheckCallCount(0);
104
105 // It is run after te delay.
106 task_environment_.FastForwardBy(base::TimeDelta::FromHours(1));
107 CheckCallCount(1);
108 task_environment_.FastForwardBy(base::TimeDelta::FromHours(1));
109 CheckCallCount(1);
110
111 task_environment_.FastForwardBy(base::TimeDelta::FromHours(1));
112 CheckCallCount(2);
113 }
114
TEST_F(PersistentRepeatingTimerTest,OldPref)115 TEST_F(PersistentRepeatingTimerTest, OldPref) {
116 pref_service_.SetTime(kLastUpdatedTimePref,
117 base::Time::Now() - base::TimeDelta::FromHours(10));
118
119 PersistentRepeatingTimer timer(
120 &pref_service_, kLastUpdatedTimePref, kTestDelay,
121 base::BindRepeating(&PersistentRepeatingTimerTest::RunTask,
122 base::Unretained(this)));
123 CheckCallCount(0);
124
125 // The task is run immediately on start.
126 timer.Start();
127 CheckCallCount(1);
128
129 task_environment_.FastForwardBy(base::TimeDelta::FromMinutes(1));
130 CheckCallCount(1);
131
132 // And after the delay.
133 task_environment_.FastForwardBy(kTestDelay);
134 CheckCallCount(2);
135 }
136
137 } // namespace signin
138