toolkit/components/places/tests/bookmarks/test_savedsearches.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     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/. */
     7 // get bookmarks root id
     8 var root = PlacesUtils.bookmarksMenuFolderId;
    10 // a search term that matches a default bookmark
    11 const searchTerm = "about";
    13 var testRoot;
    15 // main
    16 function run_test() {
    17   // create a folder to hold all the tests
    18   // this makes the tests more tolerant of changes to the default bookmarks set
    19   // also, name it using the search term, for testing that containers that match don't show up in query results
    20   testRoot = PlacesUtils.bookmarks.createFolder(
    21     root, searchTerm, PlacesUtils.bookmarks.DEFAULT_INDEX);
    23   run_next_test();
    24 }
    26 add_test(function test_savedsearches_bookmarks() {
    27   // add a bookmark that matches the search term
    28   var bookmarkId = PlacesUtils.bookmarks.insertBookmark(
    29     root, uri("http://foo.com"), PlacesUtils.bookmarks.DEFAULT_INDEX,
    30     searchTerm);
    32   // create a saved-search that matches a default bookmark
    33   var searchId = PlacesUtils.bookmarks.insertBookmark(
    34     testRoot, uri("place:terms=" + searchTerm + "&excludeQueries=1&expandQueries=1&queryType=1"),
    35     PlacesUtils.bookmarks.DEFAULT_INDEX, searchTerm);
    37   // query for the test root, expandQueries=0
    38   // the query should show up as a regular bookmark
    39   try {
    40     var options = PlacesUtils.history.getNewQueryOptions();
    41     options.expandQueries = 0;
    42     var query = PlacesUtils.history.getNewQuery();
    43     query.setFolders([testRoot], 1);
    44     var result = PlacesUtils.history.executeQuery(query, options);
    45     var rootNode = result.root;
    46     rootNode.containerOpen = true;
    47     var cc = rootNode.childCount;
    48     do_check_eq(cc, 1);
    49     for (var i = 0; i < cc; i++) {
    50       var node = rootNode.getChild(i);
    51       // test that queries have valid itemId
    52       do_check_true(node.itemId > 0);
    53       // test that the container is closed
    54       node.QueryInterface(Ci.nsINavHistoryContainerResultNode);
    55       do_check_eq(node.containerOpen, false);
    56     }
    57     rootNode.containerOpen = false;
    58   }
    59   catch(ex) {
    60     do_throw("expandQueries=0 query error: " + ex);
    61   }
    63   // bookmark saved search
    64   // query for the test root, expandQueries=1
    65   // the query should show up as a query container, with 1 child
    66   try {
    67     var options = PlacesUtils.history.getNewQueryOptions();
    68     options.expandQueries = 1;
    69     var query = PlacesUtils.history.getNewQuery();
    70     query.setFolders([testRoot], 1);
    71     var result = PlacesUtils.history.executeQuery(query, options);
    72     var rootNode = result.root;
    73     rootNode.containerOpen = true;
    74     var cc = rootNode.childCount;
    75     do_check_eq(cc, 1);
    76     for (var i = 0; i < cc; i++) {
    77       var node = rootNode.getChild(i);
    78       // test that query node type is container when expandQueries=1
    79       do_check_eq(node.type, node.RESULT_TYPE_QUERY);
    80       // test that queries (as containers) have valid itemId
    81       do_check_true(node.itemId > 0);
    82       node.QueryInterface(Ci.nsINavHistoryContainerResultNode);
    83       node.containerOpen = true;
    85       // test that queries have children when excludeItems=1
    86       // test that query nodes don't show containers (shouldn't have our folder that matches)
    87       // test that queries don't show themselves in query results (shouldn't have our saved search)
    88       do_check_eq(node.childCount, 1);
    90       // test that bookmark shows in query results
    91       var item = node.getChild(0);
    92       do_check_eq(item.itemId, bookmarkId);
    94       // XXX - FAILING - test live-update of query results - add a bookmark that matches the query
    95       //var tmpBmId = PlacesUtils.bookmarks.insertBookmark(
    96       //  root, uri("http://" + searchTerm + ".com"),
    97       //  PlacesUtils.bookmarks.DEFAULT_INDEX, searchTerm + "blah");
    98       //do_check_eq(query.childCount, 2);
   100       // XXX - test live-update of query results - delete a bookmark that matches the query
   101       //PlacesUtils.bookmarks.removeItem(tmpBMId);
   102       //do_check_eq(query.childCount, 1);
   104       // test live-update of query results - add a folder that matches the query
   105       PlacesUtils.bookmarks.createFolder(
   106         root, searchTerm + "zaa", PlacesUtils.bookmarks.DEFAULT_INDEX);
   107       do_check_eq(node.childCount, 1);
   108       // test live-update of query results - add a query that matches the query
   109       PlacesUtils.bookmarks.insertBookmark(
   110         root, uri("place:terms=foo&excludeQueries=1&expandQueries=1&queryType=1"),
   111         PlacesUtils.bookmarks.DEFAULT_INDEX, searchTerm + "blah");
   112       do_check_eq(node.childCount, 1);
   113     }
   114     rootNode.containerOpen = false;
   115   }
   116   catch(ex) {
   117     do_throw("expandQueries=1 bookmarks query: " + ex);
   118   }
   120   // delete the bookmark search
   121   PlacesUtils.bookmarks.removeItem(searchId);
   123   run_next_test();
   124 });
   126 add_task(function test_savedsearches_history() {
   127   // add a visit that matches the search term
   128   var testURI = uri("http://" + searchTerm + ".com");
   129   yield promiseAddVisits({ uri: testURI, title: searchTerm });
   131   // create a saved-search that matches the visit we added
   132   var searchId = PlacesUtils.bookmarks.insertBookmark(testRoot,
   133     uri("place:terms=" + searchTerm + "&excludeQueries=1&expandQueries=1&queryType=0"),
   134     PlacesUtils.bookmarks.DEFAULT_INDEX, searchTerm);
   136   // query for the test root, expandQueries=1
   137   // the query should show up as a query container, with 1 child
   138   try {
   139     var options = PlacesUtils.history.getNewQueryOptions();
   140     options.expandQueries = 1;
   141     var query = PlacesUtils.history.getNewQuery();
   142     query.setFolders([testRoot], 1);
   143     var result = PlacesUtils.history.executeQuery(query, options);
   144     var rootNode = result.root;
   145     rootNode.containerOpen = true;
   146     var cc = rootNode.childCount;
   147     do_check_eq(cc, 1);
   148     for (var i = 0; i < cc; i++) {
   149       var node = rootNode.getChild(i);
   150       // test that query node type is container when expandQueries=1
   151       do_check_eq(node.type, node.RESULT_TYPE_QUERY);
   152       // test that queries (as containers) have valid itemId
   153       do_check_eq(node.itemId, searchId);
   154       node.QueryInterface(Ci.nsINavHistoryContainerResultNode);
   155       node.containerOpen = true;
   157       // test that queries have children when excludeItems=1
   158       // test that query nodes don't show containers (shouldn't have our folder that matches)
   159       // test that queries don't show themselves in query results (shouldn't have our saved search)
   160       do_check_eq(node.childCount, 1);
   162       // test that history visit shows in query results
   163       var item = node.getChild(0);
   164       do_check_eq(item.type, item.RESULT_TYPE_URI);
   165       do_check_eq(item.itemId, -1); // history visit
   166       do_check_eq(item.uri, testURI.spec); // history visit
   168       // test live-update of query results - add a history visit that matches the query
   169       yield promiseAddVisits({
   170         uri: uri("http://foo.com"),
   171         title: searchTerm + "blah"
   172       });
   173       do_check_eq(node.childCount, 2);
   175       // test live-update of query results - delete a history visit that matches the query
   176       PlacesUtils.history.removePage(uri("http://foo.com"));
   177       do_check_eq(node.childCount, 1);
   178       node.containerOpen = false;
   179     }
   181     // test live-update of moved queries
   182     var tmpFolderId = PlacesUtils.bookmarks.createFolder(
   183       testRoot, "foo", PlacesUtils.bookmarks.DEFAULT_INDEX);
   184     PlacesUtils.bookmarks.moveItem(
   185       searchId, tmpFolderId, PlacesUtils.bookmarks.DEFAULT_INDEX);
   186     var tmpFolderNode = rootNode.getChild(0);
   187     do_check_eq(tmpFolderNode.itemId, tmpFolderId);
   188     tmpFolderNode.QueryInterface(Ci.nsINavHistoryContainerResultNode);
   189     tmpFolderNode.containerOpen = true;
   190     do_check_eq(tmpFolderNode.childCount, 1);
   192     // test live-update of renamed queries
   193     PlacesUtils.bookmarks.setItemTitle(searchId, "foo");
   194     do_check_eq(tmpFolderNode.title, "foo");
   196     // test live-update of deleted queries
   197     PlacesUtils.bookmarks.removeItem(searchId);
   198     try {
   199       var tmpFolderNode = root.getChild(1);
   200       do_throw("query was not removed");
   201     } catch(ex) {}
   203     tmpFolderNode.containerOpen = false;
   204     rootNode.containerOpen = false;
   205   }
   206   catch(ex) {
   207     do_throw("expandQueries=1 bookmarks query: " + ex);
   208   }
   209 });

mercurial