storage_partition_impl_unittest.cc revision 58e6fbe4ee35d65e14b626c557d37565bf8ad179
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. 4 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" 16 17namespace content { 18namespace { 19 20class TestClosureCallback { 21 public: 22 TestClosureCallback() 23 : callback_(base::Bind( 24 &TestClosureCallback::StopWaiting, base::Unretained(this))) { 25 } 26 27 void WaitForResult() { 28 wait_run_loop_.reset(new base::RunLoop()); 29 wait_run_loop_->Run(); 30 } 31 32 const base::Closure& callback() { return callback_; } 33 34 private: 35 void StopWaiting() { 36 wait_run_loop_->Quit(); 37 } 38 39 base::Closure callback_; 40 scoped_ptr<base::RunLoop> wait_run_loop_; 41 42 DISALLOW_COPY_AND_ASSIGN(TestClosureCallback); 43}; 44 45const int kDefaultClientId = 42; 46const char kCacheKey[] = "key"; 47const char kCacheValue[] = "cached value"; 48 49} // namespace 50 51class StoragePartitionShaderClearTest : public testing::Test { 52 public: 53 StoragePartitionShaderClearTest() 54 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { 55 } 56 57 virtual ~StoragePartitionShaderClearTest() {} 58 59 const base::FilePath& cache_path() { return temp_dir_.path(); } 60 61 virtual void SetUp() OVERRIDE { 62 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 63 ShaderCacheFactory::GetInstance()->SetCacheInfo(kDefaultClientId, 64 cache_path()); 65 66 cache_ = ShaderCacheFactory::GetInstance()->Get(kDefaultClientId); 67 ASSERT_TRUE(cache_.get() != NULL); 68 } 69 70 void InitCache() { 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()); 75 76 cache_->Cache(kCacheKey, kCacheValue); 77 78 net::TestCompletionCallback complete_cb; 79 80 rv = cache_->SetCacheCompleteCallback(complete_cb.callback()); 81 ASSERT_EQ(net::OK, complete_cb.GetResult(rv)); 82 } 83 84 size_t Size() { return cache_->Size(); } 85 86 private: 87 virtual void TearDown() OVERRIDE { 88 cache_ = NULL; 89 ShaderCacheFactory::GetInstance()->RemoveCacheInfo(kDefaultClientId); 90 } 91 92 base::ScopedTempDir temp_dir_; 93 content::TestBrowserThreadBundle thread_bundle_; 94 95 scoped_refptr<ShaderDiskCache> cache_; 96}; 97 98void ClearData(content::StoragePartitionImpl* sp, 99 const base::Closure& cb) { 100 base::Time time; 101 sp->AsyncClearDataBetween(content::StoragePartition::kShaderStorage, 102 time, time, cb); 103} 104 105TEST_F(StoragePartitionShaderClearTest, ClearShaderCache) { 106 InitCache(); 107 EXPECT_EQ(1u, Size()); 108 109 TestClosureCallback clear_cb; 110 StoragePartitionImpl sp(cache_path(), 111 NULL, 112 NULL, 113 NULL, 114 NULL, 115 NULL, 116 NULL, 117 scoped_ptr<WebRTCIdentityStore>()); 118 base::MessageLoop::current()->PostTask( 119 FROM_HERE, base::Bind(&ClearData, &sp, clear_cb.callback())); 120 clear_cb.WaitForResult(); 121 EXPECT_EQ(0u, Size()); 122} 123 124} // namespace content 125