toolkit/components/places/tests/autocomplete/test_special_search.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/autocomplete/test_special_search.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,184 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/**
     1.9 + * Test for bug 395161 that allows special searches that restrict results to
    1.10 + * history/bookmark/tagged items and title/url matches.
    1.11 + *
    1.12 + * Test 485122 by making sure results don't have tags when restricting result
    1.13 + * to just history either by default behavior or dynamic query restrict.
    1.14 + */
    1.15 +
    1.16 +// Define some shared uris and titles (each page needs its own uri)
    1.17 +let kURIs = [
    1.18 +  "http://url/",
    1.19 +  "http://url/2",
    1.20 +  "http://foo.bar/",
    1.21 +  "http://foo.bar/2",
    1.22 +  "http://url/star",
    1.23 +  "http://url/star/2",
    1.24 +  "http://foo.bar/star",
    1.25 +  "http://foo.bar/star/2",
    1.26 +  "http://url/tag",
    1.27 +  "http://url/tag/2",
    1.28 +  "http://foo.bar/tag",
    1.29 +  "http://foo.bar/tag/2",
    1.30 +];
    1.31 +let kTitles = [
    1.32 +  "title",
    1.33 +  "foo.bar",
    1.34 +];
    1.35 +
    1.36 +// Plain page visits
    1.37 +addPageBook(0, 0); // plain page
    1.38 +addPageBook(1, 1); // title
    1.39 +addPageBook(2, 0); // url
    1.40 +addPageBook(3, 1); // title and url
    1.41 +
    1.42 +// Bookmarked pages (no tag)
    1.43 +addPageBook(4, 0, 0); // bookmarked page
    1.44 +addPageBook(5, 1, 1); // title
    1.45 +addPageBook(6, 0, 0); // url
    1.46 +addPageBook(7, 1, 1); // title and url
    1.47 +
    1.48 +// Tagged pages
    1.49 +addPageBook(8, 0, 0, [1]); // tagged page
    1.50 +addPageBook(9, 1, 1, [1]); // title
    1.51 +addPageBook(10, 0, 0, [1]); // url
    1.52 +addPageBook(11, 1, 1, [1]); // title and url
    1.53 +
    1.54 +// Remove pages from history to treat them as unvisited, so pages that do have
    1.55 +// visits are 0,1,2,3,5,10
    1.56 +removePages([4,6,7,8,9,11]);
    1.57 +// Set some pages as typed
    1.58 +markTyped([0,10], 0);
    1.59 +markTyped([3], 1);
    1.60 +
    1.61 +// Provide for each test: description; search terms; array of gPages indices of
    1.62 +// pages that should match; optional function to be run before the test
    1.63 +let gTests = [
    1.64 +  // Test restricting searches
    1.65 +  ["0: History restrict",
    1.66 +   "^", [0,1,2,3,5,10], ignoreTags],
    1.67 +  ["1: Star restrict",
    1.68 +   "*", [4,5,6,7,8,9,10,11]],
    1.69 +  ["2: Tag restrict",
    1.70 +   "+", [8,9,10,11]],
    1.71 +
    1.72 +  // Test specials as any word position
    1.73 +  ["3: Special as first word",
    1.74 +   "^ foo bar", [1,2,3,5,10], ignoreTags],
    1.75 +  ["4: Special as middle word",
    1.76 +   "foo ^ bar", [1,2,3,5,10], ignoreTags],
    1.77 +  ["5: Special as last word",
    1.78 +   "foo bar ^", [1,2,3,5,10], ignoreTags],
    1.79 +
    1.80 +  // Test restricting and matching searches with a term
    1.81 +  ["6.1: foo ^ -> history",
    1.82 +   "foo ^", [1,2,3,5,10], ignoreTags],
    1.83 +  ["6.2: foo | -> history (change pref)",
    1.84 +   "foo |", [1,2,3,5,10], function() {ignoreTags(); changeRestrict("history", "|"); }],
    1.85 +  ["7.1: foo * -> is star",
    1.86 +   "foo *", [5,6,7,8,9,10,11], function() resetRestrict("history")],
    1.87 +  ["7.2: foo | -> is star (change pref)",
    1.88 +   "foo |", [5,6,7,8,9,10,11], function() changeRestrict("bookmark", "|")],
    1.89 +  ["8.1: foo # -> in title",
    1.90 +   "foo #", [1,3,5,7,8,9,10,11], function() resetRestrict("bookmark")],
    1.91 +  ["8.2: foo | -> in title (change pref)",
    1.92 +   "foo |", [1,3,5,7,8,9,10,11], function() changeRestrict("title", "|")],
    1.93 +  ["9.1: foo @ -> in url",
    1.94 +   "foo @", [2,3,6,7,10,11], function() resetRestrict("title")],
    1.95 +  ["9.2: foo | -> in url (change pref)",
    1.96 +   "foo |", [2,3,6,7,10,11], function() changeRestrict("url", "|")],
    1.97 +  ["10: foo + -> is tag",
    1.98 +   "foo +", [8,9,10,11], function() resetRestrict("url")],
    1.99 +  ["10.2: foo | -> is tag (change pref)",
   1.100 +   "foo |", [8,9,10,11], function() changeRestrict("tag", "|")],
   1.101 +  ["10.3: foo ~ -> is typed",
   1.102 +   "foo ~", [3,10], function() resetRestrict("tag")],
   1.103 +  ["10.4: foo | -> is typed (change pref)",
   1.104 +   "foo |", [3,10], function() changeRestrict("typed", "|")],
   1.105 +
   1.106 +  // Test various pairs of special searches
   1.107 +  ["11: foo ^ * -> history, is star",
   1.108 +   "foo ^ *", [5,10], function() resetRestrict("typed")],
   1.109 +  ["12: foo ^ # -> history, in title",
   1.110 +   "foo ^ #", [1,3,5,10], ignoreTags],
   1.111 +  ["13: foo ^ @ -> history, in url",
   1.112 +   "foo ^ @", [2,3,10], ignoreTags],
   1.113 +  ["14: foo ^ + -> history, is tag",
   1.114 +   "foo ^ +", [10]],
   1.115 +  ["14.1: foo ^ ~ -> history, is typed",
   1.116 +   "foo ^ ~", [3,10], ignoreTags],
   1.117 +  ["15: foo * # -> is star, in title",
   1.118 +   "foo * #", [5,7,8,9,10,11]],
   1.119 +  ["16: foo * @ -> is star, in url",
   1.120 +   "foo * @", [6,7,10,11]],
   1.121 +  ["17: foo * + -> same as +",
   1.122 +   "foo * +", [8,9,10,11]],
   1.123 +  ["17.1: foo * ~ -> is star, is typed",
   1.124 +   "foo * ~", [10]],
   1.125 +  ["18: foo # @ -> in title, in url",
   1.126 +   "foo # @", [3,7,10,11]],
   1.127 +  ["19: foo # + -> in title, is tag",
   1.128 +   "foo # +", [8,9,10,11]],
   1.129 +  ["19.1: foo # ~ -> in title, is typed",
   1.130 +   "foo # ~", [3,10]],
   1.131 +  ["20: foo @ + -> in url, is tag",
   1.132 +   "foo @ +", [10,11]],
   1.133 +  ["20.1: foo @ ~ -> in url, is typed",
   1.134 +   "foo @ ~", [3,10]],
   1.135 +  ["20.2: foo + ~ -> is tag, is typed",
   1.136 +   "foo + ~", [10]],
   1.137 +
   1.138 +  // Test default usage by setting certain bits of default.behavior to 1
   1.139 +  ["21: foo -> default history",
   1.140 +   "foo", [1,2,3,5,10], function() makeDefault(1)],
   1.141 +  ["22: foo -> default history, is star",
   1.142 +   "foo", [5,10], function() makeDefault(3)],
   1.143 +  ["22.1: foo -> default history, is star, is typed",
   1.144 +   "foo", [10], function() makeDefault(35)],
   1.145 +  ["23: foo -> default history, is star, in url",
   1.146 +   "foo", [10], function() makeDefault(19)],
   1.147 +
   1.148 +  // Change the default to be less restrictive to make sure we find more
   1.149 +  ["24: foo -> default is star, in url",
   1.150 +   "foo", [6,7,10,11], function() makeDefault(18)],
   1.151 +  ["25: foo -> default in url",
   1.152 +   "foo", [2,3,6,7,10,11], function() makeDefault(16)],
   1.153 +];
   1.154 +
   1.155 +function makeDefault(aDefault) {
   1.156 +  // We want to ignore tags if we're restricting to history unless we're showing
   1.157 +  if ((aDefault & 1) && !((aDefault & 2) || (aDefault & 4)))
   1.158 +    ignoreTags();
   1.159 +
   1.160 +  prefs.setIntPref("browser.urlbar.default.behavior", aDefault);
   1.161 +}
   1.162 +
   1.163 +function changeRestrict(aType, aChar)
   1.164 +{
   1.165 +  let branch = "browser.urlbar.";
   1.166 +  // "title" and "url" are different from everything else, so special case them.
   1.167 +  if (aType == "title" || aType == "url")
   1.168 +    branch += "match.";
   1.169 +  else
   1.170 +    branch += "restrict.";
   1.171 +
   1.172 +  print("changing restrict for " + aType + " to '" + aChar + "'");
   1.173 +  prefs.setCharPref(branch + aType, aChar);
   1.174 +}
   1.175 +
   1.176 +function resetRestrict(aType)
   1.177 +{
   1.178 +  let branch = "browser.urlbar.";
   1.179 +  // "title" and "url" are different from everything else, so special case them.
   1.180 +  if (aType == "title" || aType == "url")
   1.181 +    branch += "match.";
   1.182 +  else
   1.183 +    branch += "restrict.";
   1.184 +
   1.185 +  if (prefs.prefHasUserValue(branch + aType))
   1.186 +    prefs.clearUserPref(branch + aType);
   1.187 +}

mercurial