toolkit/components/places/tests/unit/test_nsINavHistoryViewer.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_nsINavHistoryViewer.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,255 @@
     1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +// Get history service
    1.11 +var histsvc = PlacesUtils.history;
    1.12 +var bhist = PlacesUtils.bhistory;
    1.13 +var bmsvc = PlacesUtils.bookmarks;
    1.14 +
    1.15 +var resultObserver = {
    1.16 +  insertedNode: null,
    1.17 +  nodeInserted: function(parent, node, newIndex) {
    1.18 +    this.insertedNode = node;
    1.19 +  },
    1.20 +  removedNode: null,
    1.21 +  nodeRemoved: function(parent, node, oldIndex) {
    1.22 +    this.removedNode = node;
    1.23 +  },
    1.24 +
    1.25 +  nodeAnnotationChanged: function() {},
    1.26 +
    1.27 +  newTitle: "",
    1.28 +  nodeChangedByTitle: null,
    1.29 +  nodeTitleChanged: function(node, newTitle) {
    1.30 +    this.nodeChangedByTitle = node;
    1.31 +    this.newTitle = newTitle;
    1.32 +  },
    1.33 +
    1.34 +  newAccessCount: 0,
    1.35 +  newTime: 0,
    1.36 +  nodeChangedByHistoryDetails: null,
    1.37 +  nodeHistoryDetailsChanged: function(node,
    1.38 +                                         updatedVisitDate,
    1.39 +                                         updatedVisitCount) {
    1.40 +    this.nodeChangedByHistoryDetails = node
    1.41 +    this.newTime = updatedVisitDate;
    1.42 +    this.newAccessCount = updatedVisitCount;
    1.43 +  },
    1.44 +
    1.45 +  movedNode: null,
    1.46 +  nodeMoved: function(node, oldParent, oldIndex, newParent, newIndex) {
    1.47 +    this.movedNode = node;
    1.48 +  },
    1.49 +  openedContainer: null,
    1.50 +  closedContainer: null,
    1.51 +  containerStateChanged: function (aNode, aOldState, aNewState) {
    1.52 +    if (aNewState == Ci.nsINavHistoryContainerResultNode.STATE_OPENED) {
    1.53 +      this.openedContainer = aNode;
    1.54 +    }
    1.55 +    else if (aNewState == Ci.nsINavHistoryContainerResultNode.STATE_CLOSED) {
    1.56 +      this.closedContainer = aNode;
    1.57 +    }
    1.58 +  },
    1.59 +  invalidatedContainer: null,
    1.60 +  invalidateContainer: function(node) {
    1.61 +    this.invalidatedContainer = node;
    1.62 +  },
    1.63 +  sortingMode: null,
    1.64 +  sortingChanged: function(sortingMode) {
    1.65 +    this.sortingMode = sortingMode;
    1.66 +  },
    1.67 +  inBatchMode: false,
    1.68 +  batching: function(aToggleMode) {
    1.69 +    do_check_neq(this.inBatchMode, aToggleMode);
    1.70 +    this.inBatchMode = aToggleMode;
    1.71 +  },
    1.72 +  result: null,
    1.73 +  reset: function() {
    1.74 +    this.insertedNode = null;
    1.75 +    this.removedNode = null;
    1.76 +    this.nodeChangedByTitle = null;
    1.77 +    this.nodeChangedByHistoryDetails = null;
    1.78 +    this.replacedNode = null;
    1.79 +    this.movedNode = null;
    1.80 +    this.openedContainer = null;
    1.81 +    this.closedContainer = null;
    1.82 +    this.invalidatedContainer = null;
    1.83 +    this.sortingMode = null;
    1.84 +  }
    1.85 +};
    1.86 +
    1.87 +var testURI = uri("http://mozilla.com");
    1.88 +
    1.89 +function run_test() {
    1.90 +  run_next_test();
    1.91 +}
    1.92 +
    1.93 +add_test(function check_history_query() {
    1.94 +  var options = histsvc.getNewQueryOptions();
    1.95 +  options.sortingMode = options.SORT_BY_DATE_DESCENDING;
    1.96 +  options.resultType = options.RESULTS_AS_VISIT;
    1.97 +  var query = histsvc.getNewQuery();
    1.98 +  var result = histsvc.executeQuery(query, options);
    1.99 +  result.addObserver(resultObserver, false);
   1.100 +  var root = result.root;
   1.101 +  root.containerOpen = true;
   1.102 +
   1.103 +  do_check_neq(resultObserver.openedContainer, null);
   1.104 +
   1.105 +  // nsINavHistoryResultObserver.nodeInserted
   1.106 +  // add a visit
   1.107 +  promiseAddVisits(testURI).then(function() {
   1.108 +    do_check_eq(testURI.spec, resultObserver.insertedNode.uri);
   1.109 +
   1.110 +    // nsINavHistoryResultObserver.nodeHistoryDetailsChanged
   1.111 +    // adding a visit causes nodeHistoryDetailsChanged for the folder
   1.112 +    do_check_eq(root.uri, resultObserver.nodeChangedByHistoryDetails.uri);
   1.113 +
   1.114 +    // nsINavHistoryResultObserver.itemTitleChanged for a leaf node
   1.115 +    promiseAddVisits({ uri: testURI, title: "baz" }).then(function () {
   1.116 +      do_check_eq(resultObserver.nodeChangedByTitle.title, "baz");
   1.117 +
   1.118 +      // nsINavHistoryResultObserver.nodeRemoved
   1.119 +      var removedURI = uri("http://google.com");
   1.120 +      promiseAddVisits(removedURI).then(function() {
   1.121 +        bhist.removePage(removedURI);
   1.122 +        do_check_eq(removedURI.spec, resultObserver.removedNode.uri);
   1.123 +
   1.124 +        // nsINavHistoryResultObserver.invalidateContainer
   1.125 +        bhist.removePagesFromHost("mozilla.com", false);
   1.126 +        do_check_eq(root.uri, resultObserver.invalidatedContainer.uri);
   1.127 +
   1.128 +        // nsINavHistoryResultObserver.sortingChanged
   1.129 +        resultObserver.invalidatedContainer = null;
   1.130 +        result.sortingMode = options.SORT_BY_TITLE_ASCENDING;
   1.131 +        do_check_eq(resultObserver.sortingMode, options.SORT_BY_TITLE_ASCENDING);
   1.132 +        do_check_eq(resultObserver.invalidatedContainer, result.root);
   1.133 +
   1.134 +        // nsINavHistoryResultObserver.invalidateContainer
   1.135 +        bhist.removeAllPages();
   1.136 +        do_check_eq(root.uri, resultObserver.invalidatedContainer.uri);
   1.137 +
   1.138 +        // nsINavHistoryResultObserver.batching
   1.139 +        do_check_false(resultObserver.inBatchMode);
   1.140 +        histsvc.runInBatchMode({
   1.141 +          runBatched: function (aUserData) {
   1.142 +            do_check_true(resultObserver.inBatchMode);
   1.143 +          }
   1.144 +        }, null);
   1.145 +        do_check_false(resultObserver.inBatchMode);
   1.146 +        bmsvc.runInBatchMode({
   1.147 +          runBatched: function (aUserData) {
   1.148 +            do_check_true(resultObserver.inBatchMode);
   1.149 +          }
   1.150 +        }, null);
   1.151 +        do_check_false(resultObserver.inBatchMode);
   1.152 +
   1.153 +        root.containerOpen = false;
   1.154 +        do_check_eq(resultObserver.closedContainer, resultObserver.openedContainer);
   1.155 +        result.removeObserver(resultObserver);
   1.156 +        resultObserver.reset();
   1.157 +        promiseAsyncUpdates().then(run_next_test);
   1.158 +      });
   1.159 +    });
   1.160 +  });
   1.161 +});
   1.162 +
   1.163 +add_test(function check_bookmarks_query() {
   1.164 +  var options = histsvc.getNewQueryOptions();
   1.165 +  var query = histsvc.getNewQuery();
   1.166 +  query.setFolders([bmsvc.bookmarksMenuFolder], 1);
   1.167 +  var result = histsvc.executeQuery(query, options);
   1.168 +  result.addObserver(resultObserver, false);
   1.169 +  var root = result.root;
   1.170 +  root.containerOpen = true;
   1.171 +
   1.172 +  do_check_neq(resultObserver.openedContainer, null);
   1.173 +
   1.174 +  // nsINavHistoryResultObserver.nodeInserted
   1.175 +  // add a bookmark
   1.176 +  var testBookmark = bmsvc.insertBookmark(bmsvc.bookmarksMenuFolder, testURI, bmsvc.DEFAULT_INDEX, "foo");
   1.177 +  do_check_eq("foo", resultObserver.insertedNode.title);
   1.178 +  do_check_eq(testURI.spec, resultObserver.insertedNode.uri);
   1.179 +
   1.180 +  // nsINavHistoryResultObserver.nodeHistoryDetailsChanged
   1.181 +  // adding a visit causes nodeHistoryDetailsChanged for the folder
   1.182 +  do_check_eq(root.uri, resultObserver.nodeChangedByHistoryDetails.uri);
   1.183 +
   1.184 +  // nsINavHistoryResultObserver.nodeTitleChanged for a leaf node
   1.185 +  bmsvc.setItemTitle(testBookmark, "baz");
   1.186 +  do_check_eq(resultObserver.nodeChangedByTitle.title, "baz");
   1.187 +  do_check_eq(resultObserver.newTitle, "baz");
   1.188 +
   1.189 +  var testBookmark2 = bmsvc.insertBookmark(bmsvc.bookmarksMenuFolder, uri("http://google.com"), bmsvc.DEFAULT_INDEX, "foo");
   1.190 +  bmsvc.moveItem(testBookmark2, bmsvc.bookmarksMenuFolder, 0);
   1.191 +  do_check_eq(resultObserver.movedNode.itemId, testBookmark2);
   1.192 +
   1.193 +  // nsINavHistoryResultObserver.nodeRemoved
   1.194 +  bmsvc.removeItem(testBookmark2);
   1.195 +  do_check_eq(testBookmark2, resultObserver.removedNode.itemId);
   1.196 +
   1.197 +  // XXX nsINavHistoryResultObserver.invalidateContainer
   1.198 +
   1.199 +  // nsINavHistoryResultObserver.sortingChanged
   1.200 +  resultObserver.invalidatedContainer = null;
   1.201 +  result.sortingMode = options.SORT_BY_TITLE_ASCENDING;
   1.202 +  do_check_eq(resultObserver.sortingMode, options.SORT_BY_TITLE_ASCENDING);
   1.203 +  do_check_eq(resultObserver.invalidatedContainer, result.root);
   1.204 +
   1.205 +  // nsINavHistoryResultObserver.batching
   1.206 +  do_check_false(resultObserver.inBatchMode);
   1.207 +  histsvc.runInBatchMode({
   1.208 +    runBatched: function (aUserData) {
   1.209 +      do_check_true(resultObserver.inBatchMode);
   1.210 +    }
   1.211 +  }, null);
   1.212 +  do_check_false(resultObserver.inBatchMode);
   1.213 +  bmsvc.runInBatchMode({
   1.214 +    runBatched: function (aUserData) {
   1.215 +      do_check_true(resultObserver.inBatchMode);
   1.216 +    }
   1.217 +  }, null);
   1.218 +  do_check_false(resultObserver.inBatchMode);
   1.219 +
   1.220 +  root.containerOpen = false;
   1.221 +  do_check_eq(resultObserver.closedContainer, resultObserver.openedContainer);
   1.222 +  result.removeObserver(resultObserver);
   1.223 +  resultObserver.reset();
   1.224 +  promiseAsyncUpdates().then(run_next_test);
   1.225 +});
   1.226 +
   1.227 +add_test(function check_mixed_query() {
   1.228 +  var options = histsvc.getNewQueryOptions();
   1.229 +  var query = histsvc.getNewQuery();
   1.230 +  query.onlyBookmarked = true;
   1.231 +  var result = histsvc.executeQuery(query, options);
   1.232 +  result.addObserver(resultObserver, false);
   1.233 +  var root = result.root;
   1.234 +  root.containerOpen = true;
   1.235 +
   1.236 +  do_check_neq(resultObserver.openedContainer, null);
   1.237 +
   1.238 +  // nsINavHistoryResultObserver.batching
   1.239 +  do_check_false(resultObserver.inBatchMode);
   1.240 +  histsvc.runInBatchMode({
   1.241 +    runBatched: function (aUserData) {
   1.242 +      do_check_true(resultObserver.inBatchMode);
   1.243 +    }
   1.244 +  }, null);
   1.245 +  do_check_false(resultObserver.inBatchMode);
   1.246 +  bmsvc.runInBatchMode({
   1.247 +    runBatched: function (aUserData) {
   1.248 +      do_check_true(resultObserver.inBatchMode);
   1.249 +    }
   1.250 +  }, null);
   1.251 +  do_check_false(resultObserver.inBatchMode);
   1.252 +
   1.253 +  root.containerOpen = false;
   1.254 +  do_check_eq(resultObserver.closedContainer, resultObserver.openedContainer);
   1.255 +  result.removeObserver(resultObserver);
   1.256 +  resultObserver.reset();
   1.257 +  promiseAsyncUpdates().then(run_next_test);
   1.258 +});

mercurial