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

Unified Diff: chrome/browser/ntp_snippets/download_suggestions_provider_unittest.cc

Issue 2673633002: [NTP::Downloads] Do not show old downloads. (Closed)
Patch Set: comments + clean rebase (sorry). Created 3 years, 10 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 | « chrome/browser/ntp_snippets/download_suggestions_provider.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/ntp_snippets/download_suggestions_provider_unittest.cc
diff --git a/chrome/browser/ntp_snippets/download_suggestions_provider_unittest.cc b/chrome/browser/ntp_snippets/download_suggestions_provider_unittest.cc
index f63cfce992910b00eccfb5a3ca2ceb4bef4fa123..43c1344ce95ac5673860d0b4ff952df4ad8cf3ed 100644
--- a/chrome/browser/ntp_snippets/download_suggestions_provider_unittest.cc
+++ b/chrome/browser/ntp_snippets/download_suggestions_provider_unittest.cc
@@ -10,6 +10,8 @@
#include "base/memory/ptr_util.h"
#include "base/observer_list.h"
#include "base/strings/string_number_conversions.h"
+#include "base/test/simple_test_clock.h"
+#include "base/time/default_clock.h"
#include "chrome/browser/ntp_snippets/fake_download_item.h"
#include "components/ntp_snippets/category.h"
#include "components/ntp_snippets/mock_content_suggestions_provider_observer.h"
@@ -268,15 +270,19 @@ class DownloadSuggestionsProviderTest : public testing::Test {
EXPECT_CALL(observer_, OnSuggestionInvalidated(_, _)).Times(AnyNumber());
}
- DownloadSuggestionsProvider* CreateLoadedProvider(bool show_assets,
- bool show_offline_pages) {
- CreateProvider(show_assets, show_offline_pages);
+ DownloadSuggestionsProvider* CreateLoadedProvider(
+ bool show_assets,
+ bool show_offline_pages,
+ std::unique_ptr<base::Clock> clock) {
+ CreateProvider(show_assets, show_offline_pages, std::move(clock));
FireHistoryQueryComplete();
return provider_.get();
}
- DownloadSuggestionsProvider* CreateProvider(bool show_assets,
- bool show_offline_pages) {
+ DownloadSuggestionsProvider* CreateProvider(
+ bool show_assets,
+ bool show_offline_pages,
+ std::unique_ptr<base::Clock> clock) {
DCHECK(!provider_);
DCHECK(show_assets || show_offline_pages);
@@ -285,7 +291,7 @@ class DownloadSuggestionsProviderTest : public testing::Test {
provider_ = base::MakeUnique<DownloadSuggestionsProvider>(
&observer_, show_offline_pages ? &offline_pages_model_ : nullptr,
show_assets ? &downloads_manager_ : nullptr, &download_history_,
- pref_service());
+ pref_service(), std::move(clock));
return provider_.get();
}
@@ -386,7 +392,8 @@ TEST_F(DownloadSuggestionsProviderTest,
HasDownloadSuggestionExtra(
/*is_download_asset=*/false,
FILE_PATH_LITERAL(""), "")))));
- CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
+ CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true,
+ base::MakeUnique<base::DefaultClock>());
}
TEST_F(DownloadSuggestionsProviderTest,
@@ -396,7 +403,8 @@ TEST_F(DownloadSuggestionsProviderTest,
EXPECT_CALL(*observer(),
OnNewSuggestions(_, downloads_category(), SizeIs(0)));
- CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
+ CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true,
+ base::MakeUnique<base::DefaultClock>());
std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads =
CreateDummyAssetDownloads({1, 2});
@@ -437,7 +445,8 @@ TEST_F(DownloadSuggestionsProviderTest, ShouldMixInBothSources) {
UnorderedElementsAre(
HasUrl("http://dummy.com/1"),
HasUrl("http://dummy.com/2"))));
- CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
+ CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true,
+ base::MakeUnique<base::DefaultClock>());
std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads =
CreateDummyAssetDownloads({1, 2});
@@ -478,7 +487,8 @@ TEST_F(DownloadSuggestionsProviderTest, ShouldSortSuggestions) {
OnNewSuggestions(_, downloads_category(),
ElementsAre(HasUrl("http://dummy.com/2"),
HasUrl("http://dummy.com/1"))));
- CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
+ CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true,
+ base::MakeUnique<base::DefaultClock>());
std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads;
asset_downloads.push_back(CreateDummyAssetDownload(3, next_week));
@@ -513,7 +523,8 @@ TEST_F(DownloadSuggestionsProviderTest,
HasUrl("http://dummy.com/2"),
HasUrl("http://download.com/1"),
HasUrl("http://download.com/2"))));
- CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
+ CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true,
+ base::MakeUnique<base::DefaultClock>());
EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)).Times(0);
EXPECT_CALL(*observer(), OnSuggestionInvalidated(_, _)).Times(0);
@@ -539,7 +550,8 @@ TEST_F(DownloadSuggestionsProviderTest,
HasUrl("http://dummy.com/2"),
HasUrl("http://download.com/1"),
HasUrl("http://download.com/2"))));
- CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
+ CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true,
+ base::MakeUnique<base::DefaultClock>());
provider()->DismissSuggestion(
GetDummySuggestionId(1, /*is_offline_page=*/true));
@@ -567,7 +579,8 @@ TEST_F(DownloadSuggestionsProviderTest, ShouldReturnDismissedSuggestions) {
HasUrl("http://dummy.com/2"),
HasUrl("http://download.com/1"),
HasUrl("http://download.com/2"))));
- CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
+ CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true,
+ base::MakeUnique<base::DefaultClock>());
provider()->DismissSuggestion(
GetDummySuggestionId(1, /*is_offline_page=*/true));
@@ -591,7 +604,8 @@ TEST_F(DownloadSuggestionsProviderTest, ShouldClearDismissedSuggestions) {
HasUrl("http://dummy.com/2"),
HasUrl("http://download.com/1"),
HasUrl("http://download.com/2"))));
- CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
+ CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true,
+ base::MakeUnique<base::DefaultClock>());
provider()->DismissSuggestion(
GetDummySuggestionId(1, /*is_offline_page=*/true));
@@ -622,7 +636,8 @@ TEST_F(DownloadSuggestionsProviderTest,
HasUrl("http://dummy.com/2"),
HasUrl("http://download.com/1"),
HasUrl("http://download.com/2"))));
- CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
+ CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true,
+ base::MakeUnique<base::DefaultClock>());
provider()->DismissSuggestion(
GetDummySuggestionId(1, /*is_offline_page=*/true));
@@ -652,7 +667,8 @@ TEST_F(DownloadSuggestionsProviderTest, ShouldReplaceDismissedItemWithNewData) {
HasUrl("http://download.com/3"),
HasUrl("http://download.com/4"),
HasUrl("http://download.com/5"))));
- CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
+ CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true,
+ base::MakeUnique<base::DefaultClock>());
provider()->DismissSuggestion(
GetDummySuggestionId(1, /*is_offline_page=*/false));
@@ -684,7 +700,8 @@ TEST_F(DownloadSuggestionsProviderTest,
UnorderedElementsAre(HasUrl("http://dummy.com/1"),
HasUrl("http://dummy.com/2"),
HasUrl("http://download.com/1"))));
- CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
+ CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true,
+ base::MakeUnique<base::DefaultClock>());
// We add another item manually, so that when it gets deleted it is not
// present in DownloadsManager list.
@@ -724,7 +741,8 @@ TEST_F(DownloadSuggestionsProviderTest, ShouldReplaceRemovedItemWithNewData) {
HasUrl("http://download.com/3"),
HasUrl("http://download.com/4"),
HasUrl("http://download.com/5"))));
- CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
+ CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true,
+ base::MakeUnique<base::DefaultClock>());
// Note that |CreateDummyAssetDownloads| creates items "downloaded" before
// |base::Time::Now()|, so for a new item the time is set in future to enforce
@@ -768,7 +786,8 @@ TEST_F(DownloadSuggestionsProviderTest, ShouldPruneOfflinePagesDismissedIDs) {
HasUrl("http://dummy.com/1"),
HasUrl("http://dummy.com/2"),
HasUrl("http://dummy.com/3"))));
- CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
+ CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true,
+ base::MakeUnique<base::DefaultClock>());
provider()->DismissSuggestion(
GetDummySuggestionId(1, /*is_offline_page=*/true));
@@ -798,7 +817,8 @@ TEST_F(DownloadSuggestionsProviderTest, ShouldPruneAssetDownloadsDismissedIDs) {
OnNewSuggestions(_, downloads_category(),
UnorderedElementsAre(HasUrl("http://download.com/1"),
HasUrl("http://download.com/2"))));
- CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
+ CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true,
+ base::MakeUnique<base::DefaultClock>());
provider()->DismissSuggestion(
GetDummySuggestionId(1, /*is_offline_page=*/false));
@@ -819,7 +839,8 @@ TEST_F(DownloadSuggestionsProviderTest, ShouldFetchAssetDownloadsOnStartup) {
OnNewSuggestions(_, downloads_category(),
UnorderedElementsAre(HasUrl("http://download.com/1"),
HasUrl("http://download.com/2"))));
- CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
+ CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true,
+ base::MakeUnique<base::DefaultClock>());
}
TEST_F(DownloadSuggestionsProviderTest,
@@ -832,7 +853,8 @@ TEST_F(DownloadSuggestionsProviderTest,
UnorderedElementsAre(
HasUrl("http://dummy.com/1"),
HasUrl("http://dummy.com/2"))));
- CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true);
+ CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true,
+ base::MakeUnique<base::DefaultClock>());
FireOfflinePageModelLoaded();
}
@@ -841,7 +863,8 @@ TEST_F(DownloadSuggestionsProviderTest,
IgnoreOnCategoryStatusChangedToAvailable();
EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)).Times(0);
- CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
+ CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true,
+ base::MakeUnique<base::DefaultClock>());
*(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2});
EXPECT_CALL(
@@ -861,7 +884,8 @@ TEST_F(DownloadSuggestionsProviderTest,
*observer(),
OnNewSuggestions(_, downloads_category(),
UnorderedElementsAre(HasUrl("http://download.com/1"))));
- CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
+ CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true,
+ base::MakeUnique<base::DefaultClock>());
EXPECT_CALL(*observer(),
OnSuggestionInvalidated(
@@ -878,7 +902,8 @@ TEST_F(DownloadSuggestionsProviderTest,
*(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2});
EXPECT_CALL(*observer(),
OnNewSuggestions(_, downloads_category(), IsEmpty()));
- CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false);
+ CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false,
+ base::MakeUnique<base::DefaultClock>());
std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads =
CreateDummyAssetDownloads({1});
@@ -900,7 +925,8 @@ TEST_F(DownloadSuggestionsProviderTest, ShouldNotShowAssetsWhenTurnedOff) {
UnorderedElementsAre(
HasUrl("http://dummy.com/1"),
HasUrl("http://dummy.com/2"))));
- CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true);
+ CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true,
+ base::MakeUnique<base::DefaultClock>());
downloads_manager()->NotifyDownloadCreated(
downloads_manager()->items()[0].get());
// This notification should not reach the provider, because the asset
@@ -920,7 +946,8 @@ TEST_F(DownloadSuggestionsProviderTest,
OnNewSuggestions(_, downloads_category(),
UnorderedElementsAre(HasUrl("http://download.com/1"),
HasUrl("http://download.com/2"))));
- CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false);
+ CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false,
+ base::MakeUnique<base::DefaultClock>());
}
TEST_F(DownloadSuggestionsProviderTest,
@@ -934,7 +961,8 @@ TEST_F(DownloadSuggestionsProviderTest,
UnorderedElementsAre(
HasUrl("http://dummy.com/1"),
HasUrl("http://dummy.com/2"))));
- CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true);
+ CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true,
+ base::MakeUnique<base::DefaultClock>());
}
TEST_F(DownloadSuggestionsProviderTest, ShouldStoreDismissedSuggestions) {
@@ -945,7 +973,8 @@ TEST_F(DownloadSuggestionsProviderTest, ShouldStoreDismissedSuggestions) {
*(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1});
*(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1});
EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _));
- CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
+ CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true,
+ base::MakeUnique<base::DefaultClock>());
provider()->DismissSuggestion(
GetDummySuggestionId(1, /*is_offline_page=*/true));
provider()->DismissSuggestion(
@@ -954,7 +983,8 @@ TEST_F(DownloadSuggestionsProviderTest, ShouldStoreDismissedSuggestions) {
DestroyProvider();
EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _));
- CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
+ CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true,
+ base::MakeUnique<base::DefaultClock>());
EXPECT_THAT(GetDismissedSuggestions(),
UnorderedElementsAre(HasUrl("http://dummy.com/1"),
HasUrl("http://download.com/1")));
@@ -968,7 +998,8 @@ TEST_F(DownloadSuggestionsProviderTest,
// Dismiss items to store them in the list of dismissed items.
*(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1});
EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _));
- CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false);
+ CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false,
+ base::MakeUnique<base::DefaultClock>());
provider()->DismissSuggestion(
GetDummySuggestionId(1, /*is_offline_page=*/false));
ASSERT_THAT(GetDismissedSuggestions(),
@@ -979,7 +1010,8 @@ TEST_F(DownloadSuggestionsProviderTest,
downloads_manager()->mutable_items()->clear();
EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)).Times(0);
- CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false);
+ CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false,
+ base::MakeUnique<base::DefaultClock>());
// Dismissed IDs should not be pruned yet, because the downloads list at the
// manager is not complete.
@@ -996,3 +1028,43 @@ TEST_F(DownloadSuggestionsProviderTest,
// Once the manager has been loaded, the ids should be pruned.
EXPECT_THAT(GetDismissedSuggestions(), IsEmpty());
}
+
+TEST_F(DownloadSuggestionsProviderTest, ShouldNotShowOutdatedDownloads) {
+ IgnoreOnCategoryStatusChangedToAvailable();
+ IgnoreOnSuggestionInvalidated();
+
+ const int kDefaultMaxDownloadAgeHours = 6 * 7 * 24;
+
+ base::Time now;
+ ASSERT_TRUE(base::Time::FromString("Tue, 31 Jan 2017 13:00:00", &now));
+ const base::Time not_outdated =
+ now - base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) +
+ base::TimeDelta::FromSeconds(1);
+ const base::Time outdated =
+ now - base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) -
+ base::TimeDelta::FromSeconds(1);
+ *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({0, 1});
+ offline_pages_model()->mutable_items()->at(0).url =
+ GURL("http://dummy.com/0");
+ offline_pages_model()->mutable_items()->at(0).creation_time = not_outdated;
+ offline_pages_model()->mutable_items()->at(1).url =
+ GURL("http://dummy.com/1");
+ offline_pages_model()->mutable_items()->at(1).creation_time = outdated;
+ *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({0, 1});
+ downloads_manager()->mutable_items()->at(0)->SetURL(
+ GURL("http://download.com/0"));
+ downloads_manager()->mutable_items()->at(0)->SetStartTime(not_outdated);
+ downloads_manager()->mutable_items()->at(1)->SetURL(
+ GURL("http://download.com/1"));
+ downloads_manager()->mutable_items()->at(1)->SetStartTime(outdated);
+
+ EXPECT_CALL(
+ *observer(),
+ OnNewSuggestions(_, downloads_category(),
+ UnorderedElementsAre(HasUrl("http://dummy.com/0"),
+ HasUrl("http://download.com/0"))));
+ auto test_clock = base::MakeUnique<base::SimpleTestClock>();
+ test_clock->SetNow(now);
+ CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true,
+ std::move(test_clock));
+}
« no previous file with comments | « chrome/browser/ntp_snippets/download_suggestions_provider.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698