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 <string> | 9 #include <string> |
9 #include <utility> | 10 #include <utility> |
10 | 11 |
11 #include "base/command_line.h" | 12 #include "base/command_line.h" |
12 #include "base/macros.h" | 13 #include "base/macros.h" |
13 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
14 #include "base/metrics/field_trial.h" | 15 #include "base/metrics/field_trial.h" |
15 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
16 #include "build/build_config.h" | 17 #include "build/build_config.h" |
17 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_conf
ig_test_utils.h" | 18 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_conf
ig_test_utils.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 context_.Init(); | 69 context_.Init(); |
69 | 70 |
70 test_context_ = DataReductionProxyTestContext::Builder() | 71 test_context_ = DataReductionProxyTestContext::Builder() |
71 .WithClient(kClient) | 72 .WithClient(kClient) |
72 .WithMockClientSocketFactory(&mock_socket_factory_) | 73 .WithMockClientSocketFactory(&mock_socket_factory_) |
73 .WithURLRequestContext(&context_) | 74 .WithURLRequestContext(&context_) |
74 .Build(); | 75 .Build(); |
75 | 76 |
76 data_reduction_proxy_network_delegate_.reset( | 77 data_reduction_proxy_network_delegate_.reset( |
77 new DataReductionProxyNetworkDelegate( | 78 new DataReductionProxyNetworkDelegate( |
78 scoped_ptr<net::NetworkDelegate>(new net::NetworkDelegateImpl()), | 79 std::unique_ptr<net::NetworkDelegate>( |
| 80 new net::NetworkDelegateImpl()), |
79 test_context_->config(), | 81 test_context_->config(), |
80 test_context_->io_data()->request_options(), | 82 test_context_->io_data()->request_options(), |
81 test_context_->configurator())); | 83 test_context_->configurator())); |
82 | 84 |
83 data_reduction_proxy_network_delegate_->InitIODataAndUMA( | 85 data_reduction_proxy_network_delegate_->InitIODataAndUMA( |
84 test_context_->io_data(), test_context_->io_data()->bypass_stats()); | 86 test_context_->io_data(), test_context_->io_data()->bypass_stats()); |
85 | 87 |
86 context_.set_network_delegate(data_reduction_proxy_network_delegate_.get()); | 88 context_.set_network_delegate(data_reduction_proxy_network_delegate_.get()); |
87 | 89 |
88 scoped_ptr<data_reduction_proxy::ContentLoFiDecider> | 90 std::unique_ptr<data_reduction_proxy::ContentLoFiDecider> |
89 data_reduction_proxy_lofi_decider( | 91 data_reduction_proxy_lofi_decider( |
90 new data_reduction_proxy::ContentLoFiDecider()); | 92 new data_reduction_proxy::ContentLoFiDecider()); |
91 test_context_->io_data()->set_lofi_decider( | 93 test_context_->io_data()->set_lofi_decider( |
92 std::move(data_reduction_proxy_lofi_decider)); | 94 std::move(data_reduction_proxy_lofi_decider)); |
93 } | 95 } |
94 | 96 |
95 scoped_ptr<net::URLRequest> CreateRequest(bool is_using_lofi) { | 97 std::unique_ptr<net::URLRequest> CreateRequest(bool is_using_lofi) { |
96 scoped_ptr<net::URLRequest> request = context_.CreateRequest( | 98 std::unique_ptr<net::URLRequest> request = context_.CreateRequest( |
97 GURL("http://www.google.com/"), net::IDLE, &delegate_); | 99 GURL("http://www.google.com/"), net::IDLE, &delegate_); |
98 | 100 |
99 content::ResourceRequestInfo::AllocateForTesting( | 101 content::ResourceRequestInfo::AllocateForTesting( |
100 request.get(), content::RESOURCE_TYPE_SUB_FRAME, NULL, -1, -1, -1, | 102 request.get(), content::RESOURCE_TYPE_SUB_FRAME, NULL, -1, -1, -1, |
101 false, // is_main_frame | 103 false, // is_main_frame |
102 false, // parent_is_main_frame | 104 false, // parent_is_main_frame |
103 false, // allow_download | 105 false, // allow_download |
104 false, // is_async | 106 false, // is_async |
105 is_using_lofi); | 107 is_using_lofi); |
106 | 108 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 header_value.find( | 157 header_value.find( |
156 chrome_proxy_lo_fi_ignore_preview_blacklist_directive()) != | 158 chrome_proxy_lo_fi_ignore_preview_blacklist_directive()) != |
157 std::string::npos); | 159 std::string::npos); |
158 } | 160 } |
159 | 161 |
160 protected: | 162 protected: |
161 base::MessageLoopForIO message_loop_; | 163 base::MessageLoopForIO message_loop_; |
162 net::MockClientSocketFactory mock_socket_factory_; | 164 net::MockClientSocketFactory mock_socket_factory_; |
163 net::TestURLRequestContext context_; | 165 net::TestURLRequestContext context_; |
164 net::TestDelegate delegate_; | 166 net::TestDelegate delegate_; |
165 scoped_ptr<DataReductionProxyTestContext> test_context_; | 167 std::unique_ptr<DataReductionProxyTestContext> test_context_; |
166 scoped_ptr<DataReductionProxyNetworkDelegate> | 168 std::unique_ptr<DataReductionProxyNetworkDelegate> |
167 data_reduction_proxy_network_delegate_; | 169 data_reduction_proxy_network_delegate_; |
168 }; | 170 }; |
169 | 171 |
170 TEST_F(ContentLoFiDeciderTest, LoFiFlags) { | 172 TEST_F(ContentLoFiDeciderTest, LoFiFlags) { |
171 // Enable Lo-Fi. | 173 // Enable Lo-Fi. |
172 const struct { | 174 const struct { |
173 bool is_using_lofi; | 175 bool is_using_lofi; |
174 bool is_using_previews; | 176 bool is_using_previews; |
175 } tests[] = { | 177 } tests[] = { |
176 {false, false}, {true, false}, {false, true}, {true, true}, | 178 {false, false}, {true, false}, {false, true}, {true, true}, |
177 }; | 179 }; |
178 | 180 |
179 for (size_t i = 0; i < arraysize(tests); ++i) { | 181 for (size_t i = 0; i < arraysize(tests); ++i) { |
180 scoped_ptr<net::URLRequest> request = CreateRequest(tests[i].is_using_lofi); | 182 std::unique_ptr<net::URLRequest> request = |
| 183 CreateRequest(tests[i].is_using_lofi); |
181 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 184 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
182 command_line->InitFromArgv(command_line->argv()); | 185 command_line->InitFromArgv(command_line->argv()); |
183 if (tests[i].is_using_previews) { | 186 if (tests[i].is_using_previews) { |
184 command_line->AppendSwitch( | 187 command_line->AppendSwitch( |
185 switches::kEnableDataReductionProxyLoFiPreview); | 188 switches::kEnableDataReductionProxyLoFiPreview); |
186 } | 189 } |
187 | 190 |
188 // No flags or field trials. The Lo-Fi header should not be added. | 191 // No flags or field trials. The Lo-Fi header should not be added. |
189 net::HttpRequestHeaders headers; | 192 net::HttpRequestHeaders headers; |
190 NotifyBeforeSendProxyHeaders(&headers, request.get(), true); | 193 NotifyBeforeSendProxyHeaders(&headers, request.get(), true); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), | 251 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), |
249 "Enabled"); | 252 "Enabled"); |
250 // Enable Lo-Fi. | 253 // Enable Lo-Fi. |
251 const struct { | 254 const struct { |
252 bool is_using_lofi; | 255 bool is_using_lofi; |
253 } tests[] = { | 256 } tests[] = { |
254 {false}, {true}, | 257 {false}, {true}, |
255 }; | 258 }; |
256 | 259 |
257 for (size_t i = 0; i < arraysize(tests); ++i) { | 260 for (size_t i = 0; i < arraysize(tests); ++i) { |
258 scoped_ptr<net::URLRequest> request = CreateRequest(tests[i].is_using_lofi); | 261 std::unique_ptr<net::URLRequest> request = |
| 262 CreateRequest(tests[i].is_using_lofi); |
259 net::HttpRequestHeaders headers; | 263 net::HttpRequestHeaders headers; |
260 NotifyBeforeSendProxyHeaders(&headers, request.get(), true); | 264 NotifyBeforeSendProxyHeaders(&headers, request.get(), true); |
261 VerifyLoFiHeader(tests[i].is_using_lofi, headers); | 265 VerifyLoFiHeader(tests[i].is_using_lofi, headers); |
262 VerifyLoFiPreviewHeader(false, headers); | 266 VerifyLoFiPreviewHeader(false, headers); |
263 VerifyLoFiIgnorePreviewBlacklistHeader(false, headers); | 267 VerifyLoFiIgnorePreviewBlacklistHeader(false, headers); |
264 } | 268 } |
265 } | 269 } |
266 | 270 |
267 TEST_F(ContentLoFiDeciderTest, LoFiControlFieldTrial) { | 271 TEST_F(ContentLoFiDeciderTest, LoFiControlFieldTrial) { |
268 base::FieldTrialList field_trial_list(nullptr); | 272 base::FieldTrialList field_trial_list(nullptr); |
269 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), | 273 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), |
270 "Control"); | 274 "Control"); |
271 // Enable Lo-Fi. | 275 // Enable Lo-Fi. |
272 const struct { | 276 const struct { |
273 bool is_using_lofi; | 277 bool is_using_lofi; |
274 } tests[] = { | 278 } tests[] = { |
275 {false}, {true}, | 279 {false}, {true}, |
276 }; | 280 }; |
277 | 281 |
278 for (size_t i = 0; i < arraysize(tests); ++i) { | 282 for (size_t i = 0; i < arraysize(tests); ++i) { |
279 scoped_ptr<net::URLRequest> request = CreateRequest(tests[i].is_using_lofi); | 283 std::unique_ptr<net::URLRequest> request = |
| 284 CreateRequest(tests[i].is_using_lofi); |
280 net::HttpRequestHeaders headers; | 285 net::HttpRequestHeaders headers; |
281 NotifyBeforeSendProxyHeaders(&headers, request.get(), true); | 286 NotifyBeforeSendProxyHeaders(&headers, request.get(), true); |
282 VerifyLoFiHeader(false, headers); | 287 VerifyLoFiHeader(false, headers); |
283 VerifyLoFiPreviewHeader(false, headers); | 288 VerifyLoFiPreviewHeader(false, headers); |
284 VerifyLoFiIgnorePreviewBlacklistHeader(false, headers); | 289 VerifyLoFiIgnorePreviewBlacklistHeader(false, headers); |
285 } | 290 } |
286 } | 291 } |
287 | 292 |
288 TEST_F(ContentLoFiDeciderTest, LoFiPreviewFieldTrial) { | 293 TEST_F(ContentLoFiDeciderTest, LoFiPreviewFieldTrial) { |
289 base::FieldTrialList field_trial_list(nullptr); | 294 base::FieldTrialList field_trial_list(nullptr); |
290 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), | 295 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), |
291 "Enabled_Preview"); | 296 "Enabled_Preview"); |
292 // Enable Lo-Fi. | 297 // Enable Lo-Fi. |
293 const struct { | 298 const struct { |
294 bool is_using_lofi; | 299 bool is_using_lofi; |
295 bool is_main_frame; | 300 bool is_main_frame; |
296 } tests[] = { | 301 } tests[] = { |
297 {false, false}, {true, false}, {false, true}, {true, true}, | 302 {false, false}, {true, false}, {false, true}, {true, true}, |
298 }; | 303 }; |
299 | 304 |
300 for (size_t i = 0; i < arraysize(tests); ++i) { | 305 for (size_t i = 0; i < arraysize(tests); ++i) { |
301 scoped_ptr<net::URLRequest> request = CreateRequest(tests[i].is_using_lofi); | 306 std::unique_ptr<net::URLRequest> request = |
| 307 CreateRequest(tests[i].is_using_lofi); |
302 if (tests[i].is_main_frame) | 308 if (tests[i].is_main_frame) |
303 request->SetLoadFlags(request->load_flags() | net::LOAD_MAIN_FRAME); | 309 request->SetLoadFlags(request->load_flags() | net::LOAD_MAIN_FRAME); |
304 net::HttpRequestHeaders headers; | 310 net::HttpRequestHeaders headers; |
305 NotifyBeforeSendProxyHeaders(&headers, request.get(), true); | 311 NotifyBeforeSendProxyHeaders(&headers, request.get(), true); |
306 VerifyLoFiHeader(false, headers); | 312 VerifyLoFiHeader(false, headers); |
307 VerifyLoFiPreviewHeader(tests[i].is_using_lofi && tests[i].is_main_frame, | 313 VerifyLoFiPreviewHeader(tests[i].is_using_lofi && tests[i].is_main_frame, |
308 headers); | 314 headers); |
309 VerifyLoFiIgnorePreviewBlacklistHeader(false, headers); | 315 VerifyLoFiIgnorePreviewBlacklistHeader(false, headers); |
310 } | 316 } |
311 } | 317 } |
(...skipping 29 matching lines...) Expand all Loading... |
341 } | 347 } |
342 | 348 |
343 if (tests[i].auto_lofi_control_group) { | 349 if (tests[i].auto_lofi_control_group) { |
344 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), | 350 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), |
345 "Control"); | 351 "Control"); |
346 } | 352 } |
347 | 353 |
348 test_context_->config()->SetNetworkProhibitivelySlow( | 354 test_context_->config()->SetNetworkProhibitivelySlow( |
349 tests[i].network_prohibitively_slow); | 355 tests[i].network_prohibitively_slow); |
350 | 356 |
351 scoped_ptr<net::URLRequest> request = | 357 std::unique_ptr<net::URLRequest> request = |
352 CreateRequest(tests[i].network_prohibitively_slow); | 358 CreateRequest(tests[i].network_prohibitively_slow); |
353 net::HttpRequestHeaders headers; | 359 net::HttpRequestHeaders headers; |
354 NotifyBeforeSendProxyHeaders(&headers, request.get(), true); | 360 NotifyBeforeSendProxyHeaders(&headers, request.get(), true); |
355 | 361 |
356 VerifyLoFiHeader(expect_lofi_header, headers); | 362 VerifyLoFiHeader(expect_lofi_header, headers); |
357 } | 363 } |
358 } | 364 } |
359 | 365 |
360 TEST_F(ContentLoFiDeciderTest, SlowConnectionsFlag) { | 366 TEST_F(ContentLoFiDeciderTest, SlowConnectionsFlag) { |
361 const struct { | 367 const struct { |
(...skipping 29 matching lines...) Expand all Loading... |
391 | 397 |
392 base::FieldTrialList field_trial_list(nullptr); | 398 base::FieldTrialList field_trial_list(nullptr); |
393 if (tests[i].auto_lofi_enabled_group) { | 399 if (tests[i].auto_lofi_enabled_group) { |
394 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), | 400 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), |
395 "Enabled"); | 401 "Enabled"); |
396 } | 402 } |
397 | 403 |
398 test_context_->config()->SetNetworkProhibitivelySlow( | 404 test_context_->config()->SetNetworkProhibitivelySlow( |
399 tests[i].network_prohibitively_slow); | 405 tests[i].network_prohibitively_slow); |
400 | 406 |
401 scoped_ptr<net::URLRequest> request = | 407 std::unique_ptr<net::URLRequest> request = |
402 CreateRequest(tests[i].network_prohibitively_slow); | 408 CreateRequest(tests[i].network_prohibitively_slow); |
403 net::HttpRequestHeaders headers; | 409 net::HttpRequestHeaders headers; |
404 NotifyBeforeSendProxyHeaders(&headers, request.get(), true); | 410 NotifyBeforeSendProxyHeaders(&headers, request.get(), true); |
405 | 411 |
406 VerifyLoFiHeader(expect_lofi_header, headers); | 412 VerifyLoFiHeader(expect_lofi_header, headers); |
407 } | 413 } |
408 } | 414 } |
409 | 415 |
410 TEST_F(ContentLoFiDeciderTest, ProxyIsNotDataReductionProxy) { | 416 TEST_F(ContentLoFiDeciderTest, ProxyIsNotDataReductionProxy) { |
411 base::FieldTrialList field_trial_list(nullptr); | 417 base::FieldTrialList field_trial_list(nullptr); |
412 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), | 418 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), |
413 "Enabled"); | 419 "Enabled"); |
414 // Enable Lo-Fi. | 420 // Enable Lo-Fi. |
415 const struct { | 421 const struct { |
416 bool is_using_lofi; | 422 bool is_using_lofi; |
417 } tests[] = { | 423 } tests[] = { |
418 {false}, {true}, | 424 {false}, {true}, |
419 }; | 425 }; |
420 | 426 |
421 for (size_t i = 0; i < arraysize(tests); ++i) { | 427 for (size_t i = 0; i < arraysize(tests); ++i) { |
422 scoped_ptr<net::URLRequest> request = CreateRequest(tests[i].is_using_lofi); | 428 std::unique_ptr<net::URLRequest> request = |
| 429 CreateRequest(tests[i].is_using_lofi); |
423 net::HttpRequestHeaders headers; | 430 net::HttpRequestHeaders headers; |
424 NotifyBeforeSendProxyHeaders(&headers, request.get(), false); | 431 NotifyBeforeSendProxyHeaders(&headers, request.get(), false); |
425 std::string header_value; | 432 std::string header_value; |
426 headers.GetHeader(chrome_proxy_header(), &header_value); | 433 headers.GetHeader(chrome_proxy_header(), &header_value); |
427 EXPECT_EQ(std::string::npos, | 434 EXPECT_EQ(std::string::npos, |
428 header_value.find(chrome_proxy_lo_fi_directive())); | 435 header_value.find(chrome_proxy_lo_fi_directive())); |
429 } | 436 } |
430 } | 437 } |
431 | 438 |
432 } // namespace data_reduction_roxy | 439 } // namespace data_reduction_roxy |
OLD | NEW |