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: * Obtains the id of the folder obtained from the query. michael@0: * michael@0: * @param aQuery michael@0: * The query to obtain the folder id from. michael@0: * @returns the folder id of the folder of the root node of the query. michael@0: */ michael@0: function folder_id(aQuery) michael@0: { michael@0: var hs = Cc["@mozilla.org/browser/nav-history-service;1"]. michael@0: getService(Ci.nsINavHistoryService); michael@0: michael@0: dump("Checking query '" + aQuery + "'\n"); michael@0: var options = { }; michael@0: var queries = { }; michael@0: var size = { }; michael@0: hs.queryStringToQueries(aQuery, queries, size, options); michael@0: var result = hs.executeQueries(queries.value, size.value, options.value); michael@0: var root = result.root; michael@0: root.containerOpen = true; michael@0: do_check_true(root.hasChildren); michael@0: var folderID = root.getChild(0).parent.itemId; michael@0: root.containerOpen = false; michael@0: return folderID; michael@0: } michael@0: michael@0: function run_test() michael@0: { michael@0: var hs = Cc["@mozilla.org/browser/nav-history-service;1"]. michael@0: getService(Ci.nsINavHistoryService); michael@0: var bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"]. michael@0: getService(Ci.nsINavBookmarksService); michael@0: michael@0: const QUERIES = [ michael@0: "place:folder=PLACES_ROOT" michael@0: , "place:folder=BOOKMARKS_MENU" michael@0: , "place:folder=TAGS" michael@0: , "place:folder=UNFILED_BOOKMARKS" michael@0: , "place:folder=TOOLBAR" michael@0: ]; michael@0: const FOLDER_IDS = [ michael@0: bs.placesRoot michael@0: , bs.bookmarksMenuFolder michael@0: , bs.tagsFolder michael@0: , bs.unfiledBookmarksFolder michael@0: , bs.toolbarFolder michael@0: ]; michael@0: michael@0: // add something in the bookmarks menu folder so a query to it returns results michael@0: bs.insertBookmark(bs.bookmarksMenuFolder, uri("http://example.com/bmf/"), michael@0: Ci.nsINavBookmarksService.DEFAULT_INDEX, "bmf"); michael@0: michael@0: // add something to the tags folder michael@0: var ts = Cc["@mozilla.org/browser/tagging-service;1"]. michael@0: getService(Ci.nsITaggingService); michael@0: ts.tagURI(uri("http://www.example.com/"), ["tag"]); michael@0: michael@0: // add something to the unfiled bookmarks folder michael@0: bs.insertBookmark(bs.unfiledBookmarksFolder, uri("http://example.com/ubf/"), michael@0: Ci.nsINavBookmarksService.DEFAULT_INDEX, "ubf"); michael@0: michael@0: // add something to the toolbar folder michael@0: bs.insertBookmark(bs.toolbarFolder, uri("http://example.com/tf/"), michael@0: Ci.nsINavBookmarksService.DEFAULT_INDEX, "tf"); michael@0: michael@0: for (var i = 0; i < QUERIES.length; i++) { michael@0: var result = folder_id(QUERIES[i]); michael@0: dump("expected " + FOLDER_IDS[i] + ", got " + result + "\n"); michael@0: do_check_eq(FOLDER_IDS[i], result); michael@0: } michael@0: }