#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/compiler_specific.h"
+#include "base/logging.h"
+#include "base/memory/scoped_ptr.h"
#include "base/pickle.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/thread.h"
+#include "content/browser/appcache/appcache_response.h"
+#include "content/browser/appcache/appcache_url_request_job.h"
#include "content/browser/appcache/mock_appcache_service.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_error_job.h"
+#include "net/url_request/url_request_job_factory.h"
#include "testing/gtest/include/gtest/gtest.h"
-#include "webkit/browser/appcache/appcache_response.h"
-#include "webkit/browser/appcache/appcache_url_request_job.h"
-
-using appcache::AppCacheEntry;
-using appcache::AppCacheStorage;
-using appcache::AppCacheResponseInfo;
-using appcache::AppCacheResponseReader;
-using appcache::AppCacheResponseWriter;
-using appcache::AppCacheURLRequestJob;
-using appcache::HttpResponseInfoIOBuffer;
-using appcache::kNoCacheId;
+#include "url/gurl.h"
+
using net::IOBuffer;
using net::WrappedIOBuffer;
namespace content {
-static const char kHttpBasicHeaders[] =
- "HTTP/1.0 200 OK\0Content-Length: 5\0\0";
-static const char kHttpBasicBody[] = "Hello";
+namespace {
+
+const char kHttpBasicHeaders[] = "HTTP/1.0 200 OK\0Content-Length: 5\0\0";
+const char kHttpBasicBody[] = "Hello";
+
+const int kNumBlocks = 4;
+const int kBlockSize = 1024;
+
+class MockURLRequestJobFactory : public net::URLRequestJobFactory {
+ public:
+ MockURLRequestJobFactory() : job_(NULL) {
+ }
+
+ virtual ~MockURLRequestJobFactory() {
+ DCHECK(!job_);
+ }
+
+ void SetJob(net::URLRequestJob* job) {
+ job_ = job;
+ }
-static const int kNumBlocks = 4;
-static const int kBlockSize = 1024;
+ bool has_job() const {
+ return job_ != NULL;
+ }
+
+ // net::URLRequestJobFactory implementation.
+ virtual net::URLRequestJob* MaybeCreateJobWithProtocolHandler(
+ const std::string& scheme,
+ net::URLRequest* request,
+ net::NetworkDelegate* network_delegate) const OVERRIDE {
+ if (job_) {
+ net::URLRequestJob* temp = job_;
+ job_ = NULL;
+ return temp;
+ } else {
+ return new net::URLRequestErrorJob(request,
+ network_delegate,
+ net::ERR_INTERNET_DISCONNECTED);
+ }
+ }
+
+ virtual bool IsHandledProtocol(const std::string& scheme) const OVERRIDE {
+ return scheme == "http";
+ };
+
+ virtual bool IsHandledURL(const GURL& url) const OVERRIDE {
+ return url.SchemeIs("http");
+ }
+
+ virtual bool IsSafeRedirectTarget(const GURL& location) const OVERRIDE {
+ return false;
+ }
+
+ private:
+ mutable net::URLRequestJob* job_;
+};
class AppCacheURLRequestJobTest : public testing::Test {
public:
bool kill_with_io_pending_;
};
- static net::URLRequestJob* MockHttpJobFactory(
- net::URLRequest* request,
- net::NetworkDelegate* network_delegate,
- const std::string& scheme) {
- if (mock_factory_job_) {
- net::URLRequestJob* temp = mock_factory_job_;
- mock_factory_job_ = NULL;
- return temp;
- } else {
- return new net::URLRequestErrorJob(request,
- network_delegate,
- net::ERR_INTERNET_DISCONNECTED);
- }
- }
-
// Helper callback to run a test on our io_thread. The io_thread is spun up
// once and reused for all tests.
template <class Method>
void SetUpTest() {
DCHECK(base::MessageLoop::current() == io_thread_->message_loop());
DCHECK(task_stack_.empty());
- orig_http_factory_ = net::URLRequest::Deprecated::RegisterProtocolFactory(
- "http", MockHttpJobFactory);
- url_request_delegate_.reset(new MockURLRequestDelegate(this));
+
storage_delegate_.reset(new MockStorageDelegate(this));
service_.reset(new MockAppCacheService());
expected_read_result_ = 0;
written_response_id_ = 0;
reader_deletion_count_down_ = 0;
writer_deletion_count_down_ = 0;
+
+ url_request_delegate_.reset(new MockURLRequestDelegate(this));
+ job_factory_.reset(new MockURLRequestJobFactory());
+ empty_context_.reset(new net::URLRequestContext());
+ empty_context_->set_job_factory(job_factory_.get());
}
void TearDownTest() {
DCHECK(base::MessageLoop::current() == io_thread_->message_loop());
- net::URLRequest::Deprecated::RegisterProtocolFactory("http",
- orig_http_factory_);
- orig_http_factory_ = NULL;
request_.reset();
- url_request_delegate_.reset();
- DCHECK(!mock_factory_job_);
while (!task_stack_.empty())
task_stack_.pop();
write_info_buffer_ = NULL;
storage_delegate_.reset();
service_.reset();
+
+ DCHECK(!job_factory_->has_job());
+ empty_context_.reset();
+ job_factory_.reset();
+ url_request_delegate_.reset();
}
void TestFinished() {
// Basic -------------------------------------------------------------------
void Basic() {
AppCacheStorage* storage = service_->storage();
- net::URLRequest request(
- GURL("http://blah/"), net::DEFAULT_PRIORITY, NULL, &empty_context_);
+ scoped_ptr<net::URLRequest> request(empty_context_->CreateRequest(
+ GURL("http://blah/"), net::DEFAULT_PRIORITY, NULL, NULL));
scoped_refptr<AppCacheURLRequestJob> job;
// Create an instance and see that it looks as expected.
- job = new AppCacheURLRequestJob(
- &request, NULL, storage, NULL, false);
+ job = new AppCacheURLRequestJob(request.get(), NULL, storage, NULL, false);
EXPECT_TRUE(job->is_waiting());
EXPECT_FALSE(job->is_delivering_appcache_response());
EXPECT_FALSE(job->is_delivering_network_response());
EXPECT_FALSE(job->has_been_started());
EXPECT_FALSE(job->has_been_killed());
EXPECT_EQ(GURL(), job->manifest_url());
- EXPECT_EQ(kNoCacheId, job->cache_id());
+ EXPECT_EQ(kAppCacheNoCacheId, job->cache_id());
EXPECT_FALSE(job->entry().has_response_id());
TestFinished();
// DeliveryOrders -----------------------------------------------------
void DeliveryOrders() {
AppCacheStorage* storage = service_->storage();
- net::URLRequest request(
- GURL("http://blah/"), net::DEFAULT_PRIORITY, NULL, &empty_context_);
+ scoped_ptr<net::URLRequest> request(empty_context_->CreateRequest(
+ GURL("http://blah/"), net::DEFAULT_PRIORITY, NULL, NULL));
scoped_refptr<AppCacheURLRequestJob> job;
// Create an instance, give it a delivery order and see that
// it looks as expected.
- job = new AppCacheURLRequestJob(&request, NULL, storage, NULL, false);
+ job = new AppCacheURLRequestJob(request.get(), NULL, storage, NULL, false);
job->DeliverErrorResponse();
EXPECT_TRUE(job->is_delivering_error_response());
EXPECT_FALSE(job->has_been_started());
- job = new AppCacheURLRequestJob(&request, NULL, storage, NULL, false);
+ job = new AppCacheURLRequestJob(request.get(), NULL, storage, NULL, false);
job->DeliverNetworkResponse();
EXPECT_TRUE(job->is_delivering_network_response());
EXPECT_FALSE(job->has_been_started());
- job = new AppCacheURLRequestJob(&request, NULL, storage, NULL, false);
+ job = new AppCacheURLRequestJob(request.get(), NULL, storage, NULL, false);
const GURL kManifestUrl("http://blah/");
const int64 kCacheId(1);
const int64 kGroupId(1);
base::Unretained(this)));
AppCacheStorage* storage = service_->storage();
- request_ = empty_context_.CreateRequest(GURL("http://blah/"),
- net::DEFAULT_PRIORITY,
- url_request_delegate_.get(),
- NULL);
+ request_ = empty_context_->CreateRequest(GURL("http://blah/"),
+ net::DEFAULT_PRIORITY,
+ url_request_delegate_.get(),
+ NULL);
// Setup to create an AppCacheURLRequestJob with orders to deliver
// a network response.
- mock_factory_job_ = new AppCacheURLRequestJob(
+ AppCacheURLRequestJob* mock_job = new AppCacheURLRequestJob(
request_.get(), NULL, storage, NULL, false);
- mock_factory_job_->DeliverNetworkResponse();
- EXPECT_TRUE(mock_factory_job_->is_delivering_network_response());
- EXPECT_FALSE(mock_factory_job_->has_been_started());
+ job_factory_->SetJob(mock_job);
+ mock_job->DeliverNetworkResponse();
+ EXPECT_TRUE(mock_job->is_delivering_network_response());
+ EXPECT_FALSE(mock_job->has_been_started());
// Start the request.
request_->Start();
// The job should have been picked up.
- EXPECT_FALSE(mock_factory_job_);
+ EXPECT_FALSE(job_factory_->has_job());
// Completion is async.
}
base::Unretained(this)));
AppCacheStorage* storage = service_->storage();
- request_ = empty_context_.CreateRequest(GURL("http://blah/"),
- net::DEFAULT_PRIORITY,
- url_request_delegate_.get(),
- NULL);
+ request_ = empty_context_->CreateRequest(GURL("http://blah/"),
+ net::DEFAULT_PRIORITY,
+ url_request_delegate_.get(),
+ NULL);
// Setup to create an AppCacheURLRequestJob with orders to deliver
// a network response.
- mock_factory_job_ = new AppCacheURLRequestJob(
+ AppCacheURLRequestJob* mock_job = new AppCacheURLRequestJob(
request_.get(), NULL, storage, NULL, false);
- mock_factory_job_->DeliverErrorResponse();
- EXPECT_TRUE(mock_factory_job_->is_delivering_error_response());
- EXPECT_FALSE(mock_factory_job_->has_been_started());
+ job_factory_->SetJob(mock_job);
+ mock_job->DeliverErrorResponse();
+ EXPECT_TRUE(mock_job->is_delivering_error_response());
+ EXPECT_FALSE(mock_job->has_been_started());
// Start the request.
request_->Start();
// The job should have been picked up.
- EXPECT_FALSE(mock_factory_job_);
+ EXPECT_FALSE(job_factory_->has_job());
// Completion is async.
}
void RequestAppCachedResource(bool start_after_delivery_orders) {
AppCacheStorage* storage = service_->storage();
- request_ = empty_context_.CreateRequest(GURL("http://blah/"),
- net::DEFAULT_PRIORITY,
- url_request_delegate_.get(),
- NULL);
+ request_ = empty_context_->CreateRequest(GURL("http://blah/"),
+ net::DEFAULT_PRIORITY,
+ url_request_delegate_.get(),
+ NULL);
// Setup to create an AppCacheURLRequestJob with orders to deliver
// a network response.
// Start the request.
EXPECT_FALSE(job->has_been_started());
- mock_factory_job_ = job.get();
+ job_factory_->SetJob(job.get());
request_->Start();
- EXPECT_FALSE(mock_factory_job_);
+ EXPECT_FALSE(job_factory_->has_job());
EXPECT_TRUE(job->has_been_started());
if (!start_after_delivery_orders) {
void MakeRangeRequest() {
AppCacheStorage* storage = service_->storage();
- request_ = empty_context_.CreateRequest(GURL("http://blah/"),
- net::DEFAULT_PRIORITY,
- url_request_delegate_.get(),
- NULL);
+ request_ = empty_context_->CreateRequest(GURL("http://blah/"),
+ net::DEFAULT_PRIORITY,
+ url_request_delegate_.get(),
+ NULL);
// Request a range, the 3 middle chars out of 'Hello'
net::HttpRequestHeaders extra_headers;
// Start the request.
EXPECT_FALSE(job->has_been_started());
- mock_factory_job_ = job.get();
+ job_factory_->SetJob(job.get());
request_->Start();
- EXPECT_FALSE(mock_factory_job_);
+ EXPECT_FALSE(job_factory_->has_job());
EXPECT_TRUE(job->has_been_started());
// Completion is async.
}
int expected_write_result_;
int writer_deletion_count_down_;
- net::URLRequest::ProtocolFactory* orig_http_factory_;
- net::URLRequestContext empty_context_;
+ scoped_ptr<MockURLRequestJobFactory> job_factory_;
+ scoped_ptr<net::URLRequestContext> empty_context_;
scoped_ptr<net::URLRequest> request_;
scoped_ptr<MockURLRequestDelegate> url_request_delegate_;
static scoped_ptr<base::Thread> io_thread_;
- static AppCacheURLRequestJob* mock_factory_job_;
};
// static
scoped_ptr<base::Thread> AppCacheURLRequestJobTest::io_thread_;
-AppCacheURLRequestJob* AppCacheURLRequestJobTest::mock_factory_job_ = NULL;
TEST_F(AppCacheURLRequestJobTest, Basic) {
RunTestOnIOThread(&AppCacheURLRequestJobTest::Basic);
RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequestWithIOPending);
}
+} // namespace
+
} // namespace content