summaryrefslogtreecommitdiffstats
path: root/webkit/quota/quota_database_unittest.cc
diff options
context:
space:
mode:
authorkinuko@chromium.org <kinuko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-03-17 07:46:00 +0000
committerkinuko@chromium.org <kinuko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-03-17 07:46:00 +0000
commit42b1020f82ad53a0d0c8591e977360e8fee3415c (patch)
tree0ba1fa306810790507fb065cb4c43c215de5812e /webkit/quota/quota_database_unittest.cc
parenta604854af9ff6f729ada6d8e5bc6f53147b0f3e4 (diff)
downloadchromium_src-42b1020f82ad53a0d0c8591e977360e8fee3415c.zip
chromium_src-42b1020f82ad53a0d0c8591e977360e8fee3415c.tar.gz
chromium_src-42b1020f82ad53a0d0c8591e977360e8fee3415c.tar.bz2
Implement quota manager database.
BUG=61676 TEST=QuotaDatabaseTest.* Review URL: http://codereview.chromium.org/6627003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@78521 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/quota/quota_database_unittest.cc')
-rw-r--r--webkit/quota/quota_database_unittest.cc161
1 files changed, 161 insertions, 0 deletions
diff --git a/webkit/quota/quota_database_unittest.cc b/webkit/quota/quota_database_unittest.cc
new file mode 100644
index 0000000..836ee16
--- /dev/null
+++ b/webkit/quota/quota_database_unittest.cc
@@ -0,0 +1,161 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "testing/gtest/include/gtest/gtest.h"
+
+#include "app/sql/connection.h"
+#include "base/file_util.h"
+#include "base/scoped_temp_dir.h"
+#include "googleurl/src/gurl.h"
+#include "webkit/quota/quota_database.h"
+
+namespace {
+
+const base::Time kZeroTime;
+
+class TestErrorDelegate : public sql::ErrorDelegate {
+ public:
+ virtual ~TestErrorDelegate() { }
+ virtual int OnError(
+ int error, sql::Connection* connection, sql::Statement* stmt) {
+ return error;
+ }
+};
+
+} // namespace
+
+namespace quota {
+
+TEST(QuotaDatabaseTest, LazyOpen) {
+ ScopedTempDir data_dir;
+ ASSERT_TRUE(data_dir.CreateUniqueTempDir());
+
+ const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db");
+ QuotaDatabase db(kDbFile);
+ EXPECT_FALSE(db.LazyOpen(false));
+ ASSERT_TRUE(db.LazyOpen(true));
+ EXPECT_TRUE(file_util::PathExists(kDbFile));
+}
+
+TEST(QuotaDatabaseTest, OriginQuota) {
+ ScopedTempDir data_dir;
+ ASSERT_TRUE(data_dir.CreateUniqueTempDir());
+
+ const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db");
+ QuotaDatabase db(kDbFile);
+ ASSERT_TRUE(db.LazyOpen(true));
+
+ const GURL kOrigin("http://foo.com:8080/");
+ const int kQuota1 = 13579;
+ const int kQuota2 = kQuota1 + 1024;
+
+ int64 quota = -1;
+ EXPECT_FALSE(db.GetOriginQuota(kOrigin, kStorageTypeTemporary, &quota));
+ EXPECT_FALSE(db.GetOriginQuota(kOrigin, kStorageTypePersistent, &quota));
+
+ // Insert quota for temporary.
+ EXPECT_TRUE(db.SetOriginQuota(kOrigin, kStorageTypeTemporary, kQuota1));
+ EXPECT_TRUE(db.GetOriginQuota(kOrigin, kStorageTypeTemporary, &quota));
+ EXPECT_EQ(kQuota1, quota);
+
+ // Update quota for temporary.
+ EXPECT_TRUE(db.SetOriginQuota(kOrigin, kStorageTypeTemporary, kQuota2));
+ EXPECT_TRUE(db.GetOriginQuota(kOrigin, kStorageTypeTemporary, &quota));
+ EXPECT_EQ(kQuota2, quota);
+
+ // Quota for persistent must not be updated.
+ EXPECT_FALSE(db.GetOriginQuota(kOrigin, kStorageTypePersistent, &quota));
+
+ // Delete temporary storage info.
+ EXPECT_TRUE(db.DeleteStorageInfo(kOrigin, kStorageTypeTemporary));
+ EXPECT_FALSE(db.GetOriginQuota(kOrigin, kStorageTypeTemporary, &quota));
+}
+
+TEST(QuotaDatabaseTest, GlobalQuota) {
+ ScopedTempDir data_dir;
+ ASSERT_TRUE(data_dir.CreateUniqueTempDir());
+
+ const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db");
+ QuotaDatabase db(kDbFile);
+ ASSERT_TRUE(db.LazyOpen(true));
+
+ const int kQuota1 = 9999;
+ const int kQuota2 = 86420;
+
+ int64 quota = -1;
+ EXPECT_FALSE(db.GetGlobalQuota(kStorageTypeTemporary, &quota));
+ EXPECT_FALSE(db.GetGlobalQuota(kStorageTypePersistent, &quota));
+
+ EXPECT_TRUE(db.SetGlobalQuota(kStorageTypeTemporary, kQuota1));
+ EXPECT_TRUE(db.GetGlobalQuota(kStorageTypeTemporary, &quota));
+ EXPECT_EQ(kQuota1, quota);
+
+ EXPECT_TRUE(db.SetGlobalQuota(kStorageTypeTemporary, kQuota1 + 1024));
+ EXPECT_TRUE(db.GetGlobalQuota(kStorageTypeTemporary, &quota));
+ EXPECT_EQ(kQuota1 + 1024, quota);
+
+ EXPECT_FALSE(db.GetGlobalQuota(kStorageTypePersistent, &quota));
+
+ EXPECT_TRUE(db.SetGlobalQuota(kStorageTypePersistent, kQuota2));
+ EXPECT_TRUE(db.GetGlobalQuota(kStorageTypePersistent, &quota));
+ EXPECT_EQ(kQuota2, quota);
+}
+
+TEST(QuotaDatabaseTest, OriginLastAccessTimeLRU) {
+ ScopedTempDir data_dir;
+ ASSERT_TRUE(data_dir.CreateUniqueTempDir());
+
+ const FilePath kDbFile = data_dir.path().AppendASCII("quota_manager.db");
+ QuotaDatabase db(kDbFile);
+ ASSERT_TRUE(db.LazyOpen(true));
+
+ std::vector<GURL> origins;
+ EXPECT_TRUE(db.GetLRUOrigins(kStorageTypeTemporary, &origins, -1, 10));
+ EXPECT_EQ(0U, origins.size());
+
+ const GURL kOrigin1("http://a/");
+ const GURL kOrigin2("http://b/");
+ const GURL kOrigin3("http://c/");
+ const GURL kOrigin4("http://p/");
+
+ // Adding three temporary storages, and
+ EXPECT_TRUE(db.SetOriginLastAccessTime(
+ kOrigin1, kStorageTypeTemporary, base::Time::FromInternalValue(10)));
+ EXPECT_TRUE(db.SetOriginLastAccessTime(
+ kOrigin2, kStorageTypeTemporary, base::Time::FromInternalValue(20)));
+ EXPECT_TRUE(db.SetOriginLastAccessTime(
+ kOrigin3, kStorageTypeTemporary, base::Time::FromInternalValue(30)));
+
+ // one persistent.
+ EXPECT_TRUE(db.SetOriginLastAccessTime(
+ kOrigin4, kStorageTypePersistent, base::Time::FromInternalValue(40)));
+
+ EXPECT_TRUE(db.GetLRUOrigins(kStorageTypeTemporary, &origins, 0, 10));
+
+ ASSERT_EQ(3U, origins.size());
+ EXPECT_EQ(kOrigin1.spec(), origins[0].spec());
+ EXPECT_EQ(kOrigin2.spec(), origins[1].spec());
+ EXPECT_EQ(kOrigin3.spec(), origins[2].spec());
+
+ EXPECT_TRUE(db.SetOriginLastAccessTime(
+ kOrigin1, kStorageTypeTemporary, base::Time::Now()));
+
+ EXPECT_TRUE(db.GetLRUOrigins(kStorageTypeTemporary, &origins, 0, 10));
+
+ // Now kOrigin1 has used_count=1, so it should not be in the returned list.
+ ASSERT_EQ(2U, origins.size());
+ EXPECT_EQ(kOrigin2.spec(), origins[0].spec());
+ EXPECT_EQ(kOrigin3.spec(), origins[1].spec());
+
+ // Query again without used_count condition.
+ EXPECT_TRUE(db.GetLRUOrigins(kStorageTypeTemporary, &origins, -1, 10));
+
+ // Now kOrigin1 must be returned as the newest one.
+ ASSERT_EQ(3U, origins.size());
+ EXPECT_EQ(kOrigin2.spec(), origins[0].spec());
+ EXPECT_EQ(kOrigin3.spec(), origins[1].spec());
+ EXPECT_EQ(kOrigin1.spec(), origins[2].spec());
+}
+
+} // namespace quota