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
3 // found in the LICENSE file.
5 #include "components/suggestions/suggestions_store.h"
7 #include "base/time/time.h"
8 #include "components/pref_registry/testing_pref_service_syncable.h"
9 #include "components/suggestions/proto/suggestions.pb.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 using user_prefs::TestingPrefServiceSyncable
;
14 namespace suggestions
{
18 const char kTestTitle
[] = "Foo site";
19 const char kTestUrl
[] = "http://foo.com/";
20 const int kTimeGapUsec
= 100000;
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
);
30 SuggestionsProfile
CreateTestSuggestions() {
31 SuggestionsProfile suggestions
;
32 ChromeSuggestion
* suggestion
= suggestions
.add_suggestions();
33 suggestion
->set_url(kTestTitle
);
34 suggestion
->set_title(kTestUrl
);
38 SuggestionsProfile
CreateTestSuggestionsProfileWithExpiry(int expired_count
,
40 int64 now_usec
= (base::Time::NowFromSystemTime() - base::Time::UnixEpoch())
42 srand(7); // Constant seed for rand() function.
43 int64 offset_limit_usec
= 30 * base::Time::kMicrosecondsPerDay
;
44 int64 offset_usec
= rand() % offset_limit_usec
+ kTimeGapUsec
;
46 SuggestionsProfile suggestions
;
47 for (int i
= 0; i
< valid_count
; i
++)
48 AddSuggestion(&suggestions
, kTestTitle
, kTestUrl
, now_usec
+ offset_usec
);
49 for (int i
= 0; i
< expired_count
; i
++)
50 AddSuggestion(&suggestions
, kTestTitle
, kTestUrl
, now_usec
- offset_usec
);
55 void ValidateSuggestions(const SuggestionsProfile
& expected
,
56 const SuggestionsProfile
& actual
) {
57 EXPECT_EQ(expected
.suggestions_size(), actual
.suggestions_size());
58 for (int i
= 0; i
< expected
.suggestions_size(); ++i
) {
59 EXPECT_EQ(expected
.suggestions(i
).url(), actual
.suggestions(i
).url());
60 EXPECT_EQ(expected
.suggestions(i
).title(), actual
.suggestions(i
).title());
61 EXPECT_EQ(expected
.suggestions(i
).expiry_ts(),
62 actual
.suggestions(i
).expiry_ts());
63 EXPECT_EQ(expected
.suggestions(i
).favicon_url(),
64 actual
.suggestions(i
).favicon_url());
65 EXPECT_EQ(expected
.suggestions(i
).thumbnail(),
66 actual
.suggestions(i
).thumbnail());
72 class SuggestionsStoreTest
: public testing::Test
{
74 SuggestionsStoreTest()
75 : pref_service_(new user_prefs::TestingPrefServiceSyncable
) {}
77 void SetUp() override
{
78 SuggestionsStore::RegisterProfilePrefs(pref_service_
->registry());
79 suggestions_store_
.reset(new SuggestionsStore(pref_service_
.get()));
83 scoped_ptr
<user_prefs::TestingPrefServiceSyncable
> pref_service_
;
84 scoped_ptr
<SuggestionsStore
> suggestions_store_
;
86 DISALLOW_COPY_AND_ASSIGN(SuggestionsStoreTest
);
89 // Tests LoadSuggestions function to filter expired suggestions.
90 TEST_F(SuggestionsStoreTest
, LoadAllExpired
) {
91 SuggestionsProfile suggestions
= CreateTestSuggestionsProfileWithExpiry(5, 0);
92 SuggestionsProfile filtered_suggestions
;
94 // Store and load. Expired suggestions should not be loaded.
95 EXPECT_TRUE(suggestions_store_
->StoreSuggestions(suggestions
));
96 EXPECT_FALSE(suggestions_store_
->LoadSuggestions(&filtered_suggestions
));
97 EXPECT_EQ(0, filtered_suggestions
.suggestions_size());
100 // Tests LoadSuggestions function to filter expired suggestions.
101 TEST_F(SuggestionsStoreTest
, LoadValidAndExpired
) {
102 SuggestionsProfile suggestions
= CreateTestSuggestionsProfileWithExpiry(5, 3);
103 SuggestionsProfile filtered_suggestions
;
105 // Store and load. Expired suggestions should not be loaded.
106 EXPECT_TRUE(suggestions_store_
->StoreSuggestions(suggestions
));
107 EXPECT_TRUE(suggestions_store_
->LoadSuggestions(&filtered_suggestions
));
108 EXPECT_EQ(3, filtered_suggestions
.suggestions_size());
111 // Tests LoadSuggestions function to filter expired suggestions.
112 TEST_F(SuggestionsStoreTest
, CheckStoreAfterLoadExpired
) {
113 SuggestionsProfile suggestions
= CreateTestSuggestionsProfileWithExpiry(5, 3);
114 SuggestionsProfile filtered_suggestions
;
116 // Store and load. Expired suggestions should not be loaded.
117 EXPECT_TRUE(suggestions_store_
->StoreSuggestions(suggestions
));
118 EXPECT_TRUE(suggestions_store_
->LoadSuggestions(&filtered_suggestions
));
120 SuggestionsProfile loaded_suggestions
;
121 EXPECT_TRUE(suggestions_store_
->LoadSuggestions(&loaded_suggestions
));
122 EXPECT_EQ(3, loaded_suggestions
.suggestions_size());
123 ValidateSuggestions(filtered_suggestions
, loaded_suggestions
);
126 TEST_F(SuggestionsStoreTest
, LoadStoreClear
) {
127 const SuggestionsProfile suggestions
= CreateTestSuggestions();
128 const SuggestionsProfile empty_suggestions
;
129 SuggestionsProfile recovered_suggestions
;
131 // Attempt to load when prefs are empty.
132 EXPECT_FALSE(suggestions_store_
->LoadSuggestions(&recovered_suggestions
));
133 ValidateSuggestions(empty_suggestions
, recovered_suggestions
);
135 // Store then reload.
136 EXPECT_TRUE(suggestions_store_
->StoreSuggestions(suggestions
));
137 EXPECT_TRUE(suggestions_store_
->LoadSuggestions(&recovered_suggestions
));
138 ValidateSuggestions(suggestions
, recovered_suggestions
);
141 suggestions_store_
->ClearSuggestions();
142 EXPECT_FALSE(suggestions_store_
->LoadSuggestions(&recovered_suggestions
));
143 ValidateSuggestions(empty_suggestions
, recovered_suggestions
);
146 } // namespace suggestions