| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 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/cache_storage/cache_storage.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback.h" |
| 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/run_loop.h" |
| 11 #include "base/stl_util.h" |
| 12 #include "base/thread_task_runner_handle.h" |
| 13 #include "content/public/test/test_browser_thread_bundle.h" |
| 14 #include "net/url_request/url_request_context_getter.h" |
| 15 #include "storage/browser/quota/quota_manager_proxy.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 namespace content { |
| 19 |
| 20 namespace { |
| 21 const char kOrigin[] = "http://example.com/"; |
| 22 } |
| 23 |
| 24 class TestCacheStorage : public CacheStorage { |
| 25 public: |
| 26 explicit TestCacheStorage(const base::FilePath& file_path) |
| 27 : CacheStorage(file_path, |
| 28 false /* memory_only */, |
| 29 base::ThreadTaskRunnerHandle::Get().get(), |
| 30 scoped_refptr<net::URLRequestContextGetter>(), |
| 31 scoped_refptr<storage::QuotaManagerProxy>(), |
| 32 base::WeakPtr<storage::BlobStorageContext>(), |
| 33 GURL(kOrigin)), |
| 34 delay_preserved_cache_callback_(false) {} |
| 35 |
| 36 void RunPreservedCacheCallback() { |
| 37 ASSERT_FALSE(preserved_cache_callback_.is_null()); |
| 38 preserved_cache_callback_.Run(); |
| 39 preserved_cache_callback_.Reset(); |
| 40 } |
| 41 |
| 42 void set_delay_preserved_cache_callback(bool should_delay) { |
| 43 delay_preserved_cache_callback_ = should_delay; |
| 44 } |
| 45 |
| 46 bool IsPreservingCache(const CacheStorageCache* cache) { |
| 47 return ContainsKey(preserved_caches_, cache); |
| 48 } |
| 49 |
| 50 private: |
| 51 void SchedulePreservedCacheRemoval(const base::Closure& callback) override { |
| 52 if (!delay_preserved_cache_callback_) { |
| 53 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); |
| 54 return; |
| 55 } |
| 56 preserved_cache_callback_ = callback; |
| 57 } |
| 58 |
| 59 bool delay_preserved_cache_callback_; |
| 60 base::Closure preserved_cache_callback_; |
| 61 }; |
| 62 |
| 63 class CacheStorageTest : public testing::Test { |
| 64 protected: |
| 65 CacheStorageTest() |
| 66 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 67 |
| 68 void SetUp() override { |
| 69 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 70 test_cache_storage_.reset(new TestCacheStorage(temp_dir_.path())); |
| 71 } |
| 72 |
| 73 bool OpenCache(const std::string& cache_name) { |
| 74 bool callback_called = false; |
| 75 test_cache_storage_->OpenCache( |
| 76 cache_name, base::Bind(&CacheStorageTest::OpenCacheCallback, |
| 77 base::Unretained(this), &callback_called)); |
| 78 base::RunLoop().RunUntilIdle(); |
| 79 EXPECT_TRUE(callback_called); |
| 80 return callback_error_ == CACHE_STORAGE_OK; |
| 81 } |
| 82 |
| 83 void OpenCacheCallback(bool* callback_called, |
| 84 const scoped_refptr<CacheStorageCache>& cache, |
| 85 CacheStorageError error) { |
| 86 *callback_called = true; |
| 87 callback_cache_ = cache; |
| 88 callback_error_ = error; |
| 89 } |
| 90 |
| 91 base::ScopedTempDir temp_dir_; |
| 92 TestBrowserThreadBundle browser_thread_bundle_; |
| 93 scoped_ptr<TestCacheStorage> test_cache_storage_; |
| 94 |
| 95 scoped_refptr<CacheStorageCache> callback_cache_; |
| 96 CacheStorageError callback_error_; |
| 97 }; |
| 98 |
| 99 TEST_F(CacheStorageTest, PreserveCache) { |
| 100 test_cache_storage_->set_delay_preserved_cache_callback(true); |
| 101 EXPECT_TRUE(OpenCache("foo")); |
| 102 EXPECT_TRUE(test_cache_storage_->IsPreservingCache(callback_cache_.get())); |
| 103 test_cache_storage_->RunPreservedCacheCallback(); |
| 104 EXPECT_FALSE(test_cache_storage_->IsPreservingCache(callback_cache_.get())); |
| 105 |
| 106 // Try opening it again, since the cache is already open (callback_cache_ is |
| 107 // referencing it) the cache shouldn't be preserved again. |
| 108 EXPECT_TRUE(OpenCache("foo")); |
| 109 EXPECT_FALSE(test_cache_storage_->IsPreservingCache(callback_cache_.get())); |
| 110 |
| 111 // Remove the reference to the cache so that it's deleted. After that, the |
| 112 // next time it's opened will require the cache to be created again and |
| 113 // preserved. |
| 114 callback_cache_ = nullptr; |
| 115 EXPECT_TRUE(OpenCache("foo")); |
| 116 EXPECT_TRUE(test_cache_storage_->IsPreservingCache(callback_cache_.get())); |
| 117 test_cache_storage_->RunPreservedCacheCallback(); |
| 118 EXPECT_FALSE(test_cache_storage_->IsPreservingCache(callback_cache_.get())); |
| 119 } |
| 120 |
| 121 } // namespace content |
| OLD | NEW |