Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 sts=2 et: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | /** |
michael@0 | 8 | * Pre Test Items go here - Note that if you need time constants or relative |
michael@0 | 9 | * times, there are several in head_queries.js. |
michael@0 | 10 | * Other things to do here might be to create some bookmark folders, for access |
michael@0 | 11 | * to the Places API's you can use the global variables defined in head_queries.js. |
michael@0 | 12 | * Here is an example of using these to create some bookmark folders: |
michael@0 | 13 | */ |
michael@0 | 14 | // Create Folder1 from root |
michael@0 | 15 | PlacesUtils.bookmarks.createFolder(PlacesUtils.placesRootId, "Folder 1", |
michael@0 | 16 | PlacesUtils.bookmarks.DEFAULT_INDEX); |
michael@0 | 17 | var folder1Id = PlacesUtils.bookmarks.getChildFolder(PlacesUtils.placesRootId, |
michael@0 | 18 | "Folder 1"); |
michael@0 | 19 | |
michael@0 | 20 | // Make Folder 1a a child of Folder 1 |
michael@0 | 21 | PlacesUtils.bookmarks.createFolder(folder1Id, "Folder 1a", |
michael@0 | 22 | PlacesUtils.bookmarks.DEFAULT_INDEX); |
michael@0 | 23 | var folder1aId = PlacesUtils.bookmarks.getChildFolder(folder1Id, "Folder 1a"); |
michael@0 | 24 | |
michael@0 | 25 | /** |
michael@0 | 26 | * The next thing we do is create a test database for us. Each test runs with |
michael@0 | 27 | * its own database (tail_queries.js will clear it after the run). Take a look |
michael@0 | 28 | * at the queryData object in head_queries.js, and you'll see how this object |
michael@0 | 29 | * works. You can call it anything you like, but I usually use "testData". |
michael@0 | 30 | * I'll include a couple of example entries in the database. |
michael@0 | 31 | * |
michael@0 | 32 | * Note that to use the compareArrayToResult API, you need to put all the |
michael@0 | 33 | * results that are in the query set at the top of the testData list, and those |
michael@0 | 34 | * results MUST be in the same sort order as the items in the resulting query. |
michael@0 | 35 | */ |
michael@0 | 36 | |
michael@0 | 37 | var testData = [ |
michael@0 | 38 | |
michael@0 | 39 | // This puts a history visit item into the database. Note that I don't |
michael@0 | 40 | // specify the "lastVisit" attribute, because I am ok with "lastVisit" |
michael@0 | 41 | // defaulting to "today". isInQuery is true as I expect this to turn up in the |
michael@0 | 42 | // query I'll be doing down below. |
michael@0 | 43 | {isInQuery: true, isVisit: true, uri: "http://foo.com/"}, |
michael@0 | 44 | |
michael@0 | 45 | // And so on to get a database that has enough elements to adequately test |
michael@0 | 46 | // the edges of your query set. Be sure to include things that are outside |
michael@0 | 47 | // the query set but can be updated so that they are included in the query |
michael@0 | 48 | // set. Here's a more complicated example to finish our examples. |
michael@0 | 49 | {isInQuery: true, isVisit: true, isDetails: true, title: "taggariffic", |
michael@0 | 50 | uri: "http://foo.com/tagging/test.html", lastVisit: beginTime, isTag: true, |
michael@0 | 51 | tagArray: ["moz"] }]; |
michael@0 | 52 | |
michael@0 | 53 | |
michael@0 | 54 | /** |
michael@0 | 55 | * run_test is where the magic happens. This is automatically run by the test |
michael@0 | 56 | * harness. It is where you do the work of creating the query, running it, and |
michael@0 | 57 | * playing with the result set. |
michael@0 | 58 | */ |
michael@0 | 59 | function run_test() { |
michael@0 | 60 | |
michael@0 | 61 | // This function in head_queries.js creates our database with the above data |
michael@0 | 62 | populateDB(testData); |
michael@0 | 63 | |
michael@0 | 64 | // Query |
michael@0 | 65 | var query = PlacesUtils.history.getNewQuery(); |
michael@0 | 66 | // Set query attributes here... |
michael@0 | 67 | |
michael@0 | 68 | // query options |
michael@0 | 69 | var options = PlacesUtils.history.getNewQueryOptions(); |
michael@0 | 70 | // Set queryOptions attributes here |
michael@0 | 71 | |
michael@0 | 72 | // Results - this gets the result set and opens it for reading and modification. |
michael@0 | 73 | var result = PlacesUtils.history.executeQuery(query, options); |
michael@0 | 74 | var root = result.root; |
michael@0 | 75 | root.containerOpen = true; |
michael@0 | 76 | |
michael@0 | 77 | // You can use this to compare the data in the array with the result set, |
michael@0 | 78 | // if the array's isInQuery: true items are sorted the same way as the result |
michael@0 | 79 | // set. |
michael@0 | 80 | compareArrayToResult(testData, root); |
michael@0 | 81 | |
michael@0 | 82 | // Make some changes to the result set |
michael@0 | 83 | // Let's add something first - you can use populateDB to append/update the |
michael@0 | 84 | // database too... |
michael@0 | 85 | var addItem = [{isInQuery: true, isVisit: true, isDetails: true, title: "moz", |
michael@0 | 86 | uri: "http://foo.com/i-am-added.html", lastVisit: jan11_800}]; |
michael@0 | 87 | populateDB(addItem); |
michael@0 | 88 | |
michael@0 | 89 | // Here's an update |
michael@0 | 90 | var change1 = [{isDetails: true, uri: "http://foo.com/", |
michael@0 | 91 | lastVisit: jan12_1730, title: "moz moz mozzie"}]; |
michael@0 | 92 | populateDB(change1); |
michael@0 | 93 | |
michael@0 | 94 | // Here's a batch update |
michael@0 | 95 | var updateBatch = { |
michael@0 | 96 | runBatched: function (aUserData) { |
michael@0 | 97 | var batchChange = [{isDetails: true, uri: "http://foo.com/changeme2", |
michael@0 | 98 | title: "moz", lastVisit: jan7_800}, |
michael@0 | 99 | {isPageAnnotation: true, uri: "http://foo.com/begin.html", |
michael@0 | 100 | annoName: badAnnoName, annoVal: val}]; |
michael@0 | 101 | populateDB(batchChange); |
michael@0 | 102 | } |
michael@0 | 103 | }; |
michael@0 | 104 | |
michael@0 | 105 | PlacesUtils.history.runInBatchMode(updateBatch, null); |
michael@0 | 106 | |
michael@0 | 107 | // Close the container when finished |
michael@0 | 108 | root.containerOpen = false; |
michael@0 | 109 | } |