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; | |
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.
| |
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) { | |
kinuko
2011/05/23 08:16:52
style-nit: space after ';'
tzik
2011/05/24 04:32:41
Done.
| |
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(); | |
kinuko
2011/05/23 08:16:52
QuotaDatabase now always keeps one transaction ope
tzik
2011/05/24 04:32:41
Done.
| |
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) { | |
kinuko
2011/05/23 08:16:52
style-nit: space after ';'
tzik
2011/05/24 04:32:41
Done.
| |
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(); | |
kinuko
2011/05/23 08:16:52
ditto
tzik
2011/05/24 04:32:41
Done.
| |
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) {} | |
kinuko
2011/05/23 08:16:52
style-nit: space after ':'
tzik
2011/05/24 04:32:41
Done.
| |
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 EXPECT_TRUE(db.DumpQuotaTable( | |
297 new QuotaTableCallback( | |
298 base::Bind(&Verifier::Run, | |
299 base::Unretained(&verifier))))); | |
300 EXPECT_TRUE(verifier.table.empty()); | |
301 } | |
302 | |
303 void DumpLastAccessTimeTable(const FilePath& kDbFile) { | |
304 base::Time now(base::Time::Now()); | |
305 LastAccessTimeTableEntry kTableEntries[] = { | |
306 {GURL("http://go/"), kStorageTypeTemporary, 2147483647, now}, | |
307 {GURL("http://oo/"), kStorageTypeTemporary, 0, now}, | |
308 {GURL("http://gle/"), kStorageTypeTemporary, 1, now}, | |
309 }; | |
310 LastAccessTimeTableEntry* begin = kTableEntries; | |
311 LastAccessTimeTableEntry* end = kTableEntries + | |
312 ARRAYSIZE_UNSAFE(kTableEntries); | |
313 | |
314 QuotaDatabase db(kDbFile); | |
315 EXPECT_TRUE(AssignLastAccessTimeTable(&db, begin, end)); | |
316 | |
317 typedef EntryVerifier<LastAccessTimeTableEntry> Verifier; | |
318 Verifier verifier(begin, end); | |
319 EXPECT_TRUE(db.DumpLastAccessTimeTable( | |
320 new LastAccessTimeTableCallback( | |
321 base::Bind(&Verifier::Run, | |
322 base::Unretained(&verifier))))); | |
323 EXPECT_TRUE(verifier.table.empty()); | |
324 } | |
195 }; | 325 }; |
196 | 326 |
197 TEST_F(QuotaDatabaseTest, LazyOpen) { | 327 TEST_F(QuotaDatabaseTest, LazyOpen) { |
198 ScopedTempDir data_dir; | 328 ScopedTempDir data_dir; |
199 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); | 329 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); |
200 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); | 330 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); |
201 LazyOpen(kDbFile); | 331 LazyOpen(kDbFile); |
202 LazyOpen(FilePath()); | 332 LazyOpen(FilePath()); |
203 } | 333 } |
204 | 334 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
240 EXPECT_FALSE(db.IsOriginDatabaseBootstrapped()); | 370 EXPECT_FALSE(db.IsOriginDatabaseBootstrapped()); |
241 } | 371 } |
242 | 372 |
243 TEST_F(QuotaDatabaseTest, RegisterOrigins) { | 373 TEST_F(QuotaDatabaseTest, RegisterOrigins) { |
244 ScopedTempDir data_dir; | 374 ScopedTempDir data_dir; |
245 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); | 375 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); |
246 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); | 376 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); |
247 RegisterOrigins(kDbFile); | 377 RegisterOrigins(kDbFile); |
248 RegisterOrigins(FilePath()); | 378 RegisterOrigins(FilePath()); |
249 } | 379 } |
380 | |
381 TEST_F(QuotaDatabaseTest, DumpQuotaTable) { | |
382 ScopedTempDir data_dir; | |
383 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); | |
384 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); | |
385 DumpQuotaTable(kDbFile); | |
386 DumpQuotaTable(FilePath()); | |
387 } | |
388 | |
389 TEST_F(QuotaDatabaseTest, DumpLastAccessTimeTable) { | |
390 ScopedTempDir data_dir; | |
391 ASSERT_TRUE(data_dir.CreateUniqueTempDir()); | |
392 const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db"); | |
393 DumpLastAccessTimeTable(kDbFile); | |
394 DumpLastAccessTimeTable(FilePath()); | |
395 } | |
250 } // namespace quota | 396 } // namespace quota |
OLD | NEW |