toolkit/components/places/tests/queries/test_onlyBookmarked.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/queries/test_onlyBookmarked.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,135 @@
     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 + * The next thing we do is create a test database for us.  Each test runs with
    1.12 + * its own database (tail_queries.js will clear it after the run).  Take a look
    1.13 + * at the queryData object in head_queries.js, and you'll see how this object
    1.14 + * works.  You can call it anything you like, but I usually use "testData".
    1.15 + * I'll include a couple of example entries in the database.
    1.16 + *
    1.17 + * Note that to use the compareArrayToResult API, you need to put all the
    1.18 + * results that are in the query set at the top of the testData list, and those
    1.19 + * results MUST be in the same sort order as the items in the resulting query.
    1.20 + */
    1.21 +
    1.22 +var testData = [
    1.23 +  // Add a bookmark that should be in the results
    1.24 +  { isBookmark: true,
    1.25 +    uri: "http://bookmarked.com/",
    1.26 +    parentFolder: PlacesUtils.toolbarFolderId,
    1.27 +    index: PlacesUtils.bookmarks.DEFAULT_INDEX,
    1.28 +    title: "",
    1.29 +    isInQuery: true },
    1.30 +
    1.31 +  // Add a bookmark that should not be in the results
    1.32 +  { isBookmark: true,
    1.33 +    uri: "http://bookmarked-elsewhere.com/",
    1.34 +    parentFolder: PlacesUtils.bookmarksMenuFolderId,
    1.35 +    index: PlacesUtils.bookmarks.DEFAULT_INDEX,
    1.36 +    title: "",
    1.37 +    isInQuery: false },
    1.38 +
    1.39 +  // Add an un-bookmarked visit
    1.40 +  { isVisit: true,
    1.41 +    uri: "http://notbookmarked.com/",
    1.42 +    isInQuery: false }
    1.43 +];
    1.44 +
    1.45 +
    1.46 +/**
    1.47 + * run_test is where the magic happens.  This is automatically run by the test
    1.48 + * harness.  It is where you do the work of creating the query, running it, and
    1.49 + * playing with the result set.
    1.50 + */
    1.51 +function run_test()
    1.52 +{
    1.53 +  run_next_test();
    1.54 +}
    1.55 +
    1.56 +add_task(function test_onlyBookmarked()
    1.57 +{
    1.58 +  // This function in head_queries.js creates our database with the above data
    1.59 +  yield task_populateDB(testData);
    1.60 +
    1.61 +  // Query
    1.62 +  var query = PlacesUtils.history.getNewQuery();
    1.63 +  query.setFolders([PlacesUtils.toolbarFolderId], 1);
    1.64 +  query.onlyBookmarked = true;
    1.65 +  
    1.66 +  // query options
    1.67 +  var options = PlacesUtils.history.getNewQueryOptions();
    1.68 +  options.queryType = options.QUERY_TYPE_HISTORY;
    1.69 +
    1.70 +  // Results - this gets the result set and opens it for reading and modification.
    1.71 +  var result = PlacesUtils.history.executeQuery(query, options);
    1.72 +  var root = result.root;
    1.73 +  root.containerOpen = true;
    1.74 +  
    1.75 +  // You can use this to compare the data in the array with the result set,
    1.76 +  // if the array's isInQuery: true items are sorted the same way as the result
    1.77 +  // set.
    1.78 +  LOG("begin first test");
    1.79 +  compareArrayToResult(testData, root);
    1.80 +  LOG("end first test");
    1.81 +
    1.82 +  /* ******************
    1.83 +  Test live-update
    1.84 +  ********************/
    1.85 + 
    1.86 +  var liveUpdateTestData = [
    1.87 +    //Add a bookmark that should show up
    1.88 +    { isBookmark: true,
    1.89 +      uri: "http://bookmarked2.com/",
    1.90 +      parentFolder: PlacesUtils.toolbarFolderId,
    1.91 +      index: PlacesUtils.bookmarks.DEFAULT_INDEX,
    1.92 +      title: "",
    1.93 +      isInQuery: true },
    1.94 +
    1.95 +    //Add a bookmark that should not show up
    1.96 +    { isBookmark: true,
    1.97 +      uri: "http://bookmarked-elsewhere2.com/",
    1.98 +      parentFolder: PlacesUtils.bookmarksMenuFolderId,
    1.99 +      index: PlacesUtils.bookmarks.DEFAULT_INDEX,
   1.100 +      title: "",
   1.101 +      isInQuery: false }
   1.102 +  ];
   1.103 +  
   1.104 +  yield task_populateDB(liveUpdateTestData); // add to the db
   1.105 +
   1.106 +  // add to the test data
   1.107 +  testData.push(liveUpdateTestData[0]);
   1.108 +  testData.push(liveUpdateTestData[1]);
   1.109 +
   1.110 +  // re-query and test
   1.111 +  LOG("begin live-update test");
   1.112 +  compareArrayToResult(testData, root);
   1.113 +  LOG("end live-update test");
   1.114 +/*
   1.115 +  // we are actually not updating during a batch.
   1.116 +  // see bug 432706 for details.
   1.117 +
   1.118 +  // Here's a batch update
   1.119 +  var updateBatch = {
   1.120 +    runBatched: function (aUserData) {
   1.121 +      liveUpdateTestData[0].uri = "http://bookmarked3.com";
   1.122 +      liveUpdateTestData[1].uri = "http://bookmarked-elsewhere3.com";
   1.123 +      populateDB(liveUpdateTestData);
   1.124 +      testData.push(liveUpdateTestData[0]);
   1.125 +      testData.push(liveUpdateTestData[1]);
   1.126 +    }
   1.127 +  };
   1.128 +
   1.129 +  PlacesUtils.history.runInBatchMode(updateBatch, null);
   1.130 +
   1.131 +  // re-query and test
   1.132 +  LOG("begin batched test");
   1.133 +  compareArrayToResult(testData, root);
   1.134 +  LOG("end batched test");
   1.135 +*/
   1.136 +  // Close the container when finished
   1.137 +  root.containerOpen = false;
   1.138 +});

mercurial