OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/previews/core/previews_black_list.h" | 5 #include "components/previews/core/previews_black_list.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 16 matching lines...) Expand all Loading... | |
27 namespace { | 27 namespace { |
28 | 28 |
29 using PreviewsBlackListTest = testing::Test; | 29 using PreviewsBlackListTest = testing::Test; |
30 | 30 |
31 } // namespace | 31 } // namespace |
32 | 32 |
33 namespace previews { | 33 namespace previews { |
34 | 34 |
35 namespace { | 35 namespace { |
36 | 36 |
37 void RunLoadCallback(LoadBlackListCallback callback, | 37 void RunLoadCallback( |
38 std::unique_ptr<BlackListItemMap> black_list_item_map) { | 38 LoadBlackListCallback callback, |
39 callback.Run(std::move(black_list_item_map)); | 39 std::unique_ptr<BlackListItemMap> black_list_item_map, |
40 std::unique_ptr<PreviewsBlackListItem> host_indifferent_black_list_item) { | |
41 callback.Run(std::move(black_list_item_map), | |
42 std::move(host_indifferent_black_list_item)); | |
40 } | 43 } |
41 | 44 |
42 class TestPreviewsOptOutStore : public PreviewsOptOutStore { | 45 class TestPreviewsOptOutStore : public PreviewsOptOutStore { |
43 public: | 46 public: |
44 TestPreviewsOptOutStore() : clear_blacklist_count_(0) {} | 47 TestPreviewsOptOutStore() : clear_blacklist_count_(0) {} |
45 ~TestPreviewsOptOutStore() override {} | 48 ~TestPreviewsOptOutStore() override {} |
46 | 49 |
47 int clear_blacklist_count() { return clear_blacklist_count_; } | 50 int clear_blacklist_count() { return clear_blacklist_count_; } |
48 | 51 |
49 private: | 52 private: |
50 // PreviewsOptOutStore implementation: | 53 // PreviewsOptOutStore implementation: |
51 void AddPreviewNavigation(bool opt_out, | 54 void AddPreviewNavigation(bool opt_out, |
52 const std::string& host_name, | 55 const std::string& host_name, |
53 PreviewsType type, | 56 PreviewsType type, |
54 base::Time now) override {} | 57 base::Time now) override {} |
55 | 58 |
56 void LoadBlackList(LoadBlackListCallback callback) override { | 59 void LoadBlackList(LoadBlackListCallback callback) override { |
57 std::unique_ptr<BlackListItemMap> black_list_item_map( | 60 std::unique_ptr<BlackListItemMap> black_list_item_map( |
58 new BlackListItemMap()); | 61 new BlackListItemMap()); |
62 std::unique_ptr<PreviewsBlackListItem> host_indifferent_black_list_item = | |
63 PreviewsBlackList::CreateHostIndifferentBlackListItem(); | |
59 base::ThreadTaskRunnerHandle::Get()->PostTask( | 64 base::ThreadTaskRunnerHandle::Get()->PostTask( |
60 FROM_HERE, base::Bind(&RunLoadCallback, callback, | 65 FROM_HERE, base::Bind(&RunLoadCallback, callback, |
61 base::Passed(&black_list_item_map))); | 66 base::Passed(&black_list_item_map), |
67 base::Passed(&host_indifferent_black_list_item))); | |
62 } | 68 } |
63 | 69 |
64 void ClearBlackList(base::Time begin_time, base::Time end_time) override { | 70 void ClearBlackList(base::Time begin_time, base::Time end_time) override { |
65 ++clear_blacklist_count_; | 71 ++clear_blacklist_count_; |
66 } | 72 } |
67 | 73 |
68 int clear_blacklist_count_; | 74 int clear_blacklist_count_; |
69 }; | 75 }; |
70 | 76 |
71 } // namespace | 77 } // namespace |
72 | 78 |
73 TEST_F(PreviewsBlackListTest, BlackListNoStore) { | 79 TEST_F(PreviewsBlackListTest, PerHostBlackListNoStore) { |
74 // Tests the black list behavior when a null OptOutSture is passed in. | 80 // Tests the black list behavior when a null OptOutSture is passed in. |
75 const GURL url_a("http://www.url_a.com"); | 81 const GURL url_a("http://www.url_a.com"); |
76 const GURL url_b("http://www.url_b.com"); | 82 const GURL url_b("http://www.url_b.com"); |
77 const size_t history = 4; | 83 const size_t per_host_history = 4; |
78 const int threshold = 2; | 84 const int per_host_threshold = 2; |
85 // Host indifferent blacklisting should have no effect with the following | |
86 // params. | |
87 const size_t host_indifferent_history = 1; | |
88 const int host_indifferent_threshold = 2; | |
79 const int duration_in_days = 365; | 89 const int duration_in_days = 365; |
80 base::FieldTrialList field_trial_list(nullptr); | 90 base::FieldTrialList field_trial_list(nullptr); |
81 std::map<std::string, std::string> params; | 91 std::map<std::string, std::string> params; |
82 params["stored_history_length"] = base::SizeTToString(history); | 92 params["per_host_max_stored_history_length"] = |
83 params["opt_out_threshold"] = base::IntToString(threshold); | 93 base::SizeTToString(per_host_history); |
84 params["black_list_duration_in_days"] = base::IntToString(duration_in_days); | 94 params["host_indifferent_max_stored_history_length"] = |
95 base::SizeTToString(host_indifferent_history); | |
96 params["per_host_opt_out_threshold"] = base::IntToString(per_host_threshold); | |
97 params["host_indifferent_opt_out_threshold"] = | |
98 base::IntToString(host_indifferent_threshold); | |
99 params["per_host_black_list_duration_in_days"] = | |
100 base::IntToString(duration_in_days); | |
85 ASSERT_TRUE( | 101 ASSERT_TRUE( |
86 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", | 102 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", |
87 params) && | 103 params) && |
88 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); | 104 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); |
89 | 105 |
90 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); | 106 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); |
91 base::Time start = test_clock->Now(); | 107 base::Time start = test_clock->Now(); |
92 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 108 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
93 | 109 |
94 std::unique_ptr<PreviewsBlackList> black_list( | 110 std::unique_ptr<PreviewsBlackList> black_list( |
95 new PreviewsBlackList(nullptr, base::WrapUnique(test_clock))); | 111 new PreviewsBlackList(nullptr, base::WrapUnique(test_clock))); |
96 | 112 |
97 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | 113 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); |
98 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 114 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
99 | 115 |
100 black_list->AddPreviewNavigation(url_a, true, PreviewsType::OFFLINE); | 116 black_list->AddPreviewNavigation(url_a, true, PreviewsType::OFFLINE); |
117 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
101 black_list->AddPreviewNavigation(url_a, true, PreviewsType::OFFLINE); | 118 black_list->AddPreviewNavigation(url_a, true, PreviewsType::OFFLINE); |
119 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
102 | 120 |
103 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | 121 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); |
104 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 122 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
105 | 123 |
106 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); | 124 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); |
125 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
107 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); | 126 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); |
127 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
108 | 128 |
109 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | 129 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); |
110 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 130 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
111 | 131 |
112 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); | 132 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); |
133 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
113 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); | 134 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); |
135 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
114 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); | 136 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); |
137 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
115 | 138 |
116 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | 139 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); |
117 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 140 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
118 | 141 |
119 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
120 black_list->ClearBlackList(start, test_clock->Now()); | 142 black_list->ClearBlackList(start, test_clock->Now()); |
121 | 143 |
122 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | 144 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); |
123 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 145 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
124 | 146 |
125 variations::testing::ClearAllVariationParams(); | 147 variations::testing::ClearAllVariationParams(); |
126 } | 148 } |
127 | 149 |
128 TEST_F(PreviewsBlackListTest, BlackListWithStore) { | 150 TEST_F(PreviewsBlackListTest, PerHostBlackListWithStore) { |
129 // Tests the black list behavior when a non-null OptOutSture is passed in. | 151 // Tests the black list behavior when a non-null OptOutSture is passed in. |
130 const GURL url_a1("http://www.url_a.com/a1"); | 152 const GURL url_a1("http://www.url_a.com/a1"); |
131 const GURL url_a2("http://www.url_a.com/a2"); | 153 const GURL url_a2("http://www.url_a.com/a2"); |
132 const GURL url_b("http://www.url_b.com"); | 154 const GURL url_b("http://www.url_b.com"); |
133 const size_t history = 4; | 155 const size_t per_host_history = 4; |
134 const int threshold = 2; | 156 const int per_host_threshold = 2; |
157 // Host indifferent blacklisting should have no effect with the following | |
158 // params. | |
159 const size_t host_indifferent_history = 1; | |
160 const int host_indifferent_threshold = 2; | |
135 const int duration_in_days = 365; | 161 const int duration_in_days = 365; |
136 base::FieldTrialList field_trial_list(nullptr); | 162 base::FieldTrialList field_trial_list(nullptr); |
137 std::map<std::string, std::string> params; | 163 std::map<std::string, std::string> params; |
138 params["stored_history_length"] = base::SizeTToString(history); | 164 params["per_host_max_stored_history_length"] = |
139 params["opt_out_threshold"] = base::IntToString(threshold); | 165 base::SizeTToString(per_host_history); |
140 params["black_list_duration_in_days"] = base::IntToString(duration_in_days); | 166 params["host_indifferent_max_stored_history_length"] = |
167 base::SizeTToString(host_indifferent_history); | |
168 params["per_host_opt_out_threshold"] = base::IntToString(per_host_threshold); | |
169 params["host_indifferent_opt_out_threshold"] = | |
170 base::IntToString(host_indifferent_threshold); | |
171 params["per_host_black_list_duration_in_days"] = | |
172 base::IntToString(duration_in_days); | |
141 ASSERT_TRUE( | 173 ASSERT_TRUE( |
142 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", | 174 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", |
143 params) && | 175 params) && |
144 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); | 176 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); |
145 | 177 |
146 base::MessageLoop loop; | 178 base::MessageLoop loop; |
147 | 179 |
148 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); | 180 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); |
149 base::Time start = test_clock->Now(); | 181 base::Time start = test_clock->Now(); |
150 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 182 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
151 | 183 |
152 TestPreviewsOptOutStore* opt_out_store = new TestPreviewsOptOutStore(); | 184 TestPreviewsOptOutStore* opt_out_store = new TestPreviewsOptOutStore(); |
153 | 185 |
154 std::unique_ptr<PreviewsBlackList> black_list(new PreviewsBlackList( | 186 std::unique_ptr<PreviewsBlackList> black_list(new PreviewsBlackList( |
155 base::WrapUnique(opt_out_store), base::WrapUnique(test_clock))); | 187 base::WrapUnique(opt_out_store), base::WrapUnique(test_clock))); |
156 | 188 |
157 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); | 189 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
158 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); | 190 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); |
159 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 191 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
160 | 192 |
161 base::RunLoop().RunUntilIdle(); | 193 base::RunLoop().RunUntilIdle(); |
162 | 194 |
163 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); | 195 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
164 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); | 196 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); |
165 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 197 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
166 | 198 |
167 black_list->AddPreviewNavigation(url_a1, true, PreviewsType::OFFLINE); | 199 black_list->AddPreviewNavigation(url_a1, true, PreviewsType::OFFLINE); |
200 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
168 black_list->AddPreviewNavigation(url_a1, true, PreviewsType::OFFLINE); | 201 black_list->AddPreviewNavigation(url_a1, true, PreviewsType::OFFLINE); |
169 | 202 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
170 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); | 203 |
171 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); | 204 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
172 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 205 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); |
173 | 206 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
174 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); | 207 |
175 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); | 208 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); |
209 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
210 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); | |
211 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
176 | 212 |
177 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); | 213 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
178 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); | 214 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); |
179 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 215 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
180 | 216 |
181 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); | 217 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); |
218 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
182 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); | 219 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); |
220 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
183 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); | 221 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); |
184 | 222 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
185 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); | 223 |
186 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); | 224 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
187 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 225 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); |
188 | 226 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
189 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 227 |
190 EXPECT_EQ(0, opt_out_store->clear_blacklist_count()); | 228 EXPECT_EQ(0, opt_out_store->clear_blacklist_count()); |
191 black_list->ClearBlackList(start, base::Time::Now()); | 229 black_list->ClearBlackList(start, base::Time::Now()); |
192 EXPECT_EQ(1, opt_out_store->clear_blacklist_count()); | 230 EXPECT_EQ(1, opt_out_store->clear_blacklist_count()); |
193 | 231 |
194 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); | 232 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
195 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); | 233 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); |
196 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 234 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
197 | 235 |
198 base::RunLoop().RunUntilIdle(); | 236 base::RunLoop().RunUntilIdle(); |
199 EXPECT_EQ(1, opt_out_store->clear_blacklist_count()); | 237 EXPECT_EQ(1, opt_out_store->clear_blacklist_count()); |
200 | 238 |
201 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); | 239 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
202 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); | 240 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
203 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 241 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
204 | 242 |
205 variations::testing::ClearAllVariationParams(); | 243 variations::testing::ClearAllVariationParams(); |
206 } | 244 } |
207 | 245 |
246 TEST_F(PreviewsBlackListTest, HostIndifferentBlackListNoStore) { | |
247 // Tests the black list behavior when a null OptOutSture is passed in. | |
248 const GURL url_a("http://www.url_a.com"); | |
249 const GURL url_b("http://www.url_b.com"); | |
250 const GURL url_c("http://www.url_c.com"); | |
251 const GURL url_d("http://www.url_d.com"); | |
252 // Per host blacklisting should have no effect with the following params. | |
253 const size_t per_host_history = 1; | |
254 const int per_host_threshold = 2; | |
255 const size_t host_indifferent_history = 4; | |
256 const int host_indifferent_threshold = 4; | |
257 const int duration_in_days = 365; | |
258 base::FieldTrialList field_trial_list(nullptr); | |
259 std::map<std::string, std::string> params; | |
260 params["per_host_max_stored_history_length"] = | |
261 base::SizeTToString(per_host_history); | |
262 params["host_indifferent_max_stored_history_length"] = | |
263 base::SizeTToString(host_indifferent_history); | |
264 params["per_host_opt_out_threshold"] = base::IntToString(per_host_threshold); | |
265 params["host_indifferent_opt_out_threshold"] = | |
266 base::IntToString(host_indifferent_threshold); | |
267 params["per_host_black_list_duration_in_days"] = | |
268 base::IntToString(duration_in_days); | |
269 ASSERT_TRUE( | |
270 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", | |
271 params) && | |
272 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); | |
273 | |
274 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); | |
275 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
276 | |
277 std::unique_ptr<PreviewsBlackList> black_list( | |
278 new PreviewsBlackList(nullptr, base::WrapUnique(test_clock))); | |
279 | |
280 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | |
281 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | |
282 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE)); | |
283 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_d, PreviewsType::OFFLINE)); | |
284 | |
285 black_list->AddPreviewNavigation(url_a, true, PreviewsType::OFFLINE); | |
286 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
287 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | |
288 | |
289 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); | |
290 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
291 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | |
292 | |
293 black_list->AddPreviewNavigation(url_c, true, PreviewsType::OFFLINE); | |
294 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
295 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | |
296 | |
297 black_list->AddPreviewNavigation(url_d, true, PreviewsType::OFFLINE); | |
298 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
299 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | |
300 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | |
301 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE)); | |
302 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_d, PreviewsType::OFFLINE)); | |
303 | |
304 black_list->AddPreviewNavigation(url_d, false, PreviewsType::OFFLINE); | |
305 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
306 | |
307 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | |
tbansal1
2016/10/25 16:15:48
Why did these become true?
RyanSturm
2016/10/25 18:15:44
Added comment.
| |
308 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | |
309 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE)); | |
310 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_d, PreviewsType::OFFLINE)); | |
311 | |
312 variations::testing::ClearAllVariationParams(); | |
313 } | |
314 | |
315 TEST_F(PreviewsBlackListTest, HostIndifferentBlackListWithStore) { | |
tbansal1
2016/10/25 16:15:48
IIUC, this test is not really much different from
RyanSturm
2016/10/25 18:15:44
Adding that seems like overkill. When I add the op
tbansal1
2016/10/28 21:59:33
May be. What I am suggesting is testing the integr
| |
316 // Tests the black list behavior when a non-null OptOutSture is passed in. | |
317 const GURL url_a("http://www.url_a.com"); | |
318 const GURL url_b("http://www.url_b.com"); | |
319 const GURL url_c("http://www.url_c.com"); | |
320 const GURL url_d("http://www.url_d.com"); | |
321 // Per host blacklisting should have no effect with the following params. | |
322 const size_t per_host_history = 1; | |
323 const int per_host_threshold = 2; | |
324 const size_t host_indifferent_history = 4; | |
325 const int host_indifferent_threshold = 4; | |
326 const int duration_in_days = 365; | |
327 base::FieldTrialList field_trial_list(nullptr); | |
328 std::map<std::string, std::string> params; | |
329 params["per_host_max_stored_history_length"] = | |
330 base::SizeTToString(per_host_history); | |
331 params["host_indifferent_max_stored_history_length"] = | |
332 base::SizeTToString(host_indifferent_history); | |
333 params["per_host_opt_out_threshold"] = base::IntToString(per_host_threshold); | |
334 params["host_indifferent_opt_out_threshold"] = | |
335 base::IntToString(host_indifferent_threshold); | |
336 params["per_host_black_list_duration_in_days"] = | |
337 base::IntToString(duration_in_days); | |
338 ASSERT_TRUE( | |
339 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", | |
340 params) && | |
341 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); | |
342 | |
343 base::MessageLoop loop; | |
344 | |
345 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); | |
346 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
347 | |
348 TestPreviewsOptOutStore* opt_out_store = new TestPreviewsOptOutStore(); | |
349 | |
350 std::unique_ptr<PreviewsBlackList> black_list(new PreviewsBlackList( | |
351 base::WrapUnique(opt_out_store), base::WrapUnique(test_clock))); | |
352 | |
353 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | |
354 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | |
355 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE)); | |
356 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_d, PreviewsType::OFFLINE)); | |
357 | |
358 base::RunLoop().RunUntilIdle(); | |
359 | |
360 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | |
361 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | |
362 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE)); | |
363 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_d, PreviewsType::OFFLINE)); | |
364 | |
365 black_list->AddPreviewNavigation(url_a, true, PreviewsType::OFFLINE); | |
366 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
367 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | |
368 | |
369 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); | |
370 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
371 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | |
372 | |
373 black_list->AddPreviewNavigation(url_c, true, PreviewsType::OFFLINE); | |
374 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
375 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | |
376 | |
377 black_list->AddPreviewNavigation(url_d, true, PreviewsType::OFFLINE); | |
378 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
379 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | |
380 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | |
381 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE)); | |
382 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_d, PreviewsType::OFFLINE)); | |
383 | |
384 black_list->AddPreviewNavigation(url_d, false, PreviewsType::OFFLINE); | |
385 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
386 | |
387 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | |
388 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | |
389 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE)); | |
390 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_d, PreviewsType::OFFLINE)); | |
391 | |
392 variations::testing::ClearAllVariationParams(); | |
393 } | |
394 | |
208 TEST_F(PreviewsBlackListTest, QueueBehavior) { | 395 TEST_F(PreviewsBlackListTest, QueueBehavior) { |
209 // Tests the black list asynchronous queue behavior. Methods called while | 396 // Tests the black list asynchronous queue behavior. Methods called while |
210 // loading are queued and should run in the order they were queued. | 397 // loading are queued and should run in the order they were queued. |
211 const GURL url("http://www.url.com"); | 398 const GURL url("http://www.url.com"); |
212 const GURL url2("http://www.url2.com"); | 399 const GURL url2("http://www.url2.com"); |
400 // Host indifferent blacklisting should have no effect with the following | |
401 // params. | |
402 const size_t host_indifferent_history = 1; | |
403 const int host_indifferent_threshold = 2; | |
213 const int duration_in_days = 365; | 404 const int duration_in_days = 365; |
214 base::FieldTrialList field_trial_list(nullptr); | 405 base::FieldTrialList field_trial_list(nullptr); |
215 std::map<std::string, std::string> params; | 406 std::map<std::string, std::string> params; |
216 params["black_list_duration_in_days"] = base::IntToString(duration_in_days); | 407 params["per_host_black_list_duration_in_days"] = |
408 base::IntToString(duration_in_days); | |
409 params["host_indifferent_max_stored_history_length"] = | |
410 base::SizeTToString(host_indifferent_history); | |
411 params["host_indifferent_opt_out_threshold"] = | |
412 base::IntToString(host_indifferent_threshold); | |
217 ASSERT_TRUE( | 413 ASSERT_TRUE( |
218 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", | 414 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", |
219 params) && | 415 params) && |
220 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); | 416 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); |
221 | 417 |
222 base::MessageLoop loop; | 418 base::MessageLoop loop; |
223 | 419 |
224 std::vector<bool> test_opt_out{true, false}; | 420 std::vector<bool> test_opt_out{true, false}; |
225 | 421 |
226 for (auto opt_out : test_opt_out) { | 422 for (auto opt_out : test_opt_out) { |
227 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); | 423 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); |
424 base::Time start = test_clock->Now(); | |
228 | 425 |
229 TestPreviewsOptOutStore* opt_out_store = new TestPreviewsOptOutStore(); | 426 TestPreviewsOptOutStore* opt_out_store = new TestPreviewsOptOutStore(); |
230 | 427 |
231 std::unique_ptr<PreviewsBlackList> black_list(new PreviewsBlackList( | 428 std::unique_ptr<PreviewsBlackList> black_list(new PreviewsBlackList( |
232 base::WrapUnique(opt_out_store), base::WrapUnique(test_clock))); | 429 base::WrapUnique(opt_out_store), base::WrapUnique(test_clock))); |
233 | 430 |
234 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); | 431 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); |
235 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); | 432 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); |
236 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); | 433 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
434 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); | |
435 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
237 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); | 436 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); |
238 base::RunLoop().RunUntilIdle(); | 437 base::RunLoop().RunUntilIdle(); |
239 EXPECT_EQ(!opt_out, | 438 EXPECT_EQ(!opt_out, |
240 black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); | 439 black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); |
241 | 440 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); |
242 base::Time start = test_clock->Now(); | 441 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
243 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
244 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); | |
245 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); | 442 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); |
246 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 443 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
247 EXPECT_EQ(0, opt_out_store->clear_blacklist_count()); | 444 EXPECT_EQ(0, opt_out_store->clear_blacklist_count()); |
248 black_list->ClearBlackList( | 445 black_list->ClearBlackList( |
249 start, test_clock->Now() + base::TimeDelta::FromSeconds(1)); | 446 start, test_clock->Now() + base::TimeDelta::FromSeconds(1)); |
250 EXPECT_EQ(1, opt_out_store->clear_blacklist_count()); | 447 EXPECT_EQ(1, opt_out_store->clear_blacklist_count()); |
251 black_list->AddPreviewNavigation(url2, opt_out, PreviewsType::OFFLINE); | 448 black_list->AddPreviewNavigation(url2, opt_out, PreviewsType::OFFLINE); |
449 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
252 black_list->AddPreviewNavigation(url2, opt_out, PreviewsType::OFFLINE); | 450 black_list->AddPreviewNavigation(url2, opt_out, PreviewsType::OFFLINE); |
451 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
253 base::RunLoop().RunUntilIdle(); | 452 base::RunLoop().RunUntilIdle(); |
254 EXPECT_EQ(1, opt_out_store->clear_blacklist_count()); | 453 EXPECT_EQ(1, opt_out_store->clear_blacklist_count()); |
255 | 454 |
256 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); | 455 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); |
257 EXPECT_EQ(!opt_out, | 456 EXPECT_EQ(!opt_out, |
258 black_list->IsLoadedAndAllowed(url2, PreviewsType::OFFLINE)); | 457 black_list->IsLoadedAndAllowed(url2, PreviewsType::OFFLINE)); |
259 } | 458 } |
260 | 459 |
261 variations::testing::ClearAllVariationParams(); | 460 variations::testing::ClearAllVariationParams(); |
262 } | 461 } |
263 | 462 |
264 TEST_F(PreviewsBlackListTest, MaxHosts) { | 463 TEST_F(PreviewsBlackListTest, MaxHosts) { |
265 // Test that the black list only stores n hosts, and it stores the correct n | 464 // Test that the black list only stores n hosts, and it stores the correct n |
266 // hosts. | 465 // hosts. |
267 const GURL url_a("http://www.url_a.com"); | 466 const GURL url_a("http://www.url_a.com"); |
268 const GURL url_b("http://www.url_b.com"); | 467 const GURL url_b("http://www.url_b.com"); |
269 const GURL url_c("http://www.url_c.com"); | 468 const GURL url_c("http://www.url_c.com"); |
270 const GURL url_d("http://www.url_d.com"); | 469 const GURL url_d("http://www.url_d.com"); |
271 const GURL url_e("http://www.url_e.com"); | 470 const GURL url_e("http://www.url_e.com"); |
272 const size_t stored_history_length = 1; | 471 const size_t stored_history_length = 1; |
273 const int opt_out_threshold = 1; | 472 const int opt_out_threshold = 1; |
274 const int black_list_duration_in_days = 365; | 473 const int black_list_duration_in_days = 365; |
275 const size_t max_hosts_in_blacklist = 2; | 474 const size_t max_hosts_in_blacklist = 2; |
475 // Host indifferent blacklisting should have no effect with the following | |
476 // params. | |
477 const size_t host_indifferent_history = 1; | |
478 const int host_indifferent_threshold = 2; | |
276 base::FieldTrialList field_trial_list(nullptr); | 479 base::FieldTrialList field_trial_list(nullptr); |
277 std::map<std::string, std::string> params; | 480 std::map<std::string, std::string> params; |
278 params["stored_history_length"] = base::SizeTToString(stored_history_length); | 481 params["per_host_stored_history_length"] = |
279 params["opt_out_threshold"] = base::IntToString(opt_out_threshold); | 482 base::SizeTToString(stored_history_length); |
280 params["black_list_duration_in_days"] = | 483 params["per_host_opt_out_threshold"] = base::IntToString(opt_out_threshold); |
484 params["per_host_black_list_duration_in_days"] = | |
281 base::IntToString(black_list_duration_in_days); | 485 base::IntToString(black_list_duration_in_days); |
282 params["max_hosts_in_blacklist"] = | 486 params["max_hosts_in_blacklist"] = |
283 base::SizeTToString(max_hosts_in_blacklist); | 487 base::SizeTToString(max_hosts_in_blacklist); |
488 params["host_indifferent_max_stored_history_length"] = | |
489 base::SizeTToString(host_indifferent_history); | |
490 params["host_indifferent_opt_out_threshold"] = | |
491 base::IntToString(host_indifferent_threshold); | |
284 ASSERT_TRUE( | 492 ASSERT_TRUE( |
285 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", | 493 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", |
286 params) && | 494 params) && |
287 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); | 495 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); |
288 | 496 |
289 base::MessageLoop loop; | 497 base::MessageLoop loop; |
290 | 498 |
291 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); | 499 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); |
292 | 500 |
293 std::unique_ptr<PreviewsBlackList> black_list( | 501 std::unique_ptr<PreviewsBlackList> black_list( |
(...skipping 15 matching lines...) Expand all Loading... | |
309 black_list->AddPreviewNavigation(url_e, true, PreviewsType::OFFLINE); | 517 black_list->AddPreviewNavigation(url_e, true, PreviewsType::OFFLINE); |
310 // url_d and url_e should remain in the map, but url_a should be evicted. | 518 // url_d and url_e should remain in the map, but url_a should be evicted. |
311 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | 519 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); |
312 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_d, PreviewsType::OFFLINE)); | 520 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_d, PreviewsType::OFFLINE)); |
313 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_e, PreviewsType::OFFLINE)); | 521 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_e, PreviewsType::OFFLINE)); |
314 | 522 |
315 variations::testing::ClearAllVariationParams(); | 523 variations::testing::ClearAllVariationParams(); |
316 } | 524 } |
317 | 525 |
318 } // namespace previews | 526 } // namespace previews |
OLD | NEW |