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

Unified Diff: components/ntp_snippets/content_suggestions_service_unittest.cc

Issue 2406573002: 📰 Persist category dismissals (Closed)
Patch Set: fix test Created 4 years, 2 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
Index: components/ntp_snippets/content_suggestions_service_unittest.cc
diff --git a/components/ntp_snippets/content_suggestions_service_unittest.cc b/components/ntp_snippets/content_suggestions_service_unittest.cc
index a2245bfd6cbe91ea5bae71ae8abed3cff4320fd8..7000f02d75ed1a0e7540fed070a7d360b7490913 100644
--- a/components/ntp_snippets/content_suggestions_service_unittest.cc
+++ b/components/ntp_snippets/content_suggestions_service_unittest.cc
@@ -19,6 +19,7 @@
#include "components/ntp_snippets/category_status.h"
#include "components/ntp_snippets/content_suggestion.h"
#include "components/ntp_snippets/content_suggestions_provider.h"
+#include "components/prefs/testing_pref_service.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/image/image.h"
@@ -67,6 +68,11 @@ class MockProvider : public ContentSuggestionsProvider {
observer()->OnNewSuggestions(this, category, std::move(suggestions));
}
+ void FireSuggestionBatchChanged(
+ ContentSuggestionsService::SuggestionBatch suggestion_batch) {
+ observer()->OnNewSuggestionBatch(this, std::move(suggestion_batch));
+ }
+
void FireCategoryStatusChanged(Category category, CategoryStatus new_status) {
statuses_[category.id()] = new_status;
observer()->OnCategoryStatusChanged(this, category, new_status);
@@ -121,9 +127,11 @@ class MockServiceObserver : public ContentSuggestionsService::Observer {
class ContentSuggestionsServiceTest : public testing::Test {
public:
- ContentSuggestionsServiceTest() {}
+ ContentSuggestionsServiceTest()
+ : pref_service_(new TestingPrefServiceSimple()) {}
void SetUp() override {
+ RegisterPrefs();
CreateContentSuggestionsService(ContentSuggestionsService::State::ENABLED);
}
@@ -190,12 +198,16 @@ class ContentSuggestionsServiceTest : public testing::Test {
MOCK_METHOD1(OnImageFetched, void(const gfx::Image&));
protected:
+ void RegisterPrefs() {
+ ContentSuggestionsService::RegisterProfilePrefs(pref_service_->registry());
+ UserClassifier::RegisterProfilePrefs(pref_service_->registry());
+ }
+
void CreateContentSuggestionsService(
ContentSuggestionsService::State enabled) {
ASSERT_FALSE(service_);
- service_.reset(new ContentSuggestionsService(enabled,
- nullptr /* history_service */,
- nullptr /* pref_service */));
+ service_.reset(new ContentSuggestionsService(
+ enabled, nullptr /* history_service */, pref_service_.get()));
}
ContentSuggestionsService* service() { return service_.get(); }
@@ -219,6 +231,7 @@ class ContentSuggestionsServiceTest : public testing::Test {
private:
std::unique_ptr<ContentSuggestionsService> service_;
+ std::unique_ptr<TestingPrefServiceSimple> pref_service_;
DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsServiceTest);
};
@@ -227,6 +240,7 @@ class ContentSuggestionsServiceDisabledTest
: public ContentSuggestionsServiceTest {
public:
void SetUp() override {
+ RegisterPrefs();
CreateContentSuggestionsService(ContentSuggestionsService::State::DISABLED);
}
};
@@ -601,4 +615,134 @@ TEST_F(ContentSuggestionsServiceTest, ShouldForwardClearHistory) {
service()->ClearHistory(begin, end, filter);
}
+TEST_F(ContentSuggestionsServiceTest, ShouldClearCategoryDismissal) {
+ // Create and register provider
+ Category category1 = service()->category_factory()->FromIDValue(1);
+ Category category10042 = service()->category_factory()->FromIDValue(10042);
+ Category category10043 = service()->category_factory()->FromIDValue(10043);
+
+ // Setup and verify initial state
+ MockProvider* provider =
+ RegisterProvider({category1, category10042, category10043});
+ provider->FireCategoryStatusChangedWithCurrentStatus(category1);
+ provider->FireCategoryStatusChangedWithCurrentStatus(category10042);
+ provider->FireCategoryStatusChangedWithCurrentStatus(category10043);
+
+ ASSERT_THAT(service()->GetCategoryStatus(category1),
+ Eq(CategoryStatus::AVAILABLE));
+ ASSERT_THAT(service()->GetCategoryStatus(category10042),
+ Eq(CategoryStatus::AVAILABLE));
+ ASSERT_THAT(service()->GetCategoryStatus(category10043),
+ Eq(CategoryStatus::AVAILABLE));
+
+ // Dismiss all the categories. None should be provided now.
+ service()->DismissCategory(category1);
+ service()->DismissCategory(category10042);
+ service()->DismissCategory(category10043);
+
+ ASSERT_THAT(service()->GetCategoryStatus(category1),
+ Eq(CategoryStatus::NOT_PROVIDED));
+ ASSERT_THAT(service()->GetCategoryStatus(category10042),
+ Eq(CategoryStatus::NOT_PROVIDED));
+ ASSERT_THAT(service()->GetCategoryStatus(category10043),
+ Eq(CategoryStatus::NOT_PROVIDED));
+
+ // Receiving a notification without suggestions should not change anything.
+ provider->FireSuggestionsChanged(category1, std::vector<ContentSuggestion>());
+
+ ASSERT_THAT(service()->GetCategoryStatus(category1),
+ Eq(CategoryStatus::NOT_PROVIDED));
+ ASSERT_THAT(service()->GetCategoryStatus(category10042),
+ Eq(CategoryStatus::NOT_PROVIDED));
+ ASSERT_THAT(service()->GetCategoryStatus(category10043),
+ Eq(CategoryStatus::NOT_PROVIDED));
+
+ // A notification with suggestions should make the category available.
+ provider->FireSuggestionsChanged(category1,
+ CreateSuggestions(category1, {1, 2}));
+
+ ASSERT_THAT(service()->GetCategoryStatus(category1),
+ Eq(CategoryStatus::AVAILABLE));
+ ASSERT_THAT(service()->GetCategoryStatus(category10042),
+ Eq(CategoryStatus::NOT_PROVIDED));
+ ASSERT_THAT(service()->GetCategoryStatus(category10043),
+ Eq(CategoryStatus::NOT_PROVIDED));
+
+ // A batch notification with suggestions should make the category available.
+ ContentSuggestionsService::SuggestionBatch suggestion_batch;
+ suggestion_batch[category10042] = CreateSuggestions(category10042, {3, 4});
+ suggestion_batch[category10043] = std::vector<ContentSuggestion>();
+ provider->FireSuggestionBatchChanged(std::move(suggestion_batch));
+
+ ASSERT_THAT(service()->GetCategoryStatus(category1),
+ Eq(CategoryStatus::AVAILABLE));
+ ASSERT_THAT(service()->GetCategoryStatus(category10042),
+ Eq(CategoryStatus::AVAILABLE));
+ ASSERT_THAT(service()->GetCategoryStatus(category10043),
+ Eq(CategoryStatus::NOT_PROVIDED));
+
+ // A batch notification with a missing category should clear its dismissal.
+ suggestion_batch = ContentSuggestionsService::SuggestionBatch();
+ suggestion_batch[category10042] = CreateSuggestions(category10042, {5, 6});
+ provider->FireSuggestionBatchChanged(std::move(suggestion_batch));
+
+ ASSERT_THAT(service()->GetCategoryStatus(category1),
+ Eq(CategoryStatus::AVAILABLE));
+ ASSERT_THAT(service()->GetCategoryStatus(category10042),
+ Eq(CategoryStatus::AVAILABLE));
+ ASSERT_THAT(service()->GetCategoryStatus(category10043),
+ Eq(CategoryStatus::AVAILABLE));
+}
+
+TEST_F(ContentSuggestionsServiceTest, ShouldNotReturnDismissedCategories) {
+ Category category = FromKnownCategory(KnownCategories::ARTICLES);
+
+ // Create and register provider
+ MockProvider* provider = RegisterProvider(category);
+ provider->FireCategoryStatusChangedWithCurrentStatus(category);
+ ASSERT_THAT(providers().count(category), Eq(1ul));
+ EXPECT_THAT(providers().at(category), Eq(provider));
+ ASSERT_THAT(service()->GetCategories(), ElementsAre(category));
+
+ // Create and register observer
+ MockServiceObserver observer;
+ service()->AddObserver(&observer);
+
+ EXPECT_THAT(service()->GetCategoryStatus(category),
+ Eq(CategoryStatus::AVAILABLE));
+ EXPECT_TRUE(service()->GetCategoryInfo(category));
+
+ service()->DismissCategory(category);
+ EXPECT_THAT(service()->GetCategories(), ElementsAre(category));
+ EXPECT_THAT(service()->GetCategoryStatus(category),
+ Eq(CategoryStatus::NOT_PROVIDED));
+ EXPECT_FALSE(service()->GetCategoryInfo(category));
+ EXPECT_TRUE(service()->GetSuggestionsForCategory(category).empty());
+
+ // OnCategoryStatusChanged
+ EXPECT_CALL(observer, OnCategoryStatusChanged(category, _)).Times(0);
+ provider->FireCategoryStatusChanged(category, CategoryStatus::SIGNED_OUT);
+ provider->FireCategoryStatusChanged(category, CategoryStatus::AVAILABLE);
+ Mock::VerifyAndClearExpectations(&observer);
+
+ // OnNewSuggestions (with suggestions)
+ EXPECT_CALL(observer, OnNewSuggestions(category));
+ provider->FireSuggestionsChanged(category,
+ CreateSuggestions(category, {1, 2}));
+ ExpectThatSuggestionsAre(category, {1, 2});
+ Mock::VerifyAndClearExpectations(&observer);
+
+ EXPECT_THAT(service()->GetCategoryStatus(category),
+ Eq(CategoryStatus::AVAILABLE));
+ EXPECT_TRUE(service()->GetCategoryInfo(category));
+ EXPECT_FALSE(service()->GetSuggestionsForCategory(category).empty());
+
+ EXPECT_CALL(observer,
+ OnCategoryStatusChanged(category, CategoryStatus::SIGNED_OUT));
+ provider->FireCategoryStatusChanged(category, CategoryStatus::SIGNED_OUT);
+ Mock::VerifyAndClearExpectations(&observer);
+
+ service()->RemoveObserver(&observer);
+}
+
} // namespace ntp_snippets

Powered by Google App Engine
This is Rietveld 408576698