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