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..65ab6aeb05a00faca12d48e0b58df42215132e9a 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" |
@@ -16,12 +18,39 @@ namespace { |
const char kTestTitle[] = "Foo site"; |
const char kTestUrl[] = "http://foo.com/"; |
+const int kTimeGapUsec = 100000; |
+ |
+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 +60,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,11 +71,67 @@ void ValidateSuggestions(const SuggestionsProfile& expected, |
} // namespace |
-TEST(SuggestionsStoreTest, LoadStoreClear) { |
- TestingPrefServiceSyncable prefs; |
- SuggestionsStore::RegisterProfilePrefs(prefs.registry()); |
- SuggestionsStore suggestions_store(&prefs); |
+class SuggestionsStoreTest : public testing::Test { |
+ public: |
+ SuggestionsStoreTest() |
+ : pref_service_(new user_prefs::TestingPrefServiceSyncable) {} |
+ |
+ virtual void SetUp() OVERRIDE { |
+ SuggestionsStore::RegisterProfilePrefs(pref_service()->registry()); |
+ } |
+ |
+ user_prefs::TestingPrefServiceSyncable* pref_service() { |
+ return pref_service_.get(); |
+ } |
+ |
+ private: |
+ scoped_ptr<user_prefs::TestingPrefServiceSyncable> pref_service_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(SuggestionsStoreTest); |
+}; |
+ |
+// Tests LoadSuggestions function to filter expired suggestions. |
+TEST_F(SuggestionsStoreTest, LoadAllExpired) { |
+ SuggestionsStore suggestions_store(pref_service()); |
+ 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_F(SuggestionsStoreTest, LoadValidAndExpired) { |
+ SuggestionsStore suggestions_store(pref_service()); |
+ 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_F(SuggestionsStoreTest, CheckStoreAfterLoadExpired) { |
+ SuggestionsStore suggestions_store(pref_service()); |
+ 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)); |
+ |
+ SuggestionsProfile loaded_suggestions; |
+ EXPECT_TRUE(suggestions_store.LoadSuggestions(&loaded_suggestions)); |
+ EXPECT_EQ(3, loaded_suggestions.suggestions_size()); |
+ ValidateSuggestions(filtered_suggestions, loaded_suggestions); |
+} |
+TEST_F(SuggestionsStoreTest, LoadStoreClear) { |
+ SuggestionsStore suggestions_store(pref_service()); |
const SuggestionsProfile suggestions = CreateTestSuggestions(); |
const SuggestionsProfile empty_suggestions; |
SuggestionsProfile recovered_suggestions; |