| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/files/scoped_temp_dir.h" | 5 #include "base/files/scoped_temp_dir.h" |
| 6 #include "base/macros.h" | 6 #include "base/macros.h" |
| 7 #include "base/memory/ptr_util.h" |
| 7 #include "base/threading/thread.h" | 8 #include "base/threading/thread.h" |
| 8 #include "base/threading/thread_task_runner_handle.h" | 9 #include "base/threading/thread_task_runner_handle.h" |
| 9 #include "content/browser/browser_thread_impl.h" | 10 #include "content/browser/browser_thread_impl.h" |
| 10 #include "content/browser/gpu/shader_disk_cache.h" | 11 #include "content/browser/gpu/shader_disk_cache.h" |
| 11 #include "content/public/test/test_browser_thread_bundle.h" | 12 #include "content/public/test/test_browser_thread_bundle.h" |
| 12 #include "net/base/test_completion_callback.h" | 13 #include "net/base/test_completion_callback.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 15 |
| 15 namespace content { | 16 namespace content { |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 const int kDefaultClientId = 42; | 19 const int kDefaultClientId = 42; |
| 19 const char kCacheKey[] = "key"; | 20 const char kCacheKey[] = "key"; |
| 20 const char kCacheValue[] = "cached value"; | 21 const char kCacheValue[] = "cached value"; |
| 21 | 22 |
| 22 } // namespace | 23 } // namespace |
| 23 | 24 |
| 24 class ShaderDiskCacheTest : public testing::Test { | 25 class ShaderDiskCacheTest : public testing::Test { |
| 25 public: | 26 public: |
| 26 ShaderDiskCacheTest() | 27 ShaderDiskCacheTest() |
| 27 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { | 28 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { |
| 28 ShaderCacheFactory::InitInstance( | 29 factory_ = base::MakeUnique<ShaderCacheFactory>( |
| 29 base::ThreadTaskRunnerHandle::Get(), | |
| 30 BrowserThread::GetTaskRunnerForThread(BrowserThread::CACHE)); | 30 BrowserThread::GetTaskRunnerForThread(BrowserThread::CACHE)); |
| 31 } | 31 } |
| 32 | 32 |
| 33 ~ShaderDiskCacheTest() override {} | 33 ~ShaderDiskCacheTest() override {} |
| 34 | 34 |
| 35 const base::FilePath& cache_path() { return temp_dir_.GetPath(); } | 35 const base::FilePath& cache_path() { return temp_dir_.GetPath(); } |
| 36 | 36 |
| 37 void InitCache() { | 37 void InitCache() { |
| 38 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 38 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 39 ShaderCacheFactory::GetInstance()->SetCacheInfo(kDefaultClientId, | 39 factory_->SetCacheInfo(kDefaultClientId, cache_path()); |
| 40 cache_path()); | |
| 41 } | 40 } |
| 42 | 41 |
| 42 ShaderCacheFactory* factory() { return factory_.get(); } |
| 43 |
| 43 private: | 44 private: |
| 44 void TearDown() override { | 45 void TearDown() override { factory_->RemoveCacheInfo(kDefaultClientId); } |
| 45 ShaderCacheFactory::GetInstance()->RemoveCacheInfo(kDefaultClientId); | |
| 46 } | |
| 47 | 46 |
| 47 std::unique_ptr<ShaderCacheFactory> factory_; |
| 48 base::ScopedTempDir temp_dir_; | 48 base::ScopedTempDir temp_dir_; |
| 49 content::TestBrowserThreadBundle thread_bundle_; | 49 content::TestBrowserThreadBundle thread_bundle_; |
| 50 | 50 |
| 51 DISALLOW_COPY_AND_ASSIGN(ShaderDiskCacheTest); | 51 DISALLOW_COPY_AND_ASSIGN(ShaderDiskCacheTest); |
| 52 }; | 52 }; |
| 53 | 53 |
| 54 TEST_F(ShaderDiskCacheTest, ClearsCache) { | 54 TEST_F(ShaderDiskCacheTest, ClearsCache) { |
| 55 InitCache(); | 55 InitCache(); |
| 56 | 56 |
| 57 scoped_refptr<ShaderDiskCache> cache = | 57 scoped_refptr<ShaderDiskCache> cache = factory()->Get(kDefaultClientId); |
| 58 ShaderCacheFactory::GetInstance()->Get(kDefaultClientId); | |
| 59 ASSERT_TRUE(cache.get() != NULL); | 58 ASSERT_TRUE(cache.get() != NULL); |
| 60 | 59 |
| 61 net::TestCompletionCallback available_cb; | 60 net::TestCompletionCallback available_cb; |
| 62 int rv = cache->SetAvailableCallback(available_cb.callback()); | 61 int rv = cache->SetAvailableCallback(available_cb.callback()); |
| 63 ASSERT_EQ(net::OK, available_cb.GetResult(rv)); | 62 ASSERT_EQ(net::OK, available_cb.GetResult(rv)); |
| 64 EXPECT_EQ(0, cache->Size()); | 63 EXPECT_EQ(0, cache->Size()); |
| 65 | 64 |
| 66 cache->Cache(kCacheKey, kCacheValue); | 65 cache->Cache(kCacheKey, kCacheValue); |
| 67 | 66 |
| 68 net::TestCompletionCallback complete_cb; | 67 net::TestCompletionCallback complete_cb; |
| 69 rv = cache->SetCacheCompleteCallback(complete_cb.callback()); | 68 rv = cache->SetCacheCompleteCallback(complete_cb.callback()); |
| 70 ASSERT_EQ(net::OK, complete_cb.GetResult(rv)); | 69 ASSERT_EQ(net::OK, complete_cb.GetResult(rv)); |
| 71 EXPECT_EQ(1, cache->Size()); | 70 EXPECT_EQ(1, cache->Size()); |
| 72 | 71 |
| 73 base::Time time; | 72 base::Time time; |
| 74 net::TestCompletionCallback clear_cb; | 73 net::TestCompletionCallback clear_cb; |
| 75 rv = cache->Clear(time, time, clear_cb.callback()); | 74 rv = cache->Clear(time, time, clear_cb.callback()); |
| 76 ASSERT_EQ(net::OK, clear_cb.GetResult(rv)); | 75 ASSERT_EQ(net::OK, clear_cb.GetResult(rv)); |
| 77 EXPECT_EQ(0, cache->Size()); | 76 EXPECT_EQ(0, cache->Size()); |
| 78 }; | 77 }; |
| 79 | 78 |
| 80 } // namespace content | 79 } // namespace content |
| OLD | NEW |