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

Unified Diff: components/ntp_snippets/category_rankers/constant_category_ranker_unittest.cc

Issue 2568033005: [NTP::SectionOrder] Replace CategoryFactory with a category ranker. (Closed)
Patch Set: rebase. 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/category_rankers/constant_category_ranker_unittest.cc
diff --git a/components/ntp_snippets/category_rankers/constant_category_ranker_unittest.cc b/components/ntp_snippets/category_rankers/constant_category_ranker_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e0286d49715dc88b98dc417b50ba20123b9b195d
--- /dev/null
+++ b/components/ntp_snippets/category_rankers/constant_category_ranker_unittest.cc
@@ -0,0 +1,124 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/ntp_snippets/category_rankers/constant_category_ranker.h"
+
+#include "components/ntp_snippets/category.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace ntp_snippets {
+
+class ConstantCategoryRankerTest : public testing::Test {
+ public:
+ ConstantCategoryRankerTest()
+ : unused_remote_category_id_(
+ static_cast<int>(KnownCategories::LAST_KNOWN_REMOTE_CATEGORY) + 1) {
+ }
+
+ int GetUnusedRemoteCategoryID() { return unused_remote_category_id_++; }
+
+ Category GetUnusedRemoteCategory() {
+ return Category::FromIDValue(GetUnusedRemoteCategoryID());
+ }
+
+ bool CompareCategories(const Category& left, const Category& right) {
+ return ranker()->Compare(left, right);
+ }
+
+ Category AddUnusedRemoteCategory() {
+ Category category = GetUnusedRemoteCategory();
+ ranker()->AppendCategoryIfNecessary(category);
+ return category;
+ }
+
+ void AddUnusedRemoteCategories(int quantity) {
+ for (int i = 0; i < quantity; ++i) {
+ AddUnusedRemoteCategory();
+ }
+ }
+
+ ConstantCategoryRanker* ranker() { return &ranker_; }
+
+ private:
+ ConstantCategoryRanker ranker_;
+ int unused_remote_category_id_;
+
+ DISALLOW_COPY_AND_ASSIGN(ConstantCategoryRankerTest);
+};
+
+TEST_F(ConstantCategoryRankerTest, ShouldSortRemoteCategoriesByWhenAdded) {
+ const Category first = GetUnusedRemoteCategory();
+ const Category second = GetUnusedRemoteCategory();
+ // Categories are added in decreasing id order to test that they are not
+ // compared by id.
+ ranker()->AppendCategoryIfNecessary(second);
+ ranker()->AppendCategoryIfNecessary(first);
+ EXPECT_TRUE(CompareCategories(second, first));
+ EXPECT_FALSE(CompareCategories(first, second));
+}
+
+TEST_F(ConstantCategoryRankerTest, ShouldSortLocalCategoriesBeforeRemote) {
+ const Category remote_category = AddUnusedRemoteCategory();
+ const Category local_category =
+ Category::FromKnownCategory(KnownCategories::BOOKMARKS);
+ EXPECT_TRUE(CompareCategories(local_category, remote_category));
+ EXPECT_FALSE(CompareCategories(remote_category, local_category));
+}
+
+TEST_F(ConstantCategoryRankerTest, CompareShouldReturnFalseForSameCategories) {
+ const Category remote_category = AddUnusedRemoteCategory();
+ EXPECT_FALSE(CompareCategories(remote_category, remote_category));
+
+ const Category local_category =
+ Category::FromKnownCategory(KnownCategories::BOOKMARKS);
+ EXPECT_FALSE(CompareCategories(local_category, local_category));
+}
+
+TEST_F(ConstantCategoryRankerTest,
+ AddingMoreRemoteCategoriesShouldNotChangePreviousOrder) {
+ AddUnusedRemoteCategories(3);
+
+ Category first = AddUnusedRemoteCategory();
+ Category second = AddUnusedRemoteCategory();
+
+ ASSERT_TRUE(CompareCategories(first, second));
+ ASSERT_FALSE(CompareCategories(second, first));
+
+ AddUnusedRemoteCategories(3);
+
+ EXPECT_TRUE(CompareCategories(first, second));
+ EXPECT_FALSE(CompareCategories(second, first));
+}
+
+TEST_F(ConstantCategoryRankerTest,
+ AddingSameCategoryTwiceShouldNotChangeOrder) {
+ Category first = AddUnusedRemoteCategory();
+ Category second = AddUnusedRemoteCategory();
+
+ ASSERT_TRUE(CompareCategories(first, second));
+ ASSERT_FALSE(CompareCategories(second, first));
+
+ ranker()->AppendCategoryIfNecessary(first);
+
+ EXPECT_TRUE(CompareCategories(first, second));
+ EXPECT_FALSE(CompareCategories(second, first));
+}
+
+TEST_F(ConstantCategoryRankerTest, ShouldSortNonConsequtiveRemoteCategories) {
+ AddUnusedRemoteCategories(3);
+
+ Category first = AddUnusedRemoteCategory();
+
+ AddUnusedRemoteCategories(3);
+
+ Category second = AddUnusedRemoteCategory();
+
+ AddUnusedRemoteCategories(3);
+
+ EXPECT_TRUE(CompareCategories(first, second));
+ EXPECT_FALSE(CompareCategories(second, first));
+}
+
+} // namespace ntp_snippets

Powered by Google App Engine
This is Rietveld 408576698