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

Side by Side Diff: components/suggestions/suggestions_store_unittest.cc

Issue 423133003: [Suggestions Service] Add support for expiring the SuggestionsStore (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Expiry timestamps for suggestions Created 6 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/suggestions/suggestions_store.h" 5 #include "components/suggestions/suggestions_store.h"
6 6
7 #include "base/time/time.h"
8
7 #include "components/pref_registry/testing_pref_service_syncable.h" 9 #include "components/pref_registry/testing_pref_service_syncable.h"
8 #include "components/suggestions/proto/suggestions.pb.h" 10 #include "components/suggestions/proto/suggestions.pb.h"
9 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
10 12
11 using user_prefs::TestingPrefServiceSyncable; 13 using user_prefs::TestingPrefServiceSyncable;
12 14
13 namespace suggestions { 15 namespace suggestions {
14 16
15 namespace { 17 namespace {
16 18
17 const char kTestTitle[] = "Foo site"; 19 const char kTestTitle[] = "Foo site";
18 const char kTestUrl[] = "http://foo.com/"; 20 const char kTestUrl[] = "http://foo.com/";
19 21
22 const int time_gap = 1000;
manzagop (departed) 2014/07/31 15:31:52 Put the unit in the name.
gayane -on leave until 09-2017 2014/08/04 13:46:32 Done.
23
20 SuggestionsProfile CreateTestSuggestions() { 24 SuggestionsProfile CreateTestSuggestions() {
21 SuggestionsProfile suggestions; 25 SuggestionsProfile suggestions;
22 ChromeSuggestion* suggestion = suggestions.add_suggestions(); 26 ChromeSuggestion* suggestion = suggestions.add_suggestions();
23 suggestion->set_url(kTestUrl); 27 suggestion->set_url(kTestUrl);
24 suggestion->set_title(kTestTitle); 28 suggestion->set_title(kTestTitle);
25 return suggestions; 29 return suggestions;
26 } 30 }
27 31
32
33 SuggestionsProfile CreateTestSuggestions_LoadExpiryTests(int expired,
manzagop (departed) 2014/07/31 15:31:52 I don't think CreateTestSuggestions_LoadExpiryTest
manzagop (departed) 2014/07/31 15:31:53 expired_count? valid_count?
gayane -on leave until 09-2017 2014/08/04 13:46:31 Done.
gayane -on leave until 09-2017 2014/08/04 13:46:32 Done.
34 int valid) {
35 int64 now = (base::Time::NowFromSystemTime()
36 -base::Time::UnixEpoch()).ToInternalValue();
manzagop (departed) 2014/07/31 15:31:51 Fix indentation.
gayane -on leave until 09-2017 2014/08/04 13:46:31 Done.
37
manzagop (departed) 2014/07/31 15:31:52 No need for the space.
gayane -on leave until 09-2017 2014/08/04 13:46:32 Done.
38 // constant seed for rand() function.
manzagop (departed) 2014/07/31 15:31:51 Move the comment to the srand line, 2 spaces after
gayane -on leave until 09-2017 2014/08/04 13:46:31 Done.
39 srand(7);
40
41 const int rand_limit = 1000000000;
42
43 SuggestionsProfile suggestions;
44
manzagop (departed) 2014/07/31 15:31:51 Too much vertical whitespace.
gayane -on leave until 09-2017 2014/08/04 13:46:31 Done.
45 for (int i = 0; i < valid; i++){
46 ChromeSuggestion* suggestion = suggestions.add_suggestions();
47 suggestion->set_url(kTestUrl);
48 suggestion->set_title(kTestTitle);
manzagop (departed) 2014/07/31 15:31:52 These 3 lines are duplicated 3 times. Have a AddSu
gayane -on leave until 09-2017 2014/08/04 13:46:32 Done.
49 int r = rand()%rand_limit;
manzagop (departed) 2014/07/31 15:31:51 r isn't very descriptive. Something like future_of
gayane -on leave until 09-2017 2014/08/04 13:46:31 Done.
50 suggestion->set_expiry_ts(now + r + time_gap);
manzagop (departed) 2014/07/31 15:31:53 I'd move the time gap into the computation of r.
gayane -on leave until 09-2017 2014/08/04 13:46:31 Done.
51
52 }
53
manzagop (departed) 2014/07/31 15:31:52 No need for this line.
gayane -on leave until 09-2017 2014/08/04 13:46:32 Done.
54 for (int i = 0; i < expired; i++){
55 ChromeSuggestion* suggestion_expired = suggestions.add_suggestions();
56 suggestion_expired->set_url(kTestUrl);
57 suggestion_expired->set_title(kTestTitle);
58 int r = rand()%rand_limit;
59 suggestion_expired->set_expiry_ts(now - r - time_gap);
60 }
61
62 return suggestions;
63 }
64
28 void ValidateSuggestions(const SuggestionsProfile& expected, 65 void ValidateSuggestions(const SuggestionsProfile& expected,
29 const SuggestionsProfile& actual) { 66 const SuggestionsProfile& actual) {
30 EXPECT_EQ(expected.suggestions_size(), actual.suggestions_size()); 67 EXPECT_EQ(expected.suggestions_size(), actual.suggestions_size());
31 for (int i = 0; i < expected.suggestions_size(); ++i) { 68 for (int i = 0; i < expected.suggestions_size(); ++i) {
32 EXPECT_EQ(expected.suggestions(i).url(), actual.suggestions(i).url()); 69 EXPECT_EQ(expected.suggestions(i).url(), actual.suggestions(i).url());
33 EXPECT_EQ(expected.suggestions(i).title(), actual.suggestions(i).title()); 70 EXPECT_EQ(expected.suggestions(i).title(), actual.suggestions(i).title());
71 EXPECT_EQ(expected.suggestions(i).expiry_ts(),
72 actual.suggestions(i).expiry_ts());
34 EXPECT_EQ(expected.suggestions(i).favicon_url(), 73 EXPECT_EQ(expected.suggestions(i).favicon_url(),
35 actual.suggestions(i).favicon_url()); 74 actual.suggestions(i).favicon_url());
36 EXPECT_EQ(expected.suggestions(i).thumbnail(), 75 EXPECT_EQ(expected.suggestions(i).thumbnail(),
37 actual.suggestions(i).thumbnail()); 76 actual.suggestions(i).thumbnail());
38 } 77 }
39 } 78 }
40 79
41 } // namespace 80 } // namespace
42 81
82 // tests LoadSuggestions function to filter expired suggestions
manzagop (departed) 2014/07/31 15:31:52 Capitalize.
gayane -on leave until 09-2017 2014/08/04 13:46:32 Done.
83 TEST(SuggestionsStoreTest, LoadOneExpired) {
84 TestingPrefServiceSyncable prefs;
85 SuggestionsStore::RegisterProfilePrefs(prefs.registry());
86 SuggestionsStore suggestions_store(&prefs);
87
88 SuggestionsProfile suggestions = CreateTestSuggestions_LoadExpiryTests(1, 0);
89 SuggestionsProfile filtered_suggestions;
90
91 // store and load. Expired suggestion should not be loaded.
manzagop (departed) 2014/07/31 15:31:52 Capitalize.
gayane -on leave until 09-2017 2014/08/04 13:46:32 Done.
92 EXPECT_TRUE(suggestions_store.StoreSuggestions(suggestions));
93 EXPECT_FALSE(suggestions_store.LoadSuggestions(&filtered_suggestions));
94 EXPECT_EQ(0, filtered_suggestions.suggestions_size());
95
96 // Clear.
97 suggestions_store.ClearSuggestions();
manzagop (departed) 2014/07/31 15:31:52 Does this do anything? Isn't clean up done from Te
gayane -on leave until 09-2017 2014/08/04 13:46:32 Removed
98 }
99
100 // tests LoadSuggestions function to filter expired suggestions
101 TEST(SuggestionsStoreTest, LoadOneValid) {
manzagop (departed) 2014/07/31 15:31:52 Hm, this test is identical to the previous up to 3
gayane -on leave until 09-2017 2014/08/04 13:46:32 What is fixture?
102 TestingPrefServiceSyncable prefs;
103 SuggestionsStore::RegisterProfilePrefs(prefs.registry());
104 SuggestionsStore suggestions_store(&prefs);
105
106 SuggestionsProfile suggestions = CreateTestSuggestions_LoadExpiryTests(0, 1);
107 SuggestionsProfile filtered_suggestions;
108
109 // store and load. Suggestion should be loaded.
110 EXPECT_TRUE(suggestions_store.StoreSuggestions(suggestions));
111 EXPECT_TRUE(suggestions_store.LoadSuggestions(&filtered_suggestions));
112 EXPECT_EQ(1, filtered_suggestions.suggestions_size());
113
114 // Clear.
115 suggestions_store.ClearSuggestions();
116 }
117
118 // tests LoadSuggestions function to filter expired suggestions
119 TEST(SuggestionsStoreTest, LoadMultipleExpired) {
manzagop (departed) 2014/07/31 15:31:51 What is the value of this test compared to the sin
gayane -on leave until 09-2017 2014/08/04 13:46:31 well I just thought of testing different cases. Un
120 TestingPrefServiceSyncable prefs;
121 SuggestionsStore::RegisterProfilePrefs(prefs.registry());
122 SuggestionsStore suggestions_store(&prefs);
123
124 SuggestionsProfile suggestions = CreateTestSuggestions_LoadExpiryTests(5, 0);
125 SuggestionsProfile filtered_suggestions;
126
127 // store and load. expired suggestions should not be loaded.
128 EXPECT_TRUE(suggestions_store.StoreSuggestions(suggestions));
129 EXPECT_FALSE(suggestions_store.LoadSuggestions(&filtered_suggestions));
130 EXPECT_EQ(0, filtered_suggestions.suggestions_size());
131
132 // Clear.
133 suggestions_store.ClearSuggestions();
134 }
135
136 // tests LoadSuggestions function to filter expired suggestions
137 TEST(SuggestionsStoreTest, LoadValidAndExpired) {
138 TestingPrefServiceSyncable prefs;
139 SuggestionsStore::RegisterProfilePrefs(prefs.registry());
140 SuggestionsStore suggestions_store(&prefs);
141
142 SuggestionsProfile suggestions = CreateTestSuggestions_LoadExpiryTests(5, 3);
143 SuggestionsProfile filtered_suggestions;
144
145 // store and load. expired suggestions should not be loaded.
146 EXPECT_TRUE(suggestions_store.StoreSuggestions(suggestions));
147 EXPECT_TRUE(suggestions_store.LoadSuggestions(&filtered_suggestions));
148 EXPECT_EQ(3, filtered_suggestions.suggestions_size());
149
150 // Clear.
151 suggestions_store.ClearSuggestions();
152 }
153
154 // tests LoadSuggestions function to filter expired suggestions
155 TEST(SuggestionsStoreTest, LoadValidAndExpiredStore) {
manzagop (departed) 2014/07/31 15:31:52 Perhaps keep only this test and the AllExpired one
manzagop (departed) 2014/07/31 15:31:52 I don't understand what the test does from the nam
gayane -on leave until 09-2017 2014/08/04 13:46:31 Done.
gayane -on leave until 09-2017 2014/08/04 13:46:32 yeah I changed the name, but I am not happy about
156 TestingPrefServiceSyncable prefs;
157 SuggestionsStore::RegisterProfilePrefs(prefs.registry());
158 SuggestionsStore suggestions_store(&prefs);
159
160 SuggestionsProfile suggestions = CreateTestSuggestions_LoadExpiryTests(5, 3);
161 SuggestionsProfile filtered_suggestions;
162
163 // store and load. expired suggestions should not be loaded.
164 EXPECT_TRUE(suggestions_store.StoreSuggestions(suggestions));
165 EXPECT_TRUE(suggestions_store.LoadSuggestions(&filtered_suggestions));
166 EXPECT_EQ(3, filtered_suggestions.suggestions_size());
167
168 SuggestionsProfile loaded_suggestions;
169 EXPECT_TRUE(suggestions_store.LoadSuggestions(&loaded_suggestions));
170 EXPECT_EQ(3, loaded_suggestions.suggestions_size());
171 ValidateSuggestions(filtered_suggestions, loaded_suggestions);
172
173 // Clear.
174 suggestions_store.ClearSuggestions();
175 }
176
43 TEST(SuggestionsStoreTest, LoadStoreClear) { 177 TEST(SuggestionsStoreTest, LoadStoreClear) {
44 TestingPrefServiceSyncable prefs; 178 TestingPrefServiceSyncable prefs;
45 SuggestionsStore::RegisterProfilePrefs(prefs.registry()); 179 SuggestionsStore::RegisterProfilePrefs(prefs.registry());
46 SuggestionsStore suggestions_store(&prefs); 180 SuggestionsStore suggestions_store(&prefs);
47 181
48 const SuggestionsProfile suggestions = CreateTestSuggestions(); 182 const SuggestionsProfile suggestions = CreateTestSuggestions();
49 const SuggestionsProfile empty_suggestions; 183 const SuggestionsProfile empty_suggestions;
50 SuggestionsProfile recovered_suggestions; 184 SuggestionsProfile recovered_suggestions;
51 185
52 // Attempt to load when prefs are empty. 186 // Attempt to load when prefs are empty.
53 EXPECT_FALSE(suggestions_store.LoadSuggestions(&recovered_suggestions)); 187 EXPECT_FALSE(suggestions_store.LoadSuggestions(&recovered_suggestions));
54 ValidateSuggestions(empty_suggestions, recovered_suggestions); 188 ValidateSuggestions(empty_suggestions, recovered_suggestions);
55 189
56 // Store then reload. 190 // Store then reload.
57 EXPECT_TRUE(suggestions_store.StoreSuggestions(suggestions)); 191 EXPECT_TRUE(suggestions_store.StoreSuggestions(suggestions));
58 EXPECT_TRUE(suggestions_store.LoadSuggestions(&recovered_suggestions)); 192 EXPECT_TRUE(suggestions_store.LoadSuggestions(&recovered_suggestions));
59 ValidateSuggestions(suggestions, recovered_suggestions); 193 ValidateSuggestions(suggestions, recovered_suggestions);
60 194
61 // Clear. 195 // Clear.
62 suggestions_store.ClearSuggestions(); 196 suggestions_store.ClearSuggestions();
63 EXPECT_FALSE(suggestions_store.LoadSuggestions(&recovered_suggestions)); 197 EXPECT_FALSE(suggestions_store.LoadSuggestions(&recovered_suggestions));
64 ValidateSuggestions(empty_suggestions, recovered_suggestions); 198 ValidateSuggestions(empty_suggestions, recovered_suggestions);
65 } 199 }
66 200
67 } // namespace suggestions 201 } // namespace suggestions
OLDNEW
« components/suggestions/suggestions_store.cc ('K') | « components/suggestions/suggestions_store.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698