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