Index: components/ntp_snippets/category_rankers/click_based_category_ranker_unittest.cc |
diff --git a/components/ntp_snippets/category_rankers/click_based_category_ranker_unittest.cc b/components/ntp_snippets/category_rankers/click_based_category_ranker_unittest.cc |
index 1956feac0b86c70d93951cd44bf86ecc2ba3fdc5..7c1901316ec776ac2ce82d5c150d3442c19b5a02 100644 |
--- a/components/ntp_snippets/category_rankers/click_based_category_ranker_unittest.cc |
+++ b/components/ntp_snippets/category_rankers/click_based_category_ranker_unittest.cc |
@@ -5,12 +5,16 @@ |
#include "components/ntp_snippets/category_rankers/click_based_category_ranker.h" |
#include "base/memory/ptr_util.h" |
+#include "base/strings/string_number_conversions.h" |
#include "base/test/simple_test_clock.h" |
#include "base/time/default_clock.h" |
#include "base/time/time.h" |
#include "components/ntp_snippets/category.h" |
#include "components/ntp_snippets/category_rankers/constant_category_ranker.h" |
+#include "components/ntp_snippets/features.h" |
+#include "components/ntp_snippets/ntp_snippets_constants.h" |
#include "components/prefs/testing_pref_service.h" |
+#include "components/variations/variations_params_manager.h" |
#include "testing/gmock/include/gmock/gmock.h" |
#include "testing/gtest/include/gtest/gtest.h" |
@@ -65,12 +69,21 @@ class ClickBasedCategoryRankerTest : public testing::Test { |
ranker()->OnCategoryDismissed(category); |
} |
+ void SetDismissedCategoryPenaltyVariationParam(int value) { |
+ variation_params_manager_.SetVariationParamsWithFeatureAssociations( |
+ ntp_snippets::kStudyName, |
+ {{"click_based_category_ranker-dismissed_category_penalty", |
+ base::IntToString(value)}}, |
+ {kCategoryRanker.name}); |
+ } |
+ |
ClickBasedCategoryRanker* ranker() { return ranker_.get(); } |
private: |
std::unique_ptr<TestingPrefServiceSimple> pref_service_; |
int unused_remote_category_id_; |
std::unique_ptr<ClickBasedCategoryRanker> ranker_; |
+ variations::testing::VariationParamsManager variation_params_manager_; |
DISALLOW_COPY_AND_ASSIGN(ClickBasedCategoryRankerTest); |
}; |
@@ -324,6 +337,8 @@ TEST_F(ClickBasedCategoryRankerTest, ShouldPersistLastDecayTimeWhenRestarted) { |
} |
TEST_F(ClickBasedCategoryRankerTest, ShouldMoveCategoryDownWhenDismissed) { |
+ SetDismissedCategoryPenaltyVariationParam(2); |
+ |
// Take top categories. |
std::vector<KnownCategories> default_order = |
ConstantCategoryRanker::GetKnownCategoriesDefaultOrder(); |
@@ -337,6 +352,8 @@ TEST_F(ClickBasedCategoryRankerTest, ShouldMoveCategoryDownWhenDismissed) { |
TEST_F(ClickBasedCategoryRankerTest, |
ShouldMoveSecondToLastCategoryDownWhenDismissed) { |
+ SetDismissedCategoryPenaltyVariationParam(2); |
+ |
// Add categories to the bottom. |
Category first = AddUnusedRemoteCategory(); |
Category second = AddUnusedRemoteCategory(); |
@@ -348,6 +365,8 @@ TEST_F(ClickBasedCategoryRankerTest, |
TEST_F(ClickBasedCategoryRankerTest, |
ShouldNotMoveCategoryTooMuchDownWhenDismissed) { |
+ SetDismissedCategoryPenaltyVariationParam(2); |
+ |
// Add enough categories to the end. |
std::vector<Category> categories; |
const int penalty = ClickBasedCategoryRanker::GetDismissedCategoryPenalty(); |
@@ -377,6 +396,8 @@ TEST_F(ClickBasedCategoryRankerTest, |
TEST_F(ClickBasedCategoryRankerTest, |
ShouldNotChangeOrderOfOtherCategoriesWhenDismissed) { |
+ SetDismissedCategoryPenaltyVariationParam(2); |
+ |
// Add enough categories to the end. |
std::vector<Category> categories; |
const int penalty = ClickBasedCategoryRanker::GetDismissedCategoryPenalty(); |
@@ -399,6 +420,8 @@ TEST_F(ClickBasedCategoryRankerTest, |
} |
TEST_F(ClickBasedCategoryRankerTest, ShouldNotMoveLastCategoryWhenDismissed) { |
+ SetDismissedCategoryPenaltyVariationParam(2); |
+ |
Category first = AddUnusedRemoteCategory(); |
Category second = AddUnusedRemoteCategory(); |
@@ -409,6 +432,8 @@ TEST_F(ClickBasedCategoryRankerTest, ShouldNotMoveLastCategoryWhenDismissed) { |
TEST_F(ClickBasedCategoryRankerTest, |
ShouldReduceLastCategoryClicksWhenDismissed) { |
+ SetDismissedCategoryPenaltyVariationParam(2); |
+ |
Category first = AddUnusedRemoteCategory(); |
Category second = AddUnusedRemoteCategory(); |
@@ -432,6 +457,46 @@ TEST_F(ClickBasedCategoryRankerTest, |
EXPECT_FALSE(CompareCategories(first, second)); |
} |
+TEST_F(ClickBasedCategoryRankerTest, |
+ ShouldTakeVariationValueForDismissedCategoryPenalty) { |
+ const int penalty = 10203; |
+ SetDismissedCategoryPenaltyVariationParam(penalty); |
+ EXPECT_EQ(penalty, ClickBasedCategoryRanker::GetDismissedCategoryPenalty()); |
+} |
+ |
+TEST_F(ClickBasedCategoryRankerTest, |
+ ShouldDoNothingWhenCategoryDismissedIfPenaltyIsZero) { |
+ SetDismissedCategoryPenaltyVariationParam(0); |
+ |
+ // Add dummy remote categories to ensure that the following categories are not |
+ // in the top anymore. |
+ AddUnusedRemoteCategories( |
+ ClickBasedCategoryRanker::GetNumTopCategoriesWithExtraMargin()); |
+ |
+ Category first = AddUnusedRemoteCategory(); |
+ Category second = AddUnusedRemoteCategory(); |
+ Category third = AddUnusedRemoteCategory(); |
+ |
+ NotifyOnSuggestionOpened( |
+ /*times=*/1, second); |
+ |
+ // This should be ignored, because the penalty is set to 0. |
+ NotifyOnCategoryDismissed(second); |
+ |
+ // The second category should stay where it was. |
+ EXPECT_TRUE(CompareCategories(first, second)); |
+ EXPECT_TRUE(CompareCategories(second, third)); |
+ |
+ // Try to move the second category up assuming that the previous click is |
+ // still there. |
+ NotifyOnSuggestionOpened( |
+ /*times=*/ClickBasedCategoryRanker::GetPassingMargin() - 1, second); |
+ |
+ // It should overtake the first category, because the dismissal should be |
+ // ignored and the click should remain. |
+ EXPECT_FALSE(CompareCategories(first, second)); |
+} |
+ |
TEST_F(ClickBasedCategoryRankerTest, ShouldRestoreDefaultOrderOnClearHistory) { |
std::vector<KnownCategories> default_order = |
ConstantCategoryRanker::GetKnownCategoriesDefaultOrder(); |