Apply _RELATIVE relocations ahead of others.
[chromium-blink-merge.git] / content / browser / database_quota_client_unittest.cc
bloba439b5176d4079707fc058a345fec6b2615bb766
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.
5 #include <map>
7 #include "base/bind.h"
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;
24 namespace content {
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 {
33 public:
34 MockDatabaseTracker()
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())
45 return false;
46 *info = OriginInfo(found->second);
47 return true;
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();
55 ++iter) {
56 origins_identifiers->push_back(iter->second.GetOriginIdentifier());
58 return true;
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();
65 ++iter) {
66 origins_info->push_back(OriginInfo(iter->second));
68 return true;
71 int DeleteDataForOrigin(const std::string& origin_identifier,
72 const net::CompletionCallback& callback) override {
73 ++delete_called_count_;
74 if (async_delete()) {
75 base::MessageLoopProxy::current()->PostTask(
76 FROM_HERE,
77 base::Bind(&MockDatabaseTracker::AsyncDeleteDataForOrigin, this,
78 callback));
79 return net::ERR_IO_PENDING;
81 return net::OK;
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; }
98 protected:
99 ~MockDatabaseTracker() override {}
101 private:
102 class MockOriginInfo : public OriginInfo {
103 public:
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;
111 total_size_ += size;
115 int delete_called_count_;
116 bool async_delete_;
117 std::map<GURL, MockOriginInfo> mock_origin_infos_;
121 // Base class for our test fixtures.
122 class DatabaseQuotaClientTest : public testing::Test {
123 public:
124 const GURL kOriginA;
125 const GURL kOriginB;
126 const GURL kOriginOther;
128 DatabaseQuotaClientTest()
129 : kOriginA("http://host"),
130 kOriginB("http://host:8000"),
131 kOriginOther("http://other"),
132 usage_(0),
133 mock_tracker_(new MockDatabaseTracker),
134 weak_factory_(this) {
137 int64 GetOriginUsage(storage::QuotaClient* client,
138 const GURL& origin,
139 storage::StorageType type) {
140 usage_ = 0;
141 client->GetOriginUsage(
142 origin, type,
143 base::Bind(&DatabaseQuotaClientTest::OnGetOriginUsageComplete,
144 weak_factory_.GetWeakPtr()));
145 base::RunLoop().RunUntilIdle();
146 return usage_;
149 const std::set<GURL>& GetOriginsForType(storage::QuotaClient* client,
150 storage::StorageType type) {
151 origins_.clear();
152 client->GetOriginsForType(
153 type,
154 base::Bind(&DatabaseQuotaClientTest::OnGetOriginsComplete,
155 weak_factory_.GetWeakPtr()));
156 base::RunLoop().RunUntilIdle();
157 return origins_;
160 const std::set<GURL>& GetOriginsForHost(storage::QuotaClient* client,
161 storage::StorageType type,
162 const std::string& host) {
163 origins_.clear();
164 client->GetOriginsForHost(
165 type, host,
166 base::Bind(&DatabaseQuotaClientTest::OnGetOriginsComplete,
167 weak_factory_.GetWeakPtr()));
168 base::RunLoop().RunUntilIdle();
169 return origins_;
172 bool DeleteOriginData(storage::QuotaClient* client,
173 storage::StorageType type,
174 const GURL& origin) {
175 delete_status_ = storage::kQuotaStatusUnknown;
176 client->DeleteOriginData(
177 origin, type,
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(); }
187 private:
188 void OnGetOriginUsageComplete(int64 usage) {
189 usage_ = usage;
192 void OnGetOriginsComplete(const std::set<GURL>& origins) {
193 origins_ = origins;
196 void OnDeleteOriginDataComplete(storage::QuotaStatusCode status) {
197 delete_status_ = status;
200 base::MessageLoop message_loop_;
201 int64 usage_;
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(),
211 mock_tracker());
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(),
226 mock_tracker());
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(),
251 mock_tracker());
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(),
266 mock_tracker());
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