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

Side by Side 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, 2 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/ntp_snippets/ntp_snippets_service.h" 5 #include "components/ntp_snippets/ntp_snippets_service.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 base::ScopedTempDir database_dir_; 475 base::ScopedTempDir database_dir_;
476 476
477 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); 477 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest);
478 }; 478 };
479 479
480 TEST_F(NTPSnippetsServiceTest, ScheduleOnStart) { 480 TEST_F(NTPSnippetsServiceTest, ScheduleOnStart) {
481 // We should get two |Schedule| calls: The first when initialization 481 // We should get two |Schedule| calls: The first when initialization
482 // completes, the second one after the automatic (since the service doesn't 482 // completes, the second one after the automatic (since the service doesn't
483 // have any data yet) fetch finishes. 483 // have any data yet) fetch finishes.
484 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); 484 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2);
485 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(0);
485 auto service = MakeSnippetsService(); 486 auto service = MakeSnippetsService();
486 487
487 // When we have no snippets are all, loading the service initiates a fetch. 488 // When we have no snippets are all, loading the service initiates a fetch.
488 base::RunLoop().RunUntilIdle(); 489 base::RunLoop().RunUntilIdle();
489 EXPECT_EQ("OK", service->snippets_fetcher()->last_status()); 490 EXPECT_EQ("OK", service->snippets_fetcher()->last_status());
490 } 491 }
491 492
493 TEST_F(NTPSnippetsServiceTest, DontRescheduleOnStart) {
494 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2);
495 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(0);
496 auto service = MakeSnippetsService();
497 base::RunLoop().RunUntilIdle();
498
499 // When recreating the service, we should not get a single |Schedule| call:
500 // The tasks are already scheduled with the correct intervals, so nothing on
501 // initialization, but the service still has no data, so one |Schedule|
502 // happens after the automatic fetch.
503 Mock::VerifyAndClearExpectations(&mock_scheduler());
504 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(1);
505 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(0);
506 ResetSnippetsService(&service);
507 base::RunLoop().RunUntilIdle();
508 }
509
492 TEST_F(NTPSnippetsServiceTest, RescheduleAfterSuccessfulFetch) { 510 TEST_F(NTPSnippetsServiceTest, RescheduleAfterSuccessfulFetch) {
493 // We should get two |Schedule| calls: The first when initialization 511 // We should get two |Schedule| calls: The first when initialization
494 // completes, the second one after the automatic (since the service doesn't 512 // completes, the second one after the automatic (since the service doesn't
495 // have any data yet) fetch finishes. 513 // have any data yet) fetch finishes.
496 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); 514 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2);
497 auto service = MakeSnippetsService(); 515 auto service = MakeSnippetsService();
498 base::RunLoop().RunUntilIdle(); 516 base::RunLoop().RunUntilIdle();
499 517
500 // A successful fetch should trigger another |Schedule|. 518 // A successful fetch should trigger another |Schedule|.
501 EXPECT_CALL(mock_scheduler(), Schedule(_, _)); 519 EXPECT_CALL(mock_scheduler(), Schedule(_, _));
502 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); 520 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()}));
503 } 521 }
504 522
505 TEST_F(NTPSnippetsServiceTest, DontRescheduleAfterFailedFetch) { 523 TEST_F(NTPSnippetsServiceTest, DontRescheduleAfterFailedFetch) {
506 // We should get two |Schedule| calls: The first when initialization 524 // We should get two |Schedule| calls: The first when initialization
507 // completes, the second one after the automatic (since the service doesn't 525 // completes, the second one after the automatic (since the service doesn't
508 // have any data yet) fetch finishes. 526 // have any data yet) fetch finishes.
509 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); 527 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2);
510 auto service = MakeSnippetsService(); 528 auto service = MakeSnippetsService();
511 base::RunLoop().RunUntilIdle(); 529 base::RunLoop().RunUntilIdle();
512 530
513 // A failed fetch should NOT trigger another |Schedule|. 531 // A failed fetch should NOT trigger another |Schedule|.
514 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(0); 532 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(0);
515 LoadFromJSONString(service.get(), GetTestJson({GetInvalidSnippet()})); 533 LoadFromJSONString(service.get(), GetTestJson({GetInvalidSnippet()}));
516 } 534 }
517 535
518 TEST_F(NTPSnippetsServiceTest, DontRescheduleBeforeInit) { 536 TEST_F(NTPSnippetsServiceTest, IgnoreRescheduleBeforeInit) {
519 // We should get two |Schedule| calls: The first when initialization 537 // We should get two |Schedule| calls: The first when initialization
520 // completes, the second one after the automatic (since the service doesn't 538 // completes, the second one after the automatic (since the service doesn't
521 // have any data yet) fetch finishes. 539 // have any data yet) fetch finishes.
522 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); 540 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2);
523 // The |RescheduleFetching| call shouldn't do anything (in particular not 541 // The |RescheduleFetching| call shouldn't do anything (in particular not
524 // result in an |Unschedule|), since the service isn't initialized yet. 542 // result in an |Unschedule|), since the service isn't initialized yet.
525 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(0); 543 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(0);
526 auto service = MakeSnippetsServiceWithoutInitialization(); 544 auto service = MakeSnippetsServiceWithoutInitialization();
527 service->RescheduleFetching(); 545 service->RescheduleFetching(false);
528 WaitForSnippetsServiceInitialization(); 546 WaitForSnippetsServiceInitialization();
547 }
529 548
530 // Now that initialization has finished, |RescheduleFetching| should work. 549 TEST_F(NTPSnippetsServiceTest, HandleForcedRescheduleBeforeInit) {
531 EXPECT_CALL(mock_scheduler(), Schedule(_, _)); 550 {
532 service->RescheduleFetching(); 551 InSequence s;
552 // The |RescheduleFetching| call with force=true should result in an
553 // |Unschedule|, since the service isn't initialized yet.
554 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(1);
555 // We should get two |Schedule| calls: The first when initialization
556 // completes, the second one after the automatic (since the service doesn't
557 // have any data yet) fetch finishes.
558 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2);
559 }
560 auto service = MakeSnippetsServiceWithoutInitialization();
561 service->RescheduleFetching(true);
562 WaitForSnippetsServiceInitialization();
533 } 563 }
534 564
535 TEST_F(NTPSnippetsServiceTest, RescheduleOnStateChange) { 565 TEST_F(NTPSnippetsServiceTest, RescheduleOnStateChange) {
536 { 566 {
537 InSequence s; 567 InSequence s;
538 // Initial startup. 568 // Initial startup.
539 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); 569 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2);
540 // Service gets disabled. 570 // Service gets disabled.
541 EXPECT_CALL(mock_scheduler(), Unschedule()); 571 EXPECT_CALL(mock_scheduler(), Unschedule());
542 // Service gets enabled again. 572 // Service gets enabled again.
543 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); 573 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2);
544 } 574 }
545 auto service = MakeSnippetsService(); 575 auto service = MakeSnippetsService();
546 ASSERT_TRUE(service->ready()); 576 ASSERT_TRUE(service->ready());
547 base::RunLoop().RunUntilIdle(); 577 base::RunLoop().RunUntilIdle();
548 578
549 service->OnDisabledReasonChanged(DisabledReason::EXPLICITLY_DISABLED); 579 service->OnDisabledReasonChanged(DisabledReason::EXPLICITLY_DISABLED);
550 ASSERT_FALSE(service->ready()); 580 ASSERT_FALSE(service->ready());
551 base::RunLoop().RunUntilIdle(); 581 base::RunLoop().RunUntilIdle();
552 582
553 service->OnDisabledReasonChanged(DisabledReason::NONE); 583 service->OnDisabledReasonChanged(DisabledReason::NONE);
554 ASSERT_TRUE(service->ready()); 584 ASSERT_TRUE(service->ready());
555 base::RunLoop().RunUntilIdle(); 585 base::RunLoop().RunUntilIdle();
556 } 586 }
557 587
588 TEST_F(NTPSnippetsServiceTest, DontUnscheduleOnShutdown) {
589 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2);
590 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(0);
591
592 auto service = MakeSnippetsService();
593 base::RunLoop().RunUntilIdle();
594
595 service.reset();
596 base::RunLoop().RunUntilIdle();
597 }
598
558 TEST_F(NTPSnippetsServiceTest, Full) { 599 TEST_F(NTPSnippetsServiceTest, Full) {
559 std::string json_str(GetTestJson({GetSnippet()})); 600 std::string json_str(GetTestJson({GetSnippet()}));
560 601
561 auto service = MakeSnippetsService(); 602 auto service = MakeSnippetsService();
562 603
563 LoadFromJSONString(service.get(), json_str); 604 LoadFromJSONString(service.get(), json_str);
564 605
565 ASSERT_THAT(observer().SuggestionsForCategory(articles_category()), 606 ASSERT_THAT(observer().SuggestionsForCategory(articles_category()),
566 SizeIs(1)); 607 SizeIs(1));
567 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1)); 608 ASSERT_THAT(service->GetSnippetsForTesting(articles_category()), SizeIs(1));
(...skipping 469 matching lines...) Expand 10 before | Expand all | Expand 10 after
1037 end = base::Time::FromTimeT(456); 1078 end = base::Time::FromTimeT(456);
1038 base::Callback<bool(const GURL& url)> filter; 1079 base::Callback<bool(const GURL& url)> filter;
1039 service->ClearHistory(begin, end, filter); 1080 service->ClearHistory(begin, end, filter);
1040 1081
1041 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); 1082 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty());
1042 EXPECT_THAT(service->GetDismissedSnippetsForTesting(articles_category()), 1083 EXPECT_THAT(service->GetDismissedSnippetsForTesting(articles_category()),
1043 IsEmpty()); 1084 IsEmpty());
1044 } 1085 }
1045 1086
1046 } // namespace ntp_snippets 1087 } // namespace ntp_snippets
OLDNEW
« 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