Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / components / suggestions / blacklist_store_unittest.cc
blob9b2f8be9e432c67d7056915a827d616fa5498466
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.
5 #include "components/suggestions/blacklist_store.h"
7 #include <set>
8 #include <string>
10 #include "base/memory/scoped_ptr.h"
12 #include "base/test/histogram_tester.h"
13 #include "components/pref_registry/testing_pref_service_syncable.h"
14 #include "components/suggestions/proto/suggestions.pb.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 using user_prefs::TestingPrefServiceSyncable;
19 namespace suggestions {
21 namespace {
23 const char kTestUrlA[] = "http://aaa.com/";
24 const char kTestUrlB[] = "http://bbb.com/";
25 const char kTestUrlC[] = "http://ccc.com/";
26 const char kTestUrlD[] = "http://ddd.com/";
28 SuggestionsProfile CreateSuggestions(std::set<std::string> urls) {
29 SuggestionsProfile suggestions;
30 for (std::set<std::string>::iterator it = urls.begin(); it != urls.end();
31 ++it) {
32 ChromeSuggestion* suggestion = suggestions.add_suggestions();
33 suggestion->set_url(*it);
35 return suggestions;
38 void ValidateSuggestions(const SuggestionsProfile& expected,
39 const SuggestionsProfile& actual) {
40 ASSERT_EQ(expected.suggestions_size(), actual.suggestions_size());
41 for (int i = 0; i < expected.suggestions_size(); ++i) {
42 EXPECT_EQ(expected.suggestions(i).url(), actual.suggestions(i).url());
43 EXPECT_EQ(expected.suggestions(i).title(), actual.suggestions(i).title());
44 EXPECT_EQ(expected.suggestions(i).favicon_url(),
45 actual.suggestions(i).favicon_url());
46 EXPECT_EQ(expected.suggestions(i).thumbnail(),
47 actual.suggestions(i).thumbnail());
51 } // namespace
53 class BlacklistStoreTest : public testing::Test {
54 public:
55 BlacklistStoreTest()
56 : pref_service_(new user_prefs::TestingPrefServiceSyncable) {}
58 void SetUp() override {
59 BlacklistStore::RegisterProfilePrefs(pref_service()->registry());
62 user_prefs::TestingPrefServiceSyncable* pref_service() {
63 return pref_service_.get();
66 private:
67 scoped_ptr<user_prefs::TestingPrefServiceSyncable> pref_service_;
69 DISALLOW_COPY_AND_ASSIGN(BlacklistStoreTest);
72 // Tests adding, removing to the blacklist and filtering.
73 TEST_F(BlacklistStoreTest, BasicInteractions) {
74 BlacklistStore blacklist_store(pref_service());
76 // Create suggestions with A, B and C. C and D will be added to the blacklist.
77 std::set<std::string> suggested_urls;
78 suggested_urls.insert(kTestUrlA);
79 suggested_urls.insert(kTestUrlB);
80 const SuggestionsProfile suggestions_filtered =
81 CreateSuggestions(suggested_urls);
82 suggested_urls.insert(kTestUrlC);
83 const SuggestionsProfile original_suggestions =
84 CreateSuggestions(suggested_urls);
85 SuggestionsProfile suggestions;
87 // Filter with an empty blacklist.
88 suggestions.CopyFrom(original_suggestions);
89 blacklist_store.FilterSuggestions(&suggestions);
90 ValidateSuggestions(original_suggestions, suggestions);
92 // Add C and D to the blacklist and filter.
93 suggestions.CopyFrom(original_suggestions);
94 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlC)));
95 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlD)));
96 blacklist_store.FilterSuggestions(&suggestions);
97 ValidateSuggestions(suggestions_filtered, suggestions);
99 // Remove C from the blacklist and filter.
100 suggestions.CopyFrom(original_suggestions);
101 EXPECT_TRUE(blacklist_store.RemoveUrl(GURL(kTestUrlC)));
102 blacklist_store.FilterSuggestions(&suggestions);
103 ValidateSuggestions(original_suggestions, suggestions);
106 TEST_F(BlacklistStoreTest, BlacklistTwiceSuceeds) {
107 BlacklistStore blacklist_store(pref_service());
108 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlA)));
109 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlA)));
112 TEST_F(BlacklistStoreTest, RemoveUnknownUrlFails) {
113 BlacklistStore blacklist_store(pref_service());
114 EXPECT_FALSE(blacklist_store.RemoveUrl(GURL(kTestUrlA)));
117 TEST_F(BlacklistStoreTest, TestGetTimeUntilReadyForUpload) {
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;
125 // Blacklist is empty.
126 EXPECT_FALSE(blacklist_store->GetTimeUntilReadyForUpload(&candidate_delta));
127 EXPECT_FALSE(blacklist_store->GetTimeUntilURLReadyForUpload(
128 GURL(kTestUrlA), &candidate_delta));
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));
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.
146 GURL retrieved;
147 EXPECT_FALSE(blacklist_store->GetCandidateForUpload(&retrieved));
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);
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));
167 // Blacklist A and B. Expect to retrieve A or B.
168 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlA)));
169 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlB)));
170 EXPECT_TRUE(blacklist_store.GetCandidateForUpload(&retrieved));
171 std::string retrieved_string = retrieved.spec();
172 EXPECT_TRUE(retrieved_string == std::string(kTestUrlA) ||
173 retrieved_string == std::string(kTestUrlB));
176 TEST_F(BlacklistStoreTest, LogsBlacklistSize) {
177 base::HistogramTester histogram_tester;
179 // Create a first store - blacklist is empty at this point.
180 scoped_ptr<BlacklistStore> blacklist_store(
181 new BlacklistStore(pref_service()));
182 histogram_tester.ExpectTotalCount("Suggestions.LocalBlacklistSize", 1);
183 histogram_tester.ExpectUniqueSample("Suggestions.LocalBlacklistSize", 0, 1);
185 // Add some content to the blacklist.
186 EXPECT_TRUE(blacklist_store->BlacklistUrl(GURL(kTestUrlA)));
187 EXPECT_TRUE(blacklist_store->BlacklistUrl(GURL(kTestUrlB)));
189 // Create a new BlacklistStore and verify the counts.
190 blacklist_store.reset(new BlacklistStore(pref_service()));
191 histogram_tester.ExpectTotalCount("Suggestions.LocalBlacklistSize", 2);
192 histogram_tester.ExpectBucketCount("Suggestions.LocalBlacklistSize", 0, 1);
193 histogram_tester.ExpectBucketCount("Suggestions.LocalBlacklistSize", 2, 1);
196 } // namespace suggestions