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

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

Issue 2911673002: New CPAT support in ContentLoFiDecider guarded by feature flag. (Closed)
Patch Set: Backed out some debug code Created 3 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
« no previous file with comments | « components/data_reduction_proxy/content/browser/content_lofi_decider.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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>
11 11
12 #include "base/command_line.h" 12 #include "base/command_line.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/message_loop/message_loop.h" 14 #include "base/message_loop/message_loop.h"
15 #include "base/metrics/field_trial.h" 15 #include "base/metrics/field_trial.h"
16 #include "base/strings/string_util.h" 16 #include "base/strings/string_util.h"
17 #include "base/strings/stringprintf.h" 17 #include "base/strings/stringprintf.h"
18 #include "base/test/scoped_feature_list.h"
18 #include "build/build_config.h" 19 #include "build/build_config.h"
19 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_conf ig_test_utils.h" 20 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_conf ig_test_utils.h"
20 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_data .h" 21 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_data .h"
21 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_netw ork_delegate.h" 22 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_netw ork_delegate.h"
22 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_requ est_options.h" 23 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_requ est_options.h"
23 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test _utils.h" 24 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test _utils.h"
25 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_featu res.h"
24 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_heade rs.h" 26 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_heade rs.h"
25 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_param s.h" 27 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_param s.h"
26 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_param s_test_utils.h" 28 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_param s_test_utils.h"
27 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_switc hes.h" 29 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_switc hes.h"
28 #include "content/public/browser/resource_request_info.h" 30 #include "content/public/browser/resource_request_info.h"
29 #include "content/public/common/previews_state.h" 31 #include "content/public/common/previews_state.h"
30 #include "net/base/load_flags.h" 32 #include "net/base/load_flags.h"
31 #include "net/base/network_delegate_impl.h" 33 #include "net/base/network_delegate_impl.h"
32 #include "net/http/http_request_headers.h" 34 #include "net/http/http_request_headers.h"
33 #include "net/proxy/proxy_info.h" 35 #include "net/proxy/proxy_info.h"
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 if (expected_if_heavy) { 187 if (expected_if_heavy) {
186 std::string lite_page_if_heavy = base::StringPrintf( 188 std::string lite_page_if_heavy = base::StringPrintf(
187 "%s;%s", lite_page_directive(), if_heavy_qualifier()); 189 "%s;%s", lite_page_directive(), if_heavy_qualifier());
188 EXPECT_EQ(expected_lofi_preview_used, lite_page_if_heavy == header_value); 190 EXPECT_EQ(expected_lofi_preview_used, lite_page_if_heavy == header_value);
189 } else { 191 } else {
190 EXPECT_EQ(expected_lofi_preview_used, 192 EXPECT_EQ(expected_lofi_preview_used,
191 header_value == lite_page_directive()); 193 header_value == lite_page_directive());
192 } 194 }
193 } 195 }
194 196
197 static void VerifyAcceptTransformHeader(const net::URLRequest& request,
198 bool expected_accept_lite_page,
199 bool expected_accept_empty_image) {
200 std::unique_ptr<data_reduction_proxy::ContentLoFiDecider> lofi_decider(
201 new data_reduction_proxy::ContentLoFiDecider());
202 net::HttpRequestHeaders headers;
203 lofi_decider->MaybeSetAcceptTransformHeader(request, false, &headers);
204
205 std::string header_value;
206 EXPECT_EQ(expected_accept_lite_page || expected_accept_empty_image,
207 headers.GetHeader(chrome_proxy_accept_transform_header(),
208 &header_value));
209 if (expected_accept_lite_page) {
210 EXPECT_TRUE(header_value == lite_page_directive());
211 } else if (expected_accept_empty_image) {
212 EXPECT_TRUE(header_value == empty_image_directive());
213 }
214 }
215
195 static void VerifyVideoHeader(bool expected_compressed_video_used, 216 static void VerifyVideoHeader(bool expected_compressed_video_used,
196 const net::HttpRequestHeaders& headers) { 217 const net::HttpRequestHeaders& headers) {
197 EXPECT_EQ(expected_compressed_video_used, 218 EXPECT_EQ(expected_compressed_video_used,
198 headers.HasHeader(chrome_proxy_accept_transform_header())); 219 headers.HasHeader(chrome_proxy_accept_transform_header()));
199 std::string header_value; 220 std::string header_value;
200 headers.GetHeader(chrome_proxy_accept_transform_header(), &header_value); 221 headers.GetHeader(chrome_proxy_accept_transform_header(), &header_value);
201 EXPECT_EQ( 222 EXPECT_EQ(
202 expected_compressed_video_used, 223 expected_compressed_video_used,
203 header_value.find(compressed_video_directive()) != std::string::npos); 224 header_value.find(compressed_video_directive()) != std::string::npos);
204 } 225 }
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 VerifyLoFiHeader(!tests[i].is_using_lite_page, !tests[i].is_using_lofi, 320 VerifyLoFiHeader(!tests[i].is_using_lite_page, !tests[i].is_using_lofi,
300 headers); 321 headers);
301 VerifyLitePageHeader(false, false, headers); 322 VerifyLitePageHeader(false, false, headers);
302 data = DataReductionProxyData::GetData(*request); 323 data = DataReductionProxyData::GetData(*request);
303 // |lofi_requested| should be set to false when Lo-Fi is enabled using 324 // |lofi_requested| should be set to false when Lo-Fi is enabled using
304 // flags. 325 // flags.
305 EXPECT_FALSE(data->lofi_requested()); 326 EXPECT_FALSE(data->lofi_requested());
306 } 327 }
307 } 328 }
308 329
330 TEST_F(ContentLoFiDeciderTest, MaybeSetAcceptTransformNoAcceptForPreviewsOff) {
331 // Turn on proxy-decides-transform feature for these unit tests.
332 base::test::ScopedFeatureList scoped_feature_list;
333 scoped_feature_list.InitAndEnableFeature(
334 features::kDataReductionProxyDecidesTransform);
335
336 std::unique_ptr<net::URLRequest> request =
337 CreateRequest(true /* is main */, content::PREVIEWS_OFF);
338 VerifyAcceptTransformHeader(*request.get(), false /* lite-page */,
339 false /* empty-image */);
340
341 request = CreateRequest(true /* is main */, content::PREVIEWS_NO_TRANSFORM);
342 VerifyAcceptTransformHeader(*request.get(), false /* lite-page */,
343 false /* empty-image */);
344
345 request = CreateRequest(true /* is main */, content::PREVIEWS_UNSPECIFIED);
346 VerifyAcceptTransformHeader(*request.get(), false /* lite-page */,
347 false /* empty-image */);
348 }
349
350 TEST_F(ContentLoFiDeciderTest, MaybeSetAcceptTransformNoAcceptForHttps) {
351 // Turn on proxy-decides-transform feature for these unit tests.
352 base::test::ScopedFeatureList scoped_feature_list;
353 scoped_feature_list.InitAndEnableFeature(
354 features::kDataReductionProxyDecidesTransform);
355
356 content::PreviewsState both_previews_enabled =
357 content::SERVER_LITE_PAGE_ON | content::SERVER_LOFI_ON;
358
359 // Verify no accept header for HTTPS.
360 std::unique_ptr<net::URLRequest> request =
361 CreateRequestByType(content::RESOURCE_TYPE_MAIN_FRAME, true /* https */,
362 both_previews_enabled);
363 VerifyAcceptTransformHeader(*request.get(), false /* lite-page */,
364 false /* empty-image */);
365
366 request = CreateRequestByType(content::RESOURCE_TYPE_IMAGE, true /* https */,
367 both_previews_enabled);
368 VerifyAcceptTransformHeader(*request.get(), false /* lite-page */,
369 false /* empty-image */);
370 }
371
372 TEST_F(ContentLoFiDeciderTest, MaybeSetAcceptTransformHeaderAcceptLitePage) {
373 // Turn on proxy-decides-transform feature for these unit tests.
374 base::test::ScopedFeatureList scoped_feature_list;
375 scoped_feature_list.InitAndEnableFeature(
376 features::kDataReductionProxyDecidesTransform);
377
378 content::PreviewsState lite_page_enabled = content::SERVER_LITE_PAGE_ON;
379
380 // Verify accepting lite-page per resource type.
381 std::unique_ptr<net::URLRequest> request =
382 CreateRequest(true /* is main */, lite_page_enabled);
383 VerifyAcceptTransformHeader(*request.get(), true /* lite-page */,
384 false /* empty-image */);
385
386 request = CreateRequest(false /* is main */, lite_page_enabled);
387 VerifyAcceptTransformHeader(*request.get(), false /* lite-page */,
388 false /* empty-image */);
389 }
390
391 TEST_F(ContentLoFiDeciderTest, MaybeSetAcceptTransformHeaderAcceptEmptyImage) {
392 // Turn on proxy-decides-transform feature for these unit tests.
393 base::test::ScopedFeatureList scoped_feature_list;
394 scoped_feature_list.InitAndEnableFeature(
395 features::kDataReductionProxyDecidesTransform);
396
397 content::PreviewsState lofi_enabled = content::SERVER_LOFI_ON;
398
399 // Verify accepting empty-image per resource type.
400 std::unique_ptr<net::URLRequest> request = CreateRequestByType(
401 content::RESOURCE_TYPE_MAIN_FRAME, false /* https */, lofi_enabled);
402 VerifyAcceptTransformHeader(*request.get(), false /* lite-page */,
403 false /* empty-image */);
404
405 request = CreateRequestByType(content::RESOURCE_TYPE_IMAGE, false /* https */,
406 lofi_enabled);
407 VerifyAcceptTransformHeader(*request.get(), false /* lite-page */,
408 true /* empty-image */);
409
410 request = CreateRequestByType(content::RESOURCE_TYPE_FAVICON,
411 false /* https */, lofi_enabled);
412 VerifyAcceptTransformHeader(*request.get(), false /* lite-page */,
413 true /* empty-image */);
414
415 request = CreateRequestByType(content::RESOURCE_TYPE_SCRIPT,
416 false /* https */, lofi_enabled);
417 VerifyAcceptTransformHeader(*request.get(), false /* lite-page */,
418 false /* empty-image */);
419
420 request = CreateRequestByType(content::RESOURCE_TYPE_STYLESHEET,
421 false /* https */, lofi_enabled);
422 VerifyAcceptTransformHeader(*request.get(), false /* lite-page */,
423 false /* empty-image */);
424 }
425
309 TEST_F(ContentLoFiDeciderTest, LoFiEnabledFieldTrial) { 426 TEST_F(ContentLoFiDeciderTest, LoFiEnabledFieldTrial) {
310 base::FieldTrialList field_trial_list(nullptr); 427 base::FieldTrialList field_trial_list(nullptr);
311 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), 428 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(),
312 "Enabled"); 429 "Enabled");
313 430
314 // Enable Lo-Fi. 431 // Enable Lo-Fi.
315 const struct { 432 const struct {
316 bool is_using_lofi; 433 bool is_using_lofi;
317 content::ResourceType resource_type; 434 content::ResourceType resource_type;
318 } tests[] = {{false, content::RESOURCE_TYPE_MAIN_FRAME}, 435 } tests[] = {{false, content::RESOURCE_TYPE_MAIN_FRAME},
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
392 net::HttpRequestHeaders headers; 509 net::HttpRequestHeaders headers;
393 NotifyBeforeSendHeaders(&headers, request.get(), true); 510 NotifyBeforeSendHeaders(&headers, request.get(), true);
394 VerifyLoFiHeader(false, false, headers); 511 VerifyLoFiHeader(false, false, headers);
395 VerifyLitePageHeader(false, false, headers); 512 VerifyLitePageHeader(false, false, headers);
396 DataReductionProxyData* data = DataReductionProxyData::GetData(*request); 513 DataReductionProxyData* data = DataReductionProxyData::GetData(*request);
397 EXPECT_EQ(tests[i].is_using_lofi, data->lofi_requested()) << i; 514 EXPECT_EQ(tests[i].is_using_lofi, data->lofi_requested()) << i;
398 } 515 }
399 } 516 }
400 517
401 TEST_F(ContentLoFiDeciderTest, LitePageFieldTrial) { 518 TEST_F(ContentLoFiDeciderTest, LitePageFieldTrial) {
519 // Turn off proxy-decides-transform feature for these unit tests.
520 base::test::ScopedFeatureList scoped_feature_list;
521 scoped_feature_list.InitAndDisableFeature(
522 features::kDataReductionProxyDecidesTransform);
523
402 base::FieldTrialList field_trial_list(nullptr); 524 base::FieldTrialList field_trial_list(nullptr);
403 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), 525 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(),
404 "Enabled_Preview"); 526 "Enabled_Preview");
405 // Enable Lo-Fi. 527 // Enable Lo-Fi.
406 const struct { 528 const struct {
407 bool is_using_lite_page; 529 bool is_using_lite_page;
408 bool is_main_frame; 530 bool is_main_frame;
409 } tests[] = { 531 } tests[] = {
410 {false, false}, {true, false}, {false, true}, {true, true}, 532 {false, false}, {true, false}, {false, true}, {true, true},
411 }; 533 };
412 534
413 for (size_t i = 0; i < arraysize(tests); ++i) { 535 for (size_t i = 0; i < arraysize(tests); ++i) {
414 std::unique_ptr<net::URLRequest> request = 536 std::unique_ptr<net::URLRequest> request =
415 CreateRequest(tests[i].is_main_frame, tests[i].is_using_lite_page 537 CreateRequest(tests[i].is_main_frame, tests[i].is_using_lite_page
416 ? content::SERVER_LITE_PAGE_ON 538 ? content::SERVER_LITE_PAGE_ON
417 : content::PREVIEWS_OFF); 539 : content::PREVIEWS_OFF);
418 net::HttpRequestHeaders headers; 540 net::HttpRequestHeaders headers;
419 NotifyBeforeSendHeaders(&headers, request.get(), true); 541 NotifyBeforeSendHeaders(&headers, request.get(), true);
420 VerifyLoFiHeader(false, false, headers); 542 VerifyLoFiHeader(false, false, headers);
421 VerifyLitePageHeader(tests[i].is_main_frame, !tests[i].is_using_lite_page, 543 VerifyLitePageHeader(tests[i].is_main_frame, !tests[i].is_using_lite_page,
422 headers); 544 headers);
423 DataReductionProxyData* data = DataReductionProxyData::GetData(*request); 545 DataReductionProxyData* data = DataReductionProxyData::GetData(*request);
424 EXPECT_EQ(tests[i].is_using_lite_page, data->lofi_requested()) << i; 546 EXPECT_EQ(tests[i].is_using_lite_page, data->lofi_requested()) << i;
425 } 547 }
426 } 548 }
427 549
428 TEST_F(ContentLoFiDeciderTest, LitePageFieldTrialFallbackDisabled) { 550 TEST_F(ContentLoFiDeciderTest, LitePageFieldTrialFallbackDisabled) {
551 // Turn off proxy-decides-transform feature for these unit tests.
552 base::test::ScopedFeatureList scoped_feature_list;
553 scoped_feature_list.InitAndDisableFeature(
554 features::kDataReductionProxyDecidesTransform);
555
429 base::FieldTrialList field_trial_list(nullptr); 556 base::FieldTrialList field_trial_list(nullptr);
430 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), 557 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(),
431 "Enabled_Preview"); 558 "Enabled_Preview");
432 // Enable Lo-Fi. 559 // Enable Lo-Fi.
433 const struct { 560 const struct {
434 bool is_using_lofi; 561 bool is_using_lofi;
435 bool is_using_lite_page; 562 bool is_using_lite_page;
436 bool is_main_frame; 563 bool is_main_frame;
437 } tests[] = { 564 } tests[] = {
438 {false, false, false}, {false, false, true}, {true, false, true}, 565 {false, false, false}, {false, false, true}, {true, false, true},
(...skipping 19 matching lines...) Expand all
458 tests[i].is_main_frame && !tests[i].is_using_lite_page, 585 tests[i].is_main_frame && !tests[i].is_using_lite_page,
459 headers); 586 headers);
460 DataReductionProxyData* data = DataReductionProxyData::GetData(*request); 587 DataReductionProxyData* data = DataReductionProxyData::GetData(*request);
461 EXPECT_EQ(tests[i].is_using_lofi || tests[i].is_using_lite_page, 588 EXPECT_EQ(tests[i].is_using_lofi || tests[i].is_using_lite_page,
462 data->lofi_requested()) 589 data->lofi_requested())
463 << i; 590 << i;
464 } 591 }
465 } 592 }
466 593
467 TEST_F(ContentLoFiDeciderTest, AutoLoFi) { 594 TEST_F(ContentLoFiDeciderTest, AutoLoFi) {
595 // Turn off proxy-decides-transform feature for these unit tests.
596 base::test::ScopedFeatureList scoped_feature_list;
597 scoped_feature_list.InitAndDisableFeature(
598 features::kDataReductionProxyDecidesTransform);
599
468 const struct { 600 const struct {
469 bool auto_lofi_enabled_group; 601 bool auto_lofi_enabled_group;
470 bool auto_lofi_control_group; 602 bool auto_lofi_control_group;
471 bool network_prohibitively_slow; 603 bool network_prohibitively_slow;
472 bool is_main_frame; 604 bool is_main_frame;
473 } tests[] = { 605 } tests[] = {
474 {false, false, false, false}, 606 {false, false, false, false},
475 {false, false, true, false}, 607 {false, false, true, false},
476 {true, false, false, false}, 608 {true, false, false, false},
477 {true, false, true, false}, 609 {true, false, true, false},
(...skipping 28 matching lines...) Expand all
506 tests[i].is_main_frame, tests[i].network_prohibitively_slow); 638 tests[i].is_main_frame, tests[i].network_prohibitively_slow);
507 net::HttpRequestHeaders headers; 639 net::HttpRequestHeaders headers;
508 NotifyBeforeSendHeaders(&headers, request.get(), true); 640 NotifyBeforeSendHeaders(&headers, request.get(), true);
509 641
510 VerifyLoFiHeader(expect_lofi_header, !tests[i].network_prohibitively_slow, 642 VerifyLoFiHeader(expect_lofi_header, !tests[i].network_prohibitively_slow,
511 headers); 643 headers);
512 } 644 }
513 } 645 }
514 646
515 TEST_F(ContentLoFiDeciderTest, SlowConnectionsFlag) { 647 TEST_F(ContentLoFiDeciderTest, SlowConnectionsFlag) {
648 // Turn off proxy-decides-transform feature for these unit tests.
649 base::test::ScopedFeatureList scoped_feature_list;
650 scoped_feature_list.InitAndDisableFeature(
651 features::kDataReductionProxyDecidesTransform);
652
516 const struct { 653 const struct {
517 bool slow_connections_flag_enabled; 654 bool slow_connections_flag_enabled;
518 bool network_prohibitively_slow; 655 bool network_prohibitively_slow;
519 bool auto_lofi_enabled_group; 656 bool auto_lofi_enabled_group;
520 bool is_main_frame; 657 bool is_main_frame;
521 } tests[] = { 658 } tests[] = {
522 {false, false, false, false}, {false, true, false, false}, 659 {false, false, false, false}, {false, true, false, false},
523 {true, false, false, false}, {true, true, false, false}, 660 {true, false, false, false}, {true, true, false, false},
524 {true, true, false, true}, {false, false, true, false}, 661 {true, true, false, true}, {false, false, true, false},
525 {false, false, true, true}, {false, true, true, false}, 662 {false, false, true, true}, {false, true, true, false},
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
733 content::CLIENT_LOFI_AUTO_RELOAD))); 870 content::CLIENT_LOFI_AUTO_RELOAD)));
734 871
735 // IsClientLoFiAutoReloadRequest() should return false for any request without 872 // IsClientLoFiAutoReloadRequest() should return false for any request without
736 // the CLIENT_LOFI_AUTO_RELOAD bit set. 873 // the CLIENT_LOFI_AUTO_RELOAD bit set.
737 EXPECT_FALSE(ContentLoFiDecider().IsClientLoFiAutoReloadRequest( 874 EXPECT_FALSE(ContentLoFiDecider().IsClientLoFiAutoReloadRequest(
738 *CreateRequestByType(content::RESOURCE_TYPE_IMAGE, false, 875 *CreateRequestByType(content::RESOURCE_TYPE_IMAGE, false,
739 content::PREVIEWS_NO_TRANSFORM))); 876 content::PREVIEWS_NO_TRANSFORM)));
740 } 877 }
741 878
742 } // namespace data_reduction_proxy 879 } // namespace data_reduction_proxy
OLDNEW
« no previous file with comments | « components/data_reduction_proxy/content/browser/content_lofi_decider.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698