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
#include <vector>
#include "chrome/browser/history/android/visit_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/urls_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 VisitSQLHandlerTest : public testing::Test {
public:
VisitSQLHandlerTest()
: urls_sql_handler_(&history_db_),
visit_sql_handler_(&history_db_) {
}
virtual ~VisitSQLHandlerTest() {}
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(VisitSQLHandlerTest);
};
TEST_F(VisitSQLHandlerTest, InsertURL) {
HistoryAndBookmarkRow row;
row.set_raw_url("http://google.com");
row.set_url(GURL("http://google.com"));
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));
VisitVector visits;
ASSERT_TRUE(history_db_.GetVisitsForURL(row.url_id(), &visits));
EXPECT_EQ(0u, visits.size());
}
TEST_F(VisitSQLHandlerTest, 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));
ASSERT_TRUE(visit_sql_handler_.Insert(&row));
URLRow url_row;
ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
VisitVector visits;
ASSERT_TRUE(history_db_.GetVisitsForURL(row.url_id(), &visits));
EXPECT_EQ(1u, visits.size());
EXPECT_EQ(row.last_visit_time(), visits[0].visit_time);
}
TEST_F(VisitSQLHandlerTest, 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));
ASSERT_TRUE(visit_sql_handler_.Insert(&row));
URLRow url_row;
ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
VisitVector visits;
ASSERT_TRUE(history_db_.GetVisitsForURL(row.url_id(), &visits));
EXPECT_EQ(1u, visits.size());
EXPECT_EQ(row.created(), visits[0].visit_time);
}
TEST_F(VisitSQLHandlerTest, 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));
ASSERT_TRUE(visit_sql_handler_.Insert(&row));
URLRow url_row;
ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
VisitVector visits;
ASSERT_TRUE(history_db_.GetVisitsForURL(row.url_id(), &visits));
EXPECT_EQ(1u, visits.size());
EXPECT_NE(Time(), visits[0].visit_time);
}
TEST_F(VisitSQLHandlerTest, 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_created(Time::Now() - TimeDelta::FromDays(1));
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));
VisitVector visits;
ASSERT_TRUE(history_db_.GetVisitsForURL(row.url_id(), &visits));
EXPECT_EQ(10u, visits.size());
EXPECT_EQ(row.created(), visits[0].visit_time);
EXPECT_EQ(row.last_visit_time(), visits[9].visit_time);
}
TEST_F(VisitSQLHandlerTest, UpdateVisitTimeAndVisitCount) {
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));
HistoryAndBookmarkRow update_row;
update_row.set_last_visit_time(Time::Now());
update_row.set_visit_count(1);
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(visit_sql_handler_.Update(update_row, ids));
VisitVector visits;
ASSERT_TRUE(history_db_.GetVisitsForURL(row.url_id(), &visits));
EXPECT_EQ(1u, visits.size());
EXPECT_EQ(update_row.last_visit_time(), visits[0].visit_time);
}
TEST_F(VisitSQLHandlerTest, UpdateVisitCountZero) {
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));
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(visit_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());
VisitVector visits;
ASSERT_TRUE(history_db_.GetVisitsForURL(row.url_id(), &visits));
EXPECT_EQ(0u, visits.size());
}
TEST_F(VisitSQLHandlerTest, UpdateBothTime) {
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));
HistoryAndBookmarkRow update_row;
update_row.set_last_visit_time(Time::Now() - TimeDelta::FromDays(9));
update_row.set_created(Time::Now() - TimeDelta::FromDays(10));
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(visit_sql_handler_.Update(update_row, ids));
VisitVector visits;
ASSERT_TRUE(history_db_.GetVisitsForURL(row.url_id(), &visits));
EXPECT_EQ(11u, visits.size());
EXPECT_EQ(update_row.created(), visits[0].visit_time);
EXPECT_EQ(update_row.last_visit_time(), visits[10].visit_time);
}
TEST_F(VisitSQLHandlerTest, 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));
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(VisitSQLHandlerTest, 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));
}
}