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 {false, false, false}, {false, true, false}, {true, false, false}, | |
294 {true, true, false}, {false, false, true}, {false, true, true}, | |
295 {true, false, true}, {true, true, true}, | |
megjablon
2015/11/23 20:53:55
git cl format did this
| |
296 }; | |
297 | |
298 for (size_t i = 0; i < arraysize(tests); ++i) { | |
299 test_context_->config()->ResetLoFiStatusForTest(); | |
300 // For the purpose of this test, Lo-Fi header is expected only if LoFi Slow | |
301 // Connection Flag is enabled or session is part of Lo-Fi enabled field | |
302 // trial. For both cases, an additional condition is that network must be | |
303 // prohibitively slow. | |
304 bool expect_lofi_header = (tests[i].slow_connections_flag_enabled && | |
305 tests[i].network_prohibitively_slow) || | |
306 (!tests[i].slow_connections_flag_enabled && | |
307 tests[i].auto_lofi_enabled_group && | |
308 tests[i].network_prohibitively_slow); | |
309 | |
310 std::string expected_header; | |
311 | |
312 if (tests[i].slow_connections_flag_enabled) { | |
313 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
314 switches::kDataReductionProxyLoFi, | |
315 switches::kDataReductionProxyLoFiValueSlowConnectionsOnly); | |
316 } | |
317 | |
318 base::FieldTrialList field_trial_list(nullptr); | |
319 if (tests[i].auto_lofi_enabled_group) { | |
320 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), | |
321 "Enabled"); | |
322 } | |
323 | |
324 test_context_->config()->SetNetworkProhibitivelySlow( | |
325 tests[i].network_prohibitively_slow); | |
326 | |
327 scoped_ptr<net::URLRequest> request = | |
328 CreateRequest(tests[i].network_prohibitively_slow); | |
329 net::HttpRequestHeaders headers; | |
330 NotifyBeforeSendProxyHeaders(&headers, request.get()); | |
331 | |
332 VerifyLoFiHeader(expect_lofi_header, headers); | |
333 } | |
334 } | |
335 | |
225 } // namespace data_reduction_roxy | 336 } // namespace data_reduction_roxy |
OLD | NEW |