OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <algorithm> | 5 #include <algorithm> |
6 #include <iterator> | 6 #include <iterator> |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "app/sql/connection.h" | 9 #include "app/sql/connection.h" |
| 10 #include "app/sql/statement.h" |
| 11 #include "app/sql/transaction.h" |
| 12 #include "base/bind.h" |
| 13 #include "base/callback.h" |
10 #include "base/file_util.h" | 14 #include "base/file_util.h" |
11 #include "base/scoped_temp_dir.h" | 15 #include "base/scoped_temp_dir.h" |
12 #include "googleurl/src/gurl.h" | 16 #include "googleurl/src/gurl.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "webkit/quota/quota_database.h" | 18 #include "webkit/quota/quota_database.h" |
15 | 19 |
16 namespace { | 20 namespace { |
17 | 21 |
18 const base::Time kZeroTime; | 22 const base::Time kZeroTime; |
19 | 23 |
20 class TestErrorDelegate : public sql::ErrorDelegate { | 24 class TestErrorDelegate : public sql::ErrorDelegate { |
21 public: | 25 public: |
22 virtual ~TestErrorDelegate() { } | 26 virtual ~TestErrorDelegate() { } |
23 virtual int OnError( | 27 virtual int OnError( |
24 int error, sql::Connection* connection, sql::Statement* stmt) { | 28 int error, sql::Connection* connection, sql::Statement* stmt) { |
25 return error; | 29 return error; |
26 } | 30 } |
27 }; | 31 }; |
28 | |
29 } // namespace | 32 } // namespace |
30 | 33 |
31 namespace quota { | 34 namespace quota { |
32 | 35 |
33 class QuotaDatabaseTest : public testing::Test { | 36 class QuotaDatabaseTest : public testing::Test { |
34 protected: | 37 protected: |
| 38 typedef QuotaDatabase::QuotaTableEntry QuotaTableEntry; |
| 39 typedef QuotaDatabase::QuotaTableCallback QuotaTableCallback; |
| 40 typedef QuotaDatabase::LastAccessTimeTableEntry LastAccessTimeTableEntry; |
| 41 typedef QuotaDatabase::LastAccessTimeTableCallback |
| 42 LastAccessTimeTableCallback; |
| 43 |
| 44 template<typename Iterator> |
| 45 bool AssignQuotaTable( |
| 46 QuotaDatabase* quota_database, Iterator itr, Iterator end) { |
| 47 if (!quota_database->LazyOpen(true)) |
| 48 return false; |
| 49 |
| 50 sql::Transaction transaction(quota_database->db_.get()); |
| 51 if (!transaction.Begin()) |
| 52 return false; |
| 53 |
| 54 for (;itr != end; ++itr) { |
| 55 const char* kSql = |
| 56 "INSERT INTO HostQuotaTable" |
| 57 " (host, type, quota)" |
| 58 " VALUES (?, ?, ?)"; |
| 59 sql::Statement statement; |
| 60 statement.Assign( |
| 61 quota_database->db_->GetCachedStatement( |
| 62 SQL_FROM_HERE, kSql)); |
| 63 EXPECT_TRUE(statement.is_valid()); |
| 64 |
| 65 statement.BindString(0, itr->host); |
| 66 statement.BindInt(1, static_cast<int>(itr->type)); |
| 67 statement.BindInt64(2, itr->quota); |
| 68 if (!statement.Run()) |
| 69 return false; |
| 70 } |
| 71 |
| 72 return transaction.Commit(); |
| 73 } |
| 74 |
| 75 template<typename Iterator> |
| 76 bool AssignLastAccessTimeTable( |
| 77 QuotaDatabase* quota_database, Iterator itr, Iterator end) { |
| 78 if (!quota_database->LazyOpen(true)) |
| 79 return false; |
| 80 |
| 81 sql::Transaction transaction(quota_database->db_.get()); |
| 82 if (!transaction.Begin()) |
| 83 return false; |
| 84 |
| 85 for (;itr != end; ++itr) { |
| 86 const char* kSql = |
| 87 "INSERT INTO OriginLastAccessTable" |
| 88 " (origin, type, used_count, last_access_time)" |
| 89 " VALUES (?, ?, ?, ?)"; |
| 90 sql::Statement statement; |
| 91 statement.Assign( |
| 92 quota_database->db_->GetCachedStatement( |
| 93 SQL_FROM_HERE, kSql)); |
| 94 EXPECT_TRUE(statement.is_valid()); |
| 95 |
| 96 statement.BindString(0, itr->origin.spec()); |
| 97 statement.BindInt(1, static_cast<int>(itr->type)); |
| 98 statement.BindInt(2, itr->used_count); |
| 99 statement.BindInt64(3, itr->last_access_time.ToInternalValue()); |
| 100 if (!statement.Run()) |
| 101 return false; |
| 102 } |
| 103 |
| 104 return transaction.Commit(); |
| 105 } |
| 106 |
35 void LazyOpen(const FilePath& kDbFile) { | 107 void LazyOpen(const FilePath& kDbFile) { |
36 QuotaDatabase db(kDbFile); | 108 QuotaDatabase db(kDbFile); |
37 EXPECT_FALSE(db.LazyOpen(false)); | 109 EXPECT_FALSE(db.LazyOpen(false)); |
38 ASSERT_TRUE(db.LazyOpen(true)); | 110 ASSERT_TRUE(db.LazyOpen(true)); |
39 EXPECT_TRUE(db.db_.get()); | 111 EXPECT_TRUE(db.db_.get()); |
40 EXPECT_TRUE(kDbFile.empty() || file_util::PathExists(kDbFile)); | 112 EXPECT_TRUE(kDbFile.empty() || file_util::PathExists(kDbFile)); |
41 } | 113 } |
42 | 114 |
43 void HostQuota(const FilePath& kDbFile) { | 115 void HostQuota(const FilePath& kDbFile) { |
44 QuotaDatabase db(kDbFile); | 116 QuotaDatabase db(kDbFile); |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 EXPECT_TRUE(db.RegisterOrigins(origins, | 257 EXPECT_TRUE(db.RegisterOrigins(origins, |
186 kStorageTypeTemporary, | 258 kStorageTypeTemporary, |
187 base::Time())); | 259 base::Time())); |
188 | 260 |
189 used_count = -1; | 261 used_count = -1; |
190 EXPECT_TRUE(db.FindOriginUsedCount(GURL("http://a/"), | 262 EXPECT_TRUE(db.FindOriginUsedCount(GURL("http://a/"), |
191 kStorageTypeTemporary, | 263 kStorageTypeTemporary, |
192 &used_count)); | 264 &used_count)); |
193 EXPECT_EQ(1, used_count); | 265 EXPECT_EQ(1, used_count); |
194 } | 266 } |
| 267 |
| 268 template<typename EntryType> |
| 269 struct EntryVerifier { |
| 270 std::set<EntryType> table; |
| 271 |
| 272 template<typename Iterator> |
| 273 EntryVerifier(Iterator itr, Iterator end) |
| 274 :table(itr, end) {} |
| 275 |
| 276 bool Run(const EntryType& entry) { |
| 277 EXPECT_EQ(1u, table.erase(entry)); |
| 278 return true; |
| 279 } |
| 280 }; |
| 281 |
| 282 void DumpQuotaTable(const FilePath& kDbFile) { |
| 283 QuotaTableEntry kTableEntries[] = { |
| 284 {"http://go/", kStorageTypeTemporary, 1}, |
| 285 {"http://oo/", kStorageTypeTemporary, 2}, |
| 286 {"http://gle/", kStorageTypePersistent, 3} |
| 287 }; |
| 288 QuotaTableEntry* begin = kTableEntries; |
| 289 QuotaTableEntry* end = kTableEntries + ARRAYSIZE_UNSAFE(kTableEntries); |
| 290 |
| 291 QuotaDatabase db(kDbFile); |
| 292 EXPECT_TRUE(AssignQuotaTable(&db, begin, end)); |
| 293 |
| 294 typedef EntryVerifier<QuotaTableEntry> Verifier; |
| 295 Verifier verifier(begin, end); |
| 296 QuotaTableCallback callback = base::Bind(&Verifier::Run, |
| 297 base::Unretained(&verifier)); |
| 298 EXPECT_TRUE(db.DumpQuotaTable(&callback)); |
| 299 EXPECT_TRUE(verifier.table.empty()); |
| 300 } |
| 301 |
| 302 void DumpLastAccessTimeTable(const FilePath& kDbFile) { |
| 303 base::Time now(base::Time::Now()); |
| 304 LastAccessTimeTableEntry kTableEntries[] = { |
| 305 {GURL("http://go/"), kStorageTypeTemporary, 2147483647, now}, |
| 306 {GURL("http://oo/"), kStorageTypeTemporary, 0, now}, |
| 307 {GURL("http://gle/"), kStorageTypeTemporary, 1, now}, |
| 308 }; |
| 309 LastAccessTimeTableEntry* begin = kTableEntries; |
| 310 LastAccessTimeTableEntry* end = kTableEntries + |
| 311 ARRAYSIZE_UNSAFE(kTableEntries); |
| 312 |
| 313 QuotaDatabase db(kDbFile); |
| 314 EXPECT_TRUE(AssignLastAccessTimeTable(&db, begin, end)); |
| 315 |
| 316 typedef EntryVerifier<LastAccessTimeTableEntry> Verifier; |
| 317 Verifier verifier(begin, end); |
| 318 LastAccessTimeTableCallback callback = |
| 319 base::Bind(&Verifier::Run, |
| 320 base::Unretained(&verifier)); |
| 321 EXPECT_TRUE(db.DumpLastAccessTimeTable(&callback)); |
| 322 EXPECT_TRUE(verifier.table.empty()); |
| 323 } |
195 }; | 324 }; |
196 | 325 |
197 TEST_F(QuotaDatabaseTest, LazyOpen) { | 326 TEST_F(QuotaDatabaseTest, LazyOpen) { |
198 ScopedTempDir data_dir; | 327 ScopedTempDir data_dir; |
199 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); | 328 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); |
200 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); | 329 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); |
201 LazyOpen(kDbFile); | 330 LazyOpen(kDbFile); |
202 LazyOpen(FilePath()); | 331 LazyOpen(FilePath()); |
203 } | 332 } |
204 | 333 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 EXPECT_FALSE(db.IsOriginDatabaseBootstrapped()); | 369 EXPECT_FALSE(db.IsOriginDatabaseBootstrapped()); |
241 } | 370 } |
242 | 371 |
243 TEST_F(QuotaDatabaseTest, RegisterOrigins) { | 372 TEST_F(QuotaDatabaseTest, RegisterOrigins) { |
244 ScopedTempDir data_dir; | 373 ScopedTempDir data_dir; |
245 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); | 374 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); |
246 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); | 375 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); |
247 RegisterOrigins(kDbFile); | 376 RegisterOrigins(kDbFile); |
248 RegisterOrigins(FilePath()); | 377 RegisterOrigins(FilePath()); |
249 } | 378 } |
| 379 |
| 380 TEST_F(QuotaDatabaseTest, DumpQuotaTable) { |
| 381 ScopedTempDir data_dir; |
| 382 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); |
| 383 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); |
| 384 DumpQuotaTable(kDbFile); |
| 385 DumpQuotaTable(FilePath()); |
| 386 } |
| 387 |
| 388 TEST_F(QuotaDatabaseTest, DumpLastAccessTimeTable) { |
| 389 ScopedTempDir data_dir; |
| 390 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); |
| 391 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); |
| 392 DumpLastAccessTimeTable(kDbFile); |
| 393 DumpLastAccessTimeTable(FilePath()); |
| 394 } |
250 } // namespace quota | 395 } // namespace quota |
OLD | NEW |