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" |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 data_reduction_proxy_network_delegate_->NotifyBeforeSendProxyHeaders( | 116 data_reduction_proxy_network_delegate_->NotifyBeforeSendProxyHeaders( |
117 request, data_reduction_proxy_info, headers); | 117 request, data_reduction_proxy_info, headers); |
118 } | 118 } |
119 | 119 |
120 static void VerifyLoFiHeader(bool expected_lofi_used, | 120 static void VerifyLoFiHeader(bool expected_lofi_used, |
121 const net::HttpRequestHeaders& headers) { | 121 const net::HttpRequestHeaders& headers) { |
122 EXPECT_TRUE(headers.HasHeader(kChromeProxyHeader)); | 122 EXPECT_TRUE(headers.HasHeader(kChromeProxyHeader)); |
123 std::string header_value; | 123 std::string header_value; |
124 headers.GetHeader(kChromeProxyHeader, &header_value); | 124 headers.GetHeader(kChromeProxyHeader, &header_value); |
125 EXPECT_EQ(expected_lofi_used, | 125 EXPECT_EQ(expected_lofi_used, |
126 header_value.find("q=low") != std::string::npos); | 126 header_value.find(kLoFiHeader) != 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(kChromeProxyHeader)); |
| 133 std::string header_value; |
| 134 headers.GetHeader(kChromeProxyHeader, &header_value); |
| 135 EXPECT_EQ(expected_lofi_experiment_used, |
| 136 header_value.find(kLoFiExperimentHeader) != std::string::npos); |
127 } | 137 } |
128 | 138 |
129 protected: | 139 protected: |
130 base::MessageLoopForIO message_loop_; | 140 base::MessageLoopForIO message_loop_; |
131 net::MockClientSocketFactory mock_socket_factory_; | 141 net::MockClientSocketFactory mock_socket_factory_; |
132 net::TestURLRequestContext context_; | 142 net::TestURLRequestContext context_; |
133 net::TestDelegate delegate_; | 143 net::TestDelegate delegate_; |
134 scoped_ptr<DataReductionProxyTestContext> test_context_; | 144 scoped_ptr<DataReductionProxyTestContext> test_context_; |
135 scoped_ptr<DataReductionProxyNetworkDelegate> | 145 scoped_ptr<DataReductionProxyNetworkDelegate> |
136 data_reduction_proxy_network_delegate_; | 146 data_reduction_proxy_network_delegate_; |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 }; | 225 }; |
216 | 226 |
217 for (size_t i = 0; i < arraysize(tests); ++i) { | 227 for (size_t i = 0; i < arraysize(tests); ++i) { |
218 scoped_ptr<net::URLRequest> request = CreateRequest(tests[i].is_using_lofi); | 228 scoped_ptr<net::URLRequest> request = CreateRequest(tests[i].is_using_lofi); |
219 net::HttpRequestHeaders headers; | 229 net::HttpRequestHeaders headers; |
220 NotifyBeforeSendProxyHeaders(&headers, request.get()); | 230 NotifyBeforeSendProxyHeaders(&headers, request.get()); |
221 VerifyLoFiHeader(false, headers); | 231 VerifyLoFiHeader(false, headers); |
222 } | 232 } |
223 } | 233 } |
224 | 234 |
| 235 TEST_F(ContentLoFiDeciderTest, AutoLoFi) { |
| 236 const struct { |
| 237 bool auto_lofi_enabled_group; |
| 238 bool auto_lofi_control_group; |
| 239 bool network_prohibitively_slow; |
| 240 } tests[] = { |
| 241 {false, false, false}, |
| 242 {false, false, true}, |
| 243 {true, false, false}, |
| 244 {true, false, true}, |
| 245 {false, true, false}, |
| 246 {false, true, true}, |
| 247 // Repeat this test data to simulate user moving out of Lo-Fi control |
| 248 // experiment. |
| 249 {false, true, false}, |
| 250 }; |
| 251 |
| 252 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 253 test_context_->config()->ResetLoFiStatusForTest(); |
| 254 // Lo-Fi header is expected only if session is part of Lo-Fi enabled field |
| 255 // trial and network is prohibitively slow. |
| 256 bool expect_lofi_header = |
| 257 tests[i].auto_lofi_enabled_group && tests[i].network_prohibitively_slow; |
| 258 bool expect_lofi_experiment_header = |
| 259 tests[i].auto_lofi_control_group && tests[i].network_prohibitively_slow; |
| 260 |
| 261 base::FieldTrialList field_trial_list(nullptr); |
| 262 if (tests[i].auto_lofi_enabled_group) { |
| 263 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), |
| 264 "Enabled"); |
| 265 } |
| 266 |
| 267 if (tests[i].auto_lofi_control_group) { |
| 268 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), |
| 269 "Control"); |
| 270 } |
| 271 |
| 272 test_context_->config()->SetNetworkProhibitivelySlow( |
| 273 tests[i].network_prohibitively_slow); |
| 274 |
| 275 scoped_ptr<net::URLRequest> request = |
| 276 CreateRequest(tests[i].network_prohibitively_slow); |
| 277 net::HttpRequestHeaders headers; |
| 278 NotifyBeforeSendProxyHeaders(&headers, request.get()); |
| 279 |
| 280 VerifyLoFiHeader(expect_lofi_header, headers); |
| 281 VerifyLoFiExperimentHeader(expect_lofi_experiment_header, headers); |
| 282 } |
| 283 } |
| 284 |
| 285 TEST_F(ContentLoFiDeciderTest, SlowConnectionsFlag) { |
| 286 const struct { |
| 287 bool slow_connections_flag_enabled; |
| 288 bool network_prohibitively_slow; |
| 289 bool auto_lofi_enabled_group; |
| 290 |
| 291 } tests[] = { |
| 292 { |
| 293 false, false, false, |
| 294 }, |
| 295 { |
| 296 false, true, false, |
| 297 }, |
| 298 { |
| 299 true, false, false, |
| 300 }, |
| 301 { |
| 302 true, true, false, |
| 303 }, |
| 304 { |
| 305 false, false, true, |
| 306 }, |
| 307 { |
| 308 false, true, true, |
| 309 }, |
| 310 { |
| 311 true, false, true, |
| 312 }, |
| 313 { |
| 314 true, true, true, |
| 315 }, |
| 316 }; |
| 317 |
| 318 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 319 test_context_->config()->ResetLoFiStatusForTest(); |
| 320 // For the purpose of this test, Lo-Fi header is expected only if LoFi Slow |
| 321 // Connection Flag is enabled or session is part of Lo-Fi enabled field |
| 322 // trial. For both cases, an additional condition is that network must be |
| 323 // prohibitively slow. |
| 324 bool expect_lofi_header = (tests[i].slow_connections_flag_enabled && |
| 325 tests[i].network_prohibitively_slow) || |
| 326 (!tests[i].slow_connections_flag_enabled && |
| 327 tests[i].auto_lofi_enabled_group && |
| 328 tests[i].network_prohibitively_slow); |
| 329 |
| 330 std::string expected_header; |
| 331 |
| 332 if (tests[i].slow_connections_flag_enabled) { |
| 333 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 334 switches::kDataReductionProxyLoFi, |
| 335 switches::kDataReductionProxyLoFiValueSlowConnectionsOnly); |
| 336 } |
| 337 |
| 338 base::FieldTrialList field_trial_list(nullptr); |
| 339 if (tests[i].auto_lofi_enabled_group) { |
| 340 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), |
| 341 "Enabled"); |
| 342 } |
| 343 |
| 344 test_context_->config()->SetNetworkProhibitivelySlow( |
| 345 tests[i].network_prohibitively_slow); |
| 346 |
| 347 scoped_ptr<net::URLRequest> request = |
| 348 CreateRequest(tests[i].network_prohibitively_slow); |
| 349 net::HttpRequestHeaders headers; |
| 350 NotifyBeforeSendProxyHeaders(&headers, request.get()); |
| 351 |
| 352 VerifyLoFiHeader(expect_lofi_header, headers); |
| 353 } |
| 354 } |
| 355 |
225 } // namespace data_reduction_roxy | 356 } // namespace data_reduction_roxy |
OLD | NEW |