Chromium Code Reviews| Index: webkit/quota/quota_database_unittest.cc |
| diff --git a/webkit/quota/quota_database_unittest.cc b/webkit/quota/quota_database_unittest.cc |
| index 6e80c851555619ac1025fff2c8c6d43ded08aad4..9a12aa187face6d7ba957234a524a8b452d47d4f 100644 |
| --- a/webkit/quota/quota_database_unittest.cc |
| +++ b/webkit/quota/quota_database_unittest.cc |
| @@ -7,6 +7,10 @@ |
| #include <set> |
| #include "app/sql/connection.h" |
| +#include "app/sql/statement.h" |
| +#include "app/sql/transaction.h" |
| +#include "base/bind.h" |
| +#include "base/callback.h" |
| #include "base/file_util.h" |
| #include "base/scoped_temp_dir.h" |
| #include "googleurl/src/gurl.h" |
| @@ -25,13 +29,81 @@ class TestErrorDelegate : public sql::ErrorDelegate { |
| return error; |
| } |
| }; |
| - |
| } // namespace |
| namespace quota { |
| class QuotaDatabaseTest : public testing::Test { |
| protected: |
| + typedef QuotaDatabase::QuotaTableEntry QuotaTableEntry; |
| + typedef QuotaDatabase::QuotaTableCallback QuotaTableCallback; |
| + typedef QuotaDatabase::LastAccessTimeTableEntry LastAccessTimeTableEntry; |
| + typedef QuotaDatabase::LastAccessTimeTableCallback |
| + LastAccessTimeTableCallback; |
|
kinuko
2011/05/23 08:16:52
style-nit: There're mixed notions but I may expect
tzik
2011/05/24 04:32:41
Done.
|
| + |
| + template<typename Iterator> |
| + bool AssignQuotaTable( |
| + QuotaDatabase* quota_database, Iterator itr, Iterator end) { |
| + if (!quota_database->LazyOpen(true)) |
| + return false; |
| + |
| + sql::Transaction transaction(quota_database->db_.get()); |
| + if (!transaction.Begin()) |
| + return false; |
| + |
| + for (;itr != end; ++itr) { |
|
kinuko
2011/05/23 08:16:52
style-nit: space after ';'
tzik
2011/05/24 04:32:41
Done.
|
| + const char* kSql = |
| + "INSERT INTO HostQuotaTable" |
| + " (host, type, quota)" |
| + " VALUES (?, ?, ?)"; |
| + sql::Statement statement; |
| + statement.Assign( |
| + quota_database->db_->GetCachedStatement( |
| + SQL_FROM_HERE, kSql)); |
| + EXPECT_TRUE(statement.is_valid()); |
| + |
| + statement.BindString(0, itr->host); |
| + statement.BindInt(1, static_cast<int>(itr->type)); |
| + statement.BindInt64(2, itr->quota); |
| + if (!statement.Run()) |
| + return false; |
| + } |
| + |
| + return transaction.Commit(); |
|
kinuko
2011/05/23 08:16:52
QuotaDatabase now always keeps one transaction ope
tzik
2011/05/24 04:32:41
Done.
|
| + } |
| + |
| + template<typename Iterator> |
| + bool AssignLastAccessTimeTable( |
| + QuotaDatabase* quota_database, Iterator itr, Iterator end) { |
| + if (!quota_database->LazyOpen(true)) |
| + return false; |
| + |
| + sql::Transaction transaction(quota_database->db_.get()); |
| + if (!transaction.Begin()) |
| + return false; |
| + |
| + for (;itr != end; ++itr) { |
|
kinuko
2011/05/23 08:16:52
style-nit: space after ';'
tzik
2011/05/24 04:32:41
Done.
|
| + const char* kSql = |
| + "INSERT INTO OriginLastAccessTable" |
| + " (origin, type, used_count, last_access_time)" |
| + " VALUES (?, ?, ?, ?)"; |
| + sql::Statement statement; |
| + statement.Assign( |
| + quota_database->db_->GetCachedStatement( |
| + SQL_FROM_HERE, kSql)); |
| + EXPECT_TRUE(statement.is_valid()); |
| + |
| + statement.BindString(0, itr->origin.spec()); |
| + statement.BindInt(1, static_cast<int>(itr->type)); |
| + statement.BindInt(2, itr->used_count); |
| + statement.BindInt64(3, itr->last_access_time.ToInternalValue()); |
| + if (!statement.Run()) |
| + return false; |
| + } |
| + |
| + return transaction.Commit(); |
|
kinuko
2011/05/23 08:16:52
ditto
tzik
2011/05/24 04:32:41
Done.
|
| + } |
| + |
| void LazyOpen(const FilePath& kDbFile) { |
| QuotaDatabase db(kDbFile); |
| EXPECT_FALSE(db.LazyOpen(false)); |
| @@ -192,6 +264,64 @@ class QuotaDatabaseTest : public testing::Test { |
| &used_count)); |
| EXPECT_EQ(1, used_count); |
| } |
| + |
| + template<typename EntryType> |
| + struct EntryVerifier { |
| + std::set<EntryType> table; |
| + |
| + template<typename Iterator> |
| + EntryVerifier(Iterator itr, Iterator end) |
| + :table(itr, end) {} |
|
kinuko
2011/05/23 08:16:52
style-nit: space after ':'
tzik
2011/05/24 04:32:41
Done.
|
| + |
| + bool Run(const EntryType& entry) { |
| + EXPECT_EQ(1u, table.erase(entry)); |
| + return true; |
| + } |
| + }; |
| + |
| + void DumpQuotaTable(const FilePath& kDbFile) { |
| + QuotaTableEntry kTableEntries[] = { |
| + {"http://go/", kStorageTypeTemporary, 1}, |
| + {"http://oo/", kStorageTypeTemporary, 2}, |
| + {"http://gle/", kStorageTypePersistent, 3} |
| + }; |
| + QuotaTableEntry* begin = kTableEntries; |
| + QuotaTableEntry* end = kTableEntries + ARRAYSIZE_UNSAFE(kTableEntries); |
| + |
| + QuotaDatabase db(kDbFile); |
| + EXPECT_TRUE(AssignQuotaTable(&db, begin, end)); |
| + |
| + typedef EntryVerifier<QuotaTableEntry> Verifier; |
| + Verifier verifier(begin, end); |
| + EXPECT_TRUE(db.DumpQuotaTable( |
| + new QuotaTableCallback( |
| + base::Bind(&Verifier::Run, |
| + base::Unretained(&verifier))))); |
| + EXPECT_TRUE(verifier.table.empty()); |
| + } |
| + |
| + void DumpLastAccessTimeTable(const FilePath& kDbFile) { |
| + base::Time now(base::Time::Now()); |
| + LastAccessTimeTableEntry kTableEntries[] = { |
| + {GURL("http://go/"), kStorageTypeTemporary, 2147483647, now}, |
| + {GURL("http://oo/"), kStorageTypeTemporary, 0, now}, |
| + {GURL("http://gle/"), kStorageTypeTemporary, 1, now}, |
| + }; |
| + LastAccessTimeTableEntry* begin = kTableEntries; |
| + LastAccessTimeTableEntry* end = kTableEntries + |
| + ARRAYSIZE_UNSAFE(kTableEntries); |
| + |
| + QuotaDatabase db(kDbFile); |
| + EXPECT_TRUE(AssignLastAccessTimeTable(&db, begin, end)); |
| + |
| + typedef EntryVerifier<LastAccessTimeTableEntry> Verifier; |
| + Verifier verifier(begin, end); |
| + EXPECT_TRUE(db.DumpLastAccessTimeTable( |
| + new LastAccessTimeTableCallback( |
| + base::Bind(&Verifier::Run, |
| + base::Unretained(&verifier))))); |
| + EXPECT_TRUE(verifier.table.empty()); |
| + } |
| }; |
| TEST_F(QuotaDatabaseTest, LazyOpen) { |
| @@ -247,4 +377,20 @@ TEST_F(QuotaDatabaseTest, RegisterOrigins) { |
| RegisterOrigins(kDbFile); |
| RegisterOrigins(FilePath()); |
| } |
| + |
| +TEST_F(QuotaDatabaseTest, DumpQuotaTable) { |
| + ScopedTempDir data_dir; |
| + ASSERT_TRUE(data_dir.CreateUniqueTempDir()); |
| + const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); |
| + DumpQuotaTable(kDbFile); |
| + DumpQuotaTable(FilePath()); |
| +} |
| + |
| +TEST_F(QuotaDatabaseTest, DumpLastAccessTimeTable) { |
| + ScopedTempDir data_dir; |
| + ASSERT_TRUE(data_dir.CreateUniqueTempDir()); |
| + const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); |
| + DumpLastAccessTimeTable(kDbFile); |
| + DumpLastAccessTimeTable(FilePath()); |
| +} |
| } // namespace quota |