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 bookmarks root id michael@0: var root = PlacesUtils.bookmarksMenuFolderId; michael@0: michael@0: // a search term that matches a default bookmark michael@0: const searchTerm = "about"; michael@0: michael@0: var testRoot; michael@0: michael@0: // main michael@0: function run_test() { michael@0: // create a folder to hold all the tests michael@0: // this makes the tests more tolerant of changes to the default bookmarks set michael@0: // also, name it using the search term, for testing that containers that match don't show up in query results michael@0: testRoot = PlacesUtils.bookmarks.createFolder( michael@0: root, searchTerm, PlacesUtils.bookmarks.DEFAULT_INDEX); michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_test(function test_savedsearches_bookmarks() { michael@0: // add a bookmark that matches the search term michael@0: var bookmarkId = PlacesUtils.bookmarks.insertBookmark( michael@0: root, uri("http://foo.com"), PlacesUtils.bookmarks.DEFAULT_INDEX, michael@0: searchTerm); michael@0: michael@0: // create a saved-search that matches a default bookmark michael@0: var searchId = PlacesUtils.bookmarks.insertBookmark( michael@0: testRoot, uri("place:terms=" + searchTerm + "&excludeQueries=1&expandQueries=1&queryType=1"), michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, searchTerm); michael@0: michael@0: // query for the test root, expandQueries=0 michael@0: // the query should show up as a regular bookmark michael@0: try { michael@0: var options = PlacesUtils.history.getNewQueryOptions(); michael@0: options.expandQueries = 0; michael@0: var query = PlacesUtils.history.getNewQuery(); michael@0: query.setFolders([testRoot], 1); michael@0: var result = PlacesUtils.history.executeQuery(query, options); michael@0: var rootNode = result.root; michael@0: rootNode.containerOpen = true; michael@0: var cc = rootNode.childCount; michael@0: do_check_eq(cc, 1); michael@0: for (var i = 0; i < cc; i++) { michael@0: var node = rootNode.getChild(i); michael@0: // test that queries have valid itemId michael@0: do_check_true(node.itemId > 0); michael@0: // test that the container is closed michael@0: node.QueryInterface(Ci.nsINavHistoryContainerResultNode); michael@0: do_check_eq(node.containerOpen, false); michael@0: } michael@0: rootNode.containerOpen = false; michael@0: } michael@0: catch(ex) { michael@0: do_throw("expandQueries=0 query error: " + ex); michael@0: } michael@0: michael@0: // bookmark saved search michael@0: // query for the test root, expandQueries=1 michael@0: // the query should show up as a query container, with 1 child michael@0: try { michael@0: var options = PlacesUtils.history.getNewQueryOptions(); michael@0: options.expandQueries = 1; michael@0: var query = PlacesUtils.history.getNewQuery(); michael@0: query.setFolders([testRoot], 1); michael@0: var result = PlacesUtils.history.executeQuery(query, options); michael@0: var rootNode = result.root; michael@0: rootNode.containerOpen = true; michael@0: var cc = rootNode.childCount; michael@0: do_check_eq(cc, 1); michael@0: for (var i = 0; i < cc; i++) { michael@0: var node = rootNode.getChild(i); michael@0: // test that query node type is container when expandQueries=1 michael@0: do_check_eq(node.type, node.RESULT_TYPE_QUERY); michael@0: // test that queries (as containers) have valid itemId michael@0: do_check_true(node.itemId > 0); michael@0: node.QueryInterface(Ci.nsINavHistoryContainerResultNode); michael@0: node.containerOpen = true; michael@0: michael@0: // test that queries have children when excludeItems=1 michael@0: // test that query nodes don't show containers (shouldn't have our folder that matches) michael@0: // test that queries don't show themselves in query results (shouldn't have our saved search) michael@0: do_check_eq(node.childCount, 1); michael@0: michael@0: // test that bookmark shows in query results michael@0: var item = node.getChild(0); michael@0: do_check_eq(item.itemId, bookmarkId); michael@0: michael@0: // XXX - FAILING - test live-update of query results - add a bookmark that matches the query michael@0: //var tmpBmId = PlacesUtils.bookmarks.insertBookmark( michael@0: // root, uri("http://" + searchTerm + ".com"), michael@0: // PlacesUtils.bookmarks.DEFAULT_INDEX, searchTerm + "blah"); michael@0: //do_check_eq(query.childCount, 2); michael@0: michael@0: // XXX - test live-update of query results - delete a bookmark that matches the query michael@0: //PlacesUtils.bookmarks.removeItem(tmpBMId); michael@0: //do_check_eq(query.childCount, 1); michael@0: michael@0: // test live-update of query results - add a folder that matches the query michael@0: PlacesUtils.bookmarks.createFolder( michael@0: root, searchTerm + "zaa", PlacesUtils.bookmarks.DEFAULT_INDEX); michael@0: do_check_eq(node.childCount, 1); michael@0: // test live-update of query results - add a query that matches the query michael@0: PlacesUtils.bookmarks.insertBookmark( michael@0: root, uri("place:terms=foo&excludeQueries=1&expandQueries=1&queryType=1"), michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, searchTerm + "blah"); michael@0: do_check_eq(node.childCount, 1); michael@0: } michael@0: rootNode.containerOpen = false; michael@0: } michael@0: catch(ex) { michael@0: do_throw("expandQueries=1 bookmarks query: " + ex); michael@0: } michael@0: michael@0: // delete the bookmark search michael@0: PlacesUtils.bookmarks.removeItem(searchId); michael@0: michael@0: run_next_test(); michael@0: }); michael@0: michael@0: add_task(function test_savedsearches_history() { michael@0: // add a visit that matches the search term michael@0: var testURI = uri("http://" + searchTerm + ".com"); michael@0: yield promiseAddVisits({ uri: testURI, title: searchTerm }); michael@0: michael@0: // create a saved-search that matches the visit we added michael@0: var searchId = PlacesUtils.bookmarks.insertBookmark(testRoot, michael@0: uri("place:terms=" + searchTerm + "&excludeQueries=1&expandQueries=1&queryType=0"), michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, searchTerm); michael@0: michael@0: // query for the test root, expandQueries=1 michael@0: // the query should show up as a query container, with 1 child michael@0: try { michael@0: var options = PlacesUtils.history.getNewQueryOptions(); michael@0: options.expandQueries = 1; michael@0: var query = PlacesUtils.history.getNewQuery(); michael@0: query.setFolders([testRoot], 1); michael@0: var result = PlacesUtils.history.executeQuery(query, options); michael@0: var rootNode = result.root; michael@0: rootNode.containerOpen = true; michael@0: var cc = rootNode.childCount; michael@0: do_check_eq(cc, 1); michael@0: for (var i = 0; i < cc; i++) { michael@0: var node = rootNode.getChild(i); michael@0: // test that query node type is container when expandQueries=1 michael@0: do_check_eq(node.type, node.RESULT_TYPE_QUERY); michael@0: // test that queries (as containers) have valid itemId michael@0: do_check_eq(node.itemId, searchId); michael@0: node.QueryInterface(Ci.nsINavHistoryContainerResultNode); michael@0: node.containerOpen = true; michael@0: michael@0: // test that queries have children when excludeItems=1 michael@0: // test that query nodes don't show containers (shouldn't have our folder that matches) michael@0: // test that queries don't show themselves in query results (shouldn't have our saved search) michael@0: do_check_eq(node.childCount, 1); michael@0: michael@0: // test that history visit shows in query results michael@0: var item = node.getChild(0); michael@0: do_check_eq(item.type, item.RESULT_TYPE_URI); michael@0: do_check_eq(item.itemId, -1); // history visit michael@0: do_check_eq(item.uri, testURI.spec); // history visit michael@0: michael@0: // test live-update of query results - add a history visit that matches the query michael@0: yield promiseAddVisits({ michael@0: uri: uri("http://foo.com"), michael@0: title: searchTerm + "blah" michael@0: }); michael@0: do_check_eq(node.childCount, 2); michael@0: michael@0: // test live-update of query results - delete a history visit that matches the query michael@0: PlacesUtils.history.removePage(uri("http://foo.com")); michael@0: do_check_eq(node.childCount, 1); michael@0: node.containerOpen = false; michael@0: } michael@0: michael@0: // test live-update of moved queries michael@0: var tmpFolderId = PlacesUtils.bookmarks.createFolder( michael@0: testRoot, "foo", PlacesUtils.bookmarks.DEFAULT_INDEX); michael@0: PlacesUtils.bookmarks.moveItem( michael@0: searchId, tmpFolderId, PlacesUtils.bookmarks.DEFAULT_INDEX); michael@0: var tmpFolderNode = rootNode.getChild(0); michael@0: do_check_eq(tmpFolderNode.itemId, tmpFolderId); michael@0: tmpFolderNode.QueryInterface(Ci.nsINavHistoryContainerResultNode); michael@0: tmpFolderNode.containerOpen = true; michael@0: do_check_eq(tmpFolderNode.childCount, 1); michael@0: michael@0: // test live-update of renamed queries michael@0: PlacesUtils.bookmarks.setItemTitle(searchId, "foo"); michael@0: do_check_eq(tmpFolderNode.title, "foo"); michael@0: michael@0: // test live-update of deleted queries michael@0: PlacesUtils.bookmarks.removeItem(searchId); michael@0: try { michael@0: var tmpFolderNode = root.getChild(1); michael@0: do_throw("query was not removed"); michael@0: } catch(ex) {} michael@0: michael@0: tmpFolderNode.containerOpen = false; michael@0: rootNode.containerOpen = false; michael@0: } michael@0: catch(ex) { michael@0: do_throw("expandQueries=1 bookmarks query: " + ex); michael@0: } michael@0: });