toolkit/components/places/tests/bookmarks/test_savedsearches.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/bookmarks/test_savedsearches.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,209 @@
     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 bookmarks root id
    1.11 +var root = PlacesUtils.bookmarksMenuFolderId;
    1.12 +
    1.13 +// a search term that matches a default bookmark
    1.14 +const searchTerm = "about";
    1.15 +
    1.16 +var testRoot;
    1.17 +
    1.18 +// main
    1.19 +function run_test() {
    1.20 +  // create a folder to hold all the tests
    1.21 +  // this makes the tests more tolerant of changes to the default bookmarks set
    1.22 +  // also, name it using the search term, for testing that containers that match don't show up in query results
    1.23 +  testRoot = PlacesUtils.bookmarks.createFolder(
    1.24 +    root, searchTerm, PlacesUtils.bookmarks.DEFAULT_INDEX);
    1.25 +
    1.26 +  run_next_test();
    1.27 +}
    1.28 +
    1.29 +add_test(function test_savedsearches_bookmarks() {
    1.30 +  // add a bookmark that matches the search term
    1.31 +  var bookmarkId = PlacesUtils.bookmarks.insertBookmark(
    1.32 +    root, uri("http://foo.com"), PlacesUtils.bookmarks.DEFAULT_INDEX,
    1.33 +    searchTerm);
    1.34 +
    1.35 +  // create a saved-search that matches a default bookmark
    1.36 +  var searchId = PlacesUtils.bookmarks.insertBookmark(
    1.37 +    testRoot, uri("place:terms=" + searchTerm + "&excludeQueries=1&expandQueries=1&queryType=1"),
    1.38 +    PlacesUtils.bookmarks.DEFAULT_INDEX, searchTerm);
    1.39 +
    1.40 +  // query for the test root, expandQueries=0
    1.41 +  // the query should show up as a regular bookmark
    1.42 +  try {
    1.43 +    var options = PlacesUtils.history.getNewQueryOptions();
    1.44 +    options.expandQueries = 0;
    1.45 +    var query = PlacesUtils.history.getNewQuery();
    1.46 +    query.setFolders([testRoot], 1);
    1.47 +    var result = PlacesUtils.history.executeQuery(query, options);
    1.48 +    var rootNode = result.root;
    1.49 +    rootNode.containerOpen = true;
    1.50 +    var cc = rootNode.childCount;
    1.51 +    do_check_eq(cc, 1);
    1.52 +    for (var i = 0; i < cc; i++) {
    1.53 +      var node = rootNode.getChild(i);
    1.54 +      // test that queries have valid itemId
    1.55 +      do_check_true(node.itemId > 0);
    1.56 +      // test that the container is closed
    1.57 +      node.QueryInterface(Ci.nsINavHistoryContainerResultNode);
    1.58 +      do_check_eq(node.containerOpen, false);
    1.59 +    }
    1.60 +    rootNode.containerOpen = false;
    1.61 +  }
    1.62 +  catch(ex) {
    1.63 +    do_throw("expandQueries=0 query error: " + ex);
    1.64 +  }
    1.65 +
    1.66 +  // bookmark saved search
    1.67 +  // query for the test root, expandQueries=1
    1.68 +  // the query should show up as a query container, with 1 child
    1.69 +  try {
    1.70 +    var options = PlacesUtils.history.getNewQueryOptions();
    1.71 +    options.expandQueries = 1;
    1.72 +    var query = PlacesUtils.history.getNewQuery();
    1.73 +    query.setFolders([testRoot], 1);
    1.74 +    var result = PlacesUtils.history.executeQuery(query, options);
    1.75 +    var rootNode = result.root;
    1.76 +    rootNode.containerOpen = true;
    1.77 +    var cc = rootNode.childCount;
    1.78 +    do_check_eq(cc, 1);
    1.79 +    for (var i = 0; i < cc; i++) {
    1.80 +      var node = rootNode.getChild(i);
    1.81 +      // test that query node type is container when expandQueries=1
    1.82 +      do_check_eq(node.type, node.RESULT_TYPE_QUERY);
    1.83 +      // test that queries (as containers) have valid itemId
    1.84 +      do_check_true(node.itemId > 0);
    1.85 +      node.QueryInterface(Ci.nsINavHistoryContainerResultNode);
    1.86 +      node.containerOpen = true;
    1.87 +
    1.88 +      // test that queries have children when excludeItems=1
    1.89 +      // test that query nodes don't show containers (shouldn't have our folder that matches)
    1.90 +      // test that queries don't show themselves in query results (shouldn't have our saved search)
    1.91 +      do_check_eq(node.childCount, 1);
    1.92 +
    1.93 +      // test that bookmark shows in query results
    1.94 +      var item = node.getChild(0);
    1.95 +      do_check_eq(item.itemId, bookmarkId);
    1.96 +
    1.97 +      // XXX - FAILING - test live-update of query results - add a bookmark that matches the query
    1.98 +      //var tmpBmId = PlacesUtils.bookmarks.insertBookmark(
    1.99 +      //  root, uri("http://" + searchTerm + ".com"),
   1.100 +      //  PlacesUtils.bookmarks.DEFAULT_INDEX, searchTerm + "blah");
   1.101 +      //do_check_eq(query.childCount, 2);
   1.102 +
   1.103 +      // XXX - test live-update of query results - delete a bookmark that matches the query
   1.104 +      //PlacesUtils.bookmarks.removeItem(tmpBMId);
   1.105 +      //do_check_eq(query.childCount, 1);
   1.106 +
   1.107 +      // test live-update of query results - add a folder that matches the query
   1.108 +      PlacesUtils.bookmarks.createFolder(
   1.109 +        root, searchTerm + "zaa", PlacesUtils.bookmarks.DEFAULT_INDEX);
   1.110 +      do_check_eq(node.childCount, 1);
   1.111 +      // test live-update of query results - add a query that matches the query
   1.112 +      PlacesUtils.bookmarks.insertBookmark(
   1.113 +        root, uri("place:terms=foo&excludeQueries=1&expandQueries=1&queryType=1"),
   1.114 +        PlacesUtils.bookmarks.DEFAULT_INDEX, searchTerm + "blah");
   1.115 +      do_check_eq(node.childCount, 1);
   1.116 +    }
   1.117 +    rootNode.containerOpen = false;
   1.118 +  }
   1.119 +  catch(ex) {
   1.120 +    do_throw("expandQueries=1 bookmarks query: " + ex);
   1.121 +  }
   1.122 +
   1.123 +  // delete the bookmark search
   1.124 +  PlacesUtils.bookmarks.removeItem(searchId);
   1.125 +
   1.126 +  run_next_test();
   1.127 +});
   1.128 +
   1.129 +add_task(function test_savedsearches_history() {
   1.130 +  // add a visit that matches the search term
   1.131 +  var testURI = uri("http://" + searchTerm + ".com");
   1.132 +  yield promiseAddVisits({ uri: testURI, title: searchTerm });
   1.133 +
   1.134 +  // create a saved-search that matches the visit we added
   1.135 +  var searchId = PlacesUtils.bookmarks.insertBookmark(testRoot,
   1.136 +    uri("place:terms=" + searchTerm + "&excludeQueries=1&expandQueries=1&queryType=0"),
   1.137 +    PlacesUtils.bookmarks.DEFAULT_INDEX, searchTerm);
   1.138 +
   1.139 +  // query for the test root, expandQueries=1
   1.140 +  // the query should show up as a query container, with 1 child
   1.141 +  try {
   1.142 +    var options = PlacesUtils.history.getNewQueryOptions();
   1.143 +    options.expandQueries = 1;
   1.144 +    var query = PlacesUtils.history.getNewQuery();
   1.145 +    query.setFolders([testRoot], 1);
   1.146 +    var result = PlacesUtils.history.executeQuery(query, options);
   1.147 +    var rootNode = result.root;
   1.148 +    rootNode.containerOpen = true;
   1.149 +    var cc = rootNode.childCount;
   1.150 +    do_check_eq(cc, 1);
   1.151 +    for (var i = 0; i < cc; i++) {
   1.152 +      var node = rootNode.getChild(i);
   1.153 +      // test that query node type is container when expandQueries=1
   1.154 +      do_check_eq(node.type, node.RESULT_TYPE_QUERY);
   1.155 +      // test that queries (as containers) have valid itemId
   1.156 +      do_check_eq(node.itemId, searchId);
   1.157 +      node.QueryInterface(Ci.nsINavHistoryContainerResultNode);
   1.158 +      node.containerOpen = true;
   1.159 +
   1.160 +      // test that queries have children when excludeItems=1
   1.161 +      // test that query nodes don't show containers (shouldn't have our folder that matches)
   1.162 +      // test that queries don't show themselves in query results (shouldn't have our saved search)
   1.163 +      do_check_eq(node.childCount, 1);
   1.164 +
   1.165 +      // test that history visit shows in query results
   1.166 +      var item = node.getChild(0);
   1.167 +      do_check_eq(item.type, item.RESULT_TYPE_URI);
   1.168 +      do_check_eq(item.itemId, -1); // history visit
   1.169 +      do_check_eq(item.uri, testURI.spec); // history visit
   1.170 +
   1.171 +      // test live-update of query results - add a history visit that matches the query
   1.172 +      yield promiseAddVisits({
   1.173 +        uri: uri("http://foo.com"),
   1.174 +        title: searchTerm + "blah"
   1.175 +      });
   1.176 +      do_check_eq(node.childCount, 2);
   1.177 +
   1.178 +      // test live-update of query results - delete a history visit that matches the query
   1.179 +      PlacesUtils.history.removePage(uri("http://foo.com"));
   1.180 +      do_check_eq(node.childCount, 1);
   1.181 +      node.containerOpen = false;
   1.182 +    }
   1.183 +
   1.184 +    // test live-update of moved queries
   1.185 +    var tmpFolderId = PlacesUtils.bookmarks.createFolder(
   1.186 +      testRoot, "foo", PlacesUtils.bookmarks.DEFAULT_INDEX);
   1.187 +    PlacesUtils.bookmarks.moveItem(
   1.188 +      searchId, tmpFolderId, PlacesUtils.bookmarks.DEFAULT_INDEX);
   1.189 +    var tmpFolderNode = rootNode.getChild(0);
   1.190 +    do_check_eq(tmpFolderNode.itemId, tmpFolderId);
   1.191 +    tmpFolderNode.QueryInterface(Ci.nsINavHistoryContainerResultNode);
   1.192 +    tmpFolderNode.containerOpen = true;
   1.193 +    do_check_eq(tmpFolderNode.childCount, 1);
   1.194 +
   1.195 +    // test live-update of renamed queries
   1.196 +    PlacesUtils.bookmarks.setItemTitle(searchId, "foo");
   1.197 +    do_check_eq(tmpFolderNode.title, "foo");
   1.198 +
   1.199 +    // test live-update of deleted queries
   1.200 +    PlacesUtils.bookmarks.removeItem(searchId);
   1.201 +    try {
   1.202 +      var tmpFolderNode = root.getChild(1);
   1.203 +      do_throw("query was not removed");
   1.204 +    } catch(ex) {}
   1.205 +
   1.206 +    tmpFolderNode.containerOpen = false;
   1.207 +    rootNode.containerOpen = false;
   1.208 +  }
   1.209 +  catch(ex) {
   1.210 +    do_throw("expandQueries=1 bookmarks query: " + ex);
   1.211 +  }
   1.212 +});

mercurial