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
3 // found in the LICENSE file.
5 #ifndef WEBKIT_SUPPORT_WEBURL_LOADER_MOCK_FACTORY_H_
6 #define WEBKIT_SUPPORT_WEBURL_LOADER_MOCK_FACTORY_H_
10 #include "base/files/file_path.h"
11 #include "third_party/WebKit/Source/Platform/chromium/public/WebURL.h"
12 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLRequest.h"
13 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLResponse.h"
21 class WebURLLoaderMock
;
23 // A factory that creates WebURLLoaderMock to simulate resource loading in
25 // You register files for specific URLs, the content of the file is then served
26 // when these URLs are loaded.
27 // In order to serve the asynchronous requests, you need to invoke
28 // ServeAsynchronousRequest.
29 class WebURLLoaderMockFactory
{
31 WebURLLoaderMockFactory();
32 virtual ~WebURLLoaderMockFactory();
34 // Called by TestWebKitPlatformSupport to create a WebURLLoader.
35 // Non-mocked request are forwarded to |default_loader| which should not be
37 virtual WebKit::WebURLLoader
* CreateURLLoader(
38 WebKit::WebURLLoader
* default_loader
);
40 // Registers a response and the contents to be served when the specified URL
42 void RegisterURL(const WebKit::WebURL
& url
,
43 const WebKit::WebURLResponse
& response
,
44 const WebKit::WebString
& filePath
);
46 // Registers an error to be served when the specified URL is requested.
47 void RegisterErrorURL(const WebKit::WebURL
& url
,
48 const WebKit::WebURLResponse
& response
,
49 const WebKit::WebURLError
& error
);
51 // Unregisters |url| so it will no longer be mocked.
52 void UnregisterURL(const WebKit::WebURL
& url
);
54 // Unregister all URLs so no URL will be mocked anymore.
55 void UnregisterAllURLs();
57 // Serves all the pending asynchronous requests.
58 void ServeAsynchronousRequests();
60 // Returns the last request handled by |ServeAsynchronousRequests()|.
61 WebKit::WebURLRequest
GetLastHandledAsynchronousRequest();
63 // Returns true if |url| was registered for being mocked.
64 bool IsMockedURL(const WebKit::WebURL
& url
);
66 // Called by the loader to load a resource.
67 void LoadSynchronously(const WebKit::WebURLRequest
& request
,
68 WebKit::WebURLResponse
* response
,
69 WebKit::WebURLError
* error
,
70 WebKit::WebData
* data
);
71 void LoadAsynchronouly(const WebKit::WebURLRequest
& request
,
72 WebURLLoaderMock
* loader
);
74 // Removes the loader from the list of pending loaders.
75 void CancelLoad(WebURLLoaderMock
* loader
);
80 // Loads the specified request and populates the response, error and data
82 void LoadRequest(const WebKit::WebURLRequest
& request
,
83 WebKit::WebURLResponse
* response
,
84 WebKit::WebURLError
* error
,
85 WebKit::WebData
* data
);
87 // Checks if the loader is pending. Otherwise, it may have been deleted.
88 bool IsPending(WebURLLoaderMock
* loader
);
90 // Reads |m_filePath| and puts its content in |data|.
91 // Returns true if it successfully read the file.
92 static bool ReadFile(const base::FilePath
& file_path
, WebKit::WebData
* data
);
94 // The loaders that have not being served data yet.
95 typedef std::map
<WebURLLoaderMock
*, WebKit::WebURLRequest
> LoaderToRequestMap
;
96 LoaderToRequestMap pending_loaders_
;
98 typedef std::map
<WebKit::WebURL
, WebKit::WebURLError
> URLToErrorMap
;
99 URLToErrorMap url_to_error_info_
;
101 // Table of the registered URLs and the responses that they should receive.
102 typedef std::map
<WebKit::WebURL
, ResponseInfo
> URLToResponseMap
;
103 URLToResponseMap url_to_reponse_info_
;
105 WebKit::WebURLRequest last_handled_asynchronous_request_
;
107 DISALLOW_COPY_AND_ASSIGN(WebURLLoaderMockFactory
);
110 #endif // WEBKIT_SUPPORT_WEBURL_LOADER_MOCK_FACTORY_H_