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: * Tests nsNavHistoryContainerResultNode::GetChildIndex(aNode) functionality. michael@0: */ michael@0: michael@0: function run_test() { michael@0: // Add a bookmark to the menu. michael@0: PlacesUtils.bookmarks.insertBookmark(PlacesUtils.bookmarksMenuFolderId, michael@0: uri("http://test.mozilla.org/bookmark/"), michael@0: Ci.nsINavBookmarksService.DEFAULT_INDEX, michael@0: "Test bookmark"); michael@0: michael@0: // Add a bookmark to unfiled folder. michael@0: PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId, michael@0: uri("http://test.mozilla.org/unfiled/"), michael@0: Ci.nsINavBookmarksService.DEFAULT_INDEX, michael@0: "Unfiled bookmark"); michael@0: michael@0: // Get the unfiled bookmark node. michael@0: let unfiledNode = getNodeAt(PlacesUtils.unfiledBookmarksFolderId, 0); michael@0: if (!unfiledNode) michael@0: do_throw("Unable to find bookmark in hierarchy!"); michael@0: do_check_eq(unfiledNode.title, "Unfiled bookmark"); michael@0: michael@0: let hs = PlacesUtils.history; michael@0: let query = hs.getNewQuery(); michael@0: query.setFolders([PlacesUtils.bookmarksMenuFolderId], 1); michael@0: let options = hs.getNewQueryOptions(); michael@0: options.queryType = options.QUERY_TYPE_BOOKMARKS; michael@0: let root = hs.executeQuery(query, options).root; michael@0: root.containerOpen = true; michael@0: michael@0: // Check functionality for proper nodes. michael@0: for (let i = 0; i < root.childCount; i++) { michael@0: let node = root.getChild(i); michael@0: print("Now testing: " + node.title); michael@0: do_check_eq(root.getChildIndex(node), i); michael@0: } michael@0: michael@0: // Now search for an invalid node and expect an exception. michael@0: try { michael@0: root.getChildIndex(unfiledNode); michael@0: do_throw("Searching for an invalid node should have thrown."); michael@0: } catch(ex) { michael@0: print("We correctly got an exception."); michael@0: } michael@0: michael@0: root.containerOpen = false; michael@0: } michael@0: michael@0: function getNodeAt(aFolderId, aIndex) { michael@0: let hs = PlacesUtils.history; michael@0: let query = hs.getNewQuery(); michael@0: query.setFolders([aFolderId], 1); michael@0: let options = hs.getNewQueryOptions(); michael@0: options.queryType = options.QUERY_TYPE_BOOKMARKS; michael@0: let root = hs.executeQuery(query, options).root; michael@0: root.containerOpen = true; michael@0: if (root.childCount < aIndex) michael@0: do_throw("Not enough children to find bookmark!"); michael@0: let node = root.getChild(aIndex); michael@0: root.containerOpen = false; michael@0: return node; michael@0: }