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

Unified Diff: components/ntp_snippets/physical_web_pages/physical_web_page_suggestions_provider_unittest.cc

Issue 2560783002: [NTP::PhysicalWeb] Implement suggestion dismissal. (Closed)
Patch Set: rebase and bauerb@ nit. 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 side-by-side diff with in-line comments
Download patch
Index: components/ntp_snippets/physical_web_pages/physical_web_page_suggestions_provider_unittest.cc
diff --git a/components/ntp_snippets/physical_web_pages/physical_web_page_suggestions_provider_unittest.cc b/components/ntp_snippets/physical_web_pages/physical_web_page_suggestions_provider_unittest.cc
index 70d4cc97d19062ed4635db8450e245720d0d9ad7..3f720f40777457521ce9a7ffba2c25f2e667e6a1 100644
--- a/components/ntp_snippets/physical_web_pages/physical_web_page_suggestions_provider_unittest.cc
+++ b/components/ntp_snippets/physical_web_pages/physical_web_page_suggestions_provider_unittest.cc
@@ -17,6 +17,7 @@
#include "components/ntp_snippets/category_factory.h"
#include "components/ntp_snippets/content_suggestions_provider.h"
#include "components/ntp_snippets/mock_content_suggestions_provider_observer.h"
+#include "components/ntp_snippets/offline_pages/offline_pages_test_utils.h"
#include "components/ntp_snippets/status.h"
#include "components/physical_web/data_source/fake_physical_web_data_source.h"
#include "components/prefs/testing_pref_service.h"
@@ -25,12 +26,15 @@
using base::DictionaryValue;
using base::ListValue;
+using ntp_snippets::test::CaptureDismissedSuggestions;
using physical_web::CreateDummyPhysicalWebPages;
using physical_web::FakePhysicalWebDataSource;
using testing::_;
using testing::AnyNumber;
+using testing::AtMost;
using testing::ElementsAre;
using testing::ElementsAreArray;
+using testing::Mock;
using testing::StrictMock;
using testing::UnorderedElementsAre;
@@ -65,7 +69,10 @@ void CompareFetchMoreResult(
class PhysicalWebPageSuggestionsProviderTest : public testing::Test {
public:
PhysicalWebPageSuggestionsProviderTest()
- : pref_service_(base::MakeUnique<TestingPrefServiceSimple>()) {}
+ : pref_service_(base::MakeUnique<TestingPrefServiceSimple>()) {
+ PhysicalWebPageSuggestionsProvider::RegisterProfilePrefs(
+ pref_service_->registry());
+ }
void IgnoreOnCategoryStatusChangedToAvailable() {
EXPECT_CALL(observer_, OnCategoryStatusChanged(_, provided_category(),
@@ -81,10 +88,16 @@ class PhysicalWebPageSuggestionsProviderTest : public testing::Test {
EXPECT_CALL(observer_, OnSuggestionInvalidated(_, _)).Times(AnyNumber());
}
+ void IgnoreOnNewSuggestions() {
+ EXPECT_CALL(observer_, OnNewSuggestions(_, provided_category(), _))
+ .Times(AnyNumber());
+ }
+
PhysicalWebPageSuggestionsProvider* CreateProvider() {
DCHECK(!provider_);
provider_ = base::MakeUnique<PhysicalWebPageSuggestionsProvider>(
- &observer_, &category_factory_, &physical_web_data_source_);
+ &observer_, &category_factory_, &physical_web_data_source_,
+ pref_service_.get());
return provider_.get();
}
@@ -95,6 +108,12 @@ class PhysicalWebPageSuggestionsProviderTest : public testing::Test {
KnownCategories::PHYSICAL_WEB_PAGES);
}
+ ContentSuggestion::ID GetDummySuggestionId(int id) {
+ return ContentSuggestion::ID(
+ provided_category(),
+ "https://resolved_url.com/" + base::IntToString(id));
+ }
+
void FireUrlFound(const std::string& url) {
physical_web_data_source_.NotifyOnFound(url);
}
@@ -206,4 +225,123 @@ TEST_F(PhysicalWebPageSuggestionsProviderTest,
run_loop.Run();
}
+TEST_F(PhysicalWebPageSuggestionsProviderTest, ShouldDismiss) {
+ IgnoreOnCategoryStatusChangedToAvailable();
+ IgnoreOnSuggestionInvalidated();
+ physical_web_data_source()->SetMetadata(CreateDummyPhysicalWebPages({1}));
+ EXPECT_CALL(*observer(), OnNewSuggestions(_, provided_category(), _))
+ .Times(AtMost(1));
+ CreateProvider();
+
+ provider()->DismissSuggestion(GetDummySuggestionId(1));
+
+ std::vector<ContentSuggestion> dismissed_suggestions;
+ provider()->GetDismissedSuggestionsForDebugging(
+ provided_category(),
+ base::Bind(&CaptureDismissedSuggestions, &dismissed_suggestions));
+ EXPECT_THAT(dismissed_suggestions,
+ ElementsAre(HasUrl("https://resolved_url.com/1")));
+}
+
+TEST_F(PhysicalWebPageSuggestionsProviderTest,
+ ShouldInvalidateSuggestionOnUrlLost) {
+ IgnoreOnCategoryStatusChangedToAvailable();
+ IgnoreOnNewSuggestions();
+ physical_web_data_source()->SetMetadata(CreateDummyPhysicalWebPages({1}));
+ CreateProvider();
+
+ EXPECT_CALL(*observer(), OnSuggestionInvalidated(_, GetDummySuggestionId(1)));
+ FireUrlLost("https://resolved_url.com/1");
+}
+
+TEST_F(PhysicalWebPageSuggestionsProviderTest,
+ ShouldNotShowDismissedSuggestions) {
+ IgnoreOnCategoryStatusChangedToAvailable();
+ IgnoreOnSuggestionInvalidated();
+ physical_web_data_source()->SetMetadata(CreateDummyPhysicalWebPages({1}));
+ EXPECT_CALL(*observer(), OnNewSuggestions(_, provided_category(), _))
+ .Times(AtMost(1));
+ CreateProvider();
+ Mock::VerifyAndClearExpectations(observer());
+
+ provider()->DismissSuggestion(GetDummySuggestionId(1));
+
+ physical_web_data_source()->SetMetadata(CreateDummyPhysicalWebPages({1, 2}));
+ EXPECT_CALL(
+ *observer(),
+ OnNewSuggestions(_, provided_category(),
+ ElementsAre(HasUrl("https://resolved_url.com/2"))));
+ FireUrlFound("https://resolved_url.com/2");
+}
+
+TEST_F(PhysicalWebPageSuggestionsProviderTest,
+ ShouldPruneDismissedSuggestionsWhenFetching) {
+ IgnoreOnCategoryStatusChangedToAvailable();
+ IgnoreOnSuggestionInvalidated();
+ IgnoreOnNewSuggestions();
+ physical_web_data_source()->SetMetadata(CreateDummyPhysicalWebPages({1, 2}));
+ CreateProvider();
+
+ provider()->DismissSuggestion(GetDummySuggestionId(1));
+ provider()->DismissSuggestion(GetDummySuggestionId(2));
+
+ physical_web_data_source()->SetMetadata(CreateDummyPhysicalWebPages({2, 3}));
+ FireUrlFound("https://resolved_url.com/3");
+
+ // The first page needs to be silently added back to the source, because
+ // |GetDismissedSuggestionsForDebugging| uses the data source to return
+ // suggestions and dismissed suggestions, which are not present there, cannot
+ // be returned.
+ physical_web_data_source()->SetMetadata(
+ CreateDummyPhysicalWebPages({1, 2, 3}));
+ std::vector<ContentSuggestion> dismissed_suggestions;
+ provider()->GetDismissedSuggestionsForDebugging(
+ provided_category(),
+ base::Bind(&CaptureDismissedSuggestions, &dismissed_suggestions));
+ EXPECT_THAT(dismissed_suggestions,
+ ElementsAre(HasUrl("https://resolved_url.com/2")));
+}
+
+TEST_F(PhysicalWebPageSuggestionsProviderTest,
+ ShouldPruneDismissedSuggestionsWhenUrlLost) {
+ IgnoreOnCategoryStatusChangedToAvailable();
+ IgnoreOnSuggestionInvalidated();
+ IgnoreOnNewSuggestions();
+ physical_web_data_source()->SetMetadata(CreateDummyPhysicalWebPages({1, 2}));
+ CreateProvider();
+
+ provider()->DismissSuggestion(GetDummySuggestionId(1));
+ provider()->DismissSuggestion(GetDummySuggestionId(2));
+
+ physical_web_data_source()->SetMetadata(CreateDummyPhysicalWebPages({2}));
+ FireUrlLost("https://resolved_url.com/1");
+
+ physical_web_data_source()->SetMetadata(CreateDummyPhysicalWebPages({1, 2}));
+ std::vector<ContentSuggestion> dismissed_suggestions;
+ provider()->GetDismissedSuggestionsForDebugging(
+ provided_category(),
+ base::Bind(&CaptureDismissedSuggestions, &dismissed_suggestions));
+ EXPECT_THAT(dismissed_suggestions,
+ ElementsAre(HasUrl("https://resolved_url.com/2")));
+}
+
+TEST_F(PhysicalWebPageSuggestionsProviderTest,
+ ShouldStoreDismissedSuggestions) {
+ IgnoreOnCategoryStatusChangedToAvailable();
+ IgnoreOnSuggestionInvalidated();
+ IgnoreOnNewSuggestions();
+ physical_web_data_source()->SetMetadata(CreateDummyPhysicalWebPages({1, 2}));
+ CreateProvider();
+ provider()->DismissSuggestion(GetDummySuggestionId(1));
+ DestroyProvider();
+
+ CreateProvider();
+ std::vector<ContentSuggestion> dismissed_suggestions;
+ provider()->GetDismissedSuggestionsForDebugging(
+ provided_category(),
+ base::Bind(&CaptureDismissedSuggestions, &dismissed_suggestions));
+ EXPECT_THAT(dismissed_suggestions,
+ ElementsAre(HasUrl("https://resolved_url.com/1")));
+}
+
} // namespace ntp_snippets

Powered by Google App Engine
This is Rietveld 408576698