toolkit/components/places/tests/unit/test_000_frecency.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.

michael@0 1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 /*
michael@0 8
michael@0 9 Autocomplete Frecency Tests
michael@0 10
michael@0 11 - add a visit for each score permutation
michael@0 12 - search
michael@0 13 - test number of matches
michael@0 14 - test each item's location in results
michael@0 15
michael@0 16 */
michael@0 17
michael@0 18 try {
michael@0 19 var histsvc = Cc["@mozilla.org/browser/nav-history-service;1"].
michael@0 20 getService(Ci.nsINavHistoryService);
michael@0 21 var bmsvc = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
michael@0 22 getService(Ci.nsINavBookmarksService);
michael@0 23 var prefs = Cc["@mozilla.org/preferences-service;1"].
michael@0 24 getService(Ci.nsIPrefBranch);
michael@0 25 } catch(ex) {
michael@0 26 do_throw("Could not get services\n");
michael@0 27 }
michael@0 28
michael@0 29 var bucketPrefs = [
michael@0 30 [ "firstBucketCutoff", "firstBucketWeight"],
michael@0 31 [ "secondBucketCutoff", "secondBucketWeight"],
michael@0 32 [ "thirdBucketCutoff", "thirdBucketWeight"],
michael@0 33 [ "fourthBucketCutoff", "fourthBucketWeight"],
michael@0 34 [ null, "defaultBucketWeight"]
michael@0 35 ];
michael@0 36
michael@0 37 var bonusPrefs = {
michael@0 38 embedVisitBonus: Ci.nsINavHistoryService.TRANSITION_EMBED,
michael@0 39 framedLinkVisitBonus: Ci.nsINavHistoryService.TRANSITION_FRAMED_LINK,
michael@0 40 linkVisitBonus: Ci.nsINavHistoryService.TRANSITION_LINK,
michael@0 41 typedVisitBonus: Ci.nsINavHistoryService.TRANSITION_TYPED,
michael@0 42 bookmarkVisitBonus: Ci.nsINavHistoryService.TRANSITION_BOOKMARK,
michael@0 43 downloadVisitBonus: Ci.nsINavHistoryService.TRANSITION_DOWNLOAD,
michael@0 44 permRedirectVisitBonus: Ci.nsINavHistoryService.TRANSITION_REDIRECT_PERMANENT,
michael@0 45 tempRedirectVisitBonus: Ci.nsINavHistoryService.TRANSITION_REDIRECT_TEMPORARY,
michael@0 46 };
michael@0 47
michael@0 48 // create test data
michael@0 49 var searchTerm = "frecency";
michael@0 50 var results = [];
michael@0 51 var matchCount = 0;
michael@0 52 var now = Date.now();
michael@0 53 var prefPrefix = "places.frecency.";
michael@0 54
michael@0 55 function task_initializeBucket(bucket) {
michael@0 56 let [cutoffName, weightName] = bucket;
michael@0 57 // get pref values
michael@0 58 var weight = 0, cutoff = 0, bonus = 0;
michael@0 59 try {
michael@0 60 weight = prefs.getIntPref(prefPrefix + weightName);
michael@0 61 } catch(ex) {}
michael@0 62 try {
michael@0 63 cutoff = prefs.getIntPref(prefPrefix + cutoffName);
michael@0 64 } catch(ex) {}
michael@0 65
michael@0 66 if (cutoff < 1)
michael@0 67 return;
michael@0 68
michael@0 69 // generate a date within the cutoff period
michael@0 70 var dateInPeriod = (now - ((cutoff - 1) * 86400 * 1000)) * 1000;
michael@0 71
michael@0 72 for (let [bonusName, visitType] in Iterator(bonusPrefs)) {
michael@0 73 var frecency = -1;
michael@0 74 var calculatedURI = null;
michael@0 75 var matchTitle = "";
michael@0 76 var bonusValue = prefs.getIntPref(prefPrefix + bonusName);
michael@0 77 // unvisited (only for first cutoff date bucket)
michael@0 78 if (bonusName == "unvisitedBookmarkBonus" || bonusName == "unvisitedTypedBonus") {
michael@0 79 if (cutoffName == "firstBucketCutoff") {
michael@0 80 var points = Math.ceil(bonusValue / parseFloat(100.0) * weight);
michael@0 81 var visitCount = 1; //bonusName == "unvisitedBookmarkBonus" ? 1 : 0;
michael@0 82 frecency = Math.ceil(visitCount * points);
michael@0 83 calculatedURI = uri("http://" + searchTerm + ".com/" +
michael@0 84 bonusName + ":" + bonusValue + "/cutoff:" + cutoff +
michael@0 85 "/weight:" + weight + "/frecency:" + frecency);
michael@0 86 if (bonusName == "unvisitedBookmarkBonus") {
michael@0 87 matchTitle = searchTerm + "UnvisitedBookmark";
michael@0 88 bmsvc.insertBookmark(bmsvc.unfiledBookmarksFolder, calculatedURI, bmsvc.DEFAULT_INDEX, matchTitle);
michael@0 89 }
michael@0 90 else {
michael@0 91 matchTitle = searchTerm + "UnvisitedTyped";
michael@0 92 yield promiseAddVisits({
michael@0 93 uri: calculatedURI,
michael@0 94 title: matchTitle,
michael@0 95 transition: visitType,
michael@0 96 visitDate: now
michael@0 97 });
michael@0 98 histsvc.markPageAsTyped(calculatedURI);
michael@0 99 }
michael@0 100 }
michael@0 101 }
michael@0 102 else {
michael@0 103 // visited
michael@0 104 // visited bookmarks get the visited bookmark bonus twice
michael@0 105 if (visitType == Ci.nsINavHistoryService.TRANSITION_BOOKMARK)
michael@0 106 bonusValue = bonusValue * 2;
michael@0 107
michael@0 108 var points = Math.ceil(1 * ((bonusValue / parseFloat(100.000000)).toFixed(6) * weight) / 1);
michael@0 109 if (!points) {
michael@0 110 if (visitType == Ci.nsINavHistoryService.TRANSITION_EMBED ||
michael@0 111 visitType == Ci.nsINavHistoryService.TRANSITION_FRAMED_LINK ||
michael@0 112 visitType == Ci.nsINavHistoryService.TRANSITION_DOWNLOAD ||
michael@0 113 bonusName == "defaultVisitBonus")
michael@0 114 frecency = 0;
michael@0 115 else
michael@0 116 frecency = -1;
michael@0 117 }
michael@0 118 else
michael@0 119 frecency = points;
michael@0 120 calculatedURI = uri("http://" + searchTerm + ".com/" +
michael@0 121 bonusName + ":" + bonusValue + "/cutoff:" + cutoff +
michael@0 122 "/weight:" + weight + "/frecency:" + frecency);
michael@0 123 if (visitType == Ci.nsINavHistoryService.TRANSITION_BOOKMARK) {
michael@0 124 matchTitle = searchTerm + "Bookmarked";
michael@0 125 bmsvc.insertBookmark(bmsvc.unfiledBookmarksFolder, calculatedURI, bmsvc.DEFAULT_INDEX, matchTitle);
michael@0 126 }
michael@0 127 else
michael@0 128 matchTitle = calculatedURI.spec.substr(calculatedURI.spec.lastIndexOf("/")+1);
michael@0 129 yield promiseAddVisits({
michael@0 130 uri: calculatedURI,
michael@0 131 transition: visitType,
michael@0 132 visitDate: dateInPeriod
michael@0 133 });
michael@0 134 }
michael@0 135
michael@0 136 if (calculatedURI && frecency) {
michael@0 137 results.push([calculatedURI, frecency, matchTitle]);
michael@0 138 yield promiseAddVisits({
michael@0 139 uri: calculatedURI,
michael@0 140 title: matchTitle,
michael@0 141 transition: visitType,
michael@0 142 visitDate: dateInPeriod
michael@0 143 });
michael@0 144 }
michael@0 145 }
michael@0 146 }
michael@0 147
michael@0 148 function AutoCompleteInput(aSearches) {
michael@0 149 this.searches = aSearches;
michael@0 150 }
michael@0 151 AutoCompleteInput.prototype = {
michael@0 152 constructor: AutoCompleteInput,
michael@0 153
michael@0 154 searches: null,
michael@0 155
michael@0 156 minResultsForPopup: 0,
michael@0 157 timeout: 10,
michael@0 158 searchParam: "",
michael@0 159 textValue: "",
michael@0 160 disableAutoComplete: false,
michael@0 161 completeDefaultIndex: false,
michael@0 162
michael@0 163 get searchCount() {
michael@0 164 return this.searches.length;
michael@0 165 },
michael@0 166
michael@0 167 getSearchAt: function(aIndex) {
michael@0 168 return this.searches[aIndex];
michael@0 169 },
michael@0 170
michael@0 171 onSearchBegin: function() {},
michael@0 172 onSearchComplete: function() {},
michael@0 173
michael@0 174 popupOpen: false,
michael@0 175
michael@0 176 popup: {
michael@0 177 setSelectedIndex: function(aIndex) {},
michael@0 178 invalidate: function() {},
michael@0 179
michael@0 180 // nsISupports implementation
michael@0 181 QueryInterface: function(iid) {
michael@0 182 if (iid.equals(Ci.nsISupports) ||
michael@0 183 iid.equals(Ci.nsIAutoCompletePopup))
michael@0 184 return this;
michael@0 185
michael@0 186 throw Components.results.NS_ERROR_NO_INTERFACE;
michael@0 187 }
michael@0 188 },
michael@0 189
michael@0 190 // nsISupports implementation
michael@0 191 QueryInterface: function(iid) {
michael@0 192 if (iid.equals(Ci.nsISupports) ||
michael@0 193 iid.equals(Ci.nsIAutoCompleteInput))
michael@0 194 return this;
michael@0 195
michael@0 196 throw Components.results.NS_ERROR_NO_INTERFACE;
michael@0 197 }
michael@0 198 }
michael@0 199
michael@0 200 function run_test()
michael@0 201 {
michael@0 202 run_next_test();
michael@0 203 }
michael@0 204
michael@0 205 add_task(function test_frecency()
michael@0 206 {
michael@0 207 for (let [, bucket] in Iterator(bucketPrefs)) {
michael@0 208 yield task_initializeBucket(bucket);
michael@0 209 }
michael@0 210
michael@0 211 // sort results by frecency
michael@0 212 results.sort(function(a,b) b[1] - a[1]);
michael@0 213 // Make sure there's enough results returned
michael@0 214 prefs.setIntPref("browser.urlbar.maxRichResults", results.length);
michael@0 215
michael@0 216 // DEBUG
michael@0 217 //results.every(function(el) { dump("result: " + el[1] + ": " + el[0].spec + " (" + el[2] + ")\n"); return true; })
michael@0 218
michael@0 219 yield promiseAsyncUpdates();
michael@0 220
michael@0 221 var controller = Components.classes["@mozilla.org/autocomplete/controller;1"].
michael@0 222 getService(Components.interfaces.nsIAutoCompleteController);
michael@0 223
michael@0 224 // Make an AutoCompleteInput that uses our searches
michael@0 225 // and confirms results on search complete
michael@0 226 var input = new AutoCompleteInput(["history"]);
michael@0 227
michael@0 228 controller.input = input;
michael@0 229
michael@0 230 // always search in history + bookmarks, no matter what the default is
michael@0 231 prefs.setIntPref("browser.urlbar.search.sources", 3);
michael@0 232 prefs.setIntPref("browser.urlbar.default.behavior", 0);
michael@0 233
michael@0 234 var numSearchesStarted = 0;
michael@0 235 input.onSearchBegin = function() {
michael@0 236 numSearchesStarted++;
michael@0 237 do_check_eq(numSearchesStarted, 1);
michael@0 238 };
michael@0 239
michael@0 240 let deferred = Promise.defer();
michael@0 241 input.onSearchComplete = function() {
michael@0 242 do_check_eq(numSearchesStarted, 1);
michael@0 243 do_check_eq(controller.searchStatus,
michael@0 244 Ci.nsIAutoCompleteController.STATUS_COMPLETE_MATCH);
michael@0 245
michael@0 246 // test that all records with non-zero frecency were matched
michael@0 247 do_check_eq(controller.matchCount, results.length);
michael@0 248
michael@0 249 // test that matches are sorted by frecency
michael@0 250 for (var i = 0; i < controller.matchCount; i++) {
michael@0 251 let searchURL = controller.getValueAt(i);
michael@0 252 let expectURL = results[i][0].spec;
michael@0 253 if (searchURL == expectURL) {
michael@0 254 do_check_eq(controller.getValueAt(i), results[i][0].spec);
michael@0 255 do_check_eq(controller.getCommentAt(i), results[i][2]);
michael@0 256 } else {
michael@0 257 // If the results didn't match exactly, perhaps it's still the right
michael@0 258 // frecency just in the wrong "order" (order of same frecency is
michael@0 259 // undefined), so check if frecency matches. This is okay because we
michael@0 260 // can still ensure the correct number of expected frecencies.
michael@0 261 let getFrecency = function(aURL) aURL.match(/frecency:(-?\d+)$/)[1];
michael@0 262 print("### checking for same frecency between '" + searchURL +
michael@0 263 "' and '" + expectURL + "'");
michael@0 264 do_check_eq(getFrecency(searchURL), getFrecency(expectURL));
michael@0 265 }
michael@0 266 }
michael@0 267 deferred.resolve();
michael@0 268 };
michael@0 269
michael@0 270 controller.startSearch(searchTerm);
michael@0 271
michael@0 272 yield deferred.promise;
michael@0 273 });

mercurial