OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "webkit/support/weburl_loader_mock_factory.h" | 5 #include "webkit/support/weburl_loader_mock_factory.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | 9 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
10 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLError.
h" | 10 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLError.
h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 response_info.file_path = | 43 response_info.file_path = |
44 FilePath(std::wstring(file_path.data(), file_path.length())); | 44 FilePath(std::wstring(file_path.data(), file_path.length())); |
45 #endif | 45 #endif |
46 DCHECK(file_util::PathExists(response_info.file_path)); | 46 DCHECK(file_util::PathExists(response_info.file_path)); |
47 } | 47 } |
48 | 48 |
49 DCHECK(url_to_reponse_info_.find(url) == url_to_reponse_info_.end()); | 49 DCHECK(url_to_reponse_info_.find(url) == url_to_reponse_info_.end()); |
50 url_to_reponse_info_[url] = response_info; | 50 url_to_reponse_info_[url] = response_info; |
51 } | 51 } |
52 | 52 |
| 53 |
| 54 void WebURLLoaderMockFactory::RegisterErrorURL(const WebURL& url, |
| 55 const WebURLResponse& response, |
| 56 const WebURLError& error) { |
| 57 DCHECK(url_to_reponse_info_.find(url) == url_to_reponse_info_.end()); |
| 58 RegisterURL(url, response, WebString()); |
| 59 url_to_error_info_[url] = error; |
| 60 } |
| 61 |
53 void WebURLLoaderMockFactory::UnregisterURL(const WebKit::WebURL& url) { | 62 void WebURLLoaderMockFactory::UnregisterURL(const WebKit::WebURL& url) { |
54 URLToResponseMap::iterator iter = url_to_reponse_info_.find(url); | 63 URLToResponseMap::iterator iter = url_to_reponse_info_.find(url); |
55 DCHECK(iter != url_to_reponse_info_.end()); | 64 DCHECK(iter != url_to_reponse_info_.end()); |
56 url_to_reponse_info_.erase(iter); | 65 url_to_reponse_info_.erase(iter); |
| 66 |
| 67 URLToErrorMap::iterator error_iter = url_to_error_info_.find(url); |
| 68 if (error_iter != url_to_error_info_.end()) |
| 69 url_to_error_info_.erase(error_iter); |
57 } | 70 } |
58 | 71 |
59 void WebURLLoaderMockFactory::UnregisterAllURLs() { | 72 void WebURLLoaderMockFactory::UnregisterAllURLs() { |
60 url_to_reponse_info_.clear(); | 73 url_to_reponse_info_.clear(); |
| 74 url_to_error_info_.clear(); |
61 } | 75 } |
62 | 76 |
63 void WebURLLoaderMockFactory::ServeAsynchronousRequests() { | 77 void WebURLLoaderMockFactory::ServeAsynchronousRequests() { |
64 last_handled_asynchronous_request_.reset(); | 78 last_handled_asynchronous_request_.reset(); |
65 // Serving a request might trigger more requests, so we cannot iterate on | 79 // Serving a request might trigger more requests, so we cannot iterate on |
66 // pending_loaders_ as it might get modified. | 80 // pending_loaders_ as it might get modified. |
67 while (!pending_loaders_.empty()) { | 81 while (!pending_loaders_.empty()) { |
68 LoaderToRequestMap::iterator iter = pending_loaders_.begin(); | 82 LoaderToRequestMap::iterator iter = pending_loaders_.begin(); |
69 WebURLLoaderMock* loader = iter->first; | 83 WebURLLoaderMock* loader = iter->first; |
70 const WebURLRequest& request = iter->second; | 84 const WebURLRequest& request = iter->second; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 WebURLLoaderMock* loader) { | 136 WebURLLoaderMock* loader) { |
123 LoaderToRequestMap::iterator iter = pending_loaders_.find(loader); | 137 LoaderToRequestMap::iterator iter = pending_loaders_.find(loader); |
124 DCHECK(iter == pending_loaders_.end()); | 138 DCHECK(iter == pending_loaders_.end()); |
125 pending_loaders_[loader] = request; | 139 pending_loaders_[loader] = request; |
126 } | 140 } |
127 | 141 |
128 void WebURLLoaderMockFactory::LoadRequest(const WebURLRequest& request, | 142 void WebURLLoaderMockFactory::LoadRequest(const WebURLRequest& request, |
129 WebURLResponse* response, | 143 WebURLResponse* response, |
130 WebURLError* error, | 144 WebURLError* error, |
131 WebData* data) { | 145 WebData* data) { |
| 146 URLToErrorMap::const_iterator error_iter = |
| 147 url_to_error_info_.find(request.url()); |
| 148 if (error_iter != url_to_error_info_.end()) |
| 149 *error = error_iter->second; |
| 150 |
132 URLToResponseMap::const_iterator iter = | 151 URLToResponseMap::const_iterator iter = |
133 url_to_reponse_info_.find(request.url()); | 152 url_to_reponse_info_.find(request.url()); |
134 if (iter == url_to_reponse_info_.end()) { | 153 if (iter == url_to_reponse_info_.end()) { |
135 // Non mocked URLs should not have been passed to the default URLLoader. | 154 // Non mocked URLs should not have been passed to the default URLLoader. |
136 NOTREACHED(); | 155 NOTREACHED(); |
137 return; | 156 return; |
138 } | 157 } |
139 | 158 |
140 if (!ReadFile(iter->second.file_path, data)) { | 159 if (!error->reason && !ReadFile(iter->second.file_path, data)) { |
141 NOTREACHED(); | 160 NOTREACHED(); |
142 return; | 161 return; |
143 } | 162 } |
144 | 163 |
145 *response = iter->second.response; | 164 *response = iter->second.response; |
146 } | 165 } |
147 | 166 |
148 bool WebURLLoaderMockFactory::IsPending(WebURLLoaderMock* loader) { | 167 bool WebURLLoaderMockFactory::IsPending(WebURLLoaderMock* loader) { |
149 LoaderToRequestMap::iterator iter = pending_loaders_.find(loader); | 168 LoaderToRequestMap::iterator iter = pending_loaders_.find(loader); |
150 return iter != pending_loaders_.end(); | 169 return iter != pending_loaders_.end(); |
(...skipping 10 matching lines...) Expand all Loading... |
161 scoped_array<char> buffer(new char[size]); | 180 scoped_array<char> buffer(new char[size]); |
162 data->reset(); | 181 data->reset(); |
163 int read_count = file_util::ReadFile(file_path, buffer.get(), size); | 182 int read_count = file_util::ReadFile(file_path, buffer.get(), size); |
164 if (read_count == -1) | 183 if (read_count == -1) |
165 return false; | 184 return false; |
166 DCHECK(read_count == size); | 185 DCHECK(read_count == size); |
167 data->assign(buffer.get(), size); | 186 data->assign(buffer.get(), size); |
168 | 187 |
169 return true; | 188 return true; |
170 } | 189 } |
OLD | NEW |