Chromium Code Reviews| Index: content/browser/service_worker/service_worker_url_request_job_unittest.cc |
| diff --git a/content/browser/service_worker/service_worker_url_request_job_unittest.cc b/content/browser/service_worker/service_worker_url_request_job_unittest.cc |
| index 931cace061a643f5876c6dadd256a801702b9f2a..31ade1eba3ec21f4568f9d74d33280421437c176 100644 |
| --- a/content/browser/service_worker/service_worker_url_request_job_unittest.cc |
| +++ b/content/browser/service_worker/service_worker_url_request_job_unittest.cc |
| @@ -6,6 +6,8 @@ |
| #include "base/callback.h" |
| #include "base/memory/scoped_ptr.h" |
| #include "base/run_loop.h" |
| +#include "content/browser/fileapi/blob_url_request_job_unittest.h" |
| +#include "content/browser/fileapi/chrome_blob_storage_context.h" |
| #include "content/browser/service_worker/embedded_worker_registry.h" |
| #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| #include "content/browser/service_worker/service_worker_context_core.h" |
| @@ -14,6 +16,9 @@ |
| #include "content/browser/service_worker/service_worker_test_utils.h" |
| #include "content/browser/service_worker/service_worker_url_request_job.h" |
| #include "content/browser/service_worker/service_worker_version.h" |
| +#include "content/common/service_worker/service_worker_messages.h" |
| +#include "content/public/browser/blob_handle.h" |
| +#include "content/public/test/test_browser_context.h" |
| #include "content/public/test/test_browser_thread_bundle.h" |
| #include "net/base/io_buffer.h" |
| #include "net/http/http_request_headers.h" |
| @@ -22,62 +27,42 @@ |
| #include "net/url_request/url_request_context.h" |
| #include "net/url_request/url_request_job_factory_impl.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| +#include "webkit/browser/blob/blob_storage_context.h" |
| +#include "webkit/browser/blob/blob_url_request_job.h" |
| +#include "webkit/common/blob/blob_data.h" |
| namespace content { |
| +class ServiceWorkerURLRequestJobTest; |
| + |
| namespace { |
| -const int kBufferSize = 1024; |
| const int kProcessID = 1; |
| const int kProviderID = 100; |
| +const char kTestData[] = "Here is sample text for the blob."; |
| -class MockURLRequestDelegate : public net::URLRequest::Delegate { |
| - public: |
| - MockURLRequestDelegate() : received_data_(new net::IOBuffer(kBufferSize)) {} |
| - virtual ~MockURLRequestDelegate() {} |
| - virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE { |
| - if (request->status().is_success()) { |
| - EXPECT_TRUE(request->response_headers()); |
| - Read(request); |
| - } |
| - } |
| - virtual void OnReadCompleted(net::URLRequest* request, |
| - int bytes_read) OVERRIDE { |
| - EXPECT_EQ(0, bytes_read); |
| - } |
| - |
| - private: |
| - void Read(net::URLRequest* request) { |
| - if (request->is_pending()) { |
| - int bytes_read = 0; |
| - request->Read(received_data_.get(), kBufferSize, &bytes_read); |
| - // For now ServiceWorkerURLRequestJob wouldn't return |
| - // any content data yet. |
| - EXPECT_EQ(0, bytes_read); |
| - } |
| - } |
| - |
| - scoped_refptr<net::IOBuffer> received_data_; |
| - base::Closure on_complete_; |
| -}; |
| - |
| -class MockProtocolHandler : public net::URLRequestJobFactory::ProtocolHandler { |
| +class MockHttpProtocolHandler |
| + : public net::URLRequestJobFactory::ProtocolHandler { |
| public: |
| - MockProtocolHandler(base::WeakPtr<ServiceWorkerProviderHost> provider_host) |
| - : provider_host_(provider_host) {} |
| - virtual ~MockProtocolHandler() {} |
| + MockHttpProtocolHandler( |
| + base::WeakPtr<ServiceWorkerProviderHost> provider_host, |
| + base::WeakPtr<webkit_blob::BlobStorageContext> blob_storage_context) |
| + : provider_host_(provider_host), |
| + blob_storage_context_(blob_storage_context) {} |
| + virtual ~MockHttpProtocolHandler() {} |
| virtual net::URLRequestJob* MaybeCreateJob( |
| net::URLRequest* request, |
| net::NetworkDelegate* network_delegate) const OVERRIDE { |
| ServiceWorkerURLRequestJob* job = new ServiceWorkerURLRequestJob( |
| - request, network_delegate, provider_host_); |
| + request, network_delegate, provider_host_, blob_storage_context_); |
| job->ForwardToServiceWorker(); |
| return job; |
| } |
| private: |
| base::WeakPtr<ServiceWorkerProviderHost> provider_host_; |
| + base::WeakPtr<webkit_blob::BlobStorageContext> blob_storage_context_; |
| }; |
| } // namespace |
| @@ -85,11 +70,17 @@ class MockProtocolHandler : public net::URLRequestJobFactory::ProtocolHandler { |
| class ServiceWorkerURLRequestJobTest : public testing::Test { |
| protected: |
| ServiceWorkerURLRequestJobTest() |
| - : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
| + : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
| + blob_data_(new webkit_blob::BlobData("blob-id:myblob")) {} |
| virtual ~ServiceWorkerURLRequestJobTest() {} |
| virtual void SetUp() OVERRIDE { |
| - helper_.reset(new EmbeddedWorkerTestHelper(kProcessID)); |
| + browser_context_.reset(new TestBrowserContext); |
| + SetUpWithHelper(new EmbeddedWorkerTestHelper(kProcessID)); |
| + } |
| + |
| + void SetUpWithHelper(EmbeddedWorkerTestHelper* helper) { |
| + helper_.reset(helper); |
| registration_ = new ServiceWorkerRegistration( |
| GURL("http://example.com/*"), |
| @@ -104,9 +95,20 @@ class ServiceWorkerURLRequestJobTest : public testing::Test { |
| kProcessID, kProviderID, helper_->context()->AsWeakPtr(), NULL)); |
| provider_host->SetActiveVersion(version_.get()); |
| - url_request_job_factory_.SetProtocolHandler( |
| - "http", new MockProtocolHandler(provider_host->AsWeakPtr())); |
| - url_request_context_.set_job_factory(&url_request_job_factory_); |
| + ChromeBlobStorageContext* blob_storage_context = |
| + ChromeBlobStorageContext::GetFor(browser_context_.get()); |
| + // Wait for blob_storage_context to finish initializing. |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl); |
| + url_request_job_factory_->SetProtocolHandler( |
| + "http", |
| + new MockHttpProtocolHandler( |
| + provider_host->AsWeakPtr(), |
| + blob_storage_context->context()->AsWeakPtr())); |
| + url_request_job_factory_->SetProtocolHandler( |
| + "blob", new MockBlobProtocolHandler(blob_data_.get(), NULL)); |
| + url_request_context_.set_job_factory(url_request_job_factory_.get()); |
| helper_->context()->AddProviderHost(provider_host.Pass()); |
| } |
| @@ -135,21 +137,26 @@ class ServiceWorkerURLRequestJobTest : public testing::Test { |
| TestBrowserThreadBundle thread_bundle_; |
| + scoped_ptr<TestBrowserContext> browser_context_; |
| scoped_ptr<EmbeddedWorkerTestHelper> helper_; |
| scoped_refptr<ServiceWorkerRegistration> registration_; |
| scoped_refptr<ServiceWorkerVersion> version_; |
| - net::URLRequestJobFactoryImpl url_request_job_factory_; |
| + scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; |
| net::URLRequestContext url_request_context_; |
| MockURLRequestDelegate url_request_delegate_; |
| scoped_ptr<net::URLRequest> request_; |
| + scoped_refptr<webkit_blob::BlobData> blob_data_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(ServiceWorkerURLRequestJobTest); |
| }; |
| TEST_F(ServiceWorkerURLRequestJobTest, Simple) { |
| version_->SetStatus(ServiceWorkerVersion::ACTIVE); |
| TestRequest(); |
| + |
| + EXPECT_EQ(std::string(), url_request_delegate_.response_data()); |
| } |
| TEST_F(ServiceWorkerURLRequestJobTest, WaitForActivation) { |
| @@ -160,6 +167,65 @@ TEST_F(ServiceWorkerURLRequestJobTest, WaitForActivation) { |
| TestRequest(); |
| EXPECT_EQ(SERVICE_WORKER_OK, status); |
| + EXPECT_EQ(std::string(), url_request_delegate_.response_data()); |
| +} |
| + |
| +// Responds to fetch events with a blob. |
| +class BlobResponder : public EmbeddedWorkerTestHelper { |
| + public: |
| + BlobResponder(int mock_render_process_id, const std::string& blob_uuid) |
| + : EmbeddedWorkerTestHelper(mock_render_process_id), |
| + blob_uuid_(blob_uuid) {} |
| + virtual ~BlobResponder() {} |
| + |
| + protected: |
| + virtual void OnFetchEvent(int embedded_worker_id, |
| + int request_id, |
| + const ServiceWorkerFetchRequest& request) OVERRIDE { |
| + SimulateSend(new ServiceWorkerHostMsg_FetchEventFinished( |
| + embedded_worker_id, |
| + request_id, |
| + SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, |
| + ServiceWorkerResponse(200, |
| + "OK", |
| + "GET", |
| + std::map<std::string, std::string>(), |
| + blob_uuid_))); |
|
falken
2014/05/22 11:40:59
One problem is that I can change blob_uuid_ to any
michaeln
2014/05/23 02:23:23
wha???
|
| + } |
| + |
| + std::string blob_uuid_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(BlobResponder); |
| +}; |
| + |
| +TEST_F(ServiceWorkerURLRequestJobTest, ResponseBlob) { |
| + ChromeBlobStorageContext* blob_storage_context = |
| + ChromeBlobStorageContext::GetFor(browser_context_.get()); |
| + std::string expected; |
| + for (int i = 0; i < 1024; ++i) { |
| + blob_data_->AppendData(kTestData); |
| + expected += kTestData; |
| + } |
| + scoped_ptr<webkit_blob::BlobDataHandle> blob_handle = |
| + blob_storage_context->context()->AddFinishedBlob(blob_data_); |
| + SetUpWithHelper(new BlobResponder(kProcessID, blob_handle->uuid())); |
|
michaeln
2014/05/23 02:23:23
oh, i see, because of how the mockprotocolhandler
falken
2014/05/23 11:15:46
Great! I could use the real BlobProtocolHandler.
|
| + |
| + version_->SetStatus(ServiceWorkerVersion::ACTIVE); |
| + |
| + request_ = |
| + url_request_context_.CreateRequest(GURL("http://example.com/foo.html"), |
| + net::DEFAULT_PRIORITY, |
| + &url_request_delegate_, |
| + NULL); |
| + |
| + request_->set_method("GET"); |
| + request_->Start(); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + // Verify response. |
| + EXPECT_TRUE(request_->status().is_success()); |
| + EXPECT_EQ(200, request_->response_headers()->response_code()); |
| + EXPECT_EQ(expected, url_request_delegate_.response_data()); |
| } |
| // TODO(kinuko): Add more tests with different response data and also for |