Chromium Code Reviews| 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 | |
| 20 void SetUp() override { unused_remote_category_id_ = 1; } | |
|
Marc Treib
2016/08/16 13:44:21
Put this in the constructor and remove SetUp
vitaliii
2016/08/17 13:45:07
Done.
| |
| 21 | |
| 22 int GetUnusedRemoteCategoryID() { | |
| 23 int id = unused_remote_category_id_; | |
| 24 ++unused_remote_category_id_; | |
|
Philipp Keck
2016/08/16 13:22:47
Isn't this whole function equivalent to "return un
Marc Treib
2016/08/17 14:41:34
+1 - I'd just do
return unused_remote_category_id+
Marc Treib
2016/08/18 08:21:53
This is still open.
vitaliii
2016/08/18 09:00:13
Done.
| |
| 25 return id; | |
| 26 } | |
| 27 | |
| 28 bool CompareCategories(const Category& left, const Category& right) const { | |
| 29 return factory()->CompareCategories(left, right); | |
| 30 } | |
| 31 | |
| 32 KnownCategories KnownCategoryByID(int id) { | |
| 33 return static_cast<KnownCategories>(id); | |
| 34 } | |
| 35 | |
| 36 CategoryFactory* factory() { return &factory_; } | |
| 37 const CategoryFactory* factory() const { return &factory_; } | |
|
Marc Treib
2016/08/16 13:44:21
I think the const version isn't required.
vitaliii
2016/08/17 13:45:07
Done.
| |
| 38 | |
| 39 private: | |
| 40 CategoryFactory factory_; | |
| 41 int unused_remote_category_id_; | |
| 42 | |
| 43 DISALLOW_COPY_AND_ASSIGN(CategoryFactoryTest); | |
| 44 }; | |
| 45 | |
| 46 TEST_F(CategoryFactoryTest, | |
| 47 FromKnownCategoryShouldReturnSameIdForSameCategories) { | |
| 48 const KnownCategories known_category = KnownCategoryByID(0); | |
|
Marc Treib
2016/08/16 13:44:21
Just use any actual known category here and get ri
vitaliii
2016/08/17 13:45:07
Done.
| |
| 49 Category first = factory()->FromKnownCategory(known_category); | |
| 50 Category second = factory()->FromKnownCategory(known_category); | |
| 51 EXPECT_EQ(first, second); | |
| 52 } | |
| 53 | |
| 54 TEST_F(CategoryFactoryTest, | |
| 55 FromRemoteCategoryShouldReturnSameIdForSameCategories) { | |
| 56 const int remote_category_id = GetUnusedRemoteCategoryID(); | |
| 57 Category first = factory()->FromRemoteCategory(remote_category_id); | |
| 58 Category second = factory()->FromRemoteCategory(remote_category_id); | |
| 59 EXPECT_EQ(first, second); | |
| 60 } | |
| 61 | |
| 62 TEST_F(CategoryFactoryTest, FromRemoteCategoryOrder) { | |
| 63 const int first_id = GetUnusedRemoteCategoryID(); | |
| 64 const int second_id = GetUnusedRemoteCategoryID(); | |
| 65 Category added_first = factory()->FromRemoteCategory(second_id); | |
| 66 Category added_second = factory()->FromRemoteCategory(first_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 const KnownCategories raw_known_category = KnownCategoryByID(0); | |
| 73 Category known_category = factory()->FromKnownCategory(raw_known_category); | |
| 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 const KnownCategories raw_known_category = KnownCategoryByID(0); | |
| 89 Category local_category = factory()->FromKnownCategory(raw_known_category); | |
| 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 const KnownCategories raw_known_category = KnownCategoryByID(0); | |
| 100 Category local_category = factory()->FromKnownCategory(raw_known_category); | |
| 101 EXPECT_FALSE(CompareCategories(local_category, local_category)); | |
| 102 } | |
| 103 | |
| 104 TEST_F(CategoryFactoryTest, CompareCategoriesSortContract) { | |
|
Marc Treib
2016/08/16 13:44:21
This name is not very expressive - it really tests
vitaliii
2016/08/17 13:45:07
Acknowledged.
Marc Treib
2016/08/17 14:41:34
CL tool usage nit: "Acknowledged" means "I read yo
| |
| 105 std::vector<int> remote_category_ids; | |
| 106 for (size_t i = 0; i < 4; ++i) { | |
|
Philipp Keck
2016/08/16 13:22:47
Include stddef.h because of size_t? Let Marc decid
Marc Treib
2016/08/16 13:44:21
Eh.. it's technically required, but I think most f
vitaliii
2016/08/17 13:45:07
Acknowledged.
| |
| 107 remote_category_ids.push_back(GetUnusedRemoteCategoryID()); | |
| 108 } | |
| 109 | |
| 110 // Categories with higher id are added first. | |
| 111 std::reverse(remote_category_ids.begin(), remote_category_ids.end()); | |
| 112 | |
| 113 std::vector<Category> categories; | |
| 114 for (int id : remote_category_ids) { | |
| 115 categories.push_back(factory()->FromRemoteCategory(id)); | |
| 116 } | |
| 117 | |
| 118 for (size_t first_i = 0; first_i < remote_category_ids.size(); ++first_i) { | |
| 119 for (size_t second_i = 0; second_i < remote_category_ids.size(); | |
| 120 ++second_i) { | |
| 121 int first_id = remote_category_ids[first_i]; | |
| 122 int second_id = remote_category_ids[second_i]; | |
| 123 EXPECT_EQ(first_id > second_id, | |
| 124 CompareCategories(categories[first_i], categories[second_i])); | |
|
Marc Treib
2016/08/16 13:44:21
Hm, I find this a bit hard to read - it's not real
vitaliii
2016/08/17 13:45:07
It is already done just for two categories in From
| |
| 125 } | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 } // namespace ntp_snippets | |
| OLD | NEW |