toolkit/components/places/tests/unit/test_frecency_observers.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/unit/test_frecency_observers.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,85 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +function run_test() {
     1.8 +  run_next_test();
     1.9 +}
    1.10 +
    1.11 +// Each of these tests a path that triggers a frecency update.  Together they
    1.12 +// hit all sites that update a frecency.
    1.13 +
    1.14 +// InsertVisitedURIs::UpdateFrecency and History::InsertPlace
    1.15 +add_task(function test_InsertVisitedURIs_UpdateFrecency_and_History_InsertPlace() {
    1.16 +  // InsertPlace is at the end of a path that UpdateFrecency is also on, so kill
    1.17 +  // two birds with one stone and expect two notifications.  Trigger the path by
    1.18 +  // adding a download.
    1.19 +  let uri = NetUtil.newURI("http://example.com/a");
    1.20 +  Cc["@mozilla.org/browser/download-history;1"].
    1.21 +    getService(Ci.nsIDownloadHistory).
    1.22 +    addDownload(uri);
    1.23 +  yield Promise.all([onFrecencyChanged(uri), onFrecencyChanged(uri)]);
    1.24 +});
    1.25 +
    1.26 +// nsNavHistory::UpdateFrecency
    1.27 +add_task(function test_nsNavHistory_UpdateFrecency() {
    1.28 +  let bm = PlacesUtils.bookmarks;
    1.29 +  let uri = NetUtil.newURI("http://example.com/b");
    1.30 +  bm.insertBookmark(bm.unfiledBookmarksFolder, uri,
    1.31 +                    Ci.nsINavBookmarksService.DEFAULT_INDEX, "test");
    1.32 +  yield onFrecencyChanged(uri);
    1.33 +});
    1.34 +
    1.35 +// nsNavHistory::invalidateFrecencies for particular pages
    1.36 +add_task(function test_nsNavHistory_invalidateFrecencies_somePages() {
    1.37 +  let uri = NetUtil.newURI("http://test-nsNavHistory-invalidateFrecencies-somePages.com/");
    1.38 +  // Bookmarking the URI is enough to add it to moz_places, and importantly, it
    1.39 +  // means that removePagesFromHost doesn't remove it from moz_places, so its
    1.40 +  // frecency is able to be changed.
    1.41 +  let bm = PlacesUtils.bookmarks;
    1.42 +  bm.insertBookmark(bm.unfiledBookmarksFolder, uri,
    1.43 +                    Ci.nsINavBookmarksService.DEFAULT_INDEX, "test");
    1.44 +  PlacesUtils.history.removePagesFromHost(uri.host, false);
    1.45 +  yield onFrecencyChanged(uri);
    1.46 +});
    1.47 +
    1.48 +// nsNavHistory::invalidateFrecencies for all pages
    1.49 +add_task(function test_nsNavHistory_invalidateFrecencies_allPages() {
    1.50 +  PlacesUtils.history.removeAllPages();
    1.51 +  yield onManyFrecenciesChanged();
    1.52 +});
    1.53 +
    1.54 +// nsNavHistory::DecayFrecency and nsNavHistory::FixInvalidFrecencies
    1.55 +add_task(function test_nsNavHistory_DecayFrecency_and_nsNavHistory_FixInvalidFrecencies() {
    1.56 +  // FixInvalidFrecencies is at the end of a path that DecayFrecency is also on,
    1.57 +  // so expect two notifications.  Trigger the path by making nsNavHistory
    1.58 +  // observe the idle-daily notification.
    1.59 +  PlacesUtils.history.QueryInterface(Ci.nsIObserver).
    1.60 +    observe(null, "idle-daily", "");
    1.61 +  yield Promise.all([onManyFrecenciesChanged(), onManyFrecenciesChanged()]);
    1.62 +});
    1.63 +
    1.64 +function onFrecencyChanged(expectedURI) {
    1.65 +  let deferred = Promise.defer();
    1.66 +  let obs = new NavHistoryObserver();
    1.67 +  obs.onFrecencyChanged =
    1.68 +    (uri, newFrecency, guid, hidden, visitDate) => {
    1.69 +      PlacesUtils.history.removeObserver(obs);
    1.70 +      do_check_true(!!uri);
    1.71 +      do_check_true(uri.equals(expectedURI));
    1.72 +      deferred.resolve();
    1.73 +    };
    1.74 +  PlacesUtils.history.addObserver(obs, false);
    1.75 +  return deferred.promise;
    1.76 +}
    1.77 +
    1.78 +function onManyFrecenciesChanged() {
    1.79 +  let deferred = Promise.defer();
    1.80 +  let obs = new NavHistoryObserver();
    1.81 +  obs.onManyFrecenciesChanged = () => {
    1.82 +    PlacesUtils.history.removeObserver(obs);
    1.83 +    do_check_true(true);
    1.84 +    deferred.resolve();
    1.85 +  };
    1.86 +  PlacesUtils.history.addObserver(obs, false);
    1.87 +  return deferred.promise;
    1.88 +}

mercurial