| Index: content/browser/service_worker/service_worker_cache_unittest.cc
|
| diff --git a/content/browser/service_worker/service_worker_cache_unittest.cc b/content/browser/service_worker/service_worker_cache_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..36646c8c8d0f1b7b95e18e9f9cd6b9f6bbf3750e
|
| --- /dev/null
|
| +++ b/content/browser/service_worker/service_worker_cache_unittest.cc
|
| @@ -0,0 +1,235 @@
|
| +// Copyright 2014 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 "content/browser/service_worker/service_worker_cache.h"
|
| +
|
| +#include "base/files/file_path.h"
|
| +#include "base/files/scoped_temp_dir.h"
|
| +#include "base/message_loop/message_loop_proxy.h"
|
| +#include "base/run_loop.h"
|
| +#include "content/browser/fileapi/chrome_blob_storage_context.h"
|
| +#include "content/browser/fileapi/mock_url_request_delegate.h"
|
| +#include "content/common/service_worker/service_worker_types.h"
|
| +#include "content/public/browser/browser_thread.h"
|
| +#include "content/public/test/test_browser_context.h"
|
| +#include "content/public/test/test_browser_thread_bundle.h"
|
| +#include "net/url_request/url_request_context.h"
|
| +#include "net/url_request/url_request_context_getter.h"
|
| +#include "net/url_request/url_request_job_factory_impl.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +#include "webkit/browser/blob/blob_data_handle.h"
|
| +#include "webkit/browser/blob/blob_storage_context.h"
|
| +#include "webkit/browser/blob/blob_url_request_job_factory.h"
|
| +#include "webkit/common/blob/blob_data.h"
|
| +
|
| +namespace content {
|
| +
|
| +namespace {
|
| +const char kTestData[] = "Hello World";
|
| +
|
| +// Returns a BlobProtocolHandler that uses |blob_storage_context|. Caller owns
|
| +// the memory.
|
| +webkit_blob::BlobProtocolHandler* CreateMockBlobProtocolHandler(
|
| + webkit_blob::BlobStorageContext* blob_storage_context) {
|
| + // The FileSystemContext and MessageLoopProxy are not actually used but a
|
| + // MessageLoopProxy is needed to avoid a DCHECK in BlobURLRequestJob ctor.
|
| + return new webkit_blob::BlobProtocolHandler(
|
| + blob_storage_context, NULL, base::MessageLoopProxy::current().get());
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +class ServiceWorkerCacheTest : public testing::Test {
|
| + protected:
|
| + ServiceWorkerCacheTest()
|
| + : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
|
| + blob_data_(new webkit_blob::BlobData("blob-id:myblob")),
|
| + callback_error_(ServiceWorkerCache::ErrorTypeOK) {}
|
| +
|
| + virtual void SetUp() OVERRIDE {
|
| + ChromeBlobStorageContext* blob_storage_context =
|
| + ChromeBlobStorageContext::GetFor(&browser_context_);
|
| + // Wait for chrome_blob_storage_context to finish initializing.
|
| + base::RunLoop().RunUntilIdle();
|
| +
|
| + url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl);
|
| + url_request_job_factory_->SetProtocolHandler(
|
| + "blob", CreateMockBlobProtocolHandler(blob_storage_context->context()));
|
| +
|
| + net::URLRequestContext* url_request_context =
|
| + browser_context_.GetRequestContext()->GetURLRequestContext();
|
| +
|
| + url_request_context->set_job_factory(url_request_job_factory_.get());
|
| +
|
| + CreateRequests(blob_storage_context);
|
| +
|
| + if (MemoryOnly()) {
|
| + cache_ = ServiceWorkerCache::CreateMemoryCache(
|
| + "test",
|
| + url_request_context,
|
| + blob_storage_context->context()->AsWeakPtr());
|
| + } else {
|
| + ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
|
| + cache_ = ServiceWorkerCache::CreatePersistentCache(
|
| + temp_dir_.path(),
|
| + "test",
|
| + url_request_context,
|
| + blob_storage_context->context()->AsWeakPtr());
|
| + }
|
| + CreateBackend();
|
| + }
|
| +
|
| + void CreateRequests(ChromeBlobStorageContext* blob_storage_context) {
|
| + std::map<std::string, std::string> headers;
|
| + headers.insert(std::make_pair("a", "a"));
|
| + headers.insert(std::make_pair("b", "b"));
|
| + body_request_.reset(new ServiceWorkerFetchRequest(
|
| + GURL("http://example.com/body.html"), "GET", headers, GURL(""), false));
|
| + no_body_request_.reset(
|
| + new ServiceWorkerFetchRequest(GURL("http://example.com/no_body.html"),
|
| + "GET",
|
| + headers,
|
| + GURL(""),
|
| + false));
|
| +
|
| + std::string expected_response;
|
| + for (int i = 0; i < 1; ++i) {
|
| + blob_data_->AppendData(kTestData);
|
| + expected_blob_data_ += kTestData;
|
| + }
|
| +
|
| + blob_handle1_ =
|
| + blob_storage_context->context()->AddFinishedBlob(blob_data_);
|
| +
|
| + body_response_.reset(
|
| + new ServiceWorkerResponse(GURL("http://example.com/body.html"),
|
| + 200,
|
| + "OK",
|
| + headers,
|
| + blob_handle1_->uuid()));
|
| +
|
| + no_body_response_.reset(new ServiceWorkerResponse(
|
| + GURL("http://example.com/no_body.html"), 200, "OK", headers, ""));
|
| + }
|
| +
|
| + void CreateBackend() {
|
| + scoped_ptr<base::RunLoop> loop(new base::RunLoop());
|
| + cache_->CreateBackend(base::Bind(&ServiceWorkerCacheTest::ErrorTypeCallback,
|
| + base::Unretained(this),
|
| + base::Unretained(loop.get())));
|
| + loop->Run();
|
| + EXPECT_EQ(ServiceWorkerCache::ErrorTypeOK, callback_error_);
|
| + }
|
| +
|
| + bool Put(ServiceWorkerFetchRequest* request,
|
| + ServiceWorkerResponse* response) {
|
| + scoped_ptr<base::RunLoop> loop(new base::RunLoop());
|
| +
|
| + cache_->Put(request,
|
| + response,
|
| + base::Bind(&ServiceWorkerCacheTest::ErrorTypeCallback,
|
| + base::Unretained(this),
|
| + base::Unretained(loop.get())));
|
| + loop->Run();
|
| +
|
| + return callback_error_ == ServiceWorkerCache::ErrorTypeOK;
|
| + }
|
| +
|
| + bool Match(ServiceWorkerFetchRequest* request) {
|
| + scoped_ptr<base::RunLoop> loop(new base::RunLoop());
|
| +
|
| + cache_->Match(request,
|
| + base::Bind(&ServiceWorkerCacheTest::ResponseAndErrorCallback,
|
| + base::Unretained(this),
|
| + base::Unretained(loop.get())));
|
| + loop->Run();
|
| +
|
| + return callback_error_ == ServiceWorkerCache::ErrorTypeOK;
|
| + }
|
| +
|
| + void ErrorTypeCallback(base::RunLoop* run_loop,
|
| + ServiceWorkerCache::ErrorType error) {
|
| + callback_error_ = error;
|
| + run_loop->Quit();
|
| + }
|
| +
|
| + void ResponseAndErrorCallback(
|
| + base::RunLoop* run_loop,
|
| + ServiceWorkerCache::ErrorType error,
|
| + scoped_ptr<ServiceWorkerResponse> response,
|
| + scoped_ptr<webkit_blob::BlobDataHandle> response_data_handle) {
|
| + callback_error_ = error;
|
| + callback_response_ = response.Pass();
|
| + callback_response_data_ = response_data_handle.Pass();
|
| + run_loop->Quit();
|
| + }
|
| +
|
| + void CopyBody(webkit_blob::BlobDataHandle* blob_handle, std::string* output) {
|
| + webkit_blob::BlobData* data = blob_handle->data();
|
| + std::vector<webkit_blob::BlobData::Item> items = data->items();
|
| + for (size_t i = 0, max = items.size(); i < max; ++i)
|
| + output->append(items[i].bytes(), items[i].length());
|
| + }
|
| +
|
| + virtual bool MemoryOnly() { return false; }
|
| +
|
| + TestBrowserContext browser_context_;
|
| + TestBrowserThreadBundle browser_thread_bundle_;
|
| + scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_;
|
| +
|
| + base::ScopedTempDir temp_dir_;
|
| + scoped_ptr<ServiceWorkerCache> cache_;
|
| +
|
| + scoped_ptr<ServiceWorkerFetchRequest> body_request_;
|
| + scoped_ptr<ServiceWorkerResponse> body_response_;
|
| + scoped_ptr<ServiceWorkerFetchRequest> no_body_request_;
|
| + scoped_ptr<ServiceWorkerResponse> no_body_response_;
|
| + scoped_refptr<webkit_blob::BlobData> blob_data_;
|
| + scoped_ptr<webkit_blob::BlobDataHandle> blob_handle1_;
|
| + std::string expected_blob_data_;
|
| +
|
| + ServiceWorkerCache::ErrorType callback_error_;
|
| + scoped_ptr<ServiceWorkerResponse> callback_response_;
|
| + scoped_ptr<webkit_blob::BlobDataHandle> callback_response_data_;
|
| +};
|
| +
|
| +class ServiceWorkerCacheTestP : public ServiceWorkerCacheTest,
|
| + public testing::WithParamInterface<bool> {
|
| + virtual bool MemoryOnly() OVERRIDE { return !GetParam(); }
|
| +};
|
| +
|
| +TEST_P(ServiceWorkerCacheTestP, PutNoBody) {
|
| + EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get()));
|
| +}
|
| +
|
| +TEST_P(ServiceWorkerCacheTestP, PutBody) {
|
| + EXPECT_TRUE(Put(body_request_.get(), body_response_.get()));
|
| +}
|
| +
|
| +TEST_P(ServiceWorkerCacheTestP, MatchNoBody) {
|
| + EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get()));
|
| + EXPECT_TRUE(Match(no_body_request_.get()));
|
| + EXPECT_EQ(200, callback_response_->status_code);
|
| + EXPECT_STREQ("OK", callback_response_->status_text.c_str());
|
| + EXPECT_STREQ("http://example.com/no_body.html",
|
| + callback_response_->url.spec().c_str());
|
| +}
|
| +
|
| +TEST_P(ServiceWorkerCacheTestP, MatchBody) {
|
| + EXPECT_TRUE(Put(body_request_.get(), body_response_.get()));
|
| + EXPECT_TRUE(Match(body_request_.get()));
|
| + EXPECT_EQ(200, callback_response_->status_code);
|
| + EXPECT_STREQ("OK", callback_response_->status_text.c_str());
|
| + EXPECT_STREQ("http://example.com/body.html",
|
| + callback_response_->url.spec().c_str());
|
| + std::string response_body;
|
| + CopyBody(callback_response_data_.get(), &response_body);
|
| + EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str());
|
| +}
|
| +
|
| +INSTANTIATE_TEST_CASE_P(ServiceWorkerCacheTest,
|
| + ServiceWorkerCacheTestP,
|
| + ::testing::Values(false, true));
|
| +
|
| +} // namespace content
|
|
|