Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(167)

Side by Side Diff: webkit/dom_storage/dom_storage_context_unittest.cc

Issue 9860018: DomStorageContext unittests (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « webkit/dom_storage/dom_storage_context.h ('k') | webkit/tools/test_shell/test_shell.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2012 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/message_loop.h"
8 #include "base/message_loop_proxy.h"
9 #include "base/scoped_temp_dir.h"
10 #include "base/threading/sequenced_worker_pool.h"
11 #include "base/time.h"
12 #include "base/utf_string_conversions.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "webkit/dom_storage/dom_storage_area.h"
15 #include "webkit/dom_storage/dom_storage_context.h"
16 #include "webkit/dom_storage/dom_storage_namespace.h"
17 #include "webkit/dom_storage/dom_storage_task_runner.h"
18 #include "webkit/dom_storage/dom_storage_types.h"
19 #include "webkit/quota/mock_special_storage_policy.h"
20
21 namespace dom_storage {
22
23 class DomStorageContextTest : public testing::Test {
24 public:
25 DomStorageContextTest()
26 : kOrigin(GURL("http://dom_storage/")),
27 kKey(ASCIIToUTF16("key")),
28 kValue(ASCIIToUTF16("value")),
29 kDontIncludeFileInfo(false),
30 kDoIncludeFileInfo(true) {
31 }
32
33 const GURL kOrigin;
34 const string16 kKey;
35 const string16 kValue;
36 const bool kDontIncludeFileInfo;
37 const bool kDoIncludeFileInfo;
38
39 virtual void SetUp() {
40 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
41 storage_policy_ = new quota::MockSpecialStoragePolicy;
42 task_runner_ = new MockDomStorageTaskRunner(
43 base::MessageLoopProxy::current());
44 context_ = new DomStorageContext(temp_dir_.path(),
45 storage_policy_,
46 task_runner_);
47 }
48
49 virtual void TearDown() {
50 MessageLoop::current()->RunAllPending();
51 }
52
53 void VerifySingleOriginRemains(const GURL& origin) {
54 // Use a new instance to examine the contexts of temp_dir_.
55 scoped_refptr<DomStorageContext> context =
56 new DomStorageContext(temp_dir_.path(), NULL, NULL);
57 std::vector<DomStorageContext::UsageInfo> infos;
58 context->GetUsageInfo(&infos, kDontIncludeFileInfo);
59 EXPECT_EQ(1u, infos.size());
60 EXPECT_EQ(origin, infos[0].origin);
61 }
62
63 protected:
64 ScopedTempDir temp_dir_;
65 scoped_refptr<quota::MockSpecialStoragePolicy> storage_policy_;
66 scoped_refptr<MockDomStorageTaskRunner> task_runner_;
67 scoped_refptr<DomStorageContext> context_;
68 DISALLOW_COPY_AND_ASSIGN(DomStorageContextTest);
69 };
70
71 TEST_F(DomStorageContextTest, Basics) {
72 // This test doesn't do much, checks that the constructor
73 // initializes members properly and that invoking methods
74 // on a newly created object w/o any data on disk do no harm.
75 EXPECT_EQ(temp_dir_.path(), context_->directory());
76 EXPECT_EQ(storage_policy_.get(), context_->special_storage_policy_.get());
77 context_->PurgeMemory();
78 context_->DeleteOrigin(GURL("http://chromium.org/"));
79 context_->DeleteDataModifiedSince(base::Time::Now());
80 const int kFirstSessionStorageNamespaceId = 1;
81 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId));
82 EXPECT_FALSE(context_->GetStorageNamespace(kFirstSessionStorageNamespaceId));
83 EXPECT_EQ(kFirstSessionStorageNamespaceId, context_->AllocateSessionId());
84 std::vector<DomStorageContext::UsageInfo> infos;
85 context_->GetUsageInfo(&infos, kDontIncludeFileInfo);
86 EXPECT_TRUE(infos.empty());
87 context_->Shutdown();
88 }
89
90 TEST_F(DomStorageContextTest, UsageInfo) {
91 // Should be empty initially
92 std::vector<DomStorageContext::UsageInfo> infos;
93 context_->GetUsageInfo(&infos, kDontIncludeFileInfo);
94 EXPECT_TRUE(infos.empty());
95 context_->GetUsageInfo(&infos, kDoIncludeFileInfo);
96 EXPECT_TRUE(infos.empty());
97
98 // Put some data into local storage and shutdown the context
99 // to ensure data is written to disk.
100 NullableString16 old_value;
101 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)->
102 OpenStorageArea(kOrigin)->SetItem(kKey, kValue, &old_value));
103 context_->Shutdown();
104 context_ = NULL;
105 MessageLoop::current()->RunAllPending();
106
107 // Create a new context that points to the same directory, see that
108 // it knows about the origin that we stored data for.
109 context_ = new DomStorageContext(temp_dir_.path(), NULL, NULL);
110 context_->GetUsageInfo(&infos, kDontIncludeFileInfo);
111 EXPECT_EQ(1u, infos.size());
112 EXPECT_EQ(kOrigin, infos[0].origin);
113 EXPECT_EQ(0u, infos[0].data_size);
114 EXPECT_EQ(base::Time(), infos[0].last_modified);
115 infos.clear();
116 context_->GetUsageInfo(&infos, kDoIncludeFileInfo);
117 EXPECT_EQ(1u, infos.size());
118 EXPECT_EQ(kOrigin, infos[0].origin);
119 EXPECT_NE(0u, infos[0].data_size);
120 EXPECT_NE(base::Time(), infos[0].last_modified);
121 }
122
123 TEST_F(DomStorageContextTest, SessionOnly) {
124 const GURL kSessionOnlyOrigin("http://www.sessiononly.com/");
125 storage_policy_->AddSessionOnly(kSessionOnlyOrigin);
126
127 // Store data for a normal and a session-only origin and then
128 // invoke Shutdown() which should delete data for session-only
129 // origins.
130 NullableString16 old_value;
131 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)->
132 OpenStorageArea(kOrigin)->SetItem(kKey, kValue, &old_value));
133 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)->
134 OpenStorageArea(kSessionOnlyOrigin)->SetItem(kKey, kValue, &old_value));
135 context_->Shutdown();
136 context_ = NULL;
137 MessageLoop::current()->RunAllPending();
138
139 // Verify that the session-only origin data is gone.
140 VerifySingleOriginRemains(kOrigin);
141 }
142
143 TEST_F(DomStorageContextTest, ClearLocalState) {
144 const GURL kProtectedOrigin("http://www.protected.com/");
145 storage_policy_->AddProtected(kProtectedOrigin);
146
147 // Store data for a normal and a protected origin, setup shutdown options
148 // to clear normal local state, then shutdown and let things flush.
149 NullableString16 old_value;
150 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)->
151 OpenStorageArea(kOrigin)->SetItem(kKey, kValue, &old_value));
152 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)->
153 OpenStorageArea(kProtectedOrigin)->SetItem(kKey, kValue, &old_value));
154 context_->SetClearLocalState(true);
155 context_->Shutdown();
156 context_ = NULL;
157 MessageLoop::current()->RunAllPending();
158
159 VerifySingleOriginRemains(kProtectedOrigin);
160 }
161
162 TEST_F(DomStorageContextTest, SaveSessionState) {
163 const GURL kSessionOnlyOrigin("http://www.sessiononly.com/");
164 storage_policy_->AddSessionOnly(kSessionOnlyOrigin);
165
166 // Store data for a session-only origin, setup to save session data, then
167 // shutdown.
168 NullableString16 old_value;
169 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)->
170 OpenStorageArea(kSessionOnlyOrigin)->SetItem(kKey, kValue, &old_value));
171 context_->SetClearLocalState(true);
172 context_->SaveSessionState(); // Should override clear behavior.
173 context_->Shutdown();
174 context_ = NULL;
175 MessageLoop::current()->RunAllPending();
176
177 VerifySingleOriginRemains(kSessionOnlyOrigin);
178 }
179
180 } // namespace dom_storage
181
OLDNEW
« no previous file with comments | « webkit/dom_storage/dom_storage_context.h ('k') | webkit/tools/test_shell/test_shell.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698