Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / content / browser / storage_partition_impl_unittest.cc
blob0298513929900eb84174da0a7c9c989ceb7ddec9
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"
17 namespace content {
18 namespace {
20 class TestClosureCallback {
21 public:
22 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_; }
34 private:
35 void StopWaiting() {
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";
49 } // namespace
51 class StoragePartitionShaderClearTest : public testing::Test {
52 public:
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,
64 cache_path());
66 cache_ = ShaderCacheFactory::GetInstance()->Get(kDefaultClientId);
67 ASSERT_TRUE(cache_.get() != NULL);
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());
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(); }
86 private:
87 virtual void TearDown() OVERRIDE {
88 cache_ = NULL;
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) {
100 base::Time time;
101 sp->AsyncClearDataBetween(content::StoragePartition::kShaderStorage,
102 time, time, cb);
105 TEST_F(StoragePartitionShaderClearTest, ClearShaderCache) {
106 InitCache();
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(
112 FROM_HERE,
113 base::Bind(&ClearData, &sp, clear_cb.callback()));
114 clear_cb.WaitForResult();
115 EXPECT_EQ(0u, Size());
118 } // namespace content