Chromium Code Reviews| 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..52f6c96056c1016a559e8b7102a78e20dac0a0c1 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 time_gap_usec = 1000; |
|
Mathieu
2014/08/04 14:39:11
check naming style for constants
gayane -on leave until 09-2017
2014/08/04 16:34:59
Done.
|
| + |
| +void AddSuggestion(SuggestionsProfile* suggestions, const char *title, |
| + const char *url, int64 expiry_ts = 0) { |
|
Mathieu
2014/08/04 14:39:11
indent one more space
Mathieu
2014/08/04 14:39:11
Hmm, I don't think I've ever seen default values i
gayane -on leave until 09-2017
2014/08/04 16:34:59
Done.
gayane -on leave until 09-2017
2014/08/04 16:34:59
Done.
|
| + ChromeSuggestion* suggestion = suggestions->add_suggestions(); |
| + suggestion->set_url(title); |
| + suggestion->set_title(url); |
| + if (expiry_ts != 0) { |
|
Mathieu
2014/08/04 14:39:11
if (expiry_ts) {
...
}
But then again you may n
gayane -on leave until 09-2017
2014/08/04 16:34:59
Done.
|
| + suggestion->set_expiry_ts(expiry_ts); |
| + } |
| +} |
| + |
| SuggestionsProfile CreateTestSuggestions() { |
| SuggestionsProfile suggestions; |
| - ChromeSuggestion* suggestion = suggestions.add_suggestions(); |
| - suggestion->set_url(kTestUrl); |
| - suggestion->set_title(kTestTitle); |
| + AddSuggestion(&suggestions, kTestTitle, kTestUrl); |
| + return suggestions; |
| +} |
| + |
| + |
| +SuggestionsProfile CreateTestSuggestionsProfileWithExpiry(int expired_count, |
| + int valid_count) { |
|
Mathieu
2014/08/04 14:39:11
indent 1 more space
gayane -on leave until 09-2017
2014/08/04 16:34:59
Done.
|
| + int64 now_usec = (base::Time::NowFromSystemTime() - base::Time::UnixEpoch()) |
| + .ToInternalValue(); |
| + srand(7); // Constant seed for rand() function. |
| + const int64 offset_limit_usec = 30 * base::Time::kMicrosecondsPerDay; |
|
Mathieu
2014/08/04 14:39:11
kConstantName
gayane -on leave until 09-2017
2014/08/04 16:34:59
removed const
|
| + SuggestionsProfile suggestions; |
| + for (int i = 0; i < valid_count; i++){ |
| + int64 offset_usec = rand() % offset_limit_usec + time_gap_usec; |
| + AddSuggestion(&suggestions, kTestTitle, kTestUrl, now_usec + offset_usec); |
| + } |
| + for (int i = 0; i < expired_count; i++){ |
| + int64 offset_usec = rand() % offset_limit_usec - time_gap_usec; |
| + 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()); |