Chromium Code Reviews| 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 <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/metrics/field_trial.h" | 11 #include "base/metrics/field_trial.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_conf ig_test_utils.h" | 13 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_conf ig_test_utils.h" |
| 14 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_netw ork_delegate.h" | 14 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_netw ork_delegate.h" |
| 15 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test _utils.h" | 15 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test _utils.h" |
| 16 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_heade rs.h" | |
| 16 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_param s.h" | 17 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_param s.h" |
| 17 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_param s_test_utils.h" | 18 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_param s_test_utils.h" |
| 18 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_switc hes.h" | 19 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_switc hes.h" |
| 19 #include "content/public/browser/resource_request_info.h" | 20 #include "content/public/browser/resource_request_info.h" |
| 20 #include "net/base/network_delegate_impl.h" | 21 #include "net/base/network_delegate_impl.h" |
| 21 #include "net/http/http_request_headers.h" | 22 #include "net/http/http_request_headers.h" |
| 22 #include "net/proxy/proxy_info.h" | 23 #include "net/proxy/proxy_info.h" |
| 23 #include "net/socket/socket_test_util.h" | 24 #include "net/socket/socket_test_util.h" |
| 24 #include "net/url_request/url_request.h" | 25 #include "net/url_request/url_request.h" |
| 25 #include "net/url_request/url_request_test_util.h" | 26 #include "net/url_request/url_request_test_util.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 28 |
| 28 namespace data_reduction_proxy { | 29 namespace data_reduction_proxy { |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 const char kChromeProxyHeader[] = "chrome-proxy"; | |
| 33 | |
| 34 #if defined(OS_ANDROID) | 33 #if defined(OS_ANDROID) |
| 35 const Client kClient = Client::CHROME_ANDROID; | 34 const Client kClient = Client::CHROME_ANDROID; |
| 36 #elif defined(OS_IOS) | 35 #elif defined(OS_IOS) |
| 37 const Client kClient = Client::CHROME_IOS; | 36 const Client kClient = Client::CHROME_IOS; |
| 38 #elif defined(OS_MACOSX) | 37 #elif defined(OS_MACOSX) |
| 39 const Client kClient = Client::CHROME_MAC; | 38 const Client kClient = Client::CHROME_MAC; |
| 40 #elif defined(OS_CHROMEOS) | 39 #elif defined(OS_CHROMEOS) |
| 41 const Client kClient = Client::CHROME_CHROMEOS; | 40 const Client kClient = Client::CHROME_CHROMEOS; |
| 42 #elif defined(OS_LINUX) | 41 #elif defined(OS_LINUX) |
| 43 const Client kClient = Client::CHROME_LINUX; | 42 const Client kClient = Client::CHROME_LINUX; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 112 base::TrimString(test_context_->config()->test_params()->DefaultOrigin(), | 111 base::TrimString(test_context_->config()->test_params()->DefaultOrigin(), |
| 113 "/", &data_reduction_proxy); | 112 "/", &data_reduction_proxy); |
| 114 data_reduction_proxy_info.UseNamedProxy(data_reduction_proxy); | 113 data_reduction_proxy_info.UseNamedProxy(data_reduction_proxy); |
| 115 | 114 |
| 116 data_reduction_proxy_network_delegate_->NotifyBeforeSendProxyHeaders( | 115 data_reduction_proxy_network_delegate_->NotifyBeforeSendProxyHeaders( |
| 117 request, data_reduction_proxy_info, headers); | 116 request, data_reduction_proxy_info, headers); |
| 118 } | 117 } |
| 119 | 118 |
| 120 static void VerifyLoFiHeader(bool expected_lofi_used, | 119 static void VerifyLoFiHeader(bool expected_lofi_used, |
| 121 const net::HttpRequestHeaders& headers) { | 120 const net::HttpRequestHeaders& headers) { |
| 122 EXPECT_TRUE(headers.HasHeader(kChromeProxyHeader)); | 121 EXPECT_TRUE(headers.HasHeader(chrome_proxy_header())); |
| 123 std::string header_value; | 122 std::string header_value; |
| 124 headers.GetHeader(kChromeProxyHeader, &header_value); | 123 headers.GetHeader(chrome_proxy_header(), &header_value); |
| 125 EXPECT_EQ(expected_lofi_used, | 124 EXPECT_EQ( |
| 126 header_value.find("q=low") != std::string::npos); | 125 expected_lofi_used, |
| 126 header_value.find(chrome_proxy_lo_fi_directive()) != std::string::npos); | |
| 127 } | |
| 128 | |
| 129 static void VerifyLoFiExperimentHeader( | |
| 130 bool expected_lofi_experiment_used, | |
| 131 const net::HttpRequestHeaders& headers) { | |
| 132 EXPECT_TRUE(headers.HasHeader(chrome_proxy_header())); | |
| 133 std::string header_value; | |
| 134 headers.GetHeader(chrome_proxy_header(), &header_value); | |
| 135 EXPECT_EQ(expected_lofi_experiment_used, | |
| 136 header_value.find(chrome_proxy_lo_fi_experiment_directive()) != | |
| 137 std::string::npos); | |
| 127 } | 138 } |
| 128 | 139 |
| 129 protected: | 140 protected: |
| 130 base::MessageLoopForIO message_loop_; | 141 base::MessageLoopForIO message_loop_; |
| 131 net::MockClientSocketFactory mock_socket_factory_; | 142 net::MockClientSocketFactory mock_socket_factory_; |
| 132 net::TestURLRequestContext context_; | 143 net::TestURLRequestContext context_; |
| 133 net::TestDelegate delegate_; | 144 net::TestDelegate delegate_; |
| 134 scoped_ptr<DataReductionProxyTestContext> test_context_; | 145 scoped_ptr<DataReductionProxyTestContext> test_context_; |
| 135 scoped_ptr<DataReductionProxyNetworkDelegate> | 146 scoped_ptr<DataReductionProxyNetworkDelegate> |
| 136 data_reduction_proxy_network_delegate_; | 147 data_reduction_proxy_network_delegate_; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 215 }; | 226 }; |
| 216 | 227 |
| 217 for (size_t i = 0; i < arraysize(tests); ++i) { | 228 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 218 scoped_ptr<net::URLRequest> request = CreateRequest(tests[i].is_using_lofi); | 229 scoped_ptr<net::URLRequest> request = CreateRequest(tests[i].is_using_lofi); |
| 219 net::HttpRequestHeaders headers; | 230 net::HttpRequestHeaders headers; |
| 220 NotifyBeforeSendProxyHeaders(&headers, request.get()); | 231 NotifyBeforeSendProxyHeaders(&headers, request.get()); |
| 221 VerifyLoFiHeader(false, headers); | 232 VerifyLoFiHeader(false, headers); |
| 222 } | 233 } |
| 223 } | 234 } |
| 224 | 235 |
| 236 TEST_F(ContentLoFiDeciderTest, AutoLoFi) { | |
| 237 const struct { | |
| 238 bool auto_lofi_enabled_group; | |
| 239 bool auto_lofi_control_group; | |
| 240 bool network_prohibitively_slow; | |
| 241 } tests[] = { | |
| 242 {false, false, false}, | |
| 243 {false, false, true}, | |
| 244 {true, false, false}, | |
| 245 {true, false, true}, | |
| 246 {false, true, false}, | |
| 247 {false, true, true}, | |
| 248 // Repeat this test data to simulate user moving out of Lo-Fi control | |
| 249 // experiment. | |
| 250 {false, true, false}, | |
| 251 }; | |
| 252 | |
| 253 for (size_t i = 0; i < arraysize(tests); ++i) { | |
| 254 test_context_->config()->ResetLoFiStatusForTest(); | |
| 255 // Lo-Fi header is expected only if session is part of Lo-Fi enabled field | |
| 256 // trial and network is prohibitively slow. | |
| 257 bool expect_lofi_header = | |
| 258 tests[i].auto_lofi_enabled_group && tests[i].network_prohibitively_slow; | |
| 259 bool expect_lofi_experiment_header = | |
| 260 tests[i].auto_lofi_control_group && tests[i].network_prohibitively_slow; | |
| 261 | |
| 262 base::FieldTrialList field_trial_list(nullptr); | |
| 263 if (tests[i].auto_lofi_enabled_group) { | |
| 264 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), | |
| 265 "Enabled"); | |
| 266 } | |
| 267 | |
| 268 if (tests[i].auto_lofi_control_group) { | |
| 269 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), | |
| 270 "Control"); | |
| 271 } | |
| 272 | |
| 273 test_context_->config()->SetNetworkProhibitivelySlow( | |
| 274 tests[i].network_prohibitively_slow); | |
| 275 | |
| 276 scoped_ptr<net::URLRequest> request = | |
| 277 CreateRequest(tests[i].network_prohibitively_slow); | |
| 278 net::HttpRequestHeaders headers; | |
| 279 NotifyBeforeSendProxyHeaders(&headers, request.get()); | |
| 280 | |
| 281 VerifyLoFiHeader(expect_lofi_header, headers); | |
| 282 VerifyLoFiExperimentHeader(expect_lofi_experiment_header, headers); | |
| 283 } | |
| 284 } | |
| 285 | |
| 286 TEST_F(ContentLoFiDeciderTest, SlowConnectionsFlag) { | |
| 287 const struct { | |
| 288 bool slow_connections_flag_enabled; | |
| 289 bool network_prohibitively_slow; | |
| 290 bool auto_lofi_enabled_group; | |
| 291 | |
| 292 } tests[] = { | |
| 293 { | |
| 294 false, false, false, | |
|
tbansal1
2015/11/23 20:39:52
Can you please make these compact (like Lines 242-
megjablon
2015/11/23 20:53:55
Done.
| |
| 295 }, | |
| 296 { | |
| 297 false, true, false, | |
| 298 }, | |
| 299 { | |
| 300 true, false, false, | |
| 301 }, | |
| 302 { | |
| 303 true, true, false, | |
| 304 }, | |
| 305 { | |
| 306 false, false, true, | |
| 307 }, | |
| 308 { | |
| 309 false, true, true, | |
| 310 }, | |
| 311 { | |
| 312 true, false, true, | |
| 313 }, | |
| 314 { | |
| 315 true, true, true, | |
| 316 }, | |
| 317 }; | |
| 318 | |
| 319 for (size_t i = 0; i < arraysize(tests); ++i) { | |
| 320 test_context_->config()->ResetLoFiStatusForTest(); | |
| 321 // For the purpose of this test, Lo-Fi header is expected only if LoFi Slow | |
| 322 // Connection Flag is enabled or session is part of Lo-Fi enabled field | |
| 323 // trial. For both cases, an additional condition is that network must be | |
| 324 // prohibitively slow. | |
| 325 bool expect_lofi_header = (tests[i].slow_connections_flag_enabled && | |
| 326 tests[i].network_prohibitively_slow) || | |
| 327 (!tests[i].slow_connections_flag_enabled && | |
| 328 tests[i].auto_lofi_enabled_group && | |
| 329 tests[i].network_prohibitively_slow); | |
| 330 | |
| 331 std::string expected_header; | |
| 332 | |
| 333 if (tests[i].slow_connections_flag_enabled) { | |
| 334 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
| 335 switches::kDataReductionProxyLoFi, | |
| 336 switches::kDataReductionProxyLoFiValueSlowConnectionsOnly); | |
| 337 } | |
| 338 | |
| 339 base::FieldTrialList field_trial_list(nullptr); | |
| 340 if (tests[i].auto_lofi_enabled_group) { | |
| 341 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), | |
| 342 "Enabled"); | |
| 343 } | |
| 344 | |
| 345 test_context_->config()->SetNetworkProhibitivelySlow( | |
| 346 tests[i].network_prohibitively_slow); | |
| 347 | |
| 348 scoped_ptr<net::URLRequest> request = | |
| 349 CreateRequest(tests[i].network_prohibitively_slow); | |
| 350 net::HttpRequestHeaders headers; | |
| 351 NotifyBeforeSendProxyHeaders(&headers, request.get()); | |
| 352 | |
| 353 VerifyLoFiHeader(expect_lofi_header, headers); | |
| 354 } | |
| 355 } | |
| 356 | |
| 225 } // namespace data_reduction_roxy | 357 } // namespace data_reduction_roxy |
| OLD | NEW |