| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/loader/test_url_loader_client.h" | |
| 6 | |
| 7 #include "base/memory/ref_counted.h" | |
| 8 #include "base/run_loop.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 | |
| 11 namespace content { | |
| 12 | |
| 13 TestURLLoaderClient::TestURLLoaderClient() : binding_(this) {} | |
| 14 TestURLLoaderClient::~TestURLLoaderClient() {} | |
| 15 | |
| 16 void TestURLLoaderClient::OnReceiveResponse( | |
| 17 const ResourceResponseHead& response_head, | |
| 18 const base::Optional<net::SSLInfo>& ssl_info, | |
| 19 mojom::DownloadedTempFilePtr downloaded_file) { | |
| 20 EXPECT_FALSE(has_received_response_); | |
| 21 EXPECT_FALSE(has_received_cached_metadata_); | |
| 22 EXPECT_FALSE(has_received_completion_); | |
| 23 has_received_response_ = true; | |
| 24 response_head_ = response_head; | |
| 25 ssl_info_ = ssl_info; | |
| 26 if (quit_closure_for_on_receive_response_) | |
| 27 quit_closure_for_on_receive_response_.Run(); | |
| 28 binding_.set_connection_error_handler(base::Bind( | |
| 29 &TestURLLoaderClient::OnConnectionError, base::Unretained(this))); | |
| 30 } | |
| 31 | |
| 32 void TestURLLoaderClient::OnReceiveRedirect( | |
| 33 const net::RedirectInfo& redirect_info, | |
| 34 const ResourceResponseHead& response_head) { | |
| 35 EXPECT_FALSE(has_received_cached_metadata_); | |
| 36 EXPECT_FALSE(response_body_.is_valid()); | |
| 37 EXPECT_FALSE(has_received_response_); | |
| 38 // Use ClearHasReceivedRedirect to accept more redirects. | |
| 39 EXPECT_FALSE(has_received_redirect_); | |
| 40 EXPECT_FALSE(has_received_completion_); | |
| 41 has_received_redirect_ = true; | |
| 42 redirect_info_ = redirect_info; | |
| 43 response_head_ = response_head; | |
| 44 if (quit_closure_for_on_receive_redirect_) | |
| 45 quit_closure_for_on_receive_redirect_.Run(); | |
| 46 } | |
| 47 | |
| 48 void TestURLLoaderClient::OnDataDownloaded(int64_t data_length, | |
| 49 int64_t encoded_data_length) { | |
| 50 EXPECT_TRUE(has_received_response_); | |
| 51 EXPECT_FALSE(has_received_completion_); | |
| 52 has_data_downloaded_ = true; | |
| 53 download_data_length_ += data_length; | |
| 54 encoded_download_data_length_ += encoded_data_length; | |
| 55 if (quit_closure_for_on_data_downloaded_) | |
| 56 quit_closure_for_on_data_downloaded_.Run(); | |
| 57 } | |
| 58 | |
| 59 void TestURLLoaderClient::OnReceiveCachedMetadata( | |
| 60 const std::vector<uint8_t>& data) { | |
| 61 EXPECT_FALSE(has_received_cached_metadata_); | |
| 62 EXPECT_TRUE(has_received_response_); | |
| 63 EXPECT_FALSE(has_received_completion_); | |
| 64 has_received_cached_metadata_ = true; | |
| 65 cached_metadata_ = | |
| 66 std::string(reinterpret_cast<const char*>(data.data()), data.size()); | |
| 67 if (quit_closure_for_on_receive_cached_metadata_) | |
| 68 quit_closure_for_on_receive_cached_metadata_.Run(); | |
| 69 } | |
| 70 | |
| 71 void TestURLLoaderClient::OnTransferSizeUpdated(int32_t transfer_size_diff) { | |
| 72 EXPECT_TRUE(has_received_response_); | |
| 73 EXPECT_FALSE(has_received_completion_); | |
| 74 EXPECT_GT(transfer_size_diff, 0); | |
| 75 body_transfer_size_ += transfer_size_diff; | |
| 76 } | |
| 77 | |
| 78 void TestURLLoaderClient::OnUploadProgress( | |
| 79 int64_t current_position, | |
| 80 int64_t total_size, | |
| 81 OnUploadProgressCallback ack_callback) { | |
| 82 EXPECT_TRUE(ack_callback); | |
| 83 EXPECT_FALSE(has_received_response_); | |
| 84 EXPECT_FALSE(has_received_completion_); | |
| 85 EXPECT_LT(0, current_position); | |
| 86 EXPECT_LE(current_position, total_size); | |
| 87 | |
| 88 has_received_upload_progress_ = true; | |
| 89 current_upload_position_ = current_position; | |
| 90 total_upload_size_ = total_size; | |
| 91 std::move(ack_callback).Run(); | |
| 92 } | |
| 93 | |
| 94 void TestURLLoaderClient::OnStartLoadingResponseBody( | |
| 95 mojo::ScopedDataPipeConsumerHandle body) { | |
| 96 EXPECT_TRUE(has_received_response_); | |
| 97 EXPECT_FALSE(has_received_completion_); | |
| 98 response_body_ = std::move(body); | |
| 99 if (quit_closure_for_on_start_loading_response_body_) | |
| 100 quit_closure_for_on_start_loading_response_body_.Run(); | |
| 101 } | |
| 102 | |
| 103 void TestURLLoaderClient::OnComplete( | |
| 104 const ResourceRequestCompletionStatus& status) { | |
| 105 EXPECT_FALSE(has_received_completion_); | |
| 106 has_received_completion_ = true; | |
| 107 completion_status_ = status; | |
| 108 if (quit_closure_for_on_complete_) | |
| 109 quit_closure_for_on_complete_.Run(); | |
| 110 } | |
| 111 | |
| 112 void TestURLLoaderClient::ClearHasReceivedRedirect() { | |
| 113 has_received_redirect_ = false; | |
| 114 } | |
| 115 | |
| 116 mojom::URLLoaderClientPtr TestURLLoaderClient::CreateInterfacePtr() { | |
| 117 mojom::URLLoaderClientPtr client_ptr; | |
| 118 binding_.Bind(mojo::MakeRequest(&client_ptr)); | |
| 119 return client_ptr; | |
| 120 } | |
| 121 | |
| 122 void TestURLLoaderClient::Unbind() { | |
| 123 binding_.Unbind(); | |
| 124 response_body_.reset(); | |
| 125 } | |
| 126 | |
| 127 void TestURLLoaderClient::RunUntilResponseReceived() { | |
| 128 if (has_received_response_) | |
| 129 return; | |
| 130 base::RunLoop run_loop; | |
| 131 quit_closure_for_on_receive_response_ = run_loop.QuitClosure(); | |
| 132 run_loop.Run(); | |
| 133 quit_closure_for_on_receive_response_.Reset(); | |
| 134 } | |
| 135 | |
| 136 void TestURLLoaderClient::RunUntilRedirectReceived() { | |
| 137 if (has_received_redirect_) | |
| 138 return; | |
| 139 base::RunLoop run_loop; | |
| 140 quit_closure_for_on_receive_redirect_ = run_loop.QuitClosure(); | |
| 141 run_loop.Run(); | |
| 142 quit_closure_for_on_receive_redirect_.Reset(); | |
| 143 } | |
| 144 | |
| 145 void TestURLLoaderClient::RunUntilDataDownloaded() { | |
| 146 if (has_data_downloaded_) | |
| 147 return; | |
| 148 base::RunLoop run_loop; | |
| 149 quit_closure_for_on_data_downloaded_ = run_loop.QuitClosure(); | |
| 150 run_loop.Run(); | |
| 151 quit_closure_for_on_data_downloaded_.Reset(); | |
| 152 } | |
| 153 | |
| 154 void TestURLLoaderClient::RunUntilCachedMetadataReceived() { | |
| 155 if (has_received_cached_metadata_) | |
| 156 return; | |
| 157 base::RunLoop run_loop; | |
| 158 quit_closure_for_on_receive_cached_metadata_ = run_loop.QuitClosure(); | |
| 159 run_loop.Run(); | |
| 160 quit_closure_for_on_receive_cached_metadata_.Reset(); | |
| 161 } | |
| 162 | |
| 163 void TestURLLoaderClient::RunUntilResponseBodyArrived() { | |
| 164 if (response_body_.is_valid()) | |
| 165 return; | |
| 166 base::RunLoop run_loop; | |
| 167 quit_closure_for_on_start_loading_response_body_ = run_loop.QuitClosure(); | |
| 168 run_loop.Run(); | |
| 169 quit_closure_for_on_start_loading_response_body_.Reset(); | |
| 170 } | |
| 171 | |
| 172 void TestURLLoaderClient::RunUntilComplete() { | |
| 173 if (has_received_completion_) | |
| 174 return; | |
| 175 base::RunLoop run_loop; | |
| 176 quit_closure_for_on_complete_ = run_loop.QuitClosure(); | |
| 177 run_loop.Run(); | |
| 178 quit_closure_for_on_complete_.Reset(); | |
| 179 } | |
| 180 | |
| 181 void TestURLLoaderClient::RunUntilConnectionError() { | |
| 182 if (has_received_connection_error_) | |
| 183 return; | |
| 184 base::RunLoop run_loop; | |
| 185 quit_closure_for_on_connection_error_ = run_loop.QuitClosure(); | |
| 186 run_loop.Run(); | |
| 187 quit_closure_for_on_connection_error_.Reset(); | |
| 188 } | |
| 189 | |
| 190 void TestURLLoaderClient::OnConnectionError() { | |
| 191 has_received_connection_error_ = true; | |
| 192 if (quit_closure_for_on_connection_error_) | |
| 193 quit_closure_for_on_connection_error_.Run(); | |
| 194 } | |
| 195 | |
| 196 } // namespace content | |
| OLD | NEW |