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