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
3 // found in the LICENSE file.
5 #include "base/files/scoped_temp_dir.h"
6 #include "base/message_loop/message_loop_proxy.h"
7 #include "base/run_loop.h"
8 #include "base/threading/thread.h"
9 #include "content/browser/browser_thread_impl.h"
10 #include "content/browser/gpu/shader_disk_cache.h"
11 #include "content/browser/storage_partition_impl.h"
12 #include "content/public/browser/storage_partition.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "net/base/test_completion_callback.h"
15 #include "testing/gtest/include/gtest/gtest.h"
20 class TestClosureCallback
{
23 : callback_(base::Bind(
24 &TestClosureCallback::StopWaiting
, base::Unretained(this))) {
27 void WaitForResult() {
28 wait_run_loop_
.reset(new base::RunLoop());
29 wait_run_loop_
->Run();
32 const base::Closure
& callback() { return callback_
; }
36 wait_run_loop_
->Quit();
39 base::Closure callback_
;
40 scoped_ptr
<base::RunLoop
> wait_run_loop_
;
42 DISALLOW_COPY_AND_ASSIGN(TestClosureCallback
);
45 const int kDefaultClientId
= 42;
46 const char kCacheKey
[] = "key";
47 const char kCacheValue
[] = "cached value";
51 class StoragePartitionShaderClearTest
: public testing::Test
{
53 StoragePartitionShaderClearTest()
54 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
) {
57 virtual ~StoragePartitionShaderClearTest() {}
59 const base::FilePath
& cache_path() { return temp_dir_
.path(); }
61 virtual void SetUp() OVERRIDE
{
62 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
63 ShaderCacheFactory::GetInstance()->SetCacheInfo(kDefaultClientId
,
66 cache_
= ShaderCacheFactory::GetInstance()->Get(kDefaultClientId
);
67 ASSERT_TRUE(cache_
.get() != NULL
);
71 net::TestCompletionCallback available_cb
;
72 int rv
= cache_
->SetAvailableCallback(available_cb
.callback());
73 ASSERT_EQ(net::OK
, available_cb
.GetResult(rv
));
74 EXPECT_EQ(0, cache_
->Size());
76 cache_
->Cache(kCacheKey
, kCacheValue
);
78 net::TestCompletionCallback complete_cb
;
80 rv
= cache_
->SetCacheCompleteCallback(complete_cb
.callback());
81 ASSERT_EQ(net::OK
, complete_cb
.GetResult(rv
));
84 size_t Size() { return cache_
->Size(); }
87 virtual void TearDown() OVERRIDE
{
89 ShaderCacheFactory::GetInstance()->RemoveCacheInfo(kDefaultClientId
);
92 base::ScopedTempDir temp_dir_
;
93 content::TestBrowserThreadBundle thread_bundle_
;
95 scoped_refptr
<ShaderDiskCache
> cache_
;
98 void ClearData(content::StoragePartitionImpl
* sp
,
99 const base::Closure
& cb
) {
101 sp
->AsyncClearDataBetween(content::StoragePartition::kShaderStorage
,
105 TEST_F(StoragePartitionShaderClearTest
, ClearShaderCache
) {
107 EXPECT_EQ(1u, Size());
109 TestClosureCallback clear_cb
;
110 StoragePartitionImpl
sp(cache_path(), NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
111 base::MessageLoop::current()->PostTask(
113 base::Bind(&ClearData
, &sp
, clear_cb
.callback()));
114 clear_cb
.WaitForResult();
115 EXPECT_EQ(0u, Size());
118 } // namespace content