Commit 13f871df authored by vitaliii's avatar vitaliii Committed by Commit bot

[NTP::Downloads] Limit number of dismissed IDs instead of pruning.

Recently we discovered, that our dismissed IDs pruning may happen at a
wrong time, when DownloadManager does not have a complete list.
Unfortunately, there is no way to check whether DownloadManager
finished starting up.
Therefore, this CL removes pruning of asset downloads and instead
limits maximal number of stored IDs to 100 (the oldest are removed
once the threshold is reached).

BUG=672758

Review-Url: https://codereview.chromium.org/2562073002
Cr-Commit-Position: refs/heads/master@{#437882}
parent 257f431c
......@@ -75,6 +75,8 @@ class DownloadSuggestionsProvider
static void RegisterProfilePrefs(PrefRegistrySimple* registry);
static int GetMaxDismissedCountForTesting();
private:
friend class DownloadSuggestionsProviderTest;
......@@ -171,11 +173,11 @@ class DownloadSuggestionsProvider
void InvalidateSuggestion(const std::string& id_within_category);
// Reads dismissed IDs related to asset downloads from prefs.
std::set<std::string> ReadAssetDismissedIDsFromPrefs() const;
std::vector<std::string> ReadAssetDismissedIDsFromPrefs() const;
// Writes |dismissed_ids| into prefs for asset downloads.
void StoreAssetDismissedIDsToPrefs(
const std::set<std::string>& dismissed_ids);
const std::vector<std::string>& dismissed_ids);
// Reads dismissed IDs related to offline page downloads from prefs.
std::set<std::string> ReadOfflinePageDismissedIDsFromPrefs() const;
......@@ -184,15 +186,15 @@ class DownloadSuggestionsProvider
void StoreOfflinePageDismissedIDsToPrefs(
const std::set<std::string>& dismissed_ids);
// Reads from prefs dismissed IDs related to either offline page or asset
// downloads (given by |for_offline_page_downloads|).
std::set<std::string> ReadDismissedIDsFromPrefs(
bool for_offline_page_downloads) const;
// Adds a suggestion ID to the dismissed list in prefs, if it is not there.
// Works for both Offline Page and Asset downloads.
void AddToDismissedStorageIfNeeded(
const ntp_snippets::ContentSuggestion::ID& suggestion_id);
// Writes |dismissed_ids| into prefs for either offline page or asset
// downloads (given by |for_offline_page_downloads|).
void StoreDismissedIDsToPrefs(bool for_offline_page_downloads,
const std::set<std::string>& dismissed_ids);
// Removes a suggestion ID from the dismissed list in prefs, if it is there.
// Works for both Offline Page and Asset downloads.
void RemoveFromDismissedStorageIfNeeded(
const ntp_snippets::ContentSuggestion::ID& suggestion_id);
void UnregisterDownloadItemObservers();
......
......@@ -38,12 +38,12 @@ using testing::AllOf;
using testing::AnyNumber;
using testing::ElementsAre;
using testing::IsEmpty;
using testing::Lt;
using testing::Mock;
using testing::Return;
using testing::SizeIs;
using testing::StrictMock;
using testing::UnorderedElementsAre;
using testing::Lt;
namespace ntp_snippets {
// These functions are implicitly used to print out values during the tests.
......@@ -926,3 +926,87 @@ TEST_F(DownloadSuggestionsProviderTest,
HasUrl("http://download.com/2"))));
CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false);
}
TEST_F(DownloadSuggestionsProviderTest,
ShouldNotPruneDismissedSuggestionsOnStartup) {
IgnoreOnCategoryStatusChangedToAvailable();
IgnoreOnSuggestionInvalidated();
// We dismiss an item to store it in the list of dismissed items.
*(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1});
EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _));
CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false);
provider()->DismissSuggestion(
GetDummySuggestionId(1, /*is_offline_page=*/false));
DestroyProvider();
// We simulate current DownloadManager behaviour;
// The download manager has not started reading the list yet, so it is empty.
downloads_manager()->mutable_items()->clear();
EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _));
CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false);
Mock::VerifyAndClearExpectations(observer());
// The first download is being read.
*(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1});
EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _))
.Times(0);
FireDownloadCreated(downloads_manager()->items()[0].get());
// The first download should not be reported, because it is dismissed.
}
TEST_F(DownloadSuggestionsProviderTest, ShouldStoreDismissedSuggestions) {
IgnoreOnCategoryStatusChangedToAvailable();
IgnoreOnSuggestionInvalidated();
// Dismiss items to store them in the list of dismissed items.
*(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1});
*(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1});
EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _));
CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
provider()->DismissSuggestion(
GetDummySuggestionId(1, /*is_offline_page=*/true));
provider()->DismissSuggestion(
GetDummySuggestionId(1, /*is_offline_page=*/false));
// Destroy and create provider to simulate turning off Chrome.
DestroyProvider();
EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _));
CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
EXPECT_THAT(GetDismissedSuggestions(),
UnorderedElementsAre(HasUrl("http://dummy.com/1"),
HasUrl("http://download.com/1")));
}
// TODO(vitaliii): Remove this test once the dismissed ids are pruned. See
// crbug.com/672758.
TEST_F(DownloadSuggestionsProviderTest, ShouldRemoveOldDismissedIdsIfTooMany) {
IgnoreOnCategoryStatusChangedToAvailable();
IgnoreOnSuggestionInvalidated();
const int kMaxDismissedIdCount =
DownloadSuggestionsProvider::GetMaxDismissedCountForTesting();
std::vector<int> ids;
for (int i = 0; i < kMaxDismissedIdCount + 1; ++i) {
ids.push_back(i);
}
*(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads(ids);
EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _));
CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false);
for (int i = 0; i < static_cast<int>(ids.size()); ++i) {
provider()->DismissSuggestion(
GetDummySuggestionId(i, /*is_offline_page=*/false));
}
EXPECT_THAT(GetDismissedSuggestions(), SizeIs(kMaxDismissedIdCount));
DestroyProvider();
// The oldest dismissed suggestion must become undismissed now. This is a
// temporary workaround and not what we want in long term. This test must be
// removed once we start pruning dismissed asset downloads on startup.
EXPECT_CALL(*observer(),
OnNewSuggestions(_, downloads_category(),
ElementsAre(HasUrl("http://download.com/0"))));
CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment