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 "testing/gtest/include/gtest/gtest.h"
15 #include "webkit/browser/database/database_quota_client.h"
16 #include "webkit/browser/database/database_tracker.h"
17 #include "webkit/browser/database/database_util.h"
18 #include "webkit/common/database/database_identifier.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 virtual bool GetOriginInfo(
40 const std::string
& origin_identifier
,
41 OriginInfo
* info
) OVERRIDE
{
42 std::map
<GURL
, MockOriginInfo
>::const_iterator found
=
43 mock_origin_infos_
.find(
44 storage::GetOriginFromIdentifier(origin_identifier
));
45 if (found
== mock_origin_infos_
.end())
47 *info
= OriginInfo(found
->second
);
51 virtual bool GetAllOriginIdentifiers(
52 std::vector
<std::string
>* origins_identifiers
) OVERRIDE
{
53 std::map
<GURL
, MockOriginInfo
>::const_iterator iter
;
54 for (iter
= mock_origin_infos_
.begin();
55 iter
!= mock_origin_infos_
.end();
57 origins_identifiers
->push_back(iter
->second
.GetOriginIdentifier());
62 virtual bool GetAllOriginsInfo(
63 std::vector
<OriginInfo
>* origins_info
) OVERRIDE
{
64 std::map
<GURL
, MockOriginInfo
>::const_iterator iter
;
65 for (iter
= mock_origin_infos_
.begin();
66 iter
!= mock_origin_infos_
.end();
68 origins_info
->push_back(OriginInfo(iter
->second
));
73 virtual int DeleteDataForOrigin(
74 const std::string
& origin_identifier
,
75 const net::CompletionCallback
& callback
) OVERRIDE
{
76 ++delete_called_count_
;
78 base::MessageLoopProxy::current()->PostTask(
80 base::Bind(&MockDatabaseTracker::AsyncDeleteDataForOrigin
, this,
82 return net::ERR_IO_PENDING
;
87 void AsyncDeleteDataForOrigin(const net::CompletionCallback
& callback
) {
88 callback
.Run(net::OK
);
91 void AddMockDatabase(const GURL
& origin
, const char* name
, int size
) {
92 MockOriginInfo
& info
= mock_origin_infos_
[origin
];
93 info
.set_origin(storage::GetIdentifierFromOrigin(origin
));
94 info
.AddMockDatabase(base::ASCIIToUTF16(name
), size
);
97 int delete_called_count() { return delete_called_count_
; }
98 bool async_delete() { return async_delete_
; }
99 void set_async_delete(bool async
) { async_delete_
= async
; }
102 virtual ~MockDatabaseTracker() {}
105 class MockOriginInfo
: public OriginInfo
{
107 void set_origin(const std::string
& origin_identifier
) {
108 origin_identifier_
= origin_identifier
;
111 void AddMockDatabase(const base::string16
& name
, int size
) {
112 EXPECT_TRUE(database_info_
.find(name
) == database_info_
.end());
113 database_info_
[name
].first
= size
;
118 int delete_called_count_
;
120 std::map
<GURL
, MockOriginInfo
> mock_origin_infos_
;
124 // Base class for our test fixtures.
125 class DatabaseQuotaClientTest
: public testing::Test
{
129 const GURL kOriginOther
;
131 DatabaseQuotaClientTest()
132 : kOriginA("http://host"),
133 kOriginB("http://host:8000"),
134 kOriginOther("http://other"),
136 mock_tracker_(new MockDatabaseTracker
),
137 weak_factory_(this) {
140 int64
GetOriginUsage(storage::QuotaClient
* client
,
142 storage::StorageType type
) {
144 client
->GetOriginUsage(
146 base::Bind(&DatabaseQuotaClientTest::OnGetOriginUsageComplete
,
147 weak_factory_
.GetWeakPtr()));
148 base::RunLoop().RunUntilIdle();
152 const std::set
<GURL
>& GetOriginsForType(storage::QuotaClient
* client
,
153 storage::StorageType type
) {
155 client
->GetOriginsForType(
157 base::Bind(&DatabaseQuotaClientTest::OnGetOriginsComplete
,
158 weak_factory_
.GetWeakPtr()));
159 base::RunLoop().RunUntilIdle();
163 const std::set
<GURL
>& GetOriginsForHost(storage::QuotaClient
* client
,
164 storage::StorageType type
,
165 const std::string
& host
) {
167 client
->GetOriginsForHost(
169 base::Bind(&DatabaseQuotaClientTest::OnGetOriginsComplete
,
170 weak_factory_
.GetWeakPtr()));
171 base::RunLoop().RunUntilIdle();
175 bool DeleteOriginData(storage::QuotaClient
* client
,
176 storage::StorageType type
,
177 const GURL
& origin
) {
178 delete_status_
= storage::kQuotaStatusUnknown
;
179 client
->DeleteOriginData(
181 base::Bind(&DatabaseQuotaClientTest::OnDeleteOriginDataComplete
,
182 weak_factory_
.GetWeakPtr()));
183 base::RunLoop().RunUntilIdle();
184 return delete_status_
== storage::kQuotaStatusOk
;
187 MockDatabaseTracker
* mock_tracker() { return mock_tracker_
.get(); }
191 void OnGetOriginUsageComplete(int64 usage
) {
195 void OnGetOriginsComplete(const std::set
<GURL
>& origins
) {
199 void OnDeleteOriginDataComplete(storage::QuotaStatusCode status
) {
200 delete_status_
= status
;
203 base::MessageLoop message_loop_
;
205 std::set
<GURL
> origins_
;
206 storage::QuotaStatusCode delete_status_
;
207 scoped_refptr
<MockDatabaseTracker
> mock_tracker_
;
208 base::WeakPtrFactory
<DatabaseQuotaClientTest
> weak_factory_
;
212 TEST_F(DatabaseQuotaClientTest
, GetOriginUsage
) {
213 DatabaseQuotaClient
client(base::MessageLoopProxy::current().get(),
216 EXPECT_EQ(0, GetOriginUsage(&client
, kOriginA
, kTemp
));
217 EXPECT_EQ(0, GetOriginUsage(&client
, kOriginA
, kPerm
));
219 mock_tracker()->AddMockDatabase(kOriginA
, "fooDB", 1000);
220 EXPECT_EQ(1000, GetOriginUsage(&client
, kOriginA
, kTemp
));
221 EXPECT_EQ(0, GetOriginUsage(&client
, kOriginA
, kPerm
));
223 EXPECT_EQ(0, GetOriginUsage(&client
, kOriginB
, kPerm
));
224 EXPECT_EQ(0, GetOriginUsage(&client
, kOriginB
, kTemp
));
227 TEST_F(DatabaseQuotaClientTest
, GetOriginsForHost
) {
228 DatabaseQuotaClient
client(base::MessageLoopProxy::current().get(),
231 EXPECT_EQ(kOriginA
.host(), kOriginB
.host());
232 EXPECT_NE(kOriginA
.host(), kOriginOther
.host());
234 std::set
<GURL
> origins
= GetOriginsForHost(&client
, kTemp
, kOriginA
.host());
235 EXPECT_TRUE(origins
.empty());
237 mock_tracker()->AddMockDatabase(kOriginA
, "fooDB", 1000);
238 origins
= GetOriginsForHost(&client
, kTemp
, kOriginA
.host());
239 EXPECT_EQ(origins
.size(), 1ul);
240 EXPECT_TRUE(origins
.find(kOriginA
) != origins
.end());
242 mock_tracker()->AddMockDatabase(kOriginB
, "barDB", 1000);
243 origins
= GetOriginsForHost(&client
, kTemp
, kOriginA
.host());
244 EXPECT_EQ(origins
.size(), 2ul);
245 EXPECT_TRUE(origins
.find(kOriginA
) != origins
.end());
246 EXPECT_TRUE(origins
.find(kOriginB
) != origins
.end());
248 EXPECT_TRUE(GetOriginsForHost(&client
, kPerm
, kOriginA
.host()).empty());
249 EXPECT_TRUE(GetOriginsForHost(&client
, kTemp
, kOriginOther
.host()).empty());
252 TEST_F(DatabaseQuotaClientTest
, GetOriginsForType
) {
253 DatabaseQuotaClient
client(base::MessageLoopProxy::current().get(),
256 EXPECT_TRUE(GetOriginsForType(&client
, kTemp
).empty());
257 EXPECT_TRUE(GetOriginsForType(&client
, kPerm
).empty());
259 mock_tracker()->AddMockDatabase(kOriginA
, "fooDB", 1000);
260 std::set
<GURL
> origins
= GetOriginsForType(&client
, kTemp
);
261 EXPECT_EQ(origins
.size(), 1ul);
262 EXPECT_TRUE(origins
.find(kOriginA
) != origins
.end());
264 EXPECT_TRUE(GetOriginsForType(&client
, kPerm
).empty());
267 TEST_F(DatabaseQuotaClientTest
, DeleteOriginData
) {
268 DatabaseQuotaClient
client(base::MessageLoopProxy::current().get(),
271 // Perm deletions are short circuited in the Client and
272 // should not reach the DatabaseTracker.
273 EXPECT_TRUE(DeleteOriginData(&client
, kPerm
, kOriginA
));
274 EXPECT_EQ(0, mock_tracker()->delete_called_count());
276 mock_tracker()->set_async_delete(false);
277 EXPECT_TRUE(DeleteOriginData(&client
, kTemp
, kOriginA
));
278 EXPECT_EQ(1, mock_tracker()->delete_called_count());
280 mock_tracker()->set_async_delete(true);
281 EXPECT_TRUE(DeleteOriginData(&client
, kTemp
, kOriginA
));
282 EXPECT_EQ(2, mock_tracker()->delete_called_count());
285 } // namespace content