OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/service_worker/service_worker_cache.h" |
| 6 |
| 7 #include "base/files/file_path.h" |
| 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/message_loop/message_loop_proxy.h" |
| 10 #include "base/run_loop.h" |
| 11 #include "content/browser/fileapi/chrome_blob_storage_context.h" |
| 12 #include "content/browser/fileapi/mock_url_request_delegate.h" |
| 13 #include "content/common/service_worker/service_worker_types.h" |
| 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/test/test_browser_context.h" |
| 16 #include "content/public/test/test_browser_thread_bundle.h" |
| 17 #include "net/url_request/url_request_context.h" |
| 18 #include "net/url_request/url_request_context_getter.h" |
| 19 #include "net/url_request/url_request_job_factory_impl.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "webkit/browser/blob/blob_data_handle.h" |
| 22 #include "webkit/browser/blob/blob_storage_context.h" |
| 23 #include "webkit/browser/blob/blob_url_request_job_factory.h" |
| 24 #include "webkit/common/blob/blob_data.h" |
| 25 |
| 26 namespace content { |
| 27 |
| 28 namespace { |
| 29 const char kTestData[] = "Hello World"; |
| 30 |
| 31 // Returns a BlobProtocolHandler that uses |blob_storage_context|. Caller owns |
| 32 // the memory. |
| 33 storage::BlobProtocolHandler* CreateMockBlobProtocolHandler( |
| 34 storage::BlobStorageContext* blob_storage_context) { |
| 35 // The FileSystemContext and MessageLoopProxy are not actually used but a |
| 36 // MessageLoopProxy is needed to avoid a DCHECK in BlobURLRequestJob ctor. |
| 37 return new storage::BlobProtocolHandler( |
| 38 blob_storage_context, NULL, base::MessageLoopProxy::current().get()); |
| 39 } |
| 40 |
| 41 } // namespace |
| 42 |
| 43 class ServiceWorkerCacheTest : public testing::Test { |
| 44 public: |
| 45 ServiceWorkerCacheTest() |
| 46 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
| 47 callback_error_(ServiceWorkerCache::ErrorTypeOK) {} |
| 48 |
| 49 virtual void SetUp() OVERRIDE { |
| 50 ChromeBlobStorageContext* blob_storage_context = |
| 51 ChromeBlobStorageContext::GetFor(&browser_context_); |
| 52 // Wait for chrome_blob_storage_context to finish initializing. |
| 53 base::RunLoop().RunUntilIdle(); |
| 54 blob_storage_context_ = blob_storage_context->context(); |
| 55 |
| 56 url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl); |
| 57 url_request_job_factory_->SetProtocolHandler( |
| 58 "blob", CreateMockBlobProtocolHandler(blob_storage_context->context())); |
| 59 |
| 60 net::URLRequestContext* url_request_context = |
| 61 browser_context_.GetRequestContext()->GetURLRequestContext(); |
| 62 |
| 63 url_request_context->set_job_factory(url_request_job_factory_.get()); |
| 64 |
| 65 CreateRequests(blob_storage_context); |
| 66 |
| 67 if (MemoryOnly()) { |
| 68 cache_ = ServiceWorkerCache::CreateMemoryCache( |
| 69 "test", |
| 70 url_request_context, |
| 71 blob_storage_context->context()->AsWeakPtr()); |
| 72 } else { |
| 73 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 74 cache_ = ServiceWorkerCache::CreatePersistentCache( |
| 75 temp_dir_.path(), |
| 76 "test", |
| 77 url_request_context, |
| 78 blob_storage_context->context()->AsWeakPtr()); |
| 79 } |
| 80 CreateBackend(); |
| 81 } |
| 82 |
| 83 void CreateRequests(ChromeBlobStorageContext* blob_storage_context) { |
| 84 std::map<std::string, std::string> headers; |
| 85 headers.insert(std::make_pair("a", "a")); |
| 86 headers.insert(std::make_pair("b", "b")); |
| 87 body_request_.reset(new ServiceWorkerFetchRequest( |
| 88 GURL("http://example.com/body.html"), "GET", headers, GURL(""), false)); |
| 89 no_body_request_.reset( |
| 90 new ServiceWorkerFetchRequest(GURL("http://example.com/no_body.html"), |
| 91 "GET", |
| 92 headers, |
| 93 GURL(""), |
| 94 false)); |
| 95 |
| 96 std::string expected_response; |
| 97 for (int i = 0; i < 100; ++i) |
| 98 expected_blob_data_ += kTestData; |
| 99 |
| 100 scoped_refptr<storage::BlobData> blob_data( |
| 101 new storage::BlobData("blob-id:myblob")); |
| 102 blob_data->AppendData(expected_blob_data_); |
| 103 |
| 104 blob_handle_ = blob_storage_context->context()->AddFinishedBlob(blob_data); |
| 105 |
| 106 body_response_.reset( |
| 107 new ServiceWorkerResponse(GURL("http://example.com/body.html"), |
| 108 200, |
| 109 "OK", |
| 110 headers, |
| 111 blob_handle_->uuid())); |
| 112 |
| 113 no_body_response_.reset(new ServiceWorkerResponse( |
| 114 GURL("http://example.com/no_body.html"), 200, "OK", headers, "")); |
| 115 } |
| 116 |
| 117 void CreateBackend() { |
| 118 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); |
| 119 cache_->CreateBackend(base::Bind(&ServiceWorkerCacheTest::ErrorTypeCallback, |
| 120 base::Unretained(this), |
| 121 base::Unretained(loop.get()))); |
| 122 loop->Run(); |
| 123 EXPECT_EQ(ServiceWorkerCache::ErrorTypeOK, callback_error_); |
| 124 } |
| 125 |
| 126 bool Put(ServiceWorkerFetchRequest* request, |
| 127 ServiceWorkerResponse* response) { |
| 128 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); |
| 129 |
| 130 cache_->Put(request, |
| 131 response, |
| 132 base::Bind(&ServiceWorkerCacheTest::ErrorTypeCallback, |
| 133 base::Unretained(this), |
| 134 base::Unretained(loop.get()))); |
| 135 // TODO(jkarlin): These functions should use base::RunLoop().RunUntilIdle() |
| 136 // once the cache uses a passed in MessageLoopProxy instead of the CACHE |
| 137 // thread. |
| 138 loop->Run(); |
| 139 |
| 140 return callback_error_ == ServiceWorkerCache::ErrorTypeOK; |
| 141 } |
| 142 |
| 143 bool Match(ServiceWorkerFetchRequest* request) { |
| 144 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); |
| 145 |
| 146 cache_->Match(request, |
| 147 base::Bind(&ServiceWorkerCacheTest::ResponseAndErrorCallback, |
| 148 base::Unretained(this), |
| 149 base::Unretained(loop.get()))); |
| 150 loop->Run(); |
| 151 |
| 152 return callback_error_ == ServiceWorkerCache::ErrorTypeOK; |
| 153 } |
| 154 |
| 155 bool Delete(ServiceWorkerFetchRequest* request) { |
| 156 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); |
| 157 |
| 158 cache_->Delete(request, |
| 159 base::Bind(&ServiceWorkerCacheTest::ErrorTypeCallback, |
| 160 base::Unretained(this), |
| 161 base::Unretained(loop.get()))); |
| 162 loop->Run(); |
| 163 |
| 164 return callback_error_ == ServiceWorkerCache::ErrorTypeOK; |
| 165 } |
| 166 |
| 167 void ErrorTypeCallback(base::RunLoop* run_loop, |
| 168 ServiceWorkerCache::ErrorType error) { |
| 169 callback_error_ = error; |
| 170 run_loop->Quit(); |
| 171 } |
| 172 |
| 173 void ResponseAndErrorCallback( |
| 174 base::RunLoop* run_loop, |
| 175 ServiceWorkerCache::ErrorType error, |
| 176 scoped_ptr<ServiceWorkerResponse> response, |
| 177 scoped_ptr<storage::BlobDataHandle> body_handle) { |
| 178 callback_error_ = error; |
| 179 callback_response_ = response.Pass(); |
| 180 |
| 181 if (error == ServiceWorkerCache::ErrorTypeOK && |
| 182 !callback_response_->blob_uuid.empty()) { |
| 183 callback_response_data_ = body_handle.Pass(); |
| 184 } |
| 185 |
| 186 run_loop->Quit(); |
| 187 } |
| 188 |
| 189 void CopyBody(storage::BlobDataHandle* blob_handle, std::string* output) { |
| 190 storage::BlobData* data = blob_handle->data(); |
| 191 std::vector<storage::BlobData::Item> items = data->items(); |
| 192 for (size_t i = 0, max = items.size(); i < max; ++i) |
| 193 output->append(items[i].bytes(), items[i].length()); |
| 194 } |
| 195 |
| 196 virtual bool MemoryOnly() { return false; } |
| 197 |
| 198 protected: |
| 199 TestBrowserContext browser_context_; |
| 200 TestBrowserThreadBundle browser_thread_bundle_; |
| 201 scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; |
| 202 storage::BlobStorageContext* blob_storage_context_; |
| 203 |
| 204 base::ScopedTempDir temp_dir_; |
| 205 scoped_ptr<ServiceWorkerCache> cache_; |
| 206 |
| 207 scoped_ptr<ServiceWorkerFetchRequest> body_request_; |
| 208 scoped_ptr<ServiceWorkerResponse> body_response_; |
| 209 scoped_ptr<ServiceWorkerFetchRequest> no_body_request_; |
| 210 scoped_ptr<ServiceWorkerResponse> no_body_response_; |
| 211 scoped_ptr<storage::BlobDataHandle> blob_handle_; |
| 212 std::string expected_blob_data_; |
| 213 |
| 214 ServiceWorkerCache::ErrorType callback_error_; |
| 215 scoped_ptr<ServiceWorkerResponse> callback_response_; |
| 216 scoped_ptr<storage::BlobDataHandle> callback_response_data_; |
| 217 }; |
| 218 |
| 219 class ServiceWorkerCacheTestP : public ServiceWorkerCacheTest, |
| 220 public testing::WithParamInterface<bool> { |
| 221 virtual bool MemoryOnly() OVERRIDE { return !GetParam(); } |
| 222 }; |
| 223 |
| 224 TEST_P(ServiceWorkerCacheTestP, PutNoBody) { |
| 225 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); |
| 226 } |
| 227 |
| 228 TEST_P(ServiceWorkerCacheTestP, PutBody) { |
| 229 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); |
| 230 } |
| 231 |
| 232 TEST_P(ServiceWorkerCacheTestP, PutBodyDropBlobRef) { |
| 233 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); |
| 234 cache_->Put(body_request_.get(), |
| 235 body_response_.get(), |
| 236 base::Bind(&ServiceWorkerCacheTestP::ErrorTypeCallback, |
| 237 base::Unretained(this), |
| 238 base::Unretained(loop.get()))); |
| 239 // The handle should be held by the cache now so the deref here should be |
| 240 // okay. |
| 241 blob_handle_.reset(); |
| 242 loop->Run(); |
| 243 |
| 244 EXPECT_EQ(ServiceWorkerCache::ErrorTypeOK, callback_error_); |
| 245 } |
| 246 |
| 247 TEST_P(ServiceWorkerCacheTestP, DeleteNoBody) { |
| 248 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); |
| 249 EXPECT_TRUE(Match(no_body_request_.get())); |
| 250 EXPECT_TRUE(Delete(no_body_request_.get())); |
| 251 EXPECT_FALSE(Match(no_body_request_.get())); |
| 252 EXPECT_FALSE(Delete(no_body_request_.get())); |
| 253 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); |
| 254 EXPECT_TRUE(Match(no_body_request_.get())); |
| 255 EXPECT_TRUE(Delete(no_body_request_.get())); |
| 256 } |
| 257 |
| 258 TEST_P(ServiceWorkerCacheTestP, DeleteBody) { |
| 259 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); |
| 260 EXPECT_TRUE(Match(body_request_.get())); |
| 261 EXPECT_TRUE(Delete(body_request_.get())); |
| 262 EXPECT_FALSE(Match(body_request_.get())); |
| 263 EXPECT_FALSE(Delete(body_request_.get())); |
| 264 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); |
| 265 EXPECT_TRUE(Match(body_request_.get())); |
| 266 EXPECT_TRUE(Delete(body_request_.get())); |
| 267 } |
| 268 |
| 269 TEST_P(ServiceWorkerCacheTestP, MatchNoBody) { |
| 270 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); |
| 271 EXPECT_TRUE(Match(no_body_request_.get())); |
| 272 EXPECT_EQ(200, callback_response_->status_code); |
| 273 EXPECT_STREQ("OK", callback_response_->status_text.c_str()); |
| 274 EXPECT_STREQ("http://example.com/no_body.html", |
| 275 callback_response_->url.spec().c_str()); |
| 276 } |
| 277 |
| 278 TEST_P(ServiceWorkerCacheTestP, MatchBody) { |
| 279 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); |
| 280 EXPECT_TRUE(Match(body_request_.get())); |
| 281 EXPECT_EQ(200, callback_response_->status_code); |
| 282 EXPECT_STREQ("OK", callback_response_->status_text.c_str()); |
| 283 EXPECT_STREQ("http://example.com/body.html", |
| 284 callback_response_->url.spec().c_str()); |
| 285 std::string response_body; |
| 286 CopyBody(callback_response_data_.get(), &response_body); |
| 287 EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str()); |
| 288 } |
| 289 |
| 290 TEST_P(ServiceWorkerCacheTestP, QuickStressNoBody) { |
| 291 for (int i = 0; i < 100; ++i) { |
| 292 EXPECT_FALSE(Match(no_body_request_.get())); |
| 293 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); |
| 294 EXPECT_TRUE(Match(no_body_request_.get())); |
| 295 EXPECT_TRUE(Delete(no_body_request_.get())); |
| 296 } |
| 297 } |
| 298 |
| 299 TEST_P(ServiceWorkerCacheTestP, QuickStressBody) { |
| 300 for (int i = 0; i < 100; ++i) { |
| 301 ASSERT_FALSE(Match(body_request_.get())); |
| 302 ASSERT_TRUE(Put(body_request_.get(), body_response_.get())); |
| 303 ASSERT_TRUE(Match(body_request_.get())); |
| 304 ASSERT_TRUE(Delete(body_request_.get())); |
| 305 } |
| 306 } |
| 307 |
| 308 INSTANTIATE_TEST_CASE_P(ServiceWorkerCacheTest, |
| 309 ServiceWorkerCacheTestP, |
| 310 ::testing::Values(false, true)); |
| 311 |
| 312 } // namespace content |
OLD | NEW |