Fix memory leak in VisitedLink tests

[email protected] (VisitedLink OWNERS)

BUG=157669


Review URL: https://chromiumcodereview.appspot.com/11274030

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@164454 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/visitedlink/visitedlink_unittest.cc b/chrome/browser/visitedlink/visitedlink_unittest.cc
index 67031497..4056110 100644
--- a/chrome/browser/visitedlink/visitedlink_unittest.cc
+++ b/chrome/browser/visitedlink/visitedlink_unittest.cc
@@ -566,8 +566,9 @@
   virtual void SetUp() {
     browser_context_.reset(new VisitCountingProfile());
     profile()->CreateHistoryService(true, false);
-    VisitedLinkMasterFactory::GetInstance()->SetTestingFactoryAndUse(profile(),
-        BuildVisitedLinkMaster);
+    master_ = static_cast<VisitedLinkMaster*>(
+        VisitedLinkMasterFactory::GetInstance()->
+            SetTestingFactoryAndUse(profile(), BuildVisitedLinkMaster));
     SetRenderProcessHostFactory(&vc_rph_factory_);
     ChromeRenderViewHostTestHarness::SetUp();
   }
@@ -576,6 +577,10 @@
     return static_cast<VisitCountingProfile*>(browser_context_.get());
   }
 
+  VisitedLinkMaster* master() const {
+    return master_;
+  }
+
   void WaitForCoalescense() {
     // Let the timer fire.
     MessageLoop::current()->PostDelayedTask(
@@ -589,6 +594,7 @@
   VisitedLinkRenderProcessHostFactory vc_rph_factory_;
 
  private:
+  VisitedLinkMaster* master_;
   content::TestBrowserThread ui_thread_;
   content::TestBrowserThread file_thread_;
 
@@ -597,13 +603,12 @@
 
 TEST_F(VisitedLinkEventsTest, Coalescense) {
   // add some URLs to master.
-  VisitedLinkMaster* master = VisitedLinkMaster::FromProfile(profile());
   // Add a few URLs.
-  master->AddURL(GURL("http://acidtests.org/"));
-  master->AddURL(GURL("http://google.com/"));
-  master->AddURL(GURL("http://chromium.org/"));
+  master()->AddURL(GURL("http://acidtests.org/"));
+  master()->AddURL(GURL("http://google.com/"));
+  master()->AddURL(GURL("http://chromium.org/"));
   // Just for kicks, add a duplicate URL. This shouldn't increase the resulting
-  master->AddURL(GURL("http://acidtests.org/"));
+  master()->AddURL(GURL("http://acidtests.org/"));
 
   // Make sure that coalescing actually occurs. There should be no links or
   // events received by the renderer.
@@ -617,9 +622,9 @@
   EXPECT_EQ(1, profile()->add_event_count());
 
   // Test whether the coalescing continues by adding a few more URLs.
-  master->AddURL(GURL("http://google.com/chrome/"));
-  master->AddURL(GURL("http://webkit.org/"));
-  master->AddURL(GURL("http://acid3.acidtests.org/"));
+  master()->AddURL(GURL("http://google.com/chrome/"));
+  master()->AddURL(GURL("http://webkit.org/"));
+  master()->AddURL(GURL("http://acid3.acidtests.org/"));
 
   WaitForCoalescense();
 
@@ -628,7 +633,7 @@
   EXPECT_EQ(2, profile()->add_event_count());
 
   // Test whether duplicate entries produce add events.
-  master->AddURL(GURL("http://acidtests.org/"));
+  master()->AddURL(GURL("http://acidtests.org/"));
 
   WaitForCoalescense();
 
@@ -637,8 +642,8 @@
   EXPECT_EQ(2, profile()->add_event_count());
 
   // Ensure that the coalescing does not resume after resetting.
-  master->AddURL(GURL("http://build.chromium.org/"));
-  master->DeleteAllURLs();
+  master()->AddURL(GURL("http://build.chromium.org/"));
+  master()->DeleteAllURLs();
 
   WaitForCoalescense();
 
@@ -649,15 +654,14 @@
 }
 
 TEST_F(VisitedLinkEventsTest, Basics) {
-  VisitedLinkMaster* master = VisitedLinkMaster::FromProfile(profile());
   rvh_tester()->CreateRenderView(string16(),
                                  MSG_ROUTING_NONE,
                                  -1);
 
   // Add a few URLs.
-  master->AddURL(GURL("http://acidtests.org/"));
-  master->AddURL(GURL("http://google.com/"));
-  master->AddURL(GURL("http://chromium.org/"));
+  master()->AddURL(GURL("http://acidtests.org/"));
+  master()->AddURL(GURL("http://google.com/"));
+  master()->AddURL(GURL("http://chromium.org/"));
 
   WaitForCoalescense();
 
@@ -665,7 +669,7 @@
   EXPECT_EQ(1, profile()->add_event_count());
   EXPECT_EQ(0, profile()->reset_event_count());
 
-  master->DeleteAllURLs();
+  master()->DeleteAllURLs();
 
   WaitForCoalescense();
 
@@ -675,7 +679,6 @@
 }
 
 TEST_F(VisitedLinkEventsTest, TabVisibility) {
-  VisitedLinkMaster* master = VisitedLinkMaster::FromProfile(profile());
   rvh_tester()->CreateRenderView(string16(),
                                  MSG_ROUTING_NONE,
                                  -1);
@@ -684,9 +687,9 @@
   rvh_tester()->SimulateWasHidden();
 
   // Add a few URLs.
-  master->AddURL(GURL("http://acidtests.org/"));
-  master->AddURL(GURL("http://google.com/"));
-  master->AddURL(GURL("http://chromium.org/"));
+  master()->AddURL(GURL("http://acidtests.org/"));
+  master()->AddURL(GURL("http://google.com/"));
+  master()->AddURL(GURL("http://chromium.org/"));
 
   WaitForCoalescense();
 
@@ -706,7 +709,7 @@
 
   // Add a bunch of URLs (over 50) to exhaust the link event buffer.
   for (int i = 0; i < 100; i++)
-    master->AddURL(TestURL(i));
+    master()->AddURL(TestURL(i));
 
   WaitForCoalescense();