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

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

Issue 1130343006: Don't share ResourceDispatcherHostImpl's timer for reporting upload progress. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@safari-backend
Patch Set: Created 5 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 (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 "base/files/file.h" 7 #include "base/files/file.h"
8 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/message_loop/message_loop_proxy.h" 10 #include "base/message_loop/message_loop_proxy.h"
11 #include "base/run_loop.h" 11 #include "base/run_loop.h"
12 #include "content/browser/browser_thread_impl.h" 12 #include "content/browser/browser_thread_impl.h"
13 #include "content/browser/loader/redirect_to_file_resource_handler.h" 13 #include "content/browser/loader/redirect_to_file_resource_handler.h"
14 #include "content/browser/loader/resource_loader_delegate.h" 14 #include "content/browser/loader/resource_loader_delegate.h"
15 #include "content/public/browser/client_certificate_delegate.h" 15 #include "content/public/browser/client_certificate_delegate.h"
16 #include "content/public/browser/resource_request_info.h" 16 #include "content/public/browser/resource_request_info.h"
17 #include "content/public/common/content_paths.h"
17 #include "content/public/common/resource_response.h" 18 #include "content/public/common/resource_response.h"
18 #include "content/public/test/mock_resource_context.h" 19 #include "content/public/test/mock_resource_context.h"
19 #include "content/public/test/test_browser_context.h" 20 #include "content/public/test/test_browser_context.h"
20 #include "content/public/test/test_browser_thread_bundle.h" 21 #include "content/public/test/test_browser_thread_bundle.h"
21 #include "content/public/test/test_renderer_host.h" 22 #include "content/public/test/test_renderer_host.h"
22 #include "content/test/test_content_browser_client.h" 23 #include "content/test/test_content_browser_client.h"
23 #include "content/test/test_web_contents.h" 24 #include "content/test/test_web_contents.h"
24 #include "ipc/ipc_message.h" 25 #include "ipc/ipc_message.h"
26 #include "net/base/elements_upload_data_stream.h"
25 #include "net/base/io_buffer.h" 27 #include "net/base/io_buffer.h"
26 #include "net/base/mock_file_stream.h" 28 #include "net/base/mock_file_stream.h"
27 #include "net/base/net_errors.h" 29 #include "net/base/net_errors.h"
28 #include "net/base/request_priority.h" 30 #include "net/base/request_priority.h"
31 #include "net/base/upload_bytes_element_reader.h"
29 #include "net/cert/x509_certificate.h" 32 #include "net/cert/x509_certificate.h"
30 #include "net/ssl/client_cert_store.h" 33 #include "net/ssl/client_cert_store.h"
31 #include "net/ssl/ssl_cert_request_info.h" 34 #include "net/ssl/ssl_cert_request_info.h"
35 #include "net/test/embedded_test_server/embedded_test_server.h"
32 #include "net/url_request/url_request.h" 36 #include "net/url_request/url_request.h"
33 #include "net/url_request/url_request_job_factory.h" 37 #include "net/url_request/url_request_job_factory.h"
34 #include "net/url_request/url_request_job_factory_impl.h" 38 #include "net/url_request/url_request_job_factory_impl.h"
35 #include "net/url_request/url_request_test_job.h" 39 #include "net/url_request/url_request_test_job.h"
36 #include "net/url_request/url_request_test_util.h" 40 #include "net/url_request/url_request_test_util.h"
37 #include "storage/browser/blob/shareable_file_reference.h" 41 #include "storage/browser/blob/shareable_file_reference.h"
38 #include "testing/gtest/include/gtest/gtest.h" 42 #include "testing/gtest/include/gtest/gtest.h"
39 43
40 using storage::ShareableFileReference; 44 using storage::ShareableFileReference;
41 45
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 explicit ResourceHandlerStub(net::URLRequest* request) 172 explicit ResourceHandlerStub(net::URLRequest* request)
169 : ResourceHandler(request), 173 : ResourceHandler(request),
170 read_buffer_(new net::IOBuffer(kReadBufSize)), 174 read_buffer_(new net::IOBuffer(kReadBufSize)),
171 defer_request_on_will_start_(false), 175 defer_request_on_will_start_(false),
172 expect_reads_(true), 176 expect_reads_(true),
173 cancel_on_read_completed_(false), 177 cancel_on_read_completed_(false),
174 defer_eof_(false), 178 defer_eof_(false),
175 received_on_will_read_(false), 179 received_on_will_read_(false),
176 received_eof_(false), 180 received_eof_(false),
177 received_response_completed_(false), 181 received_response_completed_(false),
178 total_bytes_downloaded_(0) { 182 total_bytes_downloaded_(0),
183 upload_position_(0) {
179 } 184 }
180 185
181 // If true, defers the resource load in OnWillStart. 186 // If true, defers the resource load in OnWillStart.
182 void set_defer_request_on_will_start(bool defer_request_on_will_start) { 187 void set_defer_request_on_will_start(bool defer_request_on_will_start) {
183 defer_request_on_will_start_ = defer_request_on_will_start; 188 defer_request_on_will_start_ = defer_request_on_will_start;
184 } 189 }
185 190
186 // If true, expect OnWillRead / OnReadCompleted pairs for handling 191 // If true, expect OnWillRead / OnReadCompleted pairs for handling
187 // data. Otherwise, expect OnDataDownloaded. 192 // data. Otherwise, expect OnDataDownloaded.
188 void set_expect_reads(bool expect_reads) { expect_reads_ = expect_reads; } 193 void set_expect_reads(bool expect_reads) { expect_reads_ = expect_reads; }
(...skipping 11 matching lines...) Expand all
200 bool received_response_completed() const { 205 bool received_response_completed() const {
201 return received_response_completed_; 206 return received_response_completed_;
202 } 207 }
203 const net::URLRequestStatus& status() const { return status_; } 208 const net::URLRequestStatus& status() const { return status_; }
204 int total_bytes_downloaded() const { return total_bytes_downloaded_; } 209 int total_bytes_downloaded() const { return total_bytes_downloaded_; }
205 210
206 void Resume() { 211 void Resume() {
207 controller()->Resume(); 212 controller()->Resume();
208 } 213 }
209 214
215 // Waits until OnUploadProgress is called and returns the upload position.
216 uint64 WaitForUploadProgress() {
217 wait_for_progress_loop_.reset(new base::RunLoop());
218 wait_for_progress_loop_->Run();
219 wait_for_progress_loop_.reset();
220 return upload_position_;
221 }
222
210 // ResourceHandler implementation: 223 // ResourceHandler implementation:
211 bool OnUploadProgress(uint64 position, uint64 size) override { 224 bool OnUploadProgress(uint64 position, uint64 size) override {
212 NOTREACHED(); 225 upload_position_ = position;
226 if (wait_for_progress_loop_)
227 wait_for_progress_loop_->Quit();
213 return true; 228 return true;
214 } 229 }
215 230
216 bool OnRequestRedirected(const net::RedirectInfo& redirect_info, 231 bool OnRequestRedirected(const net::RedirectInfo& redirect_info,
217 ResourceResponse* response, 232 ResourceResponse* response,
218 bool* defer) override { 233 bool* defer) override {
219 NOTREACHED(); 234 NOTREACHED();
220 return true; 235 return true;
221 } 236 }
222 237
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 bool cancel_on_read_completed_; 309 bool cancel_on_read_completed_;
295 bool defer_eof_; 310 bool defer_eof_;
296 311
297 GURL start_url_; 312 GURL start_url_;
298 scoped_refptr<ResourceResponse> response_; 313 scoped_refptr<ResourceResponse> response_;
299 bool received_on_will_read_; 314 bool received_on_will_read_;
300 bool received_eof_; 315 bool received_eof_;
301 bool received_response_completed_; 316 bool received_response_completed_;
302 net::URLRequestStatus status_; 317 net::URLRequestStatus status_;
303 int total_bytes_downloaded_; 318 int total_bytes_downloaded_;
319 scoped_ptr<base::RunLoop> wait_for_progress_loop_;
320 uint64 upload_position_;
304 }; 321 };
305 322
306 // Test browser client that captures calls to SelectClientCertificates and 323 // Test browser client that captures calls to SelectClientCertificates and
307 // records the arguments of the most recent call for later inspection. 324 // records the arguments of the most recent call for later inspection.
308 class SelectCertificateBrowserClient : public TestContentBrowserClient { 325 class SelectCertificateBrowserClient : public TestContentBrowserClient {
309 public: 326 public:
310 SelectCertificateBrowserClient() : call_count_(0) {} 327 SelectCertificateBrowserClient() : call_count_(0) {}
311 328
312 void SelectClientCertificate( 329 void SelectClientCertificate(
313 WebContents* web_contents, 330 WebContents* web_contents,
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 virtual net::URLRequestJobFactory::ProtocolHandler* CreateProtocolHandler() { 400 virtual net::URLRequestJobFactory::ProtocolHandler* CreateProtocolHandler() {
384 return net::URLRequestTestJob::CreateProtocolHandler(); 401 return net::URLRequestTestJob::CreateProtocolHandler();
385 } 402 }
386 403
387 virtual scoped_ptr<ResourceHandler> WrapResourceHandler( 404 virtual scoped_ptr<ResourceHandler> WrapResourceHandler(
388 scoped_ptr<ResourceHandlerStub> leaf_handler, 405 scoped_ptr<ResourceHandlerStub> leaf_handler,
389 net::URLRequest* request) { 406 net::URLRequest* request) {
390 return leaf_handler.Pass(); 407 return leaf_handler.Pass();
391 } 408 }
392 409
393 void SetUp() override { 410 void SetUpResourceLoader(scoped_ptr<net::URLRequest> request) {
mmenke 2015/05/21 15:41:57 Think this is worth a brief comment. Just somethi
Andre 2015/05/21 20:59:31 Done.
394 job_factory_.SetProtocolHandler("test", CreateProtocolHandler()); 411 raw_ptr_to_request_ = request.get();
395 412
396 browser_context_.reset(new TestBrowserContext());
397 scoped_refptr<SiteInstance> site_instance =
398 SiteInstance::Create(browser_context_.get());
399 web_contents_.reset(
400 TestWebContents::Create(browser_context_.get(), site_instance.get()));
401 RenderFrameHost* rfh = web_contents_->GetMainFrame(); 413 RenderFrameHost* rfh = web_contents_->GetMainFrame();
402
403 scoped_ptr<net::URLRequest> request(
404 resource_context_.GetRequestContext()->CreateRequest(
405 test_url(),
406 net::DEFAULT_PRIORITY,
407 NULL /* delegate */));
408 raw_ptr_to_request_ = request.get();
409 ResourceRequestInfo::AllocateForTesting( 414 ResourceRequestInfo::AllocateForTesting(
410 request.get(), RESOURCE_TYPE_MAIN_FRAME, &resource_context_, 415 request.get(), RESOURCE_TYPE_MAIN_FRAME, &resource_context_,
411 rfh->GetProcess()->GetID(), rfh->GetRenderViewHost()->GetRoutingID(), 416 rfh->GetProcess()->GetID(), rfh->GetRenderViewHost()->GetRoutingID(),
412 rfh->GetRoutingID(), true /* is_main_frame */, 417 rfh->GetRoutingID(), true /* is_main_frame */,
413 false /* parent_is_main_frame */, true /* allow_download */, 418 false /* parent_is_main_frame */, true /* allow_download */,
414 false /* is_async */); 419 false /* is_async */);
415 scoped_ptr<ResourceHandlerStub> resource_handler( 420 scoped_ptr<ResourceHandlerStub> resource_handler(
416 new ResourceHandlerStub(request.get())); 421 new ResourceHandlerStub(request.get()));
417 raw_ptr_resource_handler_ = resource_handler.get(); 422 raw_ptr_resource_handler_ = resource_handler.get();
418 loader_.reset(new ResourceLoader( 423 loader_.reset(new ResourceLoader(
419 request.Pass(), 424 request.Pass(),
420 WrapResourceHandler(resource_handler.Pass(), raw_ptr_to_request_), 425 WrapResourceHandler(resource_handler.Pass(), raw_ptr_to_request_),
421 this)); 426 this));
422 } 427 }
423 428
429 void SetUp() override {
430 job_factory_.SetProtocolHandler("test", CreateProtocolHandler());
431
432 browser_context_.reset(new TestBrowserContext());
433 scoped_refptr<SiteInstance> site_instance =
434 SiteInstance::Create(browser_context_.get());
435 web_contents_.reset(
436 TestWebContents::Create(browser_context_.get(), site_instance.get()));
437
438 scoped_ptr<net::URLRequest> request(
439 resource_context_.GetRequestContext()->CreateRequest(
440 test_url(),
441 net::DEFAULT_PRIORITY,
442 nullptr /* delegate */));
443 SetUpResourceLoader(request.Pass());
444 }
445
424 void TearDown() override { 446 void TearDown() override {
425 // Destroy the WebContents and pump the event loop before destroying 447 // Destroy the WebContents and pump the event loop before destroying
426 // |rvh_test_enabler_| and |thread_bundle_|. This lets asynchronous cleanup 448 // |rvh_test_enabler_| and |thread_bundle_|. This lets asynchronous cleanup
427 // tasks complete. 449 // tasks complete.
428 web_contents_.reset(); 450 web_contents_.reset();
429 base::RunLoop().RunUntilIdle(); 451 base::RunLoop().RunUntilIdle();
430 } 452 }
431 453
432 // ResourceLoaderDelegate: 454 // ResourceLoaderDelegate:
433 ResourceDispatcherHostLoginDelegate* CreateLoginDelegate( 455 ResourceDispatcherHostLoginDelegate* CreateLoginDelegate(
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
633 EXPECT_FALSE(raw_ptr_resource_handler_->received_response_completed()); 655 EXPECT_FALSE(raw_ptr_resource_handler_->received_response_completed());
634 656
635 raw_ptr_resource_handler_->Resume(); 657 raw_ptr_resource_handler_->Resume();
636 base::RunLoop().RunUntilIdle(); 658 base::RunLoop().RunUntilIdle();
637 659
638 EXPECT_TRUE(raw_ptr_resource_handler_->received_response_completed()); 660 EXPECT_TRUE(raw_ptr_resource_handler_->received_response_completed());
639 EXPECT_EQ(net::URLRequestStatus::SUCCESS, 661 EXPECT_EQ(net::URLRequestStatus::SUCCESS,
640 raw_ptr_resource_handler_->status().status()); 662 raw_ptr_resource_handler_->status().status());
641 } 663 }
642 664
665 TEST_F(ResourceLoaderTest, UploadProgress) {
mmenke 2015/05/21 15:41:57 Could you add a TODO about adding a test for the r
mmenke 2015/05/21 15:41:57 This test doesn't actually make sure the timer wor
Andre 2015/05/21 20:59:31 Done. I wrapped ChunkedUploadDataStream to make it
666 // Set up a test server.
667 net::test_server::EmbeddedTestServer server;
668 ASSERT_TRUE(server.InitializeAndWaitUntilReady());
669 base::FilePath path;
670 PathService::Get(content::DIR_TEST_DATA, &path);
671 server.ServeFilesFromDirectory(path);
672
673 scoped_ptr<net::URLRequest> request(
674 resource_context_.GetRequestContext()->CreateRequest(
675 server.GetURL("/title1.html"),
676 net::DEFAULT_PRIORITY,
677 nullptr /* delegate */));
678
679 // Start an upload.
680 std::string upload_content("test");
681 scoped_ptr<net::UploadElementReader> reader(new net::UploadBytesElementReader(
682 upload_content.data(), upload_content.size()));
683 request->set_upload(
684 net::ElementsUploadDataStream::CreateWithReader(reader.Pass(), 0));
685
686 SetUpResourceLoader(request.Pass());
687 loader_->StartRequest();
688
689 // Expect at least one upload progress, and the last one must match the upload
690 // size.
691 while (true) {
692 uint64 position = raw_ptr_resource_handler_->WaitForUploadProgress();
693 EXPECT_LE(position, upload_content.size());
mmenke 2015/05/21 15:41:57 Should also check that position is monotonically i
Andre 2015/05/21 20:59:31 Done. Added the check inside OnUploadProgress().
694 if (position == upload_content.size())
695 break;
696 }
697 }
698
643 class ResourceLoaderRedirectToFileTest : public ResourceLoaderTest { 699 class ResourceLoaderRedirectToFileTest : public ResourceLoaderTest {
644 public: 700 public:
645 ResourceLoaderRedirectToFileTest() 701 ResourceLoaderRedirectToFileTest()
646 : file_stream_(NULL), 702 : file_stream_(NULL),
647 redirect_to_file_resource_handler_(NULL) { 703 redirect_to_file_resource_handler_(NULL) {
648 } 704 }
649 705
650 base::FilePath temp_path() const { return temp_path_; } 706 base::FilePath temp_path() const { return temp_path_; }
651 ShareableFileReference* deletable_file() const { 707 ShareableFileReference* deletable_file() const {
652 return deletable_file_.get(); 708 return deletable_file_.get();
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
884 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents)); 940 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents));
885 EXPECT_EQ(test_data(), contents); 941 EXPECT_EQ(test_data(), contents);
886 942
887 // Release the loader. The file should be gone now. 943 // Release the loader. The file should be gone now.
888 ReleaseLoader(); 944 ReleaseLoader();
889 base::RunLoop().RunUntilIdle(); 945 base::RunLoop().RunUntilIdle();
890 EXPECT_FALSE(base::PathExists(temp_path())); 946 EXPECT_FALSE(base::PathExists(temp_path()));
891 } 947 }
892 948
893 } // namespace content 949 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698