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

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

Powered by Google App Engine
This is Rietveld 408576698