Revert "Merged all Chromoting Host code into remoting_core.dll (Windows)."
[chromium-blink-merge.git] / webkit / database / database_quota_client_unittest.cc
blob286347c201d951d129b0bc8bada9cd30c114e744
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.
5 #include <map>
7 #include "base/bind.h"
8 #include "base/file_path.h"
9 #include "base/message_loop.h"
10 #include "base/message_loop_proxy.h"
11 #include "base/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/database/database_quota_client.h"
16 #include "webkit/database/database_tracker.h"
17 #include "webkit/database/database_util.h"
19 namespace webkit_database {
21 // Declared to shorten the line lengths.
22 static const quota::StorageType kTemp = quota::kStorageTypeTemporary;
23 static const quota::StorageType kPerm = quota::kStorageTypePersistent;
25 // Mock tracker class the mocks up those methods of the tracker
26 // that are used by the QuotaClient.
27 class MockDatabaseTracker : public DatabaseTracker {
28 public:
29 MockDatabaseTracker()
30 : DatabaseTracker(FilePath(), false, NULL, NULL, NULL),
31 delete_called_count_(0),
32 async_delete_(false) {}
34 virtual bool GetOriginInfo(
35 const string16& origin_identifier,
36 OriginInfo* info) OVERRIDE {
37 std::map<GURL, MockOriginInfo>::const_iterator found =
38 mock_origin_infos_.find(
39 DatabaseUtil::GetOriginFromIdentifier(origin_identifier));
40 if (found == mock_origin_infos_.end())
41 return false;
42 *info = OriginInfo(found->second);
43 return true;
46 virtual bool GetAllOriginIdentifiers(
47 std::vector<string16>* origins_identifiers) OVERRIDE {
48 std::map<GURL, MockOriginInfo>::const_iterator iter;
49 for (iter = mock_origin_infos_.begin();
50 iter != mock_origin_infos_.end();
51 ++iter) {
52 origins_identifiers->push_back(iter->second.GetOrigin());
54 return true;
57 virtual bool GetAllOriginsInfo(
58 std::vector<OriginInfo>* origins_info) OVERRIDE {
59 std::map<GURL, MockOriginInfo>::const_iterator iter;
60 for (iter = mock_origin_infos_.begin();
61 iter != mock_origin_infos_.end();
62 ++iter) {
63 origins_info->push_back(OriginInfo(iter->second));
65 return true;
68 virtual int DeleteDataForOrigin(
69 const string16& origin_id,
70 const net::CompletionCallback& callback) OVERRIDE {
71 ++delete_called_count_;
72 if (async_delete()) {
73 base::MessageLoopProxy::current()->PostTask(
74 FROM_HERE,
75 base::Bind(&MockDatabaseTracker::AsyncDeleteDataForOrigin, this,
76 callback));
77 return net::ERR_IO_PENDING;
79 return net::OK;
82 void AsyncDeleteDataForOrigin(const net::CompletionCallback& callback) {
83 callback.Run(net::OK);
86 void AddMockDatabase(const GURL& origin, const char* name, int size) {
87 MockOriginInfo& info = mock_origin_infos_[origin];
88 info.set_origin(DatabaseUtil::GetOriginIdentifier(origin));
89 info.AddMockDatabase(ASCIIToUTF16(name), size);
92 int delete_called_count() { return delete_called_count_; }
93 bool async_delete() { return async_delete_; }
94 void set_async_delete(bool async) { async_delete_ = async; }
96 protected:
97 virtual ~MockDatabaseTracker() {}
99 private:
100 class MockOriginInfo : public OriginInfo {
101 public:
102 void set_origin(const string16& origin_id) {
103 origin_ = origin_id;
106 void AddMockDatabase(const string16& name, int size) {
107 EXPECT_TRUE(database_info_.find(name) == database_info_.end());
108 database_info_[name].first = size;
109 total_size_ += size;
113 int delete_called_count_;
114 bool async_delete_;
115 std::map<GURL, MockOriginInfo> mock_origin_infos_;
119 // Base class for our test fixtures.
120 class DatabaseQuotaClientTest : public testing::Test {
121 public:
122 const GURL kOriginA;
123 const GURL kOriginB;
124 const GURL kOriginOther;
126 DatabaseQuotaClientTest()
127 : kOriginA("http://host"),
128 kOriginB("http://host:8000"),
129 kOriginOther("http://other"),
130 usage_(0),
131 mock_tracker_(new MockDatabaseTracker),
132 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
135 int64 GetOriginUsage(
136 quota::QuotaClient* client,
137 const GURL& origin,
138 quota::StorageType type) {
139 usage_ = 0;
140 client->GetOriginUsage(
141 origin, type,
142 base::Bind(&DatabaseQuotaClientTest::OnGetOriginUsageComplete,
143 weak_factory_.GetWeakPtr()));
144 MessageLoop::current()->RunUntilIdle();
145 return usage_;
148 const std::set<GURL>& GetOriginsForType(
149 quota::QuotaClient* client,
150 quota::StorageType type) {
151 origins_.clear();
152 client->GetOriginsForType(
153 type,
154 base::Bind(&DatabaseQuotaClientTest::OnGetOriginsComplete,
155 weak_factory_.GetWeakPtr()));
156 MessageLoop::current()->RunUntilIdle();
157 return origins_;
160 const std::set<GURL>& GetOriginsForHost(
161 quota::QuotaClient* client,
162 quota::StorageType type,
163 const std::string& host) {
164 origins_.clear();
165 client->GetOriginsForHost(
166 type, host,
167 base::Bind(&DatabaseQuotaClientTest::OnGetOriginsComplete,
168 weak_factory_.GetWeakPtr()));
169 MessageLoop::current()->RunUntilIdle();
170 return origins_;
173 bool DeleteOriginData(
174 quota::QuotaClient* client,
175 quota::StorageType type,
176 const GURL& origin) {
177 delete_status_ = quota::kQuotaStatusUnknown;
178 client->DeleteOriginData(
179 origin, type,
180 base::Bind(&DatabaseQuotaClientTest::OnDeleteOriginDataComplete,
181 weak_factory_.GetWeakPtr()));
182 MessageLoop::current()->RunUntilIdle();
183 return delete_status_ == quota::kQuotaStatusOk;
186 MockDatabaseTracker* mock_tracker() { return mock_tracker_.get(); }
189 private:
190 void OnGetOriginUsageComplete(int64 usage) {
191 usage_ = usage;
194 void OnGetOriginsComplete(const std::set<GURL>& origins,
195 quota::StorageType type) {
196 origins_ = origins;
197 type_ = type;
200 void OnDeleteOriginDataComplete(quota::QuotaStatusCode status) {
201 delete_status_ = status;
204 MessageLoop message_loop_;
205 int64 usage_;
206 std::set<GURL> origins_;
207 quota::StorageType type_;
208 quota::QuotaStatusCode delete_status_;
209 scoped_refptr<MockDatabaseTracker> mock_tracker_;
210 base::WeakPtrFactory<DatabaseQuotaClientTest> weak_factory_;
214 TEST_F(DatabaseQuotaClientTest, GetOriginUsage) {
215 DatabaseQuotaClient client(
216 base::MessageLoopProxy::current(),
217 mock_tracker());
219 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kTemp));
220 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm));
222 mock_tracker()->AddMockDatabase(kOriginA, "fooDB", 1000);
223 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp));
224 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm));
226 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm));
227 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kTemp));
230 TEST_F(DatabaseQuotaClientTest, GetOriginsForHost) {
231 DatabaseQuotaClient client(
232 base::MessageLoopProxy::current(),
233 mock_tracker());
235 EXPECT_EQ(kOriginA.host(), kOriginB.host());
236 EXPECT_NE(kOriginA.host(), kOriginOther.host());
238 std::set<GURL> origins = GetOriginsForHost(&client, kTemp, kOriginA.host());
239 EXPECT_TRUE(origins.empty());
241 mock_tracker()->AddMockDatabase(kOriginA, "fooDB", 1000);
242 origins = GetOriginsForHost(&client, kTemp, kOriginA.host());
243 EXPECT_EQ(origins.size(), 1ul);
244 EXPECT_TRUE(origins.find(kOriginA) != origins.end());
246 mock_tracker()->AddMockDatabase(kOriginB, "barDB", 1000);
247 origins = GetOriginsForHost(&client, kTemp, kOriginA.host());
248 EXPECT_EQ(origins.size(), 2ul);
249 EXPECT_TRUE(origins.find(kOriginA) != origins.end());
250 EXPECT_TRUE(origins.find(kOriginB) != origins.end());
252 EXPECT_TRUE(GetOriginsForHost(&client, kPerm, kOriginA.host()).empty());
253 EXPECT_TRUE(GetOriginsForHost(&client, kTemp, kOriginOther.host()).empty());
256 TEST_F(DatabaseQuotaClientTest, GetOriginsForType) {
257 DatabaseQuotaClient client(
258 base::MessageLoopProxy::current(),
259 mock_tracker());
261 EXPECT_TRUE(GetOriginsForType(&client, kTemp).empty());
262 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty());
264 mock_tracker()->AddMockDatabase(kOriginA, "fooDB", 1000);
265 std::set<GURL> origins = GetOriginsForType(&client, kTemp);
266 EXPECT_EQ(origins.size(), 1ul);
267 EXPECT_TRUE(origins.find(kOriginA) != origins.end());
269 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty());
272 TEST_F(DatabaseQuotaClientTest, DeleteOriginData) {
273 DatabaseQuotaClient client(
274 base::MessageLoopProxy::current(),
275 mock_tracker());
277 // Perm deletions are short circuited in the Client and
278 // should not reach the DatabaseTracker.
279 EXPECT_TRUE(DeleteOriginData(&client, kPerm, kOriginA));
280 EXPECT_EQ(0, mock_tracker()->delete_called_count());
282 mock_tracker()->set_async_delete(false);
283 EXPECT_TRUE(DeleteOriginData(&client, kTemp, kOriginA));
284 EXPECT_EQ(1, mock_tracker()->delete_called_count());
286 mock_tracker()->set_async_delete(true);
287 EXPECT_TRUE(DeleteOriginData(&client, kTemp, kOriginA));
288 EXPECT_EQ(2, mock_tracker()->delete_called_count());
291 } // namespace webkit_database