| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/remote/remote_suggestions_scheduler_impl.h" | 5 #include "components/ntp_snippets/remote/remote_suggestions_scheduler_impl.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 482 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); | 482 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 483 // The second call to NTPOpened 4hrs later again results in a fetch. | 483 // The second call to NTPOpened 4hrs later again results in a fetch. |
| 484 EXPECT_CALL(*provider(), RefetchInTheBackground(_)); | 484 EXPECT_CALL(*provider(), RefetchInTheBackground(_)); |
| 485 } | 485 } |
| 486 | 486 |
| 487 // First enable the scheduler. | 487 // First enable the scheduler. |
| 488 ActivateProvider(); | 488 ActivateProvider(); |
| 489 // Make the first soft fetch successful. | 489 // Make the first soft fetch successful. |
| 490 scheduler()->OnBrowserForegrounded(); | 490 scheduler()->OnBrowserForegrounded(); |
| 491 signal_fetch_done.Run(Status::Success()); | 491 signal_fetch_done.Run(Status::Success()); |
| 492 // Open NTP again after 4hrs. | 492 // Open NTP again after 9hrs. |
| 493 test_clock()->Advance(base::TimeDelta::FromHours(4)); | 493 test_clock()->Advance(base::TimeDelta::FromHours(9)); |
| 494 scheduler()->OnBrowserForegrounded(); | 494 scheduler()->OnBrowserForegrounded(); |
| 495 } | 495 } |
| 496 | 496 |
| 497 TEST_F(RemoteSuggestionsSchedulerImplTest, | 497 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 498 ShouldRescheduleOnRescheduleFetching) { | 498 ShouldRescheduleOnRescheduleFetching) { |
| 499 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); | 499 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 500 scheduler()->RescheduleFetching(); | 500 scheduler()->RescheduleFetching(); |
| 501 } | 501 } |
| 502 | 502 |
| 503 TEST_F(RemoteSuggestionsSchedulerImplTest, ShouldScheduleOnActivation) { | 503 TEST_F(RemoteSuggestionsSchedulerImplTest, ShouldScheduleOnActivation) { |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 596 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER if PrefService is | 596 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER if PrefService is |
| 597 // null. Change the fallback interval for this class. | 597 // null. Change the fallback interval for this class. |
| 598 SetVariationParameter("fetching_interval_hours-fallback-active_ntp_user", | 598 SetVariationParameter("fetching_interval_hours-fallback-active_ntp_user", |
| 599 "1.5"); | 599 "1.5"); |
| 600 | 600 |
| 601 // Schedule() should get called for the second time after params have changed. | 601 // Schedule() should get called for the second time after params have changed. |
| 602 ActivateProvider(); | 602 ActivateProvider(); |
| 603 } | 603 } |
| 604 | 604 |
| 605 TEST_F(RemoteSuggestionsSchedulerImplTest, | 605 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 606 ReschedulesWhenSoftWifiParamChanges) { | 606 ReschedulesWhenShownWifiParamChanges) { |
| 607 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)).Times(2); | 607 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)).Times(2); |
| 608 ActivateProvider(); | 608 ActivateProvider(); |
| 609 | 609 |
| 610 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER if PrefService is | 610 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER if PrefService is |
| 611 // null. Change the on usage interval for this class. | 611 // null. Change the on usage interval for this class. |
| 612 SetVariationParameter("soft_fetching_interval_hours-wifi-active_ntp_user", | 612 SetVariationParameter("soft_fetching_interval_hours-wifi-active_ntp_user", |
| 613 "1.5"); | 613 "1.5"); |
| 614 | 614 |
| 615 // Schedule() should get called for the second time after params have changed. | 615 // Schedule() should get called for the second time after params have changed. |
| 616 ActivateProvider(); | 616 ActivateProvider(); |
| 617 } | 617 } |
| 618 | 618 |
| 619 TEST_F(RemoteSuggestionsSchedulerImplTest, | 619 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 620 ReschedulesWhenSoftFallbackParamChanges) { | 620 ReschedulesWhenShownFallbackParamChanges) { |
| 621 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)).Times(2); | 621 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)).Times(2); |
| 622 ActivateProvider(); | 622 ActivateProvider(); |
| 623 | 623 |
| 624 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER if PrefService is | 624 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER if PrefService is |
| 625 // null. Change the fallback interval for this class. | 625 // null. Change the fallback interval for this class. |
| 626 SetVariationParameter("soft_fetching_interval_hours-fallback-active_ntp_user", | 626 SetVariationParameter("soft_fetching_interval_hours-fallback-active_ntp_user", |
| 627 "1.5"); | 627 "1.5"); |
| 628 | 628 |
| 629 // Schedule() should get called for the second time after params have changed. | 629 // Schedule() should get called for the second time after params have changed. |
| 630 ActivateProvider(); | 630 ActivateProvider(); |
| 631 } | 631 } |
| 632 | 632 |
| 633 TEST_F(RemoteSuggestionsSchedulerImplTest, FetchIntervalForSoftTriggerOnWifi) { | 633 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 634 ReschedulesWhenStartupWifiParamChanges) { |
| 635 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)).Times(2); |
| 636 ActivateProvider(); |
| 637 |
| 638 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER if PrefService is |
| 639 // null. Change the on usage interval for this class. |
| 640 SetVariationParameter("startup_fetching_interval_hours-wifi-active_ntp_user", |
| 641 "1.5"); |
| 642 |
| 643 // Schedule() should get called for the second time after params have changed. |
| 644 ActivateProvider(); |
| 645 } |
| 646 |
| 647 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 648 ReschedulesWhenStartupFallbackParamChanges) { |
| 649 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)).Times(2); |
| 650 ActivateProvider(); |
| 651 |
| 652 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER if PrefService is |
| 653 // null. Change the fallback interval for this class. |
| 654 SetVariationParameter( |
| 655 "startup_fetching_interval_hours-fallback-active_ntp_user", "1.5"); |
| 656 |
| 657 // Schedule() should get called for the second time after params have changed. |
| 658 ActivateProvider(); |
| 659 } |
| 660 |
| 661 TEST_F(RemoteSuggestionsSchedulerImplTest, FetchIntervalForShownTriggerOnWifi) { |
| 634 // Pretend we are on WiFi (already done in ctor, we make it explicit here). | 662 // Pretend we are on WiFi (already done in ctor, we make it explicit here). |
| 635 EXPECT_CALL(*persistent_scheduler(), IsOnUnmeteredConnection()) | 663 EXPECT_CALL(*persistent_scheduler(), IsOnUnmeteredConnection()) |
| 636 .WillRepeatedly(Return(true)); | 664 .WillRepeatedly(Return(true)); |
| 637 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER which uses a 3h time | 665 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER which uses a 3h time |
| 638 // interval by default for soft background fetches on WiFi. | 666 // interval by default for shown trigger on WiFi. |
| 639 | 667 |
| 640 // Initial scheduling after being enabled. | 668 // Initial scheduling after being enabled. |
| 641 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); | 669 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 642 ActivateProvider(); | 670 ActivateProvider(); |
| 643 | 671 |
| 644 // The first call to NTPOpened results in a fetch. | 672 // The first call to NTPOpened results in a fetch. |
| 645 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; | 673 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; |
| 646 EXPECT_CALL(*provider(), RefetchInTheBackground(_)) | 674 EXPECT_CALL(*provider(), RefetchInTheBackground(_)) |
| 647 .WillOnce(SaveArg<0>(&signal_fetch_done)); | 675 .WillOnce(SaveArg<0>(&signal_fetch_done)); |
| 648 scheduler()->OnNTPOpened(); | 676 scheduler()->OnNTPOpened(); |
| 649 // Rescheduling after a succesful fetch. | 677 // Rescheduling after a succesful fetch. |
| 650 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); | 678 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 651 signal_fetch_done.Run(Status::Success()); | 679 signal_fetch_done.Run(Status::Success()); |
| 652 | 680 |
| 653 // Open NTP again after too short delay. This time no fetch is executed. | 681 // Open NTP again after too short delay. This time no fetch is executed. |
| 654 test_clock()->Advance(base::TimeDelta::FromMinutes(30)); | 682 test_clock()->Advance(base::TimeDelta::FromMinutes(30)); |
| 655 scheduler()->OnNTPOpened(); | 683 scheduler()->OnNTPOpened(); |
| 656 | 684 |
| 657 // Open NTP after another delay, now together long enough to issue a fetch. | 685 // Open NTP after another delay, now together long enough to issue a fetch. |
| 658 test_clock()->Advance(base::TimeDelta::FromMinutes(150)); | 686 test_clock()->Advance(base::TimeDelta::FromMinutes(150)); |
| 659 EXPECT_CALL(*provider(), RefetchInTheBackground(_)); | 687 EXPECT_CALL(*provider(), RefetchInTheBackground(_)); |
| 660 scheduler()->OnNTPOpened(); | 688 scheduler()->OnNTPOpened(); |
| 661 } | 689 } |
| 662 | 690 |
| 663 TEST_F(RemoteSuggestionsSchedulerImplTest, | 691 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 664 OverrideFetchIntervalForSoftTriggerOnWifi) { | 692 OverrideFetchIntervalForShownTriggerOnWifi) { |
| 665 // Pretend we are on WiFi (already done in ctor, we make it explicit here). | 693 // Pretend we are on WiFi (already done in ctor, we make it explicit here). |
| 666 EXPECT_CALL(*persistent_scheduler(), IsOnUnmeteredConnection()) | 694 EXPECT_CALL(*persistent_scheduler(), IsOnUnmeteredConnection()) |
| 667 .WillRepeatedly(Return(true)); | 695 .WillRepeatedly(Return(true)); |
| 668 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER if PrefService is | 696 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER if PrefService is |
| 669 // null. Change the on usage interval for this class from 2h to 30min. | 697 // null. Change the interval for this class from 4h to 30min. |
| 670 SetVariationParameter("soft_fetching_interval_hours-wifi-active_ntp_user", | 698 SetVariationParameter("soft_fetching_interval_hours-wifi-active_ntp_user", |
| 671 "0.5"); | 699 "0.5"); |
| 672 | 700 |
| 673 // Initial scheduling after being enabled. | 701 // Initial scheduling after being enabled. |
| 674 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); | 702 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 675 ActivateProvider(); | 703 ActivateProvider(); |
| 676 | 704 |
| 677 // The first call to NTPOpened results in a fetch. | 705 // The first call to NTPOpened results in a fetch. |
| 678 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; | 706 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; |
| 679 EXPECT_CALL(*provider(), RefetchInTheBackground(_)) | 707 EXPECT_CALL(*provider(), RefetchInTheBackground(_)) |
| 680 .WillOnce(SaveArg<0>(&signal_fetch_done)); | 708 .WillOnce(SaveArg<0>(&signal_fetch_done)); |
| 681 scheduler()->OnNTPOpened(); | 709 scheduler()->OnNTPOpened(); |
| 682 // Rescheduling after a succesful fetch. | 710 // Rescheduling after a succesful fetch. |
| 683 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); | 711 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 684 signal_fetch_done.Run(Status::Success()); | 712 signal_fetch_done.Run(Status::Success()); |
| 685 | 713 |
| 686 // Open NTP again after too short delay. This time no fetch is executed. | 714 // Open NTP again after too short delay. This time no fetch is executed. |
| 687 test_clock()->Advance(base::TimeDelta::FromMinutes(20)); | 715 test_clock()->Advance(base::TimeDelta::FromMinutes(20)); |
| 688 scheduler()->OnNTPOpened(); | 716 scheduler()->OnNTPOpened(); |
| 689 | 717 |
| 690 // Open NTP after another delay, now together long enough to issue a fetch. | 718 // Open NTP after another delay, now together long enough to issue a fetch. |
| 691 test_clock()->Advance(base::TimeDelta::FromMinutes(10)); | 719 test_clock()->Advance(base::TimeDelta::FromMinutes(10)); |
| 692 EXPECT_CALL(*provider(), RefetchInTheBackground(_)); | 720 EXPECT_CALL(*provider(), RefetchInTheBackground(_)); |
| 693 scheduler()->OnNTPOpened(); | 721 scheduler()->OnNTPOpened(); |
| 694 } | 722 } |
| 695 | 723 |
| 696 TEST_F(RemoteSuggestionsSchedulerImplTest, | 724 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 697 FetchIntervalForSoftTriggerOnFallback) { | 725 FetchIntervalForShownTriggerOnFallback) { |
| 698 // Pretend we are not on wifi -> fallback connection. | 726 // Pretend we are not on wifi -> fallback connection. |
| 699 EXPECT_CALL(*persistent_scheduler(), IsOnUnmeteredConnection()) | 727 EXPECT_CALL(*persistent_scheduler(), IsOnUnmeteredConnection()) |
| 700 .WillRepeatedly(Return(false)); | 728 .WillRepeatedly(Return(false)); |
| 701 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER which uses a 6h time | 729 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER which uses a 6h time |
| 702 // interval by default for soft background fetches not on WiFi. | 730 // interval by default for shown trigger not on WiFi. |
| 703 | 731 |
| 704 // Initial scheduling after being enabled. | 732 // Initial scheduling after being enabled. |
| 705 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); | 733 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 706 ActivateProvider(); | 734 ActivateProvider(); |
| 707 | 735 |
| 708 // The first call to NTPOpened results in a fetch. | 736 // The first call to NTPOpened results in a fetch. |
| 709 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; | 737 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; |
| 710 EXPECT_CALL(*provider(), RefetchInTheBackground(_)) | 738 EXPECT_CALL(*provider(), RefetchInTheBackground(_)) |
| 711 .WillOnce(SaveArg<0>(&signal_fetch_done)); | 739 .WillOnce(SaveArg<0>(&signal_fetch_done)); |
| 712 scheduler()->OnNTPOpened(); | 740 scheduler()->OnNTPOpened(); |
| 713 // Rescheduling after a succesful fetch. | 741 // Rescheduling after a succesful fetch. |
| 714 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); | 742 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 715 signal_fetch_done.Run(Status::Success()); | 743 signal_fetch_done.Run(Status::Success()); |
| 716 | 744 |
| 717 // Open NTP again after too short delay. This time no fetch is executed. | 745 // Open NTP again after too short delay. This time no fetch is executed. |
| 718 test_clock()->Advance(base::TimeDelta::FromMinutes(300)); | 746 test_clock()->Advance(base::TimeDelta::FromMinutes(300)); |
| 719 scheduler()->OnNTPOpened(); | 747 scheduler()->OnNTPOpened(); |
| 720 | 748 |
| 721 // Open NTP after another delay, now together long enough to issue a fetch. | 749 // Open NTP after another delay, now together long enough to issue a fetch. |
| 722 test_clock()->Advance(base::TimeDelta::FromMinutes(60)); | 750 test_clock()->Advance(base::TimeDelta::FromMinutes(60)); |
| 723 EXPECT_CALL(*provider(), RefetchInTheBackground(_)); | 751 EXPECT_CALL(*provider(), RefetchInTheBackground(_)); |
| 724 scheduler()->OnNTPOpened(); | 752 scheduler()->OnNTPOpened(); |
| 725 } | 753 } |
| 726 | 754 |
| 727 TEST_F(RemoteSuggestionsSchedulerImplTest, | 755 TEST_F(RemoteSuggestionsSchedulerImplTest, |
| 728 OverrideFetchIntervalForSoftTriggerOnFallback) { | 756 OverrideFetchIntervalForShownTriggerOnFallback) { |
| 729 // Pretend we are not on wifi -> fallback connection. | 757 // Pretend we are not on wifi -> fallback connection. |
| 730 EXPECT_CALL(*persistent_scheduler(), IsOnUnmeteredConnection()) | 758 EXPECT_CALL(*persistent_scheduler(), IsOnUnmeteredConnection()) |
| 731 .WillRepeatedly(Return(false)); | 759 .WillRepeatedly(Return(false)); |
| 732 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER if PrefService is | 760 // UserClassifier defaults to UserClass::ACTIVE_NTP_USER if PrefService is |
| 733 // null. Change the on usage interval for this class from 4h to 30min. | 761 // null. Change the interval for this class from 4h to 30min. |
| 734 SetVariationParameter("soft_fetching_interval_hours-fallback-active_ntp_user", | 762 SetVariationParameter("soft_fetching_interval_hours-fallback-active_ntp_user", |
| 735 "0.5"); | 763 "0.5"); |
| 736 | 764 |
| 737 // Initial scheduling after being enabled. | 765 // Initial scheduling after being enabled. |
| 738 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); | 766 EXPECT_CALL(*persistent_scheduler(), Schedule(_, _)); |
| 739 ActivateProvider(); | 767 ActivateProvider(); |
| 740 | 768 |
| 741 // The first call to NTPOpened results in a fetch. | 769 // The first call to NTPOpened results in a fetch. |
| 742 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; | 770 RemoteSuggestionsProvider::FetchStatusCallback signal_fetch_done; |
| 743 EXPECT_CALL(*provider(), RefetchInTheBackground(_)) | 771 EXPECT_CALL(*provider(), RefetchInTheBackground(_)) |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 833 for (int x = 0; x < 5; ++x) { | 861 for (int x = 0; x < 5; ++x) { |
| 834 scheduler()->OnPersistentSchedulerWakeUp(); | 862 scheduler()->OnPersistentSchedulerWakeUp(); |
| 835 signal_fetch_done.Run(Status::Success()); | 863 signal_fetch_done.Run(Status::Success()); |
| 836 } | 864 } |
| 837 | 865 |
| 838 // For the 6th time, it is blocked by the scheduling provider. | 866 // For the 6th time, it is blocked by the scheduling provider. |
| 839 scheduler()->OnPersistentSchedulerWakeUp(); | 867 scheduler()->OnPersistentSchedulerWakeUp(); |
| 840 } | 868 } |
| 841 | 869 |
| 842 } // namespace ntp_snippets | 870 } // namespace ntp_snippets |
| OLD | NEW |