OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 #ifndef WEBKIT_SUPPORT_WEBURL_LOADER_MOCK_FACTORY_H_ |
| 6 #define WEBKIT_SUPPORT_WEBURL_LOADER_MOCK_FACTORY_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/file_path.h" |
| 12 #include "third_party/WebKit/WebKit/chromium/public/WebURL.h" |
| 13 #include "third_party/WebKit/WebKit/chromium/public/WebURLRequest.h" |
| 14 #include "third_party/WebKit/WebKit/chromium/public/WebURLResponse.h" |
| 15 |
| 16 namespace WebKit { |
| 17 class WebData; |
| 18 struct WebURLError; |
| 19 class WebURLLoader; |
| 20 } |
| 21 |
| 22 class WebURLLoaderMock; |
| 23 |
| 24 // A factory that creates WebURLLoaderMock to simulate resource loading in |
| 25 // tests. |
| 26 // You register files for specific URLs, the content of the file is then served |
| 27 // when these URLs are loaded. |
| 28 // In order to serve the asynchronous requests, you need to invoke |
| 29 // ServeAsynchronousRequest. |
| 30 class WebURLLoaderMockFactory { |
| 31 public: |
| 32 WebURLLoaderMockFactory() {} |
| 33 virtual ~WebURLLoaderMockFactory() {} |
| 34 |
| 35 // Called by TestWebKitClient to create a WebURLLoader. |
| 36 // Non-mocked request are forwarded to |default_loader| which should not be |
| 37 // NULL. |
| 38 virtual WebKit::WebURLLoader* CreateURLLoader( |
| 39 WebKit::WebURLLoader* default_loader); |
| 40 |
| 41 // Registers a response and the contents to be served when the specified URL |
| 42 // is loaded. |
| 43 void RegisterURL(const WebKit::WebURL& url, |
| 44 const WebKit::WebURLResponse& response, |
| 45 const WebKit::WebString& filePath); |
| 46 |
| 47 // Unregisters |url| so it will no longer be mocked. |
| 48 void UnregisterURL(const WebKit::WebURL& url); |
| 49 |
| 50 // Unregister all URLs so no URL will be mocked anymore. |
| 51 void UnregisterAllURLs(); |
| 52 |
| 53 // Serves all the pending asynchronous requests. |
| 54 void ServeAsynchronousRequests(); |
| 55 |
| 56 // Returns true if |url| was registered for being mocked. |
| 57 bool IsMockedURL(const WebKit::WebURL& url); |
| 58 |
| 59 // Called by the loader to load a resource. |
| 60 void LoadSynchronously(const WebKit::WebURLRequest& request, |
| 61 WebKit::WebURLResponse* response, |
| 62 WebKit::WebURLError* error, |
| 63 WebKit::WebData* data); |
| 64 void LoadAsynchronouly(const WebKit::WebURLRequest& request, |
| 65 WebURLLoaderMock* loader); |
| 66 |
| 67 // Removes the loader from the list of pending loaders. |
| 68 void CancelLoad(WebURLLoaderMock* loader); |
| 69 |
| 70 private: |
| 71 struct ResponseInfo { |
| 72 WebKit::WebURLResponse response; |
| 73 FilePath file_path; |
| 74 }; |
| 75 |
| 76 // Loads the specified request and populates the response, error and data |
| 77 // accordingly. |
| 78 void LoadRequest(const WebKit::WebURLRequest& request, |
| 79 WebKit::WebURLResponse* response, |
| 80 WebKit::WebURLError* error, |
| 81 WebKit::WebData* data); |
| 82 |
| 83 // Reads |m_filePath| and puts its content in |data|. |
| 84 // Returns true if it successfully read the file. |
| 85 static bool ReadFile(const FilePath& file_path, WebKit::WebData* data); |
| 86 |
| 87 // The loaders that have not being served data yet. |
| 88 typedef std::map<WebURLLoaderMock*, WebKit::WebURLRequest> LoaderToRequestMap; |
| 89 LoaderToRequestMap pending_loaders_; |
| 90 |
| 91 // Table of the registered URLs and the responses that they should receive. |
| 92 typedef std::map<WebKit::WebURL, ResponseInfo> URLToResponseMap; |
| 93 URLToResponseMap url_to_reponse_info_; |
| 94 |
| 95 DISALLOW_COPY_AND_ASSIGN(WebURLLoaderMockFactory); |
| 96 }; |
| 97 |
| 98 #endif // WEBKIT_SUPPORT_WEBURL_LOADER_MOCK_FACTORY_H_ |
| 99 |
OLD | NEW |