| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/cache_storage/cache_storage_manager.h" | 5 #include "content/browser/cache_storage/cache_storage_manager.h" |
| 6 | 6 |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "base/thread_task_runner_handle.h" | 12 #include "base/thread_task_runner_handle.h" |
| 13 #include "content/browser/cache_storage/cache_storage_quota_client.h" | 13 #include "content/browser/cache_storage/cache_storage_quota_client.h" |
| 14 #include "content/browser/fileapi/chrome_blob_storage_context.h" | 14 #include "content/browser/fileapi/chrome_blob_storage_context.h" |
| 15 #include "content/browser/quota/mock_quota_manager_proxy.h" | 15 #include "content/browser/quota/mock_quota_manager_proxy.h" |
| 16 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/browser/cache_storage_usage_info.h" |
| 17 #include "content/public/test/test_browser_context.h" | 18 #include "content/public/test/test_browser_context.h" |
| 18 #include "content/public/test/test_browser_thread_bundle.h" | 19 #include "content/public/test/test_browser_thread_bundle.h" |
| 19 #include "net/url_request/url_request_context_getter.h" | 20 #include "net/url_request/url_request_context_getter.h" |
| 20 #include "storage/browser/blob/blob_storage_context.h" | 21 #include "storage/browser/blob/blob_storage_context.h" |
| 21 #include "storage/browser/quota/quota_manager_proxy.h" | 22 #include "storage/browser/quota/quota_manager_proxy.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 24 |
| 24 namespace content { | 25 namespace content { |
| 25 | 26 |
| 26 class CacheStorageManagerTest : public testing::Test { | 27 class CacheStorageManagerTest : public testing::Test { |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 CacheStorageError error, | 99 CacheStorageError error, |
| 99 scoped_ptr<ServiceWorkerResponse> response, | 100 scoped_ptr<ServiceWorkerResponse> response, |
| 100 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { | 101 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { |
| 101 callback_error_ = error; | 102 callback_error_ = error; |
| 102 callback_cache_response_ = response.Pass(); | 103 callback_cache_response_ = response.Pass(); |
| 103 // Deliberately drop the data handle as only the url is being tested. | 104 // Deliberately drop the data handle as only the url is being tested. |
| 104 run_loop->Quit(); | 105 run_loop->Quit(); |
| 105 } | 106 } |
| 106 | 107 |
| 107 bool Open(const GURL& origin, const std::string& cache_name) { | 108 bool Open(const GURL& origin, const std::string& cache_name) { |
| 108 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 109 base::RunLoop loop; |
| 109 cache_manager_->OpenCache( | 110 cache_manager_->OpenCache( |
| 110 origin, cache_name, | 111 origin, cache_name, |
| 111 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback, | 112 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback, |
| 112 base::Unretained(this), base::Unretained(loop.get()))); | 113 base::Unretained(this), base::Unretained(&loop))); |
| 113 loop->Run(); | 114 loop.Run(); |
| 114 | 115 |
| 115 bool error = callback_error_ != CACHE_STORAGE_OK; | 116 bool error = callback_error_ != CACHE_STORAGE_OK; |
| 116 if (error) | 117 if (error) |
| 117 EXPECT_TRUE(!callback_cache_.get()); | 118 EXPECT_TRUE(!callback_cache_.get()); |
| 118 else | 119 else |
| 119 EXPECT_TRUE(callback_cache_.get()); | 120 EXPECT_TRUE(callback_cache_.get()); |
| 120 return !error; | 121 return !error; |
| 121 } | 122 } |
| 122 | 123 |
| 123 bool Has(const GURL& origin, const std::string& cache_name) { | 124 bool Has(const GURL& origin, const std::string& cache_name) { |
| 124 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 125 base::RunLoop loop; |
| 125 cache_manager_->HasCache( | 126 cache_manager_->HasCache( |
| 126 origin, cache_name, | 127 origin, cache_name, |
| 127 base::Bind(&CacheStorageManagerTest::BoolAndErrorCallback, | 128 base::Bind(&CacheStorageManagerTest::BoolAndErrorCallback, |
| 128 base::Unretained(this), base::Unretained(loop.get()))); | 129 base::Unretained(this), base::Unretained(&loop))); |
| 129 loop->Run(); | 130 loop.Run(); |
| 130 | 131 |
| 131 return callback_bool_; | 132 return callback_bool_; |
| 132 } | 133 } |
| 133 | 134 |
| 134 bool Delete(const GURL& origin, const std::string& cache_name) { | 135 bool Delete(const GURL& origin, const std::string& cache_name) { |
| 135 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 136 base::RunLoop loop; |
| 136 cache_manager_->DeleteCache( | 137 cache_manager_->DeleteCache( |
| 137 origin, cache_name, | 138 origin, cache_name, |
| 138 base::Bind(&CacheStorageManagerTest::BoolAndErrorCallback, | 139 base::Bind(&CacheStorageManagerTest::BoolAndErrorCallback, |
| 139 base::Unretained(this), base::Unretained(loop.get()))); | 140 base::Unretained(this), base::Unretained(&loop))); |
| 140 loop->Run(); | 141 loop.Run(); |
| 141 | 142 |
| 142 return callback_bool_; | 143 return callback_bool_; |
| 143 } | 144 } |
| 144 | 145 |
| 145 bool Keys(const GURL& origin) { | 146 bool Keys(const GURL& origin) { |
| 146 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 147 base::RunLoop loop; |
| 147 cache_manager_->EnumerateCaches( | 148 cache_manager_->EnumerateCaches( |
| 148 origin, | 149 origin, base::Bind(&CacheStorageManagerTest::StringsAndErrorCallback, |
| 149 base::Bind(&CacheStorageManagerTest::StringsAndErrorCallback, | 150 base::Unretained(this), base::Unretained(&loop))); |
| 150 base::Unretained(this), base::Unretained(loop.get()))); | 151 loop.Run(); |
| 151 loop->Run(); | |
| 152 | 152 |
| 153 return callback_error_ == CACHE_STORAGE_OK; | 153 return callback_error_ == CACHE_STORAGE_OK; |
| 154 } | 154 } |
| 155 | 155 |
| 156 bool StorageMatch(const GURL& origin, | 156 bool StorageMatch(const GURL& origin, |
| 157 const std::string& cache_name, | 157 const std::string& cache_name, |
| 158 const GURL& url) { | 158 const GURL& url) { |
| 159 scoped_ptr<ServiceWorkerFetchRequest> request( | 159 scoped_ptr<ServiceWorkerFetchRequest> request( |
| 160 new ServiceWorkerFetchRequest()); | 160 new ServiceWorkerFetchRequest()); |
| 161 request->url = url; | 161 request->url = url; |
| 162 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 162 base::RunLoop loop; |
| 163 cache_manager_->MatchCache( | 163 cache_manager_->MatchCache( |
| 164 origin, cache_name, request.Pass(), | 164 origin, cache_name, request.Pass(), |
| 165 base::Bind(&CacheStorageManagerTest::CacheMatchCallback, | 165 base::Bind(&CacheStorageManagerTest::CacheMatchCallback, |
| 166 base::Unretained(this), base::Unretained(loop.get()))); | 166 base::Unretained(this), base::Unretained(&loop))); |
| 167 loop->Run(); | 167 loop.Run(); |
| 168 | 168 |
| 169 return callback_error_ == CACHE_STORAGE_OK; | 169 return callback_error_ == CACHE_STORAGE_OK; |
| 170 } | 170 } |
| 171 | 171 |
| 172 bool StorageMatchAll(const GURL& origin, const GURL& url) { | 172 bool StorageMatchAll(const GURL& origin, const GURL& url) { |
| 173 scoped_ptr<ServiceWorkerFetchRequest> request( | 173 scoped_ptr<ServiceWorkerFetchRequest> request( |
| 174 new ServiceWorkerFetchRequest()); | 174 new ServiceWorkerFetchRequest()); |
| 175 request->url = url; | 175 request->url = url; |
| 176 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 176 base::RunLoop loop; |
| 177 cache_manager_->MatchAllCaches( | 177 cache_manager_->MatchAllCaches( |
| 178 origin, request.Pass(), | 178 origin, request.Pass(), |
| 179 base::Bind(&CacheStorageManagerTest::CacheMatchCallback, | 179 base::Bind(&CacheStorageManagerTest::CacheMatchCallback, |
| 180 base::Unretained(this), base::Unretained(loop.get()))); | 180 base::Unretained(this), base::Unretained(&loop))); |
| 181 loop->Run(); | 181 loop.Run(); |
| 182 | 182 |
| 183 return callback_error_ == CACHE_STORAGE_OK; | 183 return callback_error_ == CACHE_STORAGE_OK; |
| 184 } | 184 } |
| 185 | 185 |
| 186 bool CachePut(const scoped_refptr<CacheStorageCache>& cache, | 186 bool CachePut(const scoped_refptr<CacheStorageCache>& cache, |
| 187 const GURL& url) { | 187 const GURL& url) { |
| 188 ServiceWorkerFetchRequest request; | 188 ServiceWorkerFetchRequest request; |
| 189 ServiceWorkerResponse response; | 189 ServiceWorkerResponse response; |
| 190 request.url = url; | 190 request.url = url; |
| 191 response.url = url; | 191 response.url = url; |
| 192 | 192 |
| 193 CacheStorageBatchOperation operation; | 193 CacheStorageBatchOperation operation; |
| 194 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; | 194 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; |
| 195 operation.request = request; | 195 operation.request = request; |
| 196 operation.response = response; | 196 operation.response = response; |
| 197 | 197 |
| 198 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 198 base::RunLoop loop; |
| 199 cache->BatchOperation( | 199 cache->BatchOperation( |
| 200 std::vector<CacheStorageBatchOperation>(1, operation), | 200 std::vector<CacheStorageBatchOperation>(1, operation), |
| 201 base::Bind(&CacheStorageManagerTest::CachePutCallback, | 201 base::Bind(&CacheStorageManagerTest::CachePutCallback, |
| 202 base::Unretained(this), base::Unretained(loop.get()))); | 202 base::Unretained(this), base::Unretained(&loop))); |
| 203 loop->Run(); | 203 loop.Run(); |
| 204 | 204 |
| 205 return callback_error_ == CACHE_STORAGE_OK; | 205 return callback_error_ == CACHE_STORAGE_OK; |
| 206 } | 206 } |
| 207 | 207 |
| 208 bool CacheMatch(const scoped_refptr<CacheStorageCache>& cache, | 208 bool CacheMatch(const scoped_refptr<CacheStorageCache>& cache, |
| 209 const GURL& url) { | 209 const GURL& url) { |
| 210 scoped_ptr<ServiceWorkerFetchRequest> request( | 210 scoped_ptr<ServiceWorkerFetchRequest> request( |
| 211 new ServiceWorkerFetchRequest()); | 211 new ServiceWorkerFetchRequest()); |
| 212 request->url = url; | 212 request->url = url; |
| 213 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 213 base::RunLoop loop; |
| 214 cache->Match( | 214 cache->Match(request.Pass(), |
| 215 request.Pass(), | 215 base::Bind(&CacheStorageManagerTest::CacheMatchCallback, |
| 216 base::Bind(&CacheStorageManagerTest::CacheMatchCallback, | 216 base::Unretained(this), base::Unretained(&loop))); |
| 217 base::Unretained(this), base::Unretained(loop.get()))); | 217 loop.Run(); |
| 218 loop->Run(); | |
| 219 | 218 |
| 220 return callback_error_ == CACHE_STORAGE_OK; | 219 return callback_error_ == CACHE_STORAGE_OK; |
| 221 } | 220 } |
| 222 | 221 |
| 223 CacheStorage* CacheStorageForOrigin(const GURL& origin) { | 222 CacheStorage* CacheStorageForOrigin(const GURL& origin) { |
| 224 return cache_manager_->FindOrCreateCacheStorage(origin); | 223 return cache_manager_->FindOrCreateCacheStorage(origin); |
| 225 } | 224 } |
| 226 | 225 |
| 226 int64 GetOriginUsage(const GURL& origin) { |
| 227 base::RunLoop loop; |
| 228 cache_manager_->GetOriginUsage( |
| 229 origin, base::Bind(&CacheStorageManagerTest::UsageCallback, |
| 230 base::Unretained(this), base::Unretained(&loop))); |
| 231 loop.Run(); |
| 232 return callback_usage_; |
| 233 } |
| 234 |
| 235 void UsageCallback(base::RunLoop* run_loop, int64 usage) { |
| 236 callback_usage_ = usage; |
| 237 run_loop->Quit(); |
| 238 } |
| 239 |
| 240 std::vector<CacheStorageUsageInfo> GetAllOriginsUsage() { |
| 241 base::RunLoop loop; |
| 242 cache_manager_->GetAllOriginsUsage( |
| 243 base::Bind(&CacheStorageManagerTest::AllOriginsUsageCallback, |
| 244 base::Unretained(this), base::Unretained(&loop))); |
| 245 loop.Run(); |
| 246 return callback_all_origins_usage_; |
| 247 } |
| 248 |
| 249 void AllOriginsUsageCallback( |
| 250 base::RunLoop* run_loop, |
| 251 const std::vector<CacheStorageUsageInfo>& usage) { |
| 252 callback_all_origins_usage_ = usage; |
| 253 run_loop->Quit(); |
| 254 } |
| 255 |
| 227 protected: | 256 protected: |
| 228 TestBrowserContext browser_context_; | 257 TestBrowserContext browser_context_; |
| 229 TestBrowserThreadBundle browser_thread_bundle_; | 258 TestBrowserThreadBundle browser_thread_bundle_; |
| 230 | 259 |
| 231 base::ScopedTempDir temp_dir_; | 260 base::ScopedTempDir temp_dir_; |
| 232 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; | 261 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; |
| 233 scoped_ptr<CacheStorageManager> cache_manager_; | 262 scoped_ptr<CacheStorageManager> cache_manager_; |
| 234 | 263 |
| 235 scoped_refptr<CacheStorageCache> callback_cache_; | 264 scoped_refptr<CacheStorageCache> callback_cache_; |
| 236 int callback_bool_; | 265 int callback_bool_; |
| 237 CacheStorageError callback_error_; | 266 CacheStorageError callback_error_; |
| 238 scoped_ptr<ServiceWorkerResponse> callback_cache_response_; | 267 scoped_ptr<ServiceWorkerResponse> callback_cache_response_; |
| 239 std::vector<std::string> callback_strings_; | 268 std::vector<std::string> callback_strings_; |
| 240 | 269 |
| 241 const GURL origin1_; | 270 const GURL origin1_; |
| 242 const GURL origin2_; | 271 const GURL origin2_; |
| 243 | 272 |
| 273 int64 callback_usage_; |
| 274 std::vector<CacheStorageUsageInfo> callback_all_origins_usage_; |
| 275 |
| 244 private: | 276 private: |
| 245 DISALLOW_COPY_AND_ASSIGN(CacheStorageManagerTest); | 277 DISALLOW_COPY_AND_ASSIGN(CacheStorageManagerTest); |
| 246 }; | 278 }; |
| 247 | 279 |
| 248 class CacheStorageManagerMemoryOnlyTest : public CacheStorageManagerTest { | 280 class CacheStorageManagerMemoryOnlyTest : public CacheStorageManagerTest { |
| 281 public: |
| 249 bool MemoryOnly() override { return true; } | 282 bool MemoryOnly() override { return true; } |
| 250 }; | 283 }; |
| 251 | 284 |
| 252 class CacheStorageManagerTestP : public CacheStorageManagerTest, | 285 class CacheStorageManagerTestP : public CacheStorageManagerTest, |
| 253 public testing::WithParamInterface<bool> { | 286 public testing::WithParamInterface<bool> { |
| 287 public: |
| 254 bool MemoryOnly() override { return !GetParam(); } | 288 bool MemoryOnly() override { return !GetParam(); } |
| 255 }; | 289 }; |
| 256 | 290 |
| 257 TEST_F(CacheStorageManagerTest, TestsRunOnIOThread) { | 291 TEST_F(CacheStorageManagerTest, TestsRunOnIOThread) { |
| 258 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 292 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 259 } | 293 } |
| 260 | 294 |
| 261 TEST_P(CacheStorageManagerTestP, OpenCache) { | 295 TEST_P(CacheStorageManagerTestP, OpenCache) { |
| 262 EXPECT_TRUE(Open(origin1_, "foo")); | 296 EXPECT_TRUE(Open(origin1_, "foo")); |
| 263 } | 297 } |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 EXPECT_TRUE(Open(origin1_, "foo")); | 525 EXPECT_TRUE(Open(origin1_, "foo")); |
| 492 EXPECT_TRUE(Delete(origin1_, "foo")); | 526 EXPECT_TRUE(Delete(origin1_, "foo")); |
| 493 EXPECT_TRUE(callback_cache_->AsWeakPtr()); | 527 EXPECT_TRUE(callback_cache_->AsWeakPtr()); |
| 494 } | 528 } |
| 495 | 529 |
| 496 TEST_P(CacheStorageManagerTestP, OpenRunsSerially) { | 530 TEST_P(CacheStorageManagerTestP, OpenRunsSerially) { |
| 497 EXPECT_FALSE(Delete(origin1_, "tmp")); // Init storage. | 531 EXPECT_FALSE(Delete(origin1_, "tmp")); // Init storage. |
| 498 CacheStorage* cache_storage = CacheStorageForOrigin(origin1_); | 532 CacheStorage* cache_storage = CacheStorageForOrigin(origin1_); |
| 499 cache_storage->StartAsyncOperationForTesting(); | 533 cache_storage->StartAsyncOperationForTesting(); |
| 500 | 534 |
| 501 scoped_ptr<base::RunLoop> open_loop(new base::RunLoop()); | 535 base::RunLoop open_loop; |
| 502 cache_manager_->OpenCache( | 536 cache_manager_->OpenCache( |
| 503 origin1_, "foo", | 537 origin1_, "foo", |
| 504 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback, | 538 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback, |
| 505 base::Unretained(this), base::Unretained(open_loop.get()))); | 539 base::Unretained(this), base::Unretained(&open_loop))); |
| 506 | 540 |
| 507 base::RunLoop().RunUntilIdle(); | 541 base::RunLoop().RunUntilIdle(); |
| 508 EXPECT_FALSE(callback_cache_); | 542 EXPECT_FALSE(callback_cache_); |
| 509 | 543 |
| 510 cache_storage->CompleteAsyncOperationForTesting(); | 544 cache_storage->CompleteAsyncOperationForTesting(); |
| 511 open_loop->Run(); | 545 open_loop.Run(); |
| 512 EXPECT_TRUE(callback_cache_); | 546 EXPECT_TRUE(callback_cache_); |
| 513 } | 547 } |
| 514 | 548 |
| 549 TEST_P(CacheStorageManagerTestP, GetOriginUsage) { |
| 550 EXPECT_EQ(0, GetOriginUsage(origin1_)); |
| 551 EXPECT_TRUE(Open(origin1_, "foo")); |
| 552 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo"))); |
| 553 EXPECT_LT(0, GetOriginUsage(origin1_)); |
| 554 EXPECT_EQ(0, GetOriginUsage(origin2_)); |
| 555 } |
| 556 |
| 557 TEST_P(CacheStorageManagerTestP, GetAllOriginsUsage) { |
| 558 EXPECT_EQ(0ULL, GetAllOriginsUsage().size()); |
| 559 EXPECT_TRUE(Open(origin1_, "foo")); |
| 560 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo"))); |
| 561 std::vector<CacheStorageUsageInfo> usage = GetAllOriginsUsage(); |
| 562 EXPECT_EQ(1ULL, usage.size()); |
| 563 const CacheStorageUsageInfo& info = usage[0]; |
| 564 EXPECT_EQ(origin1_, info.origin); |
| 565 EXPECT_LT(0, info.total_size_bytes); |
| 566 if (MemoryOnly()) |
| 567 EXPECT_TRUE(info.last_modified.is_null()); |
| 568 else |
| 569 EXPECT_FALSE(info.last_modified.is_null()); |
| 570 } |
| 571 |
| 515 TEST_F(CacheStorageManagerMemoryOnlyTest, MemoryBackedSize) { | 572 TEST_F(CacheStorageManagerMemoryOnlyTest, MemoryBackedSize) { |
| 516 CacheStorage* cache_storage = CacheStorageForOrigin(origin1_); | 573 CacheStorage* cache_storage = CacheStorageForOrigin(origin1_); |
| 517 EXPECT_EQ(0, cache_storage->MemoryBackedSize()); | 574 EXPECT_EQ(0, cache_storage->MemoryBackedSize()); |
| 518 | 575 |
| 519 EXPECT_TRUE(Open(origin1_, "foo")); | 576 EXPECT_TRUE(Open(origin1_, "foo")); |
| 520 scoped_refptr<CacheStorageCache> foo_cache = callback_cache_; | 577 scoped_refptr<CacheStorageCache> foo_cache = callback_cache_; |
| 521 EXPECT_TRUE(Open(origin1_, "bar")); | 578 EXPECT_TRUE(Open(origin1_, "bar")); |
| 522 scoped_refptr<CacheStorageCache> bar_cache = callback_cache_; | 579 scoped_refptr<CacheStorageCache> bar_cache = callback_cache_; |
| 523 EXPECT_EQ(0, cache_storage->MemoryBackedSize()); | 580 EXPECT_EQ(0, cache_storage->MemoryBackedSize()); |
| 524 | 581 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 561 ASSERT_TRUE(base::DirectoryExists(new_path_)); | 618 ASSERT_TRUE(base::DirectoryExists(new_path_)); |
| 562 | 619 |
| 563 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); | 620 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); |
| 564 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); | 621 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); |
| 565 | 622 |
| 566 ASSERT_TRUE(base::Move(new_path_, legacy_path_)); | 623 ASSERT_TRUE(base::Move(new_path_, legacy_path_)); |
| 567 ASSERT_TRUE(base::DirectoryExists(legacy_path_)); | 624 ASSERT_TRUE(base::DirectoryExists(legacy_path_)); |
| 568 ASSERT_FALSE(base::DirectoryExists(new_path_)); | 625 ASSERT_FALSE(base::DirectoryExists(new_path_)); |
| 569 } | 626 } |
| 570 | 627 |
| 571 int64 GetOriginUsage(const GURL& origin) { | |
| 572 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | |
| 573 cache_manager_->GetOriginUsage( | |
| 574 origin, | |
| 575 base::Bind(&CacheStorageMigrationTest::UsageCallback, | |
| 576 base::Unretained(this), base::Unretained(loop.get()))); | |
| 577 loop->Run(); | |
| 578 return callback_usage_; | |
| 579 } | |
| 580 | |
| 581 void UsageCallback(base::RunLoop* run_loop, int64 usage) { | |
| 582 callback_usage_ = usage; | |
| 583 run_loop->Quit(); | |
| 584 } | |
| 585 | |
| 586 base::FilePath legacy_path_; | 628 base::FilePath legacy_path_; |
| 587 base::FilePath new_path_; | 629 base::FilePath new_path_; |
| 588 | 630 |
| 589 const std::string cache1_; | 631 const std::string cache1_; |
| 590 const std::string cache2_; | 632 const std::string cache2_; |
| 591 | 633 |
| 592 int64 callback_usage_; | |
| 593 | |
| 594 DISALLOW_COPY_AND_ASSIGN(CacheStorageMigrationTest); | 634 DISALLOW_COPY_AND_ASSIGN(CacheStorageMigrationTest); |
| 595 }; | 635 }; |
| 596 | 636 |
| 597 TEST_F(CacheStorageMigrationTest, OpenCache) { | 637 TEST_F(CacheStorageMigrationTest, OpenCache) { |
| 598 EXPECT_TRUE(Open(origin1_, cache1_)); | 638 EXPECT_TRUE(Open(origin1_, cache1_)); |
| 599 EXPECT_FALSE(base::DirectoryExists(legacy_path_)); | 639 EXPECT_FALSE(base::DirectoryExists(legacy_path_)); |
| 600 EXPECT_TRUE(base::DirectoryExists(new_path_)); | 640 EXPECT_TRUE(base::DirectoryExists(new_path_)); |
| 601 | 641 |
| 602 EXPECT_TRUE(Keys(origin1_)); | 642 EXPECT_TRUE(Keys(origin1_)); |
| 603 std::vector<std::string> expected_keys; | 643 std::vector<std::string> expected_keys; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 648 class CacheStorageQuotaClientTest : public CacheStorageManagerTest { | 688 class CacheStorageQuotaClientTest : public CacheStorageManagerTest { |
| 649 protected: | 689 protected: |
| 650 CacheStorageQuotaClientTest() {} | 690 CacheStorageQuotaClientTest() {} |
| 651 | 691 |
| 652 void SetUp() override { | 692 void SetUp() override { |
| 653 CacheStorageManagerTest::SetUp(); | 693 CacheStorageManagerTest::SetUp(); |
| 654 quota_client_.reset( | 694 quota_client_.reset( |
| 655 new CacheStorageQuotaClient(cache_manager_->AsWeakPtr())); | 695 new CacheStorageQuotaClient(cache_manager_->AsWeakPtr())); |
| 656 } | 696 } |
| 657 | 697 |
| 658 void UsageCallback(base::RunLoop* run_loop, int64 usage) { | 698 void QuotaUsageCallback(base::RunLoop* run_loop, int64 usage) { |
| 659 callback_usage_ = usage; | 699 callback_quota_usage_ = usage; |
| 660 run_loop->Quit(); | 700 run_loop->Quit(); |
| 661 } | 701 } |
| 662 | 702 |
| 663 void OriginsCallback(base::RunLoop* run_loop, const std::set<GURL>& origins) { | 703 void OriginsCallback(base::RunLoop* run_loop, const std::set<GURL>& origins) { |
| 664 callback_origins_ = origins; | 704 callback_origins_ = origins; |
| 665 run_loop->Quit(); | 705 run_loop->Quit(); |
| 666 } | 706 } |
| 667 | 707 |
| 668 void DeleteOriginCallback(base::RunLoop* run_loop, | 708 void DeleteOriginCallback(base::RunLoop* run_loop, |
| 669 storage::QuotaStatusCode status) { | 709 storage::QuotaStatusCode status) { |
| 670 callback_status_ = status; | 710 callback_status_ = status; |
| 671 run_loop->Quit(); | 711 run_loop->Quit(); |
| 672 } | 712 } |
| 673 | 713 |
| 674 int64 QuotaGetOriginUsage(const GURL& origin) { | 714 int64 QuotaGetOriginUsage(const GURL& origin) { |
| 675 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 715 base::RunLoop loop; |
| 676 quota_client_->GetOriginUsage( | 716 quota_client_->GetOriginUsage( |
| 677 origin, storage::kStorageTypeTemporary, | 717 origin, storage::kStorageTypeTemporary, |
| 678 base::Bind(&CacheStorageQuotaClientTest::UsageCallback, | 718 base::Bind(&CacheStorageQuotaClientTest::QuotaUsageCallback, |
| 679 base::Unretained(this), base::Unretained(loop.get()))); | 719 base::Unretained(this), base::Unretained(&loop))); |
| 680 loop->Run(); | 720 loop.Run(); |
| 681 return callback_usage_; | 721 return callback_quota_usage_; |
| 682 } | 722 } |
| 683 | 723 |
| 684 size_t QuotaGetOriginsForType() { | 724 size_t QuotaGetOriginsForType() { |
| 685 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 725 base::RunLoop loop; |
| 686 quota_client_->GetOriginsForType( | 726 quota_client_->GetOriginsForType( |
| 687 storage::kStorageTypeTemporary, | 727 storage::kStorageTypeTemporary, |
| 688 base::Bind(&CacheStorageQuotaClientTest::OriginsCallback, | 728 base::Bind(&CacheStorageQuotaClientTest::OriginsCallback, |
| 689 base::Unretained(this), base::Unretained(loop.get()))); | 729 base::Unretained(this), base::Unretained(&loop))); |
| 690 loop->Run(); | 730 loop.Run(); |
| 691 return callback_origins_.size(); | 731 return callback_origins_.size(); |
| 692 } | 732 } |
| 693 | 733 |
| 694 size_t QuotaGetOriginsForHost(const std::string& host) { | 734 size_t QuotaGetOriginsForHost(const std::string& host) { |
| 695 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 735 base::RunLoop loop; |
| 696 quota_client_->GetOriginsForHost( | 736 quota_client_->GetOriginsForHost( |
| 697 storage::kStorageTypeTemporary, host, | 737 storage::kStorageTypeTemporary, host, |
| 698 base::Bind(&CacheStorageQuotaClientTest::OriginsCallback, | 738 base::Bind(&CacheStorageQuotaClientTest::OriginsCallback, |
| 699 base::Unretained(this), base::Unretained(loop.get()))); | 739 base::Unretained(this), base::Unretained(&loop))); |
| 700 loop->Run(); | 740 loop.Run(); |
| 701 return callback_origins_.size(); | 741 return callback_origins_.size(); |
| 702 } | 742 } |
| 703 | 743 |
| 704 bool QuotaDeleteOriginData(const GURL& origin) { | 744 bool QuotaDeleteOriginData(const GURL& origin) { |
| 705 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | 745 base::RunLoop loop; |
| 706 quota_client_->DeleteOriginData( | 746 quota_client_->DeleteOriginData( |
| 707 origin, storage::kStorageTypeTemporary, | 747 origin, storage::kStorageTypeTemporary, |
| 708 base::Bind(&CacheStorageQuotaClientTest::DeleteOriginCallback, | 748 base::Bind(&CacheStorageQuotaClientTest::DeleteOriginCallback, |
| 709 base::Unretained(this), base::Unretained(loop.get()))); | 749 base::Unretained(this), base::Unretained(&loop))); |
| 710 loop->Run(); | 750 loop.Run(); |
| 711 return callback_status_ == storage::kQuotaStatusOk; | 751 return callback_status_ == storage::kQuotaStatusOk; |
| 712 } | 752 } |
| 713 | 753 |
| 714 bool QuotaDoesSupport(storage::StorageType type) { | 754 bool QuotaDoesSupport(storage::StorageType type) { |
| 715 return quota_client_->DoesSupport(type); | 755 return quota_client_->DoesSupport(type); |
| 716 } | 756 } |
| 717 | 757 |
| 718 scoped_ptr<CacheStorageQuotaClient> quota_client_; | 758 scoped_ptr<CacheStorageQuotaClient> quota_client_; |
| 719 | 759 |
| 720 storage::QuotaStatusCode callback_status_; | 760 storage::QuotaStatusCode callback_status_; |
| 721 int64 callback_usage_; | 761 int64 callback_quota_usage_ = 0; |
| 722 std::set<GURL> callback_origins_; | 762 std::set<GURL> callback_origins_; |
| 723 | 763 |
| 724 DISALLOW_COPY_AND_ASSIGN(CacheStorageQuotaClientTest); | 764 DISALLOW_COPY_AND_ASSIGN(CacheStorageQuotaClientTest); |
| 725 }; | 765 }; |
| 726 | 766 |
| 727 class CacheStorageQuotaClientTestP : public CacheStorageQuotaClientTest, | 767 class CacheStorageQuotaClientTestP : public CacheStorageQuotaClientTest, |
| 728 public testing::WithParamInterface<bool> { | 768 public testing::WithParamInterface<bool> { |
| 729 bool MemoryOnly() override { return !GetParam(); } | 769 bool MemoryOnly() override { return !GetParam(); } |
| 730 }; | 770 }; |
| 731 | 771 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 790 | 830 |
| 791 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests, | 831 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests, |
| 792 CacheStorageManagerTestP, | 832 CacheStorageManagerTestP, |
| 793 ::testing::Values(false, true)); | 833 ::testing::Values(false, true)); |
| 794 | 834 |
| 795 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests, | 835 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests, |
| 796 CacheStorageQuotaClientTestP, | 836 CacheStorageQuotaClientTestP, |
| 797 ::testing::Values(false, true)); | 837 ::testing::Values(false, true)); |
| 798 | 838 |
| 799 } // namespace content | 839 } // namespace content |
| OLD | NEW |