- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / storage_partition_impl_unittest.cc
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
17 namespace content {
18 namespace {
19
20 class 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
45 const int kDefaultClientId = 42;
46 const char kCacheKey[] = "key";
47 const char kCacheValue[] = "cached value";
48
49 }  // namespace
50
51 class 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
98 void ClearData(content::StoragePartitionImpl* sp,
99                const base::Closure& cb) {
100   base::Time time;
101   sp->ClearDataForRange(
102       StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE,
103       StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
104       time, time, cb);
105 }
106
107 TEST_F(StoragePartitionShaderClearTest, ClearShaderCache) {
108   InitCache();
109   EXPECT_EQ(1u, Size());
110
111   TestClosureCallback clear_cb;
112   StoragePartitionImpl sp(
113       cache_path(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
114   base::MessageLoop::current()->PostTask(
115       FROM_HERE, base::Bind(&ClearData, &sp, clear_cb.callback()));
116   clear_cb.WaitForResult();
117   EXPECT_EQ(0u, Size());
118 }
119
120 }  // namespace content