This source file includes following definitions.
- CreateDatabaseFromSQL
- VerifyTablesAndColumns
- VerifyDatabaseEmpty
- SetUp
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include <map>
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/path_service.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/history/history_types.h"
#include "chrome/browser/history/top_sites_database.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/tools/profiles/thumbnail-inl.h"
#include "sql/connection.h"
#include "sql/recovery.h"
#include "sql/test/scoped_error_ignorer.h"
#include "sql/test/test_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/sqlite/sqlite3.h"
#include "url/gurl.h"
namespace {
const GURL kUrl0 = GURL("http://www.google.com/");
const GURL kUrl1 = GURL("http://www.google.com/chrome/intl/en/welcome.html");
const GURL kUrl2 = GURL("https://chrome.google.com/webstore?hl=en");
WARN_UNUSED_RESULT bool CreateDatabaseFromSQL(const base::FilePath &db_path,
const char* ascii_path) {
base::FilePath sql_path;
if (!PathService::Get(chrome::DIR_TEST_DATA, &sql_path))
return false;
sql_path = sql_path.AppendASCII("History").AppendASCII(ascii_path);
return sql::test::CreateDatabaseFromSQL(db_path, sql_path);
}
void VerifyTablesAndColumns(sql::Connection* db) {
EXPECT_EQ(2u, sql::test::CountSQLTables(db));
EXPECT_EQ(2u, sql::test::CountSQLIndices(db));
EXPECT_EQ(2u, sql::test::CountTableColumns(db, "meta"));
EXPECT_EQ(11u, sql::test::CountTableColumns(db, "thumbnails"));
}
void VerifyDatabaseEmpty(sql::Connection* db) {
size_t rows = 0;
EXPECT_TRUE(sql::test::CountTableRows(db, "thumbnails", &rows));
EXPECT_EQ(0u, rows);
}
}
namespace history {
class TopSitesDatabaseTest : public testing::Test {
protected:
virtual void SetUp() {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
file_name_ = temp_dir_.path().AppendASCII("TestTopSites.db");
}
base::ScopedTempDir temp_dir_;
base::FilePath file_name_;
};
TEST_F(TopSitesDatabaseTest, Version1) {
ASSERT_TRUE(CreateDatabaseFromSQL(file_name_, "TopSites.v1.sql"));
TopSitesDatabase db;
ASSERT_TRUE(db.Init(file_name_));
VerifyTablesAndColumns(db.db_.get());
VerifyDatabaseEmpty(db.db_.get());
}
TEST_F(TopSitesDatabaseTest, Version2) {
ASSERT_TRUE(CreateDatabaseFromSQL(file_name_, "TopSites.v2.sql"));
TopSitesDatabase db;
ASSERT_TRUE(db.Init(file_name_));
VerifyTablesAndColumns(db.db_.get());
MostVisitedURLList urls;
std::map<GURL, Images> thumbnails;
db.GetPageThumbnails(&urls, &thumbnails);
ASSERT_EQ(3u, urls.size());
ASSERT_EQ(3u, thumbnails.size());
EXPECT_EQ(kUrl0, urls[0].url);
ASSERT_EQ(sizeof(kGoogleThumbnail) - 1,
thumbnails[urls[0].url].thumbnail->size());
EXPECT_TRUE(!memcmp(thumbnails[urls[0].url].thumbnail->front(),
kGoogleThumbnail, sizeof(kGoogleThumbnail) - 1));
ASSERT_TRUE(db.RemoveURL(urls[1]));
db.GetPageThumbnails(&urls, &thumbnails);
ASSERT_EQ(2u, urls.size());
ASSERT_EQ(2u, thumbnails.size());
}
TEST_F(TopSitesDatabaseTest, Version3) {
ASSERT_TRUE(CreateDatabaseFromSQL(file_name_, "TopSites.v3.sql"));
TopSitesDatabase db;
ASSERT_TRUE(db.Init(file_name_));
VerifyTablesAndColumns(db.db_.get());
MostVisitedURLList urls;
std::map<GURL, Images> thumbnails;
db.GetPageThumbnails(&urls, &thumbnails);
ASSERT_EQ(3u, urls.size());
ASSERT_EQ(3u, thumbnails.size());
EXPECT_EQ(kUrl0, urls[0].url);
ASSERT_EQ(sizeof(kGoogleThumbnail) - 1,
thumbnails[urls[0].url].thumbnail->size());
EXPECT_TRUE(!memcmp(thumbnails[urls[0].url].thumbnail->front(),
kGoogleThumbnail, sizeof(kGoogleThumbnail) - 1));
ASSERT_TRUE(db.RemoveURL(urls[1]));
db.GetPageThumbnails(&urls, &thumbnails);
ASSERT_EQ(2u, urls.size());
ASSERT_EQ(2u, thumbnails.size());
}
TEST_F(TopSitesDatabaseTest, Recovery1) {
if (!sql::Recovery::FullRecoverySupported())
return;
EXPECT_TRUE(CreateDatabaseFromSQL(file_name_, "TopSites.v1.sql"));
EXPECT_TRUE(sql::test::CorruptSizeInHeader(file_name_));
{
sql::ScopedErrorIgnorer ignore_errors;
ignore_errors.IgnoreError(SQLITE_CORRUPT);
sql::Connection raw_db;
EXPECT_TRUE(raw_db.Open(file_name_));
EXPECT_FALSE(raw_db.IsSQLValid("PRAGMA integrity_check"));
ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
}
{
sql::ScopedErrorIgnorer ignore_errors;
ignore_errors.IgnoreError(SQLITE_CORRUPT);
TopSitesDatabase db;
ASSERT_TRUE(db.Init(file_name_));
VerifyTablesAndColumns(db.db_.get());
VerifyDatabaseEmpty(db.db_.get());
ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
}
}
TEST_F(TopSitesDatabaseTest, Recovery2) {
if (!sql::Recovery::FullRecoverySupported())
return;
EXPECT_TRUE(CreateDatabaseFromSQL(file_name_, "TopSites.v2.sql"));
EXPECT_TRUE(sql::test::CorruptSizeInHeader(file_name_));
{
sql::ScopedErrorIgnorer ignore_errors;
ignore_errors.IgnoreError(SQLITE_CORRUPT);
sql::Connection raw_db;
EXPECT_TRUE(raw_db.Open(file_name_));
EXPECT_FALSE(raw_db.IsSQLValid("PRAGMA integrity_check"));
ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
}
{
sql::ScopedErrorIgnorer ignore_errors;
ignore_errors.IgnoreError(SQLITE_CORRUPT);
TopSitesDatabase db;
ASSERT_TRUE(db.Init(file_name_));
VerifyTablesAndColumns(db.db_.get());
MostVisitedURLList urls;
std::map<GURL, Images> thumbnails;
db.GetPageThumbnails(&urls, &thumbnails);
ASSERT_EQ(3u, urls.size());
ASSERT_EQ(3u, thumbnails.size());
EXPECT_EQ(kUrl0, urls[0].url);
ASSERT_EQ(sizeof(kGoogleThumbnail) - 1,
thumbnails[urls[0].url].thumbnail->size());
EXPECT_TRUE(!memcmp(thumbnails[urls[0].url].thumbnail->front(),
kGoogleThumbnail, sizeof(kGoogleThumbnail) - 1));
ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
}
}
TEST_F(TopSitesDatabaseTest, Recovery3) {
if (!sql::Recovery::FullRecoverySupported())
return;
EXPECT_TRUE(CreateDatabaseFromSQL(file_name_, "TopSites.v3.sql"));
EXPECT_TRUE(sql::test::CorruptSizeInHeader(file_name_));
{
sql::ScopedErrorIgnorer ignore_errors;
ignore_errors.IgnoreError(SQLITE_CORRUPT);
sql::Connection raw_db;
EXPECT_TRUE(raw_db.Open(file_name_));
EXPECT_FALSE(raw_db.IsSQLValid("PRAGMA integrity_check"));
ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
}
{
sql::ScopedErrorIgnorer ignore_errors;
ignore_errors.IgnoreError(SQLITE_CORRUPT);
TopSitesDatabase db;
ASSERT_TRUE(db.Init(file_name_));
MostVisitedURLList urls;
std::map<GURL, Images> thumbnails;
db.GetPageThumbnails(&urls, &thumbnails);
ASSERT_EQ(3u, urls.size());
ASSERT_EQ(3u, thumbnails.size());
EXPECT_EQ(kUrl0, urls[0].url);
ASSERT_EQ(sizeof(kGoogleThumbnail) - 1,
thumbnails[urls[0].url].thumbnail->size());
EXPECT_TRUE(!memcmp(thumbnails[urls[0].url].thumbnail->front(),
kGoogleThumbnail, sizeof(kGoogleThumbnail) - 1));
ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
}
{
sql::Connection raw_db;
EXPECT_TRUE(raw_db.Open(file_name_));
ASSERT_EQ("ok", sql::test::IntegrityCheck(&raw_db));
}
const char kIndexName[] = "sqlite_autoindex_thumbnails_1";
const char kDeleteSql[] =
"DELETE FROM thumbnails WHERE url = "
"'http://www.google.com/chrome/intl/en/welcome.html'";
EXPECT_TRUE(
sql::test::CorruptTableOrIndex(file_name_, kIndexName, kDeleteSql));
{
sql::Connection raw_db;
EXPECT_TRUE(raw_db.Open(file_name_));
ASSERT_NE("ok", sql::test::IntegrityCheck(&raw_db));
}
{
TopSitesDatabase db;
ASSERT_TRUE(db.Init(file_name_));
{
sql::ScopedErrorIgnorer ignore_errors;
ignore_errors.IgnoreError(SQLITE_CORRUPT);
EXPECT_EQ(TopSitesDatabase::kRankOfNonExistingURL,
db.GetURLRank(MostVisitedURL(kUrl1, base::string16())));
ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
}
}
{
sql::Connection raw_db;
EXPECT_TRUE(raw_db.Open(file_name_));
ASSERT_EQ("ok", sql::test::IntegrityCheck(&raw_db));
}
{
TopSitesDatabase db;
ASSERT_TRUE(db.Init(file_name_));
VerifyTablesAndColumns(db.db_.get());
EXPECT_EQ(TopSitesDatabase::kRankOfNonExistingURL,
db.GetURLRank(MostVisitedURL(kUrl1, base::string16())));
MostVisitedURLList urls;
std::map<GURL, Images> thumbnails;
db.GetPageThumbnails(&urls, &thumbnails);
ASSERT_EQ(2u, urls.size());
ASSERT_EQ(2u, thumbnails.size());
EXPECT_EQ(kUrl0, urls[0].url);
EXPECT_EQ(kUrl2, urls[1].url);
}
}
TEST_F(TopSitesDatabaseTest, AddRemoveEditThumbnails) {
ASSERT_TRUE(CreateDatabaseFromSQL(file_name_, "TopSites.v3.sql"));
TopSitesDatabase db;
ASSERT_TRUE(db.Init(file_name_));
GURL mapsUrl = GURL("http://maps.google.com/");
MostVisitedURL url1(mapsUrl, base::ASCIIToUTF16("Google Maps"));
db.SetPageThumbnail(url1, 1, Images());
MostVisitedURLList urls;
std::map<GURL, Images> thumbnails;
db.GetPageThumbnails(&urls, &thumbnails);
ASSERT_EQ(4u, urls.size());
ASSERT_EQ(4u, thumbnails.size());
EXPECT_EQ(kUrl0, urls[0].url);
EXPECT_EQ(mapsUrl, urls[1].url);
GURL driveUrl = GURL("http://drive.google.com/");
MostVisitedURL url2(driveUrl, base::ASCIIToUTF16("Google Drive"));
url2.last_forced_time = base::Time::FromJsTime(789714000000);
db.SetPageThumbnail(url2, TopSitesDatabase::kRankOfForcedURL, Images());
db.GetPageThumbnails(&urls, &thumbnails);
ASSERT_EQ(5u, urls.size());
ASSERT_EQ(5u, thumbnails.size());
EXPECT_EQ(driveUrl, urls[0].url);
EXPECT_EQ(kUrl0, urls[1].url);
EXPECT_EQ(mapsUrl, urls[2].url);
GURL plusUrl = GURL("http://plus.google.com/");
MostVisitedURL url3(plusUrl, base::ASCIIToUTF16("Google Plus"));
url3.last_forced_time = base::Time::FromJsTime(787035600000);
db.SetPageThumbnail(url3, TopSitesDatabase::kRankOfForcedURL, Images());
db.GetPageThumbnails(&urls, &thumbnails);
ASSERT_EQ(6u, urls.size());
ASSERT_EQ(6u, thumbnails.size());
EXPECT_EQ(plusUrl, urls[0].url);
EXPECT_EQ(driveUrl, urls[1].url);
EXPECT_EQ(kUrl0, urls[2].url);
EXPECT_EQ(mapsUrl, urls[3].url);
url3.last_forced_time = base::Time::FromJsTime(792392400000);
db.SetPageThumbnail(url3, TopSitesDatabase::kRankOfForcedURL, Images());
db.GetPageThumbnails(&urls, &thumbnails);
ASSERT_EQ(6u, urls.size());
ASSERT_EQ(6u, thumbnails.size());
EXPECT_EQ(driveUrl, urls[0].url);
EXPECT_EQ(plusUrl, urls[1].url);
EXPECT_EQ(kUrl0, urls[2].url);
EXPECT_EQ(mapsUrl, urls[3].url);
url1.last_forced_time = base::Time::FromJsTime(792219600000);
db.UpdatePageRank(url1, TopSitesDatabase::kRankOfForcedURL);
db.GetPageThumbnails(&urls, &thumbnails);
ASSERT_EQ(6u, urls.size());
ASSERT_EQ(6u, thumbnails.size());
EXPECT_EQ(driveUrl, urls[0].url);
EXPECT_EQ(mapsUrl, urls[1].url);
EXPECT_EQ(plusUrl, urls[2].url);
EXPECT_EQ(kUrl0, urls[3].url);
url3.last_forced_time = base::Time();
db.SetPageThumbnail(url3, 1, Images());
db.GetPageThumbnails(&urls, &thumbnails);
ASSERT_EQ(6u, urls.size());
ASSERT_EQ(6u, thumbnails.size());
EXPECT_EQ(driveUrl, urls[0].url);
EXPECT_EQ(mapsUrl, urls[1].url);
EXPECT_EQ(kUrl0, urls[2].url);
EXPECT_EQ(plusUrl, urls[3].url);
db.UpdatePageRank(url3, 0);
db.GetPageThumbnails(&urls, &thumbnails);
ASSERT_EQ(6u, urls.size());
ASSERT_EQ(6u, thumbnails.size());
EXPECT_EQ(driveUrl, urls[0].url);
EXPECT_EQ(mapsUrl, urls[1].url);
EXPECT_EQ(plusUrl, urls[2].url);
EXPECT_EQ(kUrl0, urls[3].url);
db.SetPageThumbnail(url3, 2, Images());
db.GetPageThumbnails(&urls, &thumbnails);
ASSERT_EQ(6u, urls.size());
ASSERT_EQ(6u, thumbnails.size());
EXPECT_EQ(driveUrl, urls[0].url);
EXPECT_EQ(mapsUrl, urls[1].url);
EXPECT_EQ(kUrl0, urls[2].url);
EXPECT_EQ(plusUrl, urls[4].url);
db.RemoveURL(url3);
db.GetPageThumbnails(&urls, &thumbnails);
ASSERT_EQ(5u, urls.size());
ASSERT_EQ(5u, thumbnails.size());
EXPECT_EQ(driveUrl, urls[0].url);
EXPECT_EQ(mapsUrl, urls[1].url);
EXPECT_EQ(kUrl0, urls[2].url);
db.RemoveURL(url2);
db.GetPageThumbnails(&urls, &thumbnails);
ASSERT_EQ(4u, urls.size());
ASSERT_EQ(4u, thumbnails.size());
EXPECT_EQ(mapsUrl, urls[0].url);
EXPECT_EQ(kUrl0, urls[1].url);
}
}