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

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: Some fixes. Test class for SuggestionStoreTest 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/";
21 const int kTimeGapUsec = 100000;
22
23 void AddSuggestion(SuggestionsProfile* suggestions, const char *title,
24 const char *url, int64 expiry_ts) {
25 ChromeSuggestion* suggestion = suggestions->add_suggestions();
26 suggestion->set_url(title);
27 suggestion->set_title(url);
28 suggestion->set_expiry_ts(expiry_ts);
29 }
19 30
20 SuggestionsProfile CreateTestSuggestions() { 31 SuggestionsProfile CreateTestSuggestions() {
21 SuggestionsProfile suggestions; 32 SuggestionsProfile suggestions;
22 ChromeSuggestion* suggestion = suggestions.add_suggestions(); 33 ChromeSuggestion* suggestion = suggestions.add_suggestions();
23 suggestion->set_url(kTestUrl); 34 suggestion->set_url(kTestTitle);
24 suggestion->set_title(kTestTitle); 35 suggestion->set_title(kTestUrl);
25 return suggestions; 36 return suggestions;
26 } 37 }
27 38
39 SuggestionsProfile CreateTestSuggestionsProfileWithExpiry(int expired_count,
40 int valid_count) {
41 int64 now_usec = (base::Time::NowFromSystemTime() - base::Time::UnixEpoch())
42 .ToInternalValue();
43 srand(7); // Constant seed for rand() function.
44 int64 offset_limit_usec = 30 * base::Time::kMicrosecondsPerDay;
45 SuggestionsProfile suggestions;
46 for (int i = 0; i < valid_count; i++){
47 int64 offset_usec = rand() % offset_limit_usec + kTimeGapUsec;
48 AddSuggestion(&suggestions, kTestTitle, kTestUrl, now_usec + offset_usec);
49 }
50 for (int i = 0; i < expired_count; i++){
51 int64 offset_usec = rand() % offset_limit_usec - kTimeGapUsec;
52 AddSuggestion(&suggestions, kTestTitle, kTestUrl, now_usec - offset_usec);
53 }
54 return suggestions;
55 }
56
28 void ValidateSuggestions(const SuggestionsProfile& expected, 57 void ValidateSuggestions(const SuggestionsProfile& expected,
29 const SuggestionsProfile& actual) { 58 const SuggestionsProfile& actual) {
30 EXPECT_EQ(expected.suggestions_size(), actual.suggestions_size()); 59 EXPECT_EQ(expected.suggestions_size(), actual.suggestions_size());
31 for (int i = 0; i < expected.suggestions_size(); ++i) { 60 for (int i = 0; i < expected.suggestions_size(); ++i) {
32 EXPECT_EQ(expected.suggestions(i).url(), actual.suggestions(i).url()); 61 EXPECT_EQ(expected.suggestions(i).url(), actual.suggestions(i).url());
33 EXPECT_EQ(expected.suggestions(i).title(), actual.suggestions(i).title()); 62 EXPECT_EQ(expected.suggestions(i).title(), actual.suggestions(i).title());
63 EXPECT_EQ(expected.suggestions(i).expiry_ts(),
64 actual.suggestions(i).expiry_ts());
34 EXPECT_EQ(expected.suggestions(i).favicon_url(), 65 EXPECT_EQ(expected.suggestions(i).favicon_url(),
35 actual.suggestions(i).favicon_url()); 66 actual.suggestions(i).favicon_url());
36 EXPECT_EQ(expected.suggestions(i).thumbnail(), 67 EXPECT_EQ(expected.suggestions(i).thumbnail(),
37 actual.suggestions(i).thumbnail()); 68 actual.suggestions(i).thumbnail());
38 } 69 }
39 } 70 }
40 71
41 } // namespace 72 } // namespace
42 73
43 TEST(SuggestionsStoreTest, LoadStoreClear) { 74 class SuggestionsStoreTest : public testing::Test {
44 TestingPrefServiceSyncable prefs; 75 public:
45 SuggestionsStore::RegisterProfilePrefs(prefs.registry()); 76 SuggestionsStoreTest()
46 SuggestionsStore suggestions_store(&prefs); 77 : pref_service_(new user_prefs::TestingPrefServiceSyncable) {}
47 78
79 virtual void SetUp() OVERRIDE {
80 SuggestionsStore::RegisterProfilePrefs(pref_service()->registry());
81 }
82
83 user_prefs::TestingPrefServiceSyncable* pref_service() {
84 return pref_service_.get();
85 }
86
87 private:
88 scoped_ptr<user_prefs::TestingPrefServiceSyncable> pref_service_;
89
90 DISALLOW_COPY_AND_ASSIGN(SuggestionsStoreTest);
91 };
92
93 // Tests LoadSuggestions function to filter expired suggestions.
94 TEST_F(SuggestionsStoreTest, LoadAllExpired) {
95 SuggestionsStore suggestions_store(pref_service());
96 SuggestionsProfile suggestions = CreateTestSuggestionsProfileWithExpiry(5, 0);
97 SuggestionsProfile filtered_suggestions;
98
99 // Store and load. Expired suggestions should not be loaded.
100 EXPECT_TRUE(suggestions_store.StoreSuggestions(suggestions));
101 EXPECT_FALSE(suggestions_store.LoadSuggestions(&filtered_suggestions));
102 EXPECT_EQ(0, filtered_suggestions.suggestions_size());
103 }
104
105 // Tests LoadSuggestions function to filter expired suggestions.
106 TEST_F(SuggestionsStoreTest, LoadValidAndExpired) {
107 SuggestionsStore suggestions_store(pref_service());
108 SuggestionsProfile suggestions = CreateTestSuggestionsProfileWithExpiry(5, 3);
109 SuggestionsProfile filtered_suggestions;
110
111 // Store and load. Expired suggestions should not be loaded.
112 EXPECT_TRUE(suggestions_store.StoreSuggestions(suggestions));
113 EXPECT_TRUE(suggestions_store.LoadSuggestions(&filtered_suggestions));
114 EXPECT_EQ(3, filtered_suggestions.suggestions_size());
115 }
116
117 // Tests LoadSuggestions function to filter expired suggestions.
118 TEST_F(SuggestionsStoreTest, CheckStoreAfterLoadExpired) {
119 SuggestionsStore suggestions_store(pref_service());
120 SuggestionsProfile suggestions = CreateTestSuggestionsProfileWithExpiry(5, 3);
121 SuggestionsProfile filtered_suggestions;
122
123 // Store and load. Expired suggestions should not be loaded.
124 EXPECT_TRUE(suggestions_store.StoreSuggestions(suggestions));
125 EXPECT_TRUE(suggestions_store.LoadSuggestions(&filtered_suggestions));
126
127 SuggestionsProfile loaded_suggestions;
128 EXPECT_TRUE(suggestions_store.LoadSuggestions(&loaded_suggestions));
129 EXPECT_EQ(3, loaded_suggestions.suggestions_size());
130 ValidateSuggestions(filtered_suggestions, loaded_suggestions);
131 }
132
133 TEST_F(SuggestionsStoreTest, LoadStoreClear) {
134 SuggestionsStore suggestions_store(pref_service());
48 const SuggestionsProfile suggestions = CreateTestSuggestions(); 135 const SuggestionsProfile suggestions = CreateTestSuggestions();
49 const SuggestionsProfile empty_suggestions; 136 const SuggestionsProfile empty_suggestions;
50 SuggestionsProfile recovered_suggestions; 137 SuggestionsProfile recovered_suggestions;
51 138
52 // Attempt to load when prefs are empty. 139 // Attempt to load when prefs are empty.
53 EXPECT_FALSE(suggestions_store.LoadSuggestions(&recovered_suggestions)); 140 EXPECT_FALSE(suggestions_store.LoadSuggestions(&recovered_suggestions));
54 ValidateSuggestions(empty_suggestions, recovered_suggestions); 141 ValidateSuggestions(empty_suggestions, recovered_suggestions);
55 142
56 // Store then reload. 143 // Store then reload.
57 EXPECT_TRUE(suggestions_store.StoreSuggestions(suggestions)); 144 EXPECT_TRUE(suggestions_store.StoreSuggestions(suggestions));
58 EXPECT_TRUE(suggestions_store.LoadSuggestions(&recovered_suggestions)); 145 EXPECT_TRUE(suggestions_store.LoadSuggestions(&recovered_suggestions));
59 ValidateSuggestions(suggestions, recovered_suggestions); 146 ValidateSuggestions(suggestions, recovered_suggestions);
60 147
61 // Clear. 148 // Clear.
62 suggestions_store.ClearSuggestions(); 149 suggestions_store.ClearSuggestions();
63 EXPECT_FALSE(suggestions_store.LoadSuggestions(&recovered_suggestions)); 150 EXPECT_FALSE(suggestions_store.LoadSuggestions(&recovered_suggestions));
64 ValidateSuggestions(empty_suggestions, recovered_suggestions); 151 ValidateSuggestions(empty_suggestions, recovered_suggestions);
65 } 152 }
66 153
67 } // namespace suggestions 154 } // 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