Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(211)

Side by Side Diff: components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate_unittest.cc

Issue 1933653004: Reset the Lo-Fi main frame state when there is a new main frame request (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comments Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/core/browser/data_reduction_proxy_netw ork_delegate.h" 5 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_netw ork_delegate.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <string> 10 #include <string>
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 #include "net/url_request/url_request.h" 44 #include "net/url_request/url_request.h"
45 #include "net/url_request/url_request_test_util.h" 45 #include "net/url_request/url_request_test_util.h"
46 #include "testing/gtest/include/gtest/gtest.h" 46 #include "testing/gtest/include/gtest/gtest.h"
47 47
48 namespace data_reduction_proxy { 48 namespace data_reduction_proxy {
49 namespace { 49 namespace {
50 50
51 using TestNetworkDelegate = net::NetworkDelegateImpl; 51 using TestNetworkDelegate = net::NetworkDelegateImpl;
52 52
53 const char kChromeProxyHeader[] = "chrome-proxy"; 53 const char kChromeProxyHeader[] = "chrome-proxy";
54 const char kOtherProxy[] = "testproxy:17";
54 55
55 #if defined(OS_ANDROID) 56 #if defined(OS_ANDROID)
56 const Client kClient = Client::CHROME_ANDROID; 57 const Client kClient = Client::CHROME_ANDROID;
57 #elif defined(OS_IOS) 58 #elif defined(OS_IOS)
58 const Client kClient = Client::CHROME_IOS; 59 const Client kClient = Client::CHROME_IOS;
59 #elif defined(OS_MACOSX) 60 #elif defined(OS_MACOSX)
60 const Client kClient = Client::CHROME_MAC; 61 const Client kClient = Client::CHROME_MAC;
61 #elif defined(OS_CHROMEOS) 62 #elif defined(OS_CHROMEOS)
62 const Client kClient = Client::CHROME_CHROMEOS; 63 const Client kClient = Client::CHROME_CHROMEOS;
63 #elif defined(OS_LINUX) 64 #elif defined(OS_LINUX)
(...skipping 20 matching lines...) Expand all
84 bool IsUsingLoFiMode(const net::URLRequest& request) const override { 85 bool IsUsingLoFiMode(const net::URLRequest& request) const override {
85 return should_request_lofi_resource_; 86 return should_request_lofi_resource_;
86 } 87 }
87 88
88 void SetIsUsingLoFiMode(bool should_request_lofi_resource) { 89 void SetIsUsingLoFiMode(bool should_request_lofi_resource) {
89 should_request_lofi_resource_ = should_request_lofi_resource; 90 should_request_lofi_resource_ = should_request_lofi_resource;
90 } 91 }
91 92
92 bool MaybeAddLoFiDirectiveToHeaders( 93 bool MaybeAddLoFiDirectiveToHeaders(
93 const net::URLRequest& request, 94 const net::URLRequest& request,
94 net::HttpRequestHeaders* headers, 95 net::HttpRequestHeaders* headers) const override {
95 const net::ProxyServer& proxy_server,
96 DataReductionProxyConfig* config) const override {
97 if (should_request_lofi_resource_) { 96 if (should_request_lofi_resource_) {
98 const char kChromeProxyHeader[] = "Chrome-Proxy"; 97 const char kChromeProxyHeader[] = "Chrome-Proxy";
99 std::string header_value; 98 std::string header_value;
100 99
101 if (headers->HasHeader(kChromeProxyHeader)) { 100 if (headers->HasHeader(kChromeProxyHeader)) {
102 headers->GetHeader(kChromeProxyHeader, &header_value); 101 headers->GetHeader(kChromeProxyHeader, &header_value);
103 headers->RemoveHeader(kChromeProxyHeader); 102 headers->RemoveHeader(kChromeProxyHeader);
104 header_value += ", "; 103 header_value += ", ";
105 } 104 }
106 105
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 153
155 std::unique_ptr<TestLoFiUIService> lofi_ui_service(new TestLoFiUIService()); 154 std::unique_ptr<TestLoFiUIService> lofi_ui_service(new TestLoFiUIService());
156 lofi_ui_service_ = lofi_ui_service.get(); 155 lofi_ui_service_ = lofi_ui_service.get();
157 test_context_->io_data()->set_lofi_ui_service(std::move(lofi_ui_service)); 156 test_context_->io_data()->set_lofi_ui_service(std::move(lofi_ui_service));
158 157
159 context_.Init(); 158 context_.Init();
160 159
161 test_context_->EnableDataReductionProxyWithSecureProxyCheckSuccess(); 160 test_context_->EnableDataReductionProxyWithSecureProxyCheckSuccess();
162 } 161 }
163 162
164 static void VerifyLoFiHeader(bool expected_lofi_used, 163 static void VerifyLoFiHeader(bool expected_data_reduction_proxy_used,
tbansal1 2016/05/06 23:36:51 s/verifyLoFiHeader/VerifyHeaders/
megjablon 2016/05/07 00:29:08 Done.
164 bool expected_lofi_used,
165 const net::HttpRequestHeaders& headers) { 165 const net::HttpRequestHeaders& headers) {
166 EXPECT_TRUE(headers.HasHeader(kChromeProxyHeader)); 166 EXPECT_EQ(expected_data_reduction_proxy_used,
167 headers.HasHeader(kChromeProxyHeader));
tbansal1 2016/05/06 23:36:51 #include components/data_reduction_proxy/core/comm
megjablon 2016/05/07 00:29:08 Done.
167 std::string header_value; 168 std::string header_value;
168 headers.GetHeader(kChromeProxyHeader, &header_value); 169 headers.GetHeader(kChromeProxyHeader, &header_value);
169 EXPECT_EQ(expected_lofi_used, 170 EXPECT_EQ(expected_data_reduction_proxy_used && expected_lofi_used,
170 header_value.find("q=low") != std::string::npos); 171 header_value.find("q=low") != std::string::npos);
171 } 172 }
172 173
173 void VerifyWasLoFiModeActiveOnMainFrame(bool expected_value) { 174 void VerifyWasLoFiModeActiveOnMainFrame(bool expected_value) {
174 test_context_->RunUntilIdle(); 175 test_context_->RunUntilIdle();
175 EXPECT_EQ(expected_value, 176 EXPECT_EQ(expected_value,
176 test_context_->settings()->WasLoFiModeActiveOnMainFrame()); 177 test_context_->settings()->WasLoFiModeActiveOnMainFrame());
177 } 178 }
178 179
179 void VerifyDidNotifyLoFiResponse(bool lofi_response) const { 180 void VerifyDidNotifyLoFiResponse(bool lofi_response) const {
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 } 232 }
232 233
233 TestDataReductionProxyParams* params() const { 234 TestDataReductionProxyParams* params() const {
234 return test_context_->config()->test_params(); 235 return test_context_->config()->test_params();
235 } 236 }
236 237
237 TestDataReductionProxyConfig* config() const { 238 TestDataReductionProxyConfig* config() const {
238 return test_context_->config(); 239 return test_context_->config();
239 } 240 }
240 241
242 TestDataReductionProxyIOData* io_data() const {
243 return test_context_->io_data();
244 }
245
241 TestLoFiDecider* lofi_decider() const { return lofi_decider_; } 246 TestLoFiDecider* lofi_decider() const { return lofi_decider_; }
242 247
243 private: 248 private:
244 int64_t GetSessionNetworkStatsInfoInt64(const char* key) const { 249 int64_t GetSessionNetworkStatsInfoInt64(const char* key) const {
245 const DataReductionProxyNetworkDelegate* drp_network_delegate = 250 const DataReductionProxyNetworkDelegate* drp_network_delegate =
246 reinterpret_cast<const DataReductionProxyNetworkDelegate*>( 251 reinterpret_cast<const DataReductionProxyNetworkDelegate*>(
247 context_.network_delegate()); 252 context_.network_delegate());
248 253
249 std::unique_ptr<base::DictionaryValue> session_network_stats_info = 254 std::unique_ptr<base::DictionaryValue> session_network_stats_info =
250 base::DictionaryValue::From(base::WrapUnique( 255 base::DictionaryValue::From(base::WrapUnique(
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 headers.GetHeader(kChromeProxyHeader, &header_value); 291 headers.GetHeader(kChromeProxyHeader, &header_value);
287 EXPECT_TRUE(header_value.find("ps=") != std::string::npos); 292 EXPECT_TRUE(header_value.find("ps=") != std::string::npos);
288 EXPECT_TRUE(header_value.find("sid=") != std::string::npos); 293 EXPECT_TRUE(header_value.find("sid=") != std::string::npos);
289 } 294 }
290 295
291 TEST_F(DataReductionProxyNetworkDelegateTest, LoFiTransitions) { 296 TEST_F(DataReductionProxyNetworkDelegateTest, LoFiTransitions) {
292 // Enable Lo-Fi. 297 // Enable Lo-Fi.
293 const struct { 298 const struct {
294 bool lofi_switch_enabled; 299 bool lofi_switch_enabled;
295 bool auto_lofi_enabled; 300 bool auto_lofi_enabled;
301 bool is_data_reduction_proxy;
296 } tests[] = { 302 } tests[] = {
297 { 303 {
298 // Lo-Fi enabled through switch. 304 // Lo-Fi enabled through switch and not using a Data Reduction Proxy.
299 true, false, 305 true, false, false,
300 }, 306 },
301 { 307 {
302 // Lo-Fi enabled through field trial. 308 // Lo-Fi enabled through switch and using a Data Reduction Proxy.
303 false, true, 309 true, false, true,
310 },
311 {
312 // Lo-Fi enabled through field trial and not using a Data Reduction
313 // Proxy.
314 false, true, false,
315 },
316 {
317 // Lo-Fi enabled through field trial and using a Data Reduction Proxy.
318 false, true, true,
304 }, 319 },
305 }; 320 };
306 321
307 for (size_t i = 0; i < arraysize(tests); ++i) { 322 for (size_t i = 0; i < arraysize(tests); ++i) {
308 if (tests[i].lofi_switch_enabled) { 323 if (tests[i].lofi_switch_enabled) {
309 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( 324 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
310 switches::kDataReductionProxyLoFi, 325 switches::kDataReductionProxyLoFi,
311 switches::kDataReductionProxyLoFiValueAlwaysOn); 326 switches::kDataReductionProxyLoFiValueAlwaysOn);
312 } 327 }
313 base::FieldTrialList field_trial_list(nullptr); 328 base::FieldTrialList field_trial_list(nullptr);
314 if (tests[i].auto_lofi_enabled) { 329 if (tests[i].auto_lofi_enabled) {
315 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(), 330 base::FieldTrialList::CreateFieldTrial(params::GetLoFiFieldTrialName(),
316 "Enabled"); 331 "Enabled");
317 } 332 }
318 config()->SetNetworkProhibitivelySlow(tests[i].auto_lofi_enabled); 333 config()->SetNetworkProhibitivelySlow(tests[i].auto_lofi_enabled);
334 io_data()->SetLoFiModeActiveOnMainFrame(false);
319 335
320 net::ProxyInfo data_reduction_proxy_info; 336 net::ProxyInfo data_reduction_proxy_info;
321 std::string data_reduction_proxy; 337 std::string proxy;
322 base::TrimString(params()->DefaultOrigin(), "/", &data_reduction_proxy); 338 if (tests[i].is_data_reduction_proxy)
323 data_reduction_proxy_info.UseNamedProxy(data_reduction_proxy); 339 base::TrimString(params()->DefaultOrigin(), "/", &proxy);
340 else
341 base::TrimString(kOtherProxy, "/", &proxy);
342 data_reduction_proxy_info.UseNamedProxy(proxy);
324 343
325 { 344 {
326 // Main frame loaded. Lo-Fi should be used. 345 // Main frame loaded. Lo-Fi should be used.
327 net::HttpRequestHeaders headers; 346 net::HttpRequestHeaders headers;
328 347
329 std::unique_ptr<net::URLRequest> fake_request(FetchURLRequest( 348 std::unique_ptr<net::URLRequest> fake_request(FetchURLRequest(
330 GURL("http://www.google.com/"), nullptr, std::string(), 0)); 349 GURL("http://www.google.com/"), nullptr, std::string(), 0));
331 fake_request->SetLoadFlags(net::LOAD_MAIN_FRAME); 350 fake_request->SetLoadFlags(net::LOAD_MAIN_FRAME);
332 lofi_decider()->SetIsUsingLoFiMode( 351 lofi_decider()->SetIsUsingLoFiMode(
333 config()->ShouldEnableLoFiMode(*fake_request.get())); 352 config()->ShouldEnableLoFiMode(*fake_request.get()));
334 network_delegate()->NotifyBeforeSendProxyHeaders( 353 network_delegate()->NotifyBeforeSendProxyHeaders(
335 fake_request.get(), data_reduction_proxy_info, &headers); 354 fake_request.get(), data_reduction_proxy_info, &headers);
336 VerifyLoFiHeader(true, headers); 355 VerifyLoFiHeader(tests[i].is_data_reduction_proxy, true, headers);
337 VerifyWasLoFiModeActiveOnMainFrame(true); 356 VerifyWasLoFiModeActiveOnMainFrame(tests[i].is_data_reduction_proxy);
338 } 357 }
339 358
340 { 359 {
341 // Lo-Fi is already off. Lo-Fi should not be used. 360 // Lo-Fi is already off. Lo-Fi should not be used.
342 net::HttpRequestHeaders headers; 361 net::HttpRequestHeaders headers;
343 std::unique_ptr<net::URLRequest> fake_request(FetchURLRequest( 362 std::unique_ptr<net::URLRequest> fake_request(FetchURLRequest(
344 GURL("http://www.google.com/"), nullptr, std::string(), 0)); 363 GURL("http://www.google.com/"), nullptr, std::string(), 0));
345 lofi_decider()->SetIsUsingLoFiMode(false); 364 lofi_decider()->SetIsUsingLoFiMode(false);
346 network_delegate()->NotifyBeforeSendProxyHeaders( 365 network_delegate()->NotifyBeforeSendProxyHeaders(
347 fake_request.get(), data_reduction_proxy_info, &headers); 366 fake_request.get(), data_reduction_proxy_info, &headers);
348 VerifyLoFiHeader(false, headers); 367 VerifyLoFiHeader(tests[i].is_data_reduction_proxy, false, headers);
349 // Not a mainframe request, WasLoFiModeActiveOnMainFrame should still be 368 // Not a mainframe request, WasLoFiModeActiveOnMainFrame should still be
350 // true. 369 // true if the proxy is a Data Reduction Proxy.
351 VerifyWasLoFiModeActiveOnMainFrame(true); 370 VerifyWasLoFiModeActiveOnMainFrame(tests[i].is_data_reduction_proxy);
352 } 371 }
353 372
354 { 373 {
355 // Lo-Fi is already on. Lo-Fi should be used. 374 // Lo-Fi is already on. Lo-Fi should be used.
356 net::HttpRequestHeaders headers; 375 net::HttpRequestHeaders headers;
357 std::unique_ptr<net::URLRequest> fake_request(FetchURLRequest( 376 std::unique_ptr<net::URLRequest> fake_request(FetchURLRequest(
358 GURL("http://www.google.com/"), nullptr, std::string(), 0)); 377 GURL("http://www.google.com/"), nullptr, std::string(), 0));
359 378
360 lofi_decider()->SetIsUsingLoFiMode(true); 379 lofi_decider()->SetIsUsingLoFiMode(true);
361 network_delegate()->NotifyBeforeSendProxyHeaders( 380 network_delegate()->NotifyBeforeSendProxyHeaders(
362 fake_request.get(), data_reduction_proxy_info, &headers); 381 fake_request.get(), data_reduction_proxy_info, &headers);
363 VerifyLoFiHeader(true, headers); 382 VerifyLoFiHeader(tests[i].is_data_reduction_proxy,
383 tests[i].is_data_reduction_proxy, headers);
tbansal1 2016/05/06 23:36:51 nit: For the second argument, can you not just pas
megjablon 2016/05/07 00:29:08 Whoops yep, missed switching this one.
364 // Not a mainframe request, WasLoFiModeActiveOnMainFrame should still be 384 // Not a mainframe request, WasLoFiModeActiveOnMainFrame should still be
365 // true. 385 // true if the proxy is a Data Reduction Proxy.
366 VerifyWasLoFiModeActiveOnMainFrame(true); 386 VerifyWasLoFiModeActiveOnMainFrame(tests[i].is_data_reduction_proxy);
367 } 387 }
368 388
369 { 389 {
370 // TODO(megjablon): Can remove the cases below once
371 // WasLoFiModeActiveOnMainFrame is fixed to be per-page.
372 // Main frame request with Lo-Fi off. Lo-Fi should not be used. 390 // Main frame request with Lo-Fi off. Lo-Fi should not be used.
373 // State of Lo-Fi should persist until next page load. 391 // State of Lo-Fi should persist until next page load.
374 net::HttpRequestHeaders headers; 392 net::HttpRequestHeaders headers;
375 std::unique_ptr<net::URLRequest> fake_request(FetchURLRequest( 393 std::unique_ptr<net::URLRequest> fake_request(FetchURLRequest(
376 GURL("http://www.google.com/"), nullptr, std::string(), 0)); 394 GURL("http://www.google.com/"), nullptr, std::string(), 0));
377 fake_request->SetLoadFlags(net::LOAD_MAIN_FRAME); 395 fake_request->SetLoadFlags(net::LOAD_MAIN_FRAME);
378 lofi_decider()->SetIsUsingLoFiMode(false); 396 lofi_decider()->SetIsUsingLoFiMode(false);
379 network_delegate()->NotifyBeforeSendProxyHeaders( 397 network_delegate()->NotifyBeforeSendProxyHeaders(
380 fake_request.get(), data_reduction_proxy_info, &headers); 398 fake_request.get(), data_reduction_proxy_info, &headers);
381 VerifyLoFiHeader(false, headers); 399 VerifyLoFiHeader(tests[i].is_data_reduction_proxy, false, headers);
382 VerifyWasLoFiModeActiveOnMainFrame(false); 400 VerifyWasLoFiModeActiveOnMainFrame(false);
383 } 401 }
384 402
385 { 403 {
386 // Lo-Fi is off. Lo-Fi is still not used. 404 // Lo-Fi is off. Lo-Fi is still not used.
387 net::HttpRequestHeaders headers; 405 net::HttpRequestHeaders headers;
388 std::unique_ptr<net::URLRequest> fake_request(FetchURLRequest( 406 std::unique_ptr<net::URLRequest> fake_request(FetchURLRequest(
389 GURL("http://www.google.com/"), nullptr, std::string(), 0)); 407 GURL("http://www.google.com/"), nullptr, std::string(), 0));
390 lofi_decider()->SetIsUsingLoFiMode(false); 408 lofi_decider()->SetIsUsingLoFiMode(false);
391 network_delegate()->NotifyBeforeSendProxyHeaders( 409 network_delegate()->NotifyBeforeSendProxyHeaders(
392 fake_request.get(), data_reduction_proxy_info, &headers); 410 fake_request.get(), data_reduction_proxy_info, &headers);
393 VerifyLoFiHeader(false, headers); 411 VerifyLoFiHeader(tests[i].is_data_reduction_proxy, false, headers);
394 // Not a mainframe request, WasLoFiModeActiveOnMainFrame should still be 412 // Not a mainframe request, WasLoFiModeActiveOnMainFrame should still be
395 // false. 413 // false.
396 VerifyWasLoFiModeActiveOnMainFrame(false); 414 VerifyWasLoFiModeActiveOnMainFrame(false);
397 } 415 }
398 416
399 { 417 {
400 // Main frame request. Lo-Fi should be used. 418 // Main frame request. Lo-Fi should be used.
401 net::HttpRequestHeaders headers; 419 net::HttpRequestHeaders headers;
402 std::unique_ptr<net::URLRequest> fake_request(FetchURLRequest( 420 std::unique_ptr<net::URLRequest> fake_request(FetchURLRequest(
403 GURL("http://www.google.com/"), nullptr, std::string(), 0)); 421 GURL("http://www.google.com/"), nullptr, std::string(), 0));
404 fake_request->SetLoadFlags(net::LOAD_MAIN_FRAME); 422 fake_request->SetLoadFlags(net::LOAD_MAIN_FRAME);
405 lofi_decider()->SetIsUsingLoFiMode( 423 lofi_decider()->SetIsUsingLoFiMode(
406 config()->ShouldEnableLoFiMode(*fake_request.get())); 424 config()->ShouldEnableLoFiMode(*fake_request.get()));
407 network_delegate()->NotifyBeforeSendProxyHeaders( 425 network_delegate()->NotifyBeforeSendProxyHeaders(
408 fake_request.get(), data_reduction_proxy_info, &headers); 426 fake_request.get(), data_reduction_proxy_info, &headers);
409 VerifyLoFiHeader(true, headers); 427 VerifyLoFiHeader(tests[i].is_data_reduction_proxy,
410 VerifyWasLoFiModeActiveOnMainFrame(true); 428 tests[i].is_data_reduction_proxy, headers);
429 VerifyWasLoFiModeActiveOnMainFrame(tests[i].is_data_reduction_proxy);
411 } 430 }
412 } 431 }
413 } 432 }
414 433
415 TEST_F(DataReductionProxyNetworkDelegateTest, NetHistograms) { 434 TEST_F(DataReductionProxyNetworkDelegateTest, NetHistograms) {
416 const std::string kReceivedValidOCLHistogramName = 435 const std::string kReceivedValidOCLHistogramName =
417 "Net.HttpContentLengthWithValidOCL"; 436 "Net.HttpContentLengthWithValidOCL";
418 const std::string kOriginalValidOCLHistogramName = 437 const std::string kOriginalValidOCLHistogramName =
419 "Net.HttpOriginalContentLengthWithValidOCL"; 438 "Net.HttpOriginalContentLengthWithValidOCL";
420 const std::string kDifferenceValidOCLHistogramName = 439 const std::string kDifferenceValidOCLHistogramName =
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after
661 FetchURLRequest(GURL("http://www.google.com/"), nullptr, response_headers, 680 FetchURLRequest(GURL("http://www.google.com/"), nullptr, response_headers,
662 140); 681 140);
663 682
664 histogram_tester.ExpectBucketCount(kLoFiTransformationTypeHistogram, PREVIEW, 683 histogram_tester.ExpectBucketCount(kLoFiTransformationTypeHistogram, PREVIEW,
665 1); 684 1);
666 } 685 }
667 686
668 } // namespace 687 } // namespace
669 688
670 } // namespace data_reduction_proxy 689 } // namespace data_reduction_proxy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698