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

Side by Side Diff: components/ntp_snippets/category_factory_unittest.cc

Issue 2244123004: Added CategoryFactoryTest. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: The last comment. Created 4 years, 4 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 unified diff | Download patch
« no previous file with comments | « components/ntp_snippets/BUILD.gn ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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() : unused_remote_category_id_(1) {}
19
20 int GetUnusedRemoteCategoryID() { return unused_remote_category_id_++; }
21
22 bool CompareCategories(const Category& left, const Category& right) {
23 return factory()->CompareCategories(left, right);
24 }
25
26 void AddDummyRemoteCategories(int quantity) {
27 for (int i = 0; i < quantity; ++i) {
28 factory()->FromRemoteCategory(GetUnusedRemoteCategoryID());
29 }
30 }
31
32 CategoryFactory* factory() { return &factory_; }
33
34 private:
35 CategoryFactory factory_;
36 int unused_remote_category_id_;
37
38 DISALLOW_COPY_AND_ASSIGN(CategoryFactoryTest);
39 };
40
41 TEST_F(CategoryFactoryTest,
42 FromKnownCategoryShouldReturnSameIdForSameCategories) {
43 const KnownCategories known_category = KnownCategories::BOOKMARKS;
44 Category first = factory()->FromKnownCategory(known_category);
45 Category second = factory()->FromKnownCategory(known_category);
46 EXPECT_EQ(first, second);
47 }
48
49 TEST_F(CategoryFactoryTest,
50 FromRemoteCategoryShouldReturnSameIdForSameCategories) {
51 const int remote_category_id = GetUnusedRemoteCategoryID();
52 Category first = factory()->FromRemoteCategory(remote_category_id);
53 Category second = factory()->FromRemoteCategory(remote_category_id);
54 EXPECT_EQ(first, second);
55 }
56
57 TEST_F(CategoryFactoryTest, FromRemoteCategoryOrder) {
58 const int small_id = GetUnusedRemoteCategoryID();
59 const int large_id = GetUnusedRemoteCategoryID();
60 // Categories are added in decreasing id order to test that they are not
61 // compared by id.
62 Category added_first = factory()->FromRemoteCategory(large_id);
63 Category added_second = factory()->FromRemoteCategory(small_id);
64 EXPECT_TRUE(CompareCategories(added_first, added_second));
65 EXPECT_FALSE(CompareCategories(added_second, added_first));
66 }
67
68 TEST_F(CategoryFactoryTest, FromIDValueReturnsSameKnownCategory) {
69 Category known_category =
70 factory()->FromKnownCategory(KnownCategories::BOOKMARKS);
71 Category known_category_by_id = factory()->FromIDValue(known_category.id());
72 EXPECT_EQ(known_category, known_category_by_id);
73 }
74
75 TEST_F(CategoryFactoryTest, FromIDValueReturnsSameRemoteCategory) {
76 const int remote_category_id = GetUnusedRemoteCategoryID();
77 Category remote_category = factory()->FromRemoteCategory(remote_category_id);
78 Category remote_category_by_id = factory()->FromIDValue(remote_category.id());
79 EXPECT_EQ(remote_category, remote_category_by_id);
80 }
81
82 TEST_F(CategoryFactoryTest, CompareCategoriesLocalBeforeRemote) {
83 const int remote_category_id = GetUnusedRemoteCategoryID();
84 Category remote_category = factory()->FromRemoteCategory(remote_category_id);
85 Category local_category =
86 factory()->FromKnownCategory(KnownCategories::BOOKMARKS);
87 EXPECT_TRUE(CompareCategories(local_category, remote_category));
88 EXPECT_FALSE(CompareCategories(remote_category, local_category));
89 }
90
91 TEST_F(CategoryFactoryTest, CompareCategoriesSame) {
92 const int remote_category_id = GetUnusedRemoteCategoryID();
93 Category remote_category = factory()->FromRemoteCategory(remote_category_id);
94 EXPECT_FALSE(CompareCategories(remote_category, remote_category));
95
96 Category local_category =
97 factory()->FromKnownCategory(KnownCategories::BOOKMARKS);
98 EXPECT_FALSE(CompareCategories(local_category, local_category));
99 }
100
101 TEST_F(CategoryFactoryTest, CompareCategoriesAfterAddingNew) {
102 AddDummyRemoteCategories(3);
103
104 Category consequtive_first =
105 factory()->FromRemoteCategory(GetUnusedRemoteCategoryID());
106 Category consequtive_second =
107 factory()->FromRemoteCategory(GetUnusedRemoteCategoryID());
108
109 AddDummyRemoteCategories(3);
110
111 Category nonconsequtive_first =
112 factory()->FromRemoteCategory(GetUnusedRemoteCategoryID());
113 AddDummyRemoteCategories(3);
114 Category nonconsequtive_second =
115 factory()->FromRemoteCategory(GetUnusedRemoteCategoryID());
116
117 AddDummyRemoteCategories(3);
118
119 EXPECT_TRUE(CompareCategories(consequtive_first, consequtive_second));
120 EXPECT_FALSE(CompareCategories(consequtive_second, consequtive_first));
121
122 EXPECT_TRUE(CompareCategories(nonconsequtive_first, nonconsequtive_second));
123 EXPECT_FALSE(CompareCategories(nonconsequtive_second, nonconsequtive_first));
124 }
125
126 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698