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

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 /**
michael@0 8 * Check for correct functionality of PlacesUtils.getURLsForContainerNode and
michael@0 9 * PlacesUtils.hasChildURIs (those helpers share almost all of their code)
michael@0 10 */
michael@0 11
michael@0 12 var PU = PlacesUtils;
michael@0 13 var hs = PU.history;
michael@0 14 var bs = PU.bookmarks;
michael@0 15
michael@0 16 var tests = [
michael@0 17
michael@0 18 function() {
michael@0 19 dump("\n\n*** TEST: folder\n");
michael@0 20 // This is the folder we will check for children.
michael@0 21 var folderId = bs.createFolder(bs.toolbarFolder, "folder", bs.DEFAULT_INDEX);
michael@0 22
michael@0 23 // Create a folder and a query node inside it, these should not be considered
michael@0 24 // uri nodes.
michael@0 25 bs.createFolder(folderId, "inside folder", bs.DEFAULT_INDEX);
michael@0 26 bs.insertBookmark(folderId, uri("place:sort=1"),
michael@0 27 bs.DEFAULT_INDEX, "inside query");
michael@0 28
michael@0 29 var query = hs.getNewQuery();
michael@0 30 query.setFolders([bs.toolbarFolder], 1);
michael@0 31 var options = hs.getNewQueryOptions();
michael@0 32
michael@0 33 dump("Check folder without uri nodes\n");
michael@0 34 check_uri_nodes(query, options, 0);
michael@0 35
michael@0 36 dump("Check folder with uri nodes\n");
michael@0 37 // Add an uri node, this should be considered.
michael@0 38 bs.insertBookmark(folderId, uri("http://www.mozilla.org/"),
michael@0 39 bs.DEFAULT_INDEX, "bookmark");
michael@0 40 check_uri_nodes(query, options, 1);
michael@0 41 },
michael@0 42
michael@0 43 function() {
michael@0 44 dump("\n\n*** TEST: folder in an excludeItems root\n");
michael@0 45 // This is the folder we will check for children.
michael@0 46 var folderId = bs.createFolder(bs.toolbarFolder, "folder", bs.DEFAULT_INDEX);
michael@0 47
michael@0 48 // Create a folder and a query node inside it, these should not be considered
michael@0 49 // uri nodes.
michael@0 50 bs.createFolder(folderId, "inside folder", bs.DEFAULT_INDEX);
michael@0 51 bs.insertBookmark(folderId, uri("place:sort=1"), bs.DEFAULT_INDEX, "inside query");
michael@0 52
michael@0 53 var query = hs.getNewQuery();
michael@0 54 query.setFolders([bs.toolbarFolder], 1);
michael@0 55 var options = hs.getNewQueryOptions();
michael@0 56 options.excludeItems = true;
michael@0 57
michael@0 58 dump("Check folder without uri nodes\n");
michael@0 59 check_uri_nodes(query, options, 0);
michael@0 60
michael@0 61 dump("Check folder with uri nodes\n");
michael@0 62 // Add an uri node, this should be considered.
michael@0 63 bs.insertBookmark(folderId, uri("http://www.mozilla.org/"),
michael@0 64 bs.DEFAULT_INDEX, "bookmark");
michael@0 65 check_uri_nodes(query, options, 1);
michael@0 66 },
michael@0 67
michael@0 68 function() {
michael@0 69 dump("\n\n*** TEST: query\n");
michael@0 70 // This is the query we will check for children.
michael@0 71 bs.insertBookmark(bs.toolbarFolder, uri("place:folder=BOOKMARKS_MENU&sort=1"),
michael@0 72 bs.DEFAULT_INDEX, "inside query");
michael@0 73
michael@0 74 // Create a folder and a query node inside it, these should not be considered
michael@0 75 // uri nodes.
michael@0 76 bs.createFolder(bs.bookmarksMenuFolder, "inside folder", bs.DEFAULT_INDEX);
michael@0 77 bs.insertBookmark(bs.bookmarksMenuFolder, uri("place:sort=1"),
michael@0 78 bs.DEFAULT_INDEX, "inside query");
michael@0 79
michael@0 80 var query = hs.getNewQuery();
michael@0 81 query.setFolders([bs.toolbarFolder], 1);
michael@0 82 var options = hs.getNewQueryOptions();
michael@0 83
michael@0 84 dump("Check query without uri nodes\n");
michael@0 85 check_uri_nodes(query, options, 0);
michael@0 86
michael@0 87 dump("Check query with uri nodes\n");
michael@0 88 // Add an uri node, this should be considered.
michael@0 89 bs.insertBookmark(bs.bookmarksMenuFolder, uri("http://www.mozilla.org/"),
michael@0 90 bs.DEFAULT_INDEX, "bookmark");
michael@0 91 check_uri_nodes(query, options, 1);
michael@0 92 },
michael@0 93
michael@0 94 function() {
michael@0 95 dump("\n\n*** TEST: excludeItems Query\n");
michael@0 96 // This is the query we will check for children.
michael@0 97 bs.insertBookmark(bs.toolbarFolder, uri("place:folder=BOOKMARKS_MENU&sort=8"),
michael@0 98 bs.DEFAULT_INDEX, "inside query");
michael@0 99
michael@0 100 // Create a folder and a query node inside it, these should not be considered
michael@0 101 // uri nodes.
michael@0 102 bs.createFolder(bs.bookmarksMenuFolder, "inside folder", bs.DEFAULT_INDEX);
michael@0 103 bs.insertBookmark(bs.bookmarksMenuFolder, uri("place:sort=1"),
michael@0 104 bs.DEFAULT_INDEX, "inside query");
michael@0 105
michael@0 106 var query = hs.getNewQuery();
michael@0 107 query.setFolders([bs.toolbarFolder], 1);
michael@0 108 var options = hs.getNewQueryOptions();
michael@0 109 options.excludeItems = true;
michael@0 110
michael@0 111 dump("Check folder without uri nodes\n");
michael@0 112 check_uri_nodes(query, options, 0);
michael@0 113
michael@0 114 dump("Check folder with uri nodes\n");
michael@0 115 // Add an uri node, this should be considered.
michael@0 116 bs.insertBookmark(bs.bookmarksMenuFolder, uri("http://www.mozilla.org/"),
michael@0 117 bs.DEFAULT_INDEX, "bookmark");
michael@0 118 check_uri_nodes(query, options, 1);
michael@0 119 },
michael@0 120
michael@0 121 function() {
michael@0 122 dump("\n\n*** TEST: !expandQueries Query\n");
michael@0 123 // This is the query we will check for children.
michael@0 124 bs.insertBookmark(bs.toolbarFolder, uri("place:folder=BOOKMARKS_MENU&sort=8"),
michael@0 125 bs.DEFAULT_INDEX, "inside query");
michael@0 126
michael@0 127 // Create a folder and a query node inside it, these should not be considered
michael@0 128 // uri nodes.
michael@0 129 bs.createFolder(bs.bookmarksMenuFolder, "inside folder", bs.DEFAULT_INDEX);
michael@0 130 bs.insertBookmark(bs.bookmarksMenuFolder, uri("place:sort=1"),
michael@0 131 bs.DEFAULT_INDEX, "inside query");
michael@0 132
michael@0 133 var query = hs.getNewQuery();
michael@0 134 query.setFolders([bs.toolbarFolder], 1);
michael@0 135 var options = hs.getNewQueryOptions();
michael@0 136 options.expandQueries = false;
michael@0 137
michael@0 138 dump("Check folder without uri nodes\n");
michael@0 139 check_uri_nodes(query, options, 0);
michael@0 140
michael@0 141 dump("Check folder with uri nodes\n");
michael@0 142 // Add an uri node, this should be considered.
michael@0 143 bs.insertBookmark(bs.bookmarksMenuFolder, uri("http://www.mozilla.org/"),
michael@0 144 bs.DEFAULT_INDEX, "bookmark");
michael@0 145 check_uri_nodes(query, options, 1);
michael@0 146 }
michael@0 147
michael@0 148 ];
michael@0 149
michael@0 150 /**
michael@0 151 * Executes a query and checks number of uri nodes in the first container in
michael@0 152 * query's results. To correctly test a container ensure that the query will
michael@0 153 * return only your container in the first level.
michael@0 154 *
michael@0 155 * @param aQuery
michael@0 156 * nsINavHistoryQuery object defining the query
michael@0 157 * @param aOptions
michael@0 158 * nsINavHistoryQueryOptions object defining the query's options
michael@0 159 * @param aExpectedURINodes
michael@0 160 * number of expected uri nodes
michael@0 161 */
michael@0 162 function check_uri_nodes(aQuery, aOptions, aExpectedURINodes) {
michael@0 163 var result = hs.executeQuery(aQuery, aOptions);
michael@0 164 var root = result.root;
michael@0 165 root.containerOpen = true;
michael@0 166 var node = root.getChild(0);
michael@0 167 do_check_eq(PU.hasChildURIs(node), aExpectedURINodes > 0);
michael@0 168 do_check_eq(PU.getURLsForContainerNode(node).length, aExpectedURINodes);
michael@0 169 root.containerOpen = false;
michael@0 170 }
michael@0 171
michael@0 172 function run_test() {
michael@0 173 tests.forEach(function(aTest) {
michael@0 174 remove_all_bookmarks();
michael@0 175 aTest();
michael@0 176 });
michael@0 177 // Cleanup.
michael@0 178 remove_all_bookmarks();
michael@0 179 }

mercurial