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

Side by Side Diff: content/browser/loader/resource_loader_unittest.cc

Issue 2213683003: Update Effective Connection Type only on main frame requests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: PS Created 4 years, 4 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 (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 "content/browser/loader/resource_loader.h" 5 #include "content/browser/loader/resource_loader.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <memory> 10 #include <memory>
(...skipping 571 matching lines...) Expand 10 before | Expand all | Expand 10 after
582 } 582 }
583 583
584 virtual std::unique_ptr<ResourceHandler> WrapResourceHandler( 584 virtual std::unique_ptr<ResourceHandler> WrapResourceHandler(
585 std::unique_ptr<ResourceHandlerStub> leaf_handler, 585 std::unique_ptr<ResourceHandlerStub> leaf_handler,
586 net::URLRequest* request) { 586 net::URLRequest* request) {
587 return std::move(leaf_handler); 587 return std::move(leaf_handler);
588 } 588 }
589 589
590 // Replaces loader_ with a new one for |request|. 590 // Replaces loader_ with a new one for |request|.
591 void SetUpResourceLoader(std::unique_ptr<net::URLRequest> request, 591 void SetUpResourceLoader(std::unique_ptr<net::URLRequest> request,
592 bool is_main_frame) { 592 bool is_main_frame_request,
593 bool belongs_to_main_frame) {
593 raw_ptr_to_request_ = request.get(); 594 raw_ptr_to_request_ = request.get();
594 595
596 // A request marked as a main frame request must also belong to a main
597 // frame.
598 ASSERT_TRUE(!is_main_frame_request || belongs_to_main_frame);
599
595 ResourceType resource_type = 600 ResourceType resource_type =
RyanSturm 2016/08/08 18:13:37 nit: Would it be better to pass in ResourceType in
tbansal1 2016/08/08 18:35:25 Not sure. May be we can change in future if there
tbansal1 2016/08/08 18:45:14 Done.
596 is_main_frame ? RESOURCE_TYPE_MAIN_FRAME : RESOURCE_TYPE_SUB_FRAME; 601 is_main_frame_request ? RESOURCE_TYPE_MAIN_FRAME : RESOURCE_TYPE_OBJECT;
RyanSturm 2016/08/08 18:16:45 #include "content/public/common/resource_type.h"
tbansal1 2016/08/08 18:35:25 Done.
597 602
598 RenderFrameHost* rfh = web_contents_->GetMainFrame(); 603 RenderFrameHost* rfh = web_contents_->GetMainFrame();
599 ResourceRequestInfo::AllocateForTesting( 604 ResourceRequestInfo::AllocateForTesting(
600 request.get(), resource_type, &resource_context_, 605 request.get(), resource_type, &resource_context_,
601 rfh->GetProcess()->GetID(), rfh->GetRenderViewHost()->GetRoutingID(), 606 rfh->GetProcess()->GetID(), rfh->GetRenderViewHost()->GetRoutingID(),
602 rfh->GetRoutingID(), is_main_frame, false /* parent_is_main_frame */, 607 rfh->GetRoutingID(), belongs_to_main_frame,
603 true /* allow_download */, false /* is_async */, 608 false /* parent_is_main_frame */, true /* allow_download */,
604 false /* is_using_lofi_ */); 609 false /* is_async */, false /* is_using_lofi_ */);
605 std::unique_ptr<ResourceHandlerStub> resource_handler( 610 std::unique_ptr<ResourceHandlerStub> resource_handler(
606 new ResourceHandlerStub(request.get())); 611 new ResourceHandlerStub(request.get()));
607 raw_ptr_resource_handler_ = resource_handler.get(); 612 raw_ptr_resource_handler_ = resource_handler.get();
608 loader_.reset(new ResourceLoader( 613 loader_.reset(new ResourceLoader(
609 std::move(request), 614 std::move(request),
610 WrapResourceHandler(std::move(resource_handler), raw_ptr_to_request_), 615 WrapResourceHandler(std::move(resource_handler), raw_ptr_to_request_),
611 CertStore::GetInstance(), this)); 616 CertStore::GetInstance(), this));
612 } 617 }
613 618
614 void SetUp() override { 619 void SetUp() override {
615 job_factory_.SetProtocolHandler("test", CreateProtocolHandler()); 620 job_factory_.SetProtocolHandler("test", CreateProtocolHandler());
616 621
617 browser_context_.reset(new TestBrowserContext()); 622 browser_context_.reset(new TestBrowserContext());
618 scoped_refptr<SiteInstance> site_instance = 623 scoped_refptr<SiteInstance> site_instance =
619 SiteInstance::Create(browser_context_.get()); 624 SiteInstance::Create(browser_context_.get());
620 web_contents_.reset( 625 web_contents_.reset(
621 TestWebContents::Create(browser_context_.get(), site_instance.get())); 626 TestWebContents::Create(browser_context_.get(), site_instance.get()));
622 627
623 std::unique_ptr<net::URLRequest> request( 628 std::unique_ptr<net::URLRequest> request(
624 resource_context_.GetRequestContext()->CreateRequest( 629 resource_context_.GetRequestContext()->CreateRequest(
625 test_url(), net::DEFAULT_PRIORITY, nullptr /* delegate */)); 630 test_url(), net::DEFAULT_PRIORITY, nullptr /* delegate */));
626 SetUpResourceLoader(std::move(request), true); 631 SetUpResourceLoader(std::move(request), true, true);
627 } 632 }
628 633
629 void TearDown() override { 634 void TearDown() override {
630 // Destroy the WebContents and pump the event loop before destroying 635 // Destroy the WebContents and pump the event loop before destroying
631 // |rvh_test_enabler_| and |thread_bundle_|. This lets asynchronous cleanup 636 // |rvh_test_enabler_| and |thread_bundle_|. This lets asynchronous cleanup
632 // tasks complete. 637 // tasks complete.
633 web_contents_.reset(); 638 web_contents_.reset();
634 base::RunLoop().RunUntilIdle(); 639 base::RunLoop().RunUntilIdle();
635 } 640 }
636 641
(...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after
1105 EXPECT_EQ(test_data(), contents); 1110 EXPECT_EQ(test_data(), contents);
1106 } 1111 }
1107 1112
1108 // Test that an HTTPS resource has the expected security info attached 1113 // Test that an HTTPS resource has the expected security info attached
1109 // to it. 1114 // to it.
1110 TEST_F(HTTPSSecurityInfoResourceLoaderTest, SecurityInfoOnHTTPSResource) { 1115 TEST_F(HTTPSSecurityInfoResourceLoaderTest, SecurityInfoOnHTTPSResource) {
1111 // Start the request and wait for it to finish. 1116 // Start the request and wait for it to finish.
1112 std::unique_ptr<net::URLRequest> request( 1117 std::unique_ptr<net::URLRequest> request(
1113 resource_context_.GetRequestContext()->CreateRequest( 1118 resource_context_.GetRequestContext()->CreateRequest(
1114 test_https_url(), net::DEFAULT_PRIORITY, nullptr /* delegate */)); 1119 test_https_url(), net::DEFAULT_PRIORITY, nullptr /* delegate */));
1115 SetUpResourceLoader(std::move(request), true); 1120 SetUpResourceLoader(std::move(request), true, true);
1116 1121
1117 // Send the request and wait until it completes. 1122 // Send the request and wait until it completes.
1118 loader_->StartRequest(); 1123 loader_->StartRequest();
1119 raw_ptr_resource_handler_->WaitForResponseComplete(); 1124 raw_ptr_resource_handler_->WaitForResponseComplete();
1120 ASSERT_EQ(net::URLRequestStatus::SUCCESS, 1125 ASSERT_EQ(net::URLRequestStatus::SUCCESS,
1121 raw_ptr_to_request_->status().status()); 1126 raw_ptr_to_request_->status().status());
1122 1127
1123 ResourceResponse* response = raw_ptr_resource_handler_->response(); 1128 ResourceResponse* response = raw_ptr_resource_handler_->response();
1124 ASSERT_TRUE(response); 1129 ASSERT_TRUE(response);
1125 1130
(...skipping 18 matching lines...) Expand all
1144 1149
1145 // Test that an HTTPS redirect response has the expected security info 1150 // Test that an HTTPS redirect response has the expected security info
1146 // attached to it. 1151 // attached to it.
1147 TEST_F(HTTPSSecurityInfoResourceLoaderTest, 1152 TEST_F(HTTPSSecurityInfoResourceLoaderTest,
1148 SecurityInfoOnHTTPSRedirectResource) { 1153 SecurityInfoOnHTTPSRedirectResource) {
1149 // Start the request and wait for it to finish. 1154 // Start the request and wait for it to finish.
1150 std::unique_ptr<net::URLRequest> request( 1155 std::unique_ptr<net::URLRequest> request(
1151 resource_context_.GetRequestContext()->CreateRequest( 1156 resource_context_.GetRequestContext()->CreateRequest(
1152 test_https_redirect_url(), net::DEFAULT_PRIORITY, 1157 test_https_redirect_url(), net::DEFAULT_PRIORITY,
1153 nullptr /* delegate */)); 1158 nullptr /* delegate */));
1154 SetUpResourceLoader(std::move(request), true); 1159 SetUpResourceLoader(std::move(request), true, true);
1155 1160
1156 // Send the request and wait until it completes. 1161 // Send the request and wait until it completes.
1157 loader_->StartRequest(); 1162 loader_->StartRequest();
1158 raw_ptr_resource_handler_->WaitForResponseComplete(); 1163 raw_ptr_resource_handler_->WaitForResponseComplete();
1159 ASSERT_EQ(net::URLRequestStatus::SUCCESS, 1164 ASSERT_EQ(net::URLRequestStatus::SUCCESS,
1160 raw_ptr_to_request_->status().status()); 1165 raw_ptr_to_request_->status().status());
1161 ASSERT_TRUE(raw_ptr_resource_handler_->received_request_redirected()); 1166 ASSERT_TRUE(raw_ptr_resource_handler_->received_request_redirected());
1162 1167
1163 ResourceResponse* redirect_response = 1168 ResourceResponse* redirect_response =
1164 raw_ptr_resource_handler_->redirect_response(); 1169 raw_ptr_resource_handler_->redirect_response();
(...skipping 14 matching lines...) Expand all
1179 EXPECT_TRUE(cert->Equals(GetTestCert().get())); 1184 EXPECT_TRUE(cert->Equals(GetTestCert().get()));
1180 1185
1181 EXPECT_EQ(kTestCertError, deserialized.cert_status); 1186 EXPECT_EQ(kTestCertError, deserialized.cert_status);
1182 EXPECT_EQ(kTestConnectionStatus, deserialized.connection_status); 1187 EXPECT_EQ(kTestConnectionStatus, deserialized.connection_status);
1183 EXPECT_EQ(kTestSecurityBits, deserialized.security_bits); 1188 EXPECT_EQ(kTestSecurityBits, deserialized.security_bits);
1184 } 1189 }
1185 1190
1186 class EffectiveConnectionTypeResourceLoaderTest : public ResourceLoaderTest { 1191 class EffectiveConnectionTypeResourceLoaderTest : public ResourceLoaderTest {
1187 public: 1192 public:
1188 void VerifyEffectiveConnectionType( 1193 void VerifyEffectiveConnectionType(
1189 bool is_main_frame, 1194 bool is_main_frame_request,
1195 bool belongs_to_main_frame,
1190 net::EffectiveConnectionType set_type, 1196 net::EffectiveConnectionType set_type,
1191 net::EffectiveConnectionType expected_type) { 1197 net::EffectiveConnectionType expected_type) {
1192 network_quality_estimator()->set_effective_connection_type(set_type); 1198 network_quality_estimator()->set_effective_connection_type(set_type);
1193 1199
1194 // Start the request and wait for it to finish. 1200 // Start the request and wait for it to finish.
1195 std::unique_ptr<net::URLRequest> request( 1201 std::unique_ptr<net::URLRequest> request(
1196 resource_context_.GetRequestContext()->CreateRequest( 1202 resource_context_.GetRequestContext()->CreateRequest(
1197 test_url(), net::DEFAULT_PRIORITY, nullptr /* delegate */)); 1203 test_url(), net::DEFAULT_PRIORITY, nullptr /* delegate */));
1198 SetUpResourceLoader(std::move(request), is_main_frame); 1204 SetUpResourceLoader(std::move(request), is_main_frame_request,
1205 belongs_to_main_frame);
1199 1206
1200 // Send the request and wait until it completes. 1207 // Send the request and wait until it completes.
1201 loader_->StartRequest(); 1208 loader_->StartRequest();
1202 raw_ptr_resource_handler_->WaitForResponseComplete(); 1209 raw_ptr_resource_handler_->WaitForResponseComplete();
1203 ASSERT_EQ(net::URLRequestStatus::SUCCESS, 1210 ASSERT_EQ(net::URLRequestStatus::SUCCESS,
1204 raw_ptr_to_request_->status().status()); 1211 raw_ptr_to_request_->status().status());
1205 1212
1206 EXPECT_EQ(expected_type, 1213 EXPECT_EQ(expected_type,
1207 raw_ptr_resource_handler_->observed_effective_connection_type()); 1214 raw_ptr_resource_handler_->observed_effective_connection_type());
1208 } 1215 }
1209 }; 1216 };
1210 1217
1211 // Tests that the effective connection type is set on main frame requests. 1218 // Tests that the effective connection type is set on main frame requests.
1212 TEST_F(EffectiveConnectionTypeResourceLoaderTest, Slow2G) { 1219 TEST_F(EffectiveConnectionTypeResourceLoaderTest, Slow2G) {
1213 VerifyEffectiveConnectionType(true, net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G, 1220 VerifyEffectiveConnectionType(true, true,
1221 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G,
1214 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G); 1222 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
1215 } 1223 }
1216 1224
1217 // Tests that the effective connection type is set on main frame requests. 1225 // Tests that the effective connection type is set on main frame requests.
1218 TEST_F(EffectiveConnectionTypeResourceLoaderTest, 3G) { 1226 TEST_F(EffectiveConnectionTypeResourceLoaderTest, 3G) {
1219 VerifyEffectiveConnectionType(true, net::EFFECTIVE_CONNECTION_TYPE_3G, 1227 VerifyEffectiveConnectionType(true, true, net::EFFECTIVE_CONNECTION_TYPE_3G,
1220 net::EFFECTIVE_CONNECTION_TYPE_3G); 1228 net::EFFECTIVE_CONNECTION_TYPE_3G);
1221 } 1229 }
1222 1230
1231 // Tests that the effective connection type is not set on requests that belong
1232 // to main frame.
1233 TEST_F(EffectiveConnectionTypeResourceLoaderTest, BelongsToMainFrame) {
1234 VerifyEffectiveConnectionType(false, true, net::EFFECTIVE_CONNECTION_TYPE_3G,
1235 net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN);
1236 }
1237
1223 // Tests that the effective connection type is not set on non-main frame 1238 // Tests that the effective connection type is not set on non-main frame
1224 // requests. 1239 // requests.
1225 TEST_F(EffectiveConnectionTypeResourceLoaderTest, NotAMainFrame) { 1240 TEST_F(EffectiveConnectionTypeResourceLoaderTest, DoesNotBelongToMainFrame) {
1226 VerifyEffectiveConnectionType(false, net::EFFECTIVE_CONNECTION_TYPE_3G, 1241 VerifyEffectiveConnectionType(false, false, net::EFFECTIVE_CONNECTION_TYPE_3G,
1227 net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN); 1242 net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN);
1228 } 1243 }
1229 1244
1230 } // namespace content 1245 } // namespace content
OLDNEW
« content/browser/loader/resource_loader.cc ('K') | « content/browser/loader/resource_loader.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698