Bug 1448081 - Replaced tests using updatePlaces with PlaceTestUtils.addVisits. r=Standard8
authorsreeise <reeisesean@gmail.com>
Fri, 20 Apr 2018 11:00:17 +0100
changeset 414974 6747ccc87604
parent 414973 b154d7186790
child 414975 c4603a9efe6d
push id102449
push useredilee@gmail.com
push dateSun, 22 Apr 2018 07:36:06 +0000
treeherdermozilla-inbound@6747ccc87604 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersStandard8
bugs1448081
milestone61.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1448081 - Replaced tests using updatePlaces with PlaceTestUtils.addVisits. r=Standard8 PlacesTestUtils.addVisits provides a better api for adding history visits to the database for testing. MozReview-Commit-ID: 3VIeWz59ErM
browser/base/content/test/sanitize/browser_sanitize-timespans.js
browser/components/places/tests/browser/browser_library_downloads.js
toolkit/components/places/tests/unit/test_454977.js
toolkit/components/places/tests/unit/test_download_history.js
toolkit/components/places/tests/unit/test_isURIVisited.js
--- a/browser/base/content/test/sanitize/browser_sanitize-timespans.js
+++ b/browser/base/content/test/sanitize/browser_sanitize-timespans.js
@@ -1,10 +1,13 @@
 requestLongerTimeout(2);
 
+const {PlacesTestUtils} =
+  ChromeUtils.import("resource://testing-common/PlacesTestUtils.jsm", {});
+
 // Bug 453440 - Test the timespan-based logic of the sanitizer code
 var now_mSec = Date.now();
 var now_uSec = now_mSec * 1000;
 
 const kMsecPerMin = 60 * 1000;
 const kUsecPerMin = 60 * 1000000;
 
 function promiseFormHistoryRemoved() {
@@ -421,56 +424,47 @@ async function onHistoryReady() {
   ok(!(await PlacesUtils.history.hasVisits("http://before-today.com")),
      "Pretend visit to before-today.com should now be deleted");
 
   await countEntries("b4today", "b4today form entry should be deleted", checkZero);
 
   ok(!(await downloadExists(publicList, "fakefile-old")), "Year old download should now be deleted");
 }
 
-function setupHistory() {
-  return new Promise(resolve => {
+async function setupHistory() {
 
-    let places = [];
+  let places = [];
 
-    function addPlace(aURI, aTitle, aVisitDate) {
-      places.push({
-        uri: aURI,
-        title: aTitle,
-        visits: [{
-          visitDate: aVisitDate,
-          transitionType: Ci.nsINavHistoryService.TRANSITION_LINK
-        }]
-      });
-    }
+  function addPlace(aURI, aTitle, aVisitDate) {
+    places.push({
+      uri: aURI,
+      title: aTitle,
+      visitDate: aVisitDate,
+      transition: Ci.nsINavHistoryService.TRANSITION_LINK
+    });
+  }
 
-    addPlace(makeURI("http://10minutes.com/"), "10 minutes ago", now_uSec - 10 * kUsecPerMin);
-    addPlace(makeURI("http://1hour.com/"), "Less than 1 hour ago", now_uSec - 45 * kUsecPerMin);
-    addPlace(makeURI("http://1hour10minutes.com/"), "1 hour 10 minutes ago", now_uSec - 70 * kUsecPerMin);
-    addPlace(makeURI("http://2hour.com/"), "Less than 2 hours ago", now_uSec - 90 * kUsecPerMin);
-    addPlace(makeURI("http://2hour10minutes.com/"), "2 hours 10 minutes ago", now_uSec - 130 * kUsecPerMin);
-    addPlace(makeURI("http://4hour.com/"), "Less than 4 hours ago", now_uSec - 180 * kUsecPerMin);
-    addPlace(makeURI("http://4hour10minutes.com/"), "4 hours 10 minutesago", now_uSec - 250 * kUsecPerMin);
+  addPlace("http://10minutes.com/", "10 minutes ago", now_uSec - 10 * kUsecPerMin);
+  addPlace("http://1hour.com/", "Less than 1 hour ago", now_uSec - 45 * kUsecPerMin);
+  addPlace("http://1hour10minutes.com/", "1 hour 10 minutes ago", now_uSec - 70 * kUsecPerMin);
+  addPlace("http://2hour.com/", "Less than 2 hours ago", now_uSec - 90 * kUsecPerMin);
+  addPlace("http://2hour10minutes.com/", "2 hours 10 minutes ago", now_uSec - 130 * kUsecPerMin);
+  addPlace("http://4hour.com/", "Less than 4 hours ago", now_uSec - 180 * kUsecPerMin);
+  addPlace("http://4hour10minutes.com/", "4 hours 10 minutesago", now_uSec - 250 * kUsecPerMin);
 
-    let today = new Date();
-    today.setHours(0);
-    today.setMinutes(0);
-    today.setSeconds(1);
-    addPlace(makeURI("http://today.com/"), "Today", today.getTime() * 1000);
+  let today = new Date();
+  today.setHours(0);
+  today.setMinutes(0);
+  today.setSeconds(1);
+  addPlace("http://today.com/", "Today", today.getTime() * 1000);
 
-    let lastYear = new Date();
-    lastYear.setFullYear(lastYear.getFullYear() - 1);
-    addPlace(makeURI("http://before-today.com/"), "Before Today", lastYear.getTime() * 1000);
-    PlacesUtils.asyncHistory.updatePlaces(places, {
-      handleError: () => ok(false, "Unexpected error in adding visit."),
-      handleResult: () => { },
-      handleCompletion: () => resolve()
-    });
-
-  });
+  let lastYear = new Date();
+  lastYear.setFullYear(lastYear.getFullYear() - 1);
+  addPlace("http://before-today.com/", "Before Today", lastYear.getTime() * 1000);
+  await PlacesTestUtils.addVisits(places);
 }
 
 async function setupFormHistory() {
 
   function searchEntries(terms, params) {
     return new Promise((resolve, reject) => {
 
       let results = [];
--- a/browser/components/places/tests/browser/browser_library_downloads.js
+++ b/browser/components/places/tests/browser/browser_library_downloads.js
@@ -5,64 +5,47 @@
 /*
  * Tests bug 564900: Add folder specifically for downloads to Library left pane.
  * https://bugzilla.mozilla.org/show_bug.cgi?id=564900
  * This test visits various pages then opens the Library and ensures
  * that both the Downloads folder shows up and that the correct visits
  * are shown in it.
  */
 
-var now = Date.now();
+add_task(async function test() {
+  // Add visits.
+  await PlacesTestUtils.addVisits([{
+    uri: "http://mozilla.org",
+    transition: PlacesUtils.history.TRANSITION_TYPED
+  }, {
+    uri: "http://google.com",
+    transition: PlacesUtils.history.TRANSITION_DOWNLOAD
+  }, {
+    uri: "http://en.wikipedia.org",
+    transition: PlacesUtils.history.TRANSITION_TYPED
+  }, {
+    uri: "http://ubuntu.org",
+    transition: PlacesUtils.history.TRANSITION_DOWNLOAD
+  }]);
 
-function test() {
-  waitForExplicitFinish();
+  let library = await promiseLibrary("Downloads");
 
-  let onLibraryReady = function(win) {
-    // Add visits to compare contents with.
-    let places = [
-      { uri: NetUtil.newURI("http://mozilla.com"),
-        visits: [ new VisitInfo(PlacesUtils.history.TRANSITION_TYPED) ]
-      },
-      { uri: NetUtil.newURI("http://google.com"),
-        visits: [ new VisitInfo(PlacesUtils.history.TRANSITION_DOWNLOAD) ]
-      },
-      { uri: NetUtil.newURI("http://en.wikipedia.org"),
-        visits: [ new VisitInfo(PlacesUtils.history.TRANSITION_TYPED) ]
-      },
-      { uri: NetUtil.newURI("http://ubuntu.org"),
-        visits: [ new VisitInfo(PlacesUtils.history.TRANSITION_DOWNLOAD) ]
-      },
-    ];
-    PlacesUtils.asyncHistory.updatePlaces(places, {
-      handleResult() {},
-      handleError() {
-        ok(false, "gHistory.updatePlaces() failed");
-      },
-      handleCompletion() {
-        // Make sure Downloads is present.
-        isnot(win.PlacesOrganizer._places.selectedNode, null,
-              "Downloads is present and selected");
+  registerCleanupFunction(async () => {
+    await library.close();
+    await PlacesUtils.history.clear();
+  });
+
+  // Make sure Downloads is present.
+  Assert.notEqual(library.PlacesOrganizer._places.selectedNode, null,
+    "Downloads is present and selected");
 
+  // Check results.
+  let testURIs = ["http://ubuntu.org/", "http://google.com/"];
 
-        // Check results.
-        let testURIs = ["http://ubuntu.org/", "http://google.com/"];
-        for (let element of win.ContentArea.currentView
-                                           .associatedElement.children) {
-          is(element._shell.download.source.url, testURIs.shift(),
-             "URI matches");
-        }
+  await BrowserTestUtils.waitForCondition(() =>
+    library.ContentArea.currentView.associatedElement.children.length == testURIs.length);
 
-        win.close();
-        PlacesUtils.history.clear().then(finish);
-      }
-    });
-  };
-
-  openLibrary(onLibraryReady, "Downloads");
-}
-
-function VisitInfo(aTransitionType) {
-  this.transitionType =
-    aTransitionType === undefined ?
-      PlacesUtils.history.TRANSITION_LINK : aTransitionType;
-  this.visitDate = now++ * 1000;
-}
-VisitInfo.prototype = {};
+  for (let element of library.ContentArea.currentView
+                                          .associatedElement.children) {
+    Assert.equal(element._shell.download.source.url, testURIs.shift(),
+      "URI matches");
+  }
+});
--- a/toolkit/components/places/tests/unit/test_454977.js
+++ b/toolkit/components/places/tests/unit/test_454977.js
@@ -4,35 +4,33 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 // Cache actual visit_count value, filled by add_visit, used by check_results
 var visit_count = 0;
 
 // Returns the Place ID corresponding to an added visit.
 async function task_add_visit(aURI, aVisitType) {
-  // Add the visit asynchronously, and save its visit ID.
-  let deferUpdatePlaces = new Promise((resolve, reject) => {
-    PlacesUtils.asyncHistory.updatePlaces({
-      uri: aURI,
-      visits: [{ transitionType: aVisitType, visitDate: Date.now() * 1000 }]
-    }, {
-      handleError: function TAV_handleError() {
-        reject(new Error("Unexpected error in adding visit."));
-      },
-      handleResult(aPlaceInfo) {
-        this.visitId = aPlaceInfo.visits[0].visitId;
-      },
-      handleCompletion: function TAV_handleCompletion() {
-        resolve(this.visitId);
-      }
-    });
-  });
+  // Wait for a visits notification and get the visitId.
+  let visitId;
+  let visitsPromise = PlacesTestUtils.waitForNotification("onVisits", visits => {
+    visitId = visits[0].visitId;
+    let {uri} = visits[0];
+    return uri.equals(aURI);
+  }, "history");
 
-  let visitId = await deferUpdatePlaces;
+  // Add visits.
+  await PlacesTestUtils.addVisits([{
+    uri: aURI,
+    transition: aVisitType
+  }]);
+
+  if (aVisitType != TRANSITION_EMBED) {
+    await visitsPromise;
+  }
 
   // Increase visit_count if applicable
   if (aVisitType != 0 &&
       aVisitType != TRANSITION_EMBED &&
       aVisitType != TRANSITION_FRAMED_LINK &&
       aVisitType != TRANSITION_DOWNLOAD &&
       aVisitType != TRANSITION_RELOAD) {
     visit_count++;
--- a/toolkit/components/places/tests/unit/test_download_history.js
+++ b/toolkit/components/places/tests/unit/test_download_history.js
@@ -149,44 +149,51 @@ add_task(async function test_dh_addBookm
       });
       gDownloadHistory.removeAllDownloads();
     });
 
     gDownloadHistory.addDownload(DOWNLOAD_URI, null, Date.now() * 1000);
   });
 });
 
-add_test(function test_dh_addDownload_referrer() {
-  waitForOnVisit(function DHAD_prepareReferrer(aURI, aVisitID) {
-    Assert.ok(aURI.equals(REFERRER_URI));
-    let referrerVisitId = aVisitID;
+add_task(async function test_dh_addDownload_referrer() {
+  // Wait for visits notification and get the visit id.
+  let visitId;
+  let referrerPromise = PlacesTestUtils.waitForNotification("onVisits", visits => {
+    visitId = visits[0].visitId;
+    let {uri} = visits[0];
+    return uri.equals(REFERRER_URI);
+  }, "history");
 
-    waitForOnVisit(function DHAD_onVisit(aVisitedURI, unused, unused2, unused3,
-                                         aReferringID) {
-      Assert.ok(aVisitedURI.equals(DOWNLOAD_URI));
-      Assert.equal(aReferringID, referrerVisitId);
-
-      // Verify that the URI is already available in results at this time.
-      Assert.ok(!!page_in_database(DOWNLOAD_URI));
+  await PlacesTestUtils.addVisits([{
+    uri: REFERRER_URI,
+    transition: Ci.nsINavHistoryService.TRANSITION_TYPED
+  }]);
+  await referrerPromise;
 
-      PlacesUtils.history.clear().then(run_next_test);
-    });
-
-    gDownloadHistory.addDownload(DOWNLOAD_URI, REFERRER_URI, Date.now() * 1000);
-  });
+  // Verify results for referrer uri.
+  Assert.ok(!!PlacesTestUtils.isPageInDB(REFERRER_URI));
+  Assert.equal(visitId, 1);
 
-  // Note that we don't pass the optional callback argument here because we must
-  // ensure that we receive the onVisits notification before we call addDownload.
-  PlacesUtils.asyncHistory.updatePlaces({
-    uri: REFERRER_URI,
-    visits: [{
-      transitionType: Ci.nsINavHistoryService.TRANSITION_TYPED,
-      visitDate: Date.now() * 1000
-    }]
-  });
+  // Wait for visits notification and get the referrer Id.
+  let referrerId;
+  let downloadPromise = PlacesTestUtils.waitForNotification("onVisits", visits => {
+    referrerId = visits[0].referrerId;
+    let {uri} = visits[0];
+    return uri.equals(DOWNLOAD_URI);
+  }, "history");
+
+  gDownloadHistory.addDownload(DOWNLOAD_URI, REFERRER_URI, Date.now() * 1000);
+  await downloadPromise;
+
+  // Verify results for download uri.
+  Assert.ok(!!PlacesTestUtils.isPageInDB(DOWNLOAD_URI));
+  Assert.equal(visitId, referrerId);
+
+  await PlacesUtils.history.clear();
 });
 
 add_test(function test_dh_addDownload_disabledHistory() {
   waitForOnVisit(function DHAD_onVisit(aURI) {
     // We should only receive the notification for the non-private URI.  This
     // test is based on the assumption that visit notifications are received in
     // the same order of the addDownload calls, which is currently true because
     // database access is serialized on the same worker thread.
--- a/toolkit/components/places/tests/unit/test_isURIVisited.js
+++ b/toolkit/components/places/tests/unit/test_isURIVisited.js
@@ -18,64 +18,51 @@ const SCHEMES = {
   "view-source:http://": false,
   "chrome://browser/content/browser.xul?": false,
   "resource://": false,
   "data:,": false,
   "wyciwyg:/0/http://": false,
   "javascript:": false,
 };
 
-var gRunner;
-function run_test() {
-  do_test_pending();
-  gRunner = step();
-  gRunner.next();
-}
-
-function* step() {
+add_task(async function test_isURIVisited() {
   let history = Cc["@mozilla.org/browser/history;1"]
                   .getService(Ci.mozIAsyncHistory);
 
+  function visitsPromise(uri) {
+    return new Promise(resolve => {
+      history.isURIVisited(uri, (receivedURI, visited) => {
+        resolve([receivedURI, visited]);
+      });
+    });
+  }
+
   for (let scheme in SCHEMES) {
     info("Testing scheme " + scheme);
     for (let t in PlacesUtils.history.TRANSITIONS) {
       info("With transition " + t);
-      let transition = PlacesUtils.history.TRANSITIONS[t];
-
-      let uri = NetUtil.newURI(scheme + "mozilla.org/");
+      let aTransition = PlacesUtils.history.TRANSITIONS[t];
 
-      history.isURIVisited(uri, function(aURI, aIsVisited) {
-        Assert.ok(uri.equals(aURI));
-        Assert.ok(!aIsVisited);
+      let aURI = Services.io.newURI(scheme + "mozilla.org/");
 
-        let callback = {
-          handleError() {},
-          handleResult() {},
-          handleCompletion() {
-            info("Added visit to " + uri.spec);
-
-            history.isURIVisited(uri, function(aURI2, aIsVisited2) {
-              Assert.ok(uri.equals(aURI2));
-              Assert.ok(SCHEMES[scheme] ? aIsVisited2 : !aIsVisited2);
+      let [receivedURI1, visited1] = await visitsPromise(aURI);
+      Assert.ok(aURI.equals(receivedURI1));
+      Assert.ok(!visited1);
 
-              PlacesUtils.history.clear().then(function() {
-                history.isURIVisited(uri, function(aURI3, aIsVisited3) {
-                  Assert.ok(uri.equals(aURI3));
-                  Assert.ok(!aIsVisited3);
-                  gRunner.next();
-                });
-              });
-            });
-          },
-        };
+      if (PlacesUtils.history.canAddURI(aURI)) {
+        await PlacesTestUtils.addVisits([{
+          uri: aURI,
+          transition: aTransition
+        }]);
+        info("Added visit for " + aURI.spec);
+      }
 
-        history.updatePlaces({ uri,
-                               visits: [ { transitionType: transition,
-                                           visitDate:      Date.now() * 1000
-                                         } ]
-                             }, callback);
-      });
-      yield undefined;
+      let [receivedURI2, visited2] = await visitsPromise(aURI);
+      Assert.ok(aURI.equals(receivedURI2));
+      Assert.equal(SCHEMES[scheme], visited2);
+
+      await PlacesUtils.history.clear();
+      let [receivedURI3, visited3] = await visitsPromise(aURI);
+      Assert.ok(aURI.equals(receivedURI3));
+      Assert.ok(!visited3);
     }
   }
-
-  do_test_finished();
-}
+});