This source file includes following definitions.
- CheckDetailsAreEqual
- DatabasesTableIsEmpty
- TEST
#include "base/bind.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "sql/connection.h"
#include "sql/statement.h"
#include "sql/test/scoped_error_ignorer.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/sqlite/sqlite3.h"
#include "webkit/browser/database/databases_table.h"
using base::ASCIIToUTF16;
namespace webkit_database {
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) {
sql::Connection db;
sql::ScopedErrorIgnorer ignore_errors;
ignore_errors.IgnoreError(SQLITE_CONSTRAINT);
EXPECT_TRUE(db.OpenInMemory());
DatabasesTable databases_table(&db);
EXPECT_TRUE(databases_table.Init());
EXPECT_TRUE(DatabasesTableIsEmpty(&db));
DatabaseDetails details_in1;
DatabaseDetails details_out1;
details_in1.origin_identifier = "origin1";
details_in1.database_name = ASCIIToUTF16("db1");
details_in1.description = ASCIIToUTF16("description_db1");
details_in1.estimated_size = 100;
EXPECT_FALSE(databases_table.UpdateDatabaseDetails(details_in1));
EXPECT_FALSE(databases_table.GetDatabaseDetails(
details_in1.origin_identifier,
details_in1.database_name,
&details_out1));
EXPECT_TRUE(databases_table.InsertDatabaseDetails(details_in1));
EXPECT_TRUE(databases_table.GetDatabaseDetails(
details_in1.origin_identifier,
details_in1.database_name,
&details_out1));
EXPECT_EQ(1, databases_table.GetDatabaseID(details_in1.origin_identifier,
details_in1.database_name));
CheckDetailsAreEqual(details_in1, details_out1);
EXPECT_FALSE(databases_table.InsertDatabaseDetails(details_in1));
DatabaseDetails details_in2;
details_in2.origin_identifier = "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));
EXPECT_EQ(2, databases_table.GetDatabaseID(details_in2.origin_identifier,
details_in2.database_name));
DatabaseDetails details_in3;
details_in3.origin_identifier = "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));
EXPECT_EQ(3, databases_table.GetDatabaseID(details_in3.origin_identifier,
details_in3.database_name));
std::vector<DatabaseDetails> details_out_origin3;
EXPECT_TRUE(databases_table.GetAllDatabaseDetailsForOriginIdentifier(
"origin3", &details_out_origin3));
EXPECT_TRUE(details_out_origin3.empty());
std::vector<DatabaseDetails> details_out_origin1;
EXPECT_TRUE(databases_table.GetAllDatabaseDetailsForOriginIdentifier(
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]);
std::vector<std::string> origins_out;
EXPECT_TRUE(databases_table.GetAllOriginIdentifiers(&origins_out));
EXPECT_EQ(size_t(2), origins_out.size());
EXPECT_EQ(details_in1.origin_identifier, origins_out[0]);
EXPECT_EQ(details_in3.origin_identifier, origins_out[1]);
origins_out.clear();
EXPECT_TRUE(databases_table.DeleteOriginIdentifier(
details_in3.origin_identifier));
EXPECT_TRUE(databases_table.GetAllOriginIdentifiers(&origins_out));
EXPECT_EQ(size_t(1), origins_out.size());
EXPECT_EQ(details_in1.origin_identifier, origins_out[0]);
EXPECT_FALSE(databases_table.DeleteOriginIdentifier("unknown_origin"));
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));
EXPECT_FALSE(databases_table.DeleteDatabaseDetails(
"unknown_origin", ASCIIToUTF16("unknown_database")));
ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
}
}