This source file includes following definitions.
- now_
- ClearLastNotifications
- StarURL
- path
- SetUp
- TearDown
- BroadcastNotifications
- NotifySyncURLsDeleted
- AddExampleData
- AddExampleSourceData
- HasFavicon
- GetFavicon
- HasThumbnail
- EnsureURLInfoGone
- TEST_F
- IsStringInFile
- TEST_F
- 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 <algorithm>
#include <string>
#include <utility>
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_ptr.h"
#include "base/path_service.h"
#include "base/stl_util.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/bookmarks/bookmark_utils.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/history/archived_database.h"
#include "chrome/browser/history/expire_history_backend.h"
#include "chrome/browser/history/history_database.h"
#include "chrome/browser/history/history_notifications.h"
#include "chrome/browser/history/thumbnail_database.h"
#include "chrome/browser/history/top_sites.h"
#include "chrome/common/thumbnail_score.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/tools/profiles/thumbnail-inl.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/codec/jpeg_codec.h"
using base::Time;
using base::TimeDelta;
using base::TimeTicks;
using content::BrowserThread;
static const base::FilePath::CharType kHistoryFile[] =
FILE_PATH_LITERAL("History");
static const base::FilePath::CharType kArchivedHistoryFile[] =
FILE_PATH_LITERAL("Archived History");
static const base::FilePath::CharType kThumbnailFile[] =
FILE_PATH_LITERAL("Thumbnails");
namespace history {
class ExpireHistoryTest : public testing::Test,
public BroadcastNotificationDelegate {
public:
ExpireHistoryTest()
: bookmark_model_(NULL),
ui_thread_(BrowserThread::UI, &message_loop_),
db_thread_(BrowserThread::DB, &message_loop_),
expirer_(this, &bookmark_model_),
now_(Time::Now()) {
}
protected:
void AddExampleData(URLID url_ids[3], Time visit_times[4]);
void AddExampleSourceData(const GURL& url, URLID* id);
bool HasFavicon(chrome::FaviconID favicon_id);
bool HasThumbnail(URLID url_id);
chrome::FaviconID GetFavicon(const GURL& page_url,
chrome::IconType icon_type);
void EnsureURLInfoGone(const URLRow& row);
void ClearLastNotifications() {
STLDeleteValues(¬ifications_);
}
void StarURL(const GURL& url) {
bookmark_model_.AddURL(
bookmark_model_.bookmark_bar_node(), 0, base::string16(), url);
}
static bool IsStringInFile(const base::FilePath& filename, const char* str);
const base::FilePath& path() const { return tmp_dir_.path(); }
base::ScopedTempDir tmp_dir_;
BookmarkModel bookmark_model_;
base::MessageLoopForUI message_loop_;
content::TestBrowserThread ui_thread_;
content::TestBrowserThread db_thread_;
ExpireHistoryBackend expirer_;
scoped_ptr<HistoryDatabase> main_db_;
scoped_ptr<ArchivedDatabase> archived_db_;
scoped_ptr<ThumbnailDatabase> thumb_db_;
TestingProfile profile_;
scoped_refptr<TopSites> top_sites_;
const Time now_;
typedef std::vector< std::pair<int, HistoryDetails*> >
NotificationList;
NotificationList notifications_;
private:
virtual void SetUp() {
ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir());
base::FilePath history_name = path().Append(kHistoryFile);
main_db_.reset(new HistoryDatabase);
if (main_db_->Init(history_name) != sql::INIT_OK)
main_db_.reset();
base::FilePath archived_name = path().Append(kArchivedHistoryFile);
archived_db_.reset(new ArchivedDatabase);
if (!archived_db_->Init(archived_name))
archived_db_.reset();
base::FilePath thumb_name = path().Append(kThumbnailFile);
thumb_db_.reset(new ThumbnailDatabase);
if (thumb_db_->Init(thumb_name) != sql::INIT_OK)
thumb_db_.reset();
expirer_.SetDatabases(main_db_.get(), archived_db_.get(), thumb_db_.get());
profile_.CreateTopSites();
profile_.BlockUntilTopSitesLoaded();
top_sites_ = profile_.GetTopSites();
}
virtual void TearDown() {
top_sites_ = NULL;
ClearLastNotifications();
expirer_.SetDatabases(NULL, NULL, NULL);
main_db_.reset();
archived_db_.reset();
thumb_db_.reset();
}
virtual void BroadcastNotifications(
int type,
scoped_ptr<HistoryDetails> details) OVERRIDE {
notifications_.push_back(std::make_pair(type, details.release()));
}
virtual void NotifySyncURLsDeleted(
bool all_history,
bool archived,
URLRows* rows) OVERRIDE {}
};
void ExpireHistoryTest::AddExampleData(URLID url_ids[3], Time visit_times[4]) {
if (!main_db_.get())
return;
visit_times[3] = Time::Now();
visit_times[2] = visit_times[3] - TimeDelta::FromDays(1);
visit_times[1] = visit_times[3] - TimeDelta::FromDays(2);
visit_times[0] = visit_times[3] - TimeDelta::FromDays(3);
chrome::FaviconID favicon1 = thumb_db_->AddFavicon(
GURL("http://favicon/url1"), chrome::FAVICON);
chrome::FaviconID favicon2 = thumb_db_->AddFavicon(
GURL("http://favicon/url2"), chrome::FAVICON);
URLRow url_row1(GURL("http://www.google.com/1"));
url_row1.set_last_visit(visit_times[0]);
url_row1.set_visit_count(1);
url_ids[0] = main_db_->AddURL(url_row1);
thumb_db_->AddIconMapping(url_row1.url(), favicon1);
URLRow url_row2(GURL("http://www.google.com/2"));
url_row2.set_last_visit(visit_times[2]);
url_row2.set_visit_count(2);
url_row2.set_typed_count(1);
url_ids[1] = main_db_->AddURL(url_row2);
thumb_db_->AddIconMapping(url_row2.url(), favicon1);
URLRow url_row3(GURL("http://www.google.com/3"));
url_row3.set_last_visit(visit_times[3]);
url_row3.set_visit_count(1);
url_ids[2] = main_db_->AddURL(url_row3);
thumb_db_->AddIconMapping(url_row3.url(), favicon2);
scoped_ptr<SkBitmap> thumbnail_bitmap(
gfx::JPEGCodec::Decode(kGoogleThumbnail, sizeof(kGoogleThumbnail)));
gfx::Image thumbnail = gfx::Image::CreateFrom1xBitmap(*thumbnail_bitmap);
ThumbnailScore score(0.25, true, true, Time::Now());
Time time;
GURL gurl;
top_sites_->SetPageThumbnail(url_row1.url(), thumbnail, score);
top_sites_->SetPageThumbnail(url_row2.url(), thumbnail, score);
top_sites_->SetPageThumbnail(url_row3.url(), thumbnail, score);
VisitRow visit_row1;
visit_row1.url_id = url_ids[0];
visit_row1.visit_time = visit_times[0];
main_db_->AddVisit(&visit_row1, SOURCE_BROWSED);
VisitRow visit_row2;
visit_row2.url_id = url_ids[1];
visit_row2.visit_time = visit_times[1];
main_db_->AddVisit(&visit_row2, SOURCE_BROWSED);
VisitRow visit_row3;
visit_row3.url_id = url_ids[1];
visit_row3.visit_time = visit_times[2];
visit_row3.transition = content::PAGE_TRANSITION_TYPED;
main_db_->AddVisit(&visit_row3, SOURCE_BROWSED);
VisitRow visit_row4;
visit_row4.url_id = url_ids[2];
visit_row4.visit_time = visit_times[3];
main_db_->AddVisit(&visit_row4, SOURCE_BROWSED);
}
void ExpireHistoryTest::AddExampleSourceData(const GURL& url, URLID* id) {
if (!main_db_)
return;
Time last_visit_time = Time::Now();
URLRow url_row1(url);
url_row1.set_last_visit(last_visit_time);
url_row1.set_visit_count(4);
URLID url_id = main_db_->AddURL(url_row1);
*id = url_id;
VisitRow visit_row1(url_id, last_visit_time - TimeDelta::FromDays(4), 0,
content::PAGE_TRANSITION_TYPED, 0);
main_db_->AddVisit(&visit_row1, SOURCE_SYNCED);
VisitRow visit_row2(url_id, last_visit_time - TimeDelta::FromDays(3), 0,
content::PAGE_TRANSITION_TYPED, 0);
main_db_->AddVisit(&visit_row2, SOURCE_BROWSED);
VisitRow visit_row3(url_id, last_visit_time - TimeDelta::FromDays(2), 0,
content::PAGE_TRANSITION_TYPED, 0);
main_db_->AddVisit(&visit_row3, SOURCE_EXTENSION);
VisitRow visit_row4(
url_id, last_visit_time, 0, content::PAGE_TRANSITION_TYPED, 0);
main_db_->AddVisit(&visit_row4, SOURCE_FIREFOX_IMPORTED);
}
bool ExpireHistoryTest::HasFavicon(chrome::FaviconID favicon_id) {
if (!thumb_db_.get() || favicon_id == 0)
return false;
return thumb_db_->GetFaviconHeader(favicon_id, NULL, NULL);
}
chrome::FaviconID ExpireHistoryTest::GetFavicon(const GURL& page_url,
chrome::IconType icon_type) {
std::vector<IconMapping> icon_mappings;
if (thumb_db_->GetIconMappingsForPageURL(page_url, icon_type,
&icon_mappings)) {
return icon_mappings[0].icon_id;
}
return 0;
}
bool ExpireHistoryTest::HasThumbnail(URLID url_id) {
URLRow info;
if (!main_db_->GetURLRow(url_id, &info))
return false;
GURL url = info.url();
scoped_refptr<base::RefCountedMemory> data;
return top_sites_->GetPageThumbnail(url, false, &data);
}
void ExpireHistoryTest::EnsureURLInfoGone(const URLRow& row) {
URLRow temp_row;
EXPECT_FALSE(main_db_->GetURLRow(row.id(), &temp_row));
VisitVector visits;
main_db_->GetVisitsForURL(row.id(), &visits);
EXPECT_EQ(0U, visits.size());
bool found_delete_notification = false;
for (size_t i = 0; i < notifications_.size(); i++) {
if (notifications_[i].first == chrome::NOTIFICATION_HISTORY_URLS_DELETED) {
URLsDeletedDetails* details = reinterpret_cast<URLsDeletedDetails*>(
notifications_[i].second);
EXPECT_FALSE(details->archived);
const history::URLRows& rows(details->rows);
if (std::find_if(rows.begin(), rows.end(),
history::URLRow::URLRowHasURL(row.url())) != rows.end()) {
found_delete_notification = true;
}
} else {
EXPECT_NE(notifications_[i].first,
chrome::NOTIFICATION_HISTORY_URL_VISITED);
EXPECT_NE(notifications_[i].first,
chrome::NOTIFICATION_HISTORY_URLS_MODIFIED);
}
}
EXPECT_TRUE(found_delete_notification);
}
TEST_F(ExpireHistoryTest, DeleteFaviconsIfPossible) {
const GURL favicon_url("http://www.google.com/favicon.ico");
chrome::FaviconID icon_id = thumb_db_->AddFavicon(
favicon_url, chrome::FAVICON);
EXPECT_TRUE(icon_id);
EXPECT_TRUE(HasFavicon(icon_id));
std::set<chrome::FaviconID> favicon_set;
std::set<GURL> expired_favicons;
favicon_set.insert(icon_id);
expirer_.DeleteFaviconsIfPossible(favicon_set, &expired_favicons);
EXPECT_FALSE(HasFavicon(icon_id));
EXPECT_EQ(1U, expired_favicons.size());
EXPECT_EQ(1U, expired_favicons.count(favicon_url));
icon_id = thumb_db_->AddFavicon(
favicon_url, chrome::TOUCH_ICON);
EXPECT_TRUE(icon_id);
EXPECT_TRUE(HasFavicon(icon_id));
URLRow row(GURL("http://www.google.com/2"));
row.set_visit_count(1);
EXPECT_TRUE(main_db_->AddURL(row));
thumb_db_->AddIconMapping(row.url(), icon_id);
favicon_set.clear();
favicon_set.insert(icon_id);
expired_favicons.clear();
expirer_.DeleteFaviconsIfPossible(favicon_set, &expired_favicons);
EXPECT_TRUE(HasFavicon(icon_id));
EXPECT_TRUE(expired_favicons.empty());
}
bool ExpireHistoryTest::IsStringInFile(const base::FilePath& filename,
const char* str) {
std::string contents;
EXPECT_TRUE(base::ReadFileToString(filename, &contents));
return contents.find(str) != std::string::npos;
}
TEST_F(ExpireHistoryTest, DISABLED_DeleteURLAndFavicon) {
URLID url_ids[3];
Time visit_times[4];
AddExampleData(url_ids, visit_times);
URLRow last_row;
ASSERT_TRUE(main_db_->GetURLRow(url_ids[2], &last_row));
chrome::FaviconID favicon_id = GetFavicon(last_row.url(), chrome::FAVICON);
EXPECT_TRUE(HasFavicon(favicon_id));
VisitVector visits;
main_db_->GetVisitsForURL(url_ids[2], &visits);
ASSERT_EQ(1U, visits.size());
expirer_.DeleteURL(last_row.url());
EnsureURLInfoGone(last_row);
EXPECT_FALSE(GetFavicon(last_row.url(), chrome::FAVICON));
EXPECT_FALSE(HasFavicon(favicon_id));
}
TEST_F(ExpireHistoryTest, DeleteURLWithoutFavicon) {
URLID url_ids[3];
Time visit_times[4];
AddExampleData(url_ids, visit_times);
URLRow last_row;
ASSERT_TRUE(main_db_->GetURLRow(url_ids[1], &last_row));
chrome::FaviconID favicon_id = GetFavicon(last_row.url(), chrome::FAVICON);
EXPECT_TRUE(HasFavicon(favicon_id));
VisitVector visits;
main_db_->GetVisitsForURL(url_ids[1], &visits);
EXPECT_EQ(2U, visits.size());
expirer_.DeleteURL(last_row.url());
EnsureURLInfoGone(last_row);
EXPECT_TRUE(HasFavicon(favicon_id));
}
TEST_F(ExpireHistoryTest, DontDeleteStarredURL) {
URLID url_ids[3];
Time visit_times[4];
AddExampleData(url_ids, visit_times);
URLRow url_row;
ASSERT_TRUE(main_db_->GetURLRow(url_ids[2], &url_row));
StarURL(url_row.url());
expirer_.DeleteURL(url_row.url());
GURL url = url_row.url();
ASSERT_TRUE(main_db_->GetRowForURL(url, &url_row));
chrome::FaviconID favicon_id = GetFavicon(url_row.url(), chrome::FAVICON);
EXPECT_TRUE(HasFavicon(favicon_id));
VisitVector visits;
main_db_->GetVisitsForURL(url_row.id(), &visits);
ASSERT_EQ(0U, visits.size());
bookmark_utils::RemoveAllBookmarks(&bookmark_model_, url);
expirer_.DeleteURL(url);
EnsureURLInfoGone(url_row);
}
TEST_F(ExpireHistoryTest, DeleteURLs) {
URLID url_ids[3];
Time visit_times[4];
AddExampleData(url_ids, visit_times);
URLRow rows[3];
chrome::FaviconID favicon_ids[3];
std::vector<GURL> urls;
urls.push_back(GURL());
for (size_t i = 0; i < arraysize(rows); ++i) {
ASSERT_TRUE(main_db_->GetURLRow(url_ids[i], &rows[i]));
favicon_ids[i] = GetFavicon(rows[i].url(), chrome::FAVICON);
EXPECT_TRUE(HasFavicon(favicon_ids[i]));
urls.push_back(rows[i].url());
}
StarURL(rows[0].url());
expirer_.DeleteURLs(urls);
ASSERT_TRUE(main_db_->GetRowForURL(rows[0].url(), &rows[0]));
EnsureURLInfoGone(rows[1]);
EnsureURLInfoGone(rows[2]);
EXPECT_TRUE(HasFavicon(favicon_ids[0]));
EXPECT_TRUE(HasFavicon(favicon_ids[1]));
EXPECT_FALSE(HasFavicon(favicon_ids[2]));
}
TEST_F(ExpireHistoryTest, FlushRecentURLsUnstarred) {
URLID url_ids[3];
Time visit_times[4];
AddExampleData(url_ids, visit_times);
URLRow url_row1, url_row2;
ASSERT_TRUE(main_db_->GetURLRow(url_ids[1], &url_row1));
ASSERT_TRUE(main_db_->GetURLRow(url_ids[2], &url_row2));
VisitVector visits;
main_db_->GetVisitsForURL(url_ids[2], &visits);
ASSERT_EQ(1U, visits.size());
std::set<GURL> restrict_urls;
expirer_.ExpireHistoryBetween(restrict_urls, visit_times[2], Time());
visits.clear();
main_db_->GetVisitsForURL(url_ids[1], &visits);
EXPECT_EQ(1U, visits.size());
URLRow temp_row;
ASSERT_TRUE(main_db_->GetURLRow(url_ids[1], &temp_row));
EXPECT_TRUE(visit_times[2] == url_row1.last_visit());
EXPECT_TRUE(visit_times[1] == temp_row.last_visit());
EXPECT_EQ(2, url_row1.visit_count());
EXPECT_EQ(1, temp_row.visit_count());
EXPECT_EQ(1, url_row1.typed_count());
EXPECT_EQ(0, temp_row.typed_count());
chrome::FaviconID favicon_id = GetFavicon(url_row1.url(), chrome::FAVICON);
EXPECT_TRUE(HasFavicon(favicon_id));
chrome::FaviconID favicon_id2 = GetFavicon(url_row2.url(), chrome::FAVICON);
EnsureURLInfoGone(url_row2);
EXPECT_FALSE(HasFavicon(favicon_id2));
}
TEST_F(ExpireHistoryTest, FlushURLsForTimes) {
URLID url_ids[3];
Time visit_times[4];
AddExampleData(url_ids, visit_times);
URLRow url_row1, url_row2;
ASSERT_TRUE(main_db_->GetURLRow(url_ids[1], &url_row1));
ASSERT_TRUE(main_db_->GetURLRow(url_ids[2], &url_row2));
VisitVector visits;
main_db_->GetVisitsForURL(url_ids[2], &visits);
ASSERT_EQ(1U, visits.size());
std::vector<base::Time> times;
times.push_back(visit_times[3]);
times.push_back(visit_times[2]);
expirer_.ExpireHistoryForTimes(times);
visits.clear();
main_db_->GetVisitsForURL(url_ids[1], &visits);
EXPECT_EQ(1U, visits.size());
URLRow temp_row;
ASSERT_TRUE(main_db_->GetURLRow(url_ids[1], &temp_row));
EXPECT_TRUE(visit_times[2] == url_row1.last_visit());
EXPECT_TRUE(visit_times[1] == temp_row.last_visit());
EXPECT_EQ(2, url_row1.visit_count());
EXPECT_EQ(1, temp_row.visit_count());
EXPECT_EQ(1, url_row1.typed_count());
EXPECT_EQ(0, temp_row.typed_count());
chrome::FaviconID favicon_id = GetFavicon(url_row1.url(), chrome::FAVICON);
EXPECT_TRUE(HasFavicon(favicon_id));
chrome::FaviconID favicon_id2 = GetFavicon(url_row2.url(), chrome::FAVICON);
EnsureURLInfoGone(url_row2);
EXPECT_FALSE(HasFavicon(favicon_id2));
}
TEST_F(ExpireHistoryTest, FlushRecentURLsUnstarredRestricted) {
URLID url_ids[3];
Time visit_times[4];
AddExampleData(url_ids, visit_times);
URLRow url_row1, url_row2;
ASSERT_TRUE(main_db_->GetURLRow(url_ids[1], &url_row1));
ASSERT_TRUE(main_db_->GetURLRow(url_ids[2], &url_row2));
VisitVector visits;
main_db_->GetVisitsForURL(url_ids[2], &visits);
ASSERT_EQ(1U, visits.size());
std::set<GURL> restrict_urls;
restrict_urls.insert(url_row1.url());
expirer_.ExpireHistoryBetween(restrict_urls, visit_times[2], Time());
visits.clear();
main_db_->GetVisitsForURL(url_ids[1], &visits);
EXPECT_EQ(1U, visits.size());
URLRow temp_row;
ASSERT_TRUE(main_db_->GetURLRow(url_ids[1], &temp_row));
EXPECT_TRUE(visit_times[2] == url_row1.last_visit());
EXPECT_TRUE(visit_times[1] == temp_row.last_visit());
EXPECT_EQ(2, url_row1.visit_count());
EXPECT_EQ(1, temp_row.visit_count());
EXPECT_EQ(1, url_row1.typed_count());
EXPECT_EQ(0, temp_row.typed_count());
chrome::FaviconID favicon_id = GetFavicon(url_row1.url(), chrome::FAVICON);
EXPECT_TRUE(HasFavicon(favicon_id));
EXPECT_TRUE(main_db_->GetURLRow(url_ids[2], &temp_row));
EXPECT_TRUE(HasFavicon(favicon_id));
}
TEST_F(ExpireHistoryTest, FlushRecentURLsStarred) {
URLID url_ids[3];
Time visit_times[4];
AddExampleData(url_ids, visit_times);
URLRow url_row1, url_row2;
ASSERT_TRUE(main_db_->GetURLRow(url_ids[1], &url_row1));
ASSERT_TRUE(main_db_->GetURLRow(url_ids[2], &url_row2));
StarURL(url_row1.url());
StarURL(url_row2.url());
std::set<GURL> restrict_urls;
expirer_.ExpireHistoryBetween(restrict_urls, visit_times[2], Time());
URLRow new_url_row1, new_url_row2;
ASSERT_TRUE(main_db_->GetURLRow(url_ids[1], &new_url_row1));
ASSERT_TRUE(main_db_->GetURLRow(url_ids[2], &new_url_row2));
EXPECT_TRUE(new_url_row1.last_visit() == visit_times[1]);
EXPECT_TRUE(new_url_row2.last_visit().is_null());
EXPECT_EQ(0, new_url_row1.typed_count());
EXPECT_EQ(1, new_url_row1.visit_count());
EXPECT_EQ(0, new_url_row2.typed_count());
EXPECT_EQ(0, new_url_row2.visit_count());
chrome::FaviconID favicon_id = GetFavicon(url_row1.url(), chrome::FAVICON);
EXPECT_TRUE(HasFavicon(favicon_id));
favicon_id = GetFavicon(url_row1.url(), chrome::FAVICON);
EXPECT_TRUE(HasFavicon(favicon_id));
}
TEST_F(ExpireHistoryTest, ArchiveHistoryBeforeUnstarred) {
URLID url_ids[3];
Time visit_times[4];
AddExampleData(url_ids, visit_times);
URLRow url_row1, url_row2;
ASSERT_TRUE(main_db_->GetURLRow(url_ids[1], &url_row1));
ASSERT_TRUE(main_db_->GetURLRow(url_ids[2], &url_row2));
expirer_.ArchiveHistoryBefore(visit_times[1]);
URLRow temp_row;
EXPECT_FALSE(main_db_->GetURLRow(url_ids[0], &temp_row));
EXPECT_TRUE(main_db_->GetURLRow(url_ids[1], &temp_row));
EXPECT_TRUE(main_db_->GetURLRow(url_ids[2], &temp_row));
EXPECT_FALSE(archived_db_->GetRowForURL(url_row1.url(), NULL));
EXPECT_FALSE(archived_db_->GetRowForURL(url_row2.url(), NULL));
expirer_.ArchiveHistoryBefore(visit_times[2]);
EXPECT_FALSE(main_db_->GetURLRow(url_ids[1], &temp_row));
EXPECT_TRUE(main_db_->GetURLRow(url_ids[2], &temp_row));
URLRow archived_row;
EXPECT_TRUE(archived_db_->GetRowForURL(url_row1.url(), &archived_row));
VisitVector archived_visits;
archived_db_->GetVisitsForURL(archived_row.id(), &archived_visits);
EXPECT_EQ(1U, archived_visits.size());
}
TEST_F(ExpireHistoryTest, ArchiveHistoryBeforeStarred) {
URLID url_ids[3];
Time visit_times[4];
AddExampleData(url_ids, visit_times);
URLRow url_row0, url_row1;
ASSERT_TRUE(main_db_->GetURLRow(url_ids[0], &url_row0));
ASSERT_TRUE(main_db_->GetURLRow(url_ids[1], &url_row1));
StarURL(url_row0.url());
StarURL(url_row1.url());
expirer_.ArchiveHistoryBefore(visit_times[2]);
URLRow temp_row;
ASSERT_TRUE(main_db_->GetURLRow(url_ids[0], &temp_row));
EXPECT_FALSE(archived_db_->GetRowForURL(temp_row.url(), NULL));
VisitVector visits;
main_db_->GetVisitsForURL(temp_row.id(), &visits);
EXPECT_EQ(0U, visits.size());
ASSERT_TRUE(main_db_->GetURLRow(url_ids[1], &temp_row));
main_db_->GetVisitsForURL(temp_row.id(), &visits);
EXPECT_EQ(0U, visits.size());
ASSERT_TRUE(archived_db_->GetRowForURL(temp_row.url(), &temp_row));
archived_db_->GetVisitsForURL(temp_row.id(), &visits);
ASSERT_EQ(1U, visits.size());
EXPECT_TRUE(visit_times[2] == visits[0].visit_time);
EXPECT_TRUE(main_db_->GetURLRow(url_ids[2], &temp_row));
EXPECT_FALSE(archived_db_->GetRowForURL(temp_row.url(), NULL));
}
TEST_F(ExpireHistoryTest, ArchiveSomeOldHistory) {
URLID url_ids[3];
Time visit_times[4];
AddExampleData(url_ids, visit_times);
const ExpiringVisitsReader* reader = expirer_.GetAllVisitsReader();
EXPECT_FALSE(expirer_.ArchiveSomeOldHistory(
visit_times[0] - TimeDelta::FromDays(100), reader, 1));
EXPECT_FALSE(expirer_.ArchiveSomeOldHistory(visit_times[0], reader, 2));
EXPECT_TRUE(expirer_.ArchiveSomeOldHistory(visit_times[2], reader, 1));
}
TEST_F(ExpireHistoryTest, ExpiringVisitsReader) {
URLID url_ids[3];
Time visit_times[4];
AddExampleData(url_ids, visit_times);
const ExpiringVisitsReader* all = expirer_.GetAllVisitsReader();
const ExpiringVisitsReader* auto_subframes =
expirer_.GetAutoSubframeVisitsReader();
VisitVector visits;
Time now = Time::Now();
EXPECT_TRUE(main_db_->GetEarlyExpirationThreshold() ==
Time::FromInternalValue(1L));
EXPECT_FALSE(auto_subframes->Read(now, main_db_.get(), &visits, 1));
EXPECT_EQ(0U, visits.size());
EXPECT_TRUE(now <= main_db_->GetEarlyExpirationThreshold());
EXPECT_TRUE(all->Read(now, main_db_.get(), &visits, 1));
EXPECT_EQ(1U, visits.size());
}
TEST_F(ExpireHistoryTest, ArchiveSomeOldHistoryWithSource) {
const GURL url("www.testsource.com");
URLID url_id;
AddExampleSourceData(url, &url_id);
const ExpiringVisitsReader* reader = expirer_.GetAllVisitsReader();
ASSERT_FALSE(expirer_.ArchiveSomeOldHistory(Time::Now(), reader, 10));
URLRow archived_row;
ASSERT_TRUE(archived_db_->GetRowForURL(url, &archived_row));
VisitVector archived_visits;
archived_db_->GetVisitsForURL(archived_row.id(), &archived_visits);
ASSERT_EQ(4U, archived_visits.size());
VisitSourceMap sources;
archived_db_->GetVisitsSource(archived_visits, &sources);
ASSERT_EQ(3U, sources.size());
int result = 0;
VisitSourceMap::iterator iter;
for (int i = 0; i < 4; i++) {
iter = sources.find(archived_visits[i].visit_id);
if (iter == sources.end())
continue;
switch (iter->second) {
case history::SOURCE_EXTENSION:
result |= 0x1;
break;
case history::SOURCE_FIREFOX_IMPORTED:
result |= 0x2;
break;
case history::SOURCE_SYNCED:
result |= 0x4;
default:
break;
}
}
EXPECT_EQ(0x7, result);
main_db_->GetVisitsSource(archived_visits, &sources);
EXPECT_EQ(0U, sources.size());
main_db_->GetVisitsForURL(url_id, &archived_visits);
EXPECT_EQ(0U, archived_visits.size());
}
}