This source file includes following definitions.
- TestURL
- RebuildTable
- AddURLForRebuild
- end_
- NextURL
- HasNextURL
- add_count_
- NewTable
- Add
- Reset
- SetUp
- reset_count
- add_count
- InitVisited
- ClearDB
- Reload
- SetUp
- TearDown
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- new_table_count_
- CountAddEvent
- CountResetEvent
- CountNewTable
- add_count
- add_event_count
- reset_event_count
- new_table_count
- widgets_
- WidgetRestored
- WidgetHidden
- VisibleWidgetCount
- Send
- CreateRenderProcessHost
- SetUp
- CreateBrowserContext
- context
- master
- WaitForCoalescense
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include <cstdio>
#include <string>
#include <vector>
#include "base/file_util.h"
#include "base/memory/shared_memory.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "base/process/process_handle.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "base/time/time.h"
#include "components/visitedlink/browser/visitedlink_delegate.h"
#include "components/visitedlink/browser/visitedlink_event_listener.h"
#include "components/visitedlink/browser/visitedlink_master.h"
#include "components/visitedlink/common/visitedlink_messages.h"
#include "components/visitedlink/renderer/visitedlink_slave.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/public/test/test_renderer_host.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
using content::BrowserThread;
using content::MockRenderProcessHost;
using content::RenderViewHostTester;
namespace visitedlink {
namespace {
typedef std::vector<GURL> URLs;
const char g_test_prefix[] =
"http://www.google.com/products/foo/index.html?id=45028640526508376&seq=";
const int g_test_count = 1000;
GURL TestURL(int i) {
return GURL(base::StringPrintf("%s%d", g_test_prefix, i));
}
std::vector<VisitedLinkSlave*> g_slaves;
class TestVisitedLinkDelegate : public VisitedLinkDelegate {
public:
virtual void RebuildTable(
const scoped_refptr<URLEnumerator>& enumerator) OVERRIDE;
void AddURLForRebuild(const GURL& url);
private:
URLs rebuild_urls_;
};
void TestVisitedLinkDelegate::RebuildTable(
const scoped_refptr<URLEnumerator>& enumerator) {
for (URLs::const_iterator itr = rebuild_urls_.begin();
itr != rebuild_urls_.end();
++itr)
enumerator->OnURL(*itr);
enumerator->OnComplete(true);
}
void TestVisitedLinkDelegate::AddURLForRebuild(const GURL& url) {
rebuild_urls_.push_back(url);
}
class TestURLIterator : public VisitedLinkMaster::URLIterator {
public:
explicit TestURLIterator(const URLs& urls);
virtual const GURL& NextURL() OVERRIDE;
virtual bool HasNextURL() const OVERRIDE;
private:
URLs::const_iterator iterator_;
URLs::const_iterator end_;
};
TestURLIterator::TestURLIterator(const URLs& urls)
: iterator_(urls.begin()),
end_(urls.end()) {
}
const GURL& TestURLIterator::NextURL() {
return *(iterator_++);
}
bool TestURLIterator::HasNextURL() const {
return iterator_ != end_;
}
}
class TrackingVisitedLinkEventListener : public VisitedLinkMaster::Listener {
public:
TrackingVisitedLinkEventListener()
: reset_count_(0),
add_count_(0) {}
virtual void NewTable(base::SharedMemory* table) OVERRIDE {
if (table) {
for (std::vector<VisitedLinkSlave>::size_type i = 0;
i < g_slaves.size(); i++) {
base::SharedMemoryHandle new_handle = base::SharedMemory::NULLHandle();
table->ShareToProcess(base::GetCurrentProcessHandle(), &new_handle);
g_slaves[i]->OnUpdateVisitedLinks(new_handle);
}
}
}
virtual void Add(VisitedLinkCommon::Fingerprint) OVERRIDE { add_count_++; }
virtual void Reset() OVERRIDE { reset_count_++; }
void SetUp() {
reset_count_ = 0;
add_count_ = 0;
}
int reset_count() const { return reset_count_; }
int add_count() const { return add_count_; }
private:
int reset_count_;
int add_count_;
};
class VisitedLinkTest : public testing::Test {
protected:
bool InitVisited(int initial_size, bool suppress_rebuild) {
master_.reset(new VisitedLinkMaster(new TrackingVisitedLinkEventListener(),
&delegate_,
true,
suppress_rebuild, visited_file_,
initial_size));
return master_->Init();
}
void ClearDB() {
if (master_.get())
master_.reset(NULL);
BrowserThread::GetBlockingPool()->FlushForTesting();
}
void Reload() {
ClearDB();
ASSERT_TRUE(InitVisited(0, true));
master_->DebugValidate();
int used_count = master_->GetUsedCount();
ASSERT_EQ(used_count, g_test_count);
VisitedLinkSlave slave;
base::SharedMemoryHandle new_handle = base::SharedMemory::NULLHandle();
master_->shared_memory()->ShareToProcess(
base::GetCurrentProcessHandle(), &new_handle);
slave.OnUpdateVisitedLinks(new_handle);
g_slaves.push_back(&slave);
bool found;
for (int i = 0; i < g_test_count; i++) {
GURL cur = TestURL(i);
found = master_->IsVisited(cur);
EXPECT_TRUE(found) << "URL " << i << "not found in master.";
found = slave.IsVisited(cur);
EXPECT_TRUE(found) << "URL " << i << "not found in slave.";
}
found = master_->IsVisited(GURL("http://unfound.site/"));
ASSERT_FALSE(found);
found = slave.IsVisited(GURL("http://unfound.site/"));
ASSERT_FALSE(found);
master_->DebugValidate();
g_slaves.clear();
}
virtual void SetUp() {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
history_dir_ = temp_dir_.path().AppendASCII("VisitedLinkTest");
ASSERT_TRUE(base::CreateDirectory(history_dir_));
visited_file_ = history_dir_.Append(FILE_PATH_LITERAL("VisitedLinks"));
}
virtual void TearDown() {
ClearDB();
}
base::ScopedTempDir temp_dir_;
base::FilePath history_dir_;
base::FilePath visited_file_;
scoped_ptr<VisitedLinkMaster> master_;
TestVisitedLinkDelegate delegate_;
content::TestBrowserThreadBundle thread_bundle_;
};
TEST_F(VisitedLinkTest, DatabaseIO) {
ASSERT_TRUE(InitVisited(0, true));
for (int i = 0; i < g_test_count; i++)
master_->AddURL(TestURL(i));
Reload();
}
TEST_F(VisitedLinkTest, Delete) {
static const int32 kInitialSize = 17;
ASSERT_TRUE(InitVisited(kInitialSize, true));
const VisitedLinkCommon::Fingerprint kFingerprint0 = kInitialSize * 0 + 14;
const VisitedLinkCommon::Fingerprint kFingerprint1 = kInitialSize * 1 + 14;
const VisitedLinkCommon::Fingerprint kFingerprint2 = kInitialSize * 2 + 14;
const VisitedLinkCommon::Fingerprint kFingerprint3 = kInitialSize * 3 + 14;
const VisitedLinkCommon::Fingerprint kFingerprint4 = kInitialSize * 4 + 14;
master_->AddFingerprint(kFingerprint0, false);
master_->AddFingerprint(kFingerprint1, false);
master_->AddFingerprint(kFingerprint2, false);
master_->AddFingerprint(kFingerprint3, false);
master_->AddFingerprint(kFingerprint4, false);
EXPECT_EQ(kFingerprint3, master_->hash_table_[0]);
master_->DeleteFingerprint(kFingerprint3, false);
VisitedLinkCommon::Fingerprint zero_fingerprint = 0;
EXPECT_EQ(zero_fingerprint, master_->hash_table_[1]);
EXPECT_NE(zero_fingerprint, master_->hash_table_[0]);
master_->DeleteFingerprint(kFingerprint0, false);
master_->DeleteFingerprint(kFingerprint1, false);
master_->DeleteFingerprint(kFingerprint2, false);
master_->DeleteFingerprint(kFingerprint4, false);
EXPECT_EQ(0, master_->used_items_);
for (int i = 0; i < kInitialSize; i++)
EXPECT_EQ(zero_fingerprint, master_->hash_table_[i]) <<
"Hash table has values in it.";
}
TEST_F(VisitedLinkTest, BigDelete) {
ASSERT_TRUE(InitVisited(16381, true));
for (int32 i = 0; i < g_test_count; i++)
master_->AddURL(TestURL(i));
const int kTestDeleteCount = VisitedLinkMaster::kBigDeleteThreshold + 2;
URLs urls_to_delete;
for (int32 i = g_test_count; i < g_test_count + kTestDeleteCount; i++) {
GURL url(TestURL(i));
master_->AddURL(url);
urls_to_delete.push_back(url);
}
TestURLIterator iterator(urls_to_delete);
master_->DeleteURLs(&iterator);
master_->DebugValidate();
Reload();
}
TEST_F(VisitedLinkTest, DeleteAll) {
ASSERT_TRUE(InitVisited(0, true));
{
VisitedLinkSlave slave;
base::SharedMemoryHandle new_handle = base::SharedMemory::NULLHandle();
master_->shared_memory()->ShareToProcess(
base::GetCurrentProcessHandle(), &new_handle);
slave.OnUpdateVisitedLinks(new_handle);
g_slaves.push_back(&slave);
for (int i = 0; i < g_test_count; i++) {
master_->AddURL(TestURL(i));
ASSERT_EQ(i + 1, master_->GetUsedCount());
}
master_->DebugValidate();
for (int i = 0; i < g_test_count; i++)
EXPECT_TRUE(slave.IsVisited(TestURL(i)));
master_->DeleteAllURLs();
EXPECT_EQ(0, master_->GetUsedCount());
for (int i = 0; i < g_test_count; i++) {
EXPECT_FALSE(master_->IsVisited(TestURL(i)));
EXPECT_FALSE(slave.IsVisited(TestURL(i)));
}
g_slaves.clear();
ClearDB();
}
ASSERT_TRUE(InitVisited(0, true));
master_->DebugValidate();
EXPECT_EQ(0, master_->GetUsedCount());
for (int i = 0; i < g_test_count; i++)
EXPECT_FALSE(master_->IsVisited(TestURL(i)));
}
TEST_F(VisitedLinkTest, Resizing) {
const int32 initial_size = 17;
ASSERT_TRUE(InitVisited(initial_size, true));
VisitedLinkSlave slave;
base::SharedMemoryHandle new_handle = base::SharedMemory::NULLHandle();
master_->shared_memory()->ShareToProcess(
base::GetCurrentProcessHandle(), &new_handle);
slave.OnUpdateVisitedLinks(new_handle);
g_slaves.push_back(&slave);
int32 used_count = master_->GetUsedCount();
ASSERT_EQ(used_count, 0);
for (int i = 0; i < g_test_count; i++) {
master_->AddURL(TestURL(i));
used_count = master_->GetUsedCount();
ASSERT_EQ(i + 1, used_count);
}
int32 table_size;
VisitedLinkCommon::Fingerprint* table;
master_->GetUsageStatistics(&table_size, &table);
used_count = master_->GetUsedCount();
ASSERT_GT(table_size, used_count);
ASSERT_EQ(used_count, g_test_count) <<
"table count doesn't match the # of things we added";
int32 child_table_size;
VisitedLinkCommon::Fingerprint* child_table;
slave.GetUsageStatistics(&child_table_size, &child_table);
ASSERT_EQ(table_size, child_table_size);
for (int32 i = 0; i < table_size; i++) {
ASSERT_EQ(table[i], child_table[i]);
}
master_->DebugValidate();
g_slaves.clear();
Reload();
}
TEST_F(VisitedLinkTest, Rebuild) {
int history_count = g_test_count / 2;
for (int i = 0; i < history_count; i++)
delegate_.AddURLForRebuild(TestURL(i));
ASSERT_TRUE(InitVisited(0, false));
for (int i = history_count; i < g_test_count; i++)
master_->AddURL(TestURL(i));
master_->AddURL(TestURL(g_test_count));
URLs urls_to_delete;
urls_to_delete.push_back(TestURL(g_test_count));
TestURLIterator iterator(urls_to_delete);
master_->DeleteURLs(&iterator);
base::RunLoop run_loop;
master_->set_rebuild_complete_task(run_loop.QuitClosure());
run_loop.Run();
Reload();
EXPECT_FALSE(master_->IsVisited(TestURL(g_test_count)));
}
TEST_F(VisitedLinkTest, BigImport) {
ASSERT_TRUE(InitVisited(0, false));
int total_count = VisitedLinkMaster::kDefaultTableSize + 10;
for (int i = 0; i < total_count; i++)
master_->AddURL(TestURL(i));
base::RunLoop run_loop;
master_->set_rebuild_complete_task(run_loop.QuitClosure());
run_loop.Run();
int used_count = master_->GetUsedCount();
ASSERT_EQ(used_count, total_count);
}
TEST_F(VisitedLinkTest, Listener) {
ASSERT_TRUE(InitVisited(0, true));
for (int i = 0; i < g_test_count; i++) {
master_->AddURL(TestURL(i));
ASSERT_EQ(i + 1, master_->GetUsedCount());
}
URLs urls_to_delete;
urls_to_delete.push_back(TestURL(0));
TestURLIterator iterator(urls_to_delete);
master_->DeleteURLs(&iterator);
master_->DeleteAllURLs();
TrackingVisitedLinkEventListener* listener =
static_cast<TrackingVisitedLinkEventListener*>(master_->GetListener());
EXPECT_EQ(g_test_count, listener->add_count());
EXPECT_EQ(2, listener->reset_count());
}
class VisitCountingContext : public content::TestBrowserContext {
public:
VisitCountingContext()
: add_count_(0),
add_event_count_(0),
reset_event_count_(0),
new_table_count_(0) {}
void CountAddEvent(int by) {
add_count_ += by;
add_event_count_++;
}
void CountResetEvent() {
reset_event_count_++;
}
void CountNewTable() {
new_table_count_++;
}
int add_count() const { return add_count_; }
int add_event_count() const { return add_event_count_; }
int reset_event_count() const { return reset_event_count_; }
int new_table_count() const { return new_table_count_; }
private:
int add_count_;
int add_event_count_;
int reset_event_count_;
int new_table_count_;
};
class VisitRelayingRenderProcessHost : public MockRenderProcessHost {
public:
explicit VisitRelayingRenderProcessHost(
content::BrowserContext* browser_context)
: MockRenderProcessHost(browser_context), widgets_(0) {
content::NotificationService::current()->Notify(
content::NOTIFICATION_RENDERER_PROCESS_CREATED,
content::Source<RenderProcessHost>(this),
content::NotificationService::NoDetails());
}
virtual ~VisitRelayingRenderProcessHost() {
content::NotificationService::current()->Notify(
content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
content::Source<content::RenderProcessHost>(this),
content::NotificationService::NoDetails());
}
virtual void WidgetRestored() OVERRIDE { widgets_++; }
virtual void WidgetHidden() OVERRIDE { widgets_--; }
virtual int VisibleWidgetCount() const OVERRIDE { return widgets_; }
virtual bool Send(IPC::Message* msg) OVERRIDE {
VisitCountingContext* counting_context =
static_cast<VisitCountingContext*>(
GetBrowserContext());
if (msg->type() == ChromeViewMsg_VisitedLink_Add::ID) {
PickleIterator iter(*msg);
std::vector<uint64> fingerprints;
CHECK(IPC::ReadParam(msg, &iter, &fingerprints));
counting_context->CountAddEvent(fingerprints.size());
} else if (msg->type() == ChromeViewMsg_VisitedLink_Reset::ID) {
counting_context->CountResetEvent();
} else if (msg->type() == ChromeViewMsg_VisitedLink_NewTable::ID) {
counting_context->CountNewTable();
}
delete msg;
return true;
}
private:
int widgets_;
DISALLOW_COPY_AND_ASSIGN(VisitRelayingRenderProcessHost);
};
class VisitedLinkRenderProcessHostFactory
: public content::RenderProcessHostFactory {
public:
VisitedLinkRenderProcessHostFactory()
: content::RenderProcessHostFactory() {}
virtual content::RenderProcessHost* CreateRenderProcessHost(
content::BrowserContext* browser_context,
content::SiteInstance* site_instance) const OVERRIDE {
return new VisitRelayingRenderProcessHost(browser_context);
}
private:
DISALLOW_COPY_AND_ASSIGN(VisitedLinkRenderProcessHostFactory);
};
class VisitedLinkEventsTest : public content::RenderViewHostTestHarness {
public:
virtual void SetUp() {
SetRenderProcessHostFactory(&vc_rph_factory_);
content::RenderViewHostTestHarness::SetUp();
}
virtual content::BrowserContext* CreateBrowserContext() OVERRIDE {
VisitCountingContext* context = new VisitCountingContext();
master_.reset(new VisitedLinkMaster(context, &delegate_, true));
master_->Init();
return context;
}
VisitCountingContext* context() {
return static_cast<VisitCountingContext*>(browser_context());
}
VisitedLinkMaster* master() const {
return master_.get();
}
void WaitForCoalescense() {
base::RunLoop run_loop;
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
run_loop.QuitClosure(),
base::TimeDelta::FromMilliseconds(110));
run_loop.Run();
}
protected:
VisitedLinkRenderProcessHostFactory vc_rph_factory_;
private:
TestVisitedLinkDelegate delegate_;
scoped_ptr<VisitedLinkMaster> master_;
};
TEST_F(VisitedLinkEventsTest, Coalescense) {
master()->AddURL(GURL("http://acidtests.org/"));
master()->AddURL(GURL("http://google.com/"));
master()->AddURL(GURL("http://chromium.org/"));
master()->AddURL(GURL("http://acidtests.org/"));
EXPECT_EQ(0, context()->add_count());
EXPECT_EQ(0, context()->add_event_count());
WaitForCoalescense();
EXPECT_EQ(3, context()->add_count());
EXPECT_EQ(1, context()->add_event_count());
master()->AddURL(GURL("http://google.com/chrome/"));
master()->AddURL(GURL("http://webkit.org/"));
master()->AddURL(GURL("http://acid3.acidtests.org/"));
WaitForCoalescense();
EXPECT_EQ(6, context()->add_count());
EXPECT_EQ(2, context()->add_event_count());
master()->AddURL(GURL("http://acidtests.org/"));
WaitForCoalescense();
EXPECT_EQ(6, context()->add_count());
EXPECT_EQ(2, context()->add_event_count());
master()->AddURL(GURL("http://build.chromium.org/"));
master()->DeleteAllURLs();
WaitForCoalescense();
EXPECT_EQ(6, context()->add_count());
EXPECT_EQ(2, context()->add_event_count());
EXPECT_EQ(1, context()->reset_event_count());
}
TEST_F(VisitedLinkEventsTest, Basics) {
RenderViewHostTester::For(rvh())->CreateRenderView(base::string16(),
MSG_ROUTING_NONE,
-1);
master()->AddURL(GURL("http://acidtests.org/"));
master()->AddURL(GURL("http://google.com/"));
master()->AddURL(GURL("http://chromium.org/"));
WaitForCoalescense();
EXPECT_EQ(1, context()->add_event_count());
EXPECT_EQ(0, context()->reset_event_count());
master()->DeleteAllURLs();
WaitForCoalescense();
EXPECT_EQ(1, context()->add_event_count());
EXPECT_EQ(1, context()->reset_event_count());
}
TEST_F(VisitedLinkEventsTest, TabVisibility) {
RenderViewHostTester::For(rvh())->CreateRenderView(base::string16(),
MSG_ROUTING_NONE,
-1);
RenderViewHostTester::For(rvh())->SimulateWasHidden();
master()->AddURL(GURL("http://acidtests.org/"));
master()->AddURL(GURL("http://google.com/"));
master()->AddURL(GURL("http://chromium.org/"));
WaitForCoalescense();
EXPECT_EQ(0, context()->add_event_count());
EXPECT_EQ(0, context()->reset_event_count());
RenderViewHostTester::For(rvh())->SimulateWasShown();
EXPECT_EQ(1, context()->add_event_count());
EXPECT_EQ(0, context()->reset_event_count());
RenderViewHostTester::For(rvh())->SimulateWasHidden();
for (int i = 0; i < 100; i++)
master()->AddURL(TestURL(i));
WaitForCoalescense();
EXPECT_EQ(1, context()->add_event_count());
EXPECT_EQ(0, context()->reset_event_count());
RenderViewHostTester::For(rvh())->SimulateWasShown();
EXPECT_EQ(1, context()->add_event_count());
EXPECT_EQ(1, context()->reset_event_count());
}
TEST_F(VisitedLinkEventsTest, IgnoreRendererCreationFromDifferentContext) {
VisitCountingContext different_context;
VisitRelayingRenderProcessHost different_process_host(&different_context);
content::NotificationService::current()->Notify(
content::NOTIFICATION_RENDERER_PROCESS_CREATED,
content::Source<content::RenderProcessHost>(&different_process_host),
content::NotificationService::NoDetails());
WaitForCoalescense();
EXPECT_EQ(0, different_context.new_table_count());
}
}