Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(192)

Side by Side Diff: components/ntp_snippets/remote/remote_suggestions_scheduler_unittest.cc

Issue 2557363002: [NTP Snippets] Refactor background scheduling for remote suggestions (Closed)
Patch Set: Marc's comments #1 Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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/ntp_snippets/remote/remote_suggestions_scheduler.h"
6
7 #include <memory>
8 #include <set>
9 #include <string>
10 #include <utility>
11 #include <vector>
12
13 #include "base/command_line.h"
14 #include "base/macros.h"
15 #include "base/memory/ptr_util.h"
16 #include "base/message_loop/message_loop.h"
17 #include "base/run_loop.h"
18 #include "base/threading/thread_task_runner_handle.h"
19 #include "base/time/time.h"
20 #include "components/ntp_snippets/features.h"
21 #include "components/ntp_snippets/ntp_snippets_constants.h"
22 #include "components/ntp_snippets/pref_names.h"
23 #include "components/ntp_snippets/remote/remote_suggestions_hard_scheduler.h"
24 #include "components/ntp_snippets/remote/test_utils.h"
25 #include "components/ntp_snippets/user_classifier.h"
26 #include "components/prefs/testing_pref_service.h"
27 #include "components/variations/variations_params_manager.h"
28 #include "testing/gmock/include/gmock/gmock.h"
29 #include "testing/gtest/include/gtest/gtest.h"
30
31 using testing::ElementsAre;
32 using testing::Eq;
33 using testing::InSequence;
34 using testing::Invoke;
35 using testing::IsEmpty;
36 using testing::Mock;
37 using testing::MockFunction;
38 using testing::NiceMock;
39 using testing::Not;
40 using testing::SaveArg;
41 using testing::SizeIs;
42 using testing::StartsWith;
43 using testing::WithArgs;
44 using testing::_;
45
46 namespace ntp_snippets {
47
48 namespace {
49
50 const char* kDifferentWiFiInterval = "2";
51
52 class MockHardScheduler : public RemoteSuggestionsHardScheduler {
53 public:
54 MOCK_METHOD2(Schedule,
55 bool(base::TimeDelta period_wifi,
56 base::TimeDelta period_fallback));
57 MOCK_METHOD0(Unschedule, bool());
58 };
59
60 class MockUpdater : public RemoteSuggestionsScheduler::Updater {
61 public:
62 MOCK_METHOD0(UpdateRemoteSuggestionsBySchedule, void());
63 };
64
65 } // namespace
66
67 class RemoteSuggestionsSchedulerTest : public ::testing::Test {
68 public:
69 RemoteSuggestionsSchedulerTest()
70 : user_classifier_(/*pref_service=*/nullptr) {
71 RemoteSuggestionsScheduler::RegisterProfilePrefs(
72 utils_.pref_service()->registry());
73 }
74
75 std::unique_ptr<RemoteSuggestionsScheduler> MakeScheduler(bool enabled) {
76 auto scheduler = base::MakeUnique<RemoteSuggestionsScheduler>(
77 &mock_scheduler(), &updater(), &user_classifier_,
78 utils_.pref_service());
79
80 if (enabled) {
81 scheduler->Schedule();
82 } else {
83 scheduler->Unschedule();
84 }
85 return scheduler;
86 }
87
88 protected:
89 MockHardScheduler& mock_scheduler() { return hard_scheduler_; }
90 MockUpdater& updater() { return updater_; }
91
92 private:
93 test::RemoteSuggestionsTestUtils utils_;
94 UserClassifier user_classifier_;
95 NiceMock<MockHardScheduler> hard_scheduler_;
96 NiceMock<MockUpdater> updater_;
97
98 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsSchedulerTest);
99 };
100
101 TEST_F(RemoteSuggestionsSchedulerTest, SchedulesOnStart) {
102 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(1);
103 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(0);
104
105 auto scheduler = MakeScheduler(/*enabled=*/true);
106 }
107
108 TEST_F(RemoteSuggestionsSchedulerTest, UnschedulesOnStartWhenDisabled) {
109 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(0);
110 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(1);
111
112 auto scheduler = MakeScheduler(/*enabled=*/false);
113 }
114
115 TEST_F(RemoteSuggestionsSchedulerTest, DoesNotUnscheduleOnShutdown) {
116 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(1);
117 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(0);
118
119 auto scheduler = MakeScheduler(/*enabled=*/true);
120
121 scheduler.reset();
122 }
123
124 TEST_F(RemoteSuggestionsSchedulerTest, UnscheduleOnlyOnce) {
125 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(1);
126
127 auto scheduler = MakeScheduler(/*enabled=*/false);
128
129 // When unscheduling again, we should not get any |Schedule| calls:
130 // The tasks are already unscheduled, so no need to do it again.
131 Mock::VerifyAndClearExpectations(&mock_scheduler());
132 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(0);
133 scheduler->Unschedule();
134 }
135
136 TEST_F(RemoteSuggestionsSchedulerTest, ScheduleOnlyOnce) {
137 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(1);
138
139 auto scheduler = MakeScheduler(/*enabled=*/true);
140
141 // When scheduling again, we should not get any |Schedule| calls:
142 // The tasks are already scheduled with the correct intervals, so no need to
143 // do it again.
144 Mock::VerifyAndClearExpectations(&mock_scheduler());
145 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(0);
146 scheduler->Schedule();
147 }
148
149 TEST_F(RemoteSuggestionsSchedulerTest, RescheduleWhenWifiParamChanges) {
150 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(1);
151 auto scheduler = MakeScheduler(/*enabled=*/true);
152
153 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER if PrefService is
154 // null. Change the wifi interval for this class.
155 variations::testing::VariationParamsManager params_manager(
156 ntp_snippets::kStudyName,
157 {{"fetching_interval_hours-wifi-active_ntp_user",
158 kDifferentWiFiInterval}},
159 {kArticleSuggestionsFeature.name});
160
161 Mock::VerifyAndClearExpectations(&mock_scheduler());
162 // After the change of a paramter, it should reschedule.
163 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(1);
164 scheduler->Schedule();
165 }
166
167 TEST_F(RemoteSuggestionsSchedulerTest, RescheduleWhenFallbackParamChanges) {
168 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(1);
169 auto scheduler = MakeScheduler(/*enabled=*/true);
170
171 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER if PrefService is
172 // null. Change the wifi interval for this class.
173 variations::testing::VariationParamsManager params_manager(
174 ntp_snippets::kStudyName,
175 {{"fetching_interval_hours-fallback-active_ntp_user",
176 kDifferentWiFiInterval}},
177 {kArticleSuggestionsFeature.name});
178
179 Mock::VerifyAndClearExpectations(&mock_scheduler());
180 // After the change of a paramter, it should reschedule.
181 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(1);
182 scheduler->Schedule();
183 }
184
185 TEST_F(RemoteSuggestionsSchedulerTest, HandlesForcedReschedules) {
186 {
187 InSequence s;
188 // The initial scheduling.
189 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(1);
190 // Forced rescheduling.
191 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(1);
192 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(1);
193 }
194
195 auto scheduler = MakeScheduler(/*enabled=*/true);
196 // Forced reschedule
197 scheduler->Unschedule();
198 scheduler->Schedule();
199 }
200
201 TEST_F(RemoteSuggestionsSchedulerTest, ReschedulesAfterSuccessfulUpdate) {
202 // The initial scheduling.
203 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(1);
204 auto scheduler = MakeScheduler(/*enabled=*/true);
205 Mock::VerifyAndClearExpectations(&mock_scheduler());
206
207 // Simulate a succesfull update.
208 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(1);
209 scheduler->PerformHardUpdate();
210 scheduler->OnSuccessfulUpdate();
211 }
212
213 TEST_F(RemoteSuggestionsSchedulerTest, DoesNotRescheduleAfterFailedUpdate) {
214 // The initial scheduling.
215 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(1);
216 auto scheduler = MakeScheduler(/*enabled=*/true);
217 Mock::VerifyAndClearExpectations(&mock_scheduler());
218
219 // Simulate a succesfull update.
220 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(0);
221 scheduler->PerformHardUpdate();
222 // Failed -> do not call OnSuccessfulUpdate().
223 }
224
225 TEST_F(RemoteSuggestionsSchedulerTest, CallsUpdater) {
226 // The initial scheduling.
227 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(1);
228 auto scheduler = MakeScheduler(/*enabled=*/true);
229
230 // Simulate a succesfull update.
231 EXPECT_CALL(updater(), UpdateRemoteSuggestionsBySchedule()).Times(1);
232 scheduler->PerformHardUpdate();
233 }
234
235 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698