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

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: Adressed Marc's comments. 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; }
Marc Treib 2016/08/17 14:41:34 nit: Do this in the initializer list instead of in
vitaliii 2016/08/18 07:03:14 Done.
19
20 int GetUnusedRemoteCategoryID() {
21 int id = unused_remote_category_id_;
22 ++unused_remote_category_id_;
23 return id;
24 }
25
26 bool CompareCategories(const Category& left, const Category& right) {
27 return factory()->CompareCategories(left, right);
28 }
29
30 void AddDummyRemoteCategories(int quantity) {
31 for (int i = 0; i < quantity; ++i) {
32 factory()->FromRemoteCategory(GetUnusedRemoteCategoryID());
33 }
34 }
35
36 CategoryFactory* factory() { return &factory_; }
37
38 private:
39 CategoryFactory factory_;
40 int unused_remote_category_id_;
41
42 DISALLOW_COPY_AND_ASSIGN(CategoryFactoryTest);
43 };
44
45 TEST_F(CategoryFactoryTest,
46 FromKnownCategoryShouldReturnSameIdForSameCategories) {
47 const KnownCategories known_category = KnownCategories::BOOKMARKS;
48 Category first = factory()->FromKnownCategory(known_category);
49 Category second = factory()->FromKnownCategory(known_category);
50 EXPECT_EQ(first, second);
51 }
52
53 TEST_F(CategoryFactoryTest,
54 FromRemoteCategoryShouldReturnSameIdForSameCategories) {
55 const int remote_category_id = GetUnusedRemoteCategoryID();
56 Category first = factory()->FromRemoteCategory(remote_category_id);
57 Category second = factory()->FromRemoteCategory(remote_category_id);
58 EXPECT_EQ(first, second);
59 }
60
61 TEST_F(CategoryFactoryTest, FromRemoteCategoryOrder) {
62 const int first_id = GetUnusedRemoteCategoryID();
63 const int second_id = GetUnusedRemoteCategoryID();
64 Category added_first = factory()->FromRemoteCategory(second_id);
Marc Treib 2016/08/17 14:41:34 Add a comment here, saying that you add second_id
vitaliii 2016/08/18 07:03:14 Done.
65 Category added_second = factory()->FromRemoteCategory(first_id);
66 EXPECT_TRUE(CompareCategories(added_first, added_second));
67 EXPECT_FALSE(CompareCategories(added_second, added_first));
68 }
69
70 TEST_F(CategoryFactoryTest, FromIDValueReturnsSameKnownCategory) {
71 Category known_category =
72 factory()->FromKnownCategory(KnownCategories::BOOKMARKS);
73 Category known_category_by_id = factory()->FromIDValue(known_category.id());
74 EXPECT_EQ(known_category, known_category_by_id);
75 }
76
77 TEST_F(CategoryFactoryTest, FromIDValueReturnsSameRemoteCategory) {
78 const int remote_category_id = GetUnusedRemoteCategoryID();
79 Category remote_category = factory()->FromRemoteCategory(remote_category_id);
80 Category remote_category_by_id = factory()->FromIDValue(remote_category.id());
81 EXPECT_EQ(remote_category, remote_category_by_id);
82 }
83
84 TEST_F(CategoryFactoryTest, CompareCategoriesLocalBeforeRemote) {
85 const int remote_category_id = GetUnusedRemoteCategoryID();
86 Category remote_category = factory()->FromRemoteCategory(remote_category_id);
87 Category local_category =
88 factory()->FromKnownCategory(KnownCategories::BOOKMARKS);
89 EXPECT_TRUE(CompareCategories(local_category, remote_category));
90 EXPECT_FALSE(CompareCategories(remote_category, local_category));
91 }
92
93 TEST_F(CategoryFactoryTest, CompareCategoriesSame) {
94 const int remote_category_id = GetUnusedRemoteCategoryID();
95 Category remote_category = factory()->FromRemoteCategory(remote_category_id);
96 EXPECT_FALSE(CompareCategories(remote_category, remote_category));
97
98 Category local_category =
99 factory()->FromKnownCategory(KnownCategories::BOOKMARKS);
100 EXPECT_FALSE(CompareCategories(local_category, local_category));
101 }
102
103 TEST_F(CategoryFactoryTest, CompareCategoriesAfterAddingNew) {
104 AddDummyRemoteCategories(3);
105
106 Category consequtive_first =
107 factory()->FromRemoteCategory(GetUnusedRemoteCategoryID());
108 Category consequtive_second =
109 factory()->FromRemoteCategory(GetUnusedRemoteCategoryID());
110
111 AddDummyRemoteCategories(3);
112
113 Category nonconsequtive_first =
114 factory()->FromRemoteCategory(GetUnusedRemoteCategoryID());
115 AddDummyRemoteCategories(3);
116 Category nonconsequtive_second =
117 factory()->FromRemoteCategory(GetUnusedRemoteCategoryID());
118
119 AddDummyRemoteCategories(3);
120
121 EXPECT_TRUE(CompareCategories(consequtive_first, consequtive_second));
122 EXPECT_FALSE(CompareCategories(consequtive_second, consequtive_first));
123
124 EXPECT_TRUE(CompareCategories(nonconsequtive_first, nonconsequtive_second));
125 EXPECT_FALSE(CompareCategories(nonconsequtive_second, nonconsequtive_first));
126 }
127
128 } // 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