This source file includes following definitions.
- visit_sql_handler_
- SetUp
- TearDown
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include <vector>
#include "chrome/browser/history/android/urls_sql_handler.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/history/android/visit_sql_handler.h"
#include "chrome/browser/history/history_database.h"
#include "chrome/common/chrome_constants.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::Time;
using base::TimeDelta;
namespace history {
class UrlsSQLHandlerTest : public testing::Test {
public:
UrlsSQLHandlerTest()
: urls_sql_handler_(&history_db_),
visit_sql_handler_(&history_db_) {
}
virtual ~UrlsSQLHandlerTest() {}
protected:
virtual void SetUp() {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
base::FilePath history_db_name = temp_dir_.path().AppendASCII(
chrome::kHistoryFilename);
ASSERT_EQ(sql::INIT_OK, history_db_.Init(history_db_name));
}
virtual void TearDown() {
}
HistoryDatabase history_db_;
base::ScopedTempDir temp_dir_;
UrlsSQLHandler urls_sql_handler_;
VisitSQLHandler visit_sql_handler_;
private:
DISALLOW_COPY_AND_ASSIGN(UrlsSQLHandlerTest);
};
TEST_F(UrlsSQLHandlerTest, InsertURL) {
HistoryAndBookmarkRow row;
row.set_raw_url("http://google.com");
row.set_url(GURL("http://google.com"));
ASSERT_TRUE(urls_sql_handler_.Insert(&row));
URLRow url_row;
ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
EXPECT_EQ(row.url(), url_row.url());
EXPECT_EQ(0, url_row.visit_count());
EXPECT_EQ(Time(), url_row.last_visit());
EXPECT_EQ(row.url_id(), url_row.id());
}
TEST_F(UrlsSQLHandlerTest, InsertURLWithLastVisitTime) {
HistoryAndBookmarkRow row;
row.set_raw_url("http://google.com");
row.set_url(GURL("http://google.com"));
row.set_last_visit_time(Time::Now());
ASSERT_TRUE(urls_sql_handler_.Insert(&row));
URLRow url_row;
ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
EXPECT_EQ(row.url_id(), url_row.id());
EXPECT_EQ(row.url(), url_row.url());
EXPECT_EQ(1, url_row.visit_count());
EXPECT_EQ(row.last_visit_time(), url_row.last_visit());
}
TEST_F(UrlsSQLHandlerTest, InsertURLWithBothTime) {
HistoryAndBookmarkRow row;
row.set_raw_url("http://google.com");
row.set_url(GURL("http://google.com"));
row.set_last_visit_time(Time::Now());
row.set_created(Time::Now() - TimeDelta::FromDays(1));
ASSERT_TRUE(urls_sql_handler_.Insert(&row));
URLRow url_row;
ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
EXPECT_EQ(row.url_id(), url_row.id());
EXPECT_EQ(row.url(), url_row.url());
EXPECT_EQ(2, url_row.visit_count());
EXPECT_EQ(row.last_visit_time(), url_row.last_visit());
}
TEST_F(UrlsSQLHandlerTest, InsertURLWithCreatedTime) {
HistoryAndBookmarkRow row;
row.set_raw_url("http://google.com");
row.set_url(GURL("http://google.com"));
row.set_title(base::UTF8ToUTF16("Google"));
row.set_created(Time::Now());
ASSERT_TRUE(urls_sql_handler_.Insert(&row));
URLRow url_row;
ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
EXPECT_EQ(row.url_id(), url_row.id());
EXPECT_EQ(row.url(), url_row.url());
EXPECT_EQ(1, url_row.visit_count());
EXPECT_EQ(row.created(), url_row.last_visit());
EXPECT_EQ(row.title(), url_row.title());
}
TEST_F(UrlsSQLHandlerTest, InsertURLWithVisitCount) {
HistoryAndBookmarkRow row;
row.set_raw_url("http://google.com");
row.set_url(GURL("http://google.com"));
row.set_visit_count(1);
ASSERT_TRUE(urls_sql_handler_.Insert(&row));
URLRow url_row;
ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
EXPECT_EQ(row.url_id(), url_row.id());
EXPECT_EQ(row.url(), url_row.url());
EXPECT_EQ(1, url_row.visit_count());
EXPECT_NE(Time(), url_row.last_visit());
}
TEST_F(UrlsSQLHandlerTest, Insert) {
HistoryAndBookmarkRow row;
row.set_raw_url("http://google.com");
row.set_url(GURL("http://google.com"));
row.set_visit_count(10);
row.set_last_visit_time(Time::Now());
row.set_title(base::UTF8ToUTF16("Google"));
ASSERT_TRUE(urls_sql_handler_.Insert(&row));
URLRow url_row;
ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
EXPECT_EQ(row.url_id(), url_row.id());
EXPECT_EQ(row.url(), url_row.url());
EXPECT_EQ(10, url_row.visit_count());
EXPECT_EQ(row.last_visit_time(), url_row.last_visit());
EXPECT_EQ(row.title(), url_row.title());
}
TEST_F(UrlsSQLHandlerTest, Update) {
HistoryAndBookmarkRow row;
row.set_raw_url("http://google.com");
row.set_url(GURL("http://google.com"));
row.set_title(base::UTF8ToUTF16("Google"));
row.set_visit_count(10);
row.set_last_visit_time(Time::Now() - TimeDelta::FromDays(10));
ASSERT_TRUE(urls_sql_handler_.Insert(&row));
ASSERT_TRUE(visit_sql_handler_.Insert(&row));
URLRow url_row;
ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
EXPECT_EQ(row.url(), url_row.url());
EXPECT_EQ(10, url_row.visit_count());
EXPECT_EQ(row.last_visit_time(), url_row.last_visit());
HistoryAndBookmarkRow update_row;
update_row.set_last_visit_time(Time::Now());
update_row.set_visit_count(1);
update_row.set_title(base::UTF8ToUTF16("Google Inc"));
TableIDRow id;
id.url_id = url_row.id();
TableIDRows ids;
ids.push_back(id);
ASSERT_TRUE(urls_sql_handler_.Update(update_row, ids));
ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
EXPECT_EQ(1, url_row.visit_count());
EXPECT_EQ(update_row.last_visit_time(), url_row.last_visit());
EXPECT_EQ(update_row.title(), url_row.title());
}
TEST_F(UrlsSQLHandlerTest, UpdateLastBothTime) {
HistoryAndBookmarkRow row;
row.set_raw_url("http://google.com");
row.set_url(GURL("http://google.com"));
row.set_title(base::UTF8ToUTF16("Google"));
row.set_visit_count(10);
row.set_last_visit_time(Time::Now() - TimeDelta::FromDays(10));
ASSERT_TRUE(urls_sql_handler_.Insert(&row));
ASSERT_TRUE(visit_sql_handler_.Insert(&row));
URLRow url_row;
ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
EXPECT_EQ(row.url(), url_row.url());
EXPECT_EQ(10, url_row.visit_count());
EXPECT_EQ(row.last_visit_time(), url_row.last_visit());
HistoryAndBookmarkRow update_row1;
update_row1.set_created(url_row.last_visit());
update_row1.set_last_visit_time(Time::Now());
TableIDRow id;
id.url_id = url_row.id();
TableIDRows ids;
ids.push_back(id);
ASSERT_TRUE(urls_sql_handler_.Update(update_row1, ids));
ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
EXPECT_EQ(11, url_row.visit_count());
EXPECT_EQ(update_row1.last_visit_time(), url_row.last_visit());
HistoryAndBookmarkRow update_row;
update_row.set_created(Time::Now());
ASSERT_TRUE(urls_sql_handler_.Update(update_row, ids));
ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
EXPECT_EQ(11, url_row.visit_count());
EXPECT_EQ(update_row1.last_visit_time(), url_row.last_visit());
}
TEST_F(UrlsSQLHandlerTest, UpdateVisitCountZero) {
HistoryAndBookmarkRow row;
row.set_raw_url("http://google.com");
row.set_url(GURL("http://google.com"));
row.set_visit_count(100);
row.set_last_visit_time(Time::Now() - TimeDelta::FromDays(10));
ASSERT_TRUE(urls_sql_handler_.Insert(&row));
ASSERT_TRUE(visit_sql_handler_.Insert(&row));
URLRow url_row;
ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
EXPECT_EQ(row.url(), url_row.url());
EXPECT_EQ(100, url_row.visit_count());
EXPECT_EQ(row.last_visit_time(), url_row.last_visit());
HistoryAndBookmarkRow update_row;
update_row.set_visit_count(0);
TableIDRow id;
id.url_id = url_row.id();
TableIDRows ids;
ids.push_back(id);
ASSERT_TRUE(urls_sql_handler_.Update(update_row, ids));
ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
EXPECT_EQ(0, url_row.visit_count());
EXPECT_EQ(Time(), url_row.last_visit());
}
TEST_F(UrlsSQLHandlerTest, UpdateEarlyLastVisit) {
HistoryAndBookmarkRow row;
row.set_raw_url("http://google.com");
row.set_url(GURL("http://google.com"));
row.set_visit_count(100);
row.set_last_visit_time(Time::Now() - TimeDelta::FromDays(10));
ASSERT_TRUE(urls_sql_handler_.Insert(&row));
ASSERT_TRUE(visit_sql_handler_.Insert(&row));
URLRow url_row;
ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
EXPECT_EQ(row.url(), url_row.url());
EXPECT_EQ(100, url_row.visit_count());
EXPECT_EQ(row.last_visit_time(), url_row.last_visit());
HistoryAndBookmarkRow update_row;
update_row.set_last_visit_time(Time::Now() - TimeDelta::FromDays(11));
TableIDRow id;
id.url_id = url_row.id();
TableIDRows ids;
ids.push_back(id);
EXPECT_FALSE(urls_sql_handler_.Update(update_row, ids));
}
TEST_F(UrlsSQLHandlerTest, UpdateVisitCountIncreased) {
HistoryAndBookmarkRow row;
row.set_raw_url("http://google.com");
row.set_url(GURL("http://google.com"));
row.set_visit_count(10);
row.set_last_visit_time(Time::Now() - TimeDelta::FromDays(10));
ASSERT_TRUE(urls_sql_handler_.Insert(&row));
ASSERT_TRUE(visit_sql_handler_.Insert(&row));
URLRow url_row;
ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
EXPECT_EQ(row.url(), url_row.url());
EXPECT_EQ(10, url_row.visit_count());
EXPECT_EQ(row.last_visit_time(), url_row.last_visit());
HistoryAndBookmarkRow update_row;
update_row.set_visit_count(11);
TableIDRow id;
id.url_id = url_row.id();
TableIDRows ids;
ids.push_back(id);
ASSERT_TRUE(urls_sql_handler_.Update(update_row, ids));
ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
EXPECT_EQ(row.url(), url_row.url());
EXPECT_EQ(11, url_row.visit_count());
EXPECT_LT(row.last_visit_time(), url_row.last_visit());
}
TEST_F(UrlsSQLHandlerTest, Delete) {
HistoryAndBookmarkRow row;
row.set_raw_url("http://google.com");
row.set_url(GURL("http://google.com"));
row.set_visit_count(10);
row.set_last_visit_time(Time::Now() - TimeDelta::FromDays(10));
ASSERT_TRUE(urls_sql_handler_.Insert(&row));
URLRow url_row;
ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
EXPECT_EQ(row.url(), url_row.url());
EXPECT_EQ(10, url_row.visit_count());
EXPECT_EQ(row.last_visit_time(), url_row.last_visit());
TableIDRow id;
id.url_id = url_row.id();
TableIDRows ids;
ids.push_back(id);
ASSERT_TRUE(urls_sql_handler_.Delete(ids));
EXPECT_FALSE(history_db_.GetURLRow(row.url_id(), &url_row));
}
}