toolkit/components/places/tests/unit/test_getChildIndex.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_getChildIndex.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,69 @@
     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 + * Tests nsNavHistoryContainerResultNode::GetChildIndex(aNode) functionality.
    1.12 + */
    1.13 +
    1.14 +function run_test() {
    1.15 +  // Add a bookmark to the menu.
    1.16 +  PlacesUtils.bookmarks.insertBookmark(PlacesUtils.bookmarksMenuFolderId,
    1.17 +                                       uri("http://test.mozilla.org/bookmark/"),
    1.18 +                                       Ci.nsINavBookmarksService.DEFAULT_INDEX,
    1.19 +                                       "Test bookmark");
    1.20 +
    1.21 +  // Add a bookmark to unfiled folder.
    1.22 +  PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId,
    1.23 +                                       uri("http://test.mozilla.org/unfiled/"),
    1.24 +                                       Ci.nsINavBookmarksService.DEFAULT_INDEX,
    1.25 +                                       "Unfiled bookmark");
    1.26 +
    1.27 +  // Get the unfiled bookmark node.
    1.28 +  let unfiledNode = getNodeAt(PlacesUtils.unfiledBookmarksFolderId, 0);
    1.29 +  if (!unfiledNode)
    1.30 +    do_throw("Unable to find bookmark in hierarchy!");
    1.31 +  do_check_eq(unfiledNode.title, "Unfiled bookmark");
    1.32 +
    1.33 +  let hs = PlacesUtils.history;
    1.34 +  let query = hs.getNewQuery();
    1.35 +  query.setFolders([PlacesUtils.bookmarksMenuFolderId], 1);
    1.36 +  let options = hs.getNewQueryOptions();
    1.37 +  options.queryType = options.QUERY_TYPE_BOOKMARKS;
    1.38 +  let root = hs.executeQuery(query, options).root;
    1.39 +  root.containerOpen = true;
    1.40 +
    1.41 +  // Check functionality for proper nodes.
    1.42 +  for (let i = 0; i < root.childCount; i++) {
    1.43 +    let node = root.getChild(i);
    1.44 +    print("Now testing: " + node.title);
    1.45 +    do_check_eq(root.getChildIndex(node), i);
    1.46 +  }
    1.47 +
    1.48 +  // Now search for an invalid node and expect an exception.
    1.49 +  try {
    1.50 +    root.getChildIndex(unfiledNode);
    1.51 +    do_throw("Searching for an invalid node should have thrown.");
    1.52 +  } catch(ex) {
    1.53 +    print("We correctly got an exception.");
    1.54 +  }
    1.55 +
    1.56 +  root.containerOpen = false;
    1.57 +}
    1.58 +
    1.59 +function getNodeAt(aFolderId, aIndex) {
    1.60 +  let hs = PlacesUtils.history;
    1.61 +  let query = hs.getNewQuery();
    1.62 +  query.setFolders([aFolderId], 1);
    1.63 +  let options = hs.getNewQueryOptions();
    1.64 +  options.queryType = options.QUERY_TYPE_BOOKMARKS;
    1.65 +  let root = hs.executeQuery(query, options).root;
    1.66 +  root.containerOpen = true;
    1.67 +  if (root.childCount < aIndex)
    1.68 +    do_throw("Not enough children to find bookmark!");
    1.69 +  let node = root.getChild(aIndex);
    1.70 +  root.containerOpen = false;
    1.71 +  return node;
    1.72 +}

mercurial