michael@0: /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: * Check for correct functionality of PlacesUtils.getURLsForContainerNode and michael@0: * PlacesUtils.hasChildURIs (those helpers share almost all of their code) michael@0: */ michael@0: michael@0: var PU = PlacesUtils; michael@0: var hs = PU.history; michael@0: var bs = PU.bookmarks; michael@0: michael@0: var tests = [ michael@0: michael@0: function() { michael@0: dump("\n\n*** TEST: folder\n"); michael@0: // This is the folder we will check for children. michael@0: var folderId = bs.createFolder(bs.toolbarFolder, "folder", bs.DEFAULT_INDEX); michael@0: michael@0: // Create a folder and a query node inside it, these should not be considered michael@0: // uri nodes. michael@0: bs.createFolder(folderId, "inside folder", bs.DEFAULT_INDEX); michael@0: bs.insertBookmark(folderId, uri("place:sort=1"), michael@0: bs.DEFAULT_INDEX, "inside query"); michael@0: michael@0: var query = hs.getNewQuery(); michael@0: query.setFolders([bs.toolbarFolder], 1); michael@0: var options = hs.getNewQueryOptions(); michael@0: michael@0: dump("Check folder without uri nodes\n"); michael@0: check_uri_nodes(query, options, 0); michael@0: michael@0: dump("Check folder with uri nodes\n"); michael@0: // Add an uri node, this should be considered. michael@0: bs.insertBookmark(folderId, uri("http://www.mozilla.org/"), michael@0: bs.DEFAULT_INDEX, "bookmark"); michael@0: check_uri_nodes(query, options, 1); michael@0: }, michael@0: michael@0: function() { michael@0: dump("\n\n*** TEST: folder in an excludeItems root\n"); michael@0: // This is the folder we will check for children. michael@0: var folderId = bs.createFolder(bs.toolbarFolder, "folder", bs.DEFAULT_INDEX); michael@0: michael@0: // Create a folder and a query node inside it, these should not be considered michael@0: // uri nodes. michael@0: bs.createFolder(folderId, "inside folder", bs.DEFAULT_INDEX); michael@0: bs.insertBookmark(folderId, uri("place:sort=1"), bs.DEFAULT_INDEX, "inside query"); michael@0: michael@0: var query = hs.getNewQuery(); michael@0: query.setFolders([bs.toolbarFolder], 1); michael@0: var options = hs.getNewQueryOptions(); michael@0: options.excludeItems = true; michael@0: michael@0: dump("Check folder without uri nodes\n"); michael@0: check_uri_nodes(query, options, 0); michael@0: michael@0: dump("Check folder with uri nodes\n"); michael@0: // Add an uri node, this should be considered. michael@0: bs.insertBookmark(folderId, uri("http://www.mozilla.org/"), michael@0: bs.DEFAULT_INDEX, "bookmark"); michael@0: check_uri_nodes(query, options, 1); michael@0: }, michael@0: michael@0: function() { michael@0: dump("\n\n*** TEST: query\n"); michael@0: // This is the query we will check for children. michael@0: bs.insertBookmark(bs.toolbarFolder, uri("place:folder=BOOKMARKS_MENU&sort=1"), michael@0: bs.DEFAULT_INDEX, "inside query"); michael@0: michael@0: // Create a folder and a query node inside it, these should not be considered michael@0: // uri nodes. michael@0: bs.createFolder(bs.bookmarksMenuFolder, "inside folder", bs.DEFAULT_INDEX); michael@0: bs.insertBookmark(bs.bookmarksMenuFolder, uri("place:sort=1"), michael@0: bs.DEFAULT_INDEX, "inside query"); michael@0: michael@0: var query = hs.getNewQuery(); michael@0: query.setFolders([bs.toolbarFolder], 1); michael@0: var options = hs.getNewQueryOptions(); michael@0: michael@0: dump("Check query without uri nodes\n"); michael@0: check_uri_nodes(query, options, 0); michael@0: michael@0: dump("Check query with uri nodes\n"); michael@0: // Add an uri node, this should be considered. michael@0: bs.insertBookmark(bs.bookmarksMenuFolder, uri("http://www.mozilla.org/"), michael@0: bs.DEFAULT_INDEX, "bookmark"); michael@0: check_uri_nodes(query, options, 1); michael@0: }, michael@0: michael@0: function() { michael@0: dump("\n\n*** TEST: excludeItems Query\n"); michael@0: // This is the query we will check for children. michael@0: bs.insertBookmark(bs.toolbarFolder, uri("place:folder=BOOKMARKS_MENU&sort=8"), michael@0: bs.DEFAULT_INDEX, "inside query"); michael@0: michael@0: // Create a folder and a query node inside it, these should not be considered michael@0: // uri nodes. michael@0: bs.createFolder(bs.bookmarksMenuFolder, "inside folder", bs.DEFAULT_INDEX); michael@0: bs.insertBookmark(bs.bookmarksMenuFolder, uri("place:sort=1"), michael@0: bs.DEFAULT_INDEX, "inside query"); michael@0: michael@0: var query = hs.getNewQuery(); michael@0: query.setFolders([bs.toolbarFolder], 1); michael@0: var options = hs.getNewQueryOptions(); michael@0: options.excludeItems = true; michael@0: michael@0: dump("Check folder without uri nodes\n"); michael@0: check_uri_nodes(query, options, 0); michael@0: michael@0: dump("Check folder with uri nodes\n"); michael@0: // Add an uri node, this should be considered. michael@0: bs.insertBookmark(bs.bookmarksMenuFolder, uri("http://www.mozilla.org/"), michael@0: bs.DEFAULT_INDEX, "bookmark"); michael@0: check_uri_nodes(query, options, 1); michael@0: }, michael@0: michael@0: function() { michael@0: dump("\n\n*** TEST: !expandQueries Query\n"); michael@0: // This is the query we will check for children. michael@0: bs.insertBookmark(bs.toolbarFolder, uri("place:folder=BOOKMARKS_MENU&sort=8"), michael@0: bs.DEFAULT_INDEX, "inside query"); michael@0: michael@0: // Create a folder and a query node inside it, these should not be considered michael@0: // uri nodes. michael@0: bs.createFolder(bs.bookmarksMenuFolder, "inside folder", bs.DEFAULT_INDEX); michael@0: bs.insertBookmark(bs.bookmarksMenuFolder, uri("place:sort=1"), michael@0: bs.DEFAULT_INDEX, "inside query"); michael@0: michael@0: var query = hs.getNewQuery(); michael@0: query.setFolders([bs.toolbarFolder], 1); michael@0: var options = hs.getNewQueryOptions(); michael@0: options.expandQueries = false; michael@0: michael@0: dump("Check folder without uri nodes\n"); michael@0: check_uri_nodes(query, options, 0); michael@0: michael@0: dump("Check folder with uri nodes\n"); michael@0: // Add an uri node, this should be considered. michael@0: bs.insertBookmark(bs.bookmarksMenuFolder, uri("http://www.mozilla.org/"), michael@0: bs.DEFAULT_INDEX, "bookmark"); michael@0: check_uri_nodes(query, options, 1); michael@0: } michael@0: michael@0: ]; michael@0: michael@0: /** michael@0: * Executes a query and checks number of uri nodes in the first container in michael@0: * query's results. To correctly test a container ensure that the query will michael@0: * return only your container in the first level. michael@0: * michael@0: * @param aQuery michael@0: * nsINavHistoryQuery object defining the query michael@0: * @param aOptions michael@0: * nsINavHistoryQueryOptions object defining the query's options michael@0: * @param aExpectedURINodes michael@0: * number of expected uri nodes michael@0: */ michael@0: function check_uri_nodes(aQuery, aOptions, aExpectedURINodes) { michael@0: var result = hs.executeQuery(aQuery, aOptions); michael@0: var root = result.root; michael@0: root.containerOpen = true; michael@0: var node = root.getChild(0); michael@0: do_check_eq(PU.hasChildURIs(node), aExpectedURINodes > 0); michael@0: do_check_eq(PU.getURLsForContainerNode(node).length, aExpectedURINodes); michael@0: root.containerOpen = false; michael@0: } michael@0: michael@0: function run_test() { michael@0: tests.forEach(function(aTest) { michael@0: remove_all_bookmarks(); michael@0: aTest(); michael@0: }); michael@0: // Cleanup. michael@0: remove_all_bookmarks(); michael@0: }