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: * The next thing we do is create a test database for us. Each test runs with michael@0: * its own database (tail_queries.js will clear it after the run). Take a look michael@0: * at the queryData object in head_queries.js, and you'll see how this object michael@0: * works. You can call it anything you like, but I usually use "testData". michael@0: * I'll include a couple of example entries in the database. michael@0: * michael@0: * Note that to use the compareArrayToResult API, you need to put all the michael@0: * results that are in the query set at the top of the testData list, and those michael@0: * results MUST be in the same sort order as the items in the resulting query. michael@0: */ michael@0: michael@0: var testData = [ michael@0: // Add a bookmark that should be in the results michael@0: { isBookmark: true, michael@0: uri: "http://bookmarked.com/", michael@0: parentFolder: PlacesUtils.toolbarFolderId, michael@0: index: PlacesUtils.bookmarks.DEFAULT_INDEX, michael@0: title: "", michael@0: isInQuery: true }, michael@0: michael@0: // Add a bookmark that should not be in the results michael@0: { isBookmark: true, michael@0: uri: "http://bookmarked-elsewhere.com/", michael@0: parentFolder: PlacesUtils.bookmarksMenuFolderId, michael@0: index: PlacesUtils.bookmarks.DEFAULT_INDEX, michael@0: title: "", michael@0: isInQuery: false }, michael@0: michael@0: // Add an un-bookmarked visit michael@0: { isVisit: true, michael@0: uri: "http://notbookmarked.com/", michael@0: isInQuery: false } michael@0: ]; michael@0: michael@0: michael@0: /** michael@0: * run_test is where the magic happens. This is automatically run by the test michael@0: * harness. It is where you do the work of creating the query, running it, and michael@0: * playing with the result set. michael@0: */ michael@0: function run_test() michael@0: { michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_task(function test_onlyBookmarked() michael@0: { michael@0: // This function in head_queries.js creates our database with the above data michael@0: yield task_populateDB(testData); michael@0: michael@0: // Query michael@0: var query = PlacesUtils.history.getNewQuery(); michael@0: query.setFolders([PlacesUtils.toolbarFolderId], 1); michael@0: query.onlyBookmarked = true; michael@0: michael@0: // query options michael@0: var options = PlacesUtils.history.getNewQueryOptions(); michael@0: options.queryType = options.QUERY_TYPE_HISTORY; michael@0: michael@0: // Results - this gets the result set and opens it for reading and modification. michael@0: var result = PlacesUtils.history.executeQuery(query, options); michael@0: var root = result.root; michael@0: root.containerOpen = true; michael@0: michael@0: // You can use this to compare the data in the array with the result set, michael@0: // if the array's isInQuery: true items are sorted the same way as the result michael@0: // set. michael@0: LOG("begin first test"); michael@0: compareArrayToResult(testData, root); michael@0: LOG("end first test"); michael@0: michael@0: /* ****************** michael@0: Test live-update michael@0: ********************/ michael@0: michael@0: var liveUpdateTestData = [ michael@0: //Add a bookmark that should show up michael@0: { isBookmark: true, michael@0: uri: "http://bookmarked2.com/", michael@0: parentFolder: PlacesUtils.toolbarFolderId, michael@0: index: PlacesUtils.bookmarks.DEFAULT_INDEX, michael@0: title: "", michael@0: isInQuery: true }, michael@0: michael@0: //Add a bookmark that should not show up michael@0: { isBookmark: true, michael@0: uri: "http://bookmarked-elsewhere2.com/", michael@0: parentFolder: PlacesUtils.bookmarksMenuFolderId, michael@0: index: PlacesUtils.bookmarks.DEFAULT_INDEX, michael@0: title: "", michael@0: isInQuery: false } michael@0: ]; michael@0: michael@0: yield task_populateDB(liveUpdateTestData); // add to the db michael@0: michael@0: // add to the test data michael@0: testData.push(liveUpdateTestData[0]); michael@0: testData.push(liveUpdateTestData[1]); michael@0: michael@0: // re-query and test michael@0: LOG("begin live-update test"); michael@0: compareArrayToResult(testData, root); michael@0: LOG("end live-update test"); michael@0: /* michael@0: // we are actually not updating during a batch. michael@0: // see bug 432706 for details. michael@0: michael@0: // Here's a batch update michael@0: var updateBatch = { michael@0: runBatched: function (aUserData) { michael@0: liveUpdateTestData[0].uri = "http://bookmarked3.com"; michael@0: liveUpdateTestData[1].uri = "http://bookmarked-elsewhere3.com"; michael@0: populateDB(liveUpdateTestData); michael@0: testData.push(liveUpdateTestData[0]); michael@0: testData.push(liveUpdateTestData[1]); michael@0: } michael@0: }; michael@0: michael@0: PlacesUtils.history.runInBatchMode(updateBatch, null); michael@0: michael@0: // re-query and test michael@0: LOG("begin batched test"); michael@0: compareArrayToResult(testData, root); michael@0: LOG("end batched test"); michael@0: */ michael@0: // Close the container when finished michael@0: root.containerOpen = false; michael@0: });