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: // Get bookmark service michael@0: try { michael@0: var bmsvc = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].getService(Ci.nsINavBookmarksService); michael@0: } catch(ex) { michael@0: do_throw("Could not get nav-bookmarks-service\n"); michael@0: } michael@0: michael@0: // Get history service michael@0: try { michael@0: var histsvc = Cc["@mozilla.org/browser/nav-history-service;1"].getService(Ci.nsINavHistoryService); michael@0: } catch(ex) { michael@0: do_throw("Could not get history service\n"); michael@0: } michael@0: michael@0: // get bookmarks root id michael@0: var root = bmsvc.bookmarksMenuFolder; michael@0: michael@0: // main michael@0: function run_test() { michael@0: // test search on folder with various sorts and max results michael@0: // see bug #385829 for more details michael@0: var folder = bmsvc.createFolder(root, "bug 385829 test", bmsvc.DEFAULT_INDEX); michael@0: var b1 = bmsvc.insertBookmark(folder, uri("http://a1.com/"), michael@0: bmsvc.DEFAULT_INDEX, "1 title"); michael@0: michael@0: var b2 = bmsvc.insertBookmark(folder, uri("http://a2.com/"), michael@0: bmsvc.DEFAULT_INDEX, "2 title"); michael@0: michael@0: var b3 = bmsvc.insertBookmark(folder, uri("http://a3.com/"), michael@0: bmsvc.DEFAULT_INDEX, "3 title"); michael@0: michael@0: var b4 = bmsvc.insertBookmark(folder, uri("http://a4.com/"), michael@0: bmsvc.DEFAULT_INDEX, "4 title"); michael@0: michael@0: // ensure some unique values for date added and last modified michael@0: // for date added: b1 < b2 < b3 < b4 michael@0: // for last modified: b1 > b2 > b3 > b4 michael@0: bmsvc.setItemDateAdded(b1, 1000); michael@0: bmsvc.setItemDateAdded(b2, 2000); michael@0: bmsvc.setItemDateAdded(b3, 3000); michael@0: bmsvc.setItemDateAdded(b4, 4000); michael@0: michael@0: bmsvc.setItemLastModified(b1, 4000); michael@0: bmsvc.setItemLastModified(b2, 3000); michael@0: bmsvc.setItemLastModified(b3, 2000); michael@0: bmsvc.setItemLastModified(b4, 1000); michael@0: michael@0: var options = histsvc.getNewQueryOptions(); michael@0: var query = histsvc.getNewQuery(); michael@0: options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_BOOKMARKS; michael@0: options.maxResults = 3; michael@0: query.setFolders([folder], 1); michael@0: michael@0: var result = histsvc.executeQuery(query, options); michael@0: var rootNode = result.root; michael@0: rootNode.containerOpen = true; michael@0: michael@0: // test SORT_BY_DATEADDED_ASCENDING (live update) michael@0: result.sortingMode = options.SORT_BY_DATEADDED_ASCENDING; michael@0: do_check_eq(rootNode.childCount, 3); michael@0: do_check_eq(rootNode.getChild(0).itemId, b1); michael@0: do_check_eq(rootNode.getChild(1).itemId, b2); michael@0: do_check_eq(rootNode.getChild(2).itemId, b3); michael@0: do_check_true(rootNode.getChild(0).dateAdded < michael@0: rootNode.getChild(1).dateAdded); michael@0: do_check_true(rootNode.getChild(1).dateAdded < michael@0: rootNode.getChild(2).dateAdded); michael@0: michael@0: // test SORT_BY_DATEADDED_DESCENDING (live update) michael@0: result.sortingMode = options.SORT_BY_DATEADDED_DESCENDING; michael@0: do_check_eq(rootNode.childCount, 3); michael@0: do_check_eq(rootNode.getChild(0).itemId, b3); michael@0: do_check_eq(rootNode.getChild(1).itemId, b2); michael@0: do_check_eq(rootNode.getChild(2).itemId, b1); michael@0: do_check_true(rootNode.getChild(0).dateAdded > michael@0: rootNode.getChild(1).dateAdded); michael@0: do_check_true(rootNode.getChild(1).dateAdded > michael@0: rootNode.getChild(2).dateAdded); michael@0: michael@0: // test SORT_BY_LASTMODIFIED_ASCENDING (live update) michael@0: result.sortingMode = options.SORT_BY_LASTMODIFIED_ASCENDING; michael@0: do_check_eq(rootNode.childCount, 3); michael@0: do_check_eq(rootNode.getChild(0).itemId, b3); michael@0: do_check_eq(rootNode.getChild(1).itemId, b2); michael@0: do_check_eq(rootNode.getChild(2).itemId, b1); michael@0: do_check_true(rootNode.getChild(0).lastModified < michael@0: rootNode.getChild(1).lastModified); michael@0: do_check_true(rootNode.getChild(1).lastModified < michael@0: rootNode.getChild(2).lastModified); michael@0: michael@0: // test SORT_BY_LASTMODIFIED_DESCENDING (live update) michael@0: result.sortingMode = options.SORT_BY_LASTMODIFIED_DESCENDING; michael@0: michael@0: do_check_eq(rootNode.childCount, 3); michael@0: do_check_eq(rootNode.getChild(0).itemId, b1); michael@0: do_check_eq(rootNode.getChild(1).itemId, b2); michael@0: do_check_eq(rootNode.getChild(2).itemId, b3); michael@0: do_check_true(rootNode.getChild(0).lastModified > michael@0: rootNode.getChild(1).lastModified); michael@0: do_check_true(rootNode.getChild(1).lastModified > michael@0: rootNode.getChild(2).lastModified); michael@0: rootNode.containerOpen = false; michael@0: michael@0: // test SORT_BY_DATEADDED_ASCENDING michael@0: options.sortingMode = options.SORT_BY_DATEADDED_ASCENDING; michael@0: result = histsvc.executeQuery(query, options); michael@0: rootNode = result.root; michael@0: rootNode.containerOpen = true; michael@0: do_check_eq(rootNode.childCount, 3); michael@0: do_check_eq(rootNode.getChild(0).itemId, b1); michael@0: do_check_eq(rootNode.getChild(1).itemId, b2); michael@0: do_check_eq(rootNode.getChild(2).itemId, b3); michael@0: do_check_true(rootNode.getChild(0).dateAdded < michael@0: rootNode.getChild(1).dateAdded); michael@0: do_check_true(rootNode.getChild(1).dateAdded < michael@0: rootNode.getChild(2).dateAdded); michael@0: rootNode.containerOpen = false; michael@0: michael@0: // test SORT_BY_DATEADDED_DESCENDING michael@0: options.sortingMode = options.SORT_BY_DATEADDED_DESCENDING; michael@0: result = histsvc.executeQuery(query, options); michael@0: rootNode = result.root; michael@0: rootNode.containerOpen = true; michael@0: do_check_eq(rootNode.childCount, 3); michael@0: do_check_eq(rootNode.getChild(0).itemId, b4); michael@0: do_check_eq(rootNode.getChild(1).itemId, b3); michael@0: do_check_eq(rootNode.getChild(2).itemId, b2); michael@0: do_check_true(rootNode.getChild(0).dateAdded > michael@0: rootNode.getChild(1).dateAdded); michael@0: do_check_true(rootNode.getChild(1).dateAdded > michael@0: rootNode.getChild(2).dateAdded); michael@0: rootNode.containerOpen = false; michael@0: michael@0: // test SORT_BY_LASTMODIFIED_ASCENDING michael@0: options.sortingMode = options.SORT_BY_LASTMODIFIED_ASCENDING; michael@0: result = histsvc.executeQuery(query, options); michael@0: rootNode = result.root; michael@0: rootNode.containerOpen = true; michael@0: do_check_eq(rootNode.childCount, 3); michael@0: do_check_eq(rootNode.getChild(0).itemId, b4); michael@0: do_check_eq(rootNode.getChild(1).itemId, b3); michael@0: do_check_eq(rootNode.getChild(2).itemId, b2); michael@0: do_check_true(rootNode.getChild(0).lastModified < michael@0: rootNode.getChild(1).lastModified); michael@0: do_check_true(rootNode.getChild(1).lastModified < michael@0: rootNode.getChild(2).lastModified); michael@0: rootNode.containerOpen = false; michael@0: michael@0: // test SORT_BY_LASTMODIFIED_DESCENDING michael@0: options.sortingMode = options.SORT_BY_LASTMODIFIED_DESCENDING; michael@0: result = histsvc.executeQuery(query, options); michael@0: rootNode = result.root; michael@0: rootNode.containerOpen = true; michael@0: do_check_eq(rootNode.childCount, 3); michael@0: do_check_eq(rootNode.getChild(0).itemId, b1); michael@0: do_check_eq(rootNode.getChild(1).itemId, b2); michael@0: do_check_eq(rootNode.getChild(2).itemId, b3); michael@0: do_check_true(rootNode.getChild(0).lastModified > michael@0: rootNode.getChild(1).lastModified); michael@0: do_check_true(rootNode.getChild(1).lastModified > michael@0: rootNode.getChild(2).lastModified); michael@0: rootNode.containerOpen = false; michael@0: }