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: * Pre Test Items go here - Note that if you need time constants or relative michael@0: * times, there are several in head_queries.js. michael@0: * Other things to do here might be to create some bookmark folders, for access michael@0: * to the Places API's you can use the global variables defined in head_queries.js. michael@0: * Here is an example of using these to create some bookmark folders: michael@0: */ michael@0: // Create Folder1 from root michael@0: PlacesUtils.bookmarks.createFolder(PlacesUtils.placesRootId, "Folder 1", michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX); michael@0: var folder1Id = PlacesUtils.bookmarks.getChildFolder(PlacesUtils.placesRootId, michael@0: "Folder 1"); michael@0: michael@0: // Make Folder 1a a child of Folder 1 michael@0: PlacesUtils.bookmarks.createFolder(folder1Id, "Folder 1a", michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX); michael@0: var folder1aId = PlacesUtils.bookmarks.getChildFolder(folder1Id, "Folder 1a"); 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: michael@0: // This puts a history visit item into the database. Note that I don't michael@0: // specify the "lastVisit" attribute, because I am ok with "lastVisit" michael@0: // defaulting to "today". isInQuery is true as I expect this to turn up in the michael@0: // query I'll be doing down below. michael@0: {isInQuery: true, isVisit: true, uri: "http://foo.com/"}, michael@0: michael@0: // And so on to get a database that has enough elements to adequately test michael@0: // the edges of your query set. Be sure to include things that are outside michael@0: // the query set but can be updated so that they are included in the query michael@0: // set. Here's a more complicated example to finish our examples. michael@0: {isInQuery: true, isVisit: true, isDetails: true, title: "taggariffic", michael@0: uri: "http://foo.com/tagging/test.html", lastVisit: beginTime, isTag: true, michael@0: tagArray: ["moz"] }]; 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: // This function in head_queries.js creates our database with the above data michael@0: populateDB(testData); michael@0: michael@0: // Query michael@0: var query = PlacesUtils.history.getNewQuery(); michael@0: // Set query attributes here... michael@0: michael@0: // query options michael@0: var options = PlacesUtils.history.getNewQueryOptions(); michael@0: // Set queryOptions attributes here 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: compareArrayToResult(testData, root); michael@0: michael@0: // Make some changes to the result set michael@0: // Let's add something first - you can use populateDB to append/update the michael@0: // database too... michael@0: var addItem = [{isInQuery: true, isVisit: true, isDetails: true, title: "moz", michael@0: uri: "http://foo.com/i-am-added.html", lastVisit: jan11_800}]; michael@0: populateDB(addItem); michael@0: michael@0: // Here's an update michael@0: var change1 = [{isDetails: true, uri: "http://foo.com/", michael@0: lastVisit: jan12_1730, title: "moz moz mozzie"}]; michael@0: populateDB(change1); michael@0: michael@0: // Here's a batch update michael@0: var updateBatch = { michael@0: runBatched: function (aUserData) { michael@0: var batchChange = [{isDetails: true, uri: "http://foo.com/changeme2", michael@0: title: "moz", lastVisit: jan7_800}, michael@0: {isPageAnnotation: true, uri: "http://foo.com/begin.html", michael@0: annoName: badAnnoName, annoVal: val}]; michael@0: populateDB(batchChange); michael@0: } michael@0: }; michael@0: michael@0: PlacesUtils.history.runInBatchMode(updateBatch, null); michael@0: michael@0: // Close the container when finished michael@0: root.containerOpen = false; michael@0: }