| 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 "chrome/browser/search/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" |
| 11 #include "base/metrics/histogram_samples.h" | 11 #include "base/metrics/histogram_samples.h" |
| 12 #include "base/metrics/statistics_recorder.h" | 12 #include "base/metrics/statistics_recorder.h" |
| 13 #include "base/test/statistics_delta_reader.h" | 13 #include "base/test/statistics_delta_reader.h" |
| 14 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h" | |
| 15 #include "components/pref_registry/testing_pref_service_syncable.h" | 14 #include "components/pref_registry/testing_pref_service_syncable.h" |
| 15 #include "components/suggestions/proto/suggestions.pb.h" |
| 16 #include "content/public/test/test_browser_thread_bundle.h" | 16 #include "content/public/test/test_browser_thread_bundle.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 18 |
| 19 using user_prefs::TestingPrefServiceSyncable; | 19 using user_prefs::TestingPrefServiceSyncable; |
| 20 | 20 |
| 21 namespace suggestions { | 21 namespace suggestions { |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 const char kTestUrlA[] = "http://aaa.com/"; | 25 const char kTestUrlA[] = "http://aaa.com/"; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 45 EXPECT_EQ(expected.suggestions(i).title(), actual.suggestions(i).title()); | 45 EXPECT_EQ(expected.suggestions(i).title(), actual.suggestions(i).title()); |
| 46 EXPECT_EQ(expected.suggestions(i).favicon_url(), | 46 EXPECT_EQ(expected.suggestions(i).favicon_url(), |
| 47 actual.suggestions(i).favicon_url()); | 47 actual.suggestions(i).favicon_url()); |
| 48 EXPECT_EQ(expected.suggestions(i).thumbnail(), | 48 EXPECT_EQ(expected.suggestions(i).thumbnail(), |
| 49 actual.suggestions(i).thumbnail()); | 49 actual.suggestions(i).thumbnail()); |
| 50 } | 50 } |
| 51 } | 51 } |
| 52 | 52 |
| 53 } // namespace | 53 } // namespace |
| 54 | 54 |
| 55 TEST(BlacklistStoreTest, BasicInteractions) { | 55 class BlacklistStoreTest : public testing::Test { |
| 56 TestingPrefServiceSyncable prefs; | 56 public: |
| 57 BlacklistStore::RegisterProfilePrefs(prefs.registry()); | 57 BlacklistStoreTest() |
| 58 BlacklistStore blacklist_store(&prefs); | 58 : pref_service_(new user_prefs::TestingPrefServiceSyncable) {} |
| 59 |
| 60 virtual void SetUp() OVERRIDE { |
| 61 BlacklistStore::RegisterProfilePrefs(pref_service()->registry()); |
| 62 } |
| 63 |
| 64 user_prefs::TestingPrefServiceSyncable* pref_service() { |
| 65 return pref_service_.get(); |
| 66 } |
| 67 |
| 68 private: |
| 69 scoped_ptr<user_prefs::TestingPrefServiceSyncable> pref_service_; |
| 70 |
| 71 DISALLOW_COPY_AND_ASSIGN(BlacklistStoreTest); |
| 72 }; |
| 73 |
| 74 TEST_F(BlacklistStoreTest, BasicInteractions) { |
| 75 BlacklistStore blacklist_store(pref_service()); |
| 59 | 76 |
| 60 // Create suggestions with A, B and C. C and D will be added to the blacklist. | 77 // Create suggestions with A, B and C. C and D will be added to the blacklist. |
| 61 std::set<std::string> suggested_urls; | 78 std::set<std::string> suggested_urls; |
| 62 suggested_urls.insert(kTestUrlA); | 79 suggested_urls.insert(kTestUrlA); |
| 63 suggested_urls.insert(kTestUrlB); | 80 suggested_urls.insert(kTestUrlB); |
| 64 const SuggestionsProfile suggestions_filtered = | 81 const SuggestionsProfile suggestions_filtered = |
| 65 CreateSuggestions(suggested_urls); | 82 CreateSuggestions(suggested_urls); |
| 66 suggested_urls.insert(kTestUrlC); | 83 suggested_urls.insert(kTestUrlC); |
| 67 const SuggestionsProfile original_suggestions = | 84 const SuggestionsProfile original_suggestions = |
| 68 CreateSuggestions(suggested_urls); | 85 CreateSuggestions(suggested_urls); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 80 blacklist_store.FilterSuggestions(&suggestions); | 97 blacklist_store.FilterSuggestions(&suggestions); |
| 81 ValidateSuggestions(suggestions_filtered, suggestions); | 98 ValidateSuggestions(suggestions_filtered, suggestions); |
| 82 | 99 |
| 83 // Remove C from the blacklist and filter. | 100 // Remove C from the blacklist and filter. |
| 84 suggestions.CopyFrom(original_suggestions); | 101 suggestions.CopyFrom(original_suggestions); |
| 85 EXPECT_TRUE(blacklist_store.RemoveUrl(GURL(kTestUrlC))); | 102 EXPECT_TRUE(blacklist_store.RemoveUrl(GURL(kTestUrlC))); |
| 86 blacklist_store.FilterSuggestions(&suggestions); | 103 blacklist_store.FilterSuggestions(&suggestions); |
| 87 ValidateSuggestions(original_suggestions, suggestions); | 104 ValidateSuggestions(original_suggestions, suggestions); |
| 88 } | 105 } |
| 89 | 106 |
| 90 TEST(BlacklistStoreTest, BlacklistTwiceSuceeds) { | 107 TEST_F(BlacklistStoreTest, BlacklistTwiceSuceeds) { |
| 91 TestingPrefServiceSyncable prefs; | 108 BlacklistStore blacklist_store(pref_service()); |
| 92 BlacklistStore::RegisterProfilePrefs(prefs.registry()); | |
| 93 BlacklistStore blacklist_store(&prefs); | |
| 94 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlA))); | 109 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlA))); |
| 95 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlA))); | 110 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlA))); |
| 96 } | 111 } |
| 97 | 112 |
| 98 TEST(BlacklistStoreTest, RemoveUnknownUrlSucceeds) { | 113 TEST_F(BlacklistStoreTest, RemoveUnknownUrlSucceeds) { |
| 99 TestingPrefServiceSyncable prefs; | 114 BlacklistStore blacklist_store(pref_service()); |
| 100 BlacklistStore::RegisterProfilePrefs(prefs.registry()); | |
| 101 BlacklistStore blacklist_store(&prefs); | |
| 102 EXPECT_TRUE(blacklist_store.RemoveUrl(GURL(kTestUrlA))); | 115 EXPECT_TRUE(blacklist_store.RemoveUrl(GURL(kTestUrlA))); |
| 103 } | 116 } |
| 104 | 117 |
| 105 TEST(BlacklistStoreTest, GetFirstUrlFromBlacklist) { | 118 TEST_F(BlacklistStoreTest, GetFirstUrlFromBlacklist) { |
| 106 TestingPrefServiceSyncable prefs; | 119 BlacklistStore blacklist_store(pref_service()); |
| 107 BlacklistStore::RegisterProfilePrefs(prefs.registry()); | |
| 108 BlacklistStore blacklist_store(&prefs); | |
| 109 | 120 |
| 110 // Expect GetFirstUrlFromBlacklist fails when blacklist empty. | 121 // Expect GetFirstUrlFromBlacklist fails when blacklist empty. |
| 111 GURL retrieved; | 122 GURL retrieved; |
| 112 EXPECT_FALSE(blacklist_store.GetFirstUrlFromBlacklist(&retrieved)); | 123 EXPECT_FALSE(blacklist_store.GetFirstUrlFromBlacklist(&retrieved)); |
| 113 | 124 |
| 114 // Blacklist A and B. | 125 // Blacklist A and B. |
| 115 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlA))); | 126 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlA))); |
| 116 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlB))); | 127 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlB))); |
| 117 | 128 |
| 118 // Expect to retrieve A or B. | 129 // Expect to retrieve A or B. |
| 119 EXPECT_TRUE(blacklist_store.GetFirstUrlFromBlacklist(&retrieved)); | 130 EXPECT_TRUE(blacklist_store.GetFirstUrlFromBlacklist(&retrieved)); |
| 120 std::string retrieved_string = retrieved.spec(); | 131 std::string retrieved_string = retrieved.spec(); |
| 121 EXPECT_TRUE(retrieved_string == std::string(kTestUrlA) || | 132 EXPECT_TRUE(retrieved_string == std::string(kTestUrlA) || |
| 122 retrieved_string == std::string(kTestUrlB)); | 133 retrieved_string == std::string(kTestUrlB)); |
| 123 } | 134 } |
| 124 | 135 |
| 125 TEST(BlacklistStoreLogTest, LogsBlacklistSize) { | 136 TEST_F(BlacklistStoreTest, LogsBlacklistSize) { |
| 137 base::StatisticsDeltaReader statistics_delta_reader; |
| 126 content::TestBrowserThreadBundle bundle; | 138 content::TestBrowserThreadBundle bundle; |
| 127 base::StatisticsDeltaReader statistics_delta_reader; | |
| 128 | 139 |
| 129 // Create a first store - blacklist is empty at this point. | 140 // Create a first store - blacklist is empty at this point. |
| 130 TestingPrefServiceSyncable prefs; | 141 scoped_ptr<BlacklistStore> blacklist_store( |
| 131 BlacklistStore::RegisterProfilePrefs(prefs.registry()); | 142 new BlacklistStore(pref_service())); |
| 132 scoped_ptr<BlacklistStore> blacklist_store(new BlacklistStore(&prefs)); | |
| 133 scoped_ptr<base::HistogramSamples> samples( | 143 scoped_ptr<base::HistogramSamples> samples( |
| 134 statistics_delta_reader.GetHistogramSamplesSinceCreation( | 144 statistics_delta_reader.GetHistogramSamplesSinceCreation( |
| 135 "Suggestions.LocalBlacklistSize")); | 145 "Suggestions.LocalBlacklistSize")); |
| 136 EXPECT_EQ(1, samples->TotalCount()); | 146 EXPECT_EQ(1, samples->TotalCount()); |
| 137 EXPECT_EQ(1, samples->GetCount(0)); | 147 EXPECT_EQ(1, samples->GetCount(0)); |
| 138 | 148 |
| 139 // Add some content to the blacklist. | 149 // Add some content to the blacklist. |
| 140 EXPECT_TRUE(blacklist_store->BlacklistUrl(GURL(kTestUrlA))); | 150 EXPECT_TRUE(blacklist_store->BlacklistUrl(GURL(kTestUrlA))); |
| 141 EXPECT_TRUE(blacklist_store->BlacklistUrl(GURL(kTestUrlB))); | 151 EXPECT_TRUE(blacklist_store->BlacklistUrl(GURL(kTestUrlB))); |
| 142 | 152 |
| 143 // Create a new BlacklistStore and verify the counts. | 153 // Create a new BlacklistStore and verify the counts. |
| 144 blacklist_store.reset(new BlacklistStore(&prefs)); | 154 blacklist_store.reset(new BlacklistStore(pref_service())); |
| 145 samples = statistics_delta_reader.GetHistogramSamplesSinceCreation( | 155 samples = statistics_delta_reader.GetHistogramSamplesSinceCreation( |
| 146 "Suggestions.LocalBlacklistSize"); | 156 "Suggestions.LocalBlacklistSize"); |
| 147 EXPECT_EQ(2, samples->TotalCount()); | 157 EXPECT_EQ(2, samples->TotalCount()); |
| 148 EXPECT_EQ(1, samples->GetCount(0)); | 158 EXPECT_EQ(1, samples->GetCount(0)); |
| 149 EXPECT_EQ(1, samples->GetCount(2)); | 159 EXPECT_EQ(1, samples->GetCount(2)); |
| 150 } | 160 } |
| 151 | 161 |
| 152 } // namespace suggestions | 162 } // namespace suggestions |
| OLD | NEW |