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/databases_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 void CheckDetailsAreEqual(const DatabaseDetails
& d1
,
28 const DatabaseDetails
& d2
) {
29 EXPECT_EQ(d1
.origin_identifier
, d2
.origin_identifier
);
30 EXPECT_EQ(d1
.database_name
, d2
.database_name
);
31 EXPECT_EQ(d1
.description
, d2
.description
);
32 EXPECT_EQ(d1
.estimated_size
, d2
.estimated_size
);
35 static bool DatabasesTableIsEmpty(sql::Connection
* db
) {
36 sql::Statement
statement(db
->GetCachedStatement(
37 SQL_FROM_HERE
, "SELECT COUNT(*) FROM Databases"));
38 return (statement
.is_valid() && statement
.Step() && !statement
.ColumnInt(0));
41 TEST(DatabasesTableTest
, TestIt
) {
42 // Initialize the 'Databases' table.
45 // Set an error delegate that will make all operations return false on error.
46 scoped_refptr
<TestErrorDelegate
> error_delegate(new TestErrorDelegate());
47 db
.set_error_delegate(error_delegate
);
49 // Initialize the temp dir and the 'Databases' table.
50 EXPECT_TRUE(db
.OpenInMemory());
51 DatabasesTable
databases_table(&db
);
52 EXPECT_TRUE(databases_table
.Init());
54 // The 'Databases' table should be empty.
55 EXPECT_TRUE(DatabasesTableIsEmpty(&db
));
57 // Create the details for a databases.
58 DatabaseDetails details_in1
;
59 DatabaseDetails details_out1
;
60 details_in1
.origin_identifier
= ASCIIToUTF16("origin1");
61 details_in1
.database_name
= ASCIIToUTF16("db1");
62 details_in1
.description
= ASCIIToUTF16("description_db1");
63 details_in1
.estimated_size
= 100;
65 // Updating details for this database should fail.
66 EXPECT_FALSE(databases_table
.UpdateDatabaseDetails(details_in1
));
67 EXPECT_FALSE(databases_table
.GetDatabaseDetails(
68 details_in1
.origin_identifier
,
69 details_in1
.database_name
,
72 // Inserting details for this database should pass.
73 EXPECT_TRUE(databases_table
.InsertDatabaseDetails(details_in1
));
74 EXPECT_TRUE(databases_table
.GetDatabaseDetails(
75 details_in1
.origin_identifier
,
76 details_in1
.database_name
,
78 EXPECT_EQ(1, databases_table
.GetDatabaseID(details_in1
.origin_identifier
,
79 details_in1
.database_name
));
81 // Check that the details were correctly written to the database.
82 CheckDetailsAreEqual(details_in1
, details_out1
);
84 // Check that inserting a duplicate row fails.
85 EXPECT_FALSE(databases_table
.InsertDatabaseDetails(details_in1
));
87 // Insert details for another database with the same origin.
88 DatabaseDetails details_in2
;
89 details_in2
.origin_identifier
= ASCIIToUTF16("origin1");
90 details_in2
.database_name
= ASCIIToUTF16("db2");
91 details_in2
.description
= ASCIIToUTF16("description_db2");
92 details_in2
.estimated_size
= 200;
93 EXPECT_TRUE(databases_table
.InsertDatabaseDetails(details_in2
));
94 EXPECT_EQ(2, databases_table
.GetDatabaseID(details_in2
.origin_identifier
,
95 details_in2
.database_name
));
97 // Insert details for a third database with a different origin.
98 DatabaseDetails details_in3
;
99 details_in3
.origin_identifier
= ASCIIToUTF16("origin2");
100 details_in3
.database_name
= ASCIIToUTF16("db3");
101 details_in3
.description
= ASCIIToUTF16("description_db3");
102 details_in3
.estimated_size
= 300;
103 EXPECT_TRUE(databases_table
.InsertDatabaseDetails(details_in3
));
104 EXPECT_EQ(3, databases_table
.GetDatabaseID(details_in3
.origin_identifier
,
105 details_in3
.database_name
));
107 // There should be no database with origin "origin3".
108 std::vector
<DatabaseDetails
> details_out_origin3
;
109 EXPECT_TRUE(databases_table
.GetAllDatabaseDetailsForOrigin(
110 ASCIIToUTF16("origin3"), &details_out_origin3
));
111 EXPECT_TRUE(details_out_origin3
.empty());
113 // There should be only two databases with origin "origin1".
114 std::vector
<DatabaseDetails
> details_out_origin1
;
115 EXPECT_TRUE(databases_table
.GetAllDatabaseDetailsForOrigin(
116 details_in1
.origin_identifier
, &details_out_origin1
));
117 EXPECT_EQ(size_t(2), details_out_origin1
.size());
118 CheckDetailsAreEqual(details_in1
, details_out_origin1
[0]);
119 CheckDetailsAreEqual(details_in2
, details_out_origin1
[1]);
121 // Get the list of all origins: should be "origin1" and "origin2".
122 std::vector
<string16
> origins_out
;
123 EXPECT_TRUE(databases_table
.GetAllOrigins(&origins_out
));
124 EXPECT_EQ(size_t(2), origins_out
.size());
125 EXPECT_EQ(details_in1
.origin_identifier
, origins_out
[0]);
126 EXPECT_EQ(details_in3
.origin_identifier
, origins_out
[1]);
128 // Delete an origin and check that it's no longer in the table.
130 EXPECT_TRUE(databases_table
.DeleteOrigin(details_in3
.origin_identifier
));
131 EXPECT_TRUE(databases_table
.GetAllOrigins(&origins_out
));
132 EXPECT_EQ(size_t(1), origins_out
.size());
133 EXPECT_EQ(details_in1
.origin_identifier
, origins_out
[0]);
135 // Deleting an origin that doesn't have any record in this table should fail.
136 EXPECT_FALSE(databases_table
.DeleteOrigin(ASCIIToUTF16("unknown_origin")));
138 // Delete the details for 'db1' and check that they're no longer there.
139 EXPECT_TRUE(databases_table
.DeleteDatabaseDetails(
140 details_in1
.origin_identifier
, details_in1
.database_name
));
141 EXPECT_FALSE(databases_table
.GetDatabaseDetails(
142 details_in1
.origin_identifier
,
143 details_in1
.database_name
,
146 // Check that trying to delete a record that doesn't exist fails.
147 EXPECT_FALSE(databases_table
.DeleteDatabaseDetails(
148 ASCIIToUTF16("unknown_origin"), ASCIIToUTF16("unknown_database")));
151 } // namespace webkit_database