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
3 // found in the LICENSE file.
5 #ifndef CONTENT_TEST_WEBURL_LOADER_MOCK_FACTORY_H_
6 #define CONTENT_TEST_WEBURL_LOADER_MOCK_FACTORY_H_
10 #include "base/files/file_path.h"
11 #include "third_party/WebKit/public/platform/WebURL.h"
12 #include "third_party/WebKit/public/platform/WebURLError.h"
13 #include "third_party/WebKit/public/platform/WebURLRequest.h"
14 #include "third_party/WebKit/public/platform/WebURLResponse.h"
19 class WebURLLoaderTestDelegate
;
22 class WebURLLoaderMock
;
24 // A factory that creates WebURLLoaderMock to simulate resource loading in
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
{
32 WebURLLoaderMockFactory();
33 virtual ~WebURLLoaderMockFactory();
35 // Called by TestBlinkWebUnitTestSupport to create a WebURLLoader.
36 // Non-mocked request are forwarded to |default_loader| which should not be
38 virtual blink::WebURLLoader
* CreateURLLoader(
39 blink::WebURLLoader
* default_loader
);
41 // Registers a response and the contents to be served when the specified URL
43 void RegisterURL(const blink::WebURL
& url
,
44 const blink::WebURLResponse
& response
,
45 const blink::WebString
& filePath
);
47 // Registers an error to be served when the specified URL is requested.
48 void RegisterErrorURL(const blink::WebURL
& url
,
49 const blink::WebURLResponse
& response
,
50 const blink::WebURLError
& error
);
52 // Unregisters |url| so it will no longer be mocked.
53 void UnregisterURL(const blink::WebURL
& url
);
55 // Unregister all URLs so no URL will be mocked anymore.
56 void UnregisterAllURLs();
58 // Serves all the pending asynchronous requests.
59 void ServeAsynchronousRequests();
61 // Returns the last request handled by |ServeAsynchronousRequests()|.
62 blink::WebURLRequest
GetLastHandledAsynchronousRequest();
64 // Returns true if |url| was registered for being mocked.
65 bool IsMockedURL(const blink::WebURL
& url
);
67 // Called by the loader to load a resource.
68 void LoadSynchronously(const blink::WebURLRequest
& request
,
69 blink::WebURLResponse
* response
,
70 blink::WebURLError
* error
,
71 blink::WebData
* data
);
72 void LoadAsynchronouly(const blink::WebURLRequest
& request
,
73 WebURLLoaderMock
* loader
);
75 // Removes the loader from the list of pending loaders.
76 void CancelLoad(WebURLLoaderMock
* loader
);
78 void set_delegate(blink::WebURLLoaderTestDelegate
* delegate
) {
84 blink::WebURLResponse response
;
85 base::FilePath file_path
;
89 // Loads the specified request and populates the response, error and data
91 void LoadRequest(const blink::WebURLRequest
& request
,
92 blink::WebURLResponse
* response
,
93 blink::WebURLError
* error
,
94 blink::WebData
* data
);
96 // Checks if the loader is pending. Otherwise, it may have been deleted.
97 bool IsPending(WebURLLoaderMock
* loader
);
99 // Reads |m_filePath| and puts its content in |data|.
100 // Returns true if it successfully read the file.
101 static bool ReadFile(const base::FilePath
& file_path
, blink::WebData
* data
);
103 blink::WebURLLoaderTestDelegate
* delegate_
;
105 // The loaders that have not being served data yet.
106 typedef std::map
<WebURLLoaderMock
*, blink::WebURLRequest
> LoaderToRequestMap
;
107 LoaderToRequestMap pending_loaders_
;
109 typedef std::map
<GURL
, blink::WebURLError
> URLToErrorMap
;
110 URLToErrorMap url_to_error_info_
;
112 // Table of the registered URLs and the responses that they should receive.
113 typedef std::map
<GURL
, ResponseInfo
> URLToResponseMap
;
114 URLToResponseMap url_to_reponse_info_
;
116 DISALLOW_COPY_AND_ASSIGN(WebURLLoaderMockFactory
);
119 #endif // CONTENT_TEST_WEBURL_LOADER_MOCK_FACTORY_H_