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 = host_indifferent_history + 1; |
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 = host_indifferent_history + 1; |
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); |
| 230 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
192 | 231 |
193 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); | 232 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a1, PreviewsType::OFFLINE)); |
194 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); | 233 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url_a2, PreviewsType::OFFLINE)); |
195 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 234 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
196 | 235 |
197 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
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 urls[] = { |
219 const GURL url("http://www.url.com"); | 257 GURL("http://www.url_0.com"), GURL("http://www.url_1.com"), |
220 const GURL url2("http://www.url2.com"); | 258 GURL("http://www.url_2.com"), GURL("http://www.url_3.com"), |
| 259 }; |
| 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 = per_host_history + 1; |
| 263 const size_t host_indifferent_history = 4; |
| 264 const int host_indifferent_threshold = host_indifferent_history; |
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); |
| 279 params["single_opt_out_duration_in_seconds"] = |
| 280 base::IntToString(single_opt_out_duration); |
| 281 ASSERT_TRUE( |
| 282 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", |
| 283 params) && |
| 284 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); |
| 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(urls[0], PreviewsType::OFFLINE)); |
| 293 EXPECT_TRUE(black_list->IsLoadedAndAllowed(urls[1], PreviewsType::OFFLINE)); |
| 294 EXPECT_TRUE(black_list->IsLoadedAndAllowed(urls[2], PreviewsType::OFFLINE)); |
| 295 EXPECT_TRUE(black_list->IsLoadedAndAllowed(urls[3], PreviewsType::OFFLINE)); |
| 296 |
| 297 for (size_t i = 0; i < host_indifferent_threshold; i++) { |
| 298 black_list->AddPreviewNavigation(urls[i], true, PreviewsType::OFFLINE); |
| 299 EXPECT_EQ(i != 3, |
| 300 black_list->IsLoadedAndAllowed(urls[0], PreviewsType::OFFLINE)); |
| 301 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
| 302 } |
| 303 |
| 304 EXPECT_FALSE(black_list->IsLoadedAndAllowed(urls[0], PreviewsType::OFFLINE)); |
| 305 EXPECT_FALSE(black_list->IsLoadedAndAllowed(urls[1], PreviewsType::OFFLINE)); |
| 306 EXPECT_FALSE(black_list->IsLoadedAndAllowed(urls[2], PreviewsType::OFFLINE)); |
| 307 EXPECT_FALSE(black_list->IsLoadedAndAllowed(urls[3], PreviewsType::OFFLINE)); |
| 308 |
| 309 black_list->AddPreviewNavigation(urls[3], false, PreviewsType::OFFLINE); |
| 310 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
| 311 |
| 312 // New non-opt-out entry will cause these to be allowed now. |
| 313 EXPECT_TRUE(black_list->IsLoadedAndAllowed(urls[0], PreviewsType::OFFLINE)); |
| 314 EXPECT_TRUE(black_list->IsLoadedAndAllowed(urls[1], PreviewsType::OFFLINE)); |
| 315 EXPECT_TRUE(black_list->IsLoadedAndAllowed(urls[2], PreviewsType::OFFLINE)); |
| 316 EXPECT_TRUE(black_list->IsLoadedAndAllowed(urls[3], PreviewsType::OFFLINE)); |
| 317 |
| 318 variations::testing::ClearAllVariationParams(); |
| 319 } |
| 320 |
| 321 TEST_F(PreviewsBlackListTest, QueueBehavior) { |
| 322 // Tests the black list asynchronous queue behavior. Methods called while |
| 323 // loading the opt-out store are queued and should run in the order they were |
| 324 // queued. |
| 325 const GURL url("http://www.url.com"); |
| 326 const GURL url2("http://www.url2.com"); |
| 327 // Host indifferent blacklisting should have no effect with the following |
| 328 // params. |
| 329 const size_t host_indifferent_history = 1; |
| 330 const int host_indifferent_threshold = host_indifferent_history + 1; |
| 331 const int duration_in_days = 365; |
| 332 // Disable single opt out by setting duration to 0. |
| 333 const int single_opt_out_duration = 0; |
| 334 base::FieldTrialList field_trial_list(nullptr); |
| 335 std::map<std::string, std::string> params; |
| 336 params["per_host_black_list_duration_in_days"] = |
| 337 base::IntToString(duration_in_days); |
| 338 params["host_indifferent_max_stored_history_length"] = |
| 339 base::SizeTToString(host_indifferent_history); |
| 340 params["host_indifferent_opt_out_threshold"] = |
| 341 base::IntToString(host_indifferent_threshold); |
227 params["single_opt_out_duration_in_seconds"] = | 342 params["single_opt_out_duration_in_seconds"] = |
228 base::IntToString(single_opt_out_duration); | 343 base::IntToString(single_opt_out_duration); |
229 ASSERT_TRUE( | 344 ASSERT_TRUE( |
230 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", | 345 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", |
231 params) && | 346 params) && |
232 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); | 347 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); |
233 | 348 |
234 base::MessageLoop loop; | 349 base::MessageLoop loop; |
235 | 350 |
236 std::vector<bool> test_opt_out{true, false}; | 351 std::vector<bool> test_opt_out{true, false}; |
237 | 352 |
238 for (auto opt_out : test_opt_out) { | 353 for (auto opt_out : test_opt_out) { |
239 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); | 354 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); |
| 355 base::Time start = test_clock->Now(); |
240 | 356 |
241 TestPreviewsOptOutStore* opt_out_store = new TestPreviewsOptOutStore(); | 357 TestPreviewsOptOutStore* opt_out_store = new TestPreviewsOptOutStore(); |
242 | 358 |
243 std::unique_ptr<PreviewsBlackList> black_list(new PreviewsBlackList( | 359 std::unique_ptr<PreviewsBlackList> black_list(new PreviewsBlackList( |
244 base::WrapUnique(opt_out_store), base::WrapUnique(test_clock))); | 360 base::WrapUnique(opt_out_store), base::WrapUnique(test_clock))); |
245 | 361 |
246 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); | 362 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); |
247 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); | 363 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); |
| 364 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
248 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); | 365 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); |
| 366 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
249 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); | 367 EXPECT_FALSE(black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); |
250 base::RunLoop().RunUntilIdle(); | 368 base::RunLoop().RunUntilIdle(); |
251 EXPECT_EQ(!opt_out, | 369 EXPECT_EQ(!opt_out, |
252 black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); | 370 black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); |
253 | 371 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); |
254 base::Time start = test_clock->Now(); | |
255 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 372 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
256 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); | 373 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); |
257 black_list->AddPreviewNavigation(url, opt_out, PreviewsType::OFFLINE); | |
258 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | 374 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
259 EXPECT_EQ(0, opt_out_store->clear_blacklist_count()); | 375 EXPECT_EQ(0, opt_out_store->clear_blacklist_count()); |
260 black_list->ClearBlackList( | 376 black_list->ClearBlackList( |
261 start, test_clock->Now() + base::TimeDelta::FromSeconds(1)); | 377 start, test_clock->Now() + base::TimeDelta::FromSeconds(1)); |
262 EXPECT_EQ(1, opt_out_store->clear_blacklist_count()); | 378 EXPECT_EQ(1, opt_out_store->clear_blacklist_count()); |
263 black_list->AddPreviewNavigation(url2, opt_out, PreviewsType::OFFLINE); | 379 black_list->AddPreviewNavigation(url2, opt_out, PreviewsType::OFFLINE); |
| 380 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
264 black_list->AddPreviewNavigation(url2, opt_out, PreviewsType::OFFLINE); | 381 black_list->AddPreviewNavigation(url2, opt_out, PreviewsType::OFFLINE); |
| 382 test_clock->Advance(base::TimeDelta::FromSeconds(1)); |
265 base::RunLoop().RunUntilIdle(); | 383 base::RunLoop().RunUntilIdle(); |
266 EXPECT_EQ(1, opt_out_store->clear_blacklist_count()); | 384 EXPECT_EQ(1, opt_out_store->clear_blacklist_count()); |
267 | 385 |
268 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); | 386 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url, PreviewsType::OFFLINE)); |
269 EXPECT_EQ(!opt_out, | 387 EXPECT_EQ(!opt_out, |
270 black_list->IsLoadedAndAllowed(url2, PreviewsType::OFFLINE)); | 388 black_list->IsLoadedAndAllowed(url2, PreviewsType::OFFLINE)); |
271 } | 389 } |
272 | 390 |
273 variations::testing::ClearAllVariationParams(); | 391 variations::testing::ClearAllVariationParams(); |
274 } | 392 } |
275 | 393 |
276 TEST_F(PreviewsBlackListTest, MaxHosts) { | 394 TEST_F(PreviewsBlackListTest, MaxHosts) { |
277 // Test that the black list only stores n hosts, and it stores the correct n | 395 // Test that the black list only stores n hosts, and it stores the correct n |
278 // hosts. | 396 // hosts. |
279 const GURL url_a("http://www.url_a.com"); | 397 const GURL url_a("http://www.url_a.com"); |
280 const GURL url_b("http://www.url_b.com"); | 398 const GURL url_b("http://www.url_b.com"); |
281 const GURL url_c("http://www.url_c.com"); | 399 const GURL url_c("http://www.url_c.com"); |
282 const GURL url_d("http://www.url_d.com"); | 400 const GURL url_d("http://www.url_d.com"); |
283 const GURL url_e("http://www.url_e.com"); | 401 const GURL url_e("http://www.url_e.com"); |
284 const size_t stored_history_length = 1; | 402 const size_t stored_history_length = 1; |
285 const int opt_out_threshold = 1; | 403 const int opt_out_threshold = 1; |
286 const int black_list_duration_in_days = 365; | 404 const int black_list_duration_in_days = 365; |
287 // Disable single opt out by setting duration to 0. | 405 // Disable single opt out by setting duration to 0. |
288 const int single_opt_out_duration = 0; | 406 const int single_opt_out_duration = 0; |
289 const size_t max_hosts_in_blacklist = 2; | 407 const size_t max_hosts_in_blacklist = 2; |
| 408 // Host indifferent blacklisting should have no effect with the following |
| 409 // params. |
| 410 const size_t host_indifferent_history = 1; |
| 411 const int host_indifferent_threshold = host_indifferent_history + 1; |
290 base::FieldTrialList field_trial_list(nullptr); | 412 base::FieldTrialList field_trial_list(nullptr); |
291 std::map<std::string, std::string> params; | 413 std::map<std::string, std::string> params; |
292 params["stored_history_length"] = base::SizeTToString(stored_history_length); | 414 params["per_host_stored_history_length"] = |
293 params["opt_out_threshold"] = base::IntToString(opt_out_threshold); | 415 base::SizeTToString(stored_history_length); |
294 params["black_list_duration_in_days"] = | 416 params["per_host_opt_out_threshold"] = base::IntToString(opt_out_threshold); |
| 417 params["per_host_black_list_duration_in_days"] = |
295 base::IntToString(black_list_duration_in_days); | 418 base::IntToString(black_list_duration_in_days); |
296 params["max_hosts_in_blacklist"] = | 419 params["max_hosts_in_blacklist"] = |
297 base::SizeTToString(max_hosts_in_blacklist); | 420 base::SizeTToString(max_hosts_in_blacklist); |
| 421 params["host_indifferent_max_stored_history_length"] = |
| 422 base::SizeTToString(host_indifferent_history); |
| 423 params["host_indifferent_opt_out_threshold"] = |
| 424 base::IntToString(host_indifferent_threshold); |
298 params["single_opt_out_duration_in_seconds"] = | 425 params["single_opt_out_duration_in_seconds"] = |
299 base::IntToString(single_opt_out_duration); | 426 base::IntToString(single_opt_out_duration); |
300 ASSERT_TRUE( | 427 ASSERT_TRUE( |
301 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", | 428 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", |
302 params) && | 429 params) && |
303 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); | 430 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); |
304 | 431 |
305 base::MessageLoop loop; | 432 base::MessageLoop loop; |
306 | 433 |
307 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); | 434 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 | 462 // Test that when a user opts out of a preview, previews won't be shown until |
336 // |single_opt_out_duration| has elapsed. | 463 // |single_opt_out_duration| has elapsed. |
337 const GURL url_a("http://www.url_a.com"); | 464 const GURL url_a("http://www.url_a.com"); |
338 const GURL url_b("http://www.url_b.com"); | 465 const GURL url_b("http://www.url_b.com"); |
339 const GURL url_c("http://www.url_c.com"); | 466 const GURL url_c("http://www.url_c.com"); |
340 const size_t stored_history_length = 1; | 467 const size_t stored_history_length = 1; |
341 const int opt_out_threshold = 2; | 468 const int opt_out_threshold = 2; |
342 const int black_list_duration_in_days = 365; | 469 const int black_list_duration_in_days = 365; |
343 const int single_opt_out_duration = 5; | 470 const int single_opt_out_duration = 5; |
344 const size_t max_hosts_in_blacklist = 10; | 471 const size_t max_hosts_in_blacklist = 10; |
| 472 // Host indifferent blacklisting should have no effect with the following |
| 473 // params. |
| 474 const size_t host_indifferent_history = 1; |
| 475 const int host_indifferent_threshold = host_indifferent_history + 1; |
345 base::FieldTrialList field_trial_list(nullptr); | 476 base::FieldTrialList field_trial_list(nullptr); |
346 std::map<std::string, std::string> params; | 477 std::map<std::string, std::string> params; |
347 params["stored_history_length"] = base::SizeTToString(stored_history_length); | 478 params["per_host_stored_history_length"] = |
348 params["opt_out_threshold"] = base::IntToString(opt_out_threshold); | 479 base::SizeTToString(stored_history_length); |
349 params["black_list_duration_in_days"] = | 480 params["per_host_opt_out_threshold"] = base::IntToString(opt_out_threshold); |
| 481 params["per_host_black_list_duration_in_days"] = |
350 base::IntToString(black_list_duration_in_days); | 482 base::IntToString(black_list_duration_in_days); |
351 params["max_hosts_in_blacklist"] = | 483 params["max_hosts_in_blacklist"] = |
352 base::SizeTToString(max_hosts_in_blacklist); | 484 base::SizeTToString(max_hosts_in_blacklist); |
| 485 params["host_indifferent_max_stored_history_length"] = |
| 486 base::SizeTToString(host_indifferent_history); |
| 487 params["host_indifferent_opt_out_threshold"] = |
| 488 base::IntToString(host_indifferent_threshold); |
353 params["single_opt_out_duration_in_seconds"] = | 489 params["single_opt_out_duration_in_seconds"] = |
354 base::IntToString(single_opt_out_duration); | 490 base::IntToString(single_opt_out_duration); |
355 ASSERT_TRUE( | 491 ASSERT_TRUE( |
356 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", | 492 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", |
357 params) && | 493 params) && |
358 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); | 494 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); |
359 | 495 |
360 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); | 496 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); |
361 | 497 |
362 std::unique_ptr<PreviewsBlackList> black_list( | 498 std::unique_ptr<PreviewsBlackList> black_list( |
(...skipping 19 matching lines...) Expand all Loading... |
382 test_clock->Advance( | 518 test_clock->Advance( |
383 base::TimeDelta::FromSeconds(single_opt_out_duration + 1)); | 519 base::TimeDelta::FromSeconds(single_opt_out_duration + 1)); |
384 | 520 |
385 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); | 521 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_b, PreviewsType::OFFLINE)); |
386 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE)); | 522 EXPECT_TRUE(black_list->IsLoadedAndAllowed(url_c, PreviewsType::OFFLINE)); |
387 | 523 |
388 variations::testing::ClearAllVariationParams(); | 524 variations::testing::ClearAllVariationParams(); |
389 } | 525 } |
390 | 526 |
391 } // namespace previews | 527 } // namespace previews |
OLD | NEW |