summaryrefslogtreecommitdiffstats
path: root/chrome/browser/net
diff options
context:
space:
mode:
authorsky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-04-21 21:58:45 +0000
committersky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-04-21 21:58:45 +0000
commitb6c911a716816238f5155ef1522622d76d4665a2 (patch)
tree75fc5d30b0d421aafab8ae04737f4c867d26d917 /chrome/browser/net
parente23fced69f230fa3b1f8368722b7a308e8668077 (diff)
downloadchromium_src-b6c911a716816238f5155ef1522622d76d4665a2.zip
chromium_src-b6c911a716816238f5155ef1522622d76d4665a2.tar.gz
chromium_src-b6c911a716816238f5155ef1522622d76d4665a2.tar.bz2
Makes it possible to mock URLFetcher.
BUG=none TEST=none Review URL: http://codereview.chromium.org/87035 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@14141 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/net')
-rw-r--r--chrome/browser/net/test_url_fetcher_factory.cc29
-rw-r--r--chrome/browser/net/test_url_fetcher_factory.h78
-rw-r--r--chrome/browser/net/url_fetcher.cc18
-rw-r--r--chrome/browser/net/url_fetcher.h36
4 files changed, 157 insertions, 4 deletions
diff --git a/chrome/browser/net/test_url_fetcher_factory.cc b/chrome/browser/net/test_url_fetcher_factory.cc
new file mode 100644
index 0000000..b7bd523
--- /dev/null
+++ b/chrome/browser/net/test_url_fetcher_factory.cc
@@ -0,0 +1,29 @@
+// Copyright (c) 2009 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 "chrome/browser/net/test_url_fetcher_factory.h"
+
+TestURLFetcher::TestURLFetcher(const GURL& url,
+ URLFetcher::RequestType request_type,
+ URLFetcher::Delegate* d)
+ : URLFetcher(url, request_type, d),
+ original_url_(url) {
+}
+
+URLFetcher* TestURLFetcherFactory::CreateURLFetcher(
+ int id,
+ const GURL& url,
+ URLFetcher::RequestType request_type,
+ URLFetcher::Delegate* d) {
+ TestURLFetcher* fetcher = new TestURLFetcher(url, request_type, d);
+ fetchers_[id] = fetcher;
+ // URLFetcher's destructor requires the message loop.
+ fetcher->set_io_loop(MessageLoop::current());
+ return fetcher;
+}
+
+TestURLFetcher* TestURLFetcherFactory::GetFetcherByID(int id) const {
+ Fetchers::const_iterator i = fetchers_.find(id);
+ return i == fetchers_.end() ? NULL : i->second;
+}
diff --git a/chrome/browser/net/test_url_fetcher_factory.h b/chrome/browser/net/test_url_fetcher_factory.h
new file mode 100644
index 0000000..f6ff1ed
--- /dev/null
+++ b/chrome/browser/net/test_url_fetcher_factory.h
@@ -0,0 +1,78 @@
+// Copyright (c) 2009 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 CHROME_BROWSER_NET_TEST_URL_FETCHER_FACTORY_H_
+#define CHROME_BROWSER_NET_TEST_URL_FETCHER_FACTORY_H_
+
+#include <map>
+
+#include "chrome/browser/net/url_fetcher.h"
+#include "googleurl/src/gurl.h"
+
+// TestURLFetcher and TestURLFetcherFactory are used for testing consumers of
+// URLFetcher. TestURLFetcherFactory is a URLFetcher::Factory that creates
+// TestURLFetchers. TestURLFetcher::Start is overriden to do nothing. It is
+// expected that you'll grab the delegate from the TestURLFetcher and invoke
+// the callback method when appropriate. In this way it's easy to mock a
+// URLFetcher.
+// Typical usage:
+// // TestURLFetcher requires a MessageLoop:
+// MessageLoopForUI message_loop;
+// // Create and register factory.
+// TestURLFetcherFactory factory;
+// URLFetcher::SetFactory(&factory);
+// // Do something that triggers creation of a URLFetcher.
+// TestURLFetcher* fetcher = factory.GetFetcherByID(expected_id);
+// ASSERT(fetcher);
+// // Notify delegate with whatever data you want.
+// fetcher->delegate()->OnURLFetchComplete(...);
+// // Make sure consumer of URLFetcher does the right thing.
+// ...
+// // Reset factory.
+// URLFetcher::SetFactory(NULL);
+
+
+class TestURLFetcher : public URLFetcher {
+ public:
+ TestURLFetcher(const GURL& url, RequestType request_type, Delegate* d);
+
+ // Returns the delegate installed on the URLFetcher.
+ Delegate* delegate() const { return URLFetcher::delegate(); }
+
+ // Overriden to do nothing. It is assumed the caller will notify the delegate.
+ virtual void Start() {}
+
+ // URL we were created with. Because of how we're using URLFetcher url()
+ // always returns an empty URL. Chances are you'll want to use original_url()
+ // in your tests.
+ const GURL& original_url() const { return original_url_; }
+
+ private:
+ const GURL original_url_;
+
+ DISALLOW_COPY_AND_ASSIGN(TestURLFetcher);
+};
+
+// Simple URLFetcher::Factory method that creates TestURLFetchers. All fetchers
+// are registered in a map by the id passed to the create method.
+class TestURLFetcherFactory : public URLFetcher::Factory {
+ public:
+ TestURLFetcherFactory() {}
+
+ virtual URLFetcher* CreateURLFetcher(int id,
+ const GURL& url,
+ URLFetcher::RequestType request_type,
+ URLFetcher::Delegate* d);
+
+ TestURLFetcher* GetFetcherByID(int id) const;
+
+ private:
+ // Maps from id passed to create to the returned URLFetcher.
+ typedef std::map<int, TestURLFetcher*> Fetchers;
+ Fetchers fetchers_;
+
+ DISALLOW_COPY_AND_ASSIGN(TestURLFetcherFactory);
+};
+
+#endif // CHROME_TEST_TEST_URL_FETCHER_FACTORY_H_
diff --git a/chrome/browser/net/url_fetcher.cc b/chrome/browser/net/url_fetcher.cc
index dcec935..425cc73 100644
--- a/chrome/browser/net/url_fetcher.cc
+++ b/chrome/browser/net/url_fetcher.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Copyright (c) 2009 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.
@@ -49,6 +49,8 @@ class URLFetcher::Core
virtual void OnResponseStarted(URLRequest* request);
virtual void OnReadCompleted(URLRequest* request, int bytes_read);
+ URLFetcher::Delegate* delegate() const { return delegate_; }
+
private:
// Wrapper functions that allow us to ensure actions happen on the right
// thread.
@@ -93,6 +95,9 @@ class URLFetcher::Core
DISALLOW_COPY_AND_ASSIGN(Core);
};
+// static
+URLFetcher::Factory* URLFetcher::factory_ = NULL;
+
URLFetcher::URLFetcher(const GURL& url,
RequestType request_type,
Delegate* d)
@@ -104,6 +109,13 @@ URLFetcher::~URLFetcher() {
core_->Stop();
}
+// static
+URLFetcher* URLFetcher::Create(int id, const GURL& url,
+ RequestType request_type, Delegate* d) {
+ return factory_ ? factory_->CreateURLFetcher(id, url, request_type, d) :
+ new URLFetcher(url, request_type, d);
+}
+
URLFetcher::Core::Core(URLFetcher* fetcher,
const GURL& original_url,
RequestType request_type,
@@ -297,3 +309,7 @@ void URLFetcher::Start() {
const GURL& URLFetcher::url() const {
return core_->url_;
}
+
+URLFetcher::Delegate* URLFetcher::delegate() const {
+ return core_->delegate();
+}
diff --git a/chrome/browser/net/url_fetcher.h b/chrome/browser/net/url_fetcher.h
index 5485d7f..0ed4715 100644
--- a/chrome/browser/net/url_fetcher.h
+++ b/chrome/browser/net/url_fetcher.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Copyright (c) 2009 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.
//
@@ -76,12 +76,36 @@ class URLFetcher {
const std::string& data) = 0;
};
+ // URLFetcher::Create uses the currently registered Factory to create the
+ // URLFetcher. Factory is intended for testing.
+ class Factory {
+ public:
+ virtual URLFetcher* CreateURLFetcher(int id,
+ const GURL& url,
+ RequestType request_type,
+ Delegate* d) = 0;
+ };
+
// |url| is the URL to send the request to.
// |request_type| is the type of request to make.
// |d| the object that will receive the callback on fetch completion.
URLFetcher(const GURL& url, RequestType request_type, Delegate* d);
- ~URLFetcher();
+ virtual ~URLFetcher();
+
+ // Sets the factory used by the static method Create to create a URLFetcher.
+ // URLFetcher does not take ownership of |factory|. A value of NULL results
+ // in a URLFetcher being created directly.
+#if defined(UNIT_TEST)
+ static void set_factory(Factory* factory) { factory_ = factory; }
+#endif
+
+ // Creates a URLFetcher, ownership returns to the caller. If there is no
+ // Factory (the default) this creates and returns a new URLFetcher. See the
+ // constructor for a description of the args. |id| may be used during testing
+ // to identify who is creating the URLFetcher.
+ static URLFetcher* Create(int id, const GURL& url, RequestType request_type,
+ Delegate* d);
// This should only be used by unittests, where g_browser_process->io_thread()
// does not exist and we must specify an alternate loop. Unfortunately, we
@@ -115,11 +139,15 @@ class URLFetcher {
// Start the request. After this is called, you may not change any other
// settings.
- void Start();
+ virtual void Start();
// Return the URL that this fetcher is processing.
const GURL& url() const;
+ protected:
+ // Returns the delegate.
+ Delegate* delegate() const;
+
private:
// This class is the real guts of URLFetcher.
//
@@ -133,6 +161,8 @@ class URLFetcher {
scoped_refptr<Core> core_;
+ static Factory* factory_;
+
DISALLOW_EVIL_CONSTRUCTORS(URLFetcher);
};