| 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_heade
rs.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.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_param
s_test_utils.h" |
| 19 #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" |
| 20 #include "content/public/browser/resource_request_info.h" | 20 #include "content/public/browser/resource_request_info.h" |
| 21 #include "net/base/load_flags.h" |
| 21 #include "net/base/network_delegate_impl.h" | 22 #include "net/base/network_delegate_impl.h" |
| 22 #include "net/http/http_request_headers.h" | 23 #include "net/http/http_request_headers.h" |
| 23 #include "net/proxy/proxy_info.h" | 24 #include "net/proxy/proxy_info.h" |
| 24 #include "net/socket/socket_test_util.h" | 25 #include "net/socket/socket_test_util.h" |
| 25 #include "net/url_request/url_request.h" | 26 #include "net/url_request/url_request.h" |
| 26 #include "net/url_request/url_request_test_util.h" | 27 #include "net/url_request/url_request_test_util.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 28 | 29 |
| 29 namespace data_reduction_proxy { | 30 namespace data_reduction_proxy { |
| 30 | 31 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 static void VerifyLoFiHeader(bool expected_lofi_used, | 120 static void VerifyLoFiHeader(bool expected_lofi_used, |
| 120 const net::HttpRequestHeaders& headers) { | 121 const net::HttpRequestHeaders& headers) { |
| 121 EXPECT_TRUE(headers.HasHeader(chrome_proxy_header())); | 122 EXPECT_TRUE(headers.HasHeader(chrome_proxy_header())); |
| 122 std::string header_value; | 123 std::string header_value; |
| 123 headers.GetHeader(chrome_proxy_header(), &header_value); | 124 headers.GetHeader(chrome_proxy_header(), &header_value); |
| 124 EXPECT_EQ( | 125 EXPECT_EQ( |
| 125 expected_lofi_used, | 126 expected_lofi_used, |
| 126 header_value.find(chrome_proxy_lo_fi_directive()) != std::string::npos); | 127 header_value.find(chrome_proxy_lo_fi_directive()) != std::string::npos); |
| 127 } | 128 } |
| 128 | 129 |
| 130 static void VerifyLoFiPreviewHeader(bool expected_lofi_preview_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_preview_used, |
| 136 header_value.find(chrome_proxy_lo_fi_preview_directive()) != |
| 137 std::string::npos); |
| 138 } |
| 139 |
| 129 static void VerifyLoFiExperimentHeader( | 140 static void VerifyLoFiExperimentHeader( |
| 130 bool expected_lofi_experiment_used, | 141 bool expected_lofi_experiment_used, |
| 131 const net::HttpRequestHeaders& headers) { | 142 const net::HttpRequestHeaders& headers) { |
| 132 EXPECT_TRUE(headers.HasHeader(chrome_proxy_header())); | 143 EXPECT_TRUE(headers.HasHeader(chrome_proxy_header())); |
| 133 std::string header_value; | 144 std::string header_value; |
| 134 headers.GetHeader(chrome_proxy_header(), &header_value); | 145 headers.GetHeader(chrome_proxy_header(), &header_value); |
| 135 EXPECT_EQ(expected_lofi_experiment_used, | 146 EXPECT_EQ(expected_lofi_experiment_used, |
| 136 header_value.find(chrome_proxy_lo_fi_experiment_directive()) != | 147 header_value.find(chrome_proxy_lo_fi_experiment_directive()) != |
| 137 std::string::npos); | 148 std::string::npos); |
| 138 } | 149 } |
| 139 | 150 |
| 140 protected: | 151 protected: |
| 141 base::MessageLoopForIO message_loop_; | 152 base::MessageLoopForIO message_loop_; |
| 142 net::MockClientSocketFactory mock_socket_factory_; | 153 net::MockClientSocketFactory mock_socket_factory_; |
| 143 net::TestURLRequestContext context_; | 154 net::TestURLRequestContext context_; |
| 144 net::TestDelegate delegate_; | 155 net::TestDelegate delegate_; |
| 145 scoped_ptr<DataReductionProxyTestContext> test_context_; | 156 scoped_ptr<DataReductionProxyTestContext> test_context_; |
| 146 scoped_ptr<DataReductionProxyNetworkDelegate> | 157 scoped_ptr<DataReductionProxyNetworkDelegate> |
| 147 data_reduction_proxy_network_delegate_; | 158 data_reduction_proxy_network_delegate_; |
| 148 }; | 159 }; |
| 149 | 160 |
| 150 TEST_F(ContentLoFiDeciderTest, LoFiFlags) { | 161 TEST_F(ContentLoFiDeciderTest, LoFiFlags) { |
| 151 // Enable Lo-Fi. | 162 // Enable Lo-Fi. |
| 152 const struct { | 163 const struct { |
| 153 bool is_using_lofi; | 164 bool is_using_lofi; |
| 165 bool is_using_previews; |
| 154 } tests[] = { | 166 } tests[] = { |
| 155 {false}, {true}, | 167 {false, false}, {true, false}, {false, true}, {true, true}, |
| 156 }; | 168 }; |
| 157 | 169 |
| 158 for (size_t i = 0; i < arraysize(tests); ++i) { | 170 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 159 scoped_ptr<net::URLRequest> request = CreateRequest(tests[i].is_using_lofi); | 171 scoped_ptr<net::URLRequest> request = CreateRequest(tests[i].is_using_lofi); |
| 160 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 172 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 161 command_line->InitFromArgv(command_line->argv()); | 173 command_line->InitFromArgv(command_line->argv()); |
| 174 if (tests[i].is_using_previews) { |
| 175 command_line->AppendSwitch( |
| 176 switches::kEnableDataReductionProxyLoFiPreview); |
| 177 } |
| 162 | 178 |
| 163 // No flags or field trials. The Lo-Fi header should not be added. | 179 // No flags or field trials. The Lo-Fi header should not be added. |
| 164 net::HttpRequestHeaders headers; | 180 net::HttpRequestHeaders headers; |
| 165 NotifyBeforeSendProxyHeaders(&headers, request.get()); | 181 NotifyBeforeSendProxyHeaders(&headers, request.get()); |
| 166 VerifyLoFiHeader(false, headers); | 182 VerifyLoFiHeader(false, headers); |
| 183 VerifyLoFiPreviewHeader(false, headers); |
| 167 | 184 |
| 168 // The Lo-Fi flag is "always-on" and Lo-Fi is being used. Lo-Fi header | 185 // The Lo-Fi flag is "always-on", Lo-Fi is being used, and it's a main frame |
| 169 // should | 186 // request. Lo-Fi preview header should be added. |
| 170 // be added. | |
| 171 command_line->AppendSwitchASCII( | 187 command_line->AppendSwitchASCII( |
| 172 switches::kDataReductionProxyLoFi, | 188 switches::kDataReductionProxyLoFi, |
| 173 switches::kDataReductionProxyLoFiValueAlwaysOn); | 189 switches::kDataReductionProxyLoFiValueAlwaysOn); |
| 190 request->SetLoadFlags(request->load_flags() | net::LOAD_MAIN_FRAME); |
| 191 headers.Clear(); |
| 192 NotifyBeforeSendProxyHeaders(&headers, request.get()); |
| 193 VerifyLoFiHeader(tests[i].is_using_lofi && !tests[i].is_using_previews, |
| 194 headers); |
| 195 VerifyLoFiPreviewHeader( |
| 196 tests[i].is_using_lofi && tests[i].is_using_previews, headers); |
| 197 |
| 198 // The Lo-Fi flag is "always-on" and Lo-Fi is being used. Lo-Fi header |
| 199 // should be added. |
| 200 request->SetLoadFlags(0); |
| 174 headers.Clear(); | 201 headers.Clear(); |
| 175 NotifyBeforeSendProxyHeaders(&headers, request.get()); | 202 NotifyBeforeSendProxyHeaders(&headers, request.get()); |
| 176 VerifyLoFiHeader(tests[i].is_using_lofi, headers); | 203 VerifyLoFiHeader(tests[i].is_using_lofi, headers); |
| 204 VerifyLoFiPreviewHeader(false, headers); |
| 177 | 205 |
| 178 // The Lo-Fi flag is "cellular-only" and Lo-Fi is being used. Lo-Fi header | 206 // The Lo-Fi flag is "cellular-only" and Lo-Fi is being used. Lo-Fi header |
| 179 // should be added. | 207 // should be added. |
| 180 command_line->AppendSwitchASCII( | 208 command_line->AppendSwitchASCII( |
| 181 switches::kDataReductionProxyLoFi, | 209 switches::kDataReductionProxyLoFi, |
| 182 switches::kDataReductionProxyLoFiValueCellularOnly); | 210 switches::kDataReductionProxyLoFiValueCellularOnly); |
| 183 headers.Clear(); | 211 headers.Clear(); |
| 184 NotifyBeforeSendProxyHeaders(&headers, request.get()); | 212 NotifyBeforeSendProxyHeaders(&headers, request.get()); |
| 185 VerifyLoFiHeader(tests[i].is_using_lofi, headers); | 213 VerifyLoFiHeader(tests[i].is_using_lofi, headers); |
| 214 VerifyLoFiPreviewHeader(false, headers); |
| 186 | 215 |
| 187 // The Lo-Fi flag is "slow-connections-only" and Lo-Fi is being used. Lo-Fi | 216 // The Lo-Fi flag is "slow-connections-only" and Lo-Fi is being used. Lo-Fi |
| 188 // header should be added. | 217 // header should be added. |
| 189 command_line->AppendSwitchASCII( | 218 command_line->AppendSwitchASCII( |
| 190 switches::kDataReductionProxyLoFi, | 219 switches::kDataReductionProxyLoFi, |
| 191 switches::kDataReductionProxyLoFiValueSlowConnectionsOnly); | 220 switches::kDataReductionProxyLoFiValueSlowConnectionsOnly); |
| 192 headers.Clear(); | 221 headers.Clear(); |
| 193 NotifyBeforeSendProxyHeaders(&headers, request.get()); | 222 NotifyBeforeSendProxyHeaders(&headers, request.get()); |
| 194 VerifyLoFiHeader(tests[i].is_using_lofi, headers); | 223 VerifyLoFiHeader(tests[i].is_using_lofi, headers); |
| 224 VerifyLoFiPreviewHeader(false, headers); |
| 195 } | 225 } |
| 196 } | 226 } |
| 197 | 227 |
| 198 TEST_F(ContentLoFiDeciderTest, LoFiEnabledFieldTrial) { | 228 TEST_F(ContentLoFiDeciderTest, LoFiEnabledFieldTrial) { |
| 199 base::FieldTrialList field_trial_list(nullptr); | 229 base::FieldTrialList field_trial_list(nullptr); |
| 200 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), | 230 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), |
| 201 "Enabled"); | 231 "Enabled"); |
| 202 // Enable Lo-Fi. | 232 // Enable Lo-Fi. |
| 203 const struct { | 233 const struct { |
| 204 bool is_using_lofi; | 234 bool is_using_lofi; |
| 205 } tests[] = { | 235 } tests[] = { |
| 206 {false}, {true}, | 236 {false}, {true}, |
| 207 }; | 237 }; |
| 208 | 238 |
| 209 for (size_t i = 0; i < arraysize(tests); ++i) { | 239 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 210 scoped_ptr<net::URLRequest> request = CreateRequest(tests[i].is_using_lofi); | 240 scoped_ptr<net::URLRequest> request = CreateRequest(tests[i].is_using_lofi); |
| 211 net::HttpRequestHeaders headers; | 241 net::HttpRequestHeaders headers; |
| 212 NotifyBeforeSendProxyHeaders(&headers, request.get()); | 242 NotifyBeforeSendProxyHeaders(&headers, request.get()); |
| 213 VerifyLoFiHeader(tests[i].is_using_lofi, headers); | 243 VerifyLoFiHeader(tests[i].is_using_lofi, headers); |
| 244 VerifyLoFiPreviewHeader(false, headers); |
| 214 } | 245 } |
| 215 } | 246 } |
| 216 | 247 |
| 217 TEST_F(ContentLoFiDeciderTest, LoFiControlFieldTrial) { | 248 TEST_F(ContentLoFiDeciderTest, LoFiControlFieldTrial) { |
| 218 base::FieldTrialList field_trial_list(nullptr); | 249 base::FieldTrialList field_trial_list(nullptr); |
| 219 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), | 250 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), |
| 220 "Control"); | 251 "Control"); |
| 221 // Enable Lo-Fi. | 252 // Enable Lo-Fi. |
| 222 const struct { | 253 const struct { |
| 223 bool is_using_lofi; | 254 bool is_using_lofi; |
| 224 } tests[] = { | 255 } tests[] = { |
| 225 {false}, {true}, | 256 {false}, {true}, |
| 226 }; | 257 }; |
| 227 | 258 |
| 228 for (size_t i = 0; i < arraysize(tests); ++i) { | 259 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 229 scoped_ptr<net::URLRequest> request = CreateRequest(tests[i].is_using_lofi); | 260 scoped_ptr<net::URLRequest> request = CreateRequest(tests[i].is_using_lofi); |
| 230 net::HttpRequestHeaders headers; | 261 net::HttpRequestHeaders headers; |
| 231 NotifyBeforeSendProxyHeaders(&headers, request.get()); | 262 NotifyBeforeSendProxyHeaders(&headers, request.get()); |
| 232 VerifyLoFiHeader(false, headers); | 263 VerifyLoFiHeader(false, headers); |
| 264 VerifyLoFiPreviewHeader(false, headers); |
| 233 } | 265 } |
| 234 } | 266 } |
| 235 | 267 |
| 268 TEST_F(ContentLoFiDeciderTest, LoFiPreviewFieldTrial) { |
| 269 base::FieldTrialList field_trial_list(nullptr); |
| 270 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), |
| 271 "Enabled_Preview"); |
| 272 // Enable Lo-Fi. |
| 273 const struct { |
| 274 bool is_using_lofi; |
| 275 bool is_main_frame; |
| 276 } tests[] = { |
| 277 {false, false}, {true, false}, {false, true}, {true, true}, |
| 278 }; |
| 279 |
| 280 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 281 scoped_ptr<net::URLRequest> request = CreateRequest(tests[i].is_using_lofi); |
| 282 if (tests[i].is_main_frame) |
| 283 request->SetLoadFlags(request->load_flags() | net::LOAD_MAIN_FRAME); |
| 284 net::HttpRequestHeaders headers; |
| 285 NotifyBeforeSendProxyHeaders(&headers, request.get()); |
| 286 VerifyLoFiHeader(tests[i].is_using_lofi && !tests[i].is_main_frame, |
| 287 headers); |
| 288 VerifyLoFiPreviewHeader(tests[i].is_using_lofi && tests[i].is_main_frame, |
| 289 headers); |
| 290 } |
| 291 } |
| 292 |
| 236 TEST_F(ContentLoFiDeciderTest, AutoLoFi) { | 293 TEST_F(ContentLoFiDeciderTest, AutoLoFi) { |
| 237 const struct { | 294 const struct { |
| 238 bool auto_lofi_enabled_group; | 295 bool auto_lofi_enabled_group; |
| 239 bool auto_lofi_control_group; | 296 bool auto_lofi_control_group; |
| 240 bool network_prohibitively_slow; | 297 bool network_prohibitively_slow; |
| 241 } tests[] = { | 298 } tests[] = { |
| 242 {false, false, false}, | 299 {false, false, false}, |
| 243 {false, false, true}, | 300 {false, false, true}, |
| 244 {true, false, false}, | 301 {true, false, false}, |
| 245 {true, false, true}, | 302 {true, false, true}, |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 scoped_ptr<net::URLRequest> request = | 384 scoped_ptr<net::URLRequest> request = |
| 328 CreateRequest(tests[i].network_prohibitively_slow); | 385 CreateRequest(tests[i].network_prohibitively_slow); |
| 329 net::HttpRequestHeaders headers; | 386 net::HttpRequestHeaders headers; |
| 330 NotifyBeforeSendProxyHeaders(&headers, request.get()); | 387 NotifyBeforeSendProxyHeaders(&headers, request.get()); |
| 331 | 388 |
| 332 VerifyLoFiHeader(expect_lofi_header, headers); | 389 VerifyLoFiHeader(expect_lofi_header, headers); |
| 333 } | 390 } |
| 334 } | 391 } |
| 335 | 392 |
| 336 } // namespace data_reduction_roxy | 393 } // namespace data_reduction_roxy |
| OLD | NEW |