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 "platform/testing/weburl_loader_mock_factory_impl.h" | 5 #include "platform/testing/weburl_loader_mock_factory_impl.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 void WebURLLoaderMockFactoryImpl::UnregisterURL(const blink::WebURL& url) { | 65 void WebURLLoaderMockFactoryImpl::UnregisterURL(const blink::WebURL& url) { |
66 URLToResponseMap::iterator iter = url_to_response_info_.find(url); | 66 URLToResponseMap::iterator iter = url_to_response_info_.find(url); |
67 DCHECK(iter != url_to_response_info_.end()); | 67 DCHECK(iter != url_to_response_info_.end()); |
68 url_to_response_info_.erase(iter); | 68 url_to_response_info_.erase(iter); |
69 | 69 |
70 URLToErrorMap::iterator error_iter = url_to_error_info_.find(url); | 70 URLToErrorMap::iterator error_iter = url_to_error_info_.find(url); |
71 if (error_iter != url_to_error_info_.end()) | 71 if (error_iter != url_to_error_info_.end()) |
72 url_to_error_info_.erase(error_iter); | 72 url_to_error_info_.erase(error_iter); |
73 } | 73 } |
74 | 74 |
| 75 void WebURLLoaderMockFactoryImpl::RegisterURLProtocol( |
| 76 const WebString& protocol, |
| 77 const WebURLResponse& response, |
| 78 const WebString& file_path) { |
| 79 DCHECK(protocol.ContainsOnlyASCII()); |
| 80 |
| 81 ResponseInfo response_info; |
| 82 response_info.response = response; |
| 83 if (!file_path.IsNull() && !file_path.IsEmpty()) { |
| 84 response_info.file_path = blink::WebStringToFilePath(file_path); |
| 85 DCHECK(base::PathExists(response_info.file_path)) |
| 86 << response_info.file_path.MaybeAsASCII() << " does not exist."; |
| 87 } |
| 88 |
| 89 DCHECK(protocol_to_response_info_.find(protocol) == |
| 90 protocol_to_response_info_.end()); |
| 91 protocol_to_response_info_.Set(protocol, response_info); |
| 92 } |
| 93 |
| 94 void WebURLLoaderMockFactoryImpl::UnregisterURLProtocol( |
| 95 const WebString& protocol) { |
| 96 ProtocolToResponseMap::iterator iter = |
| 97 protocol_to_response_info_.find(protocol); |
| 98 DCHECK(iter != protocol_to_response_info_.end()); |
| 99 protocol_to_response_info_.erase(iter); |
| 100 } |
| 101 |
75 void WebURLLoaderMockFactoryImpl::UnregisterAllURLsAndClearMemoryCache() { | 102 void WebURLLoaderMockFactoryImpl::UnregisterAllURLsAndClearMemoryCache() { |
76 url_to_response_info_.clear(); | 103 url_to_response_info_.clear(); |
77 url_to_error_info_.clear(); | 104 url_to_error_info_.clear(); |
| 105 protocol_to_response_info_.clear(); |
78 GetMemoryCache()->EvictResources(); | 106 GetMemoryCache()->EvictResources(); |
79 } | 107 } |
80 | 108 |
81 void WebURLLoaderMockFactoryImpl::ServeAsynchronousRequests() { | 109 void WebURLLoaderMockFactoryImpl::ServeAsynchronousRequests() { |
82 // Serving a request might trigger more requests, so we cannot iterate on | 110 // Serving a request might trigger more requests, so we cannot iterate on |
83 // pending_loaders_ as it might get modified. | 111 // pending_loaders_ as it might get modified. |
84 while (!pending_loaders_.IsEmpty()) { | 112 while (!pending_loaders_.IsEmpty()) { |
85 LoaderToRequestMap::iterator iter = pending_loaders_.begin(); | 113 LoaderToRequestMap::iterator iter = pending_loaders_.begin(); |
86 WeakPtr<WebURLLoaderMock> loader(iter->key->GetWeakPtr()); | 114 WeakPtr<WebURLLoaderMock> loader(iter->key->GetWeakPtr()); |
87 const WebURLRequest request = iter->value; | 115 const WebURLRequest request = iter->value; |
(...skipping 15 matching lines...) Expand all Loading... |
103 // Serve the request if the loader is still active. | 131 // Serve the request if the loader is still active. |
104 if (loader && !loader->is_cancelled() && !loader->is_deferred()) { | 132 if (loader && !loader->is_cancelled() && !loader->is_deferred()) { |
105 loader->ServeAsynchronousRequest(delegate_, response, data, error); | 133 loader->ServeAsynchronousRequest(delegate_, response, data, error); |
106 RunUntilIdle(); | 134 RunUntilIdle(); |
107 } | 135 } |
108 } | 136 } |
109 RunUntilIdle(); | 137 RunUntilIdle(); |
110 } | 138 } |
111 | 139 |
112 bool WebURLLoaderMockFactoryImpl::IsMockedURL(const blink::WebURL& url) { | 140 bool WebURLLoaderMockFactoryImpl::IsMockedURL(const blink::WebURL& url) { |
113 return url_to_response_info_.find(url) != url_to_response_info_.end(); | 141 WebURLError error; |
| 142 ResponseInfo response_info; |
| 143 return LookupURL(url, &error, &response_info); |
114 } | 144 } |
115 | 145 |
116 void WebURLLoaderMockFactoryImpl::CancelLoad(WebURLLoaderMock* loader) { | 146 void WebURLLoaderMockFactoryImpl::CancelLoad(WebURLLoaderMock* loader) { |
117 pending_loaders_.erase(loader); | 147 pending_loaders_.erase(loader); |
118 } | 148 } |
119 | 149 |
120 void WebURLLoaderMockFactoryImpl::LoadSynchronously( | 150 void WebURLLoaderMockFactoryImpl::LoadSynchronously( |
121 const WebURLRequest& request, | 151 const WebURLRequest& request, |
122 WebURLResponse* response, | 152 WebURLResponse* response, |
123 WebURLError* error, | 153 WebURLError* error, |
(...skipping 14 matching lines...) Expand all Loading... |
138 if (platform_) | 168 if (platform_) |
139 platform_->RunUntilIdle(); | 169 platform_->RunUntilIdle(); |
140 else | 170 else |
141 base::RunLoop().RunUntilIdle(); | 171 base::RunLoop().RunUntilIdle(); |
142 } | 172 } |
143 | 173 |
144 void WebURLLoaderMockFactoryImpl::LoadRequest(const WebURLRequest& request, | 174 void WebURLLoaderMockFactoryImpl::LoadRequest(const WebURLRequest& request, |
145 WebURLResponse* response, | 175 WebURLResponse* response, |
146 WebURLError* error, | 176 WebURLError* error, |
147 WebData* data) { | 177 WebData* data) { |
148 URLToErrorMap::const_iterator error_iter = | 178 ResponseInfo response_info; |
149 url_to_error_info_.find(request.Url()); | 179 if (!LookupURL(request.Url(), error, &response_info)) { |
150 if (error_iter != url_to_error_info_.end()) | |
151 *error = error_iter->value; | |
152 | |
153 URLToResponseMap::const_iterator iter = | |
154 url_to_response_info_.find(request.Url()); | |
155 if (iter == url_to_response_info_.end()) { | |
156 // Non mocked URLs should not have been passed to the default URLLoader. | 180 // Non mocked URLs should not have been passed to the default URLLoader. |
157 NOTREACHED(); | 181 NOTREACHED(); |
158 return; | 182 return; |
159 } | 183 } |
160 | 184 |
161 if (!error->reason && !ReadFile(iter->value.file_path, data)) { | 185 if (!error->reason && !ReadFile(response_info.file_path, data)) { |
162 NOTREACHED(); | 186 NOTREACHED(); |
163 return; | 187 return; |
164 } | 188 } |
165 | 189 |
166 *response = iter->value.response; | 190 *response = response_info.response; |
| 191 } |
| 192 |
| 193 bool WebURLLoaderMockFactoryImpl::LookupURL(const WebURL& url, |
| 194 WebURLError* error, |
| 195 ResponseInfo* response_info) { |
| 196 URLToErrorMap::const_iterator error_iter = url_to_error_info_.find(url); |
| 197 if (error_iter != url_to_error_info_.end()) |
| 198 *error = error_iter->value; |
| 199 |
| 200 URLToResponseMap::const_iterator iter = url_to_response_info_.find(url); |
| 201 if (iter != url_to_response_info_.end()) { |
| 202 *response_info = iter->value; |
| 203 return true; |
| 204 } |
| 205 |
| 206 for (const auto& key_value_pair : protocol_to_response_info_) { |
| 207 String protocol = key_value_pair.key; |
| 208 if (url.ProtocolIs(protocol.Ascii().data())) { |
| 209 *response_info = key_value_pair.value; |
| 210 return true; |
| 211 } |
| 212 } |
| 213 |
| 214 return false; |
167 } | 215 } |
168 | 216 |
169 // static | 217 // static |
170 bool WebURLLoaderMockFactoryImpl::ReadFile(const base::FilePath& file_path, | 218 bool WebURLLoaderMockFactoryImpl::ReadFile(const base::FilePath& file_path, |
171 WebData* data) { | 219 WebData* data) { |
172 // If the path is empty then we return an empty file so tests can simulate | 220 // If the path is empty then we return an empty file so tests can simulate |
173 // requests without needing to actually load files. | 221 // requests without needing to actually load files. |
174 if (file_path.empty()) | 222 if (file_path.empty()) |
175 return true; | 223 return true; |
176 | 224 |
177 std::string buffer; | 225 std::string buffer; |
178 if (!base::ReadFileToString(file_path, &buffer)) | 226 if (!base::ReadFileToString(file_path, &buffer)) |
179 return false; | 227 return false; |
180 | 228 |
181 data->Assign(buffer.data(), buffer.size()); | 229 data->Assign(buffer.data(), buffer.size()); |
182 return true; | 230 return true; |
183 } | 231 } |
184 | 232 |
185 } // namespace blink | 233 } // namespace blink |
OLD | NEW |