Bug 1339061 - Disable frecency decay in browser_visited_notfound.js to avoid intermittent failures. r=standard8 draft
authorMarco Bonardo <mbonardo@mozilla.com>
Mon, 20 Feb 2017 15:25:29 +0100
changeset 487065 c6bd6083d1fd109cec94320206f1de7e4726fdf8
parent 484601 c0807d6938c13e43add377d5838df7168a59971e
child 546369 eb844b829f13c3f4f6171242aa085fed77718632
push id46117
push usermak77@bonardo.net
push dateMon, 20 Feb 2017 14:27:43 +0000
reviewersstandard8
bugs1339061
milestone54.0a1
Bug 1339061 - Disable frecency decay in browser_visited_notfound.js to avoid intermittent failures. r=standard8 MozReview-Commit-ID: G0jSTXE69aa
toolkit/components/places/tests/PlacesTestUtils.jsm
toolkit/components/places/tests/browser/browser_visited_notfound.js
toolkit/components/places/tests/unit/test_frecency_decay.js
--- a/toolkit/components/places/tests/PlacesTestUtils.jsm
+++ b/toolkit/components/places/tests/PlacesTestUtils.jsm
@@ -20,17 +20,17 @@ XPCOMUtils.defineLazyModuleGetter(this, 
 this.PlacesTestUtils = Object.freeze({
   /**
    * Asynchronously adds visits to a page.
    *
    * @param aPlaceInfo
    *        Can be an nsIURI, in such a case a single LINK visit will be added.
    *        Otherwise can be an object describing the visit to add, or an array
    *        of these objects:
-   *          { uri: nsIURI of the page,
+   *          { uri: href, URL or nsIURI of the page,
    *            [optional] transition: one of the TRANSITION_* from nsINavHistoryService,
    *            [optional] title: title of the page,
    *            [optional] visitDate: visit date, either in microseconds from the epoch or as a date object
    *            [optional] referrer: nsIURI of the referrer for this visit
    *          }
    *
    * @return {Promise}
    * @resolves When all visits have been added successfully.
@@ -156,29 +156,29 @@ this.PlacesTestUtils = Object.freeze({
       `SELECT count(*) FROM moz_historyvisits v
        JOIN moz_places h ON h.id = v.place_id
        WHERE url_hash = hash(:url) AND url = :url`,
       { url });
     return rows[0].getResultByIndex(0);
   }),
 
   /**
-   * Asynchronously checks the frecency for a specified page.
+   * Asynchronously returns the required DB field for a specified page.
    * @param aURI
    *        nsIURI or address to look for.
    *
    * @return {Promise}
-   * @resolves Returns the frecency.
+   * @resolves Returns the field value.
    * @rejects JavaScript exception.
    */
-  frecencyInDB: Task.async(function* (aURI) {
+  fieldInDB: Task.async(function* (aURI, field) {
     let url = aURI instanceof Ci.nsIURI ? new URL(aURI.spec) : new URL(aURI);
     let db = yield PlacesUtils.promiseDBConnection();
     let rows = yield db.executeCached(
-      `SELECT frecency FROM moz_places
+      `SELECT ${field} FROM moz_places
        WHERE url_hash = hash(:url) AND url = :url`,
       { url: url.href });
     return rows[0].getResultByIndex(0);
   }),
 
   /**
    * Marks all syncable bookmarks as synced by setting their sync statuses to
    * "NORMAL", resetting their change counters, and removing all tombstones.
--- a/toolkit/components/places/tests/browser/browser_visited_notfound.js
+++ b/toolkit/components/places/tests/browser/browser_visited_notfound.js
@@ -1,51 +1,39 @@
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * 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/. */
-
-const TEST_URI = NetUtil.newURI("http://mochi.test:8888/notFoundPage.html");
-
-function test() {
-  waitForExplicitFinish();
-
-  gBrowser.selectedTab = gBrowser.addTab();
-  registerCleanupFunction(function() {
-    gBrowser.removeCurrentTab();
+add_task(function* test() {
+  const TEST_URL = "http://mochi.test:8888/notFoundPage.html";
+  // Ensure that decay frecency doesn't kick in during tests (as a result
+  // of idle-daily).
+  Services.prefs.setCharPref("places.frecency.decayRate", "1.0");
+  let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser);
+  registerCleanupFunction(function*() {
+    Services.prefs.clearUserPref("places.frecency.decayRate");
+    yield BrowserTestUtils.removeTab(tab);
+    yield PlacesTestUtils.clearHistory();
   });
 
   // First add a visit to the page, this will ensure that later we skip
   // updating the frecency for a newly not-found page.
-  addVisits({ uri: TEST_URI }, window, () => {
-    info("Added visit");
-    fieldForUrl(TEST_URI, "frecency", aFrecency => {
-      ok(aFrecency > 0, "Frecency should be > 0");
-      continueTest(aFrecency);
-    });
-  });
-}
+  yield PlacesTestUtils.addVisits({ uri: TEST_URL });
+  let frecency = yield PlacesTestUtils.fieldInDB(TEST_URL, "frecency");
+  is(frecency, 100, "Check initial frecency");
 
-function continueTest(aOldFrecency) {
   // Used to verify errors are not marked as typed.
-  PlacesUtils.history.markPageAsTyped(TEST_URI);
-  gBrowser.selectedBrowser.loadURI(TEST_URI.spec);
+  PlacesUtils.history.markPageAsTyped(NetUtil.newURI(TEST_URL));
 
-  // Create and add history observer.
-  let historyObserver = {
-    __proto__: NavHistoryObserver.prototype,
-    onVisit(aURI, aVisitID, aTime, aSessionID, aReferringID,
-                      aTransitionType) {
-      PlacesUtils.history.removeObserver(historyObserver);
-      info("Received onVisit: " + aURI.spec);
-      fieldForUrl(aURI, "frecency", function(aFrecency) {
-        is(aFrecency, aOldFrecency, "Frecency should be unchanged");
-        fieldForUrl(aURI, "hidden", function(aHidden) {
-          is(aHidden, 0, "Page should not be hidden");
-          fieldForUrl(aURI, "typed", function(aTyped) {
-            is(aTyped, 0, "page should not be marked as typed");
-            PlacesTestUtils.clearHistory().then(finish);
-          });
-        });
-      });
-    }
-  };
-  PlacesUtils.history.addObserver(historyObserver, false);
-}
+  let promiseVisit = new Promise(resolve => {
+    let historyObserver = {
+      __proto__: NavHistoryObserver.prototype,
+      onVisit(uri) {
+        PlacesUtils.history.removeObserver(historyObserver);
+        is(uri.spec, TEST_URL, "Check visited url");
+        resolve();
+      }
+    };
+    PlacesUtils.history.addObserver(historyObserver, false);
+  });
+  gBrowser.selectedBrowser.loadURI(TEST_URL);
+  yield promiseVisit;
+
+  is(yield PlacesTestUtils.fieldInDB(TEST_URL, "frecency"), frecency, "Frecency should be unchanged");
+  is(yield PlacesTestUtils.fieldInDB(TEST_URL, "hidden"), 0, "Page should not be hidden");
+  is(yield PlacesTestUtils.fieldInDB(TEST_URL, "typed"), 0, "page should not be marked as typed");
+});
--- a/toolkit/components/places/tests/unit/test_frecency_decay.js
+++ b/toolkit/components/places/tests/unit/test_frecency_decay.js
@@ -59,13 +59,13 @@ add_task(function* test_frecency_decay()
 
   // Trigger DecayFrecency.
   let promiseMany = promiseManyFrecenciesChanged();
   PlacesUtils.history.QueryInterface(Ci.nsIObserver)
              .observe(null, "idle-daily", "");
   yield promiseMany;
 
   // Now check the new frecency is correct.
-  let newFrecency = yield PlacesTestUtils.frecencyInDB(url);
+  let newFrecency = yield PlacesTestUtils.fieldInDB(url, "frecency");
 
   Assert.equal(newFrecency, Math.round(unvisitedBookmarkFrecency * PREF_FREC_DECAY_RATE_DEF),
                "Frecencies should match");
 });