1 // Copyright (c) 2012 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 TestErrorDelegate() {}
17 virtual ~TestErrorDelegate() {}
19 virtual int OnError(int error
,
20 sql::Connection
* connection
,
21 sql::Statement
* stmt
) OVERRIDE
{
26 DISALLOW_COPY_AND_ASSIGN(TestErrorDelegate
);
31 namespace webkit_database
{
33 static void CheckDetailsAreEqual(const DatabaseDetails
& d1
,
34 const DatabaseDetails
& d2
) {
35 EXPECT_EQ(d1
.origin_identifier
, d2
.origin_identifier
);
36 EXPECT_EQ(d1
.database_name
, d2
.database_name
);
37 EXPECT_EQ(d1
.description
, d2
.description
);
38 EXPECT_EQ(d1
.estimated_size
, d2
.estimated_size
);
41 static bool DatabasesTableIsEmpty(sql::Connection
* db
) {
42 sql::Statement
statement(db
->GetCachedStatement(
43 SQL_FROM_HERE
, "SELECT COUNT(*) FROM Databases"));
44 return (statement
.is_valid() && statement
.Step() && !statement
.ColumnInt(0));
47 TEST(DatabasesTableTest
, TestIt
) {
48 // Initialize the 'Databases' table.
51 // Set an error delegate that will make all operations return false on error.
52 db
.set_error_delegate(new TestErrorDelegate());
54 // Initialize the temp dir and the 'Databases' table.
55 EXPECT_TRUE(db
.OpenInMemory());
56 DatabasesTable
databases_table(&db
);
57 EXPECT_TRUE(databases_table
.Init());
59 // The 'Databases' table should be empty.
60 EXPECT_TRUE(DatabasesTableIsEmpty(&db
));
62 // Create the details for a databases.
63 DatabaseDetails details_in1
;
64 DatabaseDetails details_out1
;
65 details_in1
.origin_identifier
= ASCIIToUTF16("origin1");
66 details_in1
.database_name
= ASCIIToUTF16("db1");
67 details_in1
.description
= ASCIIToUTF16("description_db1");
68 details_in1
.estimated_size
= 100;
70 // Updating details for this database should fail.
71 EXPECT_FALSE(databases_table
.UpdateDatabaseDetails(details_in1
));
72 EXPECT_FALSE(databases_table
.GetDatabaseDetails(
73 details_in1
.origin_identifier
,
74 details_in1
.database_name
,
77 // Inserting details for this database should pass.
78 EXPECT_TRUE(databases_table
.InsertDatabaseDetails(details_in1
));
79 EXPECT_TRUE(databases_table
.GetDatabaseDetails(
80 details_in1
.origin_identifier
,
81 details_in1
.database_name
,
83 EXPECT_EQ(1, databases_table
.GetDatabaseID(details_in1
.origin_identifier
,
84 details_in1
.database_name
));
86 // Check that the details were correctly written to the database.
87 CheckDetailsAreEqual(details_in1
, details_out1
);
89 // Check that inserting a duplicate row fails.
90 EXPECT_FALSE(databases_table
.InsertDatabaseDetails(details_in1
));
92 // Insert details for another database with the same origin.
93 DatabaseDetails details_in2
;
94 details_in2
.origin_identifier
= ASCIIToUTF16("origin1");
95 details_in2
.database_name
= ASCIIToUTF16("db2");
96 details_in2
.description
= ASCIIToUTF16("description_db2");
97 details_in2
.estimated_size
= 200;
98 EXPECT_TRUE(databases_table
.InsertDatabaseDetails(details_in2
));
99 EXPECT_EQ(2, databases_table
.GetDatabaseID(details_in2
.origin_identifier
,
100 details_in2
.database_name
));
102 // Insert details for a third database with a different origin.
103 DatabaseDetails details_in3
;
104 details_in3
.origin_identifier
= ASCIIToUTF16("origin2");
105 details_in3
.database_name
= ASCIIToUTF16("db3");
106 details_in3
.description
= ASCIIToUTF16("description_db3");
107 details_in3
.estimated_size
= 300;
108 EXPECT_TRUE(databases_table
.InsertDatabaseDetails(details_in3
));
109 EXPECT_EQ(3, databases_table
.GetDatabaseID(details_in3
.origin_identifier
,
110 details_in3
.database_name
));
112 // There should be no database with origin "origin3".
113 std::vector
<DatabaseDetails
> details_out_origin3
;
114 EXPECT_TRUE(databases_table
.GetAllDatabaseDetailsForOrigin(
115 ASCIIToUTF16("origin3"), &details_out_origin3
));
116 EXPECT_TRUE(details_out_origin3
.empty());
118 // There should be only two databases with origin "origin1".
119 std::vector
<DatabaseDetails
> details_out_origin1
;
120 EXPECT_TRUE(databases_table
.GetAllDatabaseDetailsForOrigin(
121 details_in1
.origin_identifier
, &details_out_origin1
));
122 EXPECT_EQ(size_t(2), details_out_origin1
.size());
123 CheckDetailsAreEqual(details_in1
, details_out_origin1
[0]);
124 CheckDetailsAreEqual(details_in2
, details_out_origin1
[1]);
126 // Get the list of all origins: should be "origin1" and "origin2".
127 std::vector
<base::string16
> origins_out
;
128 EXPECT_TRUE(databases_table
.GetAllOrigins(&origins_out
));
129 EXPECT_EQ(size_t(2), origins_out
.size());
130 EXPECT_EQ(details_in1
.origin_identifier
, origins_out
[0]);
131 EXPECT_EQ(details_in3
.origin_identifier
, origins_out
[1]);
133 // Delete an origin and check that it's no longer in the table.
135 EXPECT_TRUE(databases_table
.DeleteOrigin(details_in3
.origin_identifier
));
136 EXPECT_TRUE(databases_table
.GetAllOrigins(&origins_out
));
137 EXPECT_EQ(size_t(1), origins_out
.size());
138 EXPECT_EQ(details_in1
.origin_identifier
, origins_out
[0]);
140 // Deleting an origin that doesn't have any record in this table should fail.
141 EXPECT_FALSE(databases_table
.DeleteOrigin(ASCIIToUTF16("unknown_origin")));
143 // Delete the details for 'db1' and check that they're no longer there.
144 EXPECT_TRUE(databases_table
.DeleteDatabaseDetails(
145 details_in1
.origin_identifier
, details_in1
.database_name
));
146 EXPECT_FALSE(databases_table
.GetDatabaseDetails(
147 details_in1
.origin_identifier
,
148 details_in1
.database_name
,
151 // Check that trying to delete a record that doesn't exist fails.
152 EXPECT_FALSE(databases_table
.DeleteDatabaseDetails(
153 ASCIIToUTF16("unknown_origin"), ASCIIToUTF16("unknown_database")));
156 } // namespace webkit_database