diff options
author | sath <sath@yandex-team.ru> | 2015-12-15 07:10:42 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-12-15 15:11:38 +0000 |
commit | af37203df5ddaf2b1491748ca4c0428a415e0f88 (patch) | |
tree | 862f51d80de34888df4bc8e1eeed4e3b251ede67 /components/visitedlink/test/visitedlink_unittest.cc | |
parent | b356865f83a9de400dc565ab5385b8b643e22116 (diff) | |
download | chromium_src-af37203df5ddaf2b1491748ca4c0428a415e0f88.zip chromium_src-af37203df5ddaf2b1491748ca4c0428a415e0f88.tar.gz chromium_src-af37203df5ddaf2b1491748ca4c0428a415e0f88.tar.bz2 |
Load the table of visited links from database file asynchronously.
Remove access to the database file from the UI thread and reduce
startup browser delays.
R=sky@chromium.org
BUG=24163
BUG=45591
Review URL: https://codereview.chromium.org/1417943002
Cr-Commit-Position: refs/heads/master@{#365250}
Diffstat (limited to 'components/visitedlink/test/visitedlink_unittest.cc')
-rw-r--r-- | components/visitedlink/test/visitedlink_unittest.cc | 163 |
1 files changed, 136 insertions, 27 deletions
diff --git a/components/visitedlink/test/visitedlink_unittest.cc b/components/visitedlink/test/visitedlink_unittest.cc index 3fc25ce..89dad11 100644 --- a/components/visitedlink/test/visitedlink_unittest.cc +++ b/components/visitedlink/test/visitedlink_unittest.cc @@ -107,6 +107,7 @@ class TrackingVisitedLinkEventListener : public VisitedLinkMaster::Listener { public: TrackingVisitedLinkEventListener() : reset_count_(0), + completely_reset_count_(0), add_count_(0) {} void NewTable(base::SharedMemory* table) override { @@ -120,7 +121,12 @@ class TrackingVisitedLinkEventListener : public VisitedLinkMaster::Listener { } } void Add(VisitedLinkCommon::Fingerprint) override { add_count_++; } - void Reset() override { reset_count_++; } + void Reset(bool invalidate_hashes) override { + if (invalidate_hashes) + completely_reset_count_++; + else + reset_count_++; + } void SetUp() { reset_count_ = 0; @@ -128,10 +134,12 @@ class TrackingVisitedLinkEventListener : public VisitedLinkMaster::Listener { } int reset_count() const { return reset_count_; } + int completely_reset_count() { return completely_reset_count_; } int add_count() const { return add_count_; } private: int reset_count_; + int completely_reset_count_; int add_count_; }; @@ -142,14 +150,23 @@ class VisitedLinkTest : public testing::Test { // // |suppress_rebuild| is set when we're not testing rebuilding, see // the VisitedLinkMaster constructor. - bool InitVisited(int initial_size, bool suppress_rebuild) { + // + // |wait_for_io_complete| wait for result of async loading. + bool InitVisited(int initial_size, + bool suppress_rebuild, + bool wait_for_io_complete) { // Initialize the visited link system. master_.reset(new VisitedLinkMaster(new TrackingVisitedLinkEventListener(), &delegate_, true, suppress_rebuild, visited_file_, initial_size)); - return master_->Init(); + bool result = master_->Init(); + if (result && wait_for_io_complete) { + // Wait for all pending file I/O to be completed. + content::RunAllBlockingPoolTasksUntilIdle(); + } + return result; } // May be called multiple times (some tests will do this to clear things, @@ -169,7 +186,8 @@ class VisitedLinkTest : public testing::Test { // Clean up after our caller, who may have left the database open. ClearDB(); - ASSERT_TRUE(InitVisited(0, true)); + ASSERT_TRUE(InitVisited(0, true, true)); + master_->DebugValidate(); // check that the table has the proper number of entries @@ -231,7 +249,7 @@ class VisitedLinkTest : public testing::Test { // This test creates and reads some databases to make sure the data is // preserved throughout those operations. TEST_F(VisitedLinkTest, DatabaseIO) { - ASSERT_TRUE(InitVisited(0, true)); + ASSERT_TRUE(InitVisited(0, true, true)); for (int i = 0; i < g_test_count; i++) master_->AddURL(TestURL(i)); @@ -243,7 +261,7 @@ TEST_F(VisitedLinkTest, DatabaseIO) { // Checks that we can delete things properly when there are collisions. TEST_F(VisitedLinkTest, Delete) { static const int32 kInitialSize = 17; - ASSERT_TRUE(InitVisited(kInitialSize, true)); + ASSERT_TRUE(InitVisited(kInitialSize, true, true)); // Add a cluster from 14-17 wrapping around to 0. These will all hash to the // same value. @@ -281,7 +299,7 @@ TEST_F(VisitedLinkTest, Delete) { // When we delete more than kBigDeleteThreshold we trigger different behavior // where the entire file is rewritten. TEST_F(VisitedLinkTest, BigDelete) { - ASSERT_TRUE(InitVisited(16381, true)); + ASSERT_TRUE(InitVisited(16381, true, true)); // Add the base set of URLs that won't be deleted. // Reload() will test for these. @@ -305,7 +323,7 @@ TEST_F(VisitedLinkTest, BigDelete) { } TEST_F(VisitedLinkTest, DeleteAll) { - ASSERT_TRUE(InitVisited(0, true)); + ASSERT_TRUE(InitVisited(0, true, true)); { VisitedLinkSlave slave; @@ -340,7 +358,7 @@ TEST_F(VisitedLinkTest, DeleteAll) { } // Reopen and validate. - ASSERT_TRUE(InitVisited(0, true)); + ASSERT_TRUE(InitVisited(0, true, true)); master_->DebugValidate(); EXPECT_EQ(0, master_->GetUsedCount()); for (int i = 0; i < g_test_count; i++) @@ -352,7 +370,7 @@ TEST_F(VisitedLinkTest, DeleteAll) { TEST_F(VisitedLinkTest, Resizing) { // Create a very small database. const int32 initial_size = 17; - ASSERT_TRUE(InitVisited(initial_size, true)); + ASSERT_TRUE(InitVisited(initial_size, true, true)); // ...and a slave VisitedLinkSlave slave; @@ -408,7 +426,7 @@ TEST_F(VisitedLinkTest, Rebuild) { // Initialize the visited link DB. Since the visited links file doesn't exist // and we don't suppress history rebuilding, this will load from history. - ASSERT_TRUE(InitVisited(0, false)); + ASSERT_TRUE(InitVisited(0, false, false)); // While the table is rebuilding, add the rest of the URLs to the visited // link system. This isn't guaranteed to happen during the rebuild, so we @@ -445,7 +463,7 @@ TEST_F(VisitedLinkTest, Rebuild) { // Test that importing a large number of URLs will work TEST_F(VisitedLinkTest, BigImport) { - ASSERT_TRUE(InitVisited(0, false)); + ASSERT_TRUE(InitVisited(0, false, false)); // Before the table rebuilds, add a large number of URLs int total_count = VisitedLinkMaster::kDefaultTableSize + 10; @@ -463,7 +481,14 @@ TEST_F(VisitedLinkTest, BigImport) { } TEST_F(VisitedLinkTest, Listener) { - ASSERT_TRUE(InitVisited(0, true)); + ASSERT_TRUE(InitVisited(0, true, true)); + + TrackingVisitedLinkEventListener* listener = + static_cast<TrackingVisitedLinkEventListener*>(master_->GetListener()); + + // Verify that VisitedLinkMaster::Listener::Reset(true) was never called when + // the table was created. + EXPECT_EQ(0, listener->completely_reset_count()); // Add test URLs. for (int i = 0; i < g_test_count; i++) { @@ -480,14 +505,21 @@ TEST_F(VisitedLinkTest, Listener) { // ... and all of the remaining ones. master_->DeleteAllURLs(); - TrackingVisitedLinkEventListener* listener = - static_cast<TrackingVisitedLinkEventListener*>(master_->GetListener()); - // Verify that VisitedLinkMaster::Listener::Add was called for each added URL. EXPECT_EQ(g_test_count, listener->add_count()); // Verify that VisitedLinkMaster::Listener::Reset was called both when one and // all URLs are deleted. EXPECT_EQ(2, listener->reset_count()); + + ClearDB(); + + ASSERT_TRUE(InitVisited(0, true, true)); + + listener = + static_cast<TrackingVisitedLinkEventListener*>(master_->GetListener()); + // Verify that VisitedLinkMaster::Listener::Reset(true) was called when the + // table was loaded. + EXPECT_EQ(1, listener->completely_reset_count()); } class VisitCountingContext : public content::TestBrowserContext { @@ -496,6 +528,7 @@ class VisitCountingContext : public content::TestBrowserContext { : add_count_(0), add_event_count_(0), reset_event_count_(0), + completely_reset_event_count_(0), new_table_count_(0) {} void CountAddEvent(int by) { @@ -507,6 +540,10 @@ class VisitCountingContext : public content::TestBrowserContext { reset_event_count_++; } + void CountCompletelyResetEvent() { + completely_reset_event_count_++; + } + void CountNewTable() { new_table_count_++; } @@ -514,12 +551,16 @@ class VisitCountingContext : public content::TestBrowserContext { 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 completely_reset_event_count() const { + return completely_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 completely_reset_event_count_; int new_table_count_; }; @@ -556,7 +597,13 @@ class VisitRelayingRenderProcessHost : public MockRenderProcessHost { CHECK(IPC::ReadParam(msg, &iter, &fingerprints)); counting_context->CountAddEvent(fingerprints.size()); } else if (msg->type() == ChromeViewMsg_VisitedLink_Reset::ID) { - counting_context->CountResetEvent(); + base::PickleIterator iter(*msg); + bool invalidate_hashes; + CHECK(IPC::ReadParam(msg, &iter, &invalidate_hashes)); + if (invalidate_hashes) + counting_context->CountCompletelyResetEvent(); + else + counting_context->CountResetEvent(); } else if (msg->type() == ChromeViewMsg_VisitedLink_NewTable::ID) { counting_context->CountNewTable(); } @@ -605,8 +652,7 @@ class VisitedLinkEventsTest : public content::RenderViewHostTestHarness { content::BrowserContext* CreateBrowserContext() override { VisitCountingContext* context = new VisitCountingContext(); - master_.reset(new VisitedLinkMaster(context, &delegate_, true)); - master_->Init(); + CreateVisitedLinkMaster(context); return context; } @@ -630,14 +676,26 @@ class VisitedLinkEventsTest : public content::RenderViewHostTestHarness { } protected: + void CreateVisitedLinkMaster(content::BrowserContext* browser_context) { + master_.reset(new VisitedLinkMaster(browser_context, &delegate_, true)); + master_->Init(); + } + VisitedLinkRenderProcessHostFactory vc_rph_factory_; - private: TestVisitedLinkDelegate delegate_; scoped_ptr<VisitedLinkMaster> master_; }; TEST_F(VisitedLinkEventsTest, Coalescence) { + // Waiting complete rebuild the table. + content::RunAllBlockingPoolTasksUntilIdle(); + + WaitForCoalescence(); + + // After rebuild table expect reset event. + EXPECT_EQ(1, context()->reset_event_count()); + // add some URLs to master. // Add a few URLs. master()->AddURL(GURL("http://acidtests.org/")); @@ -686,13 +744,21 @@ TEST_F(VisitedLinkEventsTest, Coalescence) { // We should have no change in results except for one new reset event. EXPECT_EQ(6, context()->add_count()); EXPECT_EQ(2, context()->add_event_count()); - EXPECT_EQ(1, context()->reset_event_count()); + EXPECT_EQ(2, context()->reset_event_count()); } TEST_F(VisitedLinkEventsTest, Basics) { RenderViewHostTester::For(rvh())->CreateTestRenderView( base::string16(), MSG_ROUTING_NONE, MSG_ROUTING_NONE, -1, false); + // Waiting complete rebuild the table. + content::RunAllBlockingPoolTasksUntilIdle(); + + WaitForCoalescence(); + + // After rebuild table expect reset event. + EXPECT_EQ(1, context()->reset_event_count()); + // Add a few URLs. master()->AddURL(GURL("http://acidtests.org/")); master()->AddURL(GURL("http://google.com/")); @@ -702,7 +768,7 @@ TEST_F(VisitedLinkEventsTest, Basics) { // We now should have 1 add event. EXPECT_EQ(1, context()->add_event_count()); - EXPECT_EQ(0, context()->reset_event_count()); + EXPECT_EQ(1, context()->reset_event_count()); master()->DeleteAllURLs(); @@ -710,13 +776,21 @@ TEST_F(VisitedLinkEventsTest, Basics) { // We should have no change in add results, plus one new reset event. EXPECT_EQ(1, context()->add_event_count()); - EXPECT_EQ(1, context()->reset_event_count()); + EXPECT_EQ(2, context()->reset_event_count()); } TEST_F(VisitedLinkEventsTest, TabVisibility) { RenderViewHostTester::For(rvh())->CreateTestRenderView( base::string16(), MSG_ROUTING_NONE, MSG_ROUTING_NONE, -1, false); + // Waiting complete rebuild the table. + content::RunAllBlockingPoolTasksUntilIdle(); + + WaitForCoalescence(); + + // After rebuild table expect reset event. + EXPECT_EQ(1, context()->reset_event_count()); + // Simulate tab becoming inactive. RenderViewHostTester::For(rvh())->SimulateWasHidden(); @@ -729,14 +803,14 @@ TEST_F(VisitedLinkEventsTest, TabVisibility) { // We shouldn't have any events. EXPECT_EQ(0, context()->add_event_count()); - EXPECT_EQ(0, context()->reset_event_count()); + EXPECT_EQ(1, context()->reset_event_count()); // Simulate the tab becoming active. RenderViewHostTester::For(rvh())->SimulateWasShown(); // We should now have 3 add events, still no reset events. EXPECT_EQ(1, context()->add_event_count()); - EXPECT_EQ(0, context()->reset_event_count()); + EXPECT_EQ(1, context()->reset_event_count()); // Deactivate the tab again. RenderViewHostTester::For(rvh())->SimulateWasHidden(); @@ -749,14 +823,14 @@ TEST_F(VisitedLinkEventsTest, TabVisibility) { // Again, no change in events until tab is active. EXPECT_EQ(1, context()->add_event_count()); - EXPECT_EQ(0, context()->reset_event_count()); + EXPECT_EQ(1, context()->reset_event_count()); // Activate the tab. RenderViewHostTester::For(rvh())->SimulateWasShown(); // We should have only one more reset event. EXPECT_EQ(1, context()->add_event_count()); - EXPECT_EQ(1, context()->reset_event_count()); + EXPECT_EQ(2, context()->reset_event_count()); } // Tests that VisitedLink ignores renderer process creation notification for a @@ -774,4 +848,39 @@ TEST_F(VisitedLinkEventsTest, IgnoreRendererCreationFromDifferentContext) { EXPECT_EQ(0, different_context.new_table_count()); } +class VisitedLinkCompletelyResetEventTest : public VisitedLinkEventsTest { + public: + content::BrowserContext* CreateBrowserContext() override { + VisitCountingContext* context = new VisitCountingContext(); + CreateVisitedLinkFile(context); + CreateVisitedLinkMaster(context); + return context; + } + + void CreateVisitedLinkFile(content::BrowserContext* browser_context) { + base::FilePath visited_file = + browser_context->GetPath().Append(FILE_PATH_LITERAL("Visited Links")); + scoped_ptr<VisitedLinkMaster> master( + new VisitedLinkMaster(new TrackingVisitedLinkEventListener(), + &delegate_, true, true, visited_file, 0)); + master->Init(); + // Waiting complete create the table. + content::RunAllBlockingPoolTasksUntilIdle(); + + master.reset(); + // Wait for all pending file I/O to be completed. + content::RunAllBlockingPoolTasksUntilIdle(); + } +}; + +TEST_F(VisitedLinkCompletelyResetEventTest, LoadTable) { + // Waiting complete loading the table. + content::RunAllBlockingPoolTasksUntilIdle(); + + WaitForCoalescence(); + + // After load table expect completely reset event. + EXPECT_EQ(1, context()->completely_reset_event_count()); +} + } // namespace visitedlink |