Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/suggestions/suggestions_store.h" | 5 #include "components/suggestions/suggestions_store.h" |
| 6 | 6 |
| 7 #include "base/test/simple_test_clock.h" | |
| 7 #include "base/time/time.h" | 8 #include "base/time/time.h" |
| 8 #include "components/pref_registry/testing_pref_service_syncable.h" | 9 #include "components/pref_registry/testing_pref_service_syncable.h" |
| 9 #include "components/suggestions/proto/suggestions.pb.h" | 10 #include "components/suggestions/proto/suggestions.pb.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 12 |
| 12 using user_prefs::TestingPrefServiceSyncable; | 13 using user_prefs::TestingPrefServiceSyncable; |
| 13 | 14 |
| 14 namespace suggestions { | 15 namespace suggestions { |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 27 } | 28 } |
| 28 | 29 |
| 29 SuggestionsProfile CreateTestSuggestions() { | 30 SuggestionsProfile CreateTestSuggestions() { |
| 30 SuggestionsProfile suggestions; | 31 SuggestionsProfile suggestions; |
| 31 ChromeSuggestion* suggestion = suggestions.add_suggestions(); | 32 ChromeSuggestion* suggestion = suggestions.add_suggestions(); |
| 32 suggestion->set_url(kTestTitle); | 33 suggestion->set_url(kTestTitle); |
| 33 suggestion->set_title(kTestUrl); | 34 suggestion->set_title(kTestUrl); |
| 34 return suggestions; | 35 return suggestions; |
| 35 } | 36 } |
| 36 | 37 |
| 37 SuggestionsProfile CreateTestSuggestionsProfileWithExpiry(int expired_count, | 38 SuggestionsProfile CreateTestSuggestionsProfileWithExpiry( |
| 38 int valid_count) { | 39 base::Time expiry_time, int expired_count, int valid_count) { |
| 39 int64 now_usec = (base::Time::NowFromSystemTime() - base::Time::UnixEpoch()) | 40 int64 expiry_usec = (expiry_time - base::Time::UnixEpoch()) |
| 40 .ToInternalValue(); | 41 .ToInternalValue(); |
| 41 int64 offset_usec = 5 * base::Time::kMicrosecondsPerMinute; | 42 int64 offset_usec = 5 * base::Time::kMicrosecondsPerMinute; |
| 42 | 43 |
| 43 SuggestionsProfile suggestions; | 44 SuggestionsProfile suggestions; |
| 44 for (int i = 1; i <= valid_count; i++) | 45 for (int i = 1; i <= valid_count; i++) |
| 45 AddSuggestion(&suggestions, kTestTitle, kTestUrl, | 46 AddSuggestion(&suggestions, kTestTitle, kTestUrl, |
| 46 now_usec + offset_usec * i); | 47 expiry_usec + offset_usec * i); |
| 47 for (int i = 1; i <= expired_count; i++) | 48 for (int i = 1; i <= expired_count; i++) |
| 48 AddSuggestion(&suggestions, kTestTitle, kTestUrl, | 49 AddSuggestion(&suggestions, kTestTitle, kTestUrl, |
| 49 now_usec - offset_usec * i); | 50 expiry_usec - offset_usec * i); |
| 50 | 51 |
| 51 return suggestions; | 52 return suggestions; |
| 52 } | 53 } |
| 53 | 54 |
| 54 void ValidateSuggestions(const SuggestionsProfile& expected, | 55 void ValidateSuggestions(const SuggestionsProfile& expected, |
| 55 const SuggestionsProfile& actual) { | 56 const SuggestionsProfile& actual) { |
| 56 EXPECT_EQ(expected.suggestions_size(), actual.suggestions_size()); | 57 EXPECT_EQ(expected.suggestions_size(), actual.suggestions_size()); |
| 57 for (int i = 0; i < expected.suggestions_size(); ++i) { | 58 for (int i = 0; i < expected.suggestions_size(); ++i) { |
| 58 EXPECT_EQ(expected.suggestions(i).url(), actual.suggestions(i).url()); | 59 EXPECT_EQ(expected.suggestions(i).url(), actual.suggestions(i).url()); |
| 59 EXPECT_EQ(expected.suggestions(i).title(), actual.suggestions(i).title()); | 60 EXPECT_EQ(expected.suggestions(i).title(), actual.suggestions(i).title()); |
| 60 EXPECT_EQ(expected.suggestions(i).expiry_ts(), | 61 EXPECT_EQ(expected.suggestions(i).expiry_ts(), |
| 61 actual.suggestions(i).expiry_ts()); | 62 actual.suggestions(i).expiry_ts()); |
| 62 EXPECT_EQ(expected.suggestions(i).favicon_url(), | 63 EXPECT_EQ(expected.suggestions(i).favicon_url(), |
| 63 actual.suggestions(i).favicon_url()); | 64 actual.suggestions(i).favicon_url()); |
| 64 EXPECT_EQ(expected.suggestions(i).thumbnail(), | 65 EXPECT_EQ(expected.suggestions(i).thumbnail(), |
| 65 actual.suggestions(i).thumbnail()); | 66 actual.suggestions(i).thumbnail()); |
| 66 } | 67 } |
| 67 } | 68 } |
| 68 | 69 |
| 69 } // namespace | 70 } // namespace |
| 70 | 71 |
| 71 class SuggestionsStoreTest : public testing::Test { | 72 class SuggestionsStoreTest : public testing::Test { |
| 72 public: | 73 public: |
| 73 SuggestionsStoreTest() | 74 SuggestionsStoreTest() |
| 74 : pref_service_(new user_prefs::TestingPrefServiceSyncable) {} | 75 : pref_service_(new user_prefs::TestingPrefServiceSyncable) { |
|
Mathieu
2014/12/18 20:05:28
nit: what this change done by a formatting tool?
gayane -on leave until 09-2017
2014/12/18 21:06:49
No, I added a testing code there and then removed,
| |
| 76 } | |
| 75 | 77 |
| 76 void SetUp() override { | 78 void SetUp() override { |
| 77 SuggestionsStore::RegisterProfilePrefs(pref_service_->registry()); | 79 SuggestionsStore::RegisterProfilePrefs(pref_service_->registry()); |
| 78 suggestions_store_.reset(new SuggestionsStore(pref_service_.get())); | 80 suggestions_store_.reset(new SuggestionsStore(pref_service_.get())); |
| 81 | |
| 82 base::SimpleTestClock* test_clock(new base::SimpleTestClock()); | |
| 83 expiry_time = base::Time::FromInternalValue(13063394337546738); | |
| 84 test_clock->SetNow(expiry_time); | |
| 85 suggestions_store_->SetClockForTesting(scoped_ptr<base::Clock>(test_clock)); | |
| 79 } | 86 } |
| 80 | 87 |
| 81 protected: | 88 protected: |
| 82 scoped_ptr<user_prefs::TestingPrefServiceSyncable> pref_service_; | 89 scoped_ptr<user_prefs::TestingPrefServiceSyncable> pref_service_; |
| 83 scoped_ptr<SuggestionsStore> suggestions_store_; | 90 scoped_ptr<SuggestionsStore> suggestions_store_; |
| 91 base::Time expiry_time; | |
| 84 | 92 |
| 85 DISALLOW_COPY_AND_ASSIGN(SuggestionsStoreTest); | 93 DISALLOW_COPY_AND_ASSIGN(SuggestionsStoreTest); |
| 86 }; | 94 }; |
| 87 | 95 |
| 88 // Tests LoadSuggestions function to filter expired suggestions. | 96 // Tests LoadSuggestions function to filter expired suggestions. |
| 89 TEST_F(SuggestionsStoreTest, LoadAllExpired) { | 97 TEST_F(SuggestionsStoreTest, LoadAllExpired) { |
| 90 SuggestionsProfile suggestions = CreateTestSuggestionsProfileWithExpiry(5, 0); | 98 SuggestionsProfile suggestions = CreateTestSuggestionsProfileWithExpiry( |
| 99 expiry_time, 5, 0); | |
| 91 SuggestionsProfile filtered_suggestions; | 100 SuggestionsProfile filtered_suggestions; |
| 92 | 101 |
| 93 // Store and load. Expired suggestions should not be loaded. | 102 // Store and load. Expired suggestions should not be loaded. |
| 94 EXPECT_TRUE(suggestions_store_->StoreSuggestions(suggestions)); | 103 EXPECT_TRUE(suggestions_store_->StoreSuggestions(suggestions)); |
| 95 EXPECT_FALSE(suggestions_store_->LoadSuggestions(&filtered_suggestions)); | 104 EXPECT_FALSE(suggestions_store_->LoadSuggestions(&filtered_suggestions)); |
| 96 EXPECT_EQ(0, filtered_suggestions.suggestions_size()); | 105 EXPECT_EQ(0, filtered_suggestions.suggestions_size()); |
| 97 } | 106 } |
| 98 | 107 |
| 99 // Tests LoadSuggestions function to filter expired suggestions. | 108 // Tests LoadSuggestions function to filter expired suggestions. |
| 100 TEST_F(SuggestionsStoreTest, LoadValidAndExpired) { | 109 TEST_F(SuggestionsStoreTest, LoadValidAndExpired) { |
| 101 SuggestionsProfile suggestions = CreateTestSuggestionsProfileWithExpiry(5, 3); | 110 SuggestionsProfile suggestions = CreateTestSuggestionsProfileWithExpiry( |
| 111 expiry_time, 5, 3); | |
| 102 SuggestionsProfile filtered_suggestions; | 112 SuggestionsProfile filtered_suggestions; |
| 103 | 113 |
| 104 // Store and load. Expired suggestions should not be loaded. | 114 // Store and load. Expired suggestions should not be loaded. |
| 105 EXPECT_TRUE(suggestions_store_->StoreSuggestions(suggestions)); | 115 EXPECT_TRUE(suggestions_store_->StoreSuggestions(suggestions)); |
| 106 EXPECT_TRUE(suggestions_store_->LoadSuggestions(&filtered_suggestions)); | 116 EXPECT_TRUE(suggestions_store_->LoadSuggestions(&filtered_suggestions)); |
| 107 EXPECT_EQ(3, filtered_suggestions.suggestions_size()); | 117 EXPECT_EQ(3, filtered_suggestions.suggestions_size()); |
| 108 } | 118 } |
| 109 | 119 |
| 110 // Tests LoadSuggestions function to filter expired suggestions. | 120 // Tests LoadSuggestions function to filter expired suggestions. |
| 111 TEST_F(SuggestionsStoreTest, CheckStoreAfterLoadExpired) { | 121 TEST_F(SuggestionsStoreTest, CheckStoreAfterLoadExpired) { |
| 112 SuggestionsProfile suggestions = CreateTestSuggestionsProfileWithExpiry(5, 3); | 122 SuggestionsProfile suggestions = CreateTestSuggestionsProfileWithExpiry( |
| 123 expiry_time, 5, 3); | |
| 113 SuggestionsProfile filtered_suggestions; | 124 SuggestionsProfile filtered_suggestions; |
| 114 | 125 |
| 115 // Store and load. Expired suggestions should not be loaded. | 126 // Store and load. Expired suggestions should not be loaded. |
| 116 EXPECT_TRUE(suggestions_store_->StoreSuggestions(suggestions)); | 127 EXPECT_TRUE(suggestions_store_->StoreSuggestions(suggestions)); |
| 117 EXPECT_TRUE(suggestions_store_->LoadSuggestions(&filtered_suggestions)); | 128 EXPECT_TRUE(suggestions_store_->LoadSuggestions(&filtered_suggestions)); |
| 118 | 129 |
| 119 SuggestionsProfile loaded_suggestions; | 130 SuggestionsProfile loaded_suggestions; |
| 120 EXPECT_TRUE(suggestions_store_->LoadSuggestions(&loaded_suggestions)); | 131 EXPECT_TRUE(suggestions_store_->LoadSuggestions(&loaded_suggestions)); |
| 121 EXPECT_EQ(3, loaded_suggestions.suggestions_size()); | 132 EXPECT_EQ(3, loaded_suggestions.suggestions_size()); |
| 122 ValidateSuggestions(filtered_suggestions, loaded_suggestions); | 133 ValidateSuggestions(filtered_suggestions, loaded_suggestions); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 136 EXPECT_TRUE(suggestions_store_->LoadSuggestions(&recovered_suggestions)); | 147 EXPECT_TRUE(suggestions_store_->LoadSuggestions(&recovered_suggestions)); |
| 137 ValidateSuggestions(suggestions, recovered_suggestions); | 148 ValidateSuggestions(suggestions, recovered_suggestions); |
| 138 | 149 |
| 139 // Clear. | 150 // Clear. |
| 140 suggestions_store_->ClearSuggestions(); | 151 suggestions_store_->ClearSuggestions(); |
| 141 EXPECT_FALSE(suggestions_store_->LoadSuggestions(&recovered_suggestions)); | 152 EXPECT_FALSE(suggestions_store_->LoadSuggestions(&recovered_suggestions)); |
| 142 ValidateSuggestions(empty_suggestions, recovered_suggestions); | 153 ValidateSuggestions(empty_suggestions, recovered_suggestions); |
| 143 } | 154 } |
| 144 | 155 |
| 145 } // namespace suggestions | 156 } // namespace suggestions |
| OLD | NEW |