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