Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / service_worker / service_worker_cache_storage_manager_unittest.cc
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_storage_manager.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/quota/mock_quota_manager_proxy.h"
13 #include "content/browser/service_worker/service_worker_cache_quota_client.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_getter.h"
18 #include "storage/browser/blob/blob_storage_context.h"
19 #include "storage/browser/quota/quota_manager_proxy.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 namespace content {
23
24 class ServiceWorkerCacheStorageManagerTest : public testing::Test {
25  protected:
26   ServiceWorkerCacheStorageManagerTest()
27       : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
28         callback_bool_(false),
29         callback_error_(
30             ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR),
31         callback_cache_error_(ServiceWorkerCache::ErrorTypeOK),
32         origin1_("http://example1.com"),
33         origin2_("http://example2.com") {}
34
35   void SetUp() override {
36     ChromeBlobStorageContext* blob_storage_context(
37         ChromeBlobStorageContext::GetFor(&browser_context_));
38     // Wait for ChromeBlobStorageContext to finish initializing.
39     base::RunLoop().RunUntilIdle();
40
41     quota_manager_proxy_ = new MockQuotaManagerProxy(
42         nullptr, base::MessageLoopProxy::current().get());
43
44     net::URLRequestContext* url_request_context =
45         browser_context_.GetRequestContext()->GetURLRequestContext();
46     if (MemoryOnly()) {
47       cache_manager_ = ServiceWorkerCacheStorageManager::Create(
48           base::FilePath(),
49           base::MessageLoopProxy::current(),
50           quota_manager_proxy_);
51     } else {
52       ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
53       cache_manager_ = ServiceWorkerCacheStorageManager::Create(
54           temp_dir_.path(),
55           base::MessageLoopProxy::current(),
56           quota_manager_proxy_);
57     }
58
59     cache_manager_->SetBlobParametersForCache(
60         url_request_context, blob_storage_context->context()->AsWeakPtr());
61   }
62
63   void TearDown() override {
64     quota_manager_proxy_->SimulateQuotaManagerDestroyed();
65     base::RunLoop().RunUntilIdle();
66   }
67
68   virtual bool MemoryOnly() { return false; }
69
70   void BoolAndErrorCallback(
71       base::RunLoop* run_loop,
72       bool value,
73       ServiceWorkerCacheStorage::CacheStorageError error) {
74     callback_bool_ = value;
75     callback_error_ = error;
76     run_loop->Quit();
77   }
78
79   void CacheAndErrorCallback(
80       base::RunLoop* run_loop,
81       const scoped_refptr<ServiceWorkerCache>& cache,
82       ServiceWorkerCacheStorage::CacheStorageError error) {
83     callback_cache_ = cache;
84     callback_error_ = error;
85     run_loop->Quit();
86   }
87
88   void StringsAndErrorCallback(
89       base::RunLoop* run_loop,
90       const std::vector<std::string>& strings,
91       ServiceWorkerCacheStorage::CacheStorageError error) {
92     callback_strings_ = strings;
93     callback_error_ = error;
94     run_loop->Quit();
95   }
96
97   void CachePutCallback(base::RunLoop* run_loop,
98                         ServiceWorkerCache::ErrorType error,
99                         scoped_ptr<ServiceWorkerResponse> response,
100                         scoped_ptr<storage::BlobDataHandle> blob_data_handle) {
101     callback_cache_error_ = error;
102     run_loop->Quit();
103   }
104
105   void CacheMatchCallback(
106       base::RunLoop* run_loop,
107       ServiceWorkerCache::ErrorType error,
108       scoped_ptr<ServiceWorkerResponse> response,
109       scoped_ptr<storage::BlobDataHandle> blob_data_handle) {
110     callback_cache_error_ = error;
111     callback_cache_response_ = response.Pass();
112     // Deliberately drop the data handle as only the url is being tested.
113     run_loop->Quit();
114   }
115
116   bool Open(const GURL& origin, const std::string& cache_name) {
117     scoped_ptr<base::RunLoop> loop(new base::RunLoop());
118     cache_manager_->OpenCache(
119         origin,
120         cache_name,
121         base::Bind(&ServiceWorkerCacheStorageManagerTest::CacheAndErrorCallback,
122                    base::Unretained(this),
123                    base::Unretained(loop.get())));
124     loop->Run();
125
126     bool error = callback_error_ !=
127                  ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR;
128     if (error)
129       EXPECT_TRUE(!callback_cache_.get());
130     else
131       EXPECT_TRUE(callback_cache_.get());
132     return !error;
133   }
134
135   bool Has(const GURL& origin, const std::string& cache_name) {
136     scoped_ptr<base::RunLoop> loop(new base::RunLoop());
137     cache_manager_->HasCache(
138         origin,
139         cache_name,
140         base::Bind(&ServiceWorkerCacheStorageManagerTest::BoolAndErrorCallback,
141                    base::Unretained(this),
142                    base::Unretained(loop.get())));
143     loop->Run();
144
145     return callback_bool_;
146   }
147
148   bool Delete(const GURL& origin, const std::string& cache_name) {
149     scoped_ptr<base::RunLoop> loop(new base::RunLoop());
150     cache_manager_->DeleteCache(
151         origin,
152         cache_name,
153         base::Bind(&ServiceWorkerCacheStorageManagerTest::BoolAndErrorCallback,
154                    base::Unretained(this),
155                    base::Unretained(loop.get())));
156     loop->Run();
157
158     return callback_bool_;
159   }
160
161   bool Keys(const GURL& origin) {
162     scoped_ptr<base::RunLoop> loop(new base::RunLoop());
163     cache_manager_->EnumerateCaches(
164         origin,
165         base::Bind(
166             &ServiceWorkerCacheStorageManagerTest::StringsAndErrorCallback,
167             base::Unretained(this),
168             base::Unretained(loop.get())));
169     loop->Run();
170
171     bool error = callback_error_ !=
172                  ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR;
173     return !error;
174   }
175
176   bool CachePut(const scoped_refptr<ServiceWorkerCache>& cache,
177                 const GURL& url) {
178     scoped_ptr<ServiceWorkerFetchRequest> request(
179         new ServiceWorkerFetchRequest());
180     scoped_ptr<ServiceWorkerResponse> response(new ServiceWorkerResponse());
181     request->url = url;
182     response->url = url;
183     scoped_ptr<base::RunLoop> loop(new base::RunLoop());
184     cache->Put(
185         request.Pass(),
186         response.Pass(),
187         base::Bind(&ServiceWorkerCacheStorageManagerTest::CachePutCallback,
188                    base::Unretained(this),
189                    base::Unretained(loop.get())));
190     loop->Run();
191
192     bool error = callback_cache_error_ != ServiceWorkerCache::ErrorTypeOK;
193     return !error;
194   }
195
196   bool CacheMatch(const scoped_refptr<ServiceWorkerCache>& cache,
197                   const GURL& url) {
198     scoped_ptr<ServiceWorkerFetchRequest> request(
199         new ServiceWorkerFetchRequest());
200     request->url = url;
201     scoped_ptr<base::RunLoop> loop(new base::RunLoop());
202     cache->Match(
203         request.Pass(),
204         base::Bind(&ServiceWorkerCacheStorageManagerTest::CacheMatchCallback,
205                    base::Unretained(this),
206                    base::Unretained(loop.get())));
207     loop->Run();
208
209     bool error = callback_cache_error_ != ServiceWorkerCache::ErrorTypeOK;
210     return !error;
211   }
212
213   ServiceWorkerCacheStorage* CacheStorageForOrigin(const GURL& origin) {
214     return cache_manager_->FindOrCreateServiceWorkerCacheManager(origin);
215   }
216
217  protected:
218   TestBrowserContext browser_context_;
219   TestBrowserThreadBundle browser_thread_bundle_;
220
221   base::ScopedTempDir temp_dir_;
222   scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_;
223   scoped_ptr<ServiceWorkerCacheStorageManager> cache_manager_;
224
225   scoped_refptr<ServiceWorkerCache> callback_cache_;
226   int callback_bool_;
227   ServiceWorkerCacheStorage::CacheStorageError callback_error_;
228   ServiceWorkerCache::ErrorType callback_cache_error_;
229   scoped_ptr<ServiceWorkerResponse> callback_cache_response_;
230   std::vector<std::string> callback_strings_;
231
232   const GURL origin1_;
233   const GURL origin2_;
234
235  private:
236   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCacheStorageManagerTest);
237 };
238
239 class ServiceWorkerCacheStorageManagerMemoryOnlyTest
240     : public ServiceWorkerCacheStorageManagerTest {
241   bool MemoryOnly() override { return true; }
242 };
243
244 class ServiceWorkerCacheStorageManagerTestP
245     : public ServiceWorkerCacheStorageManagerTest,
246       public testing::WithParamInterface<bool> {
247   bool MemoryOnly() override { return !GetParam(); }
248 };
249
250 TEST_F(ServiceWorkerCacheStorageManagerTest, TestsRunOnIOThread) {
251   EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
252 }
253
254 TEST_P(ServiceWorkerCacheStorageManagerTestP, OpenCache) {
255   EXPECT_TRUE(Open(origin1_, "foo"));
256 }
257
258 TEST_P(ServiceWorkerCacheStorageManagerTestP, OpenTwoCaches) {
259   EXPECT_TRUE(Open(origin1_, "foo"));
260   EXPECT_TRUE(Open(origin1_, "bar"));
261 }
262
263 TEST_P(ServiceWorkerCacheStorageManagerTestP, CachePointersDiffer) {
264   EXPECT_TRUE(Open(origin1_, "foo"));
265   scoped_refptr<ServiceWorkerCache> cache = callback_cache_;
266   EXPECT_TRUE(Open(origin1_, "bar"));
267   EXPECT_NE(callback_cache_.get(), cache.get());
268 }
269
270 TEST_P(ServiceWorkerCacheStorageManagerTestP, Open2CachesSameNameDiffOrigins) {
271   EXPECT_TRUE(Open(origin1_, "foo"));
272   scoped_refptr<ServiceWorkerCache> cache = callback_cache_;
273   EXPECT_TRUE(Open(origin2_, "foo"));
274   EXPECT_NE(cache.get(), callback_cache_.get());
275 }
276
277 TEST_P(ServiceWorkerCacheStorageManagerTestP, OpenExistingCache) {
278   EXPECT_TRUE(Open(origin1_, "foo"));
279   scoped_refptr<ServiceWorkerCache> cache = callback_cache_;
280   EXPECT_TRUE(Open(origin1_, "foo"));
281   EXPECT_EQ(callback_cache_.get(), cache.get());
282 }
283
284 TEST_P(ServiceWorkerCacheStorageManagerTestP, HasCache) {
285   EXPECT_TRUE(Open(origin1_, "foo"));
286   EXPECT_TRUE(Has(origin1_, "foo"));
287   EXPECT_TRUE(callback_bool_);
288 }
289
290 TEST_P(ServiceWorkerCacheStorageManagerTestP, HasNonExistent) {
291   EXPECT_FALSE(Has(origin1_, "foo"));
292 }
293
294 TEST_P(ServiceWorkerCacheStorageManagerTestP, DeleteCache) {
295   EXPECT_TRUE(Open(origin1_, "foo"));
296   EXPECT_TRUE(Delete(origin1_, "foo"));
297   EXPECT_FALSE(Has(origin1_, "foo"));
298 }
299
300 TEST_P(ServiceWorkerCacheStorageManagerTestP, DeleteTwice) {
301   EXPECT_TRUE(Open(origin1_, "foo"));
302   EXPECT_TRUE(Delete(origin1_, "foo"));
303   EXPECT_FALSE(Delete(origin1_, "foo"));
304   EXPECT_EQ(ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NOT_FOUND,
305             callback_error_);
306 }
307
308 TEST_P(ServiceWorkerCacheStorageManagerTestP, EmptyKeys) {
309   EXPECT_TRUE(Keys(origin1_));
310   EXPECT_TRUE(callback_strings_.empty());
311 }
312
313 TEST_P(ServiceWorkerCacheStorageManagerTestP, SomeKeys) {
314   EXPECT_TRUE(Open(origin1_, "foo"));
315   EXPECT_TRUE(Open(origin1_, "bar"));
316   EXPECT_TRUE(Open(origin2_, "baz"));
317   EXPECT_TRUE(Keys(origin1_));
318   EXPECT_EQ(2u, callback_strings_.size());
319   std::vector<std::string> expected_keys;
320   expected_keys.push_back("foo");
321   expected_keys.push_back("bar");
322   EXPECT_EQ(expected_keys, callback_strings_);
323   EXPECT_TRUE(Keys(origin2_));
324   EXPECT_EQ(1u, callback_strings_.size());
325   EXPECT_STREQ("baz", callback_strings_[0].c_str());
326 }
327
328 TEST_P(ServiceWorkerCacheStorageManagerTestP, DeletedKeysGone) {
329   EXPECT_TRUE(Open(origin1_, "foo"));
330   EXPECT_TRUE(Open(origin1_, "bar"));
331   EXPECT_TRUE(Open(origin2_, "baz"));
332   EXPECT_TRUE(Delete(origin1_, "bar"));
333   EXPECT_TRUE(Keys(origin1_));
334   EXPECT_EQ(1u, callback_strings_.size());
335   EXPECT_STREQ("foo", callback_strings_[0].c_str());
336 }
337
338 TEST_P(ServiceWorkerCacheStorageManagerTestP, Chinese) {
339   EXPECT_TRUE(Open(origin1_, "你好"));
340   scoped_refptr<ServiceWorkerCache> cache = callback_cache_;
341   EXPECT_TRUE(Open(origin1_, "你好"));
342   EXPECT_EQ(callback_cache_.get(), cache.get());
343   EXPECT_TRUE(Keys(origin1_));
344   EXPECT_EQ(1u, callback_strings_.size());
345   EXPECT_STREQ("你好", callback_strings_[0].c_str());
346 }
347
348 TEST_F(ServiceWorkerCacheStorageManagerTest, EmptyKey) {
349   EXPECT_TRUE(Open(origin1_, ""));
350   scoped_refptr<ServiceWorkerCache> cache = callback_cache_;
351   EXPECT_TRUE(Open(origin1_, ""));
352   EXPECT_EQ(cache.get(), callback_cache_.get());
353   EXPECT_TRUE(Keys(origin1_));
354   EXPECT_EQ(1u, callback_strings_.size());
355   EXPECT_STREQ("", callback_strings_[0].c_str());
356   EXPECT_TRUE(Has(origin1_, ""));
357   EXPECT_TRUE(Delete(origin1_, ""));
358   EXPECT_TRUE(Keys(origin1_));
359   EXPECT_EQ(0u, callback_strings_.size());
360 }
361
362 TEST_F(ServiceWorkerCacheStorageManagerTest, DataPersists) {
363   EXPECT_TRUE(Open(origin1_, "foo"));
364   EXPECT_TRUE(Open(origin1_, "bar"));
365   EXPECT_TRUE(Open(origin1_, "baz"));
366   EXPECT_TRUE(Open(origin2_, "raz"));
367   EXPECT_TRUE(Delete(origin1_, "bar"));
368   quota_manager_proxy_->SimulateQuotaManagerDestroyed();
369   cache_manager_ =
370       ServiceWorkerCacheStorageManager::Create(cache_manager_.get());
371   EXPECT_TRUE(Keys(origin1_));
372   EXPECT_EQ(2u, callback_strings_.size());
373   std::vector<std::string> expected_keys;
374   expected_keys.push_back("foo");
375   expected_keys.push_back("baz");
376   EXPECT_EQ(expected_keys, callback_strings_);
377 }
378
379 TEST_F(ServiceWorkerCacheStorageManagerMemoryOnlyTest, DataLostWhenMemoryOnly) {
380   EXPECT_TRUE(Open(origin1_, "foo"));
381   EXPECT_TRUE(Open(origin2_, "baz"));
382   quota_manager_proxy_->SimulateQuotaManagerDestroyed();
383   cache_manager_ =
384       ServiceWorkerCacheStorageManager::Create(cache_manager_.get());
385   EXPECT_TRUE(Keys(origin1_));
386   EXPECT_EQ(0u, callback_strings_.size());
387 }
388
389 TEST_F(ServiceWorkerCacheStorageManagerTest, BadCacheName) {
390   // Since the implementation writes cache names to disk, ensure that we don't
391   // escape the directory.
392   const std::string bad_name = "../../../../../../../../../../../../../../foo";
393   EXPECT_TRUE(Open(origin1_, bad_name));
394   EXPECT_TRUE(Keys(origin1_));
395   EXPECT_EQ(1u, callback_strings_.size());
396   EXPECT_STREQ(bad_name.c_str(), callback_strings_[0].c_str());
397 }
398
399 TEST_F(ServiceWorkerCacheStorageManagerTest, BadOriginName) {
400   // Since the implementation writes origin names to disk, ensure that we don't
401   // escape the directory.
402   GURL bad_origin("http://../../../../../../../../../../../../../../foo");
403   EXPECT_TRUE(Open(bad_origin, "foo"));
404   EXPECT_TRUE(Keys(bad_origin));
405   EXPECT_EQ(1u, callback_strings_.size());
406   EXPECT_STREQ("foo", callback_strings_[0].c_str());
407 }
408
409 // With a persistent cache if the client drops its reference to a
410 // ServiceWorkerCache
411 // it should be deleted.
412 TEST_F(ServiceWorkerCacheStorageManagerTest, DropReference) {
413   EXPECT_TRUE(Open(origin1_, "foo"));
414   base::WeakPtr<ServiceWorkerCache> cache = callback_cache_->AsWeakPtr();
415   callback_cache_ = NULL;
416   EXPECT_TRUE(!cache);
417 }
418
419 // With a memory cache the cache can't be freed from memory until the client
420 // calls delete.
421 TEST_F(ServiceWorkerCacheStorageManagerMemoryOnlyTest,
422        MemoryLosesReferenceOnlyAfterDelete) {
423   EXPECT_TRUE(Open(origin1_, "foo"));
424   base::WeakPtr<ServiceWorkerCache> cache = callback_cache_->AsWeakPtr();
425   callback_cache_ = NULL;
426   EXPECT_TRUE(cache);
427   EXPECT_TRUE(Delete(origin1_, "foo"));
428   EXPECT_FALSE(cache);
429 }
430
431 TEST_P(ServiceWorkerCacheStorageManagerTestP, RecreateCacheOnDemand) {
432   EXPECT_TRUE(Open(origin1_, "foo"));
433   EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo")));
434   callback_cache_ = NULL;
435   EXPECT_TRUE(Open(origin1_, "foo"));
436   EXPECT_TRUE(CacheMatch(callback_cache_, GURL("http://example.com/foo")));
437 }
438
439 TEST_P(ServiceWorkerCacheStorageManagerTestP, DeleteBeforeRelease) {
440   EXPECT_TRUE(Open(origin1_, "foo"));
441   EXPECT_TRUE(Delete(origin1_, "foo"));
442   EXPECT_TRUE(callback_cache_->AsWeakPtr());
443 }
444
445 TEST_F(ServiceWorkerCacheStorageManagerMemoryOnlyTest, MemoryBackedSize) {
446   ServiceWorkerCacheStorage* cache_storage = CacheStorageForOrigin(origin1_);
447   EXPECT_EQ(0, cache_storage->MemoryBackedSize());
448
449   EXPECT_TRUE(Open(origin1_, "foo"));
450   scoped_refptr<ServiceWorkerCache> foo_cache = callback_cache_;
451   EXPECT_TRUE(Open(origin1_, "bar"));
452   scoped_refptr<ServiceWorkerCache> bar_cache = callback_cache_;
453   EXPECT_EQ(0, cache_storage->MemoryBackedSize());
454
455   EXPECT_TRUE(CachePut(foo_cache, GURL("http://example.com/foo")));
456   EXPECT_LT(0, cache_storage->MemoryBackedSize());
457   int64 foo_size = cache_storage->MemoryBackedSize();
458
459   EXPECT_TRUE(CachePut(bar_cache, GURL("http://example.com/foo")));
460   EXPECT_EQ(foo_size * 2, cache_storage->MemoryBackedSize());
461 }
462
463 TEST_F(ServiceWorkerCacheStorageManagerTest, MemoryBackedSizePersistent) {
464   ServiceWorkerCacheStorage* cache_storage = CacheStorageForOrigin(origin1_);
465   EXPECT_EQ(0, cache_storage->MemoryBackedSize());
466   EXPECT_TRUE(Open(origin1_, "foo"));
467   EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo")));
468   EXPECT_EQ(0, cache_storage->MemoryBackedSize());
469 }
470
471 class ServiceWorkerCacheQuotaClientTest
472     : public ServiceWorkerCacheStorageManagerTest {
473  protected:
474   ServiceWorkerCacheQuotaClientTest() {}
475
476   void SetUp() override {
477     ServiceWorkerCacheStorageManagerTest::SetUp();
478     quota_client_.reset(
479         new ServiceWorkerCacheQuotaClient(cache_manager_->AsWeakPtr()));
480   }
481
482   void UsageCallback(base::RunLoop* run_loop, int64 usage) {
483     callback_usage_ = usage;
484     run_loop->Quit();
485   }
486
487   void OriginsCallback(base::RunLoop* run_loop, const std::set<GURL>& origins) {
488     callback_origins_ = origins;
489     run_loop->Quit();
490   }
491
492   void DeleteOriginCallback(base::RunLoop* run_loop,
493                             storage::QuotaStatusCode status) {
494     callback_status_ = status;
495     run_loop->Quit();
496   }
497
498   int64 QuotaGetOriginUsage(const GURL& origin) {
499     scoped_ptr<base::RunLoop> loop(new base::RunLoop());
500     quota_client_->GetOriginUsage(
501         origin,
502         storage::kStorageTypeTemporary,
503         base::Bind(&ServiceWorkerCacheQuotaClientTest::UsageCallback,
504                    base::Unretained(this),
505                    base::Unretained(loop.get())));
506     loop->Run();
507     return callback_usage_;
508   }
509
510   size_t QuotaGetOriginsForType() {
511     scoped_ptr<base::RunLoop> loop(new base::RunLoop());
512     quota_client_->GetOriginsForType(
513         storage::kStorageTypeTemporary,
514         base::Bind(&ServiceWorkerCacheQuotaClientTest::OriginsCallback,
515                    base::Unretained(this),
516                    base::Unretained(loop.get())));
517     loop->Run();
518     return callback_origins_.size();
519   }
520
521   size_t QuotaGetOriginsForHost(const std::string& host) {
522     scoped_ptr<base::RunLoop> loop(new base::RunLoop());
523     quota_client_->GetOriginsForHost(
524         storage::kStorageTypeTemporary,
525         host,
526         base::Bind(&ServiceWorkerCacheQuotaClientTest::OriginsCallback,
527                    base::Unretained(this),
528                    base::Unretained(loop.get())));
529     loop->Run();
530     return callback_origins_.size();
531   }
532
533   bool QuotaDeleteOriginData(const GURL& origin) {
534     scoped_ptr<base::RunLoop> loop(new base::RunLoop());
535     quota_client_->DeleteOriginData(
536         origin,
537         storage::kStorageTypeTemporary,
538         base::Bind(&ServiceWorkerCacheQuotaClientTest::DeleteOriginCallback,
539                    base::Unretained(this),
540                    base::Unretained(loop.get())));
541     loop->Run();
542     return callback_status_ == storage::kQuotaStatusOk;
543   }
544
545   bool QuotaDoesSupport(storage::StorageType type) {
546     return quota_client_->DoesSupport(type);
547   }
548
549   scoped_ptr<ServiceWorkerCacheQuotaClient> quota_client_;
550
551   storage::QuotaStatusCode callback_status_;
552   int64 callback_usage_;
553   std::set<GURL> callback_origins_;
554
555   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCacheQuotaClientTest);
556 };
557
558 class ServiceWorkerCacheQuotaClientTestP
559     : public ServiceWorkerCacheQuotaClientTest,
560       public testing::WithParamInterface<bool> {
561   bool MemoryOnly() override { return !GetParam(); }
562 };
563
564 TEST_P(ServiceWorkerCacheQuotaClientTestP, QuotaID) {
565   EXPECT_EQ(storage::QuotaClient::kServiceWorkerCache, quota_client_->id());
566 }
567
568 TEST_P(ServiceWorkerCacheQuotaClientTestP, QuotaGetOriginUsage) {
569   EXPECT_EQ(0, QuotaGetOriginUsage(origin1_));
570   EXPECT_TRUE(Open(origin1_, "foo"));
571   EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo")));
572   EXPECT_LT(0, QuotaGetOriginUsage(origin1_));
573 }
574
575 TEST_P(ServiceWorkerCacheQuotaClientTestP, QuotaGetOriginsForType) {
576   EXPECT_EQ(0u, QuotaGetOriginsForType());
577   EXPECT_TRUE(Open(origin1_, "foo"));
578   EXPECT_TRUE(Open(origin1_, "bar"));
579   EXPECT_TRUE(Open(origin2_, "foo"));
580   EXPECT_EQ(2u, QuotaGetOriginsForType());
581 }
582
583 TEST_P(ServiceWorkerCacheQuotaClientTestP, QuotaGetOriginsForHost) {
584   EXPECT_EQ(0u, QuotaGetOriginsForHost("example.com"));
585   EXPECT_TRUE(Open(GURL("http://example.com:8080"), "foo"));
586   EXPECT_TRUE(Open(GURL("http://example.com:9000"), "foo"));
587   EXPECT_TRUE(Open(GURL("ftp://example.com"), "foo"));
588   EXPECT_TRUE(Open(GURL("http://example2.com"), "foo"));
589   EXPECT_EQ(3u, QuotaGetOriginsForHost("example.com"));
590   EXPECT_EQ(1u, QuotaGetOriginsForHost("example2.com"));
591   EXPECT_TRUE(callback_origins_.find(GURL("http://example2.com")) !=
592               callback_origins_.end());
593   EXPECT_EQ(0u, QuotaGetOriginsForHost("unknown.com"));
594 }
595
596 TEST_P(ServiceWorkerCacheQuotaClientTestP, QuotaDeleteOriginData) {
597   EXPECT_TRUE(Open(origin1_, "foo"));
598   // Call put to test that initialized caches are properly deleted too.
599   EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo")));
600   EXPECT_TRUE(Open(origin1_, "bar"));
601   EXPECT_TRUE(Open(origin2_, "baz"));
602
603   EXPECT_TRUE(QuotaDeleteOriginData(origin1_));
604
605   EXPECT_FALSE(Has(origin1_, "foo"));
606   EXPECT_FALSE(Has(origin1_, "bar"));
607   EXPECT_TRUE(Has(origin2_, "baz"));
608   EXPECT_TRUE(Open(origin1_, "foo"));
609 }
610
611 TEST_P(ServiceWorkerCacheQuotaClientTestP, QuotaDeleteEmptyOrigin) {
612   EXPECT_TRUE(QuotaDeleteOriginData(origin1_));
613 }
614
615 TEST_P(ServiceWorkerCacheQuotaClientTestP, QuotaDoesSupport) {
616   EXPECT_TRUE(QuotaDoesSupport(storage::kStorageTypeTemporary));
617   EXPECT_FALSE(QuotaDoesSupport(storage::kStorageTypePersistent));
618   EXPECT_FALSE(QuotaDoesSupport(storage::kStorageTypeSyncable));
619   EXPECT_FALSE(QuotaDoesSupport(storage::kStorageTypeQuotaNotManaged));
620   EXPECT_FALSE(QuotaDoesSupport(storage::kStorageTypeUnknown));
621 }
622
623 INSTANTIATE_TEST_CASE_P(ServiceWorkerCacheStorageManagerTests,
624                         ServiceWorkerCacheStorageManagerTestP,
625                         ::testing::Values(false, true));
626
627 INSTANTIATE_TEST_CASE_P(ServiceWorkerCacheQuotaClientTests,
628                         ServiceWorkerCacheQuotaClientTestP,
629                         ::testing::Values(false, true));
630
631 }  // namespace content