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; | |
tbansal1
2016/10/28 21:59:33
s/2/host_indifferent_history + 1/
Also, why are so
RyanSturm
2016/11/02 19:52:30
Done.
size_t is used here for the actual size of
| |
79 const int duration_in_days = 365; | 89 const int duration_in_days = 365; |
80 // Disable single opt out by setting duration to 0. | 90 // Disable single opt out by setting duration to 0. |
81 const int single_opt_out_duration = 0; | 91 const int single_opt_out_duration = 0; |
82 base::FieldTrialList field_trial_list(nullptr); | 92 base::FieldTrialList field_trial_list(nullptr); |
83 std::map<std::string, std::string> params; | 93 std::map<std::string, std::string> params; |
84 params["stored_history_length"] = base::SizeTToString(history); | 94 params["per_host_max_stored_history_length"] = |
85 params["opt_out_threshold"] = base::IntToString(threshold); | 95 base::SizeTToString(per_host_history); |
86 params["black_list_duration_in_days"] = base::IntToString(duration_in_days); | 96 params["host_indifferent_max_stored_history_length"] = |
97 base::SizeTToString(host_indifferent_history); | |
98 params["per_host_opt_out_threshold"] = base::IntToString(per_host_threshold); | |
99 params["host_indifferent_opt_out_threshold"] = | |
100 base::IntToString(host_indifferent_threshold); | |
101 params["per_host_black_list_duration_in_days"] = | |
102 base::IntToString(duration_in_days); | |
87 params["single_opt_out_duration_in_seconds"] = | 103 params["single_opt_out_duration_in_seconds"] = |
88 base::IntToString(single_opt_out_duration); | 104 base::IntToString(single_opt_out_duration); |
89 ASSERT_TRUE( | 105 ASSERT_TRUE( |
90 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", | 106 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", |
91 params) && | 107 params) && |
92 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); | 108 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); |
93 | 109 |
94 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); | 110 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); |
95 base::Time start = test_clock->Now(); | 111 base::Time start = test_clock->Now(); |
96 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 112 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
97 | 113 |
98 std::unique_ptr<PreviewsBlackList> black_list( | 114 std::unique_ptr<PreviewsBlackList> black_list( |
99 new PreviewsBlackList(nullptr, base::WrapUnique(test_clock))); | 115 new PreviewsBlackList(nullptr, base::WrapUnique(test_clock))); |
100 | 116 |
101 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | 117 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); |
102 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 118 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
103 | 119 |
104 black_list->AddPreviewNavigation(url_a, true, PreviewsType::OFFLINE); | 120 black_list->AddPreviewNavigation(url_a, true, PreviewsType::OFFLINE); |
121 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
105 black_list->AddPreviewNavigation(url_a, true, PreviewsType::OFFLINE); | 122 black_list->AddPreviewNavigation(url_a, true, PreviewsType::OFFLINE); |
123 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
106 | 124 |
107 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | 125 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); |
108 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 126 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
109 | 127 |
110 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); | 128 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); |
129 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
111 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); | 130 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); |
131 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
112 | 132 |
113 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | 133 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); |
114 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 134 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
115 | 135 |
116 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)); | |
117 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); | 138 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); |
139 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
118 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); | 140 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); |
141 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
119 | 142 |
120 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | 143 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); |
121 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 144 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
122 | 145 |
123 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
124 black_list->ClearBlackList(start, test_clock->Now()); | 146 black_list->ClearBlackList(start, test_clock->Now()); |
125 | 147 |
126 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | 148 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); |
127 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 149 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
128 | 150 |
129 variations::testing::ClearAllVariationParams(); | 151 variations::testing::ClearAllVariationParams(); |
130 } | 152 } |
131 | 153 |
132 TEST_F(PreviewsBlackListTest, BlackListWithStore) { | 154 TEST_F(PreviewsBlackListTest, PerHostBlackListWithStore) { |
133 // Tests the black list behavior when a non-null OptOutSture is passed in. | 155 // Tests the black list behavior when a non-null OptOutSture is passed in. |
134 const GURL url_a1("http://www.url_a.com/a1"); | 156 const GURL url_a1("http://www.url_a.com/a1"); |
135 const GURL url_a2("http://www.url_a.com/a2"); | 157 const GURL url_a2("http://www.url_a.com/a2"); |
136 const GURL url_b("http://www.url_b.com"); | 158 const GURL url_b("http://www.url_b.com"); |
137 const size_t history = 4; | 159 const size_t per_host_history = 4; |
138 const int threshold = 2; | 160 const int per_host_threshold = 2; |
161 // Host indifferent blacklisting should have no effect with the following | |
162 // params. | |
163 const size_t host_indifferent_history = 1; | |
164 const int host_indifferent_threshold = 2; | |
139 const int duration_in_days = 365; | 165 const int duration_in_days = 365; |
140 // Disable single opt out by setting duration to 0. | 166 // Disable single opt out by setting duration to 0. |
141 const int single_opt_out_duration = 0; | 167 const int single_opt_out_duration = 0; |
142 base::FieldTrialList field_trial_list(nullptr); | 168 base::FieldTrialList field_trial_list(nullptr); |
143 std::map<std::string, std::string> params; | 169 std::map<std::string, std::string> params; |
144 params["stored_history_length"] = base::SizeTToString(history); | 170 params["per_host_max_stored_history_length"] = |
145 params["opt_out_threshold"] = base::IntToString(threshold); | 171 base::SizeTToString(per_host_history); |
146 params["black_list_duration_in_days"] = base::IntToString(duration_in_days); | 172 params["host_indifferent_max_stored_history_length"] = |
173 base::SizeTToString(host_indifferent_history); | |
174 params["per_host_opt_out_threshold"] = base::IntToString(per_host_threshold); | |
175 params["host_indifferent_opt_out_threshold"] = | |
176 base::IntToString(host_indifferent_threshold); | |
177 params["per_host_black_list_duration_in_days"] = | |
178 base::IntToString(duration_in_days); | |
147 params["single_opt_out_duration_in_seconds"] = | 179 params["single_opt_out_duration_in_seconds"] = |
148 base::IntToString(single_opt_out_duration); | 180 base::IntToString(single_opt_out_duration); |
149 ASSERT_TRUE( | 181 ASSERT_TRUE( |
150 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", | 182 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", |
151 params) && | 183 params) && |
152 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); | 184 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); |
153 | 185 |
154 base::MessageLoop loop; | 186 base::MessageLoop loop; |
155 | 187 |
156 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); | 188 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); |
157 base::Time start = test_clock->Now(); | 189 base::Time start = test_clock->Now(); |
158 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 190 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
159 | 191 |
160 TestPreviewsOptOutStore* opt_out_store = new TestPreviewsOptOutStore(); | 192 TestPreviewsOptOutStore* opt_out_store = new TestPreviewsOptOutStore(); |
161 | 193 |
162 std::unique_ptr<PreviewsBlackList> black_list(new PreviewsBlackList( | 194 std::unique_ptr<PreviewsBlackList> black_list(new PreviewsBlackList( |
163 base::WrapUnique(opt_out_store), base::WrapUnique(test_clock))); | 195 base::WrapUnique(opt_out_store), base::WrapUnique(test_clock))); |
164 | 196 |
165 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); | 197 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
166 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); | 198 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); |
167 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 199 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
168 | 200 |
169 base::RunLoop().RunUntilIdle(); | 201 base::RunLoop().RunUntilIdle(); |
170 | 202 |
171 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); | 203 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
172 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); | 204 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); |
173 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 205 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
174 | 206 |
175 black_list->AddPreviewNavigation(url_a1, true, PreviewsType::OFFLINE); | 207 black_list->AddPreviewNavigation(url_a1, true, PreviewsType::OFFLINE); |
208 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
176 black_list->AddPreviewNavigation(url_a1, true, PreviewsType::OFFLINE); | 209 black_list->AddPreviewNavigation(url_a1, true, PreviewsType::OFFLINE); |
210 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
177 | 211 |
178 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); | 212 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
179 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); | 213 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); |
180 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 214 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
181 | 215 |
182 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); | 216 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); |
217 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
183 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); | 218 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); |
219 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
184 | 220 |
185 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); | 221 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
186 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); | 222 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); |
187 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 223 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
188 | 224 |
189 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); | 225 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); |
226 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
190 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); | 227 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); |
228 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
191 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); | 229 black_list->AddPreviewNavigation(url_b, false, PreviewsType::OFFLINE); |
192 | 230 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
193 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); | 231 |
194 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); | 232 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
195 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 233 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); |
196 | 234 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
197 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 235 |
198 EXPECT_EQ(0, opt_out_store->clear_blacklist_count()); | 236 EXPECT_EQ(0, opt_out_store->clear_blacklist_count()); |
199 black_list->ClearBlackList(start, base::Time::Now()); | 237 black_list->ClearBlackList(start, base::Time::Now()); |
200 EXPECT_EQ(1, opt_out_store->clear_blacklist_count()); | 238 EXPECT_EQ(1, opt_out_store->clear_blacklist_count()); |
201 | 239 |
202 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); | 240 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
203 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); | 241 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); |
204 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 242 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
205 | 243 |
206 base::RunLoop().RunUntilIdle(); | 244 base::RunLoop().RunUntilIdle(); |
207 EXPECT_EQ(1, opt_out_store->clear_blacklist_count()); | 245 EXPECT_EQ(1, opt_out_store->clear_blacklist_count()); |
208 | 246 |
209 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); | 247 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
210 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); | 248 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
211 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 249 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
212 | 250 |
213 variations::testing::ClearAllVariationParams(); | 251 variations::testing::ClearAllVariationParams(); |
214 } | 252 } |
215 | 253 |
216 TEST_F(PreviewsBlackListTest, QueueBehavior) { | 254 TEST_F(PreviewsBlackListTest, HostIndifferentBlackList) { |
217 // Tests the black list asynchronous queue behavior. Methods called while | 255 // Tests the black list behavior when a null OptOutSture is passed in. |
218 // loading are queued and should run in the order they were queued. | 256 const GURL url_a("http://www.url_a.com"); |
219 const GURL url("http://www.url.com"); | 257 const GURL url_b("http://www.url_b.com"); |
220 const GURL url2("http://www.url2.com"); | 258 const GURL url_c("http://www.url_c.com"); |
259 const GURL url_d("http://www.url_d.com"); | |
260 // Per host blacklisting should have no effect with the following params. | |
261 const size_t per_host_history = 1; | |
262 const int per_host_threshold = 2; | |
tbansal1
2016/10/28 21:59:34
s/2/per_host_history + 1/
RyanSturm
2016/11/02 19:52:30
Done.
| |
263 const size_t host_indifferent_history = 4; | |
264 const int host_indifferent_threshold = 4; | |
221 const int duration_in_days = 365; | 265 const int duration_in_days = 365; |
222 // Disable single opt out by setting duration to 0. | 266 // Disable single opt out by setting duration to 0. |
223 const int single_opt_out_duration = 0; | 267 const int single_opt_out_duration = 0; |
224 base::FieldTrialList field_trial_list(nullptr); | 268 base::FieldTrialList field_trial_list(nullptr); |
225 std::map<std::string, std::string> params; | 269 std::map<std::string, std::string> params; |
226 params["black_list_duration_in_days"] = base::IntToString(duration_in_days); | 270 params["per_host_max_stored_history_length"] = |
271 base::SizeTToString(per_host_history); | |
272 params["host_indifferent_max_stored_history_length"] = | |
273 base::SizeTToString(host_indifferent_history); | |
274 params["per_host_opt_out_threshold"] = base::IntToString(per_host_threshold); | |
275 params["host_indifferent_opt_out_threshold"] = | |
276 base::IntToString(host_indifferent_threshold); | |
277 params["per_host_black_list_duration_in_days"] = | |
278 base::IntToString(duration_in_days); | |
227 params["single_opt_out_duration_in_seconds"] = | 279 params["single_opt_out_duration_in_seconds"] = |
228 base::IntToString(single_opt_out_duration); | 280 base::IntToString(single_opt_out_duration); |
229 ASSERT_TRUE( | 281 ASSERT_TRUE( |
230 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", | 282 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", |
231 params) && | 283 params) && |
232 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); | 284 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); |
233 | 285 |
286 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); | |
287 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
288 | |
289 std::unique_ptr<PreviewsBlackList> black_list( | |
290 new PreviewsBlackList(nullptr, base::WrapUnique(test_clock))); | |
291 | |
292 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | |
293 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | |
294 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE)); | |
295 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_d, PreviewsType::OFFLINE)); | |
296 | |
297 black_list->AddPreviewNavigation(url_a, true, PreviewsType::OFFLINE); | |
298 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
299 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | |
300 | |
301 black_list->AddPreviewNavigation(url_b, true, PreviewsType::OFFLINE); | |
302 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
303 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | |
304 | |
305 black_list->AddPreviewNavigation(url_c, true, PreviewsType::OFFLINE); | |
306 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
307 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | |
308 | |
309 black_list->AddPreviewNavigation(url_d, true, PreviewsType::OFFLINE); | |
tbansal1
2016/10/28 21:59:33
can you put this in a loop:
for(size_t i=0; i< hos
RyanSturm
2016/11/02 19:52:30
Done.
| |
310 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
311 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | |
312 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | |
313 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE)); | |
314 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_d, PreviewsType::OFFLINE)); | |
315 | |
316 black_list->AddPreviewNavigation(url_d, false, PreviewsType::OFFLINE); | |
317 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
318 | |
319 // New non-opt-out entry will cause these to be allowed now. | |
320 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_a, PreviewsType::OFFLINE)); | |
321 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | |
322 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE)); | |
323 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_d, PreviewsType::OFFLINE)); | |
324 | |
325 variations::testing::ClearAllVariationParams(); | |
326 } | |
327 | |
328 TEST_F(PreviewsBlackListTest, QueueBehavior) { | |
329 // Tests the black list asynchronous queue behavior. Methods called while | |
330 // loading are queued and should run in the order they were queued. | |
tbansal1
2016/10/28 21:59:34
s/loading/loading opt-out store/
It is not clear
RyanSturm
2016/11/02 19:52:30
Done.
| |
331 const GURL url("http://www.url.com"); | |
332 const GURL url2("http://www.url2.com"); | |
333 // Host indifferent blacklisting should have no effect with the following | |
334 // params. | |
335 const size_t host_indifferent_history = 1; | |
336 const int host_indifferent_threshold = 2; | |
337 const int duration_in_days = 365; | |
338 // Disable single opt out by setting duration to 0. | |
339 const int single_opt_out_duration = 0; | |
340 base::FieldTrialList field_trial_list(nullptr); | |
341 std::map<std::string, std::string> params; | |
342 params["per_host_black_list_duration_in_days"] = | |
343 base::IntToString(duration_in_days); | |
344 params["host_indifferent_max_stored_history_length"] = | |
345 base::SizeTToString(host_indifferent_history); | |
346 params["host_indifferent_opt_out_threshold"] = | |
347 base::IntToString(host_indifferent_threshold); | |
348 params["single_opt_out_duration_in_seconds"] = | |
349 base::IntToString(single_opt_out_duration); | |
350 ASSERT_TRUE( | |
351 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", | |
352 params) && | |
353 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); | |
354 | |
234 base::MessageLoop loop; | 355 base::MessageLoop loop; |
235 | 356 |
236 std::vector<bool> test_opt_out{true, false}; | 357 std::vector<bool> test_opt_out{true, false}; |
237 | 358 |
238 for (auto opt_out : test_opt_out) { | 359 for (auto opt_out : test_opt_out) { |
239 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); | 360 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); |
361 base::Time start = test_clock->Now(); | |
240 | 362 |
241 TestPreviewsOptOutStore* opt_out_store = new TestPreviewsOptOutStore(); | 363 TestPreviewsOptOutStore* opt_out_store = new TestPreviewsOptOutStore(); |
242 | 364 |
243 std::unique_ptr<PreviewsBlackList> black_list(new PreviewsBlackList( | 365 std::unique_ptr<PreviewsBlackList> black_list(new PreviewsBlackList( |
244 base::WrapUnique(opt_out_store), base::WrapUnique(test_clock))); | 366 base::WrapUnique(opt_out_store), base::WrapUnique(test_clock))); |
245 | 367 |
246 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); | 368 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); |
247 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); | 369 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); |
248 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); | 370 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
371 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); | |
372 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
249 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); | 373 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); |
250 base::RunLoop().RunUntilIdle(); | 374 base::RunLoop().RunUntilIdle(); |
251 EXPECT_EQ(!opt_out, | 375 EXPECT_EQ(!opt_out, |
252 black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); | 376 black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); |
253 | 377 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); |
254 base::Time start = test_clock->Now(); | 378 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
255 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
256 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); | |
257 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); | 379 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); |
258 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 380 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
259 EXPECT_EQ(0, opt_out_store->clear_blacklist_count()); | 381 EXPECT_EQ(0, opt_out_store->clear_blacklist_count()); |
260 black_list->ClearBlackList( | 382 black_list->ClearBlackList( |
261 start, test_clock->Now() + base::TimeDelta::FromSeconds(1)); | 383 start, test_clock->Now() + base::TimeDelta::FromSeconds(1)); |
262 EXPECT_EQ(1, opt_out_store->clear_blacklist_count()); | 384 EXPECT_EQ(1, opt_out_store->clear_blacklist_count()); |
263 black_list->AddPreviewNavigation(url2, opt_out, PreviewsType::OFFLINE); | 385 black_list->AddPreviewNavigation(url2, opt_out, PreviewsType::OFFLINE); |
386 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
264 black_list->AddPreviewNavigation(url2, opt_out, PreviewsType::OFFLINE); | 387 black_list->AddPreviewNavigation(url2, opt_out, PreviewsType::OFFLINE); |
388 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
265 base::RunLoop().RunUntilIdle(); | 389 base::RunLoop().RunUntilIdle(); |
266 EXPECT_EQ(1, opt_out_store->clear_blacklist_count()); | 390 EXPECT_EQ(1, opt_out_store->clear_blacklist_count()); |
267 | 391 |
268 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); | 392 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); |
269 EXPECT_EQ(!opt_out, | 393 EXPECT_EQ(!opt_out, |
270 black_list->IsLoadedAndAllowed(url2, PreviewsType::OFFLINE)); | 394 black_list->IsLoadedAndAllowed(url2, PreviewsType::OFFLINE)); |
271 } | 395 } |
272 | 396 |
273 variations::testing::ClearAllVariationParams(); | 397 variations::testing::ClearAllVariationParams(); |
274 } | 398 } |
275 | 399 |
276 TEST_F(PreviewsBlackListTest, MaxHosts) { | 400 TEST_F(PreviewsBlackListTest, MaxHosts) { |
277 // Test that the black list only stores n hosts, and it stores the correct n | 401 // Test that the black list only stores n hosts, and it stores the correct n |
278 // hosts. | 402 // hosts. |
279 const GURL url_a("http://www.url_a.com"); | 403 const GURL url_a("http://www.url_a.com"); |
280 const GURL url_b("http://www.url_b.com"); | 404 const GURL url_b("http://www.url_b.com"); |
281 const GURL url_c("http://www.url_c.com"); | 405 const GURL url_c("http://www.url_c.com"); |
282 const GURL url_d("http://www.url_d.com"); | 406 const GURL url_d("http://www.url_d.com"); |
283 const GURL url_e("http://www.url_e.com"); | 407 const GURL url_e("http://www.url_e.com"); |
284 const size_t stored_history_length = 1; | 408 const size_t stored_history_length = 1; |
285 const int opt_out_threshold = 1; | 409 const int opt_out_threshold = 1; |
286 const int black_list_duration_in_days = 365; | 410 const int black_list_duration_in_days = 365; |
287 // Disable single opt out by setting duration to 0. | 411 // Disable single opt out by setting duration to 0. |
288 const int single_opt_out_duration = 0; | 412 const int single_opt_out_duration = 0; |
289 const size_t max_hosts_in_blacklist = 2; | 413 const size_t max_hosts_in_blacklist = 2; |
414 // Host indifferent blacklisting should have no effect with the following | |
415 // params. | |
416 const size_t host_indifferent_history = 1; | |
417 const int host_indifferent_threshold = 2; | |
290 base::FieldTrialList field_trial_list(nullptr); | 418 base::FieldTrialList field_trial_list(nullptr); |
291 std::map<std::string, std::string> params; | 419 std::map<std::string, std::string> params; |
292 params["stored_history_length"] = base::SizeTToString(stored_history_length); | 420 params["per_host_stored_history_length"] = |
293 params["opt_out_threshold"] = base::IntToString(opt_out_threshold); | 421 base::SizeTToString(stored_history_length); |
294 params["black_list_duration_in_days"] = | 422 params["per_host_opt_out_threshold"] = base::IntToString(opt_out_threshold); |
423 params["per_host_black_list_duration_in_days"] = | |
295 base::IntToString(black_list_duration_in_days); | 424 base::IntToString(black_list_duration_in_days); |
296 params["max_hosts_in_blacklist"] = | 425 params["max_hosts_in_blacklist"] = |
297 base::SizeTToString(max_hosts_in_blacklist); | 426 base::SizeTToString(max_hosts_in_blacklist); |
427 params["host_indifferent_max_stored_history_length"] = | |
428 base::SizeTToString(host_indifferent_history); | |
429 params["host_indifferent_opt_out_threshold"] = | |
430 base::IntToString(host_indifferent_threshold); | |
298 params["single_opt_out_duration_in_seconds"] = | 431 params["single_opt_out_duration_in_seconds"] = |
299 base::IntToString(single_opt_out_duration); | 432 base::IntToString(single_opt_out_duration); |
300 ASSERT_TRUE( | 433 ASSERT_TRUE( |
301 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", | 434 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", |
302 params) && | 435 params) && |
303 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); | 436 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); |
304 | 437 |
305 base::MessageLoop loop; | 438 base::MessageLoop loop; |
306 | 439 |
307 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); | 440 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); |
(...skipping 27 matching lines...) Expand all Loading... | |
335 // Test that when a user opts out of a preview, previews won't be shown until | 468 // Test that when a user opts out of a preview, previews won't be shown until |
336 // |single_opt_out_duration| has elapsed. | 469 // |single_opt_out_duration| has elapsed. |
337 const GURL url_a("http://www.url_a.com"); | 470 const GURL url_a("http://www.url_a.com"); |
338 const GURL url_b("http://www.url_b.com"); | 471 const GURL url_b("http://www.url_b.com"); |
339 const GURL url_c("http://www.url_c.com"); | 472 const GURL url_c("http://www.url_c.com"); |
340 const size_t stored_history_length = 1; | 473 const size_t stored_history_length = 1; |
341 const int opt_out_threshold = 2; | 474 const int opt_out_threshold = 2; |
342 const int black_list_duration_in_days = 365; | 475 const int black_list_duration_in_days = 365; |
343 const int single_opt_out_duration = 5; | 476 const int single_opt_out_duration = 5; |
344 const size_t max_hosts_in_blacklist = 10; | 477 const size_t max_hosts_in_blacklist = 10; |
478 // Host indifferent blacklisting should have no effect with the following | |
479 // params. | |
480 const size_t host_indifferent_history = 1; | |
481 const int host_indifferent_threshold = 2; | |
tbansal1
2016/10/28 21:59:33
same comment here and elsewhere:
s/2/host_indiffer
RyanSturm
2016/11/02 19:52:30
Done.
| |
345 base::FieldTrialList field_trial_list(nullptr); | 482 base::FieldTrialList field_trial_list(nullptr); |
346 std::map<std::string, std::string> params; | 483 std::map<std::string, std::string> params; |
347 params["stored_history_length"] = base::SizeTToString(stored_history_length); | 484 params["per_host_stored_history_length"] = |
348 params["opt_out_threshold"] = base::IntToString(opt_out_threshold); | 485 base::SizeTToString(stored_history_length); |
349 params["black_list_duration_in_days"] = | 486 params["per_host_opt_out_threshold"] = base::IntToString(opt_out_threshold); |
487 params["per_host_black_list_duration_in_days"] = | |
350 base::IntToString(black_list_duration_in_days); | 488 base::IntToString(black_list_duration_in_days); |
351 params["max_hosts_in_blacklist"] = | 489 params["max_hosts_in_blacklist"] = |
352 base::SizeTToString(max_hosts_in_blacklist); | 490 base::SizeTToString(max_hosts_in_blacklist); |
491 params["host_indifferent_max_stored_history_length"] = | |
492 base::SizeTToString(host_indifferent_history); | |
493 params["host_indifferent_opt_out_threshold"] = | |
494 base::IntToString(host_indifferent_threshold); | |
353 params["single_opt_out_duration_in_seconds"] = | 495 params["single_opt_out_duration_in_seconds"] = |
354 base::IntToString(single_opt_out_duration); | 496 base::IntToString(single_opt_out_duration); |
355 ASSERT_TRUE( | 497 ASSERT_TRUE( |
356 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", | 498 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", |
357 params) && | 499 params) && |
358 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); | 500 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); |
359 | 501 |
360 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); | 502 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); |
361 | 503 |
362 std::unique_ptr<PreviewsBlackList> black_list( | 504 std::unique_ptr<PreviewsBlackList> black_list( |
(...skipping 19 matching lines...) Expand all Loading... | |
382 test_clock->Advance( | 524 test_clock->Advance( |
383 base::TimeDelta::FromSeconds(single_opt_out_duration + 1)); | 525 base::TimeDelta::FromSeconds(single_opt_out_duration + 1)); |
384 | 526 |
385 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 527 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
386 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE)); | 528 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE)); |
387 | 529 |
388 variations::testing::ClearAllVariationParams(); | 530 variations::testing::ClearAllVariationParams(); |
389 } | 531 } |
390 | 532 |
391 } // namespace previews | 533 } // namespace previews |
OLD | NEW |