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
3 // found in the LICENSE file.
5 #include "base/string_util.h"
6 #include "base/utf_string_conversions.h"
7 #include "sql/connection.h"
8 #include "sql/statement.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "webkit/database/quota_table.h"
14 class TestErrorDelegate
: public sql::ErrorDelegate
{
16 virtual ~TestErrorDelegate() { }
18 int error
, sql::Connection
* connection
, sql::Statement
* stmt
) {
25 namespace webkit_database
{
27 static bool QuotaTableIsEmpty(sql::Connection
* db
) {
28 sql::Statement
statement(db
->GetCachedStatement(
29 SQL_FROM_HERE
, "SELECT COUNT(*) FROM Quota"));
30 return (statement
.is_valid() && statement
.Step() && !statement
.ColumnInt(0));
33 TEST(QuotaTableTest
, TestIt
) {
34 // Initialize the 'Quota' table.
37 // Set an error delegate that will make all operations return false on error.
38 scoped_refptr
<TestErrorDelegate
> error_delegate(new TestErrorDelegate());
39 db
.set_error_delegate(error_delegate
);
41 // Initialize the temp dir and the 'Databases' table.
42 EXPECT_TRUE(db
.OpenInMemory());
43 QuotaTable
quota_table(&db
);
44 EXPECT_TRUE(quota_table
.Init());
46 // The 'Quota' table should be empty.
47 EXPECT_TRUE(QuotaTableIsEmpty(&db
));
49 // Set and check the quota for a new origin.
50 string16 origin
= ASCIIToUTF16("origin");
51 EXPECT_TRUE(quota_table
.SetOriginQuota(origin
, 1000));
52 EXPECT_EQ(1000, quota_table
.GetOriginQuota(origin
));
54 // Reset and check the quota for the same origin.
55 EXPECT_TRUE(quota_table
.SetOriginQuota(origin
, 2000));
56 EXPECT_EQ(2000, quota_table
.GetOriginQuota(origin
));
58 // Clear the quota for an origin
59 EXPECT_TRUE(quota_table
.ClearOriginQuota(origin
));
60 EXPECT_TRUE(quota_table
.GetOriginQuota(origin
) < 0);
62 // Check that there's no quota set for unknown origins.
63 EXPECT_TRUE(quota_table
.GetOriginQuota(ASCIIToUTF16("unknown_origin")) < 0);
66 } // namespace webkit_database