| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/ntp_snippets/category_factory.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "components/ntp_snippets/category.h" | |
| 11 #include "testing/gmock/include/gmock/gmock.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace ntp_snippets { | |
| 15 | |
| 16 class CategoryFactoryTest : public testing::Test { | |
| 17 public: | |
| 18 CategoryFactoryTest() | |
| 19 : unused_remote_category_id_( | |
| 20 static_cast<int>(KnownCategories::LAST_KNOWN_REMOTE_CATEGORY) + 1) { | |
| 21 } | |
| 22 | |
| 23 int GetUnusedRemoteCategoryID() { return unused_remote_category_id_++; } | |
| 24 | |
| 25 bool CompareCategories(const Category& left, const Category& right) { | |
| 26 return factory()->CompareCategories(left, right); | |
| 27 } | |
| 28 | |
| 29 void AddDummyRemoteCategories(int quantity) { | |
| 30 for (int i = 0; i < quantity; ++i) { | |
| 31 factory()->FromRemoteCategory(GetUnusedRemoteCategoryID()); | |
| 32 } | |
| 33 } | |
| 34 | |
| 35 CategoryFactory* factory() { return &factory_; } | |
| 36 | |
| 37 private: | |
| 38 CategoryFactory factory_; | |
| 39 int unused_remote_category_id_; | |
| 40 | |
| 41 DISALLOW_COPY_AND_ASSIGN(CategoryFactoryTest); | |
| 42 }; | |
| 43 | |
| 44 TEST_F(CategoryFactoryTest, | |
| 45 FromKnownCategoryShouldReturnSameIdForSameCategories) { | |
| 46 const KnownCategories known_category = KnownCategories::BOOKMARKS; | |
| 47 Category first = factory()->FromKnownCategory(known_category); | |
| 48 Category second = factory()->FromKnownCategory(known_category); | |
| 49 EXPECT_EQ(first, second); | |
| 50 } | |
| 51 | |
| 52 TEST_F(CategoryFactoryTest, | |
| 53 FromRemoteCategoryShouldReturnSameIdForSameCategories) { | |
| 54 const int remote_category_id = GetUnusedRemoteCategoryID(); | |
| 55 Category first = factory()->FromRemoteCategory(remote_category_id); | |
| 56 Category second = factory()->FromRemoteCategory(remote_category_id); | |
| 57 EXPECT_EQ(first, second); | |
| 58 } | |
| 59 | |
| 60 TEST_F(CategoryFactoryTest, FromRemoteCategoryOrder) { | |
| 61 const int small_id = GetUnusedRemoteCategoryID(); | |
| 62 const int large_id = GetUnusedRemoteCategoryID(); | |
| 63 // Categories are added in decreasing id order to test that they are not | |
| 64 // compared by id. | |
| 65 Category added_first = factory()->FromRemoteCategory(large_id); | |
| 66 Category added_second = factory()->FromRemoteCategory(small_id); | |
| 67 EXPECT_TRUE(CompareCategories(added_first, added_second)); | |
| 68 EXPECT_FALSE(CompareCategories(added_second, added_first)); | |
| 69 } | |
| 70 | |
| 71 TEST_F(CategoryFactoryTest, FromIDValueReturnsSameKnownCategory) { | |
| 72 Category known_category = | |
| 73 factory()->FromKnownCategory(KnownCategories::BOOKMARKS); | |
| 74 Category known_category_by_id = factory()->FromIDValue(known_category.id()); | |
| 75 EXPECT_EQ(known_category, known_category_by_id); | |
| 76 } | |
| 77 | |
| 78 TEST_F(CategoryFactoryTest, FromIDValueReturnsSameRemoteCategory) { | |
| 79 const int remote_category_id = GetUnusedRemoteCategoryID(); | |
| 80 Category remote_category = factory()->FromRemoteCategory(remote_category_id); | |
| 81 Category remote_category_by_id = factory()->FromIDValue(remote_category.id()); | |
| 82 EXPECT_EQ(remote_category, remote_category_by_id); | |
| 83 } | |
| 84 | |
| 85 TEST_F(CategoryFactoryTest, CompareCategoriesLocalBeforeRemote) { | |
| 86 const int remote_category_id = GetUnusedRemoteCategoryID(); | |
| 87 Category remote_category = factory()->FromRemoteCategory(remote_category_id); | |
| 88 Category local_category = | |
| 89 factory()->FromKnownCategory(KnownCategories::BOOKMARKS); | |
| 90 EXPECT_TRUE(CompareCategories(local_category, remote_category)); | |
| 91 EXPECT_FALSE(CompareCategories(remote_category, local_category)); | |
| 92 } | |
| 93 | |
| 94 TEST_F(CategoryFactoryTest, CompareCategoriesSame) { | |
| 95 const int remote_category_id = GetUnusedRemoteCategoryID(); | |
| 96 Category remote_category = factory()->FromRemoteCategory(remote_category_id); | |
| 97 EXPECT_FALSE(CompareCategories(remote_category, remote_category)); | |
| 98 | |
| 99 Category local_category = | |
| 100 factory()->FromKnownCategory(KnownCategories::BOOKMARKS); | |
| 101 EXPECT_FALSE(CompareCategories(local_category, local_category)); | |
| 102 } | |
| 103 | |
| 104 TEST_F(CategoryFactoryTest, CompareCategoriesAfterAddingNew) { | |
| 105 AddDummyRemoteCategories(3); | |
| 106 | |
| 107 Category consequtive_first = | |
| 108 factory()->FromRemoteCategory(GetUnusedRemoteCategoryID()); | |
| 109 Category consequtive_second = | |
| 110 factory()->FromRemoteCategory(GetUnusedRemoteCategoryID()); | |
| 111 | |
| 112 AddDummyRemoteCategories(3); | |
| 113 | |
| 114 Category nonconsequtive_first = | |
| 115 factory()->FromRemoteCategory(GetUnusedRemoteCategoryID()); | |
| 116 AddDummyRemoteCategories(3); | |
| 117 Category nonconsequtive_second = | |
| 118 factory()->FromRemoteCategory(GetUnusedRemoteCategoryID()); | |
| 119 | |
| 120 AddDummyRemoteCategories(3); | |
| 121 | |
| 122 EXPECT_TRUE(CompareCategories(consequtive_first, consequtive_second)); | |
| 123 EXPECT_FALSE(CompareCategories(consequtive_second, consequtive_first)); | |
| 124 | |
| 125 EXPECT_TRUE(CompareCategories(nonconsequtive_first, nonconsequtive_second)); | |
| 126 EXPECT_FALSE(CompareCategories(nonconsequtive_second, nonconsequtive_first)); | |
| 127 } | |
| 128 | |
| 129 } // namespace ntp_snippets | |
| OLD | NEW |