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

Unified Diff: components/ntp_snippets/ntp_snippets_service_unittest.cc

Issue 2363753002: [NTP Snippets] Don't reschedule background fetching on every startup (Closed)
Patch Set: rebase Created 4 years, 3 months 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/ntp_snippets/ntp_snippets_service.cc ('k') | components/ntp_snippets/pref_names.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/ntp_snippets/ntp_snippets_service_unittest.cc
diff --git a/components/ntp_snippets/ntp_snippets_service_unittest.cc b/components/ntp_snippets/ntp_snippets_service_unittest.cc
index 42721eebf41e2db4b10b77934123f0636902d31c..488804047e6308bee64af0ac2ece5a72c8a0b379 100644
--- a/components/ntp_snippets/ntp_snippets_service_unittest.cc
+++ b/components/ntp_snippets/ntp_snippets_service_unittest.cc
@@ -482,6 +482,7 @@ TEST_F(NTPSnippetsServiceTest, ScheduleOnStart) {
// completes, the second one after the automatic (since the service doesn't
// have any data yet) fetch finishes.
EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2);
+ EXPECT_CALL(mock_scheduler(), Unschedule()).Times(0);
auto service = MakeSnippetsService();
// When we have no snippets are all, loading the service initiates a fetch.
@@ -489,6 +490,23 @@ TEST_F(NTPSnippetsServiceTest, ScheduleOnStart) {
EXPECT_EQ("OK", service->snippets_fetcher()->last_status());
}
+TEST_F(NTPSnippetsServiceTest, DontRescheduleOnStart) {
+ EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2);
+ EXPECT_CALL(mock_scheduler(), Unschedule()).Times(0);
+ auto service = MakeSnippetsService();
+ base::RunLoop().RunUntilIdle();
+
+ // When recreating the service, we should not get a single |Schedule| call:
+ // The tasks are already scheduled with the correct intervals, so nothing on
+ // initialization, but the service still has no data, so one |Schedule|
+ // happens after the automatic fetch.
+ Mock::VerifyAndClearExpectations(&mock_scheduler());
+ EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(1);
+ EXPECT_CALL(mock_scheduler(), Unschedule()).Times(0);
+ ResetSnippetsService(&service);
+ base::RunLoop().RunUntilIdle();
+}
+
TEST_F(NTPSnippetsServiceTest, RescheduleAfterSuccessfulFetch) {
// We should get two |Schedule| calls: The first when initialization
// completes, the second one after the automatic (since the service doesn't
@@ -515,7 +533,7 @@ TEST_F(NTPSnippetsServiceTest, DontRescheduleAfterFailedFetch) {
LoadFromJSONString(service.get(), GetTestJson({GetInvalidSnippet()}));
}
-TEST_F(NTPSnippetsServiceTest, DontRescheduleBeforeInit) {
+TEST_F(NTPSnippetsServiceTest, IgnoreRescheduleBeforeInit) {
// We should get two |Schedule| calls: The first when initialization
// completes, the second one after the automatic (since the service doesn't
// have any data yet) fetch finishes.
@@ -524,12 +542,24 @@ TEST_F(NTPSnippetsServiceTest, DontRescheduleBeforeInit) {
// result in an |Unschedule|), since the service isn't initialized yet.
EXPECT_CALL(mock_scheduler(), Unschedule()).Times(0);
auto service = MakeSnippetsServiceWithoutInitialization();
- service->RescheduleFetching();
+ service->RescheduleFetching(false);
WaitForSnippetsServiceInitialization();
+}
- // Now that initialization has finished, |RescheduleFetching| should work.
- EXPECT_CALL(mock_scheduler(), Schedule(_, _));
- service->RescheduleFetching();
+TEST_F(NTPSnippetsServiceTest, HandleForcedRescheduleBeforeInit) {
+ {
+ InSequence s;
+ // The |RescheduleFetching| call with force=true should result in an
+ // |Unschedule|, since the service isn't initialized yet.
+ EXPECT_CALL(mock_scheduler(), Unschedule()).Times(1);
+ // We should get two |Schedule| calls: The first when initialization
+ // completes, the second one after the automatic (since the service doesn't
+ // have any data yet) fetch finishes.
+ EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2);
+ }
+ auto service = MakeSnippetsServiceWithoutInitialization();
+ service->RescheduleFetching(true);
+ WaitForSnippetsServiceInitialization();
}
TEST_F(NTPSnippetsServiceTest, RescheduleOnStateChange) {
@@ -555,6 +585,17 @@ TEST_F(NTPSnippetsServiceTest, RescheduleOnStateChange) {
base::RunLoop().RunUntilIdle();
}
+TEST_F(NTPSnippetsServiceTest, DontUnscheduleOnShutdown) {
+ EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2);
+ EXPECT_CALL(mock_scheduler(), Unschedule()).Times(0);
+
+ auto service = MakeSnippetsService();
+ base::RunLoop().RunUntilIdle();
+
+ service.reset();
+ base::RunLoop().RunUntilIdle();
+}
+
TEST_F(NTPSnippetsServiceTest, Full) {
std::string json_str(GetTestJson({GetSnippet()}));
« no previous file with comments | « components/ntp_snippets/ntp_snippets_service.cc ('k') | components/ntp_snippets/pref_names.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698