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/test/simple_test_clock.h"
8 #include "base/time/time.h"
9 #include "components/pref_registry/testing_pref_service_syncable.h"
10 #include "components/suggestions/proto/suggestions.pb.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 using user_prefs::TestingPrefServiceSyncable
;
15 namespace suggestions
{
19 const char kTestTitle
[] = "Foo site";
20 const char kTestUrl
[] = "http://foo.com/";
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(
39 base::Time current_time
,
42 int64 current_time_usec
=
43 (current_time
- base::Time::UnixEpoch()).ToInternalValue();
44 int64 offset_usec
= 5 * base::Time::kMicrosecondsPerMinute
;
46 SuggestionsProfile suggestions
;
47 for (int i
= 1; i
<= valid_count
; i
++)
48 AddSuggestion(&suggestions
, kTestTitle
, kTestUrl
,
49 current_time_usec
+ offset_usec
* i
);
50 for (int i
= 1; i
<= expired_count
; i
++)
51 AddSuggestion(&suggestions
, kTestTitle
, kTestUrl
,
52 current_time_usec
- offset_usec
* i
);
57 void ValidateSuggestions(const SuggestionsProfile
& expected
,
58 const SuggestionsProfile
& actual
) {
59 EXPECT_EQ(expected
.suggestions_size(), actual
.suggestions_size());
60 for (int i
= 0; i
< expected
.suggestions_size(); ++i
) {
61 EXPECT_EQ(expected
.suggestions(i
).url(), actual
.suggestions(i
).url());
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());
65 EXPECT_EQ(expected
.suggestions(i
).favicon_url(),
66 actual
.suggestions(i
).favicon_url());
67 EXPECT_EQ(expected
.suggestions(i
).thumbnail(),
68 actual
.suggestions(i
).thumbnail());
74 class SuggestionsStoreTest
: public testing::Test
{
76 SuggestionsStoreTest()
77 : pref_service_(new user_prefs::TestingPrefServiceSyncable
) {}
79 void SetUp() override
{
80 SuggestionsStore::RegisterProfilePrefs(pref_service_
->registry());
81 suggestions_store_
.reset(new SuggestionsStore(pref_service_
.get()));
83 base::SimpleTestClock
* test_clock(new base::SimpleTestClock());
84 current_time
= base::Time::FromInternalValue(13063394337546738);
85 test_clock
->SetNow(current_time
);
86 suggestions_store_
->SetClockForTesting(scoped_ptr
<base::Clock
>(test_clock
));
90 scoped_ptr
<user_prefs::TestingPrefServiceSyncable
> pref_service_
;
91 scoped_ptr
<SuggestionsStore
> suggestions_store_
;
92 base::Time current_time
;
94 DISALLOW_COPY_AND_ASSIGN(SuggestionsStoreTest
);
97 // Tests LoadSuggestions function to filter expired suggestions.
98 TEST_F(SuggestionsStoreTest
, LoadAllExpired
) {
99 SuggestionsProfile suggestions
=
100 CreateTestSuggestionsProfileWithExpiry(current_time
, 5, 0);
101 SuggestionsProfile filtered_suggestions
;
103 // Store and load. Expired suggestions should not be loaded.
104 EXPECT_TRUE(suggestions_store_
->StoreSuggestions(suggestions
));
105 EXPECT_FALSE(suggestions_store_
->LoadSuggestions(&filtered_suggestions
));
106 EXPECT_EQ(0, filtered_suggestions
.suggestions_size());
109 // Tests LoadSuggestions function to filter expired suggestions.
110 TEST_F(SuggestionsStoreTest
, LoadValidAndExpired
) {
111 SuggestionsProfile suggestions
=
112 CreateTestSuggestionsProfileWithExpiry(current_time
, 5, 3);
113 SuggestionsProfile filtered_suggestions
;
115 // Store and load. Expired suggestions should not be loaded.
116 EXPECT_TRUE(suggestions_store_
->StoreSuggestions(suggestions
));
117 EXPECT_TRUE(suggestions_store_
->LoadSuggestions(&filtered_suggestions
));
118 EXPECT_EQ(3, filtered_suggestions
.suggestions_size());
121 // Tests LoadSuggestions function to filter expired suggestions.
122 TEST_F(SuggestionsStoreTest
, CheckStoreAfterLoadExpired
) {
123 SuggestionsProfile suggestions
=
124 CreateTestSuggestionsProfileWithExpiry(current_time
, 5, 3);
125 SuggestionsProfile filtered_suggestions
;
127 // Store and load. Expired suggestions should not be loaded.
128 EXPECT_TRUE(suggestions_store_
->StoreSuggestions(suggestions
));
129 EXPECT_TRUE(suggestions_store_
->LoadSuggestions(&filtered_suggestions
));
131 SuggestionsProfile loaded_suggestions
;
132 EXPECT_TRUE(suggestions_store_
->LoadSuggestions(&loaded_suggestions
));
133 EXPECT_EQ(3, loaded_suggestions
.suggestions_size());
134 ValidateSuggestions(filtered_suggestions
, loaded_suggestions
);
137 TEST_F(SuggestionsStoreTest
, LoadStoreClear
) {
138 const SuggestionsProfile suggestions
= CreateTestSuggestions();
139 const SuggestionsProfile empty_suggestions
;
140 SuggestionsProfile recovered_suggestions
;
142 // Attempt to load when prefs are empty.
143 EXPECT_FALSE(suggestions_store_
->LoadSuggestions(&recovered_suggestions
));
144 ValidateSuggestions(empty_suggestions
, recovered_suggestions
);
146 // Store then reload.
147 EXPECT_TRUE(suggestions_store_
->StoreSuggestions(suggestions
));
148 EXPECT_TRUE(suggestions_store_
->LoadSuggestions(&recovered_suggestions
));
149 ValidateSuggestions(suggestions
, recovered_suggestions
);
152 suggestions_store_
->ClearSuggestions();
153 EXPECT_FALSE(suggestions_store_
->LoadSuggestions(&recovered_suggestions
));
154 ValidateSuggestions(empty_suggestions
, recovered_suggestions
);
157 } // namespace suggestions