Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Side by Side Diff: chrome/browser/search/suggestions/blacklist_store_unittest.cc

Issue 330473003: Offline blacklisting for SuggestionsService. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Final merge. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/metrics/statistics_recorder.h"
11 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h"
12 #include "chrome/test/base/testing_pref_service_syncable.h"
13 #include "chrome/test/base/uma_histogram_helper.h"
14 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace suggestions {
18
19 namespace {
20
21 const char kTestUrlA[] = "http://aaa.com/";
22 const char kTestUrlB[] = "http://bbb.com/";
23 const char kTestUrlC[] = "http://ccc.com/";
24 const char kTestUrlD[] = "http://ddd.com/";
25
26 const char* const kHistogramsToSnapshot[] = {"Suggestions.LocalBlacklistSize"};
27
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);
34 }
35 return suggestions;
36 }
37
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());
48 }
49 }
50
51 } // namespace
52
53 TEST(BlacklistStoreTest, BasicInteractions) {
54 TestingPrefServiceSyncable prefs;
55 BlacklistStore::RegisterProfilePrefs(prefs.registry());
56 BlacklistStore blacklist_store(&prefs);
57
58 // Create suggestions with A, B and C. C and D will be added to the blacklist.
59 std::set<std::string> suggested_urls;
60 suggested_urls.insert(kTestUrlA);
61 suggested_urls.insert(kTestUrlB);
62 const SuggestionsProfile suggestions_filtered =
63 CreateSuggestions(suggested_urls);
64 suggested_urls.insert(kTestUrlC);
65 const SuggestionsProfile original_suggestions =
66 CreateSuggestions(suggested_urls);
67 SuggestionsProfile suggestions;
68
69 // Filter with an empty blacklist.
70 suggestions.CopyFrom(original_suggestions);
71 blacklist_store.FilterSuggestions(&suggestions);
72 ValidateSuggestions(original_suggestions, suggestions);
73
74 // Add C and D to the blacklist and filter.
75 suggestions.CopyFrom(original_suggestions);
76 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlC)));
77 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlD)));
78 blacklist_store.FilterSuggestions(&suggestions);
79 ValidateSuggestions(suggestions_filtered, suggestions);
80
81 // Remove C from the blacklist and filter.
82 suggestions.CopyFrom(original_suggestions);
83 EXPECT_TRUE(blacklist_store.RemoveUrl(GURL(kTestUrlC)));
84 blacklist_store.FilterSuggestions(&suggestions);
85 ValidateSuggestions(original_suggestions, suggestions);
86 }
87
88 TEST(BlacklistStoreTest, BlacklistTwiceSuceeds) {
89 TestingPrefServiceSyncable prefs;
90 BlacklistStore::RegisterProfilePrefs(prefs.registry());
91 BlacklistStore blacklist_store(&prefs);
92 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlA)));
93 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlA)));
94 }
95
96 TEST(BlacklistStoreTest, RemoveUnknownUrlSucceeds) {
97 TestingPrefServiceSyncable prefs;
98 BlacklistStore::RegisterProfilePrefs(prefs.registry());
99 BlacklistStore blacklist_store(&prefs);
100 EXPECT_TRUE(blacklist_store.RemoveUrl(GURL(kTestUrlA)));
101 }
102
103 TEST(BlacklistStoreTest, GetFirstUrlFromBlacklist) {
104 TestingPrefServiceSyncable prefs;
105 BlacklistStore::RegisterProfilePrefs(prefs.registry());
106 BlacklistStore blacklist_store(&prefs);
107
108 // Expect GetFirstUrlFromBlacklist fails when blacklist empty.
109 GURL retrieved;
110 EXPECT_FALSE(blacklist_store.GetFirstUrlFromBlacklist(&retrieved));
111
112 // Blacklist A and B.
113 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlA)));
114 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlB)));
115
116 // Expect to retrieve A or B.
117 EXPECT_TRUE(blacklist_store.GetFirstUrlFromBlacklist(&retrieved));
118 std::string retrieved_string = retrieved.spec();
119 EXPECT_TRUE(retrieved_string == std::string(kTestUrlA) ||
120 retrieved_string == std::string(kTestUrlB));
121 }
122
123 class BlacklistStoreLogTest : public testing::Test {
Alexei Svitkine (slow) 2014/06/20 14:41:41 If this is being used only for a single test, just
manzagop (departed) 2014/06/20 15:13:53 Done.
124 public:
125 BlacklistStoreLogTest() {
126 base::StatisticsRecorder::Initialize();
Alexei Svitkine (slow) 2014/06/20 14:41:41 Is this still needed?
manzagop (departed) 2014/06/20 15:13:53 Done.
127 }
128 UMAHistogramHelper* histogram_helper() { return &histogram_helper_; }
129
130 private:
131 UMAHistogramHelper histogram_helper_;
132 content::TestBrowserThreadBundle bundle_;
133
134 DISALLOW_COPY_AND_ASSIGN(BlacklistStoreLogTest);
135 };
136
137 TEST_F(BlacklistStoreLogTest, LogsBlacklistSize) {
138 histogram_helper()->PrepareSnapshot(
139 kHistogramsToSnapshot, arraysize(kHistogramsToSnapshot));
140
141 // Create a first store - blacklist is empty at this point.
142 TestingPrefServiceSyncable prefs;
143 BlacklistStore::RegisterProfilePrefs(prefs.registry());
144 scoped_ptr<BlacklistStore> blacklist_store(new BlacklistStore(&prefs));
145 histogram_helper()->Fetch();
146 histogram_helper()->ExpectTotalCount("Suggestions.LocalBlacklistSize", 1);
147 histogram_helper()->ExpectUniqueSample("Suggestions.LocalBlacklistSize", 0,
148 1);
149
150 // Add some content to the blacklist.
151 EXPECT_TRUE(blacklist_store->BlacklistUrl(GURL(kTestUrlA)));
152 EXPECT_TRUE(blacklist_store->BlacklistUrl(GURL(kTestUrlB)));
153
154 // Create a new BlacklistStore and verify the counts.
155 blacklist_store.reset(new BlacklistStore(&prefs));
156 histogram_helper()->Fetch();
157 histogram_helper()->ExpectTotalCount("Suggestions.LocalBlacklistSize", 2);
158 histogram_helper()->ExpectBucketCount("Suggestions.LocalBlacklistSize", 0, 1);
159 histogram_helper()->ExpectBucketCount("Suggestions.LocalBlacklistSize", 2, 1);
160 }
161
162 } // namespace suggestions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698