toolkit/components/places/tests/unit/test_utils_getURLsForContainerNode.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_utils_getURLsForContainerNode.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,179 @@
     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 + /**
    1.11 +  * Check for correct functionality of PlacesUtils.getURLsForContainerNode and
    1.12 +  * PlacesUtils.hasChildURIs (those helpers share almost all of their code)
    1.13 +  */
    1.14 +
    1.15 +var PU = PlacesUtils;
    1.16 +var hs = PU.history;
    1.17 +var bs = PU.bookmarks;
    1.18 +
    1.19 +var tests = [
    1.20 +
    1.21 +function() {
    1.22 +  dump("\n\n*** TEST: folder\n");
    1.23 +  // This is the folder we will check for children.
    1.24 +  var folderId = bs.createFolder(bs.toolbarFolder, "folder", bs.DEFAULT_INDEX);
    1.25 +
    1.26 +  // Create a folder and a query node inside it, these should not be considered
    1.27 +  // uri nodes.
    1.28 +  bs.createFolder(folderId, "inside folder", bs.DEFAULT_INDEX);
    1.29 +  bs.insertBookmark(folderId, uri("place:sort=1"),
    1.30 +                    bs.DEFAULT_INDEX, "inside query");
    1.31 +
    1.32 +  var query = hs.getNewQuery();
    1.33 +  query.setFolders([bs.toolbarFolder], 1);
    1.34 +  var options = hs.getNewQueryOptions();
    1.35 +
    1.36 +  dump("Check folder without uri nodes\n");
    1.37 +  check_uri_nodes(query, options, 0);
    1.38 +
    1.39 +  dump("Check folder with uri nodes\n");
    1.40 +  // Add an uri node, this should be considered.
    1.41 +  bs.insertBookmark(folderId, uri("http://www.mozilla.org/"),
    1.42 +                    bs.DEFAULT_INDEX, "bookmark");
    1.43 +  check_uri_nodes(query, options, 1);
    1.44 +},
    1.45 +
    1.46 +function() {
    1.47 +  dump("\n\n*** TEST: folder in an excludeItems root\n");
    1.48 +  // This is the folder we will check for children.
    1.49 +  var folderId = bs.createFolder(bs.toolbarFolder, "folder", bs.DEFAULT_INDEX);
    1.50 +
    1.51 +  // Create a folder and a query node inside it, these should not be considered
    1.52 +  // uri nodes.
    1.53 +  bs.createFolder(folderId, "inside folder", bs.DEFAULT_INDEX);
    1.54 +  bs.insertBookmark(folderId, uri("place:sort=1"), bs.DEFAULT_INDEX, "inside query");
    1.55 +
    1.56 +  var query = hs.getNewQuery();
    1.57 +  query.setFolders([bs.toolbarFolder], 1);
    1.58 +  var options = hs.getNewQueryOptions();
    1.59 +  options.excludeItems = true;
    1.60 +
    1.61 +  dump("Check folder without uri nodes\n");
    1.62 +  check_uri_nodes(query, options, 0);
    1.63 +
    1.64 +  dump("Check folder with uri nodes\n");
    1.65 +  // Add an uri node, this should be considered.
    1.66 +  bs.insertBookmark(folderId, uri("http://www.mozilla.org/"),
    1.67 +                    bs.DEFAULT_INDEX, "bookmark");
    1.68 +  check_uri_nodes(query, options, 1);
    1.69 +},
    1.70 +
    1.71 +function() {
    1.72 +  dump("\n\n*** TEST: query\n");
    1.73 +  // This is the query we will check for children.
    1.74 +  bs.insertBookmark(bs.toolbarFolder, uri("place:folder=BOOKMARKS_MENU&sort=1"),
    1.75 +                    bs.DEFAULT_INDEX, "inside query");
    1.76 +
    1.77 +  // Create a folder and a query node inside it, these should not be considered
    1.78 +  // uri nodes.
    1.79 +  bs.createFolder(bs.bookmarksMenuFolder, "inside folder", bs.DEFAULT_INDEX);
    1.80 +  bs.insertBookmark(bs.bookmarksMenuFolder, uri("place:sort=1"),
    1.81 +                    bs.DEFAULT_INDEX, "inside query");
    1.82 +
    1.83 +  var query = hs.getNewQuery();
    1.84 +  query.setFolders([bs.toolbarFolder], 1);
    1.85 +  var options = hs.getNewQueryOptions();
    1.86 +
    1.87 +  dump("Check query without uri nodes\n");
    1.88 +  check_uri_nodes(query, options, 0);
    1.89 +
    1.90 +  dump("Check query with uri nodes\n");
    1.91 +  // Add an uri node, this should be considered.
    1.92 +  bs.insertBookmark(bs.bookmarksMenuFolder, uri("http://www.mozilla.org/"),
    1.93 +                    bs.DEFAULT_INDEX, "bookmark");
    1.94 +  check_uri_nodes(query, options, 1);
    1.95 +},
    1.96 +
    1.97 +function() {
    1.98 +  dump("\n\n*** TEST: excludeItems Query\n");
    1.99 +  // This is the query we will check for children.
   1.100 +  bs.insertBookmark(bs.toolbarFolder, uri("place:folder=BOOKMARKS_MENU&sort=8"),
   1.101 +                    bs.DEFAULT_INDEX, "inside query");
   1.102 +
   1.103 +  // Create a folder and a query node inside it, these should not be considered
   1.104 +  // uri nodes.
   1.105 +  bs.createFolder(bs.bookmarksMenuFolder, "inside folder", bs.DEFAULT_INDEX);
   1.106 +  bs.insertBookmark(bs.bookmarksMenuFolder, uri("place:sort=1"),
   1.107 +                    bs.DEFAULT_INDEX, "inside query");
   1.108 +
   1.109 +  var query = hs.getNewQuery();
   1.110 +  query.setFolders([bs.toolbarFolder], 1);
   1.111 +  var options = hs.getNewQueryOptions();
   1.112 +  options.excludeItems = true;
   1.113 +
   1.114 +  dump("Check folder without uri nodes\n");
   1.115 +  check_uri_nodes(query, options, 0);
   1.116 +
   1.117 +  dump("Check folder with uri nodes\n");
   1.118 +  // Add an uri node, this should be considered.
   1.119 +  bs.insertBookmark(bs.bookmarksMenuFolder, uri("http://www.mozilla.org/"),
   1.120 +                    bs.DEFAULT_INDEX, "bookmark");
   1.121 +  check_uri_nodes(query, options, 1);
   1.122 +},
   1.123 +
   1.124 +function() {
   1.125 +  dump("\n\n*** TEST: !expandQueries Query\n");
   1.126 +  // This is the query we will check for children.
   1.127 +  bs.insertBookmark(bs.toolbarFolder, uri("place:folder=BOOKMARKS_MENU&sort=8"),
   1.128 +                    bs.DEFAULT_INDEX, "inside query");
   1.129 +
   1.130 +  // Create a folder and a query node inside it, these should not be considered
   1.131 +  // uri nodes.
   1.132 +  bs.createFolder(bs.bookmarksMenuFolder, "inside folder", bs.DEFAULT_INDEX);
   1.133 +  bs.insertBookmark(bs.bookmarksMenuFolder, uri("place:sort=1"),
   1.134 +                    bs.DEFAULT_INDEX, "inside query");
   1.135 +
   1.136 +  var query = hs.getNewQuery();
   1.137 +  query.setFolders([bs.toolbarFolder], 1);
   1.138 +  var options = hs.getNewQueryOptions();
   1.139 +  options.expandQueries = false;
   1.140 +
   1.141 +  dump("Check folder without uri nodes\n");
   1.142 +  check_uri_nodes(query, options, 0);
   1.143 +
   1.144 +  dump("Check folder with uri nodes\n");
   1.145 +  // Add an uri node, this should be considered.
   1.146 +  bs.insertBookmark(bs.bookmarksMenuFolder, uri("http://www.mozilla.org/"),
   1.147 +                    bs.DEFAULT_INDEX, "bookmark");
   1.148 +  check_uri_nodes(query, options, 1);
   1.149 +}
   1.150 +
   1.151 +];
   1.152 +
   1.153 +/**
   1.154 + * Executes a query and checks number of uri nodes in the first container in 
   1.155 + * query's results.  To correctly test a container ensure that the query will
   1.156 + * return only your container in the first level.
   1.157 + *
   1.158 + * @param  aQuery
   1.159 + *         nsINavHistoryQuery object defining the query
   1.160 + * @param  aOptions
   1.161 + *         nsINavHistoryQueryOptions object defining the query's options
   1.162 + * @param  aExpectedURINodes
   1.163 + *         number of expected uri nodes
   1.164 + */
   1.165 +function check_uri_nodes(aQuery, aOptions, aExpectedURINodes) {
   1.166 +  var result = hs.executeQuery(aQuery, aOptions);
   1.167 +  var root = result.root;
   1.168 +  root.containerOpen = true;
   1.169 +  var node = root.getChild(0);
   1.170 +  do_check_eq(PU.hasChildURIs(node), aExpectedURINodes > 0);
   1.171 +  do_check_eq(PU.getURLsForContainerNode(node).length, aExpectedURINodes);
   1.172 +  root.containerOpen = false;
   1.173 +}
   1.174 +
   1.175 +function run_test() {
   1.176 +  tests.forEach(function(aTest) {
   1.177 +                  remove_all_bookmarks();
   1.178 +                  aTest();
   1.179 +                });
   1.180 +  // Cleanup.
   1.181 +  remove_all_bookmarks();
   1.182 +}

mercurial