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

Side by Side Diff: chrome/browser/ntp_snippets/download_suggestions_provider_unittest.cc

Issue 2562073002: [NTP::Downloads] Limit number of dismissed IDs instead of pruning. (Closed)
Patch Set: rebase + test. Created 4 years 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
OLDNEW
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 "chrome/browser/ntp_snippets/download_suggestions_provider.h" 5 #include "chrome/browser/ntp_snippets/download_suggestions_provider.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/observer_list.h" 10 #include "base/observer_list.h"
(...skipping 20 matching lines...) Expand all
31 using ntp_snippets::test::FakeOfflinePageModel; 31 using ntp_snippets::test::FakeOfflinePageModel;
32 using ntp_snippets::CategoryStatus; 32 using ntp_snippets::CategoryStatus;
33 using offline_pages::ClientId; 33 using offline_pages::ClientId;
34 using offline_pages::OfflinePageItem; 34 using offline_pages::OfflinePageItem;
35 using test::FakeDownloadItem; 35 using test::FakeDownloadItem;
36 using testing::_; 36 using testing::_;
37 using testing::AllOf; 37 using testing::AllOf;
38 using testing::AnyNumber; 38 using testing::AnyNumber;
39 using testing::ElementsAre; 39 using testing::ElementsAre;
40 using testing::IsEmpty; 40 using testing::IsEmpty;
41 using testing::Lt;
41 using testing::Mock; 42 using testing::Mock;
42 using testing::Return; 43 using testing::Return;
43 using testing::SizeIs; 44 using testing::SizeIs;
44 using testing::StrictMock; 45 using testing::StrictMock;
45 using testing::UnorderedElementsAre; 46 using testing::UnorderedElementsAre;
46 using testing::Lt;
47 47
48 namespace ntp_snippets { 48 namespace ntp_snippets {
49 // These functions are implicitly used to print out values during the tests. 49 // These functions are implicitly used to print out values during the tests.
50 std::ostream& operator<<(std::ostream& os, const ContentSuggestion& value) { 50 std::ostream& operator<<(std::ostream& os, const ContentSuggestion& value) {
51 os << "{ url: " << value.url() << ", publish_date: " << value.publish_date() 51 os << "{ url: " << value.url() << ", publish_date: " << value.publish_date()
52 << "}"; 52 << "}";
53 return os; 53 return os;
54 } 54 }
55 55
56 std::ostream& operator<<(std::ostream& os, const CategoryStatus& value) { 56 std::ostream& operator<<(std::ostream& os, const CategoryStatus& value) {
(...skipping 862 matching lines...) Expand 10 before | Expand all | Expand 10 after
919 IgnoreOnSuggestionInvalidated(); 919 IgnoreOnSuggestionInvalidated();
920 920
921 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); 921 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2});
922 EXPECT_CALL( 922 EXPECT_CALL(
923 *observer(), 923 *observer(),
924 OnNewSuggestions(_, downloads_category(), 924 OnNewSuggestions(_, downloads_category(),
925 UnorderedElementsAre(HasUrl("http://download.com/1"), 925 UnorderedElementsAre(HasUrl("http://download.com/1"),
926 HasUrl("http://download.com/2")))); 926 HasUrl("http://download.com/2"))));
927 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false); 927 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false);
928 } 928 }
929
930 TEST_F(DownloadSuggestionsProviderTest,
931 ShouldNotPruneDismissedSuggestionsOnStartup) {
932 IgnoreOnCategoryStatusChangedToAvailable();
933 IgnoreOnSuggestionInvalidated();
934
935 // We dismiss an item to store it in the list of dismissed items.
936 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1});
937 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _));
938 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false);
939 provider()->DismissSuggestion(
940 GetDummySuggestionId(1, /*is_offline_page=*/false));
941 DestroyProvider();
942
943 // We simulate current DownloadManager behaviour;
944 // The download manager has not started reading the list yet, so it is empty.
945 downloads_manager()->mutable_items()->clear();
946 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _));
947 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false);
948 Mock::VerifyAndClearExpectations(observer());
949
950 // The first download is being read.
951 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1});
952 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _))
953 .Times(0);
954 FireDownloadCreated(downloads_manager()->items()[0].get());
955 // The first download should not be reported, because it is dismissed.
956 }
957
958 TEST_F(DownloadSuggestionsProviderTest, ShouldStoreDismissedSuggestions) {
959 IgnoreOnCategoryStatusChangedToAvailable();
960 IgnoreOnSuggestionInvalidated();
961
962 // Dismiss items to store them in the list of dismissed items.
963 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1});
964 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1});
965 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _));
966 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
967 provider()->DismissSuggestion(
968 GetDummySuggestionId(1, /*is_offline_page=*/true));
969 provider()->DismissSuggestion(
970 GetDummySuggestionId(1, /*is_offline_page=*/false));
971 // Destroy and create provider to simulate turning off Chrome.
972 DestroyProvider();
973
974 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _));
975 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
976 EXPECT_THAT(GetDismissedSuggestions(),
977 UnorderedElementsAre(HasUrl("http://dummy.com/1"),
978 HasUrl("http://download.com/1")));
979 }
980
981 // TODO(vitaliii): Remove this test once the dismissed ids are pruned. See
982 // crbug.com/672758.
983 TEST_F(DownloadSuggestionsProviderTest, ShouldRemoveOldDismissedIdsIfTooMany) {
984 IgnoreOnCategoryStatusChangedToAvailable();
985 IgnoreOnSuggestionInvalidated();
986
987 std::vector<int> ids;
988 for (int i = 0;
989 i < DownloadSuggestionsProvider::GetMaxDismissedCountForTesting() + 1;
Marc Treib 2016/12/12 09:29:15 optional: Could store this in a variable first, to
vitaliii 2016/12/12 15:41:58 Done.
990 ++i) {
991 ids.push_back(i);
992 }
993
994 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads(ids);
995 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _));
996 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false);
997
998 for (int i = 0; i < static_cast<int>(ids.size()); ++i) {
999 provider()->DismissSuggestion(
1000 GetDummySuggestionId(i, /*is_offline_page=*/false));
1001 }
1002
1003 EXPECT_THAT(
1004 GetDismissedSuggestions(),
1005 SizeIs(DownloadSuggestionsProvider::GetMaxDismissedCountForTesting()));
1006 DestroyProvider();
1007 // The oldest dismissed suggestion must become undismissed now.
Marc Treib 2016/12/12 09:29:15 Maybe mention that this is kind of an "anti-test"?
vitaliii 2016/12/12 15:41:58 Done.
1008 EXPECT_CALL(*observer(),
1009 OnNewSuggestions(_, downloads_category(),
1010 ElementsAre(HasUrl("http://download.com/0"))));
1011 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false);
1012 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698