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

Side by Side Diff: components/data_reduction_proxy/content/browser/content_lofi_decider_unittest.cc

Issue 2073233002: Do not trigger Lo-Fi on main frame requests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/data_reduction_proxy/content/browser/content_lofi_decider.h " 5 #include "components/data_reduction_proxy/content/browser/content_lofi_decider.h "
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <memory> 8 #include <memory>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 std::unique_ptr<DataReductionProxyTestContext> test_context_; 167 std::unique_ptr<DataReductionProxyTestContext> test_context_;
168 std::unique_ptr<DataReductionProxyNetworkDelegate> 168 std::unique_ptr<DataReductionProxyNetworkDelegate>
169 data_reduction_proxy_network_delegate_; 169 data_reduction_proxy_network_delegate_;
170 }; 170 };
171 171
172 TEST_F(ContentLoFiDeciderTest, LoFiFlags) { 172 TEST_F(ContentLoFiDeciderTest, LoFiFlags) {
173 // Enable Lo-Fi. 173 // Enable Lo-Fi.
174 const struct { 174 const struct {
175 bool is_using_lofi; 175 bool is_using_lofi;
176 bool is_using_previews; 176 bool is_using_previews;
177 bool is_main_frame;
177 } tests[] = { 178 } tests[] = {
178 {false, false}, {true, false}, {false, true}, {true, true}, 179 {false, false, false}, {false, false, true}, {true, false, true},
180 {true, false, false}, {false, true, false}, {false, true, true},
181 {true, true, true}, {true, true, false},
179 }; 182 };
180 183
181 for (size_t i = 0; i < arraysize(tests); ++i) { 184 for (size_t i = 0; i < arraysize(tests); ++i) {
182 std::unique_ptr<net::URLRequest> request = 185 std::unique_ptr<net::URLRequest> request =
183 CreateRequest(tests[i].is_using_lofi); 186 CreateRequest(tests[i].is_using_lofi);
184 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); 187 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
185 command_line->InitFromArgv(command_line->argv()); 188 command_line->InitFromArgv(command_line->argv());
186 if (tests[i].is_using_previews) { 189 if (tests[i].is_using_previews) {
187 command_line->AppendSwitch( 190 command_line->AppendSwitch(
188 switches::kEnableDataReductionProxyLoFiPreview); 191 switches::kEnableDataReductionProxyLoFiPreview);
189 } 192 }
190 193
191 // No flags or field trials. The Lo-Fi header should not be added. 194 // No flags or field trials. The Lo-Fi header should not be added.
192 net::HttpRequestHeaders headers; 195 net::HttpRequestHeaders headers;
193 NotifyBeforeSendProxyHeaders(&headers, request.get(), true); 196 NotifyBeforeSendProxyHeaders(&headers, request.get(), true);
194 VerifyLoFiHeader(false, headers); 197 VerifyLoFiHeader(false, headers);
195 VerifyLoFiPreviewHeader(false, headers); 198 VerifyLoFiPreviewHeader(false, headers);
196 VerifyLoFiIgnorePreviewBlacklistHeader(false, headers); 199 VerifyLoFiIgnorePreviewBlacklistHeader(false, headers);
197 200
198 // The Lo-Fi flag is "always-on", Lo-Fi is being used, and it's a main frame 201 // The Lo-Fi flag is "always-on", Lo-Fi is being used, and it's a main frame
199 // request. Lo-Fi preview header should be added. 202 // request. Lo-Fi preview header should be added.
200 command_line->AppendSwitchASCII( 203 command_line->AppendSwitchASCII(
201 switches::kDataReductionProxyLoFi, 204 switches::kDataReductionProxyLoFi,
202 switches::kDataReductionProxyLoFiValueAlwaysOn); 205 switches::kDataReductionProxyLoFiValueAlwaysOn);
203 request->SetLoadFlags(request->load_flags() | net::LOAD_MAIN_FRAME); 206 if (tests[i].is_main_frame)
207 request->SetLoadFlags(request->load_flags() | net::LOAD_MAIN_FRAME);
204 headers.Clear(); 208 headers.Clear();
205 NotifyBeforeSendProxyHeaders(&headers, request.get(), true); 209 NotifyBeforeSendProxyHeaders(&headers, request.get(), true);
206 VerifyLoFiHeader(tests[i].is_using_lofi && !tests[i].is_using_previews, 210 VerifyLoFiHeader(tests[i].is_using_lofi && !tests[i].is_using_previews &&
211 !tests[i].is_main_frame,
207 headers); 212 headers);
208 VerifyLoFiPreviewHeader( 213 VerifyLoFiPreviewHeader(tests[i].is_using_lofi &&
209 tests[i].is_using_lofi && tests[i].is_using_previews, headers); 214 tests[i].is_using_previews &&
210 VerifyLoFiIgnorePreviewBlacklistHeader( 215 tests[i].is_main_frame,
211 tests[i].is_using_lofi && tests[i].is_using_previews, headers); 216 headers);
217 VerifyLoFiIgnorePreviewBlacklistHeader(tests[i].is_using_lofi &&
218 tests[i].is_using_previews &&
219 tests[i].is_main_frame,
220 headers);
212 221
213 // The Lo-Fi flag is "always-on" and Lo-Fi is being used. Lo-Fi header 222 // The Lo-Fi flag is "always-on" and Lo-Fi is being used. Lo-Fi header
214 // should be added. 223 // should be added.
215 request->SetLoadFlags(0); 224 request->SetLoadFlags(0);
216 headers.Clear(); 225 headers.Clear();
217 NotifyBeforeSendProxyHeaders(&headers, request.get(), true); 226 NotifyBeforeSendProxyHeaders(&headers, request.get(), true);
218 VerifyLoFiHeader(tests[i].is_using_lofi && !tests[i].is_using_previews, 227 VerifyLoFiHeader(tests[i].is_using_lofi && !tests[i].is_using_previews,
219 headers); 228 headers);
220 VerifyLoFiPreviewHeader(false, headers); 229 VerifyLoFiPreviewHeader(false, headers);
221 VerifyLoFiIgnorePreviewBlacklistHeader(false, headers); 230 VerifyLoFiIgnorePreviewBlacklistHeader(false, headers);
(...skipping 24 matching lines...) Expand all
246 } 255 }
247 } 256 }
248 257
249 TEST_F(ContentLoFiDeciderTest, LoFiEnabledFieldTrial) { 258 TEST_F(ContentLoFiDeciderTest, LoFiEnabledFieldTrial) {
250 base::FieldTrialList field_trial_list(nullptr); 259 base::FieldTrialList field_trial_list(nullptr);
251 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), 260 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(),
252 "Enabled"); 261 "Enabled");
253 // Enable Lo-Fi. 262 // Enable Lo-Fi.
254 const struct { 263 const struct {
255 bool is_using_lofi; 264 bool is_using_lofi;
256 } tests[] = { 265 bool is_main_frame;
257 {false}, {true}, 266 } tests[] = {{false, false}, {false, true}, {true, false}, {true, true}};
258 };
259 267
260 for (size_t i = 0; i < arraysize(tests); ++i) { 268 for (size_t i = 0; i < arraysize(tests); ++i) {
261 std::unique_ptr<net::URLRequest> request = 269 std::unique_ptr<net::URLRequest> request =
262 CreateRequest(tests[i].is_using_lofi); 270 CreateRequest(tests[i].is_using_lofi);
271 if (tests[i].is_main_frame)
272 request->SetLoadFlags(request->load_flags() | net::LOAD_MAIN_FRAME);
263 net::HttpRequestHeaders headers; 273 net::HttpRequestHeaders headers;
264 NotifyBeforeSendProxyHeaders(&headers, request.get(), true); 274 NotifyBeforeSendProxyHeaders(&headers, request.get(), true);
265 VerifyLoFiHeader(tests[i].is_using_lofi, headers); 275 VerifyLoFiHeader(tests[i].is_using_lofi && !tests[i].is_main_frame,
276 headers);
266 VerifyLoFiPreviewHeader(false, headers); 277 VerifyLoFiPreviewHeader(false, headers);
267 VerifyLoFiIgnorePreviewBlacklistHeader(false, headers); 278 VerifyLoFiIgnorePreviewBlacklistHeader(false, headers);
268 } 279 }
269 } 280 }
270 281
271 TEST_F(ContentLoFiDeciderTest, LoFiControlFieldTrial) { 282 TEST_F(ContentLoFiDeciderTest, LoFiControlFieldTrial) {
272 base::FieldTrialList field_trial_list(nullptr); 283 base::FieldTrialList field_trial_list(nullptr);
273 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), 284 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(),
274 "Control"); 285 "Control");
275 // Enable Lo-Fi. 286 // Enable Lo-Fi.
276 const struct { 287 const struct {
277 bool is_using_lofi; 288 bool is_using_lofi;
278 } tests[] = { 289 bool is_main_frame;
279 {false}, {true}, 290 } tests[] = {{false, false}, {false, true}, {true, false}, {true, true}};
280 };
281 291
282 for (size_t i = 0; i < arraysize(tests); ++i) { 292 for (size_t i = 0; i < arraysize(tests); ++i) {
283 std::unique_ptr<net::URLRequest> request = 293 std::unique_ptr<net::URLRequest> request =
284 CreateRequest(tests[i].is_using_lofi); 294 CreateRequest(tests[i].is_using_lofi);
295 if (tests[i].is_main_frame)
296 request->SetLoadFlags(request->load_flags() | net::LOAD_MAIN_FRAME);
285 net::HttpRequestHeaders headers; 297 net::HttpRequestHeaders headers;
286 NotifyBeforeSendProxyHeaders(&headers, request.get(), true); 298 NotifyBeforeSendProxyHeaders(&headers, request.get(), true);
287 VerifyLoFiHeader(false, headers); 299 VerifyLoFiHeader(false, headers);
288 VerifyLoFiPreviewHeader(false, headers); 300 VerifyLoFiPreviewHeader(false, headers);
289 VerifyLoFiIgnorePreviewBlacklistHeader(false, headers); 301 VerifyLoFiIgnorePreviewBlacklistHeader(false, headers);
290 } 302 }
291 } 303 }
292 304
293 TEST_F(ContentLoFiDeciderTest, LoFiPreviewFieldTrial) { 305 TEST_F(ContentLoFiDeciderTest, LoFiPreviewFieldTrial) {
294 base::FieldTrialList field_trial_list(nullptr); 306 base::FieldTrialList field_trial_list(nullptr);
(...skipping 19 matching lines...) Expand all
314 headers); 326 headers);
315 VerifyLoFiIgnorePreviewBlacklistHeader(false, headers); 327 VerifyLoFiIgnorePreviewBlacklistHeader(false, headers);
316 } 328 }
317 } 329 }
318 330
319 TEST_F(ContentLoFiDeciderTest, AutoLoFi) { 331 TEST_F(ContentLoFiDeciderTest, AutoLoFi) {
320 const struct { 332 const struct {
321 bool auto_lofi_enabled_group; 333 bool auto_lofi_enabled_group;
322 bool auto_lofi_control_group; 334 bool auto_lofi_control_group;
323 bool network_prohibitively_slow; 335 bool network_prohibitively_slow;
336 bool is_main_frame;
324 } tests[] = { 337 } tests[] = {
325 {false, false, false}, 338 {false, false, false, false},
326 {false, false, true}, 339 {false, false, true, false},
327 {true, false, false}, 340 {true, false, false, false},
328 {true, false, true}, 341 {true, false, true, false},
329 {false, true, false}, 342 {true, false, true, true},
330 {false, true, true}, 343 {false, true, false, false},
344 {false, true, true, false},
331 // Repeat this test data to simulate user moving out of Lo-Fi control 345 // Repeat this test data to simulate user moving out of Lo-Fi control
332 // experiment. 346 // experiment.
333 {false, true, false}, 347 {false, true, false, false},
334 }; 348 };
335 349
336 for (size_t i = 0; i < arraysize(tests); ++i) { 350 for (size_t i = 0; i < arraysize(tests); ++i) {
337 test_context_->config()->ResetLoFiStatusForTest(); 351 test_context_->config()->ResetLoFiStatusForTest();
338 // Lo-Fi header is expected only if session is part of Lo-Fi enabled field 352 const bool expect_lofi_header = tests[i].auto_lofi_enabled_group &&
339 // trial and network is prohibitively slow. 353 tests[i].network_prohibitively_slow &&
340 bool expect_lofi_header = 354 !tests[i].is_main_frame;
341 tests[i].auto_lofi_enabled_group && tests[i].network_prohibitively_slow;
342 355
343 base::FieldTrialList field_trial_list(nullptr); 356 base::FieldTrialList field_trial_list(nullptr);
344 if (tests[i].auto_lofi_enabled_group) { 357 if (tests[i].auto_lofi_enabled_group) {
345 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), 358 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(),
346 "Enabled"); 359 "Enabled");
347 } 360 }
348 361
349 if (tests[i].auto_lofi_control_group) { 362 if (tests[i].auto_lofi_control_group) {
350 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), 363 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(),
351 "Control"); 364 "Control");
352 } 365 }
353 366
354 test_context_->config()->SetNetworkProhibitivelySlow( 367 test_context_->config()->SetNetworkProhibitivelySlow(
355 tests[i].network_prohibitively_slow); 368 tests[i].network_prohibitively_slow);
356 369
357 std::unique_ptr<net::URLRequest> request = 370 std::unique_ptr<net::URLRequest> request =
358 CreateRequest(tests[i].network_prohibitively_slow); 371 CreateRequest(tests[i].network_prohibitively_slow);
372 if (tests[i].is_main_frame)
373 request->SetLoadFlags(request->load_flags() | net::LOAD_MAIN_FRAME);
359 net::HttpRequestHeaders headers; 374 net::HttpRequestHeaders headers;
360 NotifyBeforeSendProxyHeaders(&headers, request.get(), true); 375 NotifyBeforeSendProxyHeaders(&headers, request.get(), true);
361 376
362 VerifyLoFiHeader(expect_lofi_header, headers); 377 VerifyLoFiHeader(expect_lofi_header, headers);
363 } 378 }
364 } 379 }
365 380
366 TEST_F(ContentLoFiDeciderTest, SlowConnectionsFlag) { 381 TEST_F(ContentLoFiDeciderTest, SlowConnectionsFlag) {
367 const struct { 382 const struct {
368 bool slow_connections_flag_enabled; 383 bool slow_connections_flag_enabled;
369 bool network_prohibitively_slow; 384 bool network_prohibitively_slow;
370 bool auto_lofi_enabled_group; 385 bool auto_lofi_enabled_group;
371 386 bool is_main_frame;
372 } tests[] = { 387 } tests[] = {
373 {false, false, false}, {false, true, false}, {true, false, false}, 388 {false, false, false, false}, {false, true, false, false},
374 {true, true, false}, {false, false, true}, {false, true, true}, 389 {true, false, false, false}, {true, true, false, false},
375 {true, false, true}, {true, true, true}, 390 {true, true, false, true}, {false, false, true, false},
391 {false, false, true, true}, {false, true, true, false},
392 {true, false, true, false}, {true, true, true, false},
376 }; 393 };
377 394
378 for (size_t i = 0; i < arraysize(tests); ++i) { 395 for (size_t i = 0; i < arraysize(tests); ++i) {
379 test_context_->config()->ResetLoFiStatusForTest(); 396 test_context_->config()->ResetLoFiStatusForTest();
380 // For the purpose of this test, Lo-Fi header is expected only if LoFi Slow 397 // For the purpose of this test, Lo-Fi header is expected only if LoFi Slow
381 // Connection Flag is enabled or session is part of Lo-Fi enabled field 398 // Connection Flag is enabled or session is part of Lo-Fi enabled field
382 // trial. For both cases, an additional condition is that network must be 399 // trial. For both cases, an additional condition is that network must be
383 // prohibitively slow. 400 // prohibitively slow.
384 bool expect_lofi_header = (tests[i].slow_connections_flag_enabled && 401 const bool expect_lofi_header = ((tests[i].slow_connections_flag_enabled &&
385 tests[i].network_prohibitively_slow) || 402 tests[i].network_prohibitively_slow) ||
386 (!tests[i].slow_connections_flag_enabled && 403 (!tests[i].slow_connections_flag_enabled &&
387 tests[i].auto_lofi_enabled_group && 404 tests[i].auto_lofi_enabled_group &&
388 tests[i].network_prohibitively_slow); 405 tests[i].network_prohibitively_slow)) &&
406 !tests[i].is_main_frame;
389 407
390 std::string expected_header; 408 std::string expected_header;
391 409
392 if (tests[i].slow_connections_flag_enabled) { 410 if (tests[i].slow_connections_flag_enabled) {
393 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( 411 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
394 switches::kDataReductionProxyLoFi, 412 switches::kDataReductionProxyLoFi,
395 switches::kDataReductionProxyLoFiValueSlowConnectionsOnly); 413 switches::kDataReductionProxyLoFiValueSlowConnectionsOnly);
396 } 414 }
397 415
398 base::FieldTrialList field_trial_list(nullptr); 416 base::FieldTrialList field_trial_list(nullptr);
399 if (tests[i].auto_lofi_enabled_group) { 417 if (tests[i].auto_lofi_enabled_group) {
400 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), 418 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(),
401 "Enabled"); 419 "Enabled");
402 } 420 }
403 421
404 test_context_->config()->SetNetworkProhibitivelySlow( 422 test_context_->config()->SetNetworkProhibitivelySlow(
405 tests[i].network_prohibitively_slow); 423 tests[i].network_prohibitively_slow);
406 424
407 std::unique_ptr<net::URLRequest> request = 425 std::unique_ptr<net::URLRequest> request =
408 CreateRequest(tests[i].network_prohibitively_slow); 426 CreateRequest(tests[i].network_prohibitively_slow);
427 if (tests[i].is_main_frame)
428 request->SetLoadFlags(request->load_flags() | net::LOAD_MAIN_FRAME);
409 net::HttpRequestHeaders headers; 429 net::HttpRequestHeaders headers;
410 NotifyBeforeSendProxyHeaders(&headers, request.get(), true); 430 NotifyBeforeSendProxyHeaders(&headers, request.get(), true);
411 431
412 VerifyLoFiHeader(expect_lofi_header, headers); 432 VerifyLoFiHeader(expect_lofi_header, headers);
413 } 433 }
414 } 434 }
415 435
416 TEST_F(ContentLoFiDeciderTest, ProxyIsNotDataReductionProxy) { 436 TEST_F(ContentLoFiDeciderTest, ProxyIsNotDataReductionProxy) {
417 base::FieldTrialList field_trial_list(nullptr); 437 base::FieldTrialList field_trial_list(nullptr);
418 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), 438 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(),
(...skipping 11 matching lines...) Expand all
430 net::HttpRequestHeaders headers; 450 net::HttpRequestHeaders headers;
431 NotifyBeforeSendProxyHeaders(&headers, request.get(), false); 451 NotifyBeforeSendProxyHeaders(&headers, request.get(), false);
432 std::string header_value; 452 std::string header_value;
433 headers.GetHeader(chrome_proxy_header(), &header_value); 453 headers.GetHeader(chrome_proxy_header(), &header_value);
434 EXPECT_EQ(std::string::npos, 454 EXPECT_EQ(std::string::npos,
435 header_value.find(chrome_proxy_lo_fi_directive())); 455 header_value.find(chrome_proxy_lo_fi_directive()));
436 } 456 }
437 } 457 }
438 458
439 } // namespace data_reduction_roxy 459 } // namespace data_reduction_roxy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698