- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / dom_storage / dom_storage_context_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/bind.h"
6 #include "base/file_util.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/message_loop/message_loop_proxy.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/threading/sequenced_worker_pool.h"
12 #include "base/time/time.h"
13 #include "content/browser/dom_storage/dom_storage_area.h"
14 #include "content/browser/dom_storage/dom_storage_context_impl.h"
15 #include "content/browser/dom_storage/dom_storage_namespace.h"
16 #include "content/browser/dom_storage/dom_storage_task_runner.h"
17 #include "content/public/browser/local_storage_usage_info.h"
18 #include "content/public/browser/session_storage_usage_info.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "webkit/browser/quota/mock_special_storage_policy.h"
21
22 namespace content {
23
24 class DOMStorageContextImplTest : public testing::Test {
25  public:
26   DOMStorageContextImplTest()
27     : kOrigin(GURL("http://dom_storage/")),
28       kKey(ASCIIToUTF16("key")),
29       kValue(ASCIIToUTF16("value")),
30       kDontIncludeFileInfo(false),
31       kDoIncludeFileInfo(true) {
32   }
33
34   const GURL kOrigin;
35   const base::string16 kKey;
36   const base::string16 kValue;
37   const bool kDontIncludeFileInfo;
38   const bool kDoIncludeFileInfo;
39
40   virtual void SetUp() {
41     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
42     storage_policy_ = new quota::MockSpecialStoragePolicy;
43     task_runner_ =
44         new MockDOMStorageTaskRunner(base::MessageLoopProxy::current().get());
45     context_ = new DOMStorageContextImpl(temp_dir_.path(),
46                                          base::FilePath(),
47                                          storage_policy_.get(),
48                                          task_runner_.get());
49   }
50
51   virtual void TearDown() {
52     base::MessageLoop::current()->RunUntilIdle();
53   }
54
55   void VerifySingleOriginRemains(const GURL& origin) {
56     // Use a new instance to examine the contexts of temp_dir_.
57     scoped_refptr<DOMStorageContextImpl> context =
58         new DOMStorageContextImpl(temp_dir_.path(), base::FilePath(),
59                                   NULL, NULL);
60     std::vector<LocalStorageUsageInfo> infos;
61     context->GetLocalStorageUsage(&infos, kDontIncludeFileInfo);
62     ASSERT_EQ(1u, infos.size());
63     EXPECT_EQ(origin, infos[0].origin);
64   }
65
66  protected:
67   base::MessageLoop message_loop_;
68   base::ScopedTempDir temp_dir_;
69   scoped_refptr<quota::MockSpecialStoragePolicy> storage_policy_;
70   scoped_refptr<MockDOMStorageTaskRunner> task_runner_;
71   scoped_refptr<DOMStorageContextImpl> context_;
72   DISALLOW_COPY_AND_ASSIGN(DOMStorageContextImplTest);
73 };
74
75 TEST_F(DOMStorageContextImplTest, Basics) {
76   // This test doesn't do much, checks that the constructor
77   // initializes members properly and that invoking methods
78   // on a newly created object w/o any data on disk do no harm.
79   EXPECT_EQ(temp_dir_.path(), context_->localstorage_directory());
80   EXPECT_EQ(base::FilePath(), context_->sessionstorage_directory());
81   EXPECT_EQ(storage_policy_.get(), context_->special_storage_policy_.get());
82   context_->PurgeMemory();
83   context_->DeleteLocalStorage(GURL("http://chromium.org/"));
84   const int kFirstSessionStorageNamespaceId = 1;
85   EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId));
86   EXPECT_FALSE(context_->GetStorageNamespace(kFirstSessionStorageNamespaceId));
87   EXPECT_EQ(kFirstSessionStorageNamespaceId, context_->AllocateSessionId());
88   std::vector<LocalStorageUsageInfo> infos;
89   context_->GetLocalStorageUsage(&infos, kDontIncludeFileInfo);
90   EXPECT_TRUE(infos.empty());
91   context_->Shutdown();
92 }
93
94 TEST_F(DOMStorageContextImplTest, UsageInfo) {
95   // Should be empty initially
96   std::vector<LocalStorageUsageInfo> infos;
97   context_->GetLocalStorageUsage(&infos, kDontIncludeFileInfo);
98   EXPECT_TRUE(infos.empty());
99   context_->GetLocalStorageUsage(&infos, kDoIncludeFileInfo);
100   EXPECT_TRUE(infos.empty());
101
102   // Put some data into local storage and shutdown the context
103   // to ensure data is written to disk.
104   base::NullableString16 old_value;
105   EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)->
106       OpenStorageArea(kOrigin)->SetItem(kKey, kValue, &old_value));
107   context_->Shutdown();
108   context_ = NULL;
109   base::MessageLoop::current()->RunUntilIdle();
110
111   // Create a new context that points to the same directory, see that
112   // it knows about the origin that we stored data for.
113   context_ = new DOMStorageContextImpl(temp_dir_.path(), base::FilePath(),
114                                        NULL, NULL);
115   context_->GetLocalStorageUsage(&infos, kDontIncludeFileInfo);
116   EXPECT_EQ(1u, infos.size());
117   EXPECT_EQ(kOrigin, infos[0].origin);
118   EXPECT_EQ(0u, infos[0].data_size);
119   EXPECT_EQ(base::Time(), infos[0].last_modified);
120   infos.clear();
121   context_->GetLocalStorageUsage(&infos, kDoIncludeFileInfo);
122   EXPECT_EQ(1u, infos.size());
123   EXPECT_EQ(kOrigin, infos[0].origin);
124   EXPECT_NE(0u, infos[0].data_size);
125   EXPECT_NE(base::Time(), infos[0].last_modified);
126 }
127
128 TEST_F(DOMStorageContextImplTest, SessionOnly) {
129   const GURL kSessionOnlyOrigin("http://www.sessiononly.com/");
130   storage_policy_->AddSessionOnly(kSessionOnlyOrigin);
131
132   // Store data for a normal and a session-only origin and then
133   // invoke Shutdown() which should delete data for session-only
134   // origins.
135   base::NullableString16 old_value;
136   EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)->
137       OpenStorageArea(kOrigin)->SetItem(kKey, kValue, &old_value));
138   EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)->
139       OpenStorageArea(kSessionOnlyOrigin)->SetItem(kKey, kValue, &old_value));
140   context_->Shutdown();
141   context_ = NULL;
142   base::MessageLoop::current()->RunUntilIdle();
143
144   // Verify that the session-only origin data is gone.
145   VerifySingleOriginRemains(kOrigin);
146 }
147
148 TEST_F(DOMStorageContextImplTest, SetForceKeepSessionState) {
149   const GURL kSessionOnlyOrigin("http://www.sessiononly.com/");
150   storage_policy_->AddSessionOnly(kSessionOnlyOrigin);
151
152   // Store data for a session-only origin, setup to save session data, then
153   // shutdown.
154   base::NullableString16 old_value;
155   EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)->
156       OpenStorageArea(kSessionOnlyOrigin)->SetItem(kKey, kValue, &old_value));
157   context_->SetForceKeepSessionState();  // Should override clear behavior.
158   context_->Shutdown();
159   context_ = NULL;
160   base::MessageLoop::current()->RunUntilIdle();
161
162   VerifySingleOriginRemains(kSessionOnlyOrigin);
163 }
164
165 TEST_F(DOMStorageContextImplTest, PersistentIds) {
166   const int kFirstSessionStorageNamespaceId = 1;
167   const std::string kPersistentId = "persistent";
168   context_->CreateSessionNamespace(kFirstSessionStorageNamespaceId,
169                                    kPersistentId);
170   DOMStorageNamespace* dom_namespace =
171       context_->GetStorageNamespace(kFirstSessionStorageNamespaceId);
172   ASSERT_TRUE(dom_namespace);
173   EXPECT_EQ(kPersistentId, dom_namespace->persistent_namespace_id());
174   // Verify that the areas inherit the persistent ID.
175   DOMStorageArea* area = dom_namespace->OpenStorageArea(kOrigin);
176   EXPECT_EQ(kPersistentId, area->persistent_namespace_id_);
177
178   // Verify that the persistent IDs are handled correctly when cloning.
179   const int kClonedSessionStorageNamespaceId = 2;
180   const std::string kClonedPersistentId = "cloned";
181   context_->CloneSessionNamespace(kFirstSessionStorageNamespaceId,
182                                   kClonedSessionStorageNamespaceId,
183                                   kClonedPersistentId);
184   DOMStorageNamespace* cloned_dom_namespace =
185       context_->GetStorageNamespace(kClonedSessionStorageNamespaceId);
186   ASSERT_TRUE(dom_namespace);
187   EXPECT_EQ(kClonedPersistentId,
188             cloned_dom_namespace->persistent_namespace_id());
189   // Verify that the areas inherit the persistent ID.
190   DOMStorageArea* cloned_area = cloned_dom_namespace->OpenStorageArea(kOrigin);
191   EXPECT_EQ(kClonedPersistentId, cloned_area->persistent_namespace_id_);
192 }
193
194 TEST_F(DOMStorageContextImplTest, DeleteSessionStorage) {
195   // Create a DOMStorageContextImpl which will save sessionStorage on disk.
196   context_ = new DOMStorageContextImpl(temp_dir_.path(),
197                                        temp_dir_.path(),
198                                        storage_policy_.get(),
199                                        task_runner_.get());
200   context_->SetSaveSessionStorageOnDisk();
201   ASSERT_EQ(temp_dir_.path(), context_->sessionstorage_directory());
202
203   // Write data.
204   const int kSessionStorageNamespaceId = 1;
205   const std::string kPersistentId = "persistent";
206   context_->CreateSessionNamespace(kSessionStorageNamespaceId,
207                                    kPersistentId);
208   DOMStorageNamespace* dom_namespace =
209       context_->GetStorageNamespace(kSessionStorageNamespaceId);
210   DOMStorageArea* area = dom_namespace->OpenStorageArea(kOrigin);
211   const base::string16 kKey(ASCIIToUTF16("foo"));
212   const base::string16 kValue(ASCIIToUTF16("bar"));
213   base::NullableString16 old_nullable_value;
214   area->SetItem(kKey, kValue, &old_nullable_value);
215   dom_namespace->CloseStorageArea(area);
216
217   // Destroy and recreate the DOMStorageContextImpl.
218   context_->Shutdown();
219   context_ = NULL;
220   base::MessageLoop::current()->RunUntilIdle();
221   context_ = new DOMStorageContextImpl(
222       temp_dir_.path(), temp_dir_.path(),
223       storage_policy_.get(), task_runner_.get());
224   context_->SetSaveSessionStorageOnDisk();
225
226   // Read the data back.
227   context_->CreateSessionNamespace(kSessionStorageNamespaceId,
228                                    kPersistentId);
229   dom_namespace = context_->GetStorageNamespace(kSessionStorageNamespaceId);
230   area = dom_namespace->OpenStorageArea(kOrigin);
231   base::NullableString16 read_value;
232   read_value = area->GetItem(kKey);
233   EXPECT_EQ(kValue, read_value.string());
234   dom_namespace->CloseStorageArea(area);
235
236   SessionStorageUsageInfo info;
237   info.origin = kOrigin;
238   info.persistent_namespace_id = kPersistentId;
239   context_->DeleteSessionStorage(info);
240
241   // Destroy and recreate again.
242   context_->Shutdown();
243   context_ = NULL;
244   base::MessageLoop::current()->RunUntilIdle();
245   context_ = new DOMStorageContextImpl(
246       temp_dir_.path(), temp_dir_.path(),
247       storage_policy_.get(), task_runner_.get());
248   context_->SetSaveSessionStorageOnDisk();
249
250   // Now there should be no data.
251   context_->CreateSessionNamespace(kSessionStorageNamespaceId,
252                                    kPersistentId);
253   dom_namespace = context_->GetStorageNamespace(kSessionStorageNamespaceId);
254   area = dom_namespace->OpenStorageArea(kOrigin);
255   read_value = area->GetItem(kKey);
256   EXPECT_TRUE(read_value.is_null());
257   dom_namespace->CloseStorageArea(area);
258   context_->Shutdown();
259   context_ = NULL;
260   base::MessageLoop::current()->RunUntilIdle();
261 }
262
263 }  // namespace content