| Index: components/suggestions/suggestions_store_unittest.cc
|
| diff --git a/components/suggestions/suggestions_store_unittest.cc b/components/suggestions/suggestions_store_unittest.cc
|
| index 08930133f01adbb6a669c99c446ff4394d7407e1..d79f1f4b35ab22b1db96e8d4a00b28acc058a82f 100644
|
| --- a/components/suggestions/suggestions_store_unittest.cc
|
| +++ b/components/suggestions/suggestions_store_unittest.cc
|
| @@ -4,6 +4,8 @@
|
|
|
| #include "components/suggestions/suggestions_store.h"
|
|
|
| +#include "base/time/time.h"
|
| +
|
| #include "components/pref_registry/testing_pref_service_syncable.h"
|
| #include "components/suggestions/proto/suggestions.pb.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
| @@ -17,11 +19,40 @@ namespace {
|
| const char kTestTitle[] = "Foo site";
|
| const char kTestUrl[] = "http://foo.com/";
|
|
|
| +const int kTimeGapUsec = 1000;
|
| +
|
| +void AddSuggestion(SuggestionsProfile* suggestions, const char *title,
|
| + const char *url, int64 expiry_ts) {
|
| + ChromeSuggestion* suggestion = suggestions->add_suggestions();
|
| + suggestion->set_url(title);
|
| + suggestion->set_title(url);
|
| + suggestion->set_expiry_ts(expiry_ts);
|
| +}
|
| +
|
| SuggestionsProfile CreateTestSuggestions() {
|
| SuggestionsProfile suggestions;
|
| ChromeSuggestion* suggestion = suggestions.add_suggestions();
|
| - suggestion->set_url(kTestUrl);
|
| - suggestion->set_title(kTestTitle);
|
| + suggestion->set_url(kTestTitle);
|
| + suggestion->set_title(kTestUrl);
|
| + return suggestions;
|
| +}
|
| +
|
| +
|
| +SuggestionsProfile CreateTestSuggestionsProfileWithExpiry(int expired_count,
|
| + int valid_count) {
|
| + int64 now_usec = (base::Time::NowFromSystemTime() - base::Time::UnixEpoch())
|
| + .ToInternalValue();
|
| + srand(7); // Constant seed for rand() function.
|
| + int64 offset_limit_usec = 30 * base::Time::kMicrosecondsPerDay;
|
| + SuggestionsProfile suggestions;
|
| + for (int i = 0; i < valid_count; i++){
|
| + int64 offset_usec = rand() % offset_limit_usec + kTimeGapUsec;
|
| + AddSuggestion(&suggestions, kTestTitle, kTestUrl, now_usec + offset_usec);
|
| + }
|
| + for (int i = 0; i < expired_count; i++){
|
| + int64 offset_usec = rand() % offset_limit_usec - kTimeGapUsec;
|
| + AddSuggestion(&suggestions, kTestTitle, kTestUrl, now_usec - offset_usec);
|
| + }
|
| return suggestions;
|
| }
|
|
|
| @@ -31,6 +62,8 @@ void ValidateSuggestions(const SuggestionsProfile& expected,
|
| for (int i = 0; i < expected.suggestions_size(); ++i) {
|
| EXPECT_EQ(expected.suggestions(i).url(), actual.suggestions(i).url());
|
| EXPECT_EQ(expected.suggestions(i).title(), actual.suggestions(i).title());
|
| + EXPECT_EQ(expected.suggestions(i).expiry_ts(),
|
| + actual.suggestions(i).expiry_ts());
|
| EXPECT_EQ(expected.suggestions(i).favicon_url(),
|
| actual.suggestions(i).favicon_url());
|
| EXPECT_EQ(expected.suggestions(i).thumbnail(),
|
| @@ -40,6 +73,56 @@ void ValidateSuggestions(const SuggestionsProfile& expected,
|
|
|
| } // namespace
|
|
|
| +// Tests LoadSuggestions function to filter expired suggestions.
|
| +TEST(SuggestionsStoreTest, LoadAllExpired) {
|
| + TestingPrefServiceSyncable prefs;
|
| + SuggestionsStore::RegisterProfilePrefs(prefs.registry());
|
| + SuggestionsStore suggestions_store(&prefs);
|
| +
|
| + SuggestionsProfile suggestions = CreateTestSuggestionsProfileWithExpiry(5, 0);
|
| + SuggestionsProfile filtered_suggestions;
|
| +
|
| + // store and load. expired suggestions should not be loaded.
|
| + EXPECT_TRUE(suggestions_store.StoreSuggestions(suggestions));
|
| + EXPECT_FALSE(suggestions_store.LoadSuggestions(&filtered_suggestions));
|
| + EXPECT_EQ(0, filtered_suggestions.suggestions_size());
|
| +}
|
| +
|
| +// Tests LoadSuggestions function to filter expired suggestions.
|
| +TEST(SuggestionsStoreTest, LoadValidAndExpired) {
|
| + TestingPrefServiceSyncable prefs;
|
| + SuggestionsStore::RegisterProfilePrefs(prefs.registry());
|
| + SuggestionsStore suggestions_store(&prefs);
|
| +
|
| + SuggestionsProfile suggestions = CreateTestSuggestionsProfileWithExpiry(5, 3);
|
| + SuggestionsProfile filtered_suggestions;
|
| +
|
| + // store and load. expired suggestions should not be loaded.
|
| + EXPECT_TRUE(suggestions_store.StoreSuggestions(suggestions));
|
| + EXPECT_TRUE(suggestions_store.LoadSuggestions(&filtered_suggestions));
|
| + EXPECT_EQ(3, filtered_suggestions.suggestions_size());
|
| +}
|
| +
|
| +// Tests LoadSuggestions function to filter expired suggestions.
|
| +TEST(SuggestionsStoreTest, CheckStoreAfterLoadExpired) {
|
| + TestingPrefServiceSyncable prefs;
|
| + SuggestionsStore::RegisterProfilePrefs(prefs.registry());
|
| + SuggestionsStore suggestions_store(&prefs);
|
| +
|
| + SuggestionsProfile suggestions = CreateTestSuggestionsProfileWithExpiry(5, 3);
|
| + SuggestionsProfile filtered_suggestions;
|
| +
|
| + // store and load. expired suggestions should not be loaded.
|
| + EXPECT_TRUE(suggestions_store.StoreSuggestions(suggestions));
|
| + EXPECT_TRUE(suggestions_store.LoadSuggestions(&filtered_suggestions));
|
| + EXPECT_EQ(3, filtered_suggestions.suggestions_size());
|
| +
|
| + SuggestionsProfile loaded_suggestions;
|
| + EXPECT_TRUE(suggestions_store.LoadSuggestions(&loaded_suggestions));
|
| + EXPECT_EQ(3, loaded_suggestions.suggestions_size());
|
| + ValidateSuggestions(filtered_suggestions, loaded_suggestions);
|
| +}
|
| +
|
| TEST(SuggestionsStoreTest, LoadStoreClear) {
|
| TestingPrefServiceSyncable prefs;
|
| SuggestionsStore::RegisterProfilePrefs(prefs.registry());
|
|
|