toolkit/components/places/tests/unit/test_frecency.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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 * Test for bug 406358 to make sure frecency works for empty input/search, but
michael@0 9 * this also tests for non-empty inputs as well. Because the interactions among
michael@0 10 * *DIFFERENT* visit counts and visit dates is not well defined, this test
michael@0 11 * holds one of the two values constant when modifying the other.
michael@0 12 *
michael@0 13 * Also test bug 419068 to make sure tagged pages don't necessarily have to be
michael@0 14 * first in the results.
michael@0 15 *
michael@0 16 * Also test bug 426166 to make sure that the results of autocomplete searches
michael@0 17 * are stable. Note that failures of this test will be intermittent by nature
michael@0 18 * since we are testing to make sure that the unstable sort algorithm used
michael@0 19 * by SQLite is not changing the order of the results on us.
michael@0 20 */
michael@0 21
michael@0 22 function AutoCompleteInput(aSearches) {
michael@0 23 this.searches = aSearches;
michael@0 24 }
michael@0 25 AutoCompleteInput.prototype = {
michael@0 26 constructor: AutoCompleteInput,
michael@0 27
michael@0 28 searches: null,
michael@0 29
michael@0 30 minResultsForPopup: 0,
michael@0 31 timeout: 10,
michael@0 32 searchParam: "",
michael@0 33 textValue: "",
michael@0 34 disableAutoComplete: false,
michael@0 35 completeDefaultIndex: false,
michael@0 36
michael@0 37 get searchCount() {
michael@0 38 return this.searches.length;
michael@0 39 },
michael@0 40
michael@0 41 getSearchAt: function(aIndex) {
michael@0 42 return this.searches[aIndex];
michael@0 43 },
michael@0 44
michael@0 45 onSearchBegin: function() {},
michael@0 46 onSearchComplete: function() {},
michael@0 47
michael@0 48 popupOpen: false,
michael@0 49
michael@0 50 popup: {
michael@0 51 setSelectedIndex: function(aIndex) {},
michael@0 52 invalidate: function() {},
michael@0 53
michael@0 54 // nsISupports implementation
michael@0 55 QueryInterface: function(iid) {
michael@0 56 if (iid.equals(Ci.nsISupports) ||
michael@0 57 iid.equals(Ci.nsIAutoCompletePopup))
michael@0 58 return this;
michael@0 59
michael@0 60 throw Components.results.NS_ERROR_NO_INTERFACE;
michael@0 61 }
michael@0 62 },
michael@0 63
michael@0 64 // nsISupports implementation
michael@0 65 QueryInterface: function(iid) {
michael@0 66 if (iid.equals(Ci.nsISupports) ||
michael@0 67 iid.equals(Ci.nsIAutoCompleteInput))
michael@0 68 return this;
michael@0 69
michael@0 70 throw Components.results.NS_ERROR_NO_INTERFACE;
michael@0 71 }
michael@0 72 }
michael@0 73
michael@0 74 function ensure_results(uris, searchTerm)
michael@0 75 {
michael@0 76 promiseAsyncUpdates().then(function () ensure_results_internal(uris,
michael@0 77 searchTerm));
michael@0 78 }
michael@0 79
michael@0 80 function ensure_results_internal(uris, searchTerm)
michael@0 81 {
michael@0 82 var controller = Components.classes["@mozilla.org/autocomplete/controller;1"].
michael@0 83 getService(Components.interfaces.nsIAutoCompleteController);
michael@0 84
michael@0 85 // Make an AutoCompleteInput that uses our searches
michael@0 86 // and confirms results on search complete
michael@0 87 var input = new AutoCompleteInput(["history"]);
michael@0 88
michael@0 89 controller.input = input;
michael@0 90
michael@0 91 var numSearchesStarted = 0;
michael@0 92 input.onSearchBegin = function() {
michael@0 93 numSearchesStarted++;
michael@0 94 do_check_eq(numSearchesStarted, 1);
michael@0 95 };
michael@0 96
michael@0 97 input.onSearchComplete = function() {
michael@0 98 do_check_eq(numSearchesStarted, 1);
michael@0 99 do_check_eq(controller.searchStatus,
michael@0 100 Ci.nsIAutoCompleteController.STATUS_COMPLETE_MATCH);
michael@0 101 do_check_eq(controller.matchCount, uris.length);
michael@0 102 for (var i=0; i<controller.matchCount; i++) {
michael@0 103 do_check_eq(controller.getValueAt(i), uris[i].spec);
michael@0 104 }
michael@0 105
michael@0 106 deferEnsureResults.resolve();
michael@0 107 };
michael@0 108
michael@0 109 controller.startSearch(searchTerm);
michael@0 110 }
michael@0 111
michael@0 112 // Get history service
michael@0 113 try {
michael@0 114 var histsvc = Cc["@mozilla.org/browser/nav-history-service;1"].
michael@0 115 getService(Ci.nsINavHistoryService);
michael@0 116 var bhist = histsvc.QueryInterface(Ci.nsIBrowserHistory);
michael@0 117 var tagssvc = Cc["@mozilla.org/browser/tagging-service;1"].
michael@0 118 getService(Ci.nsITaggingService);
michael@0 119 var bmksvc = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
michael@0 120 getService(Ci.nsINavBookmarksService);
michael@0 121 } catch(ex) {
michael@0 122 do_throw("Could not get history service\n");
michael@0 123 }
michael@0 124
michael@0 125 function task_setCountDate(aURI, aCount, aDate)
michael@0 126 {
michael@0 127 // We need visits so that frecency can be computed over multiple visits
michael@0 128 let visits = [];
michael@0 129 for (let i = 0; i < aCount; i++) {
michael@0 130 visits.push({ uri: aURI, visitDate: aDate, transition: TRANSITION_TYPED });
michael@0 131 }
michael@0 132 yield promiseAddVisits(visits);
michael@0 133 }
michael@0 134
michael@0 135 function setBookmark(aURI)
michael@0 136 {
michael@0 137 bmksvc.insertBookmark(bmksvc.bookmarksMenuFolder, aURI, -1, "bleh");
michael@0 138 }
michael@0 139
michael@0 140 function tagURI(aURI, aTags) {
michael@0 141 bmksvc.insertBookmark(bmksvc.unfiledBookmarksFolder, aURI,
michael@0 142 bmksvc.DEFAULT_INDEX, "bleh");
michael@0 143 tagssvc.tagURI(aURI, aTags);
michael@0 144 }
michael@0 145
michael@0 146 var uri1 = uri("http://site.tld/1");
michael@0 147 var uri2 = uri("http://site.tld/2");
michael@0 148 var uri3 = uri("http://aaaaaaaaaa/1");
michael@0 149 var uri4 = uri("http://aaaaaaaaaa/2");
michael@0 150
michael@0 151 // d1 is younger (should show up higher) than d2 (PRTime is in usecs not msec)
michael@0 152 // Make sure the dates fall into different frecency buckets
michael@0 153 var d1 = new Date(Date.now() - 1000 * 60 * 60) * 1000;
michael@0 154 var d2 = new Date(Date.now() - 1000 * 60 * 60 * 24 * 10) * 1000;
michael@0 155 // c1 is larger (should show up higher) than c2
michael@0 156 var c1 = 10;
michael@0 157 var c2 = 1;
michael@0 158
michael@0 159 var tests = [
michael@0 160 // test things without a search term
michael@0 161 function() {
michael@0 162 print("TEST-INFO | Test 0: same count, different date");
michael@0 163 yield task_setCountDate(uri1, c1, d1);
michael@0 164 yield task_setCountDate(uri2, c1, d2);
michael@0 165 tagURI(uri1, ["site"]);
michael@0 166 ensure_results([uri1, uri2], "");
michael@0 167 },
michael@0 168 function() {
michael@0 169 print("TEST-INFO | Test 1: same count, different date");
michael@0 170 yield task_setCountDate(uri1, c1, d2);
michael@0 171 yield task_setCountDate(uri2, c1, d1);
michael@0 172 tagURI(uri1, ["site"]);
michael@0 173 ensure_results([uri2, uri1], "");
michael@0 174 },
michael@0 175 function() {
michael@0 176 print("TEST-INFO | Test 2: different count, same date");
michael@0 177 yield task_setCountDate(uri1, c1, d1);
michael@0 178 yield task_setCountDate(uri2, c2, d1);
michael@0 179 tagURI(uri1, ["site"]);
michael@0 180 ensure_results([uri1, uri2], "");
michael@0 181 },
michael@0 182 function() {
michael@0 183 print("TEST-INFO | Test 3: different count, same date");
michael@0 184 yield task_setCountDate(uri1, c2, d1);
michael@0 185 yield task_setCountDate(uri2, c1, d1);
michael@0 186 tagURI(uri1, ["site"]);
michael@0 187 ensure_results([uri2, uri1], "");
michael@0 188 },
michael@0 189
michael@0 190 // test things with a search term
michael@0 191 function() {
michael@0 192 print("TEST-INFO | Test 4: same count, different date");
michael@0 193 yield task_setCountDate(uri1, c1, d1);
michael@0 194 yield task_setCountDate(uri2, c1, d2);
michael@0 195 tagURI(uri1, ["site"]);
michael@0 196 ensure_results([uri1, uri2], "site");
michael@0 197 },
michael@0 198 function() {
michael@0 199 print("TEST-INFO | Test 5: same count, different date");
michael@0 200 yield task_setCountDate(uri1, c1, d2);
michael@0 201 yield task_setCountDate(uri2, c1, d1);
michael@0 202 tagURI(uri1, ["site"]);
michael@0 203 ensure_results([uri2, uri1], "site");
michael@0 204 },
michael@0 205 function() {
michael@0 206 print("TEST-INFO | Test 6: different count, same date");
michael@0 207 yield task_setCountDate(uri1, c1, d1);
michael@0 208 yield task_setCountDate(uri2, c2, d1);
michael@0 209 tagURI(uri1, ["site"]);
michael@0 210 ensure_results([uri1, uri2], "site");
michael@0 211 },
michael@0 212 function() {
michael@0 213 print("TEST-INFO | Test 7: different count, same date");
michael@0 214 yield task_setCountDate(uri1, c2, d1);
michael@0 215 yield task_setCountDate(uri2, c1, d1);
michael@0 216 tagURI(uri1, ["site"]);
michael@0 217 ensure_results([uri2, uri1], "site");
michael@0 218 },
michael@0 219 // There are multiple tests for 8, hence the multiple functions
michael@0 220 // Bug 426166 section
michael@0 221 function() {
michael@0 222 print("TEST-INFO | Test 8.1a: same count, same date");
michael@0 223 setBookmark(uri3);
michael@0 224 setBookmark(uri4);
michael@0 225 ensure_results([uri4, uri3], "a");
michael@0 226 },
michael@0 227 function() {
michael@0 228 print("TEST-INFO | Test 8.1b: same count, same date");
michael@0 229 setBookmark(uri3);
michael@0 230 setBookmark(uri4);
michael@0 231 ensure_results([uri4, uri3], "aa");
michael@0 232 },
michael@0 233 function() {
michael@0 234 print("TEST-INFO | Test 8.2: same count, same date");
michael@0 235 setBookmark(uri3);
michael@0 236 setBookmark(uri4);
michael@0 237 ensure_results([uri4, uri3], "aaa");
michael@0 238 },
michael@0 239 function() {
michael@0 240 print("TEST-INFO | Test 8.3: same count, same date");
michael@0 241 setBookmark(uri3);
michael@0 242 setBookmark(uri4);
michael@0 243 ensure_results([uri4, uri3], "aaaa");
michael@0 244 },
michael@0 245 function() {
michael@0 246 print("TEST-INFO | Test 8.4: same count, same date");
michael@0 247 setBookmark(uri3);
michael@0 248 setBookmark(uri4);
michael@0 249 ensure_results([uri4, uri3], "aaa");
michael@0 250 },
michael@0 251 function() {
michael@0 252 print("TEST-INFO | Test 8.5: same count, same date");
michael@0 253 setBookmark(uri3);
michael@0 254 setBookmark(uri4);
michael@0 255 ensure_results([uri4, uri3], "aa");
michael@0 256 },
michael@0 257 function() {
michael@0 258 print("TEST-INFO | Test 8.6: same count, same date");
michael@0 259 setBookmark(uri3);
michael@0 260 setBookmark(uri4);
michael@0 261 ensure_results([uri4, uri3], "a");
michael@0 262 }
michael@0 263 ];
michael@0 264
michael@0 265 /**
michael@0 266 * This deferred object contains a promise that is resolved when the
michael@0 267 * ensure_results_internal function has finished its execution.
michael@0 268 */
michael@0 269 let deferEnsureResults;
michael@0 270
michael@0 271 /**
michael@0 272 * Test adaptive autocomplete
michael@0 273 */
michael@0 274 function run_test()
michael@0 275 {
michael@0 276 run_next_test();
michael@0 277 }
michael@0 278
michael@0 279 add_task(function test_frecency()
michael@0 280 {
michael@0 281 // always search in history + bookmarks, no matter what the default is
michael@0 282 var prefs = Cc["@mozilla.org/preferences-service;1"].
michael@0 283 getService(Ci.nsIPrefBranch);
michael@0 284 prefs.setIntPref("browser.urlbar.search.sources", 3);
michael@0 285 prefs.setIntPref("browser.urlbar.default.behavior", 0);
michael@0 286 for (let [, test] in Iterator(tests)) {
michael@0 287 remove_all_bookmarks();
michael@0 288 yield promiseClearHistory();
michael@0 289
michael@0 290 deferEnsureResults = Promise.defer();
michael@0 291 yield test();
michael@0 292 yield deferEnsureResults.promise;
michael@0 293 }
michael@0 294 });

mercurial