Chromium Code Reviews| Index: components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate_unittest.cc |
| diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate_unittest.cc |
| index 0e37cbf6c02c1c012245e48152132ffa705ffc0a..d525738b2df860a511d9e41c5100aa2b1f4c547d 100644 |
| --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate_unittest.cc |
| +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate_unittest.cc |
| @@ -98,9 +98,7 @@ class DataReductionProxyNetworkDelegateTest : public testing::Test { |
| test_context_->io_data(), bypass_stats_.get()); |
| } |
| - const net::ProxyConfig& GetProxyConfig() const { |
| - return config_; |
| - } |
| + const net::ProxyConfig& GetProxyConfig() const { return config_; } |
| MockDataReductionProxyConfig* config() const { |
| return test_context_->mock_config(); |
| @@ -125,21 +123,22 @@ class DataReductionProxyNetworkDelegateTest : public testing::Test { |
| scoped_ptr<net::URLRequest> FetchURLRequest( |
| const GURL& url, |
| const std::string& raw_response_headers, |
| - int64 response_content_length) { |
| - scoped_ptr<net::URLRequest> request = context_.CreateRequest( |
| - url, net::IDLE, &delegate_); |
| + int64 response_content_length, |
| + net::LoFiState lofi_state = net::LOFI_DEFAULT) { |
| + scoped_ptr<net::URLRequest> request = |
| + context_.CreateRequest(url, net::IDLE, &delegate_); |
| // Create a test job that will fill in the given response headers for the |
| // |fake_request|. |
| scoped_refptr<net::URLRequestTestJob> test_job( |
| - new net::URLRequestTestJob(request.get(), |
| - context_.network_delegate(), |
| + new net::URLRequestTestJob(request.get(), context_.network_delegate(), |
| raw_response_headers, std::string(), true)); |
| // Configure the interceptor to use the test job to handle the next request. |
| test_job_interceptor_->set_main_intercept_job(test_job.get()); |
| request->set_received_response_content_length(response_content_length); |
| + request->set_lofi_state(lofi_state); |
| net::HttpResponseInfo& response_info = |
| const_cast<net::HttpResponseInfo&>(request->response_info()); |
| response_info.network_accessed = true; |
| @@ -166,9 +165,7 @@ class DataReductionProxyNetworkDelegateTest : public testing::Test { |
| return test_context_->io_data(); |
| } |
| - TestDataReductionProxyConfig* config() { |
| - return test_context_->config(); |
| - } |
| + TestDataReductionProxyConfig* config() { return test_context_->config(); } |
| scoped_ptr<DataReductionProxyNetworkDelegate> |
| data_reduction_proxy_network_delegate_; |
| @@ -216,14 +213,12 @@ TEST_F(DataReductionProxyNetworkDelegateTest, LoFiTransitions) { |
| bool auto_lofi_enabled; |
| } tests[] = { |
| { |
| - // Lo-Fi enabled through switch. |
| - false, |
| - true, |
| + // Lo-Fi enabled through switch. |
| + false, true, |
| }, |
| { |
| - // Lo-Fi enabled through field trial. |
| - true, |
| - false, |
| + // Lo-Fi enabled through field trial. |
| + true, false, |
| }, |
| }; |
| @@ -255,11 +250,11 @@ TEST_F(DataReductionProxyNetworkDelegateTest, LoFiTransitions) { |
| } |
| { |
| - // Bypass cache flag used. Lo-Fi should not be used. |
| + // Lo-Fi already off. Lo-Fi should not be used. |
|
bengr
2015/08/25 00:00:02
Lo-Fi is already off. Add "is" on 267, 282, and 31
megjablon
2015/08/25 20:29:46
Done.
|
| net::HttpRequestHeaders headers; |
| scoped_ptr<net::URLRequest> fake_request( |
| FetchURLRequest(GURL("http://www.google.com/"), std::string(), 0)); |
| - fake_request->SetLoadFlags(net::LOAD_BYPASS_CACHE); |
| + fake_request->set_lofi_state(net::LOFI_OFF); |
| data_reduction_proxy_network_delegate_->NotifyBeforeSendProxyHeaders( |
| fake_request.get(), data_reduction_proxy_info, &headers); |
| VerifyLoFiHeader(false, headers); |
| @@ -269,11 +264,12 @@ TEST_F(DataReductionProxyNetworkDelegateTest, LoFiTransitions) { |
| } |
| { |
| - // Bypass cache flag not used. Lo-Fi should be used. |
| + // Lo-Fi already on. Lo-Fi should be used. |
| net::HttpRequestHeaders headers; |
| scoped_ptr<net::URLRequest> fake_request( |
| FetchURLRequest(GURL("http://www.google.com/"), std::string(), 0)); |
| + fake_request->set_lofi_state(net::LOFI_ON); |
| data_reduction_proxy_network_delegate_->NotifyBeforeSendProxyHeaders( |
| fake_request.get(), data_reduction_proxy_info, &headers); |
| VerifyLoFiHeader(true, headers); |
| @@ -283,11 +279,12 @@ TEST_F(DataReductionProxyNetworkDelegateTest, LoFiTransitions) { |
| } |
| { |
| - // Bypass cache flag used. Lo-Fi should not be used. |
| + // Lo-Fi off. Lo-Fi should not be used. |
| net::HttpRequestHeaders headers; |
| scoped_ptr<net::URLRequest> fake_request( |
| FetchURLRequest(GURL("http://www.google.com/"), std::string(), 0)); |
| fake_request->SetLoadFlags(net::LOAD_BYPASS_CACHE); |
| + fake_request->set_lofi_state(net::LOFI_OFF); |
| data_reduction_proxy_network_delegate_->NotifyBeforeSendProxyHeaders( |
| fake_request.get(), data_reduction_proxy_info, &headers); |
| VerifyLoFiHeader(false, headers); |
| @@ -297,12 +294,13 @@ TEST_F(DataReductionProxyNetworkDelegateTest, LoFiTransitions) { |
| } |
| { |
| - // Main frame request with bypass cache flag. Lo-Fi should not be used. |
| + // Main frame request with Lo-Fi off. Lo-Fi should not be used. |
| // State of Lo-Fi should persist until next page load. |
| net::HttpRequestHeaders headers; |
| scoped_ptr<net::URLRequest> fake_request( |
| FetchURLRequest(GURL("http://www.google.com/"), std::string(), 0)); |
| - fake_request->SetLoadFlags(net::LOAD_MAIN_FRAME | net::LOAD_BYPASS_CACHE); |
| + fake_request->SetLoadFlags(net::LOAD_MAIN_FRAME); |
| + fake_request->set_lofi_state(net::LOFI_OFF); |
| data_reduction_proxy_network_delegate_->NotifyBeforeSendProxyHeaders( |
| fake_request.get(), data_reduction_proxy_info, &headers); |
| VerifyLoFiHeader(false, headers); |
| @@ -310,10 +308,11 @@ TEST_F(DataReductionProxyNetworkDelegateTest, LoFiTransitions) { |
| } |
| { |
| - // Bypass cache flag not used. Lo-Fi is still not used. |
| + // Lo-Fi off. Lo-Fi is still not used. |
| net::HttpRequestHeaders headers; |
| scoped_ptr<net::URLRequest> fake_request( |
| FetchURLRequest(GURL("http://www.google.com/"), std::string(), 0)); |
| + fake_request->set_lofi_state(net::LOFI_OFF); |
| data_reduction_proxy_network_delegate_->NotifyBeforeSendProxyHeaders( |
| fake_request.get(), data_reduction_proxy_info, &headers); |
| VerifyLoFiHeader(false, headers); |
| @@ -336,6 +335,126 @@ TEST_F(DataReductionProxyNetworkDelegateTest, LoFiTransitions) { |
| } |
| } |
| +TEST_F(DataReductionProxyNetworkDelegateTest, LoFiOn) { |
| + const struct { |
| + bool lofi_switch_enabled; |
| + bool lofi_enabled_field_trial_group; |
| + bool network_prohibitively_slow; |
| + bool expect_lofi_header; |
| + int bucket_to_check_for_auto_lofi_uma; |
| + int expect_bucket_count; |
| + |
| + } tests[] = { |
| + { |
| + // Lo-Fi off. |
| + false, false, false, false, 0, |
| + 0, // not in enabled field trial, UMA is not recorded |
|
bengr
2015/08/25 00:00:01
// not in enabled field trial si UMA should not be
megjablon
2015/08/25 20:29:46
Done.
|
| + }, |
| + { |
| + // In enabled field trial group but network quality is not bad. |
| + false, true, false, false, 0, |
| + 1, // Lo-Fi request header is not used (state change: empty to empty) |
| + }, |
| + { |
| + // Not in enabled field trial group and network quality is bad. |
| + false, false, true, false, 0, |
| + 0, // not in enabled field trial, UMA is not recorded |
| + }, |
| + { |
| + // In enabled field trial group and network quality is bad. |
| + false, true, true, true, 1, |
| + 1, // Lo-Fi request header is now used (state change: empty to low) |
| + }, |
| + { |
| + // Lo-Fi enabled through command line switch. |
| + true, false, false, true, 0, |
| + 0, // not in enabled field trial, UMA is not recorded |
| + }, |
| + }; |
| + for (size_t i = 0; i < arraysize(tests); ++i) { |
| + config()->ResetLoFiStatusForTest(); |
| + if (tests[i].lofi_switch_enabled) { |
| + base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| + switches::kDataReductionProxyLoFi, |
| + switches::kDataReductionProxyLoFiValueAlwaysOn); |
| + } |
| + |
| + config()->SetIncludedInLoFiEnabledFieldTrial( |
| + tests[i].lofi_enabled_field_trial_group); |
| + |
| + config()->SetNetworkProhibitivelySlow(tests[i].network_prohibitively_slow); |
| + |
| + base::HistogramTester histogram_tester; |
| + |
| + // Data reduction proxy info |
| + net::ProxyInfo data_reduction_proxy_info; |
| + std::string data_reduction_proxy; |
| + base::TrimString(params()->DefaultOrigin(), "/", &data_reduction_proxy); |
| + data_reduction_proxy_info.UsePacString( |
| + "PROXY " + |
| + net::ProxyServer::FromURI(params()->DefaultOrigin(), |
| + net::ProxyServer::SCHEME_HTTP) |
| + .host_port_pair() |
| + .ToString() + |
| + "; DIRECT"); |
| + EXPECT_FALSE(data_reduction_proxy_info.is_empty()); |
| + |
| + net::HttpRequestHeaders headers; |
| + scoped_ptr<net::URLRequest> fake_request( |
| + FetchURLRequest(GURL("http://www.google.com/"), std::string(), 0)); |
| + fake_request->SetLoadFlags(net::LOAD_MAIN_FRAME); |
| + data_reduction_proxy_network_delegate_->NotifyBeforeSendProxyHeaders( |
| + fake_request.get(), data_reduction_proxy_info, &headers); |
| + |
| + if (tests[i].expect_bucket_count != 0) { |
| + histogram_tester.ExpectBucketCount( |
| + "DataReductionProxy.AutoLoFiRequestHeaderState.Unknown", |
| + tests[i].bucket_to_check_for_auto_lofi_uma, |
| + tests[i].expect_bucket_count); |
| + } |
| + |
| + EXPECT_EQ(tests[i].expect_lofi_header, |
| + fake_request->lofi_state() == net::LOFI_ON) |
| + << i; |
| + } |
| +} |
| + |
| +TEST_F(DataReductionProxyNetworkDelegateTest, LoFiMainframe) { |
| + config()->ResetLoFiStatusForTest(); |
| + base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| + switches::kDataReductionProxyLoFi, |
| + switches::kDataReductionProxyLoFiValueAlwaysOn); |
| + |
| + // Data reduction proxy info |
| + net::ProxyInfo data_reduction_proxy_info; |
| + std::string data_reduction_proxy; |
| + base::TrimString(params()->DefaultOrigin(), "/", &data_reduction_proxy); |
| + data_reduction_proxy_info.UsePacString( |
| + "PROXY " + |
| + net::ProxyServer::FromURI(params()->DefaultOrigin(), |
| + net::ProxyServer::SCHEME_HTTP) |
| + .host_port_pair() |
| + .ToString() + |
| + "; DIRECT"); |
| + EXPECT_FALSE(data_reduction_proxy_info.is_empty()); |
| + |
| + net::HttpRequestHeaders headers; |
| + scoped_ptr<net::URLRequest> fake_request( |
| + FetchURLRequest(GURL("http://www.google.com/"), std::string(), 0)); |
| + fake_request->SetLoadFlags(net::LOAD_MAIN_FRAME); |
| + data_reduction_proxy_network_delegate_->NotifyBeforeSendProxyHeaders( |
| + fake_request.get(), data_reduction_proxy_info, &headers); |
| + |
| + EXPECT_EQ(net::LOFI_ON, fake_request->lofi_state()); |
| + |
| + fake_request = |
| + FetchURLRequest(GURL("http://www.google.com/"), std::string(), 0); |
| + data_reduction_proxy_network_delegate_->NotifyBeforeSendProxyHeaders( |
| + fake_request.get(), data_reduction_proxy_info, &headers); |
| + |
| + EXPECT_EQ(net::LOFI_OFF, fake_request->lofi_state()); |
| +} |
| + |
| TEST_F(DataReductionProxyNetworkDelegateTest, NetHistograms) { |
| const std::string kReceivedValidOCLHistogramName = |
| "Net.HttpContentLengthWithValidOCL"; |
| @@ -380,13 +499,14 @@ TEST_F(DataReductionProxyNetworkDelegateTest, NetHistograms) { |
| HeadersToRaw(&raw_headers); |
| - scoped_ptr<net::URLRequest> fake_request( |
| - FetchURLRequest(GURL("http://www.google.com/"), |
| - raw_headers, kResponseContentLength)); |
| + scoped_ptr<net::URLRequest> fake_request(FetchURLRequest( |
| + GURL("http://www.google.com/"), raw_headers, kResponseContentLength)); |
| base::TimeDelta freshness_lifetime = |
| - fake_request->response_info().headers->GetFreshnessLifetimes( |
| - fake_request->response_info().response_time).freshness; |
| + fake_request->response_info() |
| + .headers->GetFreshnessLifetimes( |
| + fake_request->response_info().response_time) |
| + .freshness; |
| histogram_tester.ExpectUniqueSample(kReceivedValidOCLHistogramName, |
| kResponseContentLength, 1); |
| @@ -400,8 +520,8 @@ TEST_F(DataReductionProxyNetworkDelegateTest, NetHistograms) { |
| histogram_tester.ExpectUniqueSample(kOriginalHistogramName, |
| kOriginalContentLength, 1); |
| histogram_tester.ExpectUniqueSample( |
| - kDifferenceHistogramName, |
| - kOriginalContentLength - kResponseContentLength, 1); |
| + kDifferenceHistogramName, kOriginalContentLength - kResponseContentLength, |
| + 1); |
| histogram_tester.ExpectUniqueSample(kFreshnessLifetimeHistogramName, |
| freshness_lifetime.InSeconds(), 1); |
| histogram_tester.ExpectUniqueSample(kCacheableHistogramName, |
| @@ -419,31 +539,23 @@ TEST_F(DataReductionProxyNetworkDelegateTest, NetHistograms) { |
| } tests[] = { |
| { |
| - // Lo-Fi disabled. |
| - false, |
| - false, |
| - 0, |
| + // Lo-Fi disabled. |
| + false, false, 0, |
| }, |
| { |
| - // Auto Lo-Fi enabled. |
| - // This should populate Lo-Fi content length histogram. |
| - false, |
| - true, |
| - 1, |
| + // Auto Lo-Fi enabled. |
| + // This should populate Lo-Fi content length histogram. |
| + false, true, 1, |
| }, |
| { |
| - // Lo-Fi enabled through switch. |
| - // This should populate Lo-Fi content length histogram. |
| - true, |
| - false, |
| - 1, |
| + // Lo-Fi enabled through switch. |
| + // This should populate Lo-Fi content length histogram. |
| + true, false, 1, |
| }, |
| { |
| - // Lo-Fi enabled through switch and Auto Lo-Fi also enabled. |
| - // This should populate Lo-Fi content length histogram. |
| - true, |
| - true, |
| - 1, |
| + // Lo-Fi enabled through switch and Auto Lo-Fi also enabled. |
| + // This should populate Lo-Fi content length histogram. |
| + true, true, 1, |
| }, |
| }; |
| @@ -458,10 +570,10 @@ TEST_F(DataReductionProxyNetworkDelegateTest, NetHistograms) { |
| switches::kDataReductionProxyLoFiValueAlwaysOn); |
| } |
| - config()->UpdateLoFiStatusOnMainFrameRequest(false, nullptr); |
| - |
| - fake_request = (FetchURLRequest(GURL("http://www.example.com/"), |
| - raw_headers, kResponseContentLength)); |
| + fake_request = FetchURLRequest( |
| + GURL("http://www.example.com/"), raw_headers, kResponseContentLength, |
| + config()->ShouldTurnOnLoFiOnMainFrameRequest(nullptr) ? net::LOFI_ON |
| + : net::LOFI_OFF); |
| // Histograms are accumulative, so get the sum of all the tests so far. |
| int expected_count = 0; |
| @@ -499,9 +611,10 @@ TEST_F(DataReductionProxyNetworkDelegateTest, OnResolveProxyHandler) { |
| base::TrimString(params()->DefaultOrigin(), "/", &data_reduction_proxy); |
| data_reduction_proxy_info.UsePacString( |
| "PROXY " + |
| - net::ProxyServer::FromURI( |
| - params()->DefaultOrigin(), |
| - net::ProxyServer::SCHEME_HTTP).host_port_pair().ToString() + |
| + net::ProxyServer::FromURI(params()->DefaultOrigin(), |
| + net::ProxyServer::SCHEME_HTTP) |
| + .host_port_pair() |
| + .ToString() + |
| "; DIRECT"); |
| EXPECT_FALSE(data_reduction_proxy_info.is_empty()); |
| @@ -530,8 +643,8 @@ TEST_F(DataReductionProxyNetworkDelegateTest, OnResolveProxyHandler) { |
| retry_info.current_delay = base::TimeDelta::FromSeconds(1000); |
| retry_info.bad_until = base::TimeTicks().Now() + retry_info.current_delay; |
| retry_info.try_while_bad = false; |
| - data_reduction_proxy_retry_info[ |
| - data_reduction_proxy_info.proxy_server().ToURI()] = retry_info; |
| + data_reduction_proxy_retry_info[data_reduction_proxy_info.proxy_server() |
| + .ToURI()] = retry_info; |
| net::ProxyInfo result; |
| // Another proxy is used. It should be used afterwards. |
| @@ -562,26 +675,24 @@ TEST_F(DataReductionProxyNetworkDelegateTest, OnResolveProxyHandler) { |
| // Test that ws:// and wss:// URLs bypass the data reduction proxy. |
| result.UseDirect(); |
| - OnResolveProxyHandler(GURL("ws://echo.websocket.org/"), |
| - load_flags, data_reduction_proxy_config, |
| - empty_proxy_retry_info, config(), &result); |
| + OnResolveProxyHandler(GURL("ws://echo.websocket.org/"), load_flags, |
| + data_reduction_proxy_config, empty_proxy_retry_info, |
| + config(), &result); |
| EXPECT_TRUE(result.is_direct()); |
| - OnResolveProxyHandler(GURL("wss://echo.websocket.org/"), |
| - load_flags, data_reduction_proxy_config, |
| - empty_proxy_retry_info, config(), &result); |
| + OnResolveProxyHandler(GURL("wss://echo.websocket.org/"), load_flags, |
| + data_reduction_proxy_config, empty_proxy_retry_info, |
| + config(), &result); |
| EXPECT_TRUE(result.is_direct()); |
| // Without DataCompressionProxyCriticalBypass Finch trial set, the |
| // BYPASS_DATA_REDUCTION_PROXY load flag should be ignored. |
| OnResolveProxyHandler(url, load_flags, data_reduction_proxy_config, |
| - empty_proxy_retry_info, config(), |
| - &result); |
| + empty_proxy_retry_info, config(), &result); |
| EXPECT_FALSE(result.is_direct()); |
| OnResolveProxyHandler(url, load_flags, data_reduction_proxy_config, |
| - empty_proxy_retry_info, |
| - config(), &other_proxy_info); |
| + empty_proxy_retry_info, config(), &other_proxy_info); |
| EXPECT_FALSE(other_proxy_info.is_direct()); |
| } |