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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/queries/stub-test.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,109 @@
     1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +/**
    1.11 + * Pre Test Items go here - Note that if you need time constants or relative
    1.12 + * times, there are several in head_queries.js.
    1.13 + * Other things to do here might be to create some bookmark folders, for access
    1.14 + * to the Places API's you can use the global variables defined in head_queries.js.
    1.15 + * Here is an example of using these to create some bookmark folders:
    1.16 + */
    1.17 + // Create Folder1 from root
    1.18 + PlacesUtils.bookmarks.createFolder(PlacesUtils.placesRootId, "Folder 1",
    1.19 +                                    PlacesUtils.bookmarks.DEFAULT_INDEX);
    1.20 + var folder1Id = PlacesUtils.bookmarks.getChildFolder(PlacesUtils.placesRootId,
    1.21 +                                                      "Folder 1");
    1.22 +
    1.23 + // Make Folder 1a a child of Folder 1
    1.24 + PlacesUtils.bookmarks.createFolder(folder1Id, "Folder 1a",
    1.25 +                                    PlacesUtils.bookmarks.DEFAULT_INDEX);
    1.26 + var folder1aId = PlacesUtils.bookmarks.getChildFolder(folder1Id, "Folder 1a");
    1.27 + 
    1.28 +/** 
    1.29 + * The next thing we do is create a test database for us.  Each test runs with
    1.30 + * its own database (tail_queries.js will clear it after the run).  Take a look
    1.31 + * at the queryData object in head_queries.js, and you'll see how this object
    1.32 + * works.  You can call it anything you like, but I usually use "testData".
    1.33 + * I'll include a couple of example entries in the database.
    1.34 + *
    1.35 + * Note that to use the compareArrayToResult API, you need to put all the
    1.36 + * results that are in the query set at the top of the testData list, and those
    1.37 + * results MUST be in the same sort order as the items in the resulting query.
    1.38 + */
    1.39 +
    1.40 +var testData = [
    1.41 +
    1.42 +  // This puts a history visit item into the database.  Note that I don't
    1.43 +  // specify the "lastVisit" attribute, because I am ok with "lastVisit"
    1.44 +  // defaulting to "today". isInQuery is true as I expect this to turn up in the
    1.45 +  // query I'll be doing down below.
    1.46 +  {isInQuery: true, isVisit: true, uri: "http://foo.com/"},
    1.47 +
    1.48 +  // And so on to get a database that has enough elements to adequately test
    1.49 +  // the edges of your query set.  Be sure to include things that are outside
    1.50 +  // the query set but can be updated so that they are included in the query
    1.51 +  // set. Here's a more complicated example to finish our examples.
    1.52 +  {isInQuery: true, isVisit: true, isDetails: true, title: "taggariffic",
    1.53 +   uri: "http://foo.com/tagging/test.html", lastVisit: beginTime, isTag: true,
    1.54 +   tagArray: ["moz"] }];
    1.55 +
    1.56 +  
    1.57 +/**
    1.58 + * run_test is where the magic happens.  This is automatically run by the test
    1.59 + * harness.  It is where you do the work of creating the query, running it, and
    1.60 + * playing with the result set.
    1.61 + */
    1.62 +function run_test() {
    1.63 +
    1.64 +  // This function in head_queries.js creates our database with the above data
    1.65 +  populateDB(testData);
    1.66 +
    1.67 +  // Query
    1.68 +  var query = PlacesUtils.history.getNewQuery();
    1.69 +  // Set query attributes here...
    1.70 +  
    1.71 +  // query options
    1.72 +  var options = PlacesUtils.history.getNewQueryOptions();
    1.73 +  // Set queryOptions attributes here
    1.74 +
    1.75 +  // Results - this gets the result set and opens it for reading and modification.
    1.76 +  var result = PlacesUtils.history.executeQuery(query, options);
    1.77 +  var root = result.root;
    1.78 +  root.containerOpen = true;
    1.79 +  
    1.80 +  // You can use this to compare the data in the array with the result set,
    1.81 +  // if the array's isInQuery: true items are sorted the same way as the result
    1.82 +  // set.
    1.83 +  compareArrayToResult(testData, root);
    1.84 +
    1.85 +  // Make some changes to the result set
    1.86 +  // Let's add something first - you can use populateDB to append/update the
    1.87 +  // database too...
    1.88 +  var addItem = [{isInQuery: true, isVisit: true, isDetails: true, title: "moz",
    1.89 +                 uri: "http://foo.com/i-am-added.html", lastVisit: jan11_800}];
    1.90 +  populateDB(addItem);
    1.91 +  
    1.92 +  // Here's an update
    1.93 +  var change1 = [{isDetails: true, uri: "http://foo.com/",
    1.94 +                  lastVisit: jan12_1730, title: "moz moz mozzie"}];
    1.95 +  populateDB(change1);
    1.96 +
    1.97 +  // Here's a batch update
    1.98 +  var updateBatch = {
    1.99 +    runBatched: function (aUserData) {
   1.100 +      var batchChange = [{isDetails: true, uri: "http://foo.com/changeme2",
   1.101 +                          title: "moz", lastVisit: jan7_800},
   1.102 +                         {isPageAnnotation: true, uri: "http://foo.com/begin.html",
   1.103 +                          annoName: badAnnoName, annoVal: val}];
   1.104 +      populateDB(batchChange);
   1.105 +    }
   1.106 +  };
   1.107 +
   1.108 +  PlacesUtils.history.runInBatchMode(updateBatch, null);
   1.109 +
   1.110 +  // Close the container when finished
   1.111 +  root.containerOpen = false;
   1.112 +}

mercurial