summaryrefslogtreecommitdiffstats
path: root/webkit
diff options
context:
space:
mode:
authorjcivelli@google.com <jcivelli@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2010-06-21 20:54:09 +0000
committerjcivelli@google.com <jcivelli@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2010-06-21 20:54:09 +0000
commit0fbd522d848313c647d2301990ea18973d887b69 (patch)
tree1cd783a8dacbfbf640661196edb6c92a6e7c16dd /webkit
parenta74f123ad667cade1aa366bd423f6b68c99a2a78 (diff)
downloadchromium_src-0fbd522d848313c647d2301990ea18973d887b69.zip
chromium_src-0fbd522d848313c647d2301990ea18973d887b69.tar.gz
chromium_src-0fbd522d848313c647d2301990ea18973d887b69.tar.bz2
Adding a way to mock WebURLLoader in webkit_support.
This was originally in a CL in Webkit (https://bugs.webkit.org/show_bug.cgi?id=39456) but seems to make more sense in webkit_support. BUG=None TEST=None (the above CL makes use of this). Review URL: http://codereview.chromium.org/2749020 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@50382 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit')
-rw-r--r--webkit/support/test_webkit_client.cc8
-rw-r--r--webkit/support/test_webkit_client.h22
-rw-r--r--webkit/support/webkit_support.cc46
-rw-r--r--webkit/support/webkit_support.gypi4
-rw-r--r--webkit/support/webkit_support.h29
-rw-r--r--webkit/support/weburl_loader_mock.cc73
-rw-r--r--webkit/support/weburl_loader_mock.h57
-rw-r--r--webkit/support/weburl_loader_mock_factory.cc136
-rw-r--r--webkit/support/weburl_loader_mock_factory.h99
9 files changed, 440 insertions, 34 deletions
diff --git a/webkit/support/test_webkit_client.cc b/webkit/support/test_webkit_client.cc
index 389a188..0959383 100644
--- a/webkit/support/test_webkit_client.cc
+++ b/webkit/support/test_webkit_client.cc
@@ -34,6 +34,7 @@
#include "webkit/glue/webkit_glue.h"
#include "webkit/glue/webkitclient_impl.h"
#include "webkit/support/test_webkit_client.h"
+#include "webkit/support/weburl_loader_mock_factory.h"
#include "webkit/tools/test_shell/mock_webclipboard_impl.h"
#include "webkit/tools/test_shell/simple_appcache_system.h"
#include "webkit/tools/test_shell/simple_database_system.h"
@@ -51,7 +52,7 @@
using WebKit::WebScriptController;
-TestWebKitClient::TestWebKitClient() : url_loader_factory_(NULL) {
+TestWebKitClient::TestWebKitClient() {
v8::V8::SetCounterFunction(StatsTable::FindLocation);
WebKit::initialize(this);
@@ -193,9 +194,8 @@ void TestWebKitClient::prefetchHostName(const WebKit::WebString&) {
}
WebKit::WebURLLoader* TestWebKitClient::createURLLoader() {
- if (url_loader_factory_)
- return url_loader_factory_->createURLLoader();
- return webkit_glue::WebKitClientImpl::createURLLoader();
+ return url_loader_factory_.CreateURLLoader(
+ webkit_glue::WebKitClientImpl::createURLLoader());
}
WebKit::WebData TestWebKitClient::loadResource(const char* name) {
diff --git a/webkit/support/test_webkit_client.h b/webkit/support/test_webkit_client.h
index 7a2a052..c1415ab 100644
--- a/webkit/support/test_webkit_client.h
+++ b/webkit/support/test_webkit_client.h
@@ -7,20 +7,13 @@
#include "webkit/glue/webfilesystem_impl.h"
#include "webkit/glue/webkitclient_impl.h"
+#include "webkit/support/weburl_loader_mock_factory.h"
#include "webkit/tools/test_shell/mock_webclipboard_impl.h"
#include "webkit/tools/test_shell/simple_appcache_system.h"
#include "webkit/tools/test_shell/simple_database_system.h"
#include "webkit/tools/test_shell/simple_webcookiejar_impl.h"
#include "webkit/tools/test_shell/test_shell_webmimeregistry_impl.h"
-class WebURLLoaderFactory {
- public:
- virtual WebKit::WebURLLoader* createURLLoader() = 0;
-
- protected:
- virtual ~WebURLLoaderFactory() {}
-};
-
// An implementation of WebKitClient for tests.
class TestWebKitClient : public webkit_glue::WebKitClientImpl {
public:
@@ -65,11 +58,8 @@ class TestWebKitClient : public webkit_glue::WebKitClientImpl {
virtual WebKit::WebSharedWorkerRepository* sharedWorkerRepository();
virtual WebKit::WebGraphicsContext3D* createGraphicsContext3D();
- // Sets the factory used to create WebURLLoader instances.
- // The caller owns the WebURLLoaderFactory and is responsible for calling this
- // method again with NULL when it's done.
- void set_url_loader_factory(WebURLLoaderFactory* url_loader_factory) {
- url_loader_factory_ = url_loader_factory;
+ WebURLLoaderMockFactory* url_loader_factory() {
+ return &url_loader_factory_;
}
private:
@@ -80,11 +70,7 @@ class TestWebKitClient : public webkit_glue::WebKitClientImpl {
SimpleAppCacheSystem appcache_system_;
SimpleDatabaseSystem database_system_;
SimpleWebCookieJarImpl cookie_jar_;
-
- // Used to create WebURLLoader.
- // If NULL, the class defers to webkit_glue::WebKitClientImpl for creating the
- // WebURLLoader.
- WebURLLoaderFactory* url_loader_factory_;
+ WebURLLoaderMockFactory url_loader_factory_;
#if defined(OS_WIN)
WebKit::WebThemeEngine* active_theme_engine_;
diff --git a/webkit/support/webkit_support.cc b/webkit/support/webkit_support.cc
index f0e984a..a98811d 100644
--- a/webkit/support/webkit_support.cc
+++ b/webkit/support/webkit_support.cc
@@ -59,7 +59,7 @@ class TestEnvironment {
SimpleResourceLoaderBridge::Shutdown();
}
- WebKit::WebKitClient* webkit_client() { return webkit_client_.get(); }
+ TestWebKitClient* webkit_client() { return webkit_client_.get(); }
#if defined(OS_WIN)
void set_theme_engine(WebKit::WebThemeEngine* engine) {
@@ -112,11 +112,7 @@ namespace webkit_support {
static TestEnvironment* test_environment;
-void SetUpTestEnvironment() {
- SetUpTestEnvironment(false);
-}
-
-void SetUpTestEnvironment(bool unit_test_mode) {
+static void SetUpTestEnvironmentImpl(bool unit_test_mode) {
base::EnableTerminationOnHeapCorruption();
// Initialize the singleton CommandLine with fixed values. Some code refer to
@@ -130,9 +126,9 @@ void SetUpTestEnvironment(bool unit_test_mode) {
const char* kFixedArguments[] = {"DumpRenderTree"};
CommandLine::Init(arraysize(kFixedArguments), kFixedArguments);
- BeforeInitialize();
- test_environment = new TestEnvironment(unit_test_mode);
- AfterInitialize();
+ webkit_support::BeforeInitialize();
+ webkit_support::test_environment = new TestEnvironment(unit_test_mode);
+ webkit_support::AfterInitialize();
if (!unit_test_mode) {
// Load ICU data tables. This has to run after TestEnvironment is created
// because on Linux, we need base::AtExitManager.
@@ -140,6 +136,18 @@ void SetUpTestEnvironment(bool unit_test_mode) {
}
}
+void SetUpTestEnvironment(bool unit_test_mode) {
+ SetUpTestEnvironment();
+}
+
+void SetUpTestEnvironment() {
+ SetUpTestEnvironmentImpl(false);
+}
+
+void SetUpTestEnvironmentForUnitTests() {
+ SetUpTestEnvironmentImpl(true);
+}
+
void TearDownTestEnvironment() {
// Flush any remaining messages before we kill ourselves.
// http://code.google.com/p/chromium/issues/detail?id=9500
@@ -218,6 +226,26 @@ WebKit::WebString GetWebKitRootDir() {
return WebKit::WebString::fromUTF8(WideToUTF8(path.ToWStringHack()).c_str());
}
+void RegisterMockedURL(const WebKit::WebURL& url,
+ const WebKit::WebURLResponse& response,
+ const WebKit::WebString& file_path) {
+ test_environment->webkit_client()->url_loader_factory()->
+ RegisterURL(url, response, file_path);
+}
+
+void UnregisterMockedURL(const WebKit::WebURL& url) {
+ test_environment->webkit_client()->url_loader_factory()->UnregisterURL(url);
+}
+
+void UnregisterAllMockedURLs() {
+ test_environment->webkit_client()->url_loader_factory()->UnregisterAllURLs();
+}
+
+void ServeAsynchronousMockedRequests() {
+ test_environment->webkit_client()->url_loader_factory()->
+ ServeAsynchronousRequests();
+}
+
// Wrapper for debug_util
bool BeingDebugged() {
return DebugUtil::BeingDebugged();
diff --git a/webkit/support/webkit_support.gypi b/webkit/support/webkit_support.gypi
index e39840c..da9313b 100644
--- a/webkit/support/webkit_support.gypi
+++ b/webkit/support/webkit_support.gypi
@@ -31,6 +31,10 @@
'webkit_support.cc',
'webkit_support.h',
'webkit_support_glue.cc',
+ 'weburl_loader_mock.cc',
+ 'weburl_loader_mock.h',
+ 'weburl_loader_mock_factory.cc',
+ 'weburl_loader_mock_factory.h',
# TODO(tkent): Move the following files to here.
'<(DEPTH)/webkit/tools/test_shell/mac/DumpRenderTreePasteboard.h',
'<(DEPTH)/webkit/tools/test_shell/mac/DumpRenderTreePasteboard.m',
diff --git a/webkit/support/webkit_support.h b/webkit/support/webkit_support.h
index c6dfc35..694f77b1 100644
--- a/webkit/support/webkit_support.h
+++ b/webkit/support/webkit_support.h
@@ -10,6 +10,7 @@
#include "base/basictypes.h"
class Task;
+class WebURLLoaderMockFactory;
namespace WebKit {
class WebApplicationCacheHost;
class WebApplicationCacheHostClient;
@@ -21,6 +22,7 @@ class WebPlugin;
class WebString;
class WebThemeEngine;
class WebURL;
+class WebURLResponse;
struct WebPluginParams;
}
@@ -35,11 +37,17 @@ namespace webkit_support {
// |unit_test_mode| should be set to true when running in a TestSuite, in which
// case no AtExitManager is created and ICU is not initialized (as it is already
// done by the TestSuite).
-// SetUpTestEnvironment() calls WebKit::initialize().
+// SetUpTestEnvironment() and SetUpTestEnvironmentForUnitTests() calls
+// WebKit::initialize().
// TearDownTestEnvironment() calls WebKit::shutdown().
-// TODO(jcivelli): remove the next method once DumpRenderTree.cpp is not using
-// it anymore upstream.
+// SetUpTestEnvironmentForUnitTests() should be used when running in a
+// TestSuite, in which case no AtExitManager is created and ICU is not
+// initialized (as it is already done by the TestSuite).
void SetUpTestEnvironment();
+void SetUpTestEnvironmentForUnitTests();
+// TODO(jcivelli): the method below is deprecated and should be removed when
+// DumpRenderTree has been modified to use the version with no
+// parameter.
void SetUpTestEnvironment(bool unit_test_mode);
void TearDownTestEnvironment();
@@ -63,6 +71,21 @@ WebKit::WebApplicationCacheHost* CreateApplicationCacheHost(
// Returns the root directory of the WebKit code.
WebKit::WebString GetWebKitRootDir();
+// ------- URL load mocking.
+// Registers the file at |file_path| to be served when |url| is requested.
+// |response| is the response provided with the contents.
+void RegisterMockedURL(const WebKit::WebURL& url,
+ const WebKit::WebURLResponse& response,
+ const WebKit::WebString& file_path);
+
+// Unregisters URLs so they are no longer mocked.
+void UnregisterMockedURL(const WebKit::WebURL& url);
+void UnregisterAllMockedURLs();
+
+// Causes all pending asynchronous requests to be served. When this method
+// returns all the pending requests have been processed.
+void ServeAsynchronousMockedRequests();
+
// Wrappers to minimize dependecy.
// -------- Debugging
diff --git a/webkit/support/weburl_loader_mock.cc b/webkit/support/weburl_loader_mock.cc
new file mode 100644
index 0000000..18f2387
--- /dev/null
+++ b/webkit/support/weburl_loader_mock.cc
@@ -0,0 +1,73 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "webkit/support/weburl_loader_mock.h"
+
+#include "third_party/WebKit/WebKit/chromium/public/WebData.h"
+#include "third_party/WebKit/WebKit/chromium/public/WebURLLoaderClient.h"
+#include "webkit/support/weburl_loader_mock_factory.h"
+
+WebURLLoaderMock::WebURLLoaderMock(WebURLLoaderMockFactory* factory,
+ WebKit::WebURLLoader* default_loader)
+ : factory_(factory),
+ client_(NULL),
+ default_loader_(default_loader),
+ using_default_loader_(false) {
+}
+
+WebURLLoaderMock::~WebURLLoaderMock() {
+}
+
+void WebURLLoaderMock::ServeAsynchronousRequest(
+ const WebKit::WebURLResponse& response,
+ const WebKit::WebData& data,
+ const WebKit::WebURLError& error) {
+ DCHECK(!using_default_loader_);
+ if (!client_)
+ return;
+
+ client_->didReceiveResponse(this, response);
+ client_->didReceiveData(this, data.data(), data.size());
+ client_->didFinishLoading(this);
+}
+
+void WebURLLoaderMock::loadSynchronously(const WebKit::WebURLRequest& request,
+ WebKit::WebURLResponse& response,
+ WebKit::WebURLError& error,
+ WebKit::WebData& data) {
+ if (factory_->IsMockedURL(request.url())) {
+ factory_->LoadSynchronously(request, &response, &error, &data);
+ return;
+ }
+ using_default_loader_ = true;
+ default_loader_->loadSynchronously(request, response, error, data);
+}
+
+void WebURLLoaderMock::loadAsynchronously(const WebKit::WebURLRequest& request,
+ WebKit::WebURLLoaderClient* client) {
+ if (factory_->IsMockedURL(request.url())) {
+ client_ = client;
+ factory_->LoadAsynchronouly(request, this);
+ return;
+ }
+ using_default_loader_ = true;
+ default_loader_->loadAsynchronously(request, client);
+}
+
+void WebURLLoaderMock::cancel() {
+ if (using_default_loader_) {
+ default_loader_->cancel();
+ return;
+ }
+ client_ = NULL;
+ factory_->CancelLoad(this);
+}
+
+void WebURLLoaderMock::setDefersLoading(bool deferred) {
+ if (using_default_loader_) {
+ default_loader_->setDefersLoading(deferred);
+ return;
+ }
+ NOTIMPLEMENTED();
+}
diff --git a/webkit/support/weburl_loader_mock.h b/webkit/support/weburl_loader_mock.h
new file mode 100644
index 0000000..d0a690f
--- /dev/null
+++ b/webkit/support/weburl_loader_mock.h
@@ -0,0 +1,57 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef WEBKIT_SUPPORT_WEBURL_LOADER_MOCK_H_
+#define WEBKIT_SUPPORT_WEBURL_LOADER_MOCK_H_
+
+#include "base/logging.h"
+#include "base/scoped_ptr.h"
+#include "third_party/WebKit/WebKit/chromium/public/WebURLLoader.h"
+
+namespace WebKit {
+class WebData;
+struct WebURLError;
+class WebURLLoaderClient;
+class WebURLRequest;
+class WebURLResponse;
+}
+
+class WebURLLoaderMockFactory;
+
+// A simple class for mocking WebURLLoader.
+// If the WebURLLoaderMockFactory it is associated with has been configured to
+// mock the request it gets, it serves the mocked resource. Otherwise it just
+// forwards it to the default loader.
+class WebURLLoaderMock : public WebKit::WebURLLoader {
+ public:
+ // This object becomes the owner of |default_loader|.
+ WebURLLoaderMock(WebURLLoaderMockFactory* factory,
+ WebKit::WebURLLoader* default_loader);
+ virtual ~WebURLLoaderMock();
+
+ // Simulates the asynchronous request being served.
+ void ServeAsynchronousRequest(const WebKit::WebURLResponse& response,
+ const WebKit::WebData& data,
+ const WebKit::WebURLError& error);
+
+ // WebURLLoader methods:
+ virtual void loadSynchronously(const WebKit::WebURLRequest& request,
+ WebKit::WebURLResponse& response,
+ WebKit::WebURLError& error,
+ WebKit::WebData& data);
+ virtual void loadAsynchronously(const WebKit::WebURLRequest& request,
+ WebKit::WebURLLoaderClient* client);
+ virtual void cancel();
+ virtual void setDefersLoading(bool defer);
+
+ private:
+ WebURLLoaderMockFactory* factory_;
+ WebKit::WebURLLoaderClient* client_;
+ scoped_ptr<WebKit::WebURLLoader> default_loader_;
+ bool using_default_loader_;
+
+ DISALLOW_COPY_AND_ASSIGN(WebURLLoaderMock);
+};
+
+#endif // WEBKIT_SUPPORT_WEBURL_LOADER_MOCK_H_
diff --git a/webkit/support/weburl_loader_mock_factory.cc b/webkit/support/weburl_loader_mock_factory.cc
new file mode 100644
index 0000000..4f553cf
--- /dev/null
+++ b/webkit/support/weburl_loader_mock_factory.cc
@@ -0,0 +1,136 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "webkit/support/weburl_loader_mock_factory.h"
+
+#include "base/file_util.h"
+#include "third_party/WebKit/WebKit/chromium/public/WebString.h"
+#include "third_party/WebKit/WebKit/chromium/public/WebURLError.h"
+#include "third_party/WebKit/WebKit/chromium/public/WebURLRequest.h"
+#include "third_party/WebKit/WebKit/chromium/public/WebURLResponse.h"
+#include "webkit/support/weburl_loader_mock.h"
+
+using WebKit::WebData;
+using WebKit::WebString;
+using WebKit::WebURL;
+using WebKit::WebURLError;
+using WebKit::WebURLLoader;
+using WebKit::WebURLRequest;
+using WebKit::WebURLResponse;
+
+void WebURLLoaderMockFactory::RegisterURL(const WebURL& url,
+ const WebURLResponse& response,
+ const WebString& file_path) {
+ ResponseInfo response_info;
+ response_info.response = response;
+ if (!file_path.isNull() && !file_path.isEmpty()) {
+#if defined(OS_POSIX)
+ // TODO(jcivelli): On Linux, UTF8 might not be correct.
+ response_info.file_path =
+ FilePath(static_cast<std::string>(file_path.utf8()));
+#elif defined(OS_WIN)
+ response_info.file_path =
+ FilePath(std::wstring(file_path.data(), file_path.length()));
+#endif
+ DCHECK(file_util::PathExists(response_info.file_path));
+ }
+
+ DCHECK(url_to_reponse_info_.find(url) == url_to_reponse_info_.end());
+ url_to_reponse_info_[url] = response_info;
+}
+
+void WebURLLoaderMockFactory::UnregisterURL(const WebKit::WebURL& url) {
+ URLToResponseMap::iterator iter = url_to_reponse_info_.find(url);
+ DCHECK(iter != url_to_reponse_info_.end());
+ url_to_reponse_info_.erase(iter);
+}
+
+void WebURLLoaderMockFactory::UnregisterAllURLs() {
+ url_to_reponse_info_.clear();
+}
+
+void WebURLLoaderMockFactory::ServeAsynchronousRequests() {
+ // Serving a request might trigger more requests, so we cannot iterate on
+ // pending_loaders_ as it might get modified.
+ while (!pending_loaders_.empty()) {
+ LoaderToRequestMap::iterator iter = pending_loaders_.begin();
+ WebURLLoaderMock* loader = iter->first;
+ const WebURLRequest& request = iter->second;
+ WebURLResponse response;
+ WebURLError error;
+ WebData data;
+ LoadRequest(request, &response, &error, &data);
+ loader->ServeAsynchronousRequest(response, data, error);
+ pending_loaders_.erase(iter);
+ }
+}
+
+bool WebURLLoaderMockFactory::IsMockedURL(const WebKit::WebURL& url) {
+ return url_to_reponse_info_.find(url) != url_to_reponse_info_.end();
+}
+
+void WebURLLoaderMockFactory::CancelLoad(WebURLLoaderMock* loader) {
+ LoaderToRequestMap::iterator iter = pending_loaders_.find(loader);
+ DCHECK(iter != pending_loaders_.end());
+ pending_loaders_.erase(iter);
+}
+
+WebURLLoader* WebURLLoaderMockFactory::CreateURLLoader(
+ WebURLLoader* default_loader) {
+ DCHECK(default_loader);
+ return new WebURLLoaderMock(this, default_loader);
+}
+
+void WebURLLoaderMockFactory::LoadSynchronously(const WebURLRequest& request,
+ WebURLResponse* response,
+ WebURLError* error,
+ WebData* data) {
+ LoadRequest(request, response, error, data);
+}
+
+void WebURLLoaderMockFactory::LoadAsynchronouly(const WebURLRequest& request,
+ WebURLLoaderMock* loader) {
+ LoaderToRequestMap::iterator iter = pending_loaders_.find(loader);
+ DCHECK(iter == pending_loaders_.end());
+ pending_loaders_[loader] = request;
+}
+
+void WebURLLoaderMockFactory::LoadRequest(const WebURLRequest& request,
+ WebURLResponse* response,
+ WebURLError* error,
+ WebData* data) {
+ URLToResponseMap::const_iterator iter =
+ url_to_reponse_info_.find(request.url());
+ if (iter == url_to_reponse_info_.end()) {
+ // Non mocked URLs should not have been passed to the default URLLoader.
+ NOTREACHED();
+ return;
+ }
+
+ if (!ReadFile(iter->second.file_path, data)) {
+ NOTREACHED();
+ return;
+ }
+
+ *response = iter->second.response;
+}
+
+// static
+bool WebURLLoaderMockFactory::ReadFile(const FilePath& file_path,
+ WebData* data) {
+ int64 file_size = 0;
+ if (!file_util::GetFileSize(file_path, &file_size))
+ return false;
+
+ int size = static_cast<int>(file_size);
+ scoped_array<char> buffer(new char[size]);
+ data->reset();
+ int read_count = file_util::ReadFile(file_path, buffer.get(), size);
+ if (read_count == -1)
+ return false;
+ DCHECK(read_count == size);
+ data->assign(buffer.get(), size);
+
+ return true;
+}
diff --git a/webkit/support/weburl_loader_mock_factory.h b/webkit/support/weburl_loader_mock_factory.h
new file mode 100644
index 0000000..5400339
--- /dev/null
+++ b/webkit/support/weburl_loader_mock_factory.h
@@ -0,0 +1,99 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef WEBKIT_SUPPORT_WEBURL_LOADER_MOCK_FACTORY_H_
+#define WEBKIT_SUPPORT_WEBURL_LOADER_MOCK_FACTORY_H_
+
+#include <map>
+#include <vector>
+
+#include "base/file_path.h"
+#include "third_party/WebKit/WebKit/chromium/public/WebURL.h"
+#include "third_party/WebKit/WebKit/chromium/public/WebURLRequest.h"
+#include "third_party/WebKit/WebKit/chromium/public/WebURLResponse.h"
+
+namespace WebKit {
+class WebData;
+struct WebURLError;
+class WebURLLoader;
+}
+
+class WebURLLoaderMock;
+
+// A factory that creates WebURLLoaderMock to simulate resource loading in
+// tests.
+// You register files for specific URLs, the content of the file is then served
+// when these URLs are loaded.
+// In order to serve the asynchronous requests, you need to invoke
+// ServeAsynchronousRequest.
+class WebURLLoaderMockFactory {
+ public:
+ WebURLLoaderMockFactory() {}
+ virtual ~WebURLLoaderMockFactory() {}
+
+ // Called by TestWebKitClient to create a WebURLLoader.
+ // Non-mocked request are forwarded to |default_loader| which should not be
+ // NULL.
+ virtual WebKit::WebURLLoader* CreateURLLoader(
+ WebKit::WebURLLoader* default_loader);
+
+ // Registers a response and the contents to be served when the specified URL
+ // is loaded.
+ void RegisterURL(const WebKit::WebURL& url,
+ const WebKit::WebURLResponse& response,
+ const WebKit::WebString& filePath);
+
+ // Unregisters |url| so it will no longer be mocked.
+ void UnregisterURL(const WebKit::WebURL& url);
+
+ // Unregister all URLs so no URL will be mocked anymore.
+ void UnregisterAllURLs();
+
+ // Serves all the pending asynchronous requests.
+ void ServeAsynchronousRequests();
+
+ // Returns true if |url| was registered for being mocked.
+ bool IsMockedURL(const WebKit::WebURL& url);
+
+ // Called by the loader to load a resource.
+ void LoadSynchronously(const WebKit::WebURLRequest& request,
+ WebKit::WebURLResponse* response,
+ WebKit::WebURLError* error,
+ WebKit::WebData* data);
+ void LoadAsynchronouly(const WebKit::WebURLRequest& request,
+ WebURLLoaderMock* loader);
+
+ // Removes the loader from the list of pending loaders.
+ void CancelLoad(WebURLLoaderMock* loader);
+
+ private:
+ struct ResponseInfo {
+ WebKit::WebURLResponse response;
+ FilePath file_path;
+ };
+
+ // Loads the specified request and populates the response, error and data
+ // accordingly.
+ void LoadRequest(const WebKit::WebURLRequest& request,
+ WebKit::WebURLResponse* response,
+ WebKit::WebURLError* error,
+ WebKit::WebData* data);
+
+ // Reads |m_filePath| and puts its content in |data|.
+ // Returns true if it successfully read the file.
+ static bool ReadFile(const FilePath& file_path, WebKit::WebData* data);
+
+ // The loaders that have not being served data yet.
+ typedef std::map<WebURLLoaderMock*, WebKit::WebURLRequest> LoaderToRequestMap;
+ LoaderToRequestMap pending_loaders_;
+
+ // Table of the registered URLs and the responses that they should receive.
+ typedef std::map<WebKit::WebURL, ResponseInfo> URLToResponseMap;
+ URLToResponseMap url_to_reponse_info_;
+
+ DISALLOW_COPY_AND_ASSIGN(WebURLLoaderMockFactory);
+};
+
+#endif // WEBKIT_SUPPORT_WEBURL_LOADER_MOCK_FACTORY_H_
+