diff options
author | dumi@chromium.org <dumi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-02 21:41:56 +0000 |
---|---|---|
committer | dumi@chromium.org <dumi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-02 21:41:56 +0000 |
commit | 615dedfcdefe5fd2d5ea3c97aea4f2669e0d6657 (patch) | |
tree | 02038c0c0863cc0ccb766b4c9f120e039b0d8f0e /webkit/database/databases_table_unittest.cc | |
parent | 9f8b6045dedbb3189aec7910f23d1c8fa59a7641 (diff) | |
download | chromium_src-615dedfcdefe5fd2d5ea3c97aea4f2669e0d6657.zip chromium_src-615dedfcdefe5fd2d5ea3c97aea4f2669e0d6657.tar.gz chromium_src-615dedfcdefe5fd2d5ea3c97aea4f2669e0d6657.tar.bz2 |
Adding Chromium's database tracker.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/334039
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@30747 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/database/databases_table_unittest.cc')
-rw-r--r-- | webkit/database/databases_table_unittest.cc | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/webkit/database/databases_table_unittest.cc b/webkit/database/databases_table_unittest.cc new file mode 100644 index 0000000..5012cc5 --- /dev/null +++ b/webkit/database/databases_table_unittest.cc @@ -0,0 +1,126 @@ +// Copyright (c) 2009 The Chromium Authos. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "app/sql/connection.h" +#include "app/sql/statement.h" +#include "base/scoped_temp_dir.h" +#include "base/string_util.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "webkit/database/databases_table.h" + +namespace webkit_database { + +class TestErrorDelegate : public sql::ErrorDelegate { + public: + virtual ~TestErrorDelegate() { } + virtual int OnError( + int error, sql::Connection* connection, sql::Statement* stmt) { + return error; + } +}; + +static void CheckDetailsAreEqual(const DatabaseDetails& d1, + const DatabaseDetails& d2) { + EXPECT_EQ(d1.origin_identifier, d2.origin_identifier); + EXPECT_EQ(d1.database_name, d2.database_name); + EXPECT_EQ(d1.description, d2.description); + EXPECT_EQ(d1.estimated_size, d2.estimated_size); +} + +static bool DatabasesTableIsEmpty(sql::Connection* db) { + sql::Statement statement(db->GetCachedStatement( + SQL_FROM_HERE, "SELECT COUNT(*) FROM Databases")); + return (statement.is_valid() && statement.Step() && !statement.ColumnInt(0)); +} + +TEST(DatabasesTableTest, TestIt) { + // Initialize the 'Databases' table. + ScopedTempDir temp_dir; + sql::Connection db; + + // Set an error delegate that will make all operations return false on error. + scoped_refptr<TestErrorDelegate> error_delegate(new TestErrorDelegate()); + db.set_error_delegate(error_delegate); + + // Initialize the temp dir and the 'Databases' table. + EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); + EXPECT_TRUE(db.Open(temp_dir.path().Append(FILE_PATH_LITERAL("tracker.db")))); + DatabasesTable databases_table(&db); + EXPECT_TRUE(databases_table.Init()); + + // The 'Databases' table should be empty. + EXPECT_TRUE(DatabasesTableIsEmpty(&db)); + + // Create the details for a databases. + DatabaseDetails details_in1; + DatabaseDetails details_out1; + details_in1.origin_identifier = ASCIIToUTF16("origin1"); + details_in1.database_name = ASCIIToUTF16("db1"); + details_in1.description = ASCIIToUTF16("description_db1"); + details_in1.estimated_size = 100; + + // Updating details for this database should fail. + EXPECT_FALSE(databases_table.UpdateDatabaseDetails(details_in1)); + EXPECT_FALSE(databases_table.GetDatabaseDetails( + details_in1.origin_identifier, + details_in1.database_name, + &details_out1)); + + // Inserting details for this database should pass. + EXPECT_TRUE(databases_table.InsertDatabaseDetails(details_in1)); + EXPECT_TRUE(databases_table.GetDatabaseDetails( + details_in1.origin_identifier, + details_in1.database_name, + &details_out1)); + + // Check that the details were correctly written to the database. + CheckDetailsAreEqual(details_in1, details_out1); + + // Check that inserting a duplicate row fails. + EXPECT_FALSE(databases_table.InsertDatabaseDetails(details_in1)); + + // Insert details for another database with the same origin. + DatabaseDetails details_in2; + details_in2.origin_identifier = ASCIIToUTF16("origin1"); + details_in2.database_name = ASCIIToUTF16("db2"); + details_in2.description = ASCIIToUTF16("description_db2"); + details_in2.estimated_size = 200; + EXPECT_TRUE(databases_table.InsertDatabaseDetails(details_in2)); + + // Insert details for a third database with a different origin. + DatabaseDetails details_in3; + details_in3.origin_identifier = ASCIIToUTF16("origin2"); + details_in3.database_name = ASCIIToUTF16("db3"); + details_in3.description = ASCIIToUTF16("description_db3"); + details_in3.estimated_size = 300; + EXPECT_TRUE(databases_table.InsertDatabaseDetails(details_in3)); + + // There should be no database with origin "origin3". + std::vector<DatabaseDetails> details_out_origin3; + EXPECT_TRUE(databases_table.GetAllDatabaseDetailsForOrigin( + ASCIIToUTF16("origin3"), &details_out_origin3)); + EXPECT_TRUE(details_out_origin3.empty()); + + // There should be only two databases with origin "origin1". + std::vector<DatabaseDetails> details_out_origin1; + EXPECT_TRUE(databases_table.GetAllDatabaseDetailsForOrigin( + details_in1.origin_identifier, &details_out_origin1)); + EXPECT_EQ(size_t(2), details_out_origin1.size()); + CheckDetailsAreEqual(details_in1, details_out_origin1[0]); + CheckDetailsAreEqual(details_in2, details_out_origin1[1]); + + // Delete the details for 'db1' and check that they're no longer there. + EXPECT_TRUE(databases_table.DeleteDatabaseDetails( + details_in1.origin_identifier, details_in1.database_name)); + EXPECT_FALSE(databases_table.GetDatabaseDetails( + details_in1.origin_identifier, + details_in1.database_name, + &details_out1)); + + // Check that trying to delete a record that doesn't exist fails. + EXPECT_FALSE(databases_table.DeleteDatabaseDetails( + ASCIIToUTF16("unknown_origin"), ASCIIToUTF16("unknown_database"))); +} + +} // namespace webkit_database |