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_path.h"
9 #include "base/message_loop/message_loop_proxy.h"
10 #include "base/run_loop.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "net/base/completion_callback.h"
13 #include "net/base/net_errors.h"
14 #include "storage/browser/database/database_quota_client.h"
15 #include "storage/browser/database/database_tracker.h"
16 #include "storage/browser/database/database_util.h"
17 #include "storage/common/database/database_identifier.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 using storage::DatabaseQuotaClient
;
21 using storage::DatabaseTracker
;
22 using storage::OriginInfo
;
26 // Declared to shorten the line lengths.
27 static const storage::StorageType kTemp
= storage::kStorageTypeTemporary
;
28 static const storage::StorageType kPerm
= storage::kStorageTypePersistent
;
30 // Mock tracker class the mocks up those methods of the tracker
31 // that are used by the QuotaClient.
32 class MockDatabaseTracker
: public DatabaseTracker
{
35 : DatabaseTracker(base::FilePath(), false, NULL
, NULL
, NULL
),
36 delete_called_count_(0),
37 async_delete_(false) {}
39 bool GetOriginInfo(const std::string
& origin_identifier
,
40 OriginInfo
* info
) override
{
41 std::map
<GURL
, MockOriginInfo
>::const_iterator found
=
42 mock_origin_infos_
.find(
43 storage::GetOriginFromIdentifier(origin_identifier
));
44 if (found
== mock_origin_infos_
.end())
46 *info
= OriginInfo(found
->second
);
50 bool GetAllOriginIdentifiers(
51 std::vector
<std::string
>* origins_identifiers
) override
{
52 std::map
<GURL
, MockOriginInfo
>::const_iterator iter
;
53 for (iter
= mock_origin_infos_
.begin();
54 iter
!= mock_origin_infos_
.end();
56 origins_identifiers
->push_back(iter
->second
.GetOriginIdentifier());
61 bool GetAllOriginsInfo(std::vector
<OriginInfo
>* origins_info
) override
{
62 std::map
<GURL
, MockOriginInfo
>::const_iterator iter
;
63 for (iter
= mock_origin_infos_
.begin();
64 iter
!= mock_origin_infos_
.end();
66 origins_info
->push_back(OriginInfo(iter
->second
));
71 int DeleteDataForOrigin(const std::string
& origin_identifier
,
72 const net::CompletionCallback
& callback
) override
{
73 ++delete_called_count_
;
75 base::MessageLoopProxy::current()->PostTask(
77 base::Bind(&MockDatabaseTracker::AsyncDeleteDataForOrigin
, this,
79 return net::ERR_IO_PENDING
;
84 void AsyncDeleteDataForOrigin(const net::CompletionCallback
& callback
) {
85 callback
.Run(net::OK
);
88 void AddMockDatabase(const GURL
& origin
, const char* name
, int size
) {
89 MockOriginInfo
& info
= mock_origin_infos_
[origin
];
90 info
.set_origin(storage::GetIdentifierFromOrigin(origin
));
91 info
.AddMockDatabase(base::ASCIIToUTF16(name
), size
);
94 int delete_called_count() { return delete_called_count_
; }
95 bool async_delete() { return async_delete_
; }
96 void set_async_delete(bool async
) { async_delete_
= async
; }
99 ~MockDatabaseTracker() override
{}
102 class MockOriginInfo
: public OriginInfo
{
104 void set_origin(const std::string
& origin_identifier
) {
105 origin_identifier_
= origin_identifier
;
108 void AddMockDatabase(const base::string16
& name
, int size
) {
109 EXPECT_TRUE(database_info_
.find(name
) == database_info_
.end());
110 database_info_
[name
].first
= size
;
115 int delete_called_count_
;
117 std::map
<GURL
, MockOriginInfo
> mock_origin_infos_
;
121 // Base class for our test fixtures.
122 class DatabaseQuotaClientTest
: public testing::Test
{
126 const GURL kOriginOther
;
128 DatabaseQuotaClientTest()
129 : kOriginA("http://host"),
130 kOriginB("http://host:8000"),
131 kOriginOther("http://other"),
133 mock_tracker_(new MockDatabaseTracker
),
134 weak_factory_(this) {
137 int64
GetOriginUsage(storage::QuotaClient
* client
,
139 storage::StorageType type
) {
141 client
->GetOriginUsage(
143 base::Bind(&DatabaseQuotaClientTest::OnGetOriginUsageComplete
,
144 weak_factory_
.GetWeakPtr()));
145 base::RunLoop().RunUntilIdle();
149 const std::set
<GURL
>& GetOriginsForType(storage::QuotaClient
* client
,
150 storage::StorageType type
) {
152 client
->GetOriginsForType(
154 base::Bind(&DatabaseQuotaClientTest::OnGetOriginsComplete
,
155 weak_factory_
.GetWeakPtr()));
156 base::RunLoop().RunUntilIdle();
160 const std::set
<GURL
>& GetOriginsForHost(storage::QuotaClient
* client
,
161 storage::StorageType type
,
162 const std::string
& host
) {
164 client
->GetOriginsForHost(
166 base::Bind(&DatabaseQuotaClientTest::OnGetOriginsComplete
,
167 weak_factory_
.GetWeakPtr()));
168 base::RunLoop().RunUntilIdle();
172 bool DeleteOriginData(storage::QuotaClient
* client
,
173 storage::StorageType type
,
174 const GURL
& origin
) {
175 delete_status_
= storage::kQuotaStatusUnknown
;
176 client
->DeleteOriginData(
178 base::Bind(&DatabaseQuotaClientTest::OnDeleteOriginDataComplete
,
179 weak_factory_
.GetWeakPtr()));
180 base::RunLoop().RunUntilIdle();
181 return delete_status_
== storage::kQuotaStatusOk
;
184 MockDatabaseTracker
* mock_tracker() { return mock_tracker_
.get(); }
188 void OnGetOriginUsageComplete(int64 usage
) {
192 void OnGetOriginsComplete(const std::set
<GURL
>& origins
) {
196 void OnDeleteOriginDataComplete(storage::QuotaStatusCode status
) {
197 delete_status_
= status
;
200 base::MessageLoop message_loop_
;
202 std::set
<GURL
> origins_
;
203 storage::QuotaStatusCode delete_status_
;
204 scoped_refptr
<MockDatabaseTracker
> mock_tracker_
;
205 base::WeakPtrFactory
<DatabaseQuotaClientTest
> weak_factory_
;
209 TEST_F(DatabaseQuotaClientTest
, GetOriginUsage
) {
210 DatabaseQuotaClient
client(base::MessageLoopProxy::current().get(),
213 EXPECT_EQ(0, GetOriginUsage(&client
, kOriginA
, kTemp
));
214 EXPECT_EQ(0, GetOriginUsage(&client
, kOriginA
, kPerm
));
216 mock_tracker()->AddMockDatabase(kOriginA
, "fooDB", 1000);
217 EXPECT_EQ(1000, GetOriginUsage(&client
, kOriginA
, kTemp
));
218 EXPECT_EQ(0, GetOriginUsage(&client
, kOriginA
, kPerm
));
220 EXPECT_EQ(0, GetOriginUsage(&client
, kOriginB
, kPerm
));
221 EXPECT_EQ(0, GetOriginUsage(&client
, kOriginB
, kTemp
));
224 TEST_F(DatabaseQuotaClientTest
, GetOriginsForHost
) {
225 DatabaseQuotaClient
client(base::MessageLoopProxy::current().get(),
228 EXPECT_EQ(kOriginA
.host(), kOriginB
.host());
229 EXPECT_NE(kOriginA
.host(), kOriginOther
.host());
231 std::set
<GURL
> origins
= GetOriginsForHost(&client
, kTemp
, kOriginA
.host());
232 EXPECT_TRUE(origins
.empty());
234 mock_tracker()->AddMockDatabase(kOriginA
, "fooDB", 1000);
235 origins
= GetOriginsForHost(&client
, kTemp
, kOriginA
.host());
236 EXPECT_EQ(origins
.size(), 1ul);
237 EXPECT_TRUE(origins
.find(kOriginA
) != origins
.end());
239 mock_tracker()->AddMockDatabase(kOriginB
, "barDB", 1000);
240 origins
= GetOriginsForHost(&client
, kTemp
, kOriginA
.host());
241 EXPECT_EQ(origins
.size(), 2ul);
242 EXPECT_TRUE(origins
.find(kOriginA
) != origins
.end());
243 EXPECT_TRUE(origins
.find(kOriginB
) != origins
.end());
245 EXPECT_TRUE(GetOriginsForHost(&client
, kPerm
, kOriginA
.host()).empty());
246 EXPECT_TRUE(GetOriginsForHost(&client
, kTemp
, kOriginOther
.host()).empty());
249 TEST_F(DatabaseQuotaClientTest
, GetOriginsForType
) {
250 DatabaseQuotaClient
client(base::MessageLoopProxy::current().get(),
253 EXPECT_TRUE(GetOriginsForType(&client
, kTemp
).empty());
254 EXPECT_TRUE(GetOriginsForType(&client
, kPerm
).empty());
256 mock_tracker()->AddMockDatabase(kOriginA
, "fooDB", 1000);
257 std::set
<GURL
> origins
= GetOriginsForType(&client
, kTemp
);
258 EXPECT_EQ(origins
.size(), 1ul);
259 EXPECT_TRUE(origins
.find(kOriginA
) != origins
.end());
261 EXPECT_TRUE(GetOriginsForType(&client
, kPerm
).empty());
264 TEST_F(DatabaseQuotaClientTest
, DeleteOriginData
) {
265 DatabaseQuotaClient
client(base::MessageLoopProxy::current().get(),
268 // Perm deletions are short circuited in the Client and
269 // should not reach the DatabaseTracker.
270 EXPECT_TRUE(DeleteOriginData(&client
, kPerm
, kOriginA
));
271 EXPECT_EQ(0, mock_tracker()->delete_called_count());
273 mock_tracker()->set_async_delete(false);
274 EXPECT_TRUE(DeleteOriginData(&client
, kTemp
, kOriginA
));
275 EXPECT_EQ(1, mock_tracker()->delete_called_count());
277 mock_tracker()->set_async_delete(true);
278 EXPECT_TRUE(DeleteOriginData(&client
, kTemp
, kOriginA
));
279 EXPECT_EQ(2, mock_tracker()->delete_called_count());
282 } // namespace content