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.
9 #include "base/file_util.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop.h"
13 #include "base/message_loop_proxy.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "webkit/quota/mock_quota_manager.h"
16 #include "webkit/quota/mock_special_storage_policy.h"
17 #include "webkit/quota/mock_storage_client.h"
21 const char kTestOrigin1
[] = "http://host1:1/";
22 const char kTestOrigin2
[] = "http://host2:1/";
23 const char kTestOrigin3
[] = "http://host3:1/";
25 const GURL
kOrigin1(kTestOrigin1
);
26 const GURL
kOrigin2(kTestOrigin2
);
27 const GURL
kOrigin3(kTestOrigin3
);
29 const StorageType kTemporary
= kStorageTypeTemporary
;
30 const StorageType kPersistent
= kStorageTypePersistent
;
32 const QuotaClient::ID kClientFile
= QuotaClient::kFileSystem
;
33 const QuotaClient::ID kClientDB
= QuotaClient::kIndexedDatabase
;
35 class MockQuotaManagerTest
: public testing::Test
{
37 MockQuotaManagerTest()
38 : weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
39 deletion_callback_count_(0) {
42 virtual void SetUp() {
43 ASSERT_TRUE(data_dir_
.CreateUniqueTempDir());
44 policy_
= new MockSpecialStoragePolicy
;
45 manager_
= new MockQuotaManager(
46 false /* is_incognito */,
48 base::MessageLoopProxy::current(),
49 base::MessageLoopProxy::current(),
53 virtual void TearDown() {
54 // Make sure the quota manager cleans up correctly.
56 MessageLoop::current()->RunUntilIdle();
59 void GetModifiedOrigins(StorageType type
, base::Time since
) {
60 manager_
->GetOriginsModifiedSince(
62 base::Bind(&MockQuotaManagerTest::GotModifiedOrigins
,
63 weak_factory_
.GetWeakPtr()));
66 void GotModifiedOrigins(const std::set
<GURL
>& origins
, StorageType type
) {
71 void DeleteOriginData(const GURL
& origin
, StorageType type
,
72 int quota_client_mask
) {
73 manager_
->DeleteOriginData(
74 origin
, type
, quota_client_mask
,
75 base::Bind(&MockQuotaManagerTest::DeletedOriginData
,
76 weak_factory_
.GetWeakPtr()));
79 void DeletedOriginData(QuotaStatusCode status
) {
80 ++deletion_callback_count_
;
81 EXPECT_EQ(quota::kQuotaStatusOk
, status
);
84 int deletion_callback_count() const {
85 return deletion_callback_count_
;
88 MockQuotaManager
* manager() const {
89 return manager_
.get();
92 const std::set
<GURL
>& origins() const {
96 const StorageType
& type() const {
101 MessageLoop message_loop_
;
102 base::ScopedTempDir data_dir_
;
103 base::WeakPtrFactory
<MockQuotaManagerTest
> weak_factory_
;
104 scoped_refptr
<MockQuotaManager
> manager_
;
105 scoped_refptr
<MockSpecialStoragePolicy
> policy_
;
107 int deletion_callback_count_
;
109 std::set
<GURL
> origins_
;
112 DISALLOW_COPY_AND_ASSIGN(MockQuotaManagerTest
);
115 TEST_F(MockQuotaManagerTest
, BasicOriginManipulation
) {
116 EXPECT_FALSE(manager()->OriginHasData(kOrigin1
, kTemporary
, kClientFile
));
117 EXPECT_FALSE(manager()->OriginHasData(kOrigin1
, kTemporary
, kClientDB
));
118 EXPECT_FALSE(manager()->OriginHasData(kOrigin1
, kPersistent
, kClientFile
));
119 EXPECT_FALSE(manager()->OriginHasData(kOrigin1
, kPersistent
, kClientDB
));
120 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kTemporary
, kClientFile
));
121 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kTemporary
, kClientDB
));
122 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kPersistent
, kClientFile
));
123 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kPersistent
, kClientDB
));
125 manager()->AddOrigin(kOrigin1
, kTemporary
, kClientFile
, base::Time::Now());
126 EXPECT_TRUE(manager()->OriginHasData(kOrigin1
, kTemporary
, kClientFile
));
127 EXPECT_FALSE(manager()->OriginHasData(kOrigin1
, kTemporary
, kClientDB
));
128 EXPECT_FALSE(manager()->OriginHasData(kOrigin1
, kPersistent
, kClientFile
));
129 EXPECT_FALSE(manager()->OriginHasData(kOrigin1
, kPersistent
, kClientDB
));
130 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kTemporary
, kClientFile
));
131 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kTemporary
, kClientDB
));
132 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kPersistent
, kClientFile
));
133 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kPersistent
, kClientDB
));
135 manager()->AddOrigin(kOrigin1
, kPersistent
, kClientFile
, base::Time::Now());
136 EXPECT_TRUE(manager()->OriginHasData(kOrigin1
, kTemporary
, kClientFile
));
137 EXPECT_FALSE(manager()->OriginHasData(kOrigin1
, kTemporary
, kClientDB
));
138 EXPECT_TRUE(manager()->OriginHasData(kOrigin1
, kPersistent
, kClientFile
));
139 EXPECT_FALSE(manager()->OriginHasData(kOrigin1
, kPersistent
, kClientDB
));
140 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kTemporary
, kClientFile
));
141 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kTemporary
, kClientDB
));
142 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kPersistent
, kClientFile
));
143 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kPersistent
, kClientDB
));
145 manager()->AddOrigin(kOrigin2
, kTemporary
, kClientFile
| kClientDB
,
147 EXPECT_TRUE(manager()->OriginHasData(kOrigin1
, kTemporary
, kClientFile
));
148 EXPECT_FALSE(manager()->OriginHasData(kOrigin1
, kTemporary
, kClientDB
));
149 EXPECT_TRUE(manager()->OriginHasData(kOrigin1
, kPersistent
, kClientFile
));
150 EXPECT_FALSE(manager()->OriginHasData(kOrigin1
, kPersistent
, kClientDB
));
151 EXPECT_TRUE(manager()->OriginHasData(kOrigin2
, kTemporary
, kClientFile
));
152 EXPECT_TRUE(manager()->OriginHasData(kOrigin2
, kTemporary
, kClientDB
));
153 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kPersistent
, kClientFile
));
154 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kPersistent
, kClientDB
));
157 TEST_F(MockQuotaManagerTest
, OriginDeletion
) {
158 manager()->AddOrigin(kOrigin1
, kTemporary
, kClientFile
, base::Time::Now());
159 manager()->AddOrigin(kOrigin2
, kTemporary
, kClientFile
| kClientDB
,
161 manager()->AddOrigin(kOrigin3
, kTemporary
, kClientFile
| kClientDB
,
164 DeleteOriginData(kOrigin2
, kTemporary
, kClientFile
);
165 MessageLoop::current()->RunUntilIdle();
167 EXPECT_EQ(1, deletion_callback_count());
168 EXPECT_TRUE(manager()->OriginHasData(kOrigin1
, kTemporary
, kClientFile
));
169 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kTemporary
, kClientFile
));
170 EXPECT_TRUE(manager()->OriginHasData(kOrigin2
, kTemporary
, kClientDB
));
171 EXPECT_TRUE(manager()->OriginHasData(kOrigin3
, kTemporary
, kClientFile
));
172 EXPECT_TRUE(manager()->OriginHasData(kOrigin3
, kTemporary
, kClientDB
));
174 DeleteOriginData(kOrigin3
, kTemporary
, kClientFile
| kClientDB
);
175 MessageLoop::current()->RunUntilIdle();
177 EXPECT_EQ(2, deletion_callback_count());
178 EXPECT_TRUE(manager()->OriginHasData(kOrigin1
, kTemporary
, kClientFile
));
179 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kTemporary
, kClientFile
));
180 EXPECT_TRUE(manager()->OriginHasData(kOrigin2
, kTemporary
, kClientDB
));
181 EXPECT_FALSE(manager()->OriginHasData(kOrigin3
, kTemporary
, kClientFile
));
182 EXPECT_FALSE(manager()->OriginHasData(kOrigin3
, kTemporary
, kClientDB
));
185 TEST_F(MockQuotaManagerTest
, ModifiedOrigins
) {
186 base::Time now
= base::Time::Now();
187 base::Time then
= base::Time();
188 base::TimeDelta an_hour
= base::TimeDelta::FromMilliseconds(3600000);
189 base::TimeDelta a_minute
= base::TimeDelta::FromMilliseconds(60000);
191 GetModifiedOrigins(kTemporary
, then
);
192 MessageLoop::current()->RunUntilIdle();
193 EXPECT_TRUE(origins().empty());
195 manager()->AddOrigin(kOrigin1
, kTemporary
, kClientFile
, now
- an_hour
);
197 GetModifiedOrigins(kTemporary
, then
);
198 MessageLoop::current()->RunUntilIdle();
200 EXPECT_EQ(kTemporary
, type());
201 EXPECT_EQ(1UL, origins().size());
202 EXPECT_EQ(1UL, origins().count(kOrigin1
));
203 EXPECT_EQ(0UL, origins().count(kOrigin2
));
205 manager()->AddOrigin(kOrigin2
, kTemporary
, kClientFile
, now
);
207 GetModifiedOrigins(kTemporary
, then
);
208 MessageLoop::current()->RunUntilIdle();
210 EXPECT_EQ(kTemporary
, type());
211 EXPECT_EQ(2UL, origins().size());
212 EXPECT_EQ(1UL, origins().count(kOrigin1
));
213 EXPECT_EQ(1UL, origins().count(kOrigin2
));
215 GetModifiedOrigins(kTemporary
, now
- a_minute
);
216 MessageLoop::current()->RunUntilIdle();
218 EXPECT_EQ(kTemporary
, type());
219 EXPECT_EQ(1UL, origins().size());
220 EXPECT_EQ(0UL, origins().count(kOrigin1
));
221 EXPECT_EQ(1UL, origins().count(kOrigin2
));