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

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: Base version 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 "chrome/browser/search/suggestions/proto/suggestions.pb.h"
11 #include "chrome/test/base/testing_pref_service_syncable.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace suggestions {
15
16 namespace {
17
18 const char kTestUrlA[] = "http://aaa.com/";
19 const char kTestUrlB[] = "http://bbb.com/";
20 const char kTestUrlC[] = "http://ccc.com/";
21 const char kTestUrlD[] = "http://ddd.com/";
22
23 SuggestionsProfile CreateSuggestions(std::set<std::string> urls) {
24 SuggestionsProfile suggestions;
25 for (std::set<std::string>::iterator it = urls.begin(); it != urls.end();
26 ++it) {
27 ChromeSuggestion* suggestion = suggestions.add_suggestions();
28 suggestion->set_url(*it);
29 }
30 return suggestions;
31 }
32
33 void ValidateSuggestions(const SuggestionsProfile& expected,
34 const SuggestionsProfile& actual) {
35 ASSERT_EQ(expected.suggestions_size(), actual.suggestions_size());
36 for (int i = 0; i < expected.suggestions_size(); ++i) {
37 EXPECT_EQ(expected.suggestions(i).url(), actual.suggestions(i).url());
38 EXPECT_EQ(expected.suggestions(i).title(), actual.suggestions(i).title());
39 EXPECT_EQ(expected.suggestions(i).favicon_url(),
40 actual.suggestions(i).favicon_url());
41 EXPECT_EQ(expected.suggestions(i).thumbnail(),
42 actual.suggestions(i).thumbnail());
43 }
44 }
45
46 } // namespace
47
48 TEST(BlacklistStoreTest, BasicInteractions) {
49 TestingPrefServiceSyncable prefs;
50 BlacklistStore::RegisterProfilePrefs(prefs.registry());
51 BlacklistStore blacklist_store(&prefs);
52
53 // Create suggestions with A, B and C. C and D will be added to the blacklist.
54 std::set<std::string> suggested_urls;
55 suggested_urls.insert(kTestUrlA);
56 suggested_urls.insert(kTestUrlB);
57 const SuggestionsProfile suggestions_filtered =
58 CreateSuggestions(suggested_urls);
59 suggested_urls.insert(kTestUrlC);
60 const SuggestionsProfile original_suggestions =
61 CreateSuggestions(suggested_urls);
62 SuggestionsProfile suggestions;
63
64 // Filter with an empty blacklist.
65 suggestions.CopyFrom(original_suggestions);
66 blacklist_store.FilterSuggestions(&suggestions);
67 ValidateSuggestions(original_suggestions, suggestions);
68
69 // Add C and D to the blacklist and filter.
70 suggestions.CopyFrom(original_suggestions);
71 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlC)));
72 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlD)));
73 blacklist_store.FilterSuggestions(&suggestions);
74 ValidateSuggestions(suggestions_filtered, suggestions);
75
76 // Remove C from the blacklist and filter.
77 suggestions.CopyFrom(original_suggestions);
78 EXPECT_TRUE(blacklist_store.RemoveUrl(GURL(kTestUrlC)));
79 blacklist_store.FilterSuggestions(&suggestions);
80 ValidateSuggestions(original_suggestions, suggestions);
81 }
82
83 TEST(BlacklistStoreTest, BlacklistTwiceSuceeds) {
84 TestingPrefServiceSyncable prefs;
85 BlacklistStore::RegisterProfilePrefs(prefs.registry());
86 BlacklistStore blacklist_store(&prefs);
87 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlA)));
88 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlA)));
89 }
90
91 TEST(BlacklistStoreTest, RemoveUnknownUrlSucceeds) {
92 TestingPrefServiceSyncable prefs;
93 BlacklistStore::RegisterProfilePrefs(prefs.registry());
94 BlacklistStore blacklist_store(&prefs);
95 EXPECT_TRUE(blacklist_store.RemoveUrl(GURL(kTestUrlA)));
96 }
97
98 TEST(BlacklistStoreTest, GetSomeBlacklistedUrl) {
99 TestingPrefServiceSyncable prefs;
100 BlacklistStore::RegisterProfilePrefs(prefs.registry());
101 BlacklistStore blacklist_store(&prefs);
102
103 // Expect GetSomeBlacklistedUrl fails when blacklist empty.
104 GURL retrieved;
105 EXPECT_FALSE(blacklist_store.GetSomeBlacklistedUrl(&retrieved));
106
107 // Blacklist A and B.
108 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlA)));
109 EXPECT_TRUE(blacklist_store.BlacklistUrl(GURL(kTestUrlB)));
110
111 // Expect to retrieve A or B.
112 EXPECT_TRUE(blacklist_store.GetSomeBlacklistedUrl(&retrieved));
113 std::string retrieved_string = retrieved.spec();
114 EXPECT_TRUE(retrieved_string == std::string(kTestUrlA) ||
115 retrieved_string == std::string(kTestUrlB));
116 }
117
118 } // namespace suggestions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698