toolkit/components/places/tests/queries/stub-test.js

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

mercurial