| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/search/suggestions/blacklist_store.h" | |
| 6 | |
| 7 #include <set> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/metrics/histogram_samples.h" | |
| 12 #include "base/metrics/statistics_recorder.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" | |
| 16 #include "content/public/test/test_browser_thread_bundle.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | |
| 18 | |
| 19 using user_prefs::TestingPrefServiceSyncable; | |
| 20 | |
| 21 namespace suggestions { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 const char kTestUrlA[] = "http://aaa.com/"; | |
| 26 const char kTestUrlB[] = "http://bbb.com/"; | |
| 27 const char kTestUrlC[] = "http://ccc.com/"; | |
| 28 const char kTestUrlD[] = "http://ddd.com/"; | |
| 29 | |
| 30 SuggestionsProfile CreateSuggestions(std::set<std::string> urls) { | |
| 31 SuggestionsProfile suggestions; | |
| 32 for (std::set<std::string>::iterator it = urls.begin(); it != urls.end(); | |
| 33 ++it) { | |
| 34 ChromeSuggestion* suggestion = suggestions.add_suggestions(); | |
| 35 suggestion->set_url(*it); | |
| 36 } | |
| 37 return suggestions; | |
| 38 } | |
| 39 | |
| 40 void ValidateSuggestions(const SuggestionsProfile& expected, | |
| 41 const SuggestionsProfile& actual) { | |
| 42 ASSERT_EQ(expected.suggestions_size(), actual.suggestions_size()); | |
| 43 for (int i = 0; i < expected.suggestions_size(); ++i) { | |
| 44 EXPECT_EQ(expected.suggestions(i).url(), actual.suggestions(i).url()); | |
| 45 EXPECT_EQ(expected.suggestions(i).title(), actual.suggestions(i).title()); | |
| 46 EXPECT_EQ(expected.suggestions(i).favicon_url(), | |
| 47 actual.suggestions(i).favicon_url()); | |
| 48 EXPECT_EQ(expected.suggestions(i).thumbnail(), | |
| 49 actual.suggestions(i).thumbnail()); | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 } // namespace | |
| 54 | |
| 55 TEST(BlacklistStoreTest, BasicInteractions) { | |
| 56 TestingPrefServiceSyncable prefs; | |
| 57 BlacklistStore::RegisterProfilePrefs(prefs.registry()); | |
| 58 BlacklistStore blacklist_store(&prefs); | |
| 59 | |
| 60 // Create suggestions with A, B and C. C and D will be added to the blacklist. | |
| 61 std::set<std::string> suggested_urls; | |
| 62 suggested_urls.insert(kTestUrlA); | |
| 63 suggested_urls.insert(kTestUrlB); | |
| 64 const SuggestionsProfile suggestions_filtered = | |
| 65 CreateSuggestions(suggested_urls); | |
| 66 suggested_urls.insert(kTestUrlC); | |
| 67 const SuggestionsProfile original_suggestions = | |
| 68 CreateSuggestions(suggested_urls); | |
| 69 SuggestionsProfile suggestions; | |
| 70 | |
| 71 // Filter with an empty blacklist. | |
| 72 suggestions.CopyFrom(original_suggestions); | |
| 73 blacklist_store.FilterSuggestions(&suggestions); | |
| 74 ValidateSuggestions(original_suggestions, suggestions); | |
| 75 | |
| 76 // Add C and D to the blacklist and filter. | |
| 77 suggestions.CopyFrom(original_suggestions); | |
| 78 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlC))); | |
| 79 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlD))); | |
| 80 blacklist_store.FilterSuggestions(&suggestions); | |
| 81 ValidateSuggestions(suggestions_filtered, suggestions); | |
| 82 | |
| 83 // Remove C from the blacklist and filter. | |
| 84 suggestions.CopyFrom(original_suggestions); | |
| 85 EXPECT_TRUE(blacklist_store.RemoveUrl(GURL(kTestUrlC))); | |
| 86 blacklist_store.FilterSuggestions(&suggestions); | |
| 87 ValidateSuggestions(original_suggestions, suggestions); | |
| 88 } | |
| 89 | |
| 90 TEST(BlacklistStoreTest, BlacklistTwiceSuceeds) { | |
| 91 TestingPrefServiceSyncable prefs; | |
| 92 BlacklistStore::RegisterProfilePrefs(prefs.registry()); | |
| 93 BlacklistStore blacklist_store(&prefs); | |
| 94 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlA))); | |
| 95 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlA))); | |
| 96 } | |
| 97 | |
| 98 TEST(BlacklistStoreTest, RemoveUnknownUrlSucceeds) { | |
| 99 TestingPrefServiceSyncable prefs; | |
| 100 BlacklistStore::RegisterProfilePrefs(prefs.registry()); | |
| 101 BlacklistStore blacklist_store(&prefs); | |
| 102 EXPECT_TRUE(blacklist_store.RemoveUrl(GURL(kTestUrlA))); | |
| 103 } | |
| 104 | |
| 105 TEST(BlacklistStoreTest, GetFirstUrlFromBlacklist) { | |
| 106 TestingPrefServiceSyncable prefs; | |
| 107 BlacklistStore::RegisterProfilePrefs(prefs.registry()); | |
| 108 BlacklistStore blacklist_store(&prefs); | |
| 109 | |
| 110 // Expect GetFirstUrlFromBlacklist fails when blacklist empty. | |
| 111 GURL retrieved; | |
| 112 EXPECT_FALSE(blacklist_store.GetFirstUrlFromBlacklist(&retrieved)); | |
| 113 | |
| 114 // Blacklist A and B. | |
| 115 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlA))); | |
| 116 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlB))); | |
| 117 | |
| 118 // Expect to retrieve A or B. | |
| 119 EXPECT_TRUE(blacklist_store.GetFirstUrlFromBlacklist(&retrieved)); | |
| 120 std::string retrieved_string = retrieved.spec(); | |
| 121 EXPECT_TRUE(retrieved_string == std::string(kTestUrlA) || | |
| 122 retrieved_string == std::string(kTestUrlB)); | |
| 123 } | |
| 124 | |
| 125 TEST(BlacklistStoreLogTest, LogsBlacklistSize) { | |
| 126 content::TestBrowserThreadBundle bundle; | |
| 127 base::StatisticsDeltaReader statistics_delta_reader; | |
| 128 | |
| 129 // Create a first store - blacklist is empty at this point. | |
| 130 TestingPrefServiceSyncable prefs; | |
| 131 BlacklistStore::RegisterProfilePrefs(prefs.registry()); | |
| 132 scoped_ptr<BlacklistStore> blacklist_store(new BlacklistStore(&prefs)); | |
| 133 scoped_ptr<base::HistogramSamples> samples( | |
| 134 statistics_delta_reader.GetHistogramSamplesSinceCreation( | |
| 135 "Suggestions.LocalBlacklistSize")); | |
| 136 EXPECT_EQ(1, samples->TotalCount()); | |
| 137 EXPECT_EQ(1, samples->GetCount(0)); | |
| 138 | |
| 139 // Add some content to the blacklist. | |
| 140 EXPECT_TRUE(blacklist_store->BlacklistUrl(GURL(kTestUrlA))); | |
| 141 EXPECT_TRUE(blacklist_store->BlacklistUrl(GURL(kTestUrlB))); | |
| 142 | |
| 143 // Create a new BlacklistStore and verify the counts. | |
| 144 blacklist_store.reset(new BlacklistStore(&prefs)); | |
| 145 samples = statistics_delta_reader.GetHistogramSamplesSinceCreation( | |
| 146 "Suggestions.LocalBlacklistSize"); | |
| 147 EXPECT_EQ(2, samples->TotalCount()); | |
| 148 EXPECT_EQ(1, samples->GetCount(0)); | |
| 149 EXPECT_EQ(1, samples->GetCount(2)); | |
| 150 } | |
| 151 | |
| 152 } // namespace suggestions | |
| OLD | NEW |