diff options
author | caseq@google.com <caseq@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-08 10:07:05 +0000 |
---|---|---|
committer | caseq@google.com <caseq@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-08 10:07:05 +0000 |
commit | d5b73caf4505ec3fafb318096641ab010f985061 (patch) | |
tree | e29bf9e9d743fc080488366a55421c8213c96661 /webkit/appcache/appcache_update_job_unittest.cc | |
parent | 85a6b157624975c6d0ebcf29600a38899c653f62 (diff) | |
download | chromium_src-d5b73caf4505ec3fafb318096641ab010f985061.zip chromium_src-d5b73caf4505ec3fafb318096641ab010f985061.tar.gz chromium_src-d5b73caf4505ec3fafb318096641ab010f985061.tar.bz2 |
Revert r91815, "Clean up RegisterProtocolFactory calls." (broke mac canaries)
BUG=none
TEST=successful build on mac canaries
TBR=willchan
Review URL: http://codereview.chromium.org/7328015
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@91833 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/appcache/appcache_update_job_unittest.cc')
-rw-r--r-- | webkit/appcache/appcache_update_job_unittest.cc | 181 |
1 files changed, 92 insertions, 89 deletions
diff --git a/webkit/appcache/appcache_update_job_unittest.cc b/webkit/appcache/appcache_update_job_unittest.cc index 9248bdf..d419fc3 100644 --- a/webkit/appcache/appcache_update_job_unittest.cc +++ b/webkit/appcache/appcache_update_job_unittest.cc @@ -10,7 +10,6 @@ #include "net/base/net_errors.h" #include "net/http/http_response_headers.h" #include "net/url_request/url_request_error_job.h" -#include "net/url_request/url_request_job_factory.h" #include "net/url_request/url_request_test_job.h" #include "net/url_request/url_request_test_util.h" #include "webkit/appcache/appcache_group.h" @@ -50,7 +49,8 @@ class MockHttpServer { return GURL("https://cross_origin_host/" + path); } - static net::URLRequestJob* JobFactory(net::URLRequest* request) { + static net::URLRequestJob* JobFactory(net::URLRequest* request, + const std::string& scheme) { if (request->url().host() != "mockhost" && request->url().host() != "cross_origin_host") return new net::URLRequestErrorJob(request, -1); @@ -182,14 +182,6 @@ class MockHttpServer { } }; -class MockHttpServerJobFactory - : public net::URLRequestJobFactory::ProtocolHandler { - public: - virtual net::URLRequestJob* MaybeCreateJob(net::URLRequest* request) const { - return MockHttpServer::JobFactory(request); - } -}; - } // namespace class MockFrontend : public AppCacheFrontend { @@ -319,16 +311,14 @@ class MockFrontend : public AppCacheFrontend { }; // Helper factories to simulate redirected URL responses for tests. -class RedirectFactory : public net::URLRequestJobFactory::ProtocolHandler { - public: - virtual net::URLRequestJob* MaybeCreateJob(net::URLRequest* request) const { - return new net::URLRequestTestJob( - request, - net::URLRequestTestJob::test_redirect_headers(), - net::URLRequestTestJob::test_data_1(), - true); - } -}; +static net::URLRequestJob* RedirectFactory(net::URLRequest* request, + const std::string& scheme) { + return new net::URLRequestTestJob( + request, + net::URLRequestTestJob::test_redirect_headers(), + net::URLRequestTestJob::test_data_1(), + true); +} // Helper class to simulate a URL that returns retry or success. class RetryRequestTestJob : public net::URLRequestTestJob { @@ -357,7 +347,8 @@ class RetryRequestTestJob : public net::URLRequestTestJob { expected_requests_ = 0; } - static net::URLRequestJob* RetryFactory(net::URLRequest* request) { + static net::URLRequestJob* RetryFactory(net::URLRequest* request, + const std::string& scheme) { ++num_requests_; if (num_retries_ > 0 && request->original_url() == kRetryUrl) { --num_retries_; @@ -426,14 +417,6 @@ class RetryRequestTestJob : public net::URLRequestTestJob { static int expected_requests_; }; -class RetryRequestTestJobFactory - : public net::URLRequestJobFactory::ProtocolHandler { - public: - virtual net::URLRequestJob* MaybeCreateJob(net::URLRequest* request) const { - return RetryRequestTestJob::RetryFactory(request); - } -}; - // static const GURL RetryRequestTestJob::kRetryUrl("http://retry"); int RetryRequestTestJob::num_requests_ = 0; @@ -466,7 +449,8 @@ class HttpHeadersRequestTestJob : public net::URLRequestTestJob { already_checked_ = false; } - static net::URLRequestJob* IfModifiedSinceFactory(net::URLRequest* request) { + static net::URLRequestJob* IfModifiedSinceFactory(net::URLRequest* request, + const std::string& scheme) { if (!already_checked_) { already_checked_ = true; // only check once for a test const net::HttpRequestHeaders& extra_headers = @@ -482,7 +466,7 @@ class HttpHeadersRequestTestJob : public net::URLRequestTestJob { net::HttpRequestHeaders::kIfNoneMatch, &header_value) && header_value == expect_if_none_match_; } - return MockHttpServer::JobFactory(request); + return MockHttpServer::JobFactory(request, scheme); } private: @@ -500,20 +484,12 @@ std::string HttpHeadersRequestTestJob::expect_if_none_match_; bool HttpHeadersRequestTestJob::saw_if_none_match_ = false; bool HttpHeadersRequestTestJob::already_checked_ = false; -class IfModifiedSinceJobFactory - : public net::URLRequestJobFactory::ProtocolHandler { - public: - virtual net::URLRequestJob* MaybeCreateJob(net::URLRequest* request) const { - return HttpHeadersRequestTestJob::IfModifiedSinceFactory(request); - } -}; - namespace { class IOThread : public base::Thread { public: explicit IOThread(const char* name) - : base::Thread(name) { + : base::Thread(name), old_factory_(NULL), old_factory_https_(NULL) { } ~IOThread() { @@ -526,27 +502,22 @@ class IOThread : public base::Thread { return request_context_; } - void SetNewJobFactory(net::URLRequestJobFactory* job_factory) { - DCHECK(job_factory); - job_factory_.reset(job_factory); - request_context_->set_job_factory(job_factory_.get()); - } - virtual void Init() { - job_factory_.reset(new net::URLRequestJobFactory); - job_factory_->SetProtocolHandler("http", new MockHttpServerJobFactory); - job_factory_->SetProtocolHandler("https", new MockHttpServerJobFactory); + old_factory_ = net::URLRequest::RegisterProtocolFactory( + "http", MockHttpServer::JobFactory); + old_factory_https_ = net::URLRequest::RegisterProtocolFactory( + "https", MockHttpServer::JobFactory); request_context_ = new TestURLRequestContext(); - request_context_->set_job_factory(job_factory_.get()); } virtual void CleanUp() { + net::URLRequest::RegisterProtocolFactory("http", old_factory_); + net::URLRequest::RegisterProtocolFactory("https", old_factory_https_); request_context_ = NULL; - job_factory_.reset(); } - private: - scoped_ptr<net::URLRequestJobFactory> job_factory_; + net::URLRequest::ProtocolFactory* old_factory_; + net::URLRequest::ProtocolFactory* old_factory_https_; scoped_refptr<net::URLRequestContext> request_context_; }; @@ -597,12 +568,34 @@ class AppCacheUpdateJobTest : public testing::Test, expect_newest_cache_(NULL), expect_non_null_update_time_(false), tested_manifest_(NONE), - tested_manifest_path_override_(NULL) { - io_thread_.reset(new IOThread("AppCacheUpdateJob IO test thread")); + tested_manifest_path_override_(NULL), + registered_factory_(false), + old_factory_(NULL) { + } + + static void SetUpTestCase() { + io_thread_ = new IOThread("AppCacheUpdateJob IO test thread"); base::Thread::Options options(MessageLoop::TYPE_IO, 0); io_thread_->StartWithOptions(options); } + static base::WaitableEvent* io_thread_shutdown_event_; + + // Cleanup function; must be called on the IO Thread. + static void CleanupIOThread() { + io_thread_shutdown_event_->Signal(); + } + + static void TearDownTestCase() { + io_thread_shutdown_event_ = new base::WaitableEvent(false, false); + io_thread_->message_loop()->PostTask(FROM_HERE, + NewRunnableFunction(CleanupIOThread)); + io_thread_shutdown_event_->Wait(); + delete io_thread_shutdown_event_; + delete io_thread_; + io_thread_ = NULL; + } + // Use a separate IO thread to run a test. Thread will be destroyed // when it goes out of scope. template <class Method> @@ -826,9 +819,9 @@ class AppCacheUpdateJobTest : public testing::Test, void ManifestRedirectTest() { ASSERT_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()); - net::URLRequestJobFactory* new_factory(new net::URLRequestJobFactory); - new_factory->SetProtocolHandler("http", new RedirectFactory); - io_thread_->SetNewJobFactory(new_factory); + old_factory_ = + net::URLRequest::RegisterProtocolFactory("http", RedirectFactory); + registered_factory_ = true; MakeService(); group_ = new AppCacheGroup(service_.get(), GURL("http://testme"), @@ -1618,9 +1611,9 @@ class AppCacheUpdateJobTest : public testing::Test, // Set some large number of times to return retry. // Expect 1 manifest fetch and 3 retries. RetryRequestTestJob::Initialize(5, RetryRequestTestJob::RETRY_AFTER_0, 4); - net::URLRequestJobFactory* new_factory(new net::URLRequestJobFactory); - new_factory->SetProtocolHandler("http", new RetryRequestTestJobFactory); - io_thread_->SetNewJobFactory(new_factory); + old_factory_ = net::URLRequest::RegisterProtocolFactory( + "http", RetryRequestTestJob::RetryFactory); + registered_factory_ = true; MakeService(); group_ = new AppCacheGroup(service_.get(), RetryRequestTestJob::kRetryUrl, @@ -1649,9 +1642,9 @@ class AppCacheUpdateJobTest : public testing::Test, // Set some large number of times to return retry. // Expect 1 manifest fetch and 0 retries. RetryRequestTestJob::Initialize(5, RetryRequestTestJob::NO_RETRY_AFTER, 1); - net::URLRequestJobFactory* new_factory(new net::URLRequestJobFactory); - new_factory->SetProtocolHandler("http", new RetryRequestTestJobFactory); - io_thread_->SetNewJobFactory(new_factory); + old_factory_ = net::URLRequest::RegisterProtocolFactory( + "http", RetryRequestTestJob::RetryFactory); + registered_factory_ = true; MakeService(); group_ = new AppCacheGroup(service_.get(), RetryRequestTestJob::kRetryUrl, @@ -1681,9 +1674,9 @@ class AppCacheUpdateJobTest : public testing::Test, // Expect 1 request and 0 retry attempts. RetryRequestTestJob::Initialize( 5, RetryRequestTestJob::NONZERO_RETRY_AFTER, 1); - net::URLRequestJobFactory* new_factory(new net::URLRequestJobFactory); - new_factory->SetProtocolHandler("http", new RetryRequestTestJobFactory); - io_thread_->SetNewJobFactory(new_factory); + old_factory_ = net::URLRequest::RegisterProtocolFactory( + "http", RetryRequestTestJob::RetryFactory); + registered_factory_ = true; MakeService(); group_ = new AppCacheGroup(service_.get(), RetryRequestTestJob::kRetryUrl, @@ -1712,9 +1705,9 @@ class AppCacheUpdateJobTest : public testing::Test, // Set 2 as the retry limit (does not exceed the max). // Expect 1 manifest fetch, 2 retries, 1 url fetch, 1 manifest refetch. RetryRequestTestJob::Initialize(2, RetryRequestTestJob::RETRY_AFTER_0, 5); - net::URLRequestJobFactory* new_factory(new net::URLRequestJobFactory); - new_factory->SetProtocolHandler("http", new RetryRequestTestJobFactory); - io_thread_->SetNewJobFactory(new_factory); + old_factory_ = net::URLRequest::RegisterProtocolFactory( + "http", RetryRequestTestJob::RetryFactory); + registered_factory_ = true; MakeService(); group_ = new AppCacheGroup(service_.get(), RetryRequestTestJob::kRetryUrl, @@ -1743,9 +1736,9 @@ class AppCacheUpdateJobTest : public testing::Test, // Set 1 as the retry limit (does not exceed the max). // Expect 1 manifest fetch, 1 url fetch, 1 url retry, 1 manifest refetch. RetryRequestTestJob::Initialize(1, RetryRequestTestJob::RETRY_AFTER_0, 4); - net::URLRequestJobFactory* new_factory(new net::URLRequestJobFactory); - new_factory->SetProtocolHandler("http", new RetryRequestTestJobFactory); - io_thread_->SetNewJobFactory(new_factory); + old_factory_ = net::URLRequest::RegisterProtocolFactory( + "http", RetryRequestTestJob::RetryFactory); + registered_factory_ = true; MakeService(); group_ = new AppCacheGroup(service_.get(), GURL("http://retryurl"), @@ -2584,9 +2577,9 @@ class AppCacheUpdateJobTest : public testing::Test, void IfModifiedSinceTest() { ASSERT_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()); - net::URLRequestJobFactory* new_factory(new net::URLRequestJobFactory); - new_factory->SetProtocolHandler("http", new IfModifiedSinceJobFactory); - io_thread_->SetNewJobFactory(new_factory); + old_factory_ = net::URLRequest::RegisterProtocolFactory( + "http", HttpHeadersRequestTestJob::IfModifiedSinceFactory); + registered_factory_ = true; MakeService(); group_ = new AppCacheGroup(service_.get(), GURL("http://headertest"), 111); @@ -2650,9 +2643,9 @@ class AppCacheUpdateJobTest : public testing::Test, ASSERT_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()); HttpHeadersRequestTestJob::Initialize("Sat, 29 Oct 1994 19:43:31 GMT", ""); - net::URLRequestJobFactory* new_factory(new net::URLRequestJobFactory); - new_factory->SetProtocolHandler("http", new IfModifiedSinceJobFactory); - io_thread_->SetNewJobFactory(new_factory); + old_factory_ = net::URLRequest::RegisterProtocolFactory( + "http", HttpHeadersRequestTestJob::IfModifiedSinceFactory); + registered_factory_ = true; MakeService(); group_ = new AppCacheGroup( @@ -2708,9 +2701,9 @@ class AppCacheUpdateJobTest : public testing::Test, ASSERT_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()); HttpHeadersRequestTestJob::Initialize("", "\"LadeDade\""); - net::URLRequestJobFactory* new_factory(new net::URLRequestJobFactory); - new_factory->SetProtocolHandler("http", new IfModifiedSinceJobFactory); - io_thread_->SetNewJobFactory(new_factory); + old_factory_ = net::URLRequest::RegisterProtocolFactory( + "http", HttpHeadersRequestTestJob::IfModifiedSinceFactory); + registered_factory_ = true; MakeService(); group_ = new AppCacheGroup( @@ -2766,9 +2759,9 @@ class AppCacheUpdateJobTest : public testing::Test, ASSERT_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()); HttpHeadersRequestTestJob::Initialize("", "\"LadeDade\""); - net::URLRequestJobFactory* new_factory(new net::URLRequestJobFactory); - new_factory->SetProtocolHandler("http", new IfModifiedSinceJobFactory); - io_thread_->SetNewJobFactory(new_factory); + old_factory_ = net::URLRequest::RegisterProtocolFactory( + "http", HttpHeadersRequestTestJob::IfModifiedSinceFactory); + registered_factory_ = true; MakeService(); group_ = new AppCacheGroup(service_.get(), GURL("http://headertest"), 111); @@ -2801,9 +2794,9 @@ class AppCacheUpdateJobTest : public testing::Test, // Verify that code is correct when building multiple extra headers. HttpHeadersRequestTestJob::Initialize( "Sat, 29 Oct 1994 19:43:31 GMT", "\"LadeDade\""); - net::URLRequestJobFactory* new_factory(new net::URLRequestJobFactory); - new_factory->SetProtocolHandler("http", new IfModifiedSinceJobFactory); - io_thread_->SetNewJobFactory(new_factory); + old_factory_ = net::URLRequest::RegisterProtocolFactory( + "http", HttpHeadersRequestTestJob::IfModifiedSinceFactory); + registered_factory_ = true; MakeService(); group_ = new AppCacheGroup(service_.get(), GURL("http://headertest"), 111); @@ -2925,6 +2918,8 @@ class AppCacheUpdateJobTest : public testing::Test, STLDeleteContainerPointers(frontends_.begin(), frontends_.end()); response_infos_.clear(); service_.reset(NULL); + if (registered_factory_) + net::URLRequest::RegisterProtocolFactory("http", old_factory_); event_->Signal(); } @@ -3239,7 +3234,7 @@ class AppCacheUpdateJobTest : public testing::Test, PENDING_MASTER_NO_UPDATE, }; - scoped_ptr<IOThread> io_thread_; + static IOThread* io_thread_; scoped_ptr<MockAppCacheService> service_; scoped_refptr<AppCacheGroup> group_; @@ -3271,8 +3266,16 @@ class AppCacheUpdateJobTest : public testing::Test, const char* tested_manifest_path_override_; AppCache::EntryMap expect_extra_entries_; std::map<GURL, int64> expect_response_ids_; + + bool registered_factory_; + net::URLRequest::ProtocolFactory* old_factory_; }; +// static +IOThread* AppCacheUpdateJobTest::io_thread_ = NULL; +base::WaitableEvent* AppCacheUpdateJobTest::io_thread_shutdown_event_ = NULL; + + TEST_F(AppCacheUpdateJobTest, AlreadyChecking) { MockAppCacheService service; scoped_refptr<AppCacheGroup> group( |