| 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/blacklist_store.h" | 5 #include "components/suggestions/blacklist_store.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 user_prefs::TestingPrefServiceSyncable* pref_service() { | 62 user_prefs::TestingPrefServiceSyncable* pref_service() { |
| 63 return pref_service_.get(); | 63 return pref_service_.get(); |
| 64 } | 64 } |
| 65 | 65 |
| 66 private: | 66 private: |
| 67 scoped_ptr<user_prefs::TestingPrefServiceSyncable> pref_service_; | 67 scoped_ptr<user_prefs::TestingPrefServiceSyncable> pref_service_; |
| 68 | 68 |
| 69 DISALLOW_COPY_AND_ASSIGN(BlacklistStoreTest); | 69 DISALLOW_COPY_AND_ASSIGN(BlacklistStoreTest); |
| 70 }; | 70 }; |
| 71 | 71 |
| 72 // Tests adding, removing to the blacklist and filtering. |
| 72 TEST_F(BlacklistStoreTest, BasicInteractions) { | 73 TEST_F(BlacklistStoreTest, BasicInteractions) { |
| 73 BlacklistStore blacklist_store(pref_service()); | 74 BlacklistStore blacklist_store(pref_service()); |
| 74 | 75 |
| 75 // Create suggestions with A, B and C. C and D will be added to the blacklist. | 76 // Create suggestions with A, B and C. C and D will be added to the blacklist. |
| 76 std::set<std::string> suggested_urls; | 77 std::set<std::string> suggested_urls; |
| 77 suggested_urls.insert(kTestUrlA); | 78 suggested_urls.insert(kTestUrlA); |
| 78 suggested_urls.insert(kTestUrlB); | 79 suggested_urls.insert(kTestUrlB); |
| 79 const SuggestionsProfile suggestions_filtered = | 80 const SuggestionsProfile suggestions_filtered = |
| 80 CreateSuggestions(suggested_urls); | 81 CreateSuggestions(suggested_urls); |
| 81 suggested_urls.insert(kTestUrlC); | 82 suggested_urls.insert(kTestUrlC); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 101 blacklist_store.FilterSuggestions(&suggestions); | 102 blacklist_store.FilterSuggestions(&suggestions); |
| 102 ValidateSuggestions(original_suggestions, suggestions); | 103 ValidateSuggestions(original_suggestions, suggestions); |
| 103 } | 104 } |
| 104 | 105 |
| 105 TEST_F(BlacklistStoreTest, BlacklistTwiceSuceeds) { | 106 TEST_F(BlacklistStoreTest, BlacklistTwiceSuceeds) { |
| 106 BlacklistStore blacklist_store(pref_service()); | 107 BlacklistStore blacklist_store(pref_service()); |
| 107 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlA))); | 108 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlA))); |
| 108 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlA))); | 109 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlA))); |
| 109 } | 110 } |
| 110 | 111 |
| 111 TEST_F(BlacklistStoreTest, RemoveUnknownUrlSucceeds) { | 112 TEST_F(BlacklistStoreTest, RemoveUnknownUrlFails) { |
| 112 BlacklistStore blacklist_store(pref_service()); | 113 BlacklistStore blacklist_store(pref_service()); |
| 113 EXPECT_TRUE(blacklist_store.RemoveUrl(GURL(kTestUrlA))); | 114 EXPECT_FALSE(blacklist_store.RemoveUrl(GURL(kTestUrlA))); |
| 114 } | 115 } |
| 115 | 116 |
| 116 TEST_F(BlacklistStoreTest, GetFirstUrlFromBlacklist) { | 117 TEST_F(BlacklistStoreTest, TestGetTimeUntilReadyForUpload) { |
| 117 BlacklistStore blacklist_store(pref_service()); | 118 // Tests assumes completion within 1 hour. |
| 119 base::TimeDelta upload_delay = base::TimeDelta::FromHours(1); |
| 120 base::TimeDelta no_delay = base::TimeDelta::FromHours(0); |
| 121 scoped_ptr<BlacklistStore> blacklist_store( |
| 122 new BlacklistStore(pref_service(), upload_delay)); |
| 123 base::TimeDelta candidate_delta; |
| 118 | 124 |
| 119 // Expect GetFirstUrlFromBlacklist fails when blacklist empty. | 125 // Blacklist is empty. |
| 126 EXPECT_FALSE(blacklist_store->GetTimeUntilReadyForUpload(&candidate_delta)); |
| 127 EXPECT_FALSE(blacklist_store->GetTimeUntilURLReadyForUpload( |
| 128 GURL(kTestUrlA), &candidate_delta)); |
| 129 |
| 130 // Blacklist contains kTestUrlA. |
| 131 EXPECT_TRUE(blacklist_store->BlacklistUrl(GURL(kTestUrlA))); |
| 132 candidate_delta = upload_delay + base::TimeDelta::FromDays(1); |
| 133 EXPECT_TRUE(blacklist_store->GetTimeUntilReadyForUpload(&candidate_delta)); |
| 134 EXPECT_LE(candidate_delta, upload_delay); |
| 135 EXPECT_GE(candidate_delta, no_delay); |
| 136 candidate_delta = upload_delay + base::TimeDelta::FromDays(1); |
| 137 EXPECT_TRUE(blacklist_store->GetTimeUntilURLReadyForUpload( |
| 138 GURL(kTestUrlA), &candidate_delta)); |
| 139 EXPECT_LE(candidate_delta, upload_delay); |
| 140 EXPECT_GE(candidate_delta, no_delay); |
| 141 EXPECT_FALSE(blacklist_store->GetTimeUntilURLReadyForUpload( |
| 142 GURL(kTestUrlB), &candidate_delta)); |
| 143 |
| 144 // There should be no candidate for upload since the upload delay is 1 day. |
| 145 // Note: this is a test that relies on timing. |
| 120 GURL retrieved; | 146 GURL retrieved; |
| 121 EXPECT_FALSE(blacklist_store.GetFirstUrlFromBlacklist(&retrieved)); | 147 EXPECT_FALSE(blacklist_store->GetCandidateForUpload(&retrieved)); |
| 122 | 148 |
| 123 // Blacklist A and B. | 149 // Same, but with an upload delay of 0. |
| 150 blacklist_store.reset(new BlacklistStore(pref_service(), no_delay)); |
| 151 EXPECT_TRUE(blacklist_store->BlacklistUrl(GURL(kTestUrlA))); |
| 152 candidate_delta = no_delay + base::TimeDelta::FromDays(1); |
| 153 EXPECT_TRUE(blacklist_store->GetTimeUntilReadyForUpload(&candidate_delta)); |
| 154 EXPECT_EQ(candidate_delta, no_delay); |
| 155 candidate_delta = no_delay + base::TimeDelta::FromDays(1); |
| 156 EXPECT_TRUE(blacklist_store->GetTimeUntilURLReadyForUpload( |
| 157 GURL(kTestUrlA), &candidate_delta)); |
| 158 EXPECT_EQ(candidate_delta, no_delay); |
| 159 } |
| 160 |
| 161 TEST_F(BlacklistStoreTest, GetCandidateForUpload) { |
| 162 BlacklistStore blacklist_store(pref_service(), base::TimeDelta::FromDays(0)); |
| 163 // Empty blacklist. |
| 164 GURL retrieved; |
| 165 EXPECT_FALSE(blacklist_store.GetCandidateForUpload(&retrieved)); |
| 166 |
| 167 // Blacklist A and B. Expect to retrieve A or B. |
| 124 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlA))); | 168 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlA))); |
| 125 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlB))); | 169 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlB))); |
| 126 | 170 EXPECT_TRUE(blacklist_store.GetCandidateForUpload(&retrieved)); |
| 127 // Expect to retrieve A or B. | |
| 128 EXPECT_TRUE(blacklist_store.GetFirstUrlFromBlacklist(&retrieved)); | |
| 129 std::string retrieved_string = retrieved.spec(); | 171 std::string retrieved_string = retrieved.spec(); |
| 130 EXPECT_TRUE(retrieved_string == std::string(kTestUrlA) || | 172 EXPECT_TRUE(retrieved_string == std::string(kTestUrlA) || |
| 131 retrieved_string == std::string(kTestUrlB)); | 173 retrieved_string == std::string(kTestUrlB)); |
| 132 } | 174 } |
| 133 | 175 |
| 134 TEST_F(BlacklistStoreTest, LogsBlacklistSize) { | 176 TEST_F(BlacklistStoreTest, LogsBlacklistSize) { |
| 135 base::HistogramTester histogram_tester; | 177 base::HistogramTester histogram_tester; |
| 136 | 178 |
| 137 // Create a first store - blacklist is empty at this point. | 179 // Create a first store - blacklist is empty at this point. |
| 138 scoped_ptr<BlacklistStore> blacklist_store( | 180 scoped_ptr<BlacklistStore> blacklist_store( |
| 139 new BlacklistStore(pref_service())); | 181 new BlacklistStore(pref_service())); |
| 140 | |
| 141 histogram_tester.ExpectTotalCount("Suggestions.LocalBlacklistSize", 1); | 182 histogram_tester.ExpectTotalCount("Suggestions.LocalBlacklistSize", 1); |
| 142 histogram_tester.ExpectUniqueSample("Suggestions.LocalBlacklistSize", 0, 1); | 183 histogram_tester.ExpectUniqueSample("Suggestions.LocalBlacklistSize", 0, 1); |
| 143 | 184 |
| 144 // Add some content to the blacklist. | 185 // Add some content to the blacklist. |
| 145 EXPECT_TRUE(blacklist_store->BlacklistUrl(GURL(kTestUrlA))); | 186 EXPECT_TRUE(blacklist_store->BlacklistUrl(GURL(kTestUrlA))); |
| 146 EXPECT_TRUE(blacklist_store->BlacklistUrl(GURL(kTestUrlB))); | 187 EXPECT_TRUE(blacklist_store->BlacklistUrl(GURL(kTestUrlB))); |
| 147 | 188 |
| 148 // Create a new BlacklistStore and verify the counts. | 189 // Create a new BlacklistStore and verify the counts. |
| 149 blacklist_store.reset(new BlacklistStore(pref_service())); | 190 blacklist_store.reset(new BlacklistStore(pref_service())); |
| 150 | |
| 151 histogram_tester.ExpectTotalCount("Suggestions.LocalBlacklistSize", 2); | 191 histogram_tester.ExpectTotalCount("Suggestions.LocalBlacklistSize", 2); |
| 152 histogram_tester.ExpectBucketCount("Suggestions.LocalBlacklistSize", 0, 1); | 192 histogram_tester.ExpectBucketCount("Suggestions.LocalBlacklistSize", 0, 1); |
| 153 histogram_tester.ExpectBucketCount("Suggestions.LocalBlacklistSize", 2, 1); | 193 histogram_tester.ExpectBucketCount("Suggestions.LocalBlacklistSize", 2, 1); |
| 154 } | 194 } |
| 155 | 195 |
| 156 } // namespace suggestions | 196 } // namespace suggestions |
| OLD | NEW |