| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/test_url_loader_client.h" | 5 #include "content/browser/loader/test_url_loader_client.h" |
| 6 | 6 |
| 7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 10 |
| 10 namespace content { | 11 namespace content { |
| 11 | 12 |
| 12 TestURLLoaderClient::TestURLLoaderClient() : binding_(this) {} | 13 TestURLLoaderClient::TestURLLoaderClient() : binding_(this) {} |
| 13 TestURLLoaderClient::~TestURLLoaderClient() {} | 14 TestURLLoaderClient::~TestURLLoaderClient() {} |
| 14 | 15 |
| 15 void TestURLLoaderClient::OnReceiveResponse( | 16 void TestURLLoaderClient::OnReceiveResponse( |
| 16 const ResourceResponseHead& response_head) { | 17 const ResourceResponseHead& response_head) { |
| 17 has_received_response_ = true; | 18 has_received_response_ = true; |
| 18 response_head_ = response_head; | 19 response_head_ = response_head; |
| 19 if (quit_closure_for_on_received_response_) | 20 if (quit_closure_for_on_receive_response_) |
| 20 quit_closure_for_on_received_response_.Run(); | 21 quit_closure_for_on_receive_response_.Run(); |
| 22 } |
| 23 |
| 24 void TestURLLoaderClient::OnReceiveRedirect( |
| 25 const net::RedirectInfo& redirect_info, |
| 26 const ResourceResponseHead& response_head) { |
| 27 EXPECT_FALSE(response_body_.is_valid()); |
| 28 EXPECT_FALSE(has_received_response_); |
| 29 // Use ClearHasReceivedRedirect to accept more redirects. |
| 30 EXPECT_FALSE(has_received_redirect_); |
| 31 has_received_redirect_ = true; |
| 32 redirect_info_ = redirect_info; |
| 33 response_head_ = response_head; |
| 34 if (quit_closure_for_on_receive_redirect_) |
| 35 quit_closure_for_on_receive_redirect_.Run(); |
| 21 } | 36 } |
| 22 | 37 |
| 23 void TestURLLoaderClient::OnDataDownloaded(int64_t data_length, | 38 void TestURLLoaderClient::OnDataDownloaded(int64_t data_length, |
| 24 int64_t encoded_data_length) { | 39 int64_t encoded_data_length) { |
| 25 has_data_downloaded_ = true; | 40 has_data_downloaded_ = true; |
| 26 download_data_length_ += data_length; | 41 download_data_length_ += data_length; |
| 27 encoded_download_data_length_ += encoded_data_length; | 42 encoded_download_data_length_ += encoded_data_length; |
| 28 if (quit_closure_for_on_data_downloaded_) | 43 if (quit_closure_for_on_data_downloaded_) |
| 29 quit_closure_for_on_data_downloaded_.Run(); | 44 quit_closure_for_on_data_downloaded_.Run(); |
| 30 } | 45 } |
| 31 | 46 |
| 32 void TestURLLoaderClient::OnStartLoadingResponseBody( | 47 void TestURLLoaderClient::OnStartLoadingResponseBody( |
| 33 mojo::ScopedDataPipeConsumerHandle body) { | 48 mojo::ScopedDataPipeConsumerHandle body) { |
| 34 response_body_ = std::move(body); | 49 response_body_ = std::move(body); |
| 35 if (quit_closure_for_on_start_loading_response_body_) | 50 if (quit_closure_for_on_start_loading_response_body_) |
| 36 quit_closure_for_on_start_loading_response_body_.Run(); | 51 quit_closure_for_on_start_loading_response_body_.Run(); |
| 37 } | 52 } |
| 38 | 53 |
| 39 void TestURLLoaderClient::OnComplete( | 54 void TestURLLoaderClient::OnComplete( |
| 40 const ResourceRequestCompletionStatus& status) { | 55 const ResourceRequestCompletionStatus& status) { |
| 41 has_received_completion_ = true; | 56 has_received_completion_ = true; |
| 42 completion_status_ = status; | 57 completion_status_ = status; |
| 43 if (quit_closure_for_on_complete_) | 58 if (quit_closure_for_on_complete_) |
| 44 quit_closure_for_on_complete_.Run(); | 59 quit_closure_for_on_complete_.Run(); |
| 45 } | 60 } |
| 46 | 61 |
| 62 void TestURLLoaderClient::ClearHasReceivedRedirect() { |
| 63 has_received_redirect_ = false; |
| 64 } |
| 65 |
| 47 mojom::URLLoaderClientAssociatedPtrInfo | 66 mojom::URLLoaderClientAssociatedPtrInfo |
| 48 TestURLLoaderClient::CreateRemoteAssociatedPtrInfo( | 67 TestURLLoaderClient::CreateRemoteAssociatedPtrInfo( |
| 49 mojo::AssociatedGroup* associated_group) { | 68 mojo::AssociatedGroup* associated_group) { |
| 50 mojom::URLLoaderClientAssociatedPtrInfo client_ptr_info; | 69 mojom::URLLoaderClientAssociatedPtrInfo client_ptr_info; |
| 51 binding_.Bind(&client_ptr_info, associated_group); | 70 binding_.Bind(&client_ptr_info, associated_group); |
| 52 return client_ptr_info; | 71 return client_ptr_info; |
| 53 } | 72 } |
| 54 | 73 |
| 55 void TestURLLoaderClient::Unbind() { | 74 void TestURLLoaderClient::Unbind() { |
| 56 binding_.Unbind(); | 75 binding_.Unbind(); |
| 57 response_body_.reset(); | 76 response_body_.reset(); |
| 58 } | 77 } |
| 59 | 78 |
| 60 void TestURLLoaderClient::RunUntilResponseReceived() { | 79 void TestURLLoaderClient::RunUntilResponseReceived() { |
| 61 base::RunLoop run_loop; | 80 base::RunLoop run_loop; |
| 62 quit_closure_for_on_received_response_ = run_loop.QuitClosure(); | 81 quit_closure_for_on_receive_response_ = run_loop.QuitClosure(); |
| 63 run_loop.Run(); | 82 run_loop.Run(); |
| 64 quit_closure_for_on_received_response_.Reset(); | 83 quit_closure_for_on_receive_response_.Reset(); |
| 84 } |
| 85 |
| 86 void TestURLLoaderClient::RunUntilRedirectReceived() { |
| 87 base::RunLoop run_loop; |
| 88 quit_closure_for_on_receive_redirect_ = run_loop.QuitClosure(); |
| 89 run_loop.Run(); |
| 90 quit_closure_for_on_receive_redirect_.Reset(); |
| 65 } | 91 } |
| 66 | 92 |
| 67 void TestURLLoaderClient::RunUntilDataDownloaded() { | 93 void TestURLLoaderClient::RunUntilDataDownloaded() { |
| 68 base::RunLoop run_loop; | 94 base::RunLoop run_loop; |
| 69 quit_closure_for_on_data_downloaded_ = run_loop.QuitClosure(); | 95 quit_closure_for_on_data_downloaded_ = run_loop.QuitClosure(); |
| 70 run_loop.Run(); | 96 run_loop.Run(); |
| 71 quit_closure_for_on_data_downloaded_.Reset(); | 97 quit_closure_for_on_data_downloaded_.Reset(); |
| 72 } | 98 } |
| 73 | 99 |
| 74 void TestURLLoaderClient::RunUntilResponseBodyArrived() { | 100 void TestURLLoaderClient::RunUntilResponseBodyArrived() { |
| 75 if (response_body_.is_valid()) | 101 if (response_body_.is_valid()) |
| 76 return; | 102 return; |
| 77 base::RunLoop run_loop; | 103 base::RunLoop run_loop; |
| 78 quit_closure_for_on_start_loading_response_body_ = run_loop.QuitClosure(); | 104 quit_closure_for_on_start_loading_response_body_ = run_loop.QuitClosure(); |
| 79 run_loop.Run(); | 105 run_loop.Run(); |
| 80 quit_closure_for_on_start_loading_response_body_.Reset(); | 106 quit_closure_for_on_start_loading_response_body_.Reset(); |
| 81 } | 107 } |
| 82 | 108 |
| 83 void TestURLLoaderClient::RunUntilComplete() { | 109 void TestURLLoaderClient::RunUntilComplete() { |
| 84 if (has_received_completion_) | 110 if (has_received_completion_) |
| 85 return; | 111 return; |
| 86 base::RunLoop run_loop; | 112 base::RunLoop run_loop; |
| 87 quit_closure_for_on_complete_ = run_loop.QuitClosure(); | 113 quit_closure_for_on_complete_ = run_loop.QuitClosure(); |
| 88 run_loop.Run(); | 114 run_loop.Run(); |
| 89 quit_closure_for_on_complete_.Reset(); | 115 quit_closure_for_on_complete_.Reset(); |
| 90 } | 116 } |
| 91 | 117 |
| 92 } // namespace content | 118 } // namespace content |
| OLD | NEW |