| OLD | NEW |
| 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 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 content::PreviewsState previews_state) { | 100 content::PreviewsState previews_state) { |
| 101 content::ResourceRequestInfo::AllocateForTesting( | 101 content::ResourceRequestInfo::AllocateForTesting( |
| 102 request, resource_type, NULL, -1, -1, -1, | 102 request, resource_type, NULL, -1, -1, -1, |
| 103 resource_type == content::RESOURCE_TYPE_MAIN_FRAME, | 103 resource_type == content::RESOURCE_TYPE_MAIN_FRAME, |
| 104 false, // parent_is_main_frame | 104 false, // parent_is_main_frame |
| 105 false, // allow_download | 105 false, // allow_download |
| 106 false, // is_async | 106 false, // is_async |
| 107 previews_state); | 107 previews_state); |
| 108 } | 108 } |
| 109 | 109 |
| 110 std::unique_ptr<net::URLRequest> CreateRequest(bool is_main_frame, | 110 std::unique_ptr<net::URLRequest> CreateRequest( |
| 111 bool is_using_lofi) { | 111 bool is_main_frame, |
| 112 content::PreviewsState previews_state) { |
| 112 std::unique_ptr<net::URLRequest> request = context_.CreateRequest( | 113 std::unique_ptr<net::URLRequest> request = context_.CreateRequest( |
| 113 GURL("http://www.google.com/"), net::IDLE, &delegate_); | 114 GURL("http://www.google.com/"), net::IDLE, &delegate_); |
| 114 AllocateRequestInfoForTesting( | 115 AllocateRequestInfoForTesting( |
| 115 request.get(), (is_main_frame ? content::RESOURCE_TYPE_MAIN_FRAME | 116 request.get(), |
| 116 : content::RESOURCE_TYPE_SUB_FRAME), | 117 (is_main_frame ? content::RESOURCE_TYPE_MAIN_FRAME |
| 117 is_using_lofi ? content::SERVER_LOFI_ON : content::PREVIEWS_OFF); | 118 : content::RESOURCE_TYPE_SUB_FRAME), |
| 119 previews_state); |
| 118 return request; | 120 return request; |
| 119 } | 121 } |
| 120 | 122 |
| 121 std::unique_ptr<net::URLRequest> CreateRequestByType( | 123 std::unique_ptr<net::URLRequest> CreateRequestByType( |
| 122 content::ResourceType resource_type, | 124 content::ResourceType resource_type, |
| 123 bool scheme_is_https, | 125 bool scheme_is_https, |
| 124 bool is_using_lofi) { | 126 content::PreviewsState previews_state) { |
| 125 std::unique_ptr<net::URLRequest> request = | 127 std::unique_ptr<net::URLRequest> request = |
| 126 context_.CreateRequest(GURL(scheme_is_https ? "https://www.google.com/" | 128 context_.CreateRequest(GURL(scheme_is_https ? "https://www.google.com/" |
| 127 : "http://www.google.com/"), | 129 : "http://www.google.com/"), |
| 128 net::IDLE, &delegate_); | 130 net::IDLE, &delegate_); |
| 129 AllocateRequestInfoForTesting( | 131 AllocateRequestInfoForTesting(request.get(), resource_type, previews_state); |
| 130 request.get(), resource_type, | |
| 131 is_using_lofi ? content::SERVER_LOFI_ON : content::PREVIEWS_OFF); | |
| 132 return request; | 132 return request; |
| 133 } | 133 } |
| 134 | 134 |
| 135 std::unique_ptr<net::URLRequest> CreateNoTransformRequest( | |
| 136 bool is_main_frame) { | |
| 137 std::unique_ptr<net::URLRequest> request = context_.CreateRequest( | |
| 138 GURL("http://www.google.com/"), net::IDLE, &delegate_); | |
| 139 AllocateRequestInfoForTesting( | |
| 140 request.get(), (is_main_frame ? content::RESOURCE_TYPE_MAIN_FRAME | |
| 141 : content::RESOURCE_TYPE_SUB_FRAME), | |
| 142 content::PREVIEWS_NO_TRANSFORM); | |
| 143 return request; | |
| 144 } | |
| 145 | |
| 146 void DelegateStageDone(int result) {} | 135 void DelegateStageDone(int result) {} |
| 147 | 136 |
| 148 void NotifyBeforeSendHeaders(net::HttpRequestHeaders* headers, | 137 void NotifyBeforeSendHeaders(net::HttpRequestHeaders* headers, |
| 149 net::URLRequest* request, | 138 net::URLRequest* request, |
| 150 bool use_data_reduction_proxy) { | 139 bool use_data_reduction_proxy) { |
| 151 net::ProxyInfo data_reduction_proxy_info; | 140 net::ProxyInfo data_reduction_proxy_info; |
| 152 net::ProxyRetryInfoMap proxy_retry_info; | 141 net::ProxyRetryInfoMap proxy_retry_info; |
| 153 | 142 |
| 154 if (use_data_reduction_proxy) { | 143 if (use_data_reduction_proxy) { |
| 155 std::string data_reduction_proxy; | 144 std::string data_reduction_proxy; |
| 156 base::TrimString(test_context_->config()->test_params()->DefaultOrigin(), | 145 base::TrimString(test_context_->config()->test_params()->DefaultOrigin(), |
| 157 "/", &data_reduction_proxy); | 146 "/", &data_reduction_proxy); |
| 158 data_reduction_proxy_info.UseNamedProxy(data_reduction_proxy); | 147 data_reduction_proxy_info.UseNamedProxy(data_reduction_proxy); |
| 159 } else { | 148 } else { |
| 160 data_reduction_proxy_info.UseNamedProxy("proxy.com"); | 149 data_reduction_proxy_info.UseNamedProxy("proxy.com"); |
| 161 } | 150 } |
| 162 | 151 |
| 163 data_reduction_proxy_network_delegate_->NotifyBeforeStartTransaction( | 152 data_reduction_proxy_network_delegate_->NotifyBeforeStartTransaction( |
| 164 request, base::Bind(&ContentLoFiDeciderTest::DelegateStageDone, | 153 request, base::Bind(&ContentLoFiDeciderTest::DelegateStageDone, |
| 165 base::Unretained(this)), | 154 base::Unretained(this)), |
| 166 headers); | 155 headers); |
| 167 data_reduction_proxy_network_delegate_->NotifyBeforeSendHeaders( | 156 data_reduction_proxy_network_delegate_->NotifyBeforeSendHeaders( |
| 168 request, data_reduction_proxy_info, proxy_retry_info, headers); | 157 request, data_reduction_proxy_info, proxy_retry_info, headers); |
| 169 } | 158 } |
| 170 | 159 |
| 171 static void VerifyLitePageHeader(bool expected_lite_page_used, | |
| 172 const net::HttpRequestHeaders& headers) { | |
| 173 if (expected_lite_page_used) | |
| 174 EXPECT_TRUE(headers.HasHeader(chrome_proxy_accept_transform_header())); | |
| 175 std::string header_value; | |
| 176 headers.GetHeader(chrome_proxy_accept_transform_header(), &header_value); | |
| 177 EXPECT_EQ(expected_lite_page_used, | |
| 178 header_value.find(lite_page_directive()) != std::string::npos); | |
| 179 } | |
| 180 | |
| 181 static void VerifyLoFiHeader(bool expected_lofi_used, | 160 static void VerifyLoFiHeader(bool expected_lofi_used, |
| 182 bool expected_if_heavy, | 161 bool expected_if_heavy, |
| 183 const net::HttpRequestHeaders& headers) { | 162 const net::HttpRequestHeaders& headers) { |
| 184 if (expected_lofi_used) | 163 if (expected_lofi_used) |
| 185 EXPECT_TRUE(headers.HasHeader(chrome_proxy_accept_transform_header())); | 164 EXPECT_TRUE(headers.HasHeader(chrome_proxy_accept_transform_header())); |
| 186 std::string header_value; | 165 std::string header_value; |
| 187 headers.GetHeader(chrome_proxy_accept_transform_header(), &header_value); | 166 headers.GetHeader(chrome_proxy_accept_transform_header(), &header_value); |
| 188 if (expected_if_heavy) { | 167 if (expected_if_heavy) { |
| 189 std::string empty_image_if_heavy = base::StringPrintf( | 168 std::string empty_image_if_heavy = base::StringPrintf( |
| 190 "%s;%s", empty_image_directive(), if_heavy_qualifier()); | 169 "%s;%s", empty_image_directive(), if_heavy_qualifier()); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 bool is_using_lofi; | 228 bool is_using_lofi; |
| 250 bool is_using_lite_page; | 229 bool is_using_lite_page; |
| 251 bool is_main_frame; | 230 bool is_main_frame; |
| 252 } tests[] = { | 231 } tests[] = { |
| 253 {false, false, false}, {false, false, true}, {true, false, true}, | 232 {false, false, false}, {false, false, true}, {true, false, true}, |
| 254 {true, false, false}, {false, true, false}, {false, true, true}, | 233 {true, false, false}, {false, true, false}, {false, true, true}, |
| 255 {true, true, true}, {true, true, false}, | 234 {true, true, true}, {true, true, false}, |
| 256 }; | 235 }; |
| 257 | 236 |
| 258 for (size_t i = 0; i < arraysize(tests); ++i) { | 237 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 238 content::PreviewsState previews_state = content::PREVIEWS_UNSPECIFIED; |
| 239 if (tests[i].is_using_lofi) |
| 240 previews_state |= content::SERVER_LOFI_ON; |
| 241 if (tests[i].is_using_lite_page) |
| 242 previews_state |= content::SERVER_LITE_PAGE_ON; |
| 243 if (previews_state == content::PREVIEWS_UNSPECIFIED) |
| 244 previews_state = content::PREVIEWS_OFF; |
| 245 |
| 259 std::unique_ptr<net::URLRequest> request = | 246 std::unique_ptr<net::URLRequest> request = |
| 260 CreateRequest(tests[i].is_main_frame, tests[i].is_using_lofi); | 247 CreateRequest(tests[i].is_main_frame, previews_state); |
| 261 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 248 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 262 command_line->InitFromArgv(command_line->argv()); | 249 command_line->InitFromArgv(command_line->argv()); |
| 263 if (tests[i].is_using_lite_page) { | 250 if (tests[i].is_using_lite_page) { |
| 264 command_line->AppendSwitch(switches::kEnableDataReductionProxyLitePage); | 251 command_line->AppendSwitch(switches::kEnableDataReductionProxyLitePage); |
| 265 } | 252 } |
| 266 | 253 |
| 267 // No flags or field trials. The Lo-Fi header should not be added. | 254 // No flags or field trials. The Lo-Fi header should not be added. |
| 268 net::HttpRequestHeaders headers; | 255 net::HttpRequestHeaders headers; |
| 269 NotifyBeforeSendHeaders(&headers, request.get(), true); | 256 NotifyBeforeSendHeaders(&headers, request.get(), true); |
| 270 VerifyLoFiHeader(false, false, headers); | 257 VerifyLoFiHeader(false, false, headers); |
| 271 VerifyLitePageHeader(false, false, headers); | 258 VerifyLitePageHeader(false, false, headers); |
| 272 VerifyLitePageIgnoreBlacklistHeader(false, headers); | 259 VerifyLitePageIgnoreBlacklistHeader(false, headers); |
| 273 | 260 |
| 274 // The Lo-Fi flag is "always-on", Lo-Fi is being used, and it's a main frame | 261 // The Lo-Fi flag is "always-on", Lo-Fi is being used, and it's a main frame |
| 275 // request. Lo-Fi or lite page header should be added. | 262 // request. Lo-Fi or lite page header should be added. |
| 276 command_line->AppendSwitchASCII( | 263 command_line->AppendSwitchASCII( |
| 277 switches::kDataReductionProxyLoFi, | 264 switches::kDataReductionProxyLoFi, |
| 278 switches::kDataReductionProxyLoFiValueAlwaysOn); | 265 switches::kDataReductionProxyLoFiValueAlwaysOn); |
| 279 headers.Clear(); | 266 headers.Clear(); |
| 280 NotifyBeforeSendHeaders(&headers, request.get(), true); | 267 NotifyBeforeSendHeaders(&headers, request.get(), true); |
| 281 VerifyLoFiHeader(!tests[i].is_using_lite_page && !tests[i].is_main_frame, | 268 VerifyLoFiHeader(!tests[i].is_main_frame && !tests[i].is_using_lite_page, |
| 282 !tests[i].is_using_lofi, headers); | 269 !tests[i].is_main_frame && !tests[i].is_using_lofi && |
| 270 !tests[i].is_using_lite_page, |
| 271 headers); |
| 283 VerifyLitePageHeader(tests[i].is_using_lite_page && tests[i].is_main_frame, | 272 VerifyLitePageHeader(tests[i].is_using_lite_page && tests[i].is_main_frame, |
| 284 !tests[i].is_using_lofi, headers); | 273 false, headers); |
| 285 VerifyLitePageIgnoreBlacklistHeader( | 274 VerifyLitePageIgnoreBlacklistHeader( |
| 286 tests[i].is_using_lite_page && tests[i].is_main_frame, headers); | 275 tests[i].is_using_lite_page && tests[i].is_main_frame, headers); |
| 287 DataReductionProxyData* data = DataReductionProxyData::GetData(*request); | 276 DataReductionProxyData* data = DataReductionProxyData::GetData(*request); |
| 288 // |lofi_requested| should be set to false when Lo-Fi is enabled using | 277 // |lofi_requested| should be set to false when Lo-Fi is enabled using |
| 289 // flags. | 278 // flags. |
| 290 EXPECT_FALSE(data->lofi_requested()); | 279 EXPECT_FALSE(data->lofi_requested()); |
| 291 | 280 |
| 292 // The Lo-Fi flag is "always-on" and Lo-Fi is being used. Lo-Fi header | 281 // The Lo-Fi flag is "always-on" and Lo-Fi is being used. Lo-Fi header |
| 293 // should be added. | 282 // should be added. |
| 294 AllocateRequestInfoForTesting( | 283 AllocateRequestInfoForTesting( |
| 295 request.get(), content::RESOURCE_TYPE_SUB_FRAME, | 284 request.get(), content::RESOURCE_TYPE_SUB_FRAME, previews_state); |
| 296 tests[i].is_using_lofi ? content::SERVER_LOFI_ON | |
| 297 : content::PREVIEWS_OFF); | |
| 298 headers.Clear(); | 285 headers.Clear(); |
| 299 NotifyBeforeSendHeaders(&headers, request.get(), true); | 286 NotifyBeforeSendHeaders(&headers, request.get(), true); |
| 300 VerifyLoFiHeader(!tests[i].is_using_lite_page, !tests[i].is_using_lofi, | 287 VerifyLoFiHeader(!tests[i].is_using_lite_page, !tests[i].is_using_lofi, |
| 301 headers); | 288 headers); |
| 302 VerifyLitePageHeader(false, false, headers); | 289 VerifyLitePageHeader(false, false, headers); |
| 303 VerifyLitePageIgnoreBlacklistHeader(false, headers); | 290 VerifyLitePageIgnoreBlacklistHeader(false, headers); |
| 304 | 291 |
| 305 // The Lo-Fi flag is "cellular-only" and Lo-Fi is being used. Lo-Fi header | 292 // The Lo-Fi flag is "cellular-only" and Lo-Fi is being used. Lo-Fi header |
| 306 // should be added. | 293 // should be added. |
| 307 command_line->AppendSwitchASCII( | 294 command_line->AppendSwitchASCII( |
| (...skipping 25 matching lines...) Expand all Loading... |
| 333 // |lofi_requested| should be set to false when Lo-Fi is enabled using | 320 // |lofi_requested| should be set to false when Lo-Fi is enabled using |
| 334 // flags. | 321 // flags. |
| 335 EXPECT_FALSE(data->lofi_requested()); | 322 EXPECT_FALSE(data->lofi_requested()); |
| 336 } | 323 } |
| 337 } | 324 } |
| 338 | 325 |
| 339 TEST_F(ContentLoFiDeciderTest, LoFiEnabledFieldTrial) { | 326 TEST_F(ContentLoFiDeciderTest, LoFiEnabledFieldTrial) { |
| 340 base::FieldTrialList field_trial_list(nullptr); | 327 base::FieldTrialList field_trial_list(nullptr); |
| 341 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), | 328 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), |
| 342 "Enabled"); | 329 "Enabled"); |
| 330 // Add the Lite Page fallback field trial. Having this enabled when not in the |
| 331 // Enabled_Previews group should not affect how Lo-Fi works. |
| 332 base::FieldTrialList::CreateFieldTrial( |
| 333 params::GetLitePageFallbackFieldTrialName(), "Enabled"); |
| 334 |
| 343 // Enable Lo-Fi. | 335 // Enable Lo-Fi. |
| 344 const struct { | 336 const struct { |
| 345 bool is_using_lofi; | 337 bool is_using_lofi; |
| 346 content::ResourceType resource_type; | 338 content::ResourceType resource_type; |
| 347 } tests[] = {{false, content::RESOURCE_TYPE_MAIN_FRAME}, | 339 } tests[] = {{false, content::RESOURCE_TYPE_MAIN_FRAME}, |
| 348 {false, content::RESOURCE_TYPE_SUB_FRAME}, | 340 {false, content::RESOURCE_TYPE_SUB_FRAME}, |
| 349 {false, content::RESOURCE_TYPE_STYLESHEET}, | 341 {false, content::RESOURCE_TYPE_STYLESHEET}, |
| 350 {false, content::RESOURCE_TYPE_SCRIPT}, | 342 {false, content::RESOURCE_TYPE_SCRIPT}, |
| 351 {false, content::RESOURCE_TYPE_IMAGE}, | 343 {false, content::RESOURCE_TYPE_IMAGE}, |
| 352 {false, content::RESOURCE_TYPE_FONT_RESOURCE}, | 344 {false, content::RESOURCE_TYPE_FONT_RESOURCE}, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 376 {true, content::RESOURCE_TYPE_SHARED_WORKER}, | 368 {true, content::RESOURCE_TYPE_SHARED_WORKER}, |
| 377 {true, content::RESOURCE_TYPE_PREFETCH}, | 369 {true, content::RESOURCE_TYPE_PREFETCH}, |
| 378 {true, content::RESOURCE_TYPE_FAVICON}, | 370 {true, content::RESOURCE_TYPE_FAVICON}, |
| 379 {true, content::RESOURCE_TYPE_XHR}, | 371 {true, content::RESOURCE_TYPE_XHR}, |
| 380 {true, content::RESOURCE_TYPE_PING}, | 372 {true, content::RESOURCE_TYPE_PING}, |
| 381 {true, content::RESOURCE_TYPE_SERVICE_WORKER}, | 373 {true, content::RESOURCE_TYPE_SERVICE_WORKER}, |
| 382 {true, content::RESOURCE_TYPE_CSP_REPORT}, | 374 {true, content::RESOURCE_TYPE_CSP_REPORT}, |
| 383 {true, content::RESOURCE_TYPE_PLUGIN_RESOURCE}}; | 375 {true, content::RESOURCE_TYPE_PLUGIN_RESOURCE}}; |
| 384 | 376 |
| 385 for (size_t i = 0; i < arraysize(tests); ++i) { | 377 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 386 std::unique_ptr<net::URLRequest> request = CreateRequestByType( | 378 std::unique_ptr<net::URLRequest> request = |
| 387 tests[i].resource_type, false, tests[i].is_using_lofi); | 379 CreateRequestByType(tests[i].resource_type, false, |
| 380 tests[i].is_using_lofi ? content::SERVER_LOFI_ON |
| 381 : content::PREVIEWS_OFF); |
| 388 net::HttpRequestHeaders headers; | 382 net::HttpRequestHeaders headers; |
| 389 NotifyBeforeSendHeaders(&headers, request.get(), true); | 383 NotifyBeforeSendHeaders(&headers, request.get(), true); |
| 390 bool is_lofi_resource_type = | 384 bool is_lofi_resource_type = |
| 391 !(tests[i].resource_type == content::RESOURCE_TYPE_MAIN_FRAME || | 385 !(tests[i].resource_type == content::RESOURCE_TYPE_MAIN_FRAME || |
| 392 tests[i].resource_type == content::RESOURCE_TYPE_STYLESHEET || | 386 tests[i].resource_type == content::RESOURCE_TYPE_STYLESHEET || |
| 393 tests[i].resource_type == content::RESOURCE_TYPE_SCRIPT || | 387 tests[i].resource_type == content::RESOURCE_TYPE_SCRIPT || |
| 394 tests[i].resource_type == content::RESOURCE_TYPE_FONT_RESOURCE || | 388 tests[i].resource_type == content::RESOURCE_TYPE_FONT_RESOURCE || |
| 395 tests[i].resource_type == content::RESOURCE_TYPE_MEDIA || | 389 tests[i].resource_type == content::RESOURCE_TYPE_MEDIA || |
| 396 tests[i].resource_type == content::RESOURCE_TYPE_CSP_REPORT); | 390 tests[i].resource_type == content::RESOURCE_TYPE_CSP_REPORT); |
| 397 | 391 |
| 398 VerifyLoFiHeader(is_lofi_resource_type, !tests[i].is_using_lofi, headers); | 392 VerifyLoFiHeader(is_lofi_resource_type, !tests[i].is_using_lofi, headers); |
| 399 VerifyLitePageHeader(false, false, headers); | 393 VerifyLitePageHeader(false, false, headers); |
| 400 VerifyLitePageIgnoreBlacklistHeader(false, headers); | 394 VerifyLitePageIgnoreBlacklistHeader(false, headers); |
| 401 DataReductionProxyData* data = DataReductionProxyData::GetData(*request); | 395 DataReductionProxyData* data = DataReductionProxyData::GetData(*request); |
| 402 EXPECT_EQ(tests[i].is_using_lofi, data->lofi_requested()) << i; | 396 EXPECT_EQ(tests[i].is_using_lofi, data->lofi_requested()) << i; |
| 403 } | 397 } |
| 404 } | 398 } |
| 405 | 399 |
| 406 TEST_F(ContentLoFiDeciderTest, LoFiControlFieldTrial) { | 400 TEST_F(ContentLoFiDeciderTest, LoFiControlFieldTrial) { |
| 407 base::FieldTrialList field_trial_list(nullptr); | 401 base::FieldTrialList field_trial_list(nullptr); |
| 408 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), | 402 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), |
| 409 "Control"); | 403 "Control"); |
| 410 // Enable Lo-Fi. | 404 // Enable Lo-Fi. |
| 411 const struct { | 405 const struct { |
| 412 bool is_using_lofi; | 406 bool is_using_lofi; |
| 413 bool is_main_frame; | 407 bool is_main_frame; |
| 414 } tests[] = {{false, false}, {false, true}, {true, false}, {true, true}}; | 408 } tests[] = {{false, false}, {false, true}, {true, false}, {true, true}}; |
| 415 | 409 |
| 416 for (size_t i = 0; i < arraysize(tests); ++i) { | 410 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 417 std::unique_ptr<net::URLRequest> request = | 411 std::unique_ptr<net::URLRequest> request = CreateRequest( |
| 418 CreateRequest(tests[i].is_main_frame, tests[i].is_using_lofi); | 412 tests[i].is_main_frame, tests[i].is_using_lofi ? content::SERVER_LOFI_ON |
| 413 : content::PREVIEWS_OFF); |
| 419 net::HttpRequestHeaders headers; | 414 net::HttpRequestHeaders headers; |
| 420 NotifyBeforeSendHeaders(&headers, request.get(), true); | 415 NotifyBeforeSendHeaders(&headers, request.get(), true); |
| 421 VerifyLoFiHeader(false, false, headers); | 416 VerifyLoFiHeader(false, false, headers); |
| 422 VerifyLitePageHeader(false, false, headers); | 417 VerifyLitePageHeader(false, false, headers); |
| 423 VerifyLitePageIgnoreBlacklistHeader(false, headers); | 418 VerifyLitePageIgnoreBlacklistHeader(false, headers); |
| 424 DataReductionProxyData* data = DataReductionProxyData::GetData(*request); | 419 DataReductionProxyData* data = DataReductionProxyData::GetData(*request); |
| 425 EXPECT_EQ(tests[i].is_using_lofi, data->lofi_requested()) << i; | 420 EXPECT_EQ(tests[i].is_using_lofi, data->lofi_requested()) << i; |
| 426 } | 421 } |
| 427 } | 422 } |
| 428 | 423 |
| 429 TEST_F(ContentLoFiDeciderTest, LitePageFieldTrial) { | 424 TEST_F(ContentLoFiDeciderTest, LitePageFieldTrial) { |
| 430 base::FieldTrialList field_trial_list(nullptr); | 425 base::FieldTrialList field_trial_list(nullptr); |
| 431 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), | 426 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), |
| 432 "Enabled_Preview"); | 427 "Enabled_Preview"); |
| 433 // Enable Lo-Fi. | 428 // Enable Lo-Fi. |
| 434 const struct { | 429 const struct { |
| 435 bool is_using_lofi; | 430 bool is_using_lite_page; |
| 436 bool is_main_frame; | 431 bool is_main_frame; |
| 437 } tests[] = { | 432 } tests[] = { |
| 438 {false, false}, {true, false}, {false, true}, {true, true}, | 433 {false, false}, {true, false}, {false, true}, {true, true}, |
| 439 }; | 434 }; |
| 440 | 435 |
| 441 for (size_t i = 0; i < arraysize(tests); ++i) { | 436 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 442 std::unique_ptr<net::URLRequest> request = | 437 std::unique_ptr<net::URLRequest> request = |
| 443 CreateRequest(tests[i].is_main_frame, tests[i].is_using_lofi); | 438 CreateRequest(tests[i].is_main_frame, tests[i].is_using_lite_page |
| 439 ? content::SERVER_LITE_PAGE_ON |
| 440 : content::PREVIEWS_OFF); |
| 444 net::HttpRequestHeaders headers; | 441 net::HttpRequestHeaders headers; |
| 445 NotifyBeforeSendHeaders(&headers, request.get(), true); | 442 NotifyBeforeSendHeaders(&headers, request.get(), true); |
| 446 VerifyLoFiHeader(false, false, headers); | 443 VerifyLoFiHeader(false, false, headers); |
| 447 VerifyLitePageHeader(tests[i].is_main_frame, !tests[i].is_using_lofi, | 444 VerifyLitePageHeader(tests[i].is_main_frame, !tests[i].is_using_lite_page, |
| 448 headers); | 445 headers); |
| 449 VerifyLitePageIgnoreBlacklistHeader(false, headers); | 446 VerifyLitePageIgnoreBlacklistHeader(false, headers); |
| 450 DataReductionProxyData* data = DataReductionProxyData::GetData(*request); | 447 DataReductionProxyData* data = DataReductionProxyData::GetData(*request); |
| 451 EXPECT_EQ(tests[i].is_using_lofi, data->lofi_requested()) << i; | 448 EXPECT_EQ(tests[i].is_using_lite_page, data->lofi_requested()) << i; |
| 452 } | 449 } |
| 453 } | 450 } |
| 454 | 451 |
| 452 TEST_F(ContentLoFiDeciderTest, LitePageFieldTrialFallbackEnabled) { |
| 453 base::FieldTrialList field_trial_list(nullptr); |
| 454 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), |
| 455 "Enabled_Preview"); |
| 456 base::FieldTrialList::CreateFieldTrial( |
| 457 params::GetLitePageFallbackFieldTrialName(), "Enabled"); |
| 458 |
| 459 // Enable Lo-Fi. |
| 460 const struct { |
| 461 bool is_using_lofi; |
| 462 bool is_using_lite_page; |
| 463 bool is_main_frame; |
| 464 } tests[] = { |
| 465 {false, false, false}, {false, false, true}, {true, false, true}, |
| 466 {true, false, false}, {false, true, false}, {false, true, true}, |
| 467 {true, true, true}, {true, true, false}, |
| 468 }; |
| 469 |
| 470 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 471 content::PreviewsState previews_state = content::PREVIEWS_UNSPECIFIED; |
| 472 if (tests[i].is_using_lofi) |
| 473 previews_state |= content::SERVER_LOFI_ON; |
| 474 if (tests[i].is_using_lite_page) |
| 475 previews_state |= content::SERVER_LITE_PAGE_ON; |
| 476 if (previews_state == content::PREVIEWS_UNSPECIFIED) |
| 477 previews_state = content::PREVIEWS_OFF; |
| 478 |
| 479 std::unique_ptr<net::URLRequest> request = |
| 480 CreateRequest(tests[i].is_main_frame, previews_state); |
| 481 net::HttpRequestHeaders headers; |
| 482 NotifyBeforeSendHeaders(&headers, request.get(), true); |
| 483 VerifyLoFiHeader(!tests[i].is_main_frame && !tests[i].is_using_lite_page, |
| 484 !tests[i].is_main_frame && !tests[i].is_using_lofi && |
| 485 !tests[i].is_using_lite_page, |
| 486 headers); |
| 487 VerifyLitePageHeader(tests[i].is_main_frame, |
| 488 tests[i].is_main_frame && !tests[i].is_using_lite_page, |
| 489 headers); |
| 490 VerifyLitePageIgnoreBlacklistHeader(false, headers); |
| 491 DataReductionProxyData* data = DataReductionProxyData::GetData(*request); |
| 492 EXPECT_EQ(tests[i].is_using_lofi || tests[i].is_using_lite_page, |
| 493 data->lofi_requested()) |
| 494 << i; |
| 495 } |
| 496 } |
| 497 |
| 498 TEST_F(ContentLoFiDeciderTest, LitePageFieldTrialFallbackDisabled) { |
| 499 base::FieldTrialList field_trial_list(nullptr); |
| 500 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), |
| 501 "Enabled_Preview"); |
| 502 // Enable Lo-Fi. |
| 503 const struct { |
| 504 bool is_using_lofi; |
| 505 bool is_using_lite_page; |
| 506 bool is_main_frame; |
| 507 } tests[] = { |
| 508 {false, false, false}, {false, false, true}, {true, false, true}, |
| 509 {true, false, false}, {false, true, false}, {false, true, true}, |
| 510 {true, true, true}, {true, true, false}, |
| 511 }; |
| 512 |
| 513 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 514 content::PreviewsState previews_state = content::PREVIEWS_UNSPECIFIED; |
| 515 if (tests[i].is_using_lofi) |
| 516 previews_state |= content::SERVER_LOFI_ON; |
| 517 if (tests[i].is_using_lite_page) |
| 518 previews_state |= content::SERVER_LITE_PAGE_ON; |
| 519 if (previews_state == content::PREVIEWS_UNSPECIFIED) |
| 520 previews_state = content::PREVIEWS_OFF; |
| 521 |
| 522 std::unique_ptr<net::URLRequest> request = |
| 523 CreateRequest(tests[i].is_main_frame, previews_state); |
| 524 net::HttpRequestHeaders headers; |
| 525 NotifyBeforeSendHeaders(&headers, request.get(), true); |
| 526 VerifyLoFiHeader(false, false, headers); |
| 527 VerifyLitePageHeader(tests[i].is_main_frame, |
| 528 tests[i].is_main_frame && !tests[i].is_using_lite_page, |
| 529 headers); |
| 530 VerifyLitePageIgnoreBlacklistHeader(false, headers); |
| 531 DataReductionProxyData* data = DataReductionProxyData::GetData(*request); |
| 532 EXPECT_EQ(tests[i].is_using_lofi || tests[i].is_using_lite_page, |
| 533 data->lofi_requested()) |
| 534 << i; |
| 535 } |
| 536 } |
| 537 |
| 455 TEST_F(ContentLoFiDeciderTest, AutoLoFi) { | 538 TEST_F(ContentLoFiDeciderTest, AutoLoFi) { |
| 456 const struct { | 539 const struct { |
| 457 bool auto_lofi_enabled_group; | 540 bool auto_lofi_enabled_group; |
| 458 bool auto_lofi_control_group; | 541 bool auto_lofi_control_group; |
| 459 bool network_prohibitively_slow; | 542 bool network_prohibitively_slow; |
| 460 bool is_main_frame; | 543 bool is_main_frame; |
| 461 } tests[] = { | 544 } tests[] = { |
| 462 {false, false, false, false}, | 545 {false, false, false, false}, |
| 463 {false, false, true, false}, | 546 {false, false, true, false}, |
| 464 {true, false, false, false}, | 547 {true, false, false, false}, |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 552 VerifyLoFiHeader(expect_lofi_header, false, headers); | 635 VerifyLoFiHeader(expect_lofi_header, false, headers); |
| 553 } | 636 } |
| 554 } | 637 } |
| 555 | 638 |
| 556 TEST_F(ContentLoFiDeciderTest, ProxyIsNotDataReductionProxy) { | 639 TEST_F(ContentLoFiDeciderTest, ProxyIsNotDataReductionProxy) { |
| 557 base::FieldTrialList field_trial_list(nullptr); | 640 base::FieldTrialList field_trial_list(nullptr); |
| 558 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), | 641 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), |
| 559 "Enabled"); | 642 "Enabled"); |
| 560 // Enable Lo-Fi. | 643 // Enable Lo-Fi. |
| 561 const struct { | 644 const struct { |
| 562 bool is_using_lofi; | 645 content::PreviewsState previews_state; |
| 563 } tests[] = { | 646 } tests[] = { |
| 564 {false}, {true}, | 647 {content::PREVIEWS_OFF}, {content::SERVER_LOFI_ON}, |
| 565 }; | 648 }; |
| 566 | 649 |
| 567 for (size_t i = 0; i < arraysize(tests); ++i) { | 650 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 568 std::unique_ptr<net::URLRequest> request = | 651 std::unique_ptr<net::URLRequest> request = |
| 569 CreateRequest(false, tests[i].is_using_lofi); | 652 CreateRequest(false, tests[i].previews_state); |
| 570 net::HttpRequestHeaders headers; | 653 net::HttpRequestHeaders headers; |
| 571 NotifyBeforeSendHeaders(&headers, request.get(), false); | 654 NotifyBeforeSendHeaders(&headers, request.get(), false); |
| 572 std::string header_value; | 655 std::string header_value; |
| 573 headers.GetHeader(chrome_proxy_accept_transform_header(), &header_value); | 656 headers.GetHeader(chrome_proxy_accept_transform_header(), &header_value); |
| 574 EXPECT_EQ(std::string::npos, header_value.find(empty_image_directive())); | 657 EXPECT_EQ(std::string::npos, header_value.find(empty_image_directive())); |
| 575 } | 658 } |
| 576 } | 659 } |
| 577 | 660 |
| 578 TEST_F(ContentLoFiDeciderTest, VideoDirectiveNotOverridden) { | 661 TEST_F(ContentLoFiDeciderTest, VideoDirectiveNotOverridden) { |
| 579 base::FieldTrialList field_trial_list(nullptr); | 662 base::FieldTrialList field_trial_list(nullptr); |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 695 lofi_decider->MaybeSetIgnorePreviewsBlacklistDirective(&headers); | 778 lofi_decider->MaybeSetIgnorePreviewsBlacklistDirective(&headers); |
| 696 EXPECT_TRUE(headers.HasHeader(chrome_proxy_header())); | 779 EXPECT_TRUE(headers.HasHeader(chrome_proxy_header())); |
| 697 headers.GetHeader(chrome_proxy_header(), &header_value); | 780 headers.GetHeader(chrome_proxy_header(), &header_value); |
| 698 EXPECT_EQ("Foo, exp=ignore_preview_blacklist", header_value); | 781 EXPECT_EQ("Foo, exp=ignore_preview_blacklist", header_value); |
| 699 } | 782 } |
| 700 | 783 |
| 701 TEST_F(ContentLoFiDeciderTest, NoTransformDoesNotAddHeader) { | 784 TEST_F(ContentLoFiDeciderTest, NoTransformDoesNotAddHeader) { |
| 702 base::FieldTrialList field_trial_list(nullptr); | 785 base::FieldTrialList field_trial_list(nullptr); |
| 703 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), | 786 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), |
| 704 "Enabled"); | 787 "Enabled"); |
| 705 std::unique_ptr<net::URLRequest> request = CreateNoTransformRequest(false); | 788 std::unique_ptr<net::URLRequest> request = |
| 789 CreateRequest(false, content::PREVIEWS_NO_TRANSFORM); |
| 706 net::HttpRequestHeaders headers; | 790 net::HttpRequestHeaders headers; |
| 707 NotifyBeforeSendHeaders(&headers, request.get(), true); | 791 NotifyBeforeSendHeaders(&headers, request.get(), true); |
| 708 EXPECT_FALSE(headers.HasHeader(chrome_proxy_accept_transform_header())); | 792 EXPECT_FALSE(headers.HasHeader(chrome_proxy_accept_transform_header())); |
| 709 } | 793 } |
| 710 | 794 |
| 711 } // namespace data_reduction_proxy | 795 } // namespace data_reduction_proxy |
| OLD | NEW |