1 // Copyright 2014 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.
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/run_loop.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "content/browser/quota/mock_quota_manager.h"
14 #include "content/public/test/mock_special_storage_policy.h"
15 #include "content/public/test/mock_storage_client.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 using storage::kQuotaStatusOk
;
19 using storage::kStorageTypePersistent
;
20 using storage::kStorageTypeTemporary
;
24 const char kTestOrigin1
[] = "http://host1:1/";
25 const char kTestOrigin2
[] = "http://host2:1/";
26 const char kTestOrigin3
[] = "http://host3:1/";
28 const GURL
kOrigin1(kTestOrigin1
);
29 const GURL
kOrigin2(kTestOrigin2
);
30 const GURL
kOrigin3(kTestOrigin3
);
32 const StorageType kTemporary
= kStorageTypeTemporary
;
33 const StorageType kPersistent
= kStorageTypePersistent
;
35 const QuotaClient::ID kClientFile
= QuotaClient::kFileSystem
;
36 const QuotaClient::ID kClientDB
= QuotaClient::kIndexedDatabase
;
38 class MockQuotaManagerTest
: public testing::Test
{
40 MockQuotaManagerTest()
41 : deletion_callback_count_(0),
45 void SetUp() override
{
46 ASSERT_TRUE(data_dir_
.CreateUniqueTempDir());
47 policy_
= new MockSpecialStoragePolicy
;
48 manager_
= new MockQuotaManager(false /* is_incognito */, data_dir_
.path(),
49 base::ThreadTaskRunnerHandle::Get().get(),
50 base::ThreadTaskRunnerHandle::Get().get(),
54 void TearDown() override
{
55 // Make sure the quota manager cleans up correctly.
57 base::RunLoop().RunUntilIdle();
60 void GetModifiedOrigins(StorageType type
, base::Time since
) {
61 manager_
->GetOriginsModifiedSince(
63 base::Bind(&MockQuotaManagerTest::GotModifiedOrigins
,
64 weak_factory_
.GetWeakPtr()));
67 void GotModifiedOrigins(const std::set
<GURL
>& origins
, StorageType type
) {
72 void DeleteOriginData(const GURL
& origin
, StorageType type
,
73 int quota_client_mask
) {
74 manager_
->DeleteOriginData(
75 origin
, type
, quota_client_mask
,
76 base::Bind(&MockQuotaManagerTest::DeletedOriginData
,
77 weak_factory_
.GetWeakPtr()));
80 void DeletedOriginData(QuotaStatusCode status
) {
81 ++deletion_callback_count_
;
82 EXPECT_EQ(kQuotaStatusOk
, status
);
85 int deletion_callback_count() const {
86 return deletion_callback_count_
;
89 MockQuotaManager
* manager() const {
90 return manager_
.get();
93 const std::set
<GURL
>& origins() const {
97 const StorageType
& type() const {
102 base::MessageLoop message_loop_
;
103 base::ScopedTempDir data_dir_
;
104 scoped_refptr
<MockQuotaManager
> manager_
;
105 scoped_refptr
<MockSpecialStoragePolicy
> policy_
;
107 int deletion_callback_count_
;
109 std::set
<GURL
> origins_
;
112 base::WeakPtrFactory
<MockQuotaManagerTest
> weak_factory_
;
114 DISALLOW_COPY_AND_ASSIGN(MockQuotaManagerTest
);
117 TEST_F(MockQuotaManagerTest
, BasicOriginManipulation
) {
118 EXPECT_FALSE(manager()->OriginHasData(kOrigin1
, kTemporary
, kClientFile
));
119 EXPECT_FALSE(manager()->OriginHasData(kOrigin1
, kTemporary
, kClientDB
));
120 EXPECT_FALSE(manager()->OriginHasData(kOrigin1
, kPersistent
, kClientFile
));
121 EXPECT_FALSE(manager()->OriginHasData(kOrigin1
, kPersistent
, kClientDB
));
122 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kTemporary
, kClientFile
));
123 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kTemporary
, kClientDB
));
124 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kPersistent
, kClientFile
));
125 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kPersistent
, kClientDB
));
127 manager()->AddOrigin(kOrigin1
, kTemporary
, kClientFile
, base::Time::Now());
128 EXPECT_TRUE(manager()->OriginHasData(kOrigin1
, kTemporary
, kClientFile
));
129 EXPECT_FALSE(manager()->OriginHasData(kOrigin1
, kTemporary
, kClientDB
));
130 EXPECT_FALSE(manager()->OriginHasData(kOrigin1
, kPersistent
, kClientFile
));
131 EXPECT_FALSE(manager()->OriginHasData(kOrigin1
, kPersistent
, kClientDB
));
132 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kTemporary
, kClientFile
));
133 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kTemporary
, kClientDB
));
134 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kPersistent
, kClientFile
));
135 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kPersistent
, kClientDB
));
137 manager()->AddOrigin(kOrigin1
, kPersistent
, kClientFile
, base::Time::Now());
138 EXPECT_TRUE(manager()->OriginHasData(kOrigin1
, kTemporary
, kClientFile
));
139 EXPECT_FALSE(manager()->OriginHasData(kOrigin1
, kTemporary
, kClientDB
));
140 EXPECT_TRUE(manager()->OriginHasData(kOrigin1
, kPersistent
, kClientFile
));
141 EXPECT_FALSE(manager()->OriginHasData(kOrigin1
, kPersistent
, kClientDB
));
142 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kTemporary
, kClientFile
));
143 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kTemporary
, kClientDB
));
144 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kPersistent
, kClientFile
));
145 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kPersistent
, kClientDB
));
147 manager()->AddOrigin(kOrigin2
, kTemporary
, kClientFile
| kClientDB
,
149 EXPECT_TRUE(manager()->OriginHasData(kOrigin1
, kTemporary
, kClientFile
));
150 EXPECT_FALSE(manager()->OriginHasData(kOrigin1
, kTemporary
, kClientDB
));
151 EXPECT_TRUE(manager()->OriginHasData(kOrigin1
, kPersistent
, kClientFile
));
152 EXPECT_FALSE(manager()->OriginHasData(kOrigin1
, kPersistent
, kClientDB
));
153 EXPECT_TRUE(manager()->OriginHasData(kOrigin2
, kTemporary
, kClientFile
));
154 EXPECT_TRUE(manager()->OriginHasData(kOrigin2
, kTemporary
, kClientDB
));
155 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kPersistent
, kClientFile
));
156 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kPersistent
, kClientDB
));
159 TEST_F(MockQuotaManagerTest
, OriginDeletion
) {
160 manager()->AddOrigin(kOrigin1
, kTemporary
, kClientFile
, base::Time::Now());
161 manager()->AddOrigin(kOrigin2
, kTemporary
, kClientFile
| kClientDB
,
163 manager()->AddOrigin(kOrigin3
, kTemporary
, kClientFile
| kClientDB
,
166 DeleteOriginData(kOrigin2
, kTemporary
, kClientFile
);
167 base::RunLoop().RunUntilIdle();
169 EXPECT_EQ(1, deletion_callback_count());
170 EXPECT_TRUE(manager()->OriginHasData(kOrigin1
, kTemporary
, kClientFile
));
171 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kTemporary
, kClientFile
));
172 EXPECT_TRUE(manager()->OriginHasData(kOrigin2
, kTemporary
, kClientDB
));
173 EXPECT_TRUE(manager()->OriginHasData(kOrigin3
, kTemporary
, kClientFile
));
174 EXPECT_TRUE(manager()->OriginHasData(kOrigin3
, kTemporary
, kClientDB
));
176 DeleteOriginData(kOrigin3
, kTemporary
, kClientFile
| kClientDB
);
177 base::RunLoop().RunUntilIdle();
179 EXPECT_EQ(2, deletion_callback_count());
180 EXPECT_TRUE(manager()->OriginHasData(kOrigin1
, kTemporary
, kClientFile
));
181 EXPECT_FALSE(manager()->OriginHasData(kOrigin2
, kTemporary
, kClientFile
));
182 EXPECT_TRUE(manager()->OriginHasData(kOrigin2
, kTemporary
, kClientDB
));
183 EXPECT_FALSE(manager()->OriginHasData(kOrigin3
, kTemporary
, kClientFile
));
184 EXPECT_FALSE(manager()->OriginHasData(kOrigin3
, kTemporary
, kClientDB
));
187 TEST_F(MockQuotaManagerTest
, ModifiedOrigins
) {
188 base::Time now
= base::Time::Now();
189 base::Time then
= base::Time();
190 base::TimeDelta an_hour
= base::TimeDelta::FromMilliseconds(3600000);
191 base::TimeDelta a_minute
= base::TimeDelta::FromMilliseconds(60000);
193 GetModifiedOrigins(kTemporary
, then
);
194 base::RunLoop().RunUntilIdle();
195 EXPECT_TRUE(origins().empty());
197 manager()->AddOrigin(kOrigin1
, kTemporary
, kClientFile
, now
- an_hour
);
199 GetModifiedOrigins(kTemporary
, then
);
200 base::RunLoop().RunUntilIdle();
202 EXPECT_EQ(kTemporary
, type());
203 EXPECT_EQ(1UL, origins().size());
204 EXPECT_EQ(1UL, origins().count(kOrigin1
));
205 EXPECT_EQ(0UL, origins().count(kOrigin2
));
207 manager()->AddOrigin(kOrigin2
, kTemporary
, kClientFile
, now
);
209 GetModifiedOrigins(kTemporary
, then
);
210 base::RunLoop().RunUntilIdle();
212 EXPECT_EQ(kTemporary
, type());
213 EXPECT_EQ(2UL, origins().size());
214 EXPECT_EQ(1UL, origins().count(kOrigin1
));
215 EXPECT_EQ(1UL, origins().count(kOrigin2
));
217 GetModifiedOrigins(kTemporary
, now
- a_minute
);
218 base::RunLoop().RunUntilIdle();
220 EXPECT_EQ(kTemporary
, type());
221 EXPECT_EQ(1UL, origins().size());
222 EXPECT_EQ(0UL, origins().count(kOrigin1
));
223 EXPECT_EQ(1UL, origins().count(kOrigin2
));
225 } // namespace content